Merge branch 'pb/blame-funcname-range-userdiff'
[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         for i in $(test_seq 1 5)
243         do
244                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
245                 return 1
246         done &&
247         git add big &&
248         git commit -m "Add big file (1)" &&
249
250         # ensure any possible loose objects are in a pack-file
251         git maintenance run --task=loose-objects &&
252
253         rm big &&
254         for i in $(test_seq 6 10)
255         do
256                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
257                 return 1
258         done &&
259         git add big &&
260         git commit -m "Add big file (2)" &&
261
262         # ensure any possible loose objects are in a pack-file
263         git maintenance run --task=loose-objects &&
264
265         # Now run the incremental-repack task and check the batch-size
266         GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
267                 --task=incremental-repack 2>/dev/null &&
268         test_subcommand git multi-pack-index repack \
269                  --no-progress --batch-size=2147483647 <run-2g.txt
270 '
271
272 test_expect_success 'maintenance.incremental-repack.auto' '
273         git repack -adk &&
274         git config core.multiPackIndex true &&
275         git multi-pack-index write &&
276         GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
277                 -c maintenance.incremental-repack.auto=1 \
278                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
279         test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
280         test_commit A &&
281         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
282         HEAD
283         ^HEAD~1
284         EOF
285         GIT_TRACE2_EVENT=$(pwd)/trace-A git \
286                 -c maintenance.incremental-repack.auto=2 \
287                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
288         test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
289         test_commit B &&
290         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
291         HEAD
292         ^HEAD~1
293         EOF
294         GIT_TRACE2_EVENT=$(pwd)/trace-B git \
295                 -c maintenance.incremental-repack.auto=2 \
296                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
297         test_subcommand git multi-pack-index write --no-progress <trace-B
298 '
299
300 test_expect_success '--auto and --schedule incompatible' '
301         test_must_fail git maintenance run --auto --schedule=daily 2>err &&
302         test_i18ngrep "at most one" err
303 '
304
305 test_expect_success 'invalid --schedule value' '
306         test_must_fail git maintenance run --schedule=annually 2>err &&
307         test_i18ngrep "unrecognized --schedule" err
308 '
309
310 test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
311         git config maintenance.loose-objects.enabled true &&
312         git config maintenance.loose-objects.schedule hourly &&
313         git config maintenance.commit-graph.enabled true &&
314         git config maintenance.commit-graph.schedule daily &&
315         git config maintenance.incremental-repack.enabled true &&
316         git config maintenance.incremental-repack.schedule weekly &&
317
318         GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
319                 git maintenance run --schedule=hourly 2>/dev/null &&
320         test_subcommand git prune-packed --quiet <hourly.txt &&
321         test_subcommand ! git commit-graph write --split --reachable \
322                 --no-progress <hourly.txt &&
323         test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
324
325         GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
326                 git maintenance run --schedule=daily 2>/dev/null &&
327         test_subcommand git prune-packed --quiet <daily.txt &&
328         test_subcommand git commit-graph write --split --reachable \
329                 --no-progress <daily.txt &&
330         test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
331
332         GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
333                 git maintenance run --schedule=weekly 2>/dev/null &&
334         test_subcommand git prune-packed --quiet <weekly.txt &&
335         test_subcommand git commit-graph write --split --reachable \
336                 --no-progress <weekly.txt &&
337         test_subcommand git multi-pack-index write --no-progress <weekly.txt
338 '
339
340 test_expect_success 'maintenance.strategy inheritance' '
341         for task in commit-graph loose-objects incremental-repack
342         do
343                 git config --unset maintenance.$task.schedule || return 1
344         done &&
345
346         test_when_finished git config --unset maintenance.strategy &&
347         git config maintenance.strategy incremental &&
348
349         GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
350                 git maintenance run --schedule=hourly --quiet &&
351         GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
352                 git maintenance run --schedule=daily --quiet &&
353
354         test_subcommand git commit-graph write --split --reachable \
355                 --no-progress <incremental-hourly.txt &&
356         test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
357         test_subcommand ! git multi-pack-index write --no-progress \
358                 <incremental-hourly.txt &&
359
360         test_subcommand git commit-graph write --split --reachable \
361                 --no-progress <incremental-daily.txt &&
362         test_subcommand git prune-packed --quiet <incremental-daily.txt &&
363         test_subcommand git multi-pack-index write --no-progress \
364                 <incremental-daily.txt &&
365
366         # Modify defaults
367         git config maintenance.commit-graph.schedule daily &&
368         git config maintenance.loose-objects.schedule hourly &&
369         git config maintenance.incremental-repack.enabled false &&
370
371         GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
372                 git maintenance run --schedule=hourly --quiet &&
373         GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
374                 git maintenance run --schedule=daily --quiet &&
375
376         test_subcommand ! git commit-graph write --split --reachable \
377                 --no-progress <modified-hourly.txt &&
378         test_subcommand git prune-packed --quiet <modified-hourly.txt &&
379         test_subcommand ! git multi-pack-index write --no-progress \
380                 <modified-hourly.txt &&
381
382         test_subcommand git commit-graph write --split --reachable \
383                 --no-progress <modified-daily.txt &&
384         test_subcommand git prune-packed --quiet <modified-daily.txt &&
385         test_subcommand ! git multi-pack-index write --no-progress \
386                 <modified-daily.txt
387 '
388
389 test_expect_success 'register and unregister' '
390         test_when_finished git config --global --unset-all maintenance.repo &&
391         git config --global --add maintenance.repo /existing1 &&
392         git config --global --add maintenance.repo /existing2 &&
393         git config --global --get-all maintenance.repo >before &&
394
395         git maintenance register &&
396         test_cmp_config false maintenance.auto &&
397         git config --global --get-all maintenance.repo >between &&
398         cp before expect &&
399         pwd >>expect &&
400         test_cmp expect between &&
401
402         git maintenance unregister &&
403         git config --global --get-all maintenance.repo >actual &&
404         test_cmp before actual
405 '
406
407 test_expect_success 'start from empty cron table' '
408         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance start &&
409
410         # start registers the repo
411         git config --get --global maintenance.repo "$(pwd)" &&
412
413         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
414         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
415         grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
416 '
417
418 test_expect_success 'stop from existing schedule' '
419         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
420
421         # stop does not unregister the repo
422         git config --get --global maintenance.repo "$(pwd)" &&
423
424         # Operation is idempotent
425         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance stop &&
426         test_must_be_empty cron.txt
427 '
428
429 test_expect_success 'start preserves existing schedule' '
430         echo "Important information!" >cron.txt &&
431         GIT_TEST_CRONTAB="test-tool crontab cron.txt" git maintenance start &&
432         grep "Important information!" cron.txt
433 '
434
435 test_expect_success 'register preserves existing strategy' '
436         git config maintenance.strategy none &&
437         git maintenance register &&
438         test_config maintenance.strategy none &&
439         git config --unset maintenance.strategy &&
440         git maintenance register &&
441         test_config maintenance.strategy incremental
442 '
443
444 test_done