3 test_description
='git maintenance builtin'
7 GIT_TEST_COMMIT_GRAPH
=0
8 GIT_TEST_MULTI_PACK_INDEX
=0
10 test_lazy_prereq XMLLINT
'
15 if test_have_prereq XMLLINT
23 test_expect_success
'help text' '
24 test_expect_code 129 git maintenance -h 2>err &&
25 test_i18ngrep "usage: git maintenance <subcommand>" err &&
26 test_expect_code 128 git maintenance barf 2>err &&
27 test_i18ngrep "invalid subcommand: barf" err &&
28 test_expect_code 129 git maintenance 2>err &&
29 test_i18ngrep "usage: git maintenance" err
32 test_expect_success
'run [--auto|--quiet]' '
33 GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
34 git maintenance run 2>/dev/null &&
35 GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
36 git maintenance run --auto 2>/dev/null &&
37 GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
38 git maintenance run --no-quiet 2>/dev/null &&
39 test_subcommand git gc --quiet <run-no-auto.txt &&
40 test_subcommand ! git gc --auto --quiet <run-auto.txt &&
41 test_subcommand git gc --no-quiet <run-no-quiet.txt
44 test_expect_success
'maintenance.auto config option' '
45 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
46 test_subcommand git maintenance run --auto --quiet <default &&
47 GIT_TRACE2_EVENT="$(pwd)/true" \
48 git -c maintenance.auto=true \
49 commit --quiet --allow-empty -m 2 &&
50 test_subcommand git maintenance run --auto --quiet <true &&
51 GIT_TRACE2_EVENT="$(pwd)/false" \
52 git -c maintenance.auto=false \
53 commit --quiet --allow-empty -m 3 &&
54 test_subcommand ! git maintenance run --auto --quiet <false
57 test_expect_success
'maintenance.<task>.enabled' '
58 git config maintenance.gc.enabled false &&
59 git config maintenance.commit-graph.enabled true &&
60 GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
61 test_subcommand ! git gc --quiet <run-config.txt &&
62 test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
65 test_expect_success
'run --task=<task>' '
66 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
67 git maintenance run --task=commit-graph 2>/dev/null &&
68 GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
69 git maintenance run --task=gc 2>/dev/null &&
70 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
71 git maintenance run --task=commit-graph 2>/dev/null &&
72 GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
73 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
74 test_subcommand ! git gc --quiet <run-commit-graph.txt &&
75 test_subcommand git gc --quiet <run-gc.txt &&
76 test_subcommand git gc --quiet <run-both.txt &&
77 test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
78 test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
79 test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
82 test_expect_success
'core.commitGraph=false prevents write process' '
83 GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
84 git -c core.commitGraph=false maintenance run \
85 --task=commit-graph 2>/dev/null &&
86 test_subcommand ! git commit-graph write --split --reachable --no-progress \
90 test_expect_success
'commit-graph auto condition' '
91 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
93 GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
94 git -c maintenance.commit-graph.auto=1 $COMMAND &&
95 GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
96 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
100 GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
101 git -c maintenance.commit-graph.auto=0 $COMMAND &&
102 GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
103 git -c maintenance.commit-graph.auto=1 $COMMAND &&
105 git commit --allow-empty -m "second" &&
106 git commit --allow-empty -m "third" &&
108 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
109 git -c maintenance.commit-graph.auto=2 $COMMAND &&
111 COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
112 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
113 test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
114 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
115 test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
116 test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
119 test_expect_success
'run --task=bogus' '
120 test_must_fail git maintenance run --task=bogus 2>err &&
121 test_i18ngrep "is not a valid task" err
124 test_expect_success
'run --task duplicate' '
125 test_must_fail git maintenance run --task=gc --task=gc 2>err &&
126 test_i18ngrep "cannot be selected multiple times" err
129 test_expect_success
'run --task=prefetch with no remotes' '
130 git maintenance run --task=prefetch 2>err &&
131 test_must_be_empty err
134 test_expect_success
'prefetch multiple remotes' '
135 git clone . clone1 &&
136 git clone . clone2 &&
137 git remote add remote1 "file://$(pwd)/clone1" &&
138 git remote add remote2 "file://$(pwd)/clone2" &&
139 git -C clone1 switch -c one &&
140 git -C clone2 switch -c two &&
141 test_commit -C clone1 one &&
142 test_commit -C clone2 two &&
143 GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
144 fetchargs="--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet" &&
145 test_subcommand git fetch remote1 $fetchargs +refs/heads/\\*:refs/prefetch/remote1/\\* <run-prefetch.txt &&
146 test_subcommand git fetch remote2 $fetchargs +refs/heads/\\*:refs/prefetch/remote2/\\* <run-prefetch.txt &&
147 test_path_is_missing .git/refs/remotes &&
148 git log prefetch/remote1/one &&
149 git log prefetch/remote2/two &&
151 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&
152 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two
155 test_expect_success
'loose-objects task' '
156 # Repack everything so we know the state of the object dir
159 # Hack to stop maintenance from running during "git commit"
160 echo in use >.git/objects/maintenance.lock &&
162 # Assuming that "git commit" creates at least one loose object
163 test_commit create-loose-object &&
164 rm .git/objects/maintenance.lock &&
166 ls .git/objects >obj-dir-before &&
167 test_file_not_empty obj-dir-before &&
168 ls .git/objects/pack/*.pack >packs-before &&
169 test_line_count = 1 packs-before &&
171 # The first run creates a pack-file
172 # but does not delete loose objects.
173 git maintenance run --task=loose-objects &&
174 ls .git/objects >obj-dir-between &&
175 test_cmp obj-dir-before obj-dir-between &&
176 ls .git/objects/pack/*.pack >packs-between &&
177 test_line_count = 2 packs-between &&
178 ls .git/objects/pack/loose-*.pack >loose-packs &&
179 test_line_count = 1 loose-packs &&
181 # The second run deletes loose objects
182 # but does not create a pack-file.
183 git maintenance run --task=loose-objects &&
184 ls .git/objects >obj-dir-after &&
185 cat >expect <<-\EOF &&
189 test_cmp expect obj-dir-after &&
190 ls .git/objects/pack/*.pack >packs-after &&
191 test_cmp packs-between packs-after
194 test_expect_success
'maintenance.loose-objects.auto' '
196 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
197 git -c maintenance.loose-objects.auto=1 maintenance \
198 run --auto --task=loose-objects 2>/dev/null &&
199 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
200 printf data-A | git hash-object -t blob --stdin -w &&
201 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
202 git -c maintenance.loose-objects.auto=2 \
203 maintenance run --auto --task=loose-objects 2>/dev/null &&
204 test_subcommand ! git prune-packed --quiet <trace-loA &&
205 printf data-B | git hash-object -t blob --stdin -w &&
206 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
207 git -c maintenance.loose-objects.auto=2 \
208 maintenance run --auto --task=loose-objects 2>/dev/null &&
209 test_subcommand git prune-packed --quiet <trace-loB &&
210 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
211 git -c maintenance.loose-objects.auto=2 \
212 maintenance run --auto --task=loose-objects 2>/dev/null &&
213 test_subcommand git prune-packed --quiet <trace-loC
216 test_expect_success
'incremental-repack task' '
217 packDir=.git/objects/pack &&
218 for i in $(test_seq 1 5)
220 test_commit $i || return 1
223 # Create three disjoint pack-files with size BIG, small, small.
224 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
226 git pack-objects --revs $packDir/test-2 <<-\EOF &&
231 git pack-objects --revs $packDir/test-3 <<-\EOF &&
235 rm -f $packDir/pack-* &&
236 rm -f $packDir/loose-* &&
237 ls $packDir/*.pack >packs-before &&
238 test_line_count = 3 packs-before &&
240 # the job repacks the two into a new pack, but does not
241 # delete the old ones.
242 git maintenance run --task=incremental-repack &&
243 ls $packDir/*.pack >packs-between &&
244 test_line_count = 4 packs-between &&
246 # the job deletes the two old packs, and does not write
247 # a new one because the batch size is not high enough to
248 # pack the largest pack-file.
249 git maintenance run --task=incremental-repack &&
250 ls .git/objects/pack/*.pack >packs-after &&
251 test_line_count = 2 packs-after
254 test_expect_success EXPENSIVE
'incremental-repack 2g limit' '
255 test_config core.compression 0 &&
257 for i in $(test_seq 1 5)
259 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
263 git commit -qm "Add big file (1)" &&
265 # ensure any possible loose objects are in a pack-file
266 git maintenance run --task=loose-objects &&
269 for i in $(test_seq 6 10)
271 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
275 git commit -qm "Add big file (2)" &&
277 # ensure any possible loose objects are in a pack-file
278 git maintenance run --task=loose-objects &&
280 # Now run the incremental-repack task and check the batch-size
281 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
282 --task=incremental-repack 2>/dev/null &&
283 test_subcommand git multi-pack-index repack \
284 --no-progress --batch-size=2147483647 <run-2g.txt
287 test_expect_success
'maintenance.incremental-repack.auto' '
289 git config core.multiPackIndex true &&
290 git multi-pack-index write &&
291 GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
292 -c maintenance.incremental-repack.auto=1 \
293 maintenance run --auto --task=incremental-repack 2>/dev/null &&
294 test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
296 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
300 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
301 -c maintenance.incremental-repack.auto=2 \
302 maintenance run --auto --task=incremental-repack 2>/dev/null &&
303 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
305 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
309 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
310 -c maintenance.incremental-repack.auto=2 \
311 maintenance run --auto --task=incremental-repack 2>/dev/null &&
312 test_subcommand git multi-pack-index write --no-progress <trace-B
315 test_expect_success
'--auto and --schedule incompatible' '
316 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
317 test_i18ngrep "at most one" err
320 test_expect_success
'invalid --schedule value' '
321 test_must_fail git maintenance run --schedule=annually 2>err &&
322 test_i18ngrep "unrecognized --schedule" err
325 test_expect_success
'--schedule inheritance weekly -> daily -> hourly' '
326 git config maintenance.loose-objects.enabled true &&
327 git config maintenance.loose-objects.schedule hourly &&
328 git config maintenance.commit-graph.enabled true &&
329 git config maintenance.commit-graph.schedule daily &&
330 git config maintenance.incremental-repack.enabled true &&
331 git config maintenance.incremental-repack.schedule weekly &&
333 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
334 git maintenance run --schedule=hourly 2>/dev/null &&
335 test_subcommand git prune-packed --quiet <hourly.txt &&
336 test_subcommand ! git commit-graph write --split --reachable \
337 --no-progress <hourly.txt &&
338 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
340 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
341 git maintenance run --schedule=daily 2>/dev/null &&
342 test_subcommand git prune-packed --quiet <daily.txt &&
343 test_subcommand git commit-graph write --split --reachable \
344 --no-progress <daily.txt &&
345 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
347 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
348 git maintenance run --schedule=weekly 2>/dev/null &&
349 test_subcommand git prune-packed --quiet <weekly.txt &&
350 test_subcommand git commit-graph write --split --reachable \
351 --no-progress <weekly.txt &&
352 test_subcommand git multi-pack-index write --no-progress <weekly.txt
355 test_expect_success
'maintenance.strategy inheritance' '
356 for task in commit-graph loose-objects incremental-repack
358 git config --unset maintenance.$task.schedule || return 1
361 test_when_finished git config --unset maintenance.strategy &&
362 git config maintenance.strategy incremental &&
364 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
365 git maintenance run --schedule=hourly --quiet &&
366 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
367 git maintenance run --schedule=daily --quiet &&
369 test_subcommand git commit-graph write --split --reachable \
370 --no-progress <incremental-hourly.txt &&
371 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
372 test_subcommand ! git multi-pack-index write --no-progress \
373 <incremental-hourly.txt &&
375 test_subcommand git commit-graph write --split --reachable \
376 --no-progress <incremental-daily.txt &&
377 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
378 test_subcommand git multi-pack-index write --no-progress \
379 <incremental-daily.txt &&
382 git config maintenance.commit-graph.schedule daily &&
383 git config maintenance.loose-objects.schedule hourly &&
384 git config maintenance.incremental-repack.enabled false &&
386 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
387 git maintenance run --schedule=hourly --quiet &&
388 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
389 git maintenance run --schedule=daily --quiet &&
391 test_subcommand ! git commit-graph write --split --reachable \
392 --no-progress <modified-hourly.txt &&
393 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
394 test_subcommand ! git multi-pack-index write --no-progress \
395 <modified-hourly.txt &&
397 test_subcommand git commit-graph write --split --reachable \
398 --no-progress <modified-daily.txt &&
399 test_subcommand git prune-packed --quiet <modified-daily.txt &&
400 test_subcommand ! git multi-pack-index write --no-progress \
404 test_expect_success
'register and unregister' '
405 test_when_finished git config --global --unset-all maintenance.repo &&
406 git config --global --add maintenance.repo /existing1 &&
407 git config --global --add maintenance.repo /existing2 &&
408 git config --global --get-all maintenance.repo >before &&
410 git maintenance register &&
411 test_cmp_config false maintenance.auto &&
412 git config --global --get-all maintenance.repo >between &&
415 test_cmp expect between &&
417 git maintenance unregister &&
418 git config --global --get-all maintenance.repo >actual &&
419 test_cmp before actual
422 test_expect_success
!MINGW
'register and unregister with regex metacharacters' '
425 git -C "$META" maintenance register &&
426 git config --get-all --show-origin maintenance.repo &&
427 git config --get-all --global --fixed-value \
428 maintenance.repo "$(pwd)/$META" &&
429 git -C "$META" maintenance unregister &&
430 test_must_fail git config --get-all --global --fixed-value \
431 maintenance.repo "$(pwd)/$META"
434 test_expect_success
'start from empty cron table' '
435 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start &&
437 # start registers the repo
438 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
440 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
441 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
442 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
445 test_expect_success
'stop from existing schedule' '
446 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
448 # stop does not unregister the repo
449 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
451 # Operation is idempotent
452 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
453 test_must_be_empty cron.txt
456 test_expect_success
'start preserves existing schedule' '
457 echo "Important information!" >cron.txt &&
458 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start &&
459 grep "Important information!" cron.txt
462 test_expect_success
'magic markers are correct' '
463 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
464 cat >expect <<-\EOF &&
465 # BEGIN GIT MAINTENANCE SCHEDULE
466 # END GIT MAINTENANCE SCHEDULE
468 test_cmp actual expect
471 test_expect_success
'stop preserves surrounding schedule' '
472 echo "Crucial information!" >>cron.txt &&
473 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
474 grep "Important information!" cron.txt &&
475 grep "Crucial information!" cron.txt
478 test_expect_success
'start and stop macOS maintenance' '
479 # ensure $HOME can be compared against hook arguments on all platforms
480 pfx=$(cd "$HOME" && pwd) &&
482 write_script print-args <<-\EOF &&
483 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
487 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start &&
489 # start registers the repo
490 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
492 ls "$HOME/Library/LaunchAgents" >actual &&
493 cat >expect <<-\EOF &&
494 org.git-scm.git.daily.plist
495 org.git-scm.git.hourly.plist
496 org.git-scm.git.weekly.plist
498 test_cmp expect actual &&
501 for frequency in hourly daily weekly
503 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
504 test_xmllint "$PLIST" &&
505 grep schedule=$frequency "$PLIST" &&
506 echo "bootout gui/[UID] $PLIST" >>expect &&
507 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
509 test_cmp expect args &&
512 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
514 # stop does not unregister the repo
515 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
517 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
518 hourly daily weekly >expect &&
519 test_cmp expect args &&
520 ls "$HOME/Library/LaunchAgents" >actual &&
521 test_line_count = 0 actual
524 test_expect_success
'start and stop Windows maintenance' '
525 write_script print-args <<-\EOF &&
530 /xml) shift; xmlfile=$1; break ;;
534 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
538 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start &&
540 # start registers the repo
541 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
543 for frequency in hourly daily weekly
545 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
546 file=$(ls .git/schedule_${frequency}*.xml) &&
547 test_xmllint "$file" || return 1
551 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
553 # stop does not unregister the repo
554 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
556 printf "/delete /tn Git Maintenance (%s) /f\n" \
557 hourly daily weekly >expect &&
561 test_expect_success
'register preserves existing strategy' '
562 git config maintenance.strategy none &&
563 git maintenance register &&
564 test_config maintenance.strategy none &&
565 git config --unset maintenance.strategy &&
566 git maintenance register &&
567 test_config maintenance.strategy incremental
570 test_expect_success
'fails when running outside of a repository' '
571 nongit test_must_fail git maintenance run &&
572 nongit test_must_fail git maintenance stop &&
573 nongit test_must_fail git maintenance start &&
574 nongit test_must_fail git maintenance register &&
575 nongit test_must_fail git maintenance unregister