read-cache: force_verify_index_checksum

Teach git to skip verification of the SHA1-1 checksum at the end of the index file in verify_hdr() which is called from read_index() unless the "force_verify_index_checksum" global variable is set. Teach fsck to force this verification. The checksum verification is for detecting disk corruption, and for small projects, the time it takes to compute SHA-1 is not that significant, but for gigantic repositories this calculation adds significant time to every command. These effect can be seen using t/perf/p0002-read-cache.sh: Test HEAD~1 HEAD -------------------------------------------------------------------------------------- 0002.1: read_cache/discard_cache 1000 times 0.66(0.44+0.20) 0.30(0.27+0.02) -54.5% Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Apr 14, 2017 at 20:32 UTC a33fc72fe911fdb8e284c94e08e8f1dafe4d3187
4 files changed +23
builtin/fsck.c
+1
@@ -771,6 +771,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
771 }
772
773 if (keep_cache_objects) {
774 + verify_index_checksum = 1;
775 read_cache();
776 for (i = 0; i < active_nr; i++) {
777 unsigned int mode;
cache.h
+2
@@ -705,6 +705,8 @@ extern void update_index_if_able(struct index_state *, struct lock_file *);
705 extern int hold_locked_index(struct lock_file *, int);
706 extern void set_alternate_index_output(const char *);
707
708 +extern int verify_index_checksum;
709 +
710 /* Environment bits from configuration mechanism */
711 extern int trust_executable_bit;
712 extern int trust_ctime;
read-cache.c
+7
@@ -1371,6 +1371,9 @@ struct ondisk_cache_entry_extended {
1371 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
1372 ondisk_cache_entry_size(ce_namelen(ce)))
1373
1374 +/* Allow fsck to force verification of the index checksum. */
1375 +int verify_index_checksum;
1376 +
1377 static int verify_hdr(struct cache_header *hdr, unsigned long size)
1378 {
1379 git_SHA_CTX c;
@@ -1382,6 +1385,10 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
1385 hdr_version = ntohl(hdr->hdr_version);
1386 if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
1387 return error("bad index version %d", hdr_version);
1388 +
1389 + if (!verify_index_checksum)
1390 + return 0;
1391 +
1392 git_SHA1_Init(&c);
1393 git_SHA1_Update(&c, hdr, size - 20);
1394 git_SHA1_Final(sha1, &c);
t/t1450-fsck.sh
+13
@@ -689,4 +689,17 @@ test_expect_success 'bogus head does not fallback to all heads' '
689 ! grep $blob out
690 '
691
692 +test_expect_success 'detect corrupt index file in fsck' '
693 + cp .git/index .git/index.backup &&
694 + test_when_finished "mv .git/index.backup .git/index" &&
695 + echo zzzzzzzz >zzzzzzzz &&
696 + git add zzzzzzzz &&
697 + sed -e "s/zzzzzzzz/yyyyyyyy/" .git/index >.git/index.yyy &&
698 + mv .git/index.yyy .git/index &&
699 + # Confirm that fsck detects invalid checksum
700 + test_must_fail git fsck --cache &&
701 + # Confirm that status no longer complains about invalid checksum
702 + git status
703 +'
704 +
705 test_done