name-hash: stop storing trailing '/' on paths in index_state.dir_hash

When 5102c617 (Add case insensitivity support for directories when using git status, 2010-10-03) added directories to the name-hash there was only a single hash table in which both real cache entries and leading directory prefixes were registered. To distinguish between the two types of entries, directories were stored with a trailing '/'. 2092678c (name-hash.c: fix endless loop with core.ignorecase=true, 2013-02-28), however, moved directories to a separate hash table (index_state.dir_hash) but retained the (now) redundant trailing '/', thus callers continue to bear the burden of ensuring the slash's presence before searching the index for a directory. Eliminate this redundancy by storing paths in the dir-hash without the trailing '/'. An important benefit of this change is that it eliminates undocumented and dangerous behavior of dir.c:directory_exists_in_index_icase() in which it assumes not only that it can validly access one character beyond the end of its incoming directory argument, but also that that character will unconditionally be a '/'. This perilous behavior was "tolerated" because the string passed in by its lone caller always had a '/' in that position, however, things broke [1] when 2eac2a4c (ls-files -k: a directory only can be killed if the index has a non-directory, 2013-08-15) added a new caller which failed to respect the undocumented assumption. [1]: http://thread.gmane.org/gmane.comp.version-control.git/232727 Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Sep 17, 2013 at 03:06 UTC d28eec267374c476de490bff736a627da94575c5
3 files changed +8 -7
dir.c
+1 -1
@@ -889,7 +889,7 @@ enum exist_status {
889 */
890 static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
891 {
892 - const struct cache_entry *ce = cache_dir_exists(dirname, len + 1);
892 + const struct cache_entry *ce = cache_dir_exists(dirname, len);
893 unsigned char endchar;
894
895 if (!ce)
name-hash.c
+6 -5
@@ -58,9 +58,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
58 {
59 /*
60 * Throw each directory component in the hash for quick lookup
61 - * during a git status. Directory components are stored with their
61 + * during a git status. Directory components are stored without their
62 * closing slash. Despite submodules being a directory, they never
63 - * reach this point, because they are stored without a closing slash
63 + * reach this point, because they are stored
64 * in index_state.name_hash (as ordinary cache_entries).
65 *
66 * Note that the cache_entry stored with the dir_entry merely
@@ -78,6 +78,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
78 namelen--;
79 if (namelen <= 0)
80 return NULL;
81 + namelen--;
82
83 /* lookup existing entry for that directory */
84 dir = find_dir_entry(istate, ce->name, namelen);
@@ -97,7 +98,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
98 }
99
100 /* recursively add missing parent directories */
100 - dir->parent = hash_dir_entry(istate, ce, namelen - 1);
101 + dir->parent = hash_dir_entry(istate, ce, namelen);
102 }
103 return dir;
104 }
@@ -237,7 +238,7 @@ struct cache_entry *index_dir_exists(struct index_state *istate, const char *nam
238 * in the dir-hash, submodules are stored in the name-hash, so check
239 * there, as well.
240 */
240 - ce = index_file_exists(istate, name, namelen - 1, 1);
241 + ce = index_file_exists(istate, name, namelen, 1);
242 if (ce && S_ISGITLINK(ce->ce_mode))
243 return ce;
244
@@ -265,7 +266,7 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
266 struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
267 {
268 if (namelen > 0 && name[namelen - 1] == '/')
268 - return index_dir_exists(istate, name, namelen);
269 + return index_dir_exists(istate, name, namelen - 1);
270 return index_file_exists(istate, name, namelen, icase);
271 }
272
read-cache.c
+1 -1
@@ -643,7 +643,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
643 if (*ptr == '/') {
644 struct cache_entry *foundce;
645 ++ptr;
646 - foundce = index_dir_exists(istate, ce->name, ptr - ce->name);
646 + foundce = index_dir_exists(istate, ce->name, ptr - ce->name - 1);
647 if (foundce) {
648 memcpy((void *)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr);
649 startPtr = ptr;