Merge branch 'ab/progress-cleanup' into seen
[git] / t / t7900-maintenance.sh
1 #!/bin/sh
2
3 test_description='git maintenance builtin'
4
5 . ./test-lib.sh
6
7 GIT_TEST_COMMIT_GRAPH=0
8 GIT_TEST_MULTI_PACK_INDEX=0
9
10 test_lazy_prereq XMLLINT '
11         xmllint --version
12 '
13
14 test_xmllint () {
15         if test_have_prereq XMLLINT
16         then
17                 xmllint --noout "$@"
18         else
19                 true
20         fi
21 }
22
23 test_lazy_prereq SYSTEMD_ANALYZE '
24         systemd-analyze --help >out &&
25         grep verify out
26 '
27
28 test_systemd_analyze_verify () {
29         if test_have_prereq SYSTEMD_ANALYZE
30         then
31                 systemd-analyze verify "$@"
32         fi
33 }
34
35 test_expect_success 'help text' '
36         test_expect_code 129 git maintenance -h 2>err &&
37         test_i18ngrep "usage: git maintenance <subcommand>" err &&
38         test_expect_code 128 git maintenance barf 2>err &&
39         test_i18ngrep "invalid subcommand: barf" err &&
40         test_expect_code 129 git maintenance 2>err &&
41         test_i18ngrep "usage: git maintenance" err
42 '
43
44 test_expect_success 'run [--auto|--quiet]' '
45         GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
46                 git maintenance run 2>/dev/null &&
47         GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
48                 git maintenance run --auto 2>/dev/null &&
49         GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
50                 git maintenance run --no-quiet 2>/dev/null &&
51         test_subcommand git gc --quiet <run-no-auto.txt &&
52         test_subcommand ! git gc --auto --quiet <run-auto.txt &&
53         test_subcommand git gc --no-quiet <run-no-quiet.txt
54 '
55
56 test_expect_success 'maintenance.auto config option' '
57         GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
58         test_subcommand git maintenance run --auto --quiet <default &&
59         GIT_TRACE2_EVENT="$(pwd)/true" \
60                 git -c maintenance.auto=true \
61                 commit --quiet --allow-empty -m 2 &&
62         test_subcommand git maintenance run --auto --quiet  <true &&
63         GIT_TRACE2_EVENT="$(pwd)/false" \
64                 git -c maintenance.auto=false \
65                 commit --quiet --allow-empty -m 3 &&
66         test_subcommand ! git maintenance run --auto --quiet  <false
67 '
68
69 test_expect_success 'maintenance.<task>.enabled' '
70         git config maintenance.gc.enabled false &&
71         git config maintenance.commit-graph.enabled true &&
72         GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
73         test_subcommand ! git gc --quiet <run-config.txt &&
74         test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
75 '
76
77 test_expect_success 'run --task=<task>' '
78         GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
79                 git maintenance run --task=commit-graph 2>/dev/null &&
80         GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
81                 git maintenance run --task=gc 2>/dev/null &&
82         GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
83                 git maintenance run --task=commit-graph 2>/dev/null &&
84         GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
85                 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
86         test_subcommand ! git gc --quiet <run-commit-graph.txt &&
87         test_subcommand git gc --quiet <run-gc.txt &&
88         test_subcommand git gc --quiet <run-both.txt &&
89         test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
90         test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
91         test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
92 '
93
94 test_expect_success 'core.commitGraph=false prevents write process' '
95         GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
96                 git -c core.commitGraph=false maintenance run \
97                 --task=commit-graph 2>/dev/null &&
98         test_subcommand ! git commit-graph write --split --reachable --no-progress \
99                 <no-commit-graph.txt
100 '
101
102 test_expect_success 'commit-graph auto condition' '
103         COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
104
105         GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
106                 git -c maintenance.commit-graph.auto=1 $COMMAND &&
107         GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
108                 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
109
110         test_commit first &&
111
112         GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
113                 git -c maintenance.commit-graph.auto=0 $COMMAND &&
114         GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
115                 git -c maintenance.commit-graph.auto=1 $COMMAND &&
116
117         git commit --allow-empty -m "second" &&
118         git commit --allow-empty -m "third" &&
119
120         GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
121                 git -c maintenance.commit-graph.auto=2 $COMMAND &&
122
123         COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
124         test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
125         test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
126         test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
127         test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
128         test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
129 '
130
131 test_expect_success 'run --task=bogus' '
132         test_must_fail git maintenance run --task=bogus 2>err &&
133         test_i18ngrep "is not a valid task" err
134 '
135
136 test_expect_success 'run --task duplicate' '
137         test_must_fail git maintenance run --task=gc --task=gc 2>err &&
138         test_i18ngrep "cannot be selected multiple times" err
139 '
140
141 test_expect_success 'run --task=prefetch with no remotes' '
142         git maintenance run --task=prefetch 2>err &&
143         test_must_be_empty err
144 '
145
146 test_expect_success 'prefetch multiple remotes' '
147         git clone . clone1 &&
148         git clone . clone2 &&
149         git remote add remote1 "file://$(pwd)/clone1" &&
150         git remote add remote2 "file://$(pwd)/clone2" &&
151         git -C clone1 switch -c one &&
152         git -C clone2 switch -c two &&
153         test_commit -C clone1 one &&
154         test_commit -C clone2 two &&
155         GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
156         fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
157         test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
158         test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
159         test_path_is_missing .git/refs/remotes &&
160         git log prefetch/remotes/remote1/one &&
161         git log prefetch/remotes/remote2/two &&
162         git fetch --all &&
163         test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
164         test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
165
166         test_cmp_config refs/prefetch/ log.excludedecoration &&
167         git log --oneline --decorate --all >log &&
168         ! grep "prefetch" log &&
169
170         test_when_finished git config --unset remote.remote1.skipFetchAll &&
171         git config remote.remote1.skipFetchAll true &&
172         GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
173         test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
174         test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
175 '
176
177 test_expect_success 'prefetch and existing log.excludeDecoration values' '
178         git config --unset-all log.excludeDecoration &&
179         git config log.excludeDecoration refs/remotes/remote1/ &&
180         git maintenance run --task=prefetch &&
181
182         git config --get-all log.excludeDecoration >out &&
183         grep refs/remotes/remote1/ out &&
184         grep refs/prefetch/ out &&
185
186         git log --oneline --decorate --all >log &&
187         ! grep "prefetch" log &&
188         ! grep "remote1" log &&
189         grep "remote2" log &&
190
191         # a second run does not change the config
192         git maintenance run --task=prefetch &&
193         git log --oneline --decorate --all >log2 &&
194         test_cmp log log2
195 '
196
197 test_expect_success 'loose-objects task' '
198         # Repack everything so we know the state of the object dir
199         git repack -adk &&
200
201         # Hack to stop maintenance from running during "git commit"
202         echo in use >.git/objects/maintenance.lock &&
203
204         # Assuming that "git commit" creates at least one loose object
205         test_commit create-loose-object &&
206         rm .git/objects/maintenance.lock &&
207
208         ls .git/objects >obj-dir-before &&
209         test_file_not_empty obj-dir-before &&
210         ls .git/objects/pack/*.pack >packs-before &&
211         test_line_count = 1 packs-before &&
212
213         # The first run creates a pack-file
214         # but does not delete loose objects.
215         git maintenance run --task=loose-objects &&
216         ls .git/objects >obj-dir-between &&
217         test_cmp obj-dir-before obj-dir-between &&
218         ls .git/objects/pack/*.pack >packs-between &&
219         test_line_count = 2 packs-between &&
220         ls .git/objects/pack/loose-*.pack >loose-packs &&
221         test_line_count = 1 loose-packs &&
222
223         # The second run deletes loose objects
224         # but does not create a pack-file.
225         git maintenance run --task=loose-objects &&
226         ls .git/objects >obj-dir-after &&
227         cat >expect <<-\EOF &&
228         info
229         pack
230         EOF
231         test_cmp expect obj-dir-after &&
232         ls .git/objects/pack/*.pack >packs-after &&
233         test_cmp packs-between packs-after
234 '
235
236 test_expect_success 'maintenance.loose-objects.auto' '
237         git repack -adk &&
238         GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
239                 git -c maintenance.loose-objects.auto=1 maintenance \
240                 run --auto --task=loose-objects 2>/dev/null &&
241         test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
242         printf data-A | git hash-object -t blob --stdin -w &&
243         GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
244                 git -c maintenance.loose-objects.auto=2 \
245                 maintenance run --auto --task=loose-objects 2>/dev/null &&
246         test_subcommand ! git prune-packed --quiet <trace-loA &&
247         printf data-B | git hash-object -t blob --stdin -w &&
248         GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
249                 git -c maintenance.loose-objects.auto=2 \
250                 maintenance run --auto --task=loose-objects 2>/dev/null &&
251         test_subcommand git prune-packed --quiet <trace-loB &&
252         GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
253                 git -c maintenance.loose-objects.auto=2 \
254                 maintenance run --auto --task=loose-objects 2>/dev/null &&
255         test_subcommand git prune-packed --quiet <trace-loC
256 '
257
258 test_expect_success 'incremental-repack task' '
259         packDir=.git/objects/pack &&
260         for i in $(test_seq 1 5)
261         do
262                 test_commit $i || return 1
263         done &&
264
265         # Create three disjoint pack-files with size BIG, small, small.
266         echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
267         test_tick &&
268         git pack-objects --revs $packDir/test-2 <<-\EOF &&
269         HEAD~1
270         ^HEAD~2
271         EOF
272         test_tick &&
273         git pack-objects --revs $packDir/test-3 <<-\EOF &&
274         HEAD
275         ^HEAD~1
276         EOF
277
278         # Delete refs that have not been repacked in these packs.
279         git for-each-ref --format="delete %(refname)" \
280                 refs/prefetch refs/tags >refs &&
281         git update-ref --stdin <refs &&
282
283         # Replace the object directory with this pack layout.
284         rm -f $packDir/pack-* &&
285         rm -f $packDir/loose-* &&
286         ls $packDir/*.pack >packs-before &&
287         test_line_count = 3 packs-before &&
288
289         # the job repacks the two into a new pack, but does not
290         # delete the old ones.
291         git maintenance run --task=incremental-repack &&
292         ls $packDir/*.pack >packs-between &&
293         test_line_count = 4 packs-between &&
294
295         # the job deletes the two old packs, and does not write
296         # a new one because the batch size is not high enough to
297         # pack the largest pack-file.
298         git maintenance run --task=incremental-repack &&
299         ls .git/objects/pack/*.pack >packs-after &&
300         test_line_count = 2 packs-after
301 '
302
303 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
304         test_config core.compression 0 &&
305
306         for i in $(test_seq 1 5)
307         do
308                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
309                 return 1
310         done &&
311         git add big &&
312         git commit -qm "Add big file (1)" &&
313
314         # ensure any possible loose objects are in a pack-file
315         git maintenance run --task=loose-objects &&
316
317         rm big &&
318         for i in $(test_seq 6 10)
319         do
320                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
321                 return 1
322         done &&
323         git add big &&
324         git commit -qm "Add big file (2)" &&
325
326         # ensure any possible loose objects are in a pack-file
327         git maintenance run --task=loose-objects &&
328
329         # Now run the incremental-repack task and check the batch-size
330         GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
331                 --task=incremental-repack 2>/dev/null &&
332         test_subcommand git multi-pack-index repack \
333                  --no-progress --batch-size=2147483647 <run-2g.txt
334 '
335
336 test_expect_success 'maintenance.incremental-repack.auto' '
337         git repack -adk &&
338         git config core.multiPackIndex true &&
339         git multi-pack-index write &&
340         GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
341                 -c maintenance.incremental-repack.auto=1 \
342                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
343         test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
344         test_commit A &&
345         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
346         HEAD
347         ^HEAD~1
348         EOF
349         GIT_TRACE2_EVENT=$(pwd)/trace-A git \
350                 -c maintenance.incremental-repack.auto=2 \
351                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
352         test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
353         test_commit B &&
354         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
355         HEAD
356         ^HEAD~1
357         EOF
358         GIT_TRACE2_EVENT=$(pwd)/trace-B git \
359                 -c maintenance.incremental-repack.auto=2 \
360                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
361         test_subcommand git multi-pack-index write --no-progress <trace-B
362 '
363
364 test_expect_success 'pack-refs task' '
365         for n in $(test_seq 1 5)
366         do
367                 git branch -f to-pack/$n HEAD || return 1
368         done &&
369         GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
370                 git maintenance run --task=pack-refs &&
371         test_subcommand git pack-refs --all --prune <pack-refs.txt
372 '
373
374 test_expect_success '--auto and --schedule incompatible' '
375         test_must_fail git maintenance run --auto --schedule=daily 2>err &&
376         test_i18ngrep "at most one" err
377 '
378
379 test_expect_success 'invalid --schedule value' '
380         test_must_fail git maintenance run --schedule=annually 2>err &&
381         test_i18ngrep "unrecognized --schedule" err
382 '
383
384 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
385         git config maintenance.loose-objects.enabled true &&
386         git config maintenance.loose-objects.schedule hourly &&
387         git config maintenance.commit-graph.enabled true &&
388         git config maintenance.commit-graph.schedule daily &&
389         git config maintenance.incremental-repack.enabled true &&
390         git config maintenance.incremental-repack.schedule weekly &&
391
392         GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
393                 git maintenance run --schedule=hourly 2>/dev/null &&
394         test_subcommand git prune-packed --quiet <hourly.txt &&
395         test_subcommand ! git commit-graph write --split --reachable \
396                 --no-progress <hourly.txt &&
397         test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
398
399         GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
400                 git maintenance run --schedule=daily 2>/dev/null &&
401         test_subcommand git prune-packed --quiet <daily.txt &&
402         test_subcommand git commit-graph write --split --reachable \
403                 --no-progress <daily.txt &&
404         test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
405
406         GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
407                 git maintenance run --schedule=weekly 2>/dev/null &&
408         test_subcommand git prune-packed --quiet <weekly.txt &&
409         test_subcommand git commit-graph write --split --reachable \
410                 --no-progress <weekly.txt &&
411         test_subcommand git multi-pack-index write --no-progress <weekly.txt
412 '
413
414 test_expect_success 'maintenance.strategy inheritance' '
415         for task in commit-graph loose-objects incremental-repack
416         do
417                 git config --unset maintenance.$task.schedule || return 1
418         done &&
419
420         test_when_finished git config --unset maintenance.strategy &&
421         git config maintenance.strategy incremental &&
422
423         GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
424                 git maintenance run --schedule=hourly --quiet &&
425         GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
426                 git maintenance run --schedule=daily --quiet &&
427         GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
428                 git maintenance run --schedule=weekly --quiet &&
429
430         test_subcommand git commit-graph write --split --reachable \
431                 --no-progress <incremental-hourly.txt &&
432         test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
433         test_subcommand ! git multi-pack-index write --no-progress \
434                 <incremental-hourly.txt &&
435         test_subcommand ! git pack-refs --all --prune \
436                 <incremental-hourly.txt &&
437
438         test_subcommand git commit-graph write --split --reachable \
439                 --no-progress <incremental-daily.txt &&
440         test_subcommand git prune-packed --quiet <incremental-daily.txt &&
441         test_subcommand git multi-pack-index write --no-progress \
442                 <incremental-daily.txt &&
443         test_subcommand ! git pack-refs --all --prune \
444                 <incremental-daily.txt &&
445
446         test_subcommand git commit-graph write --split --reachable \
447                 --no-progress <incremental-weekly.txt &&
448         test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
449         test_subcommand git multi-pack-index write --no-progress \
450                 <incremental-weekly.txt &&
451         test_subcommand git pack-refs --all --prune \
452                 <incremental-weekly.txt &&
453
454         # Modify defaults
455         git config maintenance.commit-graph.schedule daily &&
456         git config maintenance.loose-objects.schedule hourly &&
457         git config maintenance.incremental-repack.enabled false &&
458
459         GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
460                 git maintenance run --schedule=hourly --quiet &&
461         GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
462                 git maintenance run --schedule=daily --quiet &&
463
464         test_subcommand ! git commit-graph write --split --reachable \
465                 --no-progress <modified-hourly.txt &&
466         test_subcommand git prune-packed --quiet <modified-hourly.txt &&
467         test_subcommand ! git multi-pack-index write --no-progress \
468                 <modified-hourly.txt &&
469
470         test_subcommand git commit-graph write --split --reachable \
471                 --no-progress <modified-daily.txt &&
472         test_subcommand git prune-packed --quiet <modified-daily.txt &&
473         test_subcommand ! git multi-pack-index write --no-progress \
474                 <modified-daily.txt
475 '
476
477 test_expect_success 'register and unregister' '
478         test_when_finished git config --global --unset-all maintenance.repo &&
479         git config --global --add maintenance.repo /existing1 &&
480         git config --global --add maintenance.repo /existing2 &&
481         git config --global --get-all maintenance.repo >before &&
482
483         git maintenance register &&
484         test_cmp_config false maintenance.auto &&
485         git config --global --get-all maintenance.repo >between &&
486         cp before expect &&
487         pwd >>expect &&
488         test_cmp expect between &&
489
490         git maintenance unregister &&
491         git config --global --get-all maintenance.repo >actual &&
492         test_cmp before actual
493 '
494
495 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
496         META="a+b*c" &&
497         git init "$META" &&
498         git -C "$META" maintenance register &&
499         git config --get-all --show-origin maintenance.repo &&
500         git config --get-all --global --fixed-value \
501                 maintenance.repo "$(pwd)/$META" &&
502         git -C "$META" maintenance unregister &&
503         test_must_fail git config --get-all --global --fixed-value \
504                 maintenance.repo "$(pwd)/$META"
505 '
506
507 test_expect_success 'start --scheduler=<scheduler>' '
508         test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
509         test_i18ngrep "unrecognized --scheduler argument" err &&
510
511         test_expect_code 129 git maintenance start --no-scheduler 2>err &&
512         test_i18ngrep "unknown option" err &&
513
514         test_expect_code 128 \
515                 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
516                 git maintenance start --scheduler=crontab 2>err &&
517         test_i18ngrep "fatal: crontab scheduler is not available" err
518 '
519
520 test_expect_success 'start from empty cron table' '
521         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
522
523         # start registers the repo
524         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
525
526         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
527         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
528         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
529 '
530
531 test_expect_success 'stop from existing schedule' '
532         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
533
534         # stop does not unregister the repo
535         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
536
537         # Operation is idempotent
538         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
539         test_must_be_empty cron.txt
540 '
541
542 test_expect_success 'start preserves existing schedule' '
543         echo "Important information!" >cron.txt &&
544         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
545         grep "Important information!" cron.txt
546 '
547
548 test_expect_success 'magic markers are correct' '
549         grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
550         cat >expect <<-\EOF &&
551         # BEGIN GIT MAINTENANCE SCHEDULE
552         # END GIT MAINTENANCE SCHEDULE
553         EOF
554         test_cmp actual expect
555 '
556
557 test_expect_success 'stop preserves surrounding schedule' '
558         echo "Crucial information!" >>cron.txt &&
559         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
560         grep "Important information!" cron.txt &&
561         grep "Crucial information!" cron.txt
562 '
563
564 test_expect_success 'start and stop macOS maintenance' '
565         # ensure $HOME can be compared against hook arguments on all platforms
566         pfx=$(cd "$HOME" && pwd) &&
567
568         write_script print-args <<-\EOF &&
569         echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
570         EOF
571
572         rm -f args &&
573         GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
574
575         # start registers the repo
576         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
577
578         ls "$HOME/Library/LaunchAgents" >actual &&
579         cat >expect <<-\EOF &&
580         org.git-scm.git.daily.plist
581         org.git-scm.git.hourly.plist
582         org.git-scm.git.weekly.plist
583         EOF
584         test_cmp expect actual &&
585
586         rm -f expect &&
587         for frequency in hourly daily weekly
588         do
589                 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
590                 test_xmllint "$PLIST" &&
591                 grep schedule=$frequency "$PLIST" &&
592                 echo "bootout gui/[UID] $PLIST" >>expect &&
593                 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
594         done &&
595         test_cmp expect args &&
596
597         rm -f args &&
598         GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
599
600         # stop does not unregister the repo
601         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
602
603         printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
604                 hourly daily weekly >expect &&
605         test_cmp expect args &&
606         ls "$HOME/Library/LaunchAgents" >actual &&
607         test_line_count = 0 actual
608 '
609
610 test_expect_success 'start and stop Windows maintenance' '
611         write_script print-args <<-\EOF &&
612         echo $* >>args
613         while test $# -gt 0
614         do
615                 case "$1" in
616                 /xml) shift; xmlfile=$1; break ;;
617                 *) shift ;;
618                 esac
619         done
620         test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
621         EOF
622
623         rm -f args &&
624         GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
625
626         # start registers the repo
627         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
628
629         for frequency in hourly daily weekly
630         do
631                 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
632                 file=$(ls .git/schedule_${frequency}*.xml) &&
633                 test_xmllint "$file" || return 1
634         done &&
635
636         rm -f args &&
637         GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
638
639         # stop does not unregister the repo
640         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
641
642         printf "/delete /tn Git Maintenance (%s) /f\n" \
643                 hourly daily weekly >expect &&
644         test_cmp expect args
645 '
646
647 test_expect_success 'start and stop Linux/systemd maintenance' '
648         write_script print-args <<-\EOF &&
649         printf "%s\n" "$*" >>args
650         EOF
651
652         XDG_CONFIG_HOME="$PWD" &&
653         export XDG_CONFIG_HOME &&
654         rm -f args &&
655         GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
656
657         # start registers the repo
658         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
659
660         test_systemd_analyze_verify "systemd/user/git-maintenance@.service" &&
661
662         printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
663         test_cmp expect args &&
664
665         rm -f args &&
666         GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
667
668         # stop does not unregister the repo
669         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
670
671         test_path_is_missing "systemd/user/git-maintenance@.timer" &&
672         test_path_is_missing "systemd/user/git-maintenance@.service" &&
673
674         printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
675         test_cmp expect args
676 '
677
678 test_expect_success 'start and stop when several schedulers are available' '
679         write_script print-args <<-\EOF &&
680         printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
681         EOF
682
683         rm -f args &&
684         GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
685         printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
686                 hourly daily weekly >expect &&
687         printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
688                 hourly daily weekly >>expect &&
689         printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
690         test_cmp expect args &&
691
692         rm -f args &&
693         GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
694         printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
695         printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
696                 hourly daily weekly >>expect &&
697         for frequency in hourly daily weekly
698         do
699                 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
700                 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
701                 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
702         done &&
703         test_cmp expect args &&
704
705         rm -f args &&
706         GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
707         printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
708         printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
709                 hourly daily weekly >>expect &&
710         printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
711                 hourly daily weekly >>expect &&
712         test_cmp expect args &&
713
714         rm -f args &&
715         GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
716         printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
717         printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
718                 hourly daily weekly >>expect &&
719         printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
720                 hourly daily weekly >>expect &&
721         test_cmp expect args
722 '
723
724 test_expect_success 'register preserves existing strategy' '
725         git config maintenance.strategy none &&
726         git maintenance register &&
727         test_config maintenance.strategy none &&
728         git config --unset maintenance.strategy &&
729         git maintenance register &&
730         test_config maintenance.strategy incremental
731 '
732
733 test_expect_success 'fails when running outside of a repository' '
734         nongit test_must_fail git maintenance run &&
735         nongit test_must_fail git maintenance stop &&
736         nongit test_must_fail git maintenance start &&
737         nongit test_must_fail git maintenance register &&
738         nongit test_must_fail git maintenance unregister
739 '
740
741 test_expect_success 'register and unregister bare repo' '
742         test_when_finished "git config --global --unset-all maintenance.repo || :" &&
743         test_might_fail git config --global --unset-all maintenance.repo &&
744         git init --bare barerepo &&
745         (
746                 cd barerepo &&
747                 git maintenance register &&
748                 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
749                 git maintenance unregister &&
750                 test_must_fail git config --global --get-all maintenance.repo
751         )
752 '
753
754 test_done