5
. ./test-lib.sh
6
7
objdir=.git/objects
8
+packdir=$objdir/pack
9
10
test_expect_success '--expire-to stores pruned objects (now)' '
11
git init expire-to-now &&
128
)
129
'
130
131
+generate_random_blob() {
132
+ test-tool genrandom "$@" >blob &&
133
+ git hash-object -w -t blob blob &&
134
+ rm blob
135
+}
136
+
137
+pack_random_blob () {
138
+ generate_random_blob "$@" &&
139
+ git repack -d -q >/dev/null
140
+}
141
+
142
+generate_cruft_pack () {
143
+ pack_random_blob "$@" >/dev/null &&
144
+
145
+ ls $packdir/pack-*.pack | xargs -n 1 basename >in &&
146
+ pack="$(git pack-objects --cruft $packdir/pack <in)" &&
147
+ git prune-packed &&
148
+
149
+ echo "$packdir/pack-$pack.mtimes"
150
+}
151
+
152
+test_expect_success '--max-cruft-size creates new packs when above threshold' '
153
+ git init max-cruft-size-large &&
154
+ (
155
+ cd max-cruft-size-large &&
156
+ test_commit base &&
157
+
158
+ foo="$(pack_random_blob foo $((1*1024*1024)))" &&
159
+ git repack --cruft -d &&
160
+ cruft_foo="$(ls $packdir/pack-*.mtimes)" &&
161
+
162
+ bar="$(pack_random_blob bar $((1*1024*1024)))" &&
163
+ git repack --cruft -d --max-cruft-size=1M &&
164
+ cruft_bar="$(ls $packdir/pack-*.mtimes | grep -v $cruft_foo)" &&
165
+
166
+ test-tool pack-mtimes $(basename "$cruft_foo") >foo.objects &&
167
+ test-tool pack-mtimes $(basename "$cruft_bar") >bar.objects &&
168
+
169
+ grep "^$foo" foo.objects &&
170
+ test_line_count = 1 foo.objects &&
171
+ grep "^$bar" bar.objects &&
172
+ test_line_count = 1 bar.objects
173
+ )
174
+'
175
+
176
+test_expect_success '--max-cruft-size combines existing packs when below threshold' '
177
+ git init max-cruft-size-small &&
178
+ (
179
+ cd max-cruft-size-small &&
180
+ test_commit base &&
181
+
182
+ foo="$(pack_random_blob foo $((1*1024*1024)))" &&
183
+ git repack --cruft -d &&
184
+
185
+ bar="$(pack_random_blob bar $((1*1024*1024)))" &&
186
+ git repack --cruft -d --max-cruft-size=10M &&
187
+
188
+ cruft=$(ls $packdir/pack-*.mtimes) &&
189
+ test-tool pack-mtimes $(basename "$cruft") >cruft.objects &&
190
+
191
+ grep "^$foo" cruft.objects &&
192
+ grep "^$bar" cruft.objects &&
193
+ test_line_count = 2 cruft.objects
194
+ )
195
+'
196
+
197
+test_expect_success '--max-cruft-size combines smaller packs first' '
198
+ git init max-cruft-size-consume-small &&
199
+ (
200
+ cd max-cruft-size-consume-small &&
201
+
202
+ test_commit base &&
203
+ git repack -ad &&
204
+
205
+ cruft_foo="$(generate_cruft_pack foo 524288)" && # 0.5 MiB
206
+ cruft_bar="$(generate_cruft_pack bar 524288)" && # 0.5 MiB
207
+ cruft_baz="$(generate_cruft_pack baz 1048576)" && # 1.0 MiB
208
+ cruft_quux="$(generate_cruft_pack quux 1572864)" && # 1.5 MiB
209
+
210
+ test-tool pack-mtimes "$(basename $cruft_foo)" >expect.raw &&
211
+ test-tool pack-mtimes "$(basename $cruft_bar)" >>expect.raw &&
212
+ sort expect.raw >expect.objects &&
213
+
214
+ # repacking with `--max-cruft-size=2M` should combine
215
+ # both 0.5 MiB packs together, instead of, say, one of
216
+ # the 0.5 MiB packs with the 1.0 MiB pack
217
+ ls $packdir/pack-*.mtimes | sort >cruft.before &&
218
+ git repack -d --cruft --max-cruft-size=2M &&
219
+ ls $packdir/pack-*.mtimes | sort >cruft.after &&
220
+
221
+ comm -13 cruft.before cruft.after >cruft.new &&
222
+ comm -23 cruft.before cruft.after >cruft.removed &&
223
+
224
+ test_line_count = 1 cruft.new &&
225
+ test_line_count = 2 cruft.removed &&
226
+
227
+ # the two smaller packs should be rolled up first
228
+ printf "%s\n" $cruft_foo $cruft_bar | sort >expect.removed &&
229
+ test_cmp expect.removed cruft.removed &&
230
+
231
+ # ...and contain the set of objects rolled up
232
+ test-tool pack-mtimes "$(basename $(cat cruft.new))" >actual.raw &&
233
+ sort actual.raw >actual.objects &&
234
+
235
+ test_cmp expect.objects actual.objects
236
+ )
237
+'
238
+
239
+test_expect_success 'setup --max-cruft-size with freshened objects' '
240
+ git init max-cruft-size-freshen &&
241
+ (
242
+ cd max-cruft-size-freshen &&
243
+
244
+ test_commit base &&
245
+ git repack -ad &&
246
+
247
+ foo="$(generate_random_blob foo 64)" &&
248
+ test-tool chmtime --get -10000 \
249
+ "$objdir/$(test_oid_to_path "$foo")" >foo.mtime &&
250
+
251
+ git repack --cruft -d &&
252
+
253
+ cruft="$(ls $packdir/pack-*.mtimes)" &&
254
+ test-tool pack-mtimes "$(basename $cruft)" >actual &&
255
+ echo "$foo $(cat foo.mtime)" >expect &&
256
+ test_cmp expect actual
257
+ )
258
+'
259
+
260
+test_expect_success '--max-cruft-size with freshened objects (loose)' '
261
+ (
262
+ cd max-cruft-size-freshen &&
263
+
264
+ # regenerate the object, setting its mtime to be more recent
265
+ foo="$(generate_random_blob foo 64)" &&
266
+ test-tool chmtime --get -100 \
267
+ "$objdir/$(test_oid_to_path "$foo")" >foo.mtime &&
268
+
269
+ git repack --cruft -d &&
270
+
271
+ cruft="$(ls $packdir/pack-*.mtimes)" &&
272
+ test-tool pack-mtimes "$(basename $cruft)" >actual &&
273
+ echo "$foo $(cat foo.mtime)" >expect &&
274
+ test_cmp expect actual
275
+ )
276
+'
277
+
278
+test_expect_success '--max-cruft-size with freshened objects (packed)' '
279
+ (
280
+ cd max-cruft-size-freshen &&
281
+
282
+ # regenerate the object and store it in a packfile,
283
+ # setting its mtime to be more recent
284
+ #
285
+ # store it alongside another cruft object so that we
286
+ # do not create an identical copy of the existing
287
+ # cruft pack (which contains $foo).
288
+ foo="$(generate_random_blob foo 64)" &&
289
+ bar="$(generate_random_blob bar 64)" &&
290
+ foo_pack="$(printf "%s\n" $foo $bar | git pack-objects $packdir/pack)" &&
291
+ git prune-packed &&
292
+
293
+ test-tool chmtime --get -10 \
294
+ "$packdir/pack-$foo_pack.pack" >foo.mtime &&
295
+
296
+ git repack --cruft -d &&
297
+
298
+ cruft="$(ls $packdir/pack-*.mtimes)" &&
299
+ test-tool pack-mtimes "$(basename $cruft)" >actual &&
300
+ echo "$foo $(cat foo.mtime)" >expect.raw &&
301
+ echo "$bar $(cat foo.mtime)" >>expect.raw &&
302
+ sort expect.raw >expect &&
303
+ test_cmp expect actual
304
+ )
305
+'
306
+
307
+test_expect_success '--max-cruft-size with pruning' '
308
+ git init max-cruft-size-prune &&
309
+ (
310
+ cd max-cruft-size-prune &&
311
+
312
+ test_commit base &&
313
+ foo="$(generate_random_blob foo $((1024*1024)))" &&
314
+ bar="$(generate_random_blob bar $((1024*1024)))" &&
315
+ baz="$(generate_random_blob baz $((1024*1024)))" &&
316
+
317
+ test-tool chmtime -10000 "$objdir/$(test_oid_to_path "$foo")" &&
318
+
319
+ git repack -d --cruft --max-cruft-size=1M &&
320
+
321
+ # backdate the mtimes of all cruft packs to validate
322
+ # that they were rewritten as a result of pruning
323
+ ls $packdir/pack-*.mtimes | sort >cruft.before &&
324
+ for cruft in $(cat cruft.before)
325
+ do
326
+ mtime="$(test-tool chmtime --get -10000 "$cruft")" &&
327
+ echo $cruft $mtime >>mtimes || return 1
328
+ done &&
329
+
330
+ # repack (and prune) with a --max-cruft-size to ensure
331
+ # that we appropriately split the resulting set of packs
332
+ git repack -d --cruft --max-cruft-size=1M \
333
+ --cruft-expiration=10.seconds.ago &&
334
+ ls $packdir/pack-*.mtimes | sort >cruft.after &&
335
+
336
+ for cruft in $(cat cruft.after)
337
+ do
338
+ old_mtime="$(grep $cruft mtimes | cut -d" " -f2)" &&
339
+ new_mtime="$(test-tool chmtime --get $cruft)" &&
340
+ test $old_mtime -lt $new_mtime || return 1
341
+ done &&
342
+
343
+ test_line_count = 3 cruft.before &&
344
+ test_line_count = 2 cruft.after &&
345
+ test_must_fail git cat-file -e $foo &&
346
+ git cat-file -e $bar &&
347
+ git cat-file -e $baz
348
+ )
349
+'
350
+
351
+test_expect_success '--max-cruft-size ignores non-local packs' '
352
+ repo="max-cruft-size-non-local" &&
353
+ git init $repo &&
354
+ (
355
+ cd $repo &&
356
+ test_commit base &&
357
+ generate_random_blob foo 64 &&
358
+ git repack --cruft -d
359
+ ) &&
360
+
361
+ git clone --reference=$repo $repo $repo-alt &&
362
+ (
363
+ cd $repo-alt &&
364
+
365
+ test_commit other &&
366
+ generate_random_blob bar 64 &&
367
+
368
+ # ensure that we do not attempt to pick up packs from
369
+ # the non-alternated repository, which would result in a
370
+ # crash
371
+ git repack --cruft --max-cruft-size=1M -d
372
+ )
373
+'
374
+
375
test_done