Skip to content

Commit

Permalink
assure submodule status doesn't operate if there is no worktree checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Mar 14, 2024
1 parent da45d92 commit 3753592
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
5 changes: 4 additions & 1 deletion gix/src/submodule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ pub mod status {
return Ok(status);
}

if !state.worktree_checkout {
return Ok(status);
}
let statusses = adjust_options(sm_repo.status(gix_features::progress::Discard)?)
.index_worktree_options_mut(|opts| {
if ignore == config::Ignore::Untracked {
Expand All @@ -408,7 +411,7 @@ pub mod status {
/// Return `None` if the repository clone or the worktree are missing entirely, which would leave
/// it to the caller to determine if that's considered dirty or not.
pub fn is_dirty(&self) -> Option<bool> {
if !self.state.worktree_checkout && !self.state.repository_exists {
if !self.state.worktree_checkout || !self.state.repository_exists {
return None;
}
let is_dirty =
Expand Down
Binary file modified gix/tests/fixtures/generated-archives/make_submodules.tar.xz
Binary file not shown.
9 changes: 9 additions & 0 deletions gix/tests/fixtures/make_submodules.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ git init submodule-head-changed
cd m1 && git checkout @~1
)

git init submodule-head-changed-no-worktree
(cd submodule-head-changed-no-worktree
git submodule add ../module1 m1
git commit -m "add submodule"

(cd m1 && git checkout @~1)
rm -Rf m1 && mkdir m1
)

git init modified-and-untracked
(cd modified-and-untracked
git submodule add ../module1 m1
Expand Down
35 changes: 34 additions & 1 deletion gix/tests/submodule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod open {
worktree_checkout: false,
superproject_configuration: true,
},
Some(true),
None,
)],
),
] {
Expand Down Expand Up @@ -241,6 +241,39 @@ mod open {
Ok(())
}

#[test]
fn changed_head_empty_worktree() -> crate::Result {
let repo = repo("submodule-head-changed-no-worktree")?;
let sm = repo.submodules()?.into_iter().flatten().next().expect("one submodule");

let status = sm.status(gix::submodule::config::Ignore::None, false)?;
assert_eq!(
status.state,
gix::submodule::State {
repository_exists: true,
is_old_form: false,
worktree_checkout: false,
superproject_configuration: true,
}
);
assert_eq!(
status.is_dirty(),
None,
"a missing worktree counts as no-dirty, even though the checked out HEAD changed. \
Git does the same, even though as we express it as 'not determined'"
);
assert_ne!(
status.index_id, status.checked_out_head_id,
"not considered dirty despite head mismatch"
);
assert!(
status.changes.is_none(),
"Detailed changes are never done if there is no worktree"
);

Ok(())
}

#[test]
fn is_dirty_skips_expensive_checks() -> crate::Result {
let repo = repo("submodule-head-changed-and-modified")?;
Expand Down

0 comments on commit 3753592

Please sign in to comment.