name-hash: add index_dir_find()

index_dir_exists() returns a boolean to indicate if there is a case-insensitive match in the directory name-hash, but does not provide the caller with the exact spelling of that match. Create index_dir_find() to do the case-insensitive search *and* optionally return the spelling of the matched directory prefix in a provided strbuf. To avoid code duplication, convert index_dir_exists() to be a trivial wrapper around the new index_dir_find(). Signed-off-by: Jeff Hostetler <jeffhostetler@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Feb 26, 2024 at 21:39 UTC b3165523394a3ad4ede8872b984bbc644b056cdc
2 files changed +14 -2
name-hash.c
+8 -1
@@ -685,13 +685,20 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
685 return slow_same_name(name, namelen, ce->name, len);
686 }
687
688 -int index_dir_exists(struct index_state *istate, const char *name, int namelen)
688 +int index_dir_find(struct index_state *istate, const char *name, int namelen,
689 + struct strbuf *canonical_path)
690 {
691 struct dir_entry *dir;
692
693 lazy_init_name_hash(istate);
694 expand_to_path(istate, name, namelen, 0);
695 dir = find_dir_entry(istate, name, namelen);
696 +
697 + if (canonical_path && dir && dir->nr) {
698 + strbuf_reset(canonical_path);
699 + strbuf_add(canonical_path, dir->name, dir->namelen);
700 + }
701 +
702 return dir && dir->nr;
703 }
704
name-hash.h
+6 -1
@@ -4,7 +4,12 @@
4 struct cache_entry;
5 struct index_state;
6
7 -int index_dir_exists(struct index_state *istate, const char *name, int namelen);
7 +
8 +int index_dir_find(struct index_state *istate, const char *name, int namelen,
9 + struct strbuf *canonical_path);
10 +
11 +#define index_dir_exists(i, n, l) index_dir_find((i), (n), (l), NULL)
12 +
13 void adjust_dirname_case(struct index_state *istate, char *name);
14 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
15