add-patch: do not show UI messages on stderr
[git/gitster.git] / t / t6500-gc.sh
blob43d40175f8dac940cfa669605899d78b235041c1
1 #!/bin/sh
3 test_description='basic git gc tests
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
9 test_expect_success 'setup' '
10 # do not let the amount of physical memory affects gc
11 # behavior, make sure we always pack everything to one pack by
12 # default
13 git config gc.bigPackThreshold 2g &&
14 test_oid_init
17 test_expect_success 'gc empty repository' '
18 git gc
21 test_expect_success 'gc does not leave behind pid file' '
22 git gc &&
23 test_path_is_missing .git/gc.pid
26 test_expect_success 'gc --gobbledegook' '
27 test_expect_code 129 git gc --nonsense 2>err &&
28 test_grep "[Uu]sage: git gc" err
31 test_expect_success 'gc -h with invalid configuration' '
32 mkdir broken &&
34 cd broken &&
35 git init &&
36 echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
37 test_expect_code 129 git gc -h >usage 2>&1
38 ) &&
39 test_grep "[Uu]sage" broken/usage
42 test_expect_success 'gc is not aborted due to a stale symref' '
43 git init remote &&
45 cd remote &&
46 test_commit initial &&
47 git clone . ../client &&
48 git branch -m develop &&
49 cd ../client &&
50 git fetch --prune &&
51 git gc
55 test_expect_success 'gc --keep-largest-pack' '
56 test_create_repo keep-pack &&
58 cd keep-pack &&
59 test_commit one &&
60 test_commit two &&
61 test_commit three &&
62 git gc &&
63 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
64 test_line_count = 1 pack-list &&
65 cp pack-list base-pack-list &&
66 test_commit four &&
67 git repack -d &&
68 test_commit five &&
69 git repack -d &&
70 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
71 test_line_count = 3 pack-list &&
72 git gc --keep-largest-pack &&
73 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
74 test_line_count = 2 pack-list &&
75 awk "/^P /{print \$2}" <.git/objects/info/packs >pack-info &&
76 test_line_count = 2 pack-info &&
77 test_path_is_file .git/objects/pack/$(cat base-pack-list) &&
78 git fsck
82 test_expect_success 'pre-auto-gc hook can stop auto gc' '
83 cat >err.expect <<-\EOF &&
84 no gc for you
85 EOF
87 git init pre-auto-gc-hook &&
88 test_hook -C pre-auto-gc-hook pre-auto-gc <<-\EOF &&
89 echo >&2 no gc for you &&
90 exit 1
91 EOF
93 cd pre-auto-gc-hook &&
95 git config gc.auto 3 &&
96 git config gc.autoDetach false &&
98 # We need to create two object whose sha1s start with 17
99 # since this is what git gc counts. As it happens, these
100 # two blobs will do so.
101 test_commit "$(test_oid blob17_1)" &&
102 test_commit "$(test_oid blob17_2)" &&
104 git gc --auto >../out.actual 2>../err.actual
105 ) &&
106 test_must_be_empty out.actual &&
107 test_cmp err.expect err.actual &&
109 cat >err.expect <<-\EOF &&
110 will gc for you
111 Auto packing the repository for optimum performance.
112 See "git help gc" for manual housekeeping.
115 test_hook -C pre-auto-gc-hook --clobber pre-auto-gc <<-\EOF &&
116 echo >&2 will gc for you &&
117 exit 0
120 git -C pre-auto-gc-hook gc --auto >out.actual 2>err.actual &&
122 test_must_be_empty out.actual &&
123 test_cmp err.expect err.actual
126 test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' '
127 test_config gc.auto 3 &&
128 test_config gc.autodetach false &&
129 test_config pack.writebitmaps true &&
130 # We need to create two object whose sha1s start with 17
131 # since this is what git gc counts. As it happens, these
132 # two blobs will do so.
133 test_commit "$(test_oid blob17_1)" &&
134 test_commit "$(test_oid blob17_2)" &&
135 # Our first gc will create a pack; our second will create a second pack
136 git gc --auto &&
137 ls .git/objects/pack/pack-*.pack | sort >existing_packs &&
138 test_commit "$(test_oid blob17_3)" &&
139 test_commit "$(test_oid blob17_4)" &&
141 git gc --auto 2>err &&
142 test_grep ! "^warning:" err &&
143 ls .git/objects/pack/pack-*.pack | sort >post_packs &&
144 comm -1 -3 existing_packs post_packs >new &&
145 comm -2 -3 existing_packs post_packs >del &&
146 test_line_count = 0 del && # No packs are deleted
147 test_line_count = 1 new # There is one new pack
150 test_expect_success 'gc --no-quiet' '
151 GIT_PROGRESS_DELAY=0 git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
152 test_must_be_empty stdout &&
153 test_grep "Computing commit graph generation numbers" stderr
156 test_expect_success TTY 'with TTY: gc --no-quiet' '
157 test_terminal env GIT_PROGRESS_DELAY=0 \
158 git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
159 test_must_be_empty stdout &&
160 test_grep "Enumerating objects" stderr &&
161 test_grep "Computing commit graph generation numbers" stderr
164 test_expect_success 'gc --quiet' '
165 git -c gc.writeCommitGraph=true gc --quiet >stdout 2>stderr &&
166 test_must_be_empty stdout &&
167 test_must_be_empty stderr
170 test_expect_success 'gc.reflogExpire{Unreachable,}=never skips "expire" via "gc"' '
171 test_config gc.reflogExpire never &&
172 test_config gc.reflogExpireUnreachable never &&
174 GIT_TRACE=$(pwd)/trace.out git gc &&
176 # Check that git-pack-refs is run as a sanity check (done via
177 # gc_before_repack()) but that git-expire is not.
178 grep -E "^trace: (built-in|exec|run_command): git pack-refs --" trace.out &&
179 ! grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
182 test_expect_success 'one of gc.reflogExpire{Unreachable,}=never does not skip "expire" via "gc"' '
183 >trace.out &&
184 test_config gc.reflogExpire never &&
185 GIT_TRACE=$(pwd)/trace.out git gc &&
186 grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
189 test_expect_success 'gc.repackFilter launches repack with a filter' '
190 git clone --no-local --bare . bare.git &&
192 git -C bare.git -c gc.cruftPacks=false gc &&
193 test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
195 GIT_TRACE=$(pwd)/trace.out git -C bare.git -c gc.repackFilter=blob:none \
196 -c repack.writeBitmaps=false -c gc.cruftPacks=false gc &&
197 test_stdout_line_count = 2 ls bare.git/objects/pack/*.pack &&
198 grep -E "^trace: (built-in|exec|run_command): git repack .* --filter=blob:none ?.*" trace.out
201 test_expect_success 'gc.repackFilterTo store filtered out objects' '
202 test_when_finished "rm -rf bare.git filtered.git" &&
204 git init --bare filtered.git &&
205 git -C bare.git -c gc.repackFilter=blob:none \
206 -c gc.repackFilterTo=../filtered.git/objects/pack/pack \
207 -c repack.writeBitmaps=false -c gc.cruftPacks=false gc &&
209 test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
210 test_stdout_line_count = 1 ls filtered.git/objects/pack/*.pack
213 prepare_cruft_history () {
214 test_commit base &&
216 test_commit --no-tag foo &&
217 test_commit --no-tag bar &&
218 git reset HEAD^^
221 assert_no_cruft_packs () {
222 find .git/objects/pack -name "*.mtimes" >mtimes &&
223 test_must_be_empty mtimes
226 for argv in \
227 "gc" \
228 "-c gc.cruftPacks=true gc" \
229 "-c gc.cruftPacks=false gc --cruft"
231 test_expect_success "git $argv generates a cruft pack" '
232 test_when_finished "rm -fr repo" &&
233 git init repo &&
235 cd repo &&
237 prepare_cruft_history &&
238 git $argv &&
240 find .git/objects/pack -name "*.mtimes" >mtimes &&
241 sed -e 's/\.mtimes$/\.pack/g' mtimes >packs &&
243 test_file_not_empty packs &&
244 while read pack
246 test_path_is_file "$pack" || return 1
247 done <packs
250 done
252 for argv in \
253 "gc --no-cruft" \
254 "-c gc.cruftPacks=false gc" \
255 "-c gc.cruftPacks=true gc --no-cruft"
257 test_expect_success "git $argv does not generate a cruft pack" '
258 test_when_finished "rm -fr repo" &&
259 git init repo &&
261 cd repo &&
263 prepare_cruft_history &&
264 git $argv &&
266 assert_no_cruft_packs
269 done
271 test_expect_success '--keep-largest-pack ignores cruft packs' '
272 test_when_finished "rm -fr repo" &&
273 git init repo &&
275 cd repo &&
277 # Generate a pack for reachable objects (of which there
278 # are 3), and one for unreachable objects (of which
279 # there are 6).
280 prepare_cruft_history &&
281 git gc --cruft &&
283 mtimes="$(find .git/objects/pack -type f -name "pack-*.mtimes")" &&
284 sz="$(test_file_size "${mtimes%.mtimes}.pack")" &&
286 # Ensure that the cruft pack gets removed (due to
287 # `--prune=now`) despite it being the largest pack.
288 git -c gc.bigPackThreshold=$sz gc --cruft --prune=now &&
290 assert_no_cruft_packs
294 test_expect_success 'gc.bigPackThreshold ignores cruft packs' '
295 test_when_finished "rm -fr repo" &&
296 git init repo &&
298 cd repo &&
300 # Generate a pack for reachable objects (of which there
301 # are 3), and one for unreachable objects (of which
302 # there are 6).
303 prepare_cruft_history &&
304 git gc --cruft &&
306 # Ensure that the cruft pack gets removed (due to
307 # `--prune=now`) despite it being the largest pack.
308 git gc --cruft --prune=now --keep-largest-pack &&
310 assert_no_cruft_packs
314 cruft_max_size_opts="git repack -d -l --cruft --cruft-expiration=2.weeks.ago"
316 test_expect_success 'setup for --max-cruft-size tests' '
317 git init cruft--max-size &&
319 cd cruft--max-size &&
320 prepare_cruft_history
324 test_expect_success '--max-cruft-size sets appropriate repack options' '
325 GIT_TRACE2_EVENT=$(pwd)/trace2.txt git -C cruft--max-size \
326 gc --cruft --max-cruft-size=1M &&
327 test_subcommand $cruft_max_size_opts --max-cruft-size=1048576 <trace2.txt
330 test_expect_success 'gc.maxCruftSize sets appropriate repack options' '
331 GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
332 git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft &&
333 test_subcommand $cruft_max_size_opts --max-cruft-size=2097152 <trace2.txt &&
335 GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
336 git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft \
337 --max-cruft-size=3M &&
338 test_subcommand $cruft_max_size_opts --max-cruft-size=3145728 <trace2.txt
341 run_and_wait_for_auto_gc () {
342 # We read stdout from gc for the side effect of waiting until the
343 # background gc process exits, closing its fd 9. Furthermore, the
344 # variable assignment from a command substitution preserves the
345 # exit status of the main gc process.
346 # Note: this fd trickery doesn't work on Windows, but there is no
347 # need to, because on Win the auto gc always runs in the foreground.
348 doesnt_matter=$(git gc --auto 9>&1)
351 test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
352 test_commit foo &&
353 test_commit bar &&
354 git repack &&
355 test_config gc.autopacklimit 1 &&
356 test_config gc.autodetach true &&
357 echo fleem >.git/gc.log &&
358 git gc --auto 2>err &&
359 test_grep "^warning:" err &&
360 test_config gc.logexpiry 5.days &&
361 test-tool chmtime =-345600 .git/gc.log &&
362 git gc --auto &&
363 test_config gc.logexpiry 2.days &&
364 run_and_wait_for_auto_gc &&
365 ls .git/objects/pack/pack-*.pack >packs &&
366 test_line_count = 1 packs
369 test_expect_success 'background auto gc respects lock for all operations' '
370 # make sure we run a background auto-gc
371 test_commit make-pack &&
372 git repack &&
373 test_config gc.autopacklimit 1 &&
374 test_config gc.autodetach true &&
376 # create a ref whose loose presence we can use to detect a pack-refs run
377 git update-ref refs/heads/should-be-loose HEAD &&
378 (ls -1 .git/refs/heads .git/reftable >expect || true) &&
380 # now fake a concurrent gc that holds the lock; we can use our
381 # shell pid so that it looks valid.
382 hostname=$(hostname || echo unknown) &&
383 shell_pid=$$ &&
384 if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
385 then
386 # In Git for Windows, Bash (actually, the MSYS2 runtime) has a
387 # different idea of PIDs than git.exe (actually Windows). Use
388 # the Windows PID in this case.
389 shell_pid=$(cat /proc/$shell_pid/winpid)
390 fi &&
391 printf "%d %s" "$shell_pid" "$hostname" >.git/gc.pid &&
393 # our gc should exit zero without doing anything
394 run_and_wait_for_auto_gc &&
395 (ls -1 .git/refs/heads .git/reftable >actual || true) &&
396 test_cmp expect actual
399 # DO NOT leave a detached auto gc process running near the end of the
400 # test script: it can run long enough in the background to racily
401 # interfere with the cleanup in 'test_done'.
403 test_done