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