1#!/bin/sh
23
test_description='basic git gc tests
4'
56
. ./test-lib.sh
7. "$TEST_DIRECTORY"/lib-terminal.sh
89
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
13git config gc.bigPackThreshold 2g
14'
1516
test_expect_success 'gc empty repository' '
17git gc
18'
1920
test_expect_success 'gc does not leave behind pid file' '
21git gc &&
22test_path_is_missing .git/gc.pid
23'
2425
test_expect_success 'gc --gobbledegook' '
26test_expect_code 129 git gc --nonsense 2>err &&
27test_i18ngrep "[Uu]sage: git gc" err
28'
2930
test_expect_success 'gc -h with invalid configuration' '
31mkdir broken &&
32(
33cd broken &&
34git init &&
35echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
36test_expect_code 129 git gc -h >usage 2>&1
37) &&
38test_i18ngrep "[Uu]sage" broken/usage
39'
4041
test_expect_success 'gc is not aborted due to a stale symref' '
42git init remote &&
43(
44cd remote &&
45test_commit initial &&
46git clone . ../client &&
47git branch -m develop &&
48cd ../client &&
49git fetch --prune &&
50git gc
51)
52'
5354
test_expect_success 'gc --keep-largest-pack' '
55test_create_repo keep-pack &&
56(
57cd keep-pack &&
58test_commit one &&
59test_commit two &&
60test_commit three &&
61git gc &&
62( cd .git/objects/pack && ls *.pack ) >pack-list &&
63test_line_count = 1 pack-list &&
64BASE_PACK=.git/objects/pack/pack-*.pack &&
65test_commit four &&
66git repack -d &&
67test_commit five &&
68git repack -d &&
69( cd .git/objects/pack && ls *.pack ) >pack-list &&
70test_line_count = 3 pack-list &&
71git gc --keep-largest-pack &&
72( cd .git/objects/pack && ls *.pack ) >pack-list &&
73test_line_count = 2 pack-list &&
74test_path_is_file $BASE_PACK &&
75git fsck
76)
77'
7879
test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' '
80test_config gc.auto 3 &&
81test_config gc.autodetach false &&
82test_config pack.writebitmaps true &&
83# We need to create two object whose sha1s start with 17
84# since this is what git gc counts. As it happens, these
85# two blobs will do so.
86test_commit 263 &&
87test_commit 410 &&
88# Our first gc will create a pack; our second will create a second pack
89git gc --auto &&
90ls .git/objects/pack | sort >existing_packs &&
91test_commit 523 &&
92test_commit 790 &&
9394
git gc --auto 2>err &&
95test_i18ngrep ! "^warning:" err &&
96ls .git/objects/pack/ | sort >post_packs &&
97comm -1 -3 existing_packs post_packs >new &&
98comm -2 -3 existing_packs post_packs >del &&
99test_line_count = 0 del && # No packs are deleted
100test_line_count = 2 new # There is one new pack and its .idx
101'
102103
test_expect_success 'gc --no-quiet' '
104git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
105test_must_be_empty stdout &&
106test_line_count = 1 stderr &&
107test_i18ngrep "Computing commit graph generation numbers" stderr
108'
109110
test_expect_success TTY 'with TTY: gc --no-quiet' '
111test_terminal git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
112test_must_be_empty stdout &&
113test_i18ngrep "Enumerating objects" stderr &&
114test_i18ngrep "Computing commit graph generation numbers" stderr
115'
116117
test_expect_success 'gc --quiet' '
118git -c gc.writeCommitGraph=true gc --quiet >stdout 2>stderr &&
119test_must_be_empty stdout &&
120test_must_be_empty stderr
121'
122123
test_expect_success 'gc.reflogExpire{Unreachable,}=never skips "expire" via "gc"' '
124test_config gc.reflogExpire never &&
125test_config gc.reflogExpireUnreachable never &&
126127
GIT_TRACE=$(pwd)/trace.out git gc &&
128129
# Check that git-pack-refs is run as a sanity check (done via
130# gc_before_repack()) but that git-expire is not.
131grep -E "^trace: (built-in|exec|run_command): git pack-refs --" trace.out &&
132! grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
133'
134135
test_expect_success 'one of gc.reflogExpire{Unreachable,}=never does not skip "expire" via "gc"' '
136>trace.out &&
137test_config gc.reflogExpire never &&
138GIT_TRACE=$(pwd)/trace.out git gc &&
139grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
140'
141142
run_and_wait_for_auto_gc () {
143# We read stdout from gc for the side effect of waiting until the
144# background gc process exits, closing its fd 9. Furthermore, the
145# variable assignment from a command substitution preserves the
146# exit status of the main gc process.
147# Note: this fd trickery doesn't work on Windows, but there is no
148# need to, because on Win the auto gc always runs in the foreground.
149doesnt_matter=$(git gc --auto 9>&1)
150}
151152
test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
153test_commit foo &&
154test_commit bar &&
155git repack &&
156test_config gc.autopacklimit 1 &&
157test_config gc.autodetach true &&
158echo fleem >.git/gc.log &&
159git gc --auto 2>err &&
160test_i18ngrep "^warning:" err &&
161test_config gc.logexpiry 5.days &&
162test-tool chmtime =-345600 .git/gc.log &&
163git gc --auto &&
164test_config gc.logexpiry 2.days &&
165run_and_wait_for_auto_gc &&
166ls .git/objects/pack/pack-*.pack >packs &&
167test_line_count = 1 packs
168'
169170
test_expect_success 'background auto gc respects lock for all operations' '
171# make sure we run a background auto-gc
172test_commit make-pack &&
173git repack &&
174test_config gc.autopacklimit 1 &&
175test_config gc.autodetach true &&
176177
# create a ref whose loose presence we can use to detect a pack-refs run
178git update-ref refs/heads/should-be-loose HEAD &&
179test_path_is_file .git/refs/heads/should-be-loose &&
180181
# now fake a concurrent gc that holds the lock; we can use our
182# shell pid so that it looks valid.
183hostname=$(hostname || echo unknown) &&
184shell_pid=$$ &&
185if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
186then
187# In Git for Windows, Bash (actually, the MSYS2 runtime) has a
188# different idea of PIDs than git.exe (actually Windows). Use
189# the Windows PID in this case.
190shell_pid=$(cat /proc/$shell_pid/winpid)
191fi &&
192printf "%d %s" "$shell_pid" "$hostname" >.git/gc.pid &&
193194
# our gc should exit zero without doing anything
195run_and_wait_for_auto_gc &&
196test_path_is_file .git/refs/heads/should-be-loose
197'
198199
# DO NOT leave a detached auto gc process running near the end of the
200# test script: it can run long enough in the background to racily
201# interfere with the cleanup in 'test_done'.
202203
test_done