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