Skip to content

Commit 6a8314b

Browse files
committed
fix: prefixed_entries_range() now works correctly with directory prefixes.
Previously, not all directory prefixes would work as expected due to incorrect search criteria.
1 parent 3be2b1c commit 6a8314b

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

gix-index/src/access/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ impl State {
198198
return Some(0..self.entries.len());
199199
}
200200
let prefix_len = prefix.len();
201-
let mut low = self
202-
.entries
203-
.partition_point(|e| e.path(self).get(..prefix_len).map_or(true, |p| p < prefix));
201+
let mut low = self.entries.partition_point(|e| {
202+
e.path(self)
203+
.get(..prefix_len)
204+
.map_or_else(|| e.path(self) <= &prefix[..e.path.len()], |p| p < prefix)
205+
});
204206
let mut high = low
205207
+ self.entries[low..].partition_point(|e| e.path(self).get(..prefix_len).map_or(false, |p| p <= prefix));
206208

@@ -211,9 +213,9 @@ impl State {
211213
.unwrap_or(low);
212214
}
213215
if let Some(high_entry) = self.entries.get(high) {
214-
if high_entry.stage() != 3 {
216+
if high_entry.stage() != 0 {
215217
high = self
216-
.walk_entry_stages(high_entry.path(self), high, Ordering::Greater)
218+
.walk_entry_stages(high_entry.path(self), high, Ordering::Less)
217219
.unwrap_or(high);
218220
}
219221
}

gix-index/tests/index/access.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ fn sort_entries() {
114114
"we can find the correct entry now"
115115
);
116116

117-
check_prefix(&file, "a", &["a", "an initially incorrectly ordered entry"]);
118117
check_prefix(
119118
&file,
120119
"d",
@@ -126,18 +125,44 @@ fn sort_entries() {
126125
&["d/a", "d/b", "d/c", "d/last/123", "d/last/34", "d/last/6"],
127126
);
128127
check_prefix(&file, "d/last", &["d/last/123", "d/last/34", "d/last/6"]);
128+
check_prefix(&file, "d/last/", &["d/last/123", "d/last/34", "d/last/6"]);
129129
check_prefix(&file, "d/las", &["d/last/123", "d/last/34", "d/last/6"]);
130+
check_prefix(&file, "d/last/123", &["d/last/123"]);
131+
check_prefix(&file, "d/last/34", &["d/last/34"]);
132+
check_prefix(&file, "d/last/6", &["d/last/6"]);
133+
check_prefix(&file, "x", &["x"]);
134+
check_prefix(&file, "a", &["a", "an initially incorrectly ordered entry"]);
135+
}
136+
137+
#[test]
138+
fn prefixed_entries() {
139+
let mut file = Fixture::Generated("v4_more_files_IEOT").open();
140+
let entry = file.entry(0).clone();
141+
let new_entry_path = "an initially incorrectly ordered entry".into();
142+
file.dangerously_push_entry(entry.stat, entry.id, entry.flags, entry.mode, new_entry_path);
143+
file.sort_entries();
144+
145+
check_prefix(&file, "a", &["a", "an initially incorrectly ordered entry"]);
146+
check_prefix(&file, "an", &["an initially incorrectly ordered entry"]);
147+
check_prefix(
148+
&file,
149+
"an initially incorrectly ordered entry",
150+
&["an initially incorrectly ordered entry"],
151+
);
130152
check_prefix(&file, "x", &["x"]);
153+
check_prefix(&file, "b", &["b"]);
154+
check_prefix(&file, "c", &["c"]);
131155
}
132156

133157
fn check_prefix(index: &gix_index::State, prefix: &str, expected: &[&str]) {
134158
assert_eq!(
135159
index
136160
.prefixed_entries(prefix.into())
137-
.expect("present")
161+
.unwrap_or_else(|| panic!("{prefix:?} must match at least one entry"))
138162
.iter()
139163
.map(|e| e.path(index))
140164
.collect::<Vec<_>>(),
141-
expected
165+
expected,
166+
"{prefix:?}"
142167
);
143168
}

0 commit comments

Comments
 (0)