commit-graph: when incompatible with graphs, indicate why
[git] / t / t7900-maintenance.sh
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 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
17 '
18
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
29 '
30
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
42 '
43
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
50 '
51
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
67 '
68
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 \
74                 <no-commit-graph.txt
75 '
76
77 test_expect_success 'commit-graph auto condition' '
78         COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
79
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 &&
84
85         test_commit first &&
86
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 &&
91
92         git commit --allow-empty -m "second" &&
93         git commit --allow-empty -m "third" &&
94
95         GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
96                 git -c maintenance.commit-graph.auto=2 $COMMAND &&
97
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
104 '
105
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
109 '
110
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
114 '
115
116 test_expect_success 'run --task=prefetch with no remotes' '
117         git maintenance run --task=prefetch 2>err &&
118         test_must_be_empty err
119 '
120
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 &&
137         git fetch --all &&
138         test_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&
139         test_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two
140 '
141
142 test_expect_success 'loose-objects task' '
143         # Repack everything so we know the state of the object dir
144         git repack -adk &&
145
146         # Hack to stop maintenance from running during "git commit"
147         echo in use >.git/objects/maintenance.lock &&
148
149         # Assuming that "git commit" creates at least one loose object
150         test_commit create-loose-object &&
151         rm .git/objects/maintenance.lock &&
152
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 &&
157
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 &&
167
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 &&
173         info
174         pack
175         EOF
176         test_cmp expect obj-dir-after &&
177         ls .git/objects/pack/*.pack >packs-after &&
178         test_cmp packs-between packs-after
179 '
180
181 test_expect_success 'maintenance.loose-objects.auto' '
182         git repack -adk &&
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
201 '
202
203 test_expect_success 'incremental-repack task' '
204         packDir=.git/objects/pack &&
205         for i in $(test_seq 1 5)
206         do
207                 test_commit $i || return 1
208         done &&
209
210         # Create three disjoint pack-files with size BIG, small, small.
211         echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
212         test_tick &&
213         git pack-objects --revs $packDir/test-2 <<-\EOF &&
214         HEAD~1
215         ^HEAD~2
216         EOF
217         test_tick &&
218         git pack-objects --revs $packDir/test-3 <<-\EOF &&
219         HEAD
220         ^HEAD~1
221         EOF
222         rm -f $packDir/pack-* &&
223         rm -f $packDir/loose-* &&
224         ls $packDir/*.pack >packs-before &&
225         test_line_count = 3 packs-before &&
226
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 &&
232
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
239 '
240
241 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
242         test_config core.compression 0 &&
243
244         for i in $(test_seq 1 5)
245         do
246                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
247                 return 1
248         done &&
249         git add big &&
250         git commit -qm "Add big file (1)" &&
251
252         # ensure any possible loose objects are in a pack-file
253         git maintenance run --task=loose-objects &&
254
255         rm big &&
256         for i in $(test_seq 6 10)
257         do
258                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
259                 return 1
260         done &&
261         git add big &&
262         git commit -qm "Add big file (2)" &&
263
264         # ensure any possible loose objects are in a pack-file
265         git maintenance run --task=loose-objects &&
266
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
272 '
273
274 test_expect_success 'maintenance.incremental-repack.auto' '
275         git repack -adk &&
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 &&
282         test_commit A &&
283         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
284         HEAD
285         ^HEAD~1
286         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 &&
291         test_commit B &&
292         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
293         HEAD
294         ^HEAD~1
295         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
300 '
301
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
305 '
306
307 test_expect_success 'invalid --schedule value' '
308         test_must_fail git maintenance run --schedule=annually 2>err &&
309         test_i18ngrep "unrecognized --schedule" err
310 '
311
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 &&
319
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 &&
326
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 &&
333
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
340 '
341
342 test_expect_success 'maintenance.strategy inheritance' '
343         for task in commit-graph loose-objects incremental-repack
344         do
345                 git config --unset maintenance.$task.schedule || return 1
346         done &&
347
348         test_when_finished git config --unset maintenance.strategy &&
349         git config maintenance.strategy incremental &&
350
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 &&
355
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 &&
361
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 &&
367
368         # Modify defaults
369         git config maintenance.commit-graph.schedule daily &&
370         git config maintenance.loose-objects.schedule hourly &&
371         git config maintenance.incremental-repack.enabled false &&
372
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 &&
377
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 &&
383
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 \
388                 <modified-daily.txt
389 '
390
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 &&
396
397         git maintenance register &&
398         test_cmp_config false maintenance.auto &&
399         git config --global --get-all maintenance.repo >between &&
400         cp before expect &&
401         pwd >>expect &&
402         test_cmp expect between &&
403
404         git maintenance unregister &&
405         git config --global --get-all maintenance.repo >actual &&
406         test_cmp before actual
407 '
408
409 test_expect_success !MINGW 'register and unregister with regex metacharacters' '
410         META="a+b*c" &&
411         git init "$META" &&
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"
419 '
420
421 test_expect_success 'start from empty cron table' '
422         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance start &&
423
424         # start registers the repo
425         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
426
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
430 '
431
432 test_expect_success 'stop from existing schedule' '
433         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
434
435         # stop does not unregister the repo
436         git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
437
438         # Operation is idempotent
439         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
440         test_must_be_empty cron.txt
441 '
442
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
447 '
448
449 test_expect_success 'magic markers are correct' '
450         grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
451         cat >expect <<-\EOF &&
452         # BEGIN GIT MAINTENANCE SCHEDULE
453         # END GIT MAINTENANCE SCHEDULE
454         EOF
455         test_cmp actual expect
456 '
457
458 test_expect_success 'stop preserves surrounding schedule' '
459         echo "Crucial information!" >>cron.txt &&
460         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
461         grep "Important information!" cron.txt &&
462         grep "Crucial information!" cron.txt
463 '
464
465 test_expect_success 'register preserves existing strategy' '
466         git config maintenance.strategy none &&
467         git maintenance register &&
468         test_config maintenance.strategy none &&
469         git config --unset maintenance.strategy &&
470         git maintenance register &&
471         test_config maintenance.strategy incremental
472 '
473
474 test_expect_success 'fails when running outside of a repository' '
475         nongit test_must_fail git maintenance run &&
476         nongit test_must_fail git maintenance stop &&
477         nongit test_must_fail git maintenance start &&
478         nongit test_must_fail git maintenance register &&
479         nongit test_must_fail git maintenance unregister
480 '
481
482 test_done