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.
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
15 TEST_PASSES_SANITIZE_LEAK
=true
18 test_expect_success
'enable reflogs' '
19 git config core.logallrefupdates true
22 test_expect_success
'prepare a trivial repository' '
24 git update-index --add A &&
25 git commit -m "Initial commit." &&
26 HEAD=$(git rev-parse --verify HEAD)
31 test_expect_success
'see if git show-ref works as expected' '
33 SHA1=$(cat .git/refs/heads/a) &&
34 echo "$SHA1 refs/heads/a" >expect &&
35 git show-ref a >result &&
36 test_cmp expect result
39 test_expect_success
'see if a branch still exists when packed' '
41 git pack-refs --all &&
42 rm -f .git/refs/heads/b &&
43 echo "$SHA1 refs/heads/b" >expect &&
44 git show-ref b >result &&
45 test_cmp expect result
48 test_expect_success
'git branch c/d should barf if branch c exists' '
50 git pack-refs --all &&
51 rm -f .git/refs/heads/c &&
52 test_must_fail git branch c/d
55 test_expect_success
'see if a branch still exists after git pack-refs --prune' '
57 git pack-refs --all --prune &&
58 echo "$SHA1 refs/heads/e" >expect &&
59 git show-ref e >result &&
60 test_cmp expect result
63 test_expect_success
'see if git pack-refs --prune remove ref files' '
65 git pack-refs --all --prune &&
66 ! test -f .git/refs/heads/f
69 test_expect_success
'see if git pack-refs --prune removes empty dirs' '
71 git pack-refs --all --prune &&
72 ! test -e .git/refs/heads/r
75 test_expect_success
'git branch g should work when git branch g/h has been deleted' '
77 git pack-refs --all --prune &&
80 git pack-refs --all &&
84 test_expect_success
'git branch i/j/k should barf if branch i exists' '
86 git pack-refs --all --prune &&
87 test_must_fail git branch i/j/k
90 test_expect_success
'test git branch k after branch k/l/m and k/lm have been deleted' '
95 git branch -d k/l/m &&
100 test_expect_success
'test git branch n after some branch deletion and pruning' '
105 git branch -d n/op &&
106 git pack-refs --all --prune &&
107 git branch -d n/o/p &&
111 test_expect_success
'test excluded refs are not packed' '
112 git branch dont_pack1 &&
113 git branch dont_pack2 &&
114 git branch pack_this &&
115 git pack-refs --all --exclude "refs/heads/dont_pack*" &&
116 test -f .git/refs/heads/dont_pack1 &&
117 test -f .git/refs/heads/dont_pack2 &&
118 ! test -f .git/refs/heads/pack_this'
120 test_expect_success
'test --no-exclude refs clears excluded refs' '
121 git branch dont_pack3 &&
122 git branch dont_pack4 &&
123 git pack-refs --all --exclude "refs/heads/dont_pack*" --no-exclude &&
124 ! test -f .git/refs/heads/dont_pack3 &&
125 ! test -f .git/refs/heads/dont_pack4'
127 test_expect_success
'test only included refs are packed' '
128 git branch pack_this1 &&
129 git branch pack_this2 &&
130 git tag dont_pack5 &&
131 git pack-refs --include "refs/heads/pack_this*" &&
132 test -f .git/refs/tags/dont_pack5 &&
133 ! test -f .git/refs/heads/pack_this1 &&
134 ! test -f .git/refs/heads/pack_this2'
136 test_expect_success
'test --no-include refs clears included refs' '
139 git pack-refs --include "refs/heads/pack*" --no-include &&
140 test -f .git/refs/heads/pack1 &&
141 test -f .git/refs/heads/pack2'
143 test_expect_success
'test --exclude takes precedence over --include' '
144 git branch dont_pack5 &&
145 git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
146 test -f .git/refs/heads/dont_pack5'
148 test_expect_success
'see if up-to-date packed refs are preserved' '
150 git pack-refs --all --prune &&
151 git update-ref refs/heads/q refs/heads/q &&
152 ! test -f .git/refs/heads/q
155 test_expect_success
'pack, prune and repack' '
157 git pack-refs --all --prune &&
158 git show-ref >all-of-them &&
160 git show-ref >again &&
161 test_cmp all-of-them again
164 test_expect_success
'explicit pack-refs with dangling packed reference' '
165 git commit --allow-empty -m "soon to be garbage-collected" &&
166 git pack-refs --all &&
167 git reset --hard HEAD^ &&
168 git reflog expire --expire=all --all &&
169 git prune --expire=all &&
170 git pack-refs --all 2>result &&
171 test_must_be_empty result
174 test_expect_success
'delete ref with dangling packed version' '
175 git checkout -b lamb &&
176 git commit --allow-empty -m "future garbage" &&
177 git pack-refs --all &&
178 git reset --hard HEAD^ &&
180 git reflog expire --expire=all --all &&
181 git prune --expire=all &&
182 git branch -d lamb 2>result &&
183 test_must_be_empty result
186 test_expect_success
'delete ref while another dangling packed ref' '
188 git commit --allow-empty -m "future garbage" &&
189 git pack-refs --all &&
190 git reset --hard HEAD^ &&
191 git reflog expire --expire=all --all &&
192 git prune --expire=all &&
193 git branch -d lamb 2>result &&
194 test_must_be_empty result
197 test_expect_success
'pack ref directly below refs/' '
198 git update-ref refs/top HEAD &&
199 git pack-refs --all --prune &&
200 grep refs/top .git/packed-refs &&
201 test_path_is_missing .git/refs/top
204 test_expect_success
'do not pack ref in refs/bisect' '
205 git update-ref refs/bisect/local HEAD &&
206 git pack-refs --all --prune &&
207 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
208 test_path_is_file .git/refs/bisect/local
211 test_expect_success
'disable reflogs' '
212 git config core.logallrefupdates false &&
216 test_expect_success
'create packed foo/bar/baz branch' '
217 git branch foo/bar/baz &&
218 git pack-refs --all --prune &&
219 test_path_is_missing .git/refs/heads/foo/bar/baz &&
220 test_must_fail git reflog exists refs/heads/foo/bar/baz
223 test_expect_success
'notice d/f conflict with existing directory' '
224 test_must_fail git branch foo &&
225 test_must_fail git branch foo/bar
228 test_expect_success
'existing directory reports concrete ref' '
229 test_must_fail git branch foo 2>stderr &&
230 test_grep refs/heads/foo/bar/baz stderr
233 test_expect_success
'notice d/f conflict with existing ref' '
234 test_must_fail git branch foo/bar/baz/extra &&
235 test_must_fail git branch foo/bar/baz/lots/of/extra/components
238 test_expect_success
'reject packed-refs with unterminated line' '
239 cp .git/packed-refs .git/packed-refs.bak &&
240 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
241 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
242 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
243 test_must_fail git for-each-ref >out 2>err &&
244 test_cmp expected_err err
247 test_expect_success
'reject packed-refs containing junk' '
248 cp .git/packed-refs .git/packed-refs.bak &&
249 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
250 printf "%s\n" "bogus content" >>.git/packed-refs &&
251 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
252 test_must_fail git for-each-ref >out 2>err &&
253 test_cmp expected_err err
256 test_expect_success
'reject packed-refs with a short SHA-1' '
257 cp .git/packed-refs .git/packed-refs.bak &&
258 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
259 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
260 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
261 test_must_fail git for-each-ref >out 2>err &&
262 test_cmp expected_err err
265 test_expect_success
'timeout if packed-refs.lock exists' '
266 LOCK=.git/packed-refs.lock &&
268 test_when_finished "rm -f $LOCK" &&
269 test_must_fail git pack-refs --all --prune
272 test_expect_success
'retry acquiring packed-refs.lock' '
273 LOCK=.git/packed-refs.lock &&
275 test_when_finished "wait && rm -f $LOCK" &&
277 ( sleep 1 && rm -f $LOCK ) &
279 git -c core.packedrefstimeout=3000 pack-refs --all --prune
282 test_expect_success SYMLINKS
'pack symlinked packed-refs' '
283 # First make sure that symlinking works when reading:
284 git update-ref refs/heads/lossy refs/heads/main &&
285 git for-each-ref >all-refs-before &&
286 mv .git/packed-refs .git/my-deviant-packed-refs &&
287 ln -s my-deviant-packed-refs .git/packed-refs &&
288 git for-each-ref >all-refs-linked &&
289 test_cmp all-refs-before all-refs-linked &&
290 git pack-refs --all --prune &&
291 git for-each-ref >all-refs-packed &&
292 test_cmp all-refs-before all-refs-packed &&
293 test -h .git/packed-refs &&
294 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"