Skip to content

Commit d5f5d0a

Browse files
committed
do not write out index when status does not have to
Some codepaths, such as "git status" and "git commit --dry-run", tried to opportunisticly refresh the index and write the result out. But they did so without checking if there was actually any change that needs to be written out. Noticed by Jeff King and Daniel at Rutgers.edu Signed-off-by: Junio C Hamano <[email protected]>
1 parent b4cf0f1 commit d5f5d0a

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

builtin/commit.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,13 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
334334
if (!pathspec || !*pathspec) {
335335
fd = hold_locked_index(&index_lock, 1);
336336
refresh_cache_or_die(refresh_flags);
337-
if (write_cache(fd, active_cache, active_nr) ||
338-
commit_locked_index(&index_lock))
339-
die("unable to write new_index file");
337+
if (active_cache_changed) {
338+
if (write_cache(fd, active_cache, active_nr) ||
339+
commit_locked_index(&index_lock))
340+
die("unable to write new_index file");
341+
} else {
342+
rollback_lock_file(&index_lock);
343+
}
340344
commit_style = COMMIT_AS_IS;
341345
return get_index_file();
342346
}
@@ -1067,9 +1071,11 @@ int cmd_status(int argc, const char **argv, const char *prefix)
10671071

10681072
fd = hold_locked_index(&index_lock, 0);
10691073
if (0 <= fd) {
1070-
if (!write_cache(fd, active_cache, active_nr))
1074+
if (active_cache_changed &&
1075+
!write_cache(fd, active_cache, active_nr))
10711076
commit_locked_index(&index_lock);
1072-
rollback_lock_file(&index_lock);
1077+
else
1078+
rollback_lock_file(&index_lock);
10731079
}
10741080

10751081
s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;

0 commit comments

Comments
 (0)