4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
8 #define NO_THE_INDEX_COMPATIBILITY_MACROS
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;
28 unsigned char c
= *name
++;
36 struct dir_entry
*next
;
37 struct dir_entry
*parent
;
38 struct cache_entry
*ce
;
43 static struct dir_entry
*find_dir_entry(struct index_state
*istate
,
44 const char *name
, unsigned int namelen
)
46 unsigned int hash
= hash_name(name
, namelen
);
47 struct dir_entry
*dir
;
49 for (dir
= lookup_hash(hash
, &istate
->dir_hash
); dir
; dir
= dir
->next
)
50 if (dir
->namelen
== namelen
&&
51 !strncasecmp(dir
->ce
->name
, name
, namelen
))
56 static struct dir_entry
*hash_dir_entry(struct index_state
*istate
,
57 struct cache_entry
*ce
, int namelen
)
60 * Throw each directory component in the hash for quick lookup
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
64 * in index_state.name_hash (as ordinary cache_entries).
66 * Note that the cache_entry stored with the dir_entry merely
67 * supplies the name of the directory (up to dir_entry.namelen). We
68 * track the number of 'active' files in a directory in dir_entry.nr,
69 * so we can tell if the directory is still relevant, e.g. for git
70 * status. However, if cache_entries are removed, we cannot pinpoint
71 * an exact cache_entry that's still active. It is very possible that
72 * multiple dir_entries point to the same cache_entry.
74 struct dir_entry
*dir
;
76 /* get length of parent directory */
77 while (namelen
> 0 && !is_dir_sep(ce
->name
[namelen
- 1]))
83 /* lookup existing entry for that directory */
84 dir
= find_dir_entry(istate
, ce
->name
, namelen
);
86 /* not found, create it and add to hash table */
88 unsigned int hash
= hash_name(ce
->name
, namelen
);
90 dir
= xcalloc(1, sizeof(struct dir_entry
));
91 dir
->namelen
= namelen
;
94 pdir
= insert_hash(hash
, dir
, &istate
->dir_hash
);
100 /* recursively add missing parent directories */
101 dir
->parent
= hash_dir_entry(istate
, ce
, namelen
);
106 static void add_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
108 /* Add reference to the directory entry (and parents if 0). */
109 struct dir_entry
*dir
= hash_dir_entry(istate
, ce
, ce_namelen(ce
));
110 while (dir
&& !(dir
->nr
++))
114 static void remove_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
117 * Release reference to the directory entry (and parents if 0).
119 * Note: we do not remove / free the entry because there's no
120 * hash.[ch]::remove_hash and dir->next may point to other entries
121 * that are still valid, so we must not free the memory.
123 struct dir_entry
*dir
= hash_dir_entry(istate
, ce
, ce_namelen(ce
));
124 while (dir
&& dir
->nr
&& !(--dir
->nr
))
128 static void hash_index_entry(struct index_state
*istate
, struct cache_entry
*ce
)
133 if (ce
->ce_flags
& CE_HASHED
)
135 ce
->ce_flags
|= CE_HASHED
;
137 hash
= hash_name(ce
->name
, ce_namelen(ce
));
138 pos
= insert_hash(hash
, ce
, &istate
->name_hash
);
144 if (ignore_case
&& !(ce
->ce_flags
& CE_UNHASHED
))
145 add_dir_entry(istate
, ce
);
148 static void lazy_init_name_hash(struct index_state
*istate
)
152 if (istate
->name_hash_initialized
)
154 if (istate
->cache_nr
)
155 preallocate_hash(&istate
->name_hash
, istate
->cache_nr
);
156 for (nr
= 0; nr
< istate
->cache_nr
; nr
++)
157 hash_index_entry(istate
, istate
->cache
[nr
]);
158 istate
->name_hash_initialized
= 1;
161 void add_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
163 /* if already hashed, add reference to directory entries */
164 if (ignore_case
&& (ce
->ce_flags
& CE_STATE_MASK
) == CE_STATE_MASK
)
165 add_dir_entry(istate
, ce
);
167 ce
->ce_flags
&= ~CE_UNHASHED
;
168 if (istate
->name_hash_initialized
)
169 hash_index_entry(istate
, ce
);
173 * We don't actually *remove* it, we can just mark it invalid so that
174 * we won't find it in lookups.
176 * Not only would we have to search the lists (simple enough), but
177 * we'd also have to rehash other hash buckets in case this makes the
178 * hash bucket empty (common). So it's much better to just mark
181 void remove_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
183 /* if already hashed, release reference to directory entries */
184 if (ignore_case
&& (ce
->ce_flags
& CE_STATE_MASK
) == CE_HASHED
)
185 remove_dir_entry(istate
, ce
);
187 ce
->ce_flags
|= CE_UNHASHED
;
190 static int slow_same_name(const char *name1
, int len1
, const char *name2
, int len2
)
196 unsigned char c1
= *name1
++;
197 unsigned char c2
= *name2
++;
209 static int same_name(const struct cache_entry
*ce
, const char *name
, int namelen
, int icase
)
211 int len
= ce_namelen(ce
);
214 * Always do exact compare, even if we want a case-ignoring comparison;
215 * we do the quick exact one first, because it will be the common case.
217 if (len
== namelen
&& !cache_name_compare(name
, namelen
, ce
->name
, len
))
223 return slow_same_name(name
, namelen
, ce
->name
, len
);
226 struct cache_entry
*index_dir_exists(struct index_state
*istate
, const char *name
, int namelen
)
228 struct cache_entry
*ce
;
229 struct dir_entry
*dir
;
231 lazy_init_name_hash(istate
);
232 dir
= find_dir_entry(istate
, name
, namelen
);
237 * It might be a submodule. Unlike plain directories, which are stored
238 * in the dir-hash, submodules are stored in the name-hash, so check
241 ce
= index_file_exists(istate
, name
, namelen
, 1);
242 if (ce
&& S_ISGITLINK(ce
->ce_mode
))
248 struct cache_entry
*index_file_exists(struct index_state
*istate
, const char *name
, int namelen
, int icase
)
250 unsigned int hash
= hash_name(name
, namelen
);
251 struct cache_entry
*ce
;
253 lazy_init_name_hash(istate
);
254 ce
= lookup_hash(hash
, &istate
->name_hash
);
257 if (!(ce
->ce_flags
& CE_UNHASHED
)) {
258 if (same_name(ce
, name
, namelen
, icase
))
266 struct cache_entry
*index_name_exists(struct index_state
*istate
, const char *name
, int namelen
, int icase
)
268 if (namelen
> 0 && name
[namelen
- 1] == '/')
269 return index_dir_exists(istate
, name
, namelen
- 1);
270 return index_file_exists(istate
, name
, namelen
, icase
);
273 static int free_dir_entry(void *entry
, void *unused
)
275 struct dir_entry
*dir
= entry
;
277 struct dir_entry
*next
= dir
->next
;
284 void free_name_hash(struct index_state
*istate
)
286 if (!istate
->name_hash_initialized
)
288 istate
->name_hash_initialized
= 0;
290 /* free directory entries */
291 for_each_hash(&istate
->dir_hash
, free_dir_entry
, NULL
);
293 free_hash(&istate
->name_hash
);
294 free_hash(&istate
->dir_hash
);