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