name-hash: refactor polymorphic index_name_exists()

Depending upon the absence or presence of a trailing '/' on the incoming pathname, index_name_exists() checks either if a file is present in the index or if a directory is represented within the index. Each caller explicitly chooses the mode of operation by adding or removing a trailing '/' before invoking index_name_exists(). Since these two modes of operations are disjoint and have no code in common (one searches index_state.name_hash; the other dir_hash), they can be represented more naturally as distinct functions: one to search for a file, and one for a directory. Splitting index searching into two functions relieves callers of the artificial burden of having to add or remove a slash to select the mode of operation; instead they just call the desired function. A subsequent patch will take advantage of this benefit in order to eliminate the requirement that the incoming pathname for a directory search must have a trailing slash. (In order to avoid disturbing in-flight topics, index_name_exists() is retained as a thin wrapper dispatching either to index_dir_exists() or index_file_exists().) Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Sep 17, 2013 at 03:06 UTC db5360f3f4966c4586d072bd1440880773185b81
2 files changed +34 -24
cache.h
+4
@@ -314,6 +314,8 @@ extern void free_name_hash(struct index_state *istate);
314 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
315 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
316 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
317 +#define cache_dir_exists(name, namelen) index_dir_exists(&the_index, (name), (namelen))
318 +#define cache_file_exists(name, namelen, igncase) index_file_exists(&the_index, (name), (namelen), (igncase))
319 #define cache_name_exists(name, namelen, igncase) index_name_exists(&the_index, (name), (namelen), (igncase))
320 #define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
321 #define resolve_undo_clear() resolve_undo_clear_index(&the_index)
@@ -463,6 +465,8 @@ extern int write_index(struct index_state *, int newfd);
465 extern int discard_index(struct index_state *);
466 extern int unmerged_index(const struct index_state *);
467 extern int verify_path(const char *path);
468 +extern struct cache_entry *index_dir_exists(struct index_state *istate, const char *name, int namelen);
469 +extern struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int igncase);
470 extern struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int igncase);
471 extern int index_name_pos(const struct index_state *, const char *name, int namelen);
472 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
name-hash.c
+30 -24
@@ -222,7 +222,29 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
222 return slow_same_name(name, namelen, ce->name, len);
223 }
224
225 -struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
225 +struct cache_entry *index_dir_exists(struct index_state *istate, const char *name, int namelen)
226 +{
227 + struct cache_entry *ce;
228 + struct dir_entry *dir;
229 +
230 + lazy_init_name_hash(istate);
231 + dir = find_dir_entry(istate, name, namelen);
232 + if (dir && dir->nr)
233 + return dir->ce;
234 +
235 + /*
236 + * It might be a submodule. Unlike plain directories, which are stored
237 + * in the dir-hash, submodules are stored in the name-hash, so check
238 + * there, as well.
239 + */
240 + ce = index_file_exists(istate, name, namelen - 1, 1);
241 + if (ce && S_ISGITLINK(ce->ce_mode))
242 + return ce;
243 +
244 + return NULL;
245 +}
246 +
247 +struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
248 {
249 unsigned int hash = hash_name(name, namelen);
250 struct cache_entry *ce;
@@ -237,32 +259,16 @@ struct cache_entry *index_name_exists(struct index_state *istate, const char *na
259 }
260 ce = ce->next;
261 }
240 -
241 - /*
242 - * When looking for a directory (trailing '/'), it might be a
243 - * submodule or a directory. Despite submodules being directories,
244 - * they are stored in the name hash without a closing slash.
245 - * When ignore_case is 1, directories are stored in a separate hash
246 - * table *with* their closing slash.
247 - *
248 - * The side effect of this storage technique is we have need to
249 - * lookup the directory in a separate hash table, and if not found
250 - * remove the slash from name and perform the lookup again without
251 - * the slash. If a match is made, S_ISGITLINK(ce->mode) will be
252 - * true.
253 - */
254 - if (icase && name[namelen - 1] == '/') {
255 - struct dir_entry *dir = find_dir_entry(istate, name, namelen);
256 - if (dir && dir->nr)
257 - return dir->ce;
258 -
259 - ce = index_name_exists(istate, name, namelen - 1, icase);
260 - if (ce && S_ISGITLINK(ce->ce_mode))
261 - return ce;
262 - }
262 return NULL;
263 }
264
265 +struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int icase)
266 +{
267 + if (namelen > 0 && name[namelen - 1] == '/')
268 + return index_dir_exists(istate, name, namelen);
269 + return index_file_exists(istate, name, namelen, icase);
270 +}
271 +
272 static int free_dir_entry(void *entry, void *unused)
273 {
274 struct dir_entry *dir = entry;