Skip to content

Commit 02ffd20

Browse files
committed
feat: add Repository::index_or_empty().
This is useful if a missing index should mean it's empty.
1 parent b2ce073 commit 02ffd20

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

gix/src/repository/index.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ impl crate::Repository {
5555
})
5656
}
5757

58+
/// Return the shared worktree index if present, or return a new empty one which has an association to the place where the index would be.
59+
pub fn index_or_empty(&self) -> Result<worktree::Index, worktree::open_index::Error> {
60+
Ok(self.try_index()?.unwrap_or_else(|| {
61+
worktree::Index::new(gix_fs::FileSnapshot::new(gix_index::File::from_state(
62+
gix_index::State::new(self.object_hash()),
63+
self.index_path(),
64+
)))
65+
}))
66+
}
67+
5868
/// Return a shared worktree index which is updated automatically if the in-memory snapshot has become stale as the underlying file
5969
/// on disk has changed, or `None` if no such file exists.
6070
///

gix/tests/repository/worktree.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ mod with_core_worktree_config {
111111
}
112112

113113
#[test]
114+
#[cfg(feature = "index")]
114115
fn bare_relative() -> crate::Result {
115116
let repo = repo("bare-relative-worktree");
116117

@@ -123,6 +124,8 @@ mod with_core_worktree_config {
123124
repo.work_dir().is_none(),
124125
"we simply don't load core.worktree in bare repos either to match this behaviour"
125126
);
127+
assert!(repo.try_index()?.is_none());
128+
assert!(repo.index_or_empty()?.entries().is_empty());
126129
Ok(())
127130
}
128131

0 commit comments

Comments
 (0)