branch: error copying or renaming a detached HEAD
[alt-git.git] / t / t3200-branch.sh
blob38c57de71ba52f91595c1b305064bc3b34d353c7
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 " $ZERO_OID.*$msg$" .git/logs/HEAD &&
205 grep "^$ZERO_OID.*$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 -M and -C fail on detached HEAD' '
272 git checkout HEAD^{} &&
273 test_when_finished git checkout - &&
274 echo "fatal: cannot rename the current branch while not on any." >expect &&
275 test_must_fail git branch -M must-fail 2>err &&
276 test_cmp expect err &&
277 echo "fatal: cannot copy the current branch while not on any." >expect &&
278 test_must_fail git branch -C must-fail 2>err &&
279 test_cmp expect err
282 test_expect_success 'git branch -v -d t should work' '
283 git branch t &&
284 git rev-parse --verify refs/heads/t &&
285 git branch -v -d t &&
286 test_must_fail git rev-parse --verify refs/heads/t
289 test_expect_success 'git branch -v -m t s should work' '
290 git branch t &&
291 git rev-parse --verify refs/heads/t &&
292 git branch -v -m t s &&
293 test_must_fail git rev-parse --verify refs/heads/t &&
294 git rev-parse --verify refs/heads/s &&
295 git branch -d s
298 test_expect_success 'git branch -m -d t s should fail' '
299 git branch t &&
300 git rev-parse refs/heads/t &&
301 test_must_fail git branch -m -d t s &&
302 git branch -d t &&
303 test_must_fail git rev-parse refs/heads/t
306 test_expect_success 'git branch --list -d t should fail' '
307 git branch t &&
308 git rev-parse refs/heads/t &&
309 test_must_fail git branch --list -d t &&
310 git branch -d t &&
311 test_must_fail git rev-parse refs/heads/t
314 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
315 test_when_finished "rm -rf repo1 repo2" &&
317 git init repo1 &&
318 git init repo1/sub &&
319 test_commit -C repo1/sub x &&
320 test_config_global protocol.file.allow always &&
321 git -C repo1 submodule add ./sub &&
322 git -C repo1 commit -m "adding sub" &&
324 git clone --recurse-submodules repo1 repo2 &&
325 git -C repo2/sub checkout -b work &&
326 test_must_fail git -C repo2/sub branch -D work
329 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
330 test_when_finished "rm -rf nonbare base secondary" &&
332 git init -b main nonbare &&
333 test_commit -C nonbare x &&
334 git clone --bare nonbare bare &&
335 git -C bare worktree add --detach ../secondary main &&
336 git -C secondary branch -D main
339 test_expect_success 'git branch --list -v with --abbrev' '
340 test_when_finished "git branch -D t" &&
341 git branch t &&
342 git branch -v --list t >actual.default &&
343 git branch -v --list --abbrev t >actual.abbrev &&
344 test_cmp actual.default actual.abbrev &&
346 git branch -v --list --no-abbrev t >actual.noabbrev &&
347 git branch -v --list --abbrev=0 t >actual.0abbrev &&
348 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
349 test_cmp actual.noabbrev actual.0abbrev &&
350 test_cmp actual.noabbrev actual.noabbrev-conf &&
352 git branch -v --list --abbrev=36 t >actual.36abbrev &&
353 # how many hexdigits are used?
354 read name objdefault rest <actual.abbrev &&
355 read name obj36 rest <actual.36abbrev &&
356 objfull=$(git rev-parse --verify t) &&
358 # are we really getting abbreviations?
359 test "$obj36" != "$objdefault" &&
360 expr "$obj36" : "$objdefault" >/dev/null &&
361 test "$objfull" != "$obj36" &&
362 expr "$objfull" : "$obj36" >/dev/null
366 test_expect_success 'git branch --column' '
367 COLUMNS=81 git branch --column=column >actual &&
368 cat >expect <<\EOF &&
369 a/b/c bam foo l * main n o/p r
370 abc bar j/k m/m mb o/o q topic
372 test_cmp expect actual
375 test_expect_success 'git branch --column with an extremely long branch name' '
376 long=this/is/a/part/of/long/branch/name &&
377 long=z$long/$long/$long/$long &&
378 test_when_finished "git branch -d $long" &&
379 git branch $long &&
380 COLUMNS=80 git branch --column=column >actual &&
381 cat >expect <<EOF &&
382 a/b/c
390 * main
397 topic
398 $long
400 test_cmp expect actual
403 test_expect_success 'git branch with column.*' '
404 git config column.ui column &&
405 git config column.branch "dense" &&
406 COLUMNS=80 git branch >actual &&
407 git config --unset column.branch &&
408 git config --unset column.ui &&
409 cat >expect <<\EOF &&
410 a/b/c bam foo l * main n o/p r
411 abc bar j/k m/m mb o/o q topic
413 test_cmp expect actual
416 test_expect_success 'git branch --column -v should fail' '
417 test_must_fail git branch --column -v
420 test_expect_success 'git branch -v with column.ui ignored' '
421 git config column.ui column &&
422 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
423 git config --unset column.ui &&
424 cat >expect <<\EOF &&
425 a/b/c
433 * main
440 topic
442 test_cmp expect actual
445 mv .git/config .git/config-saved
447 test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
448 git branch -m q q2 &&
449 git branch -m q2 q
452 mv .git/config-saved .git/config
454 git config branch.s/s.dummy Hello
456 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
457 git branch --create-reflog s/s &&
458 git reflog exists refs/heads/s/s &&
459 git branch --create-reflog s/t &&
460 git reflog exists refs/heads/s/t &&
461 git branch -d s/t &&
462 git branch -m s/s s &&
463 git reflog exists refs/heads/s
466 test_expect_success 'config information was renamed, too' '
467 test $(git config branch.s.dummy) = Hello &&
468 test_must_fail git config branch.s/s.dummy
471 test_expect_success 'git branch -m correctly renames multiple config sections' '
472 test_when_finished "git checkout main" &&
473 git checkout -b source main &&
475 # Assert that a config file with multiple config sections has
476 # those sections preserved...
477 cat >expect <<-\EOF &&
478 branch.dest.key1=value1
479 some.gar.b=age
480 branch.dest.key2=value2
482 cat >config.branch <<\EOF &&
483 ;; Note the lack of -\EOF above & mixed indenting here. This is
484 ;; intentional, we are also testing that the formatting of copied
485 ;; sections is preserved.
487 ;; Comment for source. Tabs
488 [branch "source"]
489 ;; Comment for the source value
490 key1 = value1
491 ;; Comment for some.gar. Spaces
492 [some "gar"]
493 ;; Comment for the some.gar value
494 b = age
495 ;; Comment for source, again. Mixed tabs/spaces.
496 [branch "source"]
497 ;; Comment for the source value, again
498 key2 = value2
500 cat config.branch >>.git/config &&
501 git branch -m source dest &&
502 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
503 test_cmp expect actual &&
505 # ...and that the comments for those sections are also
506 # preserved.
507 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
508 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
509 test_cmp expect actual
512 test_expect_success 'git branch -c dumps usage' '
513 test_expect_code 128 git branch -c 2>err &&
514 test_i18ngrep "branch name required" err
517 test_expect_success 'git branch --copy dumps usage' '
518 test_expect_code 128 git branch --copy 2>err &&
519 test_i18ngrep "branch name required" err
522 test_expect_success 'git branch -c d e should work' '
523 git branch --create-reflog d &&
524 git reflog exists refs/heads/d &&
525 git config branch.d.dummy Hello &&
526 git branch -c d e &&
527 git reflog exists refs/heads/d &&
528 git reflog exists refs/heads/e &&
529 echo Hello >expect &&
530 git config branch.e.dummy >actual &&
531 test_cmp expect actual &&
532 echo Hello >expect &&
533 git config branch.d.dummy >actual &&
534 test_cmp expect actual
537 test_expect_success 'git branch --copy is a synonym for -c' '
538 git branch --create-reflog copy &&
539 git reflog exists refs/heads/copy &&
540 git config branch.copy.dummy Hello &&
541 git branch --copy copy copy-to &&
542 git reflog exists refs/heads/copy &&
543 git reflog exists refs/heads/copy-to &&
544 echo Hello >expect &&
545 git config branch.copy.dummy >actual &&
546 test_cmp expect actual &&
547 echo Hello >expect &&
548 git config branch.copy-to.dummy >actual &&
549 test_cmp expect actual
552 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
553 git checkout -b ee &&
554 git reflog exists refs/heads/ee &&
555 git config branch.ee.dummy Hello &&
556 git branch -c ee ef &&
557 git reflog exists refs/heads/ee &&
558 git reflog exists refs/heads/ef &&
559 test $(git config branch.ee.dummy) = Hello &&
560 test $(git config branch.ef.dummy) = Hello &&
561 test $(git rev-parse --abbrev-ref HEAD) = ee
564 test_expect_success 'git branch -c f/f g/g should work' '
565 git branch --create-reflog f/f &&
566 git reflog exists refs/heads/f/f &&
567 git config branch.f/f.dummy Hello &&
568 git branch -c f/f g/g &&
569 git reflog exists refs/heads/f/f &&
570 git reflog exists refs/heads/g/g &&
571 test $(git config branch.f/f.dummy) = Hello &&
572 test $(git config branch.g/g.dummy) = Hello
575 test_expect_success 'git branch -c m2 m2 should work' '
576 git branch --create-reflog m2 &&
577 git reflog exists refs/heads/m2 &&
578 git config branch.m2.dummy Hello &&
579 git branch -c m2 m2 &&
580 git reflog exists refs/heads/m2 &&
581 test $(git config branch.m2.dummy) = Hello
584 test_expect_success 'git branch -c zz zz/zz should fail' '
585 git branch --create-reflog zz &&
586 git reflog exists refs/heads/zz &&
587 test_must_fail git branch -c zz zz/zz
590 test_expect_success 'git branch -c b/b b should fail' '
591 git branch --create-reflog b/b &&
592 test_must_fail git branch -c b/b b
595 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
596 git branch --create-reflog o/q &&
597 git reflog exists refs/heads/o/q &&
598 git reflog exists refs/heads/o/p &&
599 git branch -C o/q o/p
602 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
603 git reflog exists refs/heads/o/q &&
604 git reflog exists refs/heads/o/p &&
605 git branch -c -f o/q o/p
608 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
609 git branch qq &&
610 git branch rr &&
611 test_must_fail git branch -c qq rr/qq
614 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
615 git branch b1 &&
616 git checkout -b b2 &&
617 test_must_fail git branch -C b1 b2
620 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
621 git checkout -b c1 &&
622 git branch c2 &&
623 git branch -C c1 c2 &&
624 test $(git rev-parse --abbrev-ref HEAD) = c1
627 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
628 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
629 ! grep "$msg$" .git/logs/HEAD
632 test_expect_success 'git branch -C main should work when main is checked out' '
633 git checkout main &&
634 git branch -C main
637 test_expect_success 'git branch -C main main should work when main is checked out' '
638 git checkout main &&
639 git branch -C main main
642 test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
643 git checkout main &&
644 git branch main5 &&
645 git branch -C main5 main5
648 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
649 git branch --create-reflog cd &&
650 git reflog exists refs/heads/cd &&
651 git config branch.cd.dummy CD &&
652 git branch --create-reflog ab &&
653 git reflog exists refs/heads/ab &&
654 git config branch.ab.dummy AB &&
655 git branch -C ab cd &&
656 git reflog exists refs/heads/ab &&
657 git reflog exists refs/heads/cd &&
658 test $(git config branch.ab.dummy) = AB &&
659 test $(git config branch.cd.dummy) = AB
662 test_expect_success 'git branch -c correctly copies multiple config sections' '
663 FOO=1 &&
664 export FOO &&
665 test_when_finished "git checkout main" &&
666 git checkout -b source2 main &&
668 # Assert that a config file with multiple config sections has
669 # those sections preserved...
670 cat >expect <<-\EOF &&
671 branch.source2.key1=value1
672 branch.dest2.key1=value1
673 more.gar.b=age
674 branch.source2.key2=value2
675 branch.dest2.key2=value2
677 cat >config.branch <<\EOF &&
678 ;; Note the lack of -\EOF above & mixed indenting here. This is
679 ;; intentional, we are also testing that the formatting of copied
680 ;; sections is preserved.
682 ;; Comment for source2. Tabs
683 [branch "source2"]
684 ;; Comment for the source2 value
685 key1 = value1
686 ;; Comment for more.gar. Spaces
687 [more "gar"]
688 ;; Comment for the more.gar value
689 b = age
690 ;; Comment for source2, again. Mixed tabs/spaces.
691 [branch "source2"]
692 ;; Comment for the source2 value, again
693 key2 = value2
695 cat config.branch >>.git/config &&
696 git branch -c source2 dest2 &&
697 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
698 test_cmp expect actual &&
700 # ...and that the comments and formatting for those sections
701 # is also preserved.
702 cat >expect <<\EOF &&
703 ;; Comment for source2. Tabs
704 [branch "source2"]
705 ;; Comment for the source2 value
706 key1 = value1
707 ;; Comment for more.gar. Spaces
708 [branch "dest2"]
709 ;; Comment for the source2 value
710 key1 = value1
711 ;; Comment for more.gar. Spaces
712 [more "gar"]
713 ;; Comment for the more.gar value
714 b = age
715 ;; Comment for source2, again. Mixed tabs/spaces.
716 [branch "source2"]
717 ;; Comment for the source2 value, again
718 key2 = value2
719 [branch "dest2"]
720 ;; Comment for the source2 value, again
721 key2 = value2
723 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
724 test_cmp expect actual
727 test_expect_success 'deleting a symref' '
728 git branch target &&
729 git symbolic-ref refs/heads/symref refs/heads/target &&
730 echo "Deleted branch symref (was refs/heads/target)." >expect &&
731 git branch -d symref >actual &&
732 test_path_is_file .git/refs/heads/target &&
733 test_path_is_missing .git/refs/heads/symref &&
734 test_cmp expect actual
737 test_expect_success 'deleting a dangling symref' '
738 git symbolic-ref refs/heads/dangling-symref nowhere &&
739 test_path_is_file .git/refs/heads/dangling-symref &&
740 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
741 git branch -d dangling-symref >actual &&
742 test_path_is_missing .git/refs/heads/dangling-symref &&
743 test_cmp expect actual
746 test_expect_success 'deleting a self-referential symref' '
747 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
748 test_path_is_file .git/refs/heads/self-reference &&
749 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
750 git branch -d self-reference >actual &&
751 test_path_is_missing .git/refs/heads/self-reference &&
752 test_cmp expect actual
755 test_expect_success 'renaming a symref is not allowed' '
756 git symbolic-ref refs/heads/topic refs/heads/main &&
757 test_must_fail git branch -m topic new-topic &&
758 git symbolic-ref refs/heads/topic &&
759 test_path_is_file .git/refs/heads/main &&
760 test_path_is_missing .git/refs/heads/new-topic
763 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
764 git branch --create-reflog u &&
765 mv .git/logs/refs/heads/u real-u &&
766 ln -s real-u .git/logs/refs/heads/u &&
767 test_must_fail git branch -m u v
770 test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' '
771 test_when_finished "rm -rf subdir" &&
772 git init --bare subdir &&
774 rm -rfv subdir/refs subdir/objects subdir/packed-refs &&
775 ln -s ../.git/refs subdir/refs &&
776 ln -s ../.git/objects subdir/objects &&
777 ln -s ../.git/packed-refs subdir/packed-refs &&
779 git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
780 git rev-parse --absolute-git-dir >our.dir &&
781 ! test_cmp subdir.dir our.dir &&
783 git -C subdir log &&
784 git -C subdir branch rename-src &&
785 git rev-parse rename-src >expect &&
786 git -C subdir branch -m rename-src rename-dest &&
787 git rev-parse rename-dest >actual &&
788 test_cmp expect actual &&
789 git branch -D rename-dest
792 test_expect_success 'test tracking setup via --track' '
793 git config remote.local.url . &&
794 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
795 (git show-ref -q refs/remotes/local/main || git fetch local) &&
796 git branch --track my1 local/main &&
797 test $(git config branch.my1.remote) = local &&
798 test $(git config branch.my1.merge) = refs/heads/main
801 test_expect_success 'test tracking setup (non-wildcard, matching)' '
802 git config remote.local.url . &&
803 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
804 (git show-ref -q refs/remotes/local/main || git fetch local) &&
805 git branch --track my4 local/main &&
806 test $(git config branch.my4.remote) = local &&
807 test $(git config branch.my4.merge) = refs/heads/main
810 test_expect_success 'tracking setup fails on non-matching refspec' '
811 git config remote.local.url . &&
812 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
813 (git show-ref -q refs/remotes/local/main || git fetch local) &&
814 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
815 test_must_fail git branch --track my5 local/main &&
816 test_must_fail git config branch.my5.remote &&
817 test_must_fail git config branch.my5.merge
820 test_expect_success 'test tracking setup via config' '
821 git config branch.autosetupmerge true &&
822 git config remote.local.url . &&
823 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
824 (git show-ref -q refs/remotes/local/main || git fetch local) &&
825 git branch my3 local/main &&
826 test $(git config branch.my3.remote) = local &&
827 test $(git config branch.my3.merge) = refs/heads/main
830 test_expect_success 'test overriding tracking setup via --no-track' '
831 git config branch.autosetupmerge true &&
832 git config remote.local.url . &&
833 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
834 (git show-ref -q refs/remotes/local/main || git fetch local) &&
835 git branch --no-track my2 local/main &&
836 git config branch.autosetupmerge false &&
837 ! test "$(git config branch.my2.remote)" = local &&
838 ! test "$(git config branch.my2.merge)" = refs/heads/main
841 test_expect_success 'no tracking without .fetch entries' '
842 git config branch.autosetupmerge true &&
843 git branch my6 s &&
844 git config branch.autosetupmerge false &&
845 test -z "$(git config branch.my6.remote)" &&
846 test -z "$(git config branch.my6.merge)"
849 test_expect_success 'test tracking setup via --track but deeper' '
850 git config remote.local.url . &&
851 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
852 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
853 git branch --track my7 local/o/o &&
854 test "$(git config branch.my7.remote)" = local &&
855 test "$(git config branch.my7.merge)" = refs/heads/o/o
858 test_expect_success 'test deleting branch deletes branch config' '
859 git branch -d my7 &&
860 test -z "$(git config branch.my7.remote)" &&
861 test -z "$(git config branch.my7.merge)"
864 test_expect_success 'test deleting branch without config' '
865 git branch my7 s &&
866 sha1=$(git rev-parse my7 | cut -c 1-7) &&
867 echo "Deleted branch my7 (was $sha1)." >expect &&
868 git branch -d my7 >actual 2>&1 &&
869 test_cmp expect actual
872 test_expect_success 'deleting currently checked out branch fails' '
873 git worktree add -b my7 my7 &&
874 test_must_fail git -C my7 branch -d my7 &&
875 test_must_fail git branch -d my7 &&
876 rm -r my7 &&
877 git worktree prune
880 test_expect_success 'test --track without .fetch entries' '
881 git branch --track my8 &&
882 test "$(git config branch.my8.remote)" &&
883 test "$(git config branch.my8.merge)"
886 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
887 git config branch.autosetupmerge always &&
888 git branch my9 HEAD^ &&
889 git config branch.autosetupmerge false
892 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
893 test_must_fail git branch --track my10 HEAD^
896 test_expect_success 'branch from tag w/--track causes failure' '
897 git tag foobar &&
898 test_must_fail git branch --track my11 foobar
901 test_expect_success 'simple tracking works when remote branch name matches' '
902 test_when_finished "rm -rf otherserver" &&
903 git init otherserver &&
904 test_commit -C otherserver my_commit 1 &&
905 git -C otherserver branch feature &&
906 test_config branch.autosetupmerge simple &&
907 test_config remote.otherserver.url otherserver &&
908 test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* &&
909 git fetch otherserver &&
910 git branch feature otherserver/feature &&
911 test_cmp_config otherserver branch.feature.remote &&
912 test_cmp_config refs/heads/feature branch.feature.merge
915 test_expect_success 'simple tracking skips when remote branch name does not match' '
916 test_config branch.autosetupmerge simple &&
917 test_config remote.local.url . &&
918 test_config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
919 git fetch local &&
920 git branch my-other local/main &&
921 test_cmp_config "" --default "" branch.my-other.remote &&
922 test_cmp_config "" --default "" branch.my-other.merge
925 test_expect_success 'simple tracking skips when remote ref is not a branch' '
926 test_config branch.autosetupmerge simple &&
927 test_config remote.localtags.url . &&
928 test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* &&
929 git tag mytag12 main &&
930 git fetch localtags &&
931 git branch mytag12 localtags/mytag12 &&
932 test_cmp_config "" --default "" branch.mytag12.remote &&
933 test_cmp_config "" --default "" branch.mytag12.merge
936 test_expect_success '--set-upstream-to fails on multiple branches' '
937 echo "fatal: too many arguments to set new upstream" >expect &&
938 test_must_fail git branch --set-upstream-to main a b c 2>err &&
939 test_cmp expect err
942 test_expect_success '--set-upstream-to fails on detached HEAD' '
943 git checkout HEAD^{} &&
944 test_when_finished git checkout - &&
945 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch." >expect &&
946 test_must_fail git branch --set-upstream-to main 2>err &&
947 test_cmp expect err
950 test_expect_success '--set-upstream-to fails on a missing dst branch' '
951 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
952 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
953 test_cmp expect err
956 test_expect_success '--set-upstream-to fails on a missing src branch' '
957 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
958 test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
961 test_expect_success '--set-upstream-to fails on a non-ref' '
962 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
963 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
964 test_cmp expect err
967 test_expect_success '--set-upstream-to fails on locked config' '
968 test_when_finished "rm -f .git/config.lock" &&
969 >.git/config.lock &&
970 git branch locked &&
971 test_must_fail git branch --set-upstream-to locked 2>err &&
972 test_i18ngrep "could not lock config file .git/config" err
975 test_expect_success 'use --set-upstream-to modify HEAD' '
976 test_config branch.main.remote foo &&
977 test_config branch.main.merge foo &&
978 git branch my12 &&
979 git branch --set-upstream-to my12 &&
980 test "$(git config branch.main.remote)" = "." &&
981 test "$(git config branch.main.merge)" = "refs/heads/my12"
984 test_expect_success 'use --set-upstream-to modify a particular branch' '
985 git branch my13 &&
986 git branch --set-upstream-to main my13 &&
987 test_when_finished "git branch --unset-upstream my13" &&
988 test "$(git config branch.my13.remote)" = "." &&
989 test "$(git config branch.my13.merge)" = "refs/heads/main"
992 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
993 echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
994 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
995 test_cmp expect err
998 test_expect_success '--unset-upstream should fail if config is locked' '
999 test_when_finished "rm -f .git/config.lock" &&
1000 git branch --set-upstream-to locked &&
1001 >.git/config.lock &&
1002 test_must_fail git branch --unset-upstream 2>err &&
1003 test_i18ngrep "could not lock config file .git/config" err
1006 test_expect_success 'test --unset-upstream on HEAD' '
1007 git branch my14 &&
1008 test_config branch.main.remote foo &&
1009 test_config branch.main.merge foo &&
1010 git branch --set-upstream-to my14 &&
1011 git branch --unset-upstream &&
1012 test_must_fail git config branch.main.remote &&
1013 test_must_fail git config branch.main.merge &&
1014 # fail for a branch without upstream set
1015 echo "fatal: Branch '"'"'main'"'"' has no upstream information" >expect &&
1016 test_must_fail git branch --unset-upstream 2>err &&
1017 test_cmp expect err
1020 test_expect_success '--unset-upstream should fail on multiple branches' '
1021 echo "fatal: too many arguments to unset upstream" >expect &&
1022 test_must_fail git branch --unset-upstream a b c 2>err &&
1023 test_cmp expect err
1026 test_expect_success '--unset-upstream should fail on detached HEAD' '
1027 git checkout HEAD^{} &&
1028 test_when_finished git checkout - &&
1029 echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect &&
1030 test_must_fail git branch --unset-upstream 2>err &&
1031 test_cmp expect err
1034 test_expect_success 'test --unset-upstream on a particular branch' '
1035 git branch my15 &&
1036 git branch --set-upstream-to main my14 &&
1037 git branch --unset-upstream my14 &&
1038 test_must_fail git config branch.my14.remote &&
1039 test_must_fail git config branch.my14.merge
1042 test_expect_success 'disabled option --set-upstream fails' '
1043 test_must_fail git branch --set-upstream origin/main
1046 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
1047 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1048 cat >expect <<-\EOF &&
1049 warning: not setting branch 'my13' as its own upstream
1051 test_expect_code 1 git config branch.my13.remote &&
1052 test_expect_code 1 git config branch.my13.merge &&
1053 test_cmp expect actual
1056 # Keep this test last, as it changes the current branch
1057 cat >expect <<EOF
1058 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
1060 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
1061 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1062 git checkout -b g/h/i -l main &&
1063 test_path_is_file .git/refs/heads/g/h/i &&
1064 test_path_is_file .git/logs/refs/heads/g/h/i &&
1065 test_cmp expect .git/logs/refs/heads/g/h/i
1068 test_expect_success 'checkout -b makes reflog by default' '
1069 git checkout main &&
1070 git config --unset core.logAllRefUpdates &&
1071 git checkout -b alpha &&
1072 git rev-parse --verify alpha@{0}
1075 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1076 git checkout main &&
1077 git config core.logAllRefUpdates false &&
1078 git checkout -b beta &&
1079 test_must_fail git rev-parse --verify beta@{0}
1082 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1083 git checkout main &&
1084 git checkout -lb gamma &&
1085 git config --unset core.logAllRefUpdates &&
1086 git rev-parse --verify gamma@{0}
1089 test_expect_success 'avoid ambiguous track and advise' '
1090 git config branch.autosetupmerge true &&
1091 git config remote.ambi1.url lalala &&
1092 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1093 git config remote.ambi2.url lilili &&
1094 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1095 cat <<-EOF >expected &&
1096 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1097 hint: There are multiple remotes whose fetch refspecs map to the remote
1098 hint: tracking ref '\''refs/heads/main'\'':
1099 hint: ambi1
1100 hint: ambi2
1101 hint: ''
1102 hint: This is typically a configuration error.
1103 hint: ''
1104 hint: To support setting up tracking branches, ensure that
1105 hint: different remotes'\'' fetch refspecs map into different
1106 hint: tracking namespaces.
1108 test_must_fail git branch all1 main 2>actual &&
1109 test_cmp expected actual &&
1110 test -z "$(git config branch.all1.merge)"
1113 test_expect_success 'autosetuprebase local on a tracked local branch' '
1114 git config remote.local.url . &&
1115 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1116 git config branch.autosetuprebase local &&
1117 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1118 git branch mybase &&
1119 git branch --track myr1 mybase &&
1120 test "$(git config branch.myr1.remote)" = . &&
1121 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1122 test "$(git config branch.myr1.rebase)" = true
1125 test_expect_success 'autosetuprebase always on a tracked local branch' '
1126 git config remote.local.url . &&
1127 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1128 git config branch.autosetuprebase always &&
1129 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1130 git branch mybase2 &&
1131 git branch --track myr2 mybase &&
1132 test "$(git config branch.myr2.remote)" = . &&
1133 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1134 test "$(git config branch.myr2.rebase)" = true
1137 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1138 git config remote.local.url . &&
1139 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1140 git config branch.autosetuprebase remote &&
1141 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1142 git branch mybase3 &&
1143 git branch --track myr3 mybase2 &&
1144 test "$(git config branch.myr3.remote)" = . &&
1145 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1146 ! test "$(git config branch.myr3.rebase)" = true
1149 test_expect_success 'autosetuprebase never on a tracked local branch' '
1150 git config remote.local.url . &&
1151 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1152 git config branch.autosetuprebase never &&
1153 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1154 git branch mybase4 &&
1155 git branch --track myr4 mybase2 &&
1156 test "$(git config branch.myr4.remote)" = . &&
1157 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1158 ! test "$(git config branch.myr4.rebase)" = true
1161 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1162 git config remote.local.url . &&
1163 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1164 git config branch.autosetuprebase local &&
1165 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1166 git branch --track myr5 local/main &&
1167 test "$(git config branch.myr5.remote)" = local &&
1168 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1169 ! test "$(git config branch.myr5.rebase)" = true
1172 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1173 git config remote.local.url . &&
1174 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1175 git config branch.autosetuprebase never &&
1176 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1177 git branch --track myr6 local/main &&
1178 test "$(git config branch.myr6.remote)" = local &&
1179 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1180 ! test "$(git config branch.myr6.rebase)" = true
1183 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1184 git config remote.local.url . &&
1185 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1186 git config branch.autosetuprebase remote &&
1187 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1188 git branch --track myr7 local/main &&
1189 test "$(git config branch.myr7.remote)" = local &&
1190 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1191 test "$(git config branch.myr7.rebase)" = true
1194 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1195 git config remote.local.url . &&
1196 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1197 git config branch.autosetuprebase remote &&
1198 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1199 git branch --track myr8 local/main &&
1200 test "$(git config branch.myr8.remote)" = local &&
1201 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1202 test "$(git config branch.myr8.rebase)" = true
1205 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1206 git config --unset branch.autosetuprebase &&
1207 git config remote.local.url . &&
1208 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1209 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1210 git branch --track myr9 local/main &&
1211 test "$(git config branch.myr9.remote)" = local &&
1212 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1213 test "z$(git config branch.myr9.rebase)" = z
1216 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1217 git config remote.local.url . &&
1218 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1219 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1220 git branch mybase10 &&
1221 git branch --track myr10 mybase2 &&
1222 test "$(git config branch.myr10.remote)" = . &&
1223 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1224 test "z$(git config branch.myr10.rebase)" = z
1227 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1228 git config remote.local.url . &&
1229 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1230 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1231 git branch --no-track myr11 mybase2 &&
1232 test "z$(git config branch.myr11.remote)" = z &&
1233 test "z$(git config branch.myr11.merge)" = z &&
1234 test "z$(git config branch.myr11.rebase)" = z
1237 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1238 git config remote.local.url . &&
1239 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1240 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1241 git branch --no-track myr12 local/main &&
1242 test "z$(git config branch.myr12.remote)" = z &&
1243 test "z$(git config branch.myr12.merge)" = z &&
1244 test "z$(git config branch.myr12.rebase)" = z
1247 test_expect_success 'autosetuprebase never on an untracked local branch' '
1248 git config branch.autosetuprebase never &&
1249 git config remote.local.url . &&
1250 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1251 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1252 git branch --no-track myr13 mybase2 &&
1253 test "z$(git config branch.myr13.remote)" = z &&
1254 test "z$(git config branch.myr13.merge)" = z &&
1255 test "z$(git config branch.myr13.rebase)" = z
1258 test_expect_success 'autosetuprebase local on an untracked local branch' '
1259 git config branch.autosetuprebase local &&
1260 git config remote.local.url . &&
1261 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1262 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1263 git branch --no-track myr14 mybase2 &&
1264 test "z$(git config branch.myr14.remote)" = z &&
1265 test "z$(git config branch.myr14.merge)" = z &&
1266 test "z$(git config branch.myr14.rebase)" = z
1269 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1270 git config branch.autosetuprebase remote &&
1271 git config remote.local.url . &&
1272 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1273 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1274 git branch --no-track myr15 mybase2 &&
1275 test "z$(git config branch.myr15.remote)" = z &&
1276 test "z$(git config branch.myr15.merge)" = z &&
1277 test "z$(git config branch.myr15.rebase)" = z
1280 test_expect_success 'autosetuprebase always on an untracked local branch' '
1281 git config branch.autosetuprebase always &&
1282 git config remote.local.url . &&
1283 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1284 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1285 git branch --no-track myr16 mybase2 &&
1286 test "z$(git config branch.myr16.remote)" = z &&
1287 test "z$(git config branch.myr16.merge)" = z &&
1288 test "z$(git config branch.myr16.rebase)" = z
1291 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1292 git config branch.autosetuprebase never &&
1293 git config remote.local.url . &&
1294 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1295 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1296 git branch --no-track myr17 local/main &&
1297 test "z$(git config branch.myr17.remote)" = z &&
1298 test "z$(git config branch.myr17.merge)" = z &&
1299 test "z$(git config branch.myr17.rebase)" = z
1302 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1303 git config branch.autosetuprebase local &&
1304 git config remote.local.url . &&
1305 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1306 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1307 git branch --no-track myr18 local/main &&
1308 test "z$(git config branch.myr18.remote)" = z &&
1309 test "z$(git config branch.myr18.merge)" = z &&
1310 test "z$(git config branch.myr18.rebase)" = z
1313 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1314 git config branch.autosetuprebase remote &&
1315 git config remote.local.url . &&
1316 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1317 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1318 git branch --no-track myr19 local/main &&
1319 test "z$(git config branch.myr19.remote)" = z &&
1320 test "z$(git config branch.myr19.merge)" = z &&
1321 test "z$(git config branch.myr19.rebase)" = z
1324 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1325 git config branch.autosetuprebase always &&
1326 git config remote.local.url . &&
1327 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1328 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1329 git branch --no-track myr20 local/main &&
1330 test "z$(git config branch.myr20.remote)" = z &&
1331 test "z$(git config branch.myr20.merge)" = z &&
1332 test "z$(git config branch.myr20.rebase)" = z
1335 test_expect_success 'autosetuprebase always on detached HEAD' '
1336 git config branch.autosetupmerge always &&
1337 test_when_finished git checkout main &&
1338 git checkout HEAD^0 &&
1339 git branch my11 &&
1340 test -z "$(git config branch.my11.remote)" &&
1341 test -z "$(git config branch.my11.merge)"
1344 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1345 git config branch.autosetuprebase garbage &&
1346 test_must_fail git branch
1349 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1350 git config --unset branch.autosetuprebase &&
1351 echo "[branch] autosetuprebase" >>.git/config &&
1352 test_must_fail git branch &&
1353 git config --unset branch.autosetuprebase
1356 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1357 git checkout my9 &&
1358 git config --unset branch.my8.merge &&
1359 test_must_fail git branch -d my8
1362 test_expect_success 'attempt to delete a branch merged to its base' '
1363 # we are on my9 which is the initial commit; traditionally
1364 # we would not have allowed deleting my8 that is not merged
1365 # to my9, but it is set to track main that already has my8
1366 git config branch.my8.merge refs/heads/main &&
1367 git branch -d my8
1370 test_expect_success 'attempt to delete a branch merged to its base' '
1371 git checkout main &&
1372 echo Third >>A &&
1373 git commit -m "Third commit" A &&
1374 git branch -t my10 my9 &&
1375 git branch -f my10 HEAD^ &&
1376 # we are on main which is at the third commit, and my10
1377 # is behind us, so traditionally we would have allowed deleting
1378 # it; but my10 is set to track my9 that is further behind.
1379 test_must_fail git branch -d my10
1382 test_expect_success 'branch --delete --force removes dangling branch' '
1383 git checkout main &&
1384 test_commit unstable &&
1385 hash=$(git rev-parse HEAD) &&
1386 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1387 git branch --no-track dangling &&
1388 mv $objpath $objpath.x &&
1389 test_when_finished "mv $objpath.x $objpath" &&
1390 git branch --delete --force dangling &&
1391 git for-each-ref refs/heads/dangling >actual &&
1392 test_must_be_empty actual
1395 test_expect_success 'use --edit-description' '
1396 EDITOR=: git branch --edit-description &&
1397 test_must_fail git config branch.main.description &&
1399 write_script editor <<-\EOF &&
1400 echo "New contents" >"$1"
1402 EDITOR=./editor git branch --edit-description &&
1403 write_script editor <<-\EOF &&
1404 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1406 EDITOR=./editor git branch --edit-description &&
1407 echo "New contents" >expect &&
1408 test_cmp expect EDITOR_OUTPUT
1411 test_expect_success 'detect typo in branch name when using --edit-description' '
1412 write_script editor <<-\EOF &&
1413 echo "New contents" >"$1"
1415 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1418 test_expect_success 'refuse --edit-description on unborn branch for now' '
1419 test_when_finished "git checkout main" &&
1420 write_script editor <<-\EOF &&
1421 echo "New contents" >"$1"
1423 git checkout --orphan unborn &&
1424 test_must_fail env EDITOR=./editor git branch --edit-description
1427 test_expect_success '--merged catches invalid object names' '
1428 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1431 test_expect_success '--list during rebase' '
1432 test_when_finished "reset_rebase" &&
1433 git checkout main &&
1434 FAKE_LINES="1 edit 2" &&
1435 export FAKE_LINES &&
1436 set_fake_editor &&
1437 git rebase -i HEAD~2 &&
1438 git branch --list >actual &&
1439 test_i18ngrep "rebasing main" actual
1442 test_expect_success '--list during rebase from detached HEAD' '
1443 test_when_finished "reset_rebase && git checkout main" &&
1444 git checkout main^0 &&
1445 oid=$(git rev-parse --short HEAD) &&
1446 FAKE_LINES="1 edit 2" &&
1447 export FAKE_LINES &&
1448 set_fake_editor &&
1449 git rebase -i HEAD~2 &&
1450 git branch --list >actual &&
1451 test_i18ngrep "rebasing detached HEAD $oid" actual
1454 test_expect_success 'tracking with unexpected .fetch refspec' '
1455 rm -rf a b c d &&
1456 git init -b main a &&
1458 cd a &&
1459 test_commit a
1460 ) &&
1461 git init -b main b &&
1463 cd b &&
1464 test_commit b
1465 ) &&
1466 git init -b main c &&
1468 cd c &&
1469 test_commit c &&
1470 git remote add a ../a &&
1471 git remote add b ../b &&
1472 git fetch --all
1473 ) &&
1474 git init -b main d &&
1476 cd d &&
1477 git remote add c ../c &&
1478 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1479 git fetch c &&
1480 git branch --track local/a/main remotes/a/main &&
1481 test "$(git config branch.local/a/main.remote)" = "c" &&
1482 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1483 git rev-parse --verify a >expect &&
1484 git rev-parse --verify local/a/main >actual &&
1485 test_cmp expect actual
1489 test_expect_success 'configured committerdate sort' '
1490 git init -b main sort &&
1492 cd sort &&
1493 git config branch.sort committerdate &&
1494 test_commit initial &&
1495 git checkout -b a &&
1496 test_commit a &&
1497 git checkout -b c &&
1498 test_commit c &&
1499 git checkout -b b &&
1500 test_commit b &&
1501 git branch >actual &&
1502 cat >expect <<-\EOF &&
1503 main
1508 test_cmp expect actual
1512 test_expect_success 'option override configured sort' '
1514 cd sort &&
1515 git config branch.sort committerdate &&
1516 git branch --sort=refname >actual &&
1517 cat >expect <<-\EOF &&
1521 main
1523 test_cmp expect actual
1527 test_expect_success 'invalid sort parameter in configuration' '
1529 cd sort &&
1530 git config branch.sort "v:notvalid" &&
1532 # this works in the "listing" mode, so bad sort key
1533 # is a dying offence.
1534 test_must_fail git branch &&
1536 # these do not need to use sorting, and should all
1537 # succeed
1538 git branch newone main &&
1539 git branch -c newone newerone &&
1540 git branch -m newone newestone &&
1541 git branch -d newerone newestone
1545 test_expect_success 'tracking info copied with --track=inherit' '
1546 git branch --track=inherit foo2 my1 &&
1547 test_cmp_config local branch.foo2.remote &&
1548 test_cmp_config refs/heads/main branch.foo2.merge
1551 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1552 test_unconfig branch.autoSetupMerge &&
1553 # default config does not copy tracking info
1554 git branch foo-no-inherit my1 &&
1555 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1556 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1557 # with autoSetupMerge=inherit, we copy tracking info from my1
1558 test_config branch.autoSetupMerge inherit &&
1559 git branch foo3 my1 &&
1560 test_cmp_config local branch.foo3.remote &&
1561 test_cmp_config refs/heads/main branch.foo3.merge &&
1562 # no tracking info to inherit from main
1563 git branch main2 main &&
1564 test_cmp_config "" --default "" branch.main2.remote &&
1565 test_cmp_config "" --default "" branch.main2.merge
1568 test_expect_success '--track overrides branch.autoSetupMerge' '
1569 test_config branch.autoSetupMerge inherit &&
1570 git branch --track=direct foo4 my1 &&
1571 test_cmp_config . branch.foo4.remote &&
1572 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1573 git branch --no-track foo5 my1 &&
1574 test_cmp_config "" --default "" branch.foo5.remote &&
1575 test_cmp_config "" --default "" branch.foo5.merge
1578 test_done