Skip to content

Commit 7d03275

Browse files
committed
Fix discover::upwards when working dir is the repo
The upwards search for the repository directory takes a directory as input and then walks through the parents. It turns out that it was broken when the repository was the same as the working directory. The code checked when the directory components had been stripped to "", in case the directory was replaced with `cwd.parent()`, so the loop missed to check `cwd` itself. If the input directory was "./something", then "." was checked which then succeeded.
1 parent f7c7145 commit 7d03275

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

gix-discover/src/upwards/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,12 @@ pub(crate) mod function {
168168
}
169169
}
170170
}
171-
if cursor.parent().is_some_and(|p| p.as_os_str().is_empty()) {
172-
cursor = cwd.to_path_buf();
173-
dir_made_absolute = true;
174-
}
175-
if !cursor.pop() {
171+
if cursor.pop() {
172+
if cursor.as_os_str().is_empty() {
173+
cursor = cwd.to_path_buf();
174+
dir_made_absolute = true;
175+
}
176+
} else {
176177
if dir_made_absolute
177178
|| matches!(
178179
cursor.components().next(),

gix-discover/tests/isolated.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@ use std::path::{Path, PathBuf};
33
use gix_discover::upwards::Options;
44
use serial_test::serial;
55

6+
#[test]
7+
#[serial]
8+
fn in_cwd_upwards_from_nested_dir() -> gix_testtools::Result {
9+
let repo = gix_testtools::scripted_fixture_read_only("make_basic_repo.sh")?;
10+
11+
let _keep = gix_testtools::set_current_dir(repo)?;
12+
for dir in ["subdir", "some/very/deeply/nested/subdir"] {
13+
let (repo_path, _trust) = gix_discover::upwards(Path::new(dir))?;
14+
assert_eq!(
15+
repo_path.kind(),
16+
gix_discover::repository::Kind::WorkTree { linked_git_dir: None },
17+
);
18+
assert_eq!(repo_path.as_ref(), Path::new("."));
19+
}
20+
Ok(())
21+
}
22+
623
#[test]
724
#[serial]
825
fn upwards_bare_repo_with_index() -> gix_testtools::Result {

0 commit comments

Comments
 (0)