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