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