The second batch
[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="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
145         test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
146         test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
147         test_path_is_missing .git/refs/remotes &&
148         git log prefetch/remotes/remote1/one &&
149         git log prefetch/remotes/remote2/two &&
150         git fetch --all &&
151         test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
152         test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
153
154         test_cmp_config refs/prefetch/ log.excludedecoration &&
155         git log --oneline --decorate --all >log &&
156         ! grep "prefetch" log &&
157
158         test_when_finished git config --unset remote.remote1.skipFetchAll &&
159         git config remote.remote1.skipFetchAll true &&
160         GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
161         test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
162         test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
163 '
164
165 test_expect_success 'prefetch and existing log.excludeDecoration values' '
166         git config --unset-all log.excludeDecoration &&
167         git config log.excludeDecoration refs/remotes/remote1/ &&
168         git maintenance run --task=prefetch &&
169
170         git config --get-all log.excludeDecoration >out &&
171         grep refs/remotes/remote1/ out &&
172         grep refs/prefetch/ out &&
173
174         git log --oneline --decorate --all >log &&
175         ! grep "prefetch" log &&
176         ! grep "remote1" log &&
177         grep "remote2" log &&
178
179         # a second run does not change the config
180         git maintenance run --task=prefetch &&
181         git log --oneline --decorate --all >log2 &&
182         test_cmp log log2
183 '
184
185 test_expect_success 'loose-objects task' '
186         # Repack everything so we know the state of the object dir
187         git repack -adk &&
188
189         # Hack to stop maintenance from running during "git commit"
190         echo in use >.git/objects/maintenance.lock &&
191
192         # Assuming that "git commit" creates at least one loose object
193         test_commit create-loose-object &&
194         rm .git/objects/maintenance.lock &&
195
196         ls .git/objects >obj-dir-before &&
197         test_file_not_empty obj-dir-before &&
198         ls .git/objects/pack/*.pack >packs-before &&
199         test_line_count = 1 packs-before &&
200
201         # The first run creates a pack-file
202         # but does not delete loose objects.
203         git maintenance run --task=loose-objects &&
204         ls .git/objects >obj-dir-between &&
205         test_cmp obj-dir-before obj-dir-between &&
206         ls .git/objects/pack/*.pack >packs-between &&
207         test_line_count = 2 packs-between &&
208         ls .git/objects/pack/loose-*.pack >loose-packs &&
209         test_line_count = 1 loose-packs &&
210
211         # The second run deletes loose objects
212         # but does not create a pack-file.
213         git maintenance run --task=loose-objects &&
214         ls .git/objects >obj-dir-after &&
215         cat >expect <<-\EOF &&
216         info
217         pack
218         EOF
219         test_cmp expect obj-dir-after &&
220         ls .git/objects/pack/*.pack >packs-after &&
221         test_cmp packs-between packs-after
222 '
223
224 test_expect_success 'maintenance.loose-objects.auto' '
225         git repack -adk &&
226         GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
227                 git -c maintenance.loose-objects.auto=1 maintenance \
228                 run --auto --task=loose-objects 2>/dev/null &&
229         test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
230         printf data-A | git hash-object -t blob --stdin -w &&
231         GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
232                 git -c maintenance.loose-objects.auto=2 \
233                 maintenance run --auto --task=loose-objects 2>/dev/null &&
234         test_subcommand ! git prune-packed --quiet <trace-loA &&
235         printf data-B | git hash-object -t blob --stdin -w &&
236         GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
237                 git -c maintenance.loose-objects.auto=2 \
238                 maintenance run --auto --task=loose-objects 2>/dev/null &&
239         test_subcommand git prune-packed --quiet <trace-loB &&
240         GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
241                 git -c maintenance.loose-objects.auto=2 \
242                 maintenance run --auto --task=loose-objects 2>/dev/null &&
243         test_subcommand git prune-packed --quiet <trace-loC
244 '
245
246 test_expect_success 'incremental-repack task' '
247         packDir=.git/objects/pack &&
248         for i in $(test_seq 1 5)
249         do
250                 test_commit $i || return 1
251         done &&
252
253         # Create three disjoint pack-files with size BIG, small, small.
254         echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
255         test_tick &&
256         git pack-objects --revs $packDir/test-2 <<-\EOF &&
257         HEAD~1
258         ^HEAD~2
259         EOF
260         test_tick &&
261         git pack-objects --revs $packDir/test-3 <<-\EOF &&
262         HEAD
263         ^HEAD~1
264         EOF
265
266         # Delete refs that have not been repacked in these packs.
267         git for-each-ref --format="delete %(refname)" \
268                 refs/prefetch refs/tags >refs &&
269         git update-ref --stdin <refs &&
270
271         # Replace the object directory with this pack layout.
272         rm -f $packDir/pack-* &&
273         rm -f $packDir/loose-* &&
274         ls $packDir/*.pack >packs-before &&
275         test_line_count = 3 packs-before &&
276
277         # the job repacks the two into a new pack, but does not
278         # delete the old ones.
279         git maintenance run --task=incremental-repack &&
280         ls $packDir/*.pack >packs-between &&
281         test_line_count = 4 packs-between &&
282
283         # the job deletes the two old packs, and does not write
284         # a new one because the batch size is not high enough to
285         # pack the largest pack-file.
286         git maintenance run --task=incremental-repack &&
287         ls .git/objects/pack/*.pack >packs-after &&
288         test_line_count = 2 packs-after
289 '
290
291 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
292         test_config core.compression 0 &&
293
294         for i in $(test_seq 1 5)
295         do
296                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
297                 return 1
298         done &&
299         git add big &&
300         git commit -qm "Add big file (1)" &&
301
302         # ensure any possible loose objects are in a pack-file
303         git maintenance run --task=loose-objects &&
304
305         rm big &&
306         for i in $(test_seq 6 10)
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 (2)" &&
313
314         # ensure any possible loose objects are in a pack-file
315         git maintenance run --task=loose-objects &&
316
317         # Now run the incremental-repack task and check the batch-size
318         GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
319                 --task=incremental-repack 2>/dev/null &&
320         test_subcommand git multi-pack-index repack \
321                  --no-progress --batch-size=2147483647 <run-2g.txt
322 '
323
324 test_expect_success 'maintenance.incremental-repack.auto' '
325         git repack -adk &&
326         git config core.multiPackIndex true &&
327         git multi-pack-index write &&
328         GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
329                 -c maintenance.incremental-repack.auto=1 \
330                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
331         test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
332         test_commit A &&
333         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
334         HEAD
335         ^HEAD~1
336         EOF
337         GIT_TRACE2_EVENT=$(pwd)/trace-A git \
338                 -c maintenance.incremental-repack.auto=2 \
339                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
340         test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
341         test_commit B &&
342         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
343         HEAD
344         ^HEAD~1
345         EOF
346         GIT_TRACE2_EVENT=$(pwd)/trace-B git \
347                 -c maintenance.incremental-repack.auto=2 \
348                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
349         test_subcommand git multi-pack-index write --no-progress <trace-B
350 '
351
352 test_expect_success 'pack-refs task' '
353         for n in $(test_seq 1 5)
354         do
355                 git branch -f to-pack/$n HEAD || return 1
356         done &&
357         GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
358                 git maintenance run --task=pack-refs &&
359         ls .git/refs/heads/ >after &&
360         test_must_be_empty after &&
361         test_subcommand git pack-refs --all --prune <pack-refs.txt
362 '
363
364 test_expect_success '--auto and --schedule incompatible' '
365         test_must_fail git maintenance run --auto --schedule=daily 2>err &&
366         test_i18ngrep "at most one" err
367 '
368
369 test_expect_success 'invalid --schedule value' '
370         test_must_fail git maintenance run --schedule=annually 2>err &&
371         test_i18ngrep "unrecognized --schedule" err
372 '
373
374 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
375         git config maintenance.loose-objects.enabled true &&
376         git config maintenance.loose-objects.schedule hourly &&
377         git config maintenance.commit-graph.enabled true &&
378         git config maintenance.commit-graph.schedule daily &&
379         git config maintenance.incremental-repack.enabled true &&
380         git config maintenance.incremental-repack.schedule weekly &&
381
382         GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
383                 git maintenance run --schedule=hourly 2>/dev/null &&
384         test_subcommand git prune-packed --quiet <hourly.txt &&
385         test_subcommand ! git commit-graph write --split --reachable \
386                 --no-progress <hourly.txt &&
387         test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
388
389         GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
390                 git maintenance run --schedule=daily 2>/dev/null &&
391         test_subcommand git prune-packed --quiet <daily.txt &&
392         test_subcommand git commit-graph write --split --reachable \
393                 --no-progress <daily.txt &&
394         test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
395
396         GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
397                 git maintenance run --schedule=weekly 2>/dev/null &&
398         test_subcommand git prune-packed --quiet <weekly.txt &&
399         test_subcommand git commit-graph write --split --reachable \
400                 --no-progress <weekly.txt &&
401         test_subcommand git multi-pack-index write --no-progress <weekly.txt
402 '
403
404 test_expect_success 'maintenance.strategy inheritance' '
405         for task in commit-graph loose-objects incremental-repack
406         do
407                 git config --unset maintenance.$task.schedule || return 1
408         done &&
409
410         test_when_finished git config --unset maintenance.strategy &&
411         git config maintenance.strategy incremental &&
412
413         GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
414                 git maintenance run --schedule=hourly --quiet &&
415         GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
416                 git maintenance run --schedule=daily --quiet &&
417         GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
418                 git maintenance run --schedule=weekly --quiet &&
419
420         test_subcommand git commit-graph write --split --reachable \
421                 --no-progress <incremental-hourly.txt &&
422         test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
423         test_subcommand ! git multi-pack-index write --no-progress \
424                 <incremental-hourly.txt &&
425         test_subcommand ! git pack-refs --all --prune \
426                 <incremental-hourly.txt &&
427
428         test_subcommand git commit-graph write --split --reachable \
429                 --no-progress <incremental-daily.txt &&
430         test_subcommand git prune-packed --quiet <incremental-daily.txt &&
431         test_subcommand git multi-pack-index write --no-progress \
432                 <incremental-daily.txt &&
433         test_subcommand ! git pack-refs --all --prune \
434                 <incremental-daily.txt &&
435
436         test_subcommand git commit-graph write --split --reachable \
437                 --no-progress <incremental-weekly.txt &&
438         test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
439         test_subcommand git multi-pack-index write --no-progress \
440                 <incremental-weekly.txt &&
441         test_subcommand git pack-refs --all --prune \
442                 <incremental-weekly.txt &&
443
444         # Modify defaults
445         git config maintenance.commit-graph.schedule daily &&
446         git config maintenance.loose-objects.schedule hourly &&
447         git config maintenance.incremental-repack.enabled false &&
448
449         GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
450                 git maintenance run --schedule=hourly --quiet &&
451         GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
452                 git maintenance run --schedule=daily --quiet &&
453
454         test_subcommand ! git commit-graph write --split --reachable \
455                 --no-progress <modified-hourly.txt &&
456         test_subcommand git prune-packed --quiet <modified-hourly.txt &&
457         test_subcommand ! git multi-pack-index write --no-progress \
458                 <modified-hourly.txt &&
459
460         test_subcommand git commit-graph write --split --reachable \
461                 --no-progress <modified-daily.txt &&
462         test_subcommand git prune-packed --quiet <modified-daily.txt &&
463         test_subcommand ! git multi-pack-index write --no-progress \
464                 <modified-daily.txt
465 '
466
467 test_expect_success 'register and unregister' '
468         test_when_finished git config --global --unset-all maintenance.repo &&
469         git config --global --add maintenance.repo /existing1 &&
470         git config --global --add maintenance.repo /existing2 &&
471         git config --global --get-all maintenance.repo >before &&
472
473         git maintenance register &&
474         test_cmp_config false maintenance.auto &&
475         git config --global --get-all maintenance.repo >between &&
476         cp before expect &&
477         pwd >>expect &&
478         test_cmp expect between &&
479
480         git maintenance unregister &&
481         git config --global --get-all maintenance.repo >actual &&
482         test_cmp before actual
483 '
484
485 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
486         META="a+b*c" &&
487         git init "$META" &&
488         git -C "$META" maintenance register &&
489         git config --get-all --show-origin maintenance.repo &&
490         git config --get-all --global --fixed-value \
491                 maintenance.repo "$(pwd)/$META" &&
492         git -C "$META" maintenance unregister &&
493         test_must_fail git config --get-all --global --fixed-value \
494                 maintenance.repo "$(pwd)/$META"
495 '
496
497 test_expect_success 'start from empty cron table' '
498         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start &&
499
500         # start registers the repo
501         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
502
503         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
504         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
505         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
506 '
507
508 test_expect_success 'stop from existing schedule' '
509         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
510
511         # stop does not unregister the repo
512         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
513
514         # Operation is idempotent
515         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
516         test_must_be_empty cron.txt
517 '
518
519 test_expect_success 'start preserves existing schedule' '
520         echo "Important information!" >cron.txt &&
521         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start &&
522         grep "Important information!" cron.txt
523 '
524
525 test_expect_success 'magic markers are correct' '
526         grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
527         cat >expect <<-\EOF &&
528         # BEGIN GIT MAINTENANCE SCHEDULE
529         # END GIT MAINTENANCE SCHEDULE
530         EOF
531         test_cmp actual expect
532 '
533
534 test_expect_success 'stop preserves surrounding schedule' '
535         echo "Crucial information!" >>cron.txt &&
536         GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
537         grep "Important information!" cron.txt &&
538         grep "Crucial information!" cron.txt
539 '
540
541 test_expect_success 'start and stop macOS maintenance' '
542         # ensure $HOME can be compared against hook arguments on all platforms
543         pfx=$(cd "$HOME" && pwd) &&
544
545         write_script print-args <<-\EOF &&
546         echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
547         EOF
548
549         rm -f args &&
550         GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start &&
551
552         # start registers the repo
553         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
554
555         ls "$HOME/Library/LaunchAgents" >actual &&
556         cat >expect <<-\EOF &&
557         org.git-scm.git.daily.plist
558         org.git-scm.git.hourly.plist
559         org.git-scm.git.weekly.plist
560         EOF
561         test_cmp expect actual &&
562
563         rm -f expect &&
564         for frequency in hourly daily weekly
565         do
566                 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
567                 test_xmllint "$PLIST" &&
568                 grep schedule=$frequency "$PLIST" &&
569                 echo "bootout gui/[UID] $PLIST" >>expect &&
570                 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
571         done &&
572         test_cmp expect args &&
573
574         rm -f args &&
575         GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
576
577         # stop does not unregister the repo
578         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
579
580         printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
581                 hourly daily weekly >expect &&
582         test_cmp expect args &&
583         ls "$HOME/Library/LaunchAgents" >actual &&
584         test_line_count = 0 actual
585 '
586
587 test_expect_success 'start and stop Windows maintenance' '
588         write_script print-args <<-\EOF &&
589         echo $* >>args
590         while test $# -gt 0
591         do
592                 case "$1" in
593                 /xml) shift; xmlfile=$1; break ;;
594                 *) shift ;;
595                 esac
596         done
597         test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
598         EOF
599
600         rm -f args &&
601         GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start &&
602
603         # start registers the repo
604         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
605
606         for frequency in hourly daily weekly
607         do
608                 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
609                 file=$(ls .git/schedule_${frequency}*.xml) &&
610                 test_xmllint "$file" || return 1
611         done &&
612
613         rm -f args &&
614         GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
615
616         # stop does not unregister the repo
617         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
618
619         printf "/delete /tn Git Maintenance (%s) /f\n" \
620                 hourly daily weekly >expect &&
621         test_cmp expect args
622 '
623
624 test_expect_success 'register preserves existing strategy' '
625         git config maintenance.strategy none &&
626         git maintenance register &&
627         test_config maintenance.strategy none &&
628         git config --unset maintenance.strategy &&
629         git maintenance register &&
630         test_config maintenance.strategy incremental
631 '
632
633 test_expect_success 'fails when running outside of a repository' '
634         nongit test_must_fail git maintenance run &&
635         nongit test_must_fail git maintenance stop &&
636         nongit test_must_fail git maintenance start &&
637         nongit test_must_fail git maintenance register &&
638         nongit test_must_fail git maintenance unregister
639 '
640
641 test_expect_success 'register and unregister bare repo' '
642         test_when_finished "git config --global --unset-all maintenance.repo || :" &&
643         test_might_fail git config --global --unset-all maintenance.repo &&
644         git init --bare barerepo &&
645         (
646                 cd barerepo &&
647                 git maintenance register &&
648                 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
649                 git maintenance unregister &&
650                 test_must_fail git config --global --get-all maintenance.repo
651         )
652 '
653
654 test_done