]> git.ipfire.org Git - thirdparty/git.git/blob - t/t6500-gc.sh
Merge branch 'jc/commit-new-underscore-index-fix' into maint-2.42
[thirdparty/git.git] / t / t6500-gc.sh
1 #!/bin/sh
2
3 test_description='basic git gc tests
4 '
5
6 . ./test-lib.sh
7 . "$TEST_DIRECTORY"/lib-terminal.sh
8
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
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
21
22 obj2 sha1:410
23 obj2 sha256:174
24
25 obj3 sha1:523
26 obj3 sha256:313
27
28 obj4 sha1:790
29 obj4 sha256:481
30 EOF
31 '
32
33 test_expect_success 'gc empty repository' '
34 git gc
35 '
36
37 test_expect_success 'gc does not leave behind pid file' '
38 git gc &&
39 test_path_is_missing .git/gc.pid
40 '
41
42 test_expect_success 'gc --gobbledegook' '
43 test_expect_code 129 git gc --nonsense 2>err &&
44 test_i18ngrep "[Uu]sage: git gc" err
45 '
46
47 test_expect_success 'gc -h with invalid configuration' '
48 mkdir broken &&
49 (
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
56 '
57
58 test_expect_success 'gc is not aborted due to a stale symref' '
59 git init remote &&
60 (
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
68 )
69 '
70
71 test_expect_success 'gc --keep-largest-pack' '
72 test_create_repo keep-pack &&
73 (
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
95 )
96 '
97
98 test_expect_success 'pre-auto-gc hook can stop auto gc' '
99 cat >err.expect <<-\EOF &&
100 no gc for you
101 EOF
102
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 &&
106 exit 1
107 EOF
108 (
109 cd pre-auto-gc-hook &&
110
111 git config gc.auto 3 &&
112 git config gc.autoDetach false &&
113
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)" &&
119
120 git gc --auto >../out.actual 2>../err.actual
121 ) &&
122 test_must_be_empty out.actual &&
123 test_cmp err.expect err.actual &&
124
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.
129 EOF
130
131 test_hook -C pre-auto-gc-hook --clobber pre-auto-gc <<-\EOF &&
132 echo >&2 will gc for you &&
133 exit 0
134 EOF
135
136 git -C pre-auto-gc-hook gc --auto >out.actual 2>err.actual &&
137
138 test_must_be_empty out.actual &&
139 test_cmp err.expect err.actual
140 '
141
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
152 git gc --auto &&
153 ls .git/objects/pack/pack-*.pack | sort >existing_packs &&
154 test_commit "$(test_oid obj3)" &&
155 test_commit "$(test_oid obj4)" &&
156
157 git gc --auto 2>err &&
158 test_i18ngrep ! "^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
164 '
165
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_i18ngrep "Computing commit graph generation numbers" stderr
170 '
171
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_i18ngrep "Enumerating objects" stderr &&
177 test_i18ngrep "Computing commit graph generation numbers" stderr
178 '
179
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
184 '
185
186 test_expect_success 'gc.reflogExpire{Unreachable,}=never skips "expire" via "gc"' '
187 test_config gc.reflogExpire never &&
188 test_config gc.reflogExpireUnreachable never &&
189
190 GIT_TRACE=$(pwd)/trace.out git gc &&
191
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
196 '
197
198 test_expect_success 'one of gc.reflogExpire{Unreachable,}=never does not skip "expire" via "gc"' '
199 >trace.out &&
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
203 '
204
205 prepare_cruft_history () {
206 test_commit base &&
207
208 test_commit --no-tag foo &&
209 test_commit --no-tag bar &&
210 git reset HEAD^^
211 }
212
213 assert_no_cruft_packs () {
214 find .git/objects/pack -name "*.mtimes" >mtimes &&
215 test_must_be_empty mtimes
216 }
217
218 for argv in \
219 "gc" \
220 "-c gc.cruftPacks=true gc" \
221 "-c gc.cruftPacks=false gc --cruft"
222 do
223 test_expect_success "git $argv generates a cruft pack" '
224 test_when_finished "rm -fr repo" &&
225 git init repo &&
226 (
227 cd repo &&
228
229 prepare_cruft_history &&
230 git $argv &&
231
232 find .git/objects/pack -name "*.mtimes" >mtimes &&
233 sed -e 's/\.mtimes$/\.pack/g' mtimes >packs &&
234
235 test_file_not_empty packs &&
236 while read pack
237 do
238 test_path_is_file "$pack" || return 1
239 done <packs
240 )
241 '
242 done
243
244 for argv in \
245 "gc --no-cruft" \
246 "-c gc.cruftPacks=false gc" \
247 "-c gc.cruftPacks=true gc --no-cruft"
248 do
249 test_expect_success "git $argv does not generate a cruft pack" '
250 test_when_finished "rm -fr repo" &&
251 git init repo &&
252 (
253 cd repo &&
254
255 prepare_cruft_history &&
256 git $argv &&
257
258 assert_no_cruft_packs
259 )
260 '
261 done
262
263 test_expect_success '--keep-largest-pack ignores cruft packs' '
264 test_when_finished "rm -fr repo" &&
265 git init repo &&
266 (
267 cd repo &&
268
269 # Generate a pack for reachable objects (of which there
270 # are 3), and one for unreachable objects (of which
271 # there are 6).
272 prepare_cruft_history &&
273 git gc --cruft &&
274
275 mtimes="$(find .git/objects/pack -type f -name "pack-*.mtimes")" &&
276 sz="$(test_file_size "${mtimes%.mtimes}.pack")" &&
277
278 # Ensure that the cruft pack gets removed (due to
279 # `--prune=now`) despite it being the largest pack.
280 git -c gc.bigPackThreshold=$sz gc --cruft --prune=now &&
281
282 assert_no_cruft_packs
283 )
284 '
285
286 test_expect_success 'gc.bigPackThreshold ignores cruft packs' '
287 test_when_finished "rm -fr repo" &&
288 git init repo &&
289 (
290 cd repo &&
291
292 # Generate a pack for reachable objects (of which there
293 # are 3), and one for unreachable objects (of which
294 # there are 6).
295 prepare_cruft_history &&
296 git gc --cruft &&
297
298 # Ensure that the cruft pack gets removed (due to
299 # `--prune=now`) despite it being the largest pack.
300 git gc --cruft --prune=now --keep-largest-pack &&
301
302 assert_no_cruft_packs
303 )
304 '
305
306 run_and_wait_for_auto_gc () {
307 # We read stdout from gc for the side effect of waiting until the
308 # background gc process exits, closing its fd 9. Furthermore, the
309 # variable assignment from a command substitution preserves the
310 # exit status of the main gc process.
311 # Note: this fd trickery doesn't work on Windows, but there is no
312 # need to, because on Win the auto gc always runs in the foreground.
313 doesnt_matter=$(git gc --auto 9>&1)
314 }
315
316 test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
317 test_commit foo &&
318 test_commit bar &&
319 git repack &&
320 test_config gc.autopacklimit 1 &&
321 test_config gc.autodetach true &&
322 echo fleem >.git/gc.log &&
323 git gc --auto 2>err &&
324 test_i18ngrep "^warning:" err &&
325 test_config gc.logexpiry 5.days &&
326 test-tool chmtime =-345600 .git/gc.log &&
327 git gc --auto &&
328 test_config gc.logexpiry 2.days &&
329 run_and_wait_for_auto_gc &&
330 ls .git/objects/pack/pack-*.pack >packs &&
331 test_line_count = 1 packs
332 '
333
334 test_expect_success 'background auto gc respects lock for all operations' '
335 # make sure we run a background auto-gc
336 test_commit make-pack &&
337 git repack &&
338 test_config gc.autopacklimit 1 &&
339 test_config gc.autodetach true &&
340
341 # create a ref whose loose presence we can use to detect a pack-refs run
342 git update-ref refs/heads/should-be-loose HEAD &&
343 (ls -1 .git/refs/heads .git/reftable >expect || true) &&
344
345 # now fake a concurrent gc that holds the lock; we can use our
346 # shell pid so that it looks valid.
347 hostname=$(hostname || echo unknown) &&
348 shell_pid=$$ &&
349 if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
350 then
351 # In Git for Windows, Bash (actually, the MSYS2 runtime) has a
352 # different idea of PIDs than git.exe (actually Windows). Use
353 # the Windows PID in this case.
354 shell_pid=$(cat /proc/$shell_pid/winpid)
355 fi &&
356 printf "%d %s" "$shell_pid" "$hostname" >.git/gc.pid &&
357
358 # our gc should exit zero without doing anything
359 run_and_wait_for_auto_gc &&
360 (ls -1 .git/refs/heads .git/reftable >actual || true) &&
361 test_cmp expect actual
362 '
363
364 # DO NOT leave a detached auto gc process running near the end of the
365 # test script: it can run long enough in the background to racily
366 # interfere with the cleanup in 'test_done'.
367
368 test_done