The thirteenth batch
[alt-git.git] / t / t7900-maintenance.sh
blobabae7a97546f66567cc34f363581fe02dfecf245
1 #!/bin/sh
3 test_description='git maintenance builtin'
5 . ./test-lib.sh
7 GIT_TEST_COMMIT_GRAPH=0
8 GIT_TEST_MULTI_PACK_INDEX=0
10 test_lazy_prereq XMLLINT '
11 xmllint --version
14 test_xmllint () {
15 if test_have_prereq XMLLINT
16 then
17 xmllint --noout "$@"
18 else
19 true
23 test_lazy_prereq SYSTEMD_ANALYZE '
24 systemd-analyze verify /lib/systemd/system/basic.target
27 test_systemd_analyze_verify () {
28 if test_have_prereq SYSTEMD_ANALYZE
29 then
30 systemd-analyze verify "$@"
34 test_expect_success 'help text' '
35 test_expect_code 129 git maintenance -h >actual &&
36 test_grep "usage: git maintenance <subcommand>" actual &&
37 test_expect_code 129 git maintenance barf 2>err &&
38 test_grep "unknown subcommand: \`barf'\''" err &&
39 test_grep "usage: git maintenance" err &&
40 test_expect_code 129 git maintenance 2>err &&
41 test_grep "error: need a subcommand" err &&
42 test_grep "usage: git maintenance" err
45 test_expect_success 'run [--auto|--quiet]' '
46 GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
47 git maintenance run 2>/dev/null &&
48 GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
49 git maintenance run --auto 2>/dev/null &&
50 GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
51 git maintenance run --no-quiet 2>/dev/null &&
52 test_subcommand git gc --quiet --no-detach <run-no-auto.txt &&
53 test_subcommand ! git gc --auto --quiet --no-detach <run-auto.txt &&
54 test_subcommand git gc --no-quiet --no-detach <run-no-quiet.txt
57 test_expect_success 'maintenance.auto config option' '
58 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
59 test_subcommand git maintenance run --auto --quiet --detach <default &&
60 GIT_TRACE2_EVENT="$(pwd)/true" \
61 git -c maintenance.auto=true \
62 commit --quiet --allow-empty -m 2 &&
63 test_subcommand git maintenance run --auto --quiet --detach <true &&
64 GIT_TRACE2_EVENT="$(pwd)/false" \
65 git -c maintenance.auto=false \
66 commit --quiet --allow-empty -m 3 &&
67 test_subcommand ! git maintenance run --auto --quiet --detach <false
70 for cfg in maintenance.autoDetach gc.autoDetach
72 test_expect_success "$cfg=true config option" '
73 test_when_finished "rm -f trace" &&
74 test_config $cfg true &&
75 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
76 test_subcommand git maintenance run --auto --quiet --detach <trace
79 test_expect_success "$cfg=false config option" '
80 test_when_finished "rm -f trace" &&
81 test_config $cfg false &&
82 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
83 test_subcommand git maintenance run --auto --quiet --no-detach <trace
85 done
87 test_expect_success "maintenance.autoDetach overrides gc.autoDetach" '
88 test_when_finished "rm -f trace" &&
89 test_config maintenance.autoDetach false &&
90 test_config gc.autoDetach true &&
91 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
92 test_subcommand git maintenance run --auto --quiet --no-detach <trace
95 test_expect_success 'register uses XDG_CONFIG_HOME config if it exists' '
96 test_when_finished rm -r .config/git/config &&
98 XDG_CONFIG_HOME=.config &&
99 export XDG_CONFIG_HOME &&
100 mkdir -p $XDG_CONFIG_HOME/git &&
101 >$XDG_CONFIG_HOME/git/config &&
102 git maintenance register &&
103 git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
104 pwd >expect &&
105 test_cmp expect actual
109 test_expect_success 'register does not need XDG_CONFIG_HOME config to exist' '
110 test_when_finished git maintenance unregister &&
111 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
112 git maintenance register &&
113 git config --global --get maintenance.repo >actual &&
114 pwd >expect &&
115 test_cmp expect actual
118 test_expect_success 'unregister uses XDG_CONFIG_HOME config if it exists' '
119 test_when_finished rm -r .config/git/config &&
121 XDG_CONFIG_HOME=.config &&
122 export XDG_CONFIG_HOME &&
123 mkdir -p $XDG_CONFIG_HOME/git &&
124 >$XDG_CONFIG_HOME/git/config &&
125 git maintenance register &&
126 git maintenance unregister &&
127 test_must_fail git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
128 test_must_be_empty actual
132 test_expect_success 'unregister does not need XDG_CONFIG_HOME config to exist' '
133 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
134 git maintenance register &&
135 git maintenance unregister &&
136 test_must_fail git config --global --get maintenance.repo >actual &&
137 test_must_be_empty actual
140 test_expect_success 'maintenance.<task>.enabled' '
141 git config maintenance.gc.enabled false &&
142 git config maintenance.commit-graph.enabled true &&
143 GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
144 test_subcommand ! git gc --quiet <run-config.txt &&
145 test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
148 test_expect_success 'run --task=<task>' '
149 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
150 git maintenance run --task=commit-graph 2>/dev/null &&
151 GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
152 git maintenance run --task=gc 2>/dev/null &&
153 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
154 git maintenance run --task=commit-graph 2>/dev/null &&
155 GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
156 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
157 test_subcommand ! git gc --quiet --no-detach <run-commit-graph.txt &&
158 test_subcommand git gc --quiet --no-detach <run-gc.txt &&
159 test_subcommand git gc --quiet --no-detach <run-both.txt &&
160 test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
161 test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
162 test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
165 test_expect_success 'core.commitGraph=false prevents write process' '
166 GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
167 git -c core.commitGraph=false maintenance run \
168 --task=commit-graph 2>/dev/null &&
169 test_subcommand ! git commit-graph write --split --reachable --no-progress \
170 <no-commit-graph.txt
173 test_expect_success 'commit-graph auto condition' '
174 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
176 GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
177 git -c maintenance.commit-graph.auto=1 $COMMAND &&
178 GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
179 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
181 test_commit first &&
183 GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
184 git -c maintenance.commit-graph.auto=0 $COMMAND &&
185 GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
186 git -c maintenance.commit-graph.auto=1 $COMMAND &&
188 git commit --allow-empty -m "second" &&
189 git commit --allow-empty -m "third" &&
191 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
192 git -c maintenance.commit-graph.auto=2 $COMMAND &&
194 COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
195 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
196 test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
197 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
198 test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
199 test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
202 test_expect_success 'run --task=bogus' '
203 test_must_fail git maintenance run --task=bogus 2>err &&
204 test_grep "is not a valid task" err
207 test_expect_success 'run --task duplicate' '
208 test_must_fail git maintenance run --task=gc --task=gc 2>err &&
209 test_grep "cannot be selected multiple times" err
212 test_expect_success 'run --task=prefetch with no remotes' '
213 git maintenance run --task=prefetch 2>err &&
214 test_must_be_empty err
217 test_expect_success 'prefetch multiple remotes' '
218 git clone . clone1 &&
219 git clone . clone2 &&
220 git remote add remote1 "file://$(pwd)/clone1" &&
221 git remote add remote2 "file://$(pwd)/clone2" &&
222 git -C clone1 switch -c one &&
223 git -C clone2 switch -c two &&
224 test_commit -C clone1 one &&
225 test_commit -C clone2 two &&
226 GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
227 fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
228 test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
229 test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
230 git for-each-ref refs/remotes >actual &&
231 test_must_be_empty actual &&
232 git log prefetch/remotes/remote1/one &&
233 git log prefetch/remotes/remote2/two &&
234 git fetch --all &&
235 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
236 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
238 git log --oneline --decorate --all >log &&
239 ! grep "prefetch" log &&
241 test_when_finished git config --unset remote.remote1.skipFetchAll &&
242 git config remote.remote1.skipFetchAll true &&
243 GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
244 test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
245 test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
248 test_expect_success 'loose-objects task' '
249 # Repack everything so we know the state of the object dir
250 git repack -adk &&
252 # Hack to stop maintenance from running during "git commit"
253 echo in use >.git/objects/maintenance.lock &&
255 # Assuming that "git commit" creates at least one loose object
256 test_commit create-loose-object &&
257 rm .git/objects/maintenance.lock &&
259 ls .git/objects >obj-dir-before &&
260 test_file_not_empty obj-dir-before &&
261 ls .git/objects/pack/*.pack >packs-before &&
262 test_line_count = 1 packs-before &&
264 # The first run creates a pack-file
265 # but does not delete loose objects.
266 git maintenance run --task=loose-objects &&
267 ls .git/objects >obj-dir-between &&
268 test_cmp obj-dir-before obj-dir-between &&
269 ls .git/objects/pack/*.pack >packs-between &&
270 test_line_count = 2 packs-between &&
271 ls .git/objects/pack/loose-*.pack >loose-packs &&
272 test_line_count = 1 loose-packs &&
274 # The second run deletes loose objects
275 # but does not create a pack-file.
276 git maintenance run --task=loose-objects &&
277 ls .git/objects >obj-dir-after &&
278 cat >expect <<-\EOF &&
279 info
280 pack
282 test_cmp expect obj-dir-after &&
283 ls .git/objects/pack/*.pack >packs-after &&
284 test_cmp packs-between packs-after
287 test_expect_success 'maintenance.loose-objects.auto' '
288 git repack -adk &&
289 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
290 git -c maintenance.loose-objects.auto=1 maintenance \
291 run --auto --task=loose-objects 2>/dev/null &&
292 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
293 printf data-A | git hash-object -t blob --stdin -w &&
294 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
295 git -c maintenance.loose-objects.auto=2 \
296 maintenance run --auto --task=loose-objects 2>/dev/null &&
297 test_subcommand ! git prune-packed --quiet <trace-loA &&
298 printf data-B | git hash-object -t blob --stdin -w &&
299 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
300 git -c maintenance.loose-objects.auto=2 \
301 maintenance run --auto --task=loose-objects 2>/dev/null &&
302 test_subcommand git prune-packed --quiet <trace-loB &&
303 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
304 git -c maintenance.loose-objects.auto=2 \
305 maintenance run --auto --task=loose-objects 2>/dev/null &&
306 test_subcommand git prune-packed --quiet <trace-loC
309 test_expect_success 'incremental-repack task' '
310 packDir=.git/objects/pack &&
311 for i in $(test_seq 1 5)
313 test_commit $i || return 1
314 done &&
316 # Create three disjoint pack-files with size BIG, small, small.
317 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
318 test_tick &&
319 git pack-objects --revs $packDir/test-2 <<-\EOF &&
320 HEAD~1
321 ^HEAD~2
323 test_tick &&
324 git pack-objects --revs $packDir/test-3 <<-\EOF &&
325 HEAD
326 ^HEAD~1
329 # Delete refs that have not been repacked in these packs.
330 git for-each-ref --format="delete %(refname)" \
331 refs/prefetch refs/tags refs/remotes >refs &&
332 git update-ref --stdin <refs &&
334 # Replace the object directory with this pack layout.
335 rm -f $packDir/pack-* &&
336 rm -f $packDir/loose-* &&
337 ls $packDir/*.pack >packs-before &&
338 test_line_count = 3 packs-before &&
340 # make sure we do not have any broken refs that were
341 # missed in the deletion above
342 git for-each-ref &&
344 # the job repacks the two into a new pack, but does not
345 # delete the old ones.
346 git maintenance run --task=incremental-repack &&
347 ls $packDir/*.pack >packs-between &&
348 test_line_count = 4 packs-between &&
350 # the job deletes the two old packs, and does not write
351 # a new one because the batch size is not high enough to
352 # pack the largest pack-file.
353 git maintenance run --task=incremental-repack &&
354 ls .git/objects/pack/*.pack >packs-after &&
355 test_line_count = 2 packs-after
358 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
359 test_config core.compression 0 &&
361 for i in $(test_seq 1 5)
363 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
364 return 1
365 done &&
366 git add big &&
367 git commit -qm "Add big file (1)" &&
369 # ensure any possible loose objects are in a pack-file
370 git maintenance run --task=loose-objects &&
372 rm big &&
373 for i in $(test_seq 6 10)
375 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
376 return 1
377 done &&
378 git add big &&
379 git commit -qm "Add big file (2)" &&
381 # ensure any possible loose objects are in a pack-file
382 git maintenance run --task=loose-objects &&
384 # Now run the incremental-repack task and check the batch-size
385 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
386 --task=incremental-repack 2>/dev/null &&
387 test_subcommand git multi-pack-index repack \
388 --no-progress --batch-size=2147483647 <run-2g.txt
391 run_incremental_repack_and_verify () {
392 test_commit A &&
393 git repack -adk &&
394 git multi-pack-index write &&
395 GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
396 -c maintenance.incremental-repack.auto=1 \
397 maintenance run --auto --task=incremental-repack 2>/dev/null &&
398 test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
399 test_commit B &&
400 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
401 HEAD
402 ^HEAD~1
404 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
405 -c maintenance.incremental-repack.auto=2 \
406 maintenance run --auto --task=incremental-repack 2>/dev/null &&
407 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
408 test_commit C &&
409 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
410 HEAD
411 ^HEAD~1
413 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
414 -c maintenance.incremental-repack.auto=2 \
415 maintenance run --auto --task=incremental-repack 2>/dev/null &&
416 test_subcommand git multi-pack-index write --no-progress <trace-B
419 test_expect_success 'maintenance.incremental-repack.auto' '
420 rm -rf incremental-repack-true &&
421 git init incremental-repack-true &&
423 cd incremental-repack-true &&
424 git config core.multiPackIndex true &&
425 run_incremental_repack_and_verify
429 test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' '
430 rm -rf incremental-repack-unset &&
431 git init incremental-repack-unset &&
433 cd incremental-repack-unset &&
434 test_unconfig core.multiPackIndex &&
435 run_incremental_repack_and_verify
439 test_expect_success 'pack-refs task' '
440 for n in $(test_seq 1 5)
442 git branch -f to-pack/$n HEAD || return 1
443 done &&
444 GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
445 git maintenance run --task=pack-refs &&
446 test_subcommand git pack-refs --all --prune <pack-refs.txt
449 test_expect_success '--auto and --schedule incompatible' '
450 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
451 test_grep "at most one" err
454 test_expect_success 'invalid --schedule value' '
455 test_must_fail git maintenance run --schedule=annually 2>err &&
456 test_grep "unrecognized --schedule" err
459 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
460 git config maintenance.loose-objects.enabled true &&
461 git config maintenance.loose-objects.schedule hourly &&
462 git config maintenance.commit-graph.enabled true &&
463 git config maintenance.commit-graph.schedule daily &&
464 git config maintenance.incremental-repack.enabled true &&
465 git config maintenance.incremental-repack.schedule weekly &&
467 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
468 git maintenance run --schedule=hourly 2>/dev/null &&
469 test_subcommand git prune-packed --quiet <hourly.txt &&
470 test_subcommand ! git commit-graph write --split --reachable \
471 --no-progress <hourly.txt &&
472 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
474 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
475 git maintenance run --schedule=daily 2>/dev/null &&
476 test_subcommand git prune-packed --quiet <daily.txt &&
477 test_subcommand git commit-graph write --split --reachable \
478 --no-progress <daily.txt &&
479 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
481 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
482 git maintenance run --schedule=weekly 2>/dev/null &&
483 test_subcommand git prune-packed --quiet <weekly.txt &&
484 test_subcommand git commit-graph write --split --reachable \
485 --no-progress <weekly.txt &&
486 test_subcommand git multi-pack-index write --no-progress <weekly.txt
489 test_expect_success 'maintenance.strategy inheritance' '
490 for task in commit-graph loose-objects incremental-repack
492 git config --unset maintenance.$task.schedule || return 1
493 done &&
495 test_when_finished git config --unset maintenance.strategy &&
496 git config maintenance.strategy incremental &&
498 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
499 git maintenance run --schedule=hourly --quiet &&
500 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
501 git maintenance run --schedule=daily --quiet &&
502 GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
503 git maintenance run --schedule=weekly --quiet &&
505 test_subcommand git commit-graph write --split --reachable \
506 --no-progress <incremental-hourly.txt &&
507 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
508 test_subcommand ! git multi-pack-index write --no-progress \
509 <incremental-hourly.txt &&
510 test_subcommand ! git pack-refs --all --prune \
511 <incremental-hourly.txt &&
513 test_subcommand git commit-graph write --split --reachable \
514 --no-progress <incremental-daily.txt &&
515 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
516 test_subcommand git multi-pack-index write --no-progress \
517 <incremental-daily.txt &&
518 test_subcommand ! git pack-refs --all --prune \
519 <incremental-daily.txt &&
521 test_subcommand git commit-graph write --split --reachable \
522 --no-progress <incremental-weekly.txt &&
523 test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
524 test_subcommand git multi-pack-index write --no-progress \
525 <incremental-weekly.txt &&
526 test_subcommand git pack-refs --all --prune \
527 <incremental-weekly.txt &&
529 # Modify defaults
530 git config maintenance.commit-graph.schedule daily &&
531 git config maintenance.loose-objects.schedule hourly &&
532 git config maintenance.incremental-repack.enabled false &&
534 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
535 git maintenance run --schedule=hourly --quiet &&
536 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
537 git maintenance run --schedule=daily --quiet &&
539 test_subcommand ! git commit-graph write --split --reachable \
540 --no-progress <modified-hourly.txt &&
541 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
542 test_subcommand ! git multi-pack-index write --no-progress \
543 <modified-hourly.txt &&
545 test_subcommand git commit-graph write --split --reachable \
546 --no-progress <modified-daily.txt &&
547 test_subcommand git prune-packed --quiet <modified-daily.txt &&
548 test_subcommand ! git multi-pack-index write --no-progress \
549 <modified-daily.txt
552 test_expect_success 'register and unregister' '
553 test_when_finished git config --global --unset-all maintenance.repo &&
555 test_must_fail git maintenance unregister 2>err &&
556 grep "is not registered" err &&
557 git maintenance unregister --force &&
559 git config --global --add maintenance.repo /existing1 &&
560 git config --global --add maintenance.repo /existing2 &&
561 git config --global --get-all maintenance.repo >before &&
563 git maintenance register &&
564 test_cmp_config false maintenance.auto &&
565 git config --global --get-all maintenance.repo >between &&
566 cp before expect &&
567 pwd >>expect &&
568 test_cmp expect between &&
570 git maintenance unregister &&
571 git config --global --get-all maintenance.repo >actual &&
572 test_cmp before actual &&
574 git config --file ./other --add maintenance.repo /existing1 &&
575 git config --file ./other --add maintenance.repo /existing2 &&
576 git config --file ./other --get-all maintenance.repo >before &&
578 git maintenance register --config-file ./other &&
579 test_cmp_config false maintenance.auto &&
580 git config --file ./other --get-all maintenance.repo >between &&
581 cp before expect &&
582 pwd >>expect &&
583 test_cmp expect between &&
585 git maintenance unregister --config-file ./other &&
586 git config --file ./other --get-all maintenance.repo >actual &&
587 test_cmp before actual &&
589 test_must_fail git maintenance unregister 2>err &&
590 grep "is not registered" err &&
591 git maintenance unregister --force &&
593 test_must_fail git maintenance unregister --config-file ./other 2>err &&
594 grep "is not registered" err &&
595 git maintenance unregister --config-file ./other --force
598 test_expect_success 'register with no value for maintenance.repo' '
599 cp .git/config .git/config.orig &&
600 test_when_finished mv .git/config.orig .git/config &&
602 cat >>.git/config <<-\EOF &&
603 [maintenance]
604 repo
606 cat >expect <<-\EOF &&
607 error: missing value for '\''maintenance.repo'\''
609 git maintenance register 2>actual &&
610 test_cmp expect actual &&
611 git config maintenance.repo
614 test_expect_success 'unregister with no value for maintenance.repo' '
615 cp .git/config .git/config.orig &&
616 test_when_finished mv .git/config.orig .git/config &&
618 cat >>.git/config <<-\EOF &&
619 [maintenance]
620 repo
622 cat >expect <<-\EOF &&
623 error: missing value for '\''maintenance.repo'\''
625 test_expect_code 128 git maintenance unregister 2>actual.raw &&
626 grep ^error actual.raw >actual &&
627 test_cmp expect actual &&
628 git config maintenance.repo &&
630 git maintenance unregister --force 2>actual.raw &&
631 grep ^error actual.raw >actual &&
632 test_cmp expect actual &&
633 git config maintenance.repo
636 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
637 META="a+b*c" &&
638 git init "$META" &&
639 git -C "$META" maintenance register &&
640 git config --get-all --show-origin maintenance.repo &&
641 git config --get-all --global --fixed-value \
642 maintenance.repo "$(pwd)/$META" &&
643 git -C "$META" maintenance unregister &&
644 test_must_fail git config --get-all --global --fixed-value \
645 maintenance.repo "$(pwd)/$META"
648 test_expect_success 'start --scheduler=<scheduler>' '
649 test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
650 test_grep "unrecognized --scheduler argument" err &&
652 test_expect_code 129 git maintenance start --no-scheduler 2>err &&
653 test_grep "unknown option" err &&
655 test_expect_code 128 \
656 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
657 git maintenance start --scheduler=crontab 2>err &&
658 test_grep "fatal: crontab scheduler is not available" err
661 test_expect_success 'start from empty cron table' '
662 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
664 # start registers the repo
665 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
667 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
668 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
669 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
672 test_expect_success 'stop from existing schedule' '
673 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
675 # stop does not unregister the repo
676 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
678 # Operation is idempotent
679 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
680 test_must_be_empty cron.txt
683 test_expect_success 'start preserves existing schedule' '
684 echo "Important information!" >cron.txt &&
685 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
686 grep "Important information!" cron.txt
689 test_expect_success 'magic markers are correct' '
690 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
691 cat >expect <<-\EOF &&
692 # BEGIN GIT MAINTENANCE SCHEDULE
693 # END GIT MAINTENANCE SCHEDULE
695 test_cmp actual expect
698 test_expect_success 'stop preserves surrounding schedule' '
699 echo "Crucial information!" >>cron.txt &&
700 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
701 grep "Important information!" cron.txt &&
702 grep "Crucial information!" cron.txt
705 test_expect_success 'start and stop macOS maintenance' '
706 # ensure $HOME can be compared against hook arguments on all platforms
707 pfx=$(cd "$HOME" && pwd) &&
709 write_script print-args <<-\EOF &&
710 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
713 rm -f args &&
714 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
716 # start registers the repo
717 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
719 ls "$HOME/Library/LaunchAgents" >actual &&
720 cat >expect <<-\EOF &&
721 org.git-scm.git.daily.plist
722 org.git-scm.git.hourly.plist
723 org.git-scm.git.weekly.plist
725 test_cmp expect actual &&
727 rm -f expect &&
728 for frequency in hourly daily weekly
730 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
731 test_xmllint "$PLIST" &&
732 grep schedule=$frequency "$PLIST" &&
733 echo "bootout gui/[UID] $PLIST" >>expect &&
734 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
735 done &&
736 test_cmp expect args &&
738 rm -f args &&
739 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
741 # stop does not unregister the repo
742 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
744 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
745 hourly daily weekly >expect &&
746 test_cmp expect args &&
747 ls "$HOME/Library/LaunchAgents" >actual &&
748 test_line_count = 0 actual
751 test_expect_success 'use launchctl list to prevent extra work' '
752 # ensure we are registered
753 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
755 # do it again on a fresh args file
756 rm -f args &&
757 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
759 ls "$HOME/Library/LaunchAgents" >actual &&
760 cat >expect <<-\EOF &&
761 list org.git-scm.git.hourly
762 list org.git-scm.git.daily
763 list org.git-scm.git.weekly
765 test_cmp expect args
768 test_expect_success 'start and stop Windows maintenance' '
769 write_script print-args <<-\EOF &&
770 echo $* >>args
771 while test $# -gt 0
773 case "$1" in
774 /xml) shift; xmlfile=$1; break ;;
775 *) shift ;;
776 esac
777 done
778 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
781 rm -f args &&
782 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
784 # start registers the repo
785 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
787 for frequency in hourly daily weekly
789 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
790 file=$(ls .git/schedule_${frequency}*.xml) &&
791 test_xmllint "$file" || return 1
792 done &&
794 rm -f args &&
795 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
797 # stop does not unregister the repo
798 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
800 printf "/delete /tn Git Maintenance (%s) /f\n" \
801 hourly daily weekly >expect &&
802 test_cmp expect args
805 test_expect_success 'start and stop Linux/systemd maintenance' '
806 write_script print-args <<-\EOF &&
807 printf "%s\n" "$*" >>args
810 XDG_CONFIG_HOME="$PWD" &&
811 export XDG_CONFIG_HOME &&
812 rm -f args &&
813 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
815 # start registers the repo
816 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
818 for schedule in hourly daily weekly
820 test_path_is_file "systemd/user/git-maintenance@$schedule.timer" || return 1
821 done &&
822 test_path_is_file "systemd/user/git-maintenance@.service" &&
824 test_systemd_analyze_verify "systemd/user/git-maintenance@hourly.service" &&
825 test_systemd_analyze_verify "systemd/user/git-maintenance@daily.service" &&
826 test_systemd_analyze_verify "systemd/user/git-maintenance@weekly.service" &&
828 printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
829 test_cmp expect args &&
831 rm -f args &&
832 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
834 # stop does not unregister the repo
835 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
837 for schedule in hourly daily weekly
839 test_path_is_missing "systemd/user/git-maintenance@$schedule.timer" || return 1
840 done &&
841 test_path_is_missing "systemd/user/git-maintenance@.service" &&
843 printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
844 test_cmp expect args
847 test_expect_success 'start and stop when several schedulers are available' '
848 write_script print-args <<-\EOF &&
849 printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
852 rm -f args &&
853 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
854 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
855 hourly daily weekly >expect &&
856 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
857 hourly daily weekly >>expect &&
858 printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
859 test_cmp expect args &&
861 rm -f args &&
862 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
863 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
864 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
865 hourly daily weekly >>expect &&
866 for frequency in hourly daily weekly
868 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
869 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
870 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
871 done &&
872 test_cmp expect args &&
874 rm -f args &&
875 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
876 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
877 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
878 hourly daily weekly >>expect &&
879 printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
880 hourly daily weekly >>expect &&
881 test_cmp expect args &&
883 rm -f args &&
884 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
885 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
886 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
887 hourly daily weekly >>expect &&
888 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
889 hourly daily weekly >>expect &&
890 test_cmp expect args
893 test_expect_success 'register preserves existing strategy' '
894 git config maintenance.strategy none &&
895 git maintenance register &&
896 test_config maintenance.strategy none &&
897 git config --unset maintenance.strategy &&
898 git maintenance register &&
899 test_config maintenance.strategy incremental
902 test_expect_success 'fails when running outside of a repository' '
903 nongit test_must_fail git maintenance run &&
904 nongit test_must_fail git maintenance stop &&
905 nongit test_must_fail git maintenance start &&
906 nongit test_must_fail git maintenance register &&
907 nongit test_must_fail git maintenance unregister
910 test_expect_success 'register and unregister bare repo' '
911 test_when_finished "git config --global --unset-all maintenance.repo || :" &&
912 test_might_fail git config --global --unset-all maintenance.repo &&
913 git init --bare barerepo &&
915 cd barerepo &&
916 git maintenance register &&
917 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
918 git maintenance unregister &&
919 test_must_fail git config --global --get-all maintenance.repo
923 test_expect_success 'failed schedule prevents config change' '
924 git init --bare failcase &&
926 for scheduler in crontab launchctl schtasks systemctl
928 GIT_TEST_MAINT_SCHEDULER="$scheduler:false" &&
929 export GIT_TEST_MAINT_SCHEDULER &&
930 test_must_fail \
931 git -C failcase maintenance start &&
932 test_must_fail git -C failcase config maintenance.auto || return 1
933 done
936 test_expect_success '--no-detach causes maintenance to not run in background' '
937 test_when_finished "rm -rf repo" &&
938 git init repo &&
940 cd repo &&
942 # Prepare the repository such that git-maintenance(1) ends up
943 # outputting something.
944 test_commit something &&
945 git config set maintenance.gc.enabled false &&
946 git config set maintenance.loose-objects.enabled true &&
947 git config set maintenance.loose-objects.auto 1 &&
948 git config set maintenance.incremental-repack.enabled true &&
950 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
951 git maintenance run --no-detach >out 2>&1 &&
952 ! test_region maintenance detach trace.txt
956 test_expect_success '--detach causes maintenance to run in background' '
957 test_when_finished "rm -rf repo" &&
958 git init repo &&
960 cd repo &&
962 test_commit something &&
963 git config set maintenance.gc.enabled false &&
964 git config set maintenance.loose-objects.enabled true &&
965 git config set maintenance.loose-objects.auto 1 &&
966 git config set maintenance.incremental-repack.enabled true &&
968 # The extra file descriptor gets inherited to the child
969 # process, and by reading stdout we thus essentially wait for
970 # that descriptor to get closed, which indicates that the child
971 # is done, too.
972 does_not_matter=$(GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
973 git maintenance run --detach 9>&1) &&
974 test_region maintenance detach trace.txt
978 test_expect_success 'repacking loose objects is quiet' '
979 test_when_finished "rm -rf repo" &&
980 git init repo &&
982 cd repo &&
984 test_commit something &&
985 git config set maintenance.gc.enabled false &&
986 git config set maintenance.loose-objects.enabled true &&
987 git config set maintenance.loose-objects.auto 1 &&
989 git maintenance run --quiet >out 2>&1 &&
990 test_must_be_empty out
994 test_done