3 test_description='git maintenance builtin'
7 GIT_TEST_COMMIT_GRAPH=0
8 GIT_TEST_MULTI_PACK_INDEX=0
10 test_expect_success 'help text' '
11 test_expect_code 129 git maintenance -h 2>err &&
12 test_i18ngrep "usage: git maintenance <subcommand>" err &&
13 test_expect_code 128 git maintenance barf 2>err &&
14 test_i18ngrep "invalid subcommand: barf" err &&
15 test_expect_code 129 git maintenance 2>err &&
16 test_i18ngrep "usage: git maintenance" err
19 test_expect_success 'run [--auto|--quiet]' '
20 GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
21 git maintenance run 2>/dev/null &&
22 GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
23 git maintenance run --auto 2>/dev/null &&
24 GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
25 git maintenance run --no-quiet 2>/dev/null &&
26 test_subcommand git gc --quiet <run-no-auto.txt &&
27 test_subcommand ! git gc --auto --quiet <run-auto.txt &&
28 test_subcommand git gc --no-quiet <run-no-quiet.txt
31 test_expect_success 'maintenance.auto config option' '
32 GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
33 test_subcommand git maintenance run --auto --quiet <default &&
34 GIT_TRACE2_EVENT="$(pwd)/true" \
35 git -c maintenance.auto=true \
36 commit --quiet --allow-empty -m 2 &&
37 test_subcommand git maintenance run --auto --quiet <true &&
38 GIT_TRACE2_EVENT="$(pwd)/false" \
39 git -c maintenance.auto=false \
40 commit --quiet --allow-empty -m 3 &&
41 test_subcommand ! git maintenance run --auto --quiet <false
44 test_expect_success 'maintenance.<task>.enabled' '
45 git config maintenance.gc.enabled false &&
46 git config maintenance.commit-graph.enabled true &&
47 GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
48 test_subcommand ! git gc --quiet <run-config.txt &&
49 test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
52 test_expect_success 'run --task=<task>' '
53 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
54 git maintenance run --task=commit-graph 2>/dev/null &&
55 GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
56 git maintenance run --task=gc 2>/dev/null &&
57 GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
58 git maintenance run --task=commit-graph 2>/dev/null &&
59 GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
60 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
61 test_subcommand ! git gc --quiet <run-commit-graph.txt &&
62 test_subcommand git gc --quiet <run-gc.txt &&
63 test_subcommand git gc --quiet <run-both.txt &&
64 test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
65 test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
66 test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
69 test_expect_success 'core.commitGraph=false prevents write process' '
70 GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
71 git -c core.commitGraph=false maintenance run \
72 --task=commit-graph 2>/dev/null &&
73 test_subcommand ! git commit-graph write --split --reachable --no-progress \
77 test_expect_success 'commit-graph auto condition' '
78 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
80 GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
81 git -c maintenance.commit-graph.auto=1 $COMMAND &&
82 GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
83 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
87 GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
88 git -c maintenance.commit-graph.auto=0 $COMMAND &&
89 GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
90 git -c maintenance.commit-graph.auto=1 $COMMAND &&
92 git commit --allow-empty -m "second" &&
93 git commit --allow-empty -m "third" &&
95 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
96 git -c maintenance.commit-graph.auto=2 $COMMAND &&
98 COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
99 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
100 test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
101 test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
102 test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
103 test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
106 test_expect_success 'run --task=bogus' '
107 test_must_fail git maintenance run --task=bogus 2>err &&
108 test_i18ngrep "is not a valid task" err
111 test_expect_success 'run --task duplicate' '
112 test_must_fail git maintenance run --task=gc --task=gc 2>err &&
113 test_i18ngrep "cannot be selected multiple times" err
116 test_expect_success 'run --task=prefetch with no remotes' '
117 git maintenance run --task=prefetch 2>err &&
118 test_must_be_empty err
121 test_expect_success 'prefetch multiple remotes' '
122 git clone . clone1 &&
123 git clone . clone2 &&
124 git remote add remote1 "file://$(pwd)/clone1" &&
125 git remote add remote2 "file://$(pwd)/clone2" &&
126 git -C clone1 switch -c one &&
127 git -C clone2 switch -c two &&
128 test_commit -C clone1 one &&
129 test_commit -C clone2 two &&
130 GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
131 fetchargs="--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet" &&
132 test_subcommand git fetch remote1 $fetchargs +refs/heads/\\*:refs/prefetch/remote1/\\* <run-prefetch.txt &&
133 test_subcommand git fetch remote2 $fetchargs +refs/heads/\\*:refs/prefetch/remote2/\\* <run-prefetch.txt &&
134 test_path_is_missing .git/refs/remotes &&
135 git log prefetch/remote1/one &&
136 git log prefetch/remote2/two &&
138 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&
139 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two
142 test_expect_success 'loose-objects task' '
143 # Repack everything so we know the state of the object dir
146 # Hack to stop maintenance from running during "git commit"
147 echo in use >.git/objects/maintenance.lock &&
149 # Assuming that "git commit" creates at least one loose object
150 test_commit create-loose-object &&
151 rm .git/objects/maintenance.lock &&
153 ls .git/objects >obj-dir-before &&
154 test_file_not_empty obj-dir-before &&
155 ls .git/objects/pack/*.pack >packs-before &&
156 test_line_count = 1 packs-before &&
158 # The first run creates a pack-file
159 # but does not delete loose objects.
160 git maintenance run --task=loose-objects &&
161 ls .git/objects >obj-dir-between &&
162 test_cmp obj-dir-before obj-dir-between &&
163 ls .git/objects/pack/*.pack >packs-between &&
164 test_line_count = 2 packs-between &&
165 ls .git/objects/pack/loose-*.pack >loose-packs &&
166 test_line_count = 1 loose-packs &&
168 # The second run deletes loose objects
169 # but does not create a pack-file.
170 git maintenance run --task=loose-objects &&
171 ls .git/objects >obj-dir-after &&
172 cat >expect <<-\EOF &&
176 test_cmp expect obj-dir-after &&
177 ls .git/objects/pack/*.pack >packs-after &&
178 test_cmp packs-between packs-after
181 test_expect_success 'maintenance.loose-objects.auto' '
183 GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
184 git -c maintenance.loose-objects.auto=1 maintenance \
185 run --auto --task=loose-objects 2>/dev/null &&
186 test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
187 printf data-A | git hash-object -t blob --stdin -w &&
188 GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
189 git -c maintenance.loose-objects.auto=2 \
190 maintenance run --auto --task=loose-objects 2>/dev/null &&
191 test_subcommand ! git prune-packed --quiet <trace-loA &&
192 printf data-B | git hash-object -t blob --stdin -w &&
193 GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
194 git -c maintenance.loose-objects.auto=2 \
195 maintenance run --auto --task=loose-objects 2>/dev/null &&
196 test_subcommand git prune-packed --quiet <trace-loB &&
197 GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
198 git -c maintenance.loose-objects.auto=2 \
199 maintenance run --auto --task=loose-objects 2>/dev/null &&
200 test_subcommand git prune-packed --quiet <trace-loC
203 test_expect_success 'incremental-repack task' '
204 packDir=.git/objects/pack &&
205 for i in $(test_seq 1 5)
207 test_commit $i || return 1
210 # Create three disjoint pack-files with size BIG, small, small.
211 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
213 git pack-objects --revs $packDir/test-2 <<-\EOF &&
218 git pack-objects --revs $packDir/test-3 <<-\EOF &&
222 rm -f $packDir/pack-* &&
223 rm -f $packDir/loose-* &&
224 ls $packDir/*.pack >packs-before &&
225 test_line_count = 3 packs-before &&
227 # the job repacks the two into a new pack, but does not
228 # delete the old ones.
229 git maintenance run --task=incremental-repack &&
230 ls $packDir/*.pack >packs-between &&
231 test_line_count = 4 packs-between &&
233 # the job deletes the two old packs, and does not write
234 # a new one because the batch size is not high enough to
235 # pack the largest pack-file.
236 git maintenance run --task=incremental-repack &&
237 ls .git/objects/pack/*.pack >packs-after &&
238 test_line_count = 2 packs-after
241 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
242 test_config core.compression 0 &&
244 for i in $(test_seq 1 5)
246 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
250 git commit -qm "Add big file (1)" &&
252 # ensure any possible loose objects are in a pack-file
253 git maintenance run --task=loose-objects &&
256 for i in $(test_seq 6 10)
258 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
262 git commit -qm "Add big file (2)" &&
264 # ensure any possible loose objects are in a pack-file
265 git maintenance run --task=loose-objects &&
267 # Now run the incremental-repack task and check the batch-size
268 GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
269 --task=incremental-repack 2>/dev/null &&
270 test_subcommand git multi-pack-index repack \
271 --no-progress --batch-size=2147483647 <run-2g.txt
274 test_expect_success 'maintenance.incremental-repack.auto' '
276 git config core.multiPackIndex true &&
277 git multi-pack-index write &&
278 GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
279 -c maintenance.incremental-repack.auto=1 \
280 maintenance run --auto --task=incremental-repack 2>/dev/null &&
281 test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
283 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
287 GIT_TRACE2_EVENT=$(pwd)/trace-A git \
288 -c maintenance.incremental-repack.auto=2 \
289 maintenance run --auto --task=incremental-repack 2>/dev/null &&
290 test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
292 git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
296 GIT_TRACE2_EVENT=$(pwd)/trace-B git \
297 -c maintenance.incremental-repack.auto=2 \
298 maintenance run --auto --task=incremental-repack 2>/dev/null &&
299 test_subcommand git multi-pack-index write --no-progress <trace-B
302 test_expect_success '--auto and --schedule incompatible' '
303 test_must_fail git maintenance run --auto --schedule=daily 2>err &&
304 test_i18ngrep "at most one" err
307 test_expect_success 'invalid --schedule value' '
308 test_must_fail git maintenance run --schedule=annually 2>err &&
309 test_i18ngrep "unrecognized --schedule" err
312 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
313 git config maintenance.loose-objects.enabled true &&
314 git config maintenance.loose-objects.schedule hourly &&
315 git config maintenance.commit-graph.enabled true &&
316 git config maintenance.commit-graph.schedule daily &&
317 git config maintenance.incremental-repack.enabled true &&
318 git config maintenance.incremental-repack.schedule weekly &&
320 GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
321 git maintenance run --schedule=hourly 2>/dev/null &&
322 test_subcommand git prune-packed --quiet <hourly.txt &&
323 test_subcommand ! git commit-graph write --split --reachable \
324 --no-progress <hourly.txt &&
325 test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
327 GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
328 git maintenance run --schedule=daily 2>/dev/null &&
329 test_subcommand git prune-packed --quiet <daily.txt &&
330 test_subcommand git commit-graph write --split --reachable \
331 --no-progress <daily.txt &&
332 test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
334 GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
335 git maintenance run --schedule=weekly 2>/dev/null &&
336 test_subcommand git prune-packed --quiet <weekly.txt &&
337 test_subcommand git commit-graph write --split --reachable \
338 --no-progress <weekly.txt &&
339 test_subcommand git multi-pack-index write --no-progress <weekly.txt
342 test_expect_success 'maintenance.strategy inheritance' '
343 for task in commit-graph loose-objects incremental-repack
345 git config --unset maintenance.$task.schedule || return 1
348 test_when_finished git config --unset maintenance.strategy &&
349 git config maintenance.strategy incremental &&
351 GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
352 git maintenance run --schedule=hourly --quiet &&
353 GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
354 git maintenance run --schedule=daily --quiet &&
356 test_subcommand git commit-graph write --split --reachable \
357 --no-progress <incremental-hourly.txt &&
358 test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
359 test_subcommand ! git multi-pack-index write --no-progress \
360 <incremental-hourly.txt &&
362 test_subcommand git commit-graph write --split --reachable \
363 --no-progress <incremental-daily.txt &&
364 test_subcommand git prune-packed --quiet <incremental-daily.txt &&
365 test_subcommand git multi-pack-index write --no-progress \
366 <incremental-daily.txt &&
369 git config maintenance.commit-graph.schedule daily &&
370 git config maintenance.loose-objects.schedule hourly &&
371 git config maintenance.incremental-repack.enabled false &&
373 GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
374 git maintenance run --schedule=hourly --quiet &&
375 GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
376 git maintenance run --schedule=daily --quiet &&
378 test_subcommand ! git commit-graph write --split --reachable \
379 --no-progress <modified-hourly.txt &&
380 test_subcommand git prune-packed --quiet <modified-hourly.txt &&
381 test_subcommand ! git multi-pack-index write --no-progress \
382 <modified-hourly.txt &&
384 test_subcommand git commit-graph write --split --reachable \
385 --no-progress <modified-daily.txt &&
386 test_subcommand git prune-packed --quiet <modified-daily.txt &&
387 test_subcommand ! git multi-pack-index write --no-progress \
391 test_expect_success 'register and unregister' '
392 test_when_finished git config --global --unset-all maintenance.repo &&
393 git config --global --add maintenance.repo /existing1 &&
394 git config --global --add maintenance.repo /existing2 &&
395 git config --global --get-all maintenance.repo >before &&
397 git maintenance register &&
398 test_cmp_config false maintenance.auto &&
399 git config --global --get-all maintenance.repo >between &&
402 test_cmp expect between &&
404 git maintenance unregister &&
405 git config --global --get-all maintenance.repo >actual &&
406 test_cmp before actual
409 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
412 git -C "$META" maintenance register &&
413 git config --get-all --show-origin maintenance.repo &&
414 git config --get-all --global --fixed-value \
415 maintenance.repo "$(pwd)/$META" &&
416 git -C "$META" maintenance unregister &&
417 test_must_fail git config --get-all --global --fixed-value \
418 maintenance.repo "$(pwd)/$META"
421 test_expect_success 'start from empty cron table' '
422 GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance start &&
424 # start registers the repo
425 git config --get --global maintenance.repo "$(pwd)" &&
427 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
428 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
429 grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
432 test_expect_success 'stop from existing schedule' '
433 GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
435 # stop does not unregister the repo
436 git config --get --global maintenance.repo "$(pwd)" &&
438 # Operation is idempotent
439 GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
440 test_must_be_empty cron.txt
443 test_expect_success 'start preserves existing schedule' '
444 echo "Important information!" >cron.txt &&
445 GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance start &&
446 grep "Important information!" cron.txt
449 test_expect_success 'register preserves existing strategy' '
450 git config maintenance.strategy none &&
451 git maintenance register &&
452 test_config maintenance.strategy none &&
453 git config --unset maintenance.strategy &&
454 git maintenance register &&
455 test_config maintenance.strategy incremental