Raw
1 #!/bin/sh
2
3 test_description='Test reffiles backend'
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.sh
11
12 test_expect_success 'setup' '
13 git commit --allow-empty -m Initial &&
14 C=$(git rev-parse HEAD) &&
15 git commit --allow-empty -m Second &&
16 D=$(git rev-parse HEAD) &&
17 git commit --allow-empty -m Third &&
18 E=$(git rev-parse HEAD)
19 '
20
21 test_expect_success 'empty directory should not fool rev-parse' '
22 prefix=refs/e-rev-parse &&
23 git update-ref $prefix/foo $C &&
24 git pack-refs --all &&
25 mkdir -p .git/$prefix/foo/bar/baz &&
26 echo "$C" >expected &&
27 git rev-parse $prefix/foo >actual &&
28 test_cmp expected actual
29 '
30
31 test_expect_success 'empty directory should not fool for-each-ref' '
32 prefix=refs/e-for-each-ref &&
33 git update-ref $prefix/foo $C &&
34 git for-each-ref $prefix >expected &&
35 git pack-refs --all &&
36 mkdir -p .git/$prefix/foo/bar/baz &&
37 git for-each-ref $prefix >actual &&
38 test_cmp expected actual
39 '
40
41 test_expect_success 'empty directory should not fool create' '
42 prefix=refs/e-create &&
43 mkdir -p .git/$prefix/foo/bar/baz &&
44 printf "create %s $C\n" $prefix/foo |
45 git update-ref --stdin
46 '
47
48 test_expect_success 'empty directory should not fool verify' '
49 prefix=refs/e-verify &&
50 git update-ref $prefix/foo $C &&
51 git pack-refs --all &&
52 mkdir -p .git/$prefix/foo/bar/baz &&
53 printf "verify %s $C\n" $prefix/foo |
54 git update-ref --stdin
55 '
56
57 test_expect_success 'empty directory should not fool 1-arg update' '
58 prefix=refs/e-update-1 &&
59 git update-ref $prefix/foo $C &&
60 git pack-refs --all &&
61 mkdir -p .git/$prefix/foo/bar/baz &&
62 printf "update %s $D\n" $prefix/foo |
63 git update-ref --stdin
64 '
65
66 test_expect_success 'empty directory should not fool 2-arg update' '
67 prefix=refs/e-update-2 &&
68 git update-ref $prefix/foo $C &&
69 git pack-refs --all &&
70 mkdir -p .git/$prefix/foo/bar/baz &&
71 printf "update %s $D $C\n" $prefix/foo |
72 git update-ref --stdin
73 '
74
75 test_expect_success 'empty directory should not fool 0-arg delete' '
76 prefix=refs/e-delete-0 &&
77 git update-ref $prefix/foo $C &&
78 git pack-refs --all &&
79 mkdir -p .git/$prefix/foo/bar/baz &&
80 printf "delete %s\n" $prefix/foo |
81 git update-ref --stdin
82 '
83
84 test_expect_success 'empty directory should not fool 1-arg delete' '
85 prefix=refs/e-delete-1 &&
86 git update-ref $prefix/foo $C &&
87 git pack-refs --all &&
88 mkdir -p .git/$prefix/foo/bar/baz &&
89 printf "delete %s $C\n" $prefix/foo |
90 git update-ref --stdin
91 '
92
93 test_expect_success 'non-empty directory blocks create' - <<\EOT
94 prefix=refs/ne-create &&
95 mkdir -p .git/$prefix/foo/bar &&
96 : >.git/$prefix/foo/bar/baz.lock &&
97 test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
98 cat >expected <<-EOF &&
99 fatal: cannot lock ref '$prefix/foo': there is a non-empty directory '.git/$prefix/foo' blocking reference '$prefix/foo'
100 EOF
101 printf "%s\n" "update $prefix/foo $C" |
102 test_must_fail git update-ref --stdin 2>output.err &&
103 test_cmp expected output.err &&
104 cat >expected <<-EOF &&
105 fatal: cannot lock ref '$prefix/foo': unable to resolve reference '$prefix/foo'
106 EOF
107 printf "%s\n" "update $prefix/foo $D $C" |
108 test_must_fail git update-ref --stdin 2>output.err &&
109 test_cmp expected output.err
110 EOT
111
112 test_expect_success 'broken reference blocks create' - <<\EOT
113 prefix=refs/broken-create &&
114 mkdir -p .git/$prefix &&
115 echo "gobbledigook" >.git/$prefix/foo &&
116 test_when_finished "rm -f .git/$prefix/foo" &&
117 cat >expected <<-EOF &&
118 fatal: cannot lock ref '$prefix/foo': unable to resolve reference '$prefix/foo': reference broken
119 EOF
120 printf "%s\n" "update $prefix/foo $C" |
121 test_must_fail git update-ref --stdin 2>output.err &&
122 test_cmp expected output.err &&
123 cat >expected <<-EOF &&
124 fatal: cannot lock ref '$prefix/foo': unable to resolve reference '$prefix/foo': reference broken
125 EOF
126 printf "%s\n" "update $prefix/foo $D $C" |
127 test_must_fail git update-ref --stdin 2>output.err &&
128 test_cmp expected output.err
129 EOT
130
131 test_expect_success 'non-empty directory blocks indirect create' - <<\EOT
132 prefix=refs/ne-indirect-create &&
133 git symbolic-ref $prefix/symref $prefix/foo &&
134 mkdir -p .git/$prefix/foo/bar &&
135 : >.git/$prefix/foo/bar/baz.lock &&
136 test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
137 cat >expected <<-EOF &&
138 fatal: cannot lock ref '$prefix/symref': there is a non-empty directory '.git/$prefix/foo' blocking reference '$prefix/foo'
139 EOF
140 printf "%s\n" "update $prefix/symref $C" |
141 test_must_fail git update-ref --stdin 2>output.err &&
142 test_cmp expected output.err &&
143 cat >expected <<-EOF &&
144 fatal: cannot lock ref '$prefix/symref': unable to resolve reference '$prefix/foo'
145 EOF
146 printf "%s\n" "update $prefix/symref $D $C" |
147 test_must_fail git update-ref --stdin 2>output.err &&
148 test_cmp expected output.err
149 EOT
150
151 test_expect_success 'broken reference blocks indirect create' - <<\EOT
152 prefix=refs/broken-indirect-create &&
153 git symbolic-ref $prefix/symref $prefix/foo &&
154 echo "gobbledigook" >.git/$prefix/foo &&
155 test_when_finished "rm -f .git/$prefix/foo" &&
156 cat >expected <<-EOF &&
157 fatal: cannot lock ref '$prefix/symref': unable to resolve reference '$prefix/foo': reference broken
158 EOF
159 printf "%s\n" "update $prefix/symref $C" |
160 test_must_fail git update-ref --stdin 2>output.err &&
161 test_cmp expected output.err &&
162 cat >expected <<-EOF &&
163 fatal: cannot lock ref '$prefix/symref': unable to resolve reference '$prefix/foo': reference broken
164 EOF
165 printf "%s\n" "update $prefix/symref $D $C" |
166 test_must_fail git update-ref --stdin 2>output.err &&
167 test_cmp expected output.err
168 EOT
169
170 test_expect_success 'no bogus intermediate values during delete' '
171 prefix=refs/slow-transaction &&
172 # Set up a reference with differing loose and packed versions:
173 git update-ref $prefix/foo $C &&
174 git pack-refs --all &&
175 git update-ref $prefix/foo $D &&
176 # Now try to update the reference, but hold the `packed-refs` lock
177 # for a while to see what happens while the process is blocked:
178 : >.git/packed-refs.lock &&
179 test_when_finished "rm -f .git/packed-refs.lock" &&
180 {
181 # Note: the following command is intentionally run in the
182 # background. We increase the timeout so that `update-ref`
183 # attempts to acquire the `packed-refs` lock for much longer
184 # than it takes for us to do the check then delete it:
185 git -c core.packedrefstimeout=30000 update-ref -d $prefix/foo &
186 } &&
187 pid2=$! &&
188 # Give update-ref plenty of time to get to the point where it tries
189 # to lock packed-refs:
190 sleep 1 &&
191 # Make sure that update-ref did not complete despite the lock:
192 kill -0 $pid2 &&
193 # Verify that the reference still has its old value:
194 sha1=$(git rev-parse --verify --quiet $prefix/foo || echo undefined) &&
195 case "$sha1" in
196 $D)
197 # This is what we hope for; it means that nothing
198 # user-visible has changed yet.
199 : ;;
200 undefined)
201 # This is not correct; it means the deletion has happened
202 # already even though update-ref should not have been
203 # able to acquire the lock yet.
204 echo "$prefix/foo deleted prematurely" &&
205 break
206 ;;
207 $C)
208 # This value should never be seen. Probably the loose
209 # reference has been deleted but the packed reference
210 # is still there:
211 echo "$prefix/foo incorrectly observed to be C" &&
212 break
213 ;;
214 *)
215 # WTF?
216 echo "unexpected value observed for $prefix/foo: $sha1" &&
217 break
218 ;;
219 esac >out &&
220 rm -f .git/packed-refs.lock &&
221 wait $pid2 &&
222 test_must_be_empty out &&
223 test_must_fail git rev-parse --verify --quiet $prefix/foo
224 '
225
226 test_expect_success 'delete fails cleanly if packed-refs file is locked' - <<\EOT
227 prefix=refs/locked-packed-refs &&
228 # Set up a reference with differing loose and packed versions:
229 git update-ref $prefix/foo $C &&
230 git pack-refs --all &&
231 git update-ref $prefix/foo $D &&
232 git for-each-ref $prefix >unchanged &&
233 # Now try to delete it while the `packed-refs` lock is held:
234 : >.git/packed-refs.lock &&
235 test_when_finished "rm -f .git/packed-refs.lock" &&
236 test_must_fail git update-ref -d $prefix/foo >out 2>err &&
237 git for-each-ref $prefix >actual &&
238 test_grep "Unable to create '.*packed-refs.lock': " err &&
239 test_cmp unchanged actual
240 EOT
241
242 test_expect_success 'delete fails cleanly if packed-refs.new write fails' '
243 # Setup and expectations are similar to the test above.
244 prefix=refs/failed-packed-refs &&
245 git update-ref $prefix/foo $C &&
246 git pack-refs --all &&
247 git update-ref $prefix/foo $D &&
248 git for-each-ref $prefix >unchanged &&
249 # This should not happen in practice, but it is an easy way to get a
250 # reliable error (we open with create_tempfile(), which uses O_EXCL).
251 : >.git/packed-refs.new &&
252 test_when_finished "rm -f .git/packed-refs.new" &&
253 test_must_fail git update-ref -d $prefix/foo &&
254 git for-each-ref $prefix >actual &&
255 test_cmp unchanged actual
256 '
257
258 RWT="test-tool ref-store worktree:wt"
259 RMAIN="test-tool ref-store worktree:main"
260
261 test_expect_success 'setup worktree' '
262 test_commit first &&
263 git worktree add -b wt-main wt &&
264 (
265 cd wt &&
266 test_commit second
267 )
268 '
269
270 # Some refs (refs/bisect/*, pseudorefs) are kept per worktree, so they should
271 # only appear in the for-each-reflog output if it is called from the correct
272 # worktree, which is exercised in this test. This test is poorly written for
273 # multiple reasons: 1) it creates invalidly formatted log entries. 2) it uses
274 # direct FS access for creating the reflogs. 3) PSEUDO-WT and refs/bisect/random
275 # do not create reflogs by default, so it is not testing a realistic scenario.
276 test_expect_success 'for_each_reflog()' '
277 echo $ZERO_OID >.git/logs/PSEUDO_MAIN_HEAD &&
278 mkdir -p .git/logs/refs/bisect &&
279 echo $ZERO_OID >.git/logs/refs/bisect/random &&
280
281 echo $ZERO_OID >.git/worktrees/wt/logs/PSEUDO_WT_HEAD &&
282 mkdir -p .git/worktrees/wt/logs/refs/bisect &&
283 echo $ZERO_OID >.git/worktrees/wt/logs/refs/bisect/wt-random &&
284
285 $RWT for-each-reflog >actual &&
286 cat >expected <<-\EOF &&
287 HEAD
288 PSEUDO_WT_HEAD
289 refs/bisect/wt-random
290 refs/heads/main
291 refs/heads/wt-main
292 EOF
293 test_cmp expected actual &&
294
295 $RMAIN for-each-reflog >actual &&
296 cat >expected <<-\EOF &&
297 HEAD
298 PSEUDO_MAIN_HEAD
299 refs/bisect/random
300 refs/heads/main
301 refs/heads/wt-main
302 EOF
303 test_cmp expected actual
304 '
305
306 # Triggering the bug detected by this test requires a newline to fall
307 # exactly BUFSIZ-1 bytes from the end of the file. We don't know
308 # what that value is, since it's platform dependent. However, if
309 # we choose some value N, we also catch any D which divides N evenly
310 # (since we will read backwards in chunks of D). So we choose 8K,
311 # which catches glibc (with an 8K BUFSIZ) and *BSD (1K).
312 #
313 # Each line is 114 characters, so we need 75 to still have a few before the
314 # last 8K. The 89-character padding on the final entry lines up our
315 # newline exactly.
316 test_expect_success SHA1 'parsing reverse reflogs at BUFSIZ boundaries' '
317 git checkout -b reflogskip &&
318 zf=$(test_oid zero_2) &&
319 ident="abc <xyz> 0000000001 +0000" &&
320 for i in $(test_seq 1 75); do
321 printf "$zf%02d $zf%02d %s\t" $i $(($i+1)) "$ident" &&
322 if test $i = 75; then
323 for j in $(test_seq 1 89); do
324 printf X || return 1
325 done
326 else
327 printf X
328 fi &&
329 printf "\n" || return 1
330 done >.git/logs/refs/heads/reflogskip &&
331 git rev-parse reflogskip@{73} >actual &&
332 echo ${zf}03 >expect &&
333 test_cmp expect actual
334 '
335
336 # This test takes a lock on an individual ref; this is not supported in
337 # reftable.
338 test_expect_success 'reflog expire operates on symref not referrent' '
339 git branch --create-reflog the_symref &&
340 git branch --create-reflog referrent &&
341 git update-ref referrent HEAD &&
342 git symbolic-ref refs/heads/the_symref refs/heads/referrent &&
343 test_when_finished "rm -f .git/refs/heads/referrent.lock" &&
344 touch .git/refs/heads/referrent.lock &&
345 git reflog expire --expire=all the_symref
346 '
347
348 test_expect_success 'empty reflog' '
349 test_when_finished "rm -rf empty" &&
350 git init empty &&
351 test_commit -C empty A &&
352 >empty/.git/logs/refs/heads/foo &&
353 git -C empty reflog expire --all 2>err &&
354 test_must_be_empty err
355 '
356
357 test_expect_success SYMLINKS 'ref resolution not confused by broken symlinks' '
358 ln -s does-not-exist .git/refs/heads/broken &&
359 test_must_fail git rev-parse --verify broken
360 '
361
362 test_expect_success 'log diagnoses bogus HEAD hash' '
363 git init empty &&
364 test_when_finished "rm -rf empty" &&
365 echo 1234abcd >empty/.git/refs/heads/main &&
366 test_must_fail git -C empty log 2>stderr &&
367 test_grep broken stderr
368 '
369
370 test_expect_success 'log diagnoses bogus HEAD symref' '
371 git init empty &&
372 test-tool -C empty ref-store main create-symref HEAD refs/heads/invalid.lock &&
373 test_must_fail git -C empty log 2>stderr &&
374 test_grep broken stderr &&
375 test_must_fail git -C empty log --default totally-bogus 2>stderr &&
376 test_grep broken stderr
377 '
378
379 test_expect_success 'empty directory removal' '
380 git branch d1/d2/r1 HEAD &&
381 git branch d1/r2 HEAD &&
382 test_path_is_file .git/refs/heads/d1/d2/r1 &&
383 test_path_is_file .git/logs/refs/heads/d1/d2/r1 &&
384 git branch -d d1/d2/r1 &&
385 test_must_fail git show-ref --verify -q refs/heads/d1/d2 &&
386 test_must_fail git show-ref --verify -q logs/refs/heads/d1/d2 &&
387 test_path_is_file .git/refs/heads/d1/r2 &&
388 test_path_is_file .git/logs/refs/heads/d1/r2
389 '
390
391 test_expect_success 'symref empty directory removal' '
392 git branch e1/e2/r1 HEAD &&
393 git branch e1/r2 HEAD &&
394 git checkout e1/e2/r1 &&
395 test_when_finished "git checkout main" &&
396 test_path_is_file .git/refs/heads/e1/e2/r1 &&
397 test_path_is_file .git/logs/refs/heads/e1/e2/r1 &&
398 git update-ref -d HEAD &&
399 test_must_fail git show-ref --verify -q refs/heads/e1/e2 &&
400 test_must_fail git show-ref --verify -q logs/refs/heads/e1/e2 &&
401 test_path_is_file .git/refs/heads/e1/r2 &&
402 test_path_is_file .git/logs/refs/heads/e1/r2 &&
403 test_path_is_file .git/logs/HEAD
404 '
405
406 test_expect_success 'directory not created deleting packed ref' '
407 git branch d1/d2/r1 HEAD &&
408 git pack-refs --all &&
409 test_path_is_missing .git/refs/heads/d1/d2 &&
410 git update-ref -d refs/heads/d1/d2/r1 &&
411 test_path_is_missing .git/refs/heads/d1/d2 &&
412 test_path_is_missing .git/refs/heads/d1
413 '
414
415 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
416 git branch --create-reflog u &&
417 mv .git/logs/refs/heads/u real-u &&
418 ln -s real-u .git/logs/refs/heads/u &&
419 test_must_fail git branch -m u v
420 '
421
422 test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' '
423 test_when_finished "rm -rf subdir" &&
424 git init --bare subdir &&
425
426 rm -rf subdir/refs subdir/objects subdir/packed-refs &&
427 ln -s ../.git/refs subdir/refs &&
428 ln -s ../.git/objects subdir/objects &&
429 ln -s ../.git/packed-refs subdir/packed-refs &&
430
431 git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
432 git rev-parse --absolute-git-dir >our.dir &&
433 ! test_cmp subdir.dir our.dir &&
434
435 git -C subdir log &&
436 git -C subdir branch rename-src &&
437 git rev-parse rename-src >expect &&
438 git -C subdir branch -m rename-src rename-dest &&
439 git rev-parse rename-dest >actual &&
440 test_cmp expect actual &&
441 git branch -D rename-dest
442 '
443
444 test_expect_success MINGW,SYMLINKS_WINDOWS 'rebase when .git/logs is a symlink' '
445 git checkout main &&
446 mv .git/logs actual_logs &&
447 cmd //c "mklink /D .git\logs ..\actual_logs" &&
448 git rebase -f HEAD^ &&
449 test -L .git/logs &&
450 rm .git/logs &&
451 mv actual_logs .git/logs
452 '
453
454 test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' '
455 umask 077 &&
456 git config core.sharedRepository group &&
457 git reflog expire --all &&
458 actual="$(ls -l .git/logs/refs/heads/main)" &&
459 case "$actual" in
460 -rw-rw-*)
461 : happy
462 ;;
463 *)
464 echo Ooops, .git/logs/refs/heads/main is not 066x [$actual]
465 false
466 ;;
467 esac
468 '
469
470 test_expect_success SYMLINKS,!MINGW 'symref transaction supports symlinks' '
471 test_when_finished "git symbolic-ref -d TEST_SYMREF_HEAD" &&
472 git update-ref refs/heads/new @ &&
473 test_config core.prefersymlinkrefs true &&
474 cat >stdin <<-EOF &&
475 start
476 symref-create TEST_SYMREF_HEAD refs/heads/new
477 prepare
478 commit
479 EOF
480 git update-ref --no-deref --stdin <stdin 2>err &&
481 if test_have_prereq WITH_BREAKING_CHANGES
482 then
483 test_path_is_file .git/TEST_SYMREF_HEAD &&
484 echo "ref: refs/heads/new" >expect &&
485 test_cmp expect .git/TEST_SYMREF_HEAD &&
486 test_must_be_empty err
487 else
488 test_path_is_symlink .git/TEST_SYMREF_HEAD &&
489 test "$(test_readlink .git/TEST_SYMREF_HEAD)" = refs/heads/new &&
490 cat >expect <<-EOF &&
491 warning: ${SQ}core.preferSymlinkRefs=true${SQ} is nominated for removal.
492 hint: The use of symbolic links for symbolic refs is deprecated
493 hint: and will be removed in Git 3.0. The configuration that
494 hint: tells Git to use them is thus going away. You can unset
495 hint: it with:
496 hint:
497 hint: git config unset core.preferSymlinkRefs
498 hint:
499 hint: Git will then use the textual symref format instead.
500 EOF
501 test_cmp expect err
502 fi
503 '
504
505 test_expect_success 'symref transaction supports false symlink config' '
506 test_when_finished "git symbolic-ref -d TEST_SYMREF_HEAD" &&
507 git update-ref refs/heads/new @ &&
508 test_config core.prefersymlinkrefs false &&
509 cat >stdin <<-EOF &&
510 start
511 symref-create TEST_SYMREF_HEAD refs/heads/new
512 prepare
513 commit
514 EOF
515 git update-ref --no-deref --stdin <stdin &&
516 test_path_is_file .git/TEST_SYMREF_HEAD &&
517 git symbolic-ref TEST_SYMREF_HEAD >actual &&
518 echo refs/heads/new >expect &&
519 test_cmp expect actual
520 '
521
522 test_done