Debian Codegraph @

git/t/t0602-reffiles-fsck.sh

raw symbols SCIP
1 #!/bin/shscip-shell filesystem . . `/bin/sh`.
2
3 test_description='Test reffiles backend consistency check'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7 GIT_TEST_DEFAULT_REF_FORMAT=files
8 export GIT_TEST_DEFAULT_REF_FORMAT
9
10 . ./test-lib.shscip-shell source . . `./test-lib.sh`.
11
12 test_expect_success 'ref name should be checked' '
13 test_when_finished "rm -rf repo" &&
14 git init repo &&
15 branch_dir_prefix=.git/refs/heads &&
16 tag_dir_prefix=.git/refs/tags &&
17 (
18 cd repo &&
19
20 git commit --allow-empty -m initial &&
21 git checkout -b default-branch &&
22 git tag default-tag &&
23 git tag multi_hierarchy/default-tag &&
24
25 cp $branch_dir_prefix/default-branch $branch_dir_prefix/@ &&
26 git refs verify 2>err &&
27 test_must_be_empty err &&
28 rm $branch_dir_prefix/@ &&
29
30 cp $tag_dir_prefix/default-tag $tag_dir_prefix/tag-1.lock &&
31 git refs verify 2>err &&
32 rm $tag_dir_prefix/tag-1.lock &&
33 test_must_be_empty err &&
34
35 cp $tag_dir_prefix/default-tag $tag_dir_prefix/.lock &&
36 test_must_fail git refs verify 2>err &&
37 cat >expect <<-EOF &&
38 error: refs/tags/.lock: badRefName: invalid refname format
39 EOF
40 rm $tag_dir_prefix/.lock &&
41 test_cmp expect err &&
42
43 for refname in ".refname-starts-with-dot" "~refname-has-stride"
44 do
45 cp $branch_dir_prefix/default-branch "$branch_dir_prefix/$refname" &&
46 test_must_fail git refs verify 2>err &&
47 cat >expect <<-EOF &&
48 error: refs/heads/$refname: badRefName: invalid refname format
49 EOF
50 rm "$branch_dir_prefix/$refname" &&
51 test_cmp expect err || return 1
52 done &&
53
54 for refname in ".refname-starts-with-dot" "~refname-has-stride"
55 do
56 cp $tag_dir_prefix/default-tag "$tag_dir_prefix/$refname" &&
57 test_must_fail git refs verify 2>err &&
58 cat >expect <<-EOF &&
59 error: refs/tags/$refname: badRefName: invalid refname format
60 EOF
61 rm "$tag_dir_prefix/$refname" &&
62 test_cmp expect err || return 1
63 done &&
64
65 for refname in ".refname-starts-with-dot" "~refname-has-stride"
66 do
67 cp $tag_dir_prefix/multi_hierarchy/default-tag "$tag_dir_prefix/multi_hierarchy/$refname" &&
68 test_must_fail git refs verify 2>err &&
69 cat >expect <<-EOF &&
70 error: refs/tags/multi_hierarchy/$refname: badRefName: invalid refname format
71 EOF
72 rm "$tag_dir_prefix/multi_hierarchy/$refname" &&
73 test_cmp expect err || return 1
74 done &&
75
76 for refname in ".refname-starts-with-dot" "~refname-has-stride"
77 do
78 mkdir "$branch_dir_prefix/$refname" &&
79 cp $branch_dir_prefix/default-branch "$branch_dir_prefix/$refname/default-branch" &&
80 test_must_fail git refs verify 2>err &&
81 cat >expect <<-EOF &&
82 error: refs/heads/$refname/default-branch: badRefName: invalid refname format
83 EOF
84 rm -r "$branch_dir_prefix/$refname" &&
85 test_cmp expect err || return 1
86 done
87 )
88 '
89
90 test_expect_success 'ref name check should be adapted into fsck messages' '
91 test_when_finished "rm -rf repo" &&
92 git init repo &&
93 branch_dir_prefix=.git/refs/heads &&
94 (
95 cd repo &&
96 git commit --allow-empty -m initial &&
97 git checkout -b branch-1 &&
98
99 cp $branch_dir_prefix/branch-1 $branch_dir_prefix/.branch-1 &&
100 git -c fsck.badRefName=warn refs verify 2>err &&
101 cat >expect <<-EOF &&
102 warning: refs/heads/.branch-1: badRefName: invalid refname format
103 EOF
104 rm $branch_dir_prefix/.branch-1 &&
105 test_cmp expect err &&
106
107 cp $branch_dir_prefix/branch-1 $branch_dir_prefix/.branch-1 &&
108 git -c fsck.badRefName=ignore refs verify 2>err &&
109 test_must_be_empty err
110 )
111 '
112
113 test_expect_success 'no refs directory of worktree should not cause problems' '
114 test_when_finished "rm -rf repo" &&
115 git init repo &&
116 (
117 cd repo &&
118 test_commit initial &&
119 git worktree add --detach ./worktree &&
120
121 (
122 cd worktree &&
123 worktree_refdir="$(git rev-parse --git-dir)/refs" &&
124 # Simulate old directory layout
125 rmdir "$worktree_refdir" &&
126 git refs verify 2>err &&
127 test_must_be_empty err
128 )
129 )
130 '
131
132 test_expect_success 'ref name check should work for multiple worktrees' '
133 test_when_finished "rm -rf repo" &&
134 git init repo &&
135 (
136 cd repo &&
137 test_commit initial &&
138 git checkout -b branch-1 &&
139 test_commit second &&
140 git checkout -b branch-2 &&
141 test_commit third &&
142 git checkout -b branch-3 &&
143 git worktree add ./worktree-1 branch-1 &&
144 git worktree add ./worktree-2 branch-2 &&
145 worktree1_refdir_prefix=.git/worktrees/worktree-1/refs/worktree &&
146 worktree2_refdir_prefix=.git/worktrees/worktree-2/refs/worktree &&
147
148 (
149 cd worktree-1 &&
150 git update-ref refs/worktree/branch-4 refs/heads/branch-3
151 ) &&
152 (
153 cd worktree-2 &&
154 git update-ref refs/worktree/branch-4 refs/heads/branch-3
155 ) &&
156
157 cp $worktree1_refdir_prefix/branch-4 $worktree1_refdir_prefix/'\'' branch-5'\'' &&
158 cp $worktree2_refdir_prefix/branch-4 $worktree2_refdir_prefix/'\''~branch-6'\'' &&
159
160 test_must_fail git refs verify 2>err &&
161 cat >expect <<-EOF &&
162 error: worktrees/worktree-1/refs/worktree/ branch-5: badRefName: invalid refname format
163 error: worktrees/worktree-2/refs/worktree/~branch-6: badRefName: invalid refname format
164 EOF
165 sort err >sorted_err &&
166 test_cmp expect sorted_err &&
167
168 for worktree in "worktree-1" "worktree-2"
169 do
170 (
171 cd $worktree &&
172 test_must_fail git refs verify 2>err &&
173 cat >expect <<-EOF &&
174 error: worktrees/worktree-1/refs/worktree/ branch-5: badRefName: invalid refname format
175 error: worktrees/worktree-2/refs/worktree/~branch-6: badRefName: invalid refname format
176 EOF
177 sort err >sorted_err &&
178 test_cmp expect sorted_err || return 1
179 )
180 done
181 )
182 '
183
184 test_expect_success 'regular ref content should be checked (individual)' '
185 test_when_finished "rm -rf repo" &&
186 git init repo &&
187 branch_dir_prefix=.git/refs/heads &&
188 (
189 cd repo &&
190 test_commit default &&
191 mkdir -p "$branch_dir_prefix/a/b" &&
192
193 git refs verify 2>err &&
194 test_must_be_empty err &&
195
196 for bad_content in "$(git rev-parse main)x" "xfsazqfxcadas" "Xfsazqfxcadas"
197 do
198 printf "%s" $bad_content >$branch_dir_prefix/branch-bad &&
199 test_must_fail git refs verify 2>err &&
200 cat >expect <<-EOF &&
201 error: refs/heads/branch-bad: badRefContent: $bad_content
202 EOF
203 rm $branch_dir_prefix/branch-bad &&
204 test_cmp expect err || return 1
205 done &&
206
207 for bad_content in "$(git rev-parse main)x" "xfsazqfxcadas" "Xfsazqfxcadas"
208 do
209 printf "%s" $bad_content >$branch_dir_prefix/a/b/branch-bad &&
210 test_must_fail git refs verify 2>err &&
211 cat >expect <<-EOF &&
212 error: refs/heads/a/b/branch-bad: badRefContent: $bad_content
213 EOF
214 rm $branch_dir_prefix/a/b/branch-bad &&
215 test_cmp expect err || return 1
216 done &&
217
218 printf "%s" "$(git rev-parse main)" >$branch_dir_prefix/branch-no-newline &&
219 git refs verify 2>err &&
220 cat >expect <<-EOF &&
221 warning: refs/heads/branch-no-newline: refMissingNewline: misses LF at the end
222 EOF
223 rm $branch_dir_prefix/branch-no-newline &&
224 test_cmp expect err &&
225
226 for trailing_content in " garbage" " more garbage"
227 do
228 printf "%s" "$(git rev-parse main)$trailing_content" >$branch_dir_prefix/branch-garbage &&
229 git refs verify 2>err &&
230 cat >expect <<-EOF &&
231 warning: refs/heads/branch-garbage: trailingRefContent: has trailing garbage: '\''$trailing_content'\''
232 EOF
233 rm $branch_dir_prefix/branch-garbage &&
234 test_cmp expect err || return 1
235 done &&
236
237 printf "%s\n\n\n" "$(git rev-parse main)" >$branch_dir_prefix/branch-garbage-special &&
238 git refs verify 2>err &&
239 cat >expect <<-EOF &&
240 warning: refs/heads/branch-garbage-special: trailingRefContent: has trailing garbage: '\''
241
242
243 '\''
244 EOF
245 rm $branch_dir_prefix/branch-garbage-special &&
246 test_cmp expect err &&
247
248 printf "%s\n\n\n garbage" "$(git rev-parse main)" >$branch_dir_prefix/branch-garbage-special &&
249 git refs verify 2>err &&
250 cat >expect <<-EOF &&
251 warning: refs/heads/branch-garbage-special: trailingRefContent: has trailing garbage: '\''
252
253
254 garbage'\''
255 EOF
256 rm $branch_dir_prefix/branch-garbage-special &&
257 test_cmp expect err
258 )
259 '
260
261 test_expect_success 'regular ref content should be checked (aggregate)' '
262 test_when_finished "rm -rf repo" &&
263 git init repo &&
264 branch_dir_prefix=.git/refs/heads &&
265 tag_dir_prefix=.git/refs/tags &&
266 (
267 cd repo &&
268 test_commit default &&
269 mkdir -p "$branch_dir_prefix/a/b" &&
270
271 bad_content_1=$(git rev-parse main)x &&
272 bad_content_2=xfsazqfxcadas &&
273 bad_content_3=Xfsazqfxcadas &&
274 printf "%s" $bad_content_1 >$tag_dir_prefix/tag-bad-1 &&
275 printf "%s" $bad_content_2 >$tag_dir_prefix/tag-bad-2 &&
276 printf "%s" $bad_content_3 >$branch_dir_prefix/a/b/branch-bad &&
277 printf "%s" "$(git rev-parse main)" >$branch_dir_prefix/branch-no-newline &&
278 printf "%s garbage" "$(git rev-parse main)" >$branch_dir_prefix/branch-garbage &&
279
280 test_must_fail git refs verify 2>err &&
281 cat >expect <<-EOF &&
282 error: refs/heads/a/b/branch-bad: badRefContent: $bad_content_3
283 error: refs/tags/tag-bad-1: badRefContent: $bad_content_1
284 error: refs/tags/tag-bad-2: badRefContent: $bad_content_2
285 warning: refs/heads/branch-garbage: trailingRefContent: has trailing garbage: '\'' garbage'\''
286 warning: refs/heads/branch-no-newline: refMissingNewline: misses LF at the end
287 EOF
288 sort err >sorted_err &&
289 test_cmp expect sorted_err
290 )
291 '
292
293 test_expect_success 'textual symref content should be checked (individual)' '
294 test_when_finished "rm -rf repo" &&
295 git init repo &&
296 branch_dir_prefix=.git/refs/heads &&
297 (
298 cd repo &&
299 test_commit default &&
300 mkdir -p "$branch_dir_prefix/a/b" &&
301
302 for good_referent in "refs/heads/branch" "HEAD"
303 do
304 printf "ref: %s\n" $good_referent >$branch_dir_prefix/branch-good &&
305 git refs verify 2>err &&
306 rm $branch_dir_prefix/branch-good &&
307 test_must_be_empty err || return 1
308 done &&
309
310 for bad_referent in "refs/heads/.branch" "refs/heads/~branch" "refs/heads/?branch"
311 do
312 printf "ref: %s\n" $bad_referent >$branch_dir_prefix/branch-bad &&
313 test_must_fail git refs verify 2>err &&
314 cat >expect <<-EOF &&
315 error: refs/heads/branch-bad: badReferentName: points to invalid refname '\''$bad_referent'\''
316 EOF
317 rm $branch_dir_prefix/branch-bad &&
318 test_cmp expect err || return 1
319 done &&
320
321 printf "ref: refs/heads/branch" >$branch_dir_prefix/branch-no-newline &&
322 git refs verify 2>err &&
323 cat >expect <<-EOF &&
324 warning: refs/heads/branch-no-newline: refMissingNewline: misses LF at the end
325 EOF
326 rm $branch_dir_prefix/branch-no-newline &&
327 test_cmp expect err &&
328
329 printf "ref: refs/heads/branch " >$branch_dir_prefix/a/b/branch-trailing-1 &&
330 git refs verify 2>err &&
331 cat >expect <<-EOF &&
332 warning: refs/heads/a/b/branch-trailing-1: refMissingNewline: misses LF at the end
333 warning: refs/heads/a/b/branch-trailing-1: trailingRefContent: has trailing whitespaces or newlines
334 EOF
335 rm $branch_dir_prefix/a/b/branch-trailing-1 &&
336 test_cmp expect err &&
337
338 printf "ref: refs/heads/branch\n\n" >$branch_dir_prefix/a/b/branch-trailing-2 &&
339 git refs verify 2>err &&
340 cat >expect <<-EOF &&
341 warning: refs/heads/a/b/branch-trailing-2: trailingRefContent: has trailing whitespaces or newlines
342 EOF
343 rm $branch_dir_prefix/a/b/branch-trailing-2 &&
344 test_cmp expect err &&
345
346 printf "ref: refs/heads/branch \n" >$branch_dir_prefix/a/b/branch-trailing-3 &&
347 git refs verify 2>err &&
348 cat >expect <<-EOF &&
349 warning: refs/heads/a/b/branch-trailing-3: trailingRefContent: has trailing whitespaces or newlines
350 EOF
351 rm $branch_dir_prefix/a/b/branch-trailing-3 &&
352 test_cmp expect err &&
353
354 printf "ref: refs/heads/branch \n " >$branch_dir_prefix/a/b/branch-complicated &&
355 git refs verify 2>err &&
356 cat >expect <<-EOF &&
357 warning: refs/heads/a/b/branch-complicated: refMissingNewline: misses LF at the end
358 warning: refs/heads/a/b/branch-complicated: trailingRefContent: has trailing whitespaces or newlines
359 EOF
360 rm $branch_dir_prefix/a/b/branch-complicated &&
361 test_cmp expect err
362 )
363 '
364
365 test_expect_success 'textual symref content should be checked (aggregate)' '
366 test_when_finished "rm -rf repo" &&
367 git init repo &&
368 branch_dir_prefix=.git/refs/heads &&
369 tag_dir_prefix=.git/refs/tags &&
370 (
371 cd repo &&
372 test_commit default &&
373 mkdir -p "$branch_dir_prefix/a/b" &&
374
375 printf "ref: refs/heads/branch\n" >$branch_dir_prefix/branch-good &&
376 printf "ref: HEAD\n" >$branch_dir_prefix/branch-head &&
377 printf "ref: refs/heads/branch" >$branch_dir_prefix/branch-no-newline-1 &&
378 printf "ref: refs/heads/branch " >$branch_dir_prefix/a/b/branch-trailing-1 &&
379 printf "ref: refs/heads/branch\n\n" >$branch_dir_prefix/a/b/branch-trailing-2 &&
380 printf "ref: refs/heads/branch \n" >$branch_dir_prefix/a/b/branch-trailing-3 &&
381 printf "ref: refs/heads/branch \n " >$branch_dir_prefix/a/b/branch-complicated &&
382 printf "ref: refs/heads/.branch\n" >$branch_dir_prefix/branch-bad-1 &&
383
384 test_must_fail git refs verify 2>err &&
385 cat >expect <<-EOF &&
386 error: refs/heads/branch-bad-1: badReferentName: points to invalid refname '\''refs/heads/.branch'\''
387 warning: refs/heads/a/b/branch-complicated: refMissingNewline: misses LF at the end
388 warning: refs/heads/a/b/branch-complicated: trailingRefContent: has trailing whitespaces or newlines
389 warning: refs/heads/a/b/branch-trailing-1: refMissingNewline: misses LF at the end
390 warning: refs/heads/a/b/branch-trailing-1: trailingRefContent: has trailing whitespaces or newlines
391 warning: refs/heads/a/b/branch-trailing-2: trailingRefContent: has trailing whitespaces or newlines
392 warning: refs/heads/a/b/branch-trailing-3: trailingRefContent: has trailing whitespaces or newlines
393 warning: refs/heads/branch-no-newline-1: refMissingNewline: misses LF at the end
394 EOF
395 sort err >sorted_err &&
396 test_cmp expect sorted_err
397 )
398 '
399
400 test_expect_success 'the target of the textual symref should be checked' '
401 test_when_finished "rm -rf repo" &&
402 git init repo &&
403 branch_dir_prefix=.git/refs/heads &&
404 tag_dir_prefix=.git/refs/tags &&
405 (
406 cd repo &&
407 test_commit default &&
408 mkdir -p "$branch_dir_prefix/a/b" &&
409
410 for good_referent in "refs/heads/branch" "HEAD" "refs/tags/tag"
411 do
412 printf "ref: %s\n" $good_referent >$branch_dir_prefix/branch-good &&
413 git refs verify 2>err &&
414 rm $branch_dir_prefix/branch-good &&
415 test_must_be_empty err || return 1
416 done &&
417
418 for nonref_referent in "refs-back/heads/branch" "refs-back/tags/tag" "reflogs/refs/heads/branch"
419 do
420 printf "ref: %s\n" $nonref_referent >$branch_dir_prefix/branch-bad-1 &&
421 git refs verify 2>err &&
422 cat >expect <<-EOF &&
423 warning: refs/heads/branch-bad-1: symrefTargetIsNotARef: points to non-ref target '\''$nonref_referent'\''
424 EOF
425 rm $branch_dir_prefix/branch-bad-1 &&
426 test_cmp expect err || return 1
427 done
428 )
429 '
430
431 test_expect_success SYMLINKS 'symlink symref content should be checked' '
432 test_when_finished "rm -rf repo" &&
433 git init repo &&
434 branch_dir_prefix=.git/refs/heads &&
435 tag_dir_prefix=.git/refs/tags &&
436 (
437 cd repo &&
438 test_commit default &&
439 mkdir -p "$branch_dir_prefix/a/b" &&
440
441 ln -sf ./main $branch_dir_prefix/branch-symbolic-good &&
442 git refs verify 2>err &&
443 cat >expect <<-EOF &&
444 warning: refs/heads/branch-symbolic-good: symlinkRef: use deprecated symbolic link for symref
445 EOF
446 rm $branch_dir_prefix/branch-symbolic-good &&
447 test_cmp expect err &&
448
449 ln -sf ../../logs/branch-escape $branch_dir_prefix/branch-symbolic &&
450 git refs verify 2>err &&
451 cat >expect <<-EOF &&
452 warning: refs/heads/branch-symbolic: symlinkRef: use deprecated symbolic link for symref
453 warning: refs/heads/branch-symbolic: symrefTargetIsNotARef: points to non-ref target '\''logs/branch-escape'\''
454 EOF
455 rm $branch_dir_prefix/branch-symbolic &&
456 test_cmp expect err &&
457
458 ln -sf ./"branch " $branch_dir_prefix/branch-symbolic-bad &&
459 test_must_fail git refs verify 2>err &&
460 cat >expect <<-EOF &&
461 warning: refs/heads/branch-symbolic-bad: symlinkRef: use deprecated symbolic link for symref
462 error: refs/heads/branch-symbolic-bad: badReferentName: points to invalid refname '\''refs/heads/branch '\''
463 EOF
464 rm $branch_dir_prefix/branch-symbolic-bad &&
465 test_cmp expect err &&
466
467 ln -sf ./".tag" $tag_dir_prefix/tag-symbolic-1 &&
468 test_must_fail git refs verify 2>err &&
469 cat >expect <<-EOF &&
470 warning: refs/tags/tag-symbolic-1: symlinkRef: use deprecated symbolic link for symref
471 error: refs/tags/tag-symbolic-1: badReferentName: points to invalid refname '\''refs/tags/.tag'\''
472 EOF
473 rm $tag_dir_prefix/tag-symbolic-1 &&
474 test_cmp expect err
475 )
476 '
477
478 test_expect_success SYMLINKS 'symlink symref content should be checked (worktree)' '
479 test_when_finished "rm -rf repo" &&
480 git init repo &&
481 (
482 cd repo &&
483 test_commit default &&
484 git branch branch-1 &&
485 git branch branch-2 &&
486 git branch branch-3 &&
487 git worktree add ./worktree-1 branch-2 &&
488 git worktree add ./worktree-2 branch-3 &&
489 main_worktree_refdir_prefix=.git/refs/heads &&
490 worktree1_refdir_prefix=.git/worktrees/worktree-1/refs/worktree &&
491 worktree2_refdir_prefix=.git/worktrees/worktree-2/refs/worktree &&
492
493 (
494 cd worktree-1 &&
495 git update-ref refs/worktree/branch-4 refs/heads/branch-1
496 ) &&
497 (
498 cd worktree-2 &&
499 git update-ref refs/worktree/branch-4 refs/heads/branch-1
500 ) &&
501
502 ln -sf ../../../../refs/heads/good-branch $worktree1_refdir_prefix/branch-symbolic-good &&
503 git refs verify 2>err &&
504 cat >expect <<-EOF &&
505 warning: worktrees/worktree-1/refs/worktree/branch-symbolic-good: symlinkRef: use deprecated symbolic link for symref
506 EOF
507 rm $worktree1_refdir_prefix/branch-symbolic-good &&
508 test_cmp expect err &&
509
510 ln -sf ../../../../worktrees/worktree-1/good-branch $worktree2_refdir_prefix/branch-symbolic-good &&
511 git refs verify 2>err &&
512 cat >expect <<-EOF &&
513 warning: worktrees/worktree-2/refs/worktree/branch-symbolic-good: symlinkRef: use deprecated symbolic link for symref
514 EOF
515 rm $worktree2_refdir_prefix/branch-symbolic-good &&
516 test_cmp expect err &&
517
518 ln -sf ../../worktrees/worktree-2/good-branch $main_worktree_refdir_prefix/branch-symbolic-good &&
519 git refs verify 2>err &&
520 cat >expect <<-EOF &&
521 warning: refs/heads/branch-symbolic-good: symlinkRef: use deprecated symbolic link for symref
522 EOF
523 rm $main_worktree_refdir_prefix/branch-symbolic-good &&
524 test_cmp expect err &&
525
526 ln -sf ../../../../logs/branch-escape $worktree1_refdir_prefix/branch-symbolic &&
527 git refs verify 2>err &&
528 cat >expect <<-EOF &&
529 warning: worktrees/worktree-1/refs/worktree/branch-symbolic: symlinkRef: use deprecated symbolic link for symref
530 warning: worktrees/worktree-1/refs/worktree/branch-symbolic: symrefTargetIsNotARef: points to non-ref target '\''logs/branch-escape'\''
531 EOF
532 rm $worktree1_refdir_prefix/branch-symbolic &&
533 test_cmp expect err &&
534
535 for bad_referent_name in ".tag" "branch "
536 do
537 ln -sf ./"$bad_referent_name" $worktree1_refdir_prefix/bad-symbolic &&
538 test_must_fail git refs verify 2>err &&
539 cat >expect <<-EOF &&
540 warning: worktrees/worktree-1/refs/worktree/bad-symbolic: symlinkRef: use deprecated symbolic link for symref
541 error: worktrees/worktree-1/refs/worktree/bad-symbolic: badReferentName: points to invalid refname '\''worktrees/worktree-1/refs/worktree/$bad_referent_name'\''
542 EOF
543 rm $worktree1_refdir_prefix/bad-symbolic &&
544 test_cmp expect err &&
545
546 ln -sf ../../../../refs/heads/"$bad_referent_name" $worktree1_refdir_prefix/bad-symbolic &&
547 test_must_fail git refs verify 2>err &&
548 cat >expect <<-EOF &&
549 warning: worktrees/worktree-1/refs/worktree/bad-symbolic: symlinkRef: use deprecated symbolic link for symref
550 error: worktrees/worktree-1/refs/worktree/bad-symbolic: badReferentName: points to invalid refname '\''refs/heads/$bad_referent_name'\''
551 EOF
552 rm $worktree1_refdir_prefix/bad-symbolic &&
553 test_cmp expect err &&
554
555 ln -sf ./"$bad_referent_name" $worktree2_refdir_prefix/bad-symbolic &&
556 test_must_fail git refs verify 2>err &&
557 cat >expect <<-EOF &&
558 warning: worktrees/worktree-2/refs/worktree/bad-symbolic: symlinkRef: use deprecated symbolic link for symref
559 error: worktrees/worktree-2/refs/worktree/bad-symbolic: badReferentName: points to invalid refname '\''worktrees/worktree-2/refs/worktree/$bad_referent_name'\''
560 EOF
561 rm $worktree2_refdir_prefix/bad-symbolic &&
562 test_cmp expect err &&
563
564 ln -sf ../../../../refs/heads/"$bad_referent_name" $worktree2_refdir_prefix/bad-symbolic &&
565 test_must_fail git refs verify 2>err &&
566 cat >expect <<-EOF &&
567 warning: worktrees/worktree-2/refs/worktree/bad-symbolic: symlinkRef: use deprecated symbolic link for symref
568 error: worktrees/worktree-2/refs/worktree/bad-symbolic: badReferentName: points to invalid refname '\''refs/heads/$bad_referent_name'\''
569 EOF
570 rm $worktree2_refdir_prefix/bad-symbolic &&
571 test_cmp expect err || return 1
572 done
573 )
574 '
575
576 test_expect_success 'ref content checks should work with worktrees' '
577 test_when_finished "rm -rf repo" &&
578 git init repo &&
579 (
580 cd repo &&
581 test_commit default &&
582 git branch branch-1 &&
583 git branch branch-2 &&
584 git branch branch-3 &&
585 git worktree add ./worktree-1 branch-2 &&
586 git worktree add ./worktree-2 branch-3 &&
587 worktree1_refdir_prefix=.git/worktrees/worktree-1/refs/worktree &&
588 worktree2_refdir_prefix=.git/worktrees/worktree-2/refs/worktree &&
589
590 (
591 cd worktree-1 &&
592 git update-ref refs/worktree/branch-4 refs/heads/branch-1
593 ) &&
594 (
595 cd worktree-2 &&
596 git update-ref refs/worktree/branch-4 refs/heads/branch-1
597 ) &&
598
599 for bad_content in "$(git rev-parse HEAD)x" "xfsazqfxcadas" "Xfsazqfxcadas"
600 do
601 printf "%s" $bad_content >$worktree1_refdir_prefix/bad-branch-1 &&
602 test_must_fail git refs verify 2>err &&
603 cat >expect <<-EOF &&
604 error: worktrees/worktree-1/refs/worktree/bad-branch-1: badRefContent: $bad_content
605 EOF
606 rm $worktree1_refdir_prefix/bad-branch-1 &&
607 test_cmp expect err || return 1
608 done &&
609
610 for bad_content in "$(git rev-parse HEAD)x" "xfsazqfxcadas" "Xfsazqfxcadas"
611 do
612 printf "%s" $bad_content >$worktree2_refdir_prefix/bad-branch-2 &&
613 test_must_fail git refs verify 2>err &&
614 cat >expect <<-EOF &&
615 error: worktrees/worktree-2/refs/worktree/bad-branch-2: badRefContent: $bad_content
616 EOF
617 rm $worktree2_refdir_prefix/bad-branch-2 &&
618 test_cmp expect err || return 1
619 done &&
620
621 printf "%s" "$(git rev-parse HEAD)" >$worktree1_refdir_prefix/branch-no-newline &&
622 git refs verify 2>err &&
623 cat >expect <<-EOF &&
624 warning: worktrees/worktree-1/refs/worktree/branch-no-newline: refMissingNewline: misses LF at the end
625 EOF
626 rm $worktree1_refdir_prefix/branch-no-newline &&
627 test_cmp expect err &&
628
629 printf "%s garbage" "$(git rev-parse HEAD)" >$worktree1_refdir_prefix/branch-garbage &&
630 git refs verify 2>err &&
631 cat >expect <<-EOF &&
632 warning: worktrees/worktree-1/refs/worktree/branch-garbage: trailingRefContent: has trailing garbage: '\'' garbage'\''
633 EOF
634 rm $worktree1_refdir_prefix/branch-garbage &&
635 test_cmp expect err
636 )
637 '
638
639 test_expect_success SYMLINKS 'the filetype of packed-refs should be checked' '
640 test_when_finished "rm -rf repo" &&
641 git init repo &&
642 (
643 cd repo &&
644 test_commit default &&
645 git branch branch-1 &&
646 git branch branch-2 &&
647 git branch branch-3 &&
648 git pack-refs --all &&
649
650 mv .git/packed-refs .git/packed-refs-back &&
651 ln -sf packed-refs-back .git/packed-refs &&
652 test_must_fail git refs verify 2>err &&
653 cat >expect <<-EOF &&
654 error: packed-refs: badRefFiletype: not a regular file but a symlink
655 EOF
656 rm .git/packed-refs &&
657 test_cmp expect err &&
658
659 mkdir .git/packed-refs &&
660 test_must_fail git refs verify 2>err &&
661 cat >expect <<-EOF &&
662 error: packed-refs: badRefFiletype: not a regular file
663 EOF
664 rm -r .git/packed-refs &&
665 test_cmp expect err
666 )
667 '
668
669 test_expect_success 'empty packed-refs should be reported' '
670 test_when_finished "rm -rf repo" &&
671 git init repo &&
672 (
673 cd repo &&
674 test_commit default &&
675
676 >.git/packed-refs &&
677 git refs verify 2>err &&
678 cat >expect <<-EOF &&
679 warning: packed-refs: emptyPackedRefsFile: file is empty
680 EOF
681 rm .git/packed-refs &&
682 test_cmp expect err
683 )
684 '
685
686 test_expect_success 'packed-refs header should be checked' '
687 test_when_finished "rm -rf repo" &&
688 git init repo &&
689 (
690 cd repo &&
691 test_commit default &&
692
693 git refs verify 2>err &&
694 test_must_be_empty err &&
695
696 for bad_header in "# pack-refs wit: peeled fully-peeled sorted " \
697 "# pack-refs with traits: peeled fully-peeled sorted " \
698 "# pack-refs with a: peeled fully-peeled" \
699 "# pack-refs with:peeled fully-peeled sorted"
700 do
701 printf "%s\n" "$bad_header" >.git/packed-refs &&
702 test_must_fail git refs verify 2>err &&
703 cat >expect <<-EOF &&
704 error: packed-refs.header: badPackedRefHeader: '\''$bad_header'\'' does not start with '\''# pack-refs with: '\''
705 EOF
706 rm .git/packed-refs &&
707 test_cmp expect err || return 1
708 done
709 )
710 '
711
712 test_expect_success 'packed-refs missing header should not be reported' '
713 test_when_finished "rm -rf repo" &&
714 git init repo &&
715 (
716 cd repo &&
717 test_commit default &&
718
719 printf "$(git rev-parse HEAD) refs/heads/main\n" >.git/packed-refs &&
720 git refs verify 2>err &&
721 test_must_be_empty err
722 )
723 '
724
725 test_expect_success 'packed-refs unknown traits should not be reported' '
726 test_when_finished "rm -rf repo" &&
727 git init repo &&
728 (
729 cd repo &&
730 test_commit default &&
731
732 printf "# pack-refs with: peeled fully-peeled sorted foo\n" >.git/packed-refs &&
733 git refs verify 2>err &&
734 test_must_be_empty err
735 )
736 '
737
738 test_expect_success 'packed-refs content should be checked' '
739 test_when_finished "rm -rf repo" &&
740 git init repo &&
741 (
742 cd repo &&
743 test_commit default &&
744 git branch branch-1 &&
745 git branch branch-2 &&
746 git tag -a annotated-tag-1 -m tag-1 &&
747 git tag -a annotated-tag-2 -m tag-2 &&
748
749 branch_1_oid=$(git rev-parse branch-1) &&
750 branch_2_oid=$(git rev-parse branch-2) &&
751 tag_1_oid=$(git rev-parse annotated-tag-1) &&
752 tag_2_oid=$(git rev-parse annotated-tag-2) &&
753 tag_1_peeled_oid=$(git rev-parse annotated-tag-1^{}) &&
754 tag_2_peeled_oid=$(git rev-parse annotated-tag-2^{}) &&
755 short_oid=$(printf "%s" $tag_1_peeled_oid | cut -c 1-4) &&
756
757 cat >.git/packed-refs <<-EOF &&
758 # pack-refs with: peeled fully-peeled sorted
759 $short_oid refs/heads/branch-1
760 ${branch_1_oid}x
761 $branch_2_oid refs/heads/bad-branch
762 $branch_2_oid refs/heads/branch.
763 $tag_1_oid refs/tags/annotated-tag-3
764 ^$short_oid
765 $tag_2_oid refs/tags/annotated-tag-4.
766 ^$tag_2_peeled_oid garbage
767 EOF
768 test_must_fail git refs verify 2>err &&
769 cat >expect <<-EOF &&
770 error: packed-refs line 2: badPackedRefEntry: '\''$short_oid refs/heads/branch-1'\'' has invalid oid
771 error: packed-refs line 3: badPackedRefEntry: has no space after oid '\''$branch_1_oid'\'' but with '\''x'\''
772 error: packed-refs line 4: badRefName: has bad refname '\'' refs/heads/bad-branch'\''
773 error: packed-refs line 5: badRefName: has bad refname '\''refs/heads/branch.'\''
774 error: packed-refs line 7: badPackedRefEntry: '\''$short_oid'\'' has invalid peeled oid
775 error: packed-refs line 8: badRefName: has bad refname '\''refs/tags/annotated-tag-4.'\''
776 error: packed-refs line 9: badPackedRefEntry: has trailing garbage after peeled oid '\'' garbage'\''
777 EOF
778 test_cmp expect err
779 )
780 '
781
782 test_expect_success 'packed-ref with sorted trait should be checked' '
783 test_when_finished "rm -rf repo" &&
784 git init repo &&
785 (
786 cd repo &&
787 test_commit default &&
788 git branch branch-1 &&
789 git branch branch-2 &&
790 git tag -a annotated-tag-1 -m tag-1 &&
791 branch_1_oid=$(git rev-parse branch-1) &&
792 branch_2_oid=$(git rev-parse branch-2) &&
793 tag_1_oid=$(git rev-parse annotated-tag-1) &&
794 tag_1_peeled_oid=$(git rev-parse annotated-tag-1^{}) &&
795 refname1="refs/heads/main" &&
796 refname2="refs/heads/foo" &&
797 refname3="refs/tags/foo" &&
798
799 cat >.git/packed-refs <<-EOF &&
800 # pack-refs with: peeled fully-peeled sorted
801 EOF
802 git refs verify 2>err &&
803 rm .git/packed-refs &&
804 test_must_be_empty err &&
805
806 cat >.git/packed-refs <<-EOF &&
807 # pack-refs with: peeled fully-peeled sorted
808 $branch_2_oid $refname1
809 EOF
810 git refs verify 2>err &&
811 rm .git/packed-refs &&
812 test_must_be_empty err &&
813
814 cat >.git/packed-refs <<-EOF &&
815 # pack-refs with: peeled fully-peeled sorted
816 $branch_2_oid $refname1
817 $branch_1_oid $refname2
818 $tag_1_oid $refname3
819 EOF
820 test_must_fail git refs verify 2>err &&
821 cat >expect <<-EOF &&
822 error: packed-refs line 3: packedRefUnsorted: refname '\''$refname2'\'' is less than previous refname '\''$refname1'\''
823 EOF
824 rm .git/packed-refs &&
825 test_cmp expect err &&
826
827 cat >.git/packed-refs <<-EOF &&
828 # pack-refs with: peeled fully-peeled sorted
829 $tag_1_oid $refname3
830 ^$tag_1_peeled_oid
831 $branch_2_oid $refname2
832 EOF
833 test_must_fail git refs verify 2>err &&
834 cat >expect <<-EOF &&
835 error: packed-refs line 4: packedRefUnsorted: refname '\''$refname2'\'' is less than previous refname '\''$refname3'\''
836 EOF
837 rm .git/packed-refs &&
838 test_cmp expect err
839 )
840 '
841
842 test_expect_success 'packed-ref without sorted trait should not be checked' '
843 test_when_finished "rm -rf repo" &&
844 git init repo &&
845 (
846 cd repo &&
847 test_commit default &&
848 git branch branch-1 &&
849 git branch branch-2 &&
850 git tag -a annotated-tag-1 -m tag-1 &&
851 branch_1_oid=$(git rev-parse branch-1) &&
852 branch_2_oid=$(git rev-parse branch-2) &&
853 tag_1_oid=$(git rev-parse annotated-tag-1) &&
854 tag_1_peeled_oid=$(git rev-parse annotated-tag-1^{}) &&
855 refname1="refs/heads/main" &&
856 refname2="refs/heads/foo" &&
857 refname3="refs/tags/foo" &&
858
859 cat >.git/packed-refs <<-EOF &&
860 # pack-refs with: peeled fully-peeled
861 $branch_2_oid $refname1
862 $branch_1_oid $refname2
863 EOF
864 git refs verify 2>err &&
865 test_must_be_empty err
866 )
867 '
868
869 test_expect_success '--[no-]references option should apply to fsck' '
870 test_when_finished "rm -rf repo" &&
871 git init repo &&
872 branch_dir_prefix=.git/refs/heads &&
873 (
874 cd repo &&
875 test_commit default &&
876 for trailing_content in " garbage" " more garbage"
877 do
878 printf "%s" "$(git rev-parse HEAD)$trailing_content" >$branch_dir_prefix/branch-garbage &&
879 git fsck 2>err &&
880 cat >expect <<-EOF &&
881 warning: refs/heads/branch-garbage: trailingRefContent: has trailing garbage: '\''$trailing_content'\''
882 EOF
883 rm $branch_dir_prefix/branch-garbage &&
884 test_cmp expect err || return 1
885 done &&
886
887 for trailing_content in " garbage" " more garbage"
888 do
889 printf "%s" "$(git rev-parse HEAD)$trailing_content" >$branch_dir_prefix/branch-garbage &&
890 git fsck --references 2>err &&
891 cat >expect <<-EOF &&
892 warning: refs/heads/branch-garbage: trailingRefContent: has trailing garbage: '\''$trailing_content'\''
893 EOF
894 rm $branch_dir_prefix/branch-garbage &&
895 test_cmp expect err || return 1
896 done &&
897
898 for trailing_content in " garbage" " more garbage"
899 do
900 printf "%s" "$(git rev-parse HEAD)$trailing_content" >$branch_dir_prefix/branch-garbage &&
901 git fsck --no-references 2>err &&
902 rm $branch_dir_prefix/branch-garbage &&
903 test_must_be_empty err || return 1
904 done
905 )
906 '
907
908 test_done