]> git.ipfire.org Git - thirdparty/git.git/blob - t/t3210-pack-refs.sh
t/lib-credential: clean additional credential
[thirdparty/git.git] / t / t3210-pack-refs.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Amos Waterland
4 # Copyright (c) 2006 Christian Couder
5 #
6
7 test_description='git pack-refs should not change the branch semantic
8
9 This test runs git pack-refs and git show-ref and checks that the branch
10 semantic is still the same.
11 '
12 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
13 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
14
15 TEST_PASSES_SANITIZE_LEAK=true
16 . ./test-lib.sh
17
18 test_expect_success 'enable reflogs' '
19 git config core.logallrefupdates true
20 '
21
22 test_expect_success 'prepare a trivial repository' '
23 echo Hello > A &&
24 git update-index --add A &&
25 git commit -m "Initial commit." &&
26 HEAD=$(git rev-parse --verify HEAD)
27 '
28
29 SHA1=
30
31 test_expect_success 'see if git show-ref works as expected' '
32 git branch a &&
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
37 '
38
39 test_expect_success 'see if a branch still exists when packed' '
40 git branch b &&
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
46 '
47
48 test_expect_success 'git branch c/d should barf if branch c exists' '
49 git branch c &&
50 git pack-refs --all &&
51 rm -f .git/refs/heads/c &&
52 test_must_fail git branch c/d
53 '
54
55 test_expect_success 'see if a branch still exists after git pack-refs --prune' '
56 git branch e &&
57 git pack-refs --all --prune &&
58 echo "$SHA1 refs/heads/e" >expect &&
59 git show-ref e >result &&
60 test_cmp expect result
61 '
62
63 test_expect_success 'see if git pack-refs --prune remove ref files' '
64 git branch f &&
65 git pack-refs --all --prune &&
66 ! test -f .git/refs/heads/f
67 '
68
69 test_expect_success 'see if git pack-refs --prune removes empty dirs' '
70 git branch r/s/t &&
71 git pack-refs --all --prune &&
72 ! test -e .git/refs/heads/r
73 '
74
75 test_expect_success 'git branch g should work when git branch g/h has been deleted' '
76 git branch g/h &&
77 git pack-refs --all --prune &&
78 git branch -d g/h &&
79 git branch g &&
80 git pack-refs --all &&
81 git branch -d g
82 '
83
84 test_expect_success 'git branch i/j/k should barf if branch i exists' '
85 git branch i &&
86 git pack-refs --all --prune &&
87 test_must_fail git branch i/j/k
88 '
89
90 test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' '
91 git branch k/l &&
92 git branch k/lm &&
93 git branch -d k/l &&
94 git branch k/l/m &&
95 git branch -d k/l/m &&
96 git branch -d k/lm &&
97 git branch k
98 '
99
100 test_expect_success 'test git branch n after some branch deletion and pruning' '
101 git branch n/o &&
102 git branch n/op &&
103 git branch -d n/o &&
104 git branch n/o/p &&
105 git branch -d n/op &&
106 git pack-refs --all --prune &&
107 git branch -d n/o/p &&
108 git branch n
109 '
110
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'
119
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'
126
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'
135
136 test_expect_success 'test --no-include refs clears included refs' '
137 git branch pack1 &&
138 git branch pack2 &&
139 git pack-refs --include "refs/heads/pack*" --no-include &&
140 test -f .git/refs/heads/pack1 &&
141 test -f .git/refs/heads/pack2'
142
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'
147
148 test_expect_success 'see if up-to-date packed refs are preserved' '
149 git branch q &&
150 git pack-refs --all --prune &&
151 git update-ref refs/heads/q refs/heads/q &&
152 ! test -f .git/refs/heads/q
153 '
154
155 test_expect_success 'pack, prune and repack' '
156 git tag foo &&
157 git pack-refs --all --prune &&
158 git show-ref >all-of-them &&
159 git pack-refs &&
160 git show-ref >again &&
161 test_cmp all-of-them again
162 '
163
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
172 '
173
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^ &&
179 git checkout main &&
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
184 '
185
186 test_expect_success 'delete ref while another dangling packed ref' '
187 git branch lamb &&
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
195 '
196
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
202 '
203
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
209 '
210
211 test_expect_success 'disable reflogs' '
212 git config core.logallrefupdates false &&
213 rm -rf .git/logs
214 '
215
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
221 '
222
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
226 '
227
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
231 '
232
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
236 '
237
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
245 '
246
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
254 '
255
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
263 '
264
265 test_expect_success 'timeout if packed-refs.lock exists' '
266 LOCK=.git/packed-refs.lock &&
267 >"$LOCK" &&
268 test_when_finished "rm -f $LOCK" &&
269 test_must_fail git pack-refs --all --prune
270 '
271
272 test_expect_success 'retry acquiring packed-refs.lock' '
273 LOCK=.git/packed-refs.lock &&
274 >"$LOCK" &&
275 test_when_finished "wait && rm -f $LOCK" &&
276 {
277 ( sleep 1 && rm -f $LOCK ) &
278 } &&
279 git -c core.packedrefstimeout=3000 pack-refs --all --prune
280 '
281
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"
295 '
296
297 test_done