Raw
1 #!/bin/sh
2
3 test_description='git maintenance builtin'
4
5 . ./test-lib.sh
6
7 GIT_TEST_COMMIT_GRAPH=0
8 GIT_TEST_MULTI_PACK_INDEX=0
9
10 # Ensure that auto-maintenance detaches as usual.
11 sane_unset GIT_TEST_MAINT_AUTO_DETACH
12
13 test_lazy_prereq XMLLINT '
14 xmllint --version
15 '
16
17 test_xmllint () {
18 if test_have_prereq XMLLINT
19 then
20 xmllint --noout "$@"
21 else
22 true
23 fi
24 }
25
26 test_lazy_prereq SYSTEMD_ANALYZE '
27 systemd-analyze verify /lib/systemd/system/basic.target
28 '
29
30 test_systemd_analyze_verify () {
31 if test_have_prereq SYSTEMD_ANALYZE
32 then
33 systemd-analyze verify "$@"
34 fi
35 }
36
37 test_expect_success 'help text' '
38 test_expect_code 129 git maintenance -h >actual &&
39 test_grep "usage: git maintenance <subcommand>" actual &&
40 test_expect_code 129 git maintenance barf 2>err &&
41 test_grep "unknown subcommand: \`barf'\''" err &&
42 test_grep "usage: git maintenance" err &&
43 test_expect_code 129 git maintenance 2>err &&
44 test_grep "error: need a subcommand" err &&
45 test_grep "usage: git maintenance" err
46 '
47
48 test_expect_success 'run [--auto|--quiet] with gc strategy' '
49 test_config maintenance.strategy gc &&
50 GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
51 git maintenance run 2>/dev/null &&
52 GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
53 git maintenance run --auto 2>/dev/null &&
54 GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
55 git maintenance run --no-quiet 2>/dev/null &&
56 git maintenance is-needed &&
57 test_subcommand git gc --quiet --no-detach --skip-foreground-tasks <run-no-auto.txt &&
58 ! git maintenance is-needed --auto &&
59 test_subcommand ! git gc --auto --quiet --no-detach --skip-foreground-tasks <run-auto.txt &&
60 test_subcommand git gc --no-quiet --no-detach --skip-foreground-tasks <run-no-quiet.txt
61 '
62
63 test_expect_success 'maintenance.auto config option' '
64 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
65 test_subcommand git maintenance run --auto --quiet --detach <default &&
66 GIT_TRACE2_EVENT="$(pwd)/true" \
67 git -c maintenance.auto=true \
68 commit --quiet --allow-empty -m 2 &&
69 test_subcommand git maintenance run --auto --quiet --detach <true &&
70 GIT_TRACE2_EVENT="$(pwd)/false" \
71 git -c maintenance.auto=false \
72 commit --quiet --allow-empty -m 3 &&
73 test_subcommand ! git maintenance run --auto --quiet --detach <false
74 '
75
76 test_expect_success 'gc.auto config option' '
77 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
78 test_subcommand git maintenance run --auto --quiet --detach <default &&
79 GIT_TRACE2_EVENT="$(pwd)/true" \
80 git -c gc.auto=1 commit --quiet --allow-empty -m 2 &&
81 test_subcommand git maintenance run --auto --quiet --detach <true &&
82 GIT_TRACE2_EVENT="$(pwd)/false" \
83 git -c gc.auto=0 commit --quiet --allow-empty -m 3 &&
84 test_subcommand ! git maintenance run --auto --quiet --detach <false
85 '
86
87 test_expect_success 'maintenance.auto overrides gc.auto' '
88 test_when_finished "rm -f trace" &&
89
90 test_config maintenance.auto false &&
91 test_config gc.auto 1 &&
92 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
93 test_subcommand ! git maintenance run --auto --quiet --detach <trace &&
94
95 test_config maintenance.auto true &&
96 test_config gc.auto 0 &&
97 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
98 test_subcommand git maintenance run --auto --quiet --detach <trace
99 '
100
101 for cfg in maintenance.autoDetach gc.autoDetach
102 do
103 test_expect_success "$cfg=true config option" '
104 test_when_finished "rm -f trace" &&
105 test_config $cfg true &&
106 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
107 test_subcommand git maintenance run --auto --quiet --detach <trace
108 '
109
110 test_expect_success "$cfg=false config option" '
111 test_when_finished "rm -f trace" &&
112 test_config $cfg false &&
113 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
114 test_subcommand git maintenance run --auto --quiet --no-detach <trace
115 '
116 done
117
118 test_expect_success "maintenance.autoDetach overrides gc.autoDetach" '
119 test_when_finished "rm -f trace" &&
120 test_config maintenance.autoDetach false &&
121 test_config gc.autoDetach true &&
122 GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
123 test_subcommand git maintenance run --auto --quiet --no-detach <trace
124 '
125
126 test_expect_success 'register uses XDG_CONFIG_HOME config if it exists' '
127 test_when_finished rm -r .config/git/config &&
128 (
129 # Override HOME so that .gitconfig (which test-lib.sh may
130 # have created, e.g. to set safe.bareRepository) does not
131 # take precedence over the XDG location.
132 HOME=$PWD/must-not-exist &&
133 XDG_CONFIG_HOME=.config &&
134 export HOME XDG_CONFIG_HOME &&
135 mkdir -p $XDG_CONFIG_HOME/git &&
136 >$XDG_CONFIG_HOME/git/config &&
137 git maintenance register &&
138 git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
139 pwd >expect &&
140 test_cmp expect actual
141 )
142 '
143
144 test_expect_success 'register does not need XDG_CONFIG_HOME config to exist' '
145 test_when_finished git maintenance unregister &&
146 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
147 git maintenance register &&
148 git config --global --get maintenance.repo >actual &&
149 pwd >expect &&
150 test_cmp expect actual
151 '
152
153 test_expect_success 'unregister uses XDG_CONFIG_HOME config if it exists' '
154 test_when_finished rm -r .config/git/config &&
155 (
156 # Override HOME so that .gitconfig (which test-lib.sh may
157 # have created, e.g. to set safe.bareRepository) does not
158 # take precedence over the XDG location.
159 HOME=$PWD/must-not-exist &&
160 XDG_CONFIG_HOME=.config &&
161 export HOME XDG_CONFIG_HOME &&
162 mkdir -p $XDG_CONFIG_HOME/git &&
163 >$XDG_CONFIG_HOME/git/config &&
164 git maintenance register &&
165 git maintenance unregister &&
166 test_must_fail git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
167 test_must_be_empty actual
168 )
169 '
170
171 test_expect_success 'unregister does not need XDG_CONFIG_HOME config to exist' '
172 test_path_is_missing $XDG_CONFIG_HOME/git/config &&
173 git maintenance register &&
174 git maintenance unregister &&
175 test_must_fail git config --global --get maintenance.repo >actual &&
176 test_must_be_empty actual
177 '
178
179 test_expect_success 'maintenance.<task>.enabled' '
180 git config maintenance.gc.enabled false &&
181 git config maintenance.commit-graph.enabled true &&
182 GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
183 test_subcommand ! git gc --quiet <run-config.txt &&
184 test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
185 '
186
187 test_expect_success 'run --task=<task>' '
188 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
189 git maintenance run --task=commit-graph 2>/dev/null &&
190 GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
191 git maintenance run --task=gc 2>/dev/null &&
192 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
193 git maintenance run --task=commit-graph 2>/dev/null &&
194 GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
195 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
196 test_subcommand ! git gc --quiet --no-detach --skip-foreground-tasks <run-commit-graph.txt &&
197 test_subcommand git gc --quiet --no-detach --skip-foreground-tasks <run-gc.txt &&
198 test_subcommand git gc --quiet --no-detach --skip-foreground-tasks <run-both.txt &&
199 test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
200 test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
201 test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
202 '
203
204 test_expect_success 'core.commitGraph=false prevents write process' '
205 GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
206 git -c core.commitGraph=false maintenance run \
207 --task=commit-graph 2>/dev/null &&
208 test_subcommand ! git commit-graph write --split --reachable --no-progress \
209 <no-commit-graph.txt
210 '
211
212 test_expect_success 'commit-graph auto condition' '
213 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
214
215 GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
216 git -c maintenance.commit-graph.auto=1 $COMMAND &&
217 GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
218 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
219
220 test_commit first &&
221
222 ! git -c maintenance.commit-graph.auto=0 \
223 maintenance is-needed --auto --task=commit-graph &&
224 git -c maintenance.commit-graph.auto=1 \
225 maintenance is-needed --auto --task=commit-graph &&
226
227 GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
228 git -c maintenance.commit-graph.auto=0 $COMMAND &&
229 GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
230 git -c maintenance.commit-graph.auto=1 $COMMAND &&
231
232 git commit --allow-empty -m "second" &&
233 git commit --allow-empty -m "third" &&
234
235 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
236 git -c maintenance.commit-graph.auto=2 $COMMAND &&
237
238 COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
239 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
240 test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
241 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
242 test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
243 test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
244 '
245
246 test_expect_success 'commit-graph auto condition with merges' '
247 test_when_finished "rm -rf repo" &&
248 git init repo &&
249 (
250 cd repo &&
251 git config set maintenance.auto false &&
252 test_commit initial &&
253 git switch --create feature &&
254 test_commit feature-1 &&
255 test_commit feature-2 &&
256 git switch - &&
257 test_commit main-1 &&
258 test_commit main-2 &&
259 git merge feature &&
260
261 # We have 6 commits, none of which are covered by a commit
262 # graph. So this must be the boundary at which we start to
263 # perform maintenance.
264 test_must_fail git -c maintenance.commit-graph.auto=7 \
265 maintenance is-needed --auto --task=commit-graph &&
266 git -c maintenance.commit-graph.auto=6 \
267 maintenance is-needed --auto --task=commit-graph
268 )
269 '
270
271 test_expect_success 'run --task=bogus' '
272 test_must_fail git maintenance run --task=bogus 2>err &&
273 test_grep "is not a valid task" err
274 '
275
276 test_expect_success 'run --task duplicate' '
277 test_must_fail git maintenance run --task=gc --task=gc 2>err &&
278 test_grep "cannot be selected multiple times" err
279 '
280
281 test_expect_success 'run --task=prefetch with no remotes' '
282 git maintenance run --task=prefetch 2>err &&
283 test_must_be_empty err
284 '
285
286 test_expect_success 'prefetch multiple remotes' '
287 git clone . clone1 &&
288 git clone . clone2 &&
289 git remote add remote1 "file://$(pwd)/clone1" &&
290 git remote add remote2 "file://$(pwd)/clone2" &&
291 git -C clone1 switch -c one &&
292 git -C clone2 switch -c two &&
293 test_commit -C clone1 one &&
294 test_commit -C clone2 two &&
295 GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
296 fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
297 test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
298 test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
299 git for-each-ref refs/remotes >actual &&
300 test_must_be_empty actual &&
301 git log prefetch/remotes/remote1/one &&
302 git log prefetch/remotes/remote2/two &&
303 git fetch --all &&
304 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
305 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
306
307 git log --oneline --decorate --all >log &&
308 ! grep "prefetch" log &&
309
310 test_when_finished git config --unset remote.remote1.skipFetchAll &&
311 git config remote.remote1.skipFetchAll true &&
312 GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
313 test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
314 test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
315 '
316
317 test_expect_success 'loose-objects task' '
318 # Repack everything so we know the state of the object dir
319 git repack -adk &&
320
321 # Hack to stop maintenance from running during "git commit"
322 echo in use >.git/objects/maintenance.lock &&
323
324 # Assuming that "git commit" creates at least one loose object
325 test_commit create-loose-object &&
326 rm .git/objects/maintenance.lock &&
327
328 ls .git/objects >obj-dir-before &&
329 test_file_not_empty obj-dir-before &&
330 ls .git/objects/pack/*.pack >packs-before &&
331 test_line_count = 1 packs-before &&
332
333 # The first run creates a pack-file
334 # but does not delete loose objects.
335 git maintenance run --task=loose-objects &&
336 ls .git/objects >obj-dir-between &&
337 test_cmp obj-dir-before obj-dir-between &&
338 ls .git/objects/pack/*.pack >packs-between &&
339 test_line_count = 2 packs-between &&
340 ls .git/objects/pack/loose-*.pack >loose-packs &&
341 test_line_count = 1 loose-packs &&
342
343 # The second run deletes loose objects
344 # but does not create a pack-file.
345 git maintenance run --task=loose-objects &&
346 ls .git/objects >obj-dir-after &&
347 cat >expect <<-\EOF &&
348 info
349 pack
350 EOF
351 test_cmp expect obj-dir-after &&
352 ls .git/objects/pack/*.pack >packs-after &&
353 test_cmp packs-between packs-after
354 '
355
356 test_expect_success 'maintenance.loose-objects.auto' '
357 git repack -adk &&
358 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
359 git -c maintenance.loose-objects.auto=1 maintenance \
360 run --auto --task=loose-objects 2>/dev/null &&
361 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
362
363 printf data-A | git hash-object -t blob --stdin -w &&
364 ! git -c maintenance.loose-objects.auto=2 \
365 maintenance is-needed --auto --task=loose-objects &&
366 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
367 git -c maintenance.loose-objects.auto=2 \
368 maintenance run --auto --task=loose-objects 2>/dev/null &&
369 test_subcommand ! git prune-packed --quiet <trace-loA &&
370
371 printf data-B | git hash-object -t blob --stdin -w &&
372 git -c maintenance.loose-objects.auto=2 \
373 maintenance is-needed --auto --task=loose-objects &&
374 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
375 git -c maintenance.loose-objects.auto=2 \
376 maintenance run --auto --task=loose-objects 2>/dev/null &&
377 test_subcommand git prune-packed --quiet <trace-loB &&
378
379 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
380 git -c maintenance.loose-objects.auto=2 \
381 maintenance run --auto --task=loose-objects 2>/dev/null &&
382 test_subcommand git prune-packed --quiet <trace-loC
383 '
384
385 test_expect_success 'maintenance.loose-objects.batchSize' '
386 git init loose-batch &&
387
388 # This creates three objects per commit.
389 test_commit_bulk -C loose-batch 34 &&
390 pack=$(ls loose-batch/.git/objects/pack/pack-*.pack) &&
391 index="${pack%pack}idx" &&
392 rm "$index" &&
393 git -C loose-batch unpack-objects <"$pack" &&
394 git -C loose-batch config maintenance.loose-objects.batchSize 50 &&
395
396 GIT_PROGRESS_DELAY=0 \
397 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
398 grep "Enumerating objects: 50, done." err &&
399
400 GIT_PROGRESS_DELAY=0 \
401 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
402 grep "Enumerating objects: 50, done." err &&
403
404 GIT_PROGRESS_DELAY=0 \
405 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
406 grep "Enumerating objects: 2, done." err &&
407
408 GIT_PROGRESS_DELAY=0 \
409 git -C loose-batch maintenance run --no-quiet --task=loose-objects 2>err &&
410 test_must_be_empty err
411 '
412
413 test_expect_success 'incremental-repack task' '
414 packDir=.git/objects/pack &&
415 for i in $(test_seq 1 5)
416 do
417 test_commit $i || return 1
418 done &&
419
420 # Create three disjoint pack-files with size BIG, small, small.
421 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
422 test_tick &&
423 git pack-objects --revs $packDir/test-2 <<-\EOF &&
424 HEAD~1
425 ^HEAD~2
426 EOF
427 test_tick &&
428 git pack-objects --revs $packDir/test-3 <<-\EOF &&
429 HEAD
430 ^HEAD~1
431 EOF
432
433 # Delete refs that have not been repacked in these packs.
434 git for-each-ref --format="delete %(refname)" \
435 refs/prefetch refs/tags refs/remotes \
436 --exclude=refs/remotes/*/HEAD >refs &&
437 git update-ref --stdin <refs &&
438
439 # Replace the object directory with this pack layout.
440 rm -f $packDir/pack-* &&
441 rm -f $packDir/loose-* &&
442 ls $packDir/*.pack >packs-before &&
443 test_line_count = 3 packs-before &&
444
445 # make sure we do not have any broken refs that were
446 # missed in the deletion above
447 git for-each-ref &&
448
449 # the job repacks the two into a new pack, but does not
450 # delete the old ones.
451 git maintenance run --task=incremental-repack &&
452 ls $packDir/*.pack >packs-between &&
453 test_line_count = 4 packs-between &&
454
455 # the job deletes the two old packs, and does not write
456 # a new one because the batch size is not high enough to
457 # pack the largest pack-file.
458 git maintenance run --task=incremental-repack &&
459 ls .git/objects/pack/*.pack >packs-after &&
460 test_line_count = 2 packs-after
461 '
462
463 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
464 test_config core.compression 0 &&
465
466 for i in $(test_seq 1 5)
467 do
468 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
469 return 1
470 done &&
471 git add big &&
472 git commit -qm "Add big file (1)" &&
473
474 # ensure any possible loose objects are in a pack-file
475 git maintenance run --task=loose-objects &&
476
477 rm big &&
478 for i in $(test_seq 6 10)
479 do
480 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
481 return 1
482 done &&
483 git add big &&
484 git commit -qm "Add big file (2)" &&
485
486 # ensure any possible loose objects are in a pack-file
487 git maintenance run --task=loose-objects &&
488
489 # Now run the incremental-repack task and check the batch-size
490 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
491 --task=incremental-repack 2>/dev/null &&
492 test_subcommand git multi-pack-index repack \
493 --no-progress --batch-size=2147483647 <run-2g.txt
494 '
495
496 run_incremental_repack_and_verify () {
497 test_commit A &&
498 git repack -adk &&
499 git multi-pack-index write &&
500 ! git -c maintenance.incremental-repack.auto=1 \
501 maintenance is-needed --auto --task=incremental-repack &&
502 GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
503 -c maintenance.incremental-repack.auto=1 \
504 maintenance run --auto --task=incremental-repack 2>/dev/null &&
505 test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
506
507 test_commit B &&
508 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
509 HEAD
510 ^HEAD~1
511 EOF
512 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
513 -c maintenance.incremental-repack.auto=2 \
514 maintenance run --auto --task=incremental-repack 2>/dev/null &&
515 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
516
517 test_commit C &&
518 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
519 HEAD
520 ^HEAD~1
521 EOF
522 git -c maintenance.incremental-repack.auto=2 \
523 maintenance is-needed --auto --task=incremental-repack &&
524 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
525 -c maintenance.incremental-repack.auto=2 \
526 maintenance run --auto --task=incremental-repack 2>/dev/null &&
527 test_subcommand git multi-pack-index write --no-progress <trace-B
528 }
529
530 test_expect_success 'maintenance.incremental-repack.auto' '
531 rm -rf incremental-repack-true &&
532 git init incremental-repack-true &&
533 (
534 cd incremental-repack-true &&
535 git config core.multiPackIndex true &&
536 git config maintenance.auto false &&
537 run_incremental_repack_and_verify
538 )
539 '
540
541 test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' '
542 rm -rf incremental-repack-unset &&
543 git init incremental-repack-unset &&
544 (
545 cd incremental-repack-unset &&
546 test_unconfig core.multiPackIndex &&
547 git config maintenance.auto false &&
548 run_incremental_repack_and_verify
549 )
550 '
551
552 run_and_verify_geometric_pack () {
553 EXPECTED_PACKS="$1" &&
554
555 # Verify that we perform a geometric repack.
556 rm -f "trace2.txt" &&
557 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
558 git maintenance run --task=geometric-repack 2>/dev/null &&
559 test_subcommand git repack -d -l --geometric=2 \
560 --quiet --write-midx <trace2.txt &&
561
562 # Verify that the number of packfiles matches our expectation.
563 ls -l .git/objects/pack/*.pack >packfiles &&
564 test_line_count = "$EXPECTED_PACKS" packfiles &&
565
566 # And verify that there are no loose objects anymore.
567 git count-objects -v >count &&
568 test_grep '^count: 0$' count &&
569
570 # Verify that no orphaned .idx files were left behind. On
571 # Windows, a missing odb_to_close causes the parent to hold
572 # mmap handles on .idx files, silently preventing their
573 # deletion by the child git-repack process.
574 ls .git/objects/pack/pack-*.idx .git/objects/pack/pack-*.pack |
575 sed "s/\.pack$/.idx/" |
576 sort | uniq -u >orphaned-idx &&
577 test_must_be_empty orphaned-idx
578 }
579
580 test_expect_success 'geometric repacking task' '
581 test_when_finished "rm -rf repo" &&
582 git init repo &&
583 (
584 cd repo &&
585 git config set maintenance.auto false &&
586 test_commit initial &&
587
588 # The initial repack causes an all-into-one repack.
589 GIT_TRACE2_EVENT="$(pwd)/initial-repack.txt" \
590 git maintenance run --task=geometric-repack 2>/dev/null &&
591 test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago \
592 --quiet --write-midx <initial-repack.txt &&
593
594 # Repacking should now cause a no-op geometric repack because
595 # no packfiles need to be combined.
596 ls -l .git/objects/pack/*.pack >before &&
597 run_and_verify_geometric_pack 1 &&
598 ls -l .git/objects/pack/*.pack >after &&
599 test_cmp before after &&
600
601 # This incremental change creates a new packfile that only
602 # soaks up loose objects. The packfiles are not getting merged
603 # at this point.
604 test_commit loose &&
605 run_and_verify_geometric_pack 2 &&
606
607 # Both packfiles have 3 objects, so the next run would cause us
608 # to merge all packfiles together. This should be turned into
609 # an all-into-one-repack.
610 GIT_TRACE2_EVENT="$(pwd)/all-into-one-repack.txt" \
611 git maintenance run --task=geometric-repack 2>/dev/null &&
612 test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago \
613 --quiet --write-midx <all-into-one-repack.txt &&
614
615 # The geometric repack soaks up unreachable objects.
616 echo blob-1 | git hash-object -w --stdin -t blob &&
617 run_and_verify_geometric_pack 2 &&
618
619 # A second unreachable object should be written into another packfile.
620 echo blob-2 | git hash-object -w --stdin -t blob &&
621 run_and_verify_geometric_pack 3 &&
622
623 # And these two small packs should now be merged via the
624 # geometric repack. The large packfile should remain intact.
625 cp -R .git/objects .git/objects.save &&
626 run_and_verify_geometric_pack 2 &&
627
628 # On Windows, verify the same with legacy delete semantics
629 # that reject deletion of mmap-held .idx files.
630 if test_have_prereq MINGW
631 then
632 rm -rf .git/objects &&
633 mv .git/objects.save .git/objects &&
634 test_env GIT_TEST_LEGACY_DELETE=1 \
635 run_and_verify_geometric_pack 2
636 fi &&
637
638 # If we now add two more objects and repack twice we should
639 # then see another all-into-one repack. This time around
640 # though, as we have unreachable objects, we should also see a
641 # cruft pack.
642 echo blob-3 | git hash-object -w --stdin -t blob &&
643 echo blob-4 | git hash-object -w --stdin -t blob &&
644 run_and_verify_geometric_pack 3 &&
645 GIT_TRACE2_EVENT="$(pwd)/cruft-repack.txt" \
646 git maintenance run --task=geometric-repack 2>/dev/null &&
647 test_subcommand git repack -d -l --cruft --cruft-expiration=2.weeks.ago \
648 --quiet --write-midx <cruft-repack.txt &&
649 ls .git/objects/pack/*.pack >packs &&
650 test_line_count = 2 packs &&
651 ls .git/objects/pack/*.mtimes >cruft &&
652 test_line_count = 1 cruft
653 )
654 '
655
656 test_geometric_repack_needed () {
657 NEEDED="$1"
658 GEOMETRIC_CONFIG="$2" &&
659 rm -f trace2.txt &&
660 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
661 git ${GEOMETRIC_CONFIG:+-c maintenance.geometric-repack.$GEOMETRIC_CONFIG} \
662 maintenance run --auto --task=geometric-repack 2>/dev/null &&
663 case "$NEEDED" in
664 true)
665 test_grep "\[\"git\",\"repack\"," trace2.txt;;
666 false)
667 ! test_grep "\[\"git\",\"repack\"," trace2.txt;;
668 *)
669 BUG "invalid parameter: $NEEDED";;
670 esac
671 }
672
673 test_expect_success 'geometric repacking with --auto' '
674 test_when_finished "rm -rf repo" &&
675 git init repo &&
676 (
677 cd repo &&
678 git config set maintenance.auto false &&
679
680 # An empty repository does not need repacking, except when
681 # explicitly told to do it.
682 test_geometric_repack_needed false &&
683 test_geometric_repack_needed false auto=0 &&
684 test_geometric_repack_needed false auto=1 &&
685 test_geometric_repack_needed true auto=-1 &&
686
687 test_oid_init &&
688
689 # Loose objects cause a repack when crossing the limit. Note
690 # that the number of objects gets extrapolated by having a look
691 # at the "objects/17/" shard.
692 test_commit "$(test_oid blob17_1)" &&
693 test_geometric_repack_needed false &&
694 test_commit "$(test_oid blob17_2)" &&
695 test_geometric_repack_needed false auto=257 &&
696 test_geometric_repack_needed true auto=256 &&
697
698 # Force another repack.
699 test_commit first &&
700 test_commit second &&
701 test_geometric_repack_needed true auto=-1 &&
702
703 # We now have two packfiles that would be merged together. As
704 # such, the repack should always happen unless the user has
705 # disabled the auto task.
706 test_geometric_repack_needed false auto=0 &&
707 test_geometric_repack_needed true auto=9000
708 )
709 '
710
711 test_expect_success 'geometric repacking honors configured split factor' '
712 test_when_finished "rm -rf repo" &&
713 git init repo &&
714 (
715 cd repo &&
716 git config set maintenance.auto false &&
717
718 # Create three different packs with 9, 2 and 1 object, respectively.
719 # This is done so that only a subset of packs would be merged
720 # together so that we can verify that `git repack` receives the
721 # correct geometric factor.
722 for i in $(test_seq 9)
723 do
724 echo first-$i | git hash-object -w --stdin -t blob || return 1
725 done &&
726 git repack --geometric=2 -d &&
727
728 for i in $(test_seq 2)
729 do
730 echo second-$i | git hash-object -w --stdin -t blob || return 1
731 done &&
732 git repack --geometric=2 -d &&
733
734 echo third | git hash-object -w --stdin -t blob &&
735 git repack --geometric=2 -d &&
736
737 test_geometric_repack_needed false splitFactor=2 &&
738 test_geometric_repack_needed true splitFactor=3 &&
739 test_subcommand git repack -d -l --geometric=3 --quiet --write-midx <trace2.txt
740 )
741 '
742
743 test_expect_success 'pack-refs task' '
744 for n in $(test_seq 1 5)
745 do
746 git branch -f to-pack/$n HEAD || return 1
747 done &&
748 GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
749 git maintenance run --task=pack-refs &&
750 test_subcommand git pack-refs --all --prune <pack-refs.txt
751 '
752
753 test_expect_success 'reflog-expire task' '
754 GIT_TRACE2_EVENT="$(pwd)/reflog-expire.txt" \
755 git maintenance run --task=reflog-expire &&
756 test_subcommand git reflog expire --all <reflog-expire.txt
757 '
758
759 test_expect_success 'reflog-expire task --auto only packs when exceeding limits' '
760 git reflog expire --all --expire=now &&
761 test_commit reflog-one &&
762 test_commit reflog-two &&
763
764 ! git -c maintenance.reflog-expire.auto=3 \
765 maintenance is-needed --auto --task=reflog-expire &&
766 GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \
767 git -c maintenance.reflog-expire.auto=3 maintenance run --auto --task=reflog-expire &&
768 test_subcommand ! git reflog expire --all <reflog-expire-auto.txt &&
769
770 git -c maintenance.reflog-expire.auto=2 \
771 maintenance is-needed --auto --task=reflog-expire &&
772 GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \
773 git -c maintenance.reflog-expire.auto=2 maintenance run --auto --task=reflog-expire &&
774 test_subcommand git reflog expire --all <reflog-expire-auto.txt
775 '
776
777 test_expect_worktree_prune () {
778 negate=
779 if test "$1" = "!"
780 then
781 negate="!"
782 shift
783 fi
784
785 rm -f "worktree-prune.txt" &&
786 GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" "$@" &&
787 test_subcommand $negate git worktree prune --expire 3.months.ago <worktree-prune.txt
788 }
789
790 test_expect_success 'worktree-prune task without --auto always prunes' '
791 test_expect_worktree_prune git maintenance run --task=worktree-prune
792 '
793
794 test_expect_success 'worktree-prune task --auto only prunes with prunable worktree' '
795 test_expect_worktree_prune ! git maintenance run --auto --task=worktree-prune &&
796 mkdir .git/worktrees &&
797 : >.git/worktrees/abc &&
798 git maintenance is-needed --auto --task=worktree-prune &&
799 test_expect_worktree_prune git maintenance run --auto --task=worktree-prune
800 '
801
802 test_expect_success 'worktree-prune task with --auto honors maintenance.worktree-prune.auto' '
803 # A negative value should always prune.
804 test_expect_worktree_prune git -c maintenance.worktree-prune.auto=-1 maintenance run --auto --task=worktree-prune &&
805
806 mkdir .git/worktrees &&
807 : >.git/worktrees/first &&
808 : >.git/worktrees/second &&
809 : >.git/worktrees/third &&
810
811 # Zero should never prune.
812 test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=0 maintenance run --auto --task=worktree-prune &&
813 # A positive value should require at least this many prunable worktrees.
814 test_expect_worktree_prune ! git -c maintenance.worktree-prune.auto=4 maintenance run --auto --task=worktree-prune &&
815 git -c maintenance.worktree-prune.auto=3 maintenance is-needed --auto --task=worktree-prune &&
816 test_expect_worktree_prune git -c maintenance.worktree-prune.auto=3 maintenance run --auto --task=worktree-prune
817 '
818
819 test_expect_success 'worktree-prune task honors gc.worktreePruneExpire' '
820 git worktree add worktree &&
821 rm -rf worktree &&
822
823 rm -f worktree-prune.txt &&
824 ! git -c gc.worktreePruneExpire=1.week.ago maintenance is-needed --auto --task=worktree-prune &&
825 GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=1.week.ago maintenance run --auto --task=worktree-prune &&
826 test_subcommand ! git worktree prune --expire 1.week.ago <worktree-prune.txt &&
827 test_path_is_dir .git/worktrees/worktree &&
828
829 rm -f worktree-prune.txt &&
830 git -c gc.worktreePruneExpire=now maintenance is-needed --auto --task=worktree-prune &&
831 GIT_TRACE2_EVENT="$(pwd)/worktree-prune.txt" git -c gc.worktreePruneExpire=now maintenance run --auto --task=worktree-prune &&
832 test_subcommand git worktree prune --expire now <worktree-prune.txt &&
833 test_path_is_missing .git/worktrees/worktree
834 '
835
836 test_expect_rerere_gc () {
837 negate=
838 if test "$1" = "!"
839 then
840 negate="!"
841 shift
842 fi
843
844 rm -f "rerere-gc.txt" &&
845 GIT_TRACE2_EVENT="$(pwd)/rerere-gc.txt" "$@" &&
846 test_subcommand $negate git rerere gc <rerere-gc.txt
847 }
848
849 test_expect_success 'rerere-gc task without --auto always collects garbage' '
850 test_expect_rerere_gc git maintenance run --task=rerere-gc
851 '
852
853 test_expect_success 'rerere-gc task with --auto only prunes with prunable entries' '
854 test_when_finished "rm -rf .git/rr-cache" &&
855 ! git maintenance is-needed --auto --task=rerere-gc &&
856 test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc &&
857 mkdir .git/rr-cache &&
858 ! git maintenance is-needed --auto --task=rerere-gc &&
859 test_expect_rerere_gc ! git maintenance run --auto --task=rerere-gc &&
860 : >.git/rr-cache/entry &&
861 git maintenance is-needed --auto --task=rerere-gc &&
862 test_expect_rerere_gc git maintenance run --auto --task=rerere-gc
863 '
864
865 test_expect_success 'rerere-gc task with --auto honors maintenance.rerere-gc.auto' '
866 test_when_finished "rm -rf .git/rr-cache" &&
867
868 # A negative value should always prune.
869 git -c maintenance.rerere-gc.auto=-1 maintenance is-needed --auto --task=rerere-gc &&
870 test_expect_rerere_gc git -c maintenance.rerere-gc.auto=-1 maintenance run --auto --task=rerere-gc &&
871
872 # A positive value prunes when there is at least one entry.
873 ! git -c maintenance.rerere-gc.auto=9000 maintenance is-needed --auto --task=rerere-gc &&
874 test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc &&
875 mkdir .git/rr-cache &&
876 ! git -c maintenance.rerere-gc.auto=9000 maintenance is-needed --auto --task=rerere-gc &&
877 test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc &&
878 : >.git/rr-cache/entry-1 &&
879 git -c maintenance.rerere-gc.auto=9000 maintenance is-needed --auto --task=rerere-gc &&
880 test_expect_rerere_gc git -c maintenance.rerere-gc.auto=9000 maintenance run --auto --task=rerere-gc &&
881
882 # Zero should never prune.
883 : >.git/rr-cache/entry-1 &&
884 ! git -c maintenance.rerere-gc.auto=0 maintenance is-needed --auto --task=rerere-gc &&
885 test_expect_rerere_gc ! git -c maintenance.rerere-gc.auto=0 maintenance run --auto --task=rerere-gc
886 '
887
888 test_expect_success '--auto and --schedule incompatible' '
889 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
890 test_grep "cannot be used together" err
891 '
892
893 test_expect_success '--task and --schedule incompatible' '
894 test_must_fail git maintenance run --task=pack-refs --schedule=daily 2>err &&
895 test_grep "cannot be used together" err
896 '
897
898 test_expect_success 'invalid --schedule value' '
899 test_must_fail git maintenance run --schedule=annually 2>err &&
900 test_grep "unrecognized --schedule" err
901 '
902
903 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
904 git config maintenance.loose-objects.enabled true &&
905 git config maintenance.loose-objects.schedule hourly &&
906 git config maintenance.commit-graph.enabled true &&
907 git config maintenance.commit-graph.schedule daily &&
908 git config maintenance.incremental-repack.enabled true &&
909 git config maintenance.incremental-repack.schedule weekly &&
910
911 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
912 git maintenance run --schedule=hourly 2>/dev/null &&
913 test_subcommand git prune-packed --quiet <hourly.txt &&
914 test_subcommand ! git commit-graph write --split --reachable \
915 --no-progress <hourly.txt &&
916 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
917
918 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
919 git maintenance run --schedule=daily 2>/dev/null &&
920 test_subcommand git prune-packed --quiet <daily.txt &&
921 test_subcommand git commit-graph write --split --reachable \
922 --no-progress <daily.txt &&
923 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
924
925 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
926 git maintenance run --schedule=weekly 2>/dev/null &&
927 test_subcommand git prune-packed --quiet <weekly.txt &&
928 test_subcommand git commit-graph write --split --reachable \
929 --no-progress <weekly.txt &&
930 test_subcommand git multi-pack-index write --no-progress <weekly.txt
931 '
932
933 test_expect_success 'maintenance.strategy inheritance' '
934 for task in commit-graph loose-objects incremental-repack
935 do
936 git config --unset maintenance.$task.schedule || return 1
937 done &&
938
939 test_when_finished git config --unset maintenance.strategy &&
940 git config maintenance.strategy incremental &&
941
942 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
943 git maintenance run --schedule=hourly --quiet &&
944 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
945 git maintenance run --schedule=daily --quiet &&
946 GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
947 git maintenance run --schedule=weekly --quiet &&
948
949 test_subcommand git commit-graph write --split --reachable \
950 --no-progress <incremental-hourly.txt &&
951 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
952 test_subcommand ! git multi-pack-index write --no-progress \
953 <incremental-hourly.txt &&
954 test_subcommand ! git pack-refs --all --prune \
955 <incremental-hourly.txt &&
956
957 test_subcommand git commit-graph write --split --reachable \
958 --no-progress <incremental-daily.txt &&
959 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
960 test_subcommand git multi-pack-index write --no-progress \
961 <incremental-daily.txt &&
962 test_subcommand ! git pack-refs --all --prune \
963 <incremental-daily.txt &&
964
965 test_subcommand git commit-graph write --split --reachable \
966 --no-progress <incremental-weekly.txt &&
967 test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
968 test_subcommand git multi-pack-index write --no-progress \
969 <incremental-weekly.txt &&
970 test_subcommand git pack-refs --all --prune \
971 <incremental-weekly.txt &&
972
973 # Modify defaults
974 git config maintenance.commit-graph.schedule daily &&
975 git config maintenance.loose-objects.schedule hourly &&
976 git config maintenance.incremental-repack.enabled false &&
977
978 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
979 git maintenance run --schedule=hourly --quiet &&
980 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
981 git maintenance run --schedule=daily --quiet &&
982
983 test_subcommand ! git commit-graph write --split --reachable \
984 --no-progress <modified-hourly.txt &&
985 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
986 test_subcommand ! git multi-pack-index write --no-progress \
987 <modified-hourly.txt &&
988
989 test_subcommand git commit-graph write --split --reachable \
990 --no-progress <modified-daily.txt &&
991 test_subcommand git prune-packed --quiet <modified-daily.txt &&
992 test_subcommand ! git multi-pack-index write --no-progress \
993 <modified-daily.txt
994 '
995
996 test_strategy () {
997 STRATEGY="$1"
998 shift
999
1000 cat >expect &&
1001 rm -f trace2.txt &&
1002 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
1003 git -c maintenance.strategy=$STRATEGY maintenance run --quiet "$@" &&
1004 sed -n 's/{"event":"child_start","sid":"[^/"]*",.*,"argv":\["\(.*\)\"]}/\1/p' <trace2.txt |
1005 sed 's/","/ /g' >actual
1006 test_cmp expect actual
1007 }
1008
1009 test_expect_success 'maintenance.strategy is respected' '
1010 test_when_finished "rm -rf repo" &&
1011 git init repo &&
1012 (
1013 cd repo &&
1014 test_commit initial &&
1015
1016 test_must_fail git -c maintenance.strategy=unknown maintenance run 2>err &&
1017 test_grep "unknown maintenance strategy: .unknown." err &&
1018
1019 test_strategy incremental <<-\EOF &&
1020 git pack-refs --all --prune
1021 git reflog expire --all
1022 git gc --quiet --no-detach --skip-foreground-tasks
1023 EOF
1024
1025 test_strategy incremental --schedule=weekly <<-\EOF &&
1026 git pack-refs --all --prune
1027 git prune-packed --quiet
1028 git multi-pack-index write --no-progress
1029 git multi-pack-index expire --no-progress
1030 git multi-pack-index repack --no-progress --batch-size=1
1031 git commit-graph write --split --reachable --no-progress
1032 EOF
1033
1034 test_strategy gc <<-\EOF &&
1035 git pack-refs --all --prune
1036 git reflog expire --all
1037 git gc --quiet --no-detach --skip-foreground-tasks
1038 EOF
1039
1040 test_strategy gc --schedule=weekly <<-\EOF &&
1041 git pack-refs --all --prune
1042 git reflog expire --all
1043 git gc --quiet --no-detach --skip-foreground-tasks
1044 EOF
1045
1046 test_strategy geometric <<-\EOF &&
1047 git pack-refs --all --prune
1048 git reflog expire --all
1049 git repack -d -l --geometric=2 --quiet --write-midx
1050 git commit-graph write --split --reachable --no-progress
1051 git worktree prune --expire 3.months.ago
1052 git rerere gc
1053 EOF
1054
1055 test_strategy geometric --schedule=weekly <<-\EOF
1056 git pack-refs --all --prune
1057 git reflog expire --all
1058 git repack -d -l --geometric=2 --quiet --write-midx
1059 git commit-graph write --split --reachable --no-progress
1060 git worktree prune --expire 3.months.ago
1061 git rerere gc
1062 EOF
1063 )
1064 '
1065
1066 test_expect_success 'register and unregister' '
1067 test_when_finished git config --global --unset-all maintenance.repo &&
1068
1069 test_must_fail git maintenance unregister 2>err &&
1070 grep "is not registered" err &&
1071 git maintenance unregister --force &&
1072
1073 git config --global --add maintenance.repo /existing1 &&
1074 git config --global --add maintenance.repo /existing2 &&
1075 git config --global --get-all maintenance.repo >before &&
1076
1077 git maintenance register &&
1078 test_cmp_config false maintenance.auto &&
1079 git config --global --get-all maintenance.repo >between &&
1080 cp before expect &&
1081 pwd >>expect &&
1082 test_cmp expect between &&
1083
1084 git maintenance unregister &&
1085 git config --global --get-all maintenance.repo >actual &&
1086 test_cmp before actual &&
1087
1088 git config --file ./other --add maintenance.repo /existing1 &&
1089 git config --file ./other --add maintenance.repo /existing2 &&
1090 git config --file ./other --get-all maintenance.repo >before &&
1091
1092 git maintenance register --config-file ./other &&
1093 test_cmp_config false maintenance.auto &&
1094 git config --file ./other --get-all maintenance.repo >between &&
1095 cp before expect &&
1096 pwd >>expect &&
1097 test_cmp expect between &&
1098
1099 git maintenance unregister --config-file ./other &&
1100 git config --file ./other --get-all maintenance.repo >actual &&
1101 test_cmp before actual &&
1102
1103 test_must_fail git maintenance unregister 2>err &&
1104 grep "is not registered" err &&
1105 git maintenance unregister --force &&
1106
1107 test_must_fail git maintenance unregister --config-file ./other 2>err &&
1108 grep "is not registered" err &&
1109 git maintenance unregister --config-file ./other --force
1110 '
1111
1112 test_expect_success 'register with no value for maintenance.repo' '
1113 cp .git/config .git/config.orig &&
1114 test_when_finished mv .git/config.orig .git/config &&
1115
1116 cat >>.git/config <<-\EOF &&
1117 [maintenance]
1118 repo
1119 EOF
1120 cat >expect <<-\EOF &&
1121 error: missing value for '\''maintenance.repo'\''
1122 EOF
1123 git maintenance register 2>actual &&
1124 test_cmp expect actual &&
1125 git config maintenance.repo
1126 '
1127
1128 test_expect_success 'unregister with no value for maintenance.repo' '
1129 cp .git/config .git/config.orig &&
1130 test_when_finished mv .git/config.orig .git/config &&
1131
1132 cat >>.git/config <<-\EOF &&
1133 [maintenance]
1134 repo
1135 EOF
1136 cat >expect <<-\EOF &&
1137 error: missing value for '\''maintenance.repo'\''
1138 EOF
1139 test_expect_code 128 git maintenance unregister 2>actual.raw &&
1140 grep ^error actual.raw >actual &&
1141 test_cmp expect actual &&
1142 git config maintenance.repo &&
1143
1144 git maintenance unregister --force 2>actual.raw &&
1145 grep ^error actual.raw >actual &&
1146 test_cmp expect actual &&
1147 git config maintenance.repo
1148 '
1149
1150 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
1151 META="a+b*c" &&
1152 git init "$META" &&
1153 git -C "$META" maintenance register &&
1154 git config --get-all --show-origin maintenance.repo &&
1155 git config --get-all --global --fixed-value \
1156 maintenance.repo "$(pwd)/$META" &&
1157 git -C "$META" maintenance unregister &&
1158 test_must_fail git config --get-all --global --fixed-value \
1159 maintenance.repo "$(pwd)/$META"
1160 '
1161
1162 test_expect_success 'start without GIT_TEST_MAINT_SCHEDULER' '
1163 test_when_finished "rm -rf systemctl.log script repo" &&
1164 mkdir script &&
1165 write_script script/systemctl <<-\EOF &&
1166 echo "$*" >>../systemctl.log
1167 EOF
1168 git init repo &&
1169 (
1170 cd repo &&
1171 sane_unset GIT_TEST_MAINT_SCHEDULER &&
1172 PATH="$PWD/../script:$PATH" git maintenance start --scheduler=systemd
1173 ) &&
1174 test_grep -- "--user list-timers" systemctl.log &&
1175 test_grep -- "enable --now git-maintenance@" systemctl.log
1176 '
1177
1178 test_expect_success 'start --scheduler=<scheduler>' '
1179 test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
1180 test_grep "unrecognized --scheduler argument" err &&
1181
1182 test_expect_code 129 git maintenance start --no-scheduler 2>err &&
1183 test_grep "unknown option" err &&
1184
1185 test_expect_code 128 \
1186 env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
1187 git maintenance start --scheduler=crontab 2>err &&
1188 test_grep "fatal: crontab scheduler is not available" err
1189 '
1190
1191 test_expect_success 'start from empty cron table' '
1192 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
1193
1194 # start registers the repo
1195 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1196
1197 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
1198 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
1199 grep "for-each-repo --keep-going --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
1200 '
1201
1202 test_expect_success 'stop from existing schedule' '
1203 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
1204
1205 # stop does not unregister the repo
1206 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1207
1208 # Operation is idempotent
1209 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
1210 test_must_be_empty cron.txt
1211 '
1212
1213 test_expect_success 'start preserves existing schedule' '
1214 echo "Important information!" >cron.txt &&
1215 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
1216 grep "Important information!" cron.txt
1217 '
1218
1219 test_expect_success 'magic markers are correct' '
1220 grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
1221 cat >expect <<-\EOF &&
1222 # BEGIN GIT MAINTENANCE SCHEDULE
1223 # END GIT MAINTENANCE SCHEDULE
1224 EOF
1225 test_cmp actual expect
1226 '
1227
1228 test_expect_success 'stop preserves surrounding schedule' '
1229 echo "Crucial information!" >>cron.txt &&
1230 GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
1231 grep "Important information!" cron.txt &&
1232 grep "Crucial information!" cron.txt
1233 '
1234
1235 test_expect_success 'start and stop macOS maintenance' '
1236 # ensure $HOME can be compared against hook arguments on all platforms
1237 pfx=$(cd "$HOME" && pwd) &&
1238
1239 write_script print-args <<-\EOF &&
1240 echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
1241 EOF
1242
1243 rm -f args &&
1244 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
1245
1246 # start registers the repo
1247 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1248
1249 ls "$HOME/Library/LaunchAgents" >actual &&
1250 cat >expect <<-\EOF &&
1251 org.git-scm.git.daily.plist
1252 org.git-scm.git.hourly.plist
1253 org.git-scm.git.weekly.plist
1254 EOF
1255 test_cmp expect actual &&
1256
1257 rm -f expect &&
1258 for frequency in hourly daily weekly
1259 do
1260 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
1261 test_xmllint "$PLIST" &&
1262 grep schedule=$frequency "$PLIST" &&
1263 echo "bootout gui/[UID] $PLIST" >>expect &&
1264 echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
1265 done &&
1266 test_cmp expect args &&
1267
1268 rm -f args &&
1269 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
1270
1271 # stop does not unregister the repo
1272 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1273
1274 printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1275 hourly daily weekly >expect &&
1276 test_cmp expect args &&
1277 ls "$HOME/Library/LaunchAgents" >actual &&
1278 test_line_count = 0 actual
1279 '
1280
1281 test_expect_success 'use launchctl list to prevent extra work' '
1282 # ensure we are registered
1283 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
1284
1285 # do it again on a fresh args file
1286 rm -f args &&
1287 GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
1288
1289 ls "$HOME/Library/LaunchAgents" >actual &&
1290 cat >expect <<-\EOF &&
1291 list org.git-scm.git.hourly
1292 list org.git-scm.git.daily
1293 list org.git-scm.git.weekly
1294 EOF
1295 test_cmp expect args
1296 '
1297
1298 test_expect_success 'start and stop Windows maintenance' '
1299 write_script print-args <<-\EOF &&
1300 echo $* >>args
1301 while test $# -gt 0
1302 do
1303 case "$1" in
1304 /xml) shift; xmlfile=$1; break ;;
1305 *) shift ;;
1306 esac
1307 done
1308 test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
1309 EOF
1310
1311 rm -f args &&
1312 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
1313
1314 # start registers the repo
1315 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1316
1317 for frequency in hourly daily weekly
1318 do
1319 grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
1320 file=$(ls .git/schedule_${frequency}*.xml) &&
1321 test_xmllint "$file" || return 1
1322 done &&
1323
1324 rm -f args &&
1325 GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
1326
1327 # stop does not unregister the repo
1328 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1329
1330 printf "/delete /tn Git Maintenance (%s) /f\n" \
1331 hourly daily weekly >expect &&
1332 test_cmp expect args
1333 '
1334
1335 test_expect_success 'start and stop Linux/systemd maintenance' '
1336 write_script print-args <<-\EOF &&
1337 printf "%s\n" "$*" >>args
1338 EOF
1339
1340 XDG_CONFIG_HOME="$PWD" &&
1341 export XDG_CONFIG_HOME &&
1342 rm -f args &&
1343 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
1344
1345 # start registers the repo
1346 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1347
1348 for schedule in hourly daily weekly
1349 do
1350 test_path_is_file "systemd/user/git-maintenance@$schedule.timer" || return 1
1351 done &&
1352 test_path_is_file "systemd/user/git-maintenance@.service" &&
1353
1354 test_systemd_analyze_verify "systemd/user/git-maintenance@hourly.service" &&
1355 test_systemd_analyze_verify "systemd/user/git-maintenance@daily.service" &&
1356 test_systemd_analyze_verify "systemd/user/git-maintenance@weekly.service" &&
1357
1358 grep "core.askPass=true" "systemd/user/git-maintenance@.service" &&
1359 grep "credential.interactive=false" "systemd/user/git-maintenance@.service" &&
1360
1361 printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1362 test_cmp expect args &&
1363
1364 rm -f args &&
1365 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
1366
1367 # stop does not unregister the repo
1368 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1369
1370 for schedule in hourly daily weekly
1371 do
1372 test_path_is_missing "systemd/user/git-maintenance@$schedule.timer" || return 1
1373 done &&
1374 test_path_is_missing "systemd/user/git-maintenance@.service" &&
1375
1376 printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1377 test_cmp expect args
1378 '
1379
1380 test_expect_success 'start and stop when several schedulers are available' '
1381 write_script print-args <<-\EOF &&
1382 printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
1383 EOF
1384
1385 rm -f args &&
1386 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
1387 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1388 hourly daily weekly >expect &&
1389 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
1390 hourly daily weekly >>expect &&
1391 printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
1392 test_cmp expect args &&
1393
1394 rm -f args &&
1395 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
1396 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1397 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
1398 hourly daily weekly >>expect &&
1399 for frequency in hourly daily weekly
1400 do
1401 PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
1402 echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
1403 echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
1404 done &&
1405 test_cmp expect args &&
1406
1407 rm -f args &&
1408 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
1409 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1410 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1411 hourly daily weekly >>expect &&
1412 printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
1413 hourly daily weekly >>expect &&
1414 test_cmp expect args &&
1415
1416 rm -f args &&
1417 GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
1418 printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
1419 printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
1420 hourly daily weekly >>expect &&
1421 printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
1422 hourly daily weekly >>expect &&
1423 test_cmp expect args
1424 '
1425
1426 test_expect_success 'register preserves existing strategy' '
1427 git config maintenance.strategy none &&
1428 git maintenance register &&
1429 test_config maintenance.strategy none &&
1430 git config --unset maintenance.strategy &&
1431 git maintenance register &&
1432 test_config maintenance.strategy incremental
1433 '
1434
1435 test_expect_success 'fails when running outside of a repository' '
1436 nongit test_must_fail git maintenance run &&
1437 nongit test_must_fail git maintenance stop &&
1438 nongit test_must_fail git maintenance start &&
1439 nongit test_must_fail git maintenance register &&
1440 nongit test_must_fail git maintenance unregister
1441 '
1442
1443 test_expect_success 'fails when configured to use an invalid strategy' '
1444 test_must_fail git -c maintenance.strategy=invalid maintenance run --schedule=hourly 2>err &&
1445 test_grep "unknown maintenance strategy: .invalid." err
1446 '
1447
1448 test_expect_success 'register and unregister bare repo' '
1449 test_when_finished "git config --global --unset-all maintenance.repo || :" &&
1450 test_might_fail git config --global --unset-all maintenance.repo &&
1451 git init --bare barerepo &&
1452 (
1453 cd barerepo &&
1454 git maintenance register &&
1455 git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
1456 git maintenance unregister &&
1457 test_must_fail git config --global --get-all maintenance.repo
1458 )
1459 '
1460
1461 test_expect_success 'failed schedule prevents config change' '
1462 git init --bare failcase &&
1463
1464 for scheduler in crontab launchctl schtasks systemctl
1465 do
1466 GIT_TEST_MAINT_SCHEDULER="$scheduler:false" &&
1467 export GIT_TEST_MAINT_SCHEDULER &&
1468 test_must_fail \
1469 git -C failcase maintenance start &&
1470 test_must_fail git -C failcase config maintenance.auto || return 1
1471 done
1472 '
1473
1474 test_expect_success '--no-detach causes maintenance to not run in background' '
1475 test_when_finished "rm -rf repo" &&
1476 git init repo &&
1477 (
1478 cd repo &&
1479
1480 # Prepare the repository such that git-maintenance(1) ends up
1481 # outputting something.
1482 test_commit something &&
1483 git config set maintenance.gc.enabled false &&
1484 git config set maintenance.loose-objects.enabled true &&
1485 git config set maintenance.loose-objects.auto 1 &&
1486 git config set maintenance.incremental-repack.enabled true &&
1487
1488 GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
1489 git maintenance run --no-detach >out 2>&1 &&
1490 ! test_region maintenance detach trace.txt
1491 )
1492 '
1493
1494 test_expect_success PIPE '--detach holds maintenance lock until daemonized child exits' '
1495 test_when_finished "rm -rf repo" &&
1496 git init repo &&
1497 (
1498 cd repo &&
1499
1500 git config maintenance.auto false &&
1501 git config core.lockfilepid true &&
1502
1503 git remote add origin /does/not/exist &&
1504 git config set remote.origin.uploadpack "cat fifo-uploadpack" &&
1505
1506 mkfifo fifo-uploadpack fifo-maint &&
1507
1508 # Open the maintenance FIFO, as otherwise spawning
1509 # git-maintenance(1) would block. Note that we need to open it
1510 # as read-write, as otherwise we would block here already.
1511 exec 9<>fifo-maint &&
1512
1513 { git maintenance run --task=prefetch --detach 7>&9 & } &&
1514 parent="$!" &&
1515
1516 # Reap the parent process so that the exec call below will not
1517 # get SIGCHLD.
1518 wait "$parent" &&
1519
1520 # Open the git-upload-pack(1) FIFO for writing, which will
1521 # block until the upload-pack script opens it for reading. Once
1522 # exec returns, we know that the daemonized child is alive and
1523 # pinned.
1524 exec 8>fifo-uploadpack &&
1525
1526 test_path_is_file .git/objects/maintenance.lock &&
1527 test_path_is_file .git/objects/"maintenance~pid.lock" &&
1528
1529 # Verify that the maintenance.lock still exists, and
1530 # that it was created by the parent process, not the
1531 # child.
1532 echo "pid $parent" >expect &&
1533 test_cmp expect .git/objects/"maintenance~pid.lock" &&
1534
1535 # Reopen the maintenance FIFO as read-only so that
1536 # git-maintenance(1) is the only writer. This will cause it to
1537 # close the FIFO once the process exits.
1538 exec 9<&- &&
1539 exec 9<fifo-maint &&
1540
1541 # Close the FIFO used by git-upload-pack(1) to unblock it and
1542 # then wait until the maintenance FIFO is closed by
1543 # git-maintenance(1), indicating that it has exited.
1544 exec 8>&- &&
1545 cat <&9 &&
1546
1547 test_path_is_missing .git/objects/maintenance.lock &&
1548 test_path_is_missing .git/objects/"maintenance~pid.lock"
1549 )
1550 '
1551
1552 test_expect_success '--detach causes maintenance to run in background' '
1553 test_when_finished "rm -rf repo" &&
1554 git init repo &&
1555 (
1556 cd repo &&
1557
1558 test_commit something &&
1559 git config set maintenance.gc.enabled false &&
1560 git config set maintenance.loose-objects.enabled true &&
1561 git config set maintenance.loose-objects.auto 1 &&
1562 git config set maintenance.incremental-repack.enabled true &&
1563
1564 # The extra file descriptor gets inherited to the child
1565 # process, and by reading stdout we thus essentially wait for
1566 # that descriptor to get closed, which indicates that the child
1567 # is done, too.
1568 does_not_matter=$(GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
1569 git maintenance run --detach 9>&1) &&
1570 test_region maintenance detach trace.txt
1571 )
1572 '
1573
1574 test_expect_success 'repacking loose objects is quiet' '
1575 test_when_finished "rm -rf repo" &&
1576 git init repo &&
1577 (
1578 cd repo &&
1579
1580 test_commit something &&
1581 git config set maintenance.gc.enabled false &&
1582 git config set maintenance.loose-objects.enabled true &&
1583 git config set maintenance.loose-objects.auto 1 &&
1584
1585 git maintenance run --quiet >out 2>&1 &&
1586 test_must_be_empty out
1587 )
1588 '
1589
1590 test_expect_success 'maintenance aborts with existing lock file' '
1591 test_when_finished "rm -rf repo script" &&
1592 mkdir script &&
1593 write_script script/systemctl <<-\EOF &&
1594 true
1595 EOF
1596
1597 git init repo &&
1598 : >repo/.git/objects/schedule.lock &&
1599 test_must_fail env PATH="$PWD/script:$PATH" git -C repo maintenance start --scheduler=systemd 2>err &&
1600 test_grep "Another scheduled git-maintenance(1) process seems to be running" err
1601 '
1602
1603 test_done