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 abc should fail when abc exists' '
46 test_must_fail git branch abc
49 test_expect_success
'git branch --force abc should fail when abc is checked out' '
50 test_when_finished git switch main &&
52 test_must_fail git branch --force abc HEAD~1
55 test_expect_success
'git branch --force abc should succeed when abc exists' '
56 git rev-parse HEAD~1 >expect &&
57 git branch --force abc HEAD~1 &&
58 git rev-parse abc >actual &&
59 test_cmp expect actual
62 test_expect_success
'git branch a/b/c should create a branch' '
63 git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
66 test_expect_success
'git branch mb main... should create a branch' '
67 git branch mb main... && test_path_is_file .git/refs/heads/mb
70 test_expect_success
'git branch HEAD should fail' '
71 test_must_fail git branch HEAD
75 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
77 test_expect_success
'git branch --create-reflog d/e/f should create a branch and a log' '
78 GIT_COMMITTER_DATE="2005-05-26 23:30" \
79 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
80 test_path_is_file .git/refs/heads/d/e/f &&
81 test_path_is_file .git/logs/refs/heads/d/e/f &&
82 test_cmp expect .git/logs/refs/heads/d/e/f
85 test_expect_success
'git branch -d d/e/f should delete a branch and a log' '
86 git branch -d d/e/f &&
87 test_path_is_missing .git/refs/heads/d/e/f &&
88 test_must_fail git reflog exists refs/heads/d/e/f
91 test_expect_success
'git branch j/k should work after branch j has been deleted' '
97 test_expect_success
'git branch l should work after branch l/m has been deleted' '
103 test_expect_success
'git branch -m dumps usage' '
104 test_expect_code 128 git branch -m 2>err &&
105 test_i18ngrep "branch name required" err
108 test_expect_success
'git branch -m m broken_symref should work' '
109 test_when_finished "git branch -D broken_symref" &&
110 git branch --create-reflog m &&
111 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
112 git branch -m m broken_symref &&
113 git reflog exists refs/heads/broken_symref &&
114 test_must_fail git reflog exists refs/heads/i_am_broken
117 test_expect_success
'git branch -m m m/m should work' '
118 git branch --create-reflog m &&
119 git branch -m m m/m &&
120 git reflog exists refs/heads/m/m
123 test_expect_success
'git branch -m n/n n should work' '
124 git branch --create-reflog n/n &&
125 git branch -m n/n n &&
126 git reflog exists refs/heads/n
129 # The topmost entry in reflog for branch bbb is about branch creation.
130 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
132 test_expect_success
'git branch -m bbb should rename checked out branch' '
133 test_when_finished git branch -D bbb &&
134 test_when_finished git checkout main &&
135 git checkout -b aaa &&
136 git commit --allow-empty -m "a new commit" &&
137 git rev-parse aaa@{0} >expect &&
139 git rev-parse bbb@{1} >actual &&
140 test_cmp expect actual &&
141 git symbolic-ref HEAD >actual &&
142 echo refs/heads/bbb >expect &&
143 test_cmp expect actual
146 test_expect_success
'renaming checked out branch works with d/f conflict' '
147 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
148 test_when_finished git checkout main &&
149 git checkout -b foo &&
150 git branch -m foo/bar &&
151 git symbolic-ref HEAD >actual &&
152 echo refs/heads/foo/bar >expect &&
153 test_cmp expect actual
156 test_expect_success
'git branch -m o/o o should fail when o/p exists' '
159 test_must_fail git branch -m o/o o
162 test_expect_success
'git branch -m o/q o/p should fail when o/p exists' '
164 test_must_fail git branch -m o/q o/p
167 test_expect_success
'git branch -M o/q o/p should work when o/p exists' '
168 git branch -M o/q o/p
171 test_expect_success
'git branch -m -f o/q o/p should work when o/p exists' '
173 git branch -m -f o/q o/p
176 test_expect_success
'git branch -m q r/q should fail when r exists' '
179 test_must_fail git branch -m q r/q
182 test_expect_success
'git branch -M foo bar should fail when bar is checked out' '
184 git checkout -b foo &&
185 test_must_fail git branch -M bar foo
188 test_expect_success
'git branch -M foo bar should fail when bar is checked out in worktree' '
190 test_when_finished "git worktree remove wt && git branch -D wt" &&
191 git worktree add wt &&
192 test_must_fail git branch -M bar wt
195 test_expect_success
'git branch -M baz bam should succeed when baz is checked out' '
196 git checkout -b baz &&
198 git branch -M baz bam &&
199 test $(git rev-parse --abbrev-ref HEAD) = bam
202 test_expect_success
'git branch -M baz bam should add entries to .git/logs/HEAD' '
203 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
204 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
205 grep "^0\{40\}.*$msg$" .git/logs/HEAD
208 test_expect_success
'git branch -M should leave orphaned HEAD alone' '
209 git init -b main orphan &&
212 test_commit initial &&
213 git checkout --orphan lonely &&
214 grep lonely .git/HEAD &&
215 test_path_is_missing .git/refs/head/lonely &&
216 git branch -M main mistress &&
217 grep lonely .git/HEAD
221 test_expect_success
'resulting reflog can be shown by log -g' '
222 oid=$(git rev-parse HEAD) &&
223 cat >expect <<-EOF &&
225 HEAD@{2} $oid checkout: moving from foo to baz
227 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
228 test_cmp expect actual
231 test_expect_success
'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
233 git worktree add -b baz bazdir &&
234 git worktree add -f bazdir2 baz &&
235 git branch -M baz bam &&
236 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
237 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
238 rm -r bazdir bazdir2 &&
242 test_expect_success
'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
243 git checkout -b baz &&
244 git worktree add -f bazdir baz &&
247 git branch -M baz bam &&
248 test $(git rev-parse --abbrev-ref HEAD) = bam
250 test $(git rev-parse --abbrev-ref HEAD) = bam &&
255 test_expect_success
'git branch -M main should work when main is checked out' '
260 test_expect_success
'git branch -M main main should work when main is checked out' '
262 git branch -M main main
265 test_expect_success
'git branch -M topic topic should work when main is checked out' '
268 git branch -M topic topic
271 test_expect_success
'git branch -v -d t should work' '
273 git rev-parse --verify refs/heads/t &&
274 git branch -v -d t &&
275 test_must_fail git rev-parse --verify refs/heads/t
278 test_expect_success
'git branch -v -m t s should work' '
280 git rev-parse --verify refs/heads/t &&
281 git branch -v -m t s &&
282 test_must_fail git rev-parse --verify refs/heads/t &&
283 git rev-parse --verify refs/heads/s &&
287 test_expect_success
'git branch -m -d t s should fail' '
289 git rev-parse refs/heads/t &&
290 test_must_fail git branch -m -d t s &&
292 test_must_fail git rev-parse refs/heads/t
295 test_expect_success
'git branch --list -d t should fail' '
297 git rev-parse refs/heads/t &&
298 test_must_fail git branch --list -d t &&
300 test_must_fail git rev-parse refs/heads/t
303 test_expect_success
'deleting checked-out branch from repo that is a submodule' '
304 test_when_finished "rm -rf repo1 repo2" &&
307 git init repo1/sub &&
308 test_commit -C repo1/sub x &&
309 git -C repo1 submodule add ./sub &&
310 git -C repo1 commit -m "adding sub" &&
312 git clone --recurse-submodules repo1 repo2 &&
313 git -C repo2/sub checkout -b work &&
314 test_must_fail git -C repo2/sub branch -D work
317 test_expect_success
'bare main worktree has HEAD at branch deleted by secondary worktree' '
318 test_when_finished "rm -rf nonbare base secondary" &&
320 git init -b main nonbare &&
321 test_commit -C nonbare x &&
322 git clone --bare nonbare bare &&
323 git -C bare worktree add --detach ../secondary main &&
324 git -C secondary branch -D main
327 test_expect_success
'git branch --list -v with --abbrev' '
328 test_when_finished "git branch -D t" &&
330 git branch -v --list t >actual.default &&
331 git branch -v --list --abbrev t >actual.abbrev &&
332 test_cmp actual.default actual.abbrev &&
334 git branch -v --list --no-abbrev t >actual.noabbrev &&
335 git branch -v --list --abbrev=0 t >actual.0abbrev &&
336 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
337 test_cmp actual.noabbrev actual.0abbrev &&
338 test_cmp actual.noabbrev actual.noabbrev-conf &&
340 git branch -v --list --abbrev=36 t >actual.36abbrev &&
341 # how many hexdigits are used?
342 read name objdefault rest <actual.abbrev &&
343 read name obj36 rest <actual.36abbrev &&
344 objfull=$(git rev-parse --verify t) &&
346 # are we really getting abbreviations?
347 test "$obj36" != "$objdefault" &&
348 expr "$obj36" : "$objdefault" >/dev/null &&
349 test "$objfull" != "$obj36" &&
350 expr "$objfull" : "$obj36" >/dev/null
354 test_expect_success
'git branch --column' '
355 COLUMNS=81 git branch --column=column >actual &&
356 cat >expect <<\EOF &&
357 a/b/c bam foo l * main n o/p r
358 abc bar j/k m/m mb o/o q topic
360 test_cmp expect actual
363 test_expect_success
'git branch --column with an extremely long branch name' '
364 long=this/is/a/part/of/long/branch/name &&
365 long=z$long/$long/$long/$long &&
366 test_when_finished "git branch -d $long" &&
368 COLUMNS=80 git branch --column=column >actual &&
388 test_cmp expect actual
391 test_expect_success
'git branch with column.*' '
392 git config column.ui column &&
393 git config column.branch "dense" &&
394 COLUMNS=80 git branch >actual &&
395 git config --unset column.branch &&
396 git config --unset column.ui &&
397 cat >expect <<\EOF &&
398 a/b/c bam foo l * main n o/p r
399 abc bar j/k m/m mb o/o q topic
401 test_cmp expect actual
404 test_expect_success
'git branch --column -v should fail' '
405 test_must_fail git branch --column -v
408 test_expect_success
'git branch -v with column.ui ignored' '
409 git config column.ui column &&
410 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
411 git config --unset column.ui &&
412 cat >expect <<\EOF &&
430 test_cmp expect actual
433 mv .git
/config .git
/config-saved
435 test_expect_success SHA1
'git branch -m q q2 without config should succeed' '
436 git branch -m q q2 &&
440 mv .git
/config-saved .git
/config
442 git config branch.s
/s.dummy Hello
444 test_expect_success
'git branch -m s/s s should work when s/t is deleted' '
445 git branch --create-reflog s/s &&
446 git reflog exists refs/heads/s/s &&
447 git branch --create-reflog s/t &&
448 git reflog exists refs/heads/s/t &&
450 git branch -m s/s s &&
451 git reflog exists refs/heads/s
454 test_expect_success
'config information was renamed, too' '
455 test $(git config branch.s.dummy) = Hello &&
456 test_must_fail git config branch.s/s.dummy
459 test_expect_success
'git branch -m correctly renames multiple config sections' '
460 test_when_finished "git checkout main" &&
461 git checkout -b source main &&
463 # Assert that a config file with multiple config sections has
464 # those sections preserved...
465 cat >expect <<-\EOF &&
466 branch.dest.key1=value1
468 branch.dest.key2=value2
470 cat >config.branch <<\EOF &&
471 ;; Note the lack of -\EOF above & mixed indenting here. This is
472 ;; intentional, we are also testing that the formatting of copied
473 ;; sections is preserved.
475 ;; Comment for source. Tabs
477 ;; Comment for the source value
479 ;; Comment for some.gar. Spaces
481 ;; Comment for the some.gar value
483 ;; Comment for source, again. Mixed tabs/spaces.
485 ;; Comment for the source value, again
488 cat config.branch >>.git/config &&
489 git branch -m source dest &&
490 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
491 test_cmp expect actual &&
493 # ...and that the comments for those sections are also
495 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
496 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
497 test_cmp expect actual
500 test_expect_success
'git branch -c dumps usage' '
501 test_expect_code 128 git branch -c 2>err &&
502 test_i18ngrep "branch name required" err
505 test_expect_success
'git branch --copy dumps usage' '
506 test_expect_code 128 git branch --copy 2>err &&
507 test_i18ngrep "branch name required" err
510 test_expect_success
'git branch -c d e should work' '
511 git branch --create-reflog d &&
512 git reflog exists refs/heads/d &&
513 git config branch.d.dummy Hello &&
515 git reflog exists refs/heads/d &&
516 git reflog exists refs/heads/e &&
517 echo Hello >expect &&
518 git config branch.e.dummy >actual &&
519 test_cmp expect actual &&
520 echo Hello >expect &&
521 git config branch.d.dummy >actual &&
522 test_cmp expect actual
525 test_expect_success
'git branch --copy is a synonym for -c' '
526 git branch --create-reflog copy &&
527 git reflog exists refs/heads/copy &&
528 git config branch.copy.dummy Hello &&
529 git branch --copy copy copy-to &&
530 git reflog exists refs/heads/copy &&
531 git reflog exists refs/heads/copy-to &&
532 echo Hello >expect &&
533 git config branch.copy.dummy >actual &&
534 test_cmp expect actual &&
535 echo Hello >expect &&
536 git config branch.copy-to.dummy >actual &&
537 test_cmp expect actual
540 test_expect_success
'git branch -c ee ef should copy ee to create branch ef' '
541 git checkout -b ee &&
542 git reflog exists refs/heads/ee &&
543 git config branch.ee.dummy Hello &&
544 git branch -c ee ef &&
545 git reflog exists refs/heads/ee &&
546 git reflog exists refs/heads/ef &&
547 test $(git config branch.ee.dummy) = Hello &&
548 test $(git config branch.ef.dummy) = Hello &&
549 test $(git rev-parse --abbrev-ref HEAD) = ee
552 test_expect_success
'git branch -c f/f g/g should work' '
553 git branch --create-reflog f/f &&
554 git reflog exists refs/heads/f/f &&
555 git config branch.f/f.dummy Hello &&
556 git branch -c f/f g/g &&
557 git reflog exists refs/heads/f/f &&
558 git reflog exists refs/heads/g/g &&
559 test $(git config branch.f/f.dummy) = Hello &&
560 test $(git config branch.g/g.dummy) = Hello
563 test_expect_success
'git branch -c m2 m2 should work' '
564 git branch --create-reflog m2 &&
565 git reflog exists refs/heads/m2 &&
566 git config branch.m2.dummy Hello &&
567 git branch -c m2 m2 &&
568 git reflog exists refs/heads/m2 &&
569 test $(git config branch.m2.dummy) = Hello
572 test_expect_success
'git branch -c zz zz/zz should fail' '
573 git branch --create-reflog zz &&
574 git reflog exists refs/heads/zz &&
575 test_must_fail git branch -c zz zz/zz
578 test_expect_success
'git branch -c b/b b should fail' '
579 git branch --create-reflog b/b &&
580 test_must_fail git branch -c b/b b
583 test_expect_success
'git branch -C o/q o/p should work when o/p exists' '
584 git branch --create-reflog o/q &&
585 git reflog exists refs/heads/o/q &&
586 git reflog exists refs/heads/o/p &&
587 git branch -C o/q o/p
590 test_expect_success
'git branch -c -f o/q o/p should work when o/p exists' '
591 git reflog exists refs/heads/o/q &&
592 git reflog exists refs/heads/o/p &&
593 git branch -c -f o/q o/p
596 test_expect_success
'git branch -c qq rr/qq should fail when rr exists' '
599 test_must_fail git branch -c qq rr/qq
602 test_expect_success
'git branch -C b1 b2 should fail when b2 is checked out' '
604 git checkout -b b2 &&
605 test_must_fail git branch -C b1 b2
608 test_expect_success
'git branch -C c1 c2 should succeed when c1 is checked out' '
609 git checkout -b c1 &&
611 git branch -C c1 c2 &&
612 test $(git rev-parse --abbrev-ref HEAD) = c1
615 test_expect_success
'git branch -C c1 c2 should never touch HEAD' '
616 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
617 ! grep "$msg$" .git/logs/HEAD
620 test_expect_success
'git branch -C main should work when main is checked out' '
625 test_expect_success
'git branch -C main main should work when main is checked out' '
627 git branch -C main main
630 test_expect_success
'git branch -C main5 main5 should work when main is checked out' '
633 git branch -C main5 main5
636 test_expect_success
'git branch -C ab cd should overwrite existing config for cd' '
637 git branch --create-reflog cd &&
638 git reflog exists refs/heads/cd &&
639 git config branch.cd.dummy CD &&
640 git branch --create-reflog ab &&
641 git reflog exists refs/heads/ab &&
642 git config branch.ab.dummy AB &&
643 git branch -C ab cd &&
644 git reflog exists refs/heads/ab &&
645 git reflog exists refs/heads/cd &&
646 test $(git config branch.ab.dummy) = AB &&
647 test $(git config branch.cd.dummy) = AB
650 test_expect_success
'git branch -c correctly copies multiple config sections' '
653 test_when_finished "git checkout main" &&
654 git checkout -b source2 main &&
656 # Assert that a config file with multiple config sections has
657 # those sections preserved...
658 cat >expect <<-\EOF &&
659 branch.source2.key1=value1
660 branch.dest2.key1=value1
662 branch.source2.key2=value2
663 branch.dest2.key2=value2
665 cat >config.branch <<\EOF &&
666 ;; Note the lack of -\EOF above & mixed indenting here. This is
667 ;; intentional, we are also testing that the formatting of copied
668 ;; sections is preserved.
670 ;; Comment for source2. Tabs
672 ;; Comment for the source2 value
674 ;; Comment for more.gar. Spaces
676 ;; Comment for the more.gar value
678 ;; Comment for source2, again. Mixed tabs/spaces.
680 ;; Comment for the source2 value, again
683 cat config.branch >>.git/config &&
684 git branch -c source2 dest2 &&
685 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
686 test_cmp expect actual &&
688 # ...and that the comments and formatting for those sections
690 cat >expect <<\EOF &&
691 ;; Comment for source2. Tabs
693 ;; Comment for the source2 value
695 ;; Comment for more.gar. Spaces
697 ;; Comment for the source2 value
699 ;; Comment for more.gar. Spaces
701 ;; Comment for the more.gar value
703 ;; Comment for source2, again. Mixed tabs/spaces.
705 ;; Comment for the source2 value, again
708 ;; Comment for the source2 value, again
711 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
712 test_cmp expect actual
715 test_expect_success
'deleting a symref' '
717 git symbolic-ref refs/heads/symref refs/heads/target &&
718 echo "Deleted branch symref (was refs/heads/target)." >expect &&
719 git branch -d symref >actual &&
720 test_path_is_file .git/refs/heads/target &&
721 test_path_is_missing .git/refs/heads/symref &&
722 test_cmp expect actual
725 test_expect_success
'deleting a dangling symref' '
726 git symbolic-ref refs/heads/dangling-symref nowhere &&
727 test_path_is_file .git/refs/heads/dangling-symref &&
728 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
729 git branch -d dangling-symref >actual &&
730 test_path_is_missing .git/refs/heads/dangling-symref &&
731 test_cmp expect actual
734 test_expect_success
'deleting a self-referential symref' '
735 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
736 test_path_is_file .git/refs/heads/self-reference &&
737 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
738 git branch -d self-reference >actual &&
739 test_path_is_missing .git/refs/heads/self-reference &&
740 test_cmp expect actual
743 test_expect_success
'renaming a symref is not allowed' '
744 git symbolic-ref refs/heads/topic refs/heads/main &&
745 test_must_fail git branch -m topic new-topic &&
746 git symbolic-ref refs/heads/topic &&
747 test_path_is_file .git/refs/heads/main &&
748 test_path_is_missing .git/refs/heads/new-topic
751 test_expect_success SYMLINKS
'git branch -m u v should fail when the reflog for u is a symlink' '
752 git branch --create-reflog u &&
753 mv .git/logs/refs/heads/u real-u &&
754 ln -s real-u .git/logs/refs/heads/u &&
755 test_must_fail git branch -m u v
758 test_expect_success SYMLINKS
'git branch -m with symlinked .git/refs' '
759 test_when_finished "rm -rf subdir" &&
760 git init --bare subdir &&
762 rm -rfv subdir/refs subdir/objects subdir/packed-refs &&
763 ln -s ../.git/refs subdir/refs &&
764 ln -s ../.git/objects subdir/objects &&
765 ln -s ../.git/packed-refs subdir/packed-refs &&
767 git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
768 git rev-parse --absolute-git-dir >our.dir &&
769 ! test_cmp subdir.dir our.dir &&
772 git -C subdir branch rename-src &&
773 git rev-parse rename-src >expect &&
774 git -C subdir branch -m rename-src rename-dest &&
775 git rev-parse rename-dest >actual &&
776 test_cmp expect actual &&
777 git branch -D rename-dest
780 test_expect_success
'test tracking setup via --track' '
781 git config remote.local.url . &&
782 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
783 (git show-ref -q refs/remotes/local/main || git fetch local) &&
784 git branch --track my1 local/main &&
785 test $(git config branch.my1.remote) = local &&
786 test $(git config branch.my1.merge) = refs/heads/main
789 test_expect_success
'test tracking setup (non-wildcard, matching)' '
790 git config remote.local.url . &&
791 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
792 (git show-ref -q refs/remotes/local/main || git fetch local) &&
793 git branch --track my4 local/main &&
794 test $(git config branch.my4.remote) = local &&
795 test $(git config branch.my4.merge) = refs/heads/main
798 test_expect_success
'tracking setup fails on non-matching refspec' '
799 git config remote.local.url . &&
800 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
801 (git show-ref -q refs/remotes/local/main || git fetch local) &&
802 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
803 test_must_fail git branch --track my5 local/main &&
804 test_must_fail git config branch.my5.remote &&
805 test_must_fail git config branch.my5.merge
808 test_expect_success
'test tracking setup via config' '
809 git config branch.autosetupmerge true &&
810 git config remote.local.url . &&
811 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
812 (git show-ref -q refs/remotes/local/main || git fetch local) &&
813 git branch my3 local/main &&
814 test $(git config branch.my3.remote) = local &&
815 test $(git config branch.my3.merge) = refs/heads/main
818 test_expect_success
'test overriding tracking setup via --no-track' '
819 git config branch.autosetupmerge true &&
820 git config remote.local.url . &&
821 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
822 (git show-ref -q refs/remotes/local/main || git fetch local) &&
823 git branch --no-track my2 local/main &&
824 git config branch.autosetupmerge false &&
825 ! test "$(git config branch.my2.remote)" = local &&
826 ! test "$(git config branch.my2.merge)" = refs/heads/main
829 test_expect_success
'no tracking without .fetch entries' '
830 git config branch.autosetupmerge true &&
832 git config branch.autosetupmerge false &&
833 test -z "$(git config branch.my6.remote)" &&
834 test -z "$(git config branch.my6.merge)"
837 test_expect_success
'test tracking setup via --track but deeper' '
838 git config remote.local.url . &&
839 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
840 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
841 git branch --track my7 local/o/o &&
842 test "$(git config branch.my7.remote)" = local &&
843 test "$(git config branch.my7.merge)" = refs/heads/o/o
846 test_expect_success
'test deleting branch deletes branch config' '
848 test -z "$(git config branch.my7.remote)" &&
849 test -z "$(git config branch.my7.merge)"
852 test_expect_success
'test deleting branch without config' '
854 sha1=$(git rev-parse my7 | cut -c 1-7) &&
855 echo "Deleted branch my7 (was $sha1)." >expect &&
856 git branch -d my7 >actual 2>&1 &&
857 test_cmp expect actual
860 test_expect_success
'deleting currently checked out branch fails' '
861 git worktree add -b my7 my7 &&
862 test_must_fail git -C my7 branch -d my7 &&
863 test_must_fail git branch -d my7 &&
868 test_expect_success
'test --track without .fetch entries' '
869 git branch --track my8 &&
870 test "$(git config branch.my8.remote)" &&
871 test "$(git config branch.my8.merge)"
874 test_expect_success
'branch from non-branch HEAD w/autosetupmerge=always' '
875 git config branch.autosetupmerge always &&
876 git branch my9 HEAD^ &&
877 git config branch.autosetupmerge false
880 test_expect_success
'branch from non-branch HEAD w/--track causes failure' '
881 test_must_fail git branch --track my10 HEAD^
884 test_expect_success
'branch from tag w/--track causes failure' '
886 test_must_fail git branch --track my11 foobar
889 test_expect_success
'simple tracking works when remote branch name matches' '
890 test_when_finished "rm -rf otherserver" &&
891 git init otherserver &&
892 test_commit -C otherserver my_commit 1 &&
893 git -C otherserver branch feature &&
894 test_config branch.autosetupmerge simple &&
895 test_config remote.otherserver.url otherserver &&
896 test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* &&
897 git fetch otherserver &&
898 git branch feature otherserver/feature &&
899 test_cmp_config otherserver branch.feature.remote &&
900 test_cmp_config refs/heads/feature branch.feature.merge
903 test_expect_success
'simple tracking skips when remote branch name does not match' '
904 test_config branch.autosetupmerge simple &&
905 test_config remote.local.url . &&
906 test_config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
908 git branch my-other local/main &&
909 test_cmp_config "" --default "" branch.my-other.remote &&
910 test_cmp_config "" --default "" branch.my-other.merge
913 test_expect_success
'simple tracking skips when remote ref is not a branch' '
914 test_config branch.autosetupmerge simple &&
915 test_config remote.localtags.url . &&
916 test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* &&
917 git tag mytag12 main &&
918 git fetch localtags &&
919 git branch mytag12 localtags/mytag12 &&
920 test_cmp_config "" --default "" branch.mytag12.remote &&
921 test_cmp_config "" --default "" branch.mytag12.merge
924 test_expect_success
'--set-upstream-to fails on multiple branches' '
925 echo "fatal: too many arguments to set new upstream" >expect &&
926 test_must_fail git branch --set-upstream-to main a b c 2>err &&
930 test_expect_success
'--set-upstream-to fails on detached HEAD' '
931 git checkout HEAD^{} &&
932 test_when_finished git checkout - &&
933 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch." >expect &&
934 test_must_fail git branch --set-upstream-to main 2>err &&
938 test_expect_success
'--set-upstream-to fails on a missing dst branch' '
939 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
940 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
944 test_expect_success
'--set-upstream-to fails on a missing src branch' '
945 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
946 test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
949 test_expect_success
'--set-upstream-to fails on a non-ref' '
950 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
951 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
955 test_expect_success
'--set-upstream-to fails on locked config' '
956 test_when_finished "rm -f .git/config.lock" &&
959 test_must_fail git branch --set-upstream-to locked 2>err &&
960 test_i18ngrep "could not lock config file .git/config" err
963 test_expect_success
'use --set-upstream-to modify HEAD' '
964 test_config branch.main.remote foo &&
965 test_config branch.main.merge foo &&
967 git branch --set-upstream-to my12 &&
968 test "$(git config branch.main.remote)" = "." &&
969 test "$(git config branch.main.merge)" = "refs/heads/my12"
972 test_expect_success
'use --set-upstream-to modify a particular branch' '
974 git branch --set-upstream-to main my13 &&
975 test_when_finished "git branch --unset-upstream my13" &&
976 test "$(git config branch.my13.remote)" = "." &&
977 test "$(git config branch.my13.merge)" = "refs/heads/main"
980 test_expect_success
'--unset-upstream should fail if given a non-existent branch' '
981 echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
982 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
986 test_expect_success
'--unset-upstream should fail if config is locked' '
987 test_when_finished "rm -f .git/config.lock" &&
988 git branch --set-upstream-to locked &&
990 test_must_fail git branch --unset-upstream 2>err &&
991 test_i18ngrep "could not lock config file .git/config" err
994 test_expect_success
'test --unset-upstream on HEAD' '
996 test_config branch.main.remote foo &&
997 test_config branch.main.merge foo &&
998 git branch --set-upstream-to my14 &&
999 git branch --unset-upstream &&
1000 test_must_fail git config branch.main.remote &&
1001 test_must_fail git config branch.main.merge &&
1002 # fail for a branch without upstream set
1003 echo "fatal: Branch '"'"'main'"'"' has no upstream information" >expect &&
1004 test_must_fail git branch --unset-upstream 2>err &&
1008 test_expect_success
'--unset-upstream should fail on multiple branches' '
1009 echo "fatal: too many arguments to unset upstream" >expect &&
1010 test_must_fail git branch --unset-upstream a b c 2>err &&
1014 test_expect_success
'--unset-upstream should fail on detached HEAD' '
1015 git checkout HEAD^{} &&
1016 test_when_finished git checkout - &&
1017 echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect &&
1018 test_must_fail git branch --unset-upstream 2>err &&
1022 test_expect_success
'test --unset-upstream on a particular branch' '
1024 git branch --set-upstream-to main my14 &&
1025 git branch --unset-upstream my14 &&
1026 test_must_fail git config branch.my14.remote &&
1027 test_must_fail git config branch.my14.merge
1030 test_expect_success
'disabled option --set-upstream fails' '
1031 test_must_fail git branch --set-upstream origin/main
1034 test_expect_success
'--set-upstream-to notices an error to set branch as own upstream' "
1035 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1036 cat >expect <<-\EOF &&
1037 warning: not setting branch 'my13' as its own upstream
1039 test_expect_code 1 git config branch.my13.remote &&
1040 test_expect_code 1 git config branch.my13.merge &&
1041 test_cmp expect actual
1044 # Keep this test last, as it changes the current branch
1046 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
1048 test_expect_success
'git checkout -b g/h/i -l should create a branch and a log' '
1049 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1050 git checkout -b g/h/i -l main &&
1051 test_path_is_file .git/refs/heads/g/h/i &&
1052 test_path_is_file .git/logs/refs/heads/g/h/i &&
1053 test_cmp expect .git/logs/refs/heads/g/h/i
1056 test_expect_success
'checkout -b makes reflog by default' '
1057 git checkout main &&
1058 git config --unset core.logAllRefUpdates &&
1059 git checkout -b alpha &&
1060 git rev-parse --verify alpha@{0}
1063 test_expect_success
'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1064 git checkout main &&
1065 git config core.logAllRefUpdates false &&
1066 git checkout -b beta &&
1067 test_must_fail git rev-parse --verify beta@{0}
1070 test_expect_success
'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1071 git checkout main &&
1072 git checkout -lb gamma &&
1073 git config --unset core.logAllRefUpdates &&
1074 git rev-parse --verify gamma@{0}
1077 test_expect_success
'avoid ambiguous track and advise' '
1078 git config branch.autosetupmerge true &&
1079 git config remote.ambi1.url lalala &&
1080 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1081 git config remote.ambi2.url lilili &&
1082 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1083 cat <<-EOF >expected &&
1084 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1085 hint: There are multiple remotes whose fetch refspecs map to the remote
1086 hint: tracking ref '\''refs/heads/main'\'':
1090 hint: This is typically a configuration error.
1092 hint: To support setting up tracking branches, ensure that
1093 hint: different remotes'\'' fetch refspecs map into different
1094 hint: tracking namespaces.
1096 test_must_fail git branch all1 main 2>actual &&
1097 test_cmp expected actual &&
1098 test -z "$(git config branch.all1.merge)"
1101 test_expect_success
'autosetuprebase local on a tracked local branch' '
1102 git config remote.local.url . &&
1103 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1104 git config branch.autosetuprebase local &&
1105 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1106 git branch mybase &&
1107 git branch --track myr1 mybase &&
1108 test "$(git config branch.myr1.remote)" = . &&
1109 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1110 test "$(git config branch.myr1.rebase)" = true
1113 test_expect_success
'autosetuprebase always on a tracked local branch' '
1114 git config remote.local.url . &&
1115 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1116 git config branch.autosetuprebase always &&
1117 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1118 git branch mybase2 &&
1119 git branch --track myr2 mybase &&
1120 test "$(git config branch.myr2.remote)" = . &&
1121 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1122 test "$(git config branch.myr2.rebase)" = true
1125 test_expect_success
'autosetuprebase remote on a tracked local branch' '
1126 git config remote.local.url . &&
1127 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1128 git config branch.autosetuprebase remote &&
1129 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1130 git branch mybase3 &&
1131 git branch --track myr3 mybase2 &&
1132 test "$(git config branch.myr3.remote)" = . &&
1133 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1134 ! test "$(git config branch.myr3.rebase)" = true
1137 test_expect_success
'autosetuprebase never on a tracked local branch' '
1138 git config remote.local.url . &&
1139 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1140 git config branch.autosetuprebase never &&
1141 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1142 git branch mybase4 &&
1143 git branch --track myr4 mybase2 &&
1144 test "$(git config branch.myr4.remote)" = . &&
1145 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1146 ! test "$(git config branch.myr4.rebase)" = true
1149 test_expect_success
'autosetuprebase local on a tracked remote branch' '
1150 git config remote.local.url . &&
1151 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1152 git config branch.autosetuprebase local &&
1153 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1154 git branch --track myr5 local/main &&
1155 test "$(git config branch.myr5.remote)" = local &&
1156 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1157 ! test "$(git config branch.myr5.rebase)" = true
1160 test_expect_success
'autosetuprebase never on a tracked remote branch' '
1161 git config remote.local.url . &&
1162 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1163 git config branch.autosetuprebase never &&
1164 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1165 git branch --track myr6 local/main &&
1166 test "$(git config branch.myr6.remote)" = local &&
1167 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1168 ! test "$(git config branch.myr6.rebase)" = true
1171 test_expect_success
'autosetuprebase remote on a tracked remote branch' '
1172 git config remote.local.url . &&
1173 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1174 git config branch.autosetuprebase remote &&
1175 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1176 git branch --track myr7 local/main &&
1177 test "$(git config branch.myr7.remote)" = local &&
1178 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1179 test "$(git config branch.myr7.rebase)" = true
1182 test_expect_success
'autosetuprebase always on a tracked remote branch' '
1183 git config remote.local.url . &&
1184 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1185 git config branch.autosetuprebase remote &&
1186 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1187 git branch --track myr8 local/main &&
1188 test "$(git config branch.myr8.remote)" = local &&
1189 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1190 test "$(git config branch.myr8.rebase)" = true
1193 test_expect_success
'autosetuprebase unconfigured on a tracked remote branch' '
1194 git config --unset branch.autosetuprebase &&
1195 git config remote.local.url . &&
1196 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1197 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1198 git branch --track myr9 local/main &&
1199 test "$(git config branch.myr9.remote)" = local &&
1200 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1201 test "z$(git config branch.myr9.rebase)" = z
1204 test_expect_success
'autosetuprebase unconfigured on a tracked local branch' '
1205 git config remote.local.url . &&
1206 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1207 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1208 git branch mybase10 &&
1209 git branch --track myr10 mybase2 &&
1210 test "$(git config branch.myr10.remote)" = . &&
1211 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1212 test "z$(git config branch.myr10.rebase)" = z
1215 test_expect_success
'autosetuprebase unconfigured on untracked local branch' '
1216 git config remote.local.url . &&
1217 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1218 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1219 git branch --no-track myr11 mybase2 &&
1220 test "z$(git config branch.myr11.remote)" = z &&
1221 test "z$(git config branch.myr11.merge)" = z &&
1222 test "z$(git config branch.myr11.rebase)" = z
1225 test_expect_success
'autosetuprebase unconfigured on untracked remote branch' '
1226 git config remote.local.url . &&
1227 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1228 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1229 git branch --no-track myr12 local/main &&
1230 test "z$(git config branch.myr12.remote)" = z &&
1231 test "z$(git config branch.myr12.merge)" = z &&
1232 test "z$(git config branch.myr12.rebase)" = z
1235 test_expect_success
'autosetuprebase never on an untracked local branch' '
1236 git config branch.autosetuprebase never &&
1237 git config remote.local.url . &&
1238 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1239 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1240 git branch --no-track myr13 mybase2 &&
1241 test "z$(git config branch.myr13.remote)" = z &&
1242 test "z$(git config branch.myr13.merge)" = z &&
1243 test "z$(git config branch.myr13.rebase)" = z
1246 test_expect_success
'autosetuprebase local on an untracked local branch' '
1247 git config branch.autosetuprebase local &&
1248 git config remote.local.url . &&
1249 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1250 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1251 git branch --no-track myr14 mybase2 &&
1252 test "z$(git config branch.myr14.remote)" = z &&
1253 test "z$(git config branch.myr14.merge)" = z &&
1254 test "z$(git config branch.myr14.rebase)" = z
1257 test_expect_success
'autosetuprebase remote on an untracked local branch' '
1258 git config branch.autosetuprebase remote &&
1259 git config remote.local.url . &&
1260 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1261 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1262 git branch --no-track myr15 mybase2 &&
1263 test "z$(git config branch.myr15.remote)" = z &&
1264 test "z$(git config branch.myr15.merge)" = z &&
1265 test "z$(git config branch.myr15.rebase)" = z
1268 test_expect_success
'autosetuprebase always on an untracked local branch' '
1269 git config branch.autosetuprebase always &&
1270 git config remote.local.url . &&
1271 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1272 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1273 git branch --no-track myr16 mybase2 &&
1274 test "z$(git config branch.myr16.remote)" = z &&
1275 test "z$(git config branch.myr16.merge)" = z &&
1276 test "z$(git config branch.myr16.rebase)" = z
1279 test_expect_success
'autosetuprebase never on an untracked remote branch' '
1280 git config branch.autosetuprebase never &&
1281 git config remote.local.url . &&
1282 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1283 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1284 git branch --no-track myr17 local/main &&
1285 test "z$(git config branch.myr17.remote)" = z &&
1286 test "z$(git config branch.myr17.merge)" = z &&
1287 test "z$(git config branch.myr17.rebase)" = z
1290 test_expect_success
'autosetuprebase local on an untracked remote branch' '
1291 git config branch.autosetuprebase local &&
1292 git config remote.local.url . &&
1293 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1294 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1295 git branch --no-track myr18 local/main &&
1296 test "z$(git config branch.myr18.remote)" = z &&
1297 test "z$(git config branch.myr18.merge)" = z &&
1298 test "z$(git config branch.myr18.rebase)" = z
1301 test_expect_success
'autosetuprebase remote on an untracked remote branch' '
1302 git config branch.autosetuprebase remote &&
1303 git config remote.local.url . &&
1304 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1305 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1306 git branch --no-track myr19 local/main &&
1307 test "z$(git config branch.myr19.remote)" = z &&
1308 test "z$(git config branch.myr19.merge)" = z &&
1309 test "z$(git config branch.myr19.rebase)" = z
1312 test_expect_success
'autosetuprebase always on an untracked remote branch' '
1313 git config branch.autosetuprebase always &&
1314 git config remote.local.url . &&
1315 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1316 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1317 git branch --no-track myr20 local/main &&
1318 test "z$(git config branch.myr20.remote)" = z &&
1319 test "z$(git config branch.myr20.merge)" = z &&
1320 test "z$(git config branch.myr20.rebase)" = z
1323 test_expect_success
'autosetuprebase always on detached HEAD' '
1324 git config branch.autosetupmerge always &&
1325 test_when_finished git checkout main &&
1326 git checkout HEAD^0 &&
1328 test -z "$(git config branch.my11.remote)" &&
1329 test -z "$(git config branch.my11.merge)"
1332 test_expect_success
'detect misconfigured autosetuprebase (bad value)' '
1333 git config branch.autosetuprebase garbage &&
1334 test_must_fail git branch
1337 test_expect_success
'detect misconfigured autosetuprebase (no value)' '
1338 git config --unset branch.autosetuprebase &&
1339 echo "[branch] autosetuprebase" >>.git/config &&
1340 test_must_fail git branch &&
1341 git config --unset branch.autosetuprebase
1344 test_expect_success
'attempt to delete a branch without base and unmerged to HEAD' '
1346 git config --unset branch.my8.merge &&
1347 test_must_fail git branch -d my8
1350 test_expect_success
'attempt to delete a branch merged to its base' '
1351 # we are on my9 which is the initial commit; traditionally
1352 # we would not have allowed deleting my8 that is not merged
1353 # to my9, but it is set to track main that already has my8
1354 git config branch.my8.merge refs/heads/main &&
1358 test_expect_success
'attempt to delete a branch merged to its base' '
1359 git checkout main &&
1361 git commit -m "Third commit" A &&
1362 git branch -t my10 my9 &&
1363 git branch -f my10 HEAD^ &&
1364 # we are on main which is at the third commit, and my10
1365 # is behind us, so traditionally we would have allowed deleting
1366 # it; but my10 is set to track my9 that is further behind.
1367 test_must_fail git branch -d my10
1370 test_expect_success
'branch --delete --force removes dangling branch' '
1371 git checkout main &&
1372 test_commit unstable &&
1373 hash=$(git rev-parse HEAD) &&
1374 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1375 git branch --no-track dangling &&
1376 mv $objpath $objpath.x &&
1377 test_when_finished "mv $objpath.x $objpath" &&
1378 git branch --delete --force dangling &&
1379 git for-each-ref refs/heads/dangling >actual &&
1380 test_must_be_empty actual
1383 test_expect_success
'use --edit-description' '
1384 write_script editor <<-\EOF &&
1385 echo "New contents" >"$1"
1387 EDITOR=./editor git branch --edit-description &&
1388 write_script editor <<-\EOF &&
1389 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1391 EDITOR=./editor git branch --edit-description &&
1392 echo "New contents" >expect &&
1393 test_cmp expect EDITOR_OUTPUT
1396 test_expect_success
'detect typo in branch name when using --edit-description' '
1397 write_script editor <<-\EOF &&
1398 echo "New contents" >"$1"
1400 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1403 test_expect_success
'refuse --edit-description on unborn branch for now' '
1404 test_when_finished "git checkout main" &&
1405 write_script editor <<-\EOF &&
1406 echo "New contents" >"$1"
1408 git checkout --orphan unborn &&
1409 test_must_fail env EDITOR=./editor git branch --edit-description
1412 test_expect_success
'--merged catches invalid object names' '
1413 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1416 test_expect_success
'--list during rebase' '
1417 test_when_finished "reset_rebase" &&
1418 git checkout main &&
1419 FAKE_LINES="1 edit 2" &&
1420 export FAKE_LINES &&
1422 git rebase -i HEAD~2 &&
1423 git branch --list >actual &&
1424 test_i18ngrep "rebasing main" actual
1427 test_expect_success
'--list during rebase from detached HEAD' '
1428 test_when_finished "reset_rebase && git checkout main" &&
1429 git checkout main^0 &&
1430 oid=$(git rev-parse --short HEAD) &&
1431 FAKE_LINES="1 edit 2" &&
1432 export FAKE_LINES &&
1434 git rebase -i HEAD~2 &&
1435 git branch --list >actual &&
1436 test_i18ngrep "rebasing detached HEAD $oid" actual
1439 test_expect_success
'tracking with unexpected .fetch refspec' '
1441 git init -b main a &&
1446 git init -b main b &&
1451 git init -b main c &&
1455 git remote add a ../a &&
1456 git remote add b ../b &&
1459 git init -b main d &&
1462 git remote add c ../c &&
1463 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1465 git branch --track local/a/main remotes/a/main &&
1466 test "$(git config branch.local/a/main.remote)" = "c" &&
1467 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1468 git rev-parse --verify a >expect &&
1469 git rev-parse --verify local/a/main >actual &&
1470 test_cmp expect actual
1474 test_expect_success
'configured committerdate sort' '
1475 git init -b main sort &&
1478 git config branch.sort committerdate &&
1479 test_commit initial &&
1480 git checkout -b a &&
1482 git checkout -b c &&
1484 git checkout -b b &&
1486 git branch >actual &&
1487 cat >expect <<-\EOF &&
1493 test_cmp expect actual
1497 test_expect_success
'option override configured sort' '
1500 git config branch.sort committerdate &&
1501 git branch --sort=refname >actual &&
1502 cat >expect <<-\EOF &&
1508 test_cmp expect actual
1512 test_expect_success
'invalid sort parameter in configuration' '
1515 git config branch.sort "v:notvalid" &&
1517 # this works in the "listing" mode, so bad sort key
1518 # is a dying offence.
1519 test_must_fail git branch &&
1521 # these do not need to use sorting, and should all
1523 git branch newone main &&
1524 git branch -c newone newerone &&
1525 git branch -m newone newestone &&
1526 git branch -d newerone newestone
1530 test_expect_success
'tracking info copied with --track=inherit' '
1531 git branch --track=inherit foo2 my1 &&
1532 test_cmp_config local branch.foo2.remote &&
1533 test_cmp_config refs/heads/main branch.foo2.merge
1536 test_expect_success
'tracking info copied with autoSetupMerge=inherit' '
1537 test_unconfig branch.autoSetupMerge &&
1538 # default config does not copy tracking info
1539 git branch foo-no-inherit my1 &&
1540 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1541 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1542 # with autoSetupMerge=inherit, we copy tracking info from my1
1543 test_config branch.autoSetupMerge inherit &&
1544 git branch foo3 my1 &&
1545 test_cmp_config local branch.foo3.remote &&
1546 test_cmp_config refs/heads/main branch.foo3.merge &&
1547 # no tracking info to inherit from main
1548 git branch main2 main &&
1549 test_cmp_config "" --default "" branch.main2.remote &&
1550 test_cmp_config "" --default "" branch.main2.merge
1553 test_expect_success
'--track overrides branch.autoSetupMerge' '
1554 test_config branch.autoSetupMerge inherit &&
1555 git branch --track=direct foo4 my1 &&
1556 test_cmp_config . branch.foo4.remote &&
1557 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1558 git branch --no-track foo5 my1 &&
1559 test_cmp_config "" --default "" branch.foo5.remote &&
1560 test_cmp_config "" --default "" branch.foo5.merge