t1010: fix unnoticed failure on Windows
[git.git] / t / t6500-gc.sh
blobc2021267f2c558151567f274251844dd4abafb5c
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 &&
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
17 # algorithms.
18 test_oid_cache <<-EOF
19 obj1 sha1:263
20 obj1 sha256:34
22 obj2 sha1:410
23 obj2 sha256:174
25 obj3 sha1:523
26 obj3 sha256:313
28 obj4 sha1:790
29 obj4 sha256:481
30 EOF
33 test_expect_success 'gc empty repository' '
34 git gc
37 test_expect_success 'gc does not leave behind pid file' '
38 git gc &&
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_i18ngrep "[Uu]sage: git gc" err
47 test_expect_success 'gc -h with invalid configuration' '
48 mkdir broken &&
50 cd broken &&
51 git init &&
52 echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
53 test_expect_code 129 git gc -h >usage 2>&1
54 ) &&
55 test_i18ngrep "[Uu]sage" broken/usage
58 test_expect_success 'gc is not aborted due to a stale symref' '
59 git init remote &&
61 cd remote &&
62 test_commit initial &&
63 git clone . ../client &&
64 git branch -m develop &&
65 cd ../client &&
66 git fetch --prune &&
67 git gc
71 test_expect_success 'gc --keep-largest-pack' '
72 test_create_repo keep-pack &&
74 cd keep-pack &&
75 test_commit one &&
76 test_commit two &&
77 test_commit three &&
78 git gc &&
79 ( cd .git/objects/pack && ls *.pack ) >pack-list &&
80 test_line_count = 1 pack-list &&
81 cp pack-list base-pack-list &&
82 test_commit four &&
83 git repack -d &&
84 test_commit five &&
85 git repack -d &&
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) &&
94 git fsck
98 test_expect_success 'pre-auto-gc hook can stop auto gc' '
99 cat >err.expect <<-\EOF &&
100 no gc for you
103 git init pre-auto-gc-hook &&
105 cd pre-auto-gc-hook &&
106 write_script ".git/hooks/pre-auto-gc" <<-\EOF &&
107 echo >&2 no gc for you &&
108 exit 1
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
121 ) &&
122 test_must_be_empty out.actual &&
123 test_cmp err.expect err.actual &&
125 cat >err.expect <<-\EOF &&
126 will gc for you
127 Auto packing the repository for optimum performance.
128 See "git help gc" for manual housekeeping.
132 cd pre-auto-gc-hook &&
133 write_script ".git/hooks/pre-auto-gc" <<-\EOF &&
134 echo >&2 will gc for you &&
135 exit 0
137 git gc --auto >../out.actual 2>../err.actual
138 ) &&
140 test_must_be_empty out.actual &&
141 test_cmp err.expect err.actual
144 test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' '
145 test_config gc.auto 3 &&
146 test_config gc.autodetach false &&
147 test_config pack.writebitmaps true &&
148 # We need to create two object whose sha1s start with 17
149 # since this is what git gc counts. As it happens, these
150 # two blobs will do so.
151 test_commit "$(test_oid obj1)" &&
152 test_commit "$(test_oid obj2)" &&
153 # Our first gc will create a pack; our second will create a second pack
154 git gc --auto &&
155 ls .git/objects/pack/pack-*.pack | sort >existing_packs &&
156 test_commit "$(test_oid obj3)" &&
157 test_commit "$(test_oid obj4)" &&
159 git gc --auto 2>err &&
160 test_i18ngrep ! "^warning:" err &&
161 ls .git/objects/pack/pack-*.pack | sort >post_packs &&
162 comm -1 -3 existing_packs post_packs >new &&
163 comm -2 -3 existing_packs post_packs >del &&
164 test_line_count = 0 del && # No packs are deleted
165 test_line_count = 1 new # There is one new pack
168 test_expect_success 'gc --no-quiet' '
169 GIT_PROGRESS_DELAY=0 git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
170 test_must_be_empty stdout &&
171 test_i18ngrep "Computing commit graph generation numbers" stderr
174 test_expect_success TTY 'with TTY: gc --no-quiet' '
175 test_terminal env GIT_PROGRESS_DELAY=0 \
176 git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
177 test_must_be_empty stdout &&
178 test_i18ngrep "Enumerating objects" stderr &&
179 test_i18ngrep "Computing commit graph generation numbers" stderr
182 test_expect_success 'gc --quiet' '
183 git -c gc.writeCommitGraph=true gc --quiet >stdout 2>stderr &&
184 test_must_be_empty stdout &&
185 test_must_be_empty stderr
188 test_expect_success 'gc.reflogExpire{Unreachable,}=never skips "expire" via "gc"' '
189 test_config gc.reflogExpire never &&
190 test_config gc.reflogExpireUnreachable never &&
192 GIT_TRACE=$(pwd)/trace.out git gc &&
194 # Check that git-pack-refs is run as a sanity check (done via
195 # gc_before_repack()) but that git-expire is not.
196 grep -E "^trace: (built-in|exec|run_command): git pack-refs --" trace.out &&
197 ! grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
200 test_expect_success 'one of gc.reflogExpire{Unreachable,}=never does not skip "expire" via "gc"' '
201 >trace.out &&
202 test_config gc.reflogExpire never &&
203 GIT_TRACE=$(pwd)/trace.out git gc &&
204 grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
207 run_and_wait_for_auto_gc () {
208 # We read stdout from gc for the side effect of waiting until the
209 # background gc process exits, closing its fd 9. Furthermore, the
210 # variable assignment from a command substitution preserves the
211 # exit status of the main gc process.
212 # Note: this fd trickery doesn't work on Windows, but there is no
213 # need to, because on Win the auto gc always runs in the foreground.
214 doesnt_matter=$(git gc --auto 9>&1)
217 test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
218 test_commit foo &&
219 test_commit bar &&
220 git repack &&
221 test_config gc.autopacklimit 1 &&
222 test_config gc.autodetach true &&
223 echo fleem >.git/gc.log &&
224 git gc --auto 2>err &&
225 test_i18ngrep "^warning:" err &&
226 test_config gc.logexpiry 5.days &&
227 test-tool chmtime =-345600 .git/gc.log &&
228 git gc --auto &&
229 test_config gc.logexpiry 2.days &&
230 run_and_wait_for_auto_gc &&
231 ls .git/objects/pack/pack-*.pack >packs &&
232 test_line_count = 1 packs
235 test_expect_success 'background auto gc respects lock for all operations' '
236 # make sure we run a background auto-gc
237 test_commit make-pack &&
238 git repack &&
239 test_config gc.autopacklimit 1 &&
240 test_config gc.autodetach true &&
242 # create a ref whose loose presence we can use to detect a pack-refs run
243 git update-ref refs/heads/should-be-loose HEAD &&
244 (ls -1 .git/refs/heads .git/reftable >expect || true) &&
246 # now fake a concurrent gc that holds the lock; we can use our
247 # shell pid so that it looks valid.
248 hostname=$(hostname || echo unknown) &&
249 shell_pid=$$ &&
250 if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
251 then
252 # In Git for Windows, Bash (actually, the MSYS2 runtime) has a
253 # different idea of PIDs than git.exe (actually Windows). Use
254 # the Windows PID in this case.
255 shell_pid=$(cat /proc/$shell_pid/winpid)
256 fi &&
257 printf "%d %s" "$shell_pid" "$hostname" >.git/gc.pid &&
259 # our gc should exit zero without doing anything
260 run_and_wait_for_auto_gc &&
261 (ls -1 .git/refs/heads .git/reftable >actual || true) &&
262 test_cmp expect actual
265 # DO NOT leave a detached auto gc process running near the end of the
266 # test script: it can run long enough in the background to racily
267 # interfere with the cleanup in 'test_done'.
269 test_done