🌐 AI搜索 & 代理 主页
blob: 6d9f23e932559c58c9ebf4679a6035889f726ed2 [file] [log] [blame]
Linus Torvalds96872bc2008-03-21 20:16:241/*
2 * name-hash.c
3 *
4 * Hashing names in the index state
5 *
6 * Copyright (C) 2008 Linus Torvalds
7 */
8#define NO_THE_INDEX_COMPATIBILITY_MACROS
9#include "cache.h"
10
Karsten Blees20926782013-02-27 23:57:4811struct dir_entry {
Karsten Bleese05881a2013-11-14 19:20:5812 struct hashmap_entry ent;
Karsten Blees20926782013-02-27 23:57:4813 struct dir_entry *parent;
Karsten Blees20926782013-02-27 23:57:4814 int nr;
15 unsigned int namelen;
David Turner41284eb2015-10-21 17:54:1116 char name[FLEX_ARRAY];
Karsten Blees20926782013-02-27 23:57:4817};
18
Karsten Bleese05881a2013-11-14 19:20:5819static int dir_entry_cmp(const struct dir_entry *e1,
20 const struct dir_entry *e2, const char *name)
21{
David Turner41284eb2015-10-21 17:54:1122 return e1->namelen != e2->namelen || strncasecmp(e1->name,
23 name ? name : e2->name, e1->namelen);
Karsten Bleese05881a2013-11-14 19:20:5824}
25
Karsten Blees20926782013-02-27 23:57:4826static struct dir_entry *find_dir_entry(struct index_state *istate,
27 const char *name, unsigned int namelen)
28{
Karsten Bleese05881a2013-11-14 19:20:5829 struct dir_entry key;
30 hashmap_entry_init(&key, memihash(name, namelen));
31 key.namelen = namelen;
32 return hashmap_get(&istate->dir_hash, &key, name);
Karsten Blees20926782013-02-27 23:57:4833}
34
35static struct dir_entry *hash_dir_entry(struct index_state *istate,
36 struct cache_entry *ce, int namelen)
Joshua Jensen5102c612010-10-03 09:56:4337{
38 /*
39 * Throw each directory component in the hash for quick lookup
Eric Sunshined28eec22013-09-17 07:06:1640 * during a git status. Directory components are stored without their
Joshua Jensen5102c612010-10-03 09:56:4341 * closing slash. Despite submodules being a directory, they never
Eric Sunshined28eec22013-09-17 07:06:1642 * reach this point, because they are stored
Karsten Blees20926782013-02-27 23:57:4843 * in index_state.name_hash (as ordinary cache_entries).
Joshua Jensen5102c612010-10-03 09:56:4344 */
Karsten Blees20926782013-02-27 23:57:4845 struct dir_entry *dir;
Joshua Jensen5102c612010-10-03 09:56:4346
Karsten Blees20926782013-02-27 23:57:4847 /* get length of parent directory */
48 while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1]))
49 namelen--;
50 if (namelen <= 0)
51 return NULL;
Eric Sunshined28eec22013-09-17 07:06:1652 namelen--;
Karsten Blees20926782013-02-27 23:57:4853
54 /* lookup existing entry for that directory */
55 dir = find_dir_entry(istate, ce->name, namelen);
56 if (!dir) {
57 /* not found, create it and add to hash table */
Jeff King96ffc062016-02-22 22:44:3258 FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
Karsten Bleese05881a2013-11-14 19:20:5859 hashmap_entry_init(dir, memihash(ce->name, namelen));
Karsten Blees20926782013-02-27 23:57:4860 dir->namelen = namelen;
Karsten Bleese05881a2013-11-14 19:20:5861 hashmap_add(&istate->dir_hash, dir);
Karsten Blees20926782013-02-27 23:57:4862
63 /* recursively add missing parent directories */
Eric Sunshined28eec22013-09-17 07:06:1664 dir->parent = hash_dir_entry(istate, ce, namelen);
Joshua Jensen5102c612010-10-03 09:56:4365 }
Karsten Blees20926782013-02-27 23:57:4866 return dir;
67}
68
69static void add_dir_entry(struct index_state *istate, struct cache_entry *ce)
70{
71 /* Add reference to the directory entry (and parents if 0). */
72 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
73 while (dir && !(dir->nr++))
74 dir = dir->parent;
75}
76
77static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce)
78{
79 /*
Karsten Blees1c8cca12013-11-14 19:21:2680 * Release reference to the directory entry. If 0, remove and continue
81 * with parent directory.
Karsten Blees20926782013-02-27 23:57:4882 */
83 struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce));
Karsten Blees1c8cca12013-11-14 19:21:2684 while (dir && !(--dir->nr)) {
85 struct dir_entry *parent = dir->parent;
86 hashmap_remove(&istate->dir_hash, dir, NULL);
87 free(dir);
88 dir = parent;
89 }
Joshua Jensen5102c612010-10-03 09:56:4390}
91
Linus Torvalds96872bc2008-03-21 20:16:2492static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
93{
Linus Torvalds96872bc2008-03-21 20:16:2494 if (ce->ce_flags & CE_HASHED)
95 return;
96 ce->ce_flags |= CE_HASHED;
Karsten Blees8b013782013-11-14 19:21:5897 hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce)));
98 hashmap_add(&istate->name_hash, ce);
Joshua Jensen5102c612010-10-03 09:56:4399
Karsten Blees419a5972013-11-14 19:22:27100 if (ignore_case)
Karsten Blees20926782013-02-27 23:57:48101 add_dir_entry(istate, ce);
Linus Torvalds96872bc2008-03-21 20:16:24102}
103
Karsten Blees419a5972013-11-14 19:22:27104static int cache_entry_cmp(const struct cache_entry *ce1,
105 const struct cache_entry *ce2, const void *remove)
106{
107 /*
108 * For remove_name_hash, find the exact entry (pointer equality); for
Eric Sunshine7b359ea2014-01-02 21:57:12109 * index_file_exists, find all entries with matching hash code and
Karsten Blees419a5972013-11-14 19:22:27110 * decide whether the entry matches in same_name.
111 */
112 return remove ? !(ce1 == ce2) : 0;
113}
114
Linus Torvalds96872bc2008-03-21 20:16:24115static void lazy_init_name_hash(struct index_state *istate)
116{
117 int nr;
118
119 if (istate->name_hash_initialized)
120 return;
Karsten Blees419a5972013-11-14 19:22:27121 hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
122 istate->cache_nr);
Karsten Bleese05881a2013-11-14 19:20:58123 hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp, 0);
Linus Torvalds96872bc2008-03-21 20:16:24124 for (nr = 0; nr < istate->cache_nr; nr++)
125 hash_index_entry(istate, istate->cache[nr]);
126 istate->name_hash_initialized = 1;
127}
128
129void add_name_hash(struct index_state *istate, struct cache_entry *ce)
130{
Linus Torvalds96872bc2008-03-21 20:16:24131 if (istate->name_hash_initialized)
132 hash_index_entry(istate, ce);
133}
134
Karsten Blees20926782013-02-27 23:57:48135void remove_name_hash(struct index_state *istate, struct cache_entry *ce)
136{
Karsten Blees419a5972013-11-14 19:22:27137 if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED))
138 return;
139 ce->ce_flags &= ~CE_HASHED;
140 hashmap_remove(&istate->name_hash, ce, ce);
Karsten Blees20926782013-02-27 23:57:48141
Karsten Blees419a5972013-11-14 19:22:27142 if (ignore_case)
143 remove_dir_entry(istate, ce);
Karsten Blees20926782013-02-27 23:57:48144}
145
Linus Torvaldscd2fef52008-03-21 22:55:19146static int slow_same_name(const char *name1, int len1, const char *name2, int len2)
147{
148 if (len1 != len2)
149 return 0;
150
151 while (len1) {
152 unsigned char c1 = *name1++;
153 unsigned char c2 = *name2++;
154 len1--;
155 if (c1 != c2) {
156 c1 = toupper(c1);
157 c2 = toupper(c2);
158 if (c1 != c2)
159 return 0;
160 }
161 }
162 return 1;
163}
164
165static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase)
166{
167 int len = ce_namelen(ce);
168
169 /*
170 * Always do exact compare, even if we want a case-ignoring comparison;
171 * we do the quick exact one first, because it will be the common case.
172 */
Jeremiah Mahlerbe99ec92014-06-20 02:06:43173 if (len == namelen && !memcmp(name, ce->name, len))
Linus Torvaldscd2fef52008-03-21 22:55:19174 return 1;
175
Joshua Jensen5102c612010-10-03 09:56:43176 if (!icase)
177 return 0;
178
Karsten Blees20926782013-02-27 23:57:48179 return slow_same_name(name, namelen, ce->name, len);
Linus Torvaldscd2fef52008-03-21 22:55:19180}
181
David Turner41284eb2015-10-21 17:54:11182int index_dir_exists(struct index_state *istate, const char *name, int namelen)
Eric Sunshinedb5360f2013-09-17 07:06:14183{
Eric Sunshinedb5360f2013-09-17 07:06:14184 struct dir_entry *dir;
185
186 lazy_init_name_hash(istate);
187 dir = find_dir_entry(istate, name, namelen);
David Turner41284eb2015-10-21 17:54:11188 return dir && dir->nr;
189}
Eric Sunshinedb5360f2013-09-17 07:06:14190
David Turner41284eb2015-10-21 17:54:11191void adjust_dirname_case(struct index_state *istate, char *name)
192{
193 const char *startPtr = name;
194 const char *ptr = startPtr;
Eric Sunshinedb5360f2013-09-17 07:06:14195
David Turner41284eb2015-10-21 17:54:11196 lazy_init_name_hash(istate);
197 while (*ptr) {
198 while (*ptr && *ptr != '/')
199 ptr++;
200
201 if (*ptr == '/') {
202 struct dir_entry *dir;
203
204 ptr++;
205 dir = find_dir_entry(istate, name, ptr - name + 1);
206 if (dir) {
207 memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr);
208 startPtr = ptr;
209 }
210 }
211 }
Eric Sunshinedb5360f2013-09-17 07:06:14212}
213
214struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
Linus Torvalds96872bc2008-03-21 20:16:24215{
Linus Torvalds96872bc2008-03-21 20:16:24216 struct cache_entry *ce;
217
218 lazy_init_name_hash(istate);
Linus Torvalds96872bc2008-03-21 20:16:24219
Karsten Bleesab73a9d2014-07-02 22:22:11220 ce = hashmap_get_from_hash(&istate->name_hash,
221 memihash(name, namelen), NULL);
Linus Torvalds96872bc2008-03-21 20:16:24222 while (ce) {
Karsten Blees419a5972013-11-14 19:22:27223 if (same_name(ce, name, namelen, icase))
224 return ce;
Karsten Blees8b013782013-11-14 19:21:58225 ce = hashmap_get_next(&istate->name_hash, ce);
Linus Torvalds96872bc2008-03-21 20:16:24226 }
Linus Torvaldsdf292c72008-03-21 22:53:00227 return NULL;
Linus Torvalds96872bc2008-03-21 20:16:24228}
Karsten Blees20926782013-02-27 23:57:48229
Karsten Blees20926782013-02-27 23:57:48230void free_name_hash(struct index_state *istate)
231{
232 if (!istate->name_hash_initialized)
233 return;
234 istate->name_hash_initialized = 0;
Karsten Blees20926782013-02-27 23:57:48235
Karsten Blees8b013782013-11-14 19:21:58236 hashmap_free(&istate->name_hash, 0);
Karsten Bleese05881a2013-11-14 19:20:58237 hashmap_free(&istate->dir_hash, 1);
Karsten Blees20926782013-02-27 23:57:48238}