3 # Copyright (c) 2005 Amos Waterland
6 test_description='git branch assorted tests'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 . "$TEST_DIRECTORY"/lib-rebase.sh
14 test_expect_success 'prepare a trivial repository' '
16 git update-index --add A &&
17 git commit -m "Initial commit." &&
20 git update-index --add A &&
21 git commit -m "Second commit." &&
22 HEAD=$(git rev-parse --verify HEAD)
25 test_expect_success 'git branch --help should not have created a bogus branch' '
26 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
27 test_path_is_missing .git/refs/heads/--help
30 test_expect_success 'branch -h in broken repository' '
35 >.git/refs/heads/main &&
36 test_expect_code 129 git branch -h >usage 2>&1
38 test_i18ngrep "[Uu]sage" broken/usage
41 test_expect_success 'git branch abc should create a branch' '
42 git branch abc && test_path_is_file .git/refs/heads/abc
45 test_expect_success 'git branch a/b/c should create a branch' '
46 git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
49 test_expect_success 'git branch mb main... should create a branch' '
50 git branch mb main... && test_path_is_file .git/refs/heads/mb
53 test_expect_success 'git branch HEAD should fail' '
54 test_must_fail git branch HEAD
58 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
60 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
61 GIT_COMMITTER_DATE="2005-05-26 23:30" \
62 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
63 test_path_is_file .git/refs/heads/d/e/f &&
64 test_path_is_file .git/logs/refs/heads/d/e/f &&
65 test_cmp expect .git/logs/refs/heads/d/e/f
68 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
69 git branch -d d/e/f &&
70 test_path_is_missing .git/refs/heads/d/e/f &&
71 test_must_fail git reflog exists refs/heads/d/e/f
74 test_expect_success 'git branch j/k should work after branch j has been deleted' '
80 test_expect_success 'git branch l should work after branch l/m has been deleted' '
86 test_expect_success 'git branch -m dumps usage' '
87 test_expect_code 128 git branch -m 2>err &&
88 test_i18ngrep "branch name required" err
91 test_expect_success 'git branch -m m broken_symref should work' '
92 test_when_finished "git branch -D broken_symref" &&
93 git branch --create-reflog m &&
94 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
95 git branch -m m broken_symref &&
96 git reflog exists refs/heads/broken_symref &&
97 test_must_fail git reflog exists refs/heads/i_am_broken
100 test_expect_success 'git branch -m m m/m should work' '
101 git branch --create-reflog m &&
102 git branch -m m m/m &&
103 git reflog exists refs/heads/m/m
106 test_expect_success 'git branch -m n/n n should work' '
107 git branch --create-reflog n/n &&
108 git branch -m n/n n &&
109 git reflog exists refs/heads/n
112 # The topmost entry in reflog for branch bbb is about branch creation.
113 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
115 test_expect_success 'git branch -m bbb should rename checked out branch' '
116 test_when_finished git branch -D bbb &&
117 test_when_finished git checkout main &&
118 git checkout -b aaa &&
119 git commit --allow-empty -m "a new commit" &&
120 git rev-parse aaa@{0} >expect &&
122 git rev-parse bbb@{1} >actual &&
123 test_cmp expect actual &&
124 git symbolic-ref HEAD >actual &&
125 echo refs/heads/bbb >expect &&
126 test_cmp expect actual
129 test_expect_success 'renaming checked out branch works with d/f conflict' '
130 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
131 test_when_finished git checkout main &&
132 git checkout -b foo &&
133 git branch -m foo/bar &&
134 git symbolic-ref HEAD >actual &&
135 echo refs/heads/foo/bar >expect &&
136 test_cmp expect actual
139 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
142 test_must_fail git branch -m o/o o
145 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
147 test_must_fail git branch -m o/q o/p
150 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
151 git branch -M o/q o/p
154 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
156 git branch -m -f o/q o/p
159 test_expect_success 'git branch -m q r/q should fail when r exists' '
162 test_must_fail git branch -m q r/q
165 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
167 git checkout -b foo &&
168 test_must_fail git branch -M bar foo
171 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
172 git checkout -b baz &&
174 git branch -M baz bam &&
175 test $(git rev-parse --abbrev-ref HEAD) = bam
178 test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
179 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
180 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
181 grep "^0\{40\}.*$msg$" .git/logs/HEAD
184 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
185 git init -b main orphan &&
188 test_commit initial &&
189 git checkout --orphan lonely &&
190 grep lonely .git/HEAD &&
191 test_path_is_missing .git/refs/head/lonely &&
192 git branch -M main mistress &&
193 grep lonely .git/HEAD
197 test_expect_success 'resulting reflog can be shown by log -g' '
198 oid=$(git rev-parse HEAD) &&
199 cat >expect <<-EOF &&
201 HEAD@{2} $oid checkout: moving from foo to baz
203 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
204 test_cmp expect actual
207 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
209 git worktree add -b baz bazdir &&
210 git worktree add -f bazdir2 baz &&
211 git branch -M baz bam &&
212 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
213 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
214 rm -r bazdir bazdir2 &&
218 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
219 git checkout -b baz &&
220 git worktree add -f bazdir baz &&
223 git branch -M baz bam &&
224 test $(git rev-parse --abbrev-ref HEAD) = bam
226 test $(git rev-parse --abbrev-ref HEAD) = bam &&
231 test_expect_success 'git branch -M main should work when main is checked out' '
236 test_expect_success 'git branch -M main main should work when main is checked out' '
238 git branch -M main main
241 test_expect_success 'git branch -M topic topic should work when main is checked out' '
244 git branch -M topic topic
247 test_expect_success 'git branch -v -d t should work' '
249 git rev-parse --verify refs/heads/t &&
250 git branch -v -d t &&
251 test_must_fail git rev-parse --verify refs/heads/t
254 test_expect_success 'git branch -v -m t s should work' '
256 git rev-parse --verify refs/heads/t &&
257 git branch -v -m t s &&
258 test_must_fail git rev-parse --verify refs/heads/t &&
259 git rev-parse --verify refs/heads/s &&
263 test_expect_success 'git branch -m -d t s should fail' '
265 git rev-parse refs/heads/t &&
266 test_must_fail git branch -m -d t s &&
268 test_must_fail git rev-parse refs/heads/t
271 test_expect_success 'git branch --list -d t should fail' '
273 git rev-parse refs/heads/t &&
274 test_must_fail git branch --list -d t &&
276 test_must_fail git rev-parse refs/heads/t
279 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
280 test_when_finished "rm -rf repo1 repo2" &&
283 git init repo1/sub &&
284 test_commit -C repo1/sub x &&
285 git -C repo1 submodule add ./sub &&
286 git -C repo1 commit -m "adding sub" &&
288 git clone --recurse-submodules repo1 repo2 &&
289 git -C repo2/sub checkout -b work &&
290 test_must_fail git -C repo2/sub branch -D work
293 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
294 test_when_finished "rm -rf nonbare base secondary" &&
296 git init -b main nonbare &&
297 test_commit -C nonbare x &&
298 git clone --bare nonbare bare &&
299 git -C bare worktree add --detach ../secondary main &&
300 git -C secondary branch -D main
303 test_expect_success 'git branch --list -v with --abbrev' '
304 test_when_finished "git branch -D t" &&
306 git branch -v --list t >actual.default &&
307 git branch -v --list --abbrev t >actual.abbrev &&
308 test_cmp actual.default actual.abbrev &&
310 git branch -v --list --no-abbrev t >actual.noabbrev &&
311 git branch -v --list --abbrev=0 t >actual.0abbrev &&
312 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
313 test_cmp actual.noabbrev actual.0abbrev &&
314 test_cmp actual.noabbrev actual.noabbrev-conf &&
316 git branch -v --list --abbrev=36 t >actual.36abbrev &&
317 # how many hexdigits are used?
318 read name objdefault rest <actual.abbrev &&
319 read name obj36 rest <actual.36abbrev &&
320 objfull=$(git rev-parse --verify t) &&
322 # are we really getting abbreviations?
323 test "$obj36" != "$objdefault" &&
324 expr "$obj36" : "$objdefault" >/dev/null &&
325 test "$objfull" != "$obj36" &&
326 expr "$objfull" : "$obj36" >/dev/null
330 test_expect_success 'git branch --column' '
331 COLUMNS=81 git branch --column=column >actual &&
332 cat >expect <<\EOF &&
333 a/b/c bam foo l * main n o/p r
334 abc bar j/k m/m mb o/o q topic
336 test_cmp expect actual
339 test_expect_success 'git branch --column with an extremely long branch name' '
340 long=this/is/a/part/of/long/branch/name &&
341 long=z$long/$long/$long/$long &&
342 test_when_finished "git branch -d $long" &&
344 COLUMNS=80 git branch --column=column >actual &&
364 test_cmp expect actual
367 test_expect_success 'git branch with column.*' '
368 git config column.ui column &&
369 git config column.branch "dense" &&
370 COLUMNS=80 git branch >actual &&
371 git config --unset column.branch &&
372 git config --unset column.ui &&
373 cat >expect <<\EOF &&
374 a/b/c bam foo l * main n o/p r
375 abc bar j/k m/m mb o/o q topic
377 test_cmp expect actual
380 test_expect_success 'git branch --column -v should fail' '
381 test_must_fail git branch --column -v
384 test_expect_success 'git branch -v with column.ui ignored' '
385 git config column.ui column &&
386 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
387 git config --unset column.ui &&
388 cat >expect <<\EOF &&
406 test_cmp expect actual
409 mv .git/config .git/config-saved
411 test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
412 git branch -m q q2 &&
416 mv .git/config-saved .git/config
418 git config branch.s/s.dummy Hello
420 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
421 git branch --create-reflog s/s &&
422 git reflog exists refs/heads/s/s &&
423 git branch --create-reflog s/t &&
424 git reflog exists refs/heads/s/t &&
426 git branch -m s/s s &&
427 git reflog exists refs/heads/s
430 test_expect_success 'config information was renamed, too' '
431 test $(git config branch.s.dummy) = Hello &&
432 test_must_fail git config branch.s/s.dummy
435 test_expect_success 'git branch -m correctly renames multiple config sections' '
436 test_when_finished "git checkout main" &&
437 git checkout -b source main &&
439 # Assert that a config file with multiple config sections has
440 # those sections preserved...
441 cat >expect <<-\EOF &&
442 branch.dest.key1=value1
444 branch.dest.key2=value2
446 cat >config.branch <<\EOF &&
447 ;; Note the lack of -\EOF above & mixed indenting here. This is
448 ;; intentional, we are also testing that the formatting of copied
449 ;; sections is preserved.
451 ;; Comment for source. Tabs
453 ;; Comment for the source value
455 ;; Comment for some.gar. Spaces
457 ;; Comment for the some.gar value
459 ;; Comment for source, again. Mixed tabs/spaces.
461 ;; Comment for the source value, again
464 cat config.branch >>.git/config &&
465 git branch -m source dest &&
466 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
467 test_cmp expect actual &&
469 # ...and that the comments for those sections are also
471 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
472 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
473 test_cmp expect actual
476 test_expect_success 'git branch -c dumps usage' '
477 test_expect_code 128 git branch -c 2>err &&
478 test_i18ngrep "branch name required" err
481 test_expect_success 'git branch --copy dumps usage' '
482 test_expect_code 128 git branch --copy 2>err &&
483 test_i18ngrep "branch name required" err
486 test_expect_success 'git branch -c d e should work' '
487 git branch --create-reflog d &&
488 git reflog exists refs/heads/d &&
489 git config branch.d.dummy Hello &&
491 git reflog exists refs/heads/d &&
492 git reflog exists refs/heads/e &&
493 echo Hello >expect &&
494 git config branch.e.dummy >actual &&
495 test_cmp expect actual &&
496 echo Hello >expect &&
497 git config branch.d.dummy >actual &&
498 test_cmp expect actual
501 test_expect_success 'git branch --copy is a synonym for -c' '
502 git branch --create-reflog copy &&
503 git reflog exists refs/heads/copy &&
504 git config branch.copy.dummy Hello &&
505 git branch --copy copy copy-to &&
506 git reflog exists refs/heads/copy &&
507 git reflog exists refs/heads/copy-to &&
508 echo Hello >expect &&
509 git config branch.copy.dummy >actual &&
510 test_cmp expect actual &&
511 echo Hello >expect &&
512 git config branch.copy-to.dummy >actual &&
513 test_cmp expect actual
516 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
517 git checkout -b ee &&
518 git reflog exists refs/heads/ee &&
519 git config branch.ee.dummy Hello &&
520 git branch -c ee ef &&
521 git reflog exists refs/heads/ee &&
522 git reflog exists refs/heads/ef &&
523 test $(git config branch.ee.dummy) = Hello &&
524 test $(git config branch.ef.dummy) = Hello &&
525 test $(git rev-parse --abbrev-ref HEAD) = ee
528 test_expect_success 'git branch -c f/f g/g should work' '
529 git branch --create-reflog f/f &&
530 git reflog exists refs/heads/f/f &&
531 git config branch.f/f.dummy Hello &&
532 git branch -c f/f g/g &&
533 git reflog exists refs/heads/f/f &&
534 git reflog exists refs/heads/g/g &&
535 test $(git config branch.f/f.dummy) = Hello &&
536 test $(git config branch.g/g.dummy) = Hello
539 test_expect_success 'git branch -c m2 m2 should work' '
540 git branch --create-reflog m2 &&
541 git reflog exists refs/heads/m2 &&
542 git config branch.m2.dummy Hello &&
543 git branch -c m2 m2 &&
544 git reflog exists refs/heads/m2 &&
545 test $(git config branch.m2.dummy) = Hello
548 test_expect_success 'git branch -c zz zz/zz should fail' '
549 git branch --create-reflog zz &&
550 git reflog exists refs/heads/zz &&
551 test_must_fail git branch -c zz zz/zz
554 test_expect_success 'git branch -c b/b b should fail' '
555 git branch --create-reflog b/b &&
556 test_must_fail git branch -c b/b b
559 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
560 git branch --create-reflog o/q &&
561 git reflog exists refs/heads/o/q &&
562 git reflog exists refs/heads/o/p &&
563 git branch -C o/q o/p
566 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
567 git reflog exists refs/heads/o/q &&
568 git reflog exists refs/heads/o/p &&
569 git branch -c -f o/q o/p
572 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
575 test_must_fail git branch -c qq rr/qq
578 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
580 git checkout -b b2 &&
581 test_must_fail git branch -C b1 b2
584 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
585 git checkout -b c1 &&
587 git branch -C c1 c2 &&
588 test $(git rev-parse --abbrev-ref HEAD) = c1
591 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
592 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
593 ! grep "$msg$" .git/logs/HEAD
596 test_expect_success 'git branch -C main should work when main is checked out' '
601 test_expect_success 'git branch -C main main should work when main is checked out' '
603 git branch -C main main
606 test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
609 git branch -C main5 main5
612 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
613 git branch --create-reflog cd &&
614 git reflog exists refs/heads/cd &&
615 git config branch.cd.dummy CD &&
616 git branch --create-reflog ab &&
617 git reflog exists refs/heads/ab &&
618 git config branch.ab.dummy AB &&
619 git branch -C ab cd &&
620 git reflog exists refs/heads/ab &&
621 git reflog exists refs/heads/cd &&
622 test $(git config branch.ab.dummy) = AB &&
623 test $(git config branch.cd.dummy) = AB
626 test_expect_success 'git branch -c correctly copies multiple config sections' '
629 test_when_finished "git checkout main" &&
630 git checkout -b source2 main &&
632 # Assert that a config file with multiple config sections has
633 # those sections preserved...
634 cat >expect <<-\EOF &&
635 branch.source2.key1=value1
636 branch.dest2.key1=value1
638 branch.source2.key2=value2
639 branch.dest2.key2=value2
641 cat >config.branch <<\EOF &&
642 ;; Note the lack of -\EOF above & mixed indenting here. This is
643 ;; intentional, we are also testing that the formatting of copied
644 ;; sections is preserved.
646 ;; Comment for source2. Tabs
648 ;; Comment for the source2 value
650 ;; Comment for more.gar. Spaces
652 ;; Comment for the more.gar value
654 ;; Comment for source2, again. Mixed tabs/spaces.
656 ;; Comment for the source2 value, again
659 cat config.branch >>.git/config &&
660 git branch -c source2 dest2 &&
661 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
662 test_cmp expect actual &&
664 # ...and that the comments and formatting for those sections
666 cat >expect <<\EOF &&
667 ;; Comment for source2. Tabs
669 ;; Comment for the source2 value
671 ;; Comment for more.gar. Spaces
673 ;; Comment for the source2 value
675 ;; Comment for more.gar. Spaces
677 ;; Comment for the more.gar value
679 ;; Comment for source2, again. Mixed tabs/spaces.
681 ;; Comment for the source2 value, again
684 ;; Comment for the source2 value, again
687 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
688 test_cmp expect actual
691 test_expect_success 'deleting a symref' '
693 git symbolic-ref refs/heads/symref refs/heads/target &&
694 echo "Deleted branch symref (was refs/heads/target)." >expect &&
695 git branch -d symref >actual &&
696 test_path_is_file .git/refs/heads/target &&
697 test_path_is_missing .git/refs/heads/symref &&
698 test_cmp expect actual
701 test_expect_success 'deleting a dangling symref' '
702 git symbolic-ref refs/heads/dangling-symref nowhere &&
703 test_path_is_file .git/refs/heads/dangling-symref &&
704 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
705 git branch -d dangling-symref >actual &&
706 test_path_is_missing .git/refs/heads/dangling-symref &&
707 test_cmp expect actual
710 test_expect_success 'deleting a self-referential symref' '
711 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
712 test_path_is_file .git/refs/heads/self-reference &&
713 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
714 git branch -d self-reference >actual &&
715 test_path_is_missing .git/refs/heads/self-reference &&
716 test_cmp expect actual
719 test_expect_success 'renaming a symref is not allowed' '
720 git symbolic-ref refs/heads/topic refs/heads/main &&
721 test_must_fail git branch -m topic new-topic &&
722 git symbolic-ref refs/heads/topic &&
723 test_path_is_file .git/refs/heads/main &&
724 test_path_is_missing .git/refs/heads/new-topic
727 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
728 git branch --create-reflog u &&
729 mv .git/logs/refs/heads/u real-u &&
730 ln -s real-u .git/logs/refs/heads/u &&
731 test_must_fail git branch -m u v
734 test_expect_success 'test tracking setup via --track' '
735 git config remote.local.url . &&
736 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
737 (git show-ref -q refs/remotes/local/main || git fetch local) &&
738 git branch --track my1 local/main &&
739 test $(git config branch.my1.remote) = local &&
740 test $(git config branch.my1.merge) = refs/heads/main
743 test_expect_success 'test tracking setup (non-wildcard, matching)' '
744 git config remote.local.url . &&
745 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
746 (git show-ref -q refs/remotes/local/main || git fetch local) &&
747 git branch --track my4 local/main &&
748 test $(git config branch.my4.remote) = local &&
749 test $(git config branch.my4.merge) = refs/heads/main
752 test_expect_success 'tracking setup fails on non-matching refspec' '
753 git config remote.local.url . &&
754 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
755 (git show-ref -q refs/remotes/local/main || git fetch local) &&
756 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
757 test_must_fail git branch --track my5 local/main &&
758 test_must_fail git config branch.my5.remote &&
759 test_must_fail git config branch.my5.merge
762 test_expect_success 'test tracking setup via config' '
763 git config branch.autosetupmerge true &&
764 git config remote.local.url . &&
765 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
766 (git show-ref -q refs/remotes/local/main || git fetch local) &&
767 git branch my3 local/main &&
768 test $(git config branch.my3.remote) = local &&
769 test $(git config branch.my3.merge) = refs/heads/main
772 test_expect_success 'test overriding tracking setup via --no-track' '
773 git config branch.autosetupmerge true &&
774 git config remote.local.url . &&
775 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
776 (git show-ref -q refs/remotes/local/main || git fetch local) &&
777 git branch --no-track my2 local/main &&
778 git config branch.autosetupmerge false &&
779 ! test "$(git config branch.my2.remote)" = local &&
780 ! test "$(git config branch.my2.merge)" = refs/heads/main
783 test_expect_success 'no tracking without .fetch entries' '
784 git config branch.autosetupmerge true &&
786 git config branch.autosetupmerge false &&
787 test -z "$(git config branch.my6.remote)" &&
788 test -z "$(git config branch.my6.merge)"
791 test_expect_success 'test tracking setup via --track but deeper' '
792 git config remote.local.url . &&
793 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
794 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
795 git branch --track my7 local/o/o &&
796 test "$(git config branch.my7.remote)" = local &&
797 test "$(git config branch.my7.merge)" = refs/heads/o/o
800 test_expect_success 'test deleting branch deletes branch config' '
802 test -z "$(git config branch.my7.remote)" &&
803 test -z "$(git config branch.my7.merge)"
806 test_expect_success 'test deleting branch without config' '
808 sha1=$(git rev-parse my7 | cut -c 1-7) &&
809 echo "Deleted branch my7 (was $sha1)." >expect &&
810 git branch -d my7 >actual 2>&1 &&
811 test_cmp expect actual
814 test_expect_success 'deleting currently checked out branch fails' '
815 git worktree add -b my7 my7 &&
816 test_must_fail git -C my7 branch -d my7 &&
817 test_must_fail git branch -d my7 &&
822 test_expect_success 'test --track without .fetch entries' '
823 git branch --track my8 &&
824 test "$(git config branch.my8.remote)" &&
825 test "$(git config branch.my8.merge)"
828 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
829 git config branch.autosetupmerge always &&
830 git branch my9 HEAD^ &&
831 git config branch.autosetupmerge false
834 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
835 test_must_fail git branch --track my10 HEAD^
838 test_expect_success 'branch from tag w/--track causes failure' '
840 test_must_fail git branch --track my11 foobar
843 test_expect_success '--set-upstream-to fails on multiple branches' '
844 echo "fatal: too many arguments to set new upstream" >expect &&
845 test_must_fail git branch --set-upstream-to main a b c 2>err &&
849 test_expect_success '--set-upstream-to fails on detached HEAD' '
850 git checkout HEAD^{} &&
851 test_when_finished git checkout - &&
852 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch." >expect &&
853 test_must_fail git branch --set-upstream-to main 2>err &&
857 test_expect_success '--set-upstream-to fails on a missing dst branch' '
858 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
859 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
863 test_expect_success '--set-upstream-to fails on a missing src branch' '
864 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
865 test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
868 test_expect_success '--set-upstream-to fails on a non-ref' '
869 echo "fatal: Cannot setup tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch." >expect &&
870 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
874 test_expect_success '--set-upstream-to fails on locked config' '
875 test_when_finished "rm -f .git/config.lock" &&
878 test_must_fail git branch --set-upstream-to locked 2>err &&
879 test_i18ngrep "could not lock config file .git/config" err
882 test_expect_success 'use --set-upstream-to modify HEAD' '
883 test_config branch.main.remote foo &&
884 test_config branch.main.merge foo &&
886 git branch --set-upstream-to my12 &&
887 test "$(git config branch.main.remote)" = "." &&
888 test "$(git config branch.main.merge)" = "refs/heads/my12"
891 test_expect_success 'use --set-upstream-to modify a particular branch' '
893 git branch --set-upstream-to main my13 &&
894 test_when_finished "git branch --unset-upstream my13" &&
895 test "$(git config branch.my13.remote)" = "." &&
896 test "$(git config branch.my13.merge)" = "refs/heads/main"
899 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
900 echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
901 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
905 test_expect_success '--unset-upstream should fail if config is locked' '
906 test_when_finished "rm -f .git/config.lock" &&
907 git branch --set-upstream-to locked &&
909 test_must_fail git branch --unset-upstream 2>err &&
910 test_i18ngrep "could not lock config file .git/config" err
913 test_expect_success 'test --unset-upstream on HEAD' '
915 test_config branch.main.remote foo &&
916 test_config branch.main.merge foo &&
917 git branch --set-upstream-to my14 &&
918 git branch --unset-upstream &&
919 test_must_fail git config branch.main.remote &&
920 test_must_fail git config branch.main.merge &&
921 # fail for a branch without upstream set
922 echo "fatal: Branch '"'"'main'"'"' has no upstream information" >expect &&
923 test_must_fail git branch --unset-upstream 2>err &&
927 test_expect_success '--unset-upstream should fail on multiple branches' '
928 echo "fatal: too many arguments to unset upstream" >expect &&
929 test_must_fail git branch --unset-upstream a b c 2>err &&
933 test_expect_success '--unset-upstream should fail on detached HEAD' '
934 git checkout HEAD^{} &&
935 test_when_finished git checkout - &&
936 echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect &&
937 test_must_fail git branch --unset-upstream 2>err &&
941 test_expect_success 'test --unset-upstream on a particular branch' '
943 git branch --set-upstream-to main my14 &&
944 git branch --unset-upstream my14 &&
945 test_must_fail git config branch.my14.remote &&
946 test_must_fail git config branch.my14.merge
949 test_expect_success 'disabled option --set-upstream fails' '
950 test_must_fail git branch --set-upstream origin/main
953 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
954 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
955 cat >expect <<-\EOF &&
956 warning: Not setting branch my13 as its own upstream.
958 test_expect_code 1 git config branch.my13.remote &&
959 test_expect_code 1 git config branch.my13.merge &&
960 test_cmp expect actual
963 # Keep this test last, as it changes the current branch
965 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
967 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
968 GIT_COMMITTER_DATE="2005-05-26 23:30" \
969 git checkout -b g/h/i -l main &&
970 test_path_is_file .git/refs/heads/g/h/i &&
971 test_path_is_file .git/logs/refs/heads/g/h/i &&
972 test_cmp expect .git/logs/refs/heads/g/h/i
975 test_expect_success 'checkout -b makes reflog by default' '
977 git config --unset core.logAllRefUpdates &&
978 git checkout -b alpha &&
979 git rev-parse --verify alpha@{0}
982 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
984 git config core.logAllRefUpdates false &&
985 git checkout -b beta &&
986 test_must_fail git rev-parse --verify beta@{0}
989 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
991 git checkout -lb gamma &&
992 git config --unset core.logAllRefUpdates &&
993 git rev-parse --verify gamma@{0}
996 test_expect_success 'avoid ambiguous track' '
997 git config branch.autosetupmerge true &&
998 git config remote.ambi1.url lalala &&
999 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1000 git config remote.ambi2.url lilili &&
1001 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1002 test_must_fail git branch all1 main &&
1003 test -z "$(git config branch.all1.merge)"
1006 test_expect_success 'autosetuprebase local on a tracked local branch' '
1007 git config remote.local.url . &&
1008 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1009 git config branch.autosetuprebase local &&
1010 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1011 git branch mybase &&
1012 git branch --track myr1 mybase &&
1013 test "$(git config branch.myr1.remote)" = . &&
1014 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1015 test "$(git config branch.myr1.rebase)" = true
1018 test_expect_success 'autosetuprebase always on a tracked local branch' '
1019 git config remote.local.url . &&
1020 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1021 git config branch.autosetuprebase always &&
1022 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1023 git branch mybase2 &&
1024 git branch --track myr2 mybase &&
1025 test "$(git config branch.myr2.remote)" = . &&
1026 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1027 test "$(git config branch.myr2.rebase)" = true
1030 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1031 git config remote.local.url . &&
1032 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1033 git config branch.autosetuprebase remote &&
1034 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1035 git branch mybase3 &&
1036 git branch --track myr3 mybase2 &&
1037 test "$(git config branch.myr3.remote)" = . &&
1038 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1039 ! test "$(git config branch.myr3.rebase)" = true
1042 test_expect_success 'autosetuprebase never on a tracked local branch' '
1043 git config remote.local.url . &&
1044 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1045 git config branch.autosetuprebase never &&
1046 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1047 git branch mybase4 &&
1048 git branch --track myr4 mybase2 &&
1049 test "$(git config branch.myr4.remote)" = . &&
1050 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1051 ! test "$(git config branch.myr4.rebase)" = true
1054 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1055 git config remote.local.url . &&
1056 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1057 git config branch.autosetuprebase local &&
1058 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1059 git branch --track myr5 local/main &&
1060 test "$(git config branch.myr5.remote)" = local &&
1061 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1062 ! test "$(git config branch.myr5.rebase)" = true
1065 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1066 git config remote.local.url . &&
1067 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1068 git config branch.autosetuprebase never &&
1069 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1070 git branch --track myr6 local/main &&
1071 test "$(git config branch.myr6.remote)" = local &&
1072 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1073 ! test "$(git config branch.myr6.rebase)" = true
1076 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1077 git config remote.local.url . &&
1078 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1079 git config branch.autosetuprebase remote &&
1080 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1081 git branch --track myr7 local/main &&
1082 test "$(git config branch.myr7.remote)" = local &&
1083 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1084 test "$(git config branch.myr7.rebase)" = true
1087 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1088 git config remote.local.url . &&
1089 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1090 git config branch.autosetuprebase remote &&
1091 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1092 git branch --track myr8 local/main &&
1093 test "$(git config branch.myr8.remote)" = local &&
1094 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1095 test "$(git config branch.myr8.rebase)" = true
1098 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1099 git config --unset branch.autosetuprebase &&
1100 git config remote.local.url . &&
1101 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1102 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1103 git branch --track myr9 local/main &&
1104 test "$(git config branch.myr9.remote)" = local &&
1105 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1106 test "z$(git config branch.myr9.rebase)" = z
1109 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1110 git config remote.local.url . &&
1111 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1112 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1113 git branch mybase10 &&
1114 git branch --track myr10 mybase2 &&
1115 test "$(git config branch.myr10.remote)" = . &&
1116 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1117 test "z$(git config branch.myr10.rebase)" = z
1120 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1121 git config remote.local.url . &&
1122 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1123 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1124 git branch --no-track myr11 mybase2 &&
1125 test "z$(git config branch.myr11.remote)" = z &&
1126 test "z$(git config branch.myr11.merge)" = z &&
1127 test "z$(git config branch.myr11.rebase)" = z
1130 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1131 git config remote.local.url . &&
1132 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1133 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1134 git branch --no-track myr12 local/main &&
1135 test "z$(git config branch.myr12.remote)" = z &&
1136 test "z$(git config branch.myr12.merge)" = z &&
1137 test "z$(git config branch.myr12.rebase)" = z
1140 test_expect_success 'autosetuprebase never on an untracked local branch' '
1141 git config branch.autosetuprebase never &&
1142 git config remote.local.url . &&
1143 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1144 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1145 git branch --no-track myr13 mybase2 &&
1146 test "z$(git config branch.myr13.remote)" = z &&
1147 test "z$(git config branch.myr13.merge)" = z &&
1148 test "z$(git config branch.myr13.rebase)" = z
1151 test_expect_success 'autosetuprebase local on an untracked local branch' '
1152 git config branch.autosetuprebase local &&
1153 git config remote.local.url . &&
1154 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1155 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1156 git branch --no-track myr14 mybase2 &&
1157 test "z$(git config branch.myr14.remote)" = z &&
1158 test "z$(git config branch.myr14.merge)" = z &&
1159 test "z$(git config branch.myr14.rebase)" = z
1162 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1163 git config branch.autosetuprebase remote &&
1164 git config remote.local.url . &&
1165 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1166 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1167 git branch --no-track myr15 mybase2 &&
1168 test "z$(git config branch.myr15.remote)" = z &&
1169 test "z$(git config branch.myr15.merge)" = z &&
1170 test "z$(git config branch.myr15.rebase)" = z
1173 test_expect_success 'autosetuprebase always on an untracked local branch' '
1174 git config branch.autosetuprebase always &&
1175 git config remote.local.url . &&
1176 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1177 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1178 git branch --no-track myr16 mybase2 &&
1179 test "z$(git config branch.myr16.remote)" = z &&
1180 test "z$(git config branch.myr16.merge)" = z &&
1181 test "z$(git config branch.myr16.rebase)" = z
1184 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1185 git config branch.autosetuprebase never &&
1186 git config remote.local.url . &&
1187 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1188 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1189 git branch --no-track myr17 local/main &&
1190 test "z$(git config branch.myr17.remote)" = z &&
1191 test "z$(git config branch.myr17.merge)" = z &&
1192 test "z$(git config branch.myr17.rebase)" = z
1195 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1196 git config branch.autosetuprebase local &&
1197 git config remote.local.url . &&
1198 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1199 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1200 git branch --no-track myr18 local/main &&
1201 test "z$(git config branch.myr18.remote)" = z &&
1202 test "z$(git config branch.myr18.merge)" = z &&
1203 test "z$(git config branch.myr18.rebase)" = z
1206 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1207 git config branch.autosetuprebase remote &&
1208 git config remote.local.url . &&
1209 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1210 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1211 git branch --no-track myr19 local/main &&
1212 test "z$(git config branch.myr19.remote)" = z &&
1213 test "z$(git config branch.myr19.merge)" = z &&
1214 test "z$(git config branch.myr19.rebase)" = z
1217 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1218 git config branch.autosetuprebase always &&
1219 git config remote.local.url . &&
1220 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1221 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1222 git branch --no-track myr20 local/main &&
1223 test "z$(git config branch.myr20.remote)" = z &&
1224 test "z$(git config branch.myr20.merge)" = z &&
1225 test "z$(git config branch.myr20.rebase)" = z
1228 test_expect_success 'autosetuprebase always on detached HEAD' '
1229 git config branch.autosetupmerge always &&
1230 test_when_finished git checkout main &&
1231 git checkout HEAD^0 &&
1233 test -z "$(git config branch.my11.remote)" &&
1234 test -z "$(git config branch.my11.merge)"
1237 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1238 git config branch.autosetuprebase garbage &&
1239 test_must_fail git branch
1242 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1243 git config --unset branch.autosetuprebase &&
1244 echo "[branch] autosetuprebase" >>.git/config &&
1245 test_must_fail git branch &&
1246 git config --unset branch.autosetuprebase
1249 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1251 git config --unset branch.my8.merge &&
1252 test_must_fail git branch -d my8
1255 test_expect_success 'attempt to delete a branch merged to its base' '
1256 # we are on my9 which is the initial commit; traditionally
1257 # we would not have allowed deleting my8 that is not merged
1258 # to my9, but it is set to track main that already has my8
1259 git config branch.my8.merge refs/heads/main &&
1263 test_expect_success 'attempt to delete a branch merged to its base' '
1264 git checkout main &&
1266 git commit -m "Third commit" A &&
1267 git branch -t my10 my9 &&
1268 git branch -f my10 HEAD^ &&
1269 # we are on main which is at the third commit, and my10
1270 # is behind us, so traditionally we would have allowed deleting
1271 # it; but my10 is set to track my9 that is further behind.
1272 test_must_fail git branch -d my10
1275 test_expect_success 'use --edit-description' '
1276 write_script editor <<-\EOF &&
1277 echo "New contents" >"$1"
1279 EDITOR=./editor git branch --edit-description &&
1280 write_script editor <<-\EOF &&
1281 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1283 EDITOR=./editor git branch --edit-description &&
1284 echo "New contents" >expect &&
1285 test_cmp expect EDITOR_OUTPUT
1288 test_expect_success 'detect typo in branch name when using --edit-description' '
1289 write_script editor <<-\EOF &&
1290 echo "New contents" >"$1"
1292 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1295 test_expect_success 'refuse --edit-description on unborn branch for now' '
1296 test_when_finished "git checkout main" &&
1297 write_script editor <<-\EOF &&
1298 echo "New contents" >"$1"
1300 git checkout --orphan unborn &&
1301 test_must_fail env EDITOR=./editor git branch --edit-description
1304 test_expect_success '--merged catches invalid object names' '
1305 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1308 test_expect_success '--list during rebase' '
1309 test_when_finished "reset_rebase" &&
1310 git checkout main &&
1311 FAKE_LINES="1 edit 2" &&
1312 export FAKE_LINES &&
1314 git rebase -i HEAD~2 &&
1315 git branch --list >actual &&
1316 test_i18ngrep "rebasing main" actual
1319 test_expect_success '--list during rebase from detached HEAD' '
1320 test_when_finished "reset_rebase && git checkout main" &&
1321 git checkout main^0 &&
1322 oid=$(git rev-parse --short HEAD) &&
1323 FAKE_LINES="1 edit 2" &&
1324 export FAKE_LINES &&
1326 git rebase -i HEAD~2 &&
1327 git branch --list >actual &&
1328 test_i18ngrep "rebasing detached HEAD $oid" actual
1331 test_expect_success 'tracking with unexpected .fetch refspec' '
1333 git init -b main a &&
1338 git init -b main b &&
1343 git init -b main c &&
1347 git remote add a ../a &&
1348 git remote add b ../b &&
1351 git init -b main d &&
1354 git remote add c ../c &&
1355 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1357 git branch --track local/a/main remotes/a/main &&
1358 test "$(git config branch.local/a/main.remote)" = "c" &&
1359 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1360 git rev-parse --verify a >expect &&
1361 git rev-parse --verify local/a/main >actual &&
1362 test_cmp expect actual
1366 test_expect_success 'configured committerdate sort' '
1367 git init -b main sort &&
1370 git config branch.sort committerdate &&
1371 test_commit initial &&
1372 git checkout -b a &&
1374 git checkout -b c &&
1376 git checkout -b b &&
1378 git branch >actual &&
1379 cat >expect <<-\EOF &&
1385 test_cmp expect actual
1389 test_expect_success 'option override configured sort' '
1392 git config branch.sort committerdate &&
1393 git branch --sort=refname >actual &&
1394 cat >expect <<-\EOF &&
1400 test_cmp expect actual
1404 test_expect_success 'invalid sort parameter in configuration' '
1407 git config branch.sort "v:notvalid" &&
1408 test_must_fail git branch