Skip to content

Commit 5102c61

Browse files
jjensengitster
authored andcommitted
Add case insensitivity support for directories when using git status
When using a case preserving but case insensitive file system, directory case can differ but still refer to the same physical directory. git status reports the directory with the alternate case as an Untracked file. (That is, when mydir/filea.txt is added to the repository and then the directory on disk is renamed from mydir/ to MyDir/, git status shows MyDir/ as being untracked.) Support has been added in name-hash.c for hashing directories with a terminating slash into the name hash. When index_name_exists() is called with a directory (a name with a terminating slash), the name is not found via the normal cache_name_compare() call, but it is found in the slow_same_name() function. Additionally, in dir.c, directory_exists_in_index_icase() allows newly added directories deeper in the directory chain to be identified. Ultimately, it would be better if the file list was read in case insensitive alphabetical order from disk, but this change seems to suffice for now. The end result is the directory is looked up in a case insensitive manner and does not show in the Untracked files list. Signed-off-by: Joshua Jensen <jjensen@workspacewhiz.com> Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 10d4b02 commit 5102c61

File tree

2 files changed

+110
-2
lines changed

2 files changed

+110
-2
lines changed

dir.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,39 @@ enum exist_status {
484484
index_gitdir
485485
};
486486

487+
/*
488+
* Do not use the alphabetically stored index to look up
489+
* the directory name; instead, use the case insensitive
490+
* name hash.
491+
*/
492+
static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
493+
{
494+
struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
495+
unsigned char endchar;
496+
497+
if (!ce)
498+
return index_nonexistent;
499+
endchar = ce->name[len];
500+
501+
/*
502+
* The cache_entry structure returned will contain this dirname
503+
* and possibly additional path components.
504+
*/
505+
if (endchar == '/')
506+
return index_directory;
507+
508+
/*
509+
* If there are no additional path components, then this cache_entry
510+
* represents a submodule. Submodules, despite being directories,
511+
* are stored in the cache without a closing slash.
512+
*/
513+
if (!endchar && S_ISGITLINK(ce->ce_mode))
514+
return index_gitdir;
515+
516+
/* This should never be hit, but it exists just in case. */
517+
return index_nonexistent;
518+
}
519+
487520
/*
488521
* The index sorts alphabetically by entry name, which
489522
* means that a gitlink sorts as '\0' at the end, while
@@ -493,7 +526,12 @@ enum exist_status {
493526
*/
494527
static enum exist_status directory_exists_in_index(const char *dirname, int len)
495528
{
496-
int pos = cache_name_pos(dirname, len);
529+
int pos;
530+
531+
if (ignore_case)
532+
return directory_exists_in_index_icase(dirname, len);
533+
534+
pos = cache_name_pos(dirname, len);
497535
if (pos < 0)
498536
pos = -pos-1;
499537
while (pos < active_nr) {

name-hash.c

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,42 @@ static unsigned int hash_name(const char *name, int namelen)
3232
return hash;
3333
}
3434

35+
static void hash_index_entry_directories(struct index_state *istate, struct cache_entry *ce)
36+
{
37+
/*
38+
* Throw each directory component in the hash for quick lookup
39+
* during a git status. Directory components are stored with their
40+
* closing slash. Despite submodules being a directory, they never
41+
* reach this point, because they are stored without a closing slash
42+
* in the cache.
43+
*
44+
* Note that the cache_entry stored with the directory does not
45+
* represent the directory itself. It is a pointer to an existing
46+
* filename, and its only purpose is to represent existence of the
47+
* directory in the cache. It is very possible multiple directory
48+
* hash entries may point to the same cache_entry.
49+
*/
50+
unsigned int hash;
51+
void **pos;
52+
53+
const char *ptr = ce->name;
54+
while (*ptr) {
55+
while (*ptr && *ptr != '/')
56+
++ptr;
57+
if (*ptr == '/') {
58+
++ptr;
59+
hash = hash_name(ce->name, ptr - ce->name);
60+
if (!lookup_hash(hash, &istate->name_hash)) {
61+
pos = insert_hash(hash, ce, &istate->name_hash);
62+
if (pos) {
63+
ce->next = *pos;
64+
*pos = ce;
65+
}
66+
}
67+
}
68+
}
69+
}
70+
3571
static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
3672
{
3773
void **pos;
@@ -47,6 +83,9 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
4783
ce->next = *pos;
4884
*pos = ce;
4985
}
86+
87+
if (ignore_case)
88+
hash_index_entry_directories(istate, ce);
5089
}
5190

5291
static void lazy_init_name_hash(struct index_state *istate)
@@ -97,7 +136,21 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
97136
if (len == namelen && !cache_name_compare(name, namelen, ce->name, len))
98137
return 1;
99138

100-
return icase && slow_same_name(name, namelen, ce->name, len);
139+
if (!icase)
140+
return 0;
141+
142+
/*
143+
* If the entry we're comparing is a filename (no trailing slash), then compare
144+
* the lengths exactly.
145+
*/
146+
if (name[namelen - 1] != '/')
147+
return slow_same_name(name, namelen, ce->name, len);
148+
149+
/*
150+
* For a directory, we point to an arbitrary cache_entry filename. Just
151+
* make sure the directory portion matches.
152+
*/
153+
return slow_same_name(name, namelen, ce->name, namelen < len ? namelen : len);
101154
}
102155

103156
struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
@@ -115,5 +168,22 @@ struct cache_entry *index_name_exists(struct index_state *istate, const char *na
115168
}
116169
ce = ce->next;
117170
}
171+
172+
/*
173+
* Might be a submodule. Despite submodules being directories,
174+
* they are stored in the name hash without a closing slash.
175+
* When ignore_case is 1, directories are stored in the name hash
176+
* with their closing slash.
177+
*
178+
* The side effect of this storage technique is we have need to
179+
* remove the slash from name and perform the lookup again without
180+
* the slash. If a match is made, S_ISGITLINK(ce->mode) will be
181+
* true.
182+
*/
183+
if (icase && name[namelen - 1] == '/') {
184+
ce = index_name_exists(istate, name, namelen - 1, icase);
185+
if (ce && S_ISGITLINK(ce->ce_mode))
186+
return ce;
187+
}
118188
return NULL;
119189
}

0 commit comments

Comments
 (0)