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