History

feat: add `State::entry_mut_by_path_and_stage()`

Sebastian Thiel committed 3 years ago

Commit aa1b6ee1 parent 3ebe2d42 files · +8 −1Browse files

Unified · Split

2 files
  1. git-index/src/access/mod.rs +7 −0
  2. git-index/tests/index/file/write.rs +1 −1
git-index/src/access/mod.rs+7−0
@@ -98,6 +98,13 @@ impl State { · 97 unmodified lines
9898 .map(|idx| &self.entries[idx])
9999 }
100100
101+ /// Like [`entry_index_by_path_and_stage()`][State::entry_index_by_path_and_stage()],
102+ /// but returns the mutable entry instead of the index.
103+ pub fn entry_mut_by_path_and_stage(&mut self, path: &BStr, stage: entry::Stage) -> Option<&mut Entry> {
104+ self.entry_index_by_path_and_stage(path, stage)
105+ .map(|idx| &mut self.entries[idx])
106+ }
107+
101108 /// Return the entry at `idx` or _panic_ if the index is out of bounds.
102109 ///
103110 /// The `idx` is typically returned by [entry_by_path_and_stage()][State::entry_by_path_and_stage()].
git-index/tests/index/file/write.rs+1−1
@@ -146,7 +146,7 @@ fn remove_flag_is_respected() -> crate::Result { · 145 unmodified lines
146146 assert_eq!(index.entries().len(), total_entries);
147147 let entries_to_remove = 4;
148148 for entry in &mut index.entries_mut()[..entries_to_remove] {
149− entry.flags.toggle(git_index::entry::Flags::REMOVE);
149+ entry.flags.toggle(entry::Flags::REMOVE);
150150 }
151151 let mut buf = Vec::<u8>::new();
152152 index.write_to(&mut buf, Default::default())?;