4 * Hashing names in the index state
6 * Copyright (C) 2008 Linus Torvalds
8 #define NO_THE_INDEX_COMPATIBILITY_MACROS
12 struct hashmap_entry ent
;
13 struct dir_entry
*parent
;
14 struct cache_entry
*ce
;
19 static int dir_entry_cmp(const struct dir_entry
*e1
,
20 const struct dir_entry
*e2
, const char *name
)
22 return e1
->namelen
!= e2
->namelen
|| strncasecmp(e1
->ce
->name
,
23 name
? name
: e2
->ce
->name
, e1
->namelen
);
26 static struct dir_entry
*find_dir_entry(struct index_state
*istate
,
27 const char *name
, unsigned int namelen
)
30 hashmap_entry_init(&key
, memihash(name
, namelen
));
31 key
.namelen
= namelen
;
32 return hashmap_get(&istate
->dir_hash
, &key
, name
);
35 static struct dir_entry
*hash_dir_entry(struct index_state
*istate
,
36 struct cache_entry
*ce
, int namelen
)
39 * Throw each directory component in the hash for quick lookup
40 * during a git status. Directory components are stored without their
41 * closing slash. Despite submodules being a directory, they never
42 * reach this point, because they are stored
43 * in index_state.name_hash (as ordinary cache_entries).
45 * Note that the cache_entry stored with the dir_entry merely
46 * supplies the name of the directory (up to dir_entry.namelen). We
47 * track the number of 'active' files in a directory in dir_entry.nr,
48 * so we can tell if the directory is still relevant, e.g. for git
49 * status. However, if cache_entries are removed, we cannot pinpoint
50 * an exact cache_entry that's still active. It is very possible that
51 * multiple dir_entries point to the same cache_entry.
53 struct dir_entry
*dir
;
55 /* get length of parent directory */
56 while (namelen
> 0 && !is_dir_sep(ce
->name
[namelen
- 1]))
62 /* lookup existing entry for that directory */
63 dir
= find_dir_entry(istate
, ce
->name
, namelen
);
65 /* not found, create it and add to hash table */
66 dir
= xcalloc(1, sizeof(struct dir_entry
));
67 hashmap_entry_init(dir
, memihash(ce
->name
, namelen
));
68 dir
->namelen
= namelen
;
70 hashmap_add(&istate
->dir_hash
, dir
);
72 /* recursively add missing parent directories */
73 dir
->parent
= hash_dir_entry(istate
, ce
, namelen
);
78 static void add_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
80 /* Add reference to the directory entry (and parents if 0). */
81 struct dir_entry
*dir
= hash_dir_entry(istate
, ce
, ce_namelen(ce
));
82 while (dir
&& !(dir
->nr
++))
86 static void remove_dir_entry(struct index_state
*istate
, struct cache_entry
*ce
)
89 * Release reference to the directory entry. If 0, remove and continue
90 * with parent directory.
92 struct dir_entry
*dir
= hash_dir_entry(istate
, ce
, ce_namelen(ce
));
93 while (dir
&& !(--dir
->nr
)) {
94 struct dir_entry
*parent
= dir
->parent
;
95 hashmap_remove(&istate
->dir_hash
, dir
, NULL
);
101 static void hash_index_entry(struct index_state
*istate
, struct cache_entry
*ce
)
103 if (ce
->ce_flags
& CE_HASHED
)
105 ce
->ce_flags
|= CE_HASHED
;
106 hashmap_entry_init(ce
, memihash(ce
->name
, ce_namelen(ce
)));
107 hashmap_add(&istate
->name_hash
, ce
);
110 add_dir_entry(istate
, ce
);
113 static int cache_entry_cmp(const struct cache_entry
*ce1
,
114 const struct cache_entry
*ce2
, const void *remove
)
117 * For remove_name_hash, find the exact entry (pointer equality); for
118 * index_file_exists, find all entries with matching hash code and
119 * decide whether the entry matches in same_name.
121 return remove
? !(ce1
== ce2
) : 0;
124 static void lazy_init_name_hash(struct index_state
*istate
)
128 if (istate
->name_hash_initialized
)
130 hashmap_init(&istate
->name_hash
, (hashmap_cmp_fn
) cache_entry_cmp
,
132 hashmap_init(&istate
->dir_hash
, (hashmap_cmp_fn
) dir_entry_cmp
, 0);
133 for (nr
= 0; nr
< istate
->cache_nr
; nr
++)
134 hash_index_entry(istate
, istate
->cache
[nr
]);
135 istate
->name_hash_initialized
= 1;
138 void add_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
140 if (istate
->name_hash_initialized
)
141 hash_index_entry(istate
, ce
);
144 void remove_name_hash(struct index_state
*istate
, struct cache_entry
*ce
)
146 if (!istate
->name_hash_initialized
|| !(ce
->ce_flags
& CE_HASHED
))
148 ce
->ce_flags
&= ~CE_HASHED
;
149 hashmap_remove(&istate
->name_hash
, ce
, ce
);
152 remove_dir_entry(istate
, ce
);
155 static int slow_same_name(const char *name1
, int len1
, const char *name2
, int len2
)
161 unsigned char c1
= *name1
++;
162 unsigned char c2
= *name2
++;
174 static int same_name(const struct cache_entry
*ce
, const char *name
, int namelen
, int icase
)
176 int len
= ce_namelen(ce
);
179 * Always do exact compare, even if we want a case-ignoring comparison;
180 * we do the quick exact one first, because it will be the common case.
182 if (len
== namelen
&& !memcmp(name
, ce
->name
, len
))
188 return slow_same_name(name
, namelen
, ce
->name
, len
);
191 struct cache_entry
*index_dir_exists(struct index_state
*istate
, const char *name
, int namelen
)
193 struct cache_entry
*ce
;
194 struct dir_entry
*dir
;
196 lazy_init_name_hash(istate
);
197 dir
= find_dir_entry(istate
, name
, namelen
);
202 * It might be a submodule. Unlike plain directories, which are stored
203 * in the dir-hash, submodules are stored in the name-hash, so check
206 ce
= index_file_exists(istate
, name
, namelen
, 1);
207 if (ce
&& S_ISGITLINK(ce
->ce_mode
))
213 struct cache_entry
*index_file_exists(struct index_state
*istate
, const char *name
, int namelen
, int icase
)
215 struct cache_entry
*ce
;
216 struct hashmap_entry key
;
218 lazy_init_name_hash(istate
);
220 hashmap_entry_init(&key
, memihash(name
, namelen
));
221 ce
= hashmap_get(&istate
->name_hash
, &key
, NULL
);
223 if (same_name(ce
, name
, namelen
, icase
))
225 ce
= hashmap_get_next(&istate
->name_hash
, ce
);
230 void free_name_hash(struct index_state
*istate
)
232 if (!istate
->name_hash_initialized
)
234 istate
->name_hash_initialized
= 0;
236 hashmap_free(&istate
->name_hash
, 0);
237 hashmap_free(&istate
->dir_hash
, 1);