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