Merge branch 'rs/cocci-de-paren-call-params'
[git/debian.git] / t / t3200-branch.sh
blob3ac7ebf85f3198cde56f784456e0024205aec853
1 #!/bin/sh
3 # Copyright (c) 2005 Amos Waterland
6 test_description='git branch assorted tests'
8 . ./test-lib.sh
10 test_expect_success 'prepare a trivial repository' '
11 echo Hello >A &&
12 git update-index --add A &&
13 git commit -m "Initial commit." &&
14 echo World >>A &&
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' '
26 mkdir broken &&
28 cd broken &&
29 git init &&
30 >.git/refs/heads/master &&
31 test_expect_code 129 git branch -h >usage 2>&1
32 ) &&
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
48 cat >expect <<EOF
49 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
50 EOF
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' '
66 git branch j &&
67 git branch -d j &&
68 git branch j/k
71 test_expect_success 'git branch l should work after branch l/m has been deleted' '
72 git branch l/m &&
73 git branch -d l/m &&
74 git branch l
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" &&
84 git branch -l m &&
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' '
92 git branch -l m &&
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' '
98 git branch -l n/n &&
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 &&
112 git branch -m bbb &&
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' '
121 git branch o/o &&
122 git branch o/p &&
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' '
127 git branch o/q &&
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' '
136 git branch o/q &&
137 git branch -m -f o/q o/p
140 test_expect_success 'git branch -m q r/q should fail when r exists' '
141 git branch q &&
142 git branch r &&
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' '
147 git branch bar &&
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 &&
154 git branch bam &&
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' '
166 git init orphan &&
168 cd orphan &&
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 &&
181 HEAD@{0} $oid $msg
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 &&
201 cd bazdir3 &&
202 git branch -M baz bam &&
203 test $(git rev-parse --abbrev-ref HEAD) = bam
204 ) &&
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 &&
210 git branch -M 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' '
225 git branch t &&
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' '
232 git branch t &&
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 &&
237 git branch -d s
240 test_expect_success 'git branch -m -d t s should fail' '
241 git branch t &&
242 test_path_is_file .git/refs/heads/t &&
243 test_must_fail git branch -m -d t s &&
244 git branch -d t &&
245 test_path_is_missing .git/refs/heads/t
248 test_expect_success 'git branch --list -d t should fail' '
249 git branch t &&
250 test_path_is_file .git/refs/heads/t &&
251 test_must_fail git branch --list -d t &&
252 git branch -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" &&
258 git branch 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" &&
294 git branch $long &&
295 COLUMNS=80 git branch --column=column >actual &&
296 cat >expected <<EOF &&
297 a/b/c
305 * master
306 master2
312 $long
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 &&
339 a/b/c
347 * master
348 master2
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 &&
362 git branch -m q2 q
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' '
370 git branch -l s/s &&
371 git reflog exists refs/heads/s/s &&
372 git branch -l s/t &&
373 git reflog exists refs/heads/s/t &&
374 git branch -d 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 'git branch -m correctly renames multiple config sections' '
385 test_when_finished "git checkout master" &&
386 git checkout -b source master &&
388 # Assert that a config file with multiple config sections has
389 # those sections preserved...
390 cat >expect <<-\EOF &&
391 branch.dest.key1=value1
392 some.gar.b=age
393 branch.dest.key2=value2
395 cat >config.branch <<\EOF &&
396 ;; Note the lack of -\EOF above & mixed indenting here. This is
397 ;; intentional, we are also testing that the formatting of copied
398 ;; sections is preserved.
400 ;; Comment for source. Tabs
401 [branch "source"]
402 ;; Comment for the source value
403 key1 = value1
404 ;; Comment for some.gar. Spaces
405 [some "gar"]
406 ;; Comment for the some.gar value
407 b = age
408 ;; Comment for source, again. Mixed tabs/spaces.
409 [branch "source"]
410 ;; Comment for the source value, again
411 key2 = value2
413 cat config.branch >>.git/config &&
414 git branch -m source dest &&
415 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
416 test_cmp expect actual &&
418 # ...and that the comments for those sections are also
419 # preserved.
420 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
421 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
422 test_cmp expect actual
425 test_expect_success 'git branch -c dumps usage' '
426 test_expect_code 128 git branch -c 2>err &&
427 test_i18ngrep "branch name required" err
430 test_expect_success 'git branch --copy dumps usage' '
431 test_expect_code 128 git branch --copy 2>err &&
432 test_i18ngrep "branch name required" err
435 test_expect_success 'git branch -c d e should work' '
436 git branch -l d &&
437 git reflog exists refs/heads/d &&
438 git config branch.d.dummy Hello &&
439 git branch -c d e &&
440 git reflog exists refs/heads/d &&
441 git reflog exists refs/heads/e &&
442 echo Hello >expect &&
443 git config branch.e.dummy >actual &&
444 test_cmp expect actual &&
445 echo Hello >expect &&
446 git config branch.d.dummy >actual &&
447 test_cmp expect actual
450 test_expect_success 'git branch --copy is a synonym for -c' '
451 git branch -l copy &&
452 git reflog exists refs/heads/copy &&
453 git config branch.copy.dummy Hello &&
454 git branch --copy copy copy-to &&
455 git reflog exists refs/heads/copy &&
456 git reflog exists refs/heads/copy-to &&
457 echo Hello >expect &&
458 git config branch.copy.dummy >actual &&
459 test_cmp expect actual &&
460 echo Hello >expect &&
461 git config branch.copy-to.dummy >actual &&
462 test_cmp expect actual
465 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
466 git checkout -b ee &&
467 git reflog exists refs/heads/ee &&
468 git config branch.ee.dummy Hello &&
469 git branch -c ee ef &&
470 git reflog exists refs/heads/ee &&
471 git reflog exists refs/heads/ef &&
472 test $(git config branch.ee.dummy) = Hello &&
473 test $(git config branch.ef.dummy) = Hello &&
474 test $(git rev-parse --abbrev-ref HEAD) = ee
477 test_expect_success 'git branch -c f/f g/g should work' '
478 git branch -l f/f &&
479 git reflog exists refs/heads/f/f &&
480 git config branch.f/f.dummy Hello &&
481 git branch -c f/f g/g &&
482 git reflog exists refs/heads/f/f &&
483 git reflog exists refs/heads/g/g &&
484 test $(git config branch.f/f.dummy) = Hello &&
485 test $(git config branch.g/g.dummy) = Hello
488 test_expect_success 'git branch -c m2 m2 should work' '
489 git branch -l m2 &&
490 git reflog exists refs/heads/m2 &&
491 git config branch.m2.dummy Hello &&
492 git branch -c m2 m2 &&
493 git reflog exists refs/heads/m2 &&
494 test $(git config branch.m2.dummy) = Hello
497 test_expect_success 'git branch -c zz zz/zz should fail' '
498 git branch -l zz &&
499 git reflog exists refs/heads/zz &&
500 test_must_fail git branch -c zz zz/zz
503 test_expect_success 'git branch -c b/b b should fail' '
504 git branch -l b/b &&
505 test_must_fail git branch -c b/b b
508 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
509 git branch -l o/q &&
510 git reflog exists refs/heads/o/q &&
511 git reflog exists refs/heads/o/p &&
512 git branch -C o/q o/p
515 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
516 git reflog exists refs/heads/o/q &&
517 git reflog exists refs/heads/o/p &&
518 git branch -c -f o/q o/p
521 test_expect_success 'git branch -c qq rr/qq should fail when r exists' '
522 git branch qq &&
523 git branch rr &&
524 test_must_fail git branch -c qq rr/qq
527 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
528 git branch b1 &&
529 git checkout -b b2 &&
530 test_must_fail git branch -C b1 b2
533 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
534 git checkout -b c1 &&
535 git branch c2 &&
536 git branch -C c1 c2 &&
537 test $(git rev-parse --abbrev-ref HEAD) = c1
540 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
541 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
542 ! grep "$msg$" .git/logs/HEAD
545 test_expect_success 'git branch -C master should work when master is checked out' '
546 git checkout master &&
547 git branch -C master
550 test_expect_success 'git branch -C master master should work when master is checked out' '
551 git checkout master &&
552 git branch -C master master
555 test_expect_success 'git branch -C master5 master5 should work when master is checked out' '
556 git checkout master &&
557 git branch master5 &&
558 git branch -C master5 master5
561 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
562 git branch -l cd &&
563 git reflog exists refs/heads/cd &&
564 git config branch.cd.dummy CD &&
565 git branch -l ab &&
566 git reflog exists refs/heads/ab &&
567 git config branch.ab.dummy AB &&
568 git branch -C ab cd &&
569 git reflog exists refs/heads/ab &&
570 git reflog exists refs/heads/cd &&
571 test $(git config branch.ab.dummy) = AB &&
572 test $(git config branch.cd.dummy) = AB
575 test_expect_success 'git branch -c correctly copies multiple config sections' '
576 FOO=1 &&
577 export FOO &&
578 test_when_finished "git checkout master" &&
579 git checkout -b source2 master &&
581 # Assert that a config file with multiple config sections has
582 # those sections preserved...
583 cat >expect <<-\EOF &&
584 branch.source2.key1=value1
585 branch.dest2.key1=value1
586 more.gar.b=age
587 branch.source2.key2=value2
588 branch.dest2.key2=value2
590 cat >config.branch <<\EOF &&
591 ;; Note the lack of -\EOF above & mixed indenting here. This is
592 ;; intentional, we are also testing that the formatting of copied
593 ;; sections is preserved.
595 ;; Comment for source2. Tabs
596 [branch "source2"]
597 ;; Comment for the source2 value
598 key1 = value1
599 ;; Comment for more.gar. Spaces
600 [more "gar"]
601 ;; Comment for the more.gar value
602 b = age
603 ;; Comment for source2, again. Mixed tabs/spaces.
604 [branch "source2"]
605 ;; Comment for the source2 value, again
606 key2 = value2
608 cat config.branch >>.git/config &&
609 git branch -c source2 dest2 &&
610 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
611 test_cmp expect actual &&
613 # ...and that the comments and formatting for those sections
614 # is also preserved.
615 cat >expect <<\EOF &&
616 ;; Comment for source2. Tabs
617 [branch "source2"]
618 ;; Comment for the source2 value
619 key1 = value1
620 ;; Comment for more.gar. Spaces
621 [branch "dest2"]
622 ;; Comment for the source2 value
623 key1 = value1
624 ;; Comment for more.gar. Spaces
625 [more "gar"]
626 ;; Comment for the more.gar value
627 b = age
628 ;; Comment for source2, again. Mixed tabs/spaces.
629 [branch "source2"]
630 ;; Comment for the source2 value, again
631 key2 = value2
632 [branch "dest2"]
633 ;; Comment for the source2 value, again
634 key2 = value2
636 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
637 test_cmp expect actual
640 test_expect_success 'deleting a symref' '
641 git branch target &&
642 git symbolic-ref refs/heads/symref refs/heads/target &&
643 echo "Deleted branch symref (was refs/heads/target)." >expect &&
644 git branch -d symref >actual &&
645 test_path_is_file .git/refs/heads/target &&
646 test_path_is_missing .git/refs/heads/symref &&
647 test_i18ncmp expect actual
650 test_expect_success 'deleting a dangling symref' '
651 git symbolic-ref refs/heads/dangling-symref nowhere &&
652 test_path_is_file .git/refs/heads/dangling-symref &&
653 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
654 git branch -d dangling-symref >actual &&
655 test_path_is_missing .git/refs/heads/dangling-symref &&
656 test_i18ncmp expect actual
659 test_expect_success 'deleting a self-referential symref' '
660 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
661 test_path_is_file .git/refs/heads/self-reference &&
662 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
663 git branch -d self-reference >actual &&
664 test_path_is_missing .git/refs/heads/self-reference &&
665 test_i18ncmp expect actual
668 test_expect_success 'renaming a symref is not allowed' '
669 git symbolic-ref refs/heads/master2 refs/heads/master &&
670 test_must_fail git branch -m master2 master3 &&
671 git symbolic-ref refs/heads/master2 &&
672 test_path_is_file .git/refs/heads/master &&
673 test_path_is_missing .git/refs/heads/master3
676 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
677 git branch -l u &&
678 mv .git/logs/refs/heads/u real-u &&
679 ln -s real-u .git/logs/refs/heads/u &&
680 test_must_fail git branch -m u v
683 test_expect_success 'test tracking setup via --track' '
684 git config remote.local.url . &&
685 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
686 (git show-ref -q refs/remotes/local/master || git fetch local) &&
687 git branch --track my1 local/master &&
688 test $(git config branch.my1.remote) = local &&
689 test $(git config branch.my1.merge) = refs/heads/master
692 test_expect_success 'test tracking setup (non-wildcard, matching)' '
693 git config remote.local.url . &&
694 git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
695 (git show-ref -q refs/remotes/local/master || git fetch local) &&
696 git branch --track my4 local/master &&
697 test $(git config branch.my4.remote) = local &&
698 test $(git config branch.my4.merge) = refs/heads/master
701 test_expect_success 'tracking setup fails on non-matching refspec' '
702 git config remote.local.url . &&
703 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
704 (git show-ref -q refs/remotes/local/master || git fetch local) &&
705 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
706 test_must_fail git branch --track my5 local/master &&
707 test_must_fail git config branch.my5.remote &&
708 test_must_fail git config branch.my5.merge
711 test_expect_success 'test tracking setup via config' '
712 git config branch.autosetupmerge true &&
713 git config remote.local.url . &&
714 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
715 (git show-ref -q refs/remotes/local/master || git fetch local) &&
716 git branch my3 local/master &&
717 test $(git config branch.my3.remote) = local &&
718 test $(git config branch.my3.merge) = refs/heads/master
721 test_expect_success 'test overriding tracking setup via --no-track' '
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 --no-track my2 local/master &&
727 git config branch.autosetupmerge false &&
728 ! test "$(git config branch.my2.remote)" = local &&
729 ! test "$(git config branch.my2.merge)" = refs/heads/master
732 test_expect_success 'no tracking without .fetch entries' '
733 git config branch.autosetupmerge true &&
734 git branch my6 s &&
735 git config branch.autosetupmerge false &&
736 test -z "$(git config branch.my6.remote)" &&
737 test -z "$(git config branch.my6.merge)"
740 test_expect_success 'test tracking setup via --track but deeper' '
741 git config remote.local.url . &&
742 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
743 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
744 git branch --track my7 local/o/o &&
745 test "$(git config branch.my7.remote)" = local &&
746 test "$(git config branch.my7.merge)" = refs/heads/o/o
749 test_expect_success 'test deleting branch deletes branch config' '
750 git branch -d my7 &&
751 test -z "$(git config branch.my7.remote)" &&
752 test -z "$(git config branch.my7.merge)"
755 test_expect_success 'test deleting branch without config' '
756 git branch my7 s &&
757 sha1=$(git rev-parse my7 | cut -c 1-7) &&
758 echo "Deleted branch my7 (was $sha1)." >expect &&
759 git branch -d my7 >actual 2>&1 &&
760 test_i18ncmp expect actual
763 test_expect_success 'deleting currently checked out branch fails' '
764 git worktree add -b my7 my7 &&
765 test_must_fail git -C my7 branch -d my7 &&
766 test_must_fail git branch -d my7
769 test_expect_success 'test --track without .fetch entries' '
770 git branch --track my8 &&
771 test "$(git config branch.my8.remote)" &&
772 test "$(git config branch.my8.merge)"
775 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
776 git config branch.autosetupmerge always &&
777 git branch my9 HEAD^ &&
778 git config branch.autosetupmerge false
781 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
782 test_must_fail git branch --track my10 HEAD^
785 test_expect_success 'branch from tag w/--track causes failure' '
786 git tag foobar &&
787 test_must_fail git branch --track my11 foobar
790 test_expect_success '--set-upstream-to fails on multiple branches' '
791 test_must_fail git branch --set-upstream-to master a b c
794 test_expect_success '--set-upstream-to fails on detached HEAD' '
795 git checkout HEAD^{} &&
796 test_must_fail git branch --set-upstream-to master &&
797 git checkout -
800 test_expect_success '--set-upstream-to fails on a missing dst branch' '
801 test_must_fail git branch --set-upstream-to master does-not-exist
804 test_expect_success '--set-upstream-to fails on a missing src branch' '
805 test_must_fail git branch --set-upstream-to does-not-exist master
808 test_expect_success '--set-upstream-to fails on a non-ref' '
809 test_must_fail git branch --set-upstream-to HEAD^{}
812 test_expect_success '--set-upstream-to fails on locked config' '
813 test_when_finished "rm -f .git/config.lock" &&
814 >.git/config.lock &&
815 git branch locked &&
816 test_must_fail git branch --set-upstream-to locked
819 test_expect_success 'use --set-upstream-to modify HEAD' '
820 test_config branch.master.remote foo &&
821 test_config branch.master.merge foo &&
822 git branch my12 &&
823 git branch --set-upstream-to my12 &&
824 test "$(git config branch.master.remote)" = "." &&
825 test "$(git config branch.master.merge)" = "refs/heads/my12"
828 test_expect_success 'use --set-upstream-to modify a particular branch' '
829 git branch my13 &&
830 git branch --set-upstream-to master my13 &&
831 test_when_finished "git branch --unset-upstream my13" &&
832 test "$(git config branch.my13.remote)" = "." &&
833 test "$(git config branch.my13.merge)" = "refs/heads/master"
836 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
837 test_must_fail git branch --unset-upstream i-dont-exist
840 test_expect_success '--unset-upstream should fail if config is locked' '
841 test_when_finished "rm -f .git/config.lock" &&
842 git branch --set-upstream-to locked &&
843 >.git/config.lock &&
844 test_must_fail git branch --unset-upstream
847 test_expect_success 'test --unset-upstream on HEAD' '
848 git branch my14 &&
849 test_config branch.master.remote foo &&
850 test_config branch.master.merge foo &&
851 git branch --set-upstream-to my14 &&
852 git branch --unset-upstream &&
853 test_must_fail git config branch.master.remote &&
854 test_must_fail git config branch.master.merge &&
855 # fail for a branch without upstream set
856 test_must_fail git branch --unset-upstream
859 test_expect_success '--unset-upstream should fail on multiple branches' '
860 test_must_fail git branch --unset-upstream a b c
863 test_expect_success '--unset-upstream should fail on detached HEAD' '
864 git checkout HEAD^{} &&
865 test_must_fail git branch --unset-upstream &&
866 git checkout -
869 test_expect_success 'test --unset-upstream on a particular branch' '
870 git branch my15 &&
871 git branch --set-upstream-to master my14 &&
872 git branch --unset-upstream my14 &&
873 test_must_fail git config branch.my14.remote &&
874 test_must_fail git config branch.my14.merge
877 test_expect_success '--set-upstream fails' '
878 test_must_fail git branch --set-upstream origin/master
881 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
882 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
883 cat >expected <<-\EOF &&
884 warning: Not setting branch my13 as its own upstream.
886 test_expect_code 1 git config branch.my13.remote &&
887 test_expect_code 1 git config branch.my13.merge &&
888 test_i18ncmp expected actual
891 # Keep this test last, as it changes the current branch
892 cat >expect <<EOF
893 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
895 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
896 GIT_COMMITTER_DATE="2005-05-26 23:30" \
897 git checkout -b g/h/i -l master &&
898 test_path_is_file .git/refs/heads/g/h/i &&
899 test_path_is_file .git/logs/refs/heads/g/h/i &&
900 test_cmp expect .git/logs/refs/heads/g/h/i
903 test_expect_success 'checkout -b makes reflog by default' '
904 git checkout master &&
905 git config --unset core.logAllRefUpdates &&
906 git checkout -b alpha &&
907 git rev-parse --verify alpha@{0}
910 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
911 git checkout master &&
912 git config core.logAllRefUpdates false &&
913 git checkout -b beta &&
914 test_must_fail git rev-parse --verify beta@{0}
917 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
918 git checkout master &&
919 git checkout -lb gamma &&
920 git config --unset core.logAllRefUpdates &&
921 git rev-parse --verify gamma@{0}
924 test_expect_success 'avoid ambiguous track' '
925 git config branch.autosetupmerge true &&
926 git config remote.ambi1.url lalala &&
927 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
928 git config remote.ambi2.url lilili &&
929 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
930 test_must_fail git branch all1 master &&
931 test -z "$(git config branch.all1.merge)"
934 test_expect_success 'autosetuprebase local on a tracked local branch' '
935 git config remote.local.url . &&
936 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
937 git config branch.autosetuprebase local &&
938 (git show-ref -q refs/remotes/local/o || git fetch local) &&
939 git branch mybase &&
940 git branch --track myr1 mybase &&
941 test "$(git config branch.myr1.remote)" = . &&
942 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
943 test "$(git config branch.myr1.rebase)" = true
946 test_expect_success 'autosetuprebase always on a tracked local branch' '
947 git config remote.local.url . &&
948 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
949 git config branch.autosetuprebase always &&
950 (git show-ref -q refs/remotes/local/o || git fetch local) &&
951 git branch mybase2 &&
952 git branch --track myr2 mybase &&
953 test "$(git config branch.myr2.remote)" = . &&
954 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
955 test "$(git config branch.myr2.rebase)" = true
958 test_expect_success 'autosetuprebase remote on a tracked local branch' '
959 git config remote.local.url . &&
960 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
961 git config branch.autosetuprebase remote &&
962 (git show-ref -q refs/remotes/local/o || git fetch local) &&
963 git branch mybase3 &&
964 git branch --track myr3 mybase2 &&
965 test "$(git config branch.myr3.remote)" = . &&
966 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
967 ! test "$(git config branch.myr3.rebase)" = true
970 test_expect_success 'autosetuprebase never on a tracked local branch' '
971 git config remote.local.url . &&
972 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
973 git config branch.autosetuprebase never &&
974 (git show-ref -q refs/remotes/local/o || git fetch local) &&
975 git branch mybase4 &&
976 git branch --track myr4 mybase2 &&
977 test "$(git config branch.myr4.remote)" = . &&
978 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
979 ! test "$(git config branch.myr4.rebase)" = true
982 test_expect_success 'autosetuprebase local on a tracked remote branch' '
983 git config remote.local.url . &&
984 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
985 git config branch.autosetuprebase local &&
986 (git show-ref -q refs/remotes/local/master || git fetch local) &&
987 git branch --track myr5 local/master &&
988 test "$(git config branch.myr5.remote)" = local &&
989 test "$(git config branch.myr5.merge)" = refs/heads/master &&
990 ! test "$(git config branch.myr5.rebase)" = true
993 test_expect_success 'autosetuprebase never on a tracked remote branch' '
994 git config remote.local.url . &&
995 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
996 git config branch.autosetuprebase never &&
997 (git show-ref -q refs/remotes/local/master || git fetch local) &&
998 git branch --track myr6 local/master &&
999 test "$(git config branch.myr6.remote)" = local &&
1000 test "$(git config branch.myr6.merge)" = refs/heads/master &&
1001 ! test "$(git config branch.myr6.rebase)" = true
1004 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1005 git config remote.local.url . &&
1006 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1007 git config branch.autosetuprebase remote &&
1008 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1009 git branch --track myr7 local/master &&
1010 test "$(git config branch.myr7.remote)" = local &&
1011 test "$(git config branch.myr7.merge)" = refs/heads/master &&
1012 test "$(git config branch.myr7.rebase)" = true
1015 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1016 git config remote.local.url . &&
1017 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1018 git config branch.autosetuprebase remote &&
1019 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1020 git branch --track myr8 local/master &&
1021 test "$(git config branch.myr8.remote)" = local &&
1022 test "$(git config branch.myr8.merge)" = refs/heads/master &&
1023 test "$(git config branch.myr8.rebase)" = true
1026 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1027 git config --unset branch.autosetuprebase &&
1028 git config remote.local.url . &&
1029 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1030 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1031 git branch --track myr9 local/master &&
1032 test "$(git config branch.myr9.remote)" = local &&
1033 test "$(git config branch.myr9.merge)" = refs/heads/master &&
1034 test "z$(git config branch.myr9.rebase)" = z
1037 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
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/o || git fetch local) &&
1041 git branch mybase10 &&
1042 git branch --track myr10 mybase2 &&
1043 test "$(git config branch.myr10.remote)" = . &&
1044 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1045 test "z$(git config branch.myr10.rebase)" = z
1048 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1049 git config remote.local.url . &&
1050 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1051 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1052 git branch --no-track myr11 mybase2 &&
1053 test "z$(git config branch.myr11.remote)" = z &&
1054 test "z$(git config branch.myr11.merge)" = z &&
1055 test "z$(git config branch.myr11.rebase)" = z
1058 test_expect_success 'autosetuprebase unconfigured on untracked remote 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 myr12 local/master &&
1063 test "z$(git config branch.myr12.remote)" = z &&
1064 test "z$(git config branch.myr12.merge)" = z &&
1065 test "z$(git config branch.myr12.rebase)" = z
1068 test_expect_success 'autosetuprebase never on an untracked local branch' '
1069 git config branch.autosetuprebase never &&
1070 git config remote.local.url . &&
1071 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1072 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1073 git branch --no-track myr13 mybase2 &&
1074 test "z$(git config branch.myr13.remote)" = z &&
1075 test "z$(git config branch.myr13.merge)" = z &&
1076 test "z$(git config branch.myr13.rebase)" = z
1079 test_expect_success 'autosetuprebase local on an untracked local branch' '
1080 git config branch.autosetuprebase local &&
1081 git config remote.local.url . &&
1082 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1083 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1084 git branch --no-track myr14 mybase2 &&
1085 test "z$(git config branch.myr14.remote)" = z &&
1086 test "z$(git config branch.myr14.merge)" = z &&
1087 test "z$(git config branch.myr14.rebase)" = z
1090 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1091 git config branch.autosetuprebase remote &&
1092 git config remote.local.url . &&
1093 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1094 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1095 git branch --no-track myr15 mybase2 &&
1096 test "z$(git config branch.myr15.remote)" = z &&
1097 test "z$(git config branch.myr15.merge)" = z &&
1098 test "z$(git config branch.myr15.rebase)" = z
1101 test_expect_success 'autosetuprebase always on an untracked local branch' '
1102 git config branch.autosetuprebase always &&
1103 git config remote.local.url . &&
1104 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1105 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1106 git branch --no-track myr16 mybase2 &&
1107 test "z$(git config branch.myr16.remote)" = z &&
1108 test "z$(git config branch.myr16.merge)" = z &&
1109 test "z$(git config branch.myr16.rebase)" = z
1112 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1113 git config branch.autosetuprebase never &&
1114 git config remote.local.url . &&
1115 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1116 (git show-ref -q refs/remotes/local/master || git fetch local) &&
1117 git branch --no-track myr17 local/master &&
1118 test "z$(git config branch.myr17.remote)" = z &&
1119 test "z$(git config branch.myr17.merge)" = z &&
1120 test "z$(git config branch.myr17.rebase)" = z
1123 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1124 git config branch.autosetuprebase local &&
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 myr18 local/master &&
1129 test "z$(git config branch.myr18.remote)" = z &&
1130 test "z$(git config branch.myr18.merge)" = z &&
1131 test "z$(git config branch.myr18.rebase)" = z
1134 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1135 git config branch.autosetuprebase remote &&
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 myr19 local/master &&
1140 test "z$(git config branch.myr19.remote)" = z &&
1141 test "z$(git config branch.myr19.merge)" = z &&
1142 test "z$(git config branch.myr19.rebase)" = z
1145 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1146 git config branch.autosetuprebase always &&
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 myr20 local/master &&
1151 test "z$(git config branch.myr20.remote)" = z &&
1152 test "z$(git config branch.myr20.merge)" = z &&
1153 test "z$(git config branch.myr20.rebase)" = z
1156 test_expect_success 'autosetuprebase always on detached HEAD' '
1157 git config branch.autosetupmerge always &&
1158 test_when_finished git checkout master &&
1159 git checkout HEAD^0 &&
1160 git branch my11 &&
1161 test -z "$(git config branch.my11.remote)" &&
1162 test -z "$(git config branch.my11.merge)"
1165 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1166 git config branch.autosetuprebase garbage &&
1167 test_must_fail git branch
1170 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1171 git config --unset branch.autosetuprebase &&
1172 echo "[branch] autosetuprebase" >>.git/config &&
1173 test_must_fail git branch &&
1174 git config --unset branch.autosetuprebase
1177 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1178 git checkout my9 &&
1179 git config --unset branch.my8.merge &&
1180 test_must_fail git branch -d my8
1183 test_expect_success 'attempt to delete a branch merged to its base' '
1184 # we are on my9 which is the initial commit; traditionally
1185 # we would not have allowed deleting my8 that is not merged
1186 # to my9, but it is set to track master that already has my8
1187 git config branch.my8.merge refs/heads/master &&
1188 git branch -d my8
1191 test_expect_success 'attempt to delete a branch merged to its base' '
1192 git checkout master &&
1193 echo Third >>A &&
1194 git commit -m "Third commit" A &&
1195 git branch -t my10 my9 &&
1196 git branch -f my10 HEAD^ &&
1197 # we are on master which is at the third commit, and my10
1198 # is behind us, so traditionally we would have allowed deleting
1199 # it; but my10 is set to track my9 that is further behind.
1200 test_must_fail git branch -d my10
1203 test_expect_success 'use --edit-description' '
1204 write_script editor <<-\EOF &&
1205 echo "New contents" >"$1"
1207 EDITOR=./editor git branch --edit-description &&
1208 write_script editor <<-\EOF &&
1209 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1211 EDITOR=./editor git branch --edit-description &&
1212 echo "New contents" >expect &&
1213 test_cmp EDITOR_OUTPUT expect
1216 test_expect_success 'detect typo in branch name when using --edit-description' '
1217 write_script editor <<-\EOF &&
1218 echo "New contents" >"$1"
1220 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1223 test_expect_success 'refuse --edit-description on unborn branch for now' '
1224 write_script editor <<-\EOF &&
1225 echo "New contents" >"$1"
1227 git checkout --orphan unborn &&
1228 test_must_fail env EDITOR=./editor git branch --edit-description
1231 test_expect_success '--merged catches invalid object names' '
1232 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1235 test_expect_success '--merged is incompatible with --no-merged' '
1236 test_must_fail git branch --merged HEAD --no-merged HEAD
1239 test_expect_success 'tracking with unexpected .fetch refspec' '
1240 rm -rf a b c d &&
1241 git init a &&
1243 cd a &&
1244 test_commit a
1245 ) &&
1246 git init b &&
1248 cd b &&
1249 test_commit b
1250 ) &&
1251 git init c &&
1253 cd c &&
1254 test_commit c &&
1255 git remote add a ../a &&
1256 git remote add b ../b &&
1257 git fetch --all
1258 ) &&
1259 git init d &&
1261 cd d &&
1262 git remote add c ../c &&
1263 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1264 git fetch c &&
1265 git branch --track local/a/master remotes/a/master &&
1266 test "$(git config branch.local/a/master.remote)" = "c" &&
1267 test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
1268 git rev-parse --verify a >expect &&
1269 git rev-parse --verify local/a/master >actual &&
1270 test_cmp expect actual
1274 test_done