Skip to content

Commit

Permalink
feat: add State::remove_entry_at_index(pos) by position.
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Feb 13, 2025
1 parent bf85128 commit 8173096
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion gix-index/src/access/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,8 @@ impl State {

/// Physically remove all entries for which `should_remove(idx, path, entry)` returns `true`, traversing them from first to last.
///
/// Note that the memory used for the removed entries paths is not freed, as it's append-only.
/// Note that the memory used for the removed entries paths is not freed, as it's append-only, and
/// that some extensions might refer to paths which are now deleted.
///
/// ### Performance
///
Expand All @@ -534,6 +535,16 @@ impl State {
res
});
}

/// Physically remove the entry at `index`, or panic if the entry didn't exist.
///
/// This call is typically made after looking up `index`, so it's clear that it will not panic.
///
/// Note that the memory used for the removed entries paths is not freed, as it's append-only, and
/// that some extensions might refer to paths which are now deleted.
pub fn remove_entry_at_index(&mut self, index: usize) -> Entry {
self.entries.remove(index)
}
}

/// Extensions
Expand Down
12 changes: 12 additions & 0 deletions gix-index/tests/index/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ fn remove_entries() {
file.remove_entries(|_, _, _| unreachable!("should not be called"));
}

#[test]
fn remove_entry_at_index() {
let mut file = Fixture::Loose("conflicting-file").open();

file.remove_entry_at_index(0);
assert_eq!(file.entries().len(), 2);
file.remove_entry_at_index(0);
assert_eq!(file.entries().len(), 1);
file.remove_entry_at_index(0);
assert_eq!(file.entries().len(), 0);
}

#[test]
fn sort_entries() {
let mut file = Fixture::Generated("v4_more_files_IEOT").open();
Expand Down

0 comments on commit 8173096

Please sign in to comment.