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