Skip to content

Commit d28eec2

Browse files
sunshinecogitster
authored andcommitted
name-hash: stop storing trailing '/' on paths in index_state.dir_hash
When 5102c61 (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 '/'. 2092678 (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 2eac2a4 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ebbd743 commit d28eec2

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ enum exist_status {
889889
*/
890890
static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
891891
{
892-
const struct cache_entry *ce = cache_dir_exists(dirname, len + 1);
892+
const struct cache_entry *ce = cache_dir_exists(dirname, len);
893893
unsigned char endchar;
894894

895895
if (!ce)

name-hash.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
5858
{
5959
/*
6060
* 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
6262
* 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
6464
* in index_state.name_hash (as ordinary cache_entries).
6565
*
6666
* 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,
7878
namelen--;
7979
if (namelen <= 0)
8080
return NULL;
81+
namelen--;
8182

8283
/* lookup existing entry for that directory */
8384
dir = find_dir_entry(istate, ce->name, namelen);
@@ -97,7 +98,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
9798
}
9899

99100
/* recursively add missing parent directories */
100-
dir->parent = hash_dir_entry(istate, ce, namelen - 1);
101+
dir->parent = hash_dir_entry(istate, ce, namelen);
101102
}
102103
return dir;
103104
}
@@ -237,7 +238,7 @@ struct cache_entry *index_dir_exists(struct index_state *istate, const char *nam
237238
* in the dir-hash, submodules are stored in the name-hash, so check
238239
* there, as well.
239240
*/
240-
ce = index_file_exists(istate, name, namelen - 1, 1);
241+
ce = index_file_exists(istate, name, namelen, 1);
241242
if (ce && S_ISGITLINK(ce->ce_mode))
242243
return ce;
243244

@@ -265,7 +266,7 @@ struct cache_entry *index_file_exists(struct index_state *istate, const char *na
265266
struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
266267
{
267268
if (namelen > 0 && name[namelen - 1] == '/')
268-
return index_dir_exists(istate, name, namelen);
269+
return index_dir_exists(istate, name, namelen - 1);
269270
return index_file_exists(istate, name, namelen, icase);
270271
}
271272

read-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
643643
if (*ptr == '/') {
644644
struct cache_entry *foundce;
645645
++ptr;
646-
foundce = index_dir_exists(istate, ce->name, ptr - ce->name);
646+
foundce = index_dir_exists(istate, ce->name, ptr - ce->name - 1);
647647
if (foundce) {
648648
memcpy((void *)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr);
649649
startPtr = ptr;

0 commit comments

Comments
 (0)