Merge branch 'ps/pack-refs-auto' into jt/reftable-geometric-compaction
[git.git] / t / t0610-reftable-basics.sh
blob931d888bbbcbc426dcf51a0478842fd4787bee1d
1 #!/bin/sh
3 # Copyright (c) 2020 Google LLC
6 test_description='reftable basics'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 . ./test-lib.sh
12 if ! test_have_prereq REFTABLE
13 then
14 skip_all='skipping reftable tests; set GIT_TEST_DEFAULT_REF_FORMAT=reftable'
15 test_done
18 INVALID_OID=$(test_oid 001)
20 test_expect_success 'init: creates basic reftable structures' '
21 test_when_finished "rm -rf repo" &&
22 git init repo &&
23 test_path_is_dir repo/.git/reftable &&
24 test_path_is_file repo/.git/reftable/tables.list &&
25 echo reftable >expect &&
26 git -C repo rev-parse --show-ref-format >actual &&
27 test_cmp expect actual
30 test_expect_success 'init: sha256 object format via environment variable' '
31 test_when_finished "rm -rf repo" &&
32 GIT_DEFAULT_HASH=sha256 git init repo &&
33 cat >expect <<-EOF &&
34 sha256
35 reftable
36 EOF
37 git -C repo rev-parse --show-object-format --show-ref-format >actual &&
38 test_cmp expect actual
41 test_expect_success 'init: sha256 object format via option' '
42 test_when_finished "rm -rf repo" &&
43 git init --object-format=sha256 repo &&
44 cat >expect <<-EOF &&
45 sha256
46 reftable
47 EOF
48 git -C repo rev-parse --show-object-format --show-ref-format >actual &&
49 test_cmp expect actual
52 test_expect_success 'init: reinitializing reftable backend succeeds' '
53 test_when_finished "rm -rf repo" &&
54 git init repo &&
55 test_commit -C repo A &&
57 git -C repo for-each-ref >expect &&
58 git init --ref-format=reftable repo &&
59 git -C repo for-each-ref >actual &&
60 test_cmp expect actual
63 test_expect_success 'init: reinitializing files with reftable backend fails' '
64 test_when_finished "rm -rf repo" &&
65 git init --ref-format=files repo &&
66 test_commit -C repo file &&
68 cp repo/.git/HEAD expect &&
69 test_must_fail git init --ref-format=reftable repo &&
70 test_cmp expect repo/.git/HEAD
73 test_expect_success 'init: reinitializing reftable with files backend fails' '
74 test_when_finished "rm -rf repo" &&
75 git init --ref-format=reftable repo &&
76 test_commit -C repo file &&
78 cp repo/.git/HEAD expect &&
79 test_must_fail git init --ref-format=files repo &&
80 test_cmp expect repo/.git/HEAD
83 test_expect_perms () {
84 local perms="$1"
85 local file="$2"
86 local actual=$(ls -l "$file") &&
88 case "$actual" in
89 $perms*)
90 : happy
93 echo "$(basename $2) is not $perms but $actual"
94 false
96 esac
99 for umask in 002 022
101 test_expect_success POSIXPERM 'init: honors core.sharedRepository' '
102 test_when_finished "rm -rf repo" &&
104 umask $umask &&
105 git init --shared=true repo &&
106 test 1 = "$(git -C repo config core.sharedrepository)"
107 ) &&
108 test_expect_perms "-rw-rw-r--" repo/.git/reftable/tables.list &&
109 for table in repo/.git/reftable/*.ref
111 test_expect_perms "-rw-rw-r--" "$table" ||
112 return 1
113 done
115 done
117 test_expect_success 'clone: can clone reftable repository' '
118 test_when_finished "rm -rf repo clone" &&
119 git init repo &&
120 test_commit -C repo message1 file1 &&
122 git clone repo cloned &&
123 echo reftable >expect &&
124 git -C cloned rev-parse --show-ref-format >actual &&
125 test_cmp expect actual &&
126 test_path_is_file cloned/file1
129 test_expect_success 'clone: can clone reffiles into reftable repository' '
130 test_when_finished "rm -rf reffiles reftable" &&
131 git init --ref-format=files reffiles &&
132 test_commit -C reffiles A &&
133 git clone --ref-format=reftable ./reffiles reftable &&
135 git -C reffiles rev-parse HEAD >expect &&
136 git -C reftable rev-parse HEAD >actual &&
137 test_cmp expect actual &&
139 git -C reftable rev-parse --show-ref-format >actual &&
140 echo reftable >expect &&
141 test_cmp expect actual &&
143 git -C reffiles rev-parse --show-ref-format >actual &&
144 echo files >expect &&
145 test_cmp expect actual
148 test_expect_success 'clone: can clone reftable into reffiles repository' '
149 test_when_finished "rm -rf reffiles reftable" &&
150 git init --ref-format=reftable reftable &&
151 test_commit -C reftable A &&
152 git clone --ref-format=files ./reftable reffiles &&
154 git -C reftable rev-parse HEAD >expect &&
155 git -C reffiles rev-parse HEAD >actual &&
156 test_cmp expect actual &&
158 git -C reftable rev-parse --show-ref-format >actual &&
159 echo reftable >expect &&
160 test_cmp expect actual &&
162 git -C reffiles rev-parse --show-ref-format >actual &&
163 echo files >expect &&
164 test_cmp expect actual
167 test_expect_success 'ref transaction: corrupted tables cause failure' '
168 test_when_finished "rm -rf repo" &&
169 git init repo &&
171 cd repo &&
172 test_commit file1 &&
173 for f in .git/reftable/*.ref
175 : >"$f" || return 1
176 done &&
177 test_must_fail git update-ref refs/heads/main HEAD
181 test_expect_success 'ref transaction: corrupted tables.list cause failure' '
182 test_when_finished "rm -rf repo" &&
183 git init repo &&
185 cd repo &&
186 test_commit file1 &&
187 echo garbage >.git/reftable/tables.list &&
188 test_must_fail git update-ref refs/heads/main HEAD
192 test_expect_success 'ref transaction: refuses to write ref causing F/D conflict' '
193 test_when_finished "rm -rf repo" &&
194 git init repo &&
195 test_commit -C repo file &&
196 test_must_fail git -C repo update-ref refs/heads/main/forbidden
199 test_expect_success 'ref transaction: deleting ref with invalid name fails' '
200 test_when_finished "rm -rf repo" &&
201 git init repo &&
202 test_commit -C repo file &&
203 test_must_fail git -C repo update-ref -d ../../my-private-file
206 test_expect_success 'ref transaction: can skip object ID verification' '
207 test_when_finished "rm -rf repo" &&
208 git init repo &&
209 test_must_fail test-tool -C repo ref-store main update-ref msg refs/heads/branch $INVALID_OID $ZERO_OID 0 &&
210 test-tool -C repo ref-store main update-ref msg refs/heads/branch $INVALID_OID $ZERO_OID REF_SKIP_OID_VERIFICATION
213 test_expect_success 'ref transaction: updating same ref multiple times fails' '
214 test_when_finished "rm -rf repo" &&
215 git init repo &&
216 test_commit -C repo A &&
217 cat >updates <<-EOF &&
218 update refs/heads/main $A
219 update refs/heads/main $A
221 cat >expect <<-EOF &&
222 fatal: multiple updates for ref ${SQ}refs/heads/main${SQ} not allowed
224 test_must_fail git -C repo update-ref --stdin <updates 2>err &&
225 test_cmp expect err
228 test_expect_success 'ref transaction: can delete symbolic self-reference with git-symbolic-ref(1)' '
229 test_when_finished "rm -rf repo" &&
230 git init repo &&
231 git -C repo symbolic-ref refs/heads/self refs/heads/self &&
232 git -C repo symbolic-ref -d refs/heads/self
235 test_expect_success 'ref transaction: deleting symbolic self-reference without --no-deref fails' '
236 test_when_finished "rm -rf repo" &&
237 git init repo &&
238 git -C repo symbolic-ref refs/heads/self refs/heads/self &&
239 cat >expect <<-EOF &&
240 error: multiple updates for ${SQ}refs/heads/self${SQ} (including one via symref ${SQ}refs/heads/self${SQ}) are not allowed
242 test_must_fail git -C repo update-ref -d refs/heads/self 2>err &&
243 test_cmp expect err
246 test_expect_success 'ref transaction: deleting symbolic self-reference with --no-deref succeeds' '
247 test_when_finished "rm -rf repo" &&
248 git init repo &&
249 git -C repo symbolic-ref refs/heads/self refs/heads/self &&
250 git -C repo update-ref -d --no-deref refs/heads/self
253 test_expect_success 'ref transaction: creating symbolic ref fails with F/D conflict' '
254 test_when_finished "rm -rf repo" &&
255 git init repo &&
256 test_commit -C repo A &&
257 cat >expect <<-EOF &&
258 error: unable to write symref for refs/heads: file/directory conflict
260 test_must_fail git -C repo symbolic-ref refs/heads refs/heads/foo 2>err &&
261 test_cmp expect err
264 test_expect_success 'ref transaction: ref deletion' '
265 test_when_finished "rm -rf repo" &&
266 git init repo &&
268 cd repo &&
269 test_commit file &&
270 HEAD_OID=$(git show-ref -s --verify HEAD) &&
271 cat >expect <<-EOF &&
272 $HEAD_OID refs/heads/main
273 $HEAD_OID refs/tags/file
275 git show-ref >actual &&
276 test_cmp expect actual &&
278 test_must_fail git update-ref -d refs/tags/file $INVALID_OID &&
279 git show-ref >actual &&
280 test_cmp expect actual &&
282 git update-ref -d refs/tags/file $HEAD_OID &&
283 echo "$HEAD_OID refs/heads/main" >expect &&
284 git show-ref >actual &&
285 test_cmp expect actual
289 test_expect_success 'ref transaction: writes cause auto-compaction' '
290 test_when_finished "rm -rf repo" &&
292 git init repo &&
293 test_line_count = 1 repo/.git/reftable/tables.list &&
295 test_commit -C repo --no-tag A &&
296 test_line_count = 2 repo/.git/reftable/tables.list &&
298 test_commit -C repo --no-tag B &&
299 test_line_count = 1 repo/.git/reftable/tables.list
302 check_fsync_events () {
303 local trace="$1" &&
304 shift &&
306 cat >expect &&
307 sed -n \
308 -e '/^{"event":"counter",.*"category":"fsync",/ {
309 s/.*"category":"fsync",//;
310 s/}$//;
312 }' \
313 <"$trace" >actual &&
314 test_cmp expect actual
317 test_expect_success 'ref transaction: writes are synced' '
318 test_when_finished "rm -rf repo" &&
319 git init repo &&
320 test_commit -C repo initial &&
322 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
323 GIT_TEST_FSYNC=true \
324 git -C repo -c core.fsync=reference \
325 -c core.fsyncMethod=fsync update-ref refs/heads/branch HEAD &&
326 check_fsync_events trace2.txt <<-EOF
327 "name":"hardware-flush","count":2
331 test_expect_success 'ref transaction: empty transaction in empty repo' '
332 test_when_finished "rm -rf repo" &&
333 git init repo &&
334 test_commit -C repo --no-tag A &&
335 git -C repo update-ref -d refs/heads/main &&
336 test-tool -C repo ref-store main delete-refs REF_NO_DEREF msg HEAD &&
337 git -C repo update-ref --stdin <<-EOF
338 prepare
339 commit
343 test_expect_success 'ref transaction: fails gracefully when auto compaction fails' '
344 test_when_finished "rm -rf repo" &&
345 git init repo &&
347 cd repo &&
349 test_commit A &&
350 for i in $(test_seq 10)
352 git branch branch-$i &&
353 for table in .git/reftable/*.ref
355 touch "$table.lock" || exit 1
356 done ||
357 exit 1
358 done &&
359 test_line_count = 13 .git/reftable/tables.list
363 test_expect_success 'pack-refs: compacts tables' '
364 test_when_finished "rm -rf repo" &&
365 git init repo &&
367 test_commit -C repo A &&
368 ls -1 repo/.git/reftable >table-files &&
369 test_line_count = 4 table-files &&
370 test_line_count = 3 repo/.git/reftable/tables.list &&
372 git -C repo pack-refs &&
373 ls -1 repo/.git/reftable >table-files &&
374 test_line_count = 2 table-files &&
375 test_line_count = 1 repo/.git/reftable/tables.list
378 test_expect_success 'pack-refs: compaction raises locking errors' '
379 test_when_finished "rm -rf repo" &&
380 git init repo &&
381 test_commit -C repo A &&
382 touch repo/.git/reftable/tables.list.lock &&
383 cat >expect <<-EOF &&
384 error: unable to compact stack: data is locked
386 test_must_fail git -C repo pack-refs 2>err &&
387 test_cmp expect err
390 for command in pack-refs gc "maintenance run --task=pack-refs"
392 test_expect_success "$command: auto compaction" '
393 test_when_finished "rm -rf repo" &&
394 git init repo &&
396 cd repo &&
398 test_commit A &&
400 # We need a bit of setup to ensure that git-gc(1) actually
401 # triggers, and that it does not write anything to the refdb.
402 git config gc.auto 1 &&
403 git config gc.autoDetach 0 &&
404 git config gc.reflogExpire never &&
405 git config gc.reflogExpireUnreachable never &&
406 test_oid blob17_1 | git hash-object -w --stdin &&
408 # The tables should have been auto-compacted, and thus auto
409 # compaction should not have to do anything.
410 ls -1 .git/reftable >tables-expect &&
411 test_line_count = 4 tables-expect &&
412 git $command --auto &&
413 ls -1 .git/reftable >tables-actual &&
414 test_cmp tables-expect tables-actual &&
416 test_oid blob17_2 | git hash-object -w --stdin &&
418 # Lock all tables write some refs. Auto-compaction will be
419 # unable to compact tables and thus fails gracefully, leaving
420 # the stack in a sub-optimal state.
421 ls .git/reftable/*.ref |
422 while read table
424 touch "$table.lock" || exit 1
425 done &&
426 git branch B &&
427 git branch C &&
428 rm .git/reftable/*.lock &&
429 test_line_count = 5 .git/reftable/tables.list &&
431 git $command --auto &&
432 test_line_count = 1 .git/reftable/tables.list
435 done
437 test_expect_success 'pack-refs: prunes stale tables' '
438 test_when_finished "rm -rf repo" &&
439 git init repo &&
440 touch repo/.git/reftable/stale-table.ref &&
441 git -C repo pack-refs &&
442 test_path_is_missing repo/.git/reftable/stable-ref.ref
445 test_expect_success 'pack-refs: does not prune non-table files' '
446 test_when_finished "rm -rf repo" &&
447 git init repo &&
448 touch repo/.git/reftable/garbage &&
449 git -C repo pack-refs &&
450 test_path_is_file repo/.git/reftable/garbage
453 for umask in 002 022
455 test_expect_success POSIXPERM 'pack-refs: honors core.sharedRepository' '
456 test_when_finished "rm -rf repo" &&
458 umask $umask &&
459 git init --shared=true repo &&
460 test_commit -C repo A &&
461 test_line_count = 3 repo/.git/reftable/tables.list
462 ) &&
463 git -C repo pack-refs &&
464 test_expect_perms "-rw-rw-r--" repo/.git/reftable/tables.list &&
465 for table in repo/.git/reftable/*.ref
467 test_expect_perms "-rw-rw-r--" "$table" ||
468 return 1
469 done
471 done
473 test_expect_success 'packed-refs: writes are synced' '
474 test_when_finished "rm -rf repo" &&
475 git init repo &&
476 test_commit -C repo initial &&
477 test_line_count = 2 table-files &&
479 : >trace2.txt &&
480 GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
481 GIT_TEST_FSYNC=true \
482 git -C repo -c core.fsync=reference \
483 -c core.fsyncMethod=fsync pack-refs &&
484 check_fsync_events trace2.txt <<-EOF
485 "name":"hardware-flush","count":2
489 test_expect_success 'ref iterator: bogus names are flagged' '
490 test_when_finished "rm -rf repo" &&
491 git init repo &&
493 cd repo &&
494 test_commit --no-tag file &&
495 test-tool ref-store main update-ref msg "refs/heads/bogus..name" $(git rev-parse HEAD) $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
497 cat >expect <<-EOF &&
498 $ZERO_OID refs/heads/bogus..name 0xc
499 $(git rev-parse HEAD) refs/heads/main 0x0
501 test-tool ref-store main for-each-ref "" >actual &&
502 test_cmp expect actual
506 test_expect_success 'ref iterator: missing object IDs are not flagged' '
507 test_when_finished "rm -rf repo" &&
508 git init repo &&
510 cd repo &&
511 test-tool ref-store main update-ref msg "refs/heads/broken-hash" $INVALID_OID $ZERO_OID REF_SKIP_OID_VERIFICATION &&
513 cat >expect <<-EOF &&
514 $INVALID_OID refs/heads/broken-hash 0x0
516 test-tool ref-store main for-each-ref "" >actual &&
517 test_cmp expect actual
521 test_expect_success 'basic: commit and list refs' '
522 test_when_finished "rm -rf repo" &&
523 git init repo &&
524 test_commit -C repo file &&
525 test_write_lines refs/heads/main refs/tags/file >expect &&
526 git -C repo for-each-ref --format="%(refname)" >actual &&
527 test_cmp actual expect
530 test_expect_success 'basic: can write large commit message' '
531 test_when_finished "rm -rf repo" &&
532 git init repo &&
533 perl -e "
534 print \"this is a long commit message\" x 50000
535 " >commit-msg &&
536 git -C repo commit --allow-empty --file=../commit-msg
539 test_expect_success 'basic: show-ref fails with empty repository' '
540 test_when_finished "rm -rf repo" &&
541 git init repo &&
542 test_must_fail git -C repo show-ref >actual &&
543 test_must_be_empty actual
546 test_expect_success 'basic: can check out unborn branch' '
547 test_when_finished "rm -rf repo" &&
548 git init repo &&
549 git -C repo checkout -b main
552 test_expect_success 'basic: peeled tags are stored' '
553 test_when_finished "rm -rf repo" &&
554 git init repo &&
555 test_commit -C repo file &&
556 git -C repo tag -m "annotated tag" test_tag HEAD &&
557 for ref in refs/heads/main refs/tags/file refs/tags/test_tag refs/tags/test_tag^{}
559 echo "$(git -C repo rev-parse "$ref") $ref" || return 1
560 done >expect &&
561 git -C repo show-ref -d >actual &&
562 test_cmp expect actual
565 test_expect_success 'basic: for-each-ref can print symrefs' '
566 test_when_finished "rm -rf repo" &&
567 git init repo &&
569 cd repo &&
570 test_commit file &&
571 git branch &&
572 git symbolic-ref refs/heads/sym refs/heads/main &&
573 cat >expected <<-EOF &&
574 refs/heads/main
576 git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
577 test_cmp expected actual
581 test_expect_success 'basic: notes' '
582 test_when_finished "rm -rf repo" &&
583 git init repo &&
585 write_script fake_editor <<-\EOF &&
586 echo "$MSG" >"$1"
587 echo "$MSG" >&2
590 test_commit 1st &&
591 test_commit 2nd &&
592 GIT_EDITOR=./fake_editor MSG=b4 git notes add &&
593 GIT_EDITOR=./fake_editor MSG=b3 git notes edit &&
594 echo b4 >expect &&
595 git notes --ref commits@{1} show >actual &&
596 test_cmp expect actual
600 test_expect_success 'basic: stash' '
601 test_when_finished "rm -rf repo" &&
602 git init repo &&
604 cd repo &&
605 test_commit file &&
606 git stash list >expect &&
607 test_line_count = 0 expect &&
609 echo hoi >>file.t &&
610 git stash push -m stashed &&
611 git stash list >expect &&
612 test_line_count = 1 expect &&
614 git stash clear &&
615 git stash list >expect &&
616 test_line_count = 0 expect
620 test_expect_success 'basic: cherry-pick' '
621 test_when_finished "rm -rf repo" &&
622 git init repo &&
624 cd repo &&
625 test_commit message1 file1 &&
626 test_commit message2 file2 &&
627 git branch source &&
628 git checkout HEAD^ &&
629 test_commit message3 file3 &&
630 git cherry-pick source &&
631 test_path_is_file file2
635 test_expect_success 'basic: rebase' '
636 test_when_finished "rm -rf repo" &&
637 git init repo &&
639 cd repo &&
640 test_commit message1 file1 &&
641 test_commit message2 file2 &&
642 git branch source &&
643 git checkout HEAD^ &&
644 test_commit message3 file3 &&
645 git rebase source &&
646 test_path_is_file file2
650 test_expect_success 'reflog: can delete separate reflog entries' '
651 test_when_finished "rm -rf repo" &&
652 git init repo &&
654 cd repo &&
656 test_commit file &&
657 test_commit file2 &&
658 test_commit file3 &&
659 test_commit file4 &&
660 git reflog >actual &&
661 grep file3 actual &&
663 git reflog delete HEAD@{1} &&
664 git reflog >actual &&
665 ! grep file3 actual
669 test_expect_success 'reflog: can switch to previous branch' '
670 test_when_finished "rm -rf repo" &&
671 git init repo &&
673 cd repo &&
674 test_commit file1 &&
675 git checkout -b branch1 &&
676 test_commit file2 &&
677 git checkout -b branch2 &&
678 git switch - &&
679 git rev-parse --symbolic-full-name HEAD >actual &&
680 echo refs/heads/branch1 >expect &&
681 test_cmp actual expect
685 test_expect_success 'reflog: copying branch writes reflog entry' '
686 test_when_finished "rm -rf repo" &&
687 git init repo &&
689 cd repo &&
690 test_commit file1 &&
691 test_commit file2 &&
692 oid=$(git rev-parse --short HEAD) &&
693 git branch src &&
694 cat >expect <<-EOF &&
695 ${oid} dst@{0}: Branch: copied refs/heads/src to refs/heads/dst
696 ${oid} dst@{1}: branch: Created from main
698 git branch -c src dst &&
699 git reflog dst >actual &&
700 test_cmp expect actual
704 test_expect_success 'reflog: renaming branch writes reflog entry' '
705 test_when_finished "rm -rf repo" &&
706 git init repo &&
708 cd repo &&
709 git symbolic-ref HEAD refs/heads/before &&
710 test_commit file &&
711 git show-ref >expected.refs &&
712 sed s/before/after/g <expected.refs >expected &&
713 git branch -M after &&
714 git show-ref >actual &&
715 test_cmp expected actual &&
716 echo refs/heads/after >expected &&
717 git symbolic-ref HEAD >actual &&
718 test_cmp expected actual
722 test_expect_success 'reflog: can store empty logs' '
723 test_when_finished "rm -rf repo" &&
724 git init repo &&
726 cd repo &&
728 test_must_fail test-tool ref-store main reflog-exists refs/heads/branch &&
729 test-tool ref-store main create-reflog refs/heads/branch &&
730 test-tool ref-store main reflog-exists refs/heads/branch &&
731 test-tool ref-store main for-each-reflog-ent-reverse refs/heads/branch >actual &&
732 test_must_be_empty actual
736 test_expect_success 'reflog: expiry empties reflog' '
737 test_when_finished "rm -rf repo" &&
738 git init repo &&
740 cd repo &&
742 test_commit initial &&
743 git checkout -b branch &&
744 test_commit fileA &&
745 test_commit fileB &&
747 cat >expect <<-EOF &&
748 commit: fileB
749 commit: fileA
750 branch: Created from HEAD
752 git reflog show --format="%gs" refs/heads/branch >actual &&
753 test_cmp expect actual &&
755 git reflog expire branch --expire=all &&
756 git reflog show --format="%gs" refs/heads/branch >actual &&
757 test_must_be_empty actual &&
758 test-tool ref-store main reflog-exists refs/heads/branch
762 test_expect_success 'reflog: can be deleted' '
763 test_when_finished "rm -rf repo" &&
764 git init repo &&
766 cd repo &&
767 test_commit initial &&
768 test-tool ref-store main reflog-exists refs/heads/main &&
769 test-tool ref-store main delete-reflog refs/heads/main &&
770 test_must_fail test-tool ref-store main reflog-exists refs/heads/main
774 test_expect_success 'reflog: garbage collection deletes reflog entries' '
775 test_when_finished "rm -rf repo" &&
776 git init repo &&
778 cd repo &&
780 for count in $(test_seq 1 10)
782 test_commit "number $count" file.t $count number-$count ||
783 return 1
784 done &&
785 git reflog refs/heads/main >actual &&
786 test_line_count = 10 actual &&
787 grep "commit (initial): number 1" actual &&
788 grep "commit: number 10" actual &&
790 git gc &&
791 git reflog refs/heads/main >actual &&
792 test_line_count = 0 actual
796 test_expect_success 'reflog: updates via HEAD update HEAD reflog' '
797 test_when_finished "rm -rf repo" &&
798 git init repo &&
800 cd repo &&
801 test_commit main-one &&
802 git checkout -b new-branch &&
803 test_commit new-one &&
804 test_commit new-two &&
806 echo new-one >expect &&
807 git log -1 --format=%s HEAD@{1} >actual &&
808 test_cmp expect actual
812 test_expect_success 'worktree: adding worktree creates separate stack' '
813 test_when_finished "rm -rf repo worktree" &&
814 git init repo &&
815 test_commit -C repo A &&
817 git -C repo worktree add ../worktree &&
818 test_path_is_file repo/.git/worktrees/worktree/refs/heads &&
819 echo "ref: refs/heads/.invalid" >expect &&
820 test_cmp expect repo/.git/worktrees/worktree/HEAD &&
821 test_path_is_dir repo/.git/worktrees/worktree/reftable &&
822 test_path_is_file repo/.git/worktrees/worktree/reftable/tables.list
825 test_expect_success 'worktree: pack-refs in main repo packs main refs' '
826 test_when_finished "rm -rf repo worktree" &&
827 git init repo &&
828 test_commit -C repo A &&
829 git -C repo worktree add ../worktree &&
831 test_line_count = 3 repo/.git/worktrees/worktree/reftable/tables.list &&
832 test_line_count = 4 repo/.git/reftable/tables.list &&
833 git -C repo pack-refs &&
834 test_line_count = 3 repo/.git/worktrees/worktree/reftable/tables.list &&
835 test_line_count = 1 repo/.git/reftable/tables.list
838 test_expect_success 'worktree: pack-refs in worktree packs worktree refs' '
839 test_when_finished "rm -rf repo worktree" &&
840 git init repo &&
841 test_commit -C repo A &&
842 git -C repo worktree add ../worktree &&
844 test_line_count = 3 repo/.git/worktrees/worktree/reftable/tables.list &&
845 test_line_count = 4 repo/.git/reftable/tables.list &&
846 git -C worktree pack-refs &&
847 test_line_count = 1 repo/.git/worktrees/worktree/reftable/tables.list &&
848 test_line_count = 4 repo/.git/reftable/tables.list
851 test_expect_success 'worktree: creating shared ref updates main stack' '
852 test_when_finished "rm -rf repo worktree" &&
853 git init repo &&
854 test_commit -C repo A &&
856 git -C repo worktree add ../worktree &&
857 git -C repo pack-refs &&
858 git -C worktree pack-refs &&
859 test_line_count = 1 repo/.git/worktrees/worktree/reftable/tables.list &&
860 test_line_count = 1 repo/.git/reftable/tables.list &&
862 git -C worktree update-ref refs/heads/shared HEAD &&
863 test_line_count = 1 repo/.git/worktrees/worktree/reftable/tables.list &&
864 test_line_count = 2 repo/.git/reftable/tables.list
867 test_expect_success 'worktree: creating per-worktree ref updates worktree stack' '
868 test_when_finished "rm -rf repo worktree" &&
869 git init repo &&
870 test_commit -C repo A &&
872 git -C repo worktree add ../worktree &&
873 git -C repo pack-refs &&
874 git -C worktree pack-refs &&
875 test_line_count = 1 repo/.git/worktrees/worktree/reftable/tables.list &&
876 test_line_count = 1 repo/.git/reftable/tables.list &&
878 git -C worktree update-ref refs/bisect/per-worktree HEAD &&
879 test_line_count = 2 repo/.git/worktrees/worktree/reftable/tables.list &&
880 test_line_count = 1 repo/.git/reftable/tables.list
883 test_expect_success 'worktree: creating per-worktree ref from main repo' '
884 test_when_finished "rm -rf repo worktree" &&
885 git init repo &&
886 test_commit -C repo A &&
888 git -C repo worktree add ../worktree &&
889 git -C repo pack-refs &&
890 git -C worktree pack-refs &&
891 test_line_count = 1 repo/.git/worktrees/worktree/reftable/tables.list &&
892 test_line_count = 1 repo/.git/reftable/tables.list &&
894 git -C repo update-ref worktrees/worktree/refs/bisect/per-worktree HEAD &&
895 test_line_count = 2 repo/.git/worktrees/worktree/reftable/tables.list &&
896 test_line_count = 1 repo/.git/reftable/tables.list
899 test_expect_success 'worktree: creating per-worktree ref from second worktree' '
900 test_when_finished "rm -rf repo wt1 wt2" &&
901 git init repo &&
902 test_commit -C repo A &&
904 git -C repo worktree add ../wt1 &&
905 git -C repo worktree add ../wt2 &&
906 git -C repo pack-refs &&
907 git -C wt1 pack-refs &&
908 git -C wt2 pack-refs &&
909 test_line_count = 1 repo/.git/worktrees/wt1/reftable/tables.list &&
910 test_line_count = 1 repo/.git/worktrees/wt2/reftable/tables.list &&
911 test_line_count = 1 repo/.git/reftable/tables.list &&
913 git -C wt1 update-ref worktrees/wt2/refs/bisect/per-worktree HEAD &&
914 test_line_count = 1 repo/.git/worktrees/wt1/reftable/tables.list &&
915 test_line_count = 2 repo/.git/worktrees/wt2/reftable/tables.list &&
916 test_line_count = 1 repo/.git/reftable/tables.list
919 test_expect_success 'worktree: can create shared and per-worktree ref in one transaction' '
920 test_when_finished "rm -rf repo worktree" &&
921 git init repo &&
922 test_commit -C repo A &&
924 git -C repo worktree add ../worktree &&
925 git -C repo pack-refs &&
926 git -C worktree pack-refs &&
927 test_line_count = 1 repo/.git/worktrees/worktree/reftable/tables.list &&
928 test_line_count = 1 repo/.git/reftable/tables.list &&
930 cat >stdin <<-EOF &&
931 create worktrees/worktree/refs/bisect/per-worktree HEAD
932 create refs/branches/shared HEAD
934 git -C repo update-ref --stdin <stdin &&
935 test_line_count = 2 repo/.git/worktrees/worktree/reftable/tables.list &&
936 test_line_count = 2 repo/.git/reftable/tables.list
939 test_expect_success 'worktree: can access common refs' '
940 test_when_finished "rm -rf repo worktree" &&
941 git init repo &&
942 test_commit -C repo file1 &&
943 git -C repo branch branch1 &&
944 git -C repo worktree add ../worktree &&
946 echo refs/heads/worktree >expect &&
947 git -C worktree symbolic-ref HEAD >actual &&
948 test_cmp expect actual &&
949 git -C worktree checkout branch1
952 test_expect_success 'worktree: adds worktree with detached HEAD' '
953 test_when_finished "rm -rf repo worktree" &&
955 git init repo &&
956 test_commit -C repo A &&
957 git -C repo rev-parse main >expect &&
959 git -C repo worktree add --detach ../worktree main &&
960 git -C worktree rev-parse HEAD >actual &&
961 test_cmp expect actual
964 test_expect_success 'fetch: accessing FETCH_HEAD special ref works' '
965 test_when_finished "rm -rf repo sub" &&
967 git init sub &&
968 test_commit -C sub two &&
969 git -C sub rev-parse HEAD >expect &&
971 git init repo &&
972 test_commit -C repo one &&
973 git -C repo fetch ../sub &&
974 git -C repo rev-parse FETCH_HEAD >actual &&
975 test_cmp expect actual
978 test_done