summaryrefslogtreecommitdiffstats
path: root/t/t5335-compact-multi-pack-index.sh
blob: 40f3844282f04efce0a7a2630bf71057ccac95f4 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/sh

test_description='multi-pack-index compaction'

. ./test-lib.sh

GIT_TEST_MULTI_PACK_INDEX=0
GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL=0

objdir=.git/objects
packdir=$objdir/pack
midxdir=$packdir/multi-pack-index.d
midx_chain=$midxdir/multi-pack-index-chain

nth_line() {
	local n="$1"
	shift
	awk "NR==$n" "$@"
}

write_packs () {
	for c in "$@"
	do
		test_commit "$c" &&

		git pack-objects --all --unpacked $packdir/pack-$c &&
		git prune-packed &&

		git multi-pack-index write --incremental --bitmap || return 1
	done
}

test_midx_layer_packs () {
	local checksum="$1" &&
	shift &&

	test-tool read-midx $objdir "$checksum" >out &&

	printf "%s\n" "$@" >expect &&
	# NOTE: do *not* pipe through sort here, we want to ensure the
	# order of packs is preserved during compaction.
	grep "^pack-" out | cut -d"-" -f2 >actual &&

	test_cmp expect actual
}

test_midx_layer_object_uniqueness () {
	: >objs.all
	while read layer
	do
		test-tool read-midx --show-objects $objdir "$layer" >out &&
		grep "\.pack$" out | cut -d" " -f1 | sort >objs.layer &&
		test_stdout_line_count = 0 comm -12 objs.all objs.layer &&
		cat objs.all objs.layer | sort >objs.tmp &&
		mv objs.tmp objs.all || return 1
	done <$midx_chain
}

test_expect_success 'MIDX compaction with lex-ordered pack names' '
	git init midx-compact-lex-order &&
	(
		cd midx-compact-lex-order &&

		git config maintenance.auto false &&

		write_packs A B C D E &&
		test_line_count = 5 $midx_chain &&

		git multi-pack-index compact --incremental --bitmap \
			"$(nth_line 2 "$midx_chain")" \
			"$(nth_line 4 "$midx_chain")" &&
		test_line_count = 3 $midx_chain &&

		test_midx_layer_packs "$(nth_line 1 "$midx_chain")" A &&
		test_midx_layer_packs "$(nth_line 2 "$midx_chain")" B C D &&
		test_midx_layer_packs "$(nth_line 3 "$midx_chain")" E &&

		test_midx_layer_object_uniqueness
	)
'

test_expect_success 'MIDX compaction with non-lex-ordered pack names' '
	git init midx-compact-non-lex-order &&
	(
		cd midx-compact-non-lex-order &&

		git config maintenance.auto false &&

		write_packs D C A B E &&
		test_line_count = 5 $midx_chain &&

		git multi-pack-index compact --incremental --bitmap \
			"$(nth_line 2 "$midx_chain")" \
			"$(nth_line 4 "$midx_chain")" &&
		test_line_count = 3 $midx_chain &&

		test_midx_layer_packs "$(nth_line 1 "$midx_chain")" D &&
		test_midx_layer_packs "$(nth_line 2 "$midx_chain")" C A B &&
		test_midx_layer_packs "$(nth_line 3 "$midx_chain")" E &&

		test_midx_layer_object_uniqueness
	)
'

test_expect_success 'setup for bogus MIDX compaction scenarios' '
	git init midx-compact-bogus &&
	(
		cd midx-compact-bogus &&

		git config maintenance.auto false &&

		write_packs A B C
	)
'

test_expect_success 'MIDX compaction with missing endpoints' '
	(
		cd midx-compact-bogus &&

		test_must_fail git multi-pack-index compact --incremental \
			"<missing>" "<missing>" 2>err &&
		test_grep "could not find MIDX: <missing>" err &&

		test_must_fail git multi-pack-index compact --incremental \
			"<missing>" "$(nth_line 2 "$midx_chain")" 2>err &&
		test_grep "could not find MIDX: <missing>" err &&

		test_must_fail git multi-pack-index compact --incremental \
			"$(nth_line 2 "$midx_chain")" "<missing>" 2>err &&
		test_grep "could not find MIDX: <missing>" err
	)
'

test_expect_success 'MIDX compaction with reversed endpoints' '
	(
		cd midx-compact-bogus &&

		from="$(nth_line 3 "$midx_chain")" &&
		to="$(nth_line 1 "$midx_chain")" &&

		test_must_fail git multi-pack-index compact --incremental \
			"$from" "$to" 2>err &&

		test_grep "MIDX $from must be an ancestor of $to" err
	)
