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