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