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." &&
16 git update-index --add A &&
17 git commit -m "Second commit." &&
18 HEAD=$(git rev-parse --verify HEAD)
21 test_expect_success 'git branch --help should not have created a bogus branch' '
22 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
23 test_path_is_missing .git/refs/heads/--help
26 test_expect_success 'branch -h in broken repository' '
31 >.git/refs/heads/master &&
32 test_expect_code 129 git branch -h >usage 2>&1
34 test_i18ngrep "[Uu]sage" broken/usage
37 test_expect_success 'git branch abc should create a branch' '
38 git branch abc && test_path_is_file .git/refs/heads/abc
41 test_expect_success 'git branch a/b/c should create a branch' '
42 git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
45 test_expect_success 'git branch mb master... should create a branch' '
46 git branch mb master... && test_path_is_file .git/refs/heads/mb
49 test_expect_success 'git branch HEAD should fail' '
50 test_must_fail git branch HEAD
54 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
56 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
57 GIT_COMMITTER_DATE="2005-05-26 23:30" \
58 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
59 test_path_is_file .git/refs/heads/d/e/f &&
60 test_path_is_file .git/logs/refs/heads/d/e/f &&
61 test_cmp expect .git/logs/refs/heads/d/e/f
64 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
65 git branch -d d/e/f &&
66 test_path_is_missing .git/refs/heads/d/e/f &&
67 test_must_fail git reflog exists refs/heads/d/e/f
70 test_expect_success 'git branch j/k should work after branch j has been deleted' '
76 test_expect_success 'git branch l should work after branch l/m has been deleted' '
82 test_expect_success 'git branch -m dumps usage' '
83 test_expect_code 128 git branch -m 2>err &&
84 test_i18ngrep "branch name required" err
87 test_expect_success 'git branch -m m broken_symref should work' '
88 test_when_finished "git branch -D broken_symref" &&
89 git branch --create-reflog m &&
90 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
91 git branch -m m broken_symref &&
92 git reflog exists refs/heads/broken_symref &&
93 test_must_fail git reflog exists refs/heads/i_am_broken
96 test_expect_success 'git branch -m m m/m should work' '
97 git branch --create-reflog m &&
98 git branch -m m m/m &&
99 git reflog exists refs/heads/m/m
102 test_expect_success 'git branch -m n/n n should work' '
103 git branch --create-reflog n/n &&
104 git branch -m n/n n &&
105 git reflog exists refs/heads/n
108 # The topmost entry in reflog for branch bbb is about branch creation.
109 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
111 test_expect_success 'git branch -m bbb should rename checked out branch' '
112 test_when_finished git branch -D bbb &&
113 test_when_finished git checkout master &&
114 git checkout -b aaa &&
115 git commit --allow-empty -m "a new commit" &&
116 git rev-parse aaa@{0} >expect &&
118 git rev-parse bbb@{1} >actual &&
119 test_cmp expect actual &&
120 git symbolic-ref HEAD >actual &&
121 echo refs/heads/bbb >expect &&
122 test_cmp expect actual
125 test_expect_success 'renaming checked out branch works with d/f conflict' '
126 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
127 test_when_finished git checkout master &&
128 git checkout -b foo &&
129 git branch -m foo/bar &&
130 git symbolic-ref HEAD >actual &&
131 echo refs/heads/foo/bar >expect &&
132 test_cmp expect actual
135 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
138 test_must_fail git branch -m o/o o
141 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
143 test_must_fail git branch -m o/q o/p
146 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
147 git branch -M o/q o/p
150 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
152 git branch -m -f o/q o/p
155 test_expect_success 'git branch -m q r/q should fail when r exists' '
158 test_must_fail git branch -m q r/q
161 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
163 git checkout -b foo &&
164 test_must_fail git branch -M bar foo
167 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
168 git checkout -b baz &&
170 git branch -M baz bam &&
171 test $(git rev-parse --abbrev-ref HEAD) = bam
174 test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
175 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
176 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
177 grep "^0\{40\}.*$msg$" .git/logs/HEAD
180 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
184 test_commit initial &&
185 git checkout --orphan lonely &&
186 grep lonely .git/HEAD &&
187 test_path_is_missing .git/refs/head/lonely &&
188 git branch -M master mistress &&
189 grep lonely .git/HEAD
193 test_expect_success 'resulting reflog can be shown by log -g' '
194 oid=$(git rev-parse HEAD) &&
195 cat >expect <<-EOF &&
197 HEAD@{2} $oid checkout: moving from foo to baz
199 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
200 test_cmp expect actual
203 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
204 git checkout master &&
205 git worktree add -b baz bazdir &&
206 git worktree add -f bazdir2 baz &&
207 git branch -M baz bam &&
208 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
209 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
210 rm -r bazdir bazdir2 &&
214 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
215 git checkout -b baz &&
216 git worktree add -f bazdir baz &&
219 git branch -M baz bam &&
220 test $(git rev-parse --abbrev-ref HEAD) = bam
222 test $(git rev-parse --abbrev-ref HEAD) = bam &&
227 test_expect_success 'git branch -M master should work when master is checked out' '
228 git checkout master &&
232 test_expect_success 'git branch -M master master should work when master is checked out' '
233 git checkout master &&
234 git branch -M master master
237 test_expect_success 'git branch -M topic topic should work when master is checked out' '
238 git checkout master &&
240 git branch -M topic topic
243 test_expect_success 'git branch -v -d t should work' '
245 git rev-parse --verify refs/heads/t &&
246 git branch -v -d t &&
247 test_must_fail git rev-parse --verify refs/heads/t
250 test_expect_success 'git branch -v -m t s should work' '
252 git rev-parse --verify refs/heads/t &&
253 git branch -v -m t s &&
254 test_must_fail git rev-parse --verify refs/heads/t &&
255 git rev-parse --verify refs/heads/s &&
259 test_expect_success 'git branch -m -d t s should fail' '
261 git rev-parse refs/heads/t &&
262 test_must_fail git branch -m -d t s &&
264 test_must_fail git rev-parse refs/heads/t
267 test_expect_success 'git branch --list -d t should fail' '
269 git rev-parse refs/heads/t &&
270 test_must_fail git branch --list -d t &&
272 test_must_fail git rev-parse refs/heads/t
275 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
276 test_when_finished "rm -rf repo1 repo2" &&
279 git init repo1/sub &&
280 test_commit -C repo1/sub x &&
281 git -C repo1 submodule add ./sub &&
282 git -C repo1 commit -m "adding sub" &&
284 git clone --recurse-submodules repo1 repo2 &&
285 git -C repo2/sub checkout -b work &&
286 test_must_fail git -C repo2/sub branch -D work
289 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
290 test_when_finished "rm -rf nonbare base secondary" &&
293 test_commit -C nonbare x &&
294 git clone --bare nonbare bare &&
295 git -C bare worktree add --detach ../secondary master &&
296 git -C secondary branch -D master
299 test_expect_success 'git branch --list -v with --abbrev' '
300 test_when_finished "git branch -D t" &&
302 git branch -v --list t >actual.default &&
303 git branch -v --list --abbrev t >actual.abbrev &&
304 test_cmp actual.default actual.abbrev &&
306 git branch -v --list --no-abbrev t >actual.noabbrev &&
307 git branch -v --list --abbrev=0 t >actual.0abbrev &&
308 test_cmp actual.noabbrev actual.0abbrev &&
310 git branch -v --list --abbrev=36 t >actual.36abbrev &&
311 # how many hexdigits are used?
312 read name objdefault rest <actual.abbrev &&
313 read name obj36 rest <actual.36abbrev &&
314 objfull=$(git rev-parse --verify t) &&
316 # are we really getting abbreviations?
317 test "$obj36" != "$objdefault" &&
318 expr "$obj36" : "$objdefault" >/dev/null &&
319 test "$objfull" != "$obj36" &&
320 expr "$objfull" : "$obj36" >/dev/null
324 test_expect_success 'git branch --column' '
325 COLUMNS=81 git branch --column=column >actual &&
326 cat >expect <<\EOF &&
327 a/b/c bam foo l * master n o/p r
328 abc bar j/k m/m mb o/o q topic
330 test_cmp expect actual
333 test_expect_success 'git branch --column with an extremely long branch name' '
334 long=this/is/a/part/of/long/branch/name &&
335 long=z$long/$long/$long/$long &&
336 test_when_finished "git branch -d $long" &&
338 COLUMNS=80 git branch --column=column >actual &&
358 test_cmp expect actual
361 test_expect_success 'git branch with column.*' '
362 git config column.ui column &&
363 git config column.branch "dense" &&
364 COLUMNS=80 git branch >actual &&
365 git config --unset column.branch &&
366 git config --unset column.ui &&
367 cat >expect <<\EOF &&
368 a/b/c bam foo l * master n o/p r
369 abc bar j/k m/m mb o/o q topic
371 test_cmp expect actual
374 test_expect_success 'git branch --column -v should fail' '
375 test_must_fail git branch --column -v
378 test_expect_success 'git branch -v with column.ui ignored' '
379 git config column.ui column &&
380 COLUMNS=80 git branch -v | cut -c -9 | sed "s/ *$//" >actual &&
381 git config --unset column.ui &&
382 cat >expect <<\EOF &&
400 test_cmp expect actual
403 mv .git/config .git/config-saved
405 test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
406 git branch -m q q2 &&
410 mv .git/config-saved .git/config
412 git config branch.s/s.dummy Hello
414 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
415 git branch --create-reflog s/s &&
416 git reflog exists refs/heads/s/s &&
417 git branch --create-reflog s/t &&
418 git reflog exists refs/heads/s/t &&
420 git branch -m s/s s &&
421 git reflog exists refs/heads/s
424 test_expect_success 'config information was renamed, too' '
425 test $(git config branch.s.dummy) = Hello &&
426 test_must_fail git config branch.s/s.dummy
429 test_expect_success 'git branch -m correctly renames multiple config sections' '
430 test_when_finished "git checkout master" &&
431 git checkout -b source master &&
433 # Assert that a config file with multiple config sections has
434 # those sections preserved...
435 cat >expect <<-\EOF &&
436 branch.dest.key1=value1
438 branch.dest.key2=value2
440 cat >config.branch <<\EOF &&
441 ;; Note the lack of -\EOF above & mixed indenting here. This is
442 ;; intentional, we are also testing that the formatting of copied
443 ;; sections is preserved.
445 ;; Comment for source. Tabs
447 ;; Comment for the source value
449 ;; Comment for some.gar. Spaces
451 ;; Comment for the some.gar value
453 ;; Comment for source, again. Mixed tabs/spaces.
455 ;; Comment for the source value, again
458 cat config.branch >>.git/config &&
459 git branch -m source dest &&
460 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
461 test_cmp expect actual &&
463 # ...and that the comments for those sections are also
465 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
466 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
467 test_cmp expect actual
470 test_expect_success 'git branch -c dumps usage' '
471 test_expect_code 128 git branch -c 2>err &&
472 test_i18ngrep "branch name required" err
475 test_expect_success 'git branch --copy dumps usage' '
476 test_expect_code 128 git branch --copy 2>err &&
477 test_i18ngrep "branch name required" err
480 test_expect_success 'git branch -c d e should work' '
481 git branch --create-reflog d &&
482 git reflog exists refs/heads/d &&
483 git config branch.d.dummy Hello &&
485 git reflog exists refs/heads/d &&
486 git reflog exists refs/heads/e &&
487 echo Hello >expect &&
488 git config branch.e.dummy >actual &&
489 test_cmp expect actual &&
490 echo Hello >expect &&
491 git config branch.d.dummy >actual &&
492 test_cmp expect actual
495 test_expect_success 'git branch --copy is a synonym for -c' '
496 git branch --create-reflog copy &&
497 git reflog exists refs/heads/copy &&
498 git config branch.copy.dummy Hello &&
499 git branch --copy copy copy-to &&
500 git reflog exists refs/heads/copy &&
501 git reflog exists refs/heads/copy-to &&
502 echo Hello >expect &&
503 git config branch.copy.dummy >actual &&
504 test_cmp expect actual &&
505 echo Hello >expect &&
506 git config branch.copy-to.dummy >actual &&
507 test_cmp expect actual
510 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
511 git checkout -b ee &&
512 git reflog exists refs/heads/ee &&
513 git config branch.ee.dummy Hello &&
514 git branch -c ee ef &&
515 git reflog exists refs/heads/ee &&
516 git reflog exists refs/heads/ef &&
517 test $(git config branch.ee.dummy) = Hello &&
518 test $(git config branch.ef.dummy) = Hello &&
519 test $(git rev-parse --abbrev-ref HEAD) = ee
522 test_expect_success 'git branch -c f/f g/g should work' '
523 git branch --create-reflog f/f &&
524 git reflog exists refs/heads/f/f &&
525 git config branch.f/f.dummy Hello &&
526 git branch -c f/f g/g &&
527 git reflog exists refs/heads/f/f &&
528 git reflog exists refs/heads/g/g &&
529 test $(git config branch.f/f.dummy) = Hello &&
530 test $(git config branch.g/g.dummy) = Hello
533 test_expect_success 'git branch -c m2 m2 should work' '
534 git branch --create-reflog m2 &&
535 git reflog exists refs/heads/m2 &&
536 git config branch.m2.dummy Hello &&
537 git branch -c m2 m2 &&
538 git reflog exists refs/heads/m2 &&
539 test $(git config branch.m2.dummy) = Hello
542 test_expect_success 'git branch -c zz zz/zz should fail' '
543 git branch --create-reflog zz &&
544 git reflog exists refs/heads/zz &&
545 test_must_fail git branch -c zz zz/zz
548 test_expect_success 'git branch -c b/b b should fail' '
549 git branch --create-reflog b/b &&
550 test_must_fail git branch -c b/b b
553 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
554 git branch --create-reflog o/q &&
555 git reflog exists refs/heads/o/q &&
556 git reflog exists refs/heads/o/p &&
557 git branch -C o/q o/p
560 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
561 git reflog exists refs/heads/o/q &&
562 git reflog exists refs/heads/o/p &&
563 git branch -c -f o/q o/p
566 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
569 test_must_fail git branch -c qq rr/qq
572 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
574 git checkout -b b2 &&
575 test_must_fail git branch -C b1 b2
578 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
579 git checkout -b c1 &&
581 git branch -C c1 c2 &&
582 test $(git rev-parse --abbrev-ref HEAD) = c1
585 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
586 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
587 ! grep "$msg$" .git/logs/HEAD
590 test_expect_success 'git branch -C master should work when master is checked out' '
591 git checkout master &&
595 test_expect_success 'git branch -C master master should work when master is checked out' '
596 git checkout master &&
597 git branch -C master master
600 test_expect_success 'git branch -C main5 main5 should work when master is checked out' '
601 git checkout master &&
603 git branch -C main5 main5
606 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
607 git branch --create-reflog cd &&
608 git reflog exists refs/heads/cd &&
609 git config branch.cd.dummy CD &&
610 git branch --create-reflog ab &&
611 git reflog exists refs/heads/ab &&
612 git config branch.ab.dummy AB &&
613 git branch -C ab cd &&
614 git reflog exists refs/heads/ab &&
615 git reflog exists refs/heads/cd &&
616 test $(git config branch.ab.dummy) = AB &&
617 test $(git config branch.cd.dummy) = AB
620 test_expect_success 'git branch -c correctly copies multiple config sections' '
623 test_when_finished "git checkout master" &&
624 git checkout -b source2 master &&
626 # Assert that a config file with multiple config sections has
627 # those sections preserved...
628 cat >expect <<-\EOF &&
629 branch.source2.key1=value1
630 branch.dest2.key1=value1
632 branch.source2.key2=value2
633 branch.dest2.key2=value2
635 cat >config.branch <<\EOF &&
636 ;; Note the lack of -\EOF above & mixed indenting here. This is
637 ;; intentional, we are also testing that the formatting of copied
638 ;; sections is preserved.
640 ;; Comment for source2. Tabs
642 ;; Comment for the source2 value
644 ;; Comment for more.gar. Spaces
646 ;; Comment for the more.gar value
648 ;; Comment for source2, again. Mixed tabs/spaces.
650 ;; Comment for the source2 value, again
653 cat config.branch >>.git/config &&
654 git branch -c source2 dest2 &&
655 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
656 test_cmp expect actual &&
658 # ...and that the comments and formatting for those sections
660 cat >expect <<\EOF &&
661 ;; Comment for source2. Tabs
663 ;; Comment for the source2 value
665 ;; Comment for more.gar. Spaces
667 ;; Comment for the source2 value
669 ;; Comment for more.gar. Spaces
671 ;; Comment for the more.gar value
673 ;; Comment for source2, again. Mixed tabs/spaces.
675 ;; Comment for the source2 value, again
678 ;; Comment for the source2 value, again
681 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
682 test_cmp expect actual
685 test_expect_success 'deleting a symref' '
687 git symbolic-ref refs/heads/symref refs/heads/target &&
688 echo "Deleted branch symref (was refs/heads/target)." >expect &&
689 git branch -d symref >actual &&
690 test_path_is_file .git/refs/heads/target &&
691 test_path_is_missing .git/refs/heads/symref &&
692 test_i18ncmp expect actual
695 test_expect_success 'deleting a dangling symref' '
696 git symbolic-ref refs/heads/dangling-symref nowhere &&
697 test_path_is_file .git/refs/heads/dangling-symref &&
698 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
699 git branch -d dangling-symref >actual &&
700 test_path_is_missing .git/refs/heads/dangling-symref &&
701 test_i18ncmp expect actual
704 test_expect_success 'deleting a self-referential symref' '
705 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
706 test_path_is_file .git/refs/heads/self-reference &&
707 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
708 git branch -d self-reference >actual &&
709 test_path_is_missing .git/refs/heads/self-reference &&
710 test_i18ncmp expect actual
713 test_expect_success 'renaming a symref is not allowed' '
714 git symbolic-ref refs/heads/topic refs/heads/master &&
715 test_must_fail git branch -m topic new-topic &&
716 git symbolic-ref refs/heads/topic &&
717 test_path_is_file .git/refs/heads/master &&
718 test_path_is_missing .git/refs/heads/new-topic
721 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
722 git branch --create-reflog u &&
723 mv .git/logs/refs/heads/u real-u &&
724 ln -s real-u .git/logs/refs/heads/u &&
725 test_must_fail git branch -m u v
728 test_expect_success 'test tracking setup via --track' '
729 git config remote.local.url . &&
730 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
731 (git show-ref -q refs/remotes/local/master || git fetch local) &&
732 git branch --track my1 local/master &&
733 test $(git config branch.my1.remote) = local &&
734 test $(git config branch.my1.merge) = refs/heads/master
737 test_expect_success 'test tracking setup (non-wildcard, matching)' '
738 git config remote.local.url . &&
739 git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
740 (git show-ref -q refs/remotes/local/master || git fetch local) &&
741 git branch --track my4 local/master &&
742 test $(git config branch.my4.remote) = local &&
743 test $(git config branch.my4.merge) = refs/heads/master
746 test_expect_success 'tracking setup fails on non-matching refspec' '
747 git config remote.local.url . &&
748 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
749 (git show-ref -q refs/remotes/local/master || git fetch local) &&
750 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
751 test_must_fail git branch --track my5 local/master &&
752 test_must_fail git config branch.my5.remote &&
753 test_must_fail git config branch.my5.merge
756 test_expect_success 'test tracking setup via config' '
757 git config branch.autosetupmerge true &&
758 git config remote.local.url . &&
759 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
760 (git show-ref -q refs/remotes/local/master || git fetch local) &&
761 git branch my3 local/master &&
762 test $(git config branch.my3.remote) = local &&
763 test $(git config branch.my3.merge) = refs/heads/master
766 test_expect_success 'test overriding tracking setup via --no-track' '
767 git config branch.autosetupmerge true &&
768 git config remote.local.url . &&
769 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
770 (git show-ref -q refs/remotes/local/master || git fetch local) &&
771 git branch --no-track my2 local/master &&
772 git config branch.autosetupmerge false &&
773 ! test "$(git config branch.my2.remote)" = local &&
774 ! test "$(git config branch.my2.merge)" = refs/heads/master
777 test_expect_success 'no tracking without .fetch entries' '
778 git config branch.autosetupmerge true &&
780 git config branch.autosetupmerge false &&
781 test -z "$(git config branch.my6.remote)" &&
782 test -z "$(git config branch.my6.merge)"
785 test_expect_success 'test tracking setup via --track but deeper' '
786 git config remote.local.url . &&
787 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
788 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
789 git branch --track my7 local/o/o &&
790 test "$(git config branch.my7.remote)" = local &&
791 test "$(git config branch.my7.merge)" = refs/heads/o/o
794 test_expect_success 'test deleting branch deletes branch config' '
796 test -z "$(git config branch.my7.remote)" &&
797 test -z "$(git config branch.my7.merge)"
800 test_expect_success 'test deleting branch without config' '
802 sha1=$(git rev-parse my7 | cut -c 1-7) &&
803 echo "Deleted branch my7 (was $sha1)." >expect &&
804 git branch -d my7 >actual 2>&1 &&
805 test_i18ncmp expect actual
808 test_expect_success 'deleting currently checked out branch fails' '
809 git worktree add -b my7 my7 &&
810 test_must_fail git -C my7 branch -d my7 &&
811 test_must_fail git branch -d my7 &&
816 test_expect_success 'test --track without .fetch entries' '
817 git branch --track my8 &&
818 test "$(git config branch.my8.remote)" &&
819 test "$(git config branch.my8.merge)"
822 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
823 git config branch.autosetupmerge always &&
824 git branch my9 HEAD^ &&
825 git config branch.autosetupmerge false
828 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
829 test_must_fail git branch --track my10 HEAD^
832 test_expect_success 'branch from tag w/--track causes failure' '
834 test_must_fail git branch --track my11 foobar
837 test_expect_success '--set-upstream-to fails on multiple branches' '
838 echo "fatal: too many arguments to set new upstream" >expect &&
839 test_must_fail git branch --set-upstream-to master a b c 2>err &&
840 test_i18ncmp expect err
843 test_expect_success '--set-upstream-to fails on detached HEAD' '
844 git checkout HEAD^{} &&
845 test_when_finished git checkout - &&
846 echo "fatal: could not set upstream of HEAD to master when it does not point to any branch." >expect &&
847 test_must_fail git branch --set-upstream-to master 2>err &&
848 test_i18ncmp expect err
851 test_expect_success '--set-upstream-to fails on a missing dst branch' '
852 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
853 test_must_fail git branch --set-upstream-to master does-not-exist 2>err &&
854 test_i18ncmp expect err
857 test_expect_success '--set-upstream-to fails on a missing src branch' '
858 test_must_fail git branch --set-upstream-to does-not-exist master 2>err &&
859 test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
862 test_expect_success '--set-upstream-to fails on a non-ref' '
863 echo "fatal: Cannot setup tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch." >expect &&
864 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
865 test_i18ncmp expect err
868 test_expect_success '--set-upstream-to fails on locked config' '
869 test_when_finished "rm -f .git/config.lock" &&
872 test_must_fail git branch --set-upstream-to locked 2>err &&
873 test_i18ngrep "could not lock config file .git/config" err
876 test_expect_success 'use --set-upstream-to modify HEAD' '
877 test_config branch.master.remote foo &&
878 test_config branch.master.merge foo &&
880 git branch --set-upstream-to my12 &&
881 test "$(git config branch.master.remote)" = "." &&
882 test "$(git config branch.master.merge)" = "refs/heads/my12"
885 test_expect_success 'use --set-upstream-to modify a particular branch' '
887 git branch --set-upstream-to master my13 &&
888 test_when_finished "git branch --unset-upstream my13" &&
889 test "$(git config branch.my13.remote)" = "." &&
890 test "$(git config branch.my13.merge)" = "refs/heads/master"
893 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
894 echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
895 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
896 test_i18ncmp expect err
899 test_expect_success '--unset-upstream should fail if config is locked' '
900 test_when_finished "rm -f .git/config.lock" &&
901 git branch --set-upstream-to locked &&
903 test_must_fail git branch --unset-upstream 2>err &&
904 test_i18ngrep "could not lock config file .git/config" err
907 test_expect_success 'test --unset-upstream on HEAD' '
909 test_config branch.master.remote foo &&
910 test_config branch.master.merge foo &&
911 git branch --set-upstream-to my14 &&
912 git branch --unset-upstream &&
913 test_must_fail git config branch.master.remote &&
914 test_must_fail git config branch.master.merge &&
915 # fail for a branch without upstream set
916 echo "fatal: Branch '"'"'master'"'"' has no upstream information" >expect &&
917 test_must_fail git branch --unset-upstream 2>err &&
918 test_i18ncmp expect err
921 test_expect_success '--unset-upstream should fail on multiple branches' '
922 echo "fatal: too many arguments to unset upstream" >expect &&
923 test_must_fail git branch --unset-upstream a b c 2>err &&
924 test_i18ncmp expect err
927 test_expect_success '--unset-upstream should fail on detached HEAD' '
928 git checkout HEAD^{} &&
929 test_when_finished git checkout - &&
930 echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect &&
931 test_must_fail git branch --unset-upstream 2>err &&
932 test_i18ncmp expect err
935 test_expect_success 'test --unset-upstream on a particular branch' '
937 git branch --set-upstream-to master my14 &&
938 git branch --unset-upstream my14 &&
939 test_must_fail git config branch.my14.remote &&
940 test_must_fail git config branch.my14.merge
943 test_expect_success 'disabled option --set-upstream fails' '
944 test_must_fail git branch --set-upstream origin/master
947 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
948 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
949 cat >expect <<-\EOF &&
950 warning: Not setting branch my13 as its own upstream.
952 test_expect_code 1 git config branch.my13.remote &&
953 test_expect_code 1 git config branch.my13.merge &&
954 test_i18ncmp expect actual
957 # Keep this test last, as it changes the current branch
959 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
961 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
962 GIT_COMMITTER_DATE="2005-05-26 23:30" \
963 git checkout -b g/h/i -l master &&
964 test_path_is_file .git/refs/heads/g/h/i &&
965 test_path_is_file .git/logs/refs/heads/g/h/i &&
966 test_cmp expect .git/logs/refs/heads/g/h/i
969 test_expect_success 'checkout -b makes reflog by default' '
970 git checkout master &&
971 git config --unset core.logAllRefUpdates &&
972 git checkout -b alpha &&
973 git rev-parse --verify alpha@{0}
976 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
977 git checkout master &&
978 git config core.logAllRefUpdates false &&
979 git checkout -b beta &&
980 test_must_fail git rev-parse --verify beta@{0}
983 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
984 git checkout master &&
985 git checkout -lb gamma &&
986 git config --unset core.logAllRefUpdates &&
987 git rev-parse --verify gamma@{0}
990 test_expect_success 'avoid ambiguous track' '
991 git config branch.autosetupmerge true &&
992 git config remote.ambi1.url lalala &&
993 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
994 git config remote.ambi2.url lilili &&
995 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
996 test_must_fail git branch all1 master &&
997 test -z "$(git config branch.all1.merge)"
1000 test_expect_success 'autosetuprebase local on a tracked local branch' '
1001 git config remote.local.url . &&
1002 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1003 git config branch.autosetuprebase local &&
1004 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1005 git branch mybase &&
1006 git branch --track myr1 mybase &&
1007 test "$(git config branch.myr1.remote)" = . &&
1008 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1009 test "$(git config branch.myr1.rebase)" = true
1012 test_expect_success 'autosetuprebase always on a tracked local branch' '
1013 git config remote.local.url . &&
1014 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1015 git config branch.autosetuprebase always &&
1016 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1017 git branch mybase2 &&
1018 git branch --track myr2 mybase &&
1019 test "$(git config branch.myr2.remote)" = . &&
1020 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1021 test "$(git config branch.myr2.rebase)" = true
1024 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1025 git config remote.local.url . &&
1026 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1027 git config branch.autosetuprebase remote &&
1028 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1029 git branch mybase3 &&
1030 git branch --track myr3 mybase2 &&
1031 test "$(git config branch.myr3.remote)" = . &&
1032 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1033 ! test "$(git config branch.myr3.rebase)" = true
1036 test_expect_success 'autosetuprebase never on a tracked local branch' '
1037 git config remote.local.url . &&
1038 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1039 git config branch.autosetuprebase never &&
1040 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1041 git branch mybase4 &&
1042 git branch --track myr4 mybase2 &&
1043 test "$(git config branch.myr4.remote)" = . &&
1044 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1045 ! test "$(git config branch.myr4.rebase)" = true
1048 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1049 git config remote.local.url . &&
1050 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1051 git config branch.autosetuprebase local &&
1052 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1053 git branch --track myr5 local/master &&
1054 test "$(git config branch.myr5.remote)" = local &&
1055 test "$(git config branch.myr5.merge)" = refs/heads/master &&
1056 ! test "$(git config branch.myr5.rebase)" = true
1059 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1060 git config remote.local.url . &&
1061 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1062 git config branch.autosetuprebase never &&
1063 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1064 git branch --track myr6 local/master &&
1065 test "$(git config branch.myr6.remote)" = local &&
1066 test "$(git config branch.myr6.merge)" = refs/heads/master &&
1067 ! test "$(git config branch.myr6.rebase)" = true
1070 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1071 git config remote.local.url . &&
1072 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1073 git config branch.autosetuprebase remote &&
1074 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1075 git branch --track myr7 local/master &&
1076 test "$(git config branch.myr7.remote)" = local &&
1077 test "$(git config branch.myr7.merge)" = refs/heads/master &&
1078 test "$(git config branch.myr7.rebase)" = true
1081 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1082 git config remote.local.url . &&
1083 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1084 git config branch.autosetuprebase remote &&
1085 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1086 git branch --track myr8 local/master &&
1087 test "$(git config branch.myr8.remote)" = local &&
1088 test "$(git config branch.myr8.merge)" = refs/heads/master &&
1089 test "$(git config branch.myr8.rebase)" = true
1092 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1093 git config --unset branch.autosetuprebase &&
1094 git config remote.local.url . &&
1095 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1096 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1097 git branch --track myr9 local/master &&
1098 test "$(git config branch.myr9.remote)" = local &&
1099 test "$(git config branch.myr9.merge)" = refs/heads/master &&
1100 test "z$(git config branch.myr9.rebase)" = z
1103 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1104 git config remote.local.url . &&
1105 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1106 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1107 git branch mybase10 &&
1108 git branch --track myr10 mybase2 &&
1109 test "$(git config branch.myr10.remote)" = . &&
1110 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1111 test "z$(git config branch.myr10.rebase)" = z
1114 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1115 git config remote.local.url . &&
1116 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1117 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1118 git branch --no-track myr11 mybase2 &&
1119 test "z$(git config branch.myr11.remote)" = z &&
1120 test "z$(git config branch.myr11.merge)" = z &&
1121 test "z$(git config branch.myr11.rebase)" = z
1124 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1125 git config remote.local.url . &&
1126 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1127 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1128 git branch --no-track myr12 local/master &&
1129 test "z$(git config branch.myr12.remote)" = z &&
1130 test "z$(git config branch.myr12.merge)" = z &&
1131 test "z$(git config branch.myr12.rebase)" = z
1134 test_expect_success 'autosetuprebase never on an untracked local branch' '
1135 git config branch.autosetuprebase never &&
1136 git config remote.local.url . &&
1137 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1138 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1139 git branch --no-track myr13 mybase2 &&
1140 test "z$(git config branch.myr13.remote)" = z &&
1141 test "z$(git config branch.myr13.merge)" = z &&
1142 test "z$(git config branch.myr13.rebase)" = z
1145 test_expect_success 'autosetuprebase local on an untracked local branch' '
1146 git config branch.autosetuprebase local &&
1147 git config remote.local.url . &&
1148 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1149 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1150 git branch --no-track myr14 mybase2 &&
1151 test "z$(git config branch.myr14.remote)" = z &&
1152 test "z$(git config branch.myr14.merge)" = z &&
1153 test "z$(git config branch.myr14.rebase)" = z
1156 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1157 git config branch.autosetuprebase remote &&
1158 git config remote.local.url . &&
1159 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1160 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1161 git branch --no-track myr15 mybase2 &&
1162 test "z$(git config branch.myr15.remote)" = z &&
1163 test "z$(git config branch.myr15.merge)" = z &&
1164 test "z$(git config branch.myr15.rebase)" = z
1167 test_expect_success 'autosetuprebase always on an untracked local branch' '
1168 git config branch.autosetuprebase always &&
1169 git config remote.local.url . &&
1170 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1171 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1172 git branch --no-track myr16 mybase2 &&
1173 test "z$(git config branch.myr16.remote)" = z &&
1174 test "z$(git config branch.myr16.merge)" = z &&
1175 test "z$(git config branch.myr16.rebase)" = z
1178 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1179 git config branch.autosetuprebase never &&
1180 git config remote.local.url . &&
1181 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1182 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1183 git branch --no-track myr17 local/master &&
1184 test "z$(git config branch.myr17.remote)" = z &&
1185 test "z$(git config branch.myr17.merge)" = z &&
1186 test "z$(git config branch.myr17.rebase)" = z
1189 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1190 git config branch.autosetuprebase local &&
1191 git config remote.local.url . &&
1192 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1193 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1194 git branch --no-track myr18 local/master &&
1195 test "z$(git config branch.myr18.remote)" = z &&
1196 test "z$(git config branch.myr18.merge)" = z &&
1197 test "z$(git config branch.myr18.rebase)" = z
1200 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1201 git config branch.autosetuprebase remote &&
1202 git config remote.local.url . &&
1203 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1204 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1205 git branch --no-track myr19 local/master &&
1206 test "z$(git config branch.myr19.remote)" = z &&
1207 test "z$(git config branch.myr19.merge)" = z &&
1208 test "z$(git config branch.myr19.rebase)" = z
1211 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1212 git config branch.autosetuprebase always &&
1213 git config remote.local.url . &&
1214 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1215 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1216 git branch --no-track myr20 local/master &&
1217 test "z$(git config branch.myr20.remote)" = z &&
1218 test "z$(git config branch.myr20.merge)" = z &&
1219 test "z$(git config branch.myr20.rebase)" = z
1222 test_expect_success 'autosetuprebase always on detached HEAD' '
1223 git config branch.autosetupmerge always &&
1224 test_when_finished git checkout master &&
1225 git checkout HEAD^0 &&
1227 test -z "$(git config branch.my11.remote)" &&
1228 test -z "$(git config branch.my11.merge)"
1231 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1232 git config branch.autosetuprebase garbage &&
1233 test_must_fail git branch
1236 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1237 git config --unset branch.autosetuprebase &&
1238 echo "[branch] autosetuprebase" >>.git/config &&
1239 test_must_fail git branch &&
1240 git config --unset branch.autosetuprebase
1243 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1245 git config --unset branch.my8.merge &&
1246 test_must_fail git branch -d my8
1249 test_expect_success 'attempt to delete a branch merged to its base' '
1250 # we are on my9 which is the initial commit; traditionally
1251 # we would not have allowed deleting my8 that is not merged
1252 # to my9, but it is set to track master that already has my8
1253 git config branch.my8.merge refs/heads/master &&
1257 test_expect_success 'attempt to delete a branch merged to its base' '
1258 git checkout master &&
1260 git commit -m "Third commit" A &&
1261 git branch -t my10 my9 &&
1262 git branch -f my10 HEAD^ &&
1263 # we are on master which is at the third commit, and my10
1264 # is behind us, so traditionally we would have allowed deleting
1265 # it; but my10 is set to track my9 that is further behind.
1266 test_must_fail git branch -d my10
1269 test_expect_success 'use --edit-description' '
1270 write_script editor <<-\EOF &&
1271 echo "New contents" >"$1"
1273 EDITOR=./editor git branch --edit-description &&
1274 write_script editor <<-\EOF &&
1275 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1277 EDITOR=./editor git branch --edit-description &&
1278 echo "New contents" >expect &&
1279 test_cmp expect EDITOR_OUTPUT
1282 test_expect_success 'detect typo in branch name when using --edit-description' '
1283 write_script editor <<-\EOF &&
1284 echo "New contents" >"$1"
1286 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1289 test_expect_success 'refuse --edit-description on unborn branch for now' '
1290 test_when_finished "git checkout master" &&
1291 write_script editor <<-\EOF &&
1292 echo "New contents" >"$1"
1294 git checkout --orphan unborn &&
1295 test_must_fail env EDITOR=./editor git branch --edit-description
1298 test_expect_success '--merged catches invalid object names' '
1299 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1302 test_expect_success '--list during rebase' '
1303 test_when_finished "reset_rebase" &&
1304 git checkout master &&
1305 FAKE_LINES="1 edit 2" &&
1306 export FAKE_LINES &&
1308 git rebase -i HEAD~2 &&
1309 git branch --list >actual &&
1310 test_i18ngrep "rebasing master" actual
1313 test_expect_success '--list during rebase from detached HEAD' '
1314 test_when_finished "reset_rebase && git checkout master" &&
1315 git checkout master^0 &&
1316 oid=$(git rev-parse --short HEAD) &&
1317 FAKE_LINES="1 edit 2" &&
1318 export FAKE_LINES &&
1320 git rebase -i HEAD~2 &&
1321 git branch --list >actual &&
1322 test_i18ngrep "rebasing detached HEAD $oid" actual
1325 test_expect_success 'tracking with unexpected .fetch refspec' '
1341 git remote add a ../a &&
1342 git remote add b ../b &&
1348 git remote add c ../c &&
1349 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1351 git branch --track local/a/master remotes/a/master &&
1352 test "$(git config branch.local/a/master.remote)" = "c" &&
1353 test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
1354 git rev-parse --verify a >expect &&
1355 git rev-parse --verify local/a/master >actual &&
1356 test_cmp expect actual
1360 test_expect_success 'configured committerdate sort' '
1364 git config branch.sort committerdate &&
1365 test_commit initial &&
1366 git checkout -b a &&
1368 git checkout -b c &&
1370 git checkout -b b &&
1372 git branch >actual &&
1373 cat >expect <<-\EOF &&
1379 test_cmp expect actual
1383 test_expect_success 'option override configured sort' '
1386 git config branch.sort committerdate &&
1387 git branch --sort=refname >actual &&
1388 cat >expect <<-\EOF &&
1394 test_cmp expect actual
1398 test_expect_success 'invalid sort parameter in configuration' '
1401 git config branch.sort "v:notvalid" &&
1402 test_must_fail git branch