Skip to content

Commit f8952e4

Browse files
committed
fix: don't be too generous when extrapolating worktree directories.
Previously it was possible that a non-bare repository that didn't have worktree directory incorrectly claimed it had one.
1 parent 208cf5a commit f8952e4

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

gix/src/open/repository.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::result_large_err)]
2-
use std::{borrow::Cow, path::PathBuf};
3-
42
use gix_features::threading::OwnShared;
3+
use std::ffi::OsStr;
4+
use std::{borrow::Cow, path::PathBuf};
55

66
use super::{Error, Options};
77
use crate::{
@@ -86,7 +86,7 @@ impl ThreadSafeRepository {
8686
}
8787
};
8888

89-
// The be altered later based on `core.precomposeUnicode`.
89+
// To be altered later based on `core.precomposeUnicode`.
9090
let cwd = gix_fs::current_dir(false)?;
9191
let (git_dir, worktree_dir) = gix_discover::repository::Path::from_dot_git_dir(path, kind, &cwd)
9292
.expect("we have sanitized path with is_git()")
@@ -284,7 +284,7 @@ impl ThreadSafeRepository {
284284
}
285285

286286
match worktree_dir {
287-
None if !config.is_bare => {
287+
None if !config.is_bare && refs.git_dir().extension() == Some(OsStr::new(gix_discover::DOT_GIT_DIR)) => {
288288
worktree_dir = Some(git_dir.parent().expect("parent is always available").to_owned());
289289
}
290290
Some(_) => {
Binary file not shown.

gix/tests/fixtures/make_basic_repo.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ git init all-untracked
3636
git init empty-core-excludes
3737
(cd empty-core-excludes
3838
echo $'[core]\n\texcludesFile = ' >> .git/config
39+
)
40+
41+
git clone --bare . non-bare-without-worktree
42+
(cd non-bare-without-worktree
43+
git config core.bare false
3944
)

gix/tests/gix/repository/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ mod dirwalk {
4242
("bare.git".into(), Directory),
4343
("empty-core-excludes".into(), Repository),
4444
("non-bare-repo-without-index".into(), Repository),
45+
("non-bare-without-worktree".into(), Directory),
4546
("some".into(), Directory),
4647
];
4748
assert_eq!(

gix/tests/gix/repository/open.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,30 @@ fn bare_repo_with_index() -> crate::Result {
6262
repo.is_bare(),
6363
"it's properly classified as it reads the configuration (and has no worktree)"
6464
);
65+
assert_eq!(repo.work_dir(), None);
66+
Ok(())
67+
}
68+
69+
#[test]
70+
fn non_bare_non_git_repo_without_worktree() -> crate::Result {
71+
let repo = named_subrepo_opts(
72+
"make_basic_repo.sh",
73+
"non-bare-without-worktree",
74+
gix::open::Options::isolated(),
75+
)?;
76+
assert!(!repo.is_bare());
77+
assert_eq!(repo.work_dir(), None, "it doesn't assume that workdir exists");
78+
79+
let repo = gix::open_opts(
80+
repo.git_dir().join("objects").join(".."),
81+
gix::open::Options::isolated(),
82+
)?;
83+
assert!(!repo.is_bare());
84+
assert_eq!(
85+
repo.work_dir(),
86+
None,
87+
"it figures this out even if a non-normalized gitdir is used"
88+
);
6589
Ok(())
6690
}
6791

0 commit comments

Comments
 (0)