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