Raw
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Amos Waterland
4 #
5
6 test_description='git branch assorted tests'
7
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
11 . ./test-lib.sh
12 . "$TEST_DIRECTORY"/lib-rebase.sh
13
14 test_expect_success 'prepare a trivial repository' '
15 echo Hello >A &&
16 git update-index --add A &&
17 git commit -m "Initial commit." &&
18 git branch -M main &&
19 echo World >>A &&
20 git update-index --add A &&
21 git commit -m "Second commit." &&
22 HEAD=$(git rev-parse --verify HEAD)
23 '
24
25 test_expect_success 'git branch --help should not have created a bogus branch' '
26 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
27 test_ref_missing refs/heads/--help
28 '
29
30 test_expect_success REFFILES 'branch -h in broken repository' '
31 mkdir broken &&
32 (
33 cd broken &&
34 git init -b main &&
35 >.git/refs/heads/main &&
36 test_expect_code 129 git branch -h >usage 2>&1
37 ) &&
38 test_grep "[Uu]sage" broken/usage
39 '
40
41 test_expect_success 'git branch abc should create a branch' '
42 git branch abc &&
43 test_ref_exists refs/heads/abc
44 '
45
46 test_expect_success 'git branch abc should fail when abc exists' '
47 test_must_fail git branch abc
48 '
49
50 test_expect_success 'git branch --force abc should fail when abc is checked out' '
51 test_when_finished git switch main &&
52 git switch abc &&
53 test_must_fail git branch --force abc HEAD~1
54 '
55
56 test_expect_success 'git branch --force abc should succeed when abc exists' '
57 git rev-parse HEAD~1 >expect &&
58 git branch --force abc HEAD~1 &&
59 git rev-parse abc >actual &&
60 test_cmp expect actual
61 '
62
63 test_expect_success 'git branch a/b/c should create a branch' '
64 git branch a/b/c &&
65 test_ref_exists refs/heads/a/b/c
66 '
67
68 test_expect_success 'git branch mb main... should create a branch' '
69 git branch mb main... &&
70 test_ref_exists refs/heads/mb
71 '
72
73 test_expect_success 'git branch HEAD should fail' '
74 test_must_fail git branch HEAD
75 '
76
77 test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
78 GIT_COMMITTER_DATE="2005-05-26 23:30" \
79 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
80 test_ref_exists refs/heads/d/e/f &&
81 cat >expect <<-EOF &&
82 $HEAD refs/heads/d/e/f@{0}: branch: Created from main
83 EOF
84 git reflog show --no-abbrev-commit refs/heads/d/e/f >actual &&
85 test_cmp expect actual
86 '
87
88 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
89 git branch -d d/e/f &&
90 test_ref_missing refs/heads/d/e/f &&
91 test_must_fail git reflog exists refs/heads/d/e/f
92 '
93
94 test_expect_success 'git branch j/k should work after branch j has been deleted' '
95 git branch j &&
96 git branch -d j &&
97 git branch j/k
98 '
99
100 test_expect_success 'git branch l should work after branch l/m has been deleted' '
101 git branch l/m &&
102 git branch -d l/m &&
103 git branch l
104 '
105
106 test_expect_success 'git branch -m dumps usage' '
107 test_expect_code 128 git branch -m 2>err &&
108 test_grep "branch name required" err
109 '
110
111 test_expect_success 'git branch -m m broken_symref should work' '
112 test_when_finished "git branch -D broken_symref" &&
113 git branch --create-reflog m &&
114 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
115 git branch -m m broken_symref &&
116 git reflog exists refs/heads/broken_symref &&
117 test_must_fail git reflog exists refs/heads/i_am_broken
118 '
119
120 test_expect_success 'git branch -m m m/m should work' '
121 git branch --create-reflog m &&
122 git branch -m m m/m &&
123 git reflog exists refs/heads/m/m
124 '
125
126 test_expect_success 'git branch -m n/n n should work' '
127 git branch --create-reflog n/n &&
128 git branch -m n/n n &&
129 git reflog exists refs/heads/n
130 '
131
132 # The topmost entry in reflog for branch bbb is about branch creation.
133 # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
134
135 test_expect_success 'git branch -m bbb should rename checked out branch' '
136 test_when_finished git branch -D bbb &&
137 test_when_finished git checkout main &&
138 git checkout -b aaa &&
139 git commit --allow-empty -m "a new commit" &&
140 git rev-parse aaa@{0} >expect &&
141 git branch -m bbb &&
142 git rev-parse bbb@{1} >actual &&
143 test_cmp expect actual &&
144 git symbolic-ref HEAD >actual &&
145 echo refs/heads/bbb >expect &&
146 test_cmp expect actual
147 '
148
149 test_expect_success 'renaming checked out branch works with d/f conflict' '
150 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
151 test_when_finished git checkout main &&
152 git checkout -b foo &&
153 git branch -m foo/bar &&
154 git symbolic-ref HEAD >actual &&
155 echo refs/heads/foo/bar >expect &&
156 test_cmp expect actual
157 '
158
159 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
160 git branch o/o &&
161 git branch o/p &&
162 test_must_fail git branch -m o/o o
163 '
164
165 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
166 git branch o/q &&
167 test_must_fail git branch -m o/q o/p
168 '
169
170 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
171 git branch -M o/q o/p
172 '
173
174 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
175 git branch o/q &&
176 git branch -m -f o/q o/p
177 '
178
179 test_expect_success 'git branch -m q r/q should fail when r exists' '
180 git branch q &&
181 git branch r &&
182 test_must_fail git branch -m q r/q
183 '
184
185 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
186 git branch bar &&
187 git checkout -b foo &&
188 test_must_fail git branch -M bar foo
189 '
190
191 test_expect_success 'git branch -M foo bar should fail when bar is checked out in worktree' '
192 git branch -f bar &&
193 test_when_finished "git worktree remove wt && git branch -D wt" &&
194 git worktree add wt &&
195 test_must_fail git branch -M bar wt
196 '
197
198 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
199 git checkout -b baz &&
200 git branch bam &&
201 git branch -M baz bam &&
202 test $(git rev-parse --abbrev-ref HEAD) = bam
203 '
204
205 test_expect_success 'git branch -M baz bam should add entries to HEAD reflog' '
206 git reflog show HEAD >actual &&
207 grep "HEAD@{0}: Branch: renamed refs/heads/baz to refs/heads/bam" actual
208 '
209
210 test_expect_success 'git branch -M should leave orphaned HEAD alone' '
211 git init -b main orphan &&
212 (
213 cd orphan &&
214 test_commit initial &&
215 git checkout --orphan lonely &&
216 git symbolic-ref HEAD >expect &&
217 echo refs/heads/lonely >actual &&
218 test_cmp expect actual &&
219 test_ref_missing refs/head/lonely &&
220 git branch -M main mistress &&
221 git symbolic-ref HEAD >expect &&
222 test_cmp expect actual
223 )
224 '
225
226 test_expect_success 'resulting reflog can be shown by log -g' '
227 oid=$(git rev-parse HEAD) &&
228 cat >expect <<-EOF &&
229 HEAD@{0} $oid Branch: renamed refs/heads/baz to refs/heads/bam
230 HEAD@{2} $oid checkout: moving from foo to baz
231 EOF
232 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
233 test_cmp expect actual
234 '
235
236 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
237 git checkout main &&
238 git worktree add -b baz bazdir &&
239 git worktree add -f bazdir2 baz &&
240 git branch -M baz bam &&
241 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
242 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
243 rm -r bazdir bazdir2 &&
244 git worktree prune
245 '
246
247 test_expect_success REFFILES 'git branch -M fails if updating any linked working tree fails' '
248 git worktree add -b baz bazdir1 &&
249 git worktree add -f bazdir2 baz &&
250 touch .git/worktrees/bazdir1/HEAD.lock &&
251 test_must_fail git branch -M baz bam &&
252 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
253 git branch -M bam baz &&
254 rm .git/worktrees/bazdir1/HEAD.lock &&
255 touch .git/worktrees/bazdir2/HEAD.lock &&
256 test_must_fail git branch -M baz bam &&
257 test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam &&
258 rm -rf bazdir1 bazdir2 &&
259 git worktree prune
260 '
261
262 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
263 git checkout -b baz &&
264 git worktree add -f bazdir baz &&
265 (
266 cd bazdir &&
267 git branch -M baz bam &&
268 echo bam >expect &&
269 git rev-parse --abbrev-ref HEAD >actual &&
270 test_cmp expect actual
271 ) &&
272 echo bam >expect &&
273 git rev-parse --abbrev-ref HEAD >actual &&
274 test_cmp expect actual &&
275 rm -r bazdir &&
276 git worktree prune
277 '
278
279 test_expect_success 'git branch -M main should work when main is checked out' '
280 git checkout main &&
281 git branch -M main
282 '
283
284 test_expect_success 'git branch -M main main should work when main is checked out' '
285 git checkout main &&
286 git branch -M main main
287 '
288
289 test_expect_success 'git branch -M topic topic should work when main is checked out' '
290 git checkout main &&
291 git branch topic &&
292 git branch -M topic topic
293 '
294
295 test_expect_success 'git branch -M and -C fail on detached HEAD' '
296 git checkout HEAD^{} &&
297 test_when_finished git checkout - &&
298 echo "fatal: cannot rename the current branch while not on any" >expect &&
299 test_must_fail git branch -M must-fail 2>err &&
300 test_cmp expect err &&
301 echo "fatal: cannot copy the current branch while not on any" >expect &&
302 test_must_fail git branch -C must-fail 2>err &&
303 test_cmp expect err
304 '
305
306 test_expect_success 'git branch -m should work with orphan branches' '
307 test_when_finished git checkout - &&
308 test_when_finished git worktree remove -f wt &&
309 git worktree add wt --detach &&
310 # rename orphan in another worktreee
311 git -C wt checkout --orphan orphan-foo-wt &&
312 git branch -m orphan-foo-wt orphan-bar-wt &&
313 test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) &&
314 # rename orphan in the current worktree
315 git checkout --orphan orphan-foo &&
316 git branch -m orphan-foo orphan-bar &&
317 test orphan-bar=$(git branch --show-current)
318 '
319
320 test_expect_success 'git branch -d on orphan HEAD (merged)' '
321 test_when_finished git checkout main &&
322 git checkout --orphan orphan &&
323 test_when_finished "rm -rf .git/objects/commit-graph*" &&
324 git commit-graph write --reachable &&
325 git branch --track to-delete main &&
326 git branch -d to-delete
327 '
328
329 test_expect_success 'git branch -d on orphan HEAD (merged, graph)' '
330 test_when_finished git checkout main &&
331 git checkout --orphan orphan &&
332 git branch --track to-delete main &&
333 git branch -d to-delete
334 '
335
336 test_expect_success 'git branch -d on orphan HEAD (unmerged)' '
337 test_when_finished git checkout main &&
338 git checkout --orphan orphan &&
339 test_when_finished "git branch -D to-delete" &&
340 git branch to-delete main &&
341 test_must_fail git branch -d to-delete 2>err &&
342 grep "not fully merged" err
343 '
344
345 test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' '
346 test_when_finished git checkout main &&
347 git checkout --orphan orphan &&
348 test_when_finished "git branch -D to-delete" &&
349 git branch to-delete main &&
350 test_when_finished "rm -rf .git/objects/commit-graph*" &&
351 git commit-graph write --reachable &&
352 test_must_fail git branch -d to-delete 2>err &&
353 grep "not fully merged" err
354 '
355
356 test_expect_success 'git branch -v -d t should work' '
357 git branch t &&
358 git rev-parse --verify refs/heads/t &&
359 git branch -v -d t &&
360 test_must_fail git rev-parse --verify refs/heads/t
361 '
362
363 test_expect_success 'git branch -v -m t s should work' '
364 git branch t &&
365 git rev-parse --verify refs/heads/t &&
366 git branch -v -m t s &&
367 test_must_fail git rev-parse --verify refs/heads/t &&
368 git rev-parse --verify refs/heads/s &&
369 git branch -d s
370 '
371
372 test_expect_success 'git branch -m -d t s should fail' '
373 git branch t &&
374 git rev-parse refs/heads/t &&
375 test_must_fail git branch -m -d t s &&
376 git branch -d t &&
377 test_must_fail git rev-parse refs/heads/t
378 '
379
380 test_expect_success 'git branch --list -d t should fail' '
381 git branch t &&
382 git rev-parse refs/heads/t &&
383 test_must_fail git branch --list -d t &&
384 git branch -d t &&
385 test_must_fail git rev-parse refs/heads/t
386 '
387
388 test_expect_success 'deleting checked-out branch from repo that is a submodule' '
389 test_when_finished "rm -rf repo1 repo2" &&
390
391 git init repo1 &&
392 git init repo1/sub &&
393 test_commit -C repo1/sub x &&
394 test_config_global protocol.file.allow always &&
395 git -C repo1 submodule add ./sub &&
396 git -C repo1 commit -m "adding sub" &&
397
398 git clone --recurse-submodules repo1 repo2 &&
399 git -C repo2/sub checkout -b work &&
400 test_must_fail git -C repo2/sub branch -D work
401 '
402
403 test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
404 test_when_finished "rm -rf nonbare base secondary" &&
405
406 git init -b main nonbare &&
407 test_commit -C nonbare x &&
408 git clone --bare nonbare bare &&
409 git -C bare worktree add --detach ../secondary main &&
410 git -C secondary branch -D main
411 '
412
413 test_expect_success 'secondary worktrees recognize core.bare=true in main config.worktree' '
414 test_when_finished "rm -rf bare_repo non_bare_repo secondary_worktree" &&
415 git init -b main non_bare_repo &&
416 test_commit -C non_bare_repo x &&
417
418 git clone --bare non_bare_repo bare_repo &&
419 git -C bare_repo config extensions.worktreeConfig true &&
420 git -C bare_repo config unset core.bare &&
421 git -C bare_repo config --worktree core.bare true &&
422
423 git -C bare_repo worktree add ../secondary_worktree &&
424 git -C secondary_worktree checkout main
425 '
426
427 test_expect_success 'git branch --list -v with --abbrev' '
428 test_when_finished "git branch -D t" &&
429 git branch t &&
430 git branch -v --list t >actual.default &&
431 git branch -v --list --abbrev t >actual.abbrev &&
432 test_cmp actual.default actual.abbrev &&
433
434 git branch -v --list --no-abbrev t >actual.noabbrev &&
435 git branch -v --list --abbrev=0 t >actual.0abbrev &&
436 git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
437 test_cmp actual.noabbrev actual.0abbrev &&
438 test_cmp actual.noabbrev actual.noabbrev-conf &&
439
440 git branch -v --list --abbrev=36 t >actual.36abbrev &&
441 # how many hexdigits are used?
442 read name objdefault rest <actual.abbrev &&
443 read name obj36 rest <actual.36abbrev &&
444 objfull=$(git rev-parse --verify t) &&
445
446 # are we really getting abbreviations?
447 test "$obj36" != "$objdefault" &&
448 expr "$obj36" : "$objdefault" >/dev/null &&
449 test "$objfull" != "$obj36" &&
450 expr "$objfull" : "$obj36" >/dev/null
451
452 '
453
454 test_expect_success 'git branch --column' '
455 COLUMNS=81 git branch --column=column >actual &&
456 cat >expect <<-\EOF &&
457 a/b/c bam foo l * main n o/p r
458 abc bar j/k m/m mb o/o q topic
459 EOF
460 test_cmp expect actual
461 '
462
463 test_expect_success 'git branch --column with an extremely long branch name' '
464 long=this/is/a/part/of/long/branch/name &&
465 long=z$long/$long/$long/$long &&
466 test_when_finished "git branch -d $long" &&
467 git branch $long &&
468 COLUMNS=80 git branch --column=column >actual &&
469 cat >expect <<-EOF &&
470 a/b/c
471 abc
472 bam
473 bar
474 foo
475 j/k
476 l
477 m/m
478 * main
479 mb
480 n
481 o/o
482 o/p
483 q
484 r
485 topic
486 $long
487 EOF
488 test_cmp expect actual
489 '
490
491 test_expect_success 'git branch with column.*' '
492 git config column.ui column &&
493 git config column.branch "dense" &&
494 COLUMNS=80 git branch >actual &&
495 git config --unset column.branch &&
496 git config --unset column.ui &&
497 cat >expect <<-\EOF &&
498 a/b/c bam foo l * main n o/p r
499 abc bar j/k m/m mb o/o q topic
500 EOF
501 test_cmp expect actual
502 '
503
504 test_expect_success 'git branch --column -v should fail' '
505 test_must_fail git branch --column -v
506 '
507
508 test_expect_success 'git branch -v with column.ui ignored' '
509 git config column.ui column &&
510 COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
511 git config --unset column.ui &&
512 cat >expect <<-\EOF &&
513 a/b/c
514 abc
515 bam
516 bar
517 foo
518 j/k
519 l
520 m/m
521 * main
522 mb
523 n
524 o/o
525 o/p
526 q
527 r
528 topic
529 EOF
530 test_cmp expect actual
531 '
532
533 test_expect_success DEFAULT_REPO_FORMAT 'git branch -m q q2 without config should succeed' '
534 test_when_finished mv .git/config-saved .git/config &&
535 mv .git/config .git/config-saved &&
536 git branch -m q q2 &&
537 git branch -m q2 q
538 '
539
540 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
541 git config branch.s/s.dummy Hello &&
542 git branch --create-reflog s/s &&
543 git reflog exists refs/heads/s/s &&
544 git branch --create-reflog s/t &&
545 git reflog exists refs/heads/s/t &&
546 git branch -d s/t &&
547 git branch -m s/s s &&
548 git reflog exists refs/heads/s
549 '
550
551 test_expect_success 'config information was renamed, too' '
552 test $(git config branch.s.dummy) = Hello &&
553 test_must_fail git config branch.s/s.dummy
554 '
555
556 test_expect_success 'git branch -m correctly renames multiple config sections' '
557 test_when_finished "git checkout main" &&
558 git checkout -b source main &&
559
560 # Assert that a config file with multiple config sections has
561 # those sections preserved...
562 cat >expect <<-\EOF &&
563 branch.dest.key1=value1
564 some.gar.b=age
565 branch.dest.key2=value2
566 EOF
567 cat >config.branch <<\EOF &&
568 ;; Note the lack of -\EOF above & mixed indenting here. This is
569 ;; intentional, we are also testing that the formatting of copied
570 ;; sections is preserved.
571
572 ;; Comment for source. Tabs
573 [branch "source"]
574 ;; Comment for the source value
575 key1 = value1
576 ;; Comment for some.gar. Spaces
577 [some "gar"]
578 ;; Comment for the some.gar value
579 b = age
580 ;; Comment for source, again. Mixed tabs/spaces.
581 [branch "source"]
582 ;; Comment for the source value, again
583 key2 = value2
584 EOF
585 cat config.branch >>.git/config &&
586 git branch -m source dest &&
587 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
588 test_cmp expect actual &&
589
590 # ...and that the comments for those sections are also
591 # preserved.
592 sed "s/\"source\"/\"dest\"/" config.branch >expect &&
593 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
594 test_cmp expect actual
595 '
596
597 test_expect_success 'git branch -c dumps usage' '
598 test_expect_code 128 git branch -c 2>err &&
599 test_grep "branch name required" err
600 '
601
602 test_expect_success 'git branch --copy dumps usage' '
603 test_expect_code 128 git branch --copy 2>err &&
604 test_grep "branch name required" err
605 '
606
607 test_expect_success 'git branch -c d e should work' '
608 git branch --create-reflog d &&
609 git reflog exists refs/heads/d &&
610 git config branch.d.dummy Hello &&
611 git branch -c d e &&
612 git reflog exists refs/heads/d &&
613 git reflog exists refs/heads/e &&
614 echo Hello >expect &&
615 git config branch.e.dummy >actual &&
616 test_cmp expect actual &&
617 echo Hello >expect &&
618 git config branch.d.dummy >actual &&
619 test_cmp expect actual
620 '
621
622 test_expect_success 'git branch --copy is a synonym for -c' '
623 git branch --create-reflog copy &&
624 git reflog exists refs/heads/copy &&
625 git config branch.copy.dummy Hello &&
626 git branch --copy copy copy-to &&
627 git reflog exists refs/heads/copy &&
628 git reflog exists refs/heads/copy-to &&
629 echo Hello >expect &&
630 git config branch.copy.dummy >actual &&
631 test_cmp expect actual &&
632 echo Hello >expect &&
633 git config branch.copy-to.dummy >actual &&
634 test_cmp expect actual
635 '
636
637 test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
638 git checkout -b ee &&
639 git reflog exists refs/heads/ee &&
640 git config branch.ee.dummy Hello &&
641 git branch -c ee ef &&
642 git reflog exists refs/heads/ee &&
643 git reflog exists refs/heads/ef &&
644 test $(git config branch.ee.dummy) = Hello &&
645 test $(git config branch.ef.dummy) = Hello &&
646 test $(git rev-parse --abbrev-ref HEAD) = ee
647 '
648
649 test_expect_success 'git branch -c f/f g/g should work' '
650 git branch --create-reflog f/f &&
651 git reflog exists refs/heads/f/f &&
652 git config branch.f/f.dummy Hello &&
653 git branch -c f/f g/g &&
654 git reflog exists refs/heads/f/f &&
655 git reflog exists refs/heads/g/g &&
656 test $(git config branch.f/f.dummy) = Hello &&
657 test $(git config branch.g/g.dummy) = Hello
658 '
659
660 test_expect_success 'git branch -c m2 m2 should work' '
661 git branch --create-reflog m2 &&
662 git reflog exists refs/heads/m2 &&
663 git config branch.m2.dummy Hello &&
664 git branch -c m2 m2 &&
665 git reflog exists refs/heads/m2 &&
666 test $(git config branch.m2.dummy) = Hello
667 '
668
669 test_expect_success 'git branch -c zz zz/zz should fail' '
670 git branch --create-reflog zz &&
671 git reflog exists refs/heads/zz &&
672 test_must_fail git branch -c zz zz/zz
673 '
674
675 test_expect_success 'git branch -c b/b b should fail' '
676 git branch --create-reflog b/b &&
677 test_must_fail git branch -c b/b b
678 '
679
680 test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
681 git branch --create-reflog o/q &&
682 git reflog exists refs/heads/o/q &&
683 git reflog exists refs/heads/o/p &&
684 git branch -C o/q o/p
685 '
686
687 test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
688 git reflog exists refs/heads/o/q &&
689 git reflog exists refs/heads/o/p &&
690 git branch -c -f o/q o/p
691 '
692
693 test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
694 git branch qq &&
695 git branch rr &&
696 test_must_fail git branch -c qq rr/qq
697 '
698
699 test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
700 git branch b1 &&
701 git checkout -b b2 &&
702 test_must_fail git branch -C b1 b2
703 '
704
705 test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
706 git checkout -b c1 &&
707 git branch c2 &&
708 git branch -C c1 c2 &&
709 test $(git rev-parse --abbrev-ref HEAD) = c1
710 '
711
712 test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
713 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
714 git reflog HEAD >actual &&
715 ! grep "$msg$" actual
716 '
717
718 test_expect_success 'git branch -C main should work when main is checked out' '
719 git checkout main &&
720 git branch -C main
721 '
722
723 test_expect_success 'git branch -C main main should work when main is checked out' '
724 git checkout main &&
725 git branch -C main main
726 '
727
728 test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
729 git checkout main &&
730 git branch main5 &&
731 git branch -C main5 main5
732 '
733
734 test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
735 git branch --create-reflog cd &&
736 git reflog exists refs/heads/cd &&
737 git config branch.cd.dummy CD &&
738 git branch --create-reflog ab &&
739 git reflog exists refs/heads/ab &&
740 git config branch.ab.dummy AB &&
741 git branch -C ab cd &&
742 git reflog exists refs/heads/ab &&
743 git reflog exists refs/heads/cd &&
744 test $(git config branch.ab.dummy) = AB &&
745 test $(git config branch.cd.dummy) = AB
746 '
747
748 test_expect_success 'git branch -c correctly copies multiple config sections' '
749 FOO=1 &&
750 export FOO &&
751 test_when_finished "git checkout main" &&
752 git checkout -b source2 main &&
753
754 # Assert that a config file with multiple config sections has
755 # those sections preserved...
756 cat >expect <<-\EOF &&
757 branch.source2.key1=value1
758 branch.dest2.key1=value1
759 more.gar.b=age
760 branch.source2.key2=value2
761 branch.dest2.key2=value2
762 EOF
763 cat >config.branch <<\EOF &&
764 ;; Note the lack of -\EOF above & mixed indenting here. This is
765 ;; intentional, we are also testing that the formatting of copied
766 ;; sections is preserved.
767
768 ;; Comment for source2. Tabs
769 [branch "source2"]
770 ;; Comment for the source2 value
771 key1 = value1
772 ;; Comment for more.gar. Spaces
773 [more "gar"]
774 ;; Comment for the more.gar value
775 b = age
776 ;; Comment for source2, again. Mixed tabs/spaces.
777 [branch "source2"]
778 ;; Comment for the source2 value, again
779 key2 = value2
780 EOF
781 cat config.branch >>.git/config &&
782 git branch -c source2 dest2 &&
783 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
784 test_cmp expect actual &&
785
786 # ...and that the comments and formatting for those sections
787 # is also preserved.
788 cat >expect <<\EOF &&
789 ;; Comment for source2. Tabs
790 [branch "source2"]
791 ;; Comment for the source2 value
792 key1 = value1
793 ;; Comment for more.gar. Spaces
794 [branch "dest2"]
795 ;; Comment for the source2 value
796 key1 = value1
797 ;; Comment for more.gar. Spaces
798 [more "gar"]
799 ;; Comment for the more.gar value
800 b = age
801 ;; Comment for source2, again. Mixed tabs/spaces.
802 [branch "source2"]
803 ;; Comment for the source2 value, again
804 key2 = value2
805 [branch "dest2"]
806 ;; Comment for the source2 value, again
807 key2 = value2
808 EOF
809 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
810 test_cmp expect actual
811 '
812
813 test_expect_success 'deleting a symref' '
814 git branch target &&
815 git symbolic-ref refs/heads/symref refs/heads/target &&
816 echo "Deleted branch symref (was refs/heads/target)." >expect &&
817 git branch -d symref >actual &&
818 test_ref_exists refs/heads/target &&
819 test_ref_missing refs/heads/symref &&
820 test_cmp expect actual
821 '
822
823 test_expect_success 'deleting a dangling symref' '
824 git symbolic-ref refs/heads/dangling-symref nowhere &&
825 git symbolic-ref --no-recurse refs/heads/dangling-symref &&
826 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
827 git branch -d dangling-symref >actual &&
828 test_ref_missing refs/heads/dangling-symref &&
829 test_cmp expect actual
830 '
831
832 test_expect_success 'deleting a self-referential symref' '
833 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
834 test_ref_exists refs/heads/self-reference &&
835 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
836 git branch -d self-reference >actual &&
837 test_ref_missing refs/heads/self-reference &&
838 test_cmp expect actual
839 '
840
841 test_expect_success 'renaming a symref is not allowed' '
842 git symbolic-ref refs/heads/topic refs/heads/main &&
843 test_must_fail git branch -m topic new-topic &&
844 git symbolic-ref refs/heads/topic &&
845 test_ref_exists refs/heads/main &&
846 test_ref_missing refs/heads/new-topic
847 '
848
849 test_expect_success 'test tracking setup via --track' '
850 git config remote.local.url . &&
851 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
852 (git show-ref -q refs/remotes/local/main || git fetch local) &&
853 git branch --track my1 local/main &&
854 test $(git config branch.my1.remote) = local &&
855 test $(git config branch.my1.merge) = refs/heads/main
856 '
857
858 test_expect_success 'test tracking setup (non-wildcard, matching)' '
859 git config remote.local.url . &&
860 git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
861 (git show-ref -q refs/remotes/local/main || git fetch local) &&
862 git branch --track my4 local/main &&
863 test $(git config branch.my4.remote) = local &&
864 test $(git config branch.my4.merge) = refs/heads/main
865 '
866
867 test_expect_success 'tracking setup fails on non-matching refspec' '
868 git config remote.local.url . &&
869 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
870 (git show-ref -q refs/remotes/local/main || git fetch local) &&
871 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
872 test_must_fail git branch --track my5 local/main &&
873 test_must_fail git config branch.my5.remote &&
874 test_must_fail git config branch.my5.merge
875 '
876
877 test_expect_success 'test tracking setup via config' '
878 git config branch.autosetupmerge true &&
879 git config remote.local.url . &&
880 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
881 (git show-ref -q refs/remotes/local/main || git fetch local) &&
882 git branch my3 local/main &&
883 test $(git config branch.my3.remote) = local &&
884 test $(git config branch.my3.merge) = refs/heads/main
885 '
886
887 test_expect_success 'test overriding tracking setup via --no-track' '
888 git config branch.autosetupmerge true &&
889 git config remote.local.url . &&
890 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
891 (git show-ref -q refs/remotes/local/main || git fetch local) &&
892 git branch --no-track my2 local/main &&
893 git config branch.autosetupmerge false &&
894 ! test "$(git config branch.my2.remote)" = local &&
895 ! test "$(git config branch.my2.merge)" = refs/heads/main
896 '
897
898 test_expect_success 'no tracking without .fetch entries' '
899 git config branch.autosetupmerge true &&
900 git branch my6 s &&
901 git config branch.autosetupmerge false &&
902 test -z "$(git config branch.my6.remote)" &&
903 test -z "$(git config branch.my6.merge)"
904 '
905
906 test_expect_success 'test tracking setup via --track but deeper' '
907 git config remote.local.url . &&
908 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
909 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
910 git branch --track my7 local/o/o &&
911 test "$(git config branch.my7.remote)" = local &&
912 test "$(git config branch.my7.merge)" = refs/heads/o/o
913 '
914
915 test_expect_success 'test deleting branch deletes branch config' '
916 git branch -d my7 &&
917 test -z "$(git config branch.my7.remote)" &&
918 test -z "$(git config branch.my7.merge)"
919 '
920
921 test_expect_success 'test deleting branch without config' '
922 git branch my7 s &&
923 sha1=$(git rev-parse my7 | cut -c 1-7) &&
924 echo "Deleted branch my7 (was $sha1)." >expect &&
925 git branch -d my7 >actual 2>&1 &&
926 test_cmp expect actual
927 '
928
929 test_expect_success 'deleting currently checked out branch fails' '
930 git worktree add -b my7 my7 &&
931 test_must_fail git -C my7 branch -d my7 &&
932 test_must_fail git branch -d my7 2>actual &&
933 grep "^error: cannot delete branch .my7. used by worktree at " actual &&
934 rm -r my7 &&
935 git worktree prune
936 '
937
938 test_expect_success 'deleting in-use branch fails' '
939 git worktree add my7 &&
940 test_commit -C my7 bt7 &&
941 git -C my7 bisect start HEAD HEAD~2 &&
942 test_must_fail git -C my7 branch -d my7 &&
943 test_must_fail git branch -d my7 2>actual &&
944 grep "^error: cannot delete branch .my7. used by worktree at " actual &&
945 rm -r my7 &&
946 git worktree prune
947 '
948
949 test_expect_success 'test --track without .fetch entries' '
950 git branch --track my8 &&
951 test "$(git config branch.my8.remote)" &&
952 test "$(git config branch.my8.merge)"
953 '
954
955 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
956 git config branch.autosetupmerge always &&
957 git branch my9 HEAD^ &&
958 git config branch.autosetupmerge false
959 '
960
961 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
962 test_must_fail git branch --track my10 HEAD^
963 '
964
965 test_expect_success 'branch from tag w/--track causes failure' '
966 git tag foobar &&
967 test_must_fail git branch --track my11 foobar
968 '
969
970 test_expect_success 'simple tracking works when remote branch name matches' '
971 test_when_finished "rm -rf otherserver" &&
972 git init otherserver &&
973 test_commit -C otherserver my_commit 1 &&
974 git -C otherserver branch feature &&
975 test_config branch.autosetupmerge simple &&
976 test_config remote.otherserver.url otherserver &&
977 test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* &&
978 git fetch otherserver &&
979 git branch feature otherserver/feature &&
980 test_cmp_config otherserver branch.feature.remote &&
981 test_cmp_config refs/heads/feature branch.feature.merge
982 '
983
984 test_expect_success 'simple tracking skips when remote branch name does not match' '
985 test_config branch.autosetupmerge simple &&
986 test_config remote.local.url . &&
987 test_config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
988 git fetch local &&
989 git branch my-other local/main &&
990 test_cmp_config "" --default "" branch.my-other.remote &&
991 test_cmp_config "" --default "" branch.my-other.merge
992 '
993
994 test_expect_success 'simple tracking skips when remote ref is not a branch' '
995 test_config branch.autosetupmerge simple &&
996 test_config remote.localtags.url . &&
997 test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* &&
998 git tag mytag12 main &&
999 git fetch localtags &&
1000 git branch mytag12 localtags/mytag12 &&
1001 test_cmp_config "" --default "" branch.mytag12.remote &&
1002 test_cmp_config "" --default "" branch.mytag12.merge
1003 '
1004
1005 test_expect_success '--set-upstream-to fails on multiple branches' '
1006 echo "fatal: too many arguments to set new upstream" >expect &&
1007 test_must_fail git branch --set-upstream-to main a b c 2>err &&
1008 test_cmp expect err
1009 '
1010
1011 test_expect_success '--set-upstream-to fails on detached HEAD' '
1012 git checkout HEAD^{} &&
1013 test_when_finished git checkout - &&
1014 echo "fatal: could not set upstream of HEAD to main when it does not point to any branch" >expect &&
1015 test_must_fail git branch --set-upstream-to main 2>err &&
1016 test_cmp expect err
1017 '
1018
1019 test_expect_success '--set-upstream-to fails on a missing dst branch' '
1020 echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
1021 test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
1022 test_cmp expect err
1023 '
1024
1025 test_expect_success '--set-upstream-to fails on a missing src branch' '
1026 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
1027 test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
1028 '
1029
1030 test_expect_success '--set-upstream-to fails on a non-ref' '
1031 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
1032 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
1033 test_cmp expect err
1034 '
1035
1036 test_expect_success '--set-upstream-to fails on locked config' '
1037 test_when_finished "rm -f .git/config.lock" &&
1038 >.git/config.lock &&
1039 git branch locked &&
1040 test_must_fail git branch --set-upstream-to locked 2>err &&
1041 test_grep "could not lock config file .git/config" err
1042 '
1043
1044 test_expect_success 'use --set-upstream-to modify HEAD' '
1045 test_config branch.main.remote foo &&
1046 test_config branch.main.merge foo &&
1047 git branch my12 &&
1048 git branch --set-upstream-to my12 &&
1049 test "$(git config branch.main.remote)" = "." &&
1050 test "$(git config branch.main.merge)" = "refs/heads/my12"
1051 '
1052
1053 test_expect_success 'use --set-upstream-to modify a particular branch' '
1054 git branch my13 &&
1055 git branch --set-upstream-to main my13 &&
1056 test_when_finished "git branch --unset-upstream my13" &&
1057 test "$(git config branch.my13.remote)" = "." &&
1058 test "$(git config branch.my13.merge)" = "refs/heads/main"
1059 '
1060
1061 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
1062 echo "fatal: branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
1063 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
1064 test_cmp expect err
1065 '
1066
1067 test_expect_success '--unset-upstream should fail if config is locked' '
1068 test_when_finished "rm -f .git/config.lock" &&
1069 git branch --set-upstream-to locked &&
1070 >.git/config.lock &&
1071 test_must_fail git branch --unset-upstream 2>err &&
1072 test_grep "could not lock config file .git/config" err
1073 '
1074
1075 test_expect_success 'test --unset-upstream on HEAD' '
1076 git branch my14 &&
1077 test_config branch.main.remote foo &&
1078 test_config branch.main.merge foo &&
1079 git branch --set-upstream-to my14 &&
1080 git branch --unset-upstream &&
1081 test_must_fail git config branch.main.remote &&
1082 test_must_fail git config branch.main.merge &&
1083 # fail for a branch without upstream set
1084 echo "fatal: branch '"'"'main'"'"' has no upstream information" >expect &&
1085 test_must_fail git branch --unset-upstream 2>err &&
1086 test_cmp expect err
1087 '
1088
1089 test_expect_success '--unset-upstream should fail on multiple branches' '
1090 echo "fatal: too many arguments to unset upstream" >expect &&
1091 test_must_fail git branch --unset-upstream a b c 2>err &&
1092 test_cmp expect err
1093 '
1094
1095 test_expect_success '--unset-upstream should fail on detached HEAD' '
1096 git checkout HEAD^{} &&
1097 test_when_finished git checkout - &&
1098 echo "fatal: could not unset upstream of HEAD when it does not point to any branch" >expect &&
1099 test_must_fail git branch --unset-upstream 2>err &&
1100 test_cmp expect err
1101 '
1102
1103 test_expect_success 'test --unset-upstream on a particular branch' '
1104 git branch my15 &&
1105 git branch --set-upstream-to main my14 &&
1106 git branch --unset-upstream my14 &&
1107 test_must_fail git config branch.my14.remote &&
1108 test_must_fail git config branch.my14.merge
1109 '
1110
1111 test_expect_success 'disabled option --set-upstream fails' '
1112 test_must_fail git branch --set-upstream origin/main
1113 '
1114
1115 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
1116 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1117 cat >expect <<-\EOF &&
1118 warning: not setting branch 'my13' as its own upstream
1119 EOF
1120 test_expect_code 1 git config branch.my13.remote &&
1121 test_expect_code 1 git config branch.my13.merge &&
1122 test_cmp expect actual
1123 "
1124
1125 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
1126 test_when_finished git checkout main &&
1127 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1128 git checkout -b g/h/i -l main &&
1129 test_ref_exists refs/heads/g/h/i &&
1130 cat >expect <<-EOF &&
1131 $HEAD refs/heads/g/h/i@{0}: branch: Created from main
1132 EOF
1133 git reflog show --no-abbrev-commit refs/heads/g/h/i >actual &&
1134 test_cmp expect actual
1135 '
1136
1137 test_expect_success 'checkout -b makes reflog by default' '
1138 git checkout main &&
1139 git config --unset core.logAllRefUpdates &&
1140 git checkout -b alpha &&
1141 git rev-parse --verify alpha@{0}
1142 '
1143
1144 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1145 git checkout main &&
1146 git config core.logAllRefUpdates false &&
1147 git checkout -b beta &&
1148 test_must_fail git rev-parse --verify beta@{0}
1149 '
1150
1151 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1152 git checkout main &&
1153 git checkout -lb gamma &&
1154 git config --unset core.logAllRefUpdates &&
1155 git rev-parse --verify gamma@{0}
1156 '
1157
1158 test_expect_success 'avoid ambiguous track and advise' '
1159 git config branch.autosetupmerge true &&
1160 git config remote.ambi1.url lalala &&
1161 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1162 git config remote.ambi2.url lilili &&
1163 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1164 cat <<-EOF >expected &&
1165 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1166 hint: There are multiple remotes whose fetch refspecs map to the remote
1167 hint: tracking ref '\''refs/heads/main'\'':
1168 hint: ambi1
1169 hint: ambi2
1170 hint:
1171 hint: This is typically a configuration error.
1172 hint:
1173 hint: To support setting up tracking branches, ensure that
1174 hint: different remotes'\'' fetch refspecs map into different
1175 hint: tracking namespaces.
1176 EOF
1177 test_must_fail git branch all1 main 2>actual &&
1178 test_cmp expected actual &&
1179 test -z "$(git config branch.all1.merge)"
1180 '
1181
1182 test_expect_success 'autosetuprebase local on a tracked local branch' '
1183 git config remote.local.url . &&
1184 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1185 git config branch.autosetuprebase local &&
1186 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1187 git branch mybase &&
1188 git branch --track myr1 mybase &&
1189 test "$(git config branch.myr1.remote)" = . &&
1190 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1191 test "$(git config branch.myr1.rebase)" = true
1192 '
1193
1194 test_expect_success 'autosetuprebase always on a tracked local branch' '
1195 git config remote.local.url . &&
1196 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1197 git config branch.autosetuprebase always &&
1198 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1199 git branch mybase2 &&
1200 git branch --track myr2 mybase &&
1201 test "$(git config branch.myr2.remote)" = . &&
1202 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1203 test "$(git config branch.myr2.rebase)" = true
1204 '
1205
1206 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1207 git config remote.local.url . &&
1208 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1209 git config branch.autosetuprebase remote &&
1210 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1211 git branch mybase3 &&
1212 git branch --track myr3 mybase2 &&
1213 test "$(git config branch.myr3.remote)" = . &&
1214 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1215 ! test "$(git config branch.myr3.rebase)" = true
1216 '
1217
1218 test_expect_success 'autosetuprebase never on a tracked local branch' '
1219 git config remote.local.url . &&
1220 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1221 git config branch.autosetuprebase never &&
1222 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1223 git branch mybase4 &&
1224 git branch --track myr4 mybase2 &&
1225 test "$(git config branch.myr4.remote)" = . &&
1226 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1227 ! test "$(git config branch.myr4.rebase)" = true
1228 '
1229
1230 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1231 git config remote.local.url . &&
1232 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1233 git config branch.autosetuprebase local &&
1234 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1235 git branch --track myr5 local/main &&
1236 test "$(git config branch.myr5.remote)" = local &&
1237 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1238 ! test "$(git config branch.myr5.rebase)" = true
1239 '
1240
1241 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1242 git config remote.local.url . &&
1243 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1244 git config branch.autosetuprebase never &&
1245 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1246 git branch --track myr6 local/main &&
1247 test "$(git config branch.myr6.remote)" = local &&
1248 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1249 ! test "$(git config branch.myr6.rebase)" = true
1250 '
1251
1252 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1253 git config remote.local.url . &&
1254 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1255 git config branch.autosetuprebase remote &&
1256 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1257 git branch --track myr7 local/main &&
1258 test "$(git config branch.myr7.remote)" = local &&
1259 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1260 test "$(git config branch.myr7.rebase)" = true
1261 '
1262
1263 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1264 git config remote.local.url . &&
1265 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1266 git config branch.autosetuprebase remote &&
1267 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1268 git branch --track myr8 local/main &&
1269 test "$(git config branch.myr8.remote)" = local &&
1270 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1271 test "$(git config branch.myr8.rebase)" = true
1272 '
1273
1274 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1275 git config --unset branch.autosetuprebase &&
1276 git config remote.local.url . &&
1277 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1278 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1279 git branch --track myr9 local/main &&
1280 test "$(git config branch.myr9.remote)" = local &&
1281 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1282 test "z$(git config branch.myr9.rebase)" = z
1283 '
1284
1285 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1286 git config remote.local.url . &&
1287 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1288 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1289 git branch mybase10 &&
1290 git branch --track myr10 mybase2 &&
1291 test "$(git config branch.myr10.remote)" = . &&
1292 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1293 test "z$(git config branch.myr10.rebase)" = z
1294 '
1295
1296 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1297 git config remote.local.url . &&
1298 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1299 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1300 git branch --no-track myr11 mybase2 &&
1301 test "z$(git config branch.myr11.remote)" = z &&
1302 test "z$(git config branch.myr11.merge)" = z &&
1303 test "z$(git config branch.myr11.rebase)" = z
1304 '
1305
1306 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1307 git config remote.local.url . &&
1308 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1309 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1310 git branch --no-track myr12 local/main &&
1311 test "z$(git config branch.myr12.remote)" = z &&
1312 test "z$(git config branch.myr12.merge)" = z &&
1313 test "z$(git config branch.myr12.rebase)" = z
1314 '
1315
1316 test_expect_success 'autosetuprebase never on an untracked local branch' '
1317 git config branch.autosetuprebase never &&
1318 git config remote.local.url . &&
1319 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1320 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1321 git branch --no-track myr13 mybase2 &&
1322 test "z$(git config branch.myr13.remote)" = z &&
1323 test "z$(git config branch.myr13.merge)" = z &&
1324 test "z$(git config branch.myr13.rebase)" = z
1325 '
1326
1327 test_expect_success 'autosetuprebase local on an untracked local branch' '
1328 git config branch.autosetuprebase local &&
1329 git config remote.local.url . &&
1330 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1331 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1332 git branch --no-track myr14 mybase2 &&
1333 test "z$(git config branch.myr14.remote)" = z &&
1334 test "z$(git config branch.myr14.merge)" = z &&
1335 test "z$(git config branch.myr14.rebase)" = z
1336 '
1337
1338 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1339 git config branch.autosetuprebase remote &&
1340 git config remote.local.url . &&
1341 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1342 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1343 git branch --no-track myr15 mybase2 &&
1344 test "z$(git config branch.myr15.remote)" = z &&
1345 test "z$(git config branch.myr15.merge)" = z &&
1346 test "z$(git config branch.myr15.rebase)" = z
1347 '
1348
1349 test_expect_success 'autosetuprebase always on an untracked local branch' '
1350 git config branch.autosetuprebase always &&
1351 git config remote.local.url . &&
1352 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1353 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1354 git branch --no-track myr16 mybase2 &&
1355 test "z$(git config branch.myr16.remote)" = z &&
1356 test "z$(git config branch.myr16.merge)" = z &&
1357 test "z$(git config branch.myr16.rebase)" = z
1358 '
1359
1360 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1361 git config branch.autosetuprebase never &&
1362 git config remote.local.url . &&
1363 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1364 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1365 git branch --no-track myr17 local/main &&
1366 test "z$(git config branch.myr17.remote)" = z &&
1367 test "z$(git config branch.myr17.merge)" = z &&
1368 test "z$(git config branch.myr17.rebase)" = z
1369 '
1370
1371 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1372 git config branch.autosetuprebase local &&
1373 git config remote.local.url . &&
1374 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1375 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1376 git branch --no-track myr18 local/main &&
1377 test "z$(git config branch.myr18.remote)" = z &&
1378 test "z$(git config branch.myr18.merge)" = z &&
1379 test "z$(git config branch.myr18.rebase)" = z
1380 '
1381
1382 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1383 git config branch.autosetuprebase remote &&
1384 git config remote.local.url . &&
1385 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1386 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1387 git branch --no-track myr19 local/main &&
1388 test "z$(git config branch.myr19.remote)" = z &&
1389 test "z$(git config branch.myr19.merge)" = z &&
1390 test "z$(git config branch.myr19.rebase)" = z
1391 '
1392
1393 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1394 git config branch.autosetuprebase always &&
1395 git config remote.local.url . &&
1396 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1397 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1398 git branch --no-track myr20 local/main &&
1399 test "z$(git config branch.myr20.remote)" = z &&
1400 test "z$(git config branch.myr20.merge)" = z &&
1401 test "z$(git config branch.myr20.rebase)" = z
1402 '
1403
1404 test_expect_success 'autosetuprebase always on detached HEAD' '
1405 git config branch.autosetupmerge always &&
1406 test_when_finished git checkout main &&
1407 git checkout HEAD^0 &&
1408 git branch my11 &&
1409 test -z "$(git config branch.my11.remote)" &&
1410 test -z "$(git config branch.my11.merge)"
1411 '
1412
1413 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1414 git config branch.autosetuprebase garbage &&
1415 test_must_fail git branch
1416 '
1417
1418 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1419 git config --unset branch.autosetuprebase &&
1420 echo "[branch] autosetuprebase" >>.git/config &&
1421 test_must_fail git branch &&
1422 git config --unset branch.autosetuprebase
1423 '
1424
1425 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1426 git checkout my9 &&
1427 git config --unset branch.my8.merge &&
1428 test_must_fail git branch -d my8
1429 '
1430
1431 test_expect_success 'attempt to delete a branch merged to its base' '
1432 # we are on my9 which is the initial commit; traditionally
1433 # we would not have allowed deleting my8 that is not merged
1434 # to my9, but it is set to track main that already has my8
1435 git config branch.my8.merge refs/heads/main &&
1436 git branch -d my8
1437 '
1438
1439 test_expect_success 'attempt to delete a branch merged to its base' '
1440 git checkout main &&
1441 echo Third >>A &&
1442 git commit -m "Third commit" A &&
1443 git branch -t my10 my9 &&
1444 git branch -f my10 HEAD^ &&
1445 # we are on main which is at the third commit, and my10
1446 # is behind us, so traditionally we would have allowed deleting
1447 # it; but my10 is set to track my9 that is further behind.
1448 test_must_fail git branch -d my10
1449 '
1450
1451 test_expect_success 'branch --delete --force removes dangling branch' '
1452 git checkout main &&
1453 test_commit unstable &&
1454 hash=$(git rev-parse HEAD) &&
1455 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1456 git branch --no-track dangling &&
1457 mv $objpath $objpath.x &&
1458 test_when_finished "mv $objpath.x $objpath" &&
1459 git branch --delete --force dangling &&
1460 git for-each-ref refs/heads/dangling >actual &&
1461 test_must_be_empty actual
1462 '
1463
1464 test_expect_success 'use --edit-description' '
1465 EDITOR=: git branch --edit-description &&
1466 test_expect_code 1 git config branch.main.description &&
1467
1468 write_script editor <<-\EOF &&
1469 echo "New contents" >"$1"
1470 EOF
1471 EDITOR=./editor git branch --edit-description &&
1472 write_script editor <<-\EOF &&
1473 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1474 EOF
1475 EDITOR=./editor git branch --edit-description &&
1476 echo "New contents" >expect &&
1477 test_cmp expect EDITOR_OUTPUT
1478 '
1479
1480 test_expect_success 'detect typo in branch name when using --edit-description' '
1481 write_script editor <<-\EOF &&
1482 echo "New contents" >"$1"
1483 EOF
1484 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1485 '
1486
1487 test_expect_success 'refuse --edit-description on unborn branch for now' '
1488 test_when_finished "git checkout main" &&
1489 write_script editor <<-\EOF &&
1490 echo "New contents" >"$1"
1491 EOF
1492 git checkout --orphan unborn &&
1493 test_must_fail env EDITOR=./editor git branch --edit-description
1494 '
1495
1496 test_expect_success '--merged catches invalid object names' '
1497 test_must_fail git branch --merged $ZERO_OID 2>err &&
1498 test_grep "must point to a commit" err
1499 '
1500
1501 test_expect_success '--list during rebase' '
1502 test_when_finished "reset_rebase" &&
1503 git checkout main &&
1504 FAKE_LINES="1 edit 2" &&
1505 export FAKE_LINES &&
1506 set_fake_editor &&
1507 git rebase -i HEAD~2 &&
1508 git branch --list >actual &&
1509 test_grep "rebasing main" actual
1510 '
1511
1512 test_expect_success '--list during rebase from detached HEAD' '
1513 test_when_finished "reset_rebase && git checkout main" &&
1514 git checkout main^0 &&
1515 oid=$(git rev-parse --short HEAD) &&
1516 FAKE_LINES="1 edit 2" &&
1517 export FAKE_LINES &&
1518 set_fake_editor &&
1519 git rebase -i HEAD~2 &&
1520 git branch --list >actual &&
1521 test_grep "rebasing detached HEAD $oid" actual
1522 '
1523
1524 test_expect_success 'tracking with unexpected .fetch refspec' '
1525 rm -rf a b c d &&
1526 git init -b main a &&
1527 (
1528 cd a &&
1529 test_commit a
1530 ) &&
1531 git init -b main b &&
1532 (
1533 cd b &&
1534 test_commit b
1535 ) &&
1536 git init -b main c &&
1537 (
1538 cd c &&
1539 test_commit c &&
1540 git remote add a ../a &&
1541 git remote add b ../b &&
1542 git fetch --all
1543 ) &&
1544 git init -b main d &&
1545 (
1546 cd d &&
1547 git remote add c ../c &&
1548 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1549 git fetch c &&
1550 git branch --track local/a/main remotes/a/main &&
1551 test "$(git config branch.local/a/main.remote)" = "c" &&
1552 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1553 git rev-parse --verify a >expect &&
1554 git rev-parse --verify local/a/main >actual &&
1555 test_cmp expect actual
1556 )
1557 '
1558
1559 test_expect_success 'configured committerdate sort' '
1560 git init -b main sort &&
1561 test_config -C sort branch.sort "committerdate" &&
1562
1563 (
1564 cd sort &&
1565 test_commit initial &&
1566 git checkout -b a &&
1567 test_commit a &&
1568 git checkout -b c &&
1569 test_commit c &&
1570 git checkout -b b &&
1571 test_commit b &&
1572 git branch >actual &&
1573 cat >expect <<-\EOF &&
1574 main
1575 a
1576 c
1577 * b
1578 EOF
1579 test_cmp expect actual
1580 )
1581 '
1582
1583 test_expect_success 'option override configured sort' '
1584 test_config -C sort branch.sort "committerdate" &&
1585
1586 (
1587 cd sort &&
1588 git branch --sort=refname >actual &&
1589 cat >expect <<-\EOF &&
1590 a
1591 * b
1592 c
1593 main
1594 EOF
1595 test_cmp expect actual
1596 )
1597 '
1598
1599 test_expect_success '--no-sort cancels config sort keys' '
1600 test_config -C sort branch.sort "-refname" &&
1601
1602 (
1603 cd sort &&
1604
1605 # objecttype is identical for all of them, so sort falls back on
1606 # default (ascending refname)
1607 git branch \
1608 --no-sort \
1609 --sort="objecttype" >actual &&
1610 cat >expect <<-\EOF &&
1611 a
1612 * b
1613 c
1614 main
1615 EOF
1616 test_cmp expect actual
1617 )
1618
1619 '
1620
1621 test_expect_success '--no-sort cancels command line sort keys' '
1622 (
1623 cd sort &&
1624
1625 # objecttype is identical for all of them, so sort falls back on
1626 # default (ascending refname)
1627 git branch \
1628 --sort="-refname" \
1629 --no-sort \
1630 --sort="objecttype" >actual &&
1631 cat >expect <<-\EOF &&
1632 a
1633 * b
1634 c
1635 main
1636 EOF
1637 test_cmp expect actual
1638 )
1639 '
1640
1641 test_expect_success '--no-sort without subsequent --sort prints expected branches' '
1642 (
1643 cd sort &&
1644
1645 # Sort the results with `sort` for a consistent comparison
1646 # against expected
1647 git branch --no-sort | sort >actual &&
1648 cat >expect <<-\EOF &&
1649 a
1650 c
1651 main
1652 * b
1653 EOF
1654 test_cmp expect actual
1655 )
1656 '
1657
1658 test_expect_success 'invalid sort parameter in configuration' '
1659 test_config -C sort branch.sort "v:notvalid" &&
1660
1661 (
1662 cd sort &&
1663
1664 # this works in the "listing" mode, so bad sort key
1665 # is a dying offence.
1666 test_must_fail git branch &&
1667
1668 # these do not need to use sorting, and should all
1669 # succeed
1670 git branch newone main &&
1671 git branch -c newone newerone &&
1672 git branch -m newone newestone &&
1673 git branch -d newerone newestone
1674 )
1675 '
1676
1677 test_expect_success 'tracking info copied with --track=inherit' '
1678 git branch --track=inherit foo2 my1 &&
1679 test_cmp_config local branch.foo2.remote &&
1680 test_cmp_config refs/heads/main branch.foo2.merge
1681 '
1682
1683 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1684 test_unconfig branch.autoSetupMerge &&
1685 # default config does not copy tracking info
1686 git branch foo-no-inherit my1 &&
1687 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1688 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1689 # with autoSetupMerge=inherit, we copy tracking info from my1
1690 test_config branch.autoSetupMerge inherit &&
1691 git branch foo3 my1 &&
1692 test_cmp_config local branch.foo3.remote &&
1693 test_cmp_config refs/heads/main branch.foo3.merge &&
1694 # no tracking info to inherit from main
1695 git branch main2 main &&
1696 test_cmp_config "" --default "" branch.main2.remote &&
1697 test_cmp_config "" --default "" branch.main2.merge
1698 '
1699
1700 test_expect_success '--track overrides branch.autoSetupMerge' '
1701 test_config branch.autoSetupMerge inherit &&
1702 git branch --track=direct foo4 my1 &&
1703 test_cmp_config . branch.foo4.remote &&
1704 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1705 git branch --no-track foo5 my1 &&
1706 test_cmp_config "" --default "" branch.foo5.remote &&
1707 test_cmp_config "" --default "" branch.foo5.merge
1708 '
1709
1710 test_expect_success 'errors if given a bad branch name' '
1711 cat <<-EOF >expect &&
1712 fatal: ${SQ}foo..bar${SQ} is not a valid branch name
1713 hint: See ${SQ}git help check-ref-format${SQ}
1714 hint: Disable this message with "git config set advice.refSyntax false"
1715 EOF
1716 test_must_fail git branch foo..bar >actual 2>&1 &&
1717 test_cmp expect actual
1718 '
1719
1720 test_done