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 'git branch -m o/o o should fail when o/p exists' '
123 test_must_fail git branch -m o/o o
126 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
128 test_must_fail git branch -m o/q o/p
131 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
132 git branch -M o/q o/p
135 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
137 git branch -m -f o/q o/p
140 test_expect_success 'git branch -m q r/q should fail when r exists' '
143 test_must_fail git branch -m q r/q
146 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
148 git checkout -b foo &&
149 test_must_fail git branch -M bar foo
152 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
153 git checkout -b baz &&
155 git branch -M baz bam &&
156 test $(git rev-parse --abbrev-ref HEAD) = bam
159 test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
160 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
161 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
162 grep "^0\{40\}.*$msg$" .git/logs/HEAD
165 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
169 test_commit initial &&
170 git checkout --orphan lonely &&
171 grep lonely .git/HEAD &&
172 test_path_is_missing .git/refs/head/lonely &&
173 git branch -M master mistress &&
174 grep lonely .git/HEAD
178 test_expect_success 'resulting reflog can be shown by log -g' '
179 oid=$(git rev-parse HEAD) &&
180 cat >expect <<-EOF &&
182 HEAD@{2} $oid checkout: moving from foo to baz
184 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
185 test_cmp expect actual
188 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
189 git checkout master &&
190 git worktree add -b baz bazdir &&
191 git worktree add -f bazdir2 baz &&
192 git branch -M baz bam &&
193 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
194 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
197 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
198 git checkout -b baz &&
199 git worktree add -f bazdir3 baz &&
202 git branch -M baz bam &&
203 test $(git rev-parse --abbrev-ref HEAD) = bam
205 test $(git rev-parse --abbrev-ref HEAD) = bam
208 test_expect_success 'git branch -M master should work when master is checked out' '
209 git checkout master &&
213 test_expect_success 'git branch -M master master should work when master is checked out' '
214 git checkout master &&
215 git branch -M master master
218 test_expect_success 'git branch -M master2 master2 should work when master is checked out' '
219 git checkout master &&
220 git branch master2 &&
221 git branch -M master2 master2
224 test_expect_success 'git branch -v -d t should work' '
226 test_path_is_file .git/refs/heads/t &&
227 git branch -v -d t &&
228 test_path_is_missing .git/refs/heads/t
231 test_expect_success 'git branch -v -m t s should work' '
233 test_path_is_file .git/refs/heads/t &&
234 git branch -v -m t s &&
235 test_path_is_missing .git/refs/heads/t &&
236 test_path_is_file .git/refs/heads/s &&
240 test_expect_success 'git branch -m -d t s should fail' '
242 test_path_is_file .git/refs/heads/t &&
243 test_must_fail git branch -m -d t s &&
245 test_path_is_missing .git/refs/heads/t
248 test_expect_success 'git branch --list -d t should fail' '
250 test_path_is_file .git/refs/heads/t &&
251 test_must_fail git branch --list -d t &&
253 test_path_is_missing .git/refs/heads/t
256 test_expect_success 'git branch --list -v with --abbrev' '
257 test_when_finished "git branch -D t" &&
259 git branch -v --list t >actual.default &&
260 git branch -v --list --abbrev t >actual.abbrev &&
261 test_cmp actual.default actual.abbrev &&
263 git branch -v --list --no-abbrev t >actual.noabbrev &&
264 git branch -v --list --abbrev=0 t >actual.0abbrev &&
265 test_cmp actual.noabbrev actual.0abbrev &&
267 git branch -v --list --abbrev=36 t >actual.36abbrev &&
268 # how many hexdigits are used?
269 read name objdefault rest <actual.abbrev &&
270 read name obj36 rest <actual.36abbrev &&
271 objfull=$(git rev-parse --verify t) &&
273 # are we really getting abbreviations?
274 test "$obj36" != "$objdefault" &&
275 expr "$obj36" : "$objdefault" >/dev/null &&
276 test "$objfull" != "$obj36" &&
277 expr "$objfull" : "$obj36" >/dev/null
281 test_expect_success 'git branch --column' '
282 COLUMNS=81 git branch --column=column >actual &&
283 cat >expected <<\EOF &&
284 a/b/c bam foo l * master n o/p r
285 abc bar j/k m/m master2 o/o q
287 test_cmp expected actual
290 test_expect_success 'git branch --column with an extremely long branch name' '
291 long=this/is/a/part/of/long/branch/name &&
292 long=z$long/$long/$long/$long &&
293 test_when_finished "git branch -d $long" &&
295 COLUMNS=80 git branch --column=column >actual &&
296 cat >expected <<EOF &&
314 test_cmp expected actual
317 test_expect_success 'git branch with column.*' '
318 git config column.ui column &&
319 git config column.branch "dense" &&
320 COLUMNS=80 git branch >actual &&
321 git config --unset column.branch &&
322 git config --unset column.ui &&
323 cat >expected <<\EOF &&
324 a/b/c bam foo l * master n o/p r
325 abc bar j/k m/m master2 o/o q
327 test_cmp expected actual
330 test_expect_success 'git branch --column -v should fail' '
331 test_must_fail git branch --column -v
334 test_expect_success 'git branch -v with column.ui ignored' '
335 git config column.ui column &&
336 COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
337 git config --unset column.ui &&
338 cat >expected <<\EOF &&
355 test_cmp expected actual
358 mv .git/config .git/config-saved
360 test_expect_success 'git branch -m q q2 without config should succeed' '
361 git branch -m q q2 &&
365 mv .git/config-saved .git/config
367 git config branch.s/s.dummy Hello
369 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
371 git reflog exists refs/heads/s/s &&
373 git reflog exists refs/heads/s/t &&
375 git branch -m s/s s &&
376 git reflog exists refs/heads/s
379 test_expect_success 'config information was renamed, too' '
380 test $(git config branch.s.dummy) = Hello &&
381 test_must_fail git config branch.s/s.dummy
384 test_expect_success 'deleting a symref' '
386 git symbolic-ref refs/heads/symref refs/heads/target &&
387 echo "Deleted branch symref (was refs/heads/target)." >expect &&
388 git branch -d symref >actual &&
389 test_path_is_file .git/refs/heads/target &&
390 test_path_is_missing .git/refs/heads/symref &&
391 test_i18ncmp expect actual
394 test_expect_success 'deleting a dangling symref' '
395 git symbolic-ref refs/heads/dangling-symref nowhere &&
396 test_path_is_file .git/refs/heads/dangling-symref &&
397 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
398 git branch -d dangling-symref >actual &&
399 test_path_is_missing .git/refs/heads/dangling-symref &&
400 test_i18ncmp expect actual
403 test_expect_success 'deleting a self-referential symref' '
404 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
405 test_path_is_file .git/refs/heads/self-reference &&
406 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
407 git branch -d self-reference >actual &&
408 test_path_is_missing .git/refs/heads/self-reference &&
409 test_i18ncmp expect actual
412 test_expect_success 'renaming a symref is not allowed' '
413 git symbolic-ref refs/heads/master2 refs/heads/master &&
414 test_must_fail git branch -m master2 master3 &&
415 git symbolic-ref refs/heads/master2 &&
416 test_path_is_file .git/refs/heads/master &&
417 test_path_is_missing .git/refs/heads/master3
420 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
422 mv .git/logs/refs/heads/u real-u &&
423 ln -s real-u .git/logs/refs/heads/u &&
424 test_must_fail git branch -m u v
427 test_expect_success 'test tracking setup via --track' '
428 git config remote.local.url . &&
429 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
430 (git show-ref -q refs/remotes/local/master || git fetch local) &&
431 git branch --track my1 local/master &&
432 test $(git config branch.my1.remote) = local &&
433 test $(git config branch.my1.merge) = refs/heads/master
436 test_expect_success 'test tracking setup (non-wildcard, matching)' '
437 git config remote.local.url . &&
438 git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
439 (git show-ref -q refs/remotes/local/master || git fetch local) &&
440 git branch --track my4 local/master &&
441 test $(git config branch.my4.remote) = local &&
442 test $(git config branch.my4.merge) = refs/heads/master
445 test_expect_success 'tracking setup fails on non-matching refspec' '
446 git config remote.local.url . &&
447 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
448 (git show-ref -q refs/remotes/local/master || git fetch local) &&
449 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
450 test_must_fail git branch --track my5 local/master &&
451 test_must_fail git config branch.my5.remote &&
452 test_must_fail git config branch.my5.merge
455 test_expect_success 'test tracking setup via config' '
456 git config branch.autosetupmerge true &&
457 git config remote.local.url . &&
458 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
459 (git show-ref -q refs/remotes/local/master || git fetch local) &&
460 git branch my3 local/master &&
461 test $(git config branch.my3.remote) = local &&
462 test $(git config branch.my3.merge) = refs/heads/master
465 test_expect_success 'test overriding tracking setup via --no-track' '
466 git config branch.autosetupmerge true &&
467 git config remote.local.url . &&
468 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
469 (git show-ref -q refs/remotes/local/master || git fetch local) &&
470 git branch --no-track my2 local/master &&
471 git config branch.autosetupmerge false &&
472 ! test "$(git config branch.my2.remote)" = local &&
473 ! test "$(git config branch.my2.merge)" = refs/heads/master
476 test_expect_success 'no tracking without .fetch entries' '
477 git config branch.autosetupmerge true &&
479 git config branch.autosetupmerge false &&
480 test -z "$(git config branch.my6.remote)" &&
481 test -z "$(git config branch.my6.merge)"
484 test_expect_success 'test tracking setup via --track but deeper' '
485 git config remote.local.url . &&
486 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
487 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
488 git branch --track my7 local/o/o &&
489 test "$(git config branch.my7.remote)" = local &&
490 test "$(git config branch.my7.merge)" = refs/heads/o/o
493 test_expect_success 'test deleting branch deletes branch config' '
495 test -z "$(git config branch.my7.remote)" &&
496 test -z "$(git config branch.my7.merge)"
499 test_expect_success 'test deleting branch without config' '
501 sha1=$(git rev-parse my7 | cut -c 1-7) &&
502 echo "Deleted branch my7 (was $sha1)." >expect &&
503 git branch -d my7 >actual 2>&1 &&
504 test_i18ncmp expect actual
507 test_expect_success 'deleting currently checked out branch fails' '
508 git worktree add -b my7 my7 &&
509 test_must_fail git -C my7 branch -d my7 &&
510 test_must_fail git branch -d my7
513 test_expect_success 'test --track without .fetch entries' '
514 git branch --track my8 &&
515 test "$(git config branch.my8.remote)" &&
516 test "$(git config branch.my8.merge)"
519 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
520 git config branch.autosetupmerge always &&
521 git branch my9 HEAD^ &&
522 git config branch.autosetupmerge false
525 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
526 test_must_fail git branch --track my10 HEAD^
529 test_expect_success 'branch from tag w/--track causes failure' '
531 test_must_fail git branch --track my11 foobar
534 test_expect_success '--set-upstream-to fails on multiple branches' '
535 test_must_fail git branch --set-upstream-to master a b c
538 test_expect_success '--set-upstream-to fails on detached HEAD' '
539 git checkout HEAD^{} &&
540 test_must_fail git branch --set-upstream-to master &&
544 test_expect_success '--set-upstream-to fails on a missing dst branch' '
545 test_must_fail git branch --set-upstream-to master does-not-exist
548 test_expect_success '--set-upstream-to fails on a missing src branch' '
549 test_must_fail git branch --set-upstream-to does-not-exist master
552 test_expect_success '--set-upstream-to fails on a non-ref' '
553 test_must_fail git branch --set-upstream-to HEAD^{}
556 test_expect_success '--set-upstream-to fails on locked config' '
557 test_when_finished "rm -f .git/config.lock" &&
560 test_must_fail git branch --set-upstream-to locked
563 test_expect_success 'use --set-upstream-to modify HEAD' '
564 test_config branch.master.remote foo &&
565 test_config branch.master.merge foo &&
567 git branch --set-upstream-to my12 &&
568 test "$(git config branch.master.remote)" = "." &&
569 test "$(git config branch.master.merge)" = "refs/heads/my12"
572 test_expect_success 'use --set-upstream-to modify a particular branch' '
574 git branch --set-upstream-to master my13 &&
575 test_when_finished "git branch --unset-upstream my13" &&
576 test "$(git config branch.my13.remote)" = "." &&
577 test "$(git config branch.my13.merge)" = "refs/heads/master"
580 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
581 test_must_fail git branch --unset-upstream i-dont-exist
584 test_expect_success '--unset-upstream should fail if config is locked' '
585 test_when_finished "rm -f .git/config.lock" &&
586 git branch --set-upstream-to locked &&
588 test_must_fail git branch --unset-upstream
591 test_expect_success 'test --unset-upstream on HEAD' '
593 test_config branch.master.remote foo &&
594 test_config branch.master.merge foo &&
595 git branch --set-upstream-to my14 &&
596 git branch --unset-upstream &&
597 test_must_fail git config branch.master.remote &&
598 test_must_fail git config branch.master.merge &&
599 # fail for a branch without upstream set
600 test_must_fail git branch --unset-upstream
603 test_expect_success '--unset-upstream should fail on multiple branches' '
604 test_must_fail git branch --unset-upstream a b c
607 test_expect_success '--unset-upstream should fail on detached HEAD' '
608 git checkout HEAD^{} &&
609 test_must_fail git branch --unset-upstream &&
613 test_expect_success 'test --unset-upstream on a particular branch' '
615 git branch --set-upstream-to master my14 &&
616 git branch --unset-upstream my14 &&
617 test_must_fail git config branch.my14.remote &&
618 test_must_fail git config branch.my14.merge
621 test_expect_success '--set-upstream fails' '
622 test_must_fail git branch --set-upstream origin/master
625 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
626 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
627 cat >expected <<-\EOF &&
628 warning: Not setting branch my13 as its own upstream.
630 test_expect_code 1 git config branch.my13.remote &&
631 test_expect_code 1 git config branch.my13.merge &&
632 test_i18ncmp expected actual
635 # Keep this test last, as it changes the current branch
637 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
639 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
640 GIT_COMMITTER_DATE="2005-05-26 23:30" \
641 git checkout -b g/h/i -l master &&
642 test_path_is_file .git/refs/heads/g/h/i &&
643 test_path_is_file .git/logs/refs/heads/g/h/i &&
644 test_cmp expect .git/logs/refs/heads/g/h/i
647 test_expect_success 'checkout -b makes reflog by default' '
648 git checkout master &&
649 git config --unset core.logAllRefUpdates &&
650 git checkout -b alpha &&
651 git rev-parse --verify alpha@{0}
654 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
655 git checkout master &&
656 git config core.logAllRefUpdates false &&
657 git checkout -b beta &&
658 test_must_fail git rev-parse --verify beta@{0}
661 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
662 git checkout master &&
663 git checkout -lb gamma &&
664 git config --unset core.logAllRefUpdates &&
665 git rev-parse --verify gamma@{0}
668 test_expect_success 'avoid ambiguous track' '
669 git config branch.autosetupmerge true &&
670 git config remote.ambi1.url lalala &&
671 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
672 git config remote.ambi2.url lilili &&
673 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
674 test_must_fail git branch all1 master &&
675 test -z "$(git config branch.all1.merge)"
678 test_expect_success 'autosetuprebase local on a tracked local branch' '
679 git config remote.local.url . &&
680 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
681 git config branch.autosetuprebase local &&
682 (git show-ref -q refs/remotes/local/o || git fetch local) &&
684 git branch --track myr1 mybase &&
685 test "$(git config branch.myr1.remote)" = . &&
686 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
687 test "$(git config branch.myr1.rebase)" = true
690 test_expect_success 'autosetuprebase always on a tracked local branch' '
691 git config remote.local.url . &&
692 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
693 git config branch.autosetuprebase always &&
694 (git show-ref -q refs/remotes/local/o || git fetch local) &&
695 git branch mybase2 &&
696 git branch --track myr2 mybase &&
697 test "$(git config branch.myr2.remote)" = . &&
698 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
699 test "$(git config branch.myr2.rebase)" = true
702 test_expect_success 'autosetuprebase remote on a tracked local branch' '
703 git config remote.local.url . &&
704 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
705 git config branch.autosetuprebase remote &&
706 (git show-ref -q refs/remotes/local/o || git fetch local) &&
707 git branch mybase3 &&
708 git branch --track myr3 mybase2 &&
709 test "$(git config branch.myr3.remote)" = . &&
710 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
711 ! test "$(git config branch.myr3.rebase)" = true
714 test_expect_success 'autosetuprebase never on a tracked local branch' '
715 git config remote.local.url . &&
716 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
717 git config branch.autosetuprebase never &&
718 (git show-ref -q refs/remotes/local/o || git fetch local) &&
719 git branch mybase4 &&
720 git branch --track myr4 mybase2 &&
721 test "$(git config branch.myr4.remote)" = . &&
722 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
723 ! test "$(git config branch.myr4.rebase)" = true
726 test_expect_success 'autosetuprebase local on a tracked remote branch' '
727 git config remote.local.url . &&
728 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
729 git config branch.autosetuprebase local &&
730 (git show-ref -q refs/remotes/local/master || git fetch local) &&
731 git branch --track myr5 local/master &&
732 test "$(git config branch.myr5.remote)" = local &&
733 test "$(git config branch.myr5.merge)" = refs/heads/master &&
734 ! test "$(git config branch.myr5.rebase)" = true
737 test_expect_success 'autosetuprebase never on a tracked remote branch' '
738 git config remote.local.url . &&
739 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
740 git config branch.autosetuprebase never &&
741 (git show-ref -q refs/remotes/local/master || git fetch local) &&
742 git branch --track myr6 local/master &&
743 test "$(git config branch.myr6.remote)" = local &&
744 test "$(git config branch.myr6.merge)" = refs/heads/master &&
745 ! test "$(git config branch.myr6.rebase)" = true
748 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
749 git config remote.local.url . &&
750 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
751 git config branch.autosetuprebase remote &&
752 (git show-ref -q refs/remotes/local/master || git fetch local) &&
753 git branch --track myr7 local/master &&
754 test "$(git config branch.myr7.remote)" = local &&
755 test "$(git config branch.myr7.merge)" = refs/heads/master &&
756 test "$(git config branch.myr7.rebase)" = true
759 test_expect_success 'autosetuprebase always on a tracked remote branch' '
760 git config remote.local.url . &&
761 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
762 git config branch.autosetuprebase remote &&
763 (git show-ref -q refs/remotes/local/master || git fetch local) &&
764 git branch --track myr8 local/master &&
765 test "$(git config branch.myr8.remote)" = local &&
766 test "$(git config branch.myr8.merge)" = refs/heads/master &&
767 test "$(git config branch.myr8.rebase)" = true
770 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
771 git config --unset branch.autosetuprebase &&
772 git config remote.local.url . &&
773 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
774 (git show-ref -q refs/remotes/local/master || git fetch local) &&
775 git branch --track myr9 local/master &&
776 test "$(git config branch.myr9.remote)" = local &&
777 test "$(git config branch.myr9.merge)" = refs/heads/master &&
778 test "z$(git config branch.myr9.rebase)" = z
781 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
782 git config remote.local.url . &&
783 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
784 (git show-ref -q refs/remotes/local/o || git fetch local) &&
785 git branch mybase10 &&
786 git branch --track myr10 mybase2 &&
787 test "$(git config branch.myr10.remote)" = . &&
788 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
789 test "z$(git config branch.myr10.rebase)" = z
792 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
793 git config remote.local.url . &&
794 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
795 (git show-ref -q refs/remotes/local/master || git fetch local) &&
796 git branch --no-track myr11 mybase2 &&
797 test "z$(git config branch.myr11.remote)" = z &&
798 test "z$(git config branch.myr11.merge)" = z &&
799 test "z$(git config branch.myr11.rebase)" = z
802 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
803 git config remote.local.url . &&
804 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
805 (git show-ref -q refs/remotes/local/master || git fetch local) &&
806 git branch --no-track myr12 local/master &&
807 test "z$(git config branch.myr12.remote)" = z &&
808 test "z$(git config branch.myr12.merge)" = z &&
809 test "z$(git config branch.myr12.rebase)" = z
812 test_expect_success 'autosetuprebase never on an untracked local branch' '
813 git config branch.autosetuprebase never &&
814 git config remote.local.url . &&
815 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
816 (git show-ref -q refs/remotes/local/master || git fetch local) &&
817 git branch --no-track myr13 mybase2 &&
818 test "z$(git config branch.myr13.remote)" = z &&
819 test "z$(git config branch.myr13.merge)" = z &&
820 test "z$(git config branch.myr13.rebase)" = z
823 test_expect_success 'autosetuprebase local on an untracked local branch' '
824 git config branch.autosetuprebase local &&
825 git config remote.local.url . &&
826 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
827 (git show-ref -q refs/remotes/local/master || git fetch local) &&
828 git branch --no-track myr14 mybase2 &&
829 test "z$(git config branch.myr14.remote)" = z &&
830 test "z$(git config branch.myr14.merge)" = z &&
831 test "z$(git config branch.myr14.rebase)" = z
834 test_expect_success 'autosetuprebase remote on an untracked local branch' '
835 git config branch.autosetuprebase remote &&
836 git config remote.local.url . &&
837 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
838 (git show-ref -q refs/remotes/local/master || git fetch local) &&
839 git branch --no-track myr15 mybase2 &&
840 test "z$(git config branch.myr15.remote)" = z &&
841 test "z$(git config branch.myr15.merge)" = z &&
842 test "z$(git config branch.myr15.rebase)" = z
845 test_expect_success 'autosetuprebase always on an untracked local branch' '
846 git config branch.autosetuprebase always &&
847 git config remote.local.url . &&
848 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
849 (git show-ref -q refs/remotes/local/master || git fetch local) &&
850 git branch --no-track myr16 mybase2 &&
851 test "z$(git config branch.myr16.remote)" = z &&
852 test "z$(git config branch.myr16.merge)" = z &&
853 test "z$(git config branch.myr16.rebase)" = z
856 test_expect_success 'autosetuprebase never on an untracked remote branch' '
857 git config branch.autosetuprebase never &&
858 git config remote.local.url . &&
859 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
860 (git show-ref -q refs/remotes/local/master || git fetch local) &&
861 git branch --no-track myr17 local/master &&
862 test "z$(git config branch.myr17.remote)" = z &&
863 test "z$(git config branch.myr17.merge)" = z &&
864 test "z$(git config branch.myr17.rebase)" = z
867 test_expect_success 'autosetuprebase local on an untracked remote branch' '
868 git config branch.autosetuprebase local &&
869 git config remote.local.url . &&
870 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
871 (git show-ref -q refs/remotes/local/master || git fetch local) &&
872 git branch --no-track myr18 local/master &&
873 test "z$(git config branch.myr18.remote)" = z &&
874 test "z$(git config branch.myr18.merge)" = z &&
875 test "z$(git config branch.myr18.rebase)" = z
878 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
879 git config branch.autosetuprebase remote &&
880 git config remote.local.url . &&
881 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
882 (git show-ref -q refs/remotes/local/master || git fetch local) &&
883 git branch --no-track myr19 local/master &&
884 test "z$(git config branch.myr19.remote)" = z &&
885 test "z$(git config branch.myr19.merge)" = z &&
886 test "z$(git config branch.myr19.rebase)" = z
889 test_expect_success 'autosetuprebase always on an untracked remote branch' '
890 git config branch.autosetuprebase always &&
891 git config remote.local.url . &&
892 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
893 (git show-ref -q refs/remotes/local/master || git fetch local) &&
894 git branch --no-track myr20 local/master &&
895 test "z$(git config branch.myr20.remote)" = z &&
896 test "z$(git config branch.myr20.merge)" = z &&
897 test "z$(git config branch.myr20.rebase)" = z
900 test_expect_success 'autosetuprebase always on detached HEAD' '
901 git config branch.autosetupmerge always &&
902 test_when_finished git checkout master &&
903 git checkout HEAD^0 &&
905 test -z "$(git config branch.my11.remote)" &&
906 test -z "$(git config branch.my11.merge)"
909 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
910 git config branch.autosetuprebase garbage &&
911 test_must_fail git branch
914 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
915 git config --unset branch.autosetuprebase &&
916 echo "[branch] autosetuprebase" >>.git/config &&
917 test_must_fail git branch &&
918 git config --unset branch.autosetuprebase
921 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
923 git config --unset branch.my8.merge &&
924 test_must_fail git branch -d my8
927 test_expect_success 'attempt to delete a branch merged to its base' '
928 # we are on my9 which is the initial commit; traditionally
929 # we would not have allowed deleting my8 that is not merged
930 # to my9, but it is set to track master that already has my8
931 git config branch.my8.merge refs/heads/master &&
935 test_expect_success 'attempt to delete a branch merged to its base' '
936 git checkout master &&
938 git commit -m "Third commit" A &&
939 git branch -t my10 my9 &&
940 git branch -f my10 HEAD^ &&
941 # we are on master which is at the third commit, and my10
942 # is behind us, so traditionally we would have allowed deleting
943 # it; but my10 is set to track my9 that is further behind.
944 test_must_fail git branch -d my10
947 test_expect_success 'use --edit-description' '
948 write_script editor <<-\EOF &&
949 echo "New contents" >"$1"
951 EDITOR=./editor git branch --edit-description &&
952 write_script editor <<-\EOF &&
953 git stripspace -s <"$1" >"EDITOR_OUTPUT"
955 EDITOR=./editor git branch --edit-description &&
956 echo "New contents" >expect &&
957 test_cmp EDITOR_OUTPUT expect
960 test_expect_success 'detect typo in branch name when using --edit-description' '
961 write_script editor <<-\EOF &&
962 echo "New contents" >"$1"
964 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
967 test_expect_success 'refuse --edit-description on unborn branch for now' '
968 write_script editor <<-\EOF &&
969 echo "New contents" >"$1"
971 git checkout --orphan unborn &&
972 test_must_fail env EDITOR=./editor git branch --edit-description
975 test_expect_success '--merged catches invalid object names' '
976 test_must_fail git branch --merged 0000000000000000000000000000000000000000
979 test_expect_success '--merged is incompatible with --no-merged' '
980 test_must_fail git branch --merged HEAD --no-merged HEAD
983 test_expect_success 'tracking with unexpected .fetch refspec' '
999 git remote add a ../a &&
1000 git remote add b ../b &&
1006 git remote add c ../c &&
1007 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1009 git branch --track local/a/master remotes/a/master &&
1010 test "$(git config branch.local/a/master.remote)" = "c" &&
1011 test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
1012 git rev-parse --verify a >expect &&
1013 git rev-parse --verify local/a/master >actual &&
1014 test_cmp expect actual