Skip to content

Commit

Permalink
feat: git_discover::is_git() can detect submodule dirs correctly en…
Browse files Browse the repository at this point in the history
…ough. (#482)

We currently detect them as possibly bare, which could be improved if we
allow ourselves to see `.git/modules` as `submodule` always.
  • Loading branch information
Byron committed Aug 17, 2022
1 parent 9133141 commit aa6fd97
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
17 changes: 16 additions & 1 deletion git-discover/src/is.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,22 @@ pub fn git(git_dir: impl AsRef<Path>) -> Result<crate::repository::Kind, crate::
if bare(git_dir) {
crate::repository::Kind::Bare
} else {
crate::repository::Kind::WorkTree { linked_git_dir: None }
let mut last_comp = None;
if git_dir.file_name() != Some(OsStr::new(DOT_GIT_DIR))
&& git_dir.components().rev().any(|c| {
if c.as_os_str() == OsStr::new(DOT_GIT_DIR) {
true
} else {
last_comp = Some(c.as_os_str());
false
}
})
&& last_comp == Some(OsStr::new("modules"))
{
crate::repository::Kind::Bare
} else {
crate::repository::Kind::WorkTree { linked_git_dir: None }
}
}
}
})
Expand Down
1 change: 1 addition & 0 deletions git-discover/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub enum Kind {
/// A bare repository does not have a work tree, that is files on disk beyond the `git` repository itself.
///
/// Note that this is merely a guess at this point as we didn't read the configuration yet.
/// It might also be the clone of a submodule in `.git/modules` which has its worktree configured with `core.worktree`.
Bare,
/// A `git` repository along with a checked out files in a work tree.
WorkTree {
Expand Down
1 change: 0 additions & 1 deletion git-discover/tests/upwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ mod submodules {
}

#[test]
#[ignore]
fn by_their_module_git_dir() -> crate::Result {
let dir = git_testtools::scripted_fixture_repo_read_only("make_submodules.sh")?;
let modules = dir.join("with-submodules").join(".git").join("modules");
Expand Down

0 comments on commit aa6fd97

Please sign in to comment.