Merge branch 'bc/svn-hash-oid-fix'
[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 run" 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.<task>.enabled' '
32         git config maintenance.gc.enabled false &&
33         git config maintenance.commit-graph.enabled true &&
34         GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
35         test_subcommand ! git gc --quiet <run-config.txt &&
36         test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
37 '
38
39 test_expect_success 'run --task=<task>' '
40         GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
41                 git maintenance run --task=commit-graph 2>/dev/null &&
42         GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
43                 git maintenance run --task=gc 2>/dev/null &&
44         GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
45                 git maintenance run --task=commit-graph 2>/dev/null &&
46         GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
47                 git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
48         test_subcommand ! git gc --quiet <run-commit-graph.txt &&
49         test_subcommand git gc --quiet <run-gc.txt &&
50         test_subcommand git gc --quiet <run-both.txt &&
51         test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
52         test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
53         test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
54 '
55
56 test_expect_success 'core.commitGraph=false prevents write process' '
57         GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
58                 git -c core.commitGraph=false maintenance run \
59                 --task=commit-graph 2>/dev/null &&
60         test_subcommand ! git commit-graph write --split --reachable --no-progress \
61                 <no-commit-graph.txt
62 '
63
64 test_expect_success 'commit-graph auto condition' '
65         COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
66
67         GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
68                 git -c maintenance.commit-graph.auto=1 $COMMAND &&
69         GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
70                 git -c maintenance.commit-graph.auto="-1" $COMMAND &&
71
72         test_commit first &&
73
74         GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
75                 git -c maintenance.commit-graph.auto=0 $COMMAND &&
76         GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
77                 git -c maintenance.commit-graph.auto=1 $COMMAND &&
78
79         git commit --allow-empty -m "second" &&
80         git commit --allow-empty -m "third" &&
81
82         GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
83                 git -c maintenance.commit-graph.auto=2 $COMMAND &&
84
85         COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
86         test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
87         test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
88         test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
89         test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
90         test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
91 '
92
93 test_expect_success 'run --task=bogus' '
94         test_must_fail git maintenance run --task=bogus 2>err &&
95         test_i18ngrep "is not a valid task" err
96 '
97
98 test_expect_success 'run --task duplicate' '
99         test_must_fail git maintenance run --task=gc --task=gc 2>err &&
100         test_i18ngrep "cannot be selected multiple times" err
101 '
102
103 test_expect_success 'run --task=prefetch with no remotes' '
104         git maintenance run --task=prefetch 2>err &&
105         test_must_be_empty err
106 '
107
108 test_expect_success 'prefetch multiple remotes' '
109         git clone . clone1 &&
110         git clone . clone2 &&
111         git remote add remote1 "file://$(pwd)/clone1" &&
112         git remote add remote2 "file://$(pwd)/clone2" &&
113         git -C clone1 switch -c one &&
114         git -C clone2 switch -c two &&
115         test_commit -C clone1 one &&
116         test_commit -C clone2 two &&
117         GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
118         fetchargs="--prune --no-tags --no-write-fetch-head --recurse-submodules=no --refmap= --quiet" &&
119         test_subcommand git fetch remote1 $fetchargs +refs/heads/\\*:refs/prefetch/remote1/\\* <run-prefetch.txt &&
120         test_subcommand git fetch remote2 $fetchargs +refs/heads/\\*:refs/prefetch/remote2/\\* <run-prefetch.txt &&
121         test_path_is_missing .git/refs/remotes &&
122         git log prefetch/remote1/one &&
123         git log prefetch/remote2/two &&
124         git fetch --all &&
125         test_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&
126         test_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two
127 '
128
129 test_expect_success 'loose-objects task' '
130         # Repack everything so we know the state of the object dir
131         git repack -adk &&
132
133         # Hack to stop maintenance from running during "git commit"
134         echo in use >.git/objects/maintenance.lock &&
135
136         # Assuming that "git commit" creates at least one loose object
137         test_commit create-loose-object &&
138         rm .git/objects/maintenance.lock &&
139
140         ls .git/objects >obj-dir-before &&
141         test_file_not_empty obj-dir-before &&
142         ls .git/objects/pack/*.pack >packs-before &&
143         test_line_count = 1 packs-before &&
144
145         # The first run creates a pack-file
146         # but does not delete loose objects.
147         git maintenance run --task=loose-objects &&
148         ls .git/objects >obj-dir-between &&
149         test_cmp obj-dir-before obj-dir-between &&
150         ls .git/objects/pack/*.pack >packs-between &&
151         test_line_count = 2 packs-between &&
152         ls .git/objects/pack/loose-*.pack >loose-packs &&
153         test_line_count = 1 loose-packs &&
154
155         # The second run deletes loose objects
156         # but does not create a pack-file.
157         git maintenance run --task=loose-objects &&
158         ls .git/objects >obj-dir-after &&
159         cat >expect <<-\EOF &&
160         info
161         pack
162         EOF
163         test_cmp expect obj-dir-after &&
164         ls .git/objects/pack/*.pack >packs-after &&
165         test_cmp packs-between packs-after
166 '
167
168 test_expect_success 'maintenance.loose-objects.auto' '
169         git repack -adk &&
170         GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
171                 git -c maintenance.loose-objects.auto=1 maintenance \
172                 run --auto --task=loose-objects 2>/dev/null &&
173         test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
174         printf data-A | git hash-object -t blob --stdin -w &&
175         GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
176                 git -c maintenance.loose-objects.auto=2 \
177                 maintenance run --auto --task=loose-objects 2>/dev/null &&
178         test_subcommand ! git prune-packed --quiet <trace-loA &&
179         printf data-B | git hash-object -t blob --stdin -w &&
180         GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
181                 git -c maintenance.loose-objects.auto=2 \
182                 maintenance run --auto --task=loose-objects 2>/dev/null &&
183         test_subcommand git prune-packed --quiet <trace-loB &&
184         GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
185                 git -c maintenance.loose-objects.auto=2 \
186                 maintenance run --auto --task=loose-objects 2>/dev/null &&
187         test_subcommand git prune-packed --quiet <trace-loC
188 '
189
190 test_expect_success 'incremental-repack task' '
191         packDir=.git/objects/pack &&
192         for i in $(test_seq 1 5)
193         do
194                 test_commit $i || return 1
195         done &&
196
197         # Create three disjoint pack-files with size BIG, small, small.
198         echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
199         test_tick &&
200         git pack-objects --revs $packDir/test-2 <<-\EOF &&
201         HEAD~1
202         ^HEAD~2
203         EOF
204         test_tick &&
205         git pack-objects --revs $packDir/test-3 <<-\EOF &&
206         HEAD
207         ^HEAD~1
208         EOF
209         rm -f $packDir/pack-* &&
210         rm -f $packDir/loose-* &&
211         ls $packDir/*.pack >packs-before &&
212         test_line_count = 3 packs-before &&
213
214         # the job repacks the two into a new pack, but does not
215         # delete the old ones.
216         git maintenance run --task=incremental-repack &&
217         ls $packDir/*.pack >packs-between &&
218         test_line_count = 4 packs-between &&
219
220         # the job deletes the two old packs, and does not write
221         # a new one because the batch size is not high enough to
222         # pack the largest pack-file.
223         git maintenance run --task=incremental-repack &&
224         ls .git/objects/pack/*.pack >packs-after &&
225         test_line_count = 2 packs-after
226 '
227
228 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
229         for i in $(test_seq 1 5)
230         do
231                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
232                 return 1
233         done &&
234         git add big &&
235         git commit -m "Add big file (1)" &&
236
237         # ensure any possible loose objects are in a pack-file
238         git maintenance run --task=loose-objects &&
239
240         rm big &&
241         for i in $(test_seq 6 10)
242         do
243                 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
244                 return 1
245         done &&
246         git add big &&
247         git commit -m "Add big file (2)" &&
248
249         # ensure any possible loose objects are in a pack-file
250         git maintenance run --task=loose-objects &&
251
252         # Now run the incremental-repack task and check the batch-size
253         GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
254                 --task=incremental-repack 2>/dev/null &&
255         test_subcommand git multi-pack-index repack \
256                  --no-progress --batch-size=2147483647 <run-2g.txt
257 '
258
259 test_expect_success 'maintenance.incremental-repack.auto' '
260         git repack -adk &&
261         git config core.multiPackIndex true &&
262         git multi-pack-index write &&
263         GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
264                 -c maintenance.incremental-repack.auto=1 \
265                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
266         test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
267         test_commit A &&
268         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
269         HEAD
270         ^HEAD~1
271         EOF
272         GIT_TRACE2_EVENT=$(pwd)/trace-A git \
273                 -c maintenance.incremental-repack.auto=2 \
274                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
275         test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
276         test_commit B &&
277         git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
278         HEAD
279         ^HEAD~1
280         EOF
281         GIT_TRACE2_EVENT=$(pwd)/trace-B git \
282                 -c maintenance.incremental-repack.auto=2 \
283                 maintenance run --auto --task=incremental-repack 2>/dev/null &&
284         test_subcommand git multi-pack-index write --no-progress <trace-B
285 '
286
287 test_done