]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7900-maintenance.sh
maintenance: stop writing log.excludeDecoration
[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 164
96eaffeb 165 git log --oneline --decorate --all >log &&
32f67888
DS
166 ! grep "prefetch" log &&
167
168 test_when_finished git config --unset remote.remote1.skipFetchAll &&
169 git config remote.remote1.skipFetchAll true &&
170 GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
171 test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
172 test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
96eaffeb
DS
173'
174
252cfb7c
DS
175test_expect_success 'loose-objects task' '
176 # Repack everything so we know the state of the object dir
177 git repack -adk &&
178
179 # Hack to stop maintenance from running during "git commit"
180 echo in use >.git/objects/maintenance.lock &&
181
182 # Assuming that "git commit" creates at least one loose object
183 test_commit create-loose-object &&
184 rm .git/objects/maintenance.lock &&
185
186 ls .git/objects >obj-dir-before &&
187 test_file_not_empty obj-dir-before &&
188 ls .git/objects/pack/*.pack >packs-before &&
189 test_line_count = 1 packs-before &&
190
191 # The first run creates a pack-file
192 # but does not delete loose objects.
193 git maintenance run --task=loose-objects &&
194 ls .git/objects >obj-dir-between &&
195 test_cmp obj-dir-before obj-dir-between &&
196 ls .git/objects/pack/*.pack >packs-between &&
197 test_line_count = 2 packs-between &&
198 ls .git/objects/pack/loose-*.pack >loose-packs &&
199 test_line_count = 1 loose-packs &&
200
201 # The second run deletes loose objects
202 # but does not create a pack-file.
203 git maintenance run --task=loose-objects &&
204 ls .git/objects >obj-dir-after &&
205 cat >expect <<-\EOF &&
206 info
207 pack
208 EOF
209 test_cmp expect obj-dir-after &&
210 ls .git/objects/pack/*.pack >packs-after &&
211 test_cmp packs-between packs-after
212'
213
3e220e60
DS
214test_expect_success 'maintenance.loose-objects.auto' '
215 git repack -adk &&
216 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
217 git -c maintenance.loose-objects.auto=1 maintenance \
218 run --auto --task=loose-objects 2>/dev/null &&
219 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
220 printf data-A | git hash-object -t blob --stdin -w &&
221 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
222 git -c maintenance.loose-objects.auto=2 \
223 maintenance run --auto --task=loose-objects 2>/dev/null &&
224 test_subcommand ! git prune-packed --quiet <trace-loA &&
225 printf data-B | git hash-object -t blob --stdin -w &&
226 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
227 git -c maintenance.loose-objects.auto=2 \
228 maintenance run --auto --task=loose-objects 2>/dev/null &&
229 test_subcommand git prune-packed --quiet <trace-loB &&
230 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
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-loC
234'
235
52fe41ff
DS
236test_expect_success 'incremental-repack task' '
237 packDir=.git/objects/pack &&
238 for i in $(test_seq 1 5)
239 do
240 test_commit $i || return 1
241 done &&
242
243 # Create three disjoint pack-files with size BIG, small, small.
244 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
245 test_tick &&
246 git pack-objects --revs $packDir/test-2 <<-\EOF &&
247 HEAD~1
248 ^HEAD~2
249 EOF
250 test_tick &&
251 git pack-objects --revs $packDir/test-3 <<-\EOF &&
252 HEAD
253 ^HEAD~1
254 EOF
3cf5f221
DS
255
256 # Delete refs that have not been repacked in these packs.
257 git for-each-ref --format="delete %(refname)" \
b4724242 258 refs/prefetch refs/tags refs/remotes >refs &&
3cf5f221
DS
259 git update-ref --stdin <refs &&
260
261 # Replace the object directory with this pack layout.
52fe41ff
DS
262 rm -f $packDir/pack-* &&
263 rm -f $packDir/loose-* &&
264 ls $packDir/*.pack >packs-before &&
265 test_line_count = 3 packs-before &&
266
b4724242
JK
267 # make sure we do not have any broken refs that were
268 # missed in the deletion above
269 git for-each-ref &&
270
52fe41ff
DS
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
a13e3d0e
DS
278 # a new one because the batch size is not high enough to
279 # pack the largest pack-file.
52fe41ff
DS
280 git maintenance run --task=incremental-repack &&
281 ls .git/objects/pack/*.pack >packs-after &&
a13e3d0e
DS
282 test_line_count = 2 packs-after
283'
284
285test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
a0c5ccc1
JK
286 test_config core.compression 0 &&
287
a13e3d0e
DS
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 &&
a0c5ccc1 294 git commit -qm "Add big file (1)" &&
a13e3d0e
DS
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 &&
a0c5ccc1 306 git commit -qm "Add big file (2)" &&
a13e3d0e
DS
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
52fe41ff
DS
316'
317
a897ab7e
GC
318run_incremental_repack_and_verify () {
319 test_commit A &&
e841a79a 320 git repack -adk &&
e841a79a
DS
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 &&
a897ab7e 326 test_commit B &&
e841a79a
DS
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 &&
a897ab7e 335 test_commit C &&
e841a79a
DS
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
a897ab7e
GC
344}
345
346test_expect_success 'maintenance.incremental-repack.auto' '
347 rm -rf incremental-repack-true &&
348 git init incremental-repack-true &&
349 (
350 cd incremental-repack-true &&
351 git config core.multiPackIndex true &&
352 run_incremental_repack_and_verify
353 )
354'
355
356test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' '
357 rm -rf incremental-repack-unset &&
358 git init incremental-repack-unset &&
359 (
360 cd incremental-repack-unset &&
361 test_unconfig core.multiPackIndex &&
362 run_incremental_repack_and_verify
363 )
e841a79a
DS
364'
365
41abfe15
DS
366test_expect_success 'pack-refs task' '
367 for n in $(test_seq 1 5)
368 do
369 git branch -f to-pack/$n HEAD || return 1
370 done &&
371 GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
372 git maintenance run --task=pack-refs &&
41abfe15
DS
373 test_subcommand git pack-refs --all --prune <pack-refs.txt
374'
375
b08ff1fe
DS
376test_expect_success '--auto and --schedule incompatible' '
377 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
378 test_i18ngrep "at most one" err
379'
380
381test_expect_success 'invalid --schedule value' '
382 test_must_fail git maintenance run --schedule=annually 2>err &&
383 test_i18ngrep "unrecognized --schedule" err
384'
385
386test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
387 git config maintenance.loose-objects.enabled true &&
388 git config maintenance.loose-objects.schedule hourly &&
389 git config maintenance.commit-graph.enabled true &&
390 git config maintenance.commit-graph.schedule daily &&
391 git config maintenance.incremental-repack.enabled true &&
392 git config maintenance.incremental-repack.schedule weekly &&
393
394 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
395 git maintenance run --schedule=hourly 2>/dev/null &&
396 test_subcommand git prune-packed --quiet <hourly.txt &&
397 test_subcommand ! git commit-graph write --split --reachable \
398 --no-progress <hourly.txt &&
399 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
400
401 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
402 git maintenance run --schedule=daily 2>/dev/null &&
403 test_subcommand git prune-packed --quiet <daily.txt &&
404 test_subcommand git commit-graph write --split --reachable \
405 --no-progress <daily.txt &&
406 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
407
408 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
409 git maintenance run --schedule=weekly 2>/dev/null &&
410 test_subcommand git prune-packed --quiet <weekly.txt &&
411 test_subcommand git commit-graph write --split --reachable \
412 --no-progress <weekly.txt &&
413 test_subcommand git multi-pack-index write --no-progress <weekly.txt
414'
415
a4cb1a23
DS
416test_expect_success 'maintenance.strategy inheritance' '
417 for task in commit-graph loose-objects incremental-repack
418 do
419 git config --unset maintenance.$task.schedule || return 1
420 done &&
421
422 test_when_finished git config --unset maintenance.strategy &&
423 git config maintenance.strategy incremental &&
424
425 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
426 git maintenance run --schedule=hourly --quiet &&
427 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
428 git maintenance run --schedule=daily --quiet &&
acc1c4d5
DS
429 GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
430 git maintenance run --schedule=weekly --quiet &&
a4cb1a23
DS
431
432 test_subcommand git commit-graph write --split --reachable \
433 --no-progress <incremental-hourly.txt &&
434 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
435 test_subcommand ! git multi-pack-index write --no-progress \
436 <incremental-hourly.txt &&
acc1c4d5
DS
437 test_subcommand ! git pack-refs --all --prune \
438 <incremental-hourly.txt &&
a4cb1a23
DS
439
440 test_subcommand git commit-graph write --split --reachable \
441 --no-progress <incremental-daily.txt &&
442 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
443 test_subcommand git multi-pack-index write --no-progress \
444 <incremental-daily.txt &&
acc1c4d5
DS
445 test_subcommand ! git pack-refs --all --prune \
446 <incremental-daily.txt &&
447
448 test_subcommand git commit-graph write --split --reachable \
449 --no-progress <incremental-weekly.txt &&
450 test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
451 test_subcommand git multi-pack-index write --no-progress \
452 <incremental-weekly.txt &&
453 test_subcommand git pack-refs --all --prune \
454 <incremental-weekly.txt &&
a4cb1a23
DS
455
456 # Modify defaults
457 git config maintenance.commit-graph.schedule daily &&
458 git config maintenance.loose-objects.schedule hourly &&
459 git config maintenance.incremental-repack.enabled false &&
460
461 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
462 git maintenance run --schedule=hourly --quiet &&
463 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
464 git maintenance run --schedule=daily --quiet &&
465
466 test_subcommand ! git commit-graph write --split --reachable \
467 --no-progress <modified-hourly.txt &&
468 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
469 test_subcommand ! git multi-pack-index write --no-progress \
470 <modified-hourly.txt &&
471
472 test_subcommand git commit-graph write --split --reachable \
473 --no-progress <modified-daily.txt &&
474 test_subcommand git prune-packed --quiet <modified-daily.txt &&
475 test_subcommand ! git multi-pack-index write --no-progress \
476 <modified-daily.txt
477'
478
0c18b700
DS
479test_expect_success 'register and unregister' '
480 test_when_finished git config --global --unset-all maintenance.repo &&
481 git config --global --add maintenance.repo /existing1 &&
482 git config --global --add maintenance.repo /existing2 &&
483 git config --global --get-all maintenance.repo >before &&
61f7a383 484
0c18b700 485 git maintenance register &&
61f7a383
DS
486 test_cmp_config false maintenance.auto &&
487 git config --global --get-all maintenance.repo >between &&
488 cp before expect &&
489 pwd >>expect &&
490 test_cmp expect between &&
491
0c18b700
DS
492 git maintenance unregister &&
493 git config --global --get-all maintenance.repo >actual &&
494 test_cmp before actual
495'
496
483a6d9b
DS
497test_expect_success !MINGW 'register and unregister with regex metacharacters' '
498 META="a+b*c" &&
499 git init "$META" &&
500 git -C "$META" maintenance register &&
501 git config --get-all --show-origin maintenance.repo &&
502 git config --get-all --global --fixed-value \
503 maintenance.repo "$(pwd)/$META" &&
504 git -C "$META" maintenance unregister &&
505 test_must_fail git config --get-all --global --fixed-value \
506 maintenance.repo "$(pwd)/$META"
507'
508
eba1ba9d
LH
509test_expect_success 'start --scheduler=<scheduler>' '
510 test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
511 test_i18ngrep "unrecognized --scheduler argument" err &&
512
513 test_expect_code 129 git maintenance start --no-scheduler 2>err &&
514 test_i18ngrep "unknown option" err &&
515
516 test_expect_code 128 \
517 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
518 git maintenance start --scheduler=crontab 2>err &&
519 test_i18ngrep "fatal: crontab scheduler is not available" err
520'
521
2fec604f 522test_expect_success 'start from empty cron table' '
eba1ba9d 523 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
2fec604f
DS
524
525 # start registers the repo
610a3fc9 526 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2fec604f
DS
527
528 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
529 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
530 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
531'
532
533test_expect_success 'stop from existing schedule' '
31345d55 534 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
2fec604f
DS
535
536 # stop does not unregister the repo
610a3fc9 537 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2fec604f
DS
538
539 # Operation is idempotent
31345d55 540 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
2fec604f
DS
541 test_must_be_empty cron.txt
542'
543
544test_expect_success 'start preserves existing schedule' '
545 echo "Important information!" >cron.txt &&
eba1ba9d 546 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
2fec604f
DS
547 grep "Important information!" cron.txt
548'
549
a52df25a
550test_expect_success 'magic markers are correct' '
551 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
552 cat >expect <<-\EOF &&
553 # BEGIN GIT MAINTENANCE SCHEDULE
554 # END GIT MAINTENANCE SCHEDULE
555 EOF
556 test_cmp actual expect
557'
558
66dc0a36
559test_expect_success 'stop preserves surrounding schedule' '
560 echo "Crucial information!" >>cron.txt &&
b2ace187 561 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
66dc0a36
562 grep "Important information!" cron.txt &&
563 grep "Crucial information!" cron.txt
564'
565
2afe7e35
DS
566test_expect_success 'start and stop macOS maintenance' '
567 # ensure $HOME can be compared against hook arguments on all platforms
568 pfx=$(cd "$HOME" && pwd) &&
569
570 write_script print-args <<-\EOF &&
571 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
572 EOF
573
574 rm -f args &&
eba1ba9d 575 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
2afe7e35
DS
576
577 # start registers the repo
b2ace187 578 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2afe7e35
DS
579
580 ls "$HOME/Library/LaunchAgents" >actual &&
581 cat >expect <<-\EOF &&
582 org.git-scm.git.daily.plist
583 org.git-scm.git.hourly.plist
584 org.git-scm.git.weekly.plist
585 EOF
586 test_cmp expect actual &&
587
588 rm -f expect &&
589 for frequency in hourly daily weekly
590 do
591 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
592 test_xmllint "$PLIST" &&
593 grep schedule=$frequency "$PLIST" &&
594 echo "bootout gui/[UID] $PLIST" >>expect &&
595 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
596 done &&
597 test_cmp expect args &&
598
599 rm -f args &&
600 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
601
602 # stop does not unregister the repo
b2ace187 603 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
2afe7e35
DS
604
605 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
606 hourly daily weekly >expect &&
607 test_cmp expect args &&
608 ls "$HOME/Library/LaunchAgents" >actual &&
609 test_line_count = 0 actual
a16eb6b1
DS
610'
611
612test_expect_success 'use launchctl list to prevent extra work' '
613 # ensure we are registered
ed8794ef 614 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
a16eb6b1
DS
615
616 # do it again on a fresh args file
617 rm -f args &&
ed8794ef 618 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
a16eb6b1
DS
619
620 ls "$HOME/Library/LaunchAgents" >actual &&
621 cat >expect <<-\EOF &&
622 list org.git-scm.git.hourly
623 list org.git-scm.git.daily
624 list org.git-scm.git.weekly
625 EOF
626 test_cmp expect args
2afe7e35
DS
627'
628
3797a0a7
DS
629test_expect_success 'start and stop Windows maintenance' '
630 write_script print-args <<-\EOF &&
631 echo $* >>args
632 while test $# -gt 0
633 do
634 case "$1" in
635 /xml) shift; xmlfile=$1; break ;;
636 *) shift ;;
637 esac
638 done
639 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
640 EOF
641
642 rm -f args &&
eba1ba9d 643 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
3797a0a7
DS
644
645 # start registers the repo
b2ace187 646 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
3797a0a7
DS
647
648 for frequency in hourly daily weekly
649 do
650 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
651 file=$(ls .git/schedule_${frequency}*.xml) &&
652 test_xmllint "$file" || return 1
653 done &&
654
655 rm -f args &&
656 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
657
658 # stop does not unregister the repo
b2ace187 659 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
3797a0a7
DS
660
661 printf "/delete /tn Git Maintenance (%s) /f\n" \
662 hourly daily weekly >expect &&
663 test_cmp expect args
664'
665
b681b191
LH
666test_expect_success 'start and stop Linux/systemd maintenance' '
667 write_script print-args <<-\EOF &&
668 printf "%s\n" "$*" >>args
669 EOF
670
671 XDG_CONFIG_HOME="$PWD" &&
672 export XDG_CONFIG_HOME &&
673 rm -f args &&
674 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
675
676 # start registers the repo
677 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
678
679 test_systemd_analyze_verify "systemd/user/git-maintenance@.service" &&
680
681 printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
682 test_cmp expect args &&
683
684 rm -f args &&
685 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
686
687 # stop does not unregister the repo
688 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
689
690 test_path_is_missing "systemd/user/git-maintenance@.timer" &&
691 test_path_is_missing "systemd/user/git-maintenance@.service" &&
692
693 printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
694 test_cmp expect args
695'
696
eba1ba9d
LH
697test_expect_success 'start and stop when several schedulers are available' '
698 write_script print-args <<-\EOF &&
699 printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
700 EOF
701
702 rm -f args &&
b681b191
LH
703 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
704 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
eba1ba9d 705 hourly daily weekly >expect &&
b681b191
LH
706 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
707 hourly daily weekly >>expect &&
708 printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
709 test_cmp expect args &&
710
711 rm -f args &&
712 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
713 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
714 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
715 hourly daily weekly >>expect &&
eba1ba9d
LH
716 for frequency in hourly daily weekly
717 do
718 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
719 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
720 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
721 done &&
722 test_cmp expect args &&
723
724 rm -f args &&
b681b191
LH
725 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
726 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
eba1ba9d 727 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
b681b191 728 hourly daily weekly >>expect &&
eba1ba9d
LH
729 printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
730 hourly daily weekly >>expect &&
731 test_cmp expect args &&
732
733 rm -f args &&
b681b191
LH
734 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
735 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
eba1ba9d 736 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
b681b191 737 hourly daily weekly >>expect &&
eba1ba9d
LH
738 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
739 hourly daily weekly >>expect &&
740 test_cmp expect args
741'
742
61f7a383
DS
743test_expect_success 'register preserves existing strategy' '
744 git config maintenance.strategy none &&
745 git maintenance register &&
746 test_config maintenance.strategy none &&
747 git config --unset maintenance.strategy &&
748 git maintenance register &&
749 test_config maintenance.strategy incremental
750'
751
0a1f2d05 752test_expect_success 'fails when running outside of a repository' '
e72f7def
RS
753 nongit test_must_fail git maintenance run &&
754 nongit test_must_fail git maintenance stop &&
755 nongit test_must_fail git maintenance start &&
756 nongit test_must_fail git maintenance register &&
757 nongit test_must_fail git maintenance unregister
758'
759
26c79743
ES
760test_expect_success 'register and unregister bare repo' '
761 test_when_finished "git config --global --unset-all maintenance.repo || :" &&
762 test_might_fail git config --global --unset-all maintenance.repo &&
763 git init --bare barerepo &&
764 (
765 cd barerepo &&
766 git maintenance register &&
767 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
768 git maintenance unregister &&
769 test_must_fail git config --global --get-all maintenance.repo
770 )
771'
772
2057d750 773test_done