]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7900-maintenance.sh
maintenance: fix test t7900-maintenance.sh
[thirdparty/git.git] / t / t7900-maintenance.sh
CommitLineData
2057d750
DS
1#!/bin/sh
2
3test_description='git maintenance builtin'
4
5. ./test-lib.sh
6
663b2b1b 7GIT_TEST_COMMIT_GRAPH=0
52fe41ff 8GIT_TEST_MULTI_PACK_INDEX=0
663b2b1b 9
2afe7e35
DS
10test_lazy_prereq XMLLINT '
11 xmllint --version
12'
13
14test_xmllint () {
15 if test_have_prereq XMLLINT
16 then
17 xmllint --noout "$@"
18 else
19 true
20 fi
21}
22
b681b191 23test_lazy_prereq SYSTEMD_ANALYZE '
670e5973 24 systemd-analyze verify /lib/systemd/system/basic.target
b681b191
LH
25'
26
27test_systemd_analyze_verify () {
28 if test_have_prereq SYSTEMD_ANALYZE
29 then
30 systemd-analyze verify "$@"
31 fi
32}
33
2057d750
DS
34test_expect_success 'help text' '
35 test_expect_code 129 git maintenance -h 2>err &&
0c18b700 36 test_i18ngrep "usage: git maintenance <subcommand>" err &&
2057d750
DS
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
3ddaad0e
DS
43test_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 &&
916d0626 51 test_subcommand ! git gc --auto --quiet <run-auto.txt &&
3ddaad0e 52 test_subcommand git gc --no-quiet <run-no-quiet.txt
2057d750
DS
53'
54
1942d483
DS
55test_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
65d655b5
DS
68test_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
090511bc
DS
76test_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
d334107c
DS
93test_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
8f801804
DS
101test_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
090511bc
DS
130test_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
135test_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
28cb5e66
DS
140test_expect_success 'run --task=prefetch with no remotes' '
141 git maintenance run --task=prefetch 2>err &&
142 test_must_be_empty err
143'
144
145test_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 &&
cfd781ea
DS
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 &&
28cb5e66 158 test_path_is_missing .git/refs/remotes &&
cfd781ea
DS
159 git log prefetch/remotes/remote1/one &&
160 git log prefetch/remotes/remote2/two &&
28cb5e66 161 git fetch --all &&
cfd781ea
DS
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 &&
96eaffeb
DS
164
165 test_cmp_config refs/prefetch/ log.excludedecoration &&
166 git log --oneline --decorate --all >log &&
32f67888
DS
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
96eaffeb
DS
174'
175
176test_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
28cb5e66
DS
194'
195
252cfb7c
DS
196test_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
3e220e60
DS
235test_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
52fe41ff
DS
257test_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
3cf5f221
DS
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 &&
280 git update-ref --stdin <refs &&
281
282 # Replace the object directory with this pack layout.
52fe41ff
DS
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 # the job repacks the two into a new pack, but does not
289 # delete the old ones.
290 git maintenance run --task=incremental-repack &&
291 ls $packDir/*.pack >packs-between &&
292 test_line_count = 4 packs-between &&
293
294 # the job deletes the two old packs, and does not write
a13e3d0e
DS
295 # a new one because the batch size is not high enough to
296 # pack the largest pack-file.
52fe41ff
DS
297 git maintenance run --task=incremental-repack &&
298 ls .git/objects/pack/*.pack >packs-after &&
a13e3d0e
DS
299 test_line_count = 2 packs-after
300'
301
302test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
a0c5ccc1
JK
303 test_config core.compression 0 &&
304
a13e3d0e
DS
305 for i in $(test_seq 1 5)
306 do
307 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
308 return 1
309 done &&
310 git add big &&
a0c5ccc1 311 git commit -qm "Add big file (1)" &&
a13e3d0e
DS
312
313 # ensure any possible loose objects are in a pack-file
314 git maintenance run --task=loose-objects &&
315
316 rm big &&
317 for i in $(test_seq 6 10)
318 do
319 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
320 return 1
321 done &&
322 git add big &&
a0c5ccc1 323 git commit -qm "Add big file (2)" &&
a13e3d0e
DS
324
325 # ensure any possible loose objects are in a pack-file
326 git maintenance run --task=loose-objects &&
327
328 # Now run the incremental-repack task and check the batch-size
329 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
330 --task=incremental-repack 2>/dev/null &&
331 test_subcommand git multi-pack-index repack \
332 --no-progress --batch-size=2147483647 <run-2g.txt
52fe41ff
DS
333'
334
e841a79a
DS
335test_expect_success 'maintenance.incremental-repack.auto' '
336 git repack -adk &&
337 git config core.multiPackIndex true &&
338 git multi-pack-index write &&
339 GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
340 -c maintenance.incremental-repack.auto=1 \
341 maintenance run --auto --task=incremental-repack 2>/dev/null &&
342 test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
343 test_commit A &&
344 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
345 HEAD
346 ^HEAD~1
347 EOF
348 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
349 -c maintenance.incremental-repack.auto=2 \
350 maintenance run --auto --task=incremental-repack 2>/dev/null &&
351 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
352 test_commit B &&
353 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
354 HEAD
355 ^HEAD~1
356 EOF
357 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
358 -c maintenance.incremental-repack.auto=2 \
359 maintenance run --auto --task=incremental-repack 2>/dev/null &&
360 test_subcommand git multi-pack-index write --no-progress <trace-B
361'
362
41abfe15
DS
363test_expect_success 'pack-refs task' '
364 for n in $(test_seq 1 5)
365 do
366 git branch -f to-pack/$n HEAD || return 1
367 done &&
368 GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
369 git maintenance run --task=pack-refs &&
370 ls .git/refs/heads/ >after &&
371 test_must_be_empty after &&
372 test_subcommand git pack-refs --all --prune <pack-refs.txt
373'
374
b08ff1fe
DS
375test_expect_success '--auto and --schedule incompatible' '
376 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
377 test_i18ngrep "at most one" err
378'
379
380test_expect_success 'invalid --schedule value' '
381 test_must_fail git maintenance run --schedule=annually 2>err &&
382 test_i18ngrep "unrecognized --schedule" err
383'
384
385test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
386 git config maintenance.loose-objects.enabled true &&
387 git config maintenance.loose-objects.schedule hourly &&
388 git config maintenance.commit-graph.enabled true &&
389 git config maintenance.commit-graph.schedule daily &&
390 git config maintenance.incremental-repack.enabled true &&
391 git config maintenance.incremental-repack.schedule weekly &&
392
393 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
394 git maintenance run --schedule=hourly 2>/dev/null &&
395 test_subcommand git prune-packed --quiet <hourly.txt &&
396 test_subcommand ! git commit-graph write --split --reachable \
397 --no-progress <hourly.txt &&
398 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
399
400 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
401 git maintenance run --schedule=daily 2>/dev/null &&
402 test_subcommand git prune-packed --quiet <daily.txt &&
403 test_subcommand git commit-graph write --split --reachable \
404 --no-progress <daily.txt &&
405 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
406
407 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
408 git maintenance run --schedule=weekly 2>/dev/null &&
409 test_subcommand git prune-packed --quiet <weekly.txt &&
410 test_subcommand git commit-graph write --split --reachable \
411 --no-progress <weekly.txt &&
412 test_subcommand git multi-pack-index write --no-progress <weekly.txt
413'
414
a4cb1a23
DS
415test_expect_success 'maintenance.strategy inheritance' '
416 for task in commit-graph loose-objects incremental-repack
417 do
418 git config --unset maintenance.$task.schedule || return 1
419 done &&
420
421 test_when_finished git config --unset maintenance.strategy &&
422 git config maintenance.strategy incremental &&
423
424 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
425 git maintenance run --schedule=hourly --quiet &&
426 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
427 git maintenance run --schedule=daily --quiet &&
acc1c4d5
DS
428 GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
429 git maintenance run --schedule=weekly --quiet &&
a4cb1a23
DS
430
431 test_subcommand git commit-graph write --split --reachable \
432 --no-progress <incremental-hourly.txt &&
433 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
434 test_subcommand ! git multi-pack-index write --no-progress \
435 <incremental-hourly.txt &&
acc1c4d5
DS
436 test_subcommand ! git pack-refs --all --prune \
437 <incremental-hourly.txt &&
a4cb1a23
DS
438
439 test_subcommand git commit-graph write --split --reachable \
440 --no-progress <incremental-daily.txt &&
441 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
442 test_subcommand git multi-pack-index write --no-progress \
443 <incremental-daily.txt &&
acc1c4d5
DS
444 test_subcommand ! git pack-refs --all --prune \
445 <incremental-daily.txt &&
446
447 test_subcommand git commit-graph write --split --reachable \
448 --no-progress <incremental-weekly.txt &&
449 test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
450 test_subcommand git multi-pack-index write --no-progress \
451 <incremental-weekly.txt &&
452 test_subcommand git pack-refs --all --prune \
453 <incremental-weekly.txt &&
a4cb1a23
DS
454
455 # Modify defaults
456 git config maintenance.commit-graph.schedule daily &&
457 git config maintenance.loose-objects.schedule hourly &&
458 git config maintenance.incremental-repack.enabled false &&
459
460 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
461 git maintenance run --schedule=hourly --quiet &&
462 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
463 git maintenance run --schedule=daily --quiet &&
464
465 test_subcommand ! git commit-graph write --split --reachable \
466 --no-progress <modified-hourly.txt &&
467 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
468 test_subcommand ! git multi-pack-index write --no-progress \
469 <modified-hourly.txt &&
470
471 test_subcommand git commit-graph write --split --reachable \
472 --no-progress <modified-daily.txt &&
473 test_subcommand git prune-packed --quiet <modified-daily.txt &&
474 test_subcommand ! git multi-pack-index write --no-progress \
475 <modified-daily.txt
476'
477
0c18b700
DS
478test_expect_success 'register and unregister' '
479 test_when_finished git config --global --unset-all maintenance.repo &&
480 git config --global --add maintenance.repo /existing1 &&
481 git config --global --add maintenance.repo /existing2 &&
482 git config --global --get-all maintenance.repo >before &&
61f7a383 483
0c18b700 484 git maintenance register &&
61f7a383
DS
485 test_cmp_config false maintenance.auto &&
486 git config --global --get-all maintenance.repo >between &&
487 cp before expect &&
488 pwd >>expect &&
489 test_cmp expect between &&
490
0c18b700
DS
491 git maintenance unregister &&
492 git config --global --get-all maintenance.repo >actual &&
493 test_cmp before actual
494'
495
483a6d9b
DS
496test_expect_success !MINGW 'register and unregister with regex metacharacters' '
497 META="a+b*c" &&
498 git init "$META" &&
499 git -C "$META" maintenance register &&
500 git config --get-all --show-origin maintenance.repo &&
501 git config --get-all --global --fixed-value \
502 maintenance.repo "$(pwd)/$META" &&
503 git -C "$META" maintenance unregister &&
504 test_must_fail git config --get-all --global --fixed-value \
505 maintenance.repo "$(pwd)/$META"
506'
507
eba1ba9d
LH
508test_expect_success 'start --scheduler=<scheduler>' '
509 test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
510 test_i18ngrep "unrecognized --scheduler argument" err &&
511
512 test_expect_code 129 git maintenance start --no-scheduler 2>err &&
513 test_i18ngrep "unknown option" err &&
514
515 test_expect_code 128 \
516 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
517 git maintenance start --scheduler=crontab 2>err &&
518 test_i18ngrep "fatal: crontab scheduler is not available" err
519'
520
2fec604f 521test_expect_success 'start from empty cron table' '
eba1ba9d 522 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
2fec604f
DS
523
524 # start registers the repo
610a3fc9 525 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2fec604f
DS
526
527 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
528 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
529 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
530'
531
532test_expect_success 'stop from existing schedule' '
31345d55 533 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
2fec604f
DS
534
535 # stop does not unregister the repo
610a3fc9 536 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2fec604f
DS
537
538 # Operation is idempotent
31345d55 539 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
2fec604f
DS
540 test_must_be_empty cron.txt
541'
542
543test_expect_success 'start preserves existing schedule' '
544 echo "Important information!" >cron.txt &&
eba1ba9d 545 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
2fec604f
DS
546 grep "Important information!" cron.txt
547'
548
a52df25a
549test_expect_success 'magic markers are correct' '
550 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
551 cat >expect <<-\EOF &&
552 # BEGIN GIT MAINTENANCE SCHEDULE
553 # END GIT MAINTENANCE SCHEDULE
554 EOF
555 test_cmp actual expect
556'
557
66dc0a36
558test_expect_success 'stop preserves surrounding schedule' '
559 echo "Crucial information!" >>cron.txt &&
b2ace187 560 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
66dc0a36
561 grep "Important information!" cron.txt &&
562 grep "Crucial information!" cron.txt
563'
564
2afe7e35
DS
565test_expect_success 'start and stop macOS maintenance' '
566 # ensure $HOME can be compared against hook arguments on all platforms
567 pfx=$(cd "$HOME" && pwd) &&
568
569 write_script print-args <<-\EOF &&
570 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
571 EOF
572
573 rm -f args &&
eba1ba9d 574 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
2afe7e35
DS
575
576 # start registers the repo
b2ace187 577 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2afe7e35
DS
578
579 ls "$HOME/Library/LaunchAgents" >actual &&
580 cat >expect <<-\EOF &&
581 org.git-scm.git.daily.plist
582 org.git-scm.git.hourly.plist
583 org.git-scm.git.weekly.plist
584 EOF
585 test_cmp expect actual &&
586
587 rm -f expect &&
588 for frequency in hourly daily weekly
589 do
590 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
591 test_xmllint "$PLIST" &&
592 grep schedule=$frequency "$PLIST" &&
593 echo "bootout gui/[UID] $PLIST" >>expect &&
594 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
595 done &&
596 test_cmp expect args &&
597
598 rm -f args &&
599 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
600
601 # stop does not unregister the repo
b2ace187 602 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2afe7e35
DS
603
604 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
605 hourly daily weekly >expect &&
606 test_cmp expect args &&
607 ls "$HOME/Library/LaunchAgents" >actual &&
608 test_line_count = 0 actual
609'
610
3797a0a7
DS
611test_expect_success 'start and stop Windows maintenance' '
612 write_script print-args <<-\EOF &&
613 echo $* >>args
614 while test $# -gt 0
615 do
616 case "$1" in
617 /xml) shift; xmlfile=$1; break ;;
618 *) shift ;;
619 esac
620 done
621 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
622 EOF
623
624 rm -f args &&
eba1ba9d 625 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
3797a0a7
DS
626
627 # start registers the repo
b2ace187 628 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
3797a0a7
DS
629
630 for frequency in hourly daily weekly
631 do
632 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
633 file=$(ls .git/schedule_${frequency}*.xml) &&
634 test_xmllint "$file" || return 1
635 done &&
636
637 rm -f args &&
638 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
639
640 # stop does not unregister the repo
b2ace187 641 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
3797a0a7
DS
642
643 printf "/delete /tn Git Maintenance (%s) /f\n" \
644 hourly daily weekly >expect &&
645 test_cmp expect args
646'
647
b681b191
LH
648test_expect_success 'start and stop Linux/systemd maintenance' '
649 write_script print-args <<-\EOF &&
650 printf "%s\n" "$*" >>args
651 EOF
652
653 XDG_CONFIG_HOME="$PWD" &&
654 export XDG_CONFIG_HOME &&
655 rm -f args &&
656 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
657
658 # start registers the repo
659 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
660
661 test_systemd_analyze_verify "systemd/user/git-maintenance@.service" &&
662
663 printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
664 test_cmp expect args &&
665
666 rm -f args &&
667 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
668
669 # stop does not unregister the repo
670 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
671
672 test_path_is_missing "systemd/user/git-maintenance@.timer" &&
673 test_path_is_missing "systemd/user/git-maintenance@.service" &&
674
675 printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
676 test_cmp expect args
677'
678
eba1ba9d
LH
679test_expect_success 'start and stop when several schedulers are available' '
680 write_script print-args <<-\EOF &&
681 printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
682 EOF
683
684 rm -f args &&
b681b191
LH
685 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
686 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
eba1ba9d 687 hourly daily weekly >expect &&
b681b191
LH
688 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
689 hourly daily weekly >>expect &&
690 printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
691 test_cmp expect args &&
692
693 rm -f args &&
694 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
695 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
696 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
697 hourly daily weekly >>expect &&
eba1ba9d
LH
698 for frequency in hourly daily weekly
699 do
700 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
701 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
702 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
703 done &&
704 test_cmp expect args &&
705
706 rm -f args &&
b681b191
LH
707 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
708 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
eba1ba9d 709 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
b681b191 710 hourly daily weekly >>expect &&
eba1ba9d
LH
711 printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
712 hourly daily weekly >>expect &&
713 test_cmp expect args &&
714
715 rm -f args &&
b681b191
LH
716 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
717 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
eba1ba9d 718 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
b681b191 719 hourly daily weekly >>expect &&
eba1ba9d
LH
720 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
721 hourly daily weekly >>expect &&
722 test_cmp expect args
723'
724
61f7a383
DS
725test_expect_success 'register preserves existing strategy' '
726 git config maintenance.strategy none &&
727 git maintenance register &&
728 test_config maintenance.strategy none &&
729 git config --unset maintenance.strategy &&
730 git maintenance register &&
731 test_config maintenance.strategy incremental
732'
733
0a1f2d05 734test_expect_success 'fails when running outside of a repository' '
e72f7def
RS
735 nongit test_must_fail git maintenance run &&
736 nongit test_must_fail git maintenance stop &&
737 nongit test_must_fail git maintenance start &&
738 nongit test_must_fail git maintenance register &&
739 nongit test_must_fail git maintenance unregister
740'
741
26c79743
ES
742test_expect_success 'register and unregister bare repo' '
743 test_when_finished "git config --global --unset-all maintenance.repo || :" &&
744 test_might_fail git config --global --unset-all maintenance.repo &&
745 git init --bare barerepo &&
746 (
747 cd barerepo &&
748 git maintenance register &&
749 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
750 git maintenance unregister &&
751 test_must_fail git config --global --get-all maintenance.repo
752 )
753'
754
2057d750 755test_done