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