3 # Copyright (c) 2005 Amos Waterland
4 # Copyright (c) 2006 Christian Couder
7 test_description
='git pack-refs should not change the branch semantic
9 This test runs git pack-refs and git show-ref and checks that the branch
10 semantic is still the same.
13 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
14 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
15 GIT_TEST_DEFAULT_REF_FORMAT
=files
16 export GIT_TEST_DEFAULT_REF_FORMAT
18 TEST_PASSES_SANITIZE_LEAK
=true
21 test_expect_success
'enable reflogs' '
22 git config core.logallrefupdates true
25 test_expect_success
'prepare a trivial repository' '
27 git update-index --add A &&
28 git commit -m "Initial commit." &&
29 HEAD=$(git rev-parse --verify HEAD)
32 test_expect_success
'pack-refs --prune --all' '
33 test_path_is_missing .git/packed-refs &&
34 git pack-refs --no-prune --all &&
35 test_path_is_file .git/packed-refs &&
36 N=$(find .git/refs -type f | wc -l) &&
39 git pack-refs --prune --all &&
40 test_path_is_file .git/packed-refs &&
41 N=$(find .git/refs -type f) &&
47 test_expect_success
'see if git show-ref works as expected' '
49 SHA1=$(cat .git/refs/heads/a) &&
50 echo "$SHA1 refs/heads/a" >expect &&
51 git show-ref a >result &&
52 test_cmp expect result
55 test_expect_success
'see if a branch still exists when packed' '
57 git pack-refs --all &&
58 rm -f .git/refs/heads/b &&
59 echo "$SHA1 refs/heads/b" >expect &&
60 git show-ref b >result &&
61 test_cmp expect result
64 test_expect_success
'git branch c/d should barf if branch c exists' '
66 git pack-refs --all &&
67 rm -f .git/refs/heads/c &&
68 test_must_fail git branch c/d
71 test_expect_success
'see if a branch still exists after git pack-refs --prune' '
73 git pack-refs --all --prune &&
74 echo "$SHA1 refs/heads/e" >expect &&
75 git show-ref e >result &&
76 test_cmp expect result
79 test_expect_success
'see if git pack-refs --prune remove ref files' '
81 git pack-refs --all --prune &&
82 ! test -f .git/refs/heads/f
85 test_expect_success
'see if git pack-refs --prune removes empty dirs' '
87 git pack-refs --all --prune &&
88 ! test -e .git/refs/heads/r
91 test_expect_success
'git branch g should work when git branch g/h has been deleted' '
93 git pack-refs --all --prune &&
96 git pack-refs --all &&
100 test_expect_success
'git branch i/j/k should barf if branch i exists' '
102 git pack-refs --all --prune &&
103 test_must_fail git branch i/j/k
106 test_expect_success
'test git branch k after branch k/l/m and k/lm have been deleted' '
111 git branch -d k/l/m &&
112 git branch -d k/lm &&
116 test_expect_success
'test git branch n after some branch deletion and pruning' '
121 git branch -d n/op &&
122 git pack-refs --all --prune &&
123 git branch -d n/o/p &&
127 test_expect_success
'test excluded refs are not packed' '
128 git branch dont_pack1 &&
129 git branch dont_pack2 &&
130 git branch pack_this &&
131 git pack-refs --all --exclude "refs/heads/dont_pack*" &&
132 test -f .git/refs/heads/dont_pack1 &&
133 test -f .git/refs/heads/dont_pack2 &&
134 ! test -f .git/refs/heads/pack_this'
136 test_expect_success
'test --no-exclude refs clears excluded refs' '
137 git branch dont_pack3 &&
138 git branch dont_pack4 &&
139 git pack-refs --all --exclude "refs/heads/dont_pack*" --no-exclude &&
140 ! test -f .git/refs/heads/dont_pack3 &&
141 ! test -f .git/refs/heads/dont_pack4'
143 test_expect_success
'test only included refs are packed' '
144 git branch pack_this1 &&
145 git branch pack_this2 &&
146 git tag dont_pack5 &&
147 git pack-refs --include "refs/heads/pack_this*" &&
148 test -f .git/refs/tags/dont_pack5 &&
149 ! test -f .git/refs/heads/pack_this1 &&
150 ! test -f .git/refs/heads/pack_this2'
152 test_expect_success
'test --no-include refs clears included refs' '
155 git pack-refs --include "refs/heads/pack*" --no-include &&
156 test -f .git/refs/heads/pack1 &&
157 test -f .git/refs/heads/pack2'
159 test_expect_success
'test --exclude takes precedence over --include' '
160 git branch dont_pack5 &&
161 git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
162 test -f .git/refs/heads/dont_pack5'
164 test_expect_success
'--auto packs and prunes refs as usual' '
166 test_path_is_file .git/refs/heads/auto &&
167 git pack-refs --auto --all &&
168 test_path_is_missing .git/refs/heads/auto
171 test_expect_success
'see if up-to-date packed refs are preserved' '
173 git pack-refs --all --prune &&
174 git update-ref refs/heads/q refs/heads/q &&
175 ! test -f .git/refs/heads/q
178 test_expect_success
'pack, prune and repack' '
180 git pack-refs --all --prune &&
181 git show-ref >all-of-them &&
183 git show-ref >again &&
184 test_cmp all-of-them again
187 test_expect_success
'explicit pack-refs with dangling packed reference' '
188 git commit --allow-empty -m "soon to be garbage-collected" &&
189 git pack-refs --all &&
190 git reset --hard HEAD^ &&
191 git reflog expire --expire=all --all &&
192 git prune --expire=all &&
193 git pack-refs --all 2>result &&
194 test_must_be_empty result
197 test_expect_success
'delete ref with dangling packed version' '
198 git checkout -b lamb &&
199 git commit --allow-empty -m "future garbage" &&
200 git pack-refs --all &&
201 git reset --hard HEAD^ &&
203 git reflog expire --expire=all --all &&
204 git prune --expire=all &&
205 git branch -d lamb 2>result &&
206 test_must_be_empty result
209 test_expect_success
'delete ref while another dangling packed ref' '
211 git commit --allow-empty -m "future garbage" &&
212 git pack-refs --all &&
213 git reset --hard HEAD^ &&
214 git reflog expire --expire=all --all &&
215 git prune --expire=all &&
216 git branch -d lamb 2>result &&
217 test_must_be_empty result
220 test_expect_success
'pack ref directly below refs/' '
221 git update-ref refs/top HEAD &&
222 git pack-refs --all --prune &&
223 grep refs/top .git/packed-refs &&
224 test_path_is_missing .git/refs/top
227 test_expect_success
'do not pack ref in refs/bisect' '
228 git update-ref refs/bisect/local HEAD &&
229 git pack-refs --all --prune &&
230 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
231 test_path_is_file .git/refs/bisect/local
234 test_expect_success
'disable reflogs' '
235 git config core.logallrefupdates false &&
239 test_expect_success
'create packed foo/bar/baz branch' '
240 git branch foo/bar/baz &&
241 git pack-refs --all --prune &&
242 test_path_is_missing .git/refs/heads/foo/bar/baz &&
243 test_must_fail git reflog exists refs/heads/foo/bar/baz
246 test_expect_success
'notice d/f conflict with existing directory' '
247 test_must_fail git branch foo &&
248 test_must_fail git branch foo/bar
251 test_expect_success
'existing directory reports concrete ref' '
252 test_must_fail git branch foo 2>stderr &&
253 test_grep refs/heads/foo/bar/baz stderr
256 test_expect_success
'notice d/f conflict with existing ref' '
257 test_must_fail git branch foo/bar/baz/extra &&
258 test_must_fail git branch foo/bar/baz/lots/of/extra/components
261 test_expect_success
'reject packed-refs with unterminated line' '
262 cp .git/packed-refs .git/packed-refs.bak &&
263 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
264 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
265 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
266 test_must_fail git for-each-ref >out 2>err &&
267 test_cmp expected_err err
270 test_expect_success
'reject packed-refs containing junk' '
271 cp .git/packed-refs .git/packed-refs.bak &&
272 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
273 printf "%s\n" "bogus content" >>.git/packed-refs &&
274 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
275 test_must_fail git for-each-ref >out 2>err &&
276 test_cmp expected_err err
279 test_expect_success
'reject packed-refs with a short SHA-1' '
280 cp .git/packed-refs .git/packed-refs.bak &&
281 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
282 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
283 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
284 test_must_fail git for-each-ref >out 2>err &&
285 test_cmp expected_err err
288 test_expect_success
'timeout if packed-refs.lock exists' '
289 LOCK=.git/packed-refs.lock &&
291 test_when_finished "rm -f $LOCK" &&
292 test_must_fail git pack-refs --all --prune
295 test_expect_success
'retry acquiring packed-refs.lock' '
296 LOCK=.git/packed-refs.lock &&
298 test_when_finished "wait && rm -f $LOCK" &&
300 ( sleep 1 && rm -f $LOCK ) &
302 git -c core.packedrefstimeout=3000 pack-refs --all --prune
305 test_expect_success SYMLINKS
'pack symlinked packed-refs' '
306 # First make sure that symlinking works when reading:
307 git update-ref refs/heads/lossy refs/heads/main &&
308 git for-each-ref >all-refs-before &&
309 mv .git/packed-refs .git/my-deviant-packed-refs &&
310 ln -s my-deviant-packed-refs .git/packed-refs &&
311 git for-each-ref >all-refs-linked &&
312 test_cmp all-refs-before all-refs-linked &&
313 git pack-refs --all --prune &&
314 git for-each-ref >all-refs-packed &&
315 test_cmp all-refs-before all-refs-packed &&
316 test -h .git/packed-refs &&
317 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"
320 # The 'packed-refs' file is stored directly in .git/. This means it is global
321 # to the repository, and can only contain refs that are shared across all
323 test_expect_success
'refs/worktree must not be packed' '
324 test_commit initial &&
327 git worktree add wt1 wt1 &&
328 git worktree add wt2 wt2 &&
329 git checkout initial &&
330 git update-ref refs/worktree/foo HEAD &&
331 git -C wt1 update-ref refs/worktree/foo HEAD &&
332 git -C wt2 update-ref refs/worktree/foo HEAD &&
333 git pack-refs --all &&
334 test_path_is_missing .git/refs/tags/wt1 &&
335 test_path_is_file .git/refs/worktree/foo &&
336 test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
337 test_path_is_file .git/worktrees/wt2/refs/worktree/foo
340 # we do not want to count on running pack-refs to
341 # actually pack it, as it is perfectly reasonable to
342 # skip processing a broken ref
343 test_expect_success
'create packed-refs file with broken ref' '
344 test_tick && git commit --allow-empty -m one &&
345 recoverable=$(git rev-parse HEAD) &&
346 test_tick && git commit --allow-empty -m two &&
347 missing=$(git rev-parse HEAD) &&
348 rm -f .git/refs/heads/main &&
349 cat >.git/packed-refs <<-EOF &&
350 $missing refs/heads/main
351 $recoverable refs/heads/other
353 echo $missing >expect &&
354 git rev-parse refs/heads/main >actual &&
355 test_cmp expect actual
358 test_expect_success
'pack-refs does not silently delete broken packed ref' '
359 git pack-refs --all --prune &&
360 git rev-parse refs/heads/main >actual &&
361 test_cmp expect actual
364 test_expect_success
'pack-refs does not drop broken refs during deletion' '
365 git update-ref -d refs/heads/other &&
366 git rev-parse refs/heads/main >actual &&
367 test_cmp expect actual
370 test_expect_success
'maintenance --auto unconditionally packs loose refs' '
371 git update-ref refs/heads/something HEAD &&
372 test_path_is_file .git/refs/heads/something &&
373 git rev-parse refs/heads/something >expect &&
374 git maintenance run --task=pack-refs --auto &&
375 test_path_is_missing .git/refs/heads/something &&
376 git rev-parse refs/heads/something >actual &&
377 test_cmp expect actual