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