worktree: update is_bare heuristics
[git/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 8. ./test-lib.sh
1f537be3 9. "$TEST_DIRECTORY"/lib-rebase.sh
a3b427b9 10
0d158ebb
RR
11test_expect_success 'prepare a trivial repository' '
12 echo Hello >A &&
13 git update-index --add A &&
14 git commit -m "Initial commit." &&
15 echo World >>A &&
16 git update-index --add A &&
17 git commit -m "Second commit." &&
140cd845
FC
18 HEAD=$(git rev-parse --verify HEAD)
19'
0d158ebb
RR
20
21test_expect_success 'git branch --help should not have created a bogus branch' '
01e8d327 22 test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
0d158ebb 23 test_path_is_missing .git/refs/heads/--help
41ac414e 24'
a3b427b9 25
1dacfbcf
NTND
26test_expect_success 'branch -h in broken repository' '
27 mkdir broken &&
28 (
29 cd broken &&
30 git init &&
31 >.git/refs/heads/master &&
32 test_expect_code 129 git branch -h >usage 2>&1
33 ) &&
9a001381 34 test_i18ngrep "[Uu]sage" broken/usage
1dacfbcf
NTND
35'
36
0d158ebb
RR
37test_expect_success 'git branch abc should create a branch' '
38 git branch abc && test_path_is_file .git/refs/heads/abc
39'
08db81a9 40
0d158ebb
RR
41test_expect_success 'git branch a/b/c should create a branch' '
42 git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
43'
08db81a9 44
0cb24fe8
JH
45test_expect_success 'git branch HEAD should fail' '
46 test_must_fail git branch HEAD
47'
8efb8899 48
d7fb7a37 49cat >expect <<EOF
8125a58b 50$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
d7fb7a37 51EOF
7687f19e 52test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
0d158ebb 53 GIT_COMMITTER_DATE="2005-05-26 23:30" \
7687f19e 54 git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
0d158ebb
RR
55 test_path_is_file .git/refs/heads/d/e/f &&
56 test_path_is_file .git/logs/refs/heads/d/e/f &&
57 test_cmp expect .git/logs/refs/heads/d/e/f
58'
59
60test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
61 git branch -d d/e/f &&
62 test_path_is_missing .git/refs/heads/d/e/f &&
d0ab0584 63 test_must_fail git reflog exists refs/heads/d/e/f
0d158ebb
RR
64'
65
66test_expect_success 'git branch j/k should work after branch j has been deleted' '
67 git branch j &&
68 git branch -d j &&
69 git branch j/k
70'
71
72test_expect_success 'git branch l should work after branch l/m has been deleted' '
73 git branch l/m &&
74 git branch -d l/m &&
75 git branch l
76'
77
78test_expect_success 'git branch -m dumps usage' '
79 test_expect_code 128 git branch -m 2>err &&
8054b9a6 80 test_i18ngrep "branch name required" err
0d158ebb
RR
81'
82
12fd3496
DT
83test_expect_success 'git branch -m m broken_symref should work' '
84 test_when_finished "git branch -D broken_symref" &&
7687f19e 85 git branch --create-reflog m &&
12fd3496
DT
86 git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
87 git branch -m m broken_symref &&
88 git reflog exists refs/heads/broken_symref &&
89 test_must_fail git reflog exists refs/heads/i_am_broken
90'
91
0d158ebb 92test_expect_success 'git branch -m m m/m should work' '
7687f19e 93 git branch --create-reflog m &&
0d158ebb 94 git branch -m m m/m &&
d0ab0584 95 git reflog exists refs/heads/m/m
0d158ebb
RR
96'
97
98test_expect_success 'git branch -m n/n n should work' '
7687f19e 99 git branch --create-reflog n/n &&
2f139044 100 git branch -m n/n n &&
d0ab0584 101 git reflog exists refs/heads/n
0d158ebb 102'
c976d415 103
bb8efa17
SD
104# The topmost entry in reflog for branch bbb is about branch creation.
105# Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
106
107test_expect_success 'git branch -m bbb should rename checked out branch' '
108 test_when_finished git branch -D bbb &&
109 test_when_finished git checkout master &&
110 git checkout -b aaa &&
111 git commit --allow-empty -m "a new commit" &&
112 git rev-parse aaa@{0} >expect &&
113 git branch -m bbb &&
114 git rev-parse bbb@{1} >actual &&
115 test_cmp expect actual &&
116 git symbolic-ref HEAD >actual &&
117 echo refs/heads/bbb >expect &&
118 test_cmp expect actual
119'
120
a1c1d817
JK
121test_expect_success 'renaming checked out branch works with d/f conflict' '
122 test_when_finished "git branch -D foo/bar || git branch -D foo" &&
123 test_when_finished git checkout master &&
124 git checkout -b foo &&
125 git branch -m foo/bar &&
126 git symbolic-ref HEAD >actual &&
127 echo refs/heads/foo/bar >expect &&
128 test_cmp expect actual
129'
130
41ac414e
JH
131test_expect_success 'git branch -m o/o o should fail when o/p exists' '
132 git branch o/o &&
0d158ebb 133 git branch o/p &&
d492b31c 134 test_must_fail git branch -m o/o o
41ac414e 135'
c976d415 136
ff7aa81f
MG
137test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
138 git branch o/q &&
139 test_must_fail git branch -m o/q o/p
140'
141
142test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
143 git branch -M o/q o/p
144'
145
356e91f2
MG
146test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
147 git branch o/q &&
148 git branch -m -f o/q o/p
149'
150
41ac414e
JH
151test_expect_success 'git branch -m q r/q should fail when r exists' '
152 git branch q &&
153 git branch r &&
d492b31c 154 test_must_fail git branch -m q r/q
41ac414e 155'
c976d415 156
55c4a673
CI
157test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
158 git branch bar &&
159 git checkout -b foo &&
160 test_must_fail git branch -M bar foo
161'
162
163test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
164 git checkout -b baz &&
165 git branch bam &&
70999e9c
KY
166 git branch -M baz bam &&
167 test $(git rev-parse --abbrev-ref HEAD) = bam
168'
169
39ee4c6c 170test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
893dbf5b 171 msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
39ee4c6c
KM
172 grep " 0\{40\}.*$msg$" .git/logs/HEAD &&
173 grep "^0\{40\}.*$msg$" .git/logs/HEAD
893dbf5b
KM
174'
175
31824d18
NTND
176test_expect_success 'git branch -M should leave orphaned HEAD alone' '
177 git init orphan &&
178 (
179 cd orphan &&
180 test_commit initial &&
181 git checkout --orphan lonely &&
182 grep lonely .git/HEAD &&
183 test_path_is_missing .git/refs/head/lonely &&
184 git branch -M master mistress &&
185 grep lonely .git/HEAD
186 )
187'
188
2272d3e5
JK
189test_expect_success 'resulting reflog can be shown by log -g' '
190 oid=$(git rev-parse HEAD) &&
191 cat >expect <<-EOF &&
192 HEAD@{0} $oid $msg
2272d3e5
JK
193 HEAD@{2} $oid checkout: moving from foo to baz
194 EOF
d08565bf 195 git log -g --format="%gd %H %gs" -2 HEAD >actual &&
2272d3e5
JK
196 test_cmp expect actual
197'
198
70999e9c
KY
199test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
200 git checkout master &&
201 git worktree add -b baz bazdir &&
202 git worktree add -f bazdir2 baz &&
203 git branch -M baz bam &&
204 test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
205 test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam
206'
207
208test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
209 git checkout -b baz &&
210 git worktree add -f bazdir3 baz &&
211 (
212 cd bazdir3 &&
213 git branch -M baz bam &&
214 test $(git rev-parse --abbrev-ref HEAD) = bam
215 ) &&
216 test $(git rev-parse --abbrev-ref HEAD) = bam
55c4a673
CI
217'
218
3f59481e
JN
219test_expect_success 'git branch -M master should work when master is checked out' '
220 git checkout master &&
221 git branch -M master
222'
223
224test_expect_success 'git branch -M master master should work when master is checked out' '
225 git checkout master &&
226 git branch -M master master
227'
228
229test_expect_success 'git branch -M master2 master2 should work when master is checked out' '
230 git checkout master &&
231 git branch master2 &&
232 git branch -M master2 master2
233'
234
cddd127b
MG
235test_expect_success 'git branch -v -d t should work' '
236 git branch t &&
cbc5cf7c 237 git rev-parse --verify refs/heads/t &&
cddd127b 238 git branch -v -d t &&
cbc5cf7c 239 test_must_fail git rev-parse --verify refs/heads/t
cddd127b
MG
240'
241
242test_expect_success 'git branch -v -m t s should work' '
243 git branch t &&
cbc5cf7c 244 git rev-parse --verify refs/heads/t &&
cddd127b 245 git branch -v -m t s &&
cbc5cf7c
DT
246 test_must_fail git rev-parse --verify refs/heads/t &&
247 git rev-parse --verify refs/heads/s &&
cddd127b
MG
248 git branch -d s
249'
250
251test_expect_success 'git branch -m -d t s should fail' '
252 git branch t &&
cbc5cf7c 253 git rev-parse refs/heads/t &&
cddd127b
MG
254 test_must_fail git branch -m -d t s &&
255 git branch -d t &&
cbc5cf7c 256 test_must_fail git rev-parse refs/heads/t
cddd127b
MG
257'
258
259test_expect_success 'git branch --list -d t should fail' '
260 git branch t &&
cbc5cf7c 261 git rev-parse refs/heads/t &&
cddd127b
MG
262 test_must_fail git branch --list -d t &&
263 git branch -d t &&
cbc5cf7c 264 test_must_fail git rev-parse refs/heads/t
cddd127b
MG
265'
266
f3534c98
JT
267test_expect_success 'deleting checked-out branch from repo that is a submodule' '
268 test_when_finished "rm -rf repo1 repo2" &&
269
270 git init repo1 &&
271 git init repo1/sub &&
272 test_commit -C repo1/sub x &&
273 git -C repo1 submodule add ./sub &&
274 git -C repo1 commit -m "adding sub" &&
275
276 git clone --recurse-submodules repo1 repo2 &&
277 git -C repo2/sub checkout -b work &&
278 test_must_fail git -C repo2/sub branch -D work
279'
280
281test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
282 test_when_finished "rm -rf nonbare base secondary" &&
283
284 git init nonbare &&
285 test_commit -C nonbare x &&
286 git clone --bare nonbare bare &&
287 git -C bare worktree add --detach ../secondary master &&
288 git -C secondary branch -D master
289'
290
ac5bbc02
JH
291test_expect_success 'git branch --list -v with --abbrev' '
292 test_when_finished "git branch -D t" &&
293 git branch t &&
294 git branch -v --list t >actual.default &&
295 git branch -v --list --abbrev t >actual.abbrev &&
296 test_cmp actual.default actual.abbrev &&
297
298 git branch -v --list --no-abbrev t >actual.noabbrev &&
299 git branch -v --list --abbrev=0 t >actual.0abbrev &&
300 test_cmp actual.noabbrev actual.0abbrev &&
301
302 git branch -v --list --abbrev=36 t >actual.36abbrev &&
303 # how many hexdigits are used?
304 read name objdefault rest <actual.abbrev &&
305 read name obj36 rest <actual.36abbrev &&
306 objfull=$(git rev-parse --verify t) &&
307
308 # are we really getting abbreviations?
309 test "$obj36" != "$objdefault" &&
310 expr "$obj36" : "$objdefault" >/dev/null &&
311 test "$objfull" != "$obj36" &&
312 expr "$objfull" : "$obj36" >/dev/null
313
314'
315
ebe31ef2
NTND
316test_expect_success 'git branch --column' '
317 COLUMNS=81 git branch --column=column >actual &&
318 cat >expected <<\EOF &&
319 a/b/c bam foo l * master n o/p r
320 abc bar j/k m/m master2 o/o q
321EOF
322 test_cmp expected actual
323'
324
325test_expect_success 'git branch --column with an extremely long branch name' '
326 long=this/is/a/part/of/long/branch/name &&
327 long=z$long/$long/$long/$long &&
328 test_when_finished "git branch -d $long" &&
329 git branch $long &&
330 COLUMNS=80 git branch --column=column >actual &&
331 cat >expected <<EOF &&
332 a/b/c
333 abc
334 bam
335 bar
336 foo
337 j/k
338 l
339 m/m
340* master
341 master2
342 n
343 o/o
344 o/p
345 q
346 r
347 $long
348EOF
349 test_cmp expected actual
350'
351
352test_expect_success 'git branch with column.*' '
353 git config column.ui column &&
354 git config column.branch "dense" &&
355 COLUMNS=80 git branch >actual &&
356 git config --unset column.branch &&
357 git config --unset column.ui &&
358 cat >expected <<\EOF &&
359 a/b/c bam foo l * master n o/p r
360 abc bar j/k m/m master2 o/o q
361EOF
362 test_cmp expected actual
363'
364
365test_expect_success 'git branch --column -v should fail' '
366 test_must_fail git branch --column -v
367'
368
369test_expect_success 'git branch -v with column.ui ignored' '
370 git config column.ui column &&
371 COLUMNS=80 git branch -v | cut -c -10 | sed "s/ *$//" >actual &&
372 git config --unset column.ui &&
373 cat >expected <<\EOF &&
374 a/b/c
375 abc
376 bam
377 bar
378 foo
379 j/k
380 l
381 m/m
382* master
383 master2
384 n
385 o/o
386 o/p
387 q
388 r
389EOF
390 test_cmp expected actual
391'
392
01ebb9dc
GB
393mv .git/config .git/config-saved
394
08b984fb 395test_expect_success 'git branch -m q q2 without config should succeed' '
5be60078
JH
396 git branch -m q q2 &&
397 git branch -m q2 q
01ebb9dc
GB
398'
399
400mv .git/config-saved .git/config
401
5be60078 402git config branch.s/s.dummy Hello
dc81c58c 403
0d158ebb 404test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
7687f19e 405 git branch --create-reflog s/s &&
d0ab0584 406 git reflog exists refs/heads/s/s &&
7687f19e 407 git branch --create-reflog s/t &&
d0ab0584 408 git reflog exists refs/heads/s/t &&
0d158ebb
RR
409 git branch -d s/t &&
410 git branch -m s/s s &&
d0ab0584 411 git reflog exists refs/heads/s
0d158ebb 412'
c976d415 413
0d158ebb
RR
414test_expect_success 'config information was renamed, too' '
415 test $(git config branch.s.dummy) = Hello &&
b8f354f2 416 test_must_fail git config branch.s/s.dummy
0d158ebb 417'
dc81c58c 418
c8b2cec0
ÆAB
419test_expect_success 'git branch -m correctly renames multiple config sections' '
420 test_when_finished "git checkout master" &&
421 git checkout -b source master &&
422
423 # Assert that a config file with multiple config sections has
424 # those sections preserved...
425 cat >expect <<-\EOF &&
426 branch.dest.key1=value1
427 some.gar.b=age
428 branch.dest.key2=value2
429 EOF
430 cat >config.branch <<\EOF &&
431;; Note the lack of -\EOF above & mixed indenting here. This is
432;; intentional, we are also testing that the formatting of copied
433;; sections is preserved.
434
435;; Comment for source. Tabs
436[branch "source"]
437 ;; Comment for the source value
438 key1 = value1
439;; Comment for some.gar. Spaces
440[some "gar"]
441 ;; Comment for the some.gar value
442 b = age
443;; Comment for source, again. Mixed tabs/spaces.
444[branch "source"]
445 ;; Comment for the source value, again
446 key2 = value2
447EOF
448 cat config.branch >>.git/config &&
449 git branch -m source dest &&
450 git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
451 test_cmp expect actual &&
452
453 # ...and that the comments for those sections are also
454 # preserved.
455 cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
456 sed -n -e "/Note the lack/,\$p" .git/config >actual &&
457 test_cmp expect actual
458'
459
52d59cc6
SD
460test_expect_success 'git branch -c dumps usage' '
461 test_expect_code 128 git branch -c 2>err &&
462 test_i18ngrep "branch name required" err
463'
464
465test_expect_success 'git branch --copy dumps usage' '
466 test_expect_code 128 git branch --copy 2>err &&
467 test_i18ngrep "branch name required" err
468'
469
470test_expect_success 'git branch -c d e should work' '
7687f19e 471 git branch --create-reflog d &&
52d59cc6
SD
472 git reflog exists refs/heads/d &&
473 git config branch.d.dummy Hello &&
474 git branch -c d e &&
475 git reflog exists refs/heads/d &&
476 git reflog exists refs/heads/e &&
477 echo Hello >expect &&
478 git config branch.e.dummy >actual &&
479 test_cmp expect actual &&
480 echo Hello >expect &&
481 git config branch.d.dummy >actual &&
482 test_cmp expect actual
483'
484
485test_expect_success 'git branch --copy is a synonym for -c' '
7687f19e 486 git branch --create-reflog copy &&
52d59cc6
SD
487 git reflog exists refs/heads/copy &&
488 git config branch.copy.dummy Hello &&
489 git branch --copy copy copy-to &&
490 git reflog exists refs/heads/copy &&
491 git reflog exists refs/heads/copy-to &&
492 echo Hello >expect &&
493 git config branch.copy.dummy >actual &&
494 test_cmp expect actual &&
495 echo Hello >expect &&
496 git config branch.copy-to.dummy >actual &&
497 test_cmp expect actual
498'
499
e5435ff1 500test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
52d59cc6
SD
501 git checkout -b ee &&
502 git reflog exists refs/heads/ee &&
503 git config branch.ee.dummy Hello &&
504 git branch -c ee ef &&
505 git reflog exists refs/heads/ee &&
506 git reflog exists refs/heads/ef &&
507 test $(git config branch.ee.dummy) = Hello &&
508 test $(git config branch.ef.dummy) = Hello &&
e5435ff1 509 test $(git rev-parse --abbrev-ref HEAD) = ee
52d59cc6
SD
510'
511
512test_expect_success 'git branch -c f/f g/g should work' '
7687f19e 513 git branch --create-reflog f/f &&
52d59cc6
SD
514 git reflog exists refs/heads/f/f &&
515 git config branch.f/f.dummy Hello &&
516 git branch -c f/f g/g &&
517 git reflog exists refs/heads/f/f &&
518 git reflog exists refs/heads/g/g &&
519 test $(git config branch.f/f.dummy) = Hello &&
520 test $(git config branch.g/g.dummy) = Hello
521'
522
523test_expect_success 'git branch -c m2 m2 should work' '
7687f19e 524 git branch --create-reflog m2 &&
52d59cc6
SD
525 git reflog exists refs/heads/m2 &&
526 git config branch.m2.dummy Hello &&
527 git branch -c m2 m2 &&
528 git reflog exists refs/heads/m2 &&
529 test $(git config branch.m2.dummy) = Hello
530'
531
532test_expect_success 'git branch -c zz zz/zz should fail' '
7687f19e 533 git branch --create-reflog zz &&
52d59cc6
SD
534 git reflog exists refs/heads/zz &&
535 test_must_fail git branch -c zz zz/zz
536'
537
538test_expect_success 'git branch -c b/b b should fail' '
7687f19e 539 git branch --create-reflog b/b &&
52d59cc6
SD
540 test_must_fail git branch -c b/b b
541'
542
543test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
7687f19e 544 git branch --create-reflog o/q &&
52d59cc6
SD
545 git reflog exists refs/heads/o/q &&
546 git reflog exists refs/heads/o/p &&
547 git branch -C o/q o/p
548'
549
550test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
551 git reflog exists refs/heads/o/q &&
552 git reflog exists refs/heads/o/p &&
553 git branch -c -f o/q o/p
554'
555
40c17eb1 556test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
52d59cc6
SD
557 git branch qq &&
558 git branch rr &&
559 test_must_fail git branch -c qq rr/qq
560'
561
562test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
563 git branch b1 &&
564 git checkout -b b2 &&
565 test_must_fail git branch -C b1 b2
566'
567
568test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
569 git checkout -b c1 &&
570 git branch c2 &&
571 git branch -C c1 c2 &&
e5435ff1 572 test $(git rev-parse --abbrev-ref HEAD) = c1
52d59cc6
SD
573'
574
e5435ff1 575test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
52d59cc6 576 msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
e5435ff1 577 ! grep "$msg$" .git/logs/HEAD
52d59cc6
SD
578'
579
580test_expect_success 'git branch -C master should work when master is checked out' '
581 git checkout master &&
582 git branch -C master
583'
584
585test_expect_success 'git branch -C master master should work when master is checked out' '
586 git checkout master &&
587 git branch -C master master
588'
589
590test_expect_success 'git branch -C master5 master5 should work when master is checked out' '
591 git checkout master &&
592 git branch master5 &&
593 git branch -C master5 master5
594'
595
596test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
7687f19e 597 git branch --create-reflog cd &&
52d59cc6
SD
598 git reflog exists refs/heads/cd &&
599 git config branch.cd.dummy CD &&
7687f19e 600 git branch --create-reflog ab &&
52d59cc6
SD
601 git reflog exists refs/heads/ab &&
602 git config branch.ab.dummy AB &&
603 git branch -C ab cd &&
604 git reflog exists refs/heads/ab &&
605 git reflog exists refs/heads/cd &&
606 test $(git config branch.ab.dummy) = AB &&
607 test $(git config branch.cd.dummy) = AB
608'
609
610test_expect_success 'git branch -c correctly copies multiple config sections' '
611 FOO=1 &&
612 export FOO &&
613 test_when_finished "git checkout master" &&
614 git checkout -b source2 master &&
615
616 # Assert that a config file with multiple config sections has
617 # those sections preserved...
618 cat >expect <<-\EOF &&
619 branch.source2.key1=value1
620 branch.dest2.key1=value1
621 more.gar.b=age
622 branch.source2.key2=value2
623 branch.dest2.key2=value2
624 EOF
625 cat >config.branch <<\EOF &&
626;; Note the lack of -\EOF above & mixed indenting here. This is
627;; intentional, we are also testing that the formatting of copied
628;; sections is preserved.
629
630;; Comment for source2. Tabs
631[branch "source2"]
632 ;; Comment for the source2 value
633 key1 = value1
634;; Comment for more.gar. Spaces
635[more "gar"]
636 ;; Comment for the more.gar value
637 b = age
638;; Comment for source2, again. Mixed tabs/spaces.
639[branch "source2"]
640 ;; Comment for the source2 value, again
641 key2 = value2
642EOF
643 cat config.branch >>.git/config &&
644 git branch -c source2 dest2 &&
645 git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
646 test_cmp expect actual &&
647
648 # ...and that the comments and formatting for those sections
649 # is also preserved.
650 cat >expect <<\EOF &&
651;; Comment for source2. Tabs
652[branch "source2"]
653 ;; Comment for the source2 value
654 key1 = value1
655;; Comment for more.gar. Spaces
656[branch "dest2"]
657 ;; Comment for the source2 value
658 key1 = value1
659;; Comment for more.gar. Spaces
660[more "gar"]
661 ;; Comment for the more.gar value
662 b = age
663;; Comment for source2, again. Mixed tabs/spaces.
664[branch "source2"]
665 ;; Comment for the source2 value, again
666 key2 = value2
667[branch "dest2"]
668 ;; Comment for the source2 value, again
669 key2 = value2
670EOF
671 sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
672 test_cmp expect actual
673'
674
566c7707
RS
675test_expect_success 'deleting a symref' '
676 git branch target &&
677 git symbolic-ref refs/heads/symref refs/heads/target &&
13baa9fe 678 echo "Deleted branch symref (was refs/heads/target)." >expect &&
566c7707
RS
679 git branch -d symref >actual &&
680 test_path_is_file .git/refs/heads/target &&
681 test_path_is_missing .git/refs/heads/symref &&
682 test_i18ncmp expect actual
683'
684
0fe700e3
RS
685test_expect_success 'deleting a dangling symref' '
686 git symbolic-ref refs/heads/dangling-symref nowhere &&
687 test_path_is_file .git/refs/heads/dangling-symref &&
13baa9fe 688 echo "Deleted branch dangling-symref (was nowhere)." >expect &&
0fe700e3
RS
689 git branch -d dangling-symref >actual &&
690 test_path_is_missing .git/refs/heads/dangling-symref &&
691 test_i18ncmp expect actual
692'
693
62a2d525
JN
694test_expect_success 'deleting a self-referential symref' '
695 git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
696 test_path_is_file .git/refs/heads/self-reference &&
697 echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
698 git branch -d self-reference >actual &&
699 test_path_is_missing .git/refs/heads/self-reference &&
700 test_i18ncmp expect actual
701'
702
0d158ebb 703test_expect_success 'renaming a symref is not allowed' '
eca35a25 704 git symbolic-ref refs/heads/master2 refs/heads/master &&
fa58186c
MV
705 test_must_fail git branch -m master2 master3 &&
706 git symbolic-ref refs/heads/master2 &&
376eb14a
JK
707 test_path_is_file .git/refs/heads/master &&
708 test_path_is_missing .git/refs/heads/master3
eca35a25
MV
709'
710
0d158ebb 711test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
7687f19e 712 git branch --create-reflog u &&
0d158ebb
RR
713 mv .git/logs/refs/heads/u real-u &&
714 ln -s real-u .git/logs/refs/heads/u &&
715 test_must_fail git branch -m u v
716'
717
718test_expect_success 'test tracking setup via --track' '
719 git config remote.local.url . &&
720 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
721 (git show-ref -q refs/remotes/local/master || git fetch local) &&
722 git branch --track my1 local/master &&
723 test $(git config branch.my1.remote) = local &&
724 test $(git config branch.my1.merge) = refs/heads/master
725'
726
727test_expect_success 'test tracking setup (non-wildcard, matching)' '
728 git config remote.local.url . &&
729 git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
730 (git show-ref -q refs/remotes/local/master || git fetch local) &&
731 git branch --track my4 local/master &&
732 test $(git config branch.my4.remote) = local &&
733 test $(git config branch.my4.merge) = refs/heads/master
734'
735
41c21f22 736test_expect_success 'tracking setup fails on non-matching refspec' '
0d158ebb 737 git config remote.local.url . &&
81f339dc 738 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0d158ebb 739 (git show-ref -q refs/remotes/local/master || git fetch local) &&
81f339dc 740 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
9c9cd39a
JH
741 test_must_fail git branch --track my5 local/master &&
742 test_must_fail git config branch.my5.remote &&
743 test_must_fail git config branch.my5.merge
0d158ebb
RR
744'
745
746test_expect_success 'test tracking setup via config' '
747 git config branch.autosetupmerge true &&
748 git config remote.local.url . &&
749 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
750 (git show-ref -q refs/remotes/local/master || git fetch local) &&
751 git branch my3 local/master &&
752 test $(git config branch.my3.remote) = local &&
753 test $(git config branch.my3.merge) = refs/heads/master
754'
755
756test_expect_success 'test overriding tracking setup via --no-track' '
757 git config branch.autosetupmerge true &&
758 git config remote.local.url . &&
759 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
760 (git show-ref -q refs/remotes/local/master || git fetch local) &&
761 git branch --no-track my2 local/master &&
762 git config branch.autosetupmerge false &&
763 ! test "$(git config branch.my2.remote)" = local &&
764 ! test "$(git config branch.my2.merge)" = refs/heads/master
765'
766
767test_expect_success 'no tracking without .fetch entries' '
768 git config branch.autosetupmerge true &&
769 git branch my6 s &&
002ba037 770 git config branch.autosetupmerge false &&
0d158ebb
RR
771 test -z "$(git config branch.my6.remote)" &&
772 test -z "$(git config branch.my6.merge)"
773'
774
775test_expect_success 'test tracking setup via --track but deeper' '
776 git config remote.local.url . &&
777 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
778 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
779 git branch --track my7 local/o/o &&
780 test "$(git config branch.my7.remote)" = local &&
781 test "$(git config branch.my7.merge)" = refs/heads/o/o
782'
783
784test_expect_success 'test deleting branch deletes branch config' '
785 git branch -d my7 &&
786 test -z "$(git config branch.my7.remote)" &&
787 test -z "$(git config branch.my7.merge)"
788'
789
790test_expect_success 'test deleting branch without config' '
791 git branch my7 s &&
792 sha1=$(git rev-parse my7 | cut -c 1-7) &&
793 echo "Deleted branch my7 (was $sha1)." >expect &&
794 git branch -d my7 >actual 2>&1 &&
795 test_i18ncmp expect actual
796'
797
f292244c
KY
798test_expect_success 'deleting currently checked out branch fails' '
799 git worktree add -b my7 my7 &&
800 test_must_fail git -C my7 branch -d my7 &&
801 test_must_fail git branch -d my7
802'
803
0d158ebb
RR
804test_expect_success 'test --track without .fetch entries' '
805 git branch --track my8 &&
806 test "$(git config branch.my8.remote)" &&
807 test "$(git config branch.my8.merge)"
808'
809
810test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
811 git config branch.autosetupmerge always &&
812 git branch my9 HEAD^ &&
813 git config branch.autosetupmerge false
814'
815
816test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
817 test_must_fail git branch --track my10 HEAD^
818'
819
820test_expect_success 'branch from tag w/--track causes failure' '
821 git tag foobar &&
822 test_must_fail git branch --track my11 foobar
823'
824
0cb24fe8
JH
825test_expect_success '--set-upstream-to fails on multiple branches' '
826 test_must_fail git branch --set-upstream-to master a b c
827'
828
829test_expect_success '--set-upstream-to fails on detached HEAD' '
830 git checkout HEAD^{} &&
831 test_must_fail git branch --set-upstream-to master &&
832 git checkout -
833'
834
8a3e5ecd
JK
835test_expect_success '--set-upstream-to fails on a missing dst branch' '
836 test_must_fail git branch --set-upstream-to master does-not-exist
837'
838
839test_expect_success '--set-upstream-to fails on a missing src branch' '
840 test_must_fail git branch --set-upstream-to does-not-exist master
841'
842
843test_expect_success '--set-upstream-to fails on a non-ref' '
844 test_must_fail git branch --set-upstream-to HEAD^{}
845'
846
27852b2c
PS
847test_expect_success '--set-upstream-to fails on locked config' '
848 test_when_finished "rm -f .git/config.lock" &&
849 >.git/config.lock &&
850 git branch locked &&
851 test_must_fail git branch --set-upstream-to locked
852'
853
0d158ebb
RR
854test_expect_success 'use --set-upstream-to modify HEAD' '
855 test_config branch.master.remote foo &&
856 test_config branch.master.merge foo &&
d0423ddd 857 git branch my12 &&
0d158ebb
RR
858 git branch --set-upstream-to my12 &&
859 test "$(git config branch.master.remote)" = "." &&
860 test "$(git config branch.master.merge)" = "refs/heads/my12"
861'
862
863test_expect_success 'use --set-upstream-to modify a particular branch' '
d0423ddd 864 git branch my13 &&
0d158ebb 865 git branch --set-upstream-to master my13 &&
93a6b3f2 866 test_when_finished "git branch --unset-upstream my13" &&
0d158ebb
RR
867 test "$(git config branch.my13.remote)" = "." &&
868 test "$(git config branch.my13.merge)" = "refs/heads/master"
869'
870
871test_expect_success '--unset-upstream should fail if given a non-existent branch' '
872 test_must_fail git branch --unset-upstream i-dont-exist
873'
874
b81842cb
PS
875test_expect_success '--unset-upstream should fail if config is locked' '
876 test_when_finished "rm -f .git/config.lock" &&
877 git branch --set-upstream-to locked &&
878 >.git/config.lock &&
879 test_must_fail git branch --unset-upstream
880'
881
0d158ebb 882test_expect_success 'test --unset-upstream on HEAD' '
d0423ddd 883 git branch my14 &&
0d158ebb
RR
884 test_config branch.master.remote foo &&
885 test_config branch.master.merge foo &&
886 git branch --set-upstream-to my14 &&
887 git branch --unset-upstream &&
888 test_must_fail git config branch.master.remote &&
889 test_must_fail git config branch.master.merge &&
890 # fail for a branch without upstream set
891 test_must_fail git branch --unset-upstream
892'
893
0cb24fe8
JH
894test_expect_success '--unset-upstream should fail on multiple branches' '
895 test_must_fail git branch --unset-upstream a b c
896'
897
898test_expect_success '--unset-upstream should fail on detached HEAD' '
899 git checkout HEAD^{} &&
900 test_must_fail git branch --unset-upstream &&
901 git checkout -
902'
903
0d158ebb 904test_expect_success 'test --unset-upstream on a particular branch' '
d0423ddd 905 git branch my15 &&
0d158ebb
RR
906 git branch --set-upstream-to master my14 &&
907 git branch --unset-upstream my14 &&
908 test_must_fail git config branch.my14.remote &&
909 test_must_fail git config branch.my14.merge
910'
911
cf317877 912test_expect_success 'disabled option --set-upstream fails' '
52668846 913 test_must_fail git branch --set-upstream origin/master
b347d06b
CMN
914'
915
95052d1f
BG
916test_expect_success '--set-upstream-to notices an error to set branch as own upstream' '
917 git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
918 cat >expected <<-\EOF &&
919 warning: Not setting branch my13 as its own upstream.
920 EOF
921 test_expect_code 1 git config branch.my13.remote &&
922 test_expect_code 1 git config branch.my13.merge &&
923 test_i18ncmp expected actual
924'
925
0746d19a
PB
926# Keep this test last, as it changes the current branch
927cat >expect <<EOF
8125a58b 928$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
0746d19a 929EOF
0d158ebb
RR
930test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
931 GIT_COMMITTER_DATE="2005-05-26 23:30" \
932 git checkout -b g/h/i -l master &&
933 test_path_is_file .git/refs/heads/g/h/i &&
934 test_path_is_file .git/logs/refs/heads/g/h/i &&
935 test_cmp expect .git/logs/refs/heads/g/h/i
936'
0746d19a 937
b2099957
EM
938test_expect_success 'checkout -b makes reflog by default' '
939 git checkout master &&
940 git config --unset core.logAllRefUpdates &&
941 git checkout -b alpha &&
6e6842e3 942 git rev-parse --verify alpha@{0}
b2099957
EM
943'
944
945test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
946 git checkout master &&
947 git config core.logAllRefUpdates false &&
948 git checkout -b beta &&
6e6842e3 949 test_must_fail git rev-parse --verify beta@{0}
b2099957
EM
950'
951
952test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
953 git checkout master &&
954 git checkout -lb gamma &&
955 git config --unset core.logAllRefUpdates &&
6e6842e3 956 git rev-parse --verify gamma@{0}
b2099957
EM
957'
958
1d0a694b
DB
959test_expect_success 'avoid ambiguous track' '
960 git config branch.autosetupmerge true &&
961 git config remote.ambi1.url lalala &&
962 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
963 git config remote.ambi2.url lilili &&
964 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
27852b2c 965 test_must_fail git branch all1 master &&
1d0a694b
DB
966 test -z "$(git config branch.all1.merge)"
967'
968
c998ae9b
DS
969test_expect_success 'autosetuprebase local on a tracked local branch' '
970 git config remote.local.url . &&
971 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
972 git config branch.autosetuprebase local &&
0cb0e143 973 (git show-ref -q refs/remotes/local/o || git fetch local) &&
c998ae9b
DS
974 git branch mybase &&
975 git branch --track myr1 mybase &&
976 test "$(git config branch.myr1.remote)" = . &&
977 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
978 test "$(git config branch.myr1.rebase)" = true
979'
980
981test_expect_success 'autosetuprebase always on a tracked local branch' '
982 git config remote.local.url . &&
983 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
984 git config branch.autosetuprebase always &&
0cb0e143 985 (git show-ref -q refs/remotes/local/o || git fetch local) &&
c998ae9b
DS
986 git branch mybase2 &&
987 git branch --track myr2 mybase &&
988 test "$(git config branch.myr2.remote)" = . &&
989 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
990 test "$(git config branch.myr2.rebase)" = true
991'
992
993test_expect_success 'autosetuprebase remote on a tracked local branch' '
994 git config remote.local.url . &&
995 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
996 git config branch.autosetuprebase remote &&
0cb0e143 997 (git show-ref -q refs/remotes/local/o || git fetch local) &&
c998ae9b
DS
998 git branch mybase3 &&
999 git branch --track myr3 mybase2 &&
1000 test "$(git config branch.myr3.remote)" = . &&
1001 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
1002 ! test "$(git config branch.myr3.rebase)" = true
1003'
1004
1005test_expect_success 'autosetuprebase never on a tracked local branch' '
1006 git config remote.local.url . &&
1007 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1008 git config branch.autosetuprebase never &&
0cb0e143 1009 (git show-ref -q refs/remotes/local/o || git fetch local) &&
c998ae9b
DS
1010 git branch mybase4 &&
1011 git branch --track myr4 mybase2 &&
1012 test "$(git config branch.myr4.remote)" = . &&
1013 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
1014 ! test "$(git config branch.myr4.rebase)" = true
1015'
1016
1017test_expect_success 'autosetuprebase local on a tracked remote branch' '
1018 git config remote.local.url . &&
1019 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1020 git config branch.autosetuprebase local &&
0cb0e143 1021 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1022 git branch --track myr5 local/master &&
1023 test "$(git config branch.myr5.remote)" = local &&
1024 test "$(git config branch.myr5.merge)" = refs/heads/master &&
1025 ! test "$(git config branch.myr5.rebase)" = true
1026'
1027
1028test_expect_success 'autosetuprebase never on a tracked remote branch' '
1029 git config remote.local.url . &&
1030 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1031 git config branch.autosetuprebase never &&
0cb0e143 1032 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1033 git branch --track myr6 local/master &&
1034 test "$(git config branch.myr6.remote)" = local &&
1035 test "$(git config branch.myr6.merge)" = refs/heads/master &&
1036 ! test "$(git config branch.myr6.rebase)" = true
1037'
1038
1039test_expect_success 'autosetuprebase remote on a tracked remote branch' '
1040 git config remote.local.url . &&
1041 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1042 git config branch.autosetuprebase remote &&
0cb0e143 1043 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1044 git branch --track myr7 local/master &&
1045 test "$(git config branch.myr7.remote)" = local &&
1046 test "$(git config branch.myr7.merge)" = refs/heads/master &&
1047 test "$(git config branch.myr7.rebase)" = true
1048'
1049
1050test_expect_success 'autosetuprebase always on a tracked remote branch' '
1051 git config remote.local.url . &&
1052 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
1053 git config branch.autosetuprebase remote &&
0cb0e143 1054 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1055 git branch --track myr8 local/master &&
1056 test "$(git config branch.myr8.remote)" = local &&
1057 test "$(git config branch.myr8.merge)" = refs/heads/master &&
1058 test "$(git config branch.myr8.rebase)" = true
1059'
1060
1061test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
1062 git config --unset branch.autosetuprebase &&
1063 git config remote.local.url . &&
1064 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1065 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1066 git branch --track myr9 local/master &&
1067 test "$(git config branch.myr9.remote)" = local &&
1068 test "$(git config branch.myr9.merge)" = refs/heads/master &&
1069 test "z$(git config branch.myr9.rebase)" = z
1070'
1071
1072test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
1073 git config remote.local.url . &&
1074 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1075 (git show-ref -q refs/remotes/local/o || git fetch local) &&
c998ae9b
DS
1076 git branch mybase10 &&
1077 git branch --track myr10 mybase2 &&
1078 test "$(git config branch.myr10.remote)" = . &&
1079 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
1080 test "z$(git config branch.myr10.rebase)" = z
1081'
1082
1083test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
1084 git config remote.local.url . &&
1085 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1086 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1087 git branch --no-track myr11 mybase2 &&
1088 test "z$(git config branch.myr11.remote)" = z &&
1089 test "z$(git config branch.myr11.merge)" = z &&
1090 test "z$(git config branch.myr11.rebase)" = z
1091'
1092
1093test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
1094 git config remote.local.url . &&
1095 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1096 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1097 git branch --no-track myr12 local/master &&
1098 test "z$(git config branch.myr12.remote)" = z &&
1099 test "z$(git config branch.myr12.merge)" = z &&
1100 test "z$(git config branch.myr12.rebase)" = z
1101'
1102
1103test_expect_success 'autosetuprebase never on an untracked local branch' '
1104 git config branch.autosetuprebase never &&
1105 git config remote.local.url . &&
1106 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1107 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1108 git branch --no-track myr13 mybase2 &&
1109 test "z$(git config branch.myr13.remote)" = z &&
1110 test "z$(git config branch.myr13.merge)" = z &&
1111 test "z$(git config branch.myr13.rebase)" = z
1112'
1113
1114test_expect_success 'autosetuprebase local on an untracked local branch' '
1115 git config branch.autosetuprebase local &&
1116 git config remote.local.url . &&
1117 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1118 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1119 git branch --no-track myr14 mybase2 &&
1120 test "z$(git config branch.myr14.remote)" = z &&
1121 test "z$(git config branch.myr14.merge)" = z &&
1122 test "z$(git config branch.myr14.rebase)" = z
1123'
1124
1125test_expect_success 'autosetuprebase remote on an untracked local branch' '
1126 git config branch.autosetuprebase remote &&
1127 git config remote.local.url . &&
1128 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1129 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1130 git branch --no-track myr15 mybase2 &&
1131 test "z$(git config branch.myr15.remote)" = z &&
1132 test "z$(git config branch.myr15.merge)" = z &&
1133 test "z$(git config branch.myr15.rebase)" = z
1134'
1135
1136test_expect_success 'autosetuprebase always on an untracked local branch' '
1137 git config branch.autosetuprebase always &&
1138 git config remote.local.url . &&
1139 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1140 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1141 git branch --no-track myr16 mybase2 &&
1142 test "z$(git config branch.myr16.remote)" = z &&
1143 test "z$(git config branch.myr16.merge)" = z &&
1144 test "z$(git config branch.myr16.rebase)" = z
1145'
1146
1147test_expect_success 'autosetuprebase never on an untracked remote branch' '
1148 git config branch.autosetuprebase never &&
1149 git config remote.local.url . &&
1150 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1151 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1152 git branch --no-track myr17 local/master &&
1153 test "z$(git config branch.myr17.remote)" = z &&
1154 test "z$(git config branch.myr17.merge)" = z &&
1155 test "z$(git config branch.myr17.rebase)" = z
1156'
1157
1158test_expect_success 'autosetuprebase local on an untracked remote branch' '
1159 git config branch.autosetuprebase local &&
1160 git config remote.local.url . &&
1161 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1162 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1163 git branch --no-track myr18 local/master &&
1164 test "z$(git config branch.myr18.remote)" = z &&
1165 test "z$(git config branch.myr18.merge)" = z &&
1166 test "z$(git config branch.myr18.rebase)" = z
1167'
1168
1169test_expect_success 'autosetuprebase remote on an untracked remote branch' '
1170 git config branch.autosetuprebase remote &&
1171 git config remote.local.url . &&
1172 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1173 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1174 git branch --no-track myr19 local/master &&
1175 test "z$(git config branch.myr19.remote)" = z &&
1176 test "z$(git config branch.myr19.merge)" = z &&
1177 test "z$(git config branch.myr19.rebase)" = z
1178'
1179
1180test_expect_success 'autosetuprebase always on an untracked remote branch' '
1181 git config branch.autosetuprebase always &&
1182 git config remote.local.url . &&
1183 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
0cb0e143 1184 (git show-ref -q refs/remotes/local/master || git fetch local) &&
c998ae9b
DS
1185 git branch --no-track myr20 local/master &&
1186 test "z$(git config branch.myr20.remote)" = z &&
1187 test "z$(git config branch.myr20.merge)" = z &&
1188 test "z$(git config branch.myr20.rebase)" = z
1189'
1190
21b5b1e8
JH
1191test_expect_success 'autosetuprebase always on detached HEAD' '
1192 git config branch.autosetupmerge always &&
1193 test_when_finished git checkout master &&
1194 git checkout HEAD^0 &&
1195 git branch my11 &&
1196 test -z "$(git config branch.my11.remote)" &&
1197 test -z "$(git config branch.my11.merge)"
1198'
1199
c998ae9b
DS
1200test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
1201 git config branch.autosetuprebase garbage &&
1202 test_must_fail git branch
1203'
1204
1205test_expect_success 'detect misconfigured autosetuprebase (no value)' '
1206 git config --unset branch.autosetuprebase &&
0d158ebb 1207 echo "[branch] autosetuprebase" >>.git/config &&
c998ae9b
DS
1208 test_must_fail git branch &&
1209 git config --unset branch.autosetuprebase
1210'
1211
99c419c9
JH
1212test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
1213 git checkout my9 &&
1214 git config --unset branch.my8.merge &&
1215 test_must_fail git branch -d my8
1216'
1217
1218test_expect_success 'attempt to delete a branch merged to its base' '
1219 # we are on my9 which is the initial commit; traditionally
1220 # we would not have allowed deleting my8 that is not merged
1221 # to my9, but it is set to track master that already has my8
1222 git config branch.my8.merge refs/heads/master &&
1223 git branch -d my8
1224'
1225
1226test_expect_success 'attempt to delete a branch merged to its base' '
1227 git checkout master &&
1228 echo Third >>A &&
1229 git commit -m "Third commit" A &&
1230 git branch -t my10 my9 &&
1231 git branch -f my10 HEAD^ &&
1232 # we are on master which is at the third commit, and my10
1233 # is behind us, so traditionally we would have allowed deleting
1234 # it; but my10 is set to track my9 that is further behind.
1235 test_must_fail git branch -d my10
1236'
1237
c2d17ba3
JH
1238test_expect_success 'use --edit-description' '
1239 write_script editor <<-\EOF &&
1240 echo "New contents" >"$1"
1241 EOF
1242 EDITOR=./editor git branch --edit-description &&
1243 write_script editor <<-\EOF &&
1244 git stripspace -s <"$1" >"EDITOR_OUTPUT"
1245 EOF
1246 EDITOR=./editor git branch --edit-description &&
1247 echo "New contents" >expect &&
dcbaa0b3 1248 test_cmp expect EDITOR_OUTPUT
c2d17ba3
JH
1249'
1250
1251test_expect_success 'detect typo in branch name when using --edit-description' '
1252 write_script editor <<-\EOF &&
1253 echo "New contents" >"$1"
1254 EOF
512477b1 1255 test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
c2d17ba3
JH
1256'
1257
1258test_expect_success 'refuse --edit-description on unborn branch for now' '
1259 write_script editor <<-\EOF &&
1260 echo "New contents" >"$1"
1261 EOF
1262 git checkout --orphan unborn &&
512477b1 1263 test_must_fail env EDITOR=./editor git branch --edit-description
c2d17ba3
JH
1264'
1265
6c41e975
CMN
1266test_expect_success '--merged catches invalid object names' '
1267 test_must_fail git branch --merged 0000000000000000000000000000000000000000
1268'
1269
17d6c744
ÆAB
1270test_expect_success '--merged is incompatible with --no-merged' '
1271 test_must_fail git branch --merged HEAD --no-merged HEAD
1272'
1273
1f537be3
ES
1274test_expect_success '--list during rebase' '
1275 test_when_finished "reset_rebase" &&
1276 git checkout master &&
1277 FAKE_LINES="1 edit 2" &&
1278 export FAKE_LINES &&
1279 set_fake_editor &&
1280 git rebase -i HEAD~2 &&
1281 git branch --list >actual &&
1282 test_i18ngrep "rebasing master" actual
1283'
1284
1285test_expect_success '--list during rebase from detached HEAD' '
1286 test_when_finished "reset_rebase && git checkout master" &&
1287 git checkout master^0 &&
1288 oid=$(git rev-parse --short HEAD) &&
1289 FAKE_LINES="1 edit 2" &&
1290 export FAKE_LINES &&
1291 set_fake_editor &&
1292 git rebase -i HEAD~2 &&
1293 git branch --list >actual &&
1294 test_i18ngrep "rebasing detached HEAD $oid" actual
1295'
1296
1d7358c5 1297test_expect_success 'tracking with unexpected .fetch refspec' '
b0f49ff1 1298 rm -rf a b c d &&
62d94a3a
JH
1299 git init a &&
1300 (
1301 cd a &&
1302 test_commit a
1303 ) &&
1304 git init b &&
1305 (
1306 cd b &&
1307 test_commit b
1308 ) &&
1309 git init c &&
1310 (
1311 cd c &&
1312 test_commit c &&
1313 git remote add a ../a &&
1314 git remote add b ../b &&
1315 git fetch --all
1316 ) &&
1317 git init d &&
1318 (
1319 cd d &&
1320 git remote add c ../c &&
1321 git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
1322 git fetch c &&
1323 git branch --track local/a/master remotes/a/master &&
1324 test "$(git config branch.local/a/master.remote)" = "c" &&
1325 test "$(git config branch.local/a/master.merge)" = "refs/remotes/a/master" &&
1326 git rev-parse --verify a >expect &&
1327 git rev-parse --verify local/a/master >actual &&
1328 test_cmp expect actual
1329 )
1330'
1331
560ae1c1
SM
1332test_expect_success 'configured committerdate sort' '
1333 git init sort &&
1334 (
1335 cd sort &&
1336 git config branch.sort committerdate &&
1337 test_commit initial &&
1338 git checkout -b a &&
1339 test_commit a &&
1340 git checkout -b c &&
1341 test_commit c &&
1342 git checkout -b b &&
1343 test_commit b &&
1344 git branch >actual &&
1345 cat >expect <<-\EOF &&
1346 master
1347 a
1348 c
1349 * b
1350 EOF
1351 test_cmp expect actual
1352 )
1353'
1354
1355test_expect_success 'option override configured sort' '
1356 (
1357 cd sort &&
1358 git config branch.sort committerdate &&
1359 git branch --sort=refname >actual &&
1360 cat >expect <<-\EOF &&
1361 a
1362 * b
1363 c
1364 master
1365 EOF
1366 test_cmp expect actual
1367 )
1368'
1369
1370test_expect_success 'invalid sort parameter in configuration' '
1371 (
1372 cd sort &&
1373 git config branch.sort "v:notvalid" &&
1374 test_must_fail git branch
1375 )
1376'
1377
a3b427b9 1378test_done