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