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