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
17 test_expect_success
'enable reflogs' '
18 git config core.logallrefupdates true
22 'prepare a trivial repository' \
24 git update-index --add A &&
25 git commit -m "Initial commit." &&
26 HEAD=$(git rev-parse --verify HEAD)'
31 '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 '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'
47 test_expect_success
'git branch c/d should barf if branch c exists' '
49 git pack-refs --all &&
50 rm -f .git/refs/heads/c &&
51 test_must_fail git branch c/d
55 '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'
62 test_expect_success
'see if git pack-refs --prune remove ref files' '
64 git pack-refs --all --prune &&
65 ! test -f .git/refs/heads/f
68 test_expect_success
'see if git pack-refs --prune removes empty dirs' '
70 git pack-refs --all --prune &&
71 ! test -e .git/refs/heads/r
75 'git branch g should work when git branch g/h has been deleted' \
77 git pack-refs --all --prune &&
80 git pack-refs --all &&
83 test_expect_success
'git branch i/j/k should barf if branch i exists' '
85 git pack-refs --all --prune &&
86 test_must_fail git branch i/j/k
90 'test git branch k after branch k/l/m and k/lm have been deleted' \
95 git branch -d k/l/m &&
100 '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 &&
110 test_expect_success \
111 'see if up-to-date packed refs are preserved' \
113 git pack-refs --all --prune &&
114 git update-ref refs/heads/q refs/heads/q &&
115 ! test -f .git/refs/heads/q'
117 test_expect_success
'pack, prune and repack' '
119 git pack-refs --all --prune &&
120 git show-ref >all-of-them &&
122 git show-ref >again &&
123 test_cmp all-of-them again
126 test_expect_success
'explicit pack-refs with dangling packed reference' '
127 git commit --allow-empty -m "soon to be garbage-collected" &&
128 git pack-refs --all &&
129 git reset --hard HEAD^ &&
130 git reflog expire --expire=all --all &&
131 git prune --expire=all &&
132 git pack-refs --all 2>result &&
133 test_must_be_empty result
136 test_expect_success
'delete ref with dangling packed version' '
137 git checkout -b lamb &&
138 git commit --allow-empty -m "future garbage" &&
139 git pack-refs --all &&
140 git reset --hard HEAD^ &&
142 git reflog expire --expire=all --all &&
143 git prune --expire=all &&
144 git branch -d lamb 2>result &&
145 test_must_be_empty result
148 test_expect_success
'delete ref while another dangling packed ref' '
150 git commit --allow-empty -m "future garbage" &&
151 git pack-refs --all &&
152 git reset --hard HEAD^ &&
153 git reflog expire --expire=all --all &&
154 git prune --expire=all &&
155 git branch -d lamb 2>result &&
156 test_must_be_empty result
159 test_expect_success
'pack ref directly below refs/' '
160 git update-ref refs/top HEAD &&
161 git pack-refs --all --prune &&
162 grep refs/top .git/packed-refs &&
163 test_path_is_missing .git/refs/top
166 test_expect_success
'do not pack ref in refs/bisect' '
167 git update-ref refs/bisect/local HEAD &&
168 git pack-refs --all --prune &&
169 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
170 test_path_is_file .git/refs/bisect/local
173 test_expect_success
'disable reflogs' '
174 git config core.logallrefupdates false &&
178 test_expect_success
'create packed foo/bar/baz branch' '
179 git branch foo/bar/baz &&
180 git pack-refs --all --prune &&
181 test_path_is_missing .git/refs/heads/foo/bar/baz &&
182 test_must_fail git reflog exists refs/heads/foo/bar/baz
185 test_expect_success
'notice d/f conflict with existing directory' '
186 test_must_fail git branch foo &&
187 test_must_fail git branch foo/bar
190 test_expect_success
'existing directory reports concrete ref' '
191 test_must_fail git branch foo 2>stderr &&
192 test_i18ngrep refs/heads/foo/bar/baz stderr
195 test_expect_success
'notice d/f conflict with existing ref' '
196 test_must_fail git branch foo/bar/baz/extra &&
197 test_must_fail git branch foo/bar/baz/lots/of/extra/components
200 test_expect_success
'reject packed-refs with unterminated line' '
201 cp .git/packed-refs .git/packed-refs.bak &&
202 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
203 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
204 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
205 test_must_fail git for-each-ref >out 2>err &&
206 test_cmp expected_err err
209 test_expect_success
'reject packed-refs containing junk' '
210 cp .git/packed-refs .git/packed-refs.bak &&
211 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
212 printf "%s\n" "bogus content" >>.git/packed-refs &&
213 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
214 test_must_fail git for-each-ref >out 2>err &&
215 test_cmp expected_err err
218 test_expect_success
'reject packed-refs with a short SHA-1' '
219 cp .git/packed-refs .git/packed-refs.bak &&
220 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
221 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
222 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
223 test_must_fail git for-each-ref >out 2>err &&
224 test_cmp expected_err err
227 test_expect_success
'timeout if packed-refs.lock exists' '
228 LOCK=.git/packed-refs.lock &&
230 test_when_finished "rm -f $LOCK" &&
231 test_must_fail git pack-refs --all --prune
234 test_expect_success
'retry acquiring packed-refs.lock' '
235 LOCK=.git/packed-refs.lock &&
237 test_when_finished "wait && rm -f $LOCK" &&
239 ( sleep 1 && rm -f $LOCK ) &
241 git -c core.packedrefstimeout=3000 pack-refs --all --prune
244 test_expect_success SYMLINKS
'pack symlinked packed-refs' '
245 # First make sure that symlinking works when reading:
246 git update-ref refs/heads/lossy refs/heads/main &&
247 git for-each-ref >all-refs-before &&
248 mv .git/packed-refs .git/my-deviant-packed-refs &&
249 ln -s my-deviant-packed-refs .git/packed-refs &&
250 git for-each-ref >all-refs-linked &&
251 test_cmp all-refs-before all-refs-linked &&
252 git pack-refs --all --prune &&
253 git for-each-ref >all-refs-packed &&
254 test_cmp all-refs-before all-refs-packed &&
255 test -h .git/packed-refs &&
256 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"