diff --git a/git-index/src/access.rs b/git-index/src/access.rs index c2aae182cb..6024993d2c 100644 --- a/git-index/src/access.rs +++ b/git-index/src/access.rs @@ -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 { + pub fn entry_index_by_path_and_stage(&self, path: &BStr, stage: entry::Stage) -> Option { 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 diff --git a/git-index/tests/index/file/access.rs b/git-index/tests/index/file/access.rs index 2e15c98bfd..fbe2b5374d 100644 --- a/git-index/tests/index/file/access.rs +++ b/git-index/tests/index/file/access.rs @@ -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)); } }