commit: write cache-tree data when writing index anyway

In prepare_index(), we refresh the index, and then write it to disk if this changed the index data. After running hooks we re-read the index and compute the root tree sha1 with the cache-tree machinery. This gives us a mostly free opportunity to write up-to-date cache-tree data: we can compute it in prepare_index() immediately before writing the index to disk. If we do this, we were going to write the index anyway, and the later cache-tree update has no further work to do. If we don't do it, we don't do any extra work, though we still don't have have cache-tree data after the commit. The only case that suffers badly is when the pre-commit hook changes many trees in the index. I'm writing this off as highly unusual. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Dec 6, 2011 at 18:43 UTC 11c8a74a64a49f236d475514f723123a8928463d
2 files changed +3 -1
builtin/commit.c
+2
@@ -394,6 +394,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
394 fd = hold_locked_index(&index_lock, 1);
395 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
396 refresh_cache_or_die(refresh_flags);
397 + update_main_cache_tree(1);
398 if (write_cache(fd, active_cache, active_nr) ||
399 close_lock_file(&index_lock))
400 die(_("unable to write new_index file"));
@@ -414,6 +415,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
415 fd = hold_locked_index(&index_lock, 1);
416 refresh_cache_or_die(refresh_flags);
417 if (active_cache_changed) {
418 + update_main_cache_tree(1);
419 if (write_cache(fd, active_cache, active_nr) ||
420 commit_locked_index(&index_lock))
421 die(_("unable to write new_index file"));
t/t0090-cache-tree.sh
+1 -1
@@ -70,7 +70,7 @@ test_expect_success 'test-scrap-cache-tree works' '
70 test_no_cache_tree
71 '
72
73 -test_expect_failure 'second commit has cache-tree' '
73 +test_expect_success 'second commit has cache-tree' '
74 test_commit bar &&
75 test_shallow_cache_tree
76 '