From e7d6015a10fa7c40849d14e7d085aa39ca80c9d5 Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Jul 2025 16:20:32 +0100 Subject: [PATCH 01/21] Attempt at adding parent directory --- src/lib.rs | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bcecce4..b26b7ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,12 +145,33 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - Repository::open(primary_package_dir).map_err(|error| { - Error::new( - Span::call_site(), - format!("failed to open repository at location {primary_package_dir}: {error}"), - ) - }) + + Repository::open(primary_package_dir) + .or_else(|first_error| { + // Try parent directory if opening the initial directory fails + let parent = Path::new(primary_package_dir).parent(); + if let Some(parent_dir) = parent { + Repository::open(parent_dir).map_err(|second_error| { + Error::new( + Span::call_site(), + format!( + "failed to open repository at location {primary_package_dir}: {first_error}, \ + also failed at parent directory {:?}: {second_error}", + parent_dir, + ), + ) + }) + } else { + // No parent directory exists + Err(Error::new( + Span::call_site(), + format!( + "failed to open repository at location {primary_package_dir}: {first_error}, \ + and no parent directory available" + ), + )) + } + })?; } fn get_last_commit(repo: &Repository) -> Result { From fa067142ee234bc5fee89bda6d861263d2de7556 Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Jul 2025 16:26:42 +0100 Subject: [PATCH 02/21] no-std implementation of parent path --- src/lib.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b26b7ad..fd9c231 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,7 +149,7 @@ fn get_repo() -> Result { Repository::open(primary_package_dir) .or_else(|first_error| { // Try parent directory if opening the initial directory fails - let parent = Path::new(primary_package_dir).parent(); + let parent = parent_path(primary_package_dir).unwrap(); if let Some(parent_dir) = parent { Repository::open(parent_dir).map_err(|second_error| { Error::new( @@ -174,6 +174,24 @@ fn get_repo() -> Result { })?; } +fn parent_path(path: &str) -> Option<&str> { + // Find the last separator in either style + let pos_slash = path.rfind('/'); + let pos_backslash = path.rfind('\\'); + let last = match (pos_slash, pos_backslash) { + (Some(s), Some(b)) => core::cmp::max(s, b), + (Some(s), None) => s, + (None, Some(b)) => b, + (None, None) => return None, + }; + if last == 0 { + // Only root component, e.g. "/foo" or "\foo" + Some(&path[..1]) + } else { + Some(&path[..last]) + } +} + fn get_last_commit(repo: &Repository) -> Result { let hash = repo .head() From fbc48523f779f1c8bd045f5f8a38c192c11b7c35 Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Jul 2025 16:30:16 +0100 Subject: [PATCH 03/21] WIP --- src/lib.rs | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fd9c231..4e80608 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,31 +147,7 @@ fn get_repo() -> Result { .as_str(); Repository::open(primary_package_dir) - .or_else(|first_error| { - // Try parent directory if opening the initial directory fails - let parent = parent_path(primary_package_dir).unwrap(); - if let Some(parent_dir) = parent { - Repository::open(parent_dir).map_err(|second_error| { - Error::new( - Span::call_site(), - format!( - "failed to open repository at location {primary_package_dir}: {first_error}, \ - also failed at parent directory {:?}: {second_error}", - parent_dir, - ), - ) - }) - } else { - // No parent directory exists - Err(Error::new( - Span::call_site(), - format!( - "failed to open repository at location {primary_package_dir}: {first_error}, \ - and no parent directory available" - ), - )) - } - })?; + .or_else({ Repository::open(parent_path(primary_package_dir)) }) } fn parent_path(path: &str) -> Option<&str> { From 412f3ad4a9efe890aea5acbeeec8ff36405c8093 Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Jul 2025 16:40:03 +0100 Subject: [PATCH 04/21] WIP --- src/lib.rs | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4e80608..e2bbaed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,26 +146,43 @@ fn get_repo() -> Result { ))? .as_str(); - Repository::open(primary_package_dir) - .or_else({ Repository::open(parent_path(primary_package_dir)) }) + Repository::open(primary_package_dir).or_else(|first_error| { + match parent_path(primary_package_dir) { + Ok(parent) => Repository::open(parent).map_err(|second_error| { + Error::new( + Span::call_site(), + format!( + "failed to open repository at location {primary_package_dir}: {first_error}; \ + also failed at parent path {parent}: {second_error}" + ), + ) + }), + Err(parent_path_error) => Err(Error::new( + Span::call_site(), + format!( + "failed to open repository at location {primary_package_dir}: {first_error}; \ + also failed to determine parent path: {parent_path_error}" + ), + )), + } + }) } -fn parent_path(path: &str) -> Option<&str> { - // Find the last separator in either style +fn parent_path(path: &str) -> Result<&str, Error> { let pos_slash = path.rfind('/'); let pos_backslash = path.rfind('\\'); let last = match (pos_slash, pos_backslash) { (Some(s), Some(b)) => core::cmp::max(s, b), (Some(s), None) => s, (None, Some(b)) => b, - (None, None) => return None, + (None, None) => { + return Err(Error::new( + Span::call_site(), + "failed to find parent path".to_string(), + )) + } }; - if last == 0 { - // Only root component, e.g. "/foo" or "\foo" - Some(&path[..1]) - } else { - Some(&path[..last]) - } + Ok(&path[..last]) } fn get_last_commit(repo: &Repository) -> Result { From 0e5f21aac3b69eb3331d1b1238b87f4f806bad7b Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Jul 2025 16:43:05 +0100 Subject: [PATCH 05/21] Adding MacOS specific --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e2bbaed..2eb0acf 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 { From 176be74f5f3cb76093550f44cc78f6826d0efc9b Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Jul 2025 16:46:26 +0100 Subject: [PATCH 06/21] Attempting at fixing macos issue --- .cargo/config.toml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..8c75849 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,5 @@ +[target.aarch64-apple-darwin] +# Do NOT set rustflags for -T on this target + +[target.x86_64-unknown-linux-gnu] +rustflags = ["-Tmetadata.x"] \ No newline at end of file From 6b00d209040d0c1c25e2a9e6236313445d0630de Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 4 Jul 2025 16:47:40 +0100 Subject: [PATCH 07/21] undo last commit --- .cargo/config.toml | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 8c75849..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,5 +0,0 @@ -[target.aarch64-apple-darwin] -# Do NOT set rustflags for -T on this target - -[target.x86_64-unknown-linux-gnu] -rustflags = ["-Tmetadata.x"] \ No newline at end of file From 79d72d9c161c62f77d1ec191260bc33d9503792b Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 7 Jul 2025 17:26:16 +0100 Subject: [PATCH 08/21] Attempt at recursive path --- README.md | 4 +-- src/lib.rs | 71 ++++++++++++++++++++++++----------------------- template/build.rs | 1 + 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 3caa91a..56f4cae 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. ## Running your Linter Locally diff --git a/src/lib.rs b/src/lib.rs index 2eb0acf..9192fd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,44 +146,45 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - - Repository::open(primary_package_dir).or_else(|first_error| { - match parent_path(primary_package_dir) { - Ok(parent) => Repository::open(parent).map_err(|second_error| { - Error::new( - Span::call_site(), - format!( - "failed to open repository at location {primary_package_dir}: {first_error}; \ - also failed at parent path {parent}: {second_error}" - ), - ) - }), - Err(parent_path_error) => Err(Error::new( - Span::call_site(), - format!( - "failed to open repository at location {primary_package_dir}: {first_error}; \ - also failed to determine parent path: {parent_path_error}" - ), - )), - } - }) + let git_repo_path = parent_until_git(primary_package_dir)?; + Repository::open(primary_package_dir)?; } -fn parent_path(path: &str) -> Result<&str, Error> { - let pos_slash = path.rfind('/'); - let pos_backslash = path.rfind('\\'); - let last = match (pos_slash, pos_backslash) { - (Some(s), Some(b)) => core::cmp::max(s, b), - (Some(s), None) => s, - (None, Some(b)) => b, - (None, None) => { - return Err(Error::new( - Span::call_site(), - "failed to find parent path".to_string(), - )) +fn parent_until_git(mut path: &str) -> Result<&str, Error> { + loop { + // Find last path separator position + let pos_slash = path.rfind('/'); + let pos_backslash = path.rfind('\\'); + let last = match (pos_slash, pos_backslash) { + (Some(s), Some(b)) => core::cmp::max(s, b), + (Some(s), None) => s, + (None, Some(b)) => b, + (None, None) => break, // Reached the top level + }; + + // Take the parent part + path = &path[..last]; + + // Now, find the last segment (after the previous path separator) + // (Edge case: root paths) + let seg_start = { + let prev_slash = path.rfind('/'); + let prev_backslash = path.rfind('\\'); + match (prev_slash, prev_backslash) { + (Some(s), Some(b)) => core::cmp::max(s, b) + 1, + (Some(s), None) => s + 1, + (None, Some(b)) => b + 1, + (None, None) => 0, + } + }; + + let last_segment = &path[seg_start..]; + + if last_segment == ".git" { + return Ok(path); } - }; - Ok(&path[..last]) + } + Err("no parent ending with .git found") } fn get_last_commit(repo: &Repository) -> Result { diff --git a/template/build.rs b/template/build.rs index 219dc07..8dbfec8 100644 --- a/template/build.rs +++ b/template/build.rs @@ -1,4 +1,5 @@ fn main() { println!("cargo:rerun-if-changed=./.git/"); + #[cfg(not(target_os = "macos"))] println!("cargo:rustc-link-arg=-Tmetadata.x"); } From e1533b2b61fc73eb779b7929ad141d66872c1b8b Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 7 Jul 2025 17:38:59 +0100 Subject: [PATCH 09/21] refactored repository finding --- src/lib.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9192fd0..0c76d81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,12 +146,19 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - let git_repo_path = parent_until_git(primary_package_dir)?; - Repository::open(primary_package_dir)?; + let repository_path = parent_until_git(primary_package_dir)?; + Ok(Repository::open(repository_path).map_err(|err| { + Error::new( + Span::call_site(), + format!("failed to open repository at location {repository_path}: {err};"), + ) + })?) } fn parent_until_git(mut path: &str) -> Result<&str, Error> { + let mut depth = 0; loop { + depth += 1; // Find last path separator position let pos_slash = path.rfind('/'); let pos_backslash = path.rfind('\\'); @@ -184,7 +191,10 @@ fn parent_until_git(mut path: &str) -> Result<&str, Error> { return Ok(path); } } - Err("no parent ending with .git found") + Err(Error::new( + Span::call_site(), + format!("failed to find .git folder after {depth} parents"), + )) } fn get_last_commit(repo: &Repository) -> Result { From 3e968c4a24e8f9bc083ef1d9dcf96c60e4d6cb0e Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 7 Jul 2025 17:57:14 +0100 Subject: [PATCH 10/21] Refactoring --- src/lib.rs | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0c76d81..77f747a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,16 +146,10 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - let repository_path = parent_until_git(primary_package_dir)?; - Ok(Repository::open(repository_path).map_err(|err| { - Error::new( - Span::call_site(), - format!("failed to open repository at location {repository_path}: {err};"), - ) - })?) + Ok(parent_until_git(primary_package_dir)?) } -fn parent_until_git(mut path: &str) -> Result<&str, Error> { +fn parent_until_git(mut path: &str) -> Result { let mut depth = 0; loop { depth += 1; @@ -172,23 +166,9 @@ fn parent_until_git(mut path: &str) -> Result<&str, Error> { // Take the parent part path = &path[..last]; - // Now, find the last segment (after the previous path separator) - // (Edge case: root paths) - let seg_start = { - let prev_slash = path.rfind('/'); - let prev_backslash = path.rfind('\\'); - match (prev_slash, prev_backslash) { - (Some(s), Some(b)) => core::cmp::max(s, b) + 1, - (Some(s), None) => s + 1, - (None, Some(b)) => b + 1, - (None, None) => 0, - } - }; - - let last_segment = &path[seg_start..]; - - if last_segment == ".git" { - return Ok(path); + // If this is a valid repository return it otherwise keep going + if Repository::open(path).is_ok() { + return Ok(Repository::open(path).expect("This should never fail")); } } Err(Error::new( From 2b6edc06873378c9e2966d7193d387f30335fb5f Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 7 Jul 2025 17:59:12 +0100 Subject: [PATCH 11/21] refactoring method name --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 77f747a..fa87446 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,10 +146,10 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - Ok(parent_until_git(primary_package_dir)?) + Ok(find_valid_git_root(primary_package_dir)?) } -fn parent_until_git(mut path: &str) -> Result { +fn find_valid_git_root(mut path: &str) -> Result { let mut depth = 0; loop { depth += 1; From 63d4964beb80b47fa74dc7b16ba46094af41b52e Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 7 Jul 2025 18:04:51 +0100 Subject: [PATCH 12/21] Attempting to change PROJECT GIT DIR --- src/lib.rs | 5 +++-- template/build.rs | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fa87446..d4a4c33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -167,8 +167,9 @@ fn find_valid_git_root(mut path: &str) -> Result { path = &path[..last]; // If this is a valid repository return it otherwise keep going - if Repository::open(path).is_ok() { - return Ok(Repository::open(path).expect("This should never fail")); + if let Ok(repo) = Repository::open(path) { + // Tell build.rs what the path is? + return Ok(repo); } } Err(Error::new( diff --git a/template/build.rs b/template/build.rs index 8dbfec8..66e4529 100644 --- a/template/build.rs +++ b/template/build.rs @@ -1,5 +1,8 @@ +use std::env; + fn main() { - println!("cargo:rerun-if-changed=./.git/"); + let git_dir = env::var("PROJECT_GIT_DIR").unwrap_or_else(|_| "./.git/".to_string()); + println!("cargo:rerun-if-changed={}", git_dir); #[cfg(not(target_os = "macos"))] println!("cargo:rustc-link-arg=-Tmetadata.x"); } From 3f6f432b52c139e9e3b6c33cf56508b48080abcf Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 7 Jul 2025 18:07:27 +0100 Subject: [PATCH 13/21] Fixing clippy --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index d4a4c33..b10c7c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -146,7 +146,7 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - Ok(find_valid_git_root(primary_package_dir)?) + find_valid_git_root(primary_package_dir) } fn find_valid_git_root(mut path: &str) -> Result { From 9a2ad11d671092632f942d500312a4612b01ab77 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 14 Jul 2025 14:23:08 +0100 Subject: [PATCH 14/21] Updated to use environment variables and std --- src/lib.rs | 37 +++++++------------------------------ template/build.rs | 19 ++++++++++++++++--- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b10c7c5..991f2c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,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(), @@ -146,36 +146,13 @@ fn get_repo() -> Result { "key \"workspace_root\" not found in cargo metadata".to_owned(), ))? .as_str(); - find_valid_git_root(primary_package_dir) -} - -fn find_valid_git_root(mut path: &str) -> Result { - let mut depth = 0; - loop { - depth += 1; - // Find last path separator position - let pos_slash = path.rfind('/'); - let pos_backslash = path.rfind('\\'); - let last = match (pos_slash, pos_backslash) { - (Some(s), Some(b)) => core::cmp::max(s, b), - (Some(s), None) => s, - (None, Some(b)) => b, - (None, None) => break, // Reached the top level - }; - - // Take the parent part - path = &path[..last]; - // If this is a valid repository return it otherwise keep going - if let Ok(repo) = Repository::open(path) { - // Tell build.rs what the path is? - return Ok(repo); - } - } - Err(Error::new( - Span::call_site(), - format!("failed to find .git folder after {depth} parents"), - )) + Repository::open_from_env().map_err(|error| { + Error::new( + Span::call_site(), + format!("failed to find Git Repo: {error}"), + ) + }) } fn get_last_commit(repo: &Repository) -> Result { diff --git a/template/build.rs b/template/build.rs index 66e4529..308280a 100644 --- a/template/build.rs +++ b/template/build.rs @@ -1,8 +1,21 @@ -use std::env; - fn main() { - let git_dir = env::var("PROJECT_GIT_DIR").unwrap_or_else(|_| "./.git/".to_string()); + let git_dir = env::get_var("GIT_DIR").unwrap_or_else( + find_valid_git_dir().map_err | err | { eprintln!(format!("Couldnt find Git Repo: {err}")) }, + ); println!("cargo:rerun-if-changed={}", git_dir); #[cfg(not(target_os = "macos"))] println!("cargo:rustc-link-arg=-Tmetadata.x"); } + +fn find_valid_git_root(mut path: &PathBuf) -> Result<(), &str> { + loop { + // If this is a valid repository return it otherwise keep going + if let Ok(repo) = Repository::open(path) { + std::env::set_var("GIT_DIR", path.as_os_str()); + return Ok(); + } + if !path.pop() { + Err("failed to find .git folder") + } + } +} From 27163626f1bb692125abd1376df201397e3c8531 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 14 Jul 2025 14:53:10 +0100 Subject: [PATCH 15/21] WIP --- src/lib.rs | 17 +++++++++++++++++ template/build.rs | 20 ++++---------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 991f2c0..c6adf1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -155,6 +155,23 @@ fn get_repo() -> Result { }) } +#[proc_macro] +fn find_valid_git_root(mut path: PathBuf) -> Result { + loop { + // If this is a valid repository return it otherwise keep going + if path.join(".git").exists() { + let path = path.to_str().unwrap(); + unsafe { + std::env::set_var("GIT_DIR", path); + } + return Ok(path.to_string()); + } + if !path.pop() { + return Err("failed to find .git folder"); + } + } +} + fn get_last_commit(repo: &Repository) -> Result { let hash = repo .head() diff --git a/template/build.rs b/template/build.rs index 308280a..a20abf9 100644 --- a/template/build.rs +++ b/template/build.rs @@ -1,21 +1,9 @@ +use std::path::PathBuf; + fn main() { - let git_dir = env::get_var("GIT_DIR").unwrap_or_else( - find_valid_git_dir().map_err | err | { eprintln!(format!("Couldnt find Git Repo: {err}")) }, - ); + let current_dir = std::env::current_dir().unwrap(); + let git_dir = find_valid_git_root!(current_dir).unwrap(); println!("cargo:rerun-if-changed={}", git_dir); #[cfg(not(target_os = "macos"))] println!("cargo:rustc-link-arg=-Tmetadata.x"); } - -fn find_valid_git_root(mut path: &PathBuf) -> Result<(), &str> { - loop { - // If this is a valid repository return it otherwise keep going - if let Ok(repo) = Repository::open(path) { - std::env::set_var("GIT_DIR", path.as_os_str()); - return Ok(); - } - if !path.pop() { - Err("failed to find .git folder") - } - } -} From 20c72360c3838f09f86f9ed17f1f76605c3aa9a8 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 14 Jul 2025 15:24:13 +0100 Subject: [PATCH 16/21] Added new implementation of git root functionality. --- src/lib.rs | 27 +++++++++++++++++++-------- template/build.rs | 7 +------ template/init.rhai | 3 ++- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c6adf1c..62f277a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,7 +147,14 @@ fn get_repo() -> Result { ))? .as_str(); - Repository::open_from_env().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 find Git Repo: {error}"), @@ -156,23 +163,27 @@ fn get_repo() -> Result { } #[proc_macro] -fn find_valid_git_root(mut path: PathBuf) -> Result { +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(); - unsafe { - std::env::set_var("GIT_DIR", path); - } return Ok(path.to_string()); } if !path.pop() { - return Err("failed to find .git folder"); + return Err("Cant find .git folder!"); } } } - -fn get_last_commit(repo: &Repository) -> Result { +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/build.rs b/template/build.rs index a20abf9..219dc07 100644 --- a/template/build.rs +++ b/template/build.rs @@ -1,9 +1,4 @@ -use std::path::PathBuf; - fn main() { - let current_dir = std::env::current_dir().unwrap(); - let git_dir = find_valid_git_root!(current_dir).unwrap(); - println!("cargo:rerun-if-changed={}", git_dir); - #[cfg(not(target_os = "macos"))] + println!("cargo:rerun-if-changed=./.git/"); println!("cargo:rustc-link-arg=-Tmetadata.x"); } 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(); } From 674ebd5266e0d15463bd65fd732b5ec6fb6831c8 Mon Sep 17 00:00:00 2001 From: Scott Gibb <44235263+ScottGibb@users.noreply.github.com> Date: Mon, 14 Jul 2025 20:31:35 +0100 Subject: [PATCH 17/21] Update src/lib.rs Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com> --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 62f277a..6a60f5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -183,6 +183,7 @@ fn find_valid_git_root_inner() -> Result { } } } + fn get_last_commit(repo: &Repository) -> Result, Error> { let hash = repo .head() From 57036f8fa25f5f4a96afa1277c1ba57437870321 Mon Sep 17 00:00:00 2001 From: Scott Gibb <44235263+ScottGibb@users.noreply.github.com> Date: Tue, 15 Jul 2025 10:20:00 +0100 Subject: [PATCH 18/21] Update README.md Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 56f4cae..0fd75d1 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ Future work will aim to make it possible to configure the structure of the metad ## Windows targets -**TODO!** This crate has not yet been tested for a Windows. +**TODO!** This crate has not yet been tested for a Windows target. ## Running your Linter Locally From 2e68e898e94dbf397abb40c2cdc75eb53ca1233e Mon Sep 17 00:00:00 2001 From: Scott Gibb <44235263+ScottGibb@users.noreply.github.com> Date: Tue, 15 Jul 2025 10:20:17 +0100 Subject: [PATCH 19/21] Update src/lib.rs Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com> --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6a60f5c..0e24b25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,7 +157,7 @@ fn get_repo() -> Result { Repository::open(git_root).map_err(|error| { Error::new( Span::call_site(), - format!("failed to find Git Repo: {error}"), + format!("failed to open repository at {git_root}: {error}"), ) }) } From 1d6fa59c0bee652d10b0efa8897510b51cb14929 Mon Sep 17 00:00:00 2001 From: Scott Date: Tue, 15 Jul 2025 10:29:20 +0100 Subject: [PATCH 20/21] Added borrow of git_root. Otherwise code would not compile --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 0e24b25..04cfb57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -154,7 +154,7 @@ fn get_repo() -> Result { ) })?; - Repository::open(git_root).map_err(|error| { + Repository::open(&git_root).map_err(|error| { Error::new( Span::call_site(), format!("failed to open repository at {git_root}: {error}"), From ba452662d6b11bd4e385de1e5046594e056e3935 Mon Sep 17 00:00:00 2001 From: Scott Gibb <44235263+ScottGibb@users.noreply.github.com> Date: Wed, 16 Jul 2025 12:00:54 +0100 Subject: [PATCH 21/21] Update src/lib.rs Co-authored-by: petekubiak <37507920+petekubiak@users.noreply.github.com> --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 04cfb57..994f840 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,7 +52,7 @@ pub fn include_metadata(_: TokenStream) -> TokenStream { footer: [u8; 4], } - #[cfg_attr(target_os = "macos", link_section = "__DATA,.metadata")] + #[cfg_attr(target_os = "macos", link_section = "__DATA,__metadata")] #[cfg_attr(not(target_os = "macos"), link_section = ".metadata")] #[used] static METADATA: Metadata =