3 test_description
='Test reffiles backend'
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
10 TEST_PASSES_SANITIZE_LEAK
=true
13 test_expect_success
'setup' '
14 git commit --allow-empty -m Initial &&
15 C=$(git rev-parse HEAD) &&
16 git commit --allow-empty -m Second &&
17 D=$(git rev-parse HEAD) &&
18 git commit --allow-empty -m Third &&
19 E=$(git rev-parse HEAD)
22 test_expect_success
'empty directory should not fool rev-parse' '
23 prefix=refs/e-rev-parse &&
24 git update-ref $prefix/foo $C &&
25 git pack-refs --all &&
26 mkdir -p .git/$prefix/foo/bar/baz &&
27 echo "$C" >expected &&
28 git rev-parse $prefix/foo >actual &&
29 test_cmp expected actual
32 test_expect_success
'empty directory should not fool for-each-ref' '
33 prefix=refs/e-for-each-ref &&
34 git update-ref $prefix/foo $C &&
35 git for-each-ref $prefix >expected &&
36 git pack-refs --all &&
37 mkdir -p .git/$prefix/foo/bar/baz &&
38 git for-each-ref $prefix >actual &&
39 test_cmp expected actual
42 test_expect_success
'empty directory should not fool create' '
43 prefix=refs/e-create &&
44 mkdir -p .git/$prefix/foo/bar/baz &&
45 printf "create %s $C\n" $prefix/foo |
46 git update-ref --stdin
49 test_expect_success
'empty directory should not fool verify' '
50 prefix=refs/e-verify &&
51 git update-ref $prefix/foo $C &&
52 git pack-refs --all &&
53 mkdir -p .git/$prefix/foo/bar/baz &&
54 printf "verify %s $C\n" $prefix/foo |
55 git update-ref --stdin
58 test_expect_success
'empty directory should not fool 1-arg update' '
59 prefix=refs/e-update-1 &&
60 git update-ref $prefix/foo $C &&
61 git pack-refs --all &&
62 mkdir -p .git/$prefix/foo/bar/baz &&
63 printf "update %s $D\n" $prefix/foo |
64 git update-ref --stdin
67 test_expect_success
'empty directory should not fool 2-arg update' '
68 prefix=refs/e-update-2 &&
69 git update-ref $prefix/foo $C &&
70 git pack-refs --all &&
71 mkdir -p .git/$prefix/foo/bar/baz &&
72 printf "update %s $D $C\n" $prefix/foo |
73 git update-ref --stdin
76 test_expect_success
'empty directory should not fool 0-arg delete' '
77 prefix=refs/e-delete-0 &&
78 git update-ref $prefix/foo $C &&
79 git pack-refs --all &&
80 mkdir -p .git/$prefix/foo/bar/baz &&
81 printf "delete %s\n" $prefix/foo |
82 git update-ref --stdin
85 test_expect_success
'empty directory should not fool 1-arg delete' '
86 prefix=refs/e-delete-1 &&
87 git update-ref $prefix/foo $C &&
88 git pack-refs --all &&
89 mkdir -p .git/$prefix/foo/bar/baz &&
90 printf "delete %s $C\n" $prefix/foo |
91 git update-ref --stdin
94 test_expect_success
'non-empty directory blocks create' - <<\EOT
95 prefix
=refs
/ne-create
&&
96 mkdir
-p .git
/$prefix/foo
/bar
&&
97 : >.git
/$prefix/foo
/bar
/baz.lock
&&
98 test_when_finished
"rm -f .git/$prefix/foo/bar/baz.lock" &&
99 cat >expected
<<-EOF &&
100 fatal: cannot lock ref '$prefix/foo': there is a non-empty directory '.git/$prefix/foo' blocking reference '$prefix/foo'
102 printf "%s\n" "update $prefix/foo $C" |
103 test_must_fail git update-ref
--stdin 2>output.err
&&
104 test_cmp expected output.err
&&
105 cat >expected
<<-EOF &&
106 fatal: cannot lock ref '$prefix/foo': unable to resolve reference '$prefix/foo'
108 printf "%s\n" "update $prefix/foo $D $C" |
109 test_must_fail git update-ref
--stdin 2>output.err
&&
110 test_cmp expected output.err
113 test_expect_success
'broken reference blocks create' - <<\EOT
114 prefix
=refs
/broken-create
&&
115 mkdir
-p .git
/$prefix &&
116 echo "gobbledigook" >.git
/$prefix/foo
&&
117 test_when_finished
"rm -f .git/$prefix/foo" &&
118 cat >expected
<<-EOF &&
119 fatal: cannot lock ref '$prefix/foo': unable to resolve reference '$prefix/foo': reference broken
121 printf "%s\n" "update $prefix/foo $C" |
122 test_must_fail git update-ref
--stdin 2>output.err
&&
123 test_cmp expected output.err
&&
124 cat >expected
<<-EOF &&
125 fatal: cannot lock ref '$prefix/foo': unable to resolve reference '$prefix/foo': reference broken
127 printf "%s\n" "update $prefix/foo $D $C" |
128 test_must_fail git update-ref
--stdin 2>output.err
&&
129 test_cmp expected output.err
132 test_expect_success
'non-empty directory blocks indirect create' - <<\EOT
133 prefix
=refs
/ne-indirect-create
&&
134 git symbolic-ref
$prefix/symref
$prefix/foo
&&
135 mkdir
-p .git
/$prefix/foo
/bar
&&
136 : >.git
/$prefix/foo
/bar
/baz.lock
&&
137 test_when_finished
"rm -f .git/$prefix/foo/bar/baz.lock" &&
138 cat >expected
<<-EOF &&
139 fatal: cannot lock ref '$prefix/symref': there is a non-empty directory '.git/$prefix/foo' blocking reference '$prefix/foo'
141 printf "%s\n" "update $prefix/symref $C" |
142 test_must_fail git update-ref
--stdin 2>output.err
&&
143 test_cmp expected output.err
&&
144 cat >expected
<<-EOF &&
145 fatal: cannot lock ref '$prefix/symref': unable to resolve reference '$prefix/foo'
147 printf "%s\n" "update $prefix/symref $D $C" |
148 test_must_fail git update-ref
--stdin 2>output.err
&&
149 test_cmp expected output.err
152 test_expect_success
'broken reference blocks indirect create' - <<\EOT
153 prefix
=refs
/broken-indirect-create
&&
154 git symbolic-ref
$prefix/symref
$prefix/foo
&&
155 echo "gobbledigook" >.git
/$prefix/foo
&&
156 test_when_finished
"rm -f .git/$prefix/foo" &&
157 cat >expected
<<-EOF &&
158 fatal: cannot lock ref '$prefix/symref': unable to resolve reference '$prefix/foo': reference broken
160 printf "%s\n" "update $prefix/symref $C" |
161 test_must_fail git update-ref
--stdin 2>output.err
&&
162 test_cmp expected output.err
&&
163 cat >expected
<<-EOF &&
164 fatal: cannot lock ref '$prefix/symref': unable to resolve reference '$prefix/foo': reference broken
166 printf "%s\n" "update $prefix/symref $D $C" |
167 test_must_fail git update-ref
--stdin 2>output.err
&&
168 test_cmp expected output.err
171 test_expect_success
'no bogus intermediate values during delete' '
172 prefix=refs/slow-transaction &&
173 # Set up a reference with differing loose and packed versions:
174 git update-ref $prefix/foo $C &&
175 git pack-refs --all &&
176 git update-ref $prefix/foo $D &&
177 # Now try to update the reference, but hold the `packed-refs` lock
178 # for a while to see what happens while the process is blocked:
179 : >.git/packed-refs.lock &&
180 test_when_finished "rm -f .git/packed-refs.lock" &&
182 # Note: the following command is intentionally run in the
183 # background. We increase the timeout so that `update-ref`
184 # attempts to acquire the `packed-refs` lock for much longer
185 # than it takes for us to do the check then delete it:
186 git -c core.packedrefstimeout=30000 update-ref -d $prefix/foo &
189 # Give update-ref plenty of time to get to the point where it tries
190 # to lock packed-refs:
192 # Make sure that update-ref did not complete despite the lock:
194 # Verify that the reference still has its old value:
195 sha1=$(git rev-parse --verify --quiet $prefix/foo || echo undefined) &&
198 # This is what we hope for; it means that nothing
199 # user-visible has changed yet.
202 # This is not correct; it means the deletion has happened
203 # already even though update-ref should not have been
204 # able to acquire the lock yet.
205 echo "$prefix/foo deleted prematurely" &&
209 # This value should never be seen. Probably the loose
210 # reference has been deleted but the packed reference
212 echo "$prefix/foo incorrectly observed to be C" &&
217 echo "unexpected value observed for $prefix/foo: $sha1" &&
221 rm -f .git/packed-refs.lock &&
223 test_must_be_empty out &&
224 test_must_fail git rev-parse --verify --quiet $prefix/foo
227 test_expect_success
'delete fails cleanly if packed-refs file is locked' - <<\EOT
228 prefix
=refs
/locked-packed-refs
&&
229 # Set up a reference with differing loose and packed versions:
230 git update-ref
$prefix/foo
$C &&
231 git pack-refs
--all &&
232 git update-ref
$prefix/foo
$D &&
233 git for-each-ref
$prefix >unchanged
&&
234 # Now try to delete it while the `packed-refs` lock is held:
235 : >.git
/packed-refs.lock
&&
236 test_when_finished
"rm -f .git/packed-refs.lock" &&
237 test_must_fail git update-ref
-d $prefix/foo
>out
2>err
&&
238 git for-each-ref
$prefix >actual
&&
239 test_grep
"Unable to create '.*packed-refs.lock': " err
&&
240 test_cmp unchanged actual
243 test_expect_success
'delete fails cleanly if packed-refs.new write fails' '
244 # Setup and expectations are similar to the test above.
245 prefix=refs/failed-packed-refs &&
246 git update-ref $prefix/foo $C &&
247 git pack-refs --all &&
248 git update-ref $prefix/foo $D &&
249 git for-each-ref $prefix >unchanged &&
250 # This should not happen in practice, but it is an easy way to get a
251 # reliable error (we open with create_tempfile(), which uses O_EXCL).
252 : >.git/packed-refs.new &&
253 test_when_finished "rm -f .git/packed-refs.new" &&
254 test_must_fail git update-ref -d $prefix/foo &&
255 git for-each-ref $prefix >actual &&
256 test_cmp unchanged actual
259 RWT
="test-tool ref-store worktree:wt"
260 RMAIN
="test-tool ref-store worktree:main"
262 test_expect_success
'setup worktree' '
264 git worktree add -b wt-main wt &&
271 # Some refs (refs/bisect/*, pseudorefs) are kept per worktree, so they should
272 # only appear in the for-each-reflog output if it is called from the correct
273 # worktree, which is exercised in this test. This test is poorly written for
274 # mulitple reasons: 1) it creates invalidly formatted log entres. 2) it uses
275 # direct FS access for creating the reflogs. 3) PSEUDO-WT and refs/bisect/random
276 # do not create reflogs by default, so it is not testing a realistic scenario.
277 test_expect_success
'for_each_reflog()' '
278 echo $ZERO_OID >.git/logs/PSEUDO_MAIN_HEAD &&
279 mkdir -p .git/logs/refs/bisect &&
280 echo $ZERO_OID >.git/logs/refs/bisect/random &&
282 echo $ZERO_OID >.git/worktrees/wt/logs/PSEUDO_WT_HEAD &&
283 mkdir -p .git/worktrees/wt/logs/refs/bisect &&
284 echo $ZERO_OID >.git/worktrees/wt/logs/refs/bisect/wt-random &&
286 $RWT for-each-reflog >actual &&
287 cat >expected <<-\EOF &&
290 refs/bisect/wt-random
294 test_cmp expected actual &&
296 $RMAIN for-each-reflog >actual &&
297 cat >expected <<-\EOF &&
304 test_cmp expected actual
307 # Triggering the bug detected by this test requires a newline to fall
308 # exactly BUFSIZ-1 bytes from the end of the file. We don't know
309 # what that value is, since it's platform dependent. However, if
310 # we choose some value N, we also catch any D which divides N evenly
311 # (since we will read backwards in chunks of D). So we choose 8K,
312 # which catches glibc (with an 8K BUFSIZ) and *BSD (1K).
314 # Each line is 114 characters, so we need 75 to still have a few before the
315 # last 8K. The 89-character padding on the final entry lines up our
317 test_expect_success SHA1
'parsing reverse reflogs at BUFSIZ boundaries' '
318 git checkout -b reflogskip &&
319 zf=$(test_oid zero_2) &&
320 ident="abc <xyz> 0000000001 +0000" &&
321 for i in $(test_seq 1 75); do
322 printf "$zf%02d $zf%02d %s\t" $i $(($i+1)) "$ident" &&
323 if test $i = 75; then
324 for j in $(test_seq 1 89); do
330 printf "\n" || return 1
331 done >.git/logs/refs/heads/reflogskip &&
332 git rev-parse reflogskip@{73} >actual &&
333 echo ${zf}03 >expect &&
334 test_cmp expect actual
337 # This test takes a lock on an individual ref; this is not supported in
339 test_expect_success
'reflog expire operates on symref not referrent' '
340 git branch --create-reflog the_symref &&
341 git branch --create-reflog referrent &&
342 git update-ref referrent HEAD &&
343 git symbolic-ref refs/heads/the_symref refs/heads/referrent &&
344 test_when_finished "rm -f .git/refs/heads/referrent.lock" &&
345 touch .git/refs/heads/referrent.lock &&
346 git reflog expire --expire=all the_symref
349 test_expect_success
'empty reflog' '
350 test_when_finished "rm -rf empty" &&
352 test_commit -C empty A &&
353 >empty/.git/logs/refs/heads/foo &&
354 git -C empty reflog expire --all 2>err &&
355 test_must_be_empty err
358 test_expect_success SYMLINKS
'ref resolution not confused by broken symlinks' '
359 ln -s does-not-exist .git/refs/heads/broken &&
360 test_must_fail git rev-parse --verify broken
363 test_expect_success
'log diagnoses bogus HEAD hash' '
365 test_when_finished "rm -rf empty" &&
366 echo 1234abcd >empty/.git/refs/heads/main &&
367 test_must_fail git -C empty log 2>stderr &&
368 test_grep broken stderr
371 test_expect_success
'log diagnoses bogus HEAD symref' '
373 test-tool -C empty ref-store main create-symref HEAD refs/heads/invalid.lock &&
374 test_must_fail git -C empty log 2>stderr &&
375 test_grep broken stderr &&
376 test_must_fail git -C empty log --default totally-bogus 2>stderr &&
377 test_grep broken stderr
380 test_expect_success
'empty directory removal' '
381 git branch d1/d2/r1 HEAD &&
382 git branch d1/r2 HEAD &&
383 test_path_is_file .git/refs/heads/d1/d2/r1 &&
384 test_path_is_file .git/logs/refs/heads/d1/d2/r1 &&
385 git branch -d d1/d2/r1 &&
386 test_must_fail git show-ref --verify -q refs/heads/d1/d2 &&
387 test_must_fail git show-ref --verify -q logs/refs/heads/d1/d2 &&
388 test_path_is_file .git/refs/heads/d1/r2 &&
389 test_path_is_file .git/logs/refs/heads/d1/r2
392 test_expect_success
'symref empty directory removal' '
393 git branch e1/e2/r1 HEAD &&
394 git branch e1/r2 HEAD &&
395 git checkout e1/e2/r1 &&
396 test_when_finished "git checkout main" &&
397 test_path_is_file .git/refs/heads/e1/e2/r1 &&
398 test_path_is_file .git/logs/refs/heads/e1/e2/r1 &&
399 git update-ref -d HEAD &&
400 test_must_fail git show-ref --verify -q refs/heads/e1/e2 &&
401 test_must_fail git show-ref --verify -q logs/refs/heads/e1/e2 &&
402 test_path_is_file .git/refs/heads/e1/r2 &&
403 test_path_is_file .git/logs/refs/heads/e1/r2 &&
404 test_path_is_file .git/logs/HEAD
407 test_expect_success
'directory not created deleting packed ref' '
408 git branch d1/d2/r1 HEAD &&
409 git pack-refs --all &&
410 test_path_is_missing .git/refs/heads/d1/d2 &&
411 git update-ref -d refs/heads/d1/d2/r1 &&
412 test_path_is_missing .git/refs/heads/d1/d2 &&
413 test_path_is_missing .git/refs/heads/d1
416 test_expect_success SYMLINKS
'git branch -m u v should fail when the reflog for u is a symlink' '
417 git branch --create-reflog u &&
418 mv .git/logs/refs/heads/u real-u &&
419 ln -s real-u .git/logs/refs/heads/u &&
420 test_must_fail git branch -m u v
423 test_expect_success SYMLINKS
'git branch -m with symlinked .git/refs' '
424 test_when_finished "rm -rf subdir" &&
425 git init --bare subdir &&
427 rm -rf subdir/refs subdir/objects subdir/packed-refs &&
428 ln -s ../.git/refs subdir/refs &&
429 ln -s ../.git/objects subdir/objects &&
430 ln -s ../.git/packed-refs subdir/packed-refs &&
432 git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
433 git rev-parse --absolute-git-dir >our.dir &&
434 ! test_cmp subdir.dir our.dir &&
437 git -C subdir branch rename-src &&
438 git rev-parse rename-src >expect &&
439 git -C subdir branch -m rename-src rename-dest &&
440 git rev-parse rename-dest >actual &&
441 test_cmp expect actual &&
442 git branch -D rename-dest
445 test_expect_success MINGW
,SYMLINKS_WINDOWS
'rebase when .git/logs is a symlink' '
447 mv .git/logs actual_logs &&
448 cmd //c "mklink /D .git\logs ..\actual_logs" &&
449 git rebase -f HEAD^ &&
452 mv actual_logs .git/logs
455 test_expect_success POSIXPERM
'git reflog expire honors core.sharedRepository' '
457 git config core.sharedRepository group &&
458 git reflog expire --all &&
459 actual="$(ls -l .git/logs/refs/heads/main)" &&
465 echo Ooops, .git/logs/refs/heads/main is not 066x [$actual]
471 test_expect_success SYMLINKS
'symref transaction supports symlinks' '
472 test_when_finished "git symbolic-ref -d TEST_SYMREF_HEAD" &&
473 git update-ref refs/heads/new @ &&
474 test_config core.prefersymlinkrefs true &&
477 symref-create TEST_SYMREF_HEAD refs/heads/new
481 git update-ref --no-deref --stdin <stdin &&
482 test_path_is_symlink .git/TEST_SYMREF_HEAD &&
483 test "$(test_readlink .git/TEST_SYMREF_HEAD)" = refs/heads/new
486 test_expect_success
'symref transaction supports false symlink config' '
487 test_when_finished "git symbolic-ref -d TEST_SYMREF_HEAD" &&
488 git update-ref refs/heads/new @ &&
489 test_config core.prefersymlinkrefs false &&
492 symref-create TEST_SYMREF_HEAD refs/heads/new
496 git update-ref --no-deref --stdin <stdin &&
497 test_path_is_file .git/TEST_SYMREF_HEAD &&
498 git symbolic-ref TEST_SYMREF_HEAD >actual &&
499 echo refs/heads/new >expect &&
500 test_cmp expect actual