Skip to content

Commit a33fc72

Browse files
jeffhostetlergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e1104a5 commit a33fc72

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

builtin/fsck.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
771771
}
772772

773773
if (keep_cache_objects) {
774+
verify_index_checksum = 1;
774775
read_cache();
775776
for (i = 0; i < active_nr; i++) {
776777
unsigned int mode;

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ extern void update_index_if_able(struct index_state *, struct lock_file *);
705705
extern int hold_locked_index(struct lock_file *, int);
706706
extern void set_alternate_index_output(const char *);
707707

708+
extern int verify_index_checksum;
709+
708710
/* Environment bits from configuration mechanism */
709711
extern int trust_executable_bit;
710712
extern int trust_ctime;

read-cache.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,9 @@ struct ondisk_cache_entry_extended {
13711371
ondisk_cache_entry_extended_size(ce_namelen(ce)) : \
13721372
ondisk_cache_entry_size(ce_namelen(ce)))
13731373

1374+
/* Allow fsck to force verification of the index checksum. */
1375+
int verify_index_checksum;
1376+
13741377
static int verify_hdr(struct cache_header *hdr, unsigned long size)
13751378
{
13761379
git_SHA_CTX c;
@@ -1382,6 +1385,10 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size)
13821385
hdr_version = ntohl(hdr->hdr_version);
13831386
if (hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)
13841387
return error("bad index version %d", hdr_version);
1388+
1389+
if (!verify_index_checksum)
1390+
return 0;
1391+
13851392
git_SHA1_Init(&c);
13861393
git_SHA1_Update(&c, hdr, size - 20);
13871394
git_SHA1_Final(sha1, &c);

t/t1450-fsck.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,4 +689,17 @@ test_expect_success 'bogus head does not fallback to all heads' '
689689
! grep $blob out
690690
'
691691

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+
692705
test_done

0 commit comments

Comments
 (0)