Skip to content

Commit

Permalink
feat: Stage::entry_index_by_path_and_stage(), now with `::entry_by_…
Browse files Browse the repository at this point in the history
…path_and_stage()` (#427)
  • Loading branch information
Byron committed Aug 2, 2022
1 parent 711fd5c commit 6d8d5e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 15 additions & 1 deletion git-index/src/access.rs
Expand Up @@ -64,11 +64,25 @@ impl State {
/// `path` and `stage`, or `None`.
///
/// Use the index for accessing multiple stages if they exists, but at least the single matching entry.
pub fn entry_by_path_and_stage(&self, path: &BStr, stage: entry::Stage) -> Option<usize> {
pub fn entry_index_by_path_and_stage(&self, path: &BStr, stage: entry::Stage) -> Option<usize> {
self.entries
.binary_search_by(|e| e.path(self).cmp(path).then_with(|| e.stage().cmp(&stage)))
.ok()
}

/// Like [`entry_index_by_path_and_stage()`][File::entry_index_by_path_and_stage()],
/// but returns the entry instead of the index.
pub fn entry_by_path_and_stage(&self, path: &BStr, stage: entry::Stage) -> Option<&Entry> {
self.entry_index_by_path_and_stage(path, stage)
.map(|idx| &self.entries[idx])
}

/// Return the entry at `idx` or _panic_ if the index is out of bounds.
///
/// The `idx` is typically returned by [entry_by_path_and_stage()][File::entry_by_path_and_stage()].
pub fn entry(&self, idx: usize) -> &Entry {
&self.entries[idx]
}
}

/// Extensions
Expand Down
4 changes: 3 additions & 1 deletion git-index/tests/index/file/access.rs
Expand Up @@ -6,8 +6,10 @@ fn entry_by_path_and_stage() {
for entry in file.entries() {
let path = entry.path(&file);
assert_eq!(
file.entry_by_path_and_stage(path, 0).map(|idx| &file.entries()[idx]),
file.entry_index_by_path_and_stage(path, 0)
.map(|idx| &file.entries()[idx]),
Some(entry)
);
assert_eq!(file.entry_by_path_and_stage(path, 0), Some(entry));
}
}

0 comments on commit 6d8d5e6

Please sign in to comment.