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