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