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 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
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.<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
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
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 \
64 test_expect_success 'commit-graph auto condition' '
65 COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
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 &&
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 &&
79 git commit --allow-empty -m "second" &&
80 git commit --allow-empty -m "third" &&
82 GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
83 git -c maintenance.commit-graph.auto=2 $COMMAND &&
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
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
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
103 test_expect_success 'run --task=prefetch with no remotes' '
104 git maintenance run --task=prefetch 2>err &&
105 test_must_be_empty err
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 &&
125 test_cmp_rev refs/remotes/remote1/one refs/prefetch/remote1/one &&
126 test_cmp_rev refs/remotes/remote2/two refs/prefetch/remote2/two
129 test_expect_success 'loose-objects task' '
130 # Repack everything so we know the state of the object dir
133 # Hack to stop maintenance from running during "git commit"
134 echo in use >.git/objects/maintenance.lock &&
136 # Assuming that "git commit" creates at least one loose object
137 test_commit create-loose-object &&
138 rm .git/objects/maintenance.lock &&
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 &&
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 &&
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 &&
163 test_cmp expect obj-dir-after &&
164 ls .git/objects/pack/*.pack >packs-after &&
165 test_cmp packs-between packs-after
168 test_expect_success 'maintenance.loose-objects.auto' '
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
190 test_expect_success 'incremental-repack task' '
191 packDir=.git/objects/pack &&
192 for i in $(test_seq 1 5)
194 test_commit $i || return 1
197 # Create three disjoint pack-files with size BIG, small, small.
198 echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
200 git pack-objects --revs $packDir/test-2 <<-\EOF &&
205 git pack-objects --revs $packDir/test-3 <<-\EOF &&
209 rm -f $packDir/pack-* &&
210 rm -f $packDir/loose-* &&
211 ls $packDir/*.pack >packs-before &&
212 test_line_count = 3 packs-before &&
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 &&
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
228 test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
229 for i in $(test_seq 1 5)
231 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
235 git commit -m "Add big file (1)" &&
237 # ensure any possible loose objects are in a pack-file
238 git maintenance run --task=loose-objects &&
241 for i in $(test_seq 6 10)
243 test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
247 git commit -m "Add big file (2)" &&
249 # ensure any possible loose objects are in a pack-file
250 git maintenance run --task=loose-objects &&
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
259 test_expect_success 'maintenance.incremental-repack.auto' '
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 &&
268 git pack-objects --revs .git/objects/pack/pack <<-\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 &&
277 git pack-objects --revs .git/objects/pack/pack <<-\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