3 # Copyright (c) 2005 Amos Waterland
6 test_description='git branch assorted tests'
9 . "$TEST_DIRECTORY"/lib-rebase.sh
11 test_expect_success 'prepare a trivial repository' '
13 git update-index --add A &&
14 git commit -m "Initial commit." &&
17 git update-index --add A &&
18 git commit -m "Second commit." &&
19 HEAD=$(git rev-parse --verify HEAD)
22 test_expect_success 'git branch --help should not have created a bogus branch' '
23 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
24 test_path_is_missing .git/refs/heads/--help
27 test_expect_success 'branch -h in broken repository' '
32 >.git/refs/heads/main &&
33 test_expect_code 129 git branch -h >usage 2>&1
35 test_i18ngrep "[Uu]sage" broken/usage
38 test_expect_success 'git branch abc should create a branch' '
39 git branch abc && test_path_is_file .git/refs/heads/abc
42 test_expect_success 'git branch a/b/c should create a branch' '
43 git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
46 test_expect_success 'git branch mb main... should create a branch' '
47 git branch mb main... && test_path_is_file .git/refs/heads/mb
50 test_expect_success 'git branch HEAD should fail' '
51 test_must_fail git branch HEAD
55 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
57 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
58 GIT_COMMITTER_DATE="2005-05-26 23:30" \
59 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
60 test_path_is_file .git/refs/heads/d/e/f &&
61 test_path_is_file .git/logs/refs/heads/d/e/f &&
62 test_cmp expect .git/logs/refs/heads/d/e/f
65 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
66 git branch -d d/e/f &&
67 test_path_is_missing .git/refs/heads/d/e/f &&
68 test_must_fail git reflog exists refs/heads/d/e/f
71 test_expect_success 'git branch j/k should work after branch j has been deleted' '
77 test_expect_success 'git branch l should work after branch l/m has been deleted' '
83 test_expect_success 'git branch -m dumps usage' '
84 test_expect_code 128 git branch -m 2>err &&
85 test_i18ngrep "branch name required" err
88 test_expect_success 'git branch -m m broken_symref should work' '
89 test_when_finished "git branch -D broken_symref" &&
90 git branch --create-reflog m &&
91 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
92 git branch -m m broken_symref &&
93 git reflog exists refs/heads/broken_symref &&
94 test_must_fail git reflog exists refs/heads/i_am_broken
97 test_expect_success 'git branch -m m m/m should work' '
98 git branch --create-reflog m &&
99 git branch -m m m/m &&
100 git reflog exists refs/heads/m/m
103 test_expect_success 'git branch -m n/n n should work' '
104 git branch --create-reflog n/n &&
105 git branch -m n/n n &&
106 git reflog exists refs/heads/n
109 # The topmost entry in reflog for branch bbb is about branch creation.
110 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
112 test_expect_success 'git branch -m bbb should rename checked out branch' '
113 test_when_finished git branch -D bbb &&
114 test_when_finished git checkout main &&
115 git checkout -b aaa &&
116 git commit --allow-empty -m "a new commit" &&
117 git rev-parse aaa@{0} >expect &&
119 git rev-parse bbb@{1} >actual &&
120 test_cmp expect actual &&
121 git symbolic-ref HEAD >actual &&
122 echo refs/heads/bbb >expect &&
123 test_cmp expect actual
126 test_expect_success 'renaming checked out branch works with d/f conflict' '
127 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
128 test_when_finished git checkout main &&
129 git checkout -b foo &&
130 git branch -m foo/bar &&
131 git symbolic-ref HEAD >actual &&
132 echo refs/heads/foo/bar >expect &&
133 test_cmp expect actual
136 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
139 test_must_fail git branch -m o/o o
142 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
144 test_must_fail git branch -m o/q o/p
147 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
148 git branch -M o/q o/p
151 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
153 git branch -m -f o/q o/p
156 test_expect_success 'git branch -m q r/q should fail when r exists' '
159 test_must_fail git branch -m q r/q
162 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
164 git checkout -b foo &&
165 test_must_fail git branch -M bar foo
168 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
169 git checkout -b baz &&
171 git branch -M baz bam &&
172 test $(git rev-parse --abbrev-ref HEAD) = bam
175 test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
176 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
177 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
178 grep "^0\{40\}.*$msg$" .git/logs/HEAD
181 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
182 git init -b main orphan &&
185 test_commit initial &&
186 git checkout --orphan lonely &&
187 grep lonely .git/HEAD &&
188 test_path_is_missing .git/refs/head/lonely &&
189 git branch -M main mistress &&
190 grep lonely .git/HEAD
194 test_expect_success 'resulting reflog can be shown by log -g' '
195 oid=$(git rev-parse HEAD) &&
196 cat >expect <<-EOF &&
198 HEAD@{2} $oid checkout: moving from foo to baz
200 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
201 test_cmp expect actual
204 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
206 git worktree add -b baz bazdir &&
207 git worktree add -f bazdir2 baz &&
208 git branch -M baz bam &&
209 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
210 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
211 rm -r bazdir bazdir2 &&
215 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
216 git checkout -b baz &&
217 git worktree add -f bazdir baz &&
220 git branch -M baz bam &&
221 test $(git rev-parse --abbrev-ref HEAD) = bam
223 test $(git rev-parse --abbrev-ref HEAD) = bam &&
228 test_expect_success 'git branch -M main should work when main is checked out' '
233 test_expect_success 'git branch -M main main should work when main is checked out' '
235 git branch -M main main
238 test_expect_success 'git branch -M topic topic should work when main is checked out' '
241 git branch -M topic topic
244 test_expect_success 'git branch -v -d t should work' '
246 git rev-parse --verify refs/heads/t &&
247 git branch -v -d t &&
248 test_must_fail git rev-parse --verify refs/heads/t
251 test_expect_success 'git branch -v -m t s should work' '
253 git rev-parse --verify refs/heads/t &&
254 git branch -v -m t s &&
255 test_must_fail git rev-parse --verify refs/heads/t &&
256 git rev-parse --verify refs/heads/s &&
260 test_expect_success 'git branch -m -d t s should fail' '
262 git rev-parse refs/heads/t &&
263 test_must_fail git branch -m -d t s &&
265 test_must_fail git rev-parse refs/heads/t
268 test_expect_success 'git branch --list -d t should fail' '
270 git rev-parse refs/heads/t &&
271 test_must_fail git branch --list -d t &&
273 test_must_fail git rev-parse refs/heads/t
276 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
277 test_when_finished "rm -rf repo1 repo2" &&
280 git init repo1/sub &&
281 test_commit -C repo1/sub x &&
282 git -C repo1 submodule add ./sub &&
283 git -C repo1 commit -m "adding sub" &&
285 git clone --recurse-submodules repo1 repo2 &&
286 git -C repo2/sub checkout -b work &&
287 test_must_fail git -C repo2/sub branch -D work
290 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
291 test_when_finished "rm -rf nonbare base secondary" &&
293 git init -b main nonbare &&
294 test_commit -C nonbare x &&
295 git clone --bare nonbare bare &&
296 git -C bare worktree add --detach ../secondary main &&
297 git -C secondary branch -D main
300 test_expect_success 'git branch --list -v with --abbrev' '
301 test_when_finished "git branch -D t" &&
303 git branch -v --list t >actual.default &&
304 git branch -v --list --abbrev t >actual.abbrev &&
305 test_cmp actual.default actual.abbrev &&
307 git branch -v --list --no-abbrev t >actual.noabbrev &&
308 git branch -v --list --abbrev=0 t >actual.0abbrev &&
309 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
310 test_cmp actual.noabbrev actual.0abbrev &&
311 test_cmp actual.noabbrev actual.noabbrev-conf &&
313 git branch -v --list --abbrev=36 t >actual.36abbrev &&
314 # how many hexdigits are used?
315 read name objdefault rest <actual.abbrev &&
316 read name obj36 rest <actual.36abbrev &&
317 objfull=$(git rev-parse --verify t) &&
319 # are we really getting abbreviations?
320 test "$obj36" != "$objdefault" &&
321 expr "$obj36" : "$objdefault" >/dev/null &&
322 test "$objfull" != "$obj36" &&
323 expr "$objfull" : "$obj36" >/dev/null
327 test_expect_success 'git branch --column' '
328 COLUMNS=81 git branch --column=column >actual &&
329 cat >expect <<\EOF &&
330 a/b/c bam foo l * main n o/p r
331 abc bar j/k m/m mb o/o q topic
333 test_cmp expect actual
336 test_expect_success 'git branch --column with an extremely long branch name' '
337 long=this/is/a/part/of/long/branch/name &&
338 long=z$long/$long/$long/$long &&
339 test_when_finished "git branch -d $long" &&
341 COLUMNS=80 git branch --column=column >actual &&
361 test_cmp expect actual
364 test_expect_success 'git branch with column.*' '
365 git config column.ui column &&
366 git config column.branch "dense" &&
367 COLUMNS=80 git branch >actual &&
368 git config --unset column.branch &&
369 git config --unset column.ui &&
370 cat >expect <<\EOF &&
371 a/b/c bam foo l * main n o/p r
372 abc bar j/k m/m mb o/o q topic
374 test_cmp expect actual
377 test_expect_success 'git branch --column -v should fail' '
378 test_must_fail git branch --column -v
381 test_expect_success 'git branch -v with column.ui ignored' '
382 git config column.ui column &&
383 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
384 git config --unset column.ui &&
385 cat >expect <<\EOF &&
403 test_cmp expect actual
406 mv .git/config .git/config-saved
408 test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
409 git branch -m q q2 &&
413 mv .git/config-saved .git/config
415 git config branch.s/s.dummy Hello
417 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
418 git branch --create-reflog s/s &&
419 git reflog exists refs/heads/s/s &&
420 git branch --create-reflog s/t &&
421 git reflog exists refs/heads/s/t &&
423 git branch -m s/s s &&
424 git reflog exists refs/heads/s
427 test_expect_success 'config information was renamed, too' '
428 test $(git config branch.s.dummy) = Hello &&
429 test_must_fail git config branch.s/s.dummy
432 test_expect_success 'git branch -m correctly renames multiple config sections' '
433 test_when_finished "git checkout main" &&
434 git checkout -b source main &&
436 # Assert that a config file with multiple config sections has
437 # those sections preserved...
438 cat >expect <<-\EOF &&
439 branch.dest.key1=value1
441 branch.dest.key2=value2
443 cat >config.branch <<\EOF &&
444 ;; Note the lack of -\EOF above & mixed indenting here. This is
445 ;; intentional, we are also testing that the formatting of copied
446 ;; sections is preserved.
448 ;; Comment for source. Tabs
450 ;; Comment for the source value
452 ;; Comment for some.gar. Spaces
454 ;; Comment for the some.gar value
456 ;; Comment for source, again. Mixed tabs/spaces.
458 ;; Comment for the source value, again
461 cat config.branch >>.git/config &&
462 git branch -m source dest &&
463 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
464 test_cmp expect actual &&
466 # ...and that the comments for those sections are also
468 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
469 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
470 test_cmp expect actual
473 test_expect_success 'git branch -c dumps usage' '
474 test_expect_code 128 git branch -c 2>err &&
475 test_i18ngrep "branch name required" err
478 test_expect_success 'git branch --copy dumps usage' '
479 test_expect_code 128 git branch --copy 2>err &&
480 test_i18ngrep "branch name required" err
483 test_expect_success 'git branch -c d e should work' '
484 git branch --create-reflog d &&
485 git reflog exists refs/heads/d &&
486 git config branch.d.dummy Hello &&
488 git reflog exists refs/heads/d &&
489 git reflog exists refs/heads/e &&
490 echo Hello >expect &&
491 git config branch.e.dummy >actual &&
492 test_cmp expect actual &&
493 echo Hello >expect &&
494 git config branch.d.dummy >actual &&
495 test_cmp expect actual
498 test_expect_success 'git branch --copy is a synonym for -c' '
499 git branch --create-reflog copy &&
500 git reflog exists refs/heads/copy &&
501 git config branch.copy.dummy Hello &&
502 git branch --copy copy copy-to &&
503 git reflog exists refs/heads/copy &&
504 git reflog exists refs/heads/copy-to &&
505 echo Hello >expect &&
506 git config branch.copy.dummy >actual &&
507 test_cmp expect actual &&
508 echo Hello >expect &&
509 git config branch.copy-to.dummy >actual &&
510 test_cmp expect actual
513 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
514 git checkout -b ee &&
515 git reflog exists refs/heads/ee &&
516 git config branch.ee.dummy Hello &&
517 git branch -c ee ef &&
518 git reflog exists refs/heads/ee &&
519 git reflog exists refs/heads/ef &&
520 test $(git config branch.ee.dummy) = Hello &&
521 test $(git config branch.ef.dummy) = Hello &&
522 test $(git rev-parse --abbrev-ref HEAD) = ee
525 test_expect_success 'git branch -c f/f g/g should work' '
526 git branch --create-reflog f/f &&
527 git reflog exists refs/heads/f/f &&
528 git config branch.f/f.dummy Hello &&
529 git branch -c f/f g/g &&
530 git reflog exists refs/heads/f/f &&
531 git reflog exists refs/heads/g/g &&
532 test $(git config branch.f/f.dummy) = Hello &&
533 test $(git config branch.g/g.dummy) = Hello
536 test_expect_success 'git branch -c m2 m2 should work' '
537 git branch --create-reflog m2 &&
538 git reflog exists refs/heads/m2 &&
539 git config branch.m2.dummy Hello &&
540 git branch -c m2 m2 &&
541 git reflog exists refs/heads/m2 &&
542 test $(git config branch.m2.dummy) = Hello
545 test_expect_success 'git branch -c zz zz/zz should fail' '
546 git branch --create-reflog zz &&
547 git reflog exists refs/heads/zz &&
548 test_must_fail git branch -c zz zz/zz
551 test_expect_success 'git branch -c b/b b should fail' '
552 git branch --create-reflog b/b &&
553 test_must_fail git branch -c b/b b
556 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
557 git branch --create-reflog o/q &&
558 git reflog exists refs/heads/o/q &&
559 git reflog exists refs/heads/o/p &&
560 git branch -C o/q o/p
563 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
564 git reflog exists refs/heads/o/q &&
565 git reflog exists refs/heads/o/p &&
566 git branch -c -f o/q o/p
569 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
572 test_must_fail git branch -c qq rr/qq
575 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
577 git checkout -b b2 &&
578 test_must_fail git branch -C b1 b2
581 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
582 git checkout -b c1 &&
584 git branch -C c1 c2 &&
585 test $(git rev-parse --abbrev-ref HEAD) = c1
588 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
589 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
590 ! grep "$msg$" .git/logs/HEAD
593 test_expect_success 'git branch -C main should work when main is checked out' '
598 test_expect_success 'git branch -C main main should work when main is checked out' '
600 git branch -C main main
603 test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
606 git branch -C main5 main5
609 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
610 git branch --create-reflog cd &&
611 git reflog exists refs/heads/cd &&
612 git config branch.cd.dummy CD &&
613 git branch --create-reflog ab &&
614 git reflog exists refs/heads/ab &&
615 git config branch.ab.dummy AB &&
616 git branch -C ab cd &&
617 git reflog exists refs/heads/ab &&
618 git reflog exists refs/heads/cd &&
619 test $(git config branch.ab.dummy) = AB &&
620 test $(git config branch.cd.dummy) = AB
623 test_expect_success 'git branch -c correctly copies multiple config sections' '
626 test_when_finished "git checkout main" &&
627 git checkout -b source2 main &&
629 # Assert that a config file with multiple config sections has
630 # those sections preserved...
631 cat >expect <<-\EOF &&
632 branch.source2.key1=value1
633 branch.dest2.key1=value1
635 branch.source2.key2=value2
636 branch.dest2.key2=value2
638 cat >config.branch <<\EOF &&
639 ;; Note the lack of -\EOF above & mixed indenting here. This is
640 ;; intentional, we are also testing that the formatting of copied
641 ;; sections is preserved.
643 ;; Comment for source2. Tabs
645 ;; Comment for the source2 value
647 ;; Comment for more.gar. Spaces
649 ;; Comment for the more.gar value
651 ;; Comment for source2, again. Mixed tabs/spaces.
653 ;; Comment for the source2 value, again
656 cat config.branch >>.git/config &&
657 git branch -c source2 dest2 &&
658 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
659 test_cmp expect actual &&
661 # ...and that the comments and formatting for those sections
663 cat >expect <<\EOF &&
664 ;; Comment for source2. Tabs
666 ;; Comment for the source2 value
668 ;; Comment for more.gar. Spaces
670 ;; Comment for the source2 value
672 ;; Comment for more.gar. Spaces
674 ;; Comment for the more.gar value
676 ;; Comment for source2, again. Mixed tabs/spaces.
678 ;; Comment for the source2 value, again
681 ;; Comment for the source2 value, again
684 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
685 test_cmp expect actual
688 test_expect_success 'deleting a symref' '
690 git symbolic-ref refs/heads/symref refs/heads/target &&
691 echo "Deleted branch symref (was refs/heads/target)." >expect &&
692 git branch -d symref >actual &&
693 test_path_is_file .git/refs/heads/target &&
694 test_path_is_missing .git/refs/heads/symref &&
695 test_i18ncmp expect actual
698 test_expect_success 'deleting a dangling symref' '
699 git symbolic-ref refs/heads/dangling-symref nowhere &&
700 test_path_is_file .git/refs/heads/dangling-symref &&
701 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
702 git branch -d dangling-symref >actual &&
703 test_path_is_missing .git/refs/heads/dangling-symref &&
704 test_i18ncmp expect actual
707 test_expect_success 'deleting a self-referential symref' '
708 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
709 test_path_is_file .git/refs/heads/self-reference &&
710 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
711 git branch -d self-reference >actual &&
712 test_path_is_missing .git/refs/heads/self-reference &&
713 test_i18ncmp expect actual
716 test_expect_success 'renaming a symref is not allowed' '
717 git symbolic-ref refs/heads/topic refs/heads/main &&
718 test_must_fail git branch -m topic new-topic &&
719 git symbolic-ref refs/heads/topic &&
720 test_path_is_file .git/refs/heads/main &&
721 test_path_is_missing .git/refs/heads/new-topic
724 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
725 git branch --create-reflog u &&
726 mv .git/logs/refs/heads/u real-u &&
727 ln -s real-u .git/logs/refs/heads/u &&
728 test_must_fail git branch -m u v
731 test_expect_success 'test tracking setup via --track' '
732 git config remote.local.url . &&
733 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
734 (git show-ref -q refs/remotes/local/main || git fetch local) &&
735 git branch --track my1 local/main &&
736 test $(git config branch.my1.remote) = local &&
737 test $(git config branch.my1.merge) = refs/heads/main
740 test_expect_success 'test tracking setup (non-wildcard, matching)' '
741 git config remote.local.url . &&
742 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
743 (git show-ref -q refs/remotes/local/main || git fetch local) &&
744 git branch --track my4 local/main &&
745 test $(git config branch.my4.remote) = local &&
746 test $(git config branch.my4.merge) = refs/heads/main
749 test_expect_success 'tracking setup fails on non-matching refspec' '
750 git config remote.local.url . &&
751 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
752 (git show-ref -q refs/remotes/local/main || git fetch local) &&
753 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
754 test_must_fail git branch --track my5 local/main &&
755 test_must_fail git config branch.my5.remote &&
756 test_must_fail git config branch.my5.merge
759 test_expect_success 'test tracking setup via config' '
760 git config branch.autosetupmerge true &&
761 git config remote.local.url . &&
762 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
763 (git show-ref -q refs/remotes/local/main || git fetch local) &&
764 git branch my3 local/main &&
765 test $(git config branch.my3.remote) = local &&
766 test $(git config branch.my3.merge) = refs/heads/main
769 test_expect_success 'test overriding tracking setup via --no-track' '
770 git config branch.autosetupmerge true &&
771 git config remote.local.url . &&
772 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
773 (git show-ref -q refs/remotes/local/main || git fetch local) &&
774 git branch --no-track my2 local/main &&
775 git config branch.autosetupmerge false &&
776 ! test "$(git config branch.my2.remote)" = local &&
777 ! test "$(git config branch.my2.merge)" = refs/heads/main
780 test_expect_success 'no tracking without .fetch entries' '
781 git config branch.autosetupmerge true &&
783 git config branch.autosetupmerge false &&
784 test -z "$(git config branch.my6.remote)" &&
785 test -z "$(git config branch.my6.merge)"
788 test_expect_success 'test tracking setup via --track but deeper' '
789 git config remote.local.url . &&
790 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
791 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
792 git branch --track my7 local/o/o &&
793 test "$(git config branch.my7.remote)" = local &&
794 test "$(git config branch.my7.merge)" = refs/heads/o/o
797 test_expect_success 'test deleting branch deletes branch config' '
799 test -z "$(git config branch.my7.remote)" &&
800 test -z "$(git config branch.my7.merge)"
803 test_expect_success 'test deleting branch without config' '
805 sha1=$(git rev-parse my7 | cut -c 1-7) &&
806 echo "Deleted branch my7 (was $sha1)." >expect &&
807 git branch -d my7 >actual 2>&1 &&
808 test_i18ncmp expect actual
811 test_expect_success 'deleting currently checked out branch fails' '
812 git worktree add -b my7 my7 &&
813 test_must_fail git -C my7 branch -d my7 &&
814 test_must_fail git branch -d my7 &&
819 test_expect_success 'test --track without .fetch entries' '
820 git branch --track my8 &&
821 test "$(git config branch.my8.remote)" &&
822 test "$(git config branch.my8.merge)"
825 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
826 git config branch.autosetupmerge always &&
827 git branch my9 HEAD^ &&
828 git config branch.autosetupmerge false
831 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
832 test_must_fail git branch --track my10 HEAD^
835 test_expect_success 'branch from tag w/--track causes failure' '
837 test_must_fail git branch --track my11 foobar
840 test_expect_success '--set-upstream-to fails on multiple branches' '
841 echo "fatal: too many arguments to set new upstream" >expect &&
842 test_must_fail git branch --set-upstream-to main a b c 2>err &&
843 test_i18ncmp expect err
846 test_expect_success '--set-upstream-to fails on detached HEAD' '
847 git checkout HEAD^{} &&
848 test_when_finished git checkout - &&
849 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch." >expect &&
850 test_must_fail git branch --set-upstream-to main 2>err &&
851 test_i18ncmp expect err
854 test_expect_success '--set-upstream-to fails on a missing dst branch' '
855 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
856 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
857 test_i18ncmp expect err
860 test_expect_success '--set-upstream-to fails on a missing src branch' '
861 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
862 test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
865 test_expect_success '--set-upstream-to fails on a non-ref' '
866 echo "fatal: Cannot setup tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch." >expect &&
867 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
868 test_i18ncmp expect err
871 test_expect_success '--set-upstream-to fails on locked config' '
872 test_when_finished "rm -f .git/config.lock" &&
875 test_must_fail git branch --set-upstream-to locked 2>err &&
876 test_i18ngrep "could not lock config file .git/config" err
879 test_expect_success 'use --set-upstream-to modify HEAD' '
880 test_config branch.main.remote foo &&
881 test_config branch.main.merge foo &&
883 git branch --set-upstream-to my12 &&
884 test "$(git config branch.main.remote)" = "." &&
885 test "$(git config branch.main.merge)" = "refs/heads/my12"
888 test_expect_success 'use --set-upstream-to modify a particular branch' '
890 git branch --set-upstream-to main my13 &&
891 test_when_finished "git branch --unset-upstream my13" &&
892 test "$(git config branch.my13.remote)" = "." &&
893 test "$(git config branch.my13.merge)" = "refs/heads/main"
896 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
897 echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
898 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
899 test_i18ncmp expect err
902 test_expect_success '--unset-upstream should fail if config is locked' '
903 test_when_finished "rm -f .git/config.lock" &&
904 git branch --set-upstream-to locked &&
906 test_must_fail git branch --unset-upstream 2>err &&
907 test_i18ngrep "could not lock config file .git/config" err
910 test_expect_success 'test --unset-upstream on HEAD' '
912 test_config branch.main.remote foo &&
913 test_config branch.main.merge foo &&
914 git branch --set-upstream-to my14 &&
915 git branch --unset-upstream &&
916 test_must_fail git config branch.main.remote &&
917 test_must_fail git config branch.main.merge &&
918 # fail for a branch without upstream set
919 echo "fatal: Branch '"'"'main'"'"' has no upstream information" >expect &&
920 test_must_fail git branch --unset-upstream 2>err &&
921 test_i18ncmp expect err
924 test_expect_success '--unset-upstream should fail on multiple branches' '
925 echo "fatal: too many arguments to unset upstream" >expect &&
926 test_must_fail git branch --unset-upstream a b c 2>err &&
927 test_i18ncmp expect err
930 test_expect_success '--unset-upstream should fail on detached HEAD' '
931 git checkout HEAD^{} &&
932 test_when_finished git checkout - &&
933 echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect &&
934 test_must_fail git branch --unset-upstream 2>err &&
935 test_i18ncmp expect err
938 test_expect_success 'test --unset-upstream on a particular branch' '
940 git branch --set-upstream-to main my14 &&
941 git branch --unset-upstream my14 &&
942 test_must_fail git config branch.my14.remote &&
943 test_must_fail git config branch.my14.merge
946 test_expect_success 'disabled option --set-upstream fails' '
947 test_must_fail git branch --set-upstream origin/main
950 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
951 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
952 cat >expect <<-\EOF &&
953 warning: Not setting branch my13 as its own upstream.
955 test_expect_code 1 git config branch.my13.remote &&
956 test_expect_code 1 git config branch.my13.merge &&
957 test_i18ncmp expect actual
960 # Keep this test last, as it changes the current branch
962 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
964 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
965 GIT_COMMITTER_DATE="2005-05-26 23:30" \
966 git checkout -b g/h/i -l main &&
967 test_path_is_file .git/refs/heads/g/h/i &&
968 test_path_is_file .git/logs/refs/heads/g/h/i &&
969 test_cmp expect .git/logs/refs/heads/g/h/i
972 test_expect_success 'checkout -b makes reflog by default' '
974 git config --unset core.logAllRefUpdates &&
975 git checkout -b alpha &&
976 git rev-parse --verify alpha@{0}
979 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
981 git config core.logAllRefUpdates false &&
982 git checkout -b beta &&
983 test_must_fail git rev-parse --verify beta@{0}
986 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
988 git checkout -lb gamma &&
989 git config --unset core.logAllRefUpdates &&
990 git rev-parse --verify gamma@{0}
993 test_expect_success 'avoid ambiguous track' '
994 git config branch.autosetupmerge true &&
995 git config remote.ambi1.url lalala &&
996 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
997 git config remote.ambi2.url lilili &&
998 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
999 test_must_fail git branch all1 main &&
1000 test -z "$(git config branch.all1.merge)"
1003 test_expect_success 'autosetuprebase local on a tracked local branch' '
1004 git config remote.local.url . &&
1005 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1006 git config branch.autosetuprebase local &&
1007 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1008 git branch mybase &&
1009 git branch --track myr1 mybase &&
1010 test "$(git config branch.myr1.remote)" = . &&
1011 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1012 test "$(git config branch.myr1.rebase)" = true
1015 test_expect_success 'autosetuprebase always on a tracked local branch' '
1016 git config remote.local.url . &&
1017 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1018 git config branch.autosetuprebase always &&
1019 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1020 git branch mybase2 &&
1021 git branch --track myr2 mybase &&
1022 test "$(git config branch.myr2.remote)" = . &&
1023 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1024 test "$(git config branch.myr2.rebase)" = true
1027 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1028 git config remote.local.url . &&
1029 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1030 git config branch.autosetuprebase remote &&
1031 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1032 git branch mybase3 &&
1033 git branch --track myr3 mybase2 &&
1034 test "$(git config branch.myr3.remote)" = . &&
1035 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1036 ! test "$(git config branch.myr3.rebase)" = true
1039 test_expect_success 'autosetuprebase never on a tracked local branch' '
1040 git config remote.local.url . &&
1041 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1042 git config branch.autosetuprebase never &&
1043 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1044 git branch mybase4 &&
1045 git branch --track myr4 mybase2 &&
1046 test "$(git config branch.myr4.remote)" = . &&
1047 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1048 ! test "$(git config branch.myr4.rebase)" = true
1051 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1052 git config remote.local.url . &&
1053 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1054 git config branch.autosetuprebase local &&
1055 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1056 git branch --track myr5 local/main &&
1057 test "$(git config branch.myr5.remote)" = local &&
1058 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1059 ! test "$(git config branch.myr5.rebase)" = true
1062 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1063 git config remote.local.url . &&
1064 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1065 git config branch.autosetuprebase never &&
1066 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1067 git branch --track myr6 local/main &&
1068 test "$(git config branch.myr6.remote)" = local &&
1069 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1070 ! test "$(git config branch.myr6.rebase)" = true
1073 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1074 git config remote.local.url . &&
1075 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1076 git config branch.autosetuprebase remote &&
1077 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1078 git branch --track myr7 local/main &&
1079 test "$(git config branch.myr7.remote)" = local &&
1080 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1081 test "$(git config branch.myr7.rebase)" = true
1084 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1085 git config remote.local.url . &&
1086 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1087 git config branch.autosetuprebase remote &&
1088 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1089 git branch --track myr8 local/main &&
1090 test "$(git config branch.myr8.remote)" = local &&
1091 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1092 test "$(git config branch.myr8.rebase)" = true
1095 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1096 git config --unset branch.autosetuprebase &&
1097 git config remote.local.url . &&
1098 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1099 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1100 git branch --track myr9 local/main &&
1101 test "$(git config branch.myr9.remote)" = local &&
1102 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1103 test "z$(git config branch.myr9.rebase)" = z
1106 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1107 git config remote.local.url . &&
1108 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1109 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1110 git branch mybase10 &&
1111 git branch --track myr10 mybase2 &&
1112 test "$(git config branch.myr10.remote)" = . &&
1113 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1114 test "z$(git config branch.myr10.rebase)" = z
1117 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1118 git config remote.local.url . &&
1119 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1120 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1121 git branch --no-track myr11 mybase2 &&
1122 test "z$(git config branch.myr11.remote)" = z &&
1123 test "z$(git config branch.myr11.merge)" = z &&
1124 test "z$(git config branch.myr11.rebase)" = z
1127 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1128 git config remote.local.url . &&
1129 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1130 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1131 git branch --no-track myr12 local/main &&
1132 test "z$(git config branch.myr12.remote)" = z &&
1133 test "z$(git config branch.myr12.merge)" = z &&
1134 test "z$(git config branch.myr12.rebase)" = z
1137 test_expect_success 'autosetuprebase never on an untracked local branch' '
1138 git config branch.autosetuprebase never &&
1139 git config remote.local.url . &&
1140 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1141 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1142 git branch --no-track myr13 mybase2 &&
1143 test "z$(git config branch.myr13.remote)" = z &&
1144 test "z$(git config branch.myr13.merge)" = z &&
1145 test "z$(git config branch.myr13.rebase)" = z
1148 test_expect_success 'autosetuprebase local on an untracked local branch' '
1149 git config branch.autosetuprebase local &&
1150 git config remote.local.url . &&
1151 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1152 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1153 git branch --no-track myr14 mybase2 &&
1154 test "z$(git config branch.myr14.remote)" = z &&
1155 test "z$(git config branch.myr14.merge)" = z &&
1156 test "z$(git config branch.myr14.rebase)" = z
1159 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1160 git config branch.autosetuprebase remote &&
1161 git config remote.local.url . &&
1162 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1163 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1164 git branch --no-track myr15 mybase2 &&
1165 test "z$(git config branch.myr15.remote)" = z &&
1166 test "z$(git config branch.myr15.merge)" = z &&
1167 test "z$(git config branch.myr15.rebase)" = z
1170 test_expect_success 'autosetuprebase always on an untracked local branch' '
1171 git config branch.autosetuprebase always &&
1172 git config remote.local.url . &&
1173 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1174 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1175 git branch --no-track myr16 mybase2 &&
1176 test "z$(git config branch.myr16.remote)" = z &&
1177 test "z$(git config branch.myr16.merge)" = z &&
1178 test "z$(git config branch.myr16.rebase)" = z
1181 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1182 git config branch.autosetuprebase never &&
1183 git config remote.local.url . &&
1184 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1185 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1186 git branch --no-track myr17 local/main &&
1187 test "z$(git config branch.myr17.remote)" = z &&
1188 test "z$(git config branch.myr17.merge)" = z &&
1189 test "z$(git config branch.myr17.rebase)" = z
1192 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1193 git config branch.autosetuprebase local &&
1194 git config remote.local.url . &&
1195 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1196 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1197 git branch --no-track myr18 local/main &&
1198 test "z$(git config branch.myr18.remote)" = z &&
1199 test "z$(git config branch.myr18.merge)" = z &&
1200 test "z$(git config branch.myr18.rebase)" = z
1203 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1204 git config branch.autosetuprebase remote &&
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/main || git fetch local) &&
1208 git branch --no-track myr19 local/main &&
1209 test "z$(git config branch.myr19.remote)" = z &&
1210 test "z$(git config branch.myr19.merge)" = z &&
1211 test "z$(git config branch.myr19.rebase)" = z
1214 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1215 git config branch.autosetuprebase always &&
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 myr20 local/main &&
1220 test "z$(git config branch.myr20.remote)" = z &&
1221 test "z$(git config branch.myr20.merge)" = z &&
1222 test "z$(git config branch.myr20.rebase)" = z
1225 test_expect_success 'autosetuprebase always on detached HEAD' '
1226 git config branch.autosetupmerge always &&
1227 test_when_finished git checkout main &&
1228 git checkout HEAD^0 &&
1230 test -z "$(git config branch.my11.remote)" &&
1231 test -z "$(git config branch.my11.merge)"
1234 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1235 git config branch.autosetuprebase garbage &&
1236 test_must_fail git branch
1239 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1240 git config --unset branch.autosetuprebase &&
1241 echo "[branch] autosetuprebase" >>.git/config &&
1242 test_must_fail git branch &&
1243 git config --unset branch.autosetuprebase
1246 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1248 git config --unset branch.my8.merge &&
1249 test_must_fail git branch -d my8
1252 test_expect_success 'attempt to delete a branch merged to its base' '
1253 # we are on my9 which is the initial commit; traditionally
1254 # we would not have allowed deleting my8 that is not merged
1255 # to my9, but it is set to track main that already has my8
1256 git config branch.my8.merge refs/heads/main &&
1260 test_expect_success 'attempt to delete a branch merged to its base' '
1261 git checkout main &&
1263 git commit -m "Third commit" A &&
1264 git branch -t my10 my9 &&
1265 git branch -f my10 HEAD^ &&
1266 # we are on main which is at the third commit, and my10
1267 # is behind us, so traditionally we would have allowed deleting
1268 # it; but my10 is set to track my9 that is further behind.
1269 test_must_fail git branch -d my10
1272 test_expect_success 'use --edit-description' '
1273 write_script editor <<-\EOF &&
1274 echo "New contents" >"$1"
1276 EDITOR=./editor git branch --edit-description &&
1277 write_script editor <<-\EOF &&
1278 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1280 EDITOR=./editor git branch --edit-description &&
1281 echo "New contents" >expect &&
1282 test_cmp expect EDITOR_OUTPUT
1285 test_expect_success 'detect typo in branch name when using --edit-description' '
1286 write_script editor <<-\EOF &&
1287 echo "New contents" >"$1"
1289 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1292 test_expect_success 'refuse --edit-description on unborn branch for now' '
1293 test_when_finished "git checkout main" &&
1294 write_script editor <<-\EOF &&
1295 echo "New contents" >"$1"
1297 git checkout --orphan unborn &&
1298 test_must_fail env EDITOR=./editor git branch --edit-description
1301 test_expect_success '--merged catches invalid object names' '
1302 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1305 test_expect_success '--list during rebase' '
1306 test_when_finished "reset_rebase" &&
1307 git checkout main &&
1308 FAKE_LINES="1 edit 2" &&
1309 export FAKE_LINES &&
1311 git rebase -i HEAD~2 &&
1312 git branch --list >actual &&
1313 test_i18ngrep "rebasing main" actual
1316 test_expect_success '--list during rebase from detached HEAD' '
1317 test_when_finished "reset_rebase && git checkout main" &&
1318 git checkout main^0 &&
1319 oid=$(git rev-parse --short HEAD) &&
1320 FAKE_LINES="1 edit 2" &&
1321 export FAKE_LINES &&
1323 git rebase -i HEAD~2 &&
1324 git branch --list >actual &&
1325 test_i18ngrep "rebasing detached HEAD $oid" actual
1328 test_expect_success 'tracking with unexpected .fetch refspec' '
1330 git init -b main a &&
1335 git init -b main b &&
1340 git init -b main c &&
1344 git remote add a ../a &&
1345 git remote add b ../b &&
1348 git init -b main d &&
1351 git remote add c ../c &&
1352 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1354 git branch --track local/a/main remotes/a/main &&
1355 test "$(git config branch.local/a/main.remote)" = "c" &&
1356 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1357 git rev-parse --verify a >expect &&
1358 git rev-parse --verify local/a/main >actual &&
1359 test_cmp expect actual
1363 test_expect_success 'configured committerdate sort' '
1364 git init -b main sort &&
1367 git config branch.sort committerdate &&
1368 test_commit initial &&
1369 git checkout -b a &&
1371 git checkout -b c &&
1373 git checkout -b b &&
1375 git branch >actual &&
1376 cat >expect <<-\EOF &&
1382 test_cmp expect actual
1386 test_expect_success 'option override configured sort' '
1389 git config branch.sort committerdate &&
1390 git branch --sort=refname >actual &&
1391 cat >expect <<-\EOF &&
1397 test_cmp expect actual
1401 test_expect_success 'invalid sort parameter in configuration' '
1404 git config branch.sort "v:notvalid" &&
1405 test_must_fail git branch