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 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 test_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 test_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 test_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 test_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 test_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 test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*' for bisect\$"'" 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 suggests <remote>/<branch> on slip' '
1026 test_when_finished "git remote remove slip-remote" &&
1027 git remote add slip-remote . &&
1028 git update-ref refs/remotes/slip-remote/slip-feature HEAD &&
1029 test_must_fail git branch --set-upstream-to slip-remote slip-feature 2>err &&
1030 test_grep "takes a single <remote>/<branch> argument" err &&
1031 test_grep "hint: Did you mean to use: git branch --set-upstream-to=slip-remote/slip-feature?" err &&
1032 test_must_fail git -c advice.setUpstreamFailure=false \
1033 branch --set-upstream-to slip-remote slip-feature 2>err &&
1034 test_grep ! "Did you mean" err
1035 '
1036
1037 test_expect_success '--set-upstream-to does not suggest when no matching remote ref' '
1038 test_when_finished "git remote remove slip-remote" &&
1039 git remote add slip-remote . &&
1040 test_must_fail git branch --set-upstream-to slip-remote no-such-branch 2>err &&
1041 test_grep "branch ${SQ}no-such-branch${SQ} does not exist" err &&
1042 test_grep ! "Did you mean" err
1043 '
1044
1045 test_expect_success '--set-upstream-to to a local branch is not mistaken for a slip' '
1046 git branch slip-local-upstream &&
1047 git branch slip-local-target &&
1048 git branch --set-upstream-to=slip-local-upstream slip-local-target 2>err &&
1049 test_grep ! "Did you mean" err &&
1050 echo refs/heads/slip-local-upstream >expect &&
1051 git config branch.slip-local-target.merge >actual &&
1052 test_cmp expect actual
1053 '
1054
1055 test_expect_success '--set-upstream-to slip suggestion keeps a slashed branch name' '
1056 test_when_finished "git remote remove slip-remote" &&
1057 git remote add slip-remote . &&
1058 git update-ref refs/remotes/slip-remote/slip/feature HEAD &&
1059 test_must_fail git branch --set-upstream-to slip-remote slip/feature 2>err &&
1060 test_grep "hint: Did you mean to use: git branch --set-upstream-to=slip-remote/slip/feature?" err
1061 '
1062
1063 test_expect_success '--set-upstream-to fails on a missing src branch' '
1064 test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
1065 test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
1066 '
1067
1068 test_expect_success '--set-upstream-to fails on a non-ref' '
1069 echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
1070 test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
1071 test_cmp expect err
1072 '
1073
1074 test_expect_success '--set-upstream-to fails on locked config' '
1075 test_when_finished "rm -f .git/config.lock" &&
1076 >.git/config.lock &&
1077 git branch locked &&
1078 test_must_fail git -c core.configLockTimeout=0 \
1079 branch --set-upstream-to locked 2>err &&
1080 test_grep "could not lock config file .git/config" err
1081 '
1082
1083 test_expect_success 'use --set-upstream-to modify HEAD' '
1084 test_config branch.main.remote foo &&
1085 test_config branch.main.merge foo &&
1086 git branch my12 &&
1087 git branch --set-upstream-to my12 &&
1088 test "$(git config branch.main.remote)" = "." &&
1089 test "$(git config branch.main.merge)" = "refs/heads/my12"
1090 '
1091
1092 test_expect_success 'use --set-upstream-to modify a particular branch' '
1093 git branch my13 &&
1094 git branch --set-upstream-to main my13 &&
1095 test_when_finished "git branch --unset-upstream my13" &&
1096 test "$(git config branch.my13.remote)" = "." &&
1097 test "$(git config branch.my13.merge)" = "refs/heads/main"
1098 '
1099
1100 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
1101 echo "fatal: branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
1102 test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
1103 test_cmp expect err
1104 '
1105
1106 test_expect_success '--unset-upstream should fail if config is locked' '
1107 test_when_finished "rm -f .git/config.lock" &&
1108 git branch --set-upstream-to locked &&
1109 >.git/config.lock &&
1110 test_must_fail git -c core.configLockTimeout=0 \
1111 branch --unset-upstream 2>err &&
1112 test_grep "could not lock config file .git/config" err
1113 '
1114
1115 test_expect_success 'test --unset-upstream on HEAD' '
1116 git branch my14 &&
1117 test_config branch.main.remote foo &&
1118 test_config branch.main.merge foo &&
1119 git branch --set-upstream-to my14 &&
1120 git branch --unset-upstream &&
1121 test_must_fail git config branch.main.remote &&
1122 test_must_fail git config branch.main.merge &&
1123 # fail for a branch without upstream set
1124 echo "fatal: branch '"'"'main'"'"' has no upstream information" >expect &&
1125 test_must_fail git branch --unset-upstream 2>err &&
1126 test_cmp expect err
1127 '
1128
1129 test_expect_success '--unset-upstream should fail on multiple branches' '
1130 echo "fatal: too many arguments to unset upstream" >expect &&
1131 test_must_fail git branch --unset-upstream a b c 2>err &&
1132 test_cmp expect err
1133 '
1134
1135 test_expect_success '--unset-upstream should fail on detached HEAD' '
1136 git checkout HEAD^{} &&
1137 test_when_finished git checkout - &&
1138 echo "fatal: could not unset upstream of HEAD when it does not point to any branch" >expect &&
1139 test_must_fail git branch --unset-upstream 2>err &&
1140 test_cmp expect err
1141 '
1142
1143 test_expect_success 'test --unset-upstream on a particular branch' '
1144 git branch my15 &&
1145 git branch --set-upstream-to main my14 &&
1146 git branch --unset-upstream my14 &&
1147 test_must_fail git config branch.my14.remote &&
1148 test_must_fail git config branch.my14.merge
1149 '
1150
1151 test_expect_success 'disabled option --set-upstream fails' '
1152 test_must_fail git branch --set-upstream origin/main
1153 '
1154
1155 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
1156 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
1157 cat >expect <<-\EOF &&
1158 warning: not setting branch 'my13' as its own upstream
1159 EOF
1160 test_expect_code 1 git config branch.my13.remote &&
1161 test_expect_code 1 git config branch.my13.merge &&
1162 test_cmp expect actual
1163 "
1164
1165 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
1166 test_when_finished git checkout main &&
1167 GIT_COMMITTER_DATE="2005-05-26 23:30" \
1168 git checkout -b g/h/i -l main &&
1169 test_ref_exists refs/heads/g/h/i &&
1170 cat >expect <<-EOF &&
1171 $HEAD refs/heads/g/h/i@{0}: branch: Created from main
1172 EOF
1173 git reflog show --no-abbrev-commit refs/heads/g/h/i >actual &&
1174 test_cmp expect actual
1175 '
1176
1177 test_expect_success 'checkout -b makes reflog by default' '
1178 git checkout main &&
1179 git config --unset core.logAllRefUpdates &&
1180 git checkout -b alpha &&
1181 git rev-parse --verify alpha@{0}
1182 '
1183
1184 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
1185 git checkout main &&
1186 git config core.logAllRefUpdates false &&
1187 git checkout -b beta &&
1188 test_must_fail git rev-parse --verify beta@{0}
1189 '
1190
1191 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
1192 git checkout main &&
1193 git checkout -lb gamma &&
1194 git config --unset core.logAllRefUpdates &&
1195 git rev-parse --verify gamma@{0}
1196 '
1197
1198 test_expect_success 'avoid ambiguous track and advise' '
1199 git config branch.autosetupmerge true &&
1200 git config remote.ambi1.url lalala &&
1201 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
1202 git config remote.ambi2.url lilili &&
1203 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
1204 cat <<-EOF >expected &&
1205 fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
1206 hint: There are multiple remotes whose fetch refspecs map to the remote
1207 hint: tracking ref '\''refs/heads/main'\'':
1208 hint: ambi1
1209 hint: ambi2
1210 hint:
1211 hint: This is typically a configuration error.
1212 hint:
1213 hint: To support setting up tracking branches, ensure that
1214 hint: different remotes'\'' fetch refspecs map into different
1215 hint: tracking namespaces.
1216 EOF
1217 test_must_fail git branch all1 main 2>actual &&
1218 test_cmp expected actual &&
1219 test -z "$(git config branch.all1.merge)"
1220 '
1221
1222 test_expect_success 'autosetuprebase local 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 local &&
1226 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1227 git branch mybase &&
1228 git branch --track myr1 mybase &&
1229 test "$(git config branch.myr1.remote)" = . &&
1230 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
1231 test "$(git config branch.myr1.rebase)" = true
1232 '
1233
1234 test_expect_success 'autosetuprebase always 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 always &&
1238 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1239 git branch mybase2 &&
1240 git branch --track myr2 mybase &&
1241 test "$(git config branch.myr2.remote)" = . &&
1242 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
1243 test "$(git config branch.myr2.rebase)" = true
1244 '
1245
1246 test_expect_success 'autosetuprebase remote on a tracked local branch' '
1247 git config remote.local.url . &&
1248 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1249 git config branch.autosetuprebase remote &&
1250 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1251 git branch mybase3 &&
1252 git branch --track myr3 mybase2 &&
1253 test "$(git config branch.myr3.remote)" = . &&
1254 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1255 ! test "$(git config branch.myr3.rebase)" = true
1256 '
1257
1258 test_expect_success 'autosetuprebase never on a tracked local branch' '
1259 git config remote.local.url . &&
1260 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1261 git config branch.autosetuprebase never &&
1262 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1263 git branch mybase4 &&
1264 git branch --track myr4 mybase2 &&
1265 test "$(git config branch.myr4.remote)" = . &&
1266 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1267 ! test "$(git config branch.myr4.rebase)" = true
1268 '
1269
1270 test_expect_success 'autosetuprebase local on a tracked remote branch' '
1271 git config remote.local.url . &&
1272 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1273 git config branch.autosetuprebase local &&
1274 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1275 git branch --track myr5 local/main &&
1276 test "$(git config branch.myr5.remote)" = local &&
1277 test "$(git config branch.myr5.merge)" = refs/heads/main &&
1278 ! test "$(git config branch.myr5.rebase)" = true
1279 '
1280
1281 test_expect_success 'autosetuprebase never on a tracked remote branch' '
1282 git config remote.local.url . &&
1283 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1284 git config branch.autosetuprebase never &&
1285 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1286 git branch --track myr6 local/main &&
1287 test "$(git config branch.myr6.remote)" = local &&
1288 test "$(git config branch.myr6.merge)" = refs/heads/main &&
1289 ! test "$(git config branch.myr6.rebase)" = true
1290 '
1291
1292 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1293 git config remote.local.url . &&
1294 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1295 git config branch.autosetuprebase remote &&
1296 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1297 git branch --track myr7 local/main &&
1298 test "$(git config branch.myr7.remote)" = local &&
1299 test "$(git config branch.myr7.merge)" = refs/heads/main &&
1300 test "$(git config branch.myr7.rebase)" = true
1301 '
1302
1303 test_expect_success 'autosetuprebase always on a tracked remote branch' '
1304 git config remote.local.url . &&
1305 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1306 git config branch.autosetuprebase remote &&
1307 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1308 git branch --track myr8 local/main &&
1309 test "$(git config branch.myr8.remote)" = local &&
1310 test "$(git config branch.myr8.merge)" = refs/heads/main &&
1311 test "$(git config branch.myr8.rebase)" = true
1312 '
1313
1314 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1315 git config --unset branch.autosetuprebase &&
1316 git config remote.local.url . &&
1317 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1318 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1319 git branch --track myr9 local/main &&
1320 test "$(git config branch.myr9.remote)" = local &&
1321 test "$(git config branch.myr9.merge)" = refs/heads/main &&
1322 test "z$(git config branch.myr9.rebase)" = z
1323 '
1324
1325 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1326 git config remote.local.url . &&
1327 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1328 (git show-ref -q refs/remotes/local/o || git fetch local) &&
1329 git branch mybase10 &&
1330 git branch --track myr10 mybase2 &&
1331 test "$(git config branch.myr10.remote)" = . &&
1332 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1333 test "z$(git config branch.myr10.rebase)" = z
1334 '
1335
1336 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1337 git config remote.local.url . &&
1338 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1339 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1340 git branch --no-track myr11 mybase2 &&
1341 test "z$(git config branch.myr11.remote)" = z &&
1342 test "z$(git config branch.myr11.merge)" = z &&
1343 test "z$(git config branch.myr11.rebase)" = z
1344 '
1345
1346 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1347 git config remote.local.url . &&
1348 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1349 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1350 git branch --no-track myr12 local/main &&
1351 test "z$(git config branch.myr12.remote)" = z &&
1352 test "z$(git config branch.myr12.merge)" = z &&
1353 test "z$(git config branch.myr12.rebase)" = z
1354 '
1355
1356 test_expect_success 'autosetuprebase never on an untracked local branch' '
1357 git config branch.autosetuprebase never &&
1358 git config remote.local.url . &&
1359 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1360 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1361 git branch --no-track myr13 mybase2 &&
1362 test "z$(git config branch.myr13.remote)" = z &&
1363 test "z$(git config branch.myr13.merge)" = z &&
1364 test "z$(git config branch.myr13.rebase)" = z
1365 '
1366
1367 test_expect_success 'autosetuprebase local on an untracked local branch' '
1368 git config branch.autosetuprebase local &&
1369 git config remote.local.url . &&
1370 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1371 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1372 git branch --no-track myr14 mybase2 &&
1373 test "z$(git config branch.myr14.remote)" = z &&
1374 test "z$(git config branch.myr14.merge)" = z &&
1375 test "z$(git config branch.myr14.rebase)" = z
1376 '
1377
1378 test_expect_success 'autosetuprebase remote on an untracked local branch' '
1379 git config branch.autosetuprebase remote &&
1380 git config remote.local.url . &&
1381 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1382 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1383 git branch --no-track myr15 mybase2 &&
1384 test "z$(git config branch.myr15.remote)" = z &&
1385 test "z$(git config branch.myr15.merge)" = z &&
1386 test "z$(git config branch.myr15.rebase)" = z
1387 '
1388
1389 test_expect_success 'autosetuprebase always on an untracked local branch' '
1390 git config branch.autosetuprebase always &&
1391 git config remote.local.url . &&
1392 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1393 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1394 git branch --no-track myr16 mybase2 &&
1395 test "z$(git config branch.myr16.remote)" = z &&
1396 test "z$(git config branch.myr16.merge)" = z &&
1397 test "z$(git config branch.myr16.rebase)" = z
1398 '
1399
1400 test_expect_success 'autosetuprebase never on an untracked remote branch' '
1401 git config branch.autosetuprebase never &&
1402 git config remote.local.url . &&
1403 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1404 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1405 git branch --no-track myr17 local/main &&
1406 test "z$(git config branch.myr17.remote)" = z &&
1407 test "z$(git config branch.myr17.merge)" = z &&
1408 test "z$(git config branch.myr17.rebase)" = z
1409 '
1410
1411 test_expect_success 'autosetuprebase local on an untracked remote branch' '
1412 git config branch.autosetuprebase local &&
1413 git config remote.local.url . &&
1414 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1415 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1416 git branch --no-track myr18 local/main &&
1417 test "z$(git config branch.myr18.remote)" = z &&
1418 test "z$(git config branch.myr18.merge)" = z &&
1419 test "z$(git config branch.myr18.rebase)" = z
1420 '
1421
1422 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1423 git config branch.autosetuprebase remote &&
1424 git config remote.local.url . &&
1425 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1426 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1427 git branch --no-track myr19 local/main &&
1428 test "z$(git config branch.myr19.remote)" = z &&
1429 test "z$(git config branch.myr19.merge)" = z &&
1430 test "z$(git config branch.myr19.rebase)" = z
1431 '
1432
1433 test_expect_success 'autosetuprebase always on an untracked remote branch' '
1434 git config branch.autosetuprebase always &&
1435 git config remote.local.url . &&
1436 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1437 (git show-ref -q refs/remotes/local/main || git fetch local) &&
1438 git branch --no-track myr20 local/main &&
1439 test "z$(git config branch.myr20.remote)" = z &&
1440 test "z$(git config branch.myr20.merge)" = z &&
1441 test "z$(git config branch.myr20.rebase)" = z
1442 '
1443
1444 test_expect_success 'autosetuprebase always on detached HEAD' '
1445 git config branch.autosetupmerge always &&
1446 test_when_finished git checkout main &&
1447 git checkout HEAD^0 &&
1448 git branch my11 &&
1449 test -z "$(git config branch.my11.remote)" &&
1450 test -z "$(git config branch.my11.merge)"
1451 '
1452
1453 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1454 git config branch.autosetuprebase garbage &&
1455 test_must_fail git branch
1456 '
1457
1458 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1459 git config --unset branch.autosetuprebase &&
1460 echo "[branch] autosetuprebase" >>.git/config &&
1461 test_must_fail git branch &&
1462 git config --unset branch.autosetuprebase
1463 '
1464
1465 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1466 git checkout my9 &&
1467 git config --unset branch.my8.merge &&
1468 test_must_fail git branch -d my8
1469 '
1470
1471 test_expect_success 'attempt to delete a branch merged to its base' '
1472 # we are on my9 which is the initial commit; traditionally
1473 # we would not have allowed deleting my8 that is not merged
1474 # to my9, but it is set to track main that already has my8
1475 git config branch.my8.merge refs/heads/main &&
1476 git branch -d my8
1477 '
1478
1479 test_expect_success 'attempt to delete a branch merged to its base' '
1480 git checkout main &&
1481 echo Third >>A &&
1482 git commit -m "Third commit" A &&
1483 git branch -t my10 my9 &&
1484 git branch -f my10 HEAD^ &&
1485 # we are on main which is at the third commit, and my10
1486 # is behind us, so traditionally we would have allowed deleting
1487 # it; but my10 is set to track my9 that is further behind.
1488 test_must_fail git branch -d my10
1489 '
1490
1491 test_expect_success 'branch --delete --force removes dangling branch' '
1492 git checkout main &&
1493 test_commit unstable &&
1494 hash=$(git rev-parse HEAD) &&
1495 objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
1496 git branch --no-track dangling &&
1497 mv $objpath $objpath.x &&
1498 test_when_finished "mv $objpath.x $objpath" &&
1499 git branch --delete --force dangling &&
1500 git for-each-ref refs/heads/dangling >actual &&
1501 test_must_be_empty actual
1502 '
1503
1504 test_expect_success 'use --edit-description' '
1505 EDITOR=: git branch --edit-description &&
1506 test_expect_code 1 git config branch.main.description &&
1507
1508 write_script editor <<-\EOF &&
1509 echo "New contents" >"$1"
1510 EOF
1511 EDITOR=./editor git branch --edit-description &&
1512 write_script editor <<-\EOF &&
1513 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1514 EOF
1515 EDITOR=./editor git branch --edit-description &&
1516 echo "New contents" >expect &&
1517 test_cmp expect EDITOR_OUTPUT
1518 '
1519
1520 test_expect_success 'detect typo in branch name when using --edit-description' '
1521 write_script editor <<-\EOF &&
1522 echo "New contents" >"$1"
1523 EOF
1524 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
1525 '
1526
1527 test_expect_success 'refuse --edit-description on unborn branch for now' '
1528 test_when_finished "git checkout main" &&
1529 write_script editor <<-\EOF &&
1530 echo "New contents" >"$1"
1531 EOF
1532 git checkout --orphan unborn &&
1533 test_must_fail env EDITOR=./editor git branch --edit-description
1534 '
1535
1536 test_expect_success '--merged catches invalid object names' '
1537 test_must_fail git branch --merged $ZERO_OID 2>err &&
1538 test_grep "must point to a commit" err
1539 '
1540
1541 test_expect_success '--list during rebase' '
1542 test_when_finished "reset_rebase" &&
1543 git checkout main &&
1544 FAKE_LINES="1 edit 2" &&
1545 export FAKE_LINES &&
1546 set_fake_editor &&
1547 git rebase -i HEAD~2 &&
1548 git branch --list >actual &&
1549 test_grep "rebasing main" actual
1550 '
1551
1552 test_expect_success '--list during rebase from detached HEAD' '
1553 test_when_finished "reset_rebase && git checkout main" &&
1554 git checkout main^0 &&
1555 oid=$(git rev-parse --short HEAD) &&
1556 FAKE_LINES="1 edit 2" &&
1557 export FAKE_LINES &&
1558 set_fake_editor &&
1559 git rebase -i HEAD~2 &&
1560 git branch --list >actual &&
1561 test_grep "rebasing detached HEAD $oid" actual
1562 '
1563
1564 test_expect_success 'tracking with unexpected .fetch refspec' '
1565 rm -rf a b c d &&
1566 git init -b main a &&
1567 (
1568 cd a &&
1569 test_commit a
1570 ) &&
1571 git init -b main b &&
1572 (
1573 cd b &&
1574 test_commit b
1575 ) &&
1576 git init -b main c &&
1577 (
1578 cd c &&
1579 test_commit c &&
1580 git remote add a ../a &&
1581 git remote add b ../b &&
1582 git fetch --all
1583 ) &&
1584 git init -b main d &&
1585 (
1586 cd d &&
1587 git remote add c ../c &&
1588 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1589 git fetch c &&
1590 git branch --track local/a/main remotes/a/main &&
1591 test "$(git config branch.local/a/main.remote)" = "c" &&
1592 test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
1593 git rev-parse --verify a >expect &&
1594 git rev-parse --verify local/a/main >actual &&
1595 test_cmp expect actual
1596 )
1597 '
1598
1599 test_expect_success 'configured committerdate sort' '
1600 git init -b main sort &&
1601 test_config -C sort branch.sort "committerdate" &&
1602
1603 (
1604 cd sort &&
1605 test_commit initial &&
1606 git checkout -b a &&
1607 test_commit a &&
1608 git checkout -b c &&
1609 test_commit c &&
1610 git checkout -b b &&
1611 test_commit b &&
1612 git branch >actual &&
1613 cat >expect <<-\EOF &&
1614 main
1615 a
1616 c
1617 * b
1618 EOF
1619 test_cmp expect actual
1620 )
1621 '
1622
1623 test_expect_success 'option override configured sort' '
1624 test_config -C sort branch.sort "committerdate" &&
1625
1626 (
1627 cd sort &&
1628 git branch --sort=refname >actual &&
1629 cat >expect <<-\EOF &&
1630 a
1631 * b
1632 c
1633 main
1634 EOF
1635 test_cmp expect actual
1636 )
1637 '
1638
1639 test_expect_success '--no-sort cancels config sort keys' '
1640 test_config -C sort branch.sort "-refname" &&
1641
1642 (
1643 cd sort &&
1644
1645 # objecttype is identical for all of them, so sort falls back on
1646 # default (ascending refname)
1647 git branch \
1648 --no-sort \
1649 --sort="objecttype" >actual &&
1650 cat >expect <<-\EOF &&
1651 a
1652 * b
1653 c
1654 main
1655 EOF
1656 test_cmp expect actual
1657 )
1658
1659 '
1660
1661 test_expect_success '--no-sort cancels command line sort keys' '
1662 (
1663 cd sort &&
1664
1665 # objecttype is identical for all of them, so sort falls back on
1666 # default (ascending refname)
1667 git branch \
1668 --sort="-refname" \
1669 --no-sort \
1670 --sort="objecttype" >actual &&
1671 cat >expect <<-\EOF &&
1672 a
1673 * b
1674 c
1675 main
1676 EOF
1677 test_cmp expect actual
1678 )
1679 '
1680
1681 test_expect_success '--no-sort without subsequent --sort prints expected branches' '
1682 (
1683 cd sort &&
1684
1685 # Sort the results with `sort` for a consistent comparison
1686 # against expected
1687 git branch --no-sort | sort >actual &&
1688 cat >expect <<-\EOF &&
1689 a
1690 c
1691 main
1692 * b
1693 EOF
1694 test_cmp expect actual
1695 )
1696 '
1697
1698 test_expect_success 'invalid sort parameter in configuration' '
1699 test_config -C sort branch.sort "v:notvalid" &&
1700
1701 (
1702 cd sort &&
1703
1704 # this works in the "listing" mode, so bad sort key
1705 # is a dying offence.
1706 test_must_fail git branch &&
1707
1708 # these do not need to use sorting, and should all
1709 # succeed
1710 git branch newone main &&
1711 git branch -c newone newerone &&
1712 git branch -m newone newestone &&
1713 git branch -d newerone newestone
1714 )
1715 '
1716
1717 test_expect_success 'tracking info copied with --track=inherit' '
1718 git branch --track=inherit foo2 my1 &&
1719 test_cmp_config local branch.foo2.remote &&
1720 test_cmp_config refs/heads/main branch.foo2.merge
1721 '
1722
1723 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
1724 test_unconfig branch.autoSetupMerge &&
1725 # default config does not copy tracking info
1726 git branch foo-no-inherit my1 &&
1727 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
1728 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
1729 # with autoSetupMerge=inherit, we copy tracking info from my1
1730 test_config branch.autoSetupMerge inherit &&
1731 git branch foo3 my1 &&
1732 test_cmp_config local branch.foo3.remote &&
1733 test_cmp_config refs/heads/main branch.foo3.merge &&
1734 # no tracking info to inherit from main
1735 git branch main2 main &&
1736 test_cmp_config "" --default "" branch.main2.remote &&
1737 test_cmp_config "" --default "" branch.main2.merge
1738 '
1739
1740 test_expect_success '--track overrides branch.autoSetupMerge' '
1741 test_config branch.autoSetupMerge inherit &&
1742 git branch --track=direct foo4 my1 &&
1743 test_cmp_config . branch.foo4.remote &&
1744 test_cmp_config refs/heads/my1 branch.foo4.merge &&
1745 git branch --no-track foo5 my1 &&
1746 test_cmp_config "" --default "" branch.foo5.remote &&
1747 test_cmp_config "" --default "" branch.foo5.merge
1748 '
1749
1750 test_expect_success 'errors if given a bad branch name' '
1751 cat <<-EOF >expect &&
1752 fatal: ${SQ}foo..bar${SQ} is not a valid branch name
1753 hint: See ${SQ}git help check-ref-format${SQ}
1754 hint: Disable this message with "git config set advice.refSyntax false"
1755 EOF
1756 test_must_fail git branch foo..bar >actual 2>&1 &&
1757 test_cmp expect actual
1758 '
1759
1760 test_expect_success '--forked: setup' '
1761 test_create_repo forked-upstream &&
1762 (
1763 cd forked-upstream &&
1764 test_commit base &&
1765 git branch one base &&
1766 git branch two base
1767 ) &&
1768
1769 test_create_repo forked-other &&
1770 (
1771 cd forked-other &&
1772 test_commit other-base &&
1773 git branch foreign other-base
1774 ) &&
1775
1776 git clone forked-upstream forked &&
1777 (
1778 cd forked &&
1779 git remote add -f other ../forked-other &&
1780 git branch local-base &&
1781 git branch --track local-one origin/one &&
1782 git branch --track local-two origin/two &&
1783 git branch --track local-foreign other/foreign &&
1784 git branch --track local-onbase local-base &&
1785
1786 git checkout local-one &&
1787 test_commit --no-tag local-one-work local-one.t &&
1788 git checkout local-foreign &&
1789 test_commit --no-tag local-foreign-work local-foreign.t
1790 )
1791 '
1792
1793 test_expect_success '--forked <upstream-tracking-branch> filters by upstream' '
1794 git -C forked branch --forked origin/one --format="%(refname:short)" >actual &&
1795 echo local-one >expect &&
1796 test_cmp expect actual
1797 '
1798
1799 test_expect_success '--forked <glob> filters by wildmatch' '
1800 git -C forked branch --forked "origin/*" --format="%(refname:short)" >actual &&
1801 cat >expect <<-\EOF &&
1802 local-one
1803 local-two
1804 main
1805 EOF
1806 test_cmp expect actual
1807 '
1808
1809 test_expect_success '--forked <local-branch> matches branches with local upstream' '
1810 git -C forked branch --forked local-base --format="%(refname:short)" >actual &&
1811 echo local-onbase >expect &&
1812 test_cmp expect actual
1813 '
1814
1815 test_expect_success '--forked can be repeated to widen the filter' '
1816 git -C forked branch --forked origin/one --forked other/foreign --format="%(refname:short)" >actual &&
1817 cat >expect <<-\EOF &&
1818 local-foreign
1819 local-one
1820 EOF
1821 test_cmp expect actual
1822 '
1823
1824 test_expect_success '--forked combines literal and glob arguments' '
1825 git -C forked branch --forked local-base --forked "other/*" --format="%(refname:short)" >actual &&
1826 cat >expect <<-\EOF &&
1827 local-foreign
1828 local-onbase
1829 EOF
1830 test_cmp expect actual
1831 '
1832
1833 test_expect_success '--forked "*/*" covers every remote-tracking upstream' '
1834 git -C forked branch --forked "*/*" --format="%(refname:short)" >actual &&
1835 cat >expect <<-\EOF &&
1836 local-foreign
1837 local-one
1838 local-two
1839 main
1840 EOF
1841 test_cmp expect actual
1842 '
1843
1844 test_expect_success '--forked composes with --no-merged' '
1845 git -C forked branch --forked "origin/*" --no-merged origin/one --format="%(refname:short)" >actual &&
1846 echo local-one >expect &&
1847 test_cmp expect actual
1848 '
1849
1850 test_expect_success '--forked <remote> uses the branch <remote>/HEAD points at' '
1851 git -C forked branch --forked origin --format="%(refname:short)" >actual &&
1852 echo main >expect &&
1853 test_cmp expect actual
1854 '
1855
1856 test_expect_success '--forked narrows a <pattern> argument' '
1857 git -C forked branch --forked "origin/*" "local-*" --format="%(refname:short)" >actual &&
1858 cat >expect <<-\EOF &&
1859 local-one
1860 local-two
1861 EOF
1862 test_cmp expect actual
1863 '
1864
1865 test_expect_success '--forked rejects unknown branch/pattern' '
1866 test_must_fail git -C forked branch --forked nope 2>err &&
1867 test_grep "not a valid branch or pattern" err
1868 '
1869
1870 test_expect_success '--forked requires a value' '
1871 test_must_fail git -C forked branch --forked 2>err &&
1872 test_grep "requires a value" err
1873 '
1874
1875 test_expect_success '--delete-merged: setup' '
1876 git init -b main upstream &&
1877 (
1878 cd upstream &&
1879 test_commit base &&
1880 git checkout -b next &&
1881 test_commit next-work &&
1882 git checkout main
1883 ) &&
1884 git init -b main other &&
1885 test_commit -C other other-base &&
1886 git init -b main fork
1887 '
1888
1889 setup_repo_for_delete_merged () {
1890 rm -rf repo &&
1891 git clone upstream repo &&
1892 (
1893 cd repo &&
1894 git remote add fork ../fork &&
1895 git remote add other ../other &&
1896 git config push.default current &&
1897 git fetch other
1898 )
1899 }
1900
1901 create_merged_branch () {
1902 (
1903 cd repo &&
1904 git checkout -b "$1" --track origin/next &&
1905 git commit --allow-empty -m "$1 work" &&
1906 git push origin "$1:next"
1907 )
1908 }
1909
1910 check_branches () {
1911 git for-each-ref --format="%(refname:short)" refs/heads/ >actual &&
1912 cat >expect &&
1913 test_cmp expect actual
1914 }
1915
1916 test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '
1917 setup_repo_for_delete_merged &&
1918 (
1919 cd repo &&
1920 test_cmp_config origin branch.main.remote &&
1921 test_cmp_config refs/heads/main branch.main.merge &&
1922 git checkout --detach &&
1923
1924 git branch --delete-merged */* &&
1925
1926 check_branches <<-\EOF
1927 main
1928 EOF
1929 )
1930 '
1931
1932 test_expect_success '--delete-merged deletes only selected merged branches' '
1933 setup_repo_for_delete_merged &&
1934 create_merged_branch also-merged &&
1935 create_merged_branch merged &&
1936 (
1937 cd repo &&
1938 git checkout -b unmerged --track origin/next &&
1939 git commit --allow-empty -m "unmerged work" &&
1940 git checkout -b tracks-other --track other/main &&
1941 sha=$(git rev-parse --short merged) &&
1942
1943 git branch --dry-run --delete-merged origin/next merged >actual 2>&1 &&
1944 echo "Would delete branch merged (was $sha)." >expect &&
1945 test_cmp expect actual &&
1946 git rev-parse --verify refs/heads/merged &&
1947
1948 check_branches <<-\EOF &&
1949 also-merged
1950 main
1951 merged
1952 tracks-other
1953 unmerged
1954 EOF
1955
1956 git branch --delete-merged origin/next merged >actual 2>&1 &&
1957 echo "Deleted branch merged (was $sha)." >expect &&
1958 test_cmp expect actual &&
1959
1960 check_branches <<-\EOF
1961 also-merged
1962 main
1963 tracks-other
1964 unmerged
1965 EOF
1966 )
1967 '
1968
1969 test_expect_success '--delete-merged keeps main despite a different default push remote' '
1970 setup_repo_for_delete_merged &&
1971 create_merged_branch on-next &&
1972 create_merged_branch checked-out &&
1973 create_merged_branch upstream-gone &&
1974 (
1975 cd repo &&
1976 git config remote.pushDefault fork &&
1977 git checkout -b local-to-delete --track main &&
1978 git config branch.upstream-gone.merge refs/heads/topic &&
1979 git checkout -b tracks-other --track other/main &&
1980 git checkout checked-out &&
1981
1982 git branch --delete-merged origin/* --delete-merged main &&
1983
1984 check_branches <<-\EOF
1985 checked-out
1986 main
1987 tracks-other
1988 upstream-gone
1989 EOF
1990 )
1991 '
1992
1993 test_expect_success '--delete-merged maps push refspecs to upstreams' '
1994 setup_repo_for_delete_merged &&
1995 (
1996 cd repo &&
1997 git checkout -b topic &&
1998 git config remote.origin.push "refs/heads/topic:refs/heads/published" &&
1999 git push origin &&
2000 git branch --set-upstream-to=origin/published topic &&
2001 git checkout -b other-topic --track origin/published &&
2002 git checkout --detach &&
2003
2004 git branch --delete-merged origin/published &&
2005
2006 check_branches <<-\EOF
2007 main
2008 topic
2009 EOF
2010 )
2011 '
2012
2013 test_expect_success '--delete-merged keeps the upstream of a surviving branch' '
2014 setup_repo_for_delete_merged &&
2015 create_merged_branch feature &&
2016 (
2017 cd repo &&
2018 git checkout -b topic --track feature &&
2019 git commit --allow-empty -m "topic work" &&
2020
2021 git branch --dry-run --delete-merged origin/next >out &&
2022 test_grep ! "feature" out &&
2023
2024 git branch --delete-merged origin/next 2>err &&
2025 test_must_be_empty err &&
2026
2027 check_branches <<-\EOF &&
2028 feature
2029 main
2030 topic
2031 EOF
2032
2033 git config --local --get-regexp "branch\\.(feature|topic)\\.(merge|remote)" >actual &&
2034 cat >expect <<-\EOF &&
2035 branch.feature.remote origin
2036 branch.feature.merge refs/heads/next
2037 branch.topic.remote .
2038 branch.topic.merge refs/heads/feature
2039 EOF
2040 test_cmp expect actual
2041 )
2042 '
2043
2044 test_expect_success '--delete-merged clears the deleted upstream of a protected branch' '
2045 setup_repo_for_delete_merged &&
2046 (
2047 cd repo &&
2048 git branch --track lower origin/next &&
2049 git branch --track mid lower &&
2050 git checkout -b tip --track mid &&
2051 git commit --allow-empty -m "tip work" &&
2052 sha=$(git rev-parse --short lower) &&
2053
2054 git branch --dry-run --delete-merged origin/next --delete-merged lower >actual 2>&1 &&
2055 echo "Would delete branch lower (was $sha)." >expect &&
2056 test_cmp expect actual &&
2057
2058 git config --local --get-regexp "branch\\.(lower|mid|tip)\\.(merge|remote)" >actual &&
2059 cat >expect <<-\EOF &&
2060 branch.lower.remote origin
2061 branch.lower.merge refs/heads/next
2062 branch.mid.remote .
2063 branch.mid.merge refs/heads/lower
2064 branch.tip.remote .
2065 branch.tip.merge refs/heads/mid
2066 EOF
2067 test_cmp expect actual &&
2068
2069 git branch --delete-merged origin/next --delete-merged lower >actual 2>&1 &&
2070 echo "Deleted branch lower (was $sha)." >expect &&
2071 test_cmp expect actual &&
2072
2073 check_branches <<-\EOF &&
2074 main
2075 mid
2076 tip
2077 EOF
2078
2079 git config --local --get-regexp "branch\\.(mid|tip)\\.(merge|remote)" >actual &&
2080 cat >expect <<-\EOF &&
2081 branch.tip.remote .
2082 branch.tip.merge refs/heads/mid
2083 EOF
2084 test_cmp expect actual
2085 )
2086 '
2087
2088 test_expect_success '--delete-merged result is independent of stacked branch names' '
2089 setup_repo_for_delete_merged &&
2090 (
2091 cd repo &&
2092 git branch --track c-lower origin/next &&
2093 git branch --track b-mid c-lower &&
2094 git checkout -b a-tip --track b-mid &&
2095 git commit --allow-empty -m "tip work" &&
2096
2097 git branch --delete-merged origin/next --delete-merged "c-*" &&
2098
2099 check_branches <<-\EOF &&
2100 a-tip
2101 b-mid
2102 main
2103 EOF
2104
2105 git branch --delete-merged origin/next --delete-merged "c-*" >actual 2>&1 &&
2106 test_must_be_empty actual &&
2107
2108 check_branches <<-\EOF
2109 a-tip
2110 b-mid
2111 main
2112 EOF
2113 )
2114 '
2115
2116 test_expect_success '--delete-merged requires a value' '
2117 test_must_fail git -C forked branch --delete-merged 2>err &&
2118 test_grep "requires a value" err
2119 '
2120
2121 test_expect_success '--delete-merged honours branch.<name>.deleteMerged=false' '
2122 setup_repo_for_delete_merged &&
2123 create_merged_branch deleted &&
2124 create_merged_branch kept &&
2125 (
2126 cd repo &&
2127 git config branch.kept.deleteMerged false &&
2128 git checkout --detach &&
2129
2130 git branch --delete-merged origin/next 2>err &&
2131
2132 test_grep "Skipping .kept." err &&
2133 check_branches <<-\EOF
2134 kept
2135 main
2136 EOF
2137 )
2138 '
2139
2140 test_expect_success "branch -d still deletes a deleteMerged=false branch" '
2141 setup_repo_for_delete_merged &&
2142 create_merged_branch kept &&
2143 (
2144 cd repo &&
2145 git config branch.kept.deleteMerged false &&
2146 git checkout --detach &&
2147
2148 git branch -d kept &&
2149
2150 check_branches <<-\EOF
2151 main
2152 EOF
2153 )
2154 '
2155
2156 test_expect_success '--dry-run without --delete-merged is rejected' '
2157 test_must_fail git -C forked branch --dry-run 2>err &&
2158 test_grep "requires --delete-merged" err
2159 '
2160
2161 test_done