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