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