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