@@ -58,9 +58,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
5858{
5959 /*
6060 * Throw each directory component in the hash for quick lookup
61- * during a git status. Directory components are stored with their
61+ * during a git status. Directory components are stored without their
6262 * closing slash. Despite submodules being a directory, they never
63- * reach this point, because they are stored without a closing slash
63+ * reach this point, because they are stored
6464 * in index_state.name_hash (as ordinary cache_entries).
6565 *
6666 * Note that the cache_entry stored with the dir_entry merely
@@ -78,6 +78,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
7878 namelen -- ;
7979 if (namelen <= 0 )
8080 return NULL ;
81+ namelen -- ;
8182
8283 /* lookup existing entry for that directory */
8384 dir = find_dir_entry (istate , ce -> name , namelen );
@@ -97,7 +98,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
9798 }
9899
99100 /* recursively add missing parent directories */
100- dir -> parent = hash_dir_entry (istate , ce , namelen - 1 );
101+ dir -> parent = hash_dir_entry (istate , ce , namelen );
101102 }
102103 return dir ;
103104}
@@ -222,7 +223,29 @@ static int same_name(const struct cache_entry *ce, const char *name, int namelen
222223 return slow_same_name (name , namelen , ce -> name , len );
223224}
224225
225- struct cache_entry * index_name_exists (struct index_state * istate , const char * name , int namelen , int icase )
226+ struct cache_entry * index_dir_exists (struct index_state * istate , const char * name , int namelen )
227+ {
228+ struct cache_entry * ce ;
229+ struct dir_entry * dir ;
230+
231+ lazy_init_name_hash (istate );
232+ dir = find_dir_entry (istate , name , namelen );
233+ if (dir && dir -> nr )
234+ return dir -> ce ;
235+
236+ /*
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
239+ * there, as well.
240+ */
241+ ce = index_file_exists (istate , name , namelen , 1 );
242+ if (ce && S_ISGITLINK (ce -> ce_mode ))
243+ return ce ;
244+
245+ return NULL ;
246+ }
247+
248+ struct cache_entry * index_file_exists (struct index_state * istate , const char * name , int namelen , int icase )
226249{
227250 unsigned int hash = hash_name (name , namelen );
228251 struct cache_entry * ce ;
@@ -237,32 +260,16 @@ struct cache_entry *index_name_exists(struct index_state *istate, const char *na
237260 }
238261 ce = ce -> next ;
239262 }
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- }
263263 return NULL ;
264264}
265265
266+ struct cache_entry * index_name_exists (struct index_state * istate , const char * name , int namelen , int icase )
267+ {
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 );
271+ }
272+
266273static int free_dir_entry (void * entry , void * unused )
267274{
268275 struct dir_entry * dir = entry ;
0 commit comments