t/t4*: merge a "grep | sed" pipeline
[git.git] / t / t3200-branch.sh
blobd3bbd00b818a4f26e2c19ae0ec432138200afc62
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_PASSES_SANITIZE_LEAK=true
12 . ./test-lib.sh
13 . "$TEST_DIRECTORY"/lib-rebase.sh
15 test_expect_success 'prepare a trivial repository' '
16 echo Hello >A &&
17 git update-index --add A &&
18 git commit -m "Initial commit." &&
19 git branch -M main &&
20 echo World >>A &&
21 git update-index --add A &&
22 git commit -m "Second commit." &&
23 HEAD=$(git rev-parse --verify HEAD)
26 test_expect_success 'git branch --help should not have created a bogus branch' '
27 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
28 test_ref_missing refs/heads/--help
31 test_expect_success REFFILES 'branch -h in broken repository' '
32 mkdir broken &&
34 cd broken &&
35 git init -b main &&
36 >.git/refs/heads/main &&
37 test_expect_code 129 git branch -h >usage 2>&1
38 ) &&
39 test_grep "[Uu]sage" broken/usage
42 test_expect_success 'git branch abc should create a branch' '
43 git branch abc &&
44 test_ref_exists refs/heads/abc
47 test_expect_success 'git branch abc should fail when abc exists' '
48 test_must_fail git branch abc
51 test_expect_success 'git branch --force abc should fail when abc is checked out' '
52 test_when_finished git switch main &&
53 git switch abc &&
54 test_must_fail git branch --force abc HEAD~1
57 test_expect_success 'git branch --force abc should succeed when abc exists' '
58 git rev-parse HEAD~1 >expect &&
59 git branch --force abc HEAD~1 &&
60 git rev-parse abc >actual &&
61 test_cmp expect actual
64 test_expect_success 'git branch a/b/c should create a branch' '
65 git branch a/b/c &&
66 test_ref_exists refs/heads/a/b/c
69 test_expect_success 'git branch mb main... should create a branch' '
70 git branch mb main... &&
71 test_ref_exists refs/heads/mb
74 test_expect_success 'git branch HEAD should fail' '
75 test_must_fail git branch HEAD
78 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
79 GIT_COMMITTER_DATE="2005-05-26 23:30" \
80 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
81 test_ref_exists refs/heads/d/e/f &&
82 cat >expect <<-EOF &&
83 $HEAD refs/heads/d/e/f@{0}: branch: Created from main
84 EOF
85 git reflog show --no-abbrev-commit refs/heads/d/e/f >actual &&
86 test_cmp expect actual
89 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
90 git branch -d d/e/f &&
91 test_ref_missing refs/heads/d/e/f &&
92 test_must_fail git reflog exists refs/heads/d/e/f
95 test_expect_success 'git branch j/k should work after branch j has been deleted' '
96 git branch j &&
97 git branch -d j &&
98 git branch j/k
101 test_expect_success 'git branch l should work after branch l/m has been deleted' '
102 git branch l/m &&
103 git branch -d l/m &&
104 git branch l
107 test_expect_success 'git branch -m dumps usage' '
108 test_expect_code 128 git branch -m 2>err &&
109 test_grep "branch name required" err
112 test_expect_success 'git branch -m m broken_symref should work' '
113 test_when_finished "git branch -D broken_symref" &&
114 git branch --create-reflog m &&
115 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
116 git branch -m m broken_symref &&
117 git reflog exists refs/heads/broken_symref &&
118 test_must_fail git reflog exists refs/heads/i_am_broken
121 test_expect_success 'git branch -m m m/m should work' '
122 git branch --create-reflog m &&
123 git branch -m m m/m &&
124 git reflog exists refs/heads/m/m
127 test_expect_success 'git branch -m n/n n should work' '
128 git branch --create-reflog n/n &&
129 git branch -m n/n n &&
130 git reflog exists refs/heads/n
133 # The topmost entry in reflog for branch bbb is about branch creation.
134 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
136 test_expect_success 'git branch -m bbb should rename checked out branch' '
137 test_when_finished git branch -D bbb &&
138 test_when_finished git checkout main &&
139 git checkout -b aaa &&
140 git commit --allow-empty -m "a new commit" &&
141 git rev-parse aaa@{0} >expect &&
142 git branch -m bbb &&
143 git rev-parse bbb@{1} >actual &&
144 test_cmp expect actual &&
145 git symbolic-ref HEAD >actual &&
146 echo refs/heads/bbb >expect &&
147 test_cmp expect actual
150 test_expect_success 'renaming checked out branch works with d/f conflict' '
151 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
152 test_when_finished git checkout main &&
153 git checkout -b foo &&
154 git branch -m foo/bar &&
155 git symbolic-ref HEAD >actual &&
156 echo refs/heads/foo/bar >expect &&
157 test_cmp expect actual
160 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
161 git branch o/o &&
162 git branch o/p &&
163 test_must_fail git branch -m o/o o
166 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
167 git branch o/q &&
168 test_must_fail git branch -m o/q o/p
171 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
172 git branch -M o/q o/p
175 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
176 git branch o/q &&
177 git branch -m -f o/q o/p
180 test_expect_success 'git branch -m q r/q should fail when r exists' '
181 git branch q &&
182 git branch r &&
183 test_must_fail git branch -m q r/q
186 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
187 git branch bar &&
188 git checkout -b foo &&
189 test_must_fail git branch -M bar foo
192 test_expect_success 'git branch -M foo bar should fail when bar is checked out in worktree' '
193 git branch -f bar &&
194 test_when_finished "git worktree remove wt && git branch -D wt" &&
195 git worktree add wt &&
196 test_must_fail git branch -M bar wt
199 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
200 git checkout -b baz &&
201 git branch bam &&
202 git branch -M baz bam &&
203 test $(git rev-parse --abbrev-ref HEAD) = bam
206 test_expect_success 'git branch -M baz bam should add entries to HEAD reflog' '
207 git reflog show HEAD >actual &&
208 grep "HEAD@{0}: Branch: renamed refs/heads/baz to refs/heads/bam" actual
211 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
212 git init -b main orphan &&
214 cd orphan &&
215 test_commit initial &&
216 git checkout --orphan lonely &&
217 git symbolic-ref HEAD >expect &&
218 echo refs/heads/lonely >actual &&
219 test_cmp expect actual &&
220 test_ref_missing refs/head/lonely &&
221 git branch -M main mistress &&
222 git symbolic-ref HEAD >expect &&
223 test_cmp expect actual
227 test_expect_success 'resulting reflog can be shown by log -g' '
228 oid=$(git rev-parse HEAD) &&
229 cat >expect <<-EOF &&
230 HEAD@{0} $oid Branch: renamed refs/heads/baz to refs/heads/bam
231 HEAD@{2} $oid checkout: moving from foo to baz
233 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
234 test_cmp expect actual
237 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
238 git checkout main &&
239 git worktree add -b baz bazdir &&
240 git worktree add -f bazdir2 baz &&
241 git branch -M baz bam &&
242 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
243 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
244 rm -r bazdir bazdir2 &&
245 git worktree prune
248 test_expect_success REFFILES 'git branch -M fails if updating any linked working tree fails' '
249 git worktree add -b baz bazdir1 &&
250 git worktree add -f bazdir2 baz &&
251 touch .git/worktrees/bazdir1/HEAD.lock &&
252 test_must_fail git branch -M baz bam &&
253 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
254 git branch -M bam baz &&
255 rm .git/worktrees/bazdir1/HEAD.lock &&
256 touch .git/worktrees/bazdir2/HEAD.lock &&
257 test_must_fail git branch -M baz bam &&
258 test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam &&
259 rm -rf bazdir1 bazdir2 &&
260 git worktree prune
263 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
264 git checkout -b baz &&
265 git worktree add -f bazdir baz &&
267 cd bazdir &&
268 git branch -M baz bam &&
269 echo bam >expect &&
270 git rev-parse --abbrev-ref HEAD >actual &&
271 test_cmp expect actual
272 ) &&
273 echo bam >expect &&
274 git rev-parse --abbrev-ref HEAD >actual &&
275 test_cmp expect actual &&
276 rm -r bazdir &&
277 git worktree prune
280 test_expect_success 'git branch -M main should work when main is checked out' '
281 git checkout main &&
282 git branch -M main
285 test_expect_success 'git branch -M main main should work when main is checked out' '
286 git checkout main &&
287 git branch -M main main
290 test_expect_success 'git branch -M topic topic should work when main is checked out' '
291 git checkout main &&
292 git branch topic &&
293 git branch -M topic topic
296 test_expect_success 'git branch -M and -C fail on detached HEAD' '
297 git checkout HEAD^{} &&
298 test_when_finished git checkout - &&
299 echo "fatal: cannot rename the current branch while not on any" >expect &&
300 test_must_fail git branch -M must-fail 2>err &&
301 test_cmp expect err &&
302 echo "fatal: cannot copy the current branch while not on any" >expect &&
303 test_must_fail git branch -C must-fail 2>err &&
304 test_cmp expect err
307 test_expect_success 'git branch -m should work with orphan branches' '
308 test_when_finished git checkout - &&
309 test_when_finished git worktree remove -f wt &&
310 git worktree add wt --detach &&
311 # rename orphan in another worktreee
312 git -C wt checkout --orphan orphan-foo-wt &&
313 git branch -m orphan-foo-wt orphan-bar-wt &&
314 test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) &&
315 # rename orphan in the current worktree
316 git checkout --orphan orphan-foo &&
317 git branch -m orphan-foo orphan-bar &&
318 test orphan-bar=$(git branch --show-current)
321 test_expect_success 'git branch -d on orphan HEAD (merged)' '
322 test_when_finished git checkout main &&
323 git checkout --orphan orphan &&
324 test_when_finished "rm -rf .git/objects/commit-graph*" &&
325 git commit-graph write --reachable &&
326 git branch --track to-delete main &&
327 git branch -d to-delete
330 test_expect_success 'git branch -d on orphan HEAD (merged, graph)' '
331 test_when_finished git checkout main &&
332 git checkout --orphan orphan &&
333 git branch --track to-delete main &&
334 git branch -d to-delete
337 test_expect_success 'git branch -d on orphan HEAD (unmerged)' '
338 test_when_finished git checkout main &&
339 git checkout --orphan orphan &&
340 test_when_finished "git branch -D to-delete" &&
341 git branch to-delete main &&
342 test_must_fail git branch -d to-delete 2>err &&
343 grep "not fully merged" err
346 test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' '
347 test_when_finished git checkout main &&
348 git checkout --orphan orphan &&
349 test_when_finished "git branch -D to-delete" &&
350 git branch to-delete main &&
351 test_when_finished "rm -rf .git/objects/commit-graph*" &&
352 git commit-graph write --reachable &&
353 test_must_fail git branch -d to-delete 2>err &&
354 grep "not fully merged" err
357 test_expect_success 'git branch -v -d t should work' '
358 git branch t &&
359 git rev-parse --verify refs/heads/t &&
360 git branch -v -d t &&
361 test_must_fail git rev-parse --verify refs/heads/t
364 test_expect_success 'git branch -v -m t s should work' '
365 git branch t &&
366 git rev-parse --verify refs/heads/t &&
367 git branch -v -m t s &&
368 test_must_fail git rev-parse --verify refs/heads/t &&
369 git rev-parse --verify refs/heads/s &&
370 git branch -d s
373 test_expect_success 'git branch -m -d t s should fail' '
374 git branch t &&
375 git rev-parse refs/heads/t &&
376 test_must_fail git branch -m -d t s &&
377 git branch -d t &&
378 test_must_fail git rev-parse refs/heads/t
381 test_expect_success 'git branch --list -d t should fail' '
382 git branch t &&
383 git rev-parse refs/heads/t &&
384 test_must_fail git branch --list -d t &&
385 git branch -d t &&
386 test_must_fail git rev-parse refs/heads/t
389 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
390 test_when_finished "rm -rf repo1 repo2" &&
392 git init repo1 &&
393 git init repo1/sub &&
394 test_commit -C repo1/sub x &&
395 test_config_global protocol.file.allow always &&
396 git -C repo1 submodule add ./sub &&
397 git -C repo1 commit -m "adding sub" &&
399 git clone --recurse-submodules repo1 repo2 &&
400 git -C repo2/sub checkout -b work &&
401 test_must_fail git -C repo2/sub branch -D work
404 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
405 test_when_finished "rm -rf nonbare base secondary" &&
407 git init -b main nonbare &&
408 test_commit -C nonbare x &&
409 git clone --bare nonbare bare &&
410 git -C bare worktree add --detach ../secondary main &&
411 git -C secondary branch -D main
414 test_expect_success 'git branch --list -v with --abbrev' '
415 test_when_finished "git branch -D t" &&
416 git branch t &&
417 git branch -v --list t >actual.default &&
418 git branch -v --list --abbrev t >actual.abbrev &&
419 test_cmp actual.default actual.abbrev &&
421 git branch -v --list --no-abbrev t >actual.noabbrev &&
422 git branch -v --list --abbrev=0 t >actual.0abbrev &&
423 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
424 test_cmp actual.noabbrev actual.0abbrev &&
425 test_cmp actual.noabbrev actual.noabbrev-conf &&
427 git branch -v --list --abbrev=36 t >actual.36abbrev &&
428 # how many hexdigits are used?
429 read name objdefault rest <actual.abbrev &&
430 read name obj36 rest <actual.36abbrev &&
431 objfull=$(git rev-parse --verify t) &&
433 # are we really getting abbreviations?
434 test "$obj36" != "$objdefault" &&
435 expr "$obj36" : "$objdefault" >/dev/null &&
436 test "$objfull" != "$obj36" &&
437 expr "$objfull" : "$obj36" >/dev/null
441 test_expect_success 'git branch --column' '
442 COLUMNS=81 git branch --column=column >actual &&
443 cat >expect <<-\EOF &&
444 a/b/c bam foo l * main n o/p r
445 abc bar j/k m/m mb o/o q topic
447 test_cmp expect actual
450 test_expect_success 'git branch --column with an extremely long branch name' '
451 long=this/is/a/part/of/long/branch/name &&
452 long=z$long/$long/$long/$long &&
453 test_when_finished "git branch -d $long" &&
454 git branch $long &&
455 COLUMNS=80 git branch --column=column >actual &&
456 cat >expect <<-EOF &&
457 a/b/c
465 * main
472 topic
473 $long
475 test_cmp expect actual
478 test_expect_success 'git branch with column.*' '
479 git config column.ui column &&
480 git config column.branch "dense" &&
481 COLUMNS=80 git branch >actual &&
482 git config --unset column.branch &&
483 git config --unset column.ui &&
484 cat >expect <<-\EOF &&
485 a/b/c bam foo l * main n o/p r
486 abc bar j/k m/m mb o/o q topic
488 test_cmp expect actual
491 test_expect_success 'git branch --column -v should fail' '
492 test_must_fail git branch --column -v
495 test_expect_success 'git branch -v with column.ui ignored' '
496 git config column.ui column &&
497 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
498 git config --unset column.ui &&
499 cat >expect <<-\EOF &&
500 a/b/c
508 * main
515 topic
517 test_cmp expect actual
520 test_expect_success DEFAULT_REPO_FORMAT 'git branch -m q q2 without config should succeed' '
521 test_when_finished mv .git/config-saved .git/config &&
522 mv .git/config .git/config-saved &&
523 git branch -m q q2 &&
524 git branch -m q2 q
527 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
528 git config branch.s/s.dummy Hello &&
529 git branch --create-reflog s/s &&
530 git reflog exists refs/heads/s/s &&
531 git branch --create-reflog s/t &&
532 git reflog exists refs/heads/s/t &&
533 git branch -d s/t &&
534 git branch -m s/s s &&
535 git reflog exists refs/heads/s
538 test_expect_success 'config information was renamed, too' '
539 test $(git config branch.s.dummy) = Hello &&
540 test_must_fail git config branch.s/s.dummy
543 test_expect_success 'git branch -m correctly renames multiple config sections' '
544 test_when_finished "git checkout main" &&
545 git checkout -b source main &&
547 # Assert that a config file with multiple config sections has
548 # those sections preserved...
549 cat >expect <<-\EOF &&
550 branch.dest.key1=value1
551 some.gar.b=age
552 branch.dest.key2=value2
554 cat >config.branch <<\EOF &&
555 ;; Note the lack of -\EOF above & mixed indenting here. This is
556 ;; intentional, we are also testing that the formatting of copied
557 ;; sections is preserved.
559 ;; Comment for source. Tabs
560 [branch "source"]
561 ;; Comment for the source value
562 key1 = value1
563 ;; Comment for some.gar. Spaces
564 [some "gar"]
565 ;; Comment for the some.gar value
566 b = age
567 ;; Comment for source, again. Mixed tabs/spaces.
568 [branch "source"]
569 ;; Comment for the source value, again
570 key2 = value2
572 cat config.branch >>.git/config &&
573 git branch -m source dest &&
574 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
575 test_cmp expect actual &&
577 # ...and that the comments for those sections are also
578 # preserved.
579 sed "s/\"source\"/\"dest\"/" config.branch >expect &&
580 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
581 test_cmp expect actual
584 test_expect_success 'git branch -c dumps usage' '
585 test_expect_code 128 git branch -c 2>err &&
586 test_grep "branch name required" err
589 test_expect_success 'git branch --copy dumps usage' '
590 test_expect_code 128 git branch --copy 2>err &&
591 test_grep "branch name required" err
594 test_expect_success 'git branch -c d e should work' '
595 git branch --create-reflog d &&
596 git reflog exists refs/heads/d &&
597 git config branch.d.dummy Hello &&
598 git branch -c d e &&
599 git reflog exists refs/heads/d &&
600 git reflog exists refs/heads/e &&
601 echo Hello >expect &&
602 git config branch.e.dummy >actual &&
603 test_cmp expect actual &&
604 echo Hello >expect &&
605 git config branch.d.dummy >actual &&
606 test_cmp expect actual
609 test_expect_success 'git branch --copy is a synonym for -c' '
610 git branch --create-reflog copy &&
611 git reflog exists refs/heads/copy &&
612 git config branch.copy.dummy Hello &&
613 git branch --copy copy copy-to &&
614 git reflog exists refs/heads/copy &&
615 git reflog exists refs/heads/copy-to &&
616 echo Hello >expect &&
617 git config branch.copy.dummy >actual &&
618 test_cmp expect actual &&
619 echo Hello >expect &&
620 git config branch.copy-to.dummy >actual &&
621 test_cmp expect actual
624 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
625 git checkout -b ee &&
626 git reflog exists refs/heads/ee &&
627 git config branch.ee.dummy Hello &&
628 git branch -c ee ef &&
629 git reflog exists refs/heads/ee &&
630 git reflog exists refs/heads/ef &&
631 test $(git config branch.ee.dummy) = Hello &&
632 test $(git config branch.ef.dummy) = Hello &&
633 test $(git rev-parse --abbrev-ref HEAD) = ee
636 test_expect_success 'git branch -c f/f g/g should work' '
637 git branch --create-reflog f/f &&
638 git reflog exists refs/heads/f/f &&
639 git config branch.f/f.dummy Hello &&
640 git branch -c f/f g/g &&
641 git reflog exists refs/heads/f/f &&
642 git reflog exists refs/heads/g/g &&
643 test $(git config branch.f/f.dummy) = Hello &&
644 test $(git config branch.g/g.dummy) = Hello
647 test_expect_success 'git branch -c m2 m2 should work' '
648 git branch --create-reflog m2 &&
649 git reflog exists refs/heads/m2 &&
650 git config branch.m2.dummy Hello &&
651 git branch -c m2 m2 &&
652 git reflog exists refs/heads/m2 &&
653 test $(git config branch.m2.dummy) = Hello
656 test_expect_success 'git branch -c zz zz/zz should fail' '
657 git branch --create-reflog zz &&
658 git reflog exists refs/heads/zz &&
659 test_must_fail git branch -c zz zz/zz
662 test_expect_success 'git branch -c b/b b should fail' '
663 git branch --create-reflog b/b &&
664 test_must_fail git branch -c b/b b
667 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
668 git branch --create-reflog o/q &&
669 git reflog exists refs/heads/o/q &&
670 git reflog exists refs/heads/o/p &&
671 git branch -C o/q o/p
674 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
675 git reflog exists refs/heads/o/q &&
676 git reflog exists refs/heads/o/p &&
677 git branch -c -f o/q o/p
680 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
681 git branch qq &&
682 git branch rr &&
683 test_must_fail git branch -c qq rr/qq
686 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
687 git branch b1 &&
688 git checkout -b b2 &&
689 test_must_fail git branch -C b1 b2
692 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
693 git checkout -b c1 &&
694 git branch c2 &&
695 git branch -C c1 c2 &&
696 test $(git rev-parse --abbrev-ref HEAD) = c1
699 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
700 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
701 git reflog HEAD >actual &&
702 ! grep "$msg$" actual
705 test_expect_success 'git branch -C main should work when main is checked out' '
706 git checkout main &&
707 git branch -C main
710 test_expect_success 'git branch -C main main should work when main is checked out' '
711 git checkout main &&
712 git branch -C main main
715 test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
716 git checkout main &&
717 git branch main5 &&
718 git branch -C main5 main5
721 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
722 git branch --create-reflog cd &&
723 git reflog exists refs/heads/cd &&
724 git config branch.cd.dummy CD &&
725 git branch --create-reflog ab &&
726 git reflog exists refs/heads/ab &&
727 git config branch.ab.dummy AB &&
728 git branch -C ab cd &&
729 git reflog exists refs/heads/ab &&
730 git reflog exists refs/heads/cd &&
731 test $(git config branch.ab.dummy) = AB &&
732 test $(git config branch.cd.dummy) = AB
735 test_expect_success 'git branch -c correctly copies multiple config sections' '
736 FOO=1 &&
737 export FOO &&
738 test_when_finished "git checkout main" &&
739 git checkout -b source2 main &&
741 # Assert that a config file with multiple config sections has
742 # those sections preserved...
743 cat >expect <<-\EOF &&
744 branch.source2.key1=value1
745 branch.dest2.key1=value1
746 more.gar.b=age
747 branch.source2.key2=value2
748 branch.dest2.key2=value2
750 cat >config.branch <<\EOF &&
751 ;; Note the lack of -\EOF above & mixed indenting here. This is
752 ;; intentional, we are also testing that the formatting of copied
753 ;; sections is preserved.
755 ;; Comment for source2. Tabs
756 [branch "source2"]
757 ;; Comment for the source2 value
758 key1 = value1
759 ;; Comment for more.gar. Spaces
760 [more "gar"]
761 ;; Comment for the more.gar value
762 b = age
763 ;; Comment for source2, again. Mixed tabs/spaces.
764 [branch "source2"]
765 ;; Comment for the source2 value, again
766 key2 = value2
768 cat config.branch >>.git/config &&
769 git branch -c source2 dest2 &&
770 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
771 test_cmp expect actual &&
773 # ...and that the comments and formatting for those sections
774 # is also preserved.
775 cat >expect <<\EOF &&
776 ;; Comment for source2. Tabs
777 [branch "source2"]
778 ;; Comment for the source2 value
779 key1 = value1
780 ;; Comment for more.gar. Spaces
781 [branch "dest2"]
782 ;; Comment for the source2 value
783 key1 = value1
784 ;; Comment for more.gar. Spaces
785 [more "gar"]
786 ;; Comment for the more.gar value
787 b = age
788 ;; Comment for source2, again. Mixed tabs/spaces.
789 [branch "source2"]
790 ;; Comment for the source2 value, again
791 key2 = value2
792 [branch "dest2"]
793 ;; Comment for the source2 value, again
794 key2 = value2
796 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
797 test_cmp expect actual
800 test_expect_success 'deleting a symref' '
801 git branch target &&
802 git symbolic-ref refs/heads/symref refs/heads/target &&
803 echo "Deleted branch symref (was refs/heads/target)." >expect &&
804 git branch -d symref >actual &&
805 test_ref_exists refs/heads/target &&
806 test_ref_missing refs/heads/symref &&
807 test_cmp expect actual
810 test_expect_success 'deleting a dangling symref' '
811 git symbolic-ref refs/heads/dangling-symref nowhere &&
812 git symbolic-ref --no-recurse refs/heads/dangling-symref &&
813 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
814 git branch -d dangling-symref >actual &&
815 test_ref_missing refs/heads/dangling-symref &&
816 test_cmp expect actual
819 test_expect_success 'deleting a self-referential symref' '
820 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
821 test_ref_exists refs/heads/self-reference &&
822 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
823 git branch -d self-reference >actual &&
824 test_ref_missing refs/heads/self-reference &&
825 test_cmp expect actual
828 test_expect_success 'renaming a symref is not allowed' '
829 git symbolic-ref refs/heads/topic refs/heads/main &&
830 test_must_fail git branch -m topic new-topic &&
831 git symbolic-ref refs/heads/topic &&
832 test_ref_exists refs/heads/main &&
833 test_ref_missing refs/heads/new-topic
836 test_expect_success 'test tracking setup via --track' '
837 git config remote.local.url . &&
838 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
839 (git show-ref -q refs/remotes/local/main || git fetch local) &&
840 git branch --track my1 local/main &&
841 test $(git config branch.my1.remote) = local &&
842 test $(git config branch.my1.merge) = refs/heads/main
845 test_expect_success 'test tracking setup (non-wildcard, matching)' '
846 git config remote.local.url . &&
847 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
848 (git show-ref -q refs/remotes/local/main || git fetch local) &&
849 git branch --track my4 local/main &&
850 test $(git config branch.my4.remote) = local &&
851 test $(git config branch.my4.merge) = refs/heads/main
854 test_expect_success 'tracking setup fails on non-matching refspec' '
855 git config remote.local.url . &&
856 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
857 (git show-ref -q refs/remotes/local/main || git fetch local) &&
858 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
859 test_must_fail git branch --track my5 local/main &&
860 test_must_fail git config branch.my5.remote &&
861 test_must_fail git config branch.my5.merge
864 test_expect_success 'test tracking setup via config' '
865 git config branch.autosetupmerge true &&
866 git config remote.local.url . &&
867 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
868 (git show-ref -q refs/remotes/local/main || git fetch local) &&
869 git branch my3 local/main &&
870 test $(git config branch.my3.remote) = local &&
871 test $(git config branch.my3.merge) = refs/heads/main
874 test_expect_success 'test overriding tracking setup via --no-track' '
875 git config branch.autosetupmerge true &&
876 git config remote.local.url . &&
877 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
878 (git show-ref -q refs/remotes/local/main || git fetch local) &&
879 git branch --no-track my2 local/main &&
880 git config branch.autosetupmerge false &&
881 ! test "$(git config branch.my2.remote)" = local &&
882 ! test "$(git config branch.my2.merge)" = refs/heads/main
885 test_expect_success 'no tracking without .fetch entries' '
886 git config branch.autosetupmerge true &&
887 git branch my6 s &&
888 git config branch.autosetupmerge false &&
889 test -z "$(git config branch.my6.remote)" &&
890 test -z "$(git config branch.my6.merge)"
893 test_expect_success 'test tracking setup via --track but deeper' '
894 git config remote.local.url . &&
895 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
896 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
897 git branch --track my7 local/o/o &&
898 test "$(git config branch.my7.remote)" = local &&
899 test "$(git config branch.my7.merge)" = refs/heads/o/o
902 test_expect_success 'test deleting branch deletes branch config' '
903 git branch -d my7 &&
904 test -z "$(git config branch.my7.remote)" &&
905 test -z "$(git config branch.my7.merge)"
908 test_expect_success 'test deleting branch without config' '
909 git branch my7 s &&
910 sha1=$(git rev-parse my7 | cut -c 1-7) &&
911 echo "Deleted branch my7 (was $sha1)." >expect &&
912 git branch -d my7 >actual 2>&1 &&
913 test_cmp expect actual
916 test_expect_success 'deleting currently checked out branch fails' '
917 git worktree add -b my7 my7 &&
918 test_must_fail git -C my7 branch -d my7 &&
919 test_must_fail git branch -d my7 2>actual &&
920 grep "^error: cannot delete branch .my7. used by worktree at " actual &&
921 rm -r my7 &&
922 git worktree prune
925 test_expect_success 'deleting in-use branch fails' '
926 git worktree add my7 &&
927 test_commit -C my7 bt7 &&
928 git -C my7 bisect start HEAD HEAD~2 &&
929 test_must_fail git -C my7 branch -d my7 &&
930 test_must_fail git branch -d my7 2>actual &&
931 grep "^error: cannot delete branch .my7. used by worktree at " actual &&
932 rm -r my7 &&
933 git worktree prune
936 test_expect_success 'test --track without .fetch entries' '
937 git branch --track my8 &&
938 test "$(git config branch.my8.remote)" &&
939 test "$(git config branch.my8.merge)"
942 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
943 git config branch.autosetupmerge always &&
944 git branch my9 HEAD^ &&
945 git config branch.autosetupmerge false
948 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
949 test_must_fail git branch --track my10 HEAD^
952 test_expect_success 'branch from tag w/--track causes failure' '
953 git tag foobar &&
954 test_must_fail git branch --track my11 foobar
957 test_expect_success 'simple tracking works when remote branch name matches' '
958 test_when_finished "rm -rf otherserver" &&
959 git init otherserver &&
960 test_commit -C otherserver my_commit 1 &&
961 git -C otherserver branch feature &&
962 test_config branch.autosetupmerge simple &&
963 test_config remote.otherserver.url otherserver &&
964 test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* &&
965 git fetch otherserver &&
966 git branch feature otherserver/feature &&
967 test_cmp_config otherserver branch.feature.remote &&
968 test_cmp_config refs/heads/feature branch.feature.merge
971 test_expect_success 'simple tracking skips when remote branch name does not match' '
972 test_config branch.autosetupmerge simple &&
973 test_config remote.local.url . &&
974 test_config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
975 git fetch local &&
976 git branch my-other local/main &&
977 test_cmp_config "" --default "" branch.my-other.remote &&
978 test_cmp_config "" --default "" branch.my-other.merge
981 test_expect_success 'simple tracking skips when remote ref is not a branch' '
982 test_config branch.autosetupmerge simple &&
983 test_config remote.localtags.url . &&
984 test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* &&
985 git tag mytag12 main &&
986 git fetch localtags &&
987 git branch mytag12 localtags/mytag12 &&
988 test_cmp_config "" --default "" branch.mytag12.remote &&
989 test_cmp_config "" --default "" branch.mytag12.merge
992 test_expect_success '--set-upstream-to fails on multiple branches' '
993 echo "fatal: too many arguments to set new upstream" >expect &&
994 test_must_fail git branch --set-upstream-to main a b c 2>err &&
995 test_cmp expect err
998 test_expect_success '--set-upstream-to fails on detached HEAD' '
999 git checkout HEAD^{} &&
1000 test_when_finished git checkout - &&
1001 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch" >expect &&
1002 test_must_fail git branch --set-upstream-to main 2>err &&
1003 test_cmp expect err
1006 test_expect_success '--set-upstream-to fails on a missing dst branch' '
1007 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
1008 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
1009 test_cmp expect err
1012 test_expect_success '--set-upstream-to fails on a missing src branch' '
1013 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
1014 test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
1017 test_expect_success '--set-upstream-to fails on a non-ref' '
1018 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
1019 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
1020 test_cmp expect err
1023 test_expect_success '--set-upstream-to fails on locked config' '
1024 test_when_finished "rm -f .git/config.lock" &&
1025 >.git/config.lock &&
1026 git branch locked &&
1027 test_must_fail git branch --set-upstream-to locked 2>err &&
1028 test_grep "could not lock config file .git/config" err
1031 test_expect_success 'use --set-upstream-to modify HEAD' '
1032 test_config branch.main.remote foo &&
1033 test_config branch.main.merge foo &&
1034 git branch my12 &&
1035 git branch --set-upstream-to my12 &&
1036 test "$(git config branch.main.remote)" = "." &&
1037 test "$(git config branch.main.merge)" = "refs/heads/my12"
1040 test_expect_success 'use --set-upstream-to modify a particular branch' '
1041 git branch my13 &&
1042 git branch --set-upstream-to main my13 &&
1043 test_when_finished "git branch --unset-upstream my13" &&
1044 test "$(git config branch.my13.remote)" = "." &&
1045 test "$(git config branch.my13.merge)" = "refs/heads/main"
1048 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
1049 echo "fatal: branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
1050 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
1051 test_cmp expect err
1054 test_expect_success '--unset-upstream should fail if config is locked' '
1055 test_when_finished "rm -f .git/config.lock" &&
1056 git branch --set-upstream-to locked &&
1057 >.git/config.lock &&
1058 test_must_fail git branch --unset-upstream 2>err &&
1059 test_grep "could not lock config file .git/config" err
1062 test_expect_success 'test --unset-upstream on HEAD' '
1063 git branch my14 &&
1064 test_config branch.main.remote foo &&
1065 test_config branch.main.merge foo &&
1066 git branch --set-upstream-to my14 &&
1067 git branch --unset-upstream &&
1068 test_must_fail git config branch.main.remote &&
1069 test_must_fail git config branch.main.merge &&
1070 # fail for a branch without upstream set
1071 echo "fatal: branch '"'"'main'"'"' has no upstream information" >expect &&
1072 test_must_fail git branch --unset-upstream 2>err &&
1073 test_cmp expect err
1076 test_expect_success '--unset-upstream should fail on multiple branches' '
1077 echo "fatal: too many arguments to unset upstream" >expect &&
1078 test_must_fail git branch --unset-upstream a b c 2>err &&
1079 test_cmp expect err
1082 test_expect_success '--unset-upstream should fail on detached HEAD' '
1083 git checkout HEAD^{} &&
1084 test_when_finished git checkout - &&
1085 echo "fatal: could not unset upstream of HEAD when it does not point to any branch" >expect &&
1086 test_must_fail git branch --unset-upstream 2>err &&
1087 test_cmp expect err
1090 test_expect_success 'test --unset-upstream on a particular branch' '
1091 git branch my15 &&
1092 git branch --set-upstream-to main my14 &&
1093 git branch --unset-upstream my14 &&
1094 test_must_fail git config branch.my14.remote &&
1095 test_must_fail git config branch.my14.merge
1098 test_expect_success 'disabled option --set-upstream fails' '
1099 test_must_fail git branch --set-upstream origin/main
1102 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
1103 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1104 cat >expect <<-\EOF &&
1105 warning: not setting branch 'my13' as its own upstream
1107 test_expect_code 1 git config branch.my13.remote &&
1108 test_expect_code 1 git config branch.my13.merge &&
1109 test_cmp expect actual
1112 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
1113 test_when_finished git checkout main &&
1114 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1115 git checkout -b g/h/i -l main &&
1116 test_ref_exists refs/heads/g/h/i &&
1117 cat >expect <<-EOF &&
1118 $HEAD refs/heads/g/h/i@{0}: branch: Created from main
1120 git reflog show --no-abbrev-commit refs/heads/g/h/i >actual &&
1121 test_cmp expect actual
1124 test_expect_success 'checkout -b makes reflog by default' '
1125 git checkout main &&
1126 git config --unset core.logAllRefUpdates &&
1127 git checkout -b alpha &&
1128 git rev-parse --verify alpha@{0}
1131 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1132 git checkout main &&
1133 git config core.logAllRefUpdates false &&
1134 git checkout -b beta &&
1135 test_must_fail git rev-parse --verify beta@{0}
1138 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1139 git checkout main &&
1140 git checkout -lb gamma &&
1141 git config --unset core.logAllRefUpdates &&
1142 git rev-parse --verify gamma@{0}
1145 test_expect_success 'avoid ambiguous track and advise' '
1146 git config branch.autosetupmerge true &&
1147 git config remote.ambi1.url lalala &&
1148 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1149 git config remote.ambi2.url lilili &&
1150 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1151 cat <<-EOF >expected &&
1152 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1153 hint: There are multiple remotes whose fetch refspecs map to the remote
1154 hint: tracking ref '\''refs/heads/main'\'':
1155 hint: ambi1
1156 hint: ambi2
1157 hint: ''
1158 hint: This is typically a configuration error.
1159 hint: ''
1160 hint: To support setting up tracking branches, ensure that
1161 hint: different remotes'\'' fetch refspecs map into different
1162 hint: tracking namespaces.
1164 test_must_fail git branch all1 main 2>actual &&
1165 test_cmp expected actual &&
1166 test -z "$(git config branch.all1.merge)"
1169 test_expect_success 'autosetuprebase local on a tracked local branch' '
1170 git config remote.local.url . &&
1171 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1172 git config branch.autosetuprebase local &&
1173 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1174 git branch mybase &&
1175 git branch --track myr1 mybase &&
1176 test "$(git config branch.myr1.remote)" = . &&
1177 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1178 test "$(git config branch.myr1.rebase)" = true
1181 test_expect_success 'autosetuprebase always on a tracked local branch' '
1182 git config remote.local.url . &&
1183 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1184 git config branch.autosetuprebase always &&
1185 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1186 git branch mybase2 &&
1187 git branch --track myr2 mybase &&
1188 test "$(git config branch.myr2.remote)" = . &&
1189 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1190 test "$(git config branch.myr2.rebase)" = true
1193 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1194 git config remote.local.url . &&
1195 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1196 git config branch.autosetuprebase remote &&
1197 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1198 git branch mybase3 &&
1199 git branch --track myr3 mybase2 &&
1200 test "$(git config branch.myr3.remote)" = . &&
1201 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1202 ! test "$(git config branch.myr3.rebase)" = true
1205 test_expect_success 'autosetuprebase never on a tracked local branch' '
1206 git config remote.local.url . &&
1207 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1208 git config branch.autosetuprebase never &&
1209 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1210 git branch mybase4 &&
1211 git branch --track myr4 mybase2 &&
1212 test "$(git config branch.myr4.remote)" = . &&
1213 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1214 ! test "$(git config branch.myr4.rebase)" = true
1217 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1218 git config remote.local.url . &&
1219 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1220 git config branch.autosetuprebase local &&
1221 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1222 git branch --track myr5 local/main &&
1223 test "$(git config branch.myr5.remote)" = local &&
1224 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1225 ! test "$(git config branch.myr5.rebase)" = true
1228 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1229 git config remote.local.url . &&
1230 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1231 git config branch.autosetuprebase never &&
1232 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1233 git branch --track myr6 local/main &&
1234 test "$(git config branch.myr6.remote)" = local &&
1235 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1236 ! test "$(git config branch.myr6.rebase)" = true
1239 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1240 git config remote.local.url . &&
1241 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1242 git config branch.autosetuprebase remote &&
1243 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1244 git branch --track myr7 local/main &&
1245 test "$(git config branch.myr7.remote)" = local &&
1246 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1247 test "$(git config branch.myr7.rebase)" = true
1250 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1251 git config remote.local.url . &&
1252 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1253 git config branch.autosetuprebase remote &&
1254 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1255 git branch --track myr8 local/main &&
1256 test "$(git config branch.myr8.remote)" = local &&
1257 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1258 test "$(git config branch.myr8.rebase)" = true
1261 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1262 git config --unset branch.autosetuprebase &&
1263 git config remote.local.url . &&
1264 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1265 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1266 git branch --track myr9 local/main &&
1267 test "$(git config branch.myr9.remote)" = local &&
1268 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1269 test "z$(git config branch.myr9.rebase)" = z
1272 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1273 git config remote.local.url . &&
1274 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1275 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1276 git branch mybase10 &&
1277 git branch --track myr10 mybase2 &&
1278 test "$(git config branch.myr10.remote)" = . &&
1279 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1280 test "z$(git config branch.myr10.rebase)" = z
1283 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1284 git config remote.local.url . &&
1285 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1286 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1287 git branch --no-track myr11 mybase2 &&
1288 test "z$(git config branch.myr11.remote)" = z &&
1289 test "z$(git config branch.myr11.merge)" = z &&
1290 test "z$(git config branch.myr11.rebase)" = z
1293 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1294 git config remote.local.url . &&
1295 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1296 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1297 git branch --no-track myr12 local/main &&
1298 test "z$(git config branch.myr12.remote)" = z &&
1299 test "z$(git config branch.myr12.merge)" = z &&
1300 test "z$(git config branch.myr12.rebase)" = z
1303 test_expect_success 'autosetuprebase never on an untracked local branch' '
1304 git config branch.autosetuprebase never &&
1305 git config remote.local.url . &&
1306 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1307 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1308 git branch --no-track myr13 mybase2 &&
1309 test "z$(git config branch.myr13.remote)" = z &&
1310 test "z$(git config branch.myr13.merge)" = z &&
1311 test "z$(git config branch.myr13.rebase)" = z
1314 test_expect_success 'autosetuprebase local on an untracked local branch' '
1315 git config branch.autosetuprebase local &&
1316 git config remote.local.url . &&
1317 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1318 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1319 git branch --no-track myr14 mybase2 &&
1320 test "z$(git config branch.myr14.remote)" = z &&
1321 test "z$(git config branch.myr14.merge)" = z &&
1322 test "z$(git config branch.myr14.rebase)" = z
1325 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1326 git config branch.autosetuprebase remote &&
1327 git config remote.local.url . &&
1328 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1329 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1330 git branch --no-track myr15 mybase2 &&
1331 test "z$(git config branch.myr15.remote)" = z &&
1332 test "z$(git config branch.myr15.merge)" = z &&
1333 test "z$(git config branch.myr15.rebase)" = z
1336 test_expect_success 'autosetuprebase always on an untracked local branch' '
1337 git config branch.autosetuprebase always &&
1338 git config remote.local.url . &&
1339 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1340 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1341 git branch --no-track myr16 mybase2 &&
1342 test "z$(git config branch.myr16.remote)" = z &&
1343 test "z$(git config branch.myr16.merge)" = z &&
1344 test "z$(git config branch.myr16.rebase)" = z
1347 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1348 git config branch.autosetuprebase never &&
1349 git config remote.local.url . &&
1350 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1351 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1352 git branch --no-track myr17 local/main &&
1353 test "z$(git config branch.myr17.remote)" = z &&
1354 test "z$(git config branch.myr17.merge)" = z &&
1355 test "z$(git config branch.myr17.rebase)" = z
1358 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1359 git config branch.autosetuprebase local &&
1360 git config remote.local.url . &&
1361 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1362 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1363 git branch --no-track myr18 local/main &&
1364 test "z$(git config branch.myr18.remote)" = z &&
1365 test "z$(git config branch.myr18.merge)" = z &&
1366 test "z$(git config branch.myr18.rebase)" = z
1369 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1370 git config branch.autosetuprebase remote &&
1371 git config remote.local.url . &&
1372 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1373 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1374 git branch --no-track myr19 local/main &&
1375 test "z$(git config branch.myr19.remote)" = z &&
1376 test "z$(git config branch.myr19.merge)" = z &&
1377 test "z$(git config branch.myr19.rebase)" = z
1380 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1381 git config branch.autosetuprebase always &&
1382 git config remote.local.url . &&
1383 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1384 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1385 git branch --no-track myr20 local/main &&
1386 test "z$(git config branch.myr20.remote)" = z &&
1387 test "z$(git config branch.myr20.merge)" = z &&
1388 test "z$(git config branch.myr20.rebase)" = z
1391 test_expect_success 'autosetuprebase always on detached HEAD' '
1392 git config branch.autosetupmerge always &&
1393 test_when_finished git checkout main &&
1394 git checkout HEAD^0 &&
1395 git branch my11 &&
1396 test -z "$(git config branch.my11.remote)" &&
1397 test -z "$(git config branch.my11.merge)"
1400 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1401 git config branch.autosetuprebase garbage &&
1402 test_must_fail git branch
1405 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1406 git config --unset branch.autosetuprebase &&
1407 echo "[branch] autosetuprebase" >>.git/config &&
1408 test_must_fail git branch &&
1409 git config --unset branch.autosetuprebase
1412 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1413 git checkout my9 &&
1414 git config --unset branch.my8.merge &&
1415 test_must_fail git branch -d my8
1418 test_expect_success 'attempt to delete a branch merged to its base' '
1419 # we are on my9 which is the initial commit; traditionally
1420 # we would not have allowed deleting my8 that is not merged
1421 # to my9, but it is set to track main that already has my8
1422 git config branch.my8.merge refs/heads/main &&
1423 git branch -d my8
1426 test_expect_success 'attempt to delete a branch merged to its base' '
1427 git checkout main &&
1428 echo Third >>A &&
1429 git commit -m "Third commit" A &&
1430 git branch -t my10 my9 &&
1431 git branch -f my10 HEAD^ &&
1432 # we are on main which is at the third commit, and my10
1433 # is behind us, so traditionally we would have allowed deleting
1434 # it; but my10 is set to track my9 that is further behind.
1435 test_must_fail git branch -d my10
1438 test_expect_success 'branch --delete --force removes dangling branch' '
1439 git checkout main &&
1440 test_commit unstable &&
1441 hash=$(git rev-parse HEAD) &&
1442 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1443 git branch --no-track dangling &&
1444 mv $objpath $objpath.x &&
1445 test_when_finished "mv $objpath.x $objpath" &&
1446 git branch --delete --force dangling &&
1447 git for-each-ref refs/heads/dangling >actual &&
1448 test_must_be_empty actual
1451 test_expect_success 'use --edit-description' '
1452 EDITOR=: git branch --edit-description &&
1453 test_expect_code 1 git config branch.main.description &&
1455 write_script editor <<-\EOF &&
1456 echo "New contents" >"$1"
1458 EDITOR=./editor git branch --edit-description &&
1459 write_script editor <<-\EOF &&
1460 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1462 EDITOR=./editor git branch --edit-description &&
1463 echo "New contents" >expect &&
1464 test_cmp expect EDITOR_OUTPUT
1467 test_expect_success 'detect typo in branch name when using --edit-description' '
1468 write_script editor <<-\EOF &&
1469 echo "New contents" >"$1"
1471 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1474 test_expect_success 'refuse --edit-description on unborn branch for now' '
1475 test_when_finished "git checkout main" &&
1476 write_script editor <<-\EOF &&
1477 echo "New contents" >"$1"
1479 git checkout --orphan unborn &&
1480 test_must_fail env EDITOR=./editor git branch --edit-description
1483 test_expect_success '--merged catches invalid object names' '
1484 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1487 test_expect_success '--list during rebase' '
1488 test_when_finished "reset_rebase" &&
1489 git checkout main &&
1490 FAKE_LINES="1 edit 2" &&
1491 export FAKE_LINES &&
1492 set_fake_editor &&
1493 git rebase -i HEAD~2 &&
1494 git branch --list >actual &&
1495 test_grep "rebasing main" actual
1498 test_expect_success '--list during rebase from detached HEAD' '
1499 test_when_finished "reset_rebase && git checkout main" &&
1500 git checkout main^0 &&
1501 oid=$(git rev-parse --short HEAD) &&
1502 FAKE_LINES="1 edit 2" &&
1503 export FAKE_LINES &&
1504 set_fake_editor &&
1505 git rebase -i HEAD~2 &&
1506 git branch --list >actual &&
1507 test_grep "rebasing detached HEAD $oid" actual
1510 test_expect_success 'tracking with unexpected .fetch refspec' '
1511 rm -rf a b c d &&
1512 git init -b main a &&
1514 cd a &&
1515 test_commit a
1516 ) &&
1517 git init -b main b &&
1519 cd b &&
1520 test_commit b
1521 ) &&
1522 git init -b main c &&
1524 cd c &&
1525 test_commit c &&
1526 git remote add a ../a &&
1527 git remote add b ../b &&
1528 git fetch --all
1529 ) &&
1530 git init -b main d &&
1532 cd d &&
1533 git remote add c ../c &&
1534 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1535 git fetch c &&
1536 git branch --track local/a/main remotes/a/main &&
1537 test "$(git config branch.local/a/main.remote)" = "c" &&
1538 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1539 git rev-parse --verify a >expect &&
1540 git rev-parse --verify local/a/main >actual &&
1541 test_cmp expect actual
1545 test_expect_success 'configured committerdate sort' '
1546 git init -b main sort &&
1547 test_config -C sort branch.sort "committerdate" &&
1550 cd sort &&
1551 test_commit initial &&
1552 git checkout -b a &&
1553 test_commit a &&
1554 git checkout -b c &&
1555 test_commit c &&
1556 git checkout -b b &&
1557 test_commit b &&
1558 git branch >actual &&
1559 cat >expect <<-\EOF &&
1560 main
1565 test_cmp expect actual
1569 test_expect_success 'option override configured sort' '
1570 test_config -C sort branch.sort "committerdate" &&
1573 cd sort &&
1574 git branch --sort=refname >actual &&
1575 cat >expect <<-\EOF &&
1579 main
1581 test_cmp expect actual
1585 test_expect_success '--no-sort cancels config sort keys' '
1586 test_config -C sort branch.sort "-refname" &&
1589 cd sort &&
1591 # objecttype is identical for all of them, so sort falls back on
1592 # default (ascending refname)
1593 git branch \
1594 --no-sort \
1595 --sort="objecttype" >actual &&
1596 cat >expect <<-\EOF &&
1600 main
1602 test_cmp expect actual
1607 test_expect_success '--no-sort cancels command line sort keys' '
1609 cd sort &&
1611 # objecttype is identical for all of them, so sort falls back on
1612 # default (ascending refname)
1613 git branch \
1614 --sort="-refname" \
1615 --no-sort \
1616 --sort="objecttype" >actual &&
1617 cat >expect <<-\EOF &&
1621 main
1623 test_cmp expect actual
1627 test_expect_success '--no-sort without subsequent --sort prints expected branches' '
1629 cd sort &&
1631 # Sort the results with `sort` for a consistent comparison
1632 # against expected
1633 git branch --no-sort | sort >actual &&
1634 cat >expect <<-\EOF &&
1637 main
1640 test_cmp expect actual
1644 test_expect_success 'invalid sort parameter in configuration' '
1645 test_config -C sort branch.sort "v:notvalid" &&
1648 cd sort &&
1650 # this works in the "listing" mode, so bad sort key
1651 # is a dying offence.
1652 test_must_fail git branch &&
1654 # these do not need to use sorting, and should all
1655 # succeed
1656 git branch newone main &&
1657 git branch -c newone newerone &&
1658 git branch -m newone newestone &&
1659 git branch -d newerone newestone
1663 test_expect_success 'tracking info copied with --track=inherit' '
1664 git branch --track=inherit foo2 my1 &&
1665 test_cmp_config local branch.foo2.remote &&
1666 test_cmp_config refs/heads/main branch.foo2.merge
1669 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1670 test_unconfig branch.autoSetupMerge &&
1671 # default config does not copy tracking info
1672 git branch foo-no-inherit my1 &&
1673 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1674 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1675 # with autoSetupMerge=inherit, we copy tracking info from my1
1676 test_config branch.autoSetupMerge inherit &&
1677 git branch foo3 my1 &&
1678 test_cmp_config local branch.foo3.remote &&
1679 test_cmp_config refs/heads/main branch.foo3.merge &&
1680 # no tracking info to inherit from main
1681 git branch main2 main &&
1682 test_cmp_config "" --default "" branch.main2.remote &&
1683 test_cmp_config "" --default "" branch.main2.merge
1686 test_expect_success '--track overrides branch.autoSetupMerge' '
1687 test_config branch.autoSetupMerge inherit &&
1688 git branch --track=direct foo4 my1 &&
1689 test_cmp_config . branch.foo4.remote &&
1690 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1691 git branch --no-track foo5 my1 &&
1692 test_cmp_config "" --default "" branch.foo5.remote &&
1693 test_cmp_config "" --default "" branch.foo5.merge
1696 test_expect_success 'errors if given a bad branch name' '
1697 cat <<-\EOF >expect &&
1698 fatal: '\''foo..bar'\'' is not a valid branch name
1699 hint: See `man git check-ref-format`
1700 hint: Disable this message with "git config advice.refSyntax false"
1702 test_must_fail git branch foo..bar >actual 2>&1 &&
1703 test_cmp expect actual
1706 test_done