Sync with 2.38.5
[git.git] / t / t3200-branch.sh
blob5a169b68d6af4d3db62e88dcdc4fb765ee5232a6
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 -d on orphan HEAD (merged)' '
283 test_when_finished git checkout main &&
284 git checkout --orphan orphan &&
285 test_when_finished "rm -rf .git/objects/commit-graph*" &&
286 git commit-graph write --reachable &&
287 git branch --track to-delete main &&
288 git branch -d to-delete
291 test_expect_success 'git branch -d on orphan HEAD (merged, graph)' '
292 test_when_finished git checkout main &&
293 git checkout --orphan orphan &&
294 git branch --track to-delete main &&
295 git branch -d to-delete
298 test_expect_success 'git branch -d on orphan HEAD (unmerged)' '
299 test_when_finished git checkout main &&
300 git checkout --orphan orphan &&
301 test_when_finished "git branch -D to-delete" &&
302 git branch to-delete main &&
303 test_must_fail git branch -d to-delete 2>err &&
304 grep "not fully merged" err
307 test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' '
308 test_when_finished git checkout main &&
309 git checkout --orphan orphan &&
310 test_when_finished "git branch -D to-delete" &&
311 git branch to-delete main &&
312 test_when_finished "rm -rf .git/objects/commit-graph*" &&
313 git commit-graph write --reachable &&
314 test_must_fail git branch -d to-delete 2>err &&
315 grep "not fully merged" err
318 test_expect_success 'git branch -v -d t should work' '
319 git branch t &&
320 git rev-parse --verify refs/heads/t &&
321 git branch -v -d t &&
322 test_must_fail git rev-parse --verify refs/heads/t
325 test_expect_success 'git branch -v -m t s should work' '
326 git branch t &&
327 git rev-parse --verify refs/heads/t &&
328 git branch -v -m t s &&
329 test_must_fail git rev-parse --verify refs/heads/t &&
330 git rev-parse --verify refs/heads/s &&
331 git branch -d s
334 test_expect_success 'git branch -m -d t s should fail' '
335 git branch t &&
336 git rev-parse refs/heads/t &&
337 test_must_fail git branch -m -d t s &&
338 git branch -d t &&
339 test_must_fail git rev-parse refs/heads/t
342 test_expect_success 'git branch --list -d t should fail' '
343 git branch t &&
344 git rev-parse refs/heads/t &&
345 test_must_fail git branch --list -d t &&
346 git branch -d t &&
347 test_must_fail git rev-parse refs/heads/t
350 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
351 test_when_finished "rm -rf repo1 repo2" &&
353 git init repo1 &&
354 git init repo1/sub &&
355 test_commit -C repo1/sub x &&
356 test_config_global protocol.file.allow always &&
357 git -C repo1 submodule add ./sub &&
358 git -C repo1 commit -m "adding sub" &&
360 git clone --recurse-submodules repo1 repo2 &&
361 git -C repo2/sub checkout -b work &&
362 test_must_fail git -C repo2/sub branch -D work
365 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
366 test_when_finished "rm -rf nonbare base secondary" &&
368 git init -b main nonbare &&
369 test_commit -C nonbare x &&
370 git clone --bare nonbare bare &&
371 git -C bare worktree add --detach ../secondary main &&
372 git -C secondary branch -D main
375 test_expect_success 'git branch --list -v with --abbrev' '
376 test_when_finished "git branch -D t" &&
377 git branch t &&
378 git branch -v --list t >actual.default &&
379 git branch -v --list --abbrev t >actual.abbrev &&
380 test_cmp actual.default actual.abbrev &&
382 git branch -v --list --no-abbrev t >actual.noabbrev &&
383 git branch -v --list --abbrev=0 t >actual.0abbrev &&
384 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
385 test_cmp actual.noabbrev actual.0abbrev &&
386 test_cmp actual.noabbrev actual.noabbrev-conf &&
388 git branch -v --list --abbrev=36 t >actual.36abbrev &&
389 # how many hexdigits are used?
390 read name objdefault rest <actual.abbrev &&
391 read name obj36 rest <actual.36abbrev &&
392 objfull=$(git rev-parse --verify t) &&
394 # are we really getting abbreviations?
395 test "$obj36" != "$objdefault" &&
396 expr "$obj36" : "$objdefault" >/dev/null &&
397 test "$objfull" != "$obj36" &&
398 expr "$objfull" : "$obj36" >/dev/null
402 test_expect_success 'git branch --column' '
403 COLUMNS=81 git branch --column=column >actual &&
404 cat >expect <<\EOF &&
405 a/b/c bam foo l * main n o/p r
406 abc bar j/k m/m mb o/o q topic
408 test_cmp expect actual
411 test_expect_success 'git branch --column with an extremely long branch name' '
412 long=this/is/a/part/of/long/branch/name &&
413 long=z$long/$long/$long/$long &&
414 test_when_finished "git branch -d $long" &&
415 git branch $long &&
416 COLUMNS=80 git branch --column=column >actual &&
417 cat >expect <<EOF &&
418 a/b/c
426 * main
433 topic
434 $long
436 test_cmp expect actual
439 test_expect_success 'git branch with column.*' '
440 git config column.ui column &&
441 git config column.branch "dense" &&
442 COLUMNS=80 git branch >actual &&
443 git config --unset column.branch &&
444 git config --unset column.ui &&
445 cat >expect <<\EOF &&
446 a/b/c bam foo l * main n o/p r
447 abc bar j/k m/m mb o/o q topic
449 test_cmp expect actual
452 test_expect_success 'git branch --column -v should fail' '
453 test_must_fail git branch --column -v
456 test_expect_success 'git branch -v with column.ui ignored' '
457 git config column.ui column &&
458 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
459 git config --unset column.ui &&
460 cat >expect <<\EOF &&
461 a/b/c
469 * main
476 topic
478 test_cmp expect actual
481 mv .git/config .git/config-saved
483 test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
484 git branch -m q q2 &&
485 git branch -m q2 q
488 mv .git/config-saved .git/config
490 git config branch.s/s.dummy Hello
492 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
493 git branch --create-reflog s/s &&
494 git reflog exists refs/heads/s/s &&
495 git branch --create-reflog s/t &&
496 git reflog exists refs/heads/s/t &&
497 git branch -d s/t &&
498 git branch -m s/s s &&
499 git reflog exists refs/heads/s
502 test_expect_success 'config information was renamed, too' '
503 test $(git config branch.s.dummy) = Hello &&
504 test_must_fail git config branch.s/s.dummy
507 test_expect_success 'git branch -m correctly renames multiple config sections' '
508 test_when_finished "git checkout main" &&
509 git checkout -b source main &&
511 # Assert that a config file with multiple config sections has
512 # those sections preserved...
513 cat >expect <<-\EOF &&
514 branch.dest.key1=value1
515 some.gar.b=age
516 branch.dest.key2=value2
518 cat >config.branch <<\EOF &&
519 ;; Note the lack of -\EOF above & mixed indenting here. This is
520 ;; intentional, we are also testing that the formatting of copied
521 ;; sections is preserved.
523 ;; Comment for source. Tabs
524 [branch "source"]
525 ;; Comment for the source value
526 key1 = value1
527 ;; Comment for some.gar. Spaces
528 [some "gar"]
529 ;; Comment for the some.gar value
530 b = age
531 ;; Comment for source, again. Mixed tabs/spaces.
532 [branch "source"]
533 ;; Comment for the source value, again
534 key2 = value2
536 cat config.branch >>.git/config &&
537 git branch -m source dest &&
538 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
539 test_cmp expect actual &&
541 # ...and that the comments for those sections are also
542 # preserved.
543 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
544 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
545 test_cmp expect actual
548 test_expect_success 'git branch -c dumps usage' '
549 test_expect_code 128 git branch -c 2>err &&
550 test_i18ngrep "branch name required" err
553 test_expect_success 'git branch --copy dumps usage' '
554 test_expect_code 128 git branch --copy 2>err &&
555 test_i18ngrep "branch name required" err
558 test_expect_success 'git branch -c d e should work' '
559 git branch --create-reflog d &&
560 git reflog exists refs/heads/d &&
561 git config branch.d.dummy Hello &&
562 git branch -c d e &&
563 git reflog exists refs/heads/d &&
564 git reflog exists refs/heads/e &&
565 echo Hello >expect &&
566 git config branch.e.dummy >actual &&
567 test_cmp expect actual &&
568 echo Hello >expect &&
569 git config branch.d.dummy >actual &&
570 test_cmp expect actual
573 test_expect_success 'git branch --copy is a synonym for -c' '
574 git branch --create-reflog copy &&
575 git reflog exists refs/heads/copy &&
576 git config branch.copy.dummy Hello &&
577 git branch --copy copy copy-to &&
578 git reflog exists refs/heads/copy &&
579 git reflog exists refs/heads/copy-to &&
580 echo Hello >expect &&
581 git config branch.copy.dummy >actual &&
582 test_cmp expect actual &&
583 echo Hello >expect &&
584 git config branch.copy-to.dummy >actual &&
585 test_cmp expect actual
588 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
589 git checkout -b ee &&
590 git reflog exists refs/heads/ee &&
591 git config branch.ee.dummy Hello &&
592 git branch -c ee ef &&
593 git reflog exists refs/heads/ee &&
594 git reflog exists refs/heads/ef &&
595 test $(git config branch.ee.dummy) = Hello &&
596 test $(git config branch.ef.dummy) = Hello &&
597 test $(git rev-parse --abbrev-ref HEAD) = ee
600 test_expect_success 'git branch -c f/f g/g should work' '
601 git branch --create-reflog f/f &&
602 git reflog exists refs/heads/f/f &&
603 git config branch.f/f.dummy Hello &&
604 git branch -c f/f g/g &&
605 git reflog exists refs/heads/f/f &&
606 git reflog exists refs/heads/g/g &&
607 test $(git config branch.f/f.dummy) = Hello &&
608 test $(git config branch.g/g.dummy) = Hello
611 test_expect_success 'git branch -c m2 m2 should work' '
612 git branch --create-reflog m2 &&
613 git reflog exists refs/heads/m2 &&
614 git config branch.m2.dummy Hello &&
615 git branch -c m2 m2 &&
616 git reflog exists refs/heads/m2 &&
617 test $(git config branch.m2.dummy) = Hello
620 test_expect_success 'git branch -c zz zz/zz should fail' '
621 git branch --create-reflog zz &&
622 git reflog exists refs/heads/zz &&
623 test_must_fail git branch -c zz zz/zz
626 test_expect_success 'git branch -c b/b b should fail' '
627 git branch --create-reflog b/b &&
628 test_must_fail git branch -c b/b b
631 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
632 git branch --create-reflog o/q &&
633 git reflog exists refs/heads/o/q &&
634 git reflog exists refs/heads/o/p &&
635 git branch -C o/q o/p
638 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
639 git reflog exists refs/heads/o/q &&
640 git reflog exists refs/heads/o/p &&
641 git branch -c -f o/q o/p
644 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
645 git branch qq &&
646 git branch rr &&
647 test_must_fail git branch -c qq rr/qq
650 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
651 git branch b1 &&
652 git checkout -b b2 &&
653 test_must_fail git branch -C b1 b2
656 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
657 git checkout -b c1 &&
658 git branch c2 &&
659 git branch -C c1 c2 &&
660 test $(git rev-parse --abbrev-ref HEAD) = c1
663 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
664 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
665 ! grep "$msg$" .git/logs/HEAD
668 test_expect_success 'git branch -C main should work when main is checked out' '
669 git checkout main &&
670 git branch -C main
673 test_expect_success 'git branch -C main main should work when main is checked out' '
674 git checkout main &&
675 git branch -C main main
678 test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
679 git checkout main &&
680 git branch main5 &&
681 git branch -C main5 main5
684 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
685 git branch --create-reflog cd &&
686 git reflog exists refs/heads/cd &&
687 git config branch.cd.dummy CD &&
688 git branch --create-reflog ab &&
689 git reflog exists refs/heads/ab &&
690 git config branch.ab.dummy AB &&
691 git branch -C ab cd &&
692 git reflog exists refs/heads/ab &&
693 git reflog exists refs/heads/cd &&
694 test $(git config branch.ab.dummy) = AB &&
695 test $(git config branch.cd.dummy) = AB
698 test_expect_success 'git branch -c correctly copies multiple config sections' '
699 FOO=1 &&
700 export FOO &&
701 test_when_finished "git checkout main" &&
702 git checkout -b source2 main &&
704 # Assert that a config file with multiple config sections has
705 # those sections preserved...
706 cat >expect <<-\EOF &&
707 branch.source2.key1=value1
708 branch.dest2.key1=value1
709 more.gar.b=age
710 branch.source2.key2=value2
711 branch.dest2.key2=value2
713 cat >config.branch <<\EOF &&
714 ;; Note the lack of -\EOF above & mixed indenting here. This is
715 ;; intentional, we are also testing that the formatting of copied
716 ;; sections is preserved.
718 ;; Comment for source2. Tabs
719 [branch "source2"]
720 ;; Comment for the source2 value
721 key1 = value1
722 ;; Comment for more.gar. Spaces
723 [more "gar"]
724 ;; Comment for the more.gar value
725 b = age
726 ;; Comment for source2, again. Mixed tabs/spaces.
727 [branch "source2"]
728 ;; Comment for the source2 value, again
729 key2 = value2
731 cat config.branch >>.git/config &&
732 git branch -c source2 dest2 &&
733 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
734 test_cmp expect actual &&
736 # ...and that the comments and formatting for those sections
737 # is also preserved.
738 cat >expect <<\EOF &&
739 ;; Comment for source2. Tabs
740 [branch "source2"]
741 ;; Comment for the source2 value
742 key1 = value1
743 ;; Comment for more.gar. Spaces
744 [branch "dest2"]
745 ;; Comment for the source2 value
746 key1 = value1
747 ;; Comment for more.gar. Spaces
748 [more "gar"]
749 ;; Comment for the more.gar value
750 b = age
751 ;; Comment for source2, again. Mixed tabs/spaces.
752 [branch "source2"]
753 ;; Comment for the source2 value, again
754 key2 = value2
755 [branch "dest2"]
756 ;; Comment for the source2 value, again
757 key2 = value2
759 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
760 test_cmp expect actual
763 test_expect_success 'deleting a symref' '
764 git branch target &&
765 git symbolic-ref refs/heads/symref refs/heads/target &&
766 echo "Deleted branch symref (was refs/heads/target)." >expect &&
767 git branch -d symref >actual &&
768 test_path_is_file .git/refs/heads/target &&
769 test_path_is_missing .git/refs/heads/symref &&
770 test_cmp expect actual
773 test_expect_success 'deleting a dangling symref' '
774 git symbolic-ref refs/heads/dangling-symref nowhere &&
775 test_path_is_file .git/refs/heads/dangling-symref &&
776 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
777 git branch -d dangling-symref >actual &&
778 test_path_is_missing .git/refs/heads/dangling-symref &&
779 test_cmp expect actual
782 test_expect_success 'deleting a self-referential symref' '
783 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
784 test_path_is_file .git/refs/heads/self-reference &&
785 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
786 git branch -d self-reference >actual &&
787 test_path_is_missing .git/refs/heads/self-reference &&
788 test_cmp expect actual
791 test_expect_success 'renaming a symref is not allowed' '
792 git symbolic-ref refs/heads/topic refs/heads/main &&
793 test_must_fail git branch -m topic new-topic &&
794 git symbolic-ref refs/heads/topic &&
795 test_path_is_file .git/refs/heads/main &&
796 test_path_is_missing .git/refs/heads/new-topic
799 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
800 git branch --create-reflog u &&
801 mv .git/logs/refs/heads/u real-u &&
802 ln -s real-u .git/logs/refs/heads/u &&
803 test_must_fail git branch -m u v
806 test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' '
807 test_when_finished "rm -rf subdir" &&
808 git init --bare subdir &&
810 rm -rfv subdir/refs subdir/objects subdir/packed-refs &&
811 ln -s ../.git/refs subdir/refs &&
812 ln -s ../.git/objects subdir/objects &&
813 ln -s ../.git/packed-refs subdir/packed-refs &&
815 git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
816 git rev-parse --absolute-git-dir >our.dir &&
817 ! test_cmp subdir.dir our.dir &&
819 git -C subdir log &&
820 git -C subdir branch rename-src &&
821 git rev-parse rename-src >expect &&
822 git -C subdir branch -m rename-src rename-dest &&
823 git rev-parse rename-dest >actual &&
824 test_cmp expect actual &&
825 git branch -D rename-dest
828 test_expect_success 'test tracking setup via --track' '
829 git config remote.local.url . &&
830 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
831 (git show-ref -q refs/remotes/local/main || git fetch local) &&
832 git branch --track my1 local/main &&
833 test $(git config branch.my1.remote) = local &&
834 test $(git config branch.my1.merge) = refs/heads/main
837 test_expect_success 'test tracking setup (non-wildcard, matching)' '
838 git config remote.local.url . &&
839 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
840 (git show-ref -q refs/remotes/local/main || git fetch local) &&
841 git branch --track my4 local/main &&
842 test $(git config branch.my4.remote) = local &&
843 test $(git config branch.my4.merge) = refs/heads/main
846 test_expect_success 'tracking setup fails on non-matching refspec' '
847 git config remote.local.url . &&
848 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
849 (git show-ref -q refs/remotes/local/main || git fetch local) &&
850 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
851 test_must_fail git branch --track my5 local/main &&
852 test_must_fail git config branch.my5.remote &&
853 test_must_fail git config branch.my5.merge
856 test_expect_success 'test tracking setup via config' '
857 git config branch.autosetupmerge true &&
858 git config remote.local.url . &&
859 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
860 (git show-ref -q refs/remotes/local/main || git fetch local) &&
861 git branch my3 local/main &&
862 test $(git config branch.my3.remote) = local &&
863 test $(git config branch.my3.merge) = refs/heads/main
866 test_expect_success 'test overriding tracking setup via --no-track' '
867 git config branch.autosetupmerge true &&
868 git config remote.local.url . &&
869 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
870 (git show-ref -q refs/remotes/local/main || git fetch local) &&
871 git branch --no-track my2 local/main &&
872 git config branch.autosetupmerge false &&
873 ! test "$(git config branch.my2.remote)" = local &&
874 ! test "$(git config branch.my2.merge)" = refs/heads/main
877 test_expect_success 'no tracking without .fetch entries' '
878 git config branch.autosetupmerge true &&
879 git branch my6 s &&
880 git config branch.autosetupmerge false &&
881 test -z "$(git config branch.my6.remote)" &&
882 test -z "$(git config branch.my6.merge)"
885 test_expect_success 'test tracking setup via --track but deeper' '
886 git config remote.local.url . &&
887 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
888 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
889 git branch --track my7 local/o/o &&
890 test "$(git config branch.my7.remote)" = local &&
891 test "$(git config branch.my7.merge)" = refs/heads/o/o
894 test_expect_success 'test deleting branch deletes branch config' '
895 git branch -d my7 &&
896 test -z "$(git config branch.my7.remote)" &&
897 test -z "$(git config branch.my7.merge)"
900 test_expect_success 'test deleting branch without config' '
901 git branch my7 s &&
902 sha1=$(git rev-parse my7 | cut -c 1-7) &&
903 echo "Deleted branch my7 (was $sha1)." >expect &&
904 git branch -d my7 >actual 2>&1 &&
905 test_cmp expect actual
908 test_expect_success 'deleting currently checked out branch fails' '
909 git worktree add -b my7 my7 &&
910 test_must_fail git -C my7 branch -d my7 &&
911 test_must_fail git branch -d my7 &&
912 rm -r my7 &&
913 git worktree prune
916 test_expect_success 'test --track without .fetch entries' '
917 git branch --track my8 &&
918 test "$(git config branch.my8.remote)" &&
919 test "$(git config branch.my8.merge)"
922 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
923 git config branch.autosetupmerge always &&
924 git branch my9 HEAD^ &&
925 git config branch.autosetupmerge false
928 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
929 test_must_fail git branch --track my10 HEAD^
932 test_expect_success 'branch from tag w/--track causes failure' '
933 git tag foobar &&
934 test_must_fail git branch --track my11 foobar
937 test_expect_success 'simple tracking works when remote branch name matches' '
938 test_when_finished "rm -rf otherserver" &&
939 git init otherserver &&
940 test_commit -C otherserver my_commit 1 &&
941 git -C otherserver branch feature &&
942 test_config branch.autosetupmerge simple &&
943 test_config remote.otherserver.url otherserver &&
944 test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* &&
945 git fetch otherserver &&
946 git branch feature otherserver/feature &&
947 test_cmp_config otherserver branch.feature.remote &&
948 test_cmp_config refs/heads/feature branch.feature.merge
951 test_expect_success 'simple tracking skips when remote branch name does not match' '
952 test_config branch.autosetupmerge simple &&
953 test_config remote.local.url . &&
954 test_config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
955 git fetch local &&
956 git branch my-other local/main &&
957 test_cmp_config "" --default "" branch.my-other.remote &&
958 test_cmp_config "" --default "" branch.my-other.merge
961 test_expect_success 'simple tracking skips when remote ref is not a branch' '
962 test_config branch.autosetupmerge simple &&
963 test_config remote.localtags.url . &&
964 test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* &&
965 git tag mytag12 main &&
966 git fetch localtags &&
967 git branch mytag12 localtags/mytag12 &&
968 test_cmp_config "" --default "" branch.mytag12.remote &&
969 test_cmp_config "" --default "" branch.mytag12.merge
972 test_expect_success '--set-upstream-to fails on multiple branches' '
973 echo "fatal: too many arguments to set new upstream" >expect &&
974 test_must_fail git branch --set-upstream-to main a b c 2>err &&
975 test_cmp expect err
978 test_expect_success '--set-upstream-to fails on detached HEAD' '
979 git checkout HEAD^{} &&
980 test_when_finished git checkout - &&
981 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch." >expect &&
982 test_must_fail git branch --set-upstream-to main 2>err &&
983 test_cmp expect err
986 test_expect_success '--set-upstream-to fails on a missing dst branch' '
987 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
988 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
989 test_cmp expect err
992 test_expect_success '--set-upstream-to fails on a missing src branch' '
993 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
994 test_i18ngrep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
997 test_expect_success '--set-upstream-to fails on a non-ref' '
998 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
999 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
1000 test_cmp expect err
1003 test_expect_success '--set-upstream-to fails on locked config' '
1004 test_when_finished "rm -f .git/config.lock" &&
1005 >.git/config.lock &&
1006 git branch locked &&
1007 test_must_fail git branch --set-upstream-to locked 2>err &&
1008 test_i18ngrep "could not lock config file .git/config" err
1011 test_expect_success 'use --set-upstream-to modify HEAD' '
1012 test_config branch.main.remote foo &&
1013 test_config branch.main.merge foo &&
1014 git branch my12 &&
1015 git branch --set-upstream-to my12 &&
1016 test "$(git config branch.main.remote)" = "." &&
1017 test "$(git config branch.main.merge)" = "refs/heads/my12"
1020 test_expect_success 'use --set-upstream-to modify a particular branch' '
1021 git branch my13 &&
1022 git branch --set-upstream-to main my13 &&
1023 test_when_finished "git branch --unset-upstream my13" &&
1024 test "$(git config branch.my13.remote)" = "." &&
1025 test "$(git config branch.my13.merge)" = "refs/heads/main"
1028 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
1029 echo "fatal: Branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
1030 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
1031 test_cmp expect err
1034 test_expect_success '--unset-upstream should fail if config is locked' '
1035 test_when_finished "rm -f .git/config.lock" &&
1036 git branch --set-upstream-to locked &&
1037 >.git/config.lock &&
1038 test_must_fail git branch --unset-upstream 2>err &&
1039 test_i18ngrep "could not lock config file .git/config" err
1042 test_expect_success 'test --unset-upstream on HEAD' '
1043 git branch my14 &&
1044 test_config branch.main.remote foo &&
1045 test_config branch.main.merge foo &&
1046 git branch --set-upstream-to my14 &&
1047 git branch --unset-upstream &&
1048 test_must_fail git config branch.main.remote &&
1049 test_must_fail git config branch.main.merge &&
1050 # fail for a branch without upstream set
1051 echo "fatal: Branch '"'"'main'"'"' has no upstream information" >expect &&
1052 test_must_fail git branch --unset-upstream 2>err &&
1053 test_cmp expect err
1056 test_expect_success '--unset-upstream should fail on multiple branches' '
1057 echo "fatal: too many arguments to unset upstream" >expect &&
1058 test_must_fail git branch --unset-upstream a b c 2>err &&
1059 test_cmp expect err
1062 test_expect_success '--unset-upstream should fail on detached HEAD' '
1063 git checkout HEAD^{} &&
1064 test_when_finished git checkout - &&
1065 echo "fatal: could not unset upstream of HEAD when it does not point to any branch." >expect &&
1066 test_must_fail git branch --unset-upstream 2>err &&
1067 test_cmp expect err
1070 test_expect_success 'test --unset-upstream on a particular branch' '
1071 git branch my15 &&
1072 git branch --set-upstream-to main my14 &&
1073 git branch --unset-upstream my14 &&
1074 test_must_fail git config branch.my14.remote &&
1075 test_must_fail git config branch.my14.merge
1078 test_expect_success 'disabled option --set-upstream fails' '
1079 test_must_fail git branch --set-upstream origin/main
1082 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
1083 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1084 cat >expect <<-\EOF &&
1085 warning: not setting branch 'my13' as its own upstream
1087 test_expect_code 1 git config branch.my13.remote &&
1088 test_expect_code 1 git config branch.my13.merge &&
1089 test_cmp expect actual
1092 # Keep this test last, as it changes the current branch
1093 cat >expect <<EOF
1094 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
1096 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
1097 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1098 git checkout -b g/h/i -l main &&
1099 test_path_is_file .git/refs/heads/g/h/i &&
1100 test_path_is_file .git/logs/refs/heads/g/h/i &&
1101 test_cmp expect .git/logs/refs/heads/g/h/i
1104 test_expect_success 'checkout -b makes reflog by default' '
1105 git checkout main &&
1106 git config --unset core.logAllRefUpdates &&
1107 git checkout -b alpha &&
1108 git rev-parse --verify alpha@{0}
1111 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1112 git checkout main &&
1113 git config core.logAllRefUpdates false &&
1114 git checkout -b beta &&
1115 test_must_fail git rev-parse --verify beta@{0}
1118 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1119 git checkout main &&
1120 git checkout -lb gamma &&
1121 git config --unset core.logAllRefUpdates &&
1122 git rev-parse --verify gamma@{0}
1125 test_expect_success 'avoid ambiguous track and advise' '
1126 git config branch.autosetupmerge true &&
1127 git config remote.ambi1.url lalala &&
1128 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1129 git config remote.ambi2.url lilili &&
1130 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1131 cat <<-EOF >expected &&
1132 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1133 hint: There are multiple remotes whose fetch refspecs map to the remote
1134 hint: tracking ref '\''refs/heads/main'\'':
1135 hint: ambi1
1136 hint: ambi2
1137 hint: ''
1138 hint: This is typically a configuration error.
1139 hint: ''
1140 hint: To support setting up tracking branches, ensure that
1141 hint: different remotes'\'' fetch refspecs map into different
1142 hint: tracking namespaces.
1144 test_must_fail git branch all1 main 2>actual &&
1145 test_cmp expected actual &&
1146 test -z "$(git config branch.all1.merge)"
1149 test_expect_success 'autosetuprebase local 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 local &&
1153 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1154 git branch mybase &&
1155 git branch --track myr1 mybase &&
1156 test "$(git config branch.myr1.remote)" = . &&
1157 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1158 test "$(git config branch.myr1.rebase)" = true
1161 test_expect_success 'autosetuprebase always on a tracked local branch' '
1162 git config remote.local.url . &&
1163 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1164 git config branch.autosetuprebase always &&
1165 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1166 git branch mybase2 &&
1167 git branch --track myr2 mybase &&
1168 test "$(git config branch.myr2.remote)" = . &&
1169 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1170 test "$(git config branch.myr2.rebase)" = true
1173 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1174 git config remote.local.url . &&
1175 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1176 git config branch.autosetuprebase remote &&
1177 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1178 git branch mybase3 &&
1179 git branch --track myr3 mybase2 &&
1180 test "$(git config branch.myr3.remote)" = . &&
1181 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1182 ! test "$(git config branch.myr3.rebase)" = true
1185 test_expect_success 'autosetuprebase never on a tracked local branch' '
1186 git config remote.local.url . &&
1187 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1188 git config branch.autosetuprebase never &&
1189 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1190 git branch mybase4 &&
1191 git branch --track myr4 mybase2 &&
1192 test "$(git config branch.myr4.remote)" = . &&
1193 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1194 ! test "$(git config branch.myr4.rebase)" = true
1197 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1198 git config remote.local.url . &&
1199 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1200 git config branch.autosetuprebase local &&
1201 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1202 git branch --track myr5 local/main &&
1203 test "$(git config branch.myr5.remote)" = local &&
1204 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1205 ! test "$(git config branch.myr5.rebase)" = true
1208 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1209 git config remote.local.url . &&
1210 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1211 git config branch.autosetuprebase never &&
1212 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1213 git branch --track myr6 local/main &&
1214 test "$(git config branch.myr6.remote)" = local &&
1215 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1216 ! test "$(git config branch.myr6.rebase)" = true
1219 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1220 git config remote.local.url . &&
1221 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1222 git config branch.autosetuprebase remote &&
1223 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1224 git branch --track myr7 local/main &&
1225 test "$(git config branch.myr7.remote)" = local &&
1226 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1227 test "$(git config branch.myr7.rebase)" = true
1230 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1231 git config remote.local.url . &&
1232 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1233 git config branch.autosetuprebase remote &&
1234 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1235 git branch --track myr8 local/main &&
1236 test "$(git config branch.myr8.remote)" = local &&
1237 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1238 test "$(git config branch.myr8.rebase)" = true
1241 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1242 git config --unset branch.autosetuprebase &&
1243 git config remote.local.url . &&
1244 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1245 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1246 git branch --track myr9 local/main &&
1247 test "$(git config branch.myr9.remote)" = local &&
1248 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1249 test "z$(git config branch.myr9.rebase)" = z
1252 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1253 git config remote.local.url . &&
1254 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1255 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1256 git branch mybase10 &&
1257 git branch --track myr10 mybase2 &&
1258 test "$(git config branch.myr10.remote)" = . &&
1259 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1260 test "z$(git config branch.myr10.rebase)" = z
1263 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1264 git config remote.local.url . &&
1265 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1266 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1267 git branch --no-track myr11 mybase2 &&
1268 test "z$(git config branch.myr11.remote)" = z &&
1269 test "z$(git config branch.myr11.merge)" = z &&
1270 test "z$(git config branch.myr11.rebase)" = z
1273 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1274 git config remote.local.url . &&
1275 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1276 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1277 git branch --no-track myr12 local/main &&
1278 test "z$(git config branch.myr12.remote)" = z &&
1279 test "z$(git config branch.myr12.merge)" = z &&
1280 test "z$(git config branch.myr12.rebase)" = z
1283 test_expect_success 'autosetuprebase never on an untracked local branch' '
1284 git config branch.autosetuprebase never &&
1285 git config remote.local.url . &&
1286 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1287 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1288 git branch --no-track myr13 mybase2 &&
1289 test "z$(git config branch.myr13.remote)" = z &&
1290 test "z$(git config branch.myr13.merge)" = z &&
1291 test "z$(git config branch.myr13.rebase)" = z
1294 test_expect_success 'autosetuprebase local on an untracked local branch' '
1295 git config branch.autosetuprebase local &&
1296 git config remote.local.url . &&
1297 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1298 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1299 git branch --no-track myr14 mybase2 &&
1300 test "z$(git config branch.myr14.remote)" = z &&
1301 test "z$(git config branch.myr14.merge)" = z &&
1302 test "z$(git config branch.myr14.rebase)" = z
1305 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1306 git config branch.autosetuprebase remote &&
1307 git config remote.local.url . &&
1308 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1309 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1310 git branch --no-track myr15 mybase2 &&
1311 test "z$(git config branch.myr15.remote)" = z &&
1312 test "z$(git config branch.myr15.merge)" = z &&
1313 test "z$(git config branch.myr15.rebase)" = z
1316 test_expect_success 'autosetuprebase always on an untracked local branch' '
1317 git config branch.autosetuprebase always &&
1318 git config remote.local.url . &&
1319 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1320 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1321 git branch --no-track myr16 mybase2 &&
1322 test "z$(git config branch.myr16.remote)" = z &&
1323 test "z$(git config branch.myr16.merge)" = z &&
1324 test "z$(git config branch.myr16.rebase)" = z
1327 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1328 git config branch.autosetuprebase never &&
1329 git config remote.local.url . &&
1330 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1331 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1332 git branch --no-track myr17 local/main &&
1333 test "z$(git config branch.myr17.remote)" = z &&
1334 test "z$(git config branch.myr17.merge)" = z &&
1335 test "z$(git config branch.myr17.rebase)" = z
1338 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1339 git config branch.autosetuprebase local &&
1340 git config remote.local.url . &&
1341 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1342 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1343 git branch --no-track myr18 local/main &&
1344 test "z$(git config branch.myr18.remote)" = z &&
1345 test "z$(git config branch.myr18.merge)" = z &&
1346 test "z$(git config branch.myr18.rebase)" = z
1349 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1350 git config branch.autosetuprebase remote &&
1351 git config remote.local.url . &&
1352 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1353 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1354 git branch --no-track myr19 local/main &&
1355 test "z$(git config branch.myr19.remote)" = z &&
1356 test "z$(git config branch.myr19.merge)" = z &&
1357 test "z$(git config branch.myr19.rebase)" = z
1360 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1361 git config branch.autosetuprebase always &&
1362 git config remote.local.url . &&
1363 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1364 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1365 git branch --no-track myr20 local/main &&
1366 test "z$(git config branch.myr20.remote)" = z &&
1367 test "z$(git config branch.myr20.merge)" = z &&
1368 test "z$(git config branch.myr20.rebase)" = z
1371 test_expect_success 'autosetuprebase always on detached HEAD' '
1372 git config branch.autosetupmerge always &&
1373 test_when_finished git checkout main &&
1374 git checkout HEAD^0 &&
1375 git branch my11 &&
1376 test -z "$(git config branch.my11.remote)" &&
1377 test -z "$(git config branch.my11.merge)"
1380 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1381 git config branch.autosetuprebase garbage &&
1382 test_must_fail git branch
1385 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1386 git config --unset branch.autosetuprebase &&
1387 echo "[branch] autosetuprebase" >>.git/config &&
1388 test_must_fail git branch &&
1389 git config --unset branch.autosetuprebase
1392 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1393 git checkout my9 &&
1394 git config --unset branch.my8.merge &&
1395 test_must_fail git branch -d my8
1398 test_expect_success 'attempt to delete a branch merged to its base' '
1399 # we are on my9 which is the initial commit; traditionally
1400 # we would not have allowed deleting my8 that is not merged
1401 # to my9, but it is set to track main that already has my8
1402 git config branch.my8.merge refs/heads/main &&
1403 git branch -d my8
1406 test_expect_success 'attempt to delete a branch merged to its base' '
1407 git checkout main &&
1408 echo Third >>A &&
1409 git commit -m "Third commit" A &&
1410 git branch -t my10 my9 &&
1411 git branch -f my10 HEAD^ &&
1412 # we are on main which is at the third commit, and my10
1413 # is behind us, so traditionally we would have allowed deleting
1414 # it; but my10 is set to track my9 that is further behind.
1415 test_must_fail git branch -d my10
1418 test_expect_success 'branch --delete --force removes dangling branch' '
1419 git checkout main &&
1420 test_commit unstable &&
1421 hash=$(git rev-parse HEAD) &&
1422 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1423 git branch --no-track dangling &&
1424 mv $objpath $objpath.x &&
1425 test_when_finished "mv $objpath.x $objpath" &&
1426 git branch --delete --force dangling &&
1427 git for-each-ref refs/heads/dangling >actual &&
1428 test_must_be_empty actual
1431 test_expect_success 'use --edit-description' '
1432 EDITOR=: git branch --edit-description &&
1433 test_expect_code 1 git config branch.main.description &&
1435 write_script editor <<-\EOF &&
1436 echo "New contents" >"$1"
1438 EDITOR=./editor git branch --edit-description &&
1439 write_script editor <<-\EOF &&
1440 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1442 EDITOR=./editor git branch --edit-description &&
1443 echo "New contents" >expect &&
1444 test_cmp expect EDITOR_OUTPUT
1447 test_expect_success 'detect typo in branch name when using --edit-description' '
1448 write_script editor <<-\EOF &&
1449 echo "New contents" >"$1"
1451 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1454 test_expect_success 'refuse --edit-description on unborn branch for now' '
1455 test_when_finished "git checkout main" &&
1456 write_script editor <<-\EOF &&
1457 echo "New contents" >"$1"
1459 git checkout --orphan unborn &&
1460 test_must_fail env EDITOR=./editor git branch --edit-description
1463 test_expect_success '--merged catches invalid object names' '
1464 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1467 test_expect_success '--list during rebase' '
1468 test_when_finished "reset_rebase" &&
1469 git checkout main &&
1470 FAKE_LINES="1 edit 2" &&
1471 export FAKE_LINES &&
1472 set_fake_editor &&
1473 git rebase -i HEAD~2 &&
1474 git branch --list >actual &&
1475 test_i18ngrep "rebasing main" actual
1478 test_expect_success '--list during rebase from detached HEAD' '
1479 test_when_finished "reset_rebase && git checkout main" &&
1480 git checkout main^0 &&
1481 oid=$(git rev-parse --short HEAD) &&
1482 FAKE_LINES="1 edit 2" &&
1483 export FAKE_LINES &&
1484 set_fake_editor &&
1485 git rebase -i HEAD~2 &&
1486 git branch --list >actual &&
1487 test_i18ngrep "rebasing detached HEAD $oid" actual
1490 test_expect_success 'tracking with unexpected .fetch refspec' '
1491 rm -rf a b c d &&
1492 git init -b main a &&
1494 cd a &&
1495 test_commit a
1496 ) &&
1497 git init -b main b &&
1499 cd b &&
1500 test_commit b
1501 ) &&
1502 git init -b main c &&
1504 cd c &&
1505 test_commit c &&
1506 git remote add a ../a &&
1507 git remote add b ../b &&
1508 git fetch --all
1509 ) &&
1510 git init -b main d &&
1512 cd d &&
1513 git remote add c ../c &&
1514 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1515 git fetch c &&
1516 git branch --track local/a/main remotes/a/main &&
1517 test "$(git config branch.local/a/main.remote)" = "c" &&
1518 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1519 git rev-parse --verify a >expect &&
1520 git rev-parse --verify local/a/main >actual &&
1521 test_cmp expect actual
1525 test_expect_success 'configured committerdate sort' '
1526 git init -b main sort &&
1528 cd sort &&
1529 git config branch.sort committerdate &&
1530 test_commit initial &&
1531 git checkout -b a &&
1532 test_commit a &&
1533 git checkout -b c &&
1534 test_commit c &&
1535 git checkout -b b &&
1536 test_commit b &&
1537 git branch >actual &&
1538 cat >expect <<-\EOF &&
1539 main
1544 test_cmp expect actual
1548 test_expect_success 'option override configured sort' '
1550 cd sort &&
1551 git config branch.sort committerdate &&
1552 git branch --sort=refname >actual &&
1553 cat >expect <<-\EOF &&
1557 main
1559 test_cmp expect actual
1563 test_expect_success 'invalid sort parameter in configuration' '
1565 cd sort &&
1566 git config branch.sort "v:notvalid" &&
1568 # this works in the "listing" mode, so bad sort key
1569 # is a dying offence.
1570 test_must_fail git branch &&
1572 # these do not need to use sorting, and should all
1573 # succeed
1574 git branch newone main &&
1575 git branch -c newone newerone &&
1576 git branch -m newone newestone &&
1577 git branch -d newerone newestone
1581 test_expect_success 'tracking info copied with --track=inherit' '
1582 git branch --track=inherit foo2 my1 &&
1583 test_cmp_config local branch.foo2.remote &&
1584 test_cmp_config refs/heads/main branch.foo2.merge
1587 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1588 test_unconfig branch.autoSetupMerge &&
1589 # default config does not copy tracking info
1590 git branch foo-no-inherit my1 &&
1591 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1592 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1593 # with autoSetupMerge=inherit, we copy tracking info from my1
1594 test_config branch.autoSetupMerge inherit &&
1595 git branch foo3 my1 &&
1596 test_cmp_config local branch.foo3.remote &&
1597 test_cmp_config refs/heads/main branch.foo3.merge &&
1598 # no tracking info to inherit from main
1599 git branch main2 main &&
1600 test_cmp_config "" --default "" branch.main2.remote &&
1601 test_cmp_config "" --default "" branch.main2.merge
1604 test_expect_success '--track overrides branch.autoSetupMerge' '
1605 test_config branch.autoSetupMerge inherit &&
1606 git branch --track=direct foo4 my1 &&
1607 test_cmp_config . branch.foo4.remote &&
1608 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1609 git branch --no-track foo5 my1 &&
1610 test_cmp_config "" --default "" branch.foo5.remote &&
1611 test_cmp_config "" --default "" branch.foo5.merge
1614 test_done