diff --git a/README.md b/README.md index 3caa91a..0fd75d1 100644 --- a/README.md +++ b/README.md @@ -117,9 +117,9 @@ Future work will include adding a command line tool for reading metadata from a The current structure of the metadata is fixed. Future work will aim to make it possible to configure the structure of the metadata through a configuration file. -## Windows & Mac targets +## Windows targets -**TODO!** This crate has not yet been tested for a Windows or Mac target. +**TODO!** This crate has not yet been tested for a Windows target. ## Running your Linter Locally diff --git a/src/lib.rs b/src/lib.rs index bcecce4..994f840 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,7 +52,8 @@ pub fn include_metadata(_: TokenStream) -> TokenStream { footer: [u8; 4], } - #[link_section = ".metadata"] + #[cfg_attr(target_os = "macos", link_section = "__DATA,__metadata")] + #[cfg_attr(not(target_os = "macos"), link_section = ".metadata")] #[used] static METADATA: Metadata = Metadata { @@ -133,7 +134,7 @@ fn get_repo() -> Result { format!("failed to generate Regex: {error}"), ) })?; - let primary_package_dir = pattern + pattern .captures(cargo_metadata) .ok_or(Error::new( Span::call_site(), @@ -145,15 +146,45 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - Repository::open(primary_package_dir).map_err(|error| { + + let git_root = find_valid_git_root_inner().map_err(|error| { + Error::new( + Span::call_site(), + format!("failed to find Git Repo: {error}"), + ) + })?; + + Repository::open(&git_root).map_err(|error| { Error::new( Span::call_site(), - format!("failed to open repository at location {primary_package_dir}: {error}"), + format!("failed to open repository at {git_root}: {error}"), ) }) } -fn get_last_commit(repo: &Repository) -> Result { +#[proc_macro] +pub fn find_valid_git_root(_: TokenStream) -> TokenStream { + match find_valid_git_root_inner() { + Ok(path) => quote! {#path}.into(), + Err(_) => panic!("Cant find .git folder!"), + } +} + +fn find_valid_git_root_inner() -> Result { + let mut path = std::env::current_dir().unwrap(); + loop { + // If this is a valid repository return it otherwise keep going + if path.join(".git").exists() { + let path = path.to_str().unwrap(); + return Ok(path.to_string()); + } + if !path.pop() { + return Err("Cant find .git folder!"); + } + } +} + +fn get_last_commit(repo: &Repository) -> Result, Error> { let hash = repo .head() .map_err(|error| Error::new(Span::call_site(), format!("failed to get HEAD: {error}")))? diff --git a/template/init.rhai b/template/init.rhai index 9e2d8cd..6df4745 100644 --- a/template/init.rhai +++ b/template/init.rhai @@ -5,7 +5,8 @@ if variable::get("is_init") { print(); print("*** Add the following statements to your build.rs:"); print(); - print("println!(\"cargo:rerun-if-changed=./.git/\");"); + print(println!("cargo:rerun-if-changed={}", commitment_issues::find_valid_git_root!());); + print(#[cfg(not(target_os = "macos"))]); print("println!(\"cargo:rustc-link-arg=-Tmetadata.x\");"); print(); }