Merge branch 'mh/packed-ref-store'
[git/git.git] / t / t3210-pack-refs.sh
CommitLineData
919a3c98
CC
1#!/bin/sh
2#
3# Copyright (c) 2005 Amos Waterland
4# Copyright (c) 2006 Christian Couder
5#
6
7test_description='git pack-refs should not change the branch semantic
8
9This test runs git pack-refs and git show-ref and checks that the branch
10semantic is still the same.
11'
12. ./test-lib.sh
13
cbe73331
JK
14test_expect_success 'enable reflogs' '
15 git config core.logallrefupdates true
16'
3b463c3f 17
919a3c98
CC
18test_expect_success \
19 'prepare a trivial repository' \
20 'echo Hello > A &&
5be60078 21 git update-index --add A &&
0cb0e143 22 git commit -m "Initial commit." &&
5be60078 23 HEAD=$(git rev-parse --verify HEAD)'
919a3c98
CC
24
25SHA1=
26
27test_expect_success \
28 'see if git show-ref works as expected' \
5be60078 29 'git branch a &&
13f11b95 30 SHA1=$(cat .git/refs/heads/a) &&
919a3c98 31 echo "$SHA1 refs/heads/a" >expect &&
5be60078 32 git show-ref a >result &&
4fdf71be 33 test_cmp expect result'
919a3c98
CC
34
35test_expect_success \
36 'see if a branch still exists when packed' \
5be60078
JH
37 'git branch b &&
38 git pack-refs --all &&
0f018bab 39 rm -f .git/refs/heads/b &&
919a3c98 40 echo "$SHA1 refs/heads/b" >expect &&
5be60078 41 git show-ref b >result &&
4fdf71be 42 test_cmp expect result'
919a3c98 43
41ac414e
JH
44test_expect_success 'git branch c/d should barf if branch c exists' '
45 git branch c &&
5be60078 46 git pack-refs --all &&
41ac414e 47 rm -f .git/refs/heads/c &&
d492b31c 48 test_must_fail git branch c/d
41ac414e 49'
919a3c98
CC
50
51test_expect_success \
52 'see if a branch still exists after git pack-refs --prune' \
5be60078
JH
53 'git branch e &&
54 git pack-refs --all --prune &&
919a3c98 55 echo "$SHA1 refs/heads/e" >expect &&
5be60078 56 git show-ref e >result &&
4fdf71be 57 test_cmp expect result'
919a3c98 58
41ac414e
JH
59test_expect_success 'see if git pack-refs --prune remove ref files' '
60 git branch f &&
5be60078 61 git pack-refs --all --prune &&
41ac414e
JH
62 ! test -f .git/refs/heads/f
63'
919a3c98 64
be7c6d46
GP
65test_expect_success 'see if git pack-refs --prune removes empty dirs' '
66 git branch r/s/t &&
67 git pack-refs --all --prune &&
68 ! test -e .git/refs/heads/r
69'
70
919a3c98
CC
71test_expect_success \
72 'git branch g should work when git branch g/h has been deleted' \
5be60078
JH
73 'git branch g/h &&
74 git pack-refs --all --prune &&
75 git branch -d g/h &&
76 git branch g &&
77 git pack-refs --all &&
78 git branch -d g'
919a3c98 79
41ac414e
JH
80test_expect_success 'git branch i/j/k should barf if branch i exists' '
81 git branch i &&
5be60078 82 git pack-refs --all --prune &&
d492b31c 83 test_must_fail git branch i/j/k
41ac414e 84'
14c8a681
CC
85
86test_expect_success \
87 'test git branch k after branch k/l/m and k/lm have been deleted' \
5be60078
JH
88 'git branch k/l &&
89 git branch k/lm &&
90 git branch -d k/l &&
91 git branch k/l/m &&
92 git branch -d k/l/m &&
93 git branch -d k/lm &&
94 git branch k'
14c8a681
CC
95
96test_expect_success \
97 'test git branch n after some branch deletion and pruning' \
5be60078
JH
98 'git branch n/o &&
99 git branch n/op &&
100 git branch -d n/o &&
101 git branch n/o/p &&
102 git branch -d n/op &&
103 git pack-refs --all --prune &&
104 git branch -d n/o/p &&
105 git branch n'
14c8a681 106
5bdd8d4a
CB
107test_expect_success \
108 'see if up-to-date packed refs are preserved' \
109 'git branch q &&
110 git pack-refs --all --prune &&
111 git update-ref refs/heads/q refs/heads/q &&
112 ! test -f .git/refs/heads/q'
113
1b555932 114test_expect_success 'pack, prune and repack' '
0cb0e143 115 git tag foo &&
5be60078
JH
116 git pack-refs --all --prune &&
117 git show-ref >all-of-them &&
118 git pack-refs &&
119 git show-ref >again &&
4fdf71be 120 test_cmp all-of-them again
1b555932
LT
121'
122
0a0b8d15
MH
123test_expect_success 'explicit pack-refs with dangling packed reference' '
124 git commit --allow-empty -m "soon to be garbage-collected" &&
125 git pack-refs --all &&
126 git reset --hard HEAD^ &&
127 git reflog expire --expire=all --all &&
128 git prune --expire=all &&
129 git pack-refs --all 2>result &&
130 test_cmp /dev/null result
131'
132
133test_expect_success 'delete ref with dangling packed version' '
134 git checkout -b lamb &&
135 git commit --allow-empty -m "future garbage" &&
136 git pack-refs --all &&
137 git reset --hard HEAD^ &&
138 git checkout master &&
139 git reflog expire --expire=all --all &&
140 git prune --expire=all &&
141 git branch -d lamb 2>result &&
142 test_cmp /dev/null result
143'
144
ab292bc4 145test_expect_success 'delete ref while another dangling packed ref' '
0a0b8d15
MH
146 git branch lamb &&
147 git commit --allow-empty -m "future garbage" &&
148 git pack-refs --all &&
149 git reset --hard HEAD^ &&
150 git reflog expire --expire=all --all &&
151 git prune --expire=all &&
152 git branch -d lamb 2>result &&
153 test_cmp /dev/null result
154'
155
afd11d3e
JK
156test_expect_success 'pack ref directly below refs/' '
157 git update-ref refs/top HEAD &&
158 git pack-refs --all --prune &&
159 grep refs/top .git/packed-refs &&
160 test_path_is_missing .git/refs/top
161'
162
ce414b33
DT
163test_expect_success 'do not pack ref in refs/bisect' '
164 git update-ref refs/bisect/local HEAD &&
165 git pack-refs --all --prune &&
166 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
167 test_path_is_file .git/refs/bisect/local
168'
169
cbe73331
JK
170test_expect_success 'disable reflogs' '
171 git config core.logallrefupdates false &&
172 rm -rf .git/logs
173'
174
175test_expect_success 'create packed foo/bar/baz branch' '
176 git branch foo/bar/baz &&
177 git pack-refs --all --prune &&
178 test_path_is_missing .git/refs/heads/foo/bar/baz &&
d0ab0584 179 test_must_fail git reflog exists refs/heads/foo/bar/baz
cbe73331
JK
180'
181
182test_expect_success 'notice d/f conflict with existing directory' '
183 test_must_fail git branch foo &&
184 test_must_fail git branch foo/bar
185'
186
187test_expect_success 'existing directory reports concrete ref' '
188 test_must_fail git branch foo 2>stderr &&
189 grep refs/heads/foo/bar/baz stderr
190'
191
192test_expect_success 'notice d/f conflict with existing ref' '
193 test_must_fail git branch foo/bar/baz/extra &&
194 test_must_fail git branch foo/bar/baz/lots/of/extra/components
195'
196
9308b7f3 197test_expect_success 'reject packed-refs with unterminated line' '
02a1a420
MH
198 cp .git/packed-refs .git/packed-refs.bak &&
199 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
200 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
201 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
202 test_must_fail git for-each-ref >out 2>err &&
203 test_cmp expected_err err
204'
205
9308b7f3 206test_expect_success 'reject packed-refs containing junk' '
02a1a420
MH
207 cp .git/packed-refs .git/packed-refs.bak &&
208 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
209 printf "%s\n" "bogus content" >>.git/packed-refs &&
210 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
211 test_must_fail git for-each-ref >out 2>err &&
212 test_cmp expected_err err
213'
214
9308b7f3 215test_expect_success 'reject packed-refs with a short SHA-1' '
02a1a420
MH
216 cp .git/packed-refs .git/packed-refs.bak &&
217 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
218 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
219 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
220 test_must_fail git for-each-ref >out 2>err &&
221 test_cmp expected_err err
222'
223
f4ab4f3a
MH
224test_expect_success 'timeout if packed-refs.lock exists' '
225 LOCK=.git/packed-refs.lock &&
226 >"$LOCK" &&
227 test_when_finished "rm -f $LOCK" &&
228 test_must_fail git pack-refs --all --prune
229'
230
231test_expect_success 'retry acquiring packed-refs.lock' '
232 LOCK=.git/packed-refs.lock &&
233 >"$LOCK" &&
234 test_when_finished "wait; rm -f $LOCK" &&
235 {
236 ( sleep 1 ; rm -f $LOCK ) &
237 } &&
238 git -c core.packedrefstimeout=3000 pack-refs --all --prune
239'
240
198b808e
MH
241test_expect_success SYMLINKS 'pack symlinked packed-refs' '
242 # First make sure that symlinking works when reading:
243 git update-ref refs/heads/loosy refs/heads/master &&
244 git for-each-ref >all-refs-before &&
245 mv .git/packed-refs .git/my-deviant-packed-refs &&
246 ln -s my-deviant-packed-refs .git/packed-refs &&
247 git for-each-ref >all-refs-linked &&
248 test_cmp all-refs-before all-refs-linked &&
249 git pack-refs --all --prune &&
250 git for-each-ref >all-refs-packed &&
251 test_cmp all-refs-before all-refs-packed &&
252 test -h .git/packed-refs &&
253 test "$(readlink .git/packed-refs)" = "my-deviant-packed-refs"
254'
255
919a3c98 256test_done