t7800: improve test descriptions with empty arguments
[alt-git.git] / t / t3200-branch.sh
blobc54fd9ea06e6cce4c11729f109718454f5132261
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 '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 cat >expect <<EOF
79 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
80 EOF
81 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
82 GIT_COMMITTER_DATE="2005-05-26 23:30" \
83 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
84 test_ref_exists refs/heads/d/e/f &&
85 test_path_is_file .git/logs/refs/heads/d/e/f &&
86 test_cmp expect .git/logs/refs/heads/d/e/f
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 .git/logs/HEAD' '
207 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
208 grep " $ZERO_OID.*$msg$" .git/logs/HEAD &&
209 grep "^$ZERO_OID.*$msg$" .git/logs/HEAD
212 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
213 git init -b main orphan &&
215 cd orphan &&
216 test_commit initial &&
217 git checkout --orphan lonely &&
218 grep lonely .git/HEAD &&
219 test_ref_missing refs/head/lonely &&
220 git branch -M main mistress &&
221 grep lonely .git/HEAD
225 test_expect_success 'resulting reflog can be shown by log -g' '
226 oid=$(git rev-parse HEAD) &&
227 cat >expect <<-EOF &&
228 HEAD@{0} $oid $msg
229 HEAD@{2} $oid checkout: moving from foo to baz
231 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
232 test_cmp expect actual
235 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
236 git checkout main &&
237 git worktree add -b baz bazdir &&
238 git worktree add -f bazdir2 baz &&
239 git branch -M baz bam &&
240 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
241 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
242 rm -r bazdir bazdir2 &&
243 git worktree prune
246 test_expect_success 'git branch -M fails if updating any linked working tree fails' '
247 git worktree add -b baz bazdir1 &&
248 git worktree add -f bazdir2 baz &&
249 touch .git/worktrees/bazdir1/HEAD.lock &&
250 test_must_fail git branch -M baz bam &&
251 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
252 git branch -M bam baz &&
253 rm .git/worktrees/bazdir1/HEAD.lock &&
254 touch .git/worktrees/bazdir2/HEAD.lock &&
255 test_must_fail git branch -M baz bam &&
256 test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam &&
257 rm -rf bazdir1 bazdir2 &&
258 git worktree prune
261 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
262 git checkout -b baz &&
263 git worktree add -f bazdir baz &&
265 cd bazdir &&
266 git branch -M baz bam &&
267 echo bam >expect &&
268 git rev-parse --abbrev-ref HEAD >actual &&
269 test_cmp expect actual
270 ) &&
271 echo bam >expect &&
272 git rev-parse --abbrev-ref HEAD >actual &&
273 test_cmp expect actual &&
274 rm -r bazdir &&
275 git worktree prune
278 test_expect_success 'git branch -M main should work when main is checked out' '
279 git checkout main &&
280 git branch -M main
283 test_expect_success 'git branch -M main main should work when main is checked out' '
284 git checkout main &&
285 git branch -M main main
288 test_expect_success 'git branch -M topic topic should work when main is checked out' '
289 git checkout main &&
290 git branch topic &&
291 git branch -M topic topic
294 test_expect_success 'git branch -M and -C fail on detached HEAD' '
295 git checkout HEAD^{} &&
296 test_when_finished git checkout - &&
297 echo "fatal: cannot rename the current branch while not on any" >expect &&
298 test_must_fail git branch -M must-fail 2>err &&
299 test_cmp expect err &&
300 echo "fatal: cannot copy the current branch while not on any" >expect &&
301 test_must_fail git branch -C must-fail 2>err &&
302 test_cmp expect err
305 test_expect_success 'git branch -m should work with orphan branches' '
306 test_when_finished git checkout - &&
307 test_when_finished git worktree remove -f wt &&
308 git worktree add wt --detach &&
309 # rename orphan in another worktreee
310 git -C wt checkout --orphan orphan-foo-wt &&
311 git branch -m orphan-foo-wt orphan-bar-wt &&
312 test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) &&
313 # rename orphan in the current worktree
314 git checkout --orphan orphan-foo &&
315 git branch -m orphan-foo orphan-bar &&
316 test orphan-bar=$(git branch --show-current)
319 test_expect_success 'git branch -d on orphan HEAD (merged)' '
320 test_when_finished git checkout main &&
321 git checkout --orphan orphan &&
322 test_when_finished "rm -rf .git/objects/commit-graph*" &&
323 git commit-graph write --reachable &&
324 git branch --track to-delete main &&
325 git branch -d to-delete
328 test_expect_success 'git branch -d on orphan HEAD (merged, graph)' '
329 test_when_finished git checkout main &&
330 git checkout --orphan orphan &&
331 git branch --track to-delete main &&
332 git branch -d to-delete
335 test_expect_success 'git branch -d on orphan HEAD (unmerged)' '
336 test_when_finished git checkout main &&
337 git checkout --orphan orphan &&
338 test_when_finished "git branch -D to-delete" &&
339 git branch to-delete main &&
340 test_must_fail git branch -d to-delete 2>err &&
341 grep "not fully merged" err
344 test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' '
345 test_when_finished git checkout main &&
346 git checkout --orphan orphan &&
347 test_when_finished "git branch -D to-delete" &&
348 git branch to-delete main &&
349 test_when_finished "rm -rf .git/objects/commit-graph*" &&
350 git commit-graph write --reachable &&
351 test_must_fail git branch -d to-delete 2>err &&
352 grep "not fully merged" err
355 test_expect_success 'git branch -v -d t should work' '
356 git branch t &&
357 git rev-parse --verify refs/heads/t &&
358 git branch -v -d t &&
359 test_must_fail git rev-parse --verify refs/heads/t
362 test_expect_success 'git branch -v -m t s should work' '
363 git branch t &&
364 git rev-parse --verify refs/heads/t &&
365 git branch -v -m t s &&
366 test_must_fail git rev-parse --verify refs/heads/t &&
367 git rev-parse --verify refs/heads/s &&
368 git branch -d s
371 test_expect_success 'git branch -m -d t s should fail' '
372 git branch t &&
373 git rev-parse refs/heads/t &&
374 test_must_fail git branch -m -d t s &&
375 git branch -d t &&
376 test_must_fail git rev-parse refs/heads/t
379 test_expect_success 'git branch --list -d t should fail' '
380 git branch t &&
381 git rev-parse refs/heads/t &&
382 test_must_fail git branch --list -d t &&
383 git branch -d t &&
384 test_must_fail git rev-parse refs/heads/t
387 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
388 test_when_finished "rm -rf repo1 repo2" &&
390 git init repo1 &&
391 git init repo1/sub &&
392 test_commit -C repo1/sub x &&
393 test_config_global protocol.file.allow always &&
394 git -C repo1 submodule add ./sub &&
395 git -C repo1 commit -m "adding sub" &&
397 git clone --recurse-submodules repo1 repo2 &&
398 git -C repo2/sub checkout -b work &&
399 test_must_fail git -C repo2/sub branch -D work
402 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
403 test_when_finished "rm -rf nonbare base secondary" &&
405 git init -b main nonbare &&
406 test_commit -C nonbare x &&
407 git clone --bare nonbare bare &&
408 git -C bare worktree add --detach ../secondary main &&
409 git -C secondary branch -D main
412 test_expect_success 'git branch --list -v with --abbrev' '
413 test_when_finished "git branch -D t" &&
414 git branch t &&
415 git branch -v --list t >actual.default &&
416 git branch -v --list --abbrev t >actual.abbrev &&
417 test_cmp actual.default actual.abbrev &&
419 git branch -v --list --no-abbrev t >actual.noabbrev &&
420 git branch -v --list --abbrev=0 t >actual.0abbrev &&
421 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
422 test_cmp actual.noabbrev actual.0abbrev &&
423 test_cmp actual.noabbrev actual.noabbrev-conf &&
425 git branch -v --list --abbrev=36 t >actual.36abbrev &&
426 # how many hexdigits are used?
427 read name objdefault rest <actual.abbrev &&
428 read name obj36 rest <actual.36abbrev &&
429 objfull=$(git rev-parse --verify t) &&
431 # are we really getting abbreviations?
432 test "$obj36" != "$objdefault" &&
433 expr "$obj36" : "$objdefault" >/dev/null &&
434 test "$objfull" != "$obj36" &&
435 expr "$objfull" : "$obj36" >/dev/null
439 test_expect_success 'git branch --column' '
440 COLUMNS=81 git branch --column=column >actual &&
441 cat >expect <<\EOF &&
442 a/b/c bam foo l * main n o/p r
443 abc bar j/k m/m mb o/o q topic
445 test_cmp expect actual
448 test_expect_success 'git branch --column with an extremely long branch name' '
449 long=this/is/a/part/of/long/branch/name &&
450 long=z$long/$long/$long/$long &&
451 test_when_finished "git branch -d $long" &&
452 git branch $long &&
453 COLUMNS=80 git branch --column=column >actual &&
454 cat >expect <<EOF &&
455 a/b/c
463 * main
470 topic
471 $long
473 test_cmp expect actual
476 test_expect_success 'git branch with column.*' '
477 git config column.ui column &&
478 git config column.branch "dense" &&
479 COLUMNS=80 git branch >actual &&
480 git config --unset column.branch &&
481 git config --unset column.ui &&
482 cat >expect <<\EOF &&
483 a/b/c bam foo l * main n o/p r
484 abc bar j/k m/m mb o/o q topic
486 test_cmp expect actual
489 test_expect_success 'git branch --column -v should fail' '
490 test_must_fail git branch --column -v
493 test_expect_success 'git branch -v with column.ui ignored' '
494 git config column.ui column &&
495 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
496 git config --unset column.ui &&
497 cat >expect <<\EOF &&
498 a/b/c
506 * main
513 topic
515 test_cmp expect actual
518 mv .git/config .git/config-saved
520 test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
521 git branch -m q q2 &&
522 git branch -m q2 q
525 mv .git/config-saved .git/config
527 git config branch.s/s.dummy Hello
529 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
530 git branch --create-reflog s/s &&
531 git reflog exists refs/heads/s/s &&
532 git branch --create-reflog s/t &&
533 git reflog exists refs/heads/s/t &&
534 git branch -d s/t &&
535 git branch -m s/s s &&
536 git reflog exists refs/heads/s
539 test_expect_success 'config information was renamed, too' '
540 test $(git config branch.s.dummy) = Hello &&
541 test_must_fail git config branch.s/s.dummy
544 test_expect_success 'git branch -m correctly renames multiple config sections' '
545 test_when_finished "git checkout main" &&
546 git checkout -b source main &&
548 # Assert that a config file with multiple config sections has
549 # those sections preserved...
550 cat >expect <<-\EOF &&
551 branch.dest.key1=value1
552 some.gar.b=age
553 branch.dest.key2=value2
555 cat >config.branch <<\EOF &&
556 ;; Note the lack of -\EOF above & mixed indenting here. This is
557 ;; intentional, we are also testing that the formatting of copied
558 ;; sections is preserved.
560 ;; Comment for source. Tabs
561 [branch "source"]
562 ;; Comment for the source value
563 key1 = value1
564 ;; Comment for some.gar. Spaces
565 [some "gar"]
566 ;; Comment for the some.gar value
567 b = age
568 ;; Comment for source, again. Mixed tabs/spaces.
569 [branch "source"]
570 ;; Comment for the source value, again
571 key2 = value2
573 cat config.branch >>.git/config &&
574 git branch -m source dest &&
575 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
576 test_cmp expect actual &&
578 # ...and that the comments for those sections are also
579 # preserved.
580 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
581 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
582 test_cmp expect actual
585 test_expect_success 'git branch -c dumps usage' '
586 test_expect_code 128 git branch -c 2>err &&
587 test_grep "branch name required" err
590 test_expect_success 'git branch --copy dumps usage' '
591 test_expect_code 128 git branch --copy 2>err &&
592 test_grep "branch name required" err
595 test_expect_success 'git branch -c d e should work' '
596 git branch --create-reflog d &&
597 git reflog exists refs/heads/d &&
598 git config branch.d.dummy Hello &&
599 git branch -c d e &&
600 git reflog exists refs/heads/d &&
601 git reflog exists refs/heads/e &&
602 echo Hello >expect &&
603 git config branch.e.dummy >actual &&
604 test_cmp expect actual &&
605 echo Hello >expect &&
606 git config branch.d.dummy >actual &&
607 test_cmp expect actual
610 test_expect_success 'git branch --copy is a synonym for -c' '
611 git branch --create-reflog copy &&
612 git reflog exists refs/heads/copy &&
613 git config branch.copy.dummy Hello &&
614 git branch --copy copy copy-to &&
615 git reflog exists refs/heads/copy &&
616 git reflog exists refs/heads/copy-to &&
617 echo Hello >expect &&
618 git config branch.copy.dummy >actual &&
619 test_cmp expect actual &&
620 echo Hello >expect &&
621 git config branch.copy-to.dummy >actual &&
622 test_cmp expect actual
625 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
626 git checkout -b ee &&
627 git reflog exists refs/heads/ee &&
628 git config branch.ee.dummy Hello &&
629 git branch -c ee ef &&
630 git reflog exists refs/heads/ee &&
631 git reflog exists refs/heads/ef &&
632 test $(git config branch.ee.dummy) = Hello &&
633 test $(git config branch.ef.dummy) = Hello &&
634 test $(git rev-parse --abbrev-ref HEAD) = ee
637 test_expect_success 'git branch -c f/f g/g should work' '
638 git branch --create-reflog f/f &&
639 git reflog exists refs/heads/f/f &&
640 git config branch.f/f.dummy Hello &&
641 git branch -c f/f g/g &&
642 git reflog exists refs/heads/f/f &&
643 git reflog exists refs/heads/g/g &&
644 test $(git config branch.f/f.dummy) = Hello &&
645 test $(git config branch.g/g.dummy) = Hello
648 test_expect_success 'git branch -c m2 m2 should work' '
649 git branch --create-reflog m2 &&
650 git reflog exists refs/heads/m2 &&
651 git config branch.m2.dummy Hello &&
652 git branch -c m2 m2 &&
653 git reflog exists refs/heads/m2 &&
654 test $(git config branch.m2.dummy) = Hello
657 test_expect_success 'git branch -c zz zz/zz should fail' '
658 git branch --create-reflog zz &&
659 git reflog exists refs/heads/zz &&
660 test_must_fail git branch -c zz zz/zz
663 test_expect_success 'git branch -c b/b b should fail' '
664 git branch --create-reflog b/b &&
665 test_must_fail git branch -c b/b b
668 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
669 git branch --create-reflog o/q &&
670 git reflog exists refs/heads/o/q &&
671 git reflog exists refs/heads/o/p &&
672 git branch -C o/q o/p
675 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
676 git reflog exists refs/heads/o/q &&
677 git reflog exists refs/heads/o/p &&
678 git branch -c -f o/q o/p
681 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
682 git branch qq &&
683 git branch rr &&
684 test_must_fail git branch -c qq rr/qq
687 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
688 git branch b1 &&
689 git checkout -b b2 &&
690 test_must_fail git branch -C b1 b2
693 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
694 git checkout -b c1 &&
695 git branch c2 &&
696 git branch -C c1 c2 &&
697 test $(git rev-parse --abbrev-ref HEAD) = c1
700 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
701 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
702 ! grep "$msg$" .git/logs/HEAD
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 test_path_is_file .git/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 SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
837 git branch --create-reflog u &&
838 mv .git/logs/refs/heads/u real-u &&
839 ln -s real-u .git/logs/refs/heads/u &&
840 test_must_fail git branch -m u v
843 test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' '
844 test_when_finished "rm -rf subdir" &&
845 git init --bare subdir &&
847 rm -rfv subdir/refs subdir/objects subdir/packed-refs &&
848 ln -s ../.git/refs subdir/refs &&
849 ln -s ../.git/objects subdir/objects &&
850 ln -s ../.git/packed-refs subdir/packed-refs &&
852 git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
853 git rev-parse --absolute-git-dir >our.dir &&
854 ! test_cmp subdir.dir our.dir &&
856 git -C subdir log &&
857 git -C subdir branch rename-src &&
858 git rev-parse rename-src >expect &&
859 git -C subdir branch -m rename-src rename-dest &&
860 git rev-parse rename-dest >actual &&
861 test_cmp expect actual &&
862 git branch -D rename-dest
865 test_expect_success 'test tracking setup via --track' '
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 --track my1 local/main &&
870 test $(git config branch.my1.remote) = local &&
871 test $(git config branch.my1.merge) = refs/heads/main
874 test_expect_success 'test tracking setup (non-wildcard, matching)' '
875 git config remote.local.url . &&
876 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
877 (git show-ref -q refs/remotes/local/main || git fetch local) &&
878 git branch --track my4 local/main &&
879 test $(git config branch.my4.remote) = local &&
880 test $(git config branch.my4.merge) = refs/heads/main
883 test_expect_success 'tracking setup fails on non-matching refspec' '
884 git config remote.local.url . &&
885 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
886 (git show-ref -q refs/remotes/local/main || git fetch local) &&
887 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
888 test_must_fail git branch --track my5 local/main &&
889 test_must_fail git config branch.my5.remote &&
890 test_must_fail git config branch.my5.merge
893 test_expect_success 'test tracking setup via config' '
894 git config branch.autosetupmerge true &&
895 git config remote.local.url . &&
896 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
897 (git show-ref -q refs/remotes/local/main || git fetch local) &&
898 git branch my3 local/main &&
899 test $(git config branch.my3.remote) = local &&
900 test $(git config branch.my3.merge) = refs/heads/main
903 test_expect_success 'test overriding tracking setup via --no-track' '
904 git config branch.autosetupmerge true &&
905 git config remote.local.url . &&
906 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
907 (git show-ref -q refs/remotes/local/main || git fetch local) &&
908 git branch --no-track my2 local/main &&
909 git config branch.autosetupmerge false &&
910 ! test "$(git config branch.my2.remote)" = local &&
911 ! test "$(git config branch.my2.merge)" = refs/heads/main
914 test_expect_success 'no tracking without .fetch entries' '
915 git config branch.autosetupmerge true &&
916 git branch my6 s &&
917 git config branch.autosetupmerge false &&
918 test -z "$(git config branch.my6.remote)" &&
919 test -z "$(git config branch.my6.merge)"
922 test_expect_success 'test tracking setup via --track but deeper' '
923 git config remote.local.url . &&
924 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
925 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
926 git branch --track my7 local/o/o &&
927 test "$(git config branch.my7.remote)" = local &&
928 test "$(git config branch.my7.merge)" = refs/heads/o/o
931 test_expect_success 'test deleting branch deletes branch config' '
932 git branch -d my7 &&
933 test -z "$(git config branch.my7.remote)" &&
934 test -z "$(git config branch.my7.merge)"
937 test_expect_success 'test deleting branch without config' '
938 git branch my7 s &&
939 sha1=$(git rev-parse my7 | cut -c 1-7) &&
940 echo "Deleted branch my7 (was $sha1)." >expect &&
941 git branch -d my7 >actual 2>&1 &&
942 test_cmp expect actual
945 test_expect_success 'deleting currently checked out branch fails' '
946 git worktree add -b my7 my7 &&
947 test_must_fail git -C my7 branch -d my7 &&
948 test_must_fail git branch -d my7 2>actual &&
949 grep "^error: cannot delete branch .my7. used by worktree at " actual &&
950 rm -r my7 &&
951 git worktree prune
954 test_expect_success 'deleting in-use branch fails' '
955 git worktree add my7 &&
956 test_commit -C my7 bt7 &&
957 git -C my7 bisect start HEAD HEAD~2 &&
958 test_must_fail git -C my7 branch -d my7 &&
959 test_must_fail git branch -d my7 2>actual &&
960 grep "^error: cannot delete branch .my7. used by worktree at " actual &&
961 rm -r my7 &&
962 git worktree prune
965 test_expect_success 'test --track without .fetch entries' '
966 git branch --track my8 &&
967 test "$(git config branch.my8.remote)" &&
968 test "$(git config branch.my8.merge)"
971 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
972 git config branch.autosetupmerge always &&
973 git branch my9 HEAD^ &&
974 git config branch.autosetupmerge false
977 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
978 test_must_fail git branch --track my10 HEAD^
981 test_expect_success 'branch from tag w/--track causes failure' '
982 git tag foobar &&
983 test_must_fail git branch --track my11 foobar
986 test_expect_success 'simple tracking works when remote branch name matches' '
987 test_when_finished "rm -rf otherserver" &&
988 git init otherserver &&
989 test_commit -C otherserver my_commit 1 &&
990 git -C otherserver branch feature &&
991 test_config branch.autosetupmerge simple &&
992 test_config remote.otherserver.url otherserver &&
993 test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* &&
994 git fetch otherserver &&
995 git branch feature otherserver/feature &&
996 test_cmp_config otherserver branch.feature.remote &&
997 test_cmp_config refs/heads/feature branch.feature.merge
1000 test_expect_success 'simple tracking skips when remote branch name does not match' '
1001 test_config branch.autosetupmerge simple &&
1002 test_config remote.local.url . &&
1003 test_config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1004 git fetch local &&
1005 git branch my-other local/main &&
1006 test_cmp_config "" --default "" branch.my-other.remote &&
1007 test_cmp_config "" --default "" branch.my-other.merge
1010 test_expect_success 'simple tracking skips when remote ref is not a branch' '
1011 test_config branch.autosetupmerge simple &&
1012 test_config remote.localtags.url . &&
1013 test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* &&
1014 git tag mytag12 main &&
1015 git fetch localtags &&
1016 git branch mytag12 localtags/mytag12 &&
1017 test_cmp_config "" --default "" branch.mytag12.remote &&
1018 test_cmp_config "" --default "" branch.mytag12.merge
1021 test_expect_success '--set-upstream-to fails on multiple branches' '
1022 echo "fatal: too many arguments to set new upstream" >expect &&
1023 test_must_fail git branch --set-upstream-to main a b c 2>err &&
1024 test_cmp expect err
1027 test_expect_success '--set-upstream-to fails on detached HEAD' '
1028 git checkout HEAD^{} &&
1029 test_when_finished git checkout - &&
1030 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch" >expect &&
1031 test_must_fail git branch --set-upstream-to main 2>err &&
1032 test_cmp expect err
1035 test_expect_success '--set-upstream-to fails on a missing dst branch' '
1036 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
1037 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
1038 test_cmp expect err
1041 test_expect_success '--set-upstream-to fails on a missing src branch' '
1042 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
1043 test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
1046 test_expect_success '--set-upstream-to fails on a non-ref' '
1047 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
1048 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
1049 test_cmp expect err
1052 test_expect_success '--set-upstream-to fails on locked config' '
1053 test_when_finished "rm -f .git/config.lock" &&
1054 >.git/config.lock &&
1055 git branch locked &&
1056 test_must_fail git branch --set-upstream-to locked 2>err &&
1057 test_grep "could not lock config file .git/config" err
1060 test_expect_success 'use --set-upstream-to modify HEAD' '
1061 test_config branch.main.remote foo &&
1062 test_config branch.main.merge foo &&
1063 git branch my12 &&
1064 git branch --set-upstream-to my12 &&
1065 test "$(git config branch.main.remote)" = "." &&
1066 test "$(git config branch.main.merge)" = "refs/heads/my12"
1069 test_expect_success 'use --set-upstream-to modify a particular branch' '
1070 git branch my13 &&
1071 git branch --set-upstream-to main my13 &&
1072 test_when_finished "git branch --unset-upstream my13" &&
1073 test "$(git config branch.my13.remote)" = "." &&
1074 test "$(git config branch.my13.merge)" = "refs/heads/main"
1077 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
1078 echo "fatal: branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
1079 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
1080 test_cmp expect err
1083 test_expect_success '--unset-upstream should fail if config is locked' '
1084 test_when_finished "rm -f .git/config.lock" &&
1085 git branch --set-upstream-to locked &&
1086 >.git/config.lock &&
1087 test_must_fail git branch --unset-upstream 2>err &&
1088 test_grep "could not lock config file .git/config" err
1091 test_expect_success 'test --unset-upstream on HEAD' '
1092 git branch my14 &&
1093 test_config branch.main.remote foo &&
1094 test_config branch.main.merge foo &&
1095 git branch --set-upstream-to my14 &&
1096 git branch --unset-upstream &&
1097 test_must_fail git config branch.main.remote &&
1098 test_must_fail git config branch.main.merge &&
1099 # fail for a branch without upstream set
1100 echo "fatal: branch '"'"'main'"'"' has no upstream information" >expect &&
1101 test_must_fail git branch --unset-upstream 2>err &&
1102 test_cmp expect err
1105 test_expect_success '--unset-upstream should fail on multiple branches' '
1106 echo "fatal: too many arguments to unset upstream" >expect &&
1107 test_must_fail git branch --unset-upstream a b c 2>err &&
1108 test_cmp expect err
1111 test_expect_success '--unset-upstream should fail on detached HEAD' '
1112 git checkout HEAD^{} &&
1113 test_when_finished git checkout - &&
1114 echo "fatal: could not unset upstream of HEAD when it does not point to any branch" >expect &&
1115 test_must_fail git branch --unset-upstream 2>err &&
1116 test_cmp expect err
1119 test_expect_success 'test --unset-upstream on a particular branch' '
1120 git branch my15 &&
1121 git branch --set-upstream-to main my14 &&
1122 git branch --unset-upstream my14 &&
1123 test_must_fail git config branch.my14.remote &&
1124 test_must_fail git config branch.my14.merge
1127 test_expect_success 'disabled option --set-upstream fails' '
1128 test_must_fail git branch --set-upstream origin/main
1131 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
1132 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1133 cat >expect <<-\EOF &&
1134 warning: not setting branch 'my13' as its own upstream
1136 test_expect_code 1 git config branch.my13.remote &&
1137 test_expect_code 1 git config branch.my13.merge &&
1138 test_cmp expect actual
1141 # Keep this test last, as it changes the current branch
1142 cat >expect <<EOF
1143 $ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
1145 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
1146 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1147 git checkout -b g/h/i -l main &&
1148 test_ref_exists refs/heads/g/h/i &&
1149 test_path_is_file .git/logs/refs/heads/g/h/i &&
1150 test_cmp expect .git/logs/refs/heads/g/h/i
1153 test_expect_success 'checkout -b makes reflog by default' '
1154 git checkout main &&
1155 git config --unset core.logAllRefUpdates &&
1156 git checkout -b alpha &&
1157 git rev-parse --verify alpha@{0}
1160 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1161 git checkout main &&
1162 git config core.logAllRefUpdates false &&
1163 git checkout -b beta &&
1164 test_must_fail git rev-parse --verify beta@{0}
1167 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1168 git checkout main &&
1169 git checkout -lb gamma &&
1170 git config --unset core.logAllRefUpdates &&
1171 git rev-parse --verify gamma@{0}
1174 test_expect_success 'avoid ambiguous track and advise' '
1175 git config branch.autosetupmerge true &&
1176 git config remote.ambi1.url lalala &&
1177 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1178 git config remote.ambi2.url lilili &&
1179 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1180 cat <<-EOF >expected &&
1181 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1182 hint: There are multiple remotes whose fetch refspecs map to the remote
1183 hint: tracking ref '\''refs/heads/main'\'':
1184 hint: ambi1
1185 hint: ambi2
1186 hint: ''
1187 hint: This is typically a configuration error.
1188 hint: ''
1189 hint: To support setting up tracking branches, ensure that
1190 hint: different remotes'\'' fetch refspecs map into different
1191 hint: tracking namespaces.
1193 test_must_fail git branch all1 main 2>actual &&
1194 test_cmp expected actual &&
1195 test -z "$(git config branch.all1.merge)"
1198 test_expect_success 'autosetuprebase local on a tracked local branch' '
1199 git config remote.local.url . &&
1200 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1201 git config branch.autosetuprebase local &&
1202 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1203 git branch mybase &&
1204 git branch --track myr1 mybase &&
1205 test "$(git config branch.myr1.remote)" = . &&
1206 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1207 test "$(git config branch.myr1.rebase)" = true
1210 test_expect_success 'autosetuprebase always on a tracked local branch' '
1211 git config remote.local.url . &&
1212 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1213 git config branch.autosetuprebase always &&
1214 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1215 git branch mybase2 &&
1216 git branch --track myr2 mybase &&
1217 test "$(git config branch.myr2.remote)" = . &&
1218 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1219 test "$(git config branch.myr2.rebase)" = true
1222 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1223 git config remote.local.url . &&
1224 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1225 git config branch.autosetuprebase remote &&
1226 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1227 git branch mybase3 &&
1228 git branch --track myr3 mybase2 &&
1229 test "$(git config branch.myr3.remote)" = . &&
1230 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1231 ! test "$(git config branch.myr3.rebase)" = true
1234 test_expect_success 'autosetuprebase never on a tracked local branch' '
1235 git config remote.local.url . &&
1236 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1237 git config branch.autosetuprebase never &&
1238 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1239 git branch mybase4 &&
1240 git branch --track myr4 mybase2 &&
1241 test "$(git config branch.myr4.remote)" = . &&
1242 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1243 ! test "$(git config branch.myr4.rebase)" = true
1246 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1247 git config remote.local.url . &&
1248 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1249 git config branch.autosetuprebase local &&
1250 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1251 git branch --track myr5 local/main &&
1252 test "$(git config branch.myr5.remote)" = local &&
1253 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1254 ! test "$(git config branch.myr5.rebase)" = true
1257 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1258 git config remote.local.url . &&
1259 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1260 git config branch.autosetuprebase never &&
1261 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1262 git branch --track myr6 local/main &&
1263 test "$(git config branch.myr6.remote)" = local &&
1264 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1265 ! test "$(git config branch.myr6.rebase)" = true
1268 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1269 git config remote.local.url . &&
1270 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1271 git config branch.autosetuprebase remote &&
1272 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1273 git branch --track myr7 local/main &&
1274 test "$(git config branch.myr7.remote)" = local &&
1275 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1276 test "$(git config branch.myr7.rebase)" = true
1279 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1280 git config remote.local.url . &&
1281 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1282 git config branch.autosetuprebase remote &&
1283 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1284 git branch --track myr8 local/main &&
1285 test "$(git config branch.myr8.remote)" = local &&
1286 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1287 test "$(git config branch.myr8.rebase)" = true
1290 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1291 git config --unset branch.autosetuprebase &&
1292 git config remote.local.url . &&
1293 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1294 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1295 git branch --track myr9 local/main &&
1296 test "$(git config branch.myr9.remote)" = local &&
1297 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1298 test "z$(git config branch.myr9.rebase)" = z
1301 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1302 git config remote.local.url . &&
1303 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1304 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1305 git branch mybase10 &&
1306 git branch --track myr10 mybase2 &&
1307 test "$(git config branch.myr10.remote)" = . &&
1308 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1309 test "z$(git config branch.myr10.rebase)" = z
1312 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1313 git config remote.local.url . &&
1314 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1315 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1316 git branch --no-track myr11 mybase2 &&
1317 test "z$(git config branch.myr11.remote)" = z &&
1318 test "z$(git config branch.myr11.merge)" = z &&
1319 test "z$(git config branch.myr11.rebase)" = z
1322 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1323 git config remote.local.url . &&
1324 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1325 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1326 git branch --no-track myr12 local/main &&
1327 test "z$(git config branch.myr12.remote)" = z &&
1328 test "z$(git config branch.myr12.merge)" = z &&
1329 test "z$(git config branch.myr12.rebase)" = z
1332 test_expect_success 'autosetuprebase never on an untracked local branch' '
1333 git config branch.autosetuprebase never &&
1334 git config remote.local.url . &&
1335 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1336 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1337 git branch --no-track myr13 mybase2 &&
1338 test "z$(git config branch.myr13.remote)" = z &&
1339 test "z$(git config branch.myr13.merge)" = z &&
1340 test "z$(git config branch.myr13.rebase)" = z
1343 test_expect_success 'autosetuprebase local on an untracked local branch' '
1344 git config branch.autosetuprebase local &&
1345 git config remote.local.url . &&
1346 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1347 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1348 git branch --no-track myr14 mybase2 &&
1349 test "z$(git config branch.myr14.remote)" = z &&
1350 test "z$(git config branch.myr14.merge)" = z &&
1351 test "z$(git config branch.myr14.rebase)" = z
1354 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1355 git config branch.autosetuprebase remote &&
1356 git config remote.local.url . &&
1357 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1358 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1359 git branch --no-track myr15 mybase2 &&
1360 test "z$(git config branch.myr15.remote)" = z &&
1361 test "z$(git config branch.myr15.merge)" = z &&
1362 test "z$(git config branch.myr15.rebase)" = z
1365 test_expect_success 'autosetuprebase always on an untracked local branch' '
1366 git config branch.autosetuprebase always &&
1367 git config remote.local.url . &&
1368 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1369 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1370 git branch --no-track myr16 mybase2 &&
1371 test "z$(git config branch.myr16.remote)" = z &&
1372 test "z$(git config branch.myr16.merge)" = z &&
1373 test "z$(git config branch.myr16.rebase)" = z
1376 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1377 git config branch.autosetuprebase never &&
1378 git config remote.local.url . &&
1379 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1380 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1381 git branch --no-track myr17 local/main &&
1382 test "z$(git config branch.myr17.remote)" = z &&
1383 test "z$(git config branch.myr17.merge)" = z &&
1384 test "z$(git config branch.myr17.rebase)" = z
1387 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1388 git config branch.autosetuprebase local &&
1389 git config remote.local.url . &&
1390 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1391 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1392 git branch --no-track myr18 local/main &&
1393 test "z$(git config branch.myr18.remote)" = z &&
1394 test "z$(git config branch.myr18.merge)" = z &&
1395 test "z$(git config branch.myr18.rebase)" = z
1398 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1399 git config branch.autosetuprebase remote &&
1400 git config remote.local.url . &&
1401 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1402 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1403 git branch --no-track myr19 local/main &&
1404 test "z$(git config branch.myr19.remote)" = z &&
1405 test "z$(git config branch.myr19.merge)" = z &&
1406 test "z$(git config branch.myr19.rebase)" = z
1409 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1410 git config branch.autosetuprebase always &&
1411 git config remote.local.url . &&
1412 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1413 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1414 git branch --no-track myr20 local/main &&
1415 test "z$(git config branch.myr20.remote)" = z &&
1416 test "z$(git config branch.myr20.merge)" = z &&
1417 test "z$(git config branch.myr20.rebase)" = z
1420 test_expect_success 'autosetuprebase always on detached HEAD' '
1421 git config branch.autosetupmerge always &&
1422 test_when_finished git checkout main &&
1423 git checkout HEAD^0 &&
1424 git branch my11 &&
1425 test -z "$(git config branch.my11.remote)" &&
1426 test -z "$(git config branch.my11.merge)"
1429 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1430 git config branch.autosetuprebase garbage &&
1431 test_must_fail git branch
1434 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1435 git config --unset branch.autosetuprebase &&
1436 echo "[branch] autosetuprebase" >>.git/config &&
1437 test_must_fail git branch &&
1438 git config --unset branch.autosetuprebase
1441 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1442 git checkout my9 &&
1443 git config --unset branch.my8.merge &&
1444 test_must_fail git branch -d my8
1447 test_expect_success 'attempt to delete a branch merged to its base' '
1448 # we are on my9 which is the initial commit; traditionally
1449 # we would not have allowed deleting my8 that is not merged
1450 # to my9, but it is set to track main that already has my8
1451 git config branch.my8.merge refs/heads/main &&
1452 git branch -d my8
1455 test_expect_success 'attempt to delete a branch merged to its base' '
1456 git checkout main &&
1457 echo Third >>A &&
1458 git commit -m "Third commit" A &&
1459 git branch -t my10 my9 &&
1460 git branch -f my10 HEAD^ &&
1461 # we are on main which is at the third commit, and my10
1462 # is behind us, so traditionally we would have allowed deleting
1463 # it; but my10 is set to track my9 that is further behind.
1464 test_must_fail git branch -d my10
1467 test_expect_success 'branch --delete --force removes dangling branch' '
1468 git checkout main &&
1469 test_commit unstable &&
1470 hash=$(git rev-parse HEAD) &&
1471 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1472 git branch --no-track dangling &&
1473 mv $objpath $objpath.x &&
1474 test_when_finished "mv $objpath.x $objpath" &&
1475 git branch --delete --force dangling &&
1476 git for-each-ref refs/heads/dangling >actual &&
1477 test_must_be_empty actual
1480 test_expect_success 'use --edit-description' '
1481 EDITOR=: git branch --edit-description &&
1482 test_expect_code 1 git config branch.main.description &&
1484 write_script editor <<-\EOF &&
1485 echo "New contents" >"$1"
1487 EDITOR=./editor git branch --edit-description &&
1488 write_script editor <<-\EOF &&
1489 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1491 EDITOR=./editor git branch --edit-description &&
1492 echo "New contents" >expect &&
1493 test_cmp expect EDITOR_OUTPUT
1496 test_expect_success 'detect typo in branch name when using --edit-description' '
1497 write_script editor <<-\EOF &&
1498 echo "New contents" >"$1"
1500 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1503 test_expect_success 'refuse --edit-description on unborn branch for now' '
1504 test_when_finished "git checkout main" &&
1505 write_script editor <<-\EOF &&
1506 echo "New contents" >"$1"
1508 git checkout --orphan unborn &&
1509 test_must_fail env EDITOR=./editor git branch --edit-description
1512 test_expect_success '--merged catches invalid object names' '
1513 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1516 test_expect_success '--list during rebase' '
1517 test_when_finished "reset_rebase" &&
1518 git checkout main &&
1519 FAKE_LINES="1 edit 2" &&
1520 export FAKE_LINES &&
1521 set_fake_editor &&
1522 git rebase -i HEAD~2 &&
1523 git branch --list >actual &&
1524 test_grep "rebasing main" actual
1527 test_expect_success '--list during rebase from detached HEAD' '
1528 test_when_finished "reset_rebase && git checkout main" &&
1529 git checkout main^0 &&
1530 oid=$(git rev-parse --short HEAD) &&
1531 FAKE_LINES="1 edit 2" &&
1532 export FAKE_LINES &&
1533 set_fake_editor &&
1534 git rebase -i HEAD~2 &&
1535 git branch --list >actual &&
1536 test_grep "rebasing detached HEAD $oid" actual
1539 test_expect_success 'tracking with unexpected .fetch refspec' '
1540 rm -rf a b c d &&
1541 git init -b main a &&
1543 cd a &&
1544 test_commit a
1545 ) &&
1546 git init -b main b &&
1548 cd b &&
1549 test_commit b
1550 ) &&
1551 git init -b main c &&
1553 cd c &&
1554 test_commit c &&
1555 git remote add a ../a &&
1556 git remote add b ../b &&
1557 git fetch --all
1558 ) &&
1559 git init -b main d &&
1561 cd d &&
1562 git remote add c ../c &&
1563 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1564 git fetch c &&
1565 git branch --track local/a/main remotes/a/main &&
1566 test "$(git config branch.local/a/main.remote)" = "c" &&
1567 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1568 git rev-parse --verify a >expect &&
1569 git rev-parse --verify local/a/main >actual &&
1570 test_cmp expect actual
1574 test_expect_success 'configured committerdate sort' '
1575 git init -b main sort &&
1577 cd sort &&
1578 git config branch.sort committerdate &&
1579 test_commit initial &&
1580 git checkout -b a &&
1581 test_commit a &&
1582 git checkout -b c &&
1583 test_commit c &&
1584 git checkout -b b &&
1585 test_commit b &&
1586 git branch >actual &&
1587 cat >expect <<-\EOF &&
1588 main
1593 test_cmp expect actual
1597 test_expect_success 'option override configured sort' '
1599 cd sort &&
1600 git config branch.sort committerdate &&
1601 git branch --sort=refname >actual &&
1602 cat >expect <<-\EOF &&
1606 main
1608 test_cmp expect actual
1612 test_expect_success 'invalid sort parameter in configuration' '
1614 cd sort &&
1615 git config branch.sort "v:notvalid" &&
1617 # this works in the "listing" mode, so bad sort key
1618 # is a dying offence.
1619 test_must_fail git branch &&
1621 # these do not need to use sorting, and should all
1622 # succeed
1623 git branch newone main &&
1624 git branch -c newone newerone &&
1625 git branch -m newone newestone &&
1626 git branch -d newerone newestone
1630 test_expect_success 'tracking info copied with --track=inherit' '
1631 git branch --track=inherit foo2 my1 &&
1632 test_cmp_config local branch.foo2.remote &&
1633 test_cmp_config refs/heads/main branch.foo2.merge
1636 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1637 test_unconfig branch.autoSetupMerge &&
1638 # default config does not copy tracking info
1639 git branch foo-no-inherit my1 &&
1640 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1641 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1642 # with autoSetupMerge=inherit, we copy tracking info from my1
1643 test_config branch.autoSetupMerge inherit &&
1644 git branch foo3 my1 &&
1645 test_cmp_config local branch.foo3.remote &&
1646 test_cmp_config refs/heads/main branch.foo3.merge &&
1647 # no tracking info to inherit from main
1648 git branch main2 main &&
1649 test_cmp_config "" --default "" branch.main2.remote &&
1650 test_cmp_config "" --default "" branch.main2.merge
1653 test_expect_success '--track overrides branch.autoSetupMerge' '
1654 test_config branch.autoSetupMerge inherit &&
1655 git branch --track=direct foo4 my1 &&
1656 test_cmp_config . branch.foo4.remote &&
1657 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1658 git branch --no-track foo5 my1 &&
1659 test_cmp_config "" --default "" branch.foo5.remote &&
1660 test_cmp_config "" --default "" branch.foo5.merge
1663 test_done