]> git.ipfire.org Git - thirdparty/git.git/blob - t/t0601-reffiles-pack-refs.sh
test_i18ngrep: hard deprecate and forbid its use
[thirdparty/git.git] / t / t0601-reffiles-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 if ! test_have_prereq REFFILES
19 then
20 skip_all='skipping reffiles specific tests'
21 test_done
22 fi
23
24 test_expect_success 'enable reflogs' '
25 git config core.logallrefupdates true
26 '
27
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)
33 '
34
35 test_expect_success 'pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE)' '
36 N=`find .git/refs -type f | wc -l` &&
37 test "$N" != 0 &&
38 test-tool ref-store main pack-refs PACK_REFS_PRUNE,PACK_REFS_ALL &&
39 N=`find .git/refs -type f` &&
40 test -z "$N"
41 '
42
43 SHA1=
44
45 test_expect_success 'see if git show-ref works as expected' '
46 git branch a &&
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
51 '
52
53 test_expect_success 'see if a branch still exists when packed' '
54 git branch b &&
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
60 '
61
62 test_expect_success 'git branch c/d should barf if branch c exists' '
63 git branch c &&
64 git pack-refs --all &&
65 rm -f .git/refs/heads/c &&
66 test_must_fail git branch c/d
67 '
68
69 test_expect_success 'see if a branch still exists after git pack-refs --prune' '
70 git branch e &&
71 git pack-refs --all --prune &&
72 echo "$SHA1 refs/heads/e" >expect &&
73 git show-ref e >result &&
74 test_cmp expect result
75 '
76
77 test_expect_success 'see if git pack-refs --prune remove ref files' '
78 git branch f &&
79 git pack-refs --all --prune &&
80 ! test -f .git/refs/heads/f
81 '
82
83 test_expect_success 'see if git pack-refs --prune removes empty dirs' '
84 git branch r/s/t &&
85 git pack-refs --all --prune &&
86 ! test -e .git/refs/heads/r
87 '
88
89 test_expect_success 'git branch g should work when git branch g/h has been deleted' '
90 git branch g/h &&
91 git pack-refs --all --prune &&
92 git branch -d g/h &&
93 git branch g &&
94 git pack-refs --all &&
95 git branch -d g
96 '
97
98 test_expect_success 'git branch i/j/k should barf if branch i exists' '
99 git branch i &&
100 git pack-refs --all --prune &&
101 test_must_fail git branch i/j/k
102 '
103
104 test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' '
105 git branch k/l &&
106 git branch k/lm &&
107 git branch -d k/l &&
108 git branch k/l/m &&
109 git branch -d k/l/m &&
110 git branch -d k/lm &&
111 git branch k
112 '
113
114 test_expect_success 'test git branch n after some branch deletion and pruning' '
115 git branch n/o &&
116 git branch n/op &&
117 git branch -d n/o &&
118 git branch n/o/p &&
119 git branch -d n/op &&
120 git pack-refs --all --prune &&
121 git branch -d n/o/p &&
122 git branch n
123 '
124
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'
133
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'
140
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'
149
150 test_expect_success 'test --no-include refs clears included refs' '
151 git branch pack1 &&
152 git branch pack2 &&
153 git pack-refs --include "refs/heads/pack*" --no-include &&
154 test -f .git/refs/heads/pack1 &&
155 test -f .git/refs/heads/pack2'
156
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'
161
162 test_expect_success 'see if up-to-date packed refs are preserved' '
163 git branch q &&
164 git pack-refs --all --prune &&
165 git update-ref refs/heads/q refs/heads/q &&
166 ! test -f .git/refs/heads/q
167 '
168
169 test_expect_success 'pack, prune and repack' '
170 git tag foo &&
171 git pack-refs --all --prune &&
172 git show-ref >all-of-them &&
173 git pack-refs &&
174 git show-ref >again &&
175 test_cmp all-of-them again
176 '
177
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
186 '
187
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^ &&
193 git checkout main &&
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
198 '
199
200 test_expect_success 'delete ref while another dangling packed ref' '
201 git branch lamb &&
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
209 '
210
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
216 '
217
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
223 '
224
225 test_expect_success 'disable reflogs' '
226 git config core.logallrefupdates false &&
227 rm -rf .git/logs
228 '
229
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
235 '
236
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
240 '
241
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
245 '
246
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
250 '
251
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
259 '
260
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
268 '
269
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
277 '
278
279 test_expect_success 'timeout if packed-refs.lock exists' '
280 LOCK=.git/packed-refs.lock &&
281 >"$LOCK" &&
282 test_when_finished "rm -f $LOCK" &&
283 test_must_fail git pack-refs --all --prune
284 '
285
286 test_expect_success 'retry acquiring packed-refs.lock' '
287 LOCK=.git/packed-refs.lock &&
288 >"$LOCK" &&
289 test_when_finished "wait && rm -f $LOCK" &&
290 {
291 ( sleep 1 && rm -f $LOCK ) &
292 } &&
293 git -c core.packedrefstimeout=3000 pack-refs --all --prune
294 '
295
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"
309 '
310
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
313 # worktrees.
314 test_expect_success 'refs/worktree must not be packed' '
315 test_commit initial &&
316 test_commit wt1 &&
317 test_commit wt2 &&
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
329 '
330
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
343 EOF
344 echo $missing >expect &&
345 git rev-parse refs/heads/main >actual &&
346 test_cmp expect actual
347 '
348
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
353 '
354
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
359 '
360
361 test_done