The seventh batch
[git/gitster.git] / t / t7900-maintenance.sh
blobc224c8450c85f567bc29258e18b4a59fe6682f0a
1 #!/bin/sh
3 test_description='git maintenance builtin'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 GIT_TEST_COMMIT_GRAPH=0
9 GIT_TEST_MULTI_PACK_INDEX=0
11 test_lazy_prereq XMLLINT '
12 xmllint --version
15 test_xmllint () {
16 if test_have_prereq XMLLINT
17 then
18 xmllint --noout "$@"
19 else
20 true
24 test_lazy_prereq SYSTEMD_ANALYZE '
25 systemd-analyze verify /lib/systemd/system/basic.target
28 test_systemd_analyze_verify () {
29 if test_have_prereq SYSTEMD_ANALYZE
30 then
31 systemd-analyze verify "$@"
35 test_expect_success 'help text' '
36 test_expect_code 129 git maintenance -h >actual &&
37 test_grep "usage: git maintenance <subcommand>" actual &&
38 test_expect_code 129 git maintenance barf 2>err &&
39 test_grep "unknown subcommand: \`barf'\''" err &&
40 test_grep "usage: git maintenance" err &&
41 test_expect_code 129 git maintenance 2>err &&
42 test_grep "error: need a subcommand" err &&
43 test_grep "usage: git maintenance" err
46 test_expect_success 'run [--auto|--quiet]' '
47 GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
48 git maintenance run 2>/dev/null &&
49 GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
50 git maintenance run --auto 2>/dev/null &&
51 GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
52 git maintenance run --no-quiet 2>/dev/null &&
53 test_subcommand git gc --quiet --no-detach <run-no-auto.txt &&
54 test_subcommand ! git gc --auto --quiet --no-detach <run-auto.txt &&
55 test_subcommand git gc --no-quiet --no-detach <run-no-quiet.txt
58 test_expect_success 'maintenance.auto config option' '
59 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
60 test_subcommand git maintenance run --auto --quiet --detach <default &&
61 GIT_TRACE2_EVENT="$(pwd)/true" \
62 git -c maintenance.auto=true \
63 commit --quiet --allow-empty -m 2 &&
64 test_subcommand git maintenance run --auto --quiet --detach <true &&
65 GIT_TRACE2_EVENT="$(pwd)/false" \
66 git -c maintenance.auto=false \
67 commit --quiet --allow-empty -m 3 &&
68 test_subcommand ! git maintenance run --auto --quiet --detach <false
71 for cfg in maintenance.autoDetach gc.autoDetach
73 test_expect_success "$cfg=true config option" '
74 test_when_finished "rm -f trace" &&
75 test_config $cfg true &&
76 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
77 test_subcommand git maintenance run --auto --quiet --detach <trace
80 test_expect_success "$cfg=false config option" '
81 test_when_finished "rm -f trace" &&
82 test_config $cfg false &&
83 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
84 test_subcommand git maintenance run --auto --quiet --no-detach <trace
86 done
88 test_expect_success "maintenance.autoDetach overrides gc.autoDetach" '
89 test_when_finished "rm -f trace" &&
90 test_config maintenance.autoDetach false &&
91 test_config gc.autoDetach true &&
92 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
93 test_subcommand git maintenance run --auto --quiet --no-detach <trace
96 test_expect_success 'register uses XDG_CONFIG_HOME config if it exists' '
97 test_when_finished rm -r .config/git/config &&
99 XDG_CONFIG_HOME=.config &&
100 export XDG_CONFIG_HOME &&
101 mkdir -p $XDG_CONFIG_HOME/git &&
102 >$XDG_CONFIG_HOME/git/config &&
103 git maintenance register &&
104 git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
105 pwd >expect &&
106 test_cmp expect actual
110 test_expect_success 'register does not need XDG_CONFIG_HOME config to exist' '
111 test_when_finished git maintenance unregister &&
112 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
113 git maintenance register &&
114 git config --global --get maintenance.repo >actual &&
115 pwd >expect &&
116 test_cmp expect actual
119 test_expect_success 'unregister uses XDG_CONFIG_HOME config if it exists' '
120 test_when_finished rm -r .config/git/config &&
122 XDG_CONFIG_HOME=.config &&
123 export XDG_CONFIG_HOME &&
124 mkdir -p $XDG_CONFIG_HOME/git &&
125 >$XDG_CONFIG_HOME/git/config &&
126 git maintenance register &&
127 git maintenance unregister &&
128 test_must_fail git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
129 test_must_be_empty actual
133 test_expect_success 'unregister does not need XDG_CONFIG_HOME config to exist' '
134 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
135 git maintenance register &&
136 git maintenance unregister &&
137 test_must_fail git config --global --get maintenance.repo >actual &&
138 test_must_be_empty actual
141 test_expect_success 'maintenance.<task>.enabled' '
142 git config maintenance.gc.enabled false &&
143 git config maintenance.commit-graph.enabled true &&
144 GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
145 test_subcommand ! git gc --quiet <run-config.txt &&
146 test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
149 test_expect_success 'run --task=<task>' '
150 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
151 git maintenance run --task=commit-graph 2>/dev/null &&
152 GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
153 git maintenance run --task=gc 2>/dev/null &&
154 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
155 git maintenance run --task=commit-graph 2>/dev/null &&
156 GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
157 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
158 test_subcommand ! git gc --quiet --no-detach <run-commit-graph.txt &&
159 test_subcommand git gc --quiet --no-detach <run-gc.txt &&
160 test_subcommand git gc --quiet --no-detach <run-both.txt &&
161 test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
162 test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
163 test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
166 test_expect_success 'core.commitGraph=false prevents write process' '
167 GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
168 git -c core.commitGraph=false maintenance run \
169 --task=commit-graph 2>/dev/null &&
170 test_subcommand ! git commit-graph write --split --reachable --no-progress \
171 <no-commit-graph.txt
174 test_expect_success 'commit-graph auto condition' '
175 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
177 GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
178 git -c maintenance.commit-graph.auto=1 $COMMAND &&
179 GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
180 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
182 test_commit first &&
184 GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
185 git -c maintenance.commit-graph.auto=0 $COMMAND &&
186 GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
187 git -c maintenance.commit-graph.auto=1 $COMMAND &&
189 git commit --allow-empty -m "second" &&
190 git commit --allow-empty -m "third" &&
192 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
193 git -c maintenance.commit-graph.auto=2 $COMMAND &&
195 COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
196 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
197 test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
198 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
199 test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
200 test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
203 test_expect_success 'run --task=bogus' '
204 test_must_fail git maintenance run --task=bogus 2>err &&
205 test_grep "is not a valid task" err
208 test_expect_success 'run --task duplicate' '
209 test_must_fail git maintenance run --task=gc --task=gc 2>err &&
210 test_grep "cannot be selected multiple times" err
213 test_expect_success 'run --task=prefetch with no remotes' '
214 git maintenance run --task=prefetch 2>err &&
215 test_must_be_empty err
218 test_expect_success 'prefetch multiple remotes' '
219 git clone . clone1 &&
220 git clone . clone2 &&
221 git remote add remote1 "file://$(pwd)/clone1" &&
222 git remote add remote2 "file://$(pwd)/clone2" &&
223 git -C clone1 switch -c one &&
224 git -C clone2 switch -c two &&
225 test_commit -C clone1 one &&
226 test_commit -C clone2 two &&
227 GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
228 fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
229 test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
230 test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
231 git for-each-ref refs/remotes >actual &&
232 test_must_be_empty actual &&
233 git log prefetch/remotes/remote1/one &&
234 git log prefetch/remotes/remote2/two &&
235 git fetch --all &&
236 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
237 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
239 git log --oneline --decorate --all >log &&
240 ! grep "prefetch" log &&
242 test_when_finished git config --unset remote.remote1.skipFetchAll &&
243 git config remote.remote1.skipFetchAll true &&
244 GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
245 test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
246 test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
249 test_expect_success 'loose-objects task' '
250 # Repack everything so we know the state of the object dir
251 git repack -adk &&
253 # Hack to stop maintenance from running during "git commit"
254 echo in use >.git/objects/maintenance.lock &&
256 # Assuming that "git commit" creates at least one loose object
257 test_commit create-loose-object &&
258 rm .git/objects/maintenance.lock &&
260 ls .git/objects >obj-dir-before &&
261 test_file_not_empty obj-dir-before &&
262 ls .git/objects/pack/*.pack >packs-before &&
263 test_line_count = 1 packs-before &&
265 # The first run creates a pack-file
266 # but does not delete loose objects.
267 git maintenance run --task=loose-objects &&
268 ls .git/objects >obj-dir-between &&
269 test_cmp obj-dir-before obj-dir-between &&
270 ls .git/objects/pack/*.pack >packs-between &&
271 test_line_count = 2 packs-between &&
272 ls .git/objects/pack/loose-*.pack >loose-packs &&
273 test_line_count = 1 loose-packs &&
275 # The second run deletes loose objects
276 # but does not create a pack-file.
277 git maintenance run --task=loose-objects &&
278 ls .git/objects >obj-dir-after &&
279 cat >expect <<-\EOF &&
280 info
281 pack
283 test_cmp expect obj-dir-after &&
284 ls .git/objects/pack/*.pack >packs-after &&
285 test_cmp packs-between packs-after
288 test_expect_success 'maintenance.loose-objects.auto' '
289 git repack -adk &&
290 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
291 git -c maintenance.loose-objects.auto=1 maintenance \
292 run --auto --task=loose-objects 2>/dev/null &&
293 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
294 printf data-A | git hash-object -t blob --stdin -w &&
295 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
296 git -c maintenance.loose-objects.auto=2 \
297 maintenance run --auto --task=loose-objects 2>/dev/null &&
298 test_subcommand ! git prune-packed --quiet <trace-loA &&
299 printf data-B | git hash-object -t blob --stdin -w &&
300 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
301 git -c maintenance.loose-objects.auto=2 \
302 maintenance run --auto --task=loose-objects 2>/dev/null &&
303 test_subcommand git prune-packed --quiet <trace-loB &&
304 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
305 git -c maintenance.loose-objects.auto=2 \
306 maintenance run --auto --task=loose-objects 2>/dev/null &&
307 test_subcommand git prune-packed --quiet <trace-loC
310 test_expect_success 'incremental-repack task' '
311 packDir=.git/objects/pack &&
312 for i in $(test_seq 1 5)
314 test_commit $i || return 1
315 done &&
317 # Create three disjoint pack-files with size BIG, small, small.
318 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
319 test_tick &&
320 git pack-objects --revs $packDir/test-2 <<-\EOF &&
321 HEAD~1
322 ^HEAD~2
324 test_tick &&
325 git pack-objects --revs $packDir/test-3 <<-\EOF &&
326 HEAD
327 ^HEAD~1
330 # Delete refs that have not been repacked in these packs.
331 git for-each-ref --format="delete %(refname)" \
332 refs/prefetch refs/tags refs/remotes >refs &&
333 git update-ref --stdin <refs &&
335 # Replace the object directory with this pack layout.
336 rm -f $packDir/pack-* &&
337 rm -f $packDir/loose-* &&
338 ls $packDir/*.pack >packs-before &&
339 test_line_count = 3 packs-before &&
341 # make sure we do not have any broken refs that were
342 # missed in the deletion above
343 git for-each-ref &&
345 # the job repacks the two into a new pack, but does not
346 # delete the old ones.
347 git maintenance run --task=incremental-repack &&
348 ls $packDir/*.pack >packs-between &&
349 test_line_count = 4 packs-between &&
351 # the job deletes the two old packs, and does not write
352 # a new one because the batch size is not high enough to
353 # pack the largest pack-file.
354 git maintenance run --task=incremental-repack &&
355 ls .git/objects/pack/*.pack >packs-after &&
356 test_line_count = 2 packs-after
359 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
360 test_config core.compression 0 &&
362 for i in $(test_seq 1 5)
364 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
365 return 1
366 done &&
367 git add big &&
368 git commit -qm "Add big file (1)" &&
370 # ensure any possible loose objects are in a pack-file
371 git maintenance run --task=loose-objects &&
373 rm big &&
374 for i in $(test_seq 6 10)
376 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
377 return 1
378 done &&
379 git add big &&
380 git commit -qm "Add big file (2)" &&
382 # ensure any possible loose objects are in a pack-file
383 git maintenance run --task=loose-objects &&
385 # Now run the incremental-repack task and check the batch-size
386 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
387 --task=incremental-repack 2>/dev/null &&
388 test_subcommand git multi-pack-index repack \
389 --no-progress --batch-size=2147483647 <run-2g.txt
392 run_incremental_repack_and_verify () {
393 test_commit A &&
394 git repack -adk &&
395 git multi-pack-index write &&
396 GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
397 -c maintenance.incremental-repack.auto=1 \
398 maintenance run --auto --task=incremental-repack 2>/dev/null &&
399 test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
400 test_commit B &&
401 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
402 HEAD
403 ^HEAD~1
405 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
406 -c maintenance.incremental-repack.auto=2 \
407 maintenance run --auto --task=incremental-repack 2>/dev/null &&
408 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
409 test_commit C &&
410 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
411 HEAD
412 ^HEAD~1
414 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
415 -c maintenance.incremental-repack.auto=2 \
416 maintenance run --auto --task=incremental-repack 2>/dev/null &&
417 test_subcommand git multi-pack-index write --no-progress <trace-B
420 test_expect_success 'maintenance.incremental-repack.auto' '
421 rm -rf incremental-repack-true &&
422 git init incremental-repack-true &&
424 cd incremental-repack-true &&
425 git config core.multiPackIndex true &&
426 run_incremental_repack_and_verify
430 test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' '
431 rm -rf incremental-repack-unset &&
432 git init incremental-repack-unset &&
434 cd incremental-repack-unset &&
435 test_unconfig core.multiPackIndex &&
436 run_incremental_repack_and_verify
440 test_expect_success 'pack-refs task' '
441 for n in $(test_seq 1 5)
443 git branch -f to-pack/$n HEAD || return 1
444 done &&
445 GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
446 git maintenance run --task=pack-refs &&
447 test_subcommand git pack-refs --all --prune <pack-refs.txt
450 test_expect_success '--auto and --schedule incompatible' '
451 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
452 test_grep "at most one" err
455 test_expect_success 'invalid --schedule value' '
456 test_must_fail git maintenance run --schedule=annually 2>err &&
457 test_grep "unrecognized --schedule" err
460 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
461 git config maintenance.loose-objects.enabled true &&
462 git config maintenance.loose-objects.schedule hourly &&
463 git config maintenance.commit-graph.enabled true &&
464 git config maintenance.commit-graph.schedule daily &&
465 git config maintenance.incremental-repack.enabled true &&
466 git config maintenance.incremental-repack.schedule weekly &&
468 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
469 git maintenance run --schedule=hourly 2>/dev/null &&
470 test_subcommand git prune-packed --quiet <hourly.txt &&
471 test_subcommand ! git commit-graph write --split --reachable \
472 --no-progress <hourly.txt &&
473 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
475 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
476 git maintenance run --schedule=daily 2>/dev/null &&
477 test_subcommand git prune-packed --quiet <daily.txt &&
478 test_subcommand git commit-graph write --split --reachable \
479 --no-progress <daily.txt &&
480 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
482 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
483 git maintenance run --schedule=weekly 2>/dev/null &&
484 test_subcommand git prune-packed --quiet <weekly.txt &&
485 test_subcommand git commit-graph write --split --reachable \
486 --no-progress <weekly.txt &&
487 test_subcommand git multi-pack-index write --no-progress <weekly.txt
490 test_expect_success 'maintenance.strategy inheritance' '
491 for task in commit-graph loose-objects incremental-repack
493 git config --unset maintenance.$task.schedule || return 1
494 done &&
496 test_when_finished git config --unset maintenance.strategy &&
497 git config maintenance.strategy incremental &&
499 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
500 git maintenance run --schedule=hourly --quiet &&
501 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
502 git maintenance run --schedule=daily --quiet &&
503 GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
504 git maintenance run --schedule=weekly --quiet &&
506 test_subcommand git commit-graph write --split --reachable \
507 --no-progress <incremental-hourly.txt &&
508 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
509 test_subcommand ! git multi-pack-index write --no-progress \
510 <incremental-hourly.txt &&
511 test_subcommand ! git pack-refs --all --prune \
512 <incremental-hourly.txt &&
514 test_subcommand git commit-graph write --split --reachable \
515 --no-progress <incremental-daily.txt &&
516 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
517 test_subcommand git multi-pack-index write --no-progress \
518 <incremental-daily.txt &&
519 test_subcommand ! git pack-refs --all --prune \
520 <incremental-daily.txt &&
522 test_subcommand git commit-graph write --split --reachable \
523 --no-progress <incremental-weekly.txt &&
524 test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
525 test_subcommand git multi-pack-index write --no-progress \
526 <incremental-weekly.txt &&
527 test_subcommand git pack-refs --all --prune \
528 <incremental-weekly.txt &&
530 # Modify defaults
531 git config maintenance.commit-graph.schedule daily &&
532 git config maintenance.loose-objects.schedule hourly &&
533 git config maintenance.incremental-repack.enabled false &&
535 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
536 git maintenance run --schedule=hourly --quiet &&
537 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
538 git maintenance run --schedule=daily --quiet &&
540 test_subcommand ! git commit-graph write --split --reachable \
541 --no-progress <modified-hourly.txt &&
542 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
543 test_subcommand ! git multi-pack-index write --no-progress \
544 <modified-hourly.txt &&
546 test_subcommand git commit-graph write --split --reachable \
547 --no-progress <modified-daily.txt &&
548 test_subcommand git prune-packed --quiet <modified-daily.txt &&
549 test_subcommand ! git multi-pack-index write --no-progress \
550 <modified-daily.txt
553 test_expect_success 'register and unregister' '
554 test_when_finished git config --global --unset-all maintenance.repo &&
556 test_must_fail git maintenance unregister 2>err &&
557 grep "is not registered" err &&
558 git maintenance unregister --force &&
560 git config --global --add maintenance.repo /existing1 &&
561 git config --global --add maintenance.repo /existing2 &&
562 git config --global --get-all maintenance.repo >before &&
564 git maintenance register &&
565 test_cmp_config false maintenance.auto &&
566 git config --global --get-all maintenance.repo >between &&
567 cp before expect &&
568 pwd >>expect &&
569 test_cmp expect between &&
571 git maintenance unregister &&
572 git config --global --get-all maintenance.repo >actual &&
573 test_cmp before actual &&
575 git config --file ./other --add maintenance.repo /existing1 &&
576 git config --file ./other --add maintenance.repo /existing2 &&
577 git config --file ./other --get-all maintenance.repo >before &&
579 git maintenance register --config-file ./other &&
580 test_cmp_config false maintenance.auto &&
581 git config --file ./other --get-all maintenance.repo >between &&
582 cp before expect &&
583 pwd >>expect &&
584 test_cmp expect between &&
586 git maintenance unregister --config-file ./other &&
587 git config --file ./other --get-all maintenance.repo >actual &&
588 test_cmp before actual &&
590 test_must_fail git maintenance unregister 2>err &&
591 grep "is not registered" err &&
592 git maintenance unregister --force &&
594 test_must_fail git maintenance unregister --config-file ./other 2>err &&
595 grep "is not registered" err &&
596 git maintenance unregister --config-file ./other --force
599 test_expect_success 'register with no value for maintenance.repo' '
600 cp .git/config .git/config.orig &&
601 test_when_finished mv .git/config.orig .git/config &&
603 cat >>.git/config <<-\EOF &&
604 [maintenance]
605 repo
607 cat >expect <<-\EOF &&
608 error: missing value for '\''maintenance.repo'\''
610 git maintenance register 2>actual &&
611 test_cmp expect actual &&
612 git config maintenance.repo
615 test_expect_success 'unregister with no value for maintenance.repo' '
616 cp .git/config .git/config.orig &&
617 test_when_finished mv .git/config.orig .git/config &&
619 cat >>.git/config <<-\EOF &&
620 [maintenance]
621 repo
623 cat >expect <<-\EOF &&
624 error: missing value for '\''maintenance.repo'\''
626 test_expect_code 128 git maintenance unregister 2>actual.raw &&
627 grep ^error actual.raw >actual &&
628 test_cmp expect actual &&
629 git config maintenance.repo &&
631 git maintenance unregister --force 2>actual.raw &&
632 grep ^error actual.raw >actual &&
633 test_cmp expect actual &&
634 git config maintenance.repo
637 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
638 META="a+b*c" &&
639 git init "$META" &&
640 git -C "$META" maintenance register &&
641 git config --get-all --show-origin maintenance.repo &&
642 git config --get-all --global --fixed-value \
643 maintenance.repo "$(pwd)/$META" &&
644 git -C "$META" maintenance unregister &&
645 test_must_fail git config --get-all --global --fixed-value \
646 maintenance.repo "$(pwd)/$META"
649 test_expect_success 'start without GIT_TEST_MAINT_SCHEDULER' '
650 test_when_finished "rm -rf systemctl.log script repo" &&
651 mkdir script &&
652 write_script script/systemctl <<-\EOF &&
653 echo "$*" >>../systemctl.log
655 git init repo &&
657 cd repo &&
658 sane_unset GIT_TEST_MAINT_SCHEDULER &&
659 PATH="$PWD/../script:$PATH" git maintenance start --scheduler=systemd
660 ) &&
661 test_grep -- "--user list-timers" systemctl.log &&
662 test_grep -- "enable --now git-maintenance@" systemctl.log
665 test_expect_success 'start --scheduler=<scheduler>' '
666 test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
667 test_grep "unrecognized --scheduler argument" err &&
669 test_expect_code 129 git maintenance start --no-scheduler 2>err &&
670 test_grep "unknown option" err &&
672 test_expect_code 128 \
673 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
674 git maintenance start --scheduler=crontab 2>err &&
675 test_grep "fatal: crontab scheduler is not available" err
678 test_expect_success 'start from empty cron table' '
679 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
681 # start registers the repo
682 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
684 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
685 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
686 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
689 test_expect_success 'stop from existing schedule' '
690 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
692 # stop does not unregister the repo
693 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
695 # Operation is idempotent
696 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
697 test_must_be_empty cron.txt
700 test_expect_success 'start preserves existing schedule' '
701 echo "Important information!" >cron.txt &&
702 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
703 grep "Important information!" cron.txt
706 test_expect_success 'magic markers are correct' '
707 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
708 cat >expect <<-\EOF &&
709 # BEGIN GIT MAINTENANCE SCHEDULE
710 # END GIT MAINTENANCE SCHEDULE
712 test_cmp actual expect
715 test_expect_success 'stop preserves surrounding schedule' '
716 echo "Crucial information!" >>cron.txt &&
717 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
718 grep "Important information!" cron.txt &&
719 grep "Crucial information!" cron.txt
722 test_expect_success 'start and stop macOS maintenance' '
723 # ensure $HOME can be compared against hook arguments on all platforms
724 pfx=$(cd "$HOME" && pwd) &&
726 write_script print-args <<-\EOF &&
727 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
730 rm -f args &&
731 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
733 # start registers the repo
734 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
736 ls "$HOME/Library/LaunchAgents" >actual &&
737 cat >expect <<-\EOF &&
738 org.git-scm.git.daily.plist
739 org.git-scm.git.hourly.plist
740 org.git-scm.git.weekly.plist
742 test_cmp expect actual &&
744 rm -f expect &&
745 for frequency in hourly daily weekly
747 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
748 test_xmllint "$PLIST" &&
749 grep schedule=$frequency "$PLIST" &&
750 echo "bootout gui/[UID] $PLIST" >>expect &&
751 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
752 done &&
753 test_cmp expect args &&
755 rm -f args &&
756 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
758 # stop does not unregister the repo
759 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
761 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
762 hourly daily weekly >expect &&
763 test_cmp expect args &&
764 ls "$HOME/Library/LaunchAgents" >actual &&
765 test_line_count = 0 actual
768 test_expect_success 'use launchctl list to prevent extra work' '
769 # ensure we are registered
770 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
772 # do it again on a fresh args file
773 rm -f args &&
774 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
776 ls "$HOME/Library/LaunchAgents" >actual &&
777 cat >expect <<-\EOF &&
778 list org.git-scm.git.hourly
779 list org.git-scm.git.daily
780 list org.git-scm.git.weekly
782 test_cmp expect args
785 test_expect_success 'start and stop Windows maintenance' '
786 write_script print-args <<-\EOF &&
787 echo $* >>args
788 while test $# -gt 0
790 case "$1" in
791 /xml) shift; xmlfile=$1; break ;;
792 *) shift ;;
793 esac
794 done
795 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
798 rm -f args &&
799 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
801 # start registers the repo
802 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
804 for frequency in hourly daily weekly
806 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
807 file=$(ls .git/schedule_${frequency}*.xml) &&
808 test_xmllint "$file" || return 1
809 done &&
811 rm -f args &&
812 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
814 # stop does not unregister the repo
815 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
817 printf "/delete /tn Git Maintenance (%s) /f\n" \
818 hourly daily weekly >expect &&
819 test_cmp expect args
822 test_expect_success 'start and stop Linux/systemd maintenance' '
823 write_script print-args <<-\EOF &&
824 printf "%s\n" "$*" >>args
827 XDG_CONFIG_HOME="$PWD" &&
828 export XDG_CONFIG_HOME &&
829 rm -f args &&
830 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
832 # start registers the repo
833 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
835 for schedule in hourly daily weekly
837 test_path_is_file "systemd/user/git-maintenance@$schedule.timer" || return 1
838 done &&
839 test_path_is_file "systemd/user/git-maintenance@.service" &&
841 test_systemd_analyze_verify "systemd/user/git-maintenance@hourly.service" &&
842 test_systemd_analyze_verify "systemd/user/git-maintenance@daily.service" &&
843 test_systemd_analyze_verify "systemd/user/git-maintenance@weekly.service" &&
845 grep "core.askPass=true" "systemd/user/git-maintenance@.service" &&
846 grep "credential.interactive=false" "systemd/user/git-maintenance@.service" &&
848 printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
849 test_cmp expect args &&
851 rm -f args &&
852 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
854 # stop does not unregister the repo
855 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
857 for schedule in hourly daily weekly
859 test_path_is_missing "systemd/user/git-maintenance@$schedule.timer" || return 1
860 done &&
861 test_path_is_missing "systemd/user/git-maintenance@.service" &&
863 printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
864 test_cmp expect args
867 test_expect_success 'start and stop when several schedulers are available' '
868 write_script print-args <<-\EOF &&
869 printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
872 rm -f args &&
873 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
874 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
875 hourly daily weekly >expect &&
876 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
877 hourly daily weekly >>expect &&
878 printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
879 test_cmp expect args &&
881 rm -f args &&
882 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
883 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
884 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
885 hourly daily weekly >>expect &&
886 for frequency in hourly daily weekly
888 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
889 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
890 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
891 done &&
892 test_cmp expect args &&
894 rm -f args &&
895 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
896 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
897 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
898 hourly daily weekly >>expect &&
899 printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
900 hourly daily weekly >>expect &&
901 test_cmp expect args &&
903 rm -f args &&
904 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
905 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
906 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
907 hourly daily weekly >>expect &&
908 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
909 hourly daily weekly >>expect &&
910 test_cmp expect args
913 test_expect_success 'register preserves existing strategy' '
914 git config maintenance.strategy none &&
915 git maintenance register &&
916 test_config maintenance.strategy none &&
917 git config --unset maintenance.strategy &&
918 git maintenance register &&
919 test_config maintenance.strategy incremental
922 test_expect_success 'fails when running outside of a repository' '
923 nongit test_must_fail git maintenance run &&
924 nongit test_must_fail git maintenance stop &&
925 nongit test_must_fail git maintenance start &&
926 nongit test_must_fail git maintenance register &&
927 nongit test_must_fail git maintenance unregister
930 test_expect_success 'register and unregister bare repo' '
931 test_when_finished "git config --global --unset-all maintenance.repo || :" &&
932 test_might_fail git config --global --unset-all maintenance.repo &&
933 git init --bare barerepo &&
935 cd barerepo &&
936 git maintenance register &&
937 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
938 git maintenance unregister &&
939 test_must_fail git config --global --get-all maintenance.repo
943 test_expect_success 'failed schedule prevents config change' '
944 git init --bare failcase &&
946 for scheduler in crontab launchctl schtasks systemctl
948 GIT_TEST_MAINT_SCHEDULER="$scheduler:false" &&
949 export GIT_TEST_MAINT_SCHEDULER &&
950 test_must_fail \
951 git -C failcase maintenance start &&
952 test_must_fail git -C failcase config maintenance.auto || return 1
953 done
956 test_expect_success '--no-detach causes maintenance to not run in background' '
957 test_when_finished "rm -rf repo" &&
958 git init repo &&
960 cd repo &&
962 # Prepare the repository such that git-maintenance(1) ends up
963 # outputting something.
964 test_commit something &&
965 git config set maintenance.gc.enabled false &&
966 git config set maintenance.loose-objects.enabled true &&
967 git config set maintenance.loose-objects.auto 1 &&
968 git config set maintenance.incremental-repack.enabled true &&
970 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
971 git maintenance run --no-detach >out 2>&1 &&
972 ! test_region maintenance detach trace.txt
976 test_expect_success '--detach causes maintenance to run in background' '
977 test_when_finished "rm -rf repo" &&
978 git init repo &&
980 cd repo &&
982 test_commit something &&
983 git config set maintenance.gc.enabled false &&
984 git config set maintenance.loose-objects.enabled true &&
985 git config set maintenance.loose-objects.auto 1 &&
986 git config set maintenance.incremental-repack.enabled true &&
988 # The extra file descriptor gets inherited to the child
989 # process, and by reading stdout we thus essentially wait for
990 # that descriptor to get closed, which indicates that the child
991 # is done, too.
992 does_not_matter=$(GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
993 git maintenance run --detach 9>&1) &&
994 test_region maintenance detach trace.txt
998 test_expect_success 'repacking loose objects is quiet' '
999 test_when_finished "rm -rf repo" &&
1000 git init repo &&
1002 cd repo &&
1004 test_commit something &&
1005 git config set maintenance.gc.enabled false &&
1006 git config set maintenance.loose-objects.enabled true &&
1007 git config set maintenance.loose-objects.auto 1 &&
1009 git maintenance run --quiet >out 2>&1 &&
1010 test_must_be_empty out
1014 test_done