3 test_description
='git maintenance builtin'
7 GIT_TEST_COMMIT_GRAPH
=0
8 GIT_TEST_MULTI_PACK_INDEX
=0
10 test_lazy_prereq XMLLINT
'
15 if test_have_prereq XMLLINT
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
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 <run-no-auto.txt &&
53 test_subcommand ! git gc --auto --quiet <run-auto.txt &&
54 test_subcommand git gc --no-quiet <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 <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 <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 <false
70 test_expect_success
'register uses XDG_CONFIG_HOME config if it exists' '
71 test_when_finished rm -r .config/git/config &&
73 XDG_CONFIG_HOME=.config &&
74 export XDG_CONFIG_HOME &&
75 mkdir -p $XDG_CONFIG_HOME/git &&
76 >$XDG_CONFIG_HOME/git/config &&
77 git maintenance register &&
78 git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
80 test_cmp expect actual
84 test_expect_success
'register does not need XDG_CONFIG_HOME config to exist' '
85 test_when_finished git maintenance unregister &&
86 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
87 git maintenance register &&
88 git config --global --get maintenance.repo >actual &&
90 test_cmp expect actual
93 test_expect_success
'unregister uses XDG_CONFIG_HOME config if it exists' '
94 test_when_finished rm -r .config/git/config &&
96 XDG_CONFIG_HOME=.config &&
97 export XDG_CONFIG_HOME &&
98 mkdir -p $XDG_CONFIG_HOME/git &&
99 >$XDG_CONFIG_HOME/git/config &&
100 git maintenance register &&
101 git maintenance unregister &&
102 test_must_fail git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
103 test_must_be_empty actual
107 test_expect_success
'unregister does not need XDG_CONFIG_HOME config to exist' '
108 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
109 git maintenance register &&
110 git maintenance unregister &&
111 test_must_fail git config --global --get maintenance.repo >actual &&
112 test_must_be_empty actual
115 test_expect_success
'maintenance.<task>.enabled' '
116 git config maintenance.gc.enabled false &&
117 git config maintenance.commit-graph.enabled true &&
118 GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
119 test_subcommand ! git gc --quiet <run-config.txt &&
120 test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
123 test_expect_success
'run --task=<task>' '
124 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
125 git maintenance run --task=commit-graph 2>/dev/null &&
126 GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
127 git maintenance run --task=gc 2>/dev/null &&
128 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
129 git maintenance run --task=commit-graph 2>/dev/null &&
130 GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
131 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
132 test_subcommand ! git gc --quiet <run-commit-graph.txt &&
133 test_subcommand git gc --quiet <run-gc.txt &&
134 test_subcommand git gc --quiet <run-both.txt &&
135 test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
136 test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
137 test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
140 test_expect_success
'core.commitGraph=false prevents write process' '
141 GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
142 git -c core.commitGraph=false maintenance run \
143 --task=commit-graph 2>/dev/null &&
144 test_subcommand ! git commit-graph write --split --reachable --no-progress \
148 test_expect_success
'commit-graph auto condition' '
149 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
151 GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
152 git -c maintenance.commit-graph.auto=1 $COMMAND &&
153 GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
154 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
158 GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
159 git -c maintenance.commit-graph.auto=0 $COMMAND &&
160 GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
161 git -c maintenance.commit-graph.auto=1 $COMMAND &&
163 git commit --allow-empty -m "second" &&
164 git commit --allow-empty -m "third" &&
166 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
167 git -c maintenance.commit-graph.auto=2 $COMMAND &&
169 COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
170 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
171 test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
172 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
173 test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
174 test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
177 test_expect_success
'run --task=bogus' '
178 test_must_fail git maintenance run --task=bogus 2>err &&
179 test_grep "is not a valid task" err
182 test_expect_success
'run --task duplicate' '
183 test_must_fail git maintenance run --task=gc --task=gc 2>err &&
184 test_grep "cannot be selected multiple times" err
187 test_expect_success
'run --task=prefetch with no remotes' '
188 git maintenance run --task=prefetch 2>err &&
189 test_must_be_empty err
192 test_expect_success
'prefetch multiple remotes' '
193 git clone . clone1 &&
194 git clone . clone2 &&
195 git remote add remote1 "file://$(pwd)/clone1" &&
196 git remote add remote2 "file://$(pwd)/clone2" &&
197 git -C clone1 switch -c one &&
198 git -C clone2 switch -c two &&
199 test_commit -C clone1 one &&
200 test_commit -C clone2 two &&
201 GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
202 fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
203 test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
204 test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
205 git for-each-ref refs/remotes >actual &&
206 test_must_be_empty actual &&
207 git log prefetch/remotes/remote1/one &&
208 git log prefetch/remotes/remote2/two &&
210 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
211 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
213 git log --oneline --decorate --all >log &&
214 ! grep "prefetch" log &&
216 test_when_finished git config --unset remote.remote1.skipFetchAll &&
217 git config remote.remote1.skipFetchAll true &&
218 GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
219 test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
220 test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
223 test_expect_success
'loose-objects task' '
224 # Repack everything so we know the state of the object dir
227 # Hack to stop maintenance from running during "git commit"
228 echo in use >.git/objects/maintenance.lock &&
230 # Assuming that "git commit" creates at least one loose object
231 test_commit create-loose-object &&
232 rm .git/objects/maintenance.lock &&
234 ls .git/objects >obj-dir-before &&
235 test_file_not_empty obj-dir-before &&
236 ls .git/objects/pack/*.pack >packs-before &&
237 test_line_count = 1 packs-before &&
239 # The first run creates a pack-file
240 # but does not delete loose objects.
241 git maintenance run --task=loose-objects &&
242 ls .git/objects >obj-dir-between &&
243 test_cmp obj-dir-before obj-dir-between &&
244 ls .git/objects/pack/*.pack >packs-between &&
245 test_line_count = 2 packs-between &&
246 ls .git/objects/pack/loose-*.pack >loose-packs &&
247 test_line_count = 1 loose-packs &&
249 # The second run deletes loose objects
250 # but does not create a pack-file.
251 git maintenance run --task=loose-objects &&
252 ls .git/objects >obj-dir-after &&
253 cat >expect <<-\EOF &&
257 test_cmp expect obj-dir-after &&
258 ls .git/objects/pack/*.pack >packs-after &&
259 test_cmp packs-between packs-after
262 test_expect_success
'maintenance.loose-objects.auto' '
264 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
265 git -c maintenance.loose-objects.auto=1 maintenance \
266 run --auto --task=loose-objects 2>/dev/null &&
267 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
268 printf data-A | git hash-object -t blob --stdin -w &&
269 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
270 git -c maintenance.loose-objects.auto=2 \
271 maintenance run --auto --task=loose-objects 2>/dev/null &&
272 test_subcommand ! git prune-packed --quiet <trace-loA &&
273 printf data-B | git hash-object -t blob --stdin -w &&
274 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
275 git -c maintenance.loose-objects.auto=2 \
276 maintenance run --auto --task=loose-objects 2>/dev/null &&
277 test_subcommand git prune-packed --quiet <trace-loB &&
278 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
279 git -c maintenance.loose-objects.auto=2 \
280 maintenance run --auto --task=loose-objects 2>/dev/null &&
281 test_subcommand git prune-packed --quiet <trace-loC
284 test_expect_success
'incremental-repack task' '
285 packDir=.git/objects/pack &&
286 for i in $(test_seq 1 5)
288 test_commit $i || return 1
291 # Create three disjoint pack-files with size BIG, small, small.
292 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
294 git pack-objects --revs $packDir/test-2 <<-\EOF &&
299 git pack-objects --revs $packDir/test-3 <<-\EOF &&
304 # Delete refs that have not been repacked in these packs.
305 git for-each-ref --format="delete %(refname)" \
306 refs/prefetch refs/tags refs/remotes >refs &&
307 git update-ref --stdin <refs &&
309 # Replace the object directory with this pack layout.
310 rm -f $packDir/pack-* &&
311 rm -f $packDir/loose-* &&
312 ls $packDir/*.pack >packs-before &&
313 test_line_count = 3 packs-before &&
315 # make sure we do not have any broken refs that were
316 # missed in the deletion above
319 # the job repacks the two into a new pack, but does not
320 # delete the old ones.
321 git maintenance run --task=incremental-repack &&
322 ls $packDir/*.pack >packs-between &&
323 test_line_count = 4 packs-between &&
325 # the job deletes the two old packs, and does not write
326 # a new one because the batch size is not high enough to
327 # pack the largest pack-file.
328 git maintenance run --task=incremental-repack &&
329 ls .git/objects/pack/*.pack >packs-after &&
330 test_line_count = 2 packs-after
333 test_expect_success EXPENSIVE
'incremental-repack 2g limit' '
334 test_config core.compression 0 &&
336 for i in $(test_seq 1 5)
338 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
342 git commit -qm "Add big file (1)" &&
344 # ensure any possible loose objects are in a pack-file
345 git maintenance run --task=loose-objects &&
348 for i in $(test_seq 6 10)
350 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
354 git commit -qm "Add big file (2)" &&
356 # ensure any possible loose objects are in a pack-file
357 git maintenance run --task=loose-objects &&
359 # Now run the incremental-repack task and check the batch-size
360 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
361 --task=incremental-repack 2>/dev/null &&
362 test_subcommand git multi-pack-index repack \
363 --no-progress --batch-size=2147483647 <run-2g.txt
366 run_incremental_repack_and_verify
() {
369 git multi-pack-index
write &&
370 GIT_TRACE2_EVENT
="$(pwd)/midx-init.txt" git \
371 -c maintenance.incremental-repack.auto
=1 \
372 maintenance run
--auto --task=incremental-repack
2>/dev
/null
&&
373 test_subcommand
! git multi-pack-index
write --no-progress <midx-init.txt
&&
375 git pack-objects
--revs .git
/objects
/pack
/pack
<<-\EOF &&
379 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
380 -c maintenance.incremental-repack.auto=2 \
381 maintenance run --auto --task=incremental-repack 2>/dev/null &&
382 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
384 git pack-objects --revs .git/objects/pack/pack <<-\
EOF &&
388 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
389 -c maintenance.incremental-repack.auto=2 \
390 maintenance run --auto --task=incremental-repack 2>/dev/null &&
391 test_subcommand git multi-pack-index write --no-progress <trace-B
394 test_expect_success 'maintenance.incremental-repack.auto' '
395 rm -rf incremental-repack-true &&
396 git init incremental-repack-true &&
398 cd incremental-repack-true &&
399 git config core.multiPackIndex true &&
400 run_incremental_repack_and_verify
404 test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' '
405 rm -rf incremental-repack-unset &&
406 git init incremental-repack-unset &&
408 cd incremental-repack-unset &&
409 test_unconfig core.multiPackIndex &&
410 run_incremental_repack_and_verify
414 test_expect_success 'pack-refs task' '
415 for n in $(test_seq 1 5)
417 git branch -f to-pack/$n HEAD || return 1
419 GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
420 git maintenance run --task=pack-refs &&
421 test_subcommand git pack-refs --all --prune <pack-refs.txt
424 test_expect_success '--auto and --schedule incompatible' '
425 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
426 test_grep "at most one" err
429 test_expect_success 'invalid --schedule value' '
430 test_must_fail git maintenance run --schedule=annually 2>err &&
431 test_grep "unrecognized --schedule" err
434 test_expect_success '--schedule inheritance weekly -> daily
-> hourly
' '
435 git config maintenance.loose-objects.enabled true
&&
436 git config maintenance.loose-objects.schedule hourly
&&
437 git config maintenance.commit-graph.enabled true
&&
438 git config maintenance.commit-graph.schedule daily
&&
439 git config maintenance.incremental-repack.enabled true
&&
440 git config maintenance.incremental-repack.schedule weekly
&&
442 GIT_TRACE2_EVENT
="$(pwd)/hourly.txt" \
443 git maintenance run
--schedule=hourly
2>/dev
/null
&&
444 test_subcommand git prune-packed
--quiet <hourly.txt
&&
445 test_subcommand
! git commit-graph
write --split --reachable \
446 --no-progress <hourly.txt
&&
447 test_subcommand
! git multi-pack-index
write --no-progress <hourly.txt
&&
449 GIT_TRACE2_EVENT
="$(pwd)/daily.txt" \
450 git maintenance run
--schedule=daily
2>/dev
/null
&&
451 test_subcommand git prune-packed
--quiet <daily.txt
&&
452 test_subcommand git commit-graph
write --split --reachable \
453 --no-progress <daily.txt
&&
454 test_subcommand
! git multi-pack-index
write --no-progress <daily.txt
&&
456 GIT_TRACE2_EVENT
="$(pwd)/weekly.txt" \
457 git maintenance run
--schedule=weekly
2>/dev
/null
&&
458 test_subcommand git prune-packed
--quiet <weekly.txt
&&
459 test_subcommand git commit-graph
write --split --reachable \
460 --no-progress <weekly.txt
&&
461 test_subcommand git multi-pack-index
write --no-progress <weekly.txt
464 test_expect_success 'maintenance.strategy inheritance
' '
465 for task
in commit-graph loose-objects incremental-repack
467 git config
--unset maintenance.
$task.schedule ||
return 1
470 test_when_finished git config
--unset maintenance.strategy
&&
471 git config maintenance.strategy incremental
&&
473 GIT_TRACE2_EVENT
="$(pwd)/incremental-hourly.txt" \
474 git maintenance run
--schedule=hourly
--quiet &&
475 GIT_TRACE2_EVENT
="$(pwd)/incremental-daily.txt" \
476 git maintenance run
--schedule=daily
--quiet &&
477 GIT_TRACE2_EVENT
="$(pwd)/incremental-weekly.txt" \
478 git maintenance run
--schedule=weekly
--quiet &&
480 test_subcommand git commit-graph
write --split --reachable \
481 --no-progress <incremental-hourly.txt
&&
482 test_subcommand
! git prune-packed
--quiet <incremental-hourly.txt
&&
483 test_subcommand
! git multi-pack-index
write --no-progress \
484 <incremental-hourly.txt
&&
485 test_subcommand
! git pack-refs
--all --prune \
486 <incremental-hourly.txt
&&
488 test_subcommand git commit-graph
write --split --reachable \
489 --no-progress <incremental-daily.txt
&&
490 test_subcommand git prune-packed
--quiet <incremental-daily.txt
&&
491 test_subcommand git multi-pack-index
write --no-progress \
492 <incremental-daily.txt
&&
493 test_subcommand
! git pack-refs
--all --prune \
494 <incremental-daily.txt
&&
496 test_subcommand git commit-graph
write --split --reachable \
497 --no-progress <incremental-weekly.txt
&&
498 test_subcommand git prune-packed
--quiet <incremental-weekly.txt
&&
499 test_subcommand git multi-pack-index
write --no-progress \
500 <incremental-weekly.txt
&&
501 test_subcommand git pack-refs
--all --prune \
502 <incremental-weekly.txt
&&
505 git config maintenance.commit-graph.schedule daily
&&
506 git config maintenance.loose-objects.schedule hourly
&&
507 git config maintenance.incremental-repack.enabled false
&&
509 GIT_TRACE2_EVENT
="$(pwd)/modified-hourly.txt" \
510 git maintenance run
--schedule=hourly
--quiet &&
511 GIT_TRACE2_EVENT
="$(pwd)/modified-daily.txt" \
512 git maintenance run
--schedule=daily
--quiet &&
514 test_subcommand
! git commit-graph
write --split --reachable \
515 --no-progress <modified-hourly.txt
&&
516 test_subcommand git prune-packed
--quiet <modified-hourly.txt
&&
517 test_subcommand
! git multi-pack-index
write --no-progress \
518 <modified-hourly.txt
&&
520 test_subcommand git commit-graph
write --split --reachable \
521 --no-progress <modified-daily.txt
&&
522 test_subcommand git prune-packed
--quiet <modified-daily.txt
&&
523 test_subcommand
! git multi-pack-index
write --no-progress \
527 test_expect_success 'register and unregister
' '
528 test_when_finished git config
--global --unset-all maintenance.repo
&&
530 test_must_fail git maintenance unregister
2>err
&&
531 grep "is not registered" err
&&
532 git maintenance unregister
--force &&
534 git config
--global --add maintenance.repo
/existing1
&&
535 git config
--global --add maintenance.repo
/existing2
&&
536 git config
--global --get-all maintenance.repo
>before
&&
538 git maintenance register
&&
539 test_cmp_config false maintenance.auto
&&
540 git config
--global --get-all maintenance.repo
>between
&&
543 test_cmp expect between
&&
545 git maintenance unregister
&&
546 git config
--global --get-all maintenance.repo
>actual
&&
547 test_cmp before actual
&&
549 git config
--file .
/other
--add maintenance.repo
/existing1
&&
550 git config
--file .
/other
--add maintenance.repo
/existing2
&&
551 git config
--file .
/other
--get-all maintenance.repo
>before
&&
553 git maintenance register
--config-file .
/other
&&
554 test_cmp_config false maintenance.auto
&&
555 git config
--file .
/other
--get-all maintenance.repo
>between
&&
558 test_cmp expect between
&&
560 git maintenance unregister
--config-file .
/other
&&
561 git config
--file .
/other
--get-all maintenance.repo
>actual
&&
562 test_cmp before actual
&&
564 test_must_fail git maintenance unregister
2>err
&&
565 grep "is not registered" err
&&
566 git maintenance unregister
--force &&
568 test_must_fail git maintenance unregister
--config-file .
/other
2>err
&&
569 grep "is not registered" err
&&
570 git maintenance unregister
--config-file .
/other
--force
573 test_expect_success 'register with no value
for maintenance.repo
' '
574 cp .git
/config .git
/config.orig
&&
575 test_when_finished
mv .git
/config.orig .git
/config
&&
577 cat >>.git
/config
<<-\EOF &&
581 cat >expect <<-\
EOF &&
582 error: missing value for '\''maintenance.repo'\''
584 git maintenance register 2>actual &&
585 test_cmp expect actual &&
586 git config maintenance.repo
589 test_expect_success 'unregister with no value for maintenance.repo' '
590 cp .git/config .git/config.orig &&
591 test_when_finished mv .git/config.orig .git/config &&
593 cat >>.git/config <<-\EOF &&
597 cat >expect <<-\EOF &&
598 error: missing value for '\''maintenance.repo'\''
600 test_expect_code 128 git maintenance unregister 2>actual.raw &&
601 grep ^error actual.raw >actual &&
602 test_cmp expect actual &&
603 git config maintenance.repo &&
605 git maintenance unregister --force 2>actual.raw &&
606 grep ^error actual.raw >actual &&
607 test_cmp expect actual &&
608 git config maintenance.repo
611 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
614 git -C "$META" maintenance register &&
615 git config --get-all --show-origin maintenance.repo &&
616 git config --get-all --global --fixed-value \
617 maintenance.repo "$(pwd)/$META" &&
618 git -C "$META" maintenance unregister &&
619 test_must_fail git config --get-all --global --fixed-value \
620 maintenance.repo "$(pwd)/$META"
623 test_expect_success 'start --scheduler=<scheduler>' '
624 test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
625 test_grep "unrecognized --scheduler argument" err &&
627 test_expect_code 129 git maintenance start --no-scheduler 2>err &&
628 test_grep "unknown option" err &&
630 test_expect_code 128 \
631 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
632 git maintenance start --scheduler=crontab 2>err &&
633 test_grep "fatal: crontab scheduler is not available" err
636 test_expect_success 'start from empty cron table' '
637 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
639 # start registers the repo
640 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
642 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
643 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
644 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
647 test_expect_success 'stop from existing schedule' '
648 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
650 # stop does not unregister the repo
651 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
653 # Operation is idempotent
654 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
655 test_must_be_empty cron.txt
658 test_expect_success 'start preserves existing schedule' '
659 echo "Important information!" >cron.txt &&
660 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
661 grep "Important information!" cron.txt
664 test_expect_success 'magic markers are correct' '
665 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
666 cat >expect <<-\EOF &&
667 # BEGIN GIT MAINTENANCE SCHEDULE
668 # END GIT MAINTENANCE SCHEDULE
670 test_cmp actual expect
673 test_expect_success 'stop preserves surrounding schedule' '
674 echo "Crucial information!" >>cron.txt &&
675 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
676 grep "Important information!" cron.txt &&
677 grep "Crucial information!" cron.txt
680 test_expect_success 'start and stop macOS maintenance' '
681 # ensure $HOME can be compared against hook arguments on all platforms
682 pfx=$(cd "$HOME" && pwd) &&
684 write_script print-args <<-\EOF &&
685 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
689 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
691 # start registers the repo
692 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
694 ls "$HOME/Library/LaunchAgents" >actual &&
695 cat >expect <<-\EOF &&
696 org.git-scm.git.daily.plist
697 org.git-scm.git.hourly.plist
698 org.git-scm.git.weekly.plist
700 test_cmp expect actual &&
703 for frequency in hourly daily weekly
705 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
706 test_xmllint "$PLIST" &&
707 grep schedule=$frequency "$PLIST" &&
708 echo "bootout gui/[UID] $PLIST" >>expect &&
709 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
711 test_cmp expect args &&
714 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
716 # stop does not unregister the repo
717 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
719 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
720 hourly daily weekly >expect &&
721 test_cmp expect args &&
722 ls "$HOME/Library/LaunchAgents" >actual &&
723 test_line_count = 0 actual
726 test_expect_success 'use launchctl list to prevent extra work' '
727 # ensure we are registered
728 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
730 # do it again on a fresh args file
732 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
734 ls "$HOME/Library/LaunchAgents" >actual &&
735 cat >expect <<-\EOF &&
736 list org.git-scm.git.hourly
737 list org.git-scm.git.daily
738 list org.git-scm.git.weekly
743 test_expect_success 'start and stop Windows maintenance' '
744 write_script print-args <<-\EOF &&
749 /xml) shift; xmlfile=$1; break ;;
753 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
757 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
759 # start registers the repo
760 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
762 for frequency in hourly daily weekly
764 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
765 file=$(ls .git/schedule_${frequency}*.xml) &&
766 test_xmllint "$file" || return 1
770 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
772 # stop does not unregister the repo
773 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
775 printf "/delete /tn Git Maintenance (%s) /f\n" \
776 hourly daily weekly >expect &&
780 test_expect_success 'start and stop Linux/systemd maintenance' '
781 write_script print-args <<-\EOF &&
782 printf "%s\n" "$*" >>args
785 XDG_CONFIG_HOME="$PWD" &&
786 export XDG_CONFIG_HOME &&
788 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
790 # start registers the repo
791 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
793 for schedule in hourly daily weekly
795 test_path_is_file "systemd/user/git-maintenance@$schedule.timer" || return 1
797 test_path_is_file "systemd/user/git-maintenance@.service" &&
799 test_systemd_analyze_verify "systemd/user/git-maintenance@hourly.service" &&
800 test_systemd_analyze_verify "systemd/user/git-maintenance@daily.service" &&
801 test_systemd_analyze_verify "systemd/user/git-maintenance@weekly.service" &&
803 printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
804 test_cmp expect args &&
807 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
809 # stop does not unregister the repo
810 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
812 for schedule in hourly daily weekly
814 test_path_is_missing "systemd/user/git-maintenance@$schedule.timer" || return 1
816 test_path_is_missing "systemd/user/git-maintenance@.service" &&
818 printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
822 test_expect_success 'start and stop when several schedulers are available' '
823 write_script print-args <<-\EOF &&
824 printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
828 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
829 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
830 hourly daily weekly >expect &&
831 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
832 hourly daily weekly >>expect &&
833 printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
834 test_cmp expect args &&
837 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
838 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
839 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
840 hourly daily weekly >>expect &&
841 for frequency in hourly daily weekly
843 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
844 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
845 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
847 test_cmp expect args &&
850 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
851 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
852 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
853 hourly daily weekly >>expect &&
854 printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
855 hourly daily weekly >>expect &&
856 test_cmp expect args &&
859 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
860 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
861 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
862 hourly daily weekly >>expect &&
863 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
864 hourly daily weekly >>expect &&
868 test_expect_success 'register preserves existing strategy' '
869 git config maintenance.strategy none &&
870 git maintenance register &&
871 test_config maintenance.strategy none &&
872 git config --unset maintenance.strategy &&
873 git maintenance register &&
874 test_config maintenance.strategy incremental
877 test_expect_success 'fails when running outside of a repository' '
878 nongit test_must_fail git maintenance run &&
879 nongit test_must_fail git maintenance stop &&
880 nongit test_must_fail git maintenance start &&
881 nongit test_must_fail git maintenance register &&
882 nongit test_must_fail git maintenance unregister
885 test_expect_success 'register and unregister bare repo' '
886 test_when_finished "git config --global --unset-all maintenance.repo || :" &&
887 test_might_fail git config --global --unset-all maintenance.repo &&
888 git init --bare barerepo &&
891 git maintenance register &&
892 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
893 git maintenance unregister &&
894 test_must_fail git config --global --get-all maintenance.repo
898 test_expect_success 'failed schedule prevents config change' '
899 git init --bare failcase &&
901 for scheduler in crontab launchctl schtasks systemctl
903 GIT_TEST_MAINT_SCHEDULER="$scheduler:false" &&
904 export GIT_TEST_MAINT_SCHEDULER &&
906 git -C failcase maintenance start &&
907 test_must_fail git -C failcase config maintenance.auto || return 1