]> git.ipfire.org Git - thirdparty/git.git/blame - t/t5325-reverse-index.sh
The third batch
[thirdparty/git.git] / t / t5325-reverse-index.sh
CommitLineData
e37d0b87
TB
1#!/bin/sh
2
3test_description='on-disk reverse index'
b77919ed
TB
4
5TEST_PASSES_SANITIZE_LEAK=true
e37d0b87
TB
6. ./test-lib.sh
7
35a8a354
TB
8# The below tests want control over the 'pack.writeReverseIndex' setting
9# themselves to assert various combinations of it with other options.
9f7f10a2 10sane_unset GIT_TEST_NO_WRITE_REV_INDEX
35a8a354 11
e37d0b87
TB
12packdir=.git/objects/pack
13
14test_expect_success 'setup' '
15 test_commit base &&
16
a8dd7e05 17 test_config pack.writeReverseIndex false &&
e37d0b87
TB
18 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 &&
d0fd9931 52 test_path_is_missing $rev || return 1
e37d0b87
TB
53 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
c9773343
TB
78test_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
ec8e7760
TB
91test_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
dbcf6116
TB
100test_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
6885cd7d
TB
111test_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'
0d30feef
DS
134
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
d975fe1f
DS
148test_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
5f658d1b
DS
188test_expect_success 'fsck catches invalid row position' '
189 corrupt_rev_and_verify 14 "\07" \
190 "invalid rev-index position"
191'
192
5a6072f6
DS
193test_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
e37d0b87 208test_done