@matheusbsbs / matheus-bagliano / commits / d5f5d0a944
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Jul 6, 2010 at 21:53 UTC d5f5d0a9443c3892cdf9665ed8e0f9004706ed65
1 file changed +11 -5
builtin/commit.c
+11 -5
@@ -334,9 +334,13 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
334 if (!pathspec || !*pathspec) {
335 fd = hold_locked_index(&index_lock, 1);
336 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 + }
344 commit_style = COMMIT_AS_IS;
345 return get_index_file();
346 }
@@ -1067,9 +1071,11 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1071
1072 fd = hold_locked_index(&index_lock, 0);
1073 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))
1076 commit_locked_index(&index_lock);
1072 - rollback_lock_file(&index_lock);
1077 + else
1078 + rollback_lock_file(&index_lock);
1079 }
1080
1081 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;