rebase: use correct base for --keep-base when a branch is given
[alt-git.git] / t / t3200-branch.sh
blobe12db5936155453ea86a9b84ce0321e588d955b4
1 #!/bin/sh
3 # Copyright (c) 2005 Amos Waterland
6 test_description='git branch assorted tests'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-rebase.sh
14 test_expect_success 'prepare a trivial repository' '
15 echo Hello >A &&
16 git update-index --add A &&
17 git commit -m "Initial commit." &&
18 git branch -M main &&
19 echo World >>A &&
20 git update-index --add A &&
21 git commit -m "Second commit." &&
22 HEAD=$(git rev-parse --verify HEAD)
25 test_expect_success 'git branch --help should not have created a bogus branch' '
26 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
27 test_path_is_missing .git/refs/heads/--help
30 test_expect_success 'branch -h in broken repository' '
31 mkdir broken &&
33 cd broken &&
34 git init -b main &&
35 >.git/refs/heads/main &&
36 test_expect_code 129 git branch -h >usage 2>&1
37 ) &&
38 test_i18ngrep "[Uu]sage" broken/usage
41 test_expect_success 'git branch abc should create a branch' '
42 git branch abc && test_path_is_file .git/refs/heads/abc
45 test_expect_success 'git branch abc should fail when abc exists' '
46 test_must_fail git branch abc
49 test_expect_success 'git branch --force abc should fail when abc is checked out' '
50 test_when_finished git switch main &&
51 git switch abc &&
52 test_must_fail git branch --force abc HEAD~1
55 test_expect_success 'git branch --force abc should succeed when abc exists' '
56 git rev-parse HEAD~1 >expect &&
57 git branch --force abc HEAD~1 &&
58 git rev-parse abc >actual &&
59 test_cmp expect actual
62 test_expect_success 'git branch a/b/c should create a branch' '
63 git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
66 test_expect_success 'git branch mb main... should create a branch' '
67 git branch mb main... && test_path_is_file .git/refs/heads/mb
70 test_expect_success 'git branch HEAD should fail' '
71 test_must_fail git branch HEAD
74 cat >expect <<EOF
75 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
76 EOF
77 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
78 GIT_COMMITTER_DATE="2005-05-26 23:30" \
79 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
80 test_path_is_file .git/refs/heads/d/e/f &&
81 test_path_is_file .git/logs/refs/heads/d/e/f &&
82 test_cmp expect .git/logs/refs/heads/d/e/f
85 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
86 git branch -d d/e/f &&
87 test_path_is_missing .git/refs/heads/d/e/f &&
88 test_must_fail git reflog exists refs/heads/d/e/f
91 test_expect_success 'git branch j/k should work after branch j has been deleted' '
92 git branch j &&
93 git branch -d j &&
94 git branch j/k
97 test_expect_success 'git branch l should work after branch l/m has been deleted' '
98 git branch l/m &&
99 git branch -d l/m &&
100 git branch l
103 test_expect_success 'git branch -m dumps usage' '
104 test_expect_code 128 git branch -m 2>err &&
105 test_i18ngrep "branch name required" err
108 test_expect_success 'git branch -m m broken_symref should work' '
109 test_when_finished "git branch -D broken_symref" &&
110 git branch --create-reflog m &&
111 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
112 git branch -m m broken_symref &&
113 git reflog exists refs/heads/broken_symref &&
114 test_must_fail git reflog exists refs/heads/i_am_broken
117 test_expect_success 'git branch -m m m/m should work' '
118 git branch --create-reflog m &&
119 git branch -m m m/m &&
120 git reflog exists refs/heads/m/m
123 test_expect_success 'git branch -m n/n n should work' '
124 git branch --create-reflog n/n &&
125 git branch -m n/n n &&
126 git reflog exists refs/heads/n
129 # The topmost entry in reflog for branch bbb is about branch creation.
130 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
132 test_expect_success 'git branch -m bbb should rename checked out branch' '
133 test_when_finished git branch -D bbb &&
134 test_when_finished git checkout main &&
135 git checkout -b aaa &&
136 git commit --allow-empty -m "a new commit" &&
137 git rev-parse aaa@{0} >expect &&
138 git branch -m bbb &&
139 git rev-parse bbb@{1} >actual &&
140 test_cmp expect actual &&
141 git symbolic-ref HEAD >actual &&
142 echo refs/heads/bbb >expect &&
143 test_cmp expect actual
146 test_expect_success 'renaming checked out branch works with d/f conflict' '
147 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
148 test_when_finished git checkout main &&
149 git checkout -b foo &&
150 git branch -m foo/bar &&
151 git symbolic-ref HEAD >actual &&
152 echo refs/heads/foo/bar >expect &&
153 test_cmp expect actual
156 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
157 git branch o/o &&
158 git branch o/p &&
159 test_must_fail git branch -m o/o o
162 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
163 git branch o/q &&
164 test_must_fail git branch -m o/q o/p
167 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
168 git branch -M o/q o/p
171 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
172 git branch o/q &&
173 git branch -m -f o/q o/p
176 test_expect_success 'git branch -m q r/q should fail when r exists' '
177 git branch q &&
178 git branch r &&
179 test_must_fail git branch -m q r/q
182 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
183 git branch bar &&
184 git checkout -b foo &&
185 test_must_fail git branch -M bar foo
188 test_expect_success 'git branch -M foo bar should fail when bar is checked out in worktree' '
189 git branch -f bar &&
190 test_when_finished "git worktree remove wt && git branch -D wt" &&
191 git worktree add wt &&
192 test_must_fail git branch -M bar wt
195 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
196 git checkout -b baz &&
197 git branch bam &&
198 git branch -M baz bam &&
199 test $(git rev-parse --abbrev-ref HEAD) = bam
202 test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
203 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
204 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
205 grep "^0\{40\}.*$msg$" .git/logs/HEAD
208 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
209 git init -b main orphan &&
211 cd orphan &&
212 test_commit initial &&
213 git checkout --orphan lonely &&
214 grep lonely .git/HEAD &&
215 test_path_is_missing .git/refs/head/lonely &&
216 git branch -M main mistress &&
217 grep lonely .git/HEAD
221 test_expect_success 'resulting reflog can be shown by log -g' '
222 oid=$(git rev-parse HEAD) &&
223 cat >expect <<-EOF &&
224 HEAD@{0} $oid $msg
225 HEAD@{2} $oid checkout: moving from foo to baz
227 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
228 test_cmp expect actual
231 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
232 git checkout main &&
233 git worktree add -b baz bazdir &&
234 git worktree add -f bazdir2 baz &&
235 git branch -M baz bam &&
236 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
237 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
238 rm -r bazdir bazdir2 &&
239 git worktree prune
242 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
243 git checkout -b baz &&
244 git worktree add -f bazdir baz &&
246 cd bazdir &&
247 git branch -M baz bam &&
248 test $(git rev-parse --abbrev-ref HEAD) = bam
249 ) &&
250 test $(git rev-parse --abbrev-ref HEAD) = bam &&
251 rm -r bazdir &&
252 git worktree prune
255 test_expect_success 'git branch -M main should work when main is checked out' '
256 git checkout main &&
257 git branch -M main
260 test_expect_success 'git branch -M main main should work when main is checked out' '
261 git checkout main &&
262 git branch -M main main
265 test_expect_success 'git branch -M topic topic should work when main is checked out' '
266 git checkout main &&
267 git branch topic &&
268 git branch -M topic topic
271 test_expect_success 'git branch -v -d t should work' '
272 git branch t &&
273 git rev-parse --verify refs/heads/t &&
274 git branch -v -d t &&
275 test_must_fail git rev-parse --verify refs/heads/t
278 test_expect_success 'git branch -v -m t s should work' '
279 git branch t &&
280 git rev-parse --verify refs/heads/t &&
281 git branch -v -m t s &&
282 test_must_fail git rev-parse --verify refs/heads/t &&
283 git rev-parse --verify refs/heads/s &&
284 git branch -d s
287 test_expect_success 'git branch -m -d t s should fail' '
288 git branch t &&
289 git rev-parse refs/heads/t &&
290 test_must_fail git branch -m -d t s &&
291 git branch -d t &&
292 test_must_fail git rev-parse refs/heads/t
295 test_expect_success 'git branch --list -d t should fail' '
296 git branch t &&
297 git rev-parse refs/heads/t &&
298 test_must_fail git branch --list -d t &&
299 git branch -d t &&
300 test_must_fail git rev-parse refs/heads/t
303 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
304 test_when_finished "rm -rf repo1 repo2" &&
306 git init repo1 &&
307 git init repo1/sub &&
308 test_commit -C repo1/sub x &&
309 git -C repo1 submodule add ./sub &&
310 git -C repo1 commit -m "adding sub" &&
312 git clone --recurse-submodules repo1 repo2 &&
313 git -C repo2/sub checkout -b work &&
314 test_must_fail git -C repo2/sub branch -D work
317 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
318 test_when_finished "rm -rf nonbare base secondary" &&
320 git init -b main nonbare &&
321 test_commit -C nonbare x &&
322 git clone --bare nonbare bare &&
323 git -C bare worktree add --detach ../secondary main &&
324 git -C secondary branch -D main
327 test_expect_success 'git branch --list -v with --abbrev' '
328 test_when_finished "git branch -D t" &&
329 git branch t &&
330 git branch -v --list t >actual.default &&
331 git branch -v --list --abbrev t >actual.abbrev &&
332 test_cmp actual.default actual.abbrev &&
334 git branch -v --list --no-abbrev t >actual.noabbrev &&
335 git branch -v --list --abbrev=0 t >actual.0abbrev &&
336 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
337 test_cmp actual.noabbrev actual.0abbrev &&
338 test_cmp actual.noabbrev actual.noabbrev-conf &&
340 git branch -v --list --abbrev=36 t >actual.36abbrev &&
341 # how many hexdigits are used?
342 read name objdefault rest <actual.abbrev &&
343 read name obj36 rest <actual.36abbrev &&
344 objfull=$(git rev-parse --verify t) &&
346 # are we really getting abbreviations?
347 test "$obj36" != "$objdefault" &&
348 expr "$obj36" : "$objdefault" >/dev/null &&
349 test "$objfull" != "$obj36" &&
350 expr "$objfull" : "$obj36" >/dev/null
354 test_expect_success 'git branch --column' '
355 COLUMNS=81 git branch --column=column >actual &&
356 cat >expect <<\EOF &&
357 a/b/c bam foo l * main n o/p r
358 abc bar j/k m/m mb o/o q topic
360 test_cmp expect actual
363 test_expect_success 'git branch --column with an extremely long branch name' '
364 long=this/is/a/part/of/long/branch/name &&
365 long=z$long/$long/$long/$long &&
366 test_when_finished "git branch -d $long" &&
367 git branch $long &&
368 COLUMNS=80 git branch --column=column >actual &&
369 cat >expect <<EOF &&
370 a/b/c
378 * main
385 topic
386 $long
388 test_cmp expect actual
391 test_expect_success 'git branch with column.*' '
392 git config column.ui column &&
393 git config column.branch "dense" &&
394 COLUMNS=80 git branch >actual &&
395 git config --unset column.branch &&
396 git config --unset column.ui &&
397 cat >expect <<\EOF &&
398 a/b/c bam foo l * main n o/p r
399 abc bar j/k m/m mb o/o q topic
401 test_cmp expect actual
404 test_expect_success 'git branch --column -v should fail' '
405 test_must_fail git branch --column -v
408 test_expect_success 'git branch -v with column.ui ignored' '
409 git config column.ui column &&
410 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
411 git config --unset column.ui &&
412 cat >expect <<\EOF &&
413 a/b/c
421 * main
428 topic
430 test_cmp expect actual
433 mv .git/config .git/config-saved
435 test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
436 git branch -m q q2 &&
437 git branch -m q2 q
440 mv .git/config-saved .git/config
442 git config branch.s/s.dummy Hello
444 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
445 git branch --create-reflog s/s &&
446 git reflog exists refs/heads/s/s &&
447 git branch --create-reflog s/t &&
448 git reflog exists refs/heads/s/t &&
449 git branch -d s/t &&
450 git branch -m s/s s &&
451 git reflog exists refs/heads/s
454 test_expect_success 'config information was renamed, too' '
455 test $(git config branch.s.dummy) = Hello &&
456 test_must_fail git config branch.s/s.dummy
459 test_expect_success 'git branch -m correctly renames multiple config sections' '
460 test_when_finished "git checkout main" &&
461 git checkout -b source main &&
463 # Assert that a config file with multiple config sections has
464 # those sections preserved...
465 cat >expect <<-\EOF &&
466 branch.dest.key1=value1
467 some.gar.b=age
468 branch.dest.key2=value2
470 cat >config.branch <<\EOF &&
471 ;; Note the lack of -\EOF above & mixed indenting here. This is
472 ;; intentional, we are also testing that the formatting of copied
473 ;; sections is preserved.
475 ;; Comment for source. Tabs
476 [branch "source"]
477 ;; Comment for the source value
478 key1 = value1
479 ;; Comment for some.gar. Spaces
480 [some "gar"]
481 ;; Comment for the some.gar value
482 b = age
483 ;; Comment for source, again. Mixed tabs/spaces.
484 [branch "source"]
485 ;; Comment for the source value, again
486 key2 = value2
488 cat config.branch >>.git/config &&
489 git branch -m source dest &&
490 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
491 test_cmp expect actual &&
493 # ...and that the comments for those sections are also
494 # preserved.
495 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
496 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
497 test_cmp expect actual
500 test_expect_success 'git branch -c dumps usage' '
501 test_expect_code 128 git branch -c 2>err &&
502 test_i18ngrep "branch name required" err
505 test_expect_success 'git branch --copy dumps usage' '
506 test_expect_code 128 git branch --copy 2>err &&
507 test_i18ngrep "branch name required" err
510 test_expect_success 'git branch -c d e should work' '
511 git branch --create-reflog d &&
512 git reflog exists refs/heads/d &&
513 git config branch.d.dummy Hello &&
514 git branch -c d e &&
515 git reflog exists refs/heads/d &&
516 git reflog exists refs/heads/e &&
517 echo Hello >expect &&
518 git config branch.e.dummy >actual &&
519 test_cmp expect actual &&
520 echo Hello >expect &&
521 git config branch.d.dummy >actual &&
522 test_cmp expect actual
525 test_expect_success 'git branch --copy is a synonym for -c' '
526 git branch --create-reflog copy &&
527 git reflog exists refs/heads/copy &&
528 git config branch.copy.dummy Hello &&
529 git branch --copy copy copy-to &&
530 git reflog exists refs/heads/copy &&
531 git reflog exists refs/heads/copy-to &&
532 echo Hello >expect &&
533 git config branch.copy.dummy >actual &&
534 test_cmp expect actual &&
535 echo Hello >expect &&
536 git config branch.copy-to.dummy >actual &&
537 test_cmp expect actual
540 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
541 git checkout -b ee &&
542 git reflog exists refs/heads/ee &&
543 git config branch.ee.dummy Hello &&
544 git branch -c ee ef &&
545 git reflog exists refs/heads/ee &&
546 git reflog exists refs/heads/ef &&
547 test $(git config branch.ee.dummy) = Hello &&
548 test $(git config branch.ef.dummy) = Hello &&
549 test $(git rev-parse --abbrev-ref HEAD) = ee
552 test_expect_success 'git branch -c f/f g/g should work' '
553 git branch --create-reflog f/f &&
554 git reflog exists refs/heads/f/f &&
555 git config branch.f/f.dummy Hello &&
556 git branch -c f/f g/g &&
557 git reflog exists refs/heads/f/f &&
558 git reflog exists refs/heads/g/g &&
559 test $(git config branch.f/f.dummy) = Hello &&
560 test $(git config branch.g/g.dummy) = Hello
563 test_expect_success 'git branch -c m2 m2 should work' '
564 git branch --create-reflog m2 &&
565 git reflog exists refs/heads/m2 &&
566 git config branch.m2.dummy Hello &&
567 git branch -c m2 m2 &&
568 git reflog exists refs/heads/m2 &&
569 test $(git config branch.m2.dummy) = Hello
572 test_expect_success 'git branch -c zz zz/zz should fail' '
573 git branch --create-reflog zz &&
574 git reflog exists refs/heads/zz &&
575 test_must_fail git branch -c zz zz/zz
578 test_expect_success 'git branch -c b/b b should fail' '
579 git branch --create-reflog b/b &&
580 test_must_fail git branch -c b/b b
583 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
584 git branch --create-reflog o/q &&
585 git reflog exists refs/heads/o/q &&
586 git reflog exists refs/heads/o/p &&
587 git branch -C o/q o/p
590 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
591 git reflog exists refs/heads/o/q &&
592 git reflog exists refs/heads/o/p &&
593 git branch -c -f o/q o/p
596 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
597 git branch qq &&
598 git branch rr &&
599 test_must_fail git branch -c qq rr/qq
602 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
603 git branch b1 &&
604 git checkout -b b2 &&
605 test_must_fail git branch -C b1 b2
608 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
609 git checkout -b c1 &&
610 git branch c2 &&
611 git branch -C c1 c2 &&
612 test $(git rev-parse --abbrev-ref HEAD) = c1
615 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
616 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
617 ! grep "$msg$" .git/logs/HEAD
620 test_expect_success 'git branch -C main should work when main is checked out' '
621 git checkout main &&
622 git branch -C main
625 test_expect_success 'git branch -C main main should work when main is checked out' '
626 git checkout main &&
627 git branch -C main main
630 test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
631 git checkout main &&
632 git branch main5 &&
633 git branch -C main5 main5
636 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
637 git branch --create-reflog cd &&
638 git reflog exists refs/heads/cd &&
639 git config branch.cd.dummy CD &&
640 git branch --create-reflog ab &&
641 git reflog exists refs/heads/ab &&
642 git config branch.ab.dummy AB &&
643 git branch -C ab cd &&
644 git reflog exists refs/heads/ab &&
645 git reflog exists refs/heads/cd &&
646 test $(git config branch.ab.dummy) = AB &&
647 test $(git config branch.cd.dummy) = AB
650 test_expect_success 'git branch -c correctly copies multiple config sections' '
651 FOO=1 &&
652 export FOO &&
653 test_when_finished "git checkout main" &&
654 git checkout -b source2 main &&
656 # Assert that a config file with multiple config sections has
657 # those sections preserved...
658 cat >expect <<-\EOF &&
659 branch.source2.key1=value1
660 branch.dest2.key1=value1
661 more.gar.b=age
662 branch.source2.key2=value2
663 branch.dest2.key2=value2
665 cat >config.branch <<\EOF &&
666 ;; Note the lack of -\EOF above & mixed indenting here. This is
667 ;; intentional, we are also testing that the formatting of copied
668 ;; sections is preserved.
670 ;; Comment for source2. Tabs
671 [branch "source2"]
672 ;; Comment for the source2 value
673 key1 = value1
674 ;; Comment for more.gar. Spaces
675 [more "gar"]
676 ;; Comment for the more.gar value
677 b = age
678 ;; Comment for source2, again. Mixed tabs/spaces.
679 [branch "source2"]
680 ;; Comment for the source2 value, again
681 key2 = value2
683 cat config.branch >>.git/config &&
684 git branch -c source2 dest2 &&
685 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
686 test_cmp expect actual &&
688 # ...and that the comments and formatting for those sections
689 # is also preserved.
690 cat >expect <<\EOF &&
691 ;; Comment for source2. Tabs
692 [branch "source2"]
693 ;; Comment for the source2 value
694 key1 = value1
695 ;; Comment for more.gar. Spaces
696 [branch "dest2"]
697 ;; Comment for the source2 value
698 key1 = value1
699 ;; Comment for more.gar. Spaces
700 [more "gar"]
701 ;; Comment for the more.gar value
702 b = age
703 ;; Comment for source2, again. Mixed tabs/spaces.
704 [branch "source2"]
705 ;; Comment for the source2 value, again
706 key2 = value2
707 [branch "dest2"]
708 ;; Comment for the source2 value, again
709 key2 = value2
711 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
712 test_cmp expect actual
715 test_expect_success 'deleting a symref' '
716 git branch target &&
717 git symbolic-ref refs/heads/symref refs/heads/target &&
718 echo "Deleted branch symref (was refs/heads/target)." >expect &&
719 git branch -d symref >actual &&
720 test_path_is_file .git/refs/heads/target &&
721 test_path_is_missing .git/refs/heads/symref &&
722 test_cmp expect actual
725 test_expect_success 'deleting a dangling symref' '
726 git symbolic-ref refs/heads/dangling-symref nowhere &&
727 test_path_is_file .git/refs/heads/dangling-symref &&
728 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
729 git branch -d dangling-symref >actual &&
730 test_path_is_missing .git/refs/heads/dangling-symref &&
731 test_cmp expect actual
734 test_expect_success 'deleting a self-referential symref' '
735 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
736 test_path_is_file .git/refs/heads/self-reference &&
737 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
738 git branch -d self-reference >actual &&
739 test_path_is_missing .git/refs/heads/self-reference &&
740 test_cmp expect actual
743 test_expect_success 'renaming a symref is not allowed' '
744 git symbolic-ref refs/heads/topic refs/heads/main &&
745 test_must_fail git branch -m topic new-topic &&
746 git symbolic-ref refs/heads/topic &&
747 test_path_is_file .git/refs/heads/main &&
748 test_path_is_missing .git/refs/heads/new-topic
751 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
752 git branch --create-reflog u &&
753 mv .git/logs/refs/heads/u real-u &&
754 ln -s real-u .git/logs/refs/heads/u &&
755 test_must_fail git branch -m u v
758 test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' '
759 test_when_finished "rm -rf subdir" &&
760 git init --bare subdir &&
762 rm -rfv subdir/refs subdir/objects subdir/packed-refs &&
763 ln -s ../.git/refs subdir/refs &&
764 ln -s ../.git/objects subdir/objects &&
765 ln -s ../.git/packed-refs subdir/packed-refs &&
767 git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
768 git rev-parse --absolute-git-dir >our.dir &&
769 ! test_cmp subdir.dir our.dir &&
771 git -C subdir log &&
772 git -C subdir branch rename-src &&
773 git rev-parse rename-src >expect &&
774 git -C subdir branch -m rename-src rename-dest &&
775 git rev-parse rename-dest >actual &&
776 test_cmp expect actual &&
777 git branch -D rename-dest
780 test_expect_success 'test tracking setup via --track' '
781 git config remote.local.url . &&
782 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
783 (git show-ref -q refs/remotes/local/main || git fetch local) &&
784 git branch --track my1 local/main &&
785 test $(git config branch.my1.remote) = local &&
786 test $(git config branch.my1.merge) = refs/heads/main
789 test_expect_success 'test tracking setup (non-wildcard, matching)' '
790 git config remote.local.url . &&
791 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
792 (git show-ref -q refs/remotes/local/main || git fetch local) &&
793 git branch --track my4 local/main &&
794 test $(git config branch.my4.remote) = local &&
795 test $(git config branch.my4.merge) = refs/heads/main
798 test_expect_success 'tracking setup fails on non-matching refspec' '
799 git config remote.local.url . &&
800 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
801 (git show-ref -q refs/remotes/local/main || git fetch local) &&
802 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
803 test_must_fail git branch --track my5 local/main &&
804 test_must_fail git config branch.my5.remote &&
805 test_must_fail git config branch.my5.merge
808 test_expect_success 'test tracking setup via config' '
809 git config branch.autosetupmerge true &&
810 git config remote.local.url . &&
811 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
812 (git show-ref -q refs/remotes/local/main || git fetch local) &&
813 git branch my3 local/main &&
814 test $(git config branch.my3.remote) = local &&
815 test $(git config branch.my3.merge) = refs/heads/main
818 test_expect_success 'test overriding tracking setup via --no-track' '
819 git config branch.autosetupmerge true &&
820 git config remote.local.url . &&
821 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
822 (git show-ref -q refs/remotes/local/main || git fetch local) &&
823 git branch --no-track my2 local/main &&
824 git config branch.autosetupmerge false &&
825 ! test "$(git config branch.my2.remote)" = local &&
826 ! test "$(git config branch.my2.merge)" = refs/heads/main
829 test_expect_success 'no tracking without .fetch entries' '
830 git config branch.autosetupmerge true &&
831 git branch my6 s &&
832 git config branch.autosetupmerge false &&
833 test -z "$(git config branch.my6.remote)" &&
834 test -z "$(git config branch.my6.merge)"
837 test_expect_success 'test tracking setup via --track but deeper' '
838 git config remote.local.url . &&
839 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
840 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
841 git branch --track my7 local/o/o &&
842 test "$(git config branch.my7.remote)" = local &&
843 test "$(git config branch.my7.merge)" = refs/heads/o/o
846 test_expect_success 'test deleting branch deletes branch config' '
847 git branch -d my7 &&
848 test -z "$(git config branch.my7.remote)" &&
849 test -z "$(git config branch.my7.merge)"
852 test_expect_success 'test deleting branch without config' '
853 git branch my7 s &&
854 sha1=$(git rev-parse my7 | cut -c 1-7) &&
855 echo "Deleted branch my7 (was $sha1)." >expect &&
856 git branch -d my7 >actual 2>&1 &&
857 test_cmp expect actual
860 test_expect_success 'deleting currently checked out branch fails' '
861 git worktree add -b my7 my7 &&
862 test_must_fail git -C my7 branch -d my7 &&
863 test_must_fail git branch -d my7 &&
864 rm -r my7 &&
865 git worktree prune
868 test_expect_success 'test --track without .fetch entries' '
869 git branch --track my8 &&
870 test "$(git config branch.my8.remote)" &&
871 test "$(git config branch.my8.merge)"
874 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
875 git config branch.autosetupmerge always &&
876 git branch my9 HEAD^ &&
877 git config branch.autosetupmerge false
880 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
881 test_must_fail git branch --track my10 HEAD^
884 test_expect_success 'branch from tag w/--track causes failure' '
885 git tag foobar &&
886 test_must_fail git branch --track my11 foobar
889 test_expect_success '--set-upstream-to fails on multiple branches' '
890 echo "fatal: too many arguments to set new upstream" >expect &&
891 test_must_fail git branch --set-upstream-to main a b c 2>err &&
892 test_cmp expect err
895 test_expect_success '--set-upstream-to fails on detached HEAD' '
896 git checkout HEAD^{} &&
897 test_when_finished git checkout - &&
898 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch." >expect &&
899 test_must_fail git branch --set-upstream-to main 2>err &&
900 test_cmp expect err
903 test_expect_success '--set-upstream-to fails on a missing dst branch' '
904 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
905 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
906 test_cmp expect err
909 test_expect_success '--set-upstream-to fails on a missing src branch' '
910 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
911 test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
914 test_expect_success '--set-upstream-to fails on a non-ref' '
915 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
916 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
917 test_cmp expect err
920 test_expect_success '--set-upstream-to fails on locked config' '
921 test_when_finished "rm -f .git/config.lock" &&
922 >.git/config.lock &&
923 git branch locked &&
924 test_must_fail git branch --set-upstream-to locked 2>err &&
925 test_i18ngrep "could not lock config file .git/config" err
928 test_expect_success 'use --set-upstream-to modify HEAD' '
929 test_config branch.main.remote foo &&
930 test_config branch.main.merge foo &&
931 git branch my12 &&
932 git branch --set-upstream-to my12 &&
933 test "$(git config branch.main.remote)" = "." &&
934 test "$(git config branch.main.merge)" = "refs/heads/my12"
937 test_expect_success 'use --set-upstream-to modify a particular branch' '
938 git branch my13 &&
939 git branch --set-upstream-to main my13 &&
940 test_when_finished "git branch --unset-upstream my13" &&
941 test "$(git config branch.my13.remote)" = "." &&
942 test "$(git config branch.my13.merge)" = "refs/heads/main"
945 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
946 echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
947 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
948 test_cmp expect err
951 test_expect_success '--unset-upstream should fail if config is locked' '
952 test_when_finished "rm -f .git/config.lock" &&
953 git branch --set-upstream-to locked &&
954 >.git/config.lock &&
955 test_must_fail git branch --unset-upstream 2>err &&
956 test_i18ngrep "could not lock config file .git/config" err
959 test_expect_success 'test --unset-upstream on HEAD' '
960 git branch my14 &&
961 test_config branch.main.remote foo &&
962 test_config branch.main.merge foo &&
963 git branch --set-upstream-to my14 &&
964 git branch --unset-upstream &&
965 test_must_fail git config branch.main.remote &&
966 test_must_fail git config branch.main.merge &&
967 # fail for a branch without upstream set
968 echo "fatal: Branch '"'"'main'"'"' has no upstream information" >expect &&
969 test_must_fail git branch --unset-upstream 2>err &&
970 test_cmp expect err
973 test_expect_success '--unset-upstream should fail on multiple branches' '
974 echo "fatal: too many arguments to unset upstream" >expect &&
975 test_must_fail git branch --unset-upstream a b c 2>err &&
976 test_cmp expect err
979 test_expect_success '--unset-upstream should fail on detached HEAD' '
980 git checkout HEAD^{} &&
981 test_when_finished git checkout - &&
982 echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect &&
983 test_must_fail git branch --unset-upstream 2>err &&
984 test_cmp expect err
987 test_expect_success 'test --unset-upstream on a particular branch' '
988 git branch my15 &&
989 git branch --set-upstream-to main my14 &&
990 git branch --unset-upstream my14 &&
991 test_must_fail git config branch.my14.remote &&
992 test_must_fail git config branch.my14.merge
995 test_expect_success 'disabled option --set-upstream fails' '
996 test_must_fail git branch --set-upstream origin/main
999 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
1000 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1001 cat >expect <<-\EOF &&
1002 warning: not setting branch 'my13' as its own upstream
1004 test_expect_code 1 git config branch.my13.remote &&
1005 test_expect_code 1 git config branch.my13.merge &&
1006 test_cmp expect actual
1009 # Keep this test last, as it changes the current branch
1010 cat >expect <<EOF
1011 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
1013 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
1014 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1015 git checkout -b g/h/i -l main &&
1016 test_path_is_file .git/refs/heads/g/h/i &&
1017 test_path_is_file .git/logs/refs/heads/g/h/i &&
1018 test_cmp expect .git/logs/refs/heads/g/h/i
1021 test_expect_success 'checkout -b makes reflog by default' '
1022 git checkout main &&
1023 git config --unset core.logAllRefUpdates &&
1024 git checkout -b alpha &&
1025 git rev-parse --verify alpha@{0}
1028 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1029 git checkout main &&
1030 git config core.logAllRefUpdates false &&
1031 git checkout -b beta &&
1032 test_must_fail git rev-parse --verify beta@{0}
1035 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1036 git checkout main &&
1037 git checkout -lb gamma &&
1038 git config --unset core.logAllRefUpdates &&
1039 git rev-parse --verify gamma@{0}
1042 test_expect_success 'avoid ambiguous track and advise' '
1043 git config branch.autosetupmerge true &&
1044 git config remote.ambi1.url lalala &&
1045 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1046 git config remote.ambi2.url lilili &&
1047 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1048 cat <<-EOF >expected &&
1049 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1050 hint: There are multiple remotes whose fetch refspecs map to the remote
1051 hint: tracking ref '\''refs/heads/main'\'':
1052 hint: ambi1
1053 hint: ambi2
1054 hint: ''
1055 hint: This is typically a configuration error.
1056 hint: ''
1057 hint: To support setting up tracking branches, ensure that
1058 hint: different remotes'\'' fetch refspecs map into different
1059 hint: tracking namespaces.
1061 test_must_fail git branch all1 main 2>actual &&
1062 test_cmp expected actual &&
1063 test -z "$(git config branch.all1.merge)"
1066 test_expect_success 'autosetuprebase local on a tracked local branch' '
1067 git config remote.local.url . &&
1068 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1069 git config branch.autosetuprebase local &&
1070 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1071 git branch mybase &&
1072 git branch --track myr1 mybase &&
1073 test "$(git config branch.myr1.remote)" = . &&
1074 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1075 test "$(git config branch.myr1.rebase)" = true
1078 test_expect_success 'autosetuprebase always on a tracked local branch' '
1079 git config remote.local.url . &&
1080 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1081 git config branch.autosetuprebase always &&
1082 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1083 git branch mybase2 &&
1084 git branch --track myr2 mybase &&
1085 test "$(git config branch.myr2.remote)" = . &&
1086 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1087 test "$(git config branch.myr2.rebase)" = true
1090 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1091 git config remote.local.url . &&
1092 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1093 git config branch.autosetuprebase remote &&
1094 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1095 git branch mybase3 &&
1096 git branch --track myr3 mybase2 &&
1097 test "$(git config branch.myr3.remote)" = . &&
1098 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1099 ! test "$(git config branch.myr3.rebase)" = true
1102 test_expect_success 'autosetuprebase never on a tracked local branch' '
1103 git config remote.local.url . &&
1104 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1105 git config branch.autosetuprebase never &&
1106 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1107 git branch mybase4 &&
1108 git branch --track myr4 mybase2 &&
1109 test "$(git config branch.myr4.remote)" = . &&
1110 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1111 ! test "$(git config branch.myr4.rebase)" = true
1114 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1115 git config remote.local.url . &&
1116 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1117 git config branch.autosetuprebase local &&
1118 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1119 git branch --track myr5 local/main &&
1120 test "$(git config branch.myr5.remote)" = local &&
1121 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1122 ! test "$(git config branch.myr5.rebase)" = true
1125 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1126 git config remote.local.url . &&
1127 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1128 git config branch.autosetuprebase never &&
1129 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1130 git branch --track myr6 local/main &&
1131 test "$(git config branch.myr6.remote)" = local &&
1132 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1133 ! test "$(git config branch.myr6.rebase)" = true
1136 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1137 git config remote.local.url . &&
1138 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1139 git config branch.autosetuprebase remote &&
1140 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1141 git branch --track myr7 local/main &&
1142 test "$(git config branch.myr7.remote)" = local &&
1143 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1144 test "$(git config branch.myr7.rebase)" = true
1147 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1148 git config remote.local.url . &&
1149 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1150 git config branch.autosetuprebase remote &&
1151 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1152 git branch --track myr8 local/main &&
1153 test "$(git config branch.myr8.remote)" = local &&
1154 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1155 test "$(git config branch.myr8.rebase)" = true
1158 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1159 git config --unset branch.autosetuprebase &&
1160 git config remote.local.url . &&
1161 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1162 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1163 git branch --track myr9 local/main &&
1164 test "$(git config branch.myr9.remote)" = local &&
1165 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1166 test "z$(git config branch.myr9.rebase)" = z
1169 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1170 git config remote.local.url . &&
1171 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1172 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1173 git branch mybase10 &&
1174 git branch --track myr10 mybase2 &&
1175 test "$(git config branch.myr10.remote)" = . &&
1176 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1177 test "z$(git config branch.myr10.rebase)" = z
1180 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1181 git config remote.local.url . &&
1182 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1183 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1184 git branch --no-track myr11 mybase2 &&
1185 test "z$(git config branch.myr11.remote)" = z &&
1186 test "z$(git config branch.myr11.merge)" = z &&
1187 test "z$(git config branch.myr11.rebase)" = z
1190 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
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/main || git fetch local) &&
1194 git branch --no-track myr12 local/main &&
1195 test "z$(git config branch.myr12.remote)" = z &&
1196 test "z$(git config branch.myr12.merge)" = z &&
1197 test "z$(git config branch.myr12.rebase)" = z
1200 test_expect_success 'autosetuprebase never on an untracked local branch' '
1201 git config branch.autosetuprebase never &&
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/main || git fetch local) &&
1205 git branch --no-track myr13 mybase2 &&
1206 test "z$(git config branch.myr13.remote)" = z &&
1207 test "z$(git config branch.myr13.merge)" = z &&
1208 test "z$(git config branch.myr13.rebase)" = z
1211 test_expect_success 'autosetuprebase local on an untracked local branch' '
1212 git config branch.autosetuprebase local &&
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/main || git fetch local) &&
1216 git branch --no-track myr14 mybase2 &&
1217 test "z$(git config branch.myr14.remote)" = z &&
1218 test "z$(git config branch.myr14.merge)" = z &&
1219 test "z$(git config branch.myr14.rebase)" = z
1222 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1223 git config branch.autosetuprebase remote &&
1224 git config remote.local.url . &&
1225 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1226 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1227 git branch --no-track myr15 mybase2 &&
1228 test "z$(git config branch.myr15.remote)" = z &&
1229 test "z$(git config branch.myr15.merge)" = z &&
1230 test "z$(git config branch.myr15.rebase)" = z
1233 test_expect_success 'autosetuprebase always on an untracked local branch' '
1234 git config branch.autosetuprebase always &&
1235 git config remote.local.url . &&
1236 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1237 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1238 git branch --no-track myr16 mybase2 &&
1239 test "z$(git config branch.myr16.remote)" = z &&
1240 test "z$(git config branch.myr16.merge)" = z &&
1241 test "z$(git config branch.myr16.rebase)" = z
1244 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1245 git config branch.autosetuprebase never &&
1246 git config remote.local.url . &&
1247 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1248 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1249 git branch --no-track myr17 local/main &&
1250 test "z$(git config branch.myr17.remote)" = z &&
1251 test "z$(git config branch.myr17.merge)" = z &&
1252 test "z$(git config branch.myr17.rebase)" = z
1255 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1256 git config branch.autosetuprebase local &&
1257 git config remote.local.url . &&
1258 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1259 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1260 git branch --no-track myr18 local/main &&
1261 test "z$(git config branch.myr18.remote)" = z &&
1262 test "z$(git config branch.myr18.merge)" = z &&
1263 test "z$(git config branch.myr18.rebase)" = z
1266 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1267 git config branch.autosetuprebase remote &&
1268 git config remote.local.url . &&
1269 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1270 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1271 git branch --no-track myr19 local/main &&
1272 test "z$(git config branch.myr19.remote)" = z &&
1273 test "z$(git config branch.myr19.merge)" = z &&
1274 test "z$(git config branch.myr19.rebase)" = z
1277 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1278 git config branch.autosetuprebase always &&
1279 git config remote.local.url . &&
1280 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1281 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1282 git branch --no-track myr20 local/main &&
1283 test "z$(git config branch.myr20.remote)" = z &&
1284 test "z$(git config branch.myr20.merge)" = z &&
1285 test "z$(git config branch.myr20.rebase)" = z
1288 test_expect_success 'autosetuprebase always on detached HEAD' '
1289 git config branch.autosetupmerge always &&
1290 test_when_finished git checkout main &&
1291 git checkout HEAD^0 &&
1292 git branch my11 &&
1293 test -z "$(git config branch.my11.remote)" &&
1294 test -z "$(git config branch.my11.merge)"
1297 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1298 git config branch.autosetuprebase garbage &&
1299 test_must_fail git branch
1302 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1303 git config --unset branch.autosetuprebase &&
1304 echo "[branch] autosetuprebase" >>.git/config &&
1305 test_must_fail git branch &&
1306 git config --unset branch.autosetuprebase
1309 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1310 git checkout my9 &&
1311 git config --unset branch.my8.merge &&
1312 test_must_fail git branch -d my8
1315 test_expect_success 'attempt to delete a branch merged to its base' '
1316 # we are on my9 which is the initial commit; traditionally
1317 # we would not have allowed deleting my8 that is not merged
1318 # to my9, but it is set to track main that already has my8
1319 git config branch.my8.merge refs/heads/main &&
1320 git branch -d my8
1323 test_expect_success 'attempt to delete a branch merged to its base' '
1324 git checkout main &&
1325 echo Third >>A &&
1326 git commit -m "Third commit" A &&
1327 git branch -t my10 my9 &&
1328 git branch -f my10 HEAD^ &&
1329 # we are on main which is at the third commit, and my10
1330 # is behind us, so traditionally we would have allowed deleting
1331 # it; but my10 is set to track my9 that is further behind.
1332 test_must_fail git branch -d my10
1335 test_expect_success 'branch --delete --force removes dangling branch' '
1336 git checkout main &&
1337 test_commit unstable &&
1338 hash=$(git rev-parse HEAD) &&
1339 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1340 git branch --no-track dangling &&
1341 mv $objpath $objpath.x &&
1342 test_when_finished "mv $objpath.x $objpath" &&
1343 git branch --delete --force dangling &&
1344 git for-each-ref refs/heads/dangling >actual &&
1345 test_must_be_empty actual
1348 test_expect_success 'use --edit-description' '
1349 write_script editor <<-\EOF &&
1350 echo "New contents" >"$1"
1352 EDITOR=./editor git branch --edit-description &&
1353 write_script editor <<-\EOF &&
1354 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1356 EDITOR=./editor git branch --edit-description &&
1357 echo "New contents" >expect &&
1358 test_cmp expect EDITOR_OUTPUT
1361 test_expect_success 'detect typo in branch name when using --edit-description' '
1362 write_script editor <<-\EOF &&
1363 echo "New contents" >"$1"
1365 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1368 test_expect_success 'refuse --edit-description on unborn branch for now' '
1369 test_when_finished "git checkout main" &&
1370 write_script editor <<-\EOF &&
1371 echo "New contents" >"$1"
1373 git checkout --orphan unborn &&
1374 test_must_fail env EDITOR=./editor git branch --edit-description
1377 test_expect_success '--merged catches invalid object names' '
1378 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1381 test_expect_success '--list during rebase' '
1382 test_when_finished "reset_rebase" &&
1383 git checkout main &&
1384 FAKE_LINES="1 edit 2" &&
1385 export FAKE_LINES &&
1386 set_fake_editor &&
1387 git rebase -i HEAD~2 &&
1388 git branch --list >actual &&
1389 test_i18ngrep "rebasing main" actual
1392 test_expect_success '--list during rebase from detached HEAD' '
1393 test_when_finished "reset_rebase && git checkout main" &&
1394 git checkout main^0 &&
1395 oid=$(git rev-parse --short HEAD) &&
1396 FAKE_LINES="1 edit 2" &&
1397 export FAKE_LINES &&
1398 set_fake_editor &&
1399 git rebase -i HEAD~2 &&
1400 git branch --list >actual &&
1401 test_i18ngrep "rebasing detached HEAD $oid" actual
1404 test_expect_success 'tracking with unexpected .fetch refspec' '
1405 rm -rf a b c d &&
1406 git init -b main a &&
1408 cd a &&
1409 test_commit a
1410 ) &&
1411 git init -b main b &&
1413 cd b &&
1414 test_commit b
1415 ) &&
1416 git init -b main c &&
1418 cd c &&
1419 test_commit c &&
1420 git remote add a ../a &&
1421 git remote add b ../b &&
1422 git fetch --all
1423 ) &&
1424 git init -b main d &&
1426 cd d &&
1427 git remote add c ../c &&
1428 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1429 git fetch c &&
1430 git branch --track local/a/main remotes/a/main &&
1431 test "$(git config branch.local/a/main.remote)" = "c" &&
1432 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1433 git rev-parse --verify a >expect &&
1434 git rev-parse --verify local/a/main >actual &&
1435 test_cmp expect actual
1439 test_expect_success 'configured committerdate sort' '
1440 git init -b main sort &&
1442 cd sort &&
1443 git config branch.sort committerdate &&
1444 test_commit initial &&
1445 git checkout -b a &&
1446 test_commit a &&
1447 git checkout -b c &&
1448 test_commit c &&
1449 git checkout -b b &&
1450 test_commit b &&
1451 git branch >actual &&
1452 cat >expect <<-\EOF &&
1453 main
1458 test_cmp expect actual
1462 test_expect_success 'option override configured sort' '
1464 cd sort &&
1465 git config branch.sort committerdate &&
1466 git branch --sort=refname >actual &&
1467 cat >expect <<-\EOF &&
1471 main
1473 test_cmp expect actual
1477 test_expect_success 'invalid sort parameter in configuration' '
1479 cd sort &&
1480 git config branch.sort "v:notvalid" &&
1482 # this works in the "listing" mode, so bad sort key
1483 # is a dying offence.
1484 test_must_fail git branch &&
1486 # these do not need to use sorting, and should all
1487 # succeed
1488 git branch newone main &&
1489 git branch -c newone newerone &&
1490 git branch -m newone newestone &&
1491 git branch -d newerone newestone
1495 test_expect_success 'tracking info copied with --track=inherit' '
1496 git branch --track=inherit foo2 my1 &&
1497 test_cmp_config local branch.foo2.remote &&
1498 test_cmp_config refs/heads/main branch.foo2.merge
1501 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1502 test_unconfig branch.autoSetupMerge &&
1503 # default config does not copy tracking info
1504 git branch foo-no-inherit my1 &&
1505 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1506 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1507 # with autoSetupMerge=inherit, we copy tracking info from my1
1508 test_config branch.autoSetupMerge inherit &&
1509 git branch foo3 my1 &&
1510 test_cmp_config local branch.foo3.remote &&
1511 test_cmp_config refs/heads/main branch.foo3.merge &&
1512 # no tracking info to inherit from main
1513 git branch main2 main &&
1514 test_cmp_config "" --default "" branch.main2.remote &&
1515 test_cmp_config "" --default "" branch.main2.merge
1518 test_expect_success '--track overrides branch.autoSetupMerge' '
1519 test_config branch.autoSetupMerge inherit &&
1520 git branch --track=direct foo4 my1 &&
1521 test_cmp_config . branch.foo4.remote &&
1522 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1523 git branch --no-track foo5 my1 &&
1524 test_cmp_config "" --default "" branch.foo5.remote &&
1525 test_cmp_config "" --default "" branch.foo5.merge
1528 test_done