3 test_description
='basic git gc tests
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
13 git config gc.bigPackThreshold 2g &&
15 # These are simply values which, when hashed as a blob with a newline,
16 # produce a hash where the first byte is 0x17 in their respective
33 test_expect_success
'gc empty repository' '
37 test_expect_success
'gc does not leave behind pid file' '
39 test_path_is_missing .git/gc.pid
42 test_expect_success
'gc --gobbledegook' '
43 test_expect_code 129 git gc --nonsense 2>err &&
44 test_grep "[Uu]sage: git gc" err
47 test_expect_success
'gc -h with invalid configuration' '
52 echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
53 test_expect_code 129 git gc -h >usage 2>&1
55 test_grep "[Uu]sage" broken/usage
58 test_expect_success
'gc is not aborted due to a stale symref' '
62 test_commit initial &&
63 git clone . ../client &&
64 git branch -m develop &&
71 test_expect_success
'gc --keep-largest-pack' '
72 test_create_repo keep-pack &&
79 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
80 test_line_count = 1 pack-list &&
81 cp pack-list base-pack-list &&
86 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
87 test_line_count = 3 pack-list &&
88 git gc --keep-largest-pack &&
89 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
90 test_line_count = 2 pack-list &&
91 awk "/^P /{print \$2}" <.git/objects/info/packs >pack-info &&
92 test_line_count = 2 pack-info &&
93 test_path_is_file .git/objects/pack/$(cat base-pack-list) &&
98 test_expect_success
'pre-auto-gc hook can stop auto gc' '
99 cat >err.expect <<-\EOF &&
103 git init pre-auto-gc-hook &&
104 test_hook -C pre-auto-gc-hook pre-auto-gc <<-\EOF &&
105 echo >&2 no gc for you &&
109 cd pre-auto-gc-hook &&
111 git config gc.auto 3 &&
112 git config gc.autoDetach false &&
114 # We need to create two object whose sha1s start with 17
115 # since this is what git gc counts. As it happens, these
116 # two blobs will do so.
117 test_commit "$(test_oid obj1)" &&
118 test_commit "$(test_oid obj2)" &&
120 git gc --auto >../out.actual 2>../err.actual
122 test_must_be_empty out.actual &&
123 test_cmp err.expect err.actual &&
125 cat >err.expect <<-\EOF &&
127 Auto packing the repository for optimum performance.
128 See "git help gc" for manual housekeeping.
131 test_hook -C pre-auto-gc-hook --clobber pre-auto-gc <<-\EOF &&
132 echo >&2 will gc for you &&
136 git -C pre-auto-gc-hook gc --auto >out.actual 2>err.actual &&
138 test_must_be_empty out.actual &&
139 test_cmp err.expect err.actual
142 test_expect_success
'auto gc with too many loose objects does not attempt to create bitmaps' '
143 test_config gc.auto 3 &&
144 test_config gc.autodetach false &&
145 test_config pack.writebitmaps true &&
146 # We need to create two object whose sha1s start with 17
147 # since this is what git gc counts. As it happens, these
148 # two blobs will do so.
149 test_commit "$(test_oid obj1)" &&
150 test_commit "$(test_oid obj2)" &&
151 # Our first gc will create a pack; our second will create a second pack
153 ls .git/objects/pack/pack-*.pack | sort >existing_packs &&
154 test_commit "$(test_oid obj3)" &&
155 test_commit "$(test_oid obj4)" &&
157 git gc --auto 2>err &&
158 test_grep ! "^warning:" err &&
159 ls .git/objects/pack/pack-*.pack | sort >post_packs &&
160 comm -1 -3 existing_packs post_packs >new &&
161 comm -2 -3 existing_packs post_packs >del &&
162 test_line_count = 0 del && # No packs are deleted
163 test_line_count = 1 new # There is one new pack
166 test_expect_success
'gc --no-quiet' '
167 GIT_PROGRESS_DELAY=0 git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
168 test_must_be_empty stdout &&
169 test_grep "Computing commit graph generation numbers" stderr
172 test_expect_success TTY
'with TTY: gc --no-quiet' '
173 test_terminal env GIT_PROGRESS_DELAY=0 \
174 git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
175 test_must_be_empty stdout &&
176 test_grep "Enumerating objects" stderr &&
177 test_grep "Computing commit graph generation numbers" stderr
180 test_expect_success
'gc --quiet' '
181 git -c gc.writeCommitGraph=true gc --quiet >stdout 2>stderr &&
182 test_must_be_empty stdout &&
183 test_must_be_empty stderr
186 test_expect_success
'gc.reflogExpire{Unreachable,}=never skips "expire" via "gc"' '
187 test_config gc.reflogExpire never &&
188 test_config gc.reflogExpireUnreachable never &&
190 GIT_TRACE=$(pwd)/trace.out git gc &&
192 # Check that git-pack-refs is run as a sanity check (done via
193 # gc_before_repack()) but that git-expire is not.
194 grep -E "^trace: (built-in|exec|run_command): git pack-refs --" trace.out &&
195 ! grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
198 test_expect_success
'one of gc.reflogExpire{Unreachable,}=never does not skip "expire" via "gc"' '
200 test_config gc.reflogExpire never &&
201 GIT_TRACE=$(pwd)/trace.out git gc &&
202 grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
205 test_expect_success
'gc.repackFilter launches repack with a filter' '
206 git clone --no-local --bare . bare.git &&
208 git -C bare.git -c gc.cruftPacks=false gc &&
209 test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
211 GIT_TRACE=$(pwd)/trace.out git -C bare.git -c gc.repackFilter=blob:none \
212 -c repack.writeBitmaps=false -c gc.cruftPacks=false gc &&
213 test_stdout_line_count = 2 ls bare.git/objects/pack/*.pack &&
214 grep -E "^trace: (built-in|exec|run_command): git repack .* --filter=blob:none ?.*" trace.out
217 test_expect_success
'gc.repackFilterTo store filtered out objects' '
218 test_when_finished "rm -rf bare.git filtered.git" &&
220 git init --bare filtered.git &&
221 git -C bare.git -c gc.repackFilter=blob:none \
222 -c gc.repackFilterTo=../filtered.git/objects/pack/pack \
223 -c repack.writeBitmaps=false -c gc.cruftPacks=false gc &&
225 test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
226 test_stdout_line_count = 1 ls filtered.git/objects/pack/*.pack
229 prepare_cruft_history
() {
232 test_commit
--no-tag foo
&&
233 test_commit
--no-tag bar
&&
237 assert_no_cruft_packs
() {
238 find .git
/objects
/pack
-name "*.mtimes" >mtimes
&&
239 test_must_be_empty mtimes
244 "-c gc.cruftPacks=true gc" \
245 "-c gc.cruftPacks=false gc --cruft"
247 test_expect_success
"git $argv generates a cruft pack" '
248 test_when_finished "rm -fr repo" &&
253 prepare_cruft_history &&
256 find .git/objects/pack -name "*.mtimes" >mtimes &&
257 sed -e 's
/\.mtimes$
/\.pack
/g
' mtimes >packs &&
259 test_file_not_empty packs &&
262 test_path_is_file "$pack" || return 1
270 "-c gc.cruftPacks=false gc" \
271 "-c gc.cruftPacks=true gc --no-cruft"
273 test_expect_success
"git $argv does not generate a cruft pack" '
274 test_when_finished "rm -fr repo" &&
279 prepare_cruft_history &&
282 assert_no_cruft_packs
287 test_expect_success
'--keep-largest-pack ignores cruft packs' '
288 test_when_finished "rm -fr repo" &&
293 # Generate a pack for reachable objects (of which there
294 # are 3), and one for unreachable objects (of which
296 prepare_cruft_history &&
299 mtimes="$(find .git/objects/pack -type f -name "pack-*.mtimes")" &&
300 sz="$(test_file_size "${mtimes%.mtimes}.pack")" &&
302 # Ensure that the cruft pack gets removed (due to
303 # `--prune=now`) despite it being the largest pack.
304 git -c gc.bigPackThreshold=$sz gc --cruft --prune=now &&
306 assert_no_cruft_packs
310 test_expect_success
'gc.bigPackThreshold ignores cruft packs' '
311 test_when_finished "rm -fr repo" &&
316 # Generate a pack for reachable objects (of which there
317 # are 3), and one for unreachable objects (of which
319 prepare_cruft_history &&
322 # Ensure that the cruft pack gets removed (due to
323 # `--prune=now`) despite it being the largest pack.
324 git gc --cruft --prune=now --keep-largest-pack &&
326 assert_no_cruft_packs
330 cruft_max_size_opts
="git repack -d -l --cruft --cruft-expiration=2.weeks.ago"
332 test_expect_success
'setup for --max-cruft-size tests' '
333 git init cruft--max-size &&
335 cd cruft--max-size &&
336 prepare_cruft_history
340 test_expect_success
'--max-cruft-size sets appropriate repack options' '
341 GIT_TRACE2_EVENT=$(pwd)/trace2.txt git -C cruft--max-size \
342 gc --cruft --max-cruft-size=1M &&
343 test_subcommand $cruft_max_size_opts --max-cruft-size=1048576 <trace2.txt
346 test_expect_success
'gc.maxCruftSize sets appropriate repack options' '
347 GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
348 git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft &&
349 test_subcommand $cruft_max_size_opts --max-cruft-size=2097152 <trace2.txt &&
351 GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
352 git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft \
353 --max-cruft-size=3M &&
354 test_subcommand $cruft_max_size_opts --max-cruft-size=3145728 <trace2.txt
357 run_and_wait_for_auto_gc
() {
358 # We read stdout from gc for the side effect of waiting until the
359 # background gc process exits, closing its fd 9. Furthermore, the
360 # variable assignment from a command substitution preserves the
361 # exit status of the main gc process.
362 # Note: this fd trickery doesn't work on Windows, but there is no
363 # need to, because on Win the auto gc always runs in the foreground.
364 doesnt_matter
=$
(git gc
--auto 9>&1)
367 test_expect_success
'background auto gc does not run if gc.log is present and recent but does if it is old' '
371 test_config gc.autopacklimit 1 &&
372 test_config gc.autodetach true &&
373 echo fleem >.git/gc.log &&
374 git gc --auto 2>err &&
375 test_grep "^warning:" err &&
376 test_config gc.logexpiry 5.days &&
377 test-tool chmtime =-345600 .git/gc.log &&
379 test_config gc.logexpiry 2.days &&
380 run_and_wait_for_auto_gc &&
381 ls .git/objects/pack/pack-*.pack >packs &&
382 test_line_count = 1 packs
385 test_expect_success
'background auto gc respects lock for all operations' '
386 # make sure we run a background auto-gc
387 test_commit make-pack &&
389 test_config gc.autopacklimit 1 &&
390 test_config gc.autodetach true &&
392 # create a ref whose loose presence we can use to detect a pack-refs run
393 git update-ref refs/heads/should-be-loose HEAD &&
394 (ls -1 .git/refs/heads .git/reftable >expect || true) &&
396 # now fake a concurrent gc that holds the lock; we can use our
397 # shell pid so that it looks valid.
398 hostname=$(hostname || echo unknown) &&
400 if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
402 # In Git for Windows, Bash (actually, the MSYS2 runtime) has a
403 # different idea of PIDs than git.exe (actually Windows). Use
404 # the Windows PID in this case.
405 shell_pid=$(cat /proc/$shell_pid/winpid)
407 printf "%d %s" "$shell_pid" "$hostname" >.git/gc.pid &&
409 # our gc should exit zero without doing anything
410 run_and_wait_for_auto_gc &&
411 (ls -1 .git/refs/heads .git/reftable >actual || true) &&
412 test_cmp expect actual
415 # DO NOT leave a detached auto gc process running near the end of the
416 # test script: it can run long enough in the background to racily
417 # interfere with the cleanup in 'test_done'.