branch: use BRANCH_COLOR_LOCAL in ref-filter format
[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
10 test_expect_success 'prepare a trivial repository' '
11         echo Hello >A &&
12         git update-index --add A &&
13         git commit -m "Initial commit." &&
14         echo World >>A &&
15         git update-index --add A &&
16         git commit -m "Second commit." &&
17         HEAD=$(git rev-parse --verify HEAD)
18 '
19
20 test_expect_success 'git branch --help should not have created a bogus branch' '
21         test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
22         test_path_is_missing .git/refs/heads/--help
23 '
24
25 test_expect_success 'branch -h in broken repository' '
26         mkdir broken &&
27         (
28                 cd broken &&
29                 git init &&
30                 >.git/refs/heads/master &&
31                 test_expect_code 129 git branch -h >usage 2>&1
32         ) &&
33         test_i18ngrep "[Uu]sage" broken/usage
34 '
35
36 test_expect_success 'git branch abc should create a branch' '
37         git branch abc && test_path_is_file .git/refs/heads/abc
38 '
39
40 test_expect_success 'git branch a/b/c should create a branch' '
41         git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
42 '
43
44 test_expect_success 'git branch HEAD should fail' '
45         test_must_fail git branch HEAD
46 '
47
48 cat >expect <<EOF
49 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
50 EOF
51 test_expect_success 'git branch -l d/e/f should create a branch and a log' '
52         GIT_COMMITTER_DATE="2005-05-26 23:30" \
53         git branch -l d/e/f &&
54         test_path_is_file .git/refs/heads/d/e/f &&
55         test_path_is_file .git/logs/refs/heads/d/e/f &&
56         test_cmp expect .git/logs/refs/heads/d/e/f
57 '
58
59 test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
60         git branch -d d/e/f &&
61         test_path_is_missing .git/refs/heads/d/e/f &&
62         test_must_fail git reflog exists refs/heads/d/e/f
63 '
64
65 test_expect_success 'git branch j/k should work after branch j has been deleted' '
66         git branch j &&
67         git branch -d j &&
68         git branch j/k
69 '
70
71 test_expect_success 'git branch l should work after branch l/m has been deleted' '
72         git branch l/m &&
73         git branch -d l/m &&
74         git branch l
75 '
76
77 test_expect_success 'git branch -m dumps usage' '
78         test_expect_code 128 git branch -m 2>err &&
79         test_i18ngrep "branch name required" err
80 '
81
82 test_expect_success 'git branch -m m broken_symref should work' '
83         test_when_finished "git branch -D broken_symref" &&
84         git branch -l m &&
85         git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
86         git branch -m m broken_symref &&
87         git reflog exists refs/heads/broken_symref &&
88         test_must_fail git reflog exists refs/heads/i_am_broken
89 '
90
91 test_expect_success 'git branch -m m m/m should work' '
92         git branch -l m &&
93         git branch -m m m/m &&
94         git reflog exists refs/heads/m/m
95 '
96
97 test_expect_success 'git branch -m n/n n should work' '
98         git branch -l n/n &&
99         git branch -m n/n n &&
100         git reflog exists refs/heads/n
101 '
102
103 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
104         git branch o/o &&
105         git branch o/p &&
106         test_must_fail git branch -m o/o o
107 '
108
109 test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
110         git branch o/q &&
111         test_must_fail git branch -m o/q o/p
112 '
113
114 test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
115         git branch -M o/q o/p
116 '
117
118 test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
119         git branch o/q &&
120         git branch -m -f o/q o/p
121 '
122
123 test_expect_success 'git branch -m q r/q should fail when r exists' '
124         git branch q &&
125         git branch r &&
126         test_must_fail git branch -m q r/q
127 '
128
129 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
130         git branch bar &&
131         git checkout -b foo &&
132         test_must_fail git branch -M bar foo
133 '
134
135 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
136         git checkout -b baz &&
137         git branch bam &&
138         git branch -M baz bam &&
139         test $(git rev-parse --abbrev-ref HEAD) = bam
140 '
141
142 test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
143         git checkout master &&
144         git worktree add -b baz bazdir &&
145         git worktree add -f bazdir2 baz &&
146         git branch -M baz bam &&
147         test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
148         test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
149 '
150
151 test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
152         git checkout -b baz &&
153         git worktree add -f bazdir3 baz &&
154         (
155                 cd bazdir3 &&
156                 git branch -M baz bam &&
157                 test $(git rev-parse --abbrev-ref HEAD) = bam
158         ) &&
159         test $(git rev-parse --abbrev-ref HEAD) = bam
160 '
161
162 test_expect_success 'git branch -M master should work when master is checked out' '
163         git checkout master &&
164         git branch -M master
165 '
166
167 test_expect_success 'git branch -M master master should work when master is checked out' '
168         git checkout master &&
169         git branch -M master master
170 '
171
172 test_expect_success 'git branch -M master2 master2 should work when master is checked out' '
173         git checkout master &&
174         git branch master2 &&
175         git branch -M master2 master2
176 '
177
178 test_expect_success 'git branch -v -d t should work' '
179         git branch t &&
180         test_path_is_file .git/refs/heads/t &&
181         git branch -v -d t &&
182         test_path_is_missing .git/refs/heads/t
183 '
184
185 test_expect_success 'git branch -v -m t s should work' '
186         git branch t &&
187         test_path_is_file .git/refs/heads/t &&
188         git branch -v -m t s &&
189         test_path_is_missing .git/refs/heads/t &&
190         test_path_is_file .git/refs/heads/s &&
191         git branch -d s
192 '
193
194 test_expect_success 'git branch -m -d t s should fail' '
195         git branch t &&
196         test_path_is_file .git/refs/heads/t &&
197         test_must_fail git branch -m -d t s &&
198         git branch -d t &&
199         test_path_is_missing .git/refs/heads/t
200 '
201
202 test_expect_success 'git branch --list -d t should fail' '
203         git branch t &&
204         test_path_is_file .git/refs/heads/t &&
205         test_must_fail git branch --list -d t &&
206         git branch -d t &&
207         test_path_is_missing .git/refs/heads/t
208 '
209
210 test_expect_success 'git branch --list -v with --abbrev' '
211         test_when_finished "git branch -D t" &&
212         git branch t &&
213         git branch -v --list t >actual.default &&
214         git branch -v --list --abbrev t >actual.abbrev &&
215         test_cmp actual.default actual.abbrev &&
216
217         git branch -v --list --no-abbrev t >actual.noabbrev &&
218         git branch -v --list --abbrev=0 t >actual.0abbrev &&
219         test_cmp actual.noabbrev actual.0abbrev &&
220
221         git branch -v --list --abbrev=36 t >actual.36abbrev &&
222         # how many hexdigits are used?
223         read name objdefault rest <actual.abbrev &&
224         read name obj36 rest <actual.36abbrev &&
225         objfull=$(git rev-parse --verify t) &&
226
227         # are we really getting abbreviations?
228         test "$obj36" != "$objdefault" &&
229         expr "$obj36" : "$objdefault" >/dev/null &&
230         test "$objfull" != "$obj36" &&
231         expr "$objfull" : "$obj36" >/dev/null
232
233 '
234
235 test_expect_success 'git branch --column' '
236         COLUMNS=81 git branch --column=column >actual &&
237         cat >expected <<\EOF &&
238   a/b/c     bam       foo       l       * master    n         o/p       r
239   abc       bar       j/k       m/m       master2   o/o       q
240 EOF
241         test_cmp expected actual
242 '
243
244 test_expect_success 'git branch --column with an extremely long branch name' '
245         long=this/is/a/part/of/long/branch/name &&
246         long=z$long/$long/$long/$long &&
247         test_when_finished "git branch -d $long" &&
248         git branch $long &&
249         COLUMNS=80 git branch --column=column >actual &&
250         cat >expected <<EOF &&
251   a/b/c
252   abc
253   bam
254   bar
255   foo
256   j/k
257   l
258   m/m
259 * master
260   master2
261   n
262   o/o
263   o/p
264   q
265   r
266   $long
267 EOF
268         test_cmp expected actual
269 '
270
271 test_expect_success 'git branch with column.*' '
272         git config column.ui column &&
273         git config column.branch "dense" &&
274         COLUMNS=80 git branch >actual &&
275         git config --unset column.branch &&
276         git config --unset column.ui &&
277         cat >expected <<\EOF &&
278   a/b/c   bam   foo   l   * master    n     o/p   r
279   abc     bar   j/k   m/m   master2   o/o   q
280 EOF
281         test_cmp expected actual
282 '
283
284 test_expect_success 'git branch --column -v should fail' '
285         test_must_fail git branch --column -v
286 '
287
288 test_expect_success 'git branch -v with column.ui ignored' '
289         git config column.ui column &&
290         COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
291         git config --unset column.ui &&
292         cat >expected <<\EOF &&
293   a/b/c
294   abc
295   bam
296   bar
297   foo
298   j/k
299   l
300   m/m
301 * master
302   master2
303   n
304   o/o
305   o/p
306   q
307   r
308 EOF
309         test_cmp expected actual
310 '
311
312 mv .git/config .git/config-saved
313
314 test_expect_success 'git branch -m q q2 without config should succeed' '
315         git branch -m q q2 &&
316         git branch -m q2 q
317 '
318
319 mv .git/config-saved .git/config
320
321 git config branch.s/s.dummy Hello
322
323 test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
324         git branch -l s/s &&
325         git reflog exists refs/heads/s/s &&
326         git branch -l s/t &&
327         git reflog exists refs/heads/s/t &&
328         git branch -d s/t &&
329         git branch -m s/s s &&
330         git reflog exists refs/heads/s
331 '
332
333 test_expect_success 'config information was renamed, too' '
334         test $(git config branch.s.dummy) = Hello &&
335         test_must_fail git config branch.s/s/dummy
336 '
337
338 test_expect_success 'deleting a symref' '
339         git branch target &&
340         git symbolic-ref refs/heads/symref refs/heads/target &&
341         echo "Deleted branch symref (was refs/heads/target)." >expect &&
342         git branch -d symref >actual &&
343         test_path_is_file .git/refs/heads/target &&
344         test_path_is_missing .git/refs/heads/symref &&
345         test_i18ncmp expect actual
346 '
347
348 test_expect_success 'deleting a dangling symref' '
349         git symbolic-ref refs/heads/dangling-symref nowhere &&
350         test_path_is_file .git/refs/heads/dangling-symref &&
351         echo "Deleted branch dangling-symref (was nowhere)." >expect &&
352         git branch -d dangling-symref >actual &&
353         test_path_is_missing .git/refs/heads/dangling-symref &&
354         test_i18ncmp expect actual
355 '
356
357 test_expect_success 'deleting a self-referential symref' '
358         git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
359         test_path_is_file .git/refs/heads/self-reference &&
360         echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
361         git branch -d self-reference >actual &&
362         test_path_is_missing .git/refs/heads/self-reference &&
363         test_i18ncmp expect actual
364 '
365
366 test_expect_success 'renaming a symref is not allowed' '
367         git symbolic-ref refs/heads/master2 refs/heads/master &&
368         test_must_fail git branch -m master2 master3 &&
369         git symbolic-ref refs/heads/master2 &&
370         test_path_is_file .git/refs/heads/master &&
371         test_path_is_missing .git/refs/heads/master3
372 '
373
374 test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
375         git branch -l u &&
376         mv .git/logs/refs/heads/u real-u &&
377         ln -s real-u .git/logs/refs/heads/u &&
378         test_must_fail git branch -m u v
379 '
380
381 test_expect_success 'test tracking setup via --track' '
382         git config remote.local.url . &&
383         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
384         (git show-ref -q refs/remotes/local/master || git fetch local) &&
385         git branch --track my1 local/master &&
386         test $(git config branch.my1.remote) = local &&
387         test $(git config branch.my1.merge) = refs/heads/master
388 '
389
390 test_expect_success 'test tracking setup (non-wildcard, matching)' '
391         git config remote.local.url . &&
392         git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
393         (git show-ref -q refs/remotes/local/master || git fetch local) &&
394         git branch --track my4 local/master &&
395         test $(git config branch.my4.remote) = local &&
396         test $(git config branch.my4.merge) = refs/heads/master
397 '
398
399 test_expect_success 'tracking setup fails on non-matching refspec' '
400         git config remote.local.url . &&
401         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
402         (git show-ref -q refs/remotes/local/master || git fetch local) &&
403         git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
404         test_must_fail git branch --track my5 local/master &&
405         test_must_fail git config branch.my5.remote &&
406         test_must_fail git config branch.my5.merge
407 '
408
409 test_expect_success 'test tracking setup via config' '
410         git config branch.autosetupmerge true &&
411         git config remote.local.url . &&
412         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
413         (git show-ref -q refs/remotes/local/master || git fetch local) &&
414         git branch my3 local/master &&
415         test $(git config branch.my3.remote) = local &&
416         test $(git config branch.my3.merge) = refs/heads/master
417 '
418
419 test_expect_success 'test overriding tracking setup via --no-track' '
420         git config branch.autosetupmerge true &&
421         git config remote.local.url . &&
422         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
423         (git show-ref -q refs/remotes/local/master || git fetch local) &&
424         git branch --no-track my2 local/master &&
425         git config branch.autosetupmerge false &&
426         ! test "$(git config branch.my2.remote)" = local &&
427         ! test "$(git config branch.my2.merge)" = refs/heads/master
428 '
429
430 test_expect_success 'no tracking without .fetch entries' '
431         git config branch.autosetupmerge true &&
432         git branch my6 s &&
433         git config branch.autosetupmerge false &&
434         test -z "$(git config branch.my6.remote)" &&
435         test -z "$(git config branch.my6.merge)"
436 '
437
438 test_expect_success 'test tracking setup via --track but deeper' '
439         git config remote.local.url . &&
440         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
441         (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
442         git branch --track my7 local/o/o &&
443         test "$(git config branch.my7.remote)" = local &&
444         test "$(git config branch.my7.merge)" = refs/heads/o/o
445 '
446
447 test_expect_success 'test deleting branch deletes branch config' '
448         git branch -d my7 &&
449         test -z "$(git config branch.my7.remote)" &&
450         test -z "$(git config branch.my7.merge)"
451 '
452
453 test_expect_success 'test deleting branch without config' '
454         git branch my7 s &&
455         sha1=$(git rev-parse my7 | cut -c 1-7) &&
456         echo "Deleted branch my7 (was $sha1)." >expect &&
457         git branch -d my7 >actual 2>&1 &&
458         test_i18ncmp expect actual
459 '
460
461 test_expect_success 'deleting currently checked out branch fails' '
462         git worktree add -b my7 my7 &&
463         test_must_fail git -C my7 branch -d my7 &&
464         test_must_fail git branch -d my7
465 '
466
467 test_expect_success 'test --track without .fetch entries' '
468         git branch --track my8 &&
469         test "$(git config branch.my8.remote)" &&
470         test "$(git config branch.my8.merge)"
471 '
472
473 test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
474         git config branch.autosetupmerge always &&
475         git branch my9 HEAD^ &&
476         git config branch.autosetupmerge false
477 '
478
479 test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
480         test_must_fail git branch --track my10 HEAD^
481 '
482
483 test_expect_success 'branch from tag w/--track causes failure' '
484         git tag foobar &&
485         test_must_fail git branch --track my11 foobar
486 '
487
488 test_expect_success '--set-upstream-to fails on multiple branches' '
489         test_must_fail git branch --set-upstream-to master a b c
490 '
491
492 test_expect_success '--set-upstream-to fails on detached HEAD' '
493         git checkout HEAD^{} &&
494         test_must_fail git branch --set-upstream-to master &&
495         git checkout -
496 '
497
498 test_expect_success '--set-upstream-to fails on a missing dst branch' '
499         test_must_fail git branch --set-upstream-to master does-not-exist
500 '
501
502 test_expect_success '--set-upstream-to fails on a missing src branch' '
503         test_must_fail git branch --set-upstream-to does-not-exist master
504 '
505
506 test_expect_success '--set-upstream-to fails on a non-ref' '
507         test_must_fail git branch --set-upstream-to HEAD^{}
508 '
509
510 test_expect_success '--set-upstream-to fails on locked config' '
511         test_when_finished "rm -f .git/config.lock" &&
512         >.git/config.lock &&
513         git branch locked &&
514         test_must_fail git branch --set-upstream-to locked
515 '
516
517 test_expect_success 'use --set-upstream-to modify HEAD' '
518         test_config branch.master.remote foo &&
519         test_config branch.master.merge foo &&
520         git branch my12 &&
521         git branch --set-upstream-to my12 &&
522         test "$(git config branch.master.remote)" = "." &&
523         test "$(git config branch.master.merge)" = "refs/heads/my12"
524 '
525
526 test_expect_success 'use --set-upstream-to modify a particular branch' '
527         git branch my13 &&
528         git branch --set-upstream-to master my13 &&
529         test "$(git config branch.my13.remote)" = "." &&
530         test "$(git config branch.my13.merge)" = "refs/heads/master"
531 '
532
533 test_expect_success '--unset-upstream should fail if given a non-existent branch' '
534         test_must_fail git branch --unset-upstream i-dont-exist
535 '
536
537 test_expect_success '--unset-upstream should fail if config is locked' '
538         test_when_finished "rm -f .git/config.lock" &&
539         git branch --set-upstream-to locked &&
540         >.git/config.lock &&
541         test_must_fail git branch --unset-upstream
542 '
543
544 test_expect_success 'test --unset-upstream on HEAD' '
545         git branch my14 &&
546         test_config branch.master.remote foo &&
547         test_config branch.master.merge foo &&
548         git branch --set-upstream-to my14 &&
549         git branch --unset-upstream &&
550         test_must_fail git config branch.master.remote &&
551         test_must_fail git config branch.master.merge &&
552         # fail for a branch without upstream set
553         test_must_fail git branch --unset-upstream
554 '
555
556 test_expect_success '--unset-upstream should fail on multiple branches' '
557         test_must_fail git branch --unset-upstream a b c
558 '
559
560 test_expect_success '--unset-upstream should fail on detached HEAD' '
561         git checkout HEAD^{} &&
562         test_must_fail git branch --unset-upstream &&
563         git checkout -
564 '
565
566 test_expect_success 'test --unset-upstream on a particular branch' '
567         git branch my15 &&
568         git branch --set-upstream-to master my14 &&
569         git branch --unset-upstream my14 &&
570         test_must_fail git config branch.my14.remote &&
571         test_must_fail git config branch.my14.merge
572 '
573
574 test_expect_success '--set-upstream shows message when creating a new branch that exists as remote-tracking' '
575         git update-ref refs/remotes/origin/master HEAD &&
576         git branch --set-upstream origin/master 2>actual &&
577         test_when_finished git update-ref -d refs/remotes/origin/master &&
578         test_when_finished git branch -d origin/master &&
579         cat >expected <<EOF &&
580 The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
581
582 If you wanted to make '"'master'"' track '"'origin/master'"', do this:
583
584     git branch -d origin/master
585     git branch --set-upstream-to origin/master
586 EOF
587         test_i18ncmp expected actual
588 '
589
590 test_expect_success '--set-upstream with two args only shows the deprecation message' '
591         git branch --set-upstream master my13 2>actual &&
592         test_when_finished git branch --unset-upstream master &&
593         cat >expected <<EOF &&
594 The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
595 EOF
596         test_i18ncmp expected actual
597 '
598
599 test_expect_success '--set-upstream with one arg only shows the deprecation message if the branch existed' '
600         git branch --set-upstream my13 2>actual &&
601         test_when_finished git branch --unset-upstream my13 &&
602         cat >expected <<EOF &&
603 The --set-upstream flag is deprecated and will be removed. Consider using --track or --set-upstream-to
604 EOF
605         test_i18ncmp expected actual
606 '
607
608 test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
609         git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
610         cat >expected <<-\EOF &&
611         warning: Not setting branch my13 as its own upstream.
612         EOF
613         test_expect_code 1 git config branch.my13.remote &&
614         test_expect_code 1 git config branch.my13.merge &&
615         test_i18ncmp expected actual
616 '
617
618 # Keep this test last, as it changes the current branch
619 cat >expect <<EOF
620 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
621 EOF
622 test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
623         GIT_COMMITTER_DATE="2005-05-26 23:30" \
624         git checkout -b g/h/i -l master &&
625         test_path_is_file .git/refs/heads/g/h/i &&
626         test_path_is_file .git/logs/refs/heads/g/h/i &&
627         test_cmp expect .git/logs/refs/heads/g/h/i
628 '
629
630 test_expect_success 'checkout -b makes reflog by default' '
631         git checkout master &&
632         git config --unset core.logAllRefUpdates &&
633         git checkout -b alpha &&
634         git rev-parse --verify alpha@{0}
635 '
636
637 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
638         git checkout master &&
639         git config core.logAllRefUpdates false &&
640         git checkout -b beta &&
641         test_must_fail git rev-parse --verify beta@{0}
642 '
643
644 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
645         git checkout master &&
646         git checkout -lb gamma &&
647         git config --unset core.logAllRefUpdates &&
648         git rev-parse --verify gamma@{0}
649 '
650
651 test_expect_success 'avoid ambiguous track' '
652         git config branch.autosetupmerge true &&
653         git config remote.ambi1.url lalala &&
654         git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
655         git config remote.ambi2.url lilili &&
656         git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
657         test_must_fail git branch all1 master &&
658         test -z "$(git config branch.all1.merge)"
659 '
660
661 test_expect_success 'autosetuprebase local on a tracked local branch' '
662         git config remote.local.url . &&
663         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
664         git config branch.autosetuprebase local &&
665         (git show-ref -q refs/remotes/local/o || git fetch local) &&
666         git branch mybase &&
667         git branch --track myr1 mybase &&
668         test "$(git config branch.myr1.remote)" = . &&
669         test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
670         test "$(git config branch.myr1.rebase)" = true
671 '
672
673 test_expect_success 'autosetuprebase always on a tracked local branch' '
674         git config remote.local.url . &&
675         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
676         git config branch.autosetuprebase always &&
677         (git show-ref -q refs/remotes/local/o || git fetch local) &&
678         git branch mybase2 &&
679         git branch --track myr2 mybase &&
680         test "$(git config branch.myr2.remote)" = . &&
681         test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
682         test "$(git config branch.myr2.rebase)" = true
683 '
684
685 test_expect_success 'autosetuprebase remote on a tracked local branch' '
686         git config remote.local.url . &&
687         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
688         git config branch.autosetuprebase remote &&
689         (git show-ref -q refs/remotes/local/o || git fetch local) &&
690         git branch mybase3 &&
691         git branch --track myr3 mybase2 &&
692         test "$(git config branch.myr3.remote)" = . &&
693         test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
694         ! test "$(git config branch.myr3.rebase)" = true
695 '
696
697 test_expect_success 'autosetuprebase never on a tracked local branch' '
698         git config remote.local.url . &&
699         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
700         git config branch.autosetuprebase never &&
701         (git show-ref -q refs/remotes/local/o || git fetch local) &&
702         git branch mybase4 &&
703         git branch --track myr4 mybase2 &&
704         test "$(git config branch.myr4.remote)" = . &&
705         test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
706         ! test "$(git config branch.myr4.rebase)" = true
707 '
708
709 test_expect_success 'autosetuprebase local on a tracked remote branch' '
710         git config remote.local.url . &&
711         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
712         git config branch.autosetuprebase local &&
713         (git show-ref -q refs/remotes/local/master || git fetch local) &&
714         git branch --track myr5 local/master &&
715         test "$(git config branch.myr5.remote)" = local &&
716         test "$(git config branch.myr5.merge)" = refs/heads/master &&
717         ! test "$(git config branch.myr5.rebase)" = true
718 '
719
720 test_expect_success 'autosetuprebase never on a tracked remote branch' '
721         git config remote.local.url . &&
722         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
723         git config branch.autosetuprebase never &&
724         (git show-ref -q refs/remotes/local/master || git fetch local) &&
725         git branch --track myr6 local/master &&
726         test "$(git config branch.myr6.remote)" = local &&
727         test "$(git config branch.myr6.merge)" = refs/heads/master &&
728         ! test "$(git config branch.myr6.rebase)" = true
729 '
730
731 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
732         git config remote.local.url . &&
733         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
734         git config branch.autosetuprebase remote &&
735         (git show-ref -q refs/remotes/local/master || git fetch local) &&
736         git branch --track myr7 local/master &&
737         test "$(git config branch.myr7.remote)" = local &&
738         test "$(git config branch.myr7.merge)" = refs/heads/master &&
739         test "$(git config branch.myr7.rebase)" = true
740 '
741
742 test_expect_success 'autosetuprebase always on a tracked remote branch' '
743         git config remote.local.url . &&
744         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
745         git config branch.autosetuprebase remote &&
746         (git show-ref -q refs/remotes/local/master || git fetch local) &&
747         git branch --track myr8 local/master &&
748         test "$(git config branch.myr8.remote)" = local &&
749         test "$(git config branch.myr8.merge)" = refs/heads/master &&
750         test "$(git config branch.myr8.rebase)" = true
751 '
752
753 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
754         git config --unset branch.autosetuprebase &&
755         git config remote.local.url . &&
756         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
757         (git show-ref -q refs/remotes/local/master || git fetch local) &&
758         git branch --track myr9 local/master &&
759         test "$(git config branch.myr9.remote)" = local &&
760         test "$(git config branch.myr9.merge)" = refs/heads/master &&
761         test "z$(git config branch.myr9.rebase)" = z
762 '
763
764 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
765         git config remote.local.url . &&
766         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
767         (git show-ref -q refs/remotes/local/o || git fetch local) &&
768         git branch mybase10 &&
769         git branch --track myr10 mybase2 &&
770         test "$(git config branch.myr10.remote)" = . &&
771         test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
772         test "z$(git config branch.myr10.rebase)" = z
773 '
774
775 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
776         git config remote.local.url . &&
777         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
778         (git show-ref -q refs/remotes/local/master || git fetch local) &&
779         git branch --no-track myr11 mybase2 &&
780         test "z$(git config branch.myr11.remote)" = z &&
781         test "z$(git config branch.myr11.merge)" = z &&
782         test "z$(git config branch.myr11.rebase)" = z
783 '
784
785 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
786         git config remote.local.url . &&
787         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
788         (git show-ref -q refs/remotes/local/master || git fetch local) &&
789         git branch --no-track myr12 local/master &&
790         test "z$(git config branch.myr12.remote)" = z &&
791         test "z$(git config branch.myr12.merge)" = z &&
792         test "z$(git config branch.myr12.rebase)" = z
793 '
794
795 test_expect_success 'autosetuprebase never on an untracked local branch' '
796         git config branch.autosetuprebase never &&
797         git config remote.local.url . &&
798         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
799         (git show-ref -q refs/remotes/local/master || git fetch local) &&
800         git branch --no-track myr13 mybase2 &&
801         test "z$(git config branch.myr13.remote)" = z &&
802         test "z$(git config branch.myr13.merge)" = z &&
803         test "z$(git config branch.myr13.rebase)" = z
804 '
805
806 test_expect_success 'autosetuprebase local on an untracked local branch' '
807         git config branch.autosetuprebase local &&
808         git config remote.local.url . &&
809         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
810         (git show-ref -q refs/remotes/local/master || git fetch local) &&
811         git branch --no-track myr14 mybase2 &&
812         test "z$(git config branch.myr14.remote)" = z &&
813         test "z$(git config branch.myr14.merge)" = z &&
814         test "z$(git config branch.myr14.rebase)" = z
815 '
816
817 test_expect_success 'autosetuprebase remote on an untracked local branch' '
818         git config branch.autosetuprebase remote &&
819         git config remote.local.url . &&
820         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
821         (git show-ref -q refs/remotes/local/master || git fetch local) &&
822         git branch --no-track myr15 mybase2 &&
823         test "z$(git config branch.myr15.remote)" = z &&
824         test "z$(git config branch.myr15.merge)" = z &&
825         test "z$(git config branch.myr15.rebase)" = z
826 '
827
828 test_expect_success 'autosetuprebase always on an untracked local branch' '
829         git config branch.autosetuprebase always &&
830         git config remote.local.url . &&
831         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
832         (git show-ref -q refs/remotes/local/master || git fetch local) &&
833         git branch --no-track myr16 mybase2 &&
834         test "z$(git config branch.myr16.remote)" = z &&
835         test "z$(git config branch.myr16.merge)" = z &&
836         test "z$(git config branch.myr16.rebase)" = z
837 '
838
839 test_expect_success 'autosetuprebase never on an untracked remote branch' '
840         git config branch.autosetuprebase never &&
841         git config remote.local.url . &&
842         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
843         (git show-ref -q refs/remotes/local/master || git fetch local) &&
844         git branch --no-track myr17 local/master &&
845         test "z$(git config branch.myr17.remote)" = z &&
846         test "z$(git config branch.myr17.merge)" = z &&
847         test "z$(git config branch.myr17.rebase)" = z
848 '
849
850 test_expect_success 'autosetuprebase local on an untracked remote branch' '
851         git config branch.autosetuprebase local &&
852         git config remote.local.url . &&
853         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
854         (git show-ref -q refs/remotes/local/master || git fetch local) &&
855         git branch --no-track myr18 local/master &&
856         test "z$(git config branch.myr18.remote)" = z &&
857         test "z$(git config branch.myr18.merge)" = z &&
858         test "z$(git config branch.myr18.rebase)" = z
859 '
860
861 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
862         git config branch.autosetuprebase remote &&
863         git config remote.local.url . &&
864         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
865         (git show-ref -q refs/remotes/local/master || git fetch local) &&
866         git branch --no-track myr19 local/master &&
867         test "z$(git config branch.myr19.remote)" = z &&
868         test "z$(git config branch.myr19.merge)" = z &&
869         test "z$(git config branch.myr19.rebase)" = z
870 '
871
872 test_expect_success 'autosetuprebase always on an untracked remote branch' '
873         git config branch.autosetuprebase always &&
874         git config remote.local.url . &&
875         git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
876         (git show-ref -q refs/remotes/local/master || git fetch local) &&
877         git branch --no-track myr20 local/master &&
878         test "z$(git config branch.myr20.remote)" = z &&
879         test "z$(git config branch.myr20.merge)" = z &&
880         test "z$(git config branch.myr20.rebase)" = z
881 '
882
883 test_expect_success 'autosetuprebase always on detached HEAD' '
884         git config branch.autosetupmerge always &&
885         test_when_finished git checkout master &&
886         git checkout HEAD^0 &&
887         git branch my11 &&
888         test -z "$(git config branch.my11.remote)" &&
889         test -z "$(git config branch.my11.merge)"
890 '
891
892 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
893         git config branch.autosetuprebase garbage &&
894         test_must_fail git branch
895 '
896
897 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
898         git config --unset branch.autosetuprebase &&
899         echo "[branch] autosetuprebase" >>.git/config &&
900         test_must_fail git branch &&
901         git config --unset branch.autosetuprebase
902 '
903
904 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
905         git checkout my9 &&
906         git config --unset branch.my8.merge &&
907         test_must_fail git branch -d my8
908 '
909
910 test_expect_success 'attempt to delete a branch merged to its base' '
911         # we are on my9 which is the initial commit; traditionally
912         # we would not have allowed deleting my8 that is not merged
913         # to my9, but it is set to track master that already has my8
914         git config branch.my8.merge refs/heads/master &&
915         git branch -d my8
916 '
917
918 test_expect_success 'attempt to delete a branch merged to its base' '
919         git checkout master &&
920         echo Third >>A &&
921         git commit -m "Third commit" A &&
922         git branch -t my10 my9 &&
923         git branch -f my10 HEAD^ &&
924         # we are on master which is at the third commit, and my10
925         # is behind us, so traditionally we would have allowed deleting
926         # it; but my10 is set to track my9 that is further behind.
927         test_must_fail git branch -d my10
928 '
929
930 test_expect_success 'use set-upstream on the current branch' '
931         git checkout master &&
932         git --bare init myupstream.git &&
933         git push myupstream.git master:refs/heads/frotz &&
934         git remote add origin myupstream.git &&
935         git fetch &&
936         git branch --set-upstream master origin/frotz &&
937
938         test "z$(git config branch.master.remote)" = "zorigin" &&
939         test "z$(git config branch.master.merge)" = "zrefs/heads/frotz"
940
941 '
942
943 test_expect_success 'use --edit-description' '
944         write_script editor <<-\EOF &&
945                 echo "New contents" >"$1"
946         EOF
947         EDITOR=./editor git branch --edit-description &&
948                 write_script editor <<-\EOF &&
949                 git stripspace -s <"$1" >"EDITOR_OUTPUT"
950         EOF
951         EDITOR=./editor git branch --edit-description &&
952         echo "New contents" >expect &&
953         test_cmp EDITOR_OUTPUT expect
954 '
955
956 test_expect_success 'detect typo in branch name when using --edit-description' '
957         write_script editor <<-\EOF &&
958                 echo "New contents" >"$1"
959         EOF
960         test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
961 '
962
963 test_expect_success 'refuse --edit-description on unborn branch for now' '
964         write_script editor <<-\EOF &&
965                 echo "New contents" >"$1"
966         EOF
967         git checkout --orphan unborn &&
968         test_must_fail env EDITOR=./editor git branch --edit-description
969 '
970
971 test_expect_success '--merged catches invalid object names' '
972         test_must_fail git branch --merged 0000000000000000000000000000000000000000
973 '
974
975 test_expect_success 'tracking with unexpected .fetch refspec' '
976         rm -rf a b c d &&
977         git init a &&
978         (
979                 cd a &&
980                 test_commit a
981         ) &&
982         git init b &&
983         (
984                 cd b &&
985                 test_commit b
986         ) &&
987         git init c &&
988         (
989                 cd c &&
990                 test_commit c &&
991                 git remote add a ../a &&
992                 git remote add b ../b &&
993                 git fetch --all
994         ) &&
995         git init d &&
996         (
997                 cd d &&
998                 git remote add c ../c &&
999                 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1000                 git fetch c &&
1001                 git branch --track local/a/master remotes/a/master &&
1002                 test "$(git config branch.local/a/master.remote)" = "c" &&
1003                 test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
1004                 git rev-parse --verify a >expect &&
1005                 git rev-parse --verify local/a/master >actual &&
1006                 test_cmp expect actual
1007         )
1008 '
1009
1010 test_done