'

test_expect_success 'MIDX compaction with identical endpoints' '
	(
		cd midx-compact-bogus &&

		from="$(nth_line 3 "$midx_chain")" &&
		to="$(nth_line 3 "$midx_chain")" &&

		test_must_fail git multi-pack-index compact --incremental \
			"$from" "$to" 2>err &&

		test_grep "MIDX compaction endpoints must be unique" err
	)
'

test_expect_success 'MIDX compaction with midx.version=1' '
	(
		cd midx-compact-bogus &&

		test_must_fail git -c midx.version=1 multi-pack-index compact \
			"$(nth_line 1 "$midx_chain")" \
			"$(nth_line 2 "$midx_chain")" 2>err &&

		test_grep "fatal: cannot perform MIDX compaction with v1 format" err
	)
'

midx_objs_by_pack () {
	awk '/\.pack$/ { split($3, a, "-"); print a[2], $1 }' | sort
}

tag_objs_from_pack () {
	objs="$(git rev-list --objects --no-object-names "$2")" &&
	printf "$1 %s\n" $objs | sort
}

test_expect_success 'MIDX compaction preserves pack object selection' '
	git init midx-compact-preserve-selection &&
	(
		cd midx-compact-preserve-selection &&

		git config maintenance.auto false &&

		test_commit A &&
		test_commit B &&

		# Create two packs, one containing just the objects from
		# A, and another containing all objects from the
		# repository.
		p1="$(echo A | git pack-objects --revs --delta-base-offset \
			$packdir/pack-1)" &&
		p0="$(echo B | git pack-objects --revs --delta-base-offset \
			$packdir/pack-0)" &&

		echo "pack-1-$p1.idx" | git multi-pack-index write \
			--incremental --bitmap --stdin-packs &&
		echo "pack-0-$p0.idx" | git multi-pack-index write \
			--incremental --bitmap --stdin-packs &&

		write_packs C &&

		git multi-pack-index compact --incremental --bitmap \
			"$(nth_line 1 "$midx_chain")" \
			"$(nth_line 2 "$midx_chain")" &&


		test-tool read-midx --show-objects $objdir \
			"$(nth_line 1 "$midx_chain")" >AB.info &&
		test-tool read-midx --show-objects $objdir \
			"$(nth_line 2 "$midx_chain")" >C.info &&

		midx_objs_by_pack <AB.info >AB.actual &&
		midx_objs_by_pack <C.info >C.actual &&

		{
			tag_objs_from_pack 1 A &&
			tag_objs_from_pack 0 A..B
		} | sort >AB.expect &&
		tag_objs_from_pack C B..C >C.expect &&

		test_cmp AB.expect AB.actual &&
		test_cmp C.expect C.actual
	)
'

test_expect_success 'MIDX compaction with bitmaps' '
	git init midx-compact-with-bitmaps &&
	(
		cd midx-compact-with-bitmaps &&

		git config maintenance.auto false &&

		write_packs foo bar baz quux woot &&

		test-tool read-midx --bitmap $objdir >bitmap.expect &&
		git multi-pack-index compact --incremental --bitmap \
			"$(nth_line 2 "$midx_chain")" \
			"$(nth_line 4 "$midx_chain")" &&
		test-tool read-midx --bitmap $objdir >bitmap.actual &&

		test_cmp bitmap.expect bitmap.actual &&

		true
	)
'

test_expect_success 'MIDX compaction with bitmaps (non-trivial)' '
	git init midx-compact-with-bitmaps-non-trivial &&
	(
		cd midx-compact-with-bitmaps-non-trivial &&

		git config maintenance.auto false &&

		git branch -m main &&

		#               D(4)
		#              /
		# A(1) --- B(2) --- C(3) --- G(7)
		#              \
		#               E(5) --- F(6)
		write_packs A B C &&
		git checkout -b side &&
		write_packs D &&
		git checkout -b other B &&
		write_packs E F &&
		git checkout main &&
		write_packs G &&

		# Compact layers 2-4, leaving us with:
		#
		#  [A, [B, C, D], E, F, G]
		git multi-pack-index compact --incremental --bitmap \
			"$(nth_line 2 "$midx_chain")" \
			"$(nth_line 4 "$midx_chain")" &&

		# Then compact the top two layers, condensing the above
		# such that the new 4th layer contains F and G.
		#
		#  [A, [B, C, D], E, [F, G]]
		git multi-pack-index compact --incremental --bitmap \
			"$(nth_line 4 "$midx_chain")" \
			"$(nth_line 5 "$midx_chain")"
	)
'

test_done