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