Start the 2.46 cycle
[alt-git.git] / t / t0601-reffiles-pack-refs.sh
blob7d4ab0b91aad75f8363f0f019664c81ae00b49cd
1 #!/bin/sh
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
16 . ./test-lib.sh
18 if ! test_have_prereq REFFILES
19 then
20 skip_all='skipping reffiles specific tests'
21 test_done
24 test_expect_success 'enable reflogs' '
25 git config core.logallrefupdates true
28 test_expect_success 'prepare a trivial repository' '
29 echo Hello > A &&
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 --prune --all' '
36 test_path_is_missing .git/packed-refs &&
37 git pack-refs --no-prune --all &&
38 test_path_is_file .git/packed-refs &&
39 N=$(find .git/refs -type f | wc -l) &&
40 test "$N" != 0 &&
42 git pack-refs --prune --all &&
43 test_path_is_file .git/packed-refs &&
44 N=$(find .git/refs -type f) &&
45 test -z "$N"
48 SHA1=
50 test_expect_success 'see if git show-ref works as expected' '
51 git branch a &&
52 SHA1=$(cat .git/refs/heads/a) &&
53 echo "$SHA1 refs/heads/a" >expect &&
54 git show-ref a >result &&
55 test_cmp expect result
58 test_expect_success 'see if a branch still exists when packed' '
59 git branch b &&
60 git pack-refs --all &&
61 rm -f .git/refs/heads/b &&
62 echo "$SHA1 refs/heads/b" >expect &&
63 git show-ref b >result &&
64 test_cmp expect result
67 test_expect_success 'git branch c/d should barf if branch c exists' '
68 git branch c &&
69 git pack-refs --all &&
70 rm -f .git/refs/heads/c &&
71 test_must_fail git branch c/d
74 test_expect_success 'see if a branch still exists after git pack-refs --prune' '
75 git branch e &&
76 git pack-refs --all --prune &&
77 echo "$SHA1 refs/heads/e" >expect &&
78 git show-ref e >result &&
79 test_cmp expect result
82 test_expect_success 'see if git pack-refs --prune remove ref files' '
83 git branch f &&
84 git pack-refs --all --prune &&
85 ! test -f .git/refs/heads/f
88 test_expect_success 'see if git pack-refs --prune removes empty dirs' '
89 git branch r/s/t &&
90 git pack-refs --all --prune &&
91 ! test -e .git/refs/heads/r
94 test_expect_success 'git branch g should work when git branch g/h has been deleted' '
95 git branch g/h &&
96 git pack-refs --all --prune &&
97 git branch -d g/h &&
98 git branch g &&
99 git pack-refs --all &&
100 git branch -d g
103 test_expect_success 'git branch i/j/k should barf if branch i exists' '
104 git branch i &&
105 git pack-refs --all --prune &&
106 test_must_fail git branch i/j/k
109 test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' '
110 git branch k/l &&
111 git branch k/lm &&
112 git branch -d k/l &&
113 git branch k/l/m &&
114 git branch -d k/l/m &&
115 git branch -d k/lm &&
116 git branch k
119 test_expect_success 'test git branch n after some branch deletion and pruning' '
120 git branch n/o &&
121 git branch n/op &&
122 git branch -d n/o &&
123 git branch n/o/p &&
124 git branch -d n/op &&
125 git pack-refs --all --prune &&
126 git branch -d n/o/p &&
127 git branch n
130 test_expect_success 'test excluded refs are not packed' '
131 git branch dont_pack1 &&
132 git branch dont_pack2 &&
133 git branch pack_this &&
134 git pack-refs --all --exclude "refs/heads/dont_pack*" &&
135 test -f .git/refs/heads/dont_pack1 &&
136 test -f .git/refs/heads/dont_pack2 &&
137 ! test -f .git/refs/heads/pack_this'
139 test_expect_success 'test --no-exclude refs clears excluded refs' '
140 git branch dont_pack3 &&
141 git branch dont_pack4 &&
142 git pack-refs --all --exclude "refs/heads/dont_pack*" --no-exclude &&
143 ! test -f .git/refs/heads/dont_pack3 &&
144 ! test -f .git/refs/heads/dont_pack4'
146 test_expect_success 'test only included refs are packed' '
147 git branch pack_this1 &&
148 git branch pack_this2 &&
149 git tag dont_pack5 &&
150 git pack-refs --include "refs/heads/pack_this*" &&
151 test -f .git/refs/tags/dont_pack5 &&
152 ! test -f .git/refs/heads/pack_this1 &&
153 ! test -f .git/refs/heads/pack_this2'
155 test_expect_success 'test --no-include refs clears included refs' '
156 git branch pack1 &&
157 git branch pack2 &&
158 git pack-refs --include "refs/heads/pack*" --no-include &&
159 test -f .git/refs/heads/pack1 &&
160 test -f .git/refs/heads/pack2'
162 test_expect_success 'test --exclude takes precedence over --include' '
163 git branch dont_pack5 &&
164 git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
165 test -f .git/refs/heads/dont_pack5'
167 test_expect_success '--auto packs and prunes refs as usual' '
168 git branch auto &&
169 test_path_is_file .git/refs/heads/auto &&
170 git pack-refs --auto --all &&
171 test_path_is_missing .git/refs/heads/auto
174 test_expect_success 'see if up-to-date packed refs are preserved' '
175 git branch q &&
176 git pack-refs --all --prune &&
177 git update-ref refs/heads/q refs/heads/q &&
178 ! test -f .git/refs/heads/q
181 test_expect_success 'pack, prune and repack' '
182 git tag foo &&
183 git pack-refs --all --prune &&
184 git show-ref >all-of-them &&
185 git pack-refs &&
186 git show-ref >again &&
187 test_cmp all-of-them again
190 test_expect_success 'explicit pack-refs with dangling packed reference' '
191 git commit --allow-empty -m "soon to be garbage-collected" &&
192 git pack-refs --all &&
193 git reset --hard HEAD^ &&
194 git reflog expire --expire=all --all &&
195 git prune --expire=all &&
196 git pack-refs --all 2>result &&
197 test_must_be_empty result
200 test_expect_success 'delete ref with dangling packed version' '
201 git checkout -b lamb &&
202 git commit --allow-empty -m "future garbage" &&
203 git pack-refs --all &&
204 git reset --hard HEAD^ &&
205 git checkout main &&
206 git reflog expire --expire=all --all &&
207 git prune --expire=all &&
208 git branch -d lamb 2>result &&
209 test_must_be_empty result
212 test_expect_success 'delete ref while another dangling packed ref' '
213 git branch lamb &&
214 git commit --allow-empty -m "future garbage" &&
215 git pack-refs --all &&
216 git reset --hard HEAD^ &&
217 git reflog expire --expire=all --all &&
218 git prune --expire=all &&
219 git branch -d lamb 2>result &&
220 test_must_be_empty result
223 test_expect_success 'pack ref directly below refs/' '
224 git update-ref refs/top HEAD &&
225 git pack-refs --all --prune &&
226 grep refs/top .git/packed-refs &&
227 test_path_is_missing .git/refs/top
230 test_expect_success 'do not pack ref in refs/bisect' '
231 git update-ref refs/bisect/local HEAD &&
232 git pack-refs --all --prune &&
233 ! grep refs/bisect/local .git/packed-refs >/dev/null &&
234 test_path_is_file .git/refs/bisect/local
237 test_expect_success 'disable reflogs' '
238 git config core.logallrefupdates false &&
239 rm -rf .git/logs
242 test_expect_success 'create packed foo/bar/baz branch' '
243 git branch foo/bar/baz &&
244 git pack-refs --all --prune &&
245 test_path_is_missing .git/refs/heads/foo/bar/baz &&
246 test_must_fail git reflog exists refs/heads/foo/bar/baz
249 test_expect_success 'notice d/f conflict with existing directory' '
250 test_must_fail git branch foo &&
251 test_must_fail git branch foo/bar
254 test_expect_success 'existing directory reports concrete ref' '
255 test_must_fail git branch foo 2>stderr &&
256 test_grep refs/heads/foo/bar/baz stderr
259 test_expect_success 'notice d/f conflict with existing ref' '
260 test_must_fail git branch foo/bar/baz/extra &&
261 test_must_fail git branch foo/bar/baz/lots/of/extra/components
264 test_expect_success 'reject packed-refs with unterminated line' '
265 cp .git/packed-refs .git/packed-refs.bak &&
266 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
267 printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
268 echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
269 test_must_fail git for-each-ref >out 2>err &&
270 test_cmp expected_err err
273 test_expect_success 'reject packed-refs containing junk' '
274 cp .git/packed-refs .git/packed-refs.bak &&
275 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
276 printf "%s\n" "bogus content" >>.git/packed-refs &&
277 echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
278 test_must_fail git for-each-ref >out 2>err &&
279 test_cmp expected_err err
282 test_expect_success 'reject packed-refs with a short SHA-1' '
283 cp .git/packed-refs .git/packed-refs.bak &&
284 test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
285 printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
286 printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
287 test_must_fail git for-each-ref >out 2>err &&
288 test_cmp expected_err err
291 test_expect_success 'timeout if packed-refs.lock exists' '
292 LOCK=.git/packed-refs.lock &&
293 >"$LOCK" &&
294 test_when_finished "rm -f $LOCK" &&
295 test_must_fail git pack-refs --all --prune
298 test_expect_success 'retry acquiring packed-refs.lock' '
299 LOCK=.git/packed-refs.lock &&
300 >"$LOCK" &&
301 test_when_finished "wait && rm -f $LOCK" &&
303 ( sleep 1 && rm -f $LOCK ) &
304 } &&
305 git -c core.packedrefstimeout=3000 pack-refs --all --prune
308 test_expect_success SYMLINKS 'pack symlinked packed-refs' '
309 # First make sure that symlinking works when reading:
310 git update-ref refs/heads/lossy refs/heads/main &&
311 git for-each-ref >all-refs-before &&
312 mv .git/packed-refs .git/my-deviant-packed-refs &&
313 ln -s my-deviant-packed-refs .git/packed-refs &&
314 git for-each-ref >all-refs-linked &&
315 test_cmp all-refs-before all-refs-linked &&
316 git pack-refs --all --prune &&
317 git for-each-ref >all-refs-packed &&
318 test_cmp all-refs-before all-refs-packed &&
319 test -h .git/packed-refs &&
320 test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"
323 # The 'packed-refs' file is stored directly in .git/. This means it is global
324 # to the repository, and can only contain refs that are shared across all
325 # worktrees.
326 test_expect_success 'refs/worktree must not be packed' '
327 test_commit initial &&
328 test_commit wt1 &&
329 test_commit wt2 &&
330 git worktree add wt1 wt1 &&
331 git worktree add wt2 wt2 &&
332 git checkout initial &&
333 git update-ref refs/worktree/foo HEAD &&
334 git -C wt1 update-ref refs/worktree/foo HEAD &&
335 git -C wt2 update-ref refs/worktree/foo HEAD &&
336 git pack-refs --all &&
337 test_path_is_missing .git/refs/tags/wt1 &&
338 test_path_is_file .git/refs/worktree/foo &&
339 test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
340 test_path_is_file .git/worktrees/wt2/refs/worktree/foo
343 # we do not want to count on running pack-refs to
344 # actually pack it, as it is perfectly reasonable to
345 # skip processing a broken ref
346 test_expect_success 'create packed-refs file with broken ref' '
347 test_tick && git commit --allow-empty -m one &&
348 recoverable=$(git rev-parse HEAD) &&
349 test_tick && git commit --allow-empty -m two &&
350 missing=$(git rev-parse HEAD) &&
351 rm -f .git/refs/heads/main &&
352 cat >.git/packed-refs <<-EOF &&
353 $missing refs/heads/main
354 $recoverable refs/heads/other
356 echo $missing >expect &&
357 git rev-parse refs/heads/main >actual &&
358 test_cmp expect actual
361 test_expect_success 'pack-refs does not silently delete broken packed ref' '
362 git pack-refs --all --prune &&
363 git rev-parse refs/heads/main >actual &&
364 test_cmp expect actual
367 test_expect_success 'pack-refs does not drop broken refs during deletion' '
368 git update-ref -d refs/heads/other &&
369 git rev-parse refs/heads/main >actual &&
370 test_cmp expect actual
373 test_expect_success 'maintenance --auto unconditionally packs loose refs' '
374 git update-ref refs/heads/something HEAD &&
375 test_path_is_file .git/refs/heads/something &&
376 git rev-parse refs/heads/something >expect &&
377 git maintenance run --task=pack-refs --auto &&
378 test_path_is_missing .git/refs/heads/something &&
379 git rev-parse refs/heads/something >actual &&
380 test_cmp expect actual
383 test_done