blob: 431a603ca0e61d7085f28333c6a28c120ea45828 [file] [log] [blame]
Taylor Blaue37d0b82021-01-25 18:37:26 -05001#!/bin/sh
2
3test_description='on-disk reverse index'
Taylor Blaub77919e2023-04-12 18:20:21 -04004
5TEST_PASSES_SANITIZE_LEAK=true
Taylor Blaue37d0b82021-01-25 18:37:26 -05006. ./test-lib.sh
7
Taylor Blau35a8a352021-01-25 18:37:38 -05008# The below tests want control over the 'pack.writeReverseIndex' setting
9# themselves to assert various combinations of it with other options.
Taylor Blau9f7f10a2023-04-12 18:20:36 -040010sane_unset GIT_TEST_NO_WRITE_REV_INDEX
Taylor Blau35a8a352021-01-25 18:37:38 -050011
Taylor Blaue37d0b82021-01-25 18:37:26 -050012packdir=.git/objects/pack
13
14test_expect_success 'setup' '
15 test_commit base &&
16
Taylor Blaua8dd7e02023-04-12 18:20:33 -040017 test_config pack.writeReverseIndex false &&
Taylor Blaue37d0b82021-01-25 18:37:26 -050018 pack=$(git pack-objects --all $packdir/pack) &&
19 rev=$packdir/pack-$pack.rev &&
20
21 test_path_is_missing $rev
22'
23
24test_index_pack () {
25 rm -f $rev &&
26 conf=$1 &&
27 shift &&
28 # remove the index since Windows won't overwrite an existing file
29 rm $packdir/pack-$pack.idx &&
30 git -c pack.writeReverseIndex=$conf index-pack "$@" \
31 $packdir/pack-$pack.pack
32}
33
34test_expect_success 'index-pack with pack.writeReverseIndex' '
35 test_index_pack "" &&
36 test_path_is_missing $rev &&
37
38 test_index_pack false &&
39 test_path_is_missing $rev &&
40
41 test_index_pack true &&
42 test_path_is_file $rev
43'
44
45test_expect_success 'index-pack with --[no-]rev-index' '
46 for conf in "" true false
47 do
48 test_index_pack "$conf" --rev-index &&
49 test_path_exists $rev &&
50
51 test_index_pack "$conf" --no-rev-index &&
Eric Sunshined0fd9932021-12-09 00:11:14 -050052 test_path_is_missing $rev || return 1
Taylor Blaue37d0b82021-01-25 18:37:26 -050053 done
54'
55
56test_expect_success 'index-pack can verify reverse indexes' '
57 test_when_finished "rm -f $rev" &&
58 test_index_pack true &&
59
60 test_path_is_file $rev &&
61 git index-pack --rev-index --verify $packdir/pack-$pack.pack &&
62
63 # Intentionally corrupt the reverse index.
64 chmod u+w $rev &&
65 printf "xxxx" | dd of=$rev bs=1 count=4 conv=notrunc &&
66
67 test_must_fail git index-pack --rev-index --verify \
68 $packdir/pack-$pack.pack 2>err &&
69 grep "validation error" err
70'
71
72test_expect_success 'index-pack infers reverse index name with -o' '
73 git index-pack --rev-index -o other.idx $packdir/pack-$pack.pack &&
74 test_path_is_file other.idx &&
75 test_path_is_file other.rev
76'
77
Taylor Blauc9773342021-01-25 18:37:30 -050078test_expect_success 'pack-objects respects pack.writeReverseIndex' '
79 test_when_finished "rm -fr pack-1-*" &&
80
81 git -c pack.writeReverseIndex= pack-objects --all pack-1 &&
82 test_path_is_missing pack-1-*.rev &&
83
84 git -c pack.writeReverseIndex=false pack-objects --all pack-1 &&
85 test_path_is_missing pack-1-*.rev &&
86
87 git -c pack.writeReverseIndex=true pack-objects --all pack-1 &&
88 test_path_is_file pack-1-*.rev
89'
90
Taylor Blauec8e7762021-01-25 18:37:46 -050091test_expect_success 'reverse index is not generated when available on disk' '
92 test_index_pack true &&
93 test_path_is_file $rev &&
94
95 git rev-parse HEAD >tip &&
96 GIT_TEST_REV_INDEX_DIE_IN_MEMORY=1 git cat-file \
97 --batch-check="%(objectsize:disk)" <tip
98'
99
Taylor Blaudbcf6112023-04-12 18:20:30 -0400100test_expect_success 'reverse index is ignored when pack.readReverseIndex is false' '
101 test_index_pack true &&
102 test_path_is_file $rev &&
103
104 test_config pack.readReverseIndex false &&
105
106 git rev-parse HEAD >tip &&
107 GIT_TEST_REV_INDEX_DIE_ON_DISK=1 git cat-file \
108 --batch-check="%(objectsize:disk)" <tip
109'
110
Taylor Blau6885cd72021-01-28 20:32:02 -0500111test_expect_success 'revindex in-memory vs on-disk' '
112 git init repo &&
113 test_when_finished "rm -fr repo" &&
114 (
115 cd repo &&
116
117 test_commit commit &&
118
119 git rev-list --objects --no-object-names --all >objects &&
120
121 git -c pack.writeReverseIndex=false repack -ad &&
122 test_path_is_missing $packdir/pack-*.rev &&
123 git cat-file --batch-check="%(objectsize:disk) %(objectname)" \
124 <objects >in-core &&
125
126 git -c pack.writeReverseIndex=true repack -ad &&
127 test_path_is_file $packdir/pack-*.rev &&
128 git cat-file --batch-check="%(objectsize:disk) %(objectname)" \
129 <objects >on-disk &&
130
131 test_cmp on-disk in-core
132 )
133'
Derrick Stolee0d30fee2023-04-17 16:21:38 +0000134
135test_expect_success 'fsck succeeds on good rev-index' '
136 test_when_finished rm -fr repo &&
137 git init repo &&
138 (
139 cd repo &&
140
141 test_commit commit &&
142 git -c pack.writeReverseIndex=true repack -ad &&
143 git fsck 2>err &&
144 test_must_be_empty err
145 )
146'
147
Derrick Stoleed975fe12023-04-17 16:21:39 +0000148test_expect_success 'set up rev-index corruption tests' '
149 git init corrupt &&
150 (
151 cd corrupt &&
152
153 test_commit commit &&
154 git -c pack.writeReverseIndex=true repack -ad &&
155
156 revfile=$(ls .git/objects/pack/pack-*.rev) &&
157 chmod a+w $revfile &&
158 cp $revfile $revfile.bak
159 )
160'
161
162corrupt_rev_and_verify () {
163 (
164 pos="$1" &&
165 value="$2" &&
166 error="$3" &&
167
168 cd corrupt &&
169 revfile=$(ls .git/objects/pack/pack-*.rev) &&
170
171 # Reset to original rev-file.
172 cp $revfile.bak $revfile &&
173
174 printf "$value" | dd of=$revfile bs=1 seek="$pos" conv=notrunc &&
175 test_must_fail git fsck 2>err &&
176 grep "$error" err
177 )
178}
179
180test_expect_success 'fsck catches invalid checksum' '
181 revfile=$(ls corrupt/.git/objects/pack/pack-*.rev) &&
182 orig_size=$(wc -c <$revfile) &&
183 hashpos=$((orig_size - 10)) &&
184 corrupt_rev_and_verify $hashpos bogus \
185 "invalid checksum"
186'
187
Derrick Stolee5f658d12023-04-17 16:21:40 +0000188test_expect_success 'fsck catches invalid row position' '
189 corrupt_rev_and_verify 14 "\07" \
190 "invalid rev-index position"
191'
192
Derrick Stolee5a6072f2023-04-17 16:21:41 +0000193test_expect_success 'fsck catches invalid header: magic number' '
194 corrupt_rev_and_verify 1 "\07" \
195 "reverse-index file .* has unknown signature"
196'
197
198test_expect_success 'fsck catches invalid header: version' '
199 corrupt_rev_and_verify 7 "\02" \
200 "reverse-index file .* has unsupported version"
201'
202
203test_expect_success 'fsck catches invalid header: hash function' '
204 corrupt_rev_and_verify 11 "\03" \
205 "reverse-index file .* has unsupported hash id"
206'
207
Taylor Blaue37d0b82021-01-25 18:37:26 -0500208test_done