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