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 if ! test_have_prereq REFFILES
20 skip_all
='skipping reffiles specific tests'
24 test_expect_success
'enable reflogs' '
25 git config core.logallrefupdates true
28 test_expect_success
'prepare a trivial repository' '
30 git update-index --add A &&
31 git commit -m "Initial commit." &&
32 HEAD=$(git rev-parse --verify HEAD)
35 test_expect_success
'pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE)' '
36 N=`find .git/refs -type f | wc -l` &&
38 test-tool ref-store main pack-refs PACK_REFS_PRUNE,PACK_REFS_ALL &&
39 N=`find .git/refs -type f` &&
45 test_expect_success
'see if git show-ref works as expected' '
47 SHA1=$(cat .git/refs/heads/a) &&
48 echo "$SHA1 refs/heads/a" >expect &&
49 git show-ref a >result &&
50 test_cmp expect result
53 test_expect_success
'see if a branch still exists when packed' '
55 git pack-refs --all &&
56 rm -f .git/refs/heads/b &&
57 echo "$SHA1 refs/heads/b" >expect &&
58 git show-ref b >result &&
59 test_cmp expect result
62 test_expect_success
'git branch c/d should barf if branch c exists' '
64 git pack-refs --all &&
65 rm -f .git/refs/heads/c &&
66 test_must_fail git branch c/d
69 test_expect_success
'see if a branch still exists after git pack-refs --prune' '
71 git pack-refs --all --prune &&
72 echo "$SHA1 refs/heads/e" >expect &&
73 git show-ref e >result &&
74 test_cmp expect result
77 test_expect_success
'see if git pack-refs --prune remove ref files' '
79 git pack-refs --all --prune &&
80 ! test -f .git/refs/heads/f
83 test_expect_success
'see if git pack-refs --prune removes empty dirs' '
85 git pack-refs --all --prune &&
86 ! test -e .git/refs/heads/r
89 test_expect_success
'git branch g should work when git branch g/h has been deleted' '
91 git pack-refs --all --prune &&
94 git pack-refs --all &&
98 test_expect_success
'git branch i/j/k should barf if branch i exists' '
100 git pack-refs --all --prune &&
101 test_must_fail git branch i/j/k
104 test_expect_success
'test git branch k after branch k/l/m and k/lm have been deleted' '
109 git branch -d k/l/m &&
110 git branch -d k/lm &&
114 test_expect_success
'test git branch n after some branch deletion and pruning' '
119 git branch -d n/op &&
120 git pack-refs --all --prune &&
121 git branch -d n/o/p &&
125 test_expect_success
'test excluded refs are not packed' '
126 git branch dont_pack1 &&
127 git branch dont_pack2 &&
128 git branch pack_this &&
129 git pack-refs --all --exclude "refs/heads/dont_pack*" &&
130 test -f .git/refs/heads/dont_pack1 &&
131 test -f .git/refs/heads/dont_pack2 &&
132 ! test -f .git/refs/heads/pack_this'
134 test_expect_success
'test --no-exclude refs clears excluded refs' '
135 git branch dont_pack3 &&
136 git branch dont_pack4 &&
137 git pack-refs --all --exclude "refs/heads/dont_pack*" --no-exclude &&
138 ! test -f .git/refs/heads/dont_pack3 &&
139 ! test -f .git/refs/heads/dont_pack4'
141 test_expect_success
'test only included refs are packed' '
142 git branch pack_this1 &&
143 git branch pack_this2 &&
144 git tag dont_pack5 &&
145 git pack-refs --include "refs/heads/pack_this*" &&
146 test -f .git/refs/tags/dont_pack5 &&
147 ! test -f .git/refs/heads/pack_this1 &&
148 ! test -f .git/refs/heads/pack_this2'
150 test_expect_success
'test --no-include refs clears included refs' '
153 git pack-refs --include "refs/heads/pack*" --no-include &&
154 test -f .git/refs/heads/pack1 &&
155 test -f .git/refs/heads/pack2'
157 test_expect_success
'test --exclude takes precedence over --include' '
158 git branch dont_pack5 &&
159 git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
160 test -f .git/refs/heads/dont_pack5'
162 test_expect_success
'see if up-to-date packed refs are preserved' '
164 git pack-refs --all --prune &&
165 git update-ref refs/heads/q refs/heads/q &&
166 ! test -f .git/refs/heads/q
169 test_expect_success
'pack, prune and repack' '
171 git pack-refs --all --prune &&
172 git show-ref >all-of-them &&
174 git show-ref >again &&
175 test_cmp all-of-them again
178 test_expect_success
'explicit pack-refs with dangling packed reference' '
179 git commit --allow-empty -m "soon to be garbage-collected" &&
180 git pack-refs --all &&
181 git reset --hard HEAD^ &&
182 git reflog expire --expire=all --all &&
183 git prune --expire=all &&
184 git pack-refs --all 2>result &&
185 test_must_be_empty result
188 test_expect_success
'delete ref with dangling packed version' '
189 git checkout -b lamb &&
190 git commit --allow-empty -m "future garbage" &&
191 git pack-refs --all &&
192 git reset --hard HEAD^ &&
194 git reflog expire --expire=all --all &&
195 git prune --expire=all &&
196 git branch -d lamb 2>result &&
197 test_must_be_empty result
200 test_expect_success
'delete ref while another dangling packed ref' '
202 git commit --allow-empty -m "future garbage" &&
203 git pack-refs --all &&
204 git reset --hard HEAD^ &&
205 git reflog expire --expire=all --all &&
206 git prune --expire=all &&
207 git branch -d lamb 2>result &&
208 test_must_be_empty result
211 test_expect_success
'pack ref directly below refs/' '
212 git update-ref refs/top HEAD &&
213 git pack-refs --all --prune &&
214 grep refs/top .git/packed-refs &&
215 test_path_is_missing .git/refs/top
218 test_expect_success
'do not pack ref in refs/bisect' '
219 git update-ref refs/bisect/local HEAD &&
220 git pack-refs --all --prune &&
221 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
222 test_path_is_file .git/refs/bisect/local
225 test_expect_success
'disable reflogs' '
226 git config core.logallrefupdates false &&
230 test_expect_success
'create packed foo/bar/baz branch' '
231 git branch foo/bar/baz &&
232 git pack-refs --all --prune &&
233 test_path_is_missing .git/refs/heads/foo/bar/baz &&
234 test_must_fail git reflog exists refs/heads/foo/bar/baz
237 test_expect_success
'notice d/f conflict with existing directory' '
238 test_must_fail git branch foo &&
239 test_must_fail git branch foo/bar
242 test_expect_success
'existing directory reports concrete ref' '
243 test_must_fail git branch foo 2>stderr &&
244 test_grep refs/heads/foo/bar/baz stderr
247 test_expect_success
'notice d/f conflict with existing ref' '
248 test_must_fail git branch foo/bar/baz/extra &&
249 test_must_fail git branch foo/bar/baz/lots/of/extra/components
252 test_expect_success
'reject packed-refs with unterminated line' '
253 cp .git/packed-refs .git/packed-refs.bak &&
254 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
255 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
256 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
257 test_must_fail git for-each-ref >out 2>err &&
258 test_cmp expected_err err
261 test_expect_success
'reject packed-refs containing junk' '
262 cp .git/packed-refs .git/packed-refs.bak &&
263 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
264 printf "%s\n" "bogus content" >>.git/packed-refs &&
265 echo "fatal: unexpected line in .git/packed-refs: bogus content" >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 with a short SHA-1' '
271 cp .git/packed-refs .git/packed-refs.bak &&
272 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
273 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
274 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
275 test_must_fail git for-each-ref >out 2>err &&
276 test_cmp expected_err err
279 test_expect_success
'timeout if packed-refs.lock exists' '
280 LOCK=.git/packed-refs.lock &&
282 test_when_finished "rm -f $LOCK" &&
283 test_must_fail git pack-refs --all --prune
286 test_expect_success
'retry acquiring packed-refs.lock' '
287 LOCK=.git/packed-refs.lock &&
289 test_when_finished "wait && rm -f $LOCK" &&
291 ( sleep 1 && rm -f $LOCK ) &
293 git -c core.packedrefstimeout=3000 pack-refs --all --prune
296 test_expect_success SYMLINKS
'pack symlinked packed-refs' '
297 # First make sure that symlinking works when reading:
298 git update-ref refs/heads/lossy refs/heads/main &&
299 git for-each-ref >all-refs-before &&
300 mv .git/packed-refs .git/my-deviant-packed-refs &&
301 ln -s my-deviant-packed-refs .git/packed-refs &&
302 git for-each-ref >all-refs-linked &&
303 test_cmp all-refs-before all-refs-linked &&
304 git pack-refs --all --prune &&
305 git for-each-ref >all-refs-packed &&
306 test_cmp all-refs-before all-refs-packed &&
307 test -h .git/packed-refs &&
308 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"
311 # The 'packed-refs' file is stored directly in .git/. This means it is global
312 # to the repository, and can only contain refs that are shared across all
314 test_expect_success
'refs/worktree must not be packed' '
315 test_commit initial &&
318 git worktree add wt1 wt1 &&
319 git worktree add wt2 wt2 &&
320 git checkout initial &&
321 git update-ref refs/worktree/foo HEAD &&
322 git -C wt1 update-ref refs/worktree/foo HEAD &&
323 git -C wt2 update-ref refs/worktree/foo HEAD &&
324 git pack-refs --all &&
325 test_path_is_missing .git/refs/tags/wt1 &&
326 test_path_is_file .git/refs/worktree/foo &&
327 test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
328 test_path_is_file .git/worktrees/wt2/refs/worktree/foo
331 # we do not want to count on running pack-refs to
332 # actually pack it, as it is perfectly reasonable to
333 # skip processing a broken ref
334 test_expect_success
'create packed-refs file with broken ref' '
335 test_tick && git commit --allow-empty -m one &&
336 recoverable=$(git rev-parse HEAD) &&
337 test_tick && git commit --allow-empty -m two &&
338 missing=$(git rev-parse HEAD) &&
339 rm -f .git/refs/heads/main &&
340 cat >.git/packed-refs <<-EOF &&
341 $missing refs/heads/main
342 $recoverable refs/heads/other
344 echo $missing >expect &&
345 git rev-parse refs/heads/main >actual &&
346 test_cmp expect actual
349 test_expect_success
'pack-refs does not silently delete broken packed ref' '
350 git pack-refs --all --prune &&
351 git rev-parse refs/heads/main >actual &&
352 test_cmp expect actual
355 test_expect_success
'pack-refs does not drop broken refs during deletion' '
356 git update-ref -d refs/heads/other &&
357 git rev-parse refs/heads/main >actual &&
358 test_cmp expect actual