-
Notifications
You must be signed in to change notification settings - Fork 19
fix: retrieve root_repository_path from git dir #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| // during normal execution, we want to find the repository root by looking for a .git directory | ||
| // during tests, we want `find_repository_root` to always return `None`, so that we don't have to | ||
| // create a git repository for each test | ||
|
|
||
| #[cfg(not(test))] | ||
| pub fn find_repository_root(base_dir: &Path) -> Option<PathBuf> { | ||
| _find_repository_root(base_dir) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| pub fn find_repository_root(_base_dir: &Path) -> Option<PathBuf> { | ||
| None | ||
| } | ||
|
|
||
| // the core logic is extracted into a separate function so that it can be tested | ||
| fn _find_repository_root(base_dir: &Path) -> Option<PathBuf> { | ||
| let current_dir = base_dir.canonicalize().ok()?; | ||
|
|
||
| for ancestor in current_dir.ancestors() { | ||
| let git_dir = ancestor.join(".git"); | ||
| if git_dir.exists() { | ||
| return Some(ancestor.to_path_buf()); | ||
| } | ||
| } | ||
|
|
||
| log::warn!("Could not find repository root"); | ||
|
|
||
| None | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_find_repository_root() { | ||
| // create an empty directory in a tmp directory, add a nested .git directory | ||
| // and check if the repository root is found when calling _find_repository_root from a nested directory | ||
| let tmp_dir = tempfile::tempdir().unwrap(); | ||
| let base_dir = tmp_dir.path().join("base-dir"); | ||
| let git_dir = base_dir.join(".git"); | ||
| std::fs::create_dir_all(git_dir).unwrap(); | ||
| let nested_current_dir = base_dir.join("nested").join("deeply"); | ||
| std::fs::create_dir_all(&nested_current_dir).unwrap(); | ||
|
|
||
| let repository_root = _find_repository_root(&nested_current_dir).unwrap(); | ||
| assert_eq!(repository_root, base_dir.canonicalize().unwrap()); | ||
|
|
||
| tmp_dir.close().unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_find_repository_root_no_git_dir() { | ||
| // create an empty directory in a tmp directory and check if the repository root is not found | ||
| let tmp_dir = tempfile::tempdir().unwrap(); | ||
| let base_dir = tmp_dir.path().join("base-dir"); | ||
| std::fs::create_dir_all(&base_dir).unwrap(); | ||
|
|
||
| let repository_root = _find_repository_root(&base_dir); | ||
| assert_eq!(repository_root, None); | ||
|
|
||
| tmp_dir.close().unwrap(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| mod find_repository_root; | ||
| mod get_env_var; | ||
|
|
||
| pub use find_repository_root::find_repository_root; | ||
| pub use get_env_var::get_env_variable; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.