Merge branch 'rr/tests-dedup-test-config'
[git.git] / name-hash.c
blob9bac31a6ab7aa80586828e7f48ab829e5b77a740
1 /*
2 * name-hash.c
4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
7 */
8 #define NO_THE_INDEX_COMPATIBILITY_MACROS
9 #include "cache.h"
12 * This removes bit 5 if bit 6 is set.
14 * That will make US-ASCII characters hash to their upper-case
15 * equivalent. We could easily do this one whole word at a time,
16 * but that's for future worries.
18 static inline unsigned char icase_hash(unsigned char c)
20 return c & ~((c & 0x40) >> 1);
23 static unsigned int hash_name(const char *name, int namelen)
25 unsigned int hash = 0x123;
27 while (namelen--) {
28 unsigned char c = *name++;
29 c = icase_hash(c);
30 hash = hash*101 + c;
32 return hash;
35 static void hash_index_entry_directories(struct index_state *istate, struct cache_entry *ce)
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.
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.
50 unsigned int hash;
51 void **pos;
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 pos = insert_hash(hash, ce, &istate->name_hash);
61 if (pos) {
62 ce->dir_next = *pos;
63 *pos = ce;
69 static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
71 void **pos;
72 unsigned int hash;
74 if (ce->ce_flags & CE_HASHED)
75 return;
76 ce->ce_flags |= CE_HASHED;
77 ce->next = ce->dir_next = NULL;
78 hash = hash_name(ce->name, ce_namelen(ce));
79 pos = insert_hash(hash, ce, &istate->name_hash);
80 if (pos) {
81 ce->next = *pos;
82 *pos = ce;
85 if (ignore_case)
86 hash_index_entry_directories(istate, ce);
89 static void lazy_init_name_hash(struct index_state *istate)
91 int nr;
93 if (istate->name_hash_initialized)
94 return;
95 if (istate->cache_nr)
96 preallocate_hash(&istate->name_hash, istate->cache_nr);
97 for (nr = 0; nr < istate->cache_nr; nr++)
98 hash_index_entry(istate, istate->cache[nr]);
99 istate->name_hash_initialized = 1;
102 void add_name_hash(struct index_state *istate, struct cache_entry *ce)
104 ce->ce_flags &= ~CE_UNHASHED;
105 if (istate->name_hash_initialized)
106 hash_index_entry(istate, ce);
109 static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
111 if (len1 != len2)
112 return 0;
114 while (len1) {
115 unsigned char c1 = *name1++;
116 unsigned char c2 = *name2++;
117 len1--;
118 if (c1 != c2) {
119 c1 = toupper(c1);
120 c2 = toupper(c2);
121 if (c1 != c2)
122 return 0;
125 return 1;
128 static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase)
130 int len = ce_namelen(ce);
133 * Always do exact compare, even if we want a case-ignoring comparison;
134 * we do the quick exact one first, because it will be the common case.
136 if (len == namelen && !cache_name_compare(name, namelen, ce->name, len))
137 return 1;
139 if (!icase)
140 return 0;
143 * If the entry we're comparing is a filename (no trailing slash), then compare
144 * the lengths exactly.
146 if (name[namelen - 1] != '/')
147 return slow_same_name(name, namelen, ce->name, len);
150 * For a directory, we point to an arbitrary cache_entry filename. Just
151 * make sure the directory portion matches.
153 return slow_same_name(name, namelen, ce->name, namelen < len ? namelen : len);
156 struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
158 unsigned int hash = hash_name(name, namelen);
159 struct cache_entry *ce;
161 lazy_init_name_hash(istate);
162 ce = lookup_hash(hash, &istate->name_hash);
164 while (ce) {
165 if (!(ce->ce_flags & CE_UNHASHED)) {
166 if (same_name(ce, name, namelen, icase))
167 return ce;
169 if (icase && name[namelen - 1] == '/')
170 ce = ce->dir_next;
171 else
172 ce = ce->next;
176 * Might be a submodule. Despite submodules being directories,
177 * they are stored in the name hash without a closing slash.
178 * When ignore_case is 1, directories are stored in the name hash
179 * with their closing slash.
181 * The side effect of this storage technique is we have need to
182 * remove the slash from name and perform the lookup again without
183 * the slash. If a match is made, S_ISGITLINK(ce->mode) will be
184 * true.
186 if (icase && name[namelen - 1] == '/') {
187 ce = index_name_exists(istate, name, namelen - 1, icase);
188 if (ce && S_ISGITLINK(ce->ce_mode))
189 return ce;
191 return NULL;