Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95a8d2d

Browse files
committed
feat!: rename State::entry_closest_to_directory(_icase)() to State::entry_closest_to_directory_or_directory(_icase)().
This also changes the meaning, allowing it to return directory entries, which makes them a perfect match.
1 parent 58507f8 commit 95a8d2d

8 files changed

Lines changed: 45 additions & 62 deletions

File tree

gix-dir/src/walk/classify.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,14 @@ fn resolve_file_type_with_index(
388388
// TODO(perf): multi-threaded hash-table so it's always used, even for case-sensitive lookups, just like Git does it.
389389
let (uptodate_kind, index_kind) = if let Some(accelerate) = ignore_case {
390390
match index.entry_by_path_icase(rela_path.as_bstr(), true, accelerate) {
391-
None => {
392-
icase_directory_to_kinds(index.entry_closest_to_directory_icase(rela_path.as_bstr(), true, accelerate))
393-
}
391+
None => icase_directory_to_kinds(index.entry_closest_to_directory_or_directory_icase(
392+
rela_path.as_bstr(),
393+
true,
394+
accelerate,
395+
)),
394396
Some(entry) => {
395-
let icase_dir = index.entry_closest_to_directory_icase(rela_path.as_bstr(), true, accelerate);
397+
let icase_dir =
398+
index.entry_closest_to_directory_or_directory_icase(rela_path.as_bstr(), true, accelerate);
396399
let directory_matches_exactly = icase_dir.is_some_and(|dir| {
397400
let path = dir.path(index);
398401
let slash_idx = path.rfind_byte(b'/').expect("dir");

gix-index/src/access/mod.rs

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,10 @@ impl State {
217217
}
218218

219219
/// Return the entry (at any stage) that is inside `directory`, or `None`,
220-
/// using `lookup` for acceleration.
221-
/// Note that submodules are not detected as directories and the user should
222-
/// make another call to [`entry_by_path_icase()`](Self::entry_by_path_icase) to check for this
223-
/// possibility. Doing so might also reveal a sparse directory.
220+
/// or a directory itself like a submodule or sparse directory, using `lookup` for acceleration.
224221
///
225222
/// If `ignore_case` is set, a case-insensitive (ASCII-folding only) search will be performed.
226-
pub fn entry_closest_to_directory_icase<'a>(
223+
pub fn entry_closest_to_directory_or_directory_icase<'a>(
227224
&'a self,
228225
directory: &BStr,
229226
ignore_case: bool,
@@ -244,27 +241,15 @@ impl State {
244241
.map(|dir| dir.entry)
245242
}
246243

247-
/// Return the entry (at any stage) that is inside of `directory`, or `None`.
248-
/// Note that submodules are not detected as directories and the user should
249-
/// make another call to [`entry_by_path_icase()`](Self::entry_by_path_icase) to check for this
250-
/// possibility. Doing so might also reveal a sparse directory.
244+
/// Return the entry (at any stage) that is inside `directory`, or `None`,
245+
/// or that is a directory itself like a submodule or sparse directory.
251246
///
252-
/// Note that this is a case-sensitive search.
253-
pub fn entry_closest_to_directory(&self, directory: &BStr) -> Option<&Entry> {
254-
self.entry_closest_to_directory_inner(directory, DirectoryEntry::Allow)
255-
}
256-
257-
/// If `dirmode` is [`DirectoryEntry::Allow`] then allow returning the directory entry itself.
258-
fn entry_closest_to_directory_inner(&self, directory: &BStr, dirmode: DirectoryEntry) -> Option<&Entry> {
247+
/// Note that this is a *case-sensitive* search.
248+
pub fn entry_closest_to_directory_or_directory(&self, directory: &BStr) -> Option<&Entry> {
259249
let idx = match self.entry_index_by_path(directory) {
260250
Ok(idx) => {
261-
return match dirmode {
262-
DirectoryEntry::Allow => {
263-
let entry = &self.entries[idx];
264-
entry_is_dir(entry).then_some(entry)
265-
}
266-
DirectoryEntry::Deny => None,
267-
};
251+
let entry = &self.entries[idx];
252+
return entry_is_dir(entry).then_some(entry);
268253
}
269254
Err(closest_idx) => closest_idx,
270255
};
@@ -295,8 +280,7 @@ impl State {
295280
///
296281
/// Note that this is a case-sensitive search.
297282
pub fn path_is_directory(&self, path: &BStr) -> bool {
298-
self.entry_closest_to_directory_inner(path, DirectoryEntry::Allow)
299-
.is_some()
283+
self.entry_closest_to_directory_or_directory(path).is_some()
300284
}
301285

302286
/// Check if `path` is a directory that contains entries in the index or is a submodule,
@@ -316,7 +300,7 @@ impl State {
316300
ignore_case: bool,
317301
lookup: &AccelerateLookup<'a>,
318302
) -> bool {
319-
self.entry_closest_to_directory_icase(path, ignore_case, lookup)
303+
self.entry_closest_to_directory_or_directory_icase(path, ignore_case, lookup)
320304
.is_some()
321305
}
322306

@@ -651,11 +635,6 @@ impl State {
651635
}
652636
}
653637

654-
enum DirectoryEntry {
655-
Allow,
656-
Deny,
657-
}
658-
659638
fn entry_is_dir(entry: &Entry) -> bool {
660639
entry.mode.is_sparse() || entry.mode.is_submodule()
661640
}

gix-index/tests/fixtures/generated-archives/V2_empty.tar renamed to gix-index/tests/fixtures/generated-archives/v2_empty.tar

File renamed without changes.
File renamed without changes.

gix-index/tests/index/access.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn dirwalk_api_and_icase_support() {
4848
last_pos += 1;
4949

5050
let entry = file
51-
.entry_closest_to_directory(dir)
51+
.entry_closest_to_directory_or_directory(dir)
5252
.unwrap_or_else(|| panic!("didn't find {dir}"));
5353
assert!(
5454
entry.path(&file).starts_with(dir),
@@ -57,69 +57,70 @@ fn dirwalk_api_and_icase_support() {
5757

5858
let dir_upper: BString = dir.to_ascii_uppercase().into();
5959
let other_entry = file
60-
.entry_closest_to_directory_icase(dir_upper.as_bstr(), true, &icase)
60+
.entry_closest_to_directory_or_directory_icase(dir_upper.as_bstr(), true, &icase)
6161
.unwrap_or_else(|| panic!("didn't find upper-cased {dir_upper}"));
6262
assert_eq!(other_entry, entry, "the first entry is always the same, no matter what kind of search is conducted (as there are no clashes/ambiguities here)");
6363
}
6464
}
6565
}
6666

6767
#[test]
68-
fn entry_closest_to_directory_with_submodule() {
69-
let file = Fixture::Generated("V2_all_file_kinds").open();
68+
fn entry_closest_to_directory_or_directory_with_submodule() {
69+
let file = Fixture::Generated("v2_all_file_kinds").open();
7070

7171
assert!(
72-
file.entry_closest_to_directory("d".into()).is_some(),
72+
file.entry_closest_to_directory_or_directory("d".into()).is_some(),
7373
"this is a directory"
7474
);
7575
assert!(
76-
file.entry_closest_to_directory("sub".into()).is_some(),
76+
file.entry_closest_to_directory_or_directory("sub".into()).is_some(),
7777
"this is a checked in repository, a directory itself"
7878
);
7979
assert!(
80-
file.entry_closest_to_directory("sub-worktree".into()).is_some(),
80+
file.entry_closest_to_directory_or_directory("sub-worktree".into())
81+
.is_some(),
8182
"a submodule that is officially registered, absolutely the same as 'sub' in the index."
8283
);
8384
assert!(
84-
file.entry_closest_to_directory("a".into()).is_none(),
85+
file.entry_closest_to_directory_or_directory("a".into()).is_none(),
8586
"'a' is a file, and we ask for a directory"
8687
);
8788
}
8889

8990
#[test]
90-
fn entry_closest_to_directory_icase_with_submodule() {
91-
let file = Fixture::Generated("V2_all_file_kinds").open();
91+
fn entry_closest_to_directory_or_directory_icase_with_submodule() {
92+
let file = Fixture::Generated("v2_all_file_kinds").open();
9293
let icase = file.prepare_icase_backing();
9394

9495
assert!(
95-
file.entry_closest_to_directory_icase("D".into(), true, &icase)
96+
file.entry_closest_to_directory_or_directory_icase("D".into(), true, &icase)
9697
.is_some(),
9798
"this is a directory"
9899
);
99100
assert!(file
100-
.entry_closest_to_directory_icase("D".into(), false, &icase)
101+
.entry_closest_to_directory_or_directory_icase("D".into(), false, &icase)
101102
.is_none());
102103

103104
assert!(
104-
file.entry_closest_to_directory_icase("SuB".into(), true, &icase)
105+
file.entry_closest_to_directory_or_directory_icase("SuB".into(), true, &icase)
105106
.is_some(),
106107
"this is a checked in repository, a directory itself"
107108
);
108109
assert!(file
109-
.entry_closest_to_directory_icase("SuB".into(), false, &icase)
110+
.entry_closest_to_directory_or_directory_icase("SuB".into(), false, &icase)
110111
.is_none());
111112

112113
assert!(
113-
file.entry_closest_to_directory_icase("SUB-worktree".into(), true, &icase)
114+
file.entry_closest_to_directory_or_directory_icase("SUB-worktree".into(), true, &icase)
114115
.is_some(),
115116
"a submodule that is officially registered, absolutely the same as 'sub' in the index."
116117
);
117118
assert!(file
118-
.entry_closest_to_directory_icase("SUB-worktree".into(), false, &icase)
119+
.entry_closest_to_directory_or_directory_icase("SUB-worktree".into(), false, &icase)
119120
.is_none());
120121

121122
assert!(
122-
file.entry_closest_to_directory_icase("A".into(), true, &icase)
123+
file.entry_closest_to_directory_or_directory_icase("A".into(), true, &icase)
123124
.is_none(),
124125
"'a' is a file, and we ask for a directory"
125126
);
@@ -146,7 +147,7 @@ fn ignorecase_clashes_and_order() {
146147
last_pos += 1;
147148

148149
let entry = file
149-
.entry_closest_to_directory(dir)
150+
.entry_closest_to_directory_or_directory(dir)
150151
.unwrap_or_else(|| panic!("didn't find {dir}"));
151152
assert!(
152153
entry.path(&file).starts_with(dir),
@@ -171,16 +172,16 @@ fn ignorecase_clashes_and_order() {
171172
);
172173

173174
assert!(
174-
file.entry_closest_to_directory("d".into()).is_none(),
175+
file.entry_closest_to_directory_or_directory("d".into()).is_none(),
175176
"this is a file, and this directory search isn't case-sensitive"
176177
);
177-
let entry = file.entry_closest_to_directory("D".into());
178+
let entry = file.entry_closest_to_directory_or_directory("D".into());
178179
assert_eq!(
179180
entry.map(|e| e.path(&file)).expect("present"),
180181
"D/B",
181182
"this is a directory, indeed, we find the first file in it"
182183
);
183-
let entry_icase = file.entry_closest_to_directory_icase("d".into(), true, &icase);
184+
let entry_icase = file.entry_closest_to_directory_or_directory_icase("d".into(), true, &icase);
184185
assert_eq!(
185186
entry_icase, entry,
186187
"case-insensitive searches don't confuse directories and files, so `d` finds `D`, the directory."
@@ -388,7 +389,7 @@ fn check_prefix(index: &gix_index::State, prefix: &str, expected: &[&str]) {
388389

389390
#[test]
390391
fn path_is_directory_with_submodule() {
391-
let file = Fixture::Generated("V2_all_file_kinds").open();
392+
let file = Fixture::Generated("v2_all_file_kinds").open();
392393

393394
assert!(file.path_is_directory("sub-worktree".into()), "a submodule worktree");
394395
assert!(file.path_is_directory("d".into()), "a single-letter directory");
@@ -491,7 +492,7 @@ fn path_is_directory_icase() {
491492

492493
#[test]
493494
fn path_is_directory_icase_with_submodule() {
494-
let file = Fixture::Generated("V2_all_file_kinds").open();
495+
let file = Fixture::Generated("v2_all_file_kinds").open();
495496
let icase = file.prepare_icase_backing();
496497

497498
assert!(

gix-index/tests/index/file/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ mod from_state {
3737
let fixtures = [
3838
(Loose("extended-flags"), V3),
3939
(Generated("v2"), V2),
40-
(Generated("V2_empty"), V2),
40+
(Generated("v2_empty"), V2),
4141
(Generated("v2_more_files"), V2),
4242
(Generated("v2_all_file_kinds"), V2),
4343
(Generated("v4_more_files_IEOT"), V2),

gix-index/tests/index/file/read.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn v2_with_single_entry_tree_and_eoie_ext() {
6666
}
6767
#[test]
6868
fn v2_empty() {
69-
let file = file("V2_empty");
69+
let file = file("v2_empty");
7070
assert_eq!(file.version(), Version::V2);
7171
assert_eq!(file.entries().len(), 0);
7272
let tree = file.tree().unwrap();

gix-index/tests/index/file/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn roundtrips() -> crate::Result {
1717
end_of_index_entry: true,
1818
}),
1919
),
20-
(Generated("V2_empty"), only_tree_ext()),
20+
(Generated("v2_empty"), only_tree_ext()),
2121
(Generated("v2_more_files"), only_tree_ext()),
2222
(Generated("v2_all_file_kinds"), only_tree_ext()),
2323
];
@@ -128,7 +128,7 @@ fn state_comparisons_with_various_extension_configurations() {
128128
Loose("REUC"),
129129
Loose("UNTR-with-oids"),
130130
Loose("UNTR"),
131-
Generated("V2_empty"),
131+
Generated("v2_empty"),
132132
Generated("v2"),
133133
Generated("v2_more_files"),
134134
Generated("v2_all_file_kinds"),

0 commit comments

Comments
 (0)