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