interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t3200-branch.sh

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