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