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