Skip to content

Commit 4ac8371

Browse files
committed
Merge branch 'dl/branch-from-3dot-merge-base'
"git branch new A...B" and "git checkout -b new A...B" have been taught that in their contexts, the notation A...B means "the merge base between these two commits", just like "git checkout A...B" detaches HEAD at that commit. * dl/branch-from-3dot-merge-base: branch: make create_branch accept a merge base rev t2018: cleanup in current test
2 parents 3c9b393 + e3d6539 commit 4ac8371

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

Documentation/git-branch.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ argument is missing it defaults to `HEAD` (i.e. the tip of the current
4545
branch).
4646

4747
The command's second form creates a new branch head named <branchname>
48-
which points to the current `HEAD`, or <start-point> if given.
48+
which points to the current `HEAD`, or <start-point> if given. As a
49+
special case, for <start-point>, you may use `"A...B"` as a shortcut for
50+
the merge base of `A` and `B` if there is exactly one merge base. You
51+
can leave out at most one of `A` and `B`, in which case it defaults to
52+
`HEAD`.
4953

5054
Note that this will create the new branch, but it will not switch the
5155
working tree to it; use "git checkout <newbranch>" to switch to the

Documentation/git-checkout.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,10 @@ leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
313313
<start_point>::
314314
The name of a commit at which to start the new branch; see
315315
linkgit:git-branch[1] for details. Defaults to HEAD.
316+
+
317+
As a special case, you may use `"A...B"` as a shortcut for the
318+
merge base of `A` and `B` if there is exactly one merge base. You can
319+
leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
316320

317321
<tree-ish>::
318322
Tree to checkout from (when paths are given). If not specified,

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ void create_branch(struct repository *r,
269269
}
270270

271271
real_ref = NULL;
272-
if (get_oid(start_name, &oid)) {
272+
if (get_oid_mb(start_name, &oid)) {
273273
if (explicit_tracking) {
274274
if (advice_set_upstream_failure) {
275275
error(_(upstream_missing), start_name);

t/t2018-checkout-branch.sh

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,66 +60,66 @@ test_expect_success 'setup' '
6060
'
6161

6262
test_expect_success 'checkout -b to a new branch, set to HEAD' '
63+
test_when_finished "
64+
git checkout branch1 &&
65+
test_might_fail git branch -D branch2" &&
6366
do_checkout branch2
6467
'
6568

66-
test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
67-
git checkout branch1 &&
68-
git branch -D branch2 &&
69+
test_expect_success 'checkout -b to a merge base' '
70+
test_when_finished "
71+
git checkout branch1 &&
72+
test_might_fail git branch -D branch2" &&
73+
git checkout -b branch2 branch1...
74+
'
6975

76+
test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
77+
test_when_finished "
78+
git checkout branch1 &&
79+
test_might_fail git branch -D branch2" &&
7080
do_checkout branch2 $HEAD1
7181
'
7282

7383
test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
74-
git checkout branch1 &&
75-
76-
# clean up from previous test
77-
git branch -D branch2 &&
78-
7984
setup_dirty_unmergeable &&
8085
test_must_fail do_checkout branch2 $HEAD1 &&
8186
test_dirty_unmergeable
8287
'
8388

8489
test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
90+
test_when_finished "
91+
git checkout branch1 &&
92+
test_might_fail git branch -D branch2" &&
93+
8594
# still dirty and on branch1
8695
do_checkout branch2 $HEAD1 "-f -b" &&
8796
test_must_fail test_dirty_unmergeable
8897
'
8998

9099
test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
91-
git checkout branch1 &&
92-
93-
# clean up from previous test
94-
git branch -D branch2 &&
100+
test_when_finished "
101+
git reset --hard &&
102+
git checkout branch1 &&
103+
test_might_fail git branch -D branch2" &&
95104
96105
setup_dirty_mergeable &&
97106
do_checkout branch2 $HEAD1 &&
98107
test_dirty_mergeable
99108
'
100109

101110
test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
102-
# clean up from previous test
103-
git reset --hard &&
104-
105-
git checkout branch1 &&
106-
107-
# clean up from previous test
108-
git branch -D branch2 &&
109-
111+
test_when_finished git reset --hard HEAD &&
110112
setup_dirty_mergeable &&
111113
do_checkout branch2 $HEAD1 "-f -b" &&
112114
test_must_fail test_dirty_mergeable
113115
'
114116

115117
test_expect_success 'checkout -b to an existing branch fails' '
116-
git reset --hard HEAD &&
117-
118+
test_when_finished git reset --hard HEAD &&
118119
test_must_fail do_checkout branch2 $HEAD2
119120
'
120121

121122
test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
122-
git reset --hard HEAD &&
123123
git checkout branch1 &&
124124
git checkout branch2 &&
125125
echo >expect "fatal: A branch named '\''branch1'\'' already exists." &&
@@ -133,6 +133,12 @@ test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
133133
do_checkout branch2 "" -B
134134
'
135135

136+
test_expect_success 'checkout -B to a merge base' '
137+
git checkout branch1 &&
138+
139+
git checkout -B branch2 branch1...
140+
'
141+
136142
test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
137143
git checkout $(git rev-parse --verify HEAD) &&
138144
@@ -160,6 +166,7 @@ test_expect_success 'checkout -f -B to an existing branch with unmergeable chang
160166
'
161167

162168
test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
169+
test_when_finished git reset --hard &&
163170
git checkout branch1 &&
164171
165172
setup_dirty_mergeable &&
@@ -168,9 +175,6 @@ test_expect_success 'checkout -B to an existing branch preserves mergeable chang
168175
'
169176

170177
test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
171-
# clean up from previous test
172-
git reset --hard &&
173-
174178
git checkout branch1 &&
175179
176180
setup_dirty_mergeable &&

t/t3200-branch.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ test_expect_success 'git branch a/b/c should create a branch' '
4242
git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
4343
'
4444

45+
test_expect_success 'git branch mb master... should create a branch' '
46+
git branch mb master... && test_path_is_file .git/refs/heads/mb
47+
'
48+
4549
test_expect_success 'git branch HEAD should fail' '
4650
test_must_fail git branch HEAD
4751
'
@@ -316,8 +320,8 @@ test_expect_success 'git branch --list -v with --abbrev' '
316320
test_expect_success 'git branch --column' '
317321
COLUMNS=81 git branch --column=column >actual &&
318322
cat >expected <<\EOF &&
319-
a/b/c bam foo l * master n o/p r
320-
abc bar j/k m/m master2 o/o q
323+
a/b/c bam foo l * master mb o/o q
324+
abc bar j/k m/m master2 n o/p r
321325
EOF
322326
test_cmp expected actual
323327
'
@@ -339,6 +343,7 @@ test_expect_success 'git branch --column with an extremely long branch name' '
339343
m/m
340344
* master
341345
master2
346+
mb
342347
n
343348
o/o
344349
o/p
@@ -356,8 +361,8 @@ test_expect_success 'git branch with column.*' '
356361
git config --unset column.branch &&
357362
git config --unset column.ui &&
358363
cat >expected <<\EOF &&
359-
a/b/c bam foo l * master n o/p r
360-
abc bar j/k m/m master2 o/o q
364+
a/b/c bam foo l * master mb o/o q
365+
abc bar j/k m/m master2 n o/p r
361366
EOF
362367
test_cmp expected actual
363368
'
@@ -381,6 +386,7 @@ test_expect_success 'git branch -v with column.ui ignored' '
381386
m/m
382387
* master
383388
master2
389+
mb
384390
n
385391
o/o
386392
o/p

0 commit comments

Comments
 (0)