Raw
1 #!/bin/sh
2
3 test_description='git maintenance builtin'
4
5 . ./test-lib.sh
6
7 GIT_TEST_COMMIT_GRAPH=0
8 GIT_TEST_MULTI_PACK_INDEX=0
9
10 # Ensure that auto-maintenance detaches as usual.
11 sane_unset GIT_TEST_MAINT_AUTO_DETACH
12
13 test_lazy_prereq XMLLINT '
14 xmllint --version
15 '
16
17 test_xmllint () {
18 if test_have_prereq XMLLINT
19 then
20 xmllint --noout "$@"
21 else
22 true
23 fi
24 }
25
26 test_lazy_prereq SYSTEMD_ANALYZE '
27 systemd-analyze verify /lib/systemd/system/basic.target
28 '
29
30 test_systemd_analyze_verify () {
31 if test_have_prereq SYSTEMD_ANALYZE
32 then
33 systemd-analyze verify "$@"
34 fi
35 }
36
37 test_expect_success 'help text' '
38 git maintenance -h >actual &&
39 test_grep "usage: git maintenance <subcommand>" actual &&
40 test_expect_code 129 git maintenance barf 2>err &&
41 test_grep "unknown subcommand: \`barf'\''" err &&
42 test_grep "usage: git maintenance" err &&
43 test_expect_code 129 git maintenance 2>err &&
44 test_grep "error: need a subcommand" err &&
45 test_grep "usage: git maintenance" err
46 '
47
48 test_expect_success 'run [--auto|--quiet] with gc strategy' '
49 test_config maintenance.strategy gc &&
50 GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
51 git maintenance run 2>/dev/null &&
52 GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
53 git maintenance run --auto 2>/dev/null &&
54 GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
55 git maintenance run --no-quiet 2>/dev/null &&
56 git maintenance is-needed &&
57 test_subcommand git gc --quiet --no-detach --skip-foreground-tasks <run-no-auto.txt &&
58 ! git maintenance is-needed --auto &&
59 test_subcommand ! git gc --auto --quiet --no-detach --skip-foreground-tasks <run-auto.txt &&
60 test_subcommand git gc --no-quiet --no-detach --skip-foreground-tasks <run-no-quiet.txt
61 '
62
63 test_expect_success 'maintenance.auto config option' '
64 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
65 test_subcommand git maintenance run --auto --quiet --detach <default &&
66 GIT_TRACE2_EVENT="$(pwd)/true" \
67 git -c maintenance.auto=true \
68 commit --quiet --allow-empty -m 2 &&
69 test_subcommand git maintenance run --auto --quiet --detach <true &&
70 GIT_TRACE2_EVENT="$(pwd)/false" \
71 git -c maintenance.auto=false \
72 commit --quiet --allow-empty -m 3 &&
73 test_subcommand ! git maintenance run --auto --quiet --detach <false
74 '
75
76 test_expect_success 'gc.auto config option' '
77 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
78 test_subcommand git maintenance run --auto --quiet --detach <default &&
79 GIT_TRACE2_EVENT="$(pwd)/true" \
80 git -c gc.auto=1 commit --quiet --allow-empty -m 2 &&
81 test_subcommand git maintenance run --auto --quiet --detach <true &&
82 GIT_TRACE2_EVENT="$(pwd)/false" \
83 git -c gc.auto=0 commit --quiet --allow-empty -m 3 &&
84 test_subcommand ! git maintenance run --auto --quiet --detach <false
85 '
86
87 test_expect_success 'maintenance.auto overrides gc.auto' '
88 test_when_finished "rm -f trace" &&
89
90 test_config maintenance.auto false &&
91 test_config gc.auto 1 &&
92 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
93 test_subcommand ! git maintenance run --auto --quiet --detach <trace &&
94
95 test_config maintenance.auto true &&
96 test_config gc.auto 0 &&
97 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
98 test_subcommand git maintenance run --auto --quiet --detach <trace
99 '
100
101 for cfg in maintenance.autoDetach gc.autoDetach
102 do
103 test_expect_success "$cfg=true config option" '
104 test_when_finished "rm -f trace" &&
105 test_config $cfg true &&
106 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
107 test_subcommand git maintenance run --auto --quiet --detach <trace
108 '
109
110 test_expect_success "$cfg=false config option" '
111 test_when_finished "rm -f trace" &&
112 test_config $cfg false &&
113 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
114 test_subcommand git maintenance run --auto --quiet --no-detach <trace
115 '
116 done
117
118 test_expect_success "maintenance.autoDetach overrides gc.autoDetach" '
119 test_when_finished "rm -f trace" &&
120 test_config maintenance.autoDetach false &&
121 test_config gc.autoDetach true &&
122 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
123 test_subcommand git maintenance run --auto --quiet --no-detach <trace
124 '
125
126 test_expect_success 'register uses XDG_CONFIG_HOME config if it exists' '
127 test_when_finished rm -r .config/git/config &&
128 (
129 # Override HOME so that .gitconfig (which test-lib.sh may
130 # have created, e.g. to set safe.bareRepository) does not
131 # take precedence over the XDG location.
132 HOME=$PWD/must-not-exist &&
133 XDG_CONFIG_HOME=.config &&
134 export HOME XDG_CONFIG_HOME &&
135 mkdir -p $XDG_CONFIG_HOME/git &&
136 >$XDG_CONFIG_HOME/git/config &&
137 git maintenance register &&
138 git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
139 pwd >expect &&
140 test_cmp expect actual
141 )
142 '
143
144 test_expect_success 'register does not need XDG_CONFIG_HOME config to exist' '
145 test_when_finished git maintenance unregister &&
146 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
147 git maintenance register &&
148 git config --global --get maintenance.repo >actual &&
149 pwd >expect &&
150 test_cmp expect actual
151 '
152
153 test_expect_success 'unregister uses XDG_CONFIG_HOME config if it exists' '
154 test_when_finished rm -r .config/git/config &&
155 (
156 # Override HOME so that .gitconfig (which test-lib.sh may
157 # have created, e.g. to set safe.bareRepository) does not
158 # take precedence over the XDG location.
159 HOME=$PWD/must-not-exist &&
160 XDG_CONFIG_HOME=.config &&
161 export HOME XDG_CONFIG_HOME &&
162 mkdir -p $XDG_CONFIG_HOME/git &&
163 >$XDG_CONFIG_HOME/git/config &&
164 git maintenance register &&
165 git maintenance unregister &&
166 test_must_fail git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
167 test_must_be_empty actual
168 )
169 '
170
171 test_expect_success 'unregister does not need XDG_CONFIG_HOME config to exist' '
172 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
173 git maintenance register &&
174 git maintenance unregister &&
175 test_must_fail git config --global --get maintenance.repo >actual &&
176 test_must_be_empty actual
177 '
178
179 test_expect_success 'maintenance.<task>.enabled' '
180 git config maintenance.gc.enabled false &&
181 git config maintenance.commit-graph.enabled true &&
182 GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
183 test_subcommand ! git gc --quiet <run-config.txt &&
184 test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
185 '
186
187 test_expect_success 'run --task=<task>' '
188 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
189 git maintenance run --task=commit-graph 2>/dev/null &&
190 GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
191 git maintenance run --task=gc 2>/dev/null &&
192 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
193 git maintenance run --task=commit-graph 2>/dev/null &&
194 GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
195 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
196 test_subcommand ! git gc --quiet --no-detach --skip-foreground-tasks <run-commit-graph.txt &&
197 test_subcommand git gc --quiet --no-detach --skip-foreground-tasks <run-gc.txt &&
198 test_subcommand git gc --quiet --no-detach --skip-foreground-tasks <run-both.txt &&
199 test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
200 test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
201 test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
202 '
203
204 test_expect_success 'core.commitGraph=false prevents write process' '
205 GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
206 git -c core.commitGraph=false maintenance run \
207 --task=commit-graph 2>/dev/null &&
208 test_subcommand ! git commit-graph write --split --reachable --no-progress \
209 <no-commit-graph.txt
210 '
211
212 test_expect_success 'commit-graph auto condition' '
213 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
214
215 GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
216 git -c maintenance.commit-graph.auto=1 $COMMAND &&
217 GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
218 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
219
220 test_commit first &&
221
222 ! git -c maintenance.commit-graph.auto=0 \
223 maintenance is-needed --auto --task=commit-graph &&
224 git -c maintenance.commit-graph.auto=1 \
225 maintenance is-needed --auto --task=commit-graph &&
226
227 GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
228 git -c maintenance.commit-graph.auto=0 $COMMAND &&
229 GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
230 git -c maintenance.commit-graph.auto=1 $COMMAND &&
231
232 git commit --allow-empty -m "second" &&
233 git commit --allow-empty -m "third" &&
234
235 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
236 git -c maintenance.commit-graph.auto=2 $COMMAND &&
237
238 COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
239 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
240 test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
241 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
242 test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
243 test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
244 '
245
246 test_expect_success 'commit-graph auto condition with merges' '
247 test_when_finished "rm -rf repo" &&
248 git init repo &&
249 (
250 cd repo &&
251 git config set maintenance.auto false &&
252 test_commit initial &&
253 git switch --create feature &&
254 test_commit feature-1 &&
255 test_commit feature-2 &&
256 git switch - &&
257 test_commit main-1 &&
258 test_commit main-2 &&
259 git merge feature &&
260
261 # We have 6 commits, none of which are covered by a commit
262 # graph. So this must be the boundary at which we start to
263 # perform maintenance.
264 test_must_fail git -c maintenance.commit-graph.auto=7 \
265 maintenance is-needed --auto --task=commit-graph &&
266 git -c maintenance.commit-graph.auto=6 \
267 maintenance is-needed --auto --task=commit-graph
268 )
269 '
270
271 test_expect_success 'run --task=bogus' '
272 test_must_fail git maintenance run --task=bogus 2>err &&
273 test_grep "is not a valid task" err
274 '
275
276 test_expect_success 'run --task duplicate' '
277 test_must_fail git maintenance run --task=gc --task=gc 2>err &&
278 test_grep "cannot be selected multiple times" err
279 '
280
281 test_expect_success 'run --task=prefetch with no remotes' '
282 git maintenance run --task=prefetch 2>err &&
283 test_must_be_empty err
284 '
285
286 test_expect_success 'prefetch multiple remotes' '
287 git clone . clone1 &&
288 git clone . clone2 &&
289 git remote add remote1 "file://$(pwd)/clone1" &&
290 git remote add remote2 "file://$(pwd)/clone2" &&
291 git -C clone1 switch -c one &&
292 git -C clone2 switch -c two &&
293 test_commit -C clone1 one &&
294 test_commit -C clone2 two &&
295 GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
296 fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
297 test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
298 test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
299 git for-each-ref refs/remotes >actual &&
300 test_must_be_empty actual &&
301 git log prefetch/remotes/remote1/one &&
302 git log prefetch/remotes/remote2/two &&
303 git fetch --all &&
304 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
305 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
306
307 git log --oneline --decorate --all >log &&
308 test_grep ! "prefetch" log &&
309
310 test_when_finished git config --unset remote.remote1.skipFetchAll &&
311 git config remote.remote1.skipFetchAll true &&
312 GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
313 test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
314 test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
315 '
316
317 test_expect_success 'loose-objects task' '
318 # Repack everything so we know the state of the object dir
319 git repack -adk &&
320
321 # Hack to stop maintenance from running during "git commit"
322 echo in use >.git/objects/maintenance.lock &&
323
324 # Assuming that "git commit" creates at least one loose object
325 test_commit create-loose-object &&
326 rm .git/objects/maintenance.lock &&
327
328 ls .git/objects >obj-dir-before &&
329 test_file_not_empty obj-dir-before &&
330 ls .git/objects/pack/*.pack >packs-before &&
331 test_line_count = 1 packs-before &&
332
333 # The first run creates a pack-file
334 # but does not delete loose objects.
335 git maintenance run --task=loose-objects &&
336 ls .git/objects >obj-dir-between &&
337 test_cmp obj-dir-before obj-dir-between &&
338 ls .git/objects/pack/*.pack >packs-between &&
339 test_line_count = 2 packs-between &&
340 ls .git/objects/pack/loose-*.pack >loose-packs &&
341 test_line_count = 1 loose-packs &&
342
343 # The second run deletes loose objects
344 # but does not create a pack-file.
345 git maintenance run --task=loose-objects &&
346 ls .git/objects >obj-dir-after &&
347 cat >expect <<-\EOF &&
348 info
349 pack
350 EOF
351 test_cmp expect obj-dir-after &&
352 ls .git/objects/pack/*.pack >packs-after &&
353 test_cmp packs-between packs-after
354 '
355
356 test_expect_success 'maintenance.loose-objects.auto' '
357 git repack -adk &&
358 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
359 git -c maintenance.loose-objects.auto=1 maintenance \
360 run --auto --task=loose-objects 2>/dev/null &&
361 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
362
363 printf data-A | git hash-object -t blob --stdin -w &&
364 ! git -c maintenance.loose-objects.auto=2 \
365 maintenance is-needed --auto --task=loose-objects &&
366 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
367 git -c maintenance.loose-objects.auto=2 \
368 maintenance run --auto --task=loose-objects 2>/dev/null &&
369 test_subcommand ! git prune-packed --quiet <trace-loA &&
370
371 printf data-B | git hash-object -t blob --stdin -w &&
372 git -c maintenance.loose-objects.auto=2 \
373 maintenance is-needed --auto --task=loose-objects &&
374 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
375 git -c maintenance.loose-objects.auto=2 \
376 maintenance run --auto --task=loose-objects 2>/dev/null &&
377 test_subcommand git prune-packed --quiet <trace-loB &&
378
379 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
380 git -c maintenance.loose-objects.auto=2 \
381 maintenance run --auto --task=loose-objects 2>/dev/null &&
382 test_subcommand git prune-packed --quiet <trace-loC
383 '
384
385 test_expect_success 'maintenance.loose-objects.batchSize' '
386 git init loose-batch &&
387
388 # This creates three objects per commit.
389 test_commit_bulk -C loose-batch 34 &&
390 pack=$(ls loose-batch/.git/objects/pack/pack-*.pack) &&
391 index="${pack%pack}idx" &&
392 rm "$index" &&
393 git -C loose-batch unpack-objects <"$pack" &&
394 git -C loose-batch config maintenance.loose-objects.batchSize 50 &&
395
396 GIT_PROGRESS_DELAY=0 \
397 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
398 test_grep "Enumerating objects: 50, done." err &&
399
400 GIT_PROGRESS_DELAY=0 \
401 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
402 test_grep "Enumerating objects: 50, done." err &&
403
404 GIT_PROGRESS_DELAY=0 \
405 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
406 test_grep "Enumerating objects: 2, done." err &&
407
408 GIT_PROGRESS_DELAY=0 \
409 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
410 test_must_be_empty err
411 '
412
413 test_expect_success 'incremental-repack task' '
414 packDir=.git/objects/pack &&
415 for i in $(test_seq 1 5)
416 do
417 test_commit $i || return 1
418 done &&
419
420 # Create three disjoint pack-files with size BIG, small, small.
421 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
422 test_tick &&
423 git pack-objects --revs $packDir/test-2 <<-\EOF &&
424 HEAD~1
425 ^HEAD~2
426 EOF
427 test_tick &&
428 git pack-objects --revs $packDir/test-3 <<-\EOF &&
429 HEAD
430 ^HEAD~1
431 EOF
432
433 # Delete refs that have not been repacked in these packs.
434 git for-each-ref --format="delete %(refname)" \
435 refs/prefetch refs/tags refs/remotes \
436 --exclude=refs/remotes/*/HEAD >refs &&
437 git update-ref --stdin <refs &&
438
439 # Replace the object directory with this pack layout.
440 rm -f $packDir/pack-* &&
441 rm -f $packDir/loose-* &&
442 ls $packDir/*.pack >packs-before &&
443 test_line_count = 3 packs-before &&
444
445 # make sure we do not have any broken refs that were
446 # missed in the deletion above
447 git for-each-ref &&
448
449 # the job repacks the two into a new pack, but does not
450 # delete the old ones.
451 git maintenance run --task=incremental-repack &&
452 ls $packDir/*.pack >packs-between &&
453 test_line_count = 4 packs-between &&
454
455 # the job deletes the two old packs, and does not write
456 # a new one because the batch size is not high enough to
457 # pack the largest pack-file.
458 git maintenance run --task=incremental-repack &&
459 ls .git/objects/pack/*.pack >packs-after &&
460 test_line_count = 2 packs-after
461 '
462
463 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
464 test_when_finished rm -rf expensive-repo &&
465 git init expensive-repo &&
466 (
467 cd expensive-repo &&
468 git config set core.compression 0 &&
469 git config set maintenance.auto false &&
470
471 for i in $(test_seq 1 5)
472 do
473 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
474 return 1
475 done &&
476 git add big &&
477 git commit -qm "Add big file (1)" &&
478
479 # ensure any possible loose objects are in a pack-file
480 git maintenance run --task=loose-objects &&
481
482 rm big &&
483 for i in $(test_seq 6 10)
484 do
485 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
486 return 1
487 done &&
488 git add big &&
489 git commit -qm "Add big file (2)" &&
490
491 # ensure any possible loose objects are in a pack-file
492 git maintenance run --task=loose-objects &&
493
494 # Now run the incremental-repack task and check the batch-size
495 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
496 --task=incremental-repack 2>/dev/null &&
497 test_subcommand git multi-pack-index repack \
498 --no-progress --batch-size=2147483647 <run-2g.txt
499 )
500 '
501
502 run_incremental_repack_and_verify () {
503 test_commit A &&
504 git repack -adk &&
505 git multi-pack-index write &&
506 ! git -c maintenance.incremental-repack.auto=1 \
507 maintenance is-needed --auto --task=incremental-repack &&
508 GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
509 -c maintenance.incremental-repack.auto=1 \
510 maintenance run --auto --task=incremental-repack 2>/dev/null &&
511 test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
512
513 test_commit B &&
514 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
515 HEAD
516 ^HEAD~1
517 EOF
518 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
519 -c maintenance.incremental-repack.auto=2 \
520 maintenance run --auto --task=incremental-repack 2>/dev/null &&
521 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
522
523 test_commit C &&
524 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
525 HEAD
526 ^HEAD~1
527 EOF
528 git -c maintenance.incremental-repack.auto=2 \
529 maintenance is-needed --auto --task=incremental-repack &&
530 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
531 -c maintenance.incremental-repack.auto=2 \
532 maintenance run --auto --task=incremental-repack 2>/dev/null &&
533 test_subcommand git multi-pack-index write --no-progress <trace-B
534 }
535
536 test_expect_success 'maintenance.incremental-repack.auto' '
537 rm -rf incremental-repack-true &&
538 git init incremental-repack-true &&
539 (
540 cd incremental-repack-true &&
541 git config core.multiPackIndex true &&
542 git config maintenance.auto false &&
543 run_incremental_repack_and_verify
544 )
545 '
546
547 test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' '
548 rm -rf incremental-repack-unset &&
549 git init incremental-repack-unset &&
550 (
551 cd incremental-repack-unset &&
552 test_unconfig core.multiPackIndex &&
553 git config maintenance.auto false &&
554 run_incremental_repack_and_verify
555 )
556 '
557
558 run_and_verify_geometric_pack () {
559 EXPECTED_PACKS="$1" &&
560
561 # Verify that we perform a geometric repack.
562 rm -f "trace2.txt" &&
563 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
564 git maintenance run --task=geometric-repack 2>/dev/null &&
565 test_subcommand git repack -d -l --geometric=2 \
566 --quiet --write-midx <trace2.txt &&
567
568 # Verify that the number of packfiles matches our expectation.
569 ls -l .git/objects/pack/*.pack >packfiles &&
570 test_line_count = "$EXPECTED_PACKS" packfiles &&
571
572 # And verify that there are no loose objects anymore.
573 git count-objects -v >count &&
574 test_grep '^count: 0$' count &&
575
576 # Verify that no orphaned .idx files were left behind. On
577 # Windows, a missing odb_to_close causes the parent to hold
578 # mmap handles on .idx files, silently preventing their
579 # deletion by the child git-repack process.
580 ls .git/objects/pack/pack-*.idx .git/objects/pack/pack-*.pack |
581 sed "s/\.pack$/.idx/" |
582 sort | uniq -u >orphaned-idx &&
583 test_must_be_empty orphaned-idx
584 }
585
586 test_expect_success 'geometric repacking task' '
587 test_when_finished "rm -rf repo" &&
588 git init repo &&
589 (
590 cd repo &&
591 git config set maintenance.auto false &&
592 test_commit initial &&
593
594 # The initial repack causes an all-into-one repack.
595 GIT_TRACE2_EVENT="$(pwd)/initial-repack.txt" \
596 git maintenance run --task=geometric-repack 2>/dev/null &&
597 test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago \
598 --quiet --write-midx <initial-repack.txt &&
599
600 # Repacking should now cause a no-op geometric repack because
601 # no packfiles need to be combined.
602 ls -l .git/objects/pack/*.pack >before &&
603 run_and_verify_geometric_pack 1 &&
604 ls -l .git/objects/pack/*.pack >after &&
605 test_cmp before after &&
606
607 # This incremental change creates a new packfile that only
608 # soaks up loose objects. The packfiles are not getting merged
609 # at this point.
610 test_commit loose &&
611 run_and_verify_geometric_pack 2 &&
612
613 # Both packfiles have 3 objects, so the next run would cause us
614 # to merge all packfiles together. This should be turned into
615 # an all-into-one-repack.
616 GIT_TRACE2_EVENT="$(pwd)/all-into-one-repack.txt" \
617 git maintenance run --task=geometric-repack 2>/dev/null &&
618 test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago \
619 --quiet --write-midx <all-into-one-repack.txt &&
620
621 # The geometric repack soaks up unreachable objects.
622 echo blob-1 | git hash-object -w --stdin -t blob &&
623 run_and_verify_geometric_pack 2 &&
624
625 # A second unreachable object should be written into another packfile.
626 echo blob-2 | git hash-object -w --stdin -t blob &&
627 run_and_verify_geometric_pack 3 &&
628
629 # And these two small packs should now be merged via the
630 # geometric repack. The large packfile should remain intact.
631 cp -R .git/objects .git/objects.save &&
632 run_and_verify_geometric_pack 2 &&
633
634 # On Windows, verify the same with legacy delete semantics
635 # that reject deletion of mmap-held .idx files.
636 if test_have_prereq MINGW
637 then
638 rm -rf .git/objects &&
639 mv .git/objects.save .git/objects &&
640 test_env GIT_TEST_LEGACY_DELETE=1 \
641 run_and_verify_geometric_pack 2
642 fi &&
643
644 # If we now add two more objects and repack twice we should
645 # then see another all-into-one repack. This time around
646 # though, as we have unreachable objects, we should also see a
647 # cruft pack.
648 echo blob-3 | git hash-object -w --stdin -t blob &&
649 echo blob-4 | git hash-object -w --stdin -t blob &&
650 run_and_verify_geometric_pack 3 &&
651 GIT_TRACE2_EVENT="$(pwd)/cruft-repack.txt" \
652 git maintenance run --task=geometric-repack 2>/dev/null &&
653 test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago \
654 --quiet --write-midx <cruft-repack.txt &&
655 ls .git/objects/pack/*.pack >packs &&
656 test_line_count = 2 packs &&
657 ls .git/objects/pack/*.mtimes >cruft &&
658 test_line_count = 1 cruft
659 )
660 '
661
662 test_geometric_repack_needed () {
663 NEEDED="$1"
664 GEOMETRIC_CONFIG="$2" &&
665 rm -f trace2.txt &&
666 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
667 git ${GEOMETRIC_CONFIG:+-c maintenance.geometric-repack.$GEOMETRIC_CONFIG} \
668 maintenance run --auto --task=geometric-repack 2>/dev/null &&
669 case "$NEEDED" in
670 true)
671 test_grep "\[\"git\",\"repack\"," trace2.txt;;
672 false)
673 ! test_grep "\[\"git\",\"repack\"," trace2.txt;;
674 *)
675 BUG "invalid parameter: $NEEDED";;
676 esac
677 }
678
679 test_expect_success 'geometric repacking with --auto' '
680 test_when_finished "rm -rf repo" &&
681 git init repo &&
682 (
683 cd repo &&
684 git config set maintenance.auto false &&
685
686 # An empty repository does not need repacking, except when
687 # explicitly told to do it.
688 test_geometric_repack_needed false &&
689 test_geometric_repack_needed false auto=0 &&
690 test_geometric_repack_needed false auto=1 &&
691 test_geometric_repack_needed true auto=-1 &&
692
693 test_oid_init &&
694
695 # Loose objects cause a repack when crossing the limit. Note
696 # that the number of objects gets extrapolated by having a look
697 # at the "objects/17/" shard.
698 test_commit "$(test_oid blob17_1)" &&
699 test_geometric_repack_needed false &&
700 test_commit "$(test_oid blob17_2)" &&
701 test_geometric_repack_needed false auto=257 &&
702 test_geometric_repack_needed true auto=256 &&
703
704 # Force another repack.
705 test_commit first &&
706 test_commit second &&
707 test_geometric_repack_needed true auto=-1 &&
708
709 # We now have two packfiles that would be merged together. As
710 # such, the repack should always happen unless the user has
711 # disabled the auto task.
712 test_geometric_repack_needed false auto=0 &&
713 test_geometric_repack_needed true auto=9000
714 )
715 '
716
717 test_expect_success 'geometric repacking honors configured split factor' '
718 test_when_finished "rm -rf repo" &&
719 git init repo &&
720 (
721 cd repo &&
722 git config set maintenance.auto false &&
723
724 # Create three different packs with 9, 2 and 1 object, respectively.
725 # This is done so that only a subset of packs would be merged
726 # together so that we can verify that `git repack` receives the
727 # correct geometric factor.
728 for i in $(test_seq 9)
729 do
730 echo first-$i | git hash-object -w --stdin -t blob || return 1
731 done &&
732 git repack --geometric=2 -d &&
733
734 for i in $(test_seq 2)
735 do
736 echo second-$i | git hash-object -w --stdin -t blob || return 1
737 done &&
738 git repack --geometric=2 -d &&
739
740 echo third | git hash-object -w --stdin -t blob &&
741 git repack --geometric=2 -d &&
742
743 test_geometric_repack_needed false splitFactor=2 &&
744 test_geometric_repack_needed true splitFactor=3 &&
745 test_subcommand git repack -d -l --geometric=3 --quiet --write-midx <trace2.txt
746 )
747 '
748
749 test_expect_success 'pack-refs task' '
750 for n in $(test_seq 1 5)
751 do
752 git branch -f to-pack/$n HEAD || return 1
753 done &&
754 GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
755 git maintenance run --task=pack-refs &&
756 test_subcommand git pack-refs --all --prune <pack-refs.txt
757 '
758
759 test_expect_success 'reflog-expire task' '
760 GIT_TRACE2_EVENT="$(pwd)/reflog-expire.txt" \
761 git maintenance run --task=reflog-expire &&
762 test_subcommand git reflog expire --all <reflog-expire.txt
763 '
764
765 test_expect_success 'reflog-expire task --auto only packs when exceeding limits' '
766 git reflog expire --all --expire=now &&
767 test_commit reflog-one &&
768 test_commit reflog-two &&
769
770 ! git -c maintenance.reflog-expire.auto=3 \
771 maintenance is-needed --auto --task=reflog-expire &&
772 GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \
773 git -c maintenance.reflog-expire.auto=3 maintenance run --auto --task=reflog-expire &&
774 test_subcommand ! git reflog expire --all <reflog-expire-auto.txt &&
775
776 git -c maintenance.reflog-expire.auto=2 \
777 maintenance is-needed --auto --task=reflog-expire &&
778 GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \
779 git -c maintenance.reflog-expire.auto=2 maintenance run --auto --task=reflog-expire &&
780 test_subcommand git reflog expire --all <reflog-expire-auto.txt
781 '
782
783 test_expect_worktree_prune () {
784 negate=
785 if test "$1" = "!"
786 then
787 negate="!"
788 shift
789 fi
790
791 rm -f "worktree-prune.txt" &&
792 GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" "$@" &&
793 test_subcommand $negate git worktree prune --expire 3.months.ago <worktree-prune.txt
794 }
795
796 test_expect_success 'worktree-prune task without --auto always prunes' '
797 test_expect_worktree_prune git maintenance run --task=worktree-prune
798 '
799
800 test_expect_success 'worktree-prune task --auto only prunes with prunable worktree' '
801 test_expect_worktree_prune ! git maintenance run --auto --task=worktree-prune &&
802 mkdir .git/worktrees &&
803 : >.git/worktrees/abc &&
804 git maintenance is-needed --auto --task=worktree-prune &&
805 test_expect_worktree_prune git maintenance run --auto --task=worktree-prune
806 '
807
808 test_expect_success 'worktree-prune task with --auto honors maintenance.worktree-prune.auto' '
809 # A negative value should always prune.
810 test_expect_worktree_prune git -c maintenance.worktree-prune.auto=-1 maintenance run --auto --task=worktree-prune &&
811
812 mkdir .git/worktrees &&
813 : >.git/worktrees/first &&
814 : >.git/worktrees/second &&
815 : >.git/worktrees/third &&
816
817 # Zero should never prune.
818 test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=0 maintenance run --auto --task=worktree-prune &&
819 # A positive value should require at least this many prunable worktrees.
820 test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=4 maintenance run --auto --task=worktree-prune &&
821 git -c maintenance.worktree-prune.auto=3 maintenance is-needed --auto --task=worktree-prune &&
822 test_expect_worktree_prune git -c maintenance.worktree-prune.auto=3 maintenance run --auto --task=worktree-prune
823 '
824
825 test_expect_success 'worktree-prune task honors gc.worktreePruneExpire' '
826 git worktree add worktree &&
827 rm -rf worktree &&
828
829 rm -f worktree-prune.txt &&
830 ! git -c gc.worktreePruneExpire=1.week.ago maintenance is-needed --auto --task=worktree-prune &&
831 GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=1.week.ago maintenance run --auto --task=worktree-prune &&
832 test_subcommand ! git worktree prune --expire 1.week.ago <worktree-prune.txt &&
833 test_path_is_dir .git/worktrees/worktree &&
834
835 rm -f worktree-prune.txt &&
836 git -c gc.worktreePruneExpire=now maintenance is-needed --auto --task=worktree-prune &&
837 GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=now maintenance run --auto --task=worktree-prune &&
838 test_subcommand git worktree prune --expire now <worktree-prune.txt &&
839 test_path_is_missing .git/worktrees/worktree
840 '
841
842 test_expect_rerere_gc () {
843 negate=
844 if test "$1" = "!"
845 then
846 negate="!"
847 shift
848 fi
849
850 rm -f "rerere-gc.txt" &&
851 GIT_TRACE2_EVENT="$(pwd)/rerere-gc.txt" "$@" &&
852 test_subcommand $negate git rerere gc <rerere-gc.txt
853 }
854
855 test_expect_success 'rerere-gc task without --auto always collects garbage' '
856 test_expect_rerere_gc git maintenance run --task=rerere-gc
857 '
858
859 test_expect_success 'rerere-gc task with --auto only prunes with prunable entries' '
860 test_when_finished "rm -rf .git/rr-cache" &&
861 ! git maintenance is-needed --auto --task=rerere-gc &&
862 test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc &&
863 mkdir .git/rr-cache &&
864 ! git maintenance is-needed --auto --task=rerere-gc &&
865 test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc &&
866 : >.git/rr-cache/entry &&
867 git maintenance is-needed --auto --task=rerere-gc &&
868 test_expect_rerere_gc git maintenance run --auto --task=rerere-gc
869 '
870
871 test_expect_success 'rerere-gc task with --auto honors maintenance.rerere-gc.auto' '
872 test_when_finished "rm -rf .git/rr-cache" &&
873
874 # A negative value should always prune.
875 git -c maintenance.rerere-gc.auto=-1 maintenance is-needed --auto --task=rerere-gc &&
876 test_expect_rerere_gc git -c maintenance.rerere-gc.auto=-1 maintenance run --auto --task=rerere-gc &&
877
878 # A positive value prunes when there is at least one entry.
879 ! git -c maintenance.rerere-gc.auto=9000 maintenance is-needed --auto --task=rerere-gc &&
880 test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc &&
881 mkdir .git/rr-cache &&
882 ! git -c maintenance.rerere-gc.auto=9000 maintenance is-needed --auto --task=rerere-gc &&
883 test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc &&
884 : >.git/rr-cache/entry-1 &&
885 git -c maintenance.rerere-gc.auto=9000 maintenance is-needed --auto --task=rerere-gc &&
886 test_expect_rerere_gc git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc &&
887
888 # Zero should never prune.
889 : >.git/rr-cache/entry-1 &&
890 ! git -c maintenance.rerere-gc.auto=0 maintenance is-needed --auto --task=rerere-gc &&
891 test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=0 maintenance run --auto --task=rerere-gc
892 '
893
894 test_expect_success '--auto and --schedule incompatible' '
895 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
896 test_grep "cannot be used together" err
897 '
898
899 test_expect_success '--task and --schedule incompatible' '
900 test_must_fail git maintenance run --task=pack-refs --schedule=daily 2>err &&
901 test_grep "cannot be used together" err
902 '
903
904 test_expect_success 'invalid --schedule value' '
905 test_must_fail git maintenance run --schedule=annually 2>err &&
906 test_grep "unrecognized --schedule" err
907 '
908
909 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
910 git config maintenance.loose-objects.enabled true &&
911 git config maintenance.loose-objects.schedule hourly &&
912 git config maintenance.commit-graph.enabled true &&
913 git config maintenance.commit-graph.schedule daily &&
914 git config maintenance.incremental-repack.enabled true &&
915 git config maintenance.incremental-repack.schedule weekly &&
916
917 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
918 git maintenance run --schedule=hourly 2>/dev/null &&
919 test_subcommand git prune-packed --quiet <hourly.txt &&
920 test_subcommand ! git commit-graph write --split --reachable \
921 --no-progress <hourly.txt &&
922 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
923
924 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
925 git maintenance run --schedule=daily 2>/dev/null &&
926 test_subcommand git prune-packed --quiet <daily.txt &&
927 test_subcommand git commit-graph write --split --reachable \
928 --no-progress <daily.txt &&
929 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
930
931 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
932 git maintenance run --schedule=weekly 2>/dev/null &&
933 test_subcommand git prune-packed --quiet <weekly.txt &&
934 test_subcommand git commit-graph write --split --reachable \
935 --no-progress <weekly.txt &&
936 test_subcommand git multi-pack-index write --no-progress <weekly.txt
937 '
938
939 test_expect_success 'maintenance.strategy inheritance' '
940 for task in commit-graph loose-objects incremental-repack
941 do
942 git config --unset maintenance.$task.schedule || return 1
943 done &&
944
945 test_when_finished git config --unset maintenance.strategy &&
946 git config maintenance.strategy incremental &&
947
948 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
949 git maintenance run --schedule=hourly --quiet &&
950 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
951 git maintenance run --schedule=daily --quiet &&
952 GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
953 git maintenance run --schedule=weekly --quiet &&
954
955 test_subcommand git commit-graph write --split --reachable \
956 --no-progress <incremental-hourly.txt &&
957 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
958 test_subcommand ! git multi-pack-index write --no-progress \
959 <incremental-hourly.txt &&
960 test_subcommand ! git pack-refs --all --prune \
961 <incremental-hourly.txt &&
962
963 test_subcommand git commit-graph write --split --reachable \
964 --no-progress <incremental-daily.txt &&
965 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
966 test_subcommand git multi-pack-index write --no-progress \
967 <incremental-daily.txt &&
968 test_subcommand ! git pack-refs --all --prune \
969 <incremental-daily.txt &&
970
971 test_subcommand git commit-graph write --split --reachable \
972 --no-progress <incremental-weekly.txt &&
973 test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
974 test_subcommand git multi-pack-index write --no-progress \
975 <incremental-weekly.txt &&
976 test_subcommand git pack-refs --all --prune \
977 <incremental-weekly.txt &&
978
979 # Modify defaults
980 git config maintenance.commit-graph.schedule daily &&
981 git config maintenance.loose-objects.schedule hourly &&
982 git config maintenance.incremental-repack.enabled false &&
983
984 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
985 git maintenance run --schedule=hourly --quiet &&
986 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
987 git maintenance run --schedule=daily --quiet &&
988
989 test_subcommand ! git commit-graph write --split --reachable \
990 --no-progress <modified-hourly.txt &&
991 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
992 test_subcommand ! git multi-pack-index write --no-progress \
993 <modified-hourly.txt &&
994
995 test_subcommand git commit-graph write --split --reachable \
996 --no-progress <modified-daily.txt &&
997 test_subcommand git prune-packed --quiet <modified-daily.txt &&
998 test_subcommand ! git multi-pack-index write --no-progress \
999 <modified-daily.txt
1000 '
1001
1002 test_strategy () {
1003 STRATEGY="$1"
1004 shift
1005
1006 cat >expect &&
1007 rm -f trace2.txt &&
1008 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1009 git -c maintenance.strategy=$STRATEGY maintenance run --quiet "$@" &&
1010 sed -n 's/{"event":"child_start","sid":"[^/"]*",.*,"argv":\["\(.*\)\"]}/\1/p' <trace2.txt |
1011 sed 's/","/ /g' >actual
1012 test_cmp expect actual
1013 }
1014
1015 test_expect_success 'maintenance.strategy is respected' '
1016 test_when_finished "rm -rf repo" &&
1017 git init repo &&
1018 (
1019 cd repo &&
1020 test_commit initial &&
1021
1022 test_must_fail git -c maintenance.strategy=unknown maintenance run 2>err &&
1023 test_grep "unknown maintenance strategy: .unknown." err &&
1024
1025 test_strategy incremental <<-\EOF &&
1026 git pack-refs --all --prune
1027 git reflog expire --all
1028 git gc --quiet --no-detach --skip-foreground-tasks
1029 EOF
1030
1031 test_strategy incremental --schedule=weekly <<-\EOF &&
1032 git pack-refs --all --prune
1033 git prune-packed --quiet
1034 git multi-pack-index write --no-progress
1035 git multi-pack-index expire --no-progress
1036 git multi-pack-index repack --no-progress --batch-size=1
1037 git commit-graph write --split --reachable --no-progress
1038 EOF
1039
1040 test_strategy gc <<-\EOF &&
1041 git pack-refs --all --prune
1042 git reflog expire --all
1043 git gc --quiet --no-detach --skip-foreground-tasks
1044 EOF
1045
1046 test_strategy gc --schedule=weekly <<-\EOF &&
1047 git pack-refs --all --prune
1048 git reflog expire --all
1049 git gc --quiet --no-detach --skip-foreground-tasks
1050 EOF
1051
1052 test_strategy geometric <<-\EOF &&
1053 git pack-refs --all --prune
1054 git reflog expire --all
1055 git repack -d -l --geometric=2 --quiet --write-midx
1056 git commit-graph write --split --reachable --no-progress
1057 git worktree prune --expire 3.months.ago
1058 git rerere gc
1059 EOF
1060
1061 test_strategy geometric --schedule=weekly <<-\EOF
1062 git pack-refs --all --prune
1063 git reflog expire --all
1064 git repack -d -l --geometric=2 --quiet --write-midx
1065 git commit-graph write --split --reachable --no-progress
1066 git worktree prune --expire 3.months.ago
1067 git rerere gc
1068 EOF
1069 )
1070 '
1071
1072 test_expect_success 'register and unregister' '
1073 test_when_finished git config --global --unset-all maintenance.repo &&
1074
1075 test_must_fail git maintenance unregister 2>err &&
1076 test_grep "is not registered" err &&
1077 git maintenance unregister --force &&
1078
1079 git config --global --add maintenance.repo /existing1 &&
1080 git config --global --add maintenance.repo /existing2 &&
1081 git config --global --get-all maintenance.repo >before &&
1082
1083 git maintenance register &&
1084 test_cmp_config false maintenance.auto &&
1085 git config --global --get-all maintenance.repo >between &&
1086 cp before expect &&
1087 pwd >>expect &&
1088 test_cmp expect between &&
1089
1090 git maintenance unregister &&
1091 git config --global --get-all maintenance.repo >actual &&
1092 test_cmp before actual &&
1093
1094 git config --file ./other --add maintenance.repo /existing1 &&
1095 git config --file ./other --add maintenance.repo /existing2 &&
1096 git config --file ./other --get-all maintenance.repo >before &&
1097
1098 git maintenance register --config-file ./other &&
1099 test_cmp_config false maintenance.auto &&
1100 git config --file ./other --get-all maintenance.repo >between &&
1101 cp before expect &&
1102 pwd >>expect &&
1103 test_cmp expect between &&
1104
1105 git maintenance unregister --config-file ./other &&
1106 git config --file ./other --get-all maintenance.repo >actual &&
1107 test_cmp before actual &&
1108
1109 test_must_fail git maintenance unregister 2>err &&
1110 test_grep "is not registered" err &&
1111 git maintenance unregister --force &&
1112
1113 test_must_fail git maintenance unregister --config-file ./other 2>err &&
1114 test_grep "is not registered" err &&
1115 git maintenance unregister --config-file ./other --force
1116 '
1117
1118 test_expect_success 'register with no value for maintenance.repo' '
1119 cp .git/config .git/config.orig &&
1120 test_when_finished mv .git/config.orig .git/config &&
1121
1122 cat >>.git/config <<-\EOF &&
1123 [maintenance]
1124 repo
1125 EOF
1126 cat >expect <<-\EOF &&
1127 error: missing value for '\''maintenance.repo'\''
1128 EOF
1129 git maintenance register 2>actual &&
1130 test_cmp expect actual &&
1131 git config maintenance.repo
1132 '
1133
1134 test_expect_success 'unregister with no value for maintenance.repo' '
1135 cp .git/config .git/config.orig &&
1136 test_when_finished mv .git/config.orig .git/config &&
1137
1138 cat >>.git/config <<-\EOF &&
1139 [maintenance]
1140 repo
1141 EOF
1142 cat >expect <<-\EOF &&
1143 error: missing value for '\''maintenance.repo'\''
1144 EOF
1145 test_expect_code 128 git maintenance unregister 2>actual.raw &&
1146 grep ^error actual.raw >actual &&
1147 test_cmp expect actual &&
1148 git config maintenance.repo &&
1149
1150 git maintenance unregister --force 2>actual.raw &&
1151 grep ^error actual.raw >actual &&
1152 test_cmp expect actual &&
1153 git config maintenance.repo
1154 '
1155
1156 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
1157 META="a+b*c" &&
1158 git init "$META" &&
1159 git -C "$META" maintenance register &&
1160 git config --get-all --show-origin maintenance.repo &&
1161 git config --get-all --global --fixed-value \
1162 maintenance.repo "$(pwd)/$META" &&
1163 git -C "$META" maintenance unregister &&
1164 test_must_fail git config --get-all --global --fixed-value \
1165 maintenance.repo "$(pwd)/$META"
1166 '
1167
1168 test_expect_success 'start without GIT_TEST_MAINT_SCHEDULER' '
1169 test_when_finished "rm -rf systemctl.log script repo" &&
1170 mkdir script &&
1171 write_script script/systemctl <<-\EOF &&
1172 echo "$*" >>../systemctl.log
1173 EOF
1174 git init repo &&
1175 (
1176 cd repo &&
1177 sane_unset GIT_TEST_MAINT_SCHEDULER &&
1178 PATH="$PWD/../script:$PATH" git maintenance start --scheduler=systemd
1179 ) &&
1180 test_grep -- "--user list-timers" systemctl.log &&
1181 test_grep -- "enable --now git-maintenance@" systemctl.log
1182 '
1183
1184 test_expect_success 'start --scheduler=<scheduler>' '
1185 test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
1186 test_grep "unrecognized --scheduler argument" err &&
1187
1188 test_expect_code 129 git maintenance start --no-scheduler 2>err &&
1189 test_grep "unknown option" err &&
1190
1191 test_expect_code 128 \
1192 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
1193 git maintenance start --scheduler=crontab 2>err &&
1194 test_grep "fatal: crontab scheduler is not available" err
1195 '
1196
1197 test_expect_success 'start from empty cron table' '
1198 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
1199
1200 # start registers the repo
1201 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1202
1203 test_grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
1204 test_grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
1205 test_grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
1206 '
1207
1208 test_expect_success 'stop from existing schedule' '
1209 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
1210
1211 # stop does not unregister the repo
1212 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1213
1214 # Operation is idempotent
1215 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
1216 test_must_be_empty cron.txt
1217 '
1218
1219 test_expect_success 'start preserves existing schedule' '
1220 echo "Important information!" >cron.txt &&
1221 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
1222 test_grep "Important information!" cron.txt
1223 '
1224
1225 test_expect_success 'magic markers are correct' '
1226 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
1227 cat >expect <<-\EOF &&
1228 # BEGIN GIT MAINTENANCE SCHEDULE
1229 # END GIT MAINTENANCE SCHEDULE
1230 EOF
1231 test_cmp actual expect
1232 '
1233
1234 test_expect_success 'stop preserves surrounding schedule' '
1235 echo "Crucial information!" >>cron.txt &&
1236 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
1237 test_grep "Important information!" cron.txt &&
1238 test_grep "Crucial information!" cron.txt
1239 '
1240
1241 test_expect_success 'start and stop macOS maintenance' '
1242 # ensure $HOME can be compared against hook arguments on all platforms
1243 pfx=$(cd "$HOME" && pwd) &&
1244
1245 write_script print-args <<-\EOF &&
1246 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
1247 EOF
1248
1249 rm -f args &&
1250 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
1251
1252 # start registers the repo
1253 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1254
1255 ls "$HOME/Library/LaunchAgents" >actual &&
1256 cat >expect <<-\EOF &&
1257 org.git-scm.git.daily.plist
1258 org.git-scm.git.hourly.plist
1259 org.git-scm.git.weekly.plist
1260 EOF
1261 test_cmp expect actual &&
1262
1263 rm -f expect &&
1264 for frequency in hourly daily weekly
1265 do
1266 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
1267 test_xmllint "$PLIST" &&
1268 test_grep schedule=$frequency "$PLIST" &&
1269 echo "bootout gui/[UID] $PLIST" >>expect &&
1270 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
1271 done &&
1272 test_cmp expect args &&
1273
1274 rm -f args &&
1275 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
1276
1277 # stop does not unregister the repo
1278 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1279
1280 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1281 hourly daily weekly >expect &&
1282 test_cmp expect args &&
1283 ls "$HOME/Library/LaunchAgents" >actual &&
1284 test_line_count = 0 actual
1285 '
1286
1287 test_expect_success 'use launchctl list to prevent extra work' '
1288 # ensure we are registered
1289 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
1290
1291 # do it again on a fresh args file
1292 rm -f args &&
1293 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
1294
1295 ls "$HOME/Library/LaunchAgents" >actual &&
1296 cat >expect <<-\EOF &&
1297 list org.git-scm.git.hourly
1298 list org.git-scm.git.daily
1299 list org.git-scm.git.weekly
1300 EOF
1301 test_cmp expect args
1302 '
1303
1304 test_expect_success 'start and stop Windows maintenance' '
1305 write_script print-args <<-\EOF &&
1306 echo $* >>args
1307 while test $# -gt 0
1308 do
1309 case "$1" in
1310 /xml) shift; xmlfile=$1; break ;;
1311 *) shift ;;
1312 esac
1313 done
1314 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
1315 EOF
1316
1317 rm -f args &&
1318 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
1319
1320 # start registers the repo
1321 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1322
1323 for frequency in hourly daily weekly
1324 do
1325 test_grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
1326 file=$(ls .git/schedule_${frequency}*.xml) &&
1327 test_xmllint "$file" || return 1
1328 done &&
1329
1330 rm -f args &&
1331 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
1332
1333 # stop does not unregister the repo
1334 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1335
1336 printf "/delete /tn Git Maintenance (%s) /f\n" \
1337 hourly daily weekly >expect &&
1338 test_cmp expect args
1339 '
1340
1341 test_expect_success 'start and stop Linux/systemd maintenance' '
1342 write_script print-args <<-\EOF &&
1343 printf "%s\n" "$*" >>args
1344 EOF
1345
1346 XDG_CONFIG_HOME="$PWD" &&
1347 export XDG_CONFIG_HOME &&
1348 rm -f args &&
1349 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
1350
1351 # start registers the repo
1352 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1353
1354 for schedule in hourly daily weekly
1355 do
1356 test_path_is_file "systemd/user/git-maintenance@$schedule.timer" || return 1
1357 done &&
1358 test_path_is_file "systemd/user/git-maintenance@.service" &&
1359
1360 test_systemd_analyze_verify "systemd/user/git-maintenance@hourly.service" &&
1361 test_systemd_analyze_verify "systemd/user/git-maintenance@daily.service" &&
1362 test_systemd_analyze_verify "systemd/user/git-maintenance@weekly.service" &&
1363
1364 test_grep "core.askPass=true" "systemd/user/git-maintenance@.service" &&
1365 test_grep "credential.interactive=false" "systemd/user/git-maintenance@.service" &&
1366
1367 printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1368 test_cmp expect args &&
1369
1370 rm -f args &&
1371 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
1372
1373 # stop does not unregister the repo
1374 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1375
1376 for schedule in hourly daily weekly
1377 do
1378 test_path_is_missing "systemd/user/git-maintenance@$schedule.timer" || return 1
1379 done &&
1380 test_path_is_missing "systemd/user/git-maintenance@.service" &&
1381
1382 printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1383 test_cmp expect args
1384 '
1385
1386 test_expect_success 'start and stop when several schedulers are available' '
1387 write_script print-args <<-\EOF &&
1388 printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
1389 EOF
1390
1391 rm -f args &&
1392 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
1393 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1394 hourly daily weekly >expect &&
1395 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
1396 hourly daily weekly >>expect &&
1397 printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
1398 test_cmp expect args &&
1399
1400 rm -f args &&
1401 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
1402 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1403 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
1404 hourly daily weekly >>expect &&
1405 for frequency in hourly daily weekly
1406 do
1407 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
1408 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
1409 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
1410 done &&
1411 test_cmp expect args &&
1412
1413 rm -f args &&
1414 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
1415 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1416 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1417 hourly daily weekly >>expect &&
1418 printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
1419 hourly daily weekly >>expect &&
1420 test_cmp expect args &&
1421
1422 rm -f args &&
1423 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
1424 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1425 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1426 hourly daily weekly >>expect &&
1427 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
1428 hourly daily weekly >>expect &&
1429 test_cmp expect args
1430 '
1431
1432 test_expect_success 'register preserves existing strategy' '
1433 git config maintenance.strategy none &&
1434 git maintenance register &&
1435 test_config maintenance.strategy none &&
1436 git config --unset maintenance.strategy &&
1437 git maintenance register &&
1438 test_config maintenance.strategy incremental
1439 '
1440
1441 test_expect_success 'fails when running outside of a repository' '
1442 nongit test_must_fail git maintenance run &&
1443 nongit test_must_fail git maintenance stop &&
1444 nongit test_must_fail git maintenance start &&
1445 nongit test_must_fail git maintenance register &&
1446 nongit test_must_fail git maintenance unregister
1447 '
1448
1449 test_expect_success 'fails when configured to use an invalid strategy' '
1450 test_must_fail git -c maintenance.strategy=invalid maintenance run --schedule=hourly 2>err &&
1451 test_grep "unknown maintenance strategy: .invalid." err
1452 '
1453
1454 test_expect_success 'register and unregister bare repo' '
1455 test_when_finished "git config --global --unset-all maintenance.repo || :" &&
1456 test_might_fail git config --global --unset-all maintenance.repo &&
1457 git init --bare barerepo &&
1458 (
1459 cd barerepo &&
1460 git maintenance register &&
1461 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1462 git maintenance unregister &&
1463 test_must_fail git config --global --get-all maintenance.repo
1464 )
1465 '
1466
1467 test_expect_success 'failed schedule prevents config change' '
1468 git init --bare failcase &&
1469
1470 for scheduler in crontab launchctl schtasks systemctl
1471 do
1472 GIT_TEST_MAINT_SCHEDULER="$scheduler:false" &&
1473 export GIT_TEST_MAINT_SCHEDULER &&
1474 test_must_fail \
1475 git -C failcase maintenance start &&
1476 test_must_fail git -C failcase config maintenance.auto || return 1
1477 done
1478 '
1479
1480 test_expect_success '--no-detach causes maintenance to not run in background' '
1481 test_when_finished "rm -rf repo" &&
1482 git init repo &&
1483 (
1484 cd repo &&
1485
1486 # Prepare the repository such that git-maintenance(1) ends up
1487 # outputting something.
1488 test_commit something &&
1489 git config set maintenance.gc.enabled false &&
1490 git config set maintenance.loose-objects.enabled true &&
1491 git config set maintenance.loose-objects.auto 1 &&
1492 git config set maintenance.incremental-repack.enabled true &&
1493
1494 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
1495 git maintenance run --no-detach >out 2>&1 &&
1496 ! test_region maintenance detach trace.txt
1497 )
1498 '
1499
1500 test_expect_success PIPE '--detach holds maintenance lock until daemonized child exits' '
1501 test_when_finished "rm -rf repo" &&
1502 git init repo &&
1503 (
1504 cd repo &&
1505
1506 git config maintenance.auto false &&
1507 git config core.lockfilepid true &&
1508
1509 git remote add origin /does/not/exist &&
1510 git config set remote.origin.uploadpack "cat fifo-uploadpack" &&
1511
1512 mkfifo fifo-uploadpack fifo-maint &&
1513
1514 # Open the maintenance FIFO, as otherwise spawning
1515 # git-maintenance(1) would block. Note that we need to open it
1516 # as read-write, as otherwise we would block here already.
1517 exec 9<>fifo-maint &&
1518
1519 { git maintenance run --task=prefetch --detach 7>&9 & } &&
1520 parent="$!" &&
1521
1522 # Reap the parent process so that the exec call below will not
1523 # get SIGCHLD.
1524 wait "$parent" &&
1525
1526 # Open the git-upload-pack(1) FIFO for writing, which will
1527 # block until the upload-pack script opens it for reading. Once
1528 # exec returns, we know that the daemonized child is alive and
1529 # pinned.
1530 exec 8>fifo-uploadpack &&
1531
1532 test_path_is_file .git/objects/maintenance.lock &&
1533 test_path_is_file .git/objects/"maintenance~pid.lock" &&
1534
1535 # Verify that the maintenance.lock still exists, and
1536 # that it was created by the parent process, not the
1537 # child.
1538 echo "pid $parent" >expect &&
1539 test_cmp expect .git/objects/"maintenance~pid.lock" &&
1540
1541 # Reopen the maintenance FIFO as read-only so that
1542 # git-maintenance(1) is the only writer. This will cause it to
1543 # close the FIFO once the process exits.
1544 exec 9<&- &&
1545 exec 9<fifo-maint &&
1546
1547 # Close the FIFO used by git-upload-pack(1) to unblock it and
1548 # then wait until the maintenance FIFO is closed by
1549 # git-maintenance(1), indicating that it has exited.
1550 exec 8>&- &&
1551 cat <&9 &&
1552
1553 test_path_is_missing .git/objects/maintenance.lock &&
1554 test_path_is_missing .git/objects/"maintenance~pid.lock"
1555 )
1556 '
1557
1558 test_expect_success '--detach causes maintenance to run in background' '
1559 test_when_finished "rm -rf repo" &&
1560 git init repo &&
1561 (
1562 cd repo &&
1563
1564 test_commit something &&
1565 git config set maintenance.gc.enabled false &&
1566 git config set maintenance.loose-objects.enabled true &&
1567 git config set maintenance.loose-objects.auto 1 &&
1568 git config set maintenance.incremental-repack.enabled true &&
1569
1570 # The extra file descriptor gets inherited to the child
1571 # process, and by reading stdout we thus essentially wait for
1572 # that descriptor to get closed, which indicates that the child
1573 # is done, too.
1574 does_not_matter=$(GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
1575 git maintenance run --detach 9>&1) &&
1576 test_region maintenance detach trace.txt
1577 )
1578 '
1579
1580 test_expect_success 'repacking loose objects is quiet' '
1581 test_when_finished "rm -rf repo" &&
1582 git init repo &&
1583 (
1584 cd repo &&
1585
1586 test_commit something &&
1587 git config set maintenance.gc.enabled false &&
1588 git config set maintenance.loose-objects.enabled true &&
1589 git config set maintenance.loose-objects.auto 1 &&
1590
1591 git maintenance run --quiet >out 2>&1 &&
1592 test_must_be_empty out
1593 )
1594 '
1595
1596 test_expect_success 'maintenance aborts with existing lock file' '
1597 test_when_finished "rm -rf repo script" &&
1598 mkdir script &&
1599 write_script script/systemctl <<-\EOF &&
1600 true
1601 EOF
1602
1603 git init repo &&
1604 : >repo/.git/objects/schedule.lock &&
1605 test_must_fail env PATH="$PWD/script:$PATH" git -C repo maintenance start --scheduler=systemd 2>err &&
1606 test_grep "Another scheduled git-maintenance(1) process seems to be running" err
1607 '
1608
1609 test_done