3 # Copyright (c) 2005 Amos Waterland
6 test_description='git branch assorted tests'
10 test_expect_success 'prepare a trivial repository' '
12 git update-index --add A &&
13 git commit -m "Initial commit." &&
15 git update-index --add A &&
16 git commit -m "Second commit." &&
17 HEAD=$(git rev-parse --verify HEAD)
20 test_expect_success 'git branch --help should not have created a bogus branch' '
21 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
22 test_path_is_missing .git/refs/heads/--help
25 test_expect_success 'branch -h in broken repository' '
30 >.git/refs/heads/master &&
31 test_expect_code 129 git branch -h >usage 2>&1
33 test_i18ngrep "[Uu]sage" broken/usage
36 test_expect_success 'git branch abc should create a branch' '
37 git branch abc && test_path_is_file .git/refs/heads/abc
40 test_expect_success 'git branch a/b/c should create a branch' '
41 git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
44 test_expect_success 'git branch HEAD should fail' '
45 test_must_fail git branch HEAD
49 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
51 test_expect_success 'git branch -l d/e/f should create a branch and a log' '
52 GIT_COMMITTER_DATE="2005-05-26 23:30" \
53 git branch -l d/e/f &&
54 test_path_is_file .git/refs/heads/d/e/f &&
55 test_path_is_file .git/logs/refs/heads/d/e/f &&
56 test_cmp expect .git/logs/refs/heads/d/e/f
59 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
60 git branch -d d/e/f &&
61 test_path_is_missing .git/refs/heads/d/e/f &&
62 test_must_fail git reflog exists refs/heads/d/e/f
65 test_expect_success 'git branch j/k should work after branch j has been deleted' '
71 test_expect_success 'git branch l should work after branch l/m has been deleted' '
77 test_expect_success 'git branch -m dumps usage' '
78 test_expect_code 128 git branch -m 2>err &&
79 test_i18ngrep "branch name required" err
82 test_expect_success 'git branch -m m broken_symref should work' '
83 test_when_finished "git branch -D broken_symref" &&
85 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
86 git branch -m m broken_symref &&
87 git reflog exists refs/heads/broken_symref &&
88 test_must_fail git reflog exists refs/heads/i_am_broken
91 test_expect_success 'git branch -m m m/m should work' '
93 git branch -m m m/m &&
94 git reflog exists refs/heads/m/m
97 test_expect_success 'git branch -m n/n n should work' '
99 git branch -m n/n n &&
100 git reflog exists refs/heads/n
103 # The topmost entry in reflog for branch bbb is about branch creation.
104 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
106 test_expect_success 'git branch -m bbb should rename checked out branch' '
107 test_when_finished git branch -D bbb &&
108 test_when_finished git checkout master &&
109 git checkout -b aaa &&
110 git commit --allow-empty -m "a new commit" &&
111 git rev-parse aaa@{0} >expect &&
113 git rev-parse bbb@{1} >actual &&
114 test_cmp expect actual &&
115 git symbolic-ref HEAD >actual &&
116 echo refs/heads/bbb >expect &&
117 test_cmp expect actual
120 test_expect_success 'renaming checked out branch works with d/f conflict' '
121 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
122 test_when_finished git checkout master &&
123 git checkout -b foo &&
124 git branch -m foo/bar &&
125 git symbolic-ref HEAD >actual &&
126 echo refs/heads/foo/bar >expect &&
127 test_cmp expect actual
130 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
133 test_must_fail git branch -m o/o o
136 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
138 test_must_fail git branch -m o/q o/p
141 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
142 git branch -M o/q o/p
145 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
147 git branch -m -f o/q o/p
150 test_expect_success 'git branch -m q r/q should fail when r exists' '
153 test_must_fail git branch -m q r/q
156 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
158 git checkout -b foo &&
159 test_must_fail git branch -M bar foo
162 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
163 git checkout -b baz &&
165 git branch -M baz bam &&
166 test $(git rev-parse --abbrev-ref HEAD) = bam
169 test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
170 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
171 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
172 grep "^0\{40\}.*$msg$" .git/logs/HEAD
175 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
179 test_commit initial &&
180 git checkout --orphan lonely &&
181 grep lonely .git/HEAD &&
182 test_path_is_missing .git/refs/head/lonely &&
183 git branch -M master mistress &&
184 grep lonely .git/HEAD
188 test_expect_success 'resulting reflog can be shown by log -g' '
189 oid=$(git rev-parse HEAD) &&
190 cat >expect <<-EOF &&
192 HEAD@{2} $oid checkout: moving from foo to baz
194 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
195 test_cmp expect actual
198 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
199 git checkout master &&
200 git worktree add -b baz bazdir &&
201 git worktree add -f bazdir2 baz &&
202 git branch -M baz bam &&
203 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
204 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
207 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
208 git checkout -b baz &&
209 git worktree add -f bazdir3 baz &&
212 git branch -M baz bam &&
213 test $(git rev-parse --abbrev-ref HEAD) = bam
215 test $(git rev-parse --abbrev-ref HEAD) = bam
218 test_expect_success 'git branch -M master should work when master is checked out' '
219 git checkout master &&
223 test_expect_success 'git branch -M master master should work when master is checked out' '
224 git checkout master &&
225 git branch -M master master
228 test_expect_success 'git branch -M master2 master2 should work when master is checked out' '
229 git checkout master &&
230 git branch master2 &&
231 git branch -M master2 master2
234 test_expect_success 'git branch -v -d t should work' '
236 test_path_is_file .git/refs/heads/t &&
237 git branch -v -d t &&
238 test_path_is_missing .git/refs/heads/t
241 test_expect_success 'git branch -v -m t s should work' '
243 test_path_is_file .git/refs/heads/t &&
244 git branch -v -m t s &&
245 test_path_is_missing .git/refs/heads/t &&
246 test_path_is_file .git/refs/heads/s &&
250 test_expect_success 'git branch -m -d t s should fail' '
252 test_path_is_file .git/refs/heads/t &&
253 test_must_fail git branch -m -d t s &&
255 test_path_is_missing .git/refs/heads/t
258 test_expect_success 'git branch --list -d t should fail' '
260 test_path_is_file .git/refs/heads/t &&
261 test_must_fail git branch --list -d t &&
263 test_path_is_missing .git/refs/heads/t
266 test_expect_success 'git branch --list -v with --abbrev' '
267 test_when_finished "git branch -D t" &&
269 git branch -v --list t >actual.default &&
270 git branch -v --list --abbrev t >actual.abbrev &&
271 test_cmp actual.default actual.abbrev &&
273 git branch -v --list --no-abbrev t >actual.noabbrev &&
274 git branch -v --list --abbrev=0 t >actual.0abbrev &&
275 test_cmp actual.noabbrev actual.0abbrev &&
277 git branch -v --list --abbrev=36 t >actual.36abbrev &&
278 # how many hexdigits are used?
279 read name objdefault rest <actual.abbrev &&
280 read name obj36 rest <actual.36abbrev &&
281 objfull=$(git rev-parse --verify t) &&
283 # are we really getting abbreviations?
284 test "$obj36" != "$objdefault" &&
285 expr "$obj36" : "$objdefault" >/dev/null &&
286 test "$objfull" != "$obj36" &&
287 expr "$objfull" : "$obj36" >/dev/null
291 test_expect_success 'git branch --column' '
292 COLUMNS=81 git branch --column=column >actual &&
293 cat >expected <<\EOF &&
294 a/b/c bam foo l * master n o/p r
295 abc bar j/k m/m master2 o/o q
297 test_cmp expected actual
300 test_expect_success 'git branch --column with an extremely long branch name' '
301 long=this/is/a/part/of/long/branch/name &&
302 long=z$long/$long/$long/$long &&
303 test_when_finished "git branch -d $long" &&
305 COLUMNS=80 git branch --column=column >actual &&
306 cat >expected <<EOF &&
324 test_cmp expected actual
327 test_expect_success 'git branch with column.*' '
328 git config column.ui column &&
329 git config column.branch "dense" &&
330 COLUMNS=80 git branch >actual &&
331 git config --unset column.branch &&
332 git config --unset column.ui &&
333 cat >expected <<\EOF &&
334 a/b/c bam foo l * master n o/p r
335 abc bar j/k m/m master2 o/o q
337 test_cmp expected actual
340 test_expect_success 'git branch --column -v should fail' '
341 test_must_fail git branch --column -v
344 test_expect_success 'git branch -v with column.ui ignored' '
345 git config column.ui column &&
346 COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
347 git config --unset column.ui &&
348 cat >expected <<\EOF &&
365 test_cmp expected actual
368 mv .git/config .git/config-saved
370 test_expect_success 'git branch -m q q2 without config should succeed' '
371 git branch -m q q2 &&
375 mv .git/config-saved .git/config
377 git config branch.s/s.dummy Hello
379 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
381 git reflog exists refs/heads/s/s &&
383 git reflog exists refs/heads/s/t &&
385 git branch -m s/s s &&
386 git reflog exists refs/heads/s
389 test_expect_success 'config information was renamed, too' '
390 test $(git config branch.s.dummy) = Hello &&
391 test_must_fail git config branch.s/s.dummy
394 test_expect_success 'git branch -m correctly renames multiple config sections' '
395 test_when_finished "git checkout master" &&
396 git checkout -b source master &&
398 # Assert that a config file with multiple config sections has
399 # those sections preserved...
400 cat >expect <<-\EOF &&
401 branch.dest.key1=value1
403 branch.dest.key2=value2
405 cat >config.branch <<\EOF &&
406 ;; Note the lack of -\EOF above & mixed indenting here. This is
407 ;; intentional, we are also testing that the formatting of copied
408 ;; sections is preserved.
410 ;; Comment for source. Tabs
412 ;; Comment for the source value
414 ;; Comment for some.gar. Spaces
416 ;; Comment for the some.gar value
418 ;; Comment for source, again. Mixed tabs/spaces.
420 ;; Comment for the source value, again
423 cat config.branch >>.git/config &&
424 git branch -m source dest &&
425 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
426 test_cmp expect actual &&
428 # ...and that the comments for those sections are also
430 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
431 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
432 test_cmp expect actual
435 test_expect_success 'git branch -c dumps usage' '
436 test_expect_code 128 git branch -c 2>err &&
437 test_i18ngrep "branch name required" err
440 test_expect_success 'git branch --copy dumps usage' '
441 test_expect_code 128 git branch --copy 2>err &&
442 test_i18ngrep "branch name required" err
445 test_expect_success 'git branch -c d e should work' '
447 git reflog exists refs/heads/d &&
448 git config branch.d.dummy Hello &&
450 git reflog exists refs/heads/d &&
451 git reflog exists refs/heads/e &&
452 echo Hello >expect &&
453 git config branch.e.dummy >actual &&
454 test_cmp expect actual &&
455 echo Hello >expect &&
456 git config branch.d.dummy >actual &&
457 test_cmp expect actual
460 test_expect_success 'git branch --copy is a synonym for -c' '
461 git branch -l copy &&
462 git reflog exists refs/heads/copy &&
463 git config branch.copy.dummy Hello &&
464 git branch --copy copy copy-to &&
465 git reflog exists refs/heads/copy &&
466 git reflog exists refs/heads/copy-to &&
467 echo Hello >expect &&
468 git config branch.copy.dummy >actual &&
469 test_cmp expect actual &&
470 echo Hello >expect &&
471 git config branch.copy-to.dummy >actual &&
472 test_cmp expect actual
475 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
476 git checkout -b ee &&
477 git reflog exists refs/heads/ee &&
478 git config branch.ee.dummy Hello &&
479 git branch -c ee ef &&
480 git reflog exists refs/heads/ee &&
481 git reflog exists refs/heads/ef &&
482 test $(git config branch.ee.dummy) = Hello &&
483 test $(git config branch.ef.dummy) = Hello &&
484 test $(git rev-parse --abbrev-ref HEAD) = ee
487 test_expect_success 'git branch -c f/f g/g should work' '
489 git reflog exists refs/heads/f/f &&
490 git config branch.f/f.dummy Hello &&
491 git branch -c f/f g/g &&
492 git reflog exists refs/heads/f/f &&
493 git reflog exists refs/heads/g/g &&
494 test $(git config branch.f/f.dummy) = Hello &&
495 test $(git config branch.g/g.dummy) = Hello
498 test_expect_success 'git branch -c m2 m2 should work' '
500 git reflog exists refs/heads/m2 &&
501 git config branch.m2.dummy Hello &&
502 git branch -c m2 m2 &&
503 git reflog exists refs/heads/m2 &&
504 test $(git config branch.m2.dummy) = Hello
507 test_expect_success 'git branch -c zz zz/zz should fail' '
509 git reflog exists refs/heads/zz &&
510 test_must_fail git branch -c zz zz/zz
513 test_expect_success 'git branch -c b/b b should fail' '
515 test_must_fail git branch -c b/b b
518 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
520 git reflog exists refs/heads/o/q &&
521 git reflog exists refs/heads/o/p &&
522 git branch -C o/q o/p
525 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
526 git reflog exists refs/heads/o/q &&
527 git reflog exists refs/heads/o/p &&
528 git branch -c -f o/q o/p
531 test_expect_success 'git branch -c qq rr/qq should fail when r exists' '
534 test_must_fail git branch -c qq rr/qq
537 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
539 git checkout -b b2 &&
540 test_must_fail git branch -C b1 b2
543 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
544 git checkout -b c1 &&
546 git branch -C c1 c2 &&
547 test $(git rev-parse --abbrev-ref HEAD) = c1
550 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
551 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
552 ! grep "$msg$" .git/logs/HEAD
555 test_expect_success 'git branch -C master should work when master is checked out' '
556 git checkout master &&
560 test_expect_success 'git branch -C master master should work when master is checked out' '
561 git checkout master &&
562 git branch -C master master
565 test_expect_success 'git branch -C master5 master5 should work when master is checked out' '
566 git checkout master &&
567 git branch master5 &&
568 git branch -C master5 master5
571 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
573 git reflog exists refs/heads/cd &&
574 git config branch.cd.dummy CD &&
576 git reflog exists refs/heads/ab &&
577 git config branch.ab.dummy AB &&
578 git branch -C ab cd &&
579 git reflog exists refs/heads/ab &&
580 git reflog exists refs/heads/cd &&
581 test $(git config branch.ab.dummy) = AB &&
582 test $(git config branch.cd.dummy) = AB
585 test_expect_success 'git branch -c correctly copies multiple config sections' '
588 test_when_finished "git checkout master" &&
589 git checkout -b source2 master &&
591 # Assert that a config file with multiple config sections has
592 # those sections preserved...
593 cat >expect <<-\EOF &&
594 branch.source2.key1=value1
595 branch.dest2.key1=value1
597 branch.source2.key2=value2
598 branch.dest2.key2=value2
600 cat >config.branch <<\EOF &&
601 ;; Note the lack of -\EOF above & mixed indenting here. This is
602 ;; intentional, we are also testing that the formatting of copied
603 ;; sections is preserved.
605 ;; Comment for source2. Tabs
607 ;; Comment for the source2 value
609 ;; Comment for more.gar. Spaces
611 ;; Comment for the more.gar value
613 ;; Comment for source2, again. Mixed tabs/spaces.
615 ;; Comment for the source2 value, again
618 cat config.branch >>.git/config &&
619 git branch -c source2 dest2 &&
620 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
621 test_cmp expect actual &&
623 # ...and that the comments and formatting for those sections
625 cat >expect <<\EOF &&
626 ;; Comment for source2. Tabs
628 ;; Comment for the source2 value
630 ;; Comment for more.gar. Spaces
632 ;; Comment for the source2 value
634 ;; Comment for more.gar. Spaces
636 ;; Comment for the more.gar value
638 ;; Comment for source2, again. Mixed tabs/spaces.
640 ;; Comment for the source2 value, again
643 ;; Comment for the source2 value, again
646 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
647 test_cmp expect actual
650 test_expect_success 'deleting a symref' '
652 git symbolic-ref refs/heads/symref refs/heads/target &&
653 echo "Deleted branch symref (was refs/heads/target)." >expect &&
654 git branch -d symref >actual &&
655 test_path_is_file .git/refs/heads/target &&
656 test_path_is_missing .git/refs/heads/symref &&
657 test_i18ncmp expect actual
660 test_expect_success 'deleting a dangling symref' '
661 git symbolic-ref refs/heads/dangling-symref nowhere &&
662 test_path_is_file .git/refs/heads/dangling-symref &&
663 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
664 git branch -d dangling-symref >actual &&
665 test_path_is_missing .git/refs/heads/dangling-symref &&
666 test_i18ncmp expect actual
669 test_expect_success 'deleting a self-referential symref' '
670 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
671 test_path_is_file .git/refs/heads/self-reference &&
672 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
673 git branch -d self-reference >actual &&
674 test_path_is_missing .git/refs/heads/self-reference &&
675 test_i18ncmp expect actual
678 test_expect_success 'renaming a symref is not allowed' '
679 git symbolic-ref refs/heads/master2 refs/heads/master &&
680 test_must_fail git branch -m master2 master3 &&
681 git symbolic-ref refs/heads/master2 &&
682 test_path_is_file .git/refs/heads/master &&
683 test_path_is_missing .git/refs/heads/master3
686 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
688 mv .git/logs/refs/heads/u real-u &&
689 ln -s real-u .git/logs/refs/heads/u &&
690 test_must_fail git branch -m u v
693 test_expect_success 'test tracking setup via --track' '
694 git config remote.local.url . &&
695 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
696 (git show-ref -q refs/remotes/local/master || git fetch local) &&
697 git branch --track my1 local/master &&
698 test $(git config branch.my1.remote) = local &&
699 test $(git config branch.my1.merge) = refs/heads/master
702 test_expect_success 'test tracking setup (non-wildcard, matching)' '
703 git config remote.local.url . &&
704 git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
705 (git show-ref -q refs/remotes/local/master || git fetch local) &&
706 git branch --track my4 local/master &&
707 test $(git config branch.my4.remote) = local &&
708 test $(git config branch.my4.merge) = refs/heads/master
711 test_expect_success 'tracking setup fails on non-matching refspec' '
712 git config remote.local.url . &&
713 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
714 (git show-ref -q refs/remotes/local/master || git fetch local) &&
715 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
716 test_must_fail git branch --track my5 local/master &&
717 test_must_fail git config branch.my5.remote &&
718 test_must_fail git config branch.my5.merge
721 test_expect_success 'test tracking setup via config' '
722 git config branch.autosetupmerge true &&
723 git config remote.local.url . &&
724 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
725 (git show-ref -q refs/remotes/local/master || git fetch local) &&
726 git branch my3 local/master &&
727 test $(git config branch.my3.remote) = local &&
728 test $(git config branch.my3.merge) = refs/heads/master
731 test_expect_success 'test overriding tracking setup via --no-track' '
732 git config branch.autosetupmerge true &&
733 git config remote.local.url . &&
734 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
735 (git show-ref -q refs/remotes/local/master || git fetch local) &&
736 git branch --no-track my2 local/master &&
737 git config branch.autosetupmerge false &&
738 ! test "$(git config branch.my2.remote)" = local &&
739 ! test "$(git config branch.my2.merge)" = refs/heads/master
742 test_expect_success 'no tracking without .fetch entries' '
743 git config branch.autosetupmerge true &&
745 git config branch.autosetupmerge false &&
746 test -z "$(git config branch.my6.remote)" &&
747 test -z "$(git config branch.my6.merge)"
750 test_expect_success 'test tracking setup via --track but deeper' '
751 git config remote.local.url . &&
752 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
753 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
754 git branch --track my7 local/o/o &&
755 test "$(git config branch.my7.remote)" = local &&
756 test "$(git config branch.my7.merge)" = refs/heads/o/o
759 test_expect_success 'test deleting branch deletes branch config' '
761 test -z "$(git config branch.my7.remote)" &&
762 test -z "$(git config branch.my7.merge)"
765 test_expect_success 'test deleting branch without config' '
767 sha1=$(git rev-parse my7 | cut -c 1-7) &&
768 echo "Deleted branch my7 (was $sha1)." >expect &&
769 git branch -d my7 >actual 2>&1 &&
770 test_i18ncmp expect actual
773 test_expect_success 'deleting currently checked out branch fails' '
774 git worktree add -b my7 my7 &&
775 test_must_fail git -C my7 branch -d my7 &&
776 test_must_fail git branch -d my7
779 test_expect_success 'test --track without .fetch entries' '
780 git branch --track my8 &&
781 test "$(git config branch.my8.remote)" &&
782 test "$(git config branch.my8.merge)"
785 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
786 git config branch.autosetupmerge always &&
787 git branch my9 HEAD^ &&
788 git config branch.autosetupmerge false
791 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
792 test_must_fail git branch --track my10 HEAD^
795 test_expect_success 'branch from tag w/--track causes failure' '
797 test_must_fail git branch --track my11 foobar
800 test_expect_success '--set-upstream-to fails on multiple branches' '
801 test_must_fail git branch --set-upstream-to master a b c
804 test_expect_success '--set-upstream-to fails on detached HEAD' '
805 git checkout HEAD^{} &&
806 test_must_fail git branch --set-upstream-to master &&
810 test_expect_success '--set-upstream-to fails on a missing dst branch' '
811 test_must_fail git branch --set-upstream-to master does-not-exist
814 test_expect_success '--set-upstream-to fails on a missing src branch' '
815 test_must_fail git branch --set-upstream-to does-not-exist master
818 test_expect_success '--set-upstream-to fails on a non-ref' '
819 test_must_fail git branch --set-upstream-to HEAD^{}
822 test_expect_success '--set-upstream-to fails on locked config' '
823 test_when_finished "rm -f .git/config.lock" &&
826 test_must_fail git branch --set-upstream-to locked
829 test_expect_success 'use --set-upstream-to modify HEAD' '
830 test_config branch.master.remote foo &&
831 test_config branch.master.merge foo &&
833 git branch --set-upstream-to my12 &&
834 test "$(git config branch.master.remote)" = "." &&
835 test "$(git config branch.master.merge)" = "refs/heads/my12"
838 test_expect_success 'use --set-upstream-to modify a particular branch' '
840 git branch --set-upstream-to master my13 &&
841 test_when_finished "git branch --unset-upstream my13" &&
842 test "$(git config branch.my13.remote)" = "." &&
843 test "$(git config branch.my13.merge)" = "refs/heads/master"
846 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
847 test_must_fail git branch --unset-upstream i-dont-exist
850 test_expect_success '--unset-upstream should fail if config is locked' '
851 test_when_finished "rm -f .git/config.lock" &&
852 git branch --set-upstream-to locked &&
854 test_must_fail git branch --unset-upstream
857 test_expect_success 'test --unset-upstream on HEAD' '
859 test_config branch.master.remote foo &&
860 test_config branch.master.merge foo &&
861 git branch --set-upstream-to my14 &&
862 git branch --unset-upstream &&
863 test_must_fail git config branch.master.remote &&
864 test_must_fail git config branch.master.merge &&
865 # fail for a branch without upstream set
866 test_must_fail git branch --unset-upstream
869 test_expect_success '--unset-upstream should fail on multiple branches' '
870 test_must_fail git branch --unset-upstream a b c
873 test_expect_success '--unset-upstream should fail on detached HEAD' '
874 git checkout HEAD^{} &&
875 test_must_fail git branch --unset-upstream &&
879 test_expect_success 'test --unset-upstream on a particular branch' '
881 git branch --set-upstream-to master my14 &&
882 git branch --unset-upstream my14 &&
883 test_must_fail git config branch.my14.remote &&
884 test_must_fail git config branch.my14.merge
887 test_expect_success '--set-upstream fails' '
888 test_must_fail git branch --set-upstream origin/master
891 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
892 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
893 cat >expected <<-\EOF &&
894 warning: Not setting branch my13 as its own upstream.
896 test_expect_code 1 git config branch.my13.remote &&
897 test_expect_code 1 git config branch.my13.merge &&
898 test_i18ncmp expected actual
901 # Keep this test last, as it changes the current branch
903 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
905 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
906 GIT_COMMITTER_DATE="2005-05-26 23:30" \
907 git checkout -b g/h/i -l master &&
908 test_path_is_file .git/refs/heads/g/h/i &&
909 test_path_is_file .git/logs/refs/heads/g/h/i &&
910 test_cmp expect .git/logs/refs/heads/g/h/i
913 test_expect_success 'checkout -b makes reflog by default' '
914 git checkout master &&
915 git config --unset core.logAllRefUpdates &&
916 git checkout -b alpha &&
917 git rev-parse --verify alpha@{0}
920 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
921 git checkout master &&
922 git config core.logAllRefUpdates false &&
923 git checkout -b beta &&
924 test_must_fail git rev-parse --verify beta@{0}
927 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
928 git checkout master &&
929 git checkout -lb gamma &&
930 git config --unset core.logAllRefUpdates &&
931 git rev-parse --verify gamma@{0}
934 test_expect_success 'avoid ambiguous track' '
935 git config branch.autosetupmerge true &&
936 git config remote.ambi1.url lalala &&
937 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
938 git config remote.ambi2.url lilili &&
939 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
940 test_must_fail git branch all1 master &&
941 test -z "$(git config branch.all1.merge)"
944 test_expect_success 'autosetuprebase local on a tracked local branch' '
945 git config remote.local.url . &&
946 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
947 git config branch.autosetuprebase local &&
948 (git show-ref -q refs/remotes/local/o || git fetch local) &&
950 git branch --track myr1 mybase &&
951 test "$(git config branch.myr1.remote)" = . &&
952 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
953 test "$(git config branch.myr1.rebase)" = true
956 test_expect_success 'autosetuprebase always on a tracked local branch' '
957 git config remote.local.url . &&
958 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
959 git config branch.autosetuprebase always &&
960 (git show-ref -q refs/remotes/local/o || git fetch local) &&
961 git branch mybase2 &&
962 git branch --track myr2 mybase &&
963 test "$(git config branch.myr2.remote)" = . &&
964 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
965 test "$(git config branch.myr2.rebase)" = true
968 test_expect_success 'autosetuprebase remote on a tracked local branch' '
969 git config remote.local.url . &&
970 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
971 git config branch.autosetuprebase remote &&
972 (git show-ref -q refs/remotes/local/o || git fetch local) &&
973 git branch mybase3 &&
974 git branch --track myr3 mybase2 &&
975 test "$(git config branch.myr3.remote)" = . &&
976 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
977 ! test "$(git config branch.myr3.rebase)" = true
980 test_expect_success 'autosetuprebase never on a tracked local branch' '
981 git config remote.local.url . &&
982 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
983 git config branch.autosetuprebase never &&
984 (git show-ref -q refs/remotes/local/o || git fetch local) &&
985 git branch mybase4 &&
986 git branch --track myr4 mybase2 &&
987 test "$(git config branch.myr4.remote)" = . &&
988 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
989 ! test "$(git config branch.myr4.rebase)" = true
992 test_expect_success 'autosetuprebase local on a tracked remote branch' '
993 git config remote.local.url . &&
994 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
995 git config branch.autosetuprebase local &&
996 (git show-ref -q refs/remotes/local/master || git fetch local) &&
997 git branch --track myr5 local/master &&
998 test "$(git config branch.myr5.remote)" = local &&
999 test "$(git config branch.myr5.merge)" = refs/heads/master &&
1000 ! test "$(git config branch.myr5.rebase)" = true
1003 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1004 git config remote.local.url . &&
1005 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1006 git config branch.autosetuprebase never &&
1007 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1008 git branch --track myr6 local/master &&
1009 test "$(git config branch.myr6.remote)" = local &&
1010 test "$(git config branch.myr6.merge)" = refs/heads/master &&
1011 ! test "$(git config branch.myr6.rebase)" = true
1014 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1015 git config remote.local.url . &&
1016 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1017 git config branch.autosetuprebase remote &&
1018 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1019 git branch --track myr7 local/master &&
1020 test "$(git config branch.myr7.remote)" = local &&
1021 test "$(git config branch.myr7.merge)" = refs/heads/master &&
1022 test "$(git config branch.myr7.rebase)" = true
1025 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1026 git config remote.local.url . &&
1027 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1028 git config branch.autosetuprebase remote &&
1029 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1030 git branch --track myr8 local/master &&
1031 test "$(git config branch.myr8.remote)" = local &&
1032 test "$(git config branch.myr8.merge)" = refs/heads/master &&
1033 test "$(git config branch.myr8.rebase)" = true
1036 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1037 git config --unset branch.autosetuprebase &&
1038 git config remote.local.url . &&
1039 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1040 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1041 git branch --track myr9 local/master &&
1042 test "$(git config branch.myr9.remote)" = local &&
1043 test "$(git config branch.myr9.merge)" = refs/heads/master &&
1044 test "z$(git config branch.myr9.rebase)" = z
1047 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1048 git config remote.local.url . &&
1049 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1050 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1051 git branch mybase10 &&
1052 git branch --track myr10 mybase2 &&
1053 test "$(git config branch.myr10.remote)" = . &&
1054 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1055 test "z$(git config branch.myr10.rebase)" = z
1058 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1059 git config remote.local.url . &&
1060 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1061 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1062 git branch --no-track myr11 mybase2 &&
1063 test "z$(git config branch.myr11.remote)" = z &&
1064 test "z$(git config branch.myr11.merge)" = z &&
1065 test "z$(git config branch.myr11.rebase)" = z
1068 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1069 git config remote.local.url . &&
1070 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1071 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1072 git branch --no-track myr12 local/master &&
1073 test "z$(git config branch.myr12.remote)" = z &&
1074 test "z$(git config branch.myr12.merge)" = z &&
1075 test "z$(git config branch.myr12.rebase)" = z
1078 test_expect_success 'autosetuprebase never on an untracked local branch' '
1079 git config branch.autosetuprebase never &&
1080 git config remote.local.url . &&
1081 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1082 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1083 git branch --no-track myr13 mybase2 &&
1084 test "z$(git config branch.myr13.remote)" = z &&
1085 test "z$(git config branch.myr13.merge)" = z &&
1086 test "z$(git config branch.myr13.rebase)" = z
1089 test_expect_success 'autosetuprebase local on an untracked local branch' '
1090 git config branch.autosetuprebase local &&
1091 git config remote.local.url . &&
1092 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1093 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1094 git branch --no-track myr14 mybase2 &&
1095 test "z$(git config branch.myr14.remote)" = z &&
1096 test "z$(git config branch.myr14.merge)" = z &&
1097 test "z$(git config branch.myr14.rebase)" = z
1100 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1101 git config branch.autosetuprebase remote &&
1102 git config remote.local.url . &&
1103 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1104 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1105 git branch --no-track myr15 mybase2 &&
1106 test "z$(git config branch.myr15.remote)" = z &&
1107 test "z$(git config branch.myr15.merge)" = z &&
1108 test "z$(git config branch.myr15.rebase)" = z
1111 test_expect_success 'autosetuprebase always on an untracked local branch' '
1112 git config branch.autosetuprebase always &&
1113 git config remote.local.url . &&
1114 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1115 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1116 git branch --no-track myr16 mybase2 &&
1117 test "z$(git config branch.myr16.remote)" = z &&
1118 test "z$(git config branch.myr16.merge)" = z &&
1119 test "z$(git config branch.myr16.rebase)" = z
1122 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1123 git config branch.autosetuprebase never &&
1124 git config remote.local.url . &&
1125 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1126 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1127 git branch --no-track myr17 local/master &&
1128 test "z$(git config branch.myr17.remote)" = z &&
1129 test "z$(git config branch.myr17.merge)" = z &&
1130 test "z$(git config branch.myr17.rebase)" = z
1133 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1134 git config branch.autosetuprebase local &&
1135 git config remote.local.url . &&
1136 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1137 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1138 git branch --no-track myr18 local/master &&
1139 test "z$(git config branch.myr18.remote)" = z &&
1140 test "z$(git config branch.myr18.merge)" = z &&
1141 test "z$(git config branch.myr18.rebase)" = z
1144 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1145 git config branch.autosetuprebase remote &&
1146 git config remote.local.url . &&
1147 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1148 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1149 git branch --no-track myr19 local/master &&
1150 test "z$(git config branch.myr19.remote)" = z &&
1151 test "z$(git config branch.myr19.merge)" = z &&
1152 test "z$(git config branch.myr19.rebase)" = z
1155 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1156 git config branch.autosetuprebase always &&
1157 git config remote.local.url . &&
1158 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1159 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1160 git branch --no-track myr20 local/master &&
1161 test "z$(git config branch.myr20.remote)" = z &&
1162 test "z$(git config branch.myr20.merge)" = z &&
1163 test "z$(git config branch.myr20.rebase)" = z
1166 test_expect_success 'autosetuprebase always on detached HEAD' '
1167 git config branch.autosetupmerge always &&
1168 test_when_finished git checkout master &&
1169 git checkout HEAD^0 &&
1171 test -z "$(git config branch.my11.remote)" &&
1172 test -z "$(git config branch.my11.merge)"
1175 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1176 git config branch.autosetuprebase garbage &&
1177 test_must_fail git branch
1180 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1181 git config --unset branch.autosetuprebase &&
1182 echo "[branch] autosetuprebase" >>.git/config &&
1183 test_must_fail git branch &&
1184 git config --unset branch.autosetuprebase
1187 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1189 git config --unset branch.my8.merge &&
1190 test_must_fail git branch -d my8
1193 test_expect_success 'attempt to delete a branch merged to its base' '
1194 # we are on my9 which is the initial commit; traditionally
1195 # we would not have allowed deleting my8 that is not merged
1196 # to my9, but it is set to track master that already has my8
1197 git config branch.my8.merge refs/heads/master &&
1201 test_expect_success 'attempt to delete a branch merged to its base' '
1202 git checkout master &&
1204 git commit -m "Third commit" A &&
1205 git branch -t my10 my9 &&
1206 git branch -f my10 HEAD^ &&
1207 # we are on master which is at the third commit, and my10
1208 # is behind us, so traditionally we would have allowed deleting
1209 # it; but my10 is set to track my9 that is further behind.
1210 test_must_fail git branch -d my10
1213 test_expect_success 'use --edit-description' '
1214 write_script editor <<-\EOF &&
1215 echo "New contents" >"$1"
1217 EDITOR=./editor git branch --edit-description &&
1218 write_script editor <<-\EOF &&
1219 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1221 EDITOR=./editor git branch --edit-description &&
1222 echo "New contents" >expect &&
1223 test_cmp EDITOR_OUTPUT expect
1226 test_expect_success 'detect typo in branch name when using --edit-description' '
1227 write_script editor <<-\EOF &&
1228 echo "New contents" >"$1"
1230 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1233 test_expect_success 'refuse --edit-description on unborn branch for now' '
1234 write_script editor <<-\EOF &&
1235 echo "New contents" >"$1"
1237 git checkout --orphan unborn &&
1238 test_must_fail env EDITOR=./editor git branch --edit-description
1241 test_expect_success '--merged catches invalid object names' '
1242 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1245 test_expect_success '--merged is incompatible with --no-merged' '
1246 test_must_fail git branch --merged HEAD --no-merged HEAD
1249 test_expect_success 'tracking with unexpected .fetch refspec' '
1265 git remote add a ../a &&
1266 git remote add b ../b &&
1272 git remote add c ../c &&
1273 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1275 git branch --track local/a/master remotes/a/master &&
1276 test "$(git config branch.local/a/master.remote)" = "c" &&
1277 test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
1278 git rev-parse --verify a >expect &&
1279 git rev-parse --verify local/a/master >actual &&
1280 test_cmp expect actual