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