Skip to content

Commit 0781b8a

Browse files
committed
add_file_to_index: skip rehashing if the cached stat already matches
An earlier commit 366bfcb broke git-add by moving read_cache() call down, because it wanted the directory walking code to grab paths that are already in the index. The change serves its purpose, but introduces a regression because the responsibility of avoiding unnecessary reindexing by matching the cached stat is shifted nowhere. This makes it the job of add_file_to_index() function. Signed-off-by: Junio C Hamano <[email protected]>
1 parent bf655fd commit 0781b8a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

read-cache.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static int index_name_pos_also_unmerged(struct index_state *istate,
380380

381381
int add_file_to_index(struct index_state *istate, const char *path, int verbose)
382382
{
383-
int size, namelen;
383+
int size, namelen, pos;
384384
struct stat st;
385385
struct cache_entry *ce;
386386

@@ -414,6 +414,15 @@ int add_file_to_index(struct index_state *istate, const char *path, int verbose)
414414
ce->ce_mode = ce_mode_from_stat(ent, st.st_mode);
415415
}
416416

417+
pos = index_name_pos(istate, ce->name, namelen);
418+
if (0 <= pos &&
419+
!ce_stage(istate->cache[pos]) &&
420+
!ie_modified(istate, istate->cache[pos], &st, 1)) {
421+
/* Nothing changed, really */
422+
free(ce);
423+
return 0;
424+
}
425+
417426
if (index_path(ce->sha1, path, &st, 1))
418427
die("unable to index file %s", path);
419428
if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE))

0 commit comments

Comments
 (0)