Skip to content

Commit e3d6539

Browse files
Denton-Lgitster
authored andcommitted
branch: make create_branch accept a merge base rev
When we ran something like $ git checkout -b test master... it would fail with the message fatal: Not a valid object name: 'master...'. This was caused by the call to `create_branch` where `start_name` is expected to be a valid rev. However, git-checkout allows the branch to be a valid _merge base_ rev (i.e. with a "...") so it was possible for an invalid rev to be passed in. Make `create_branch` accept a merge base rev so that this case does not error out. As a side-effect, teach git-branch how to handle merge base revs as well. Helped-by: Junio C Hamano <[email protected]> Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 27434bf commit e3d6539

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
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
@@ -301,6 +301,10 @@ leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
301301
<start_point>::
302302
The name of a commit at which to start the new branch; see
303303
linkgit:git-branch[1] for details. Defaults to HEAD.
304+
+
305+
As a special case, you may use `"A...B"` as a shortcut for the
306+
merge base of `A` and `B` if there is exactly one merge base. You can
307+
leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
304308

305309
<tree-ish>::
306310
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
@@ -268,7 +268,7 @@ void create_branch(struct repository *r,
268268
}
269269

270270
real_ref = NULL;
271-
if (get_oid(start_name, &oid)) {
271+
if (get_oid_mb(start_name, &oid)) {
272272
if (explicit_tracking) {
273273
if (advice_set_upstream_failure) {
274274
error(_(upstream_missing), start_name);

t/t2018-checkout-branch.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ test_expect_success 'checkout -b to a new branch, set to HEAD' '
6666
do_checkout branch2
6767
'
6868

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+
'
75+
6976
test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
7077
test_when_finished "
7178
git checkout branch1 &&
@@ -126,6 +133,12 @@ test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
126133
do_checkout branch2 "" -B
127134
'
128135

136+
test_expect_success 'checkout -B to a merge base' '
137+
git checkout branch1 &&
138+
139+
git checkout -B branch2 branch1...
140+
'
141+
129142
test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
130143
git checkout $(git rev-parse --verify HEAD) &&
131144

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
'
@@ -292,8 +296,8 @@ test_expect_success 'git branch --list -v with --abbrev' '
292296
test_expect_success 'git branch --column' '
293297
COLUMNS=81 git branch --column=column >actual &&
294298
cat >expected <<\EOF &&
295-
a/b/c bam foo l * master n o/p r
296-
abc bar j/k m/m master2 o/o q
299+
a/b/c bam foo l * master mb o/o q
300+
abc bar j/k m/m master2 n o/p r
297301
EOF
298302
test_cmp expected actual
299303
'
@@ -315,6 +319,7 @@ test_expect_success 'git branch --column with an extremely long branch name' '
315319
m/m
316320
* master
317321
master2
322+
mb
318323
n
319324
o/o
320325
o/p
@@ -332,8 +337,8 @@ test_expect_success 'git branch with column.*' '
332337
git config --unset column.branch &&
333338
git config --unset column.ui &&
334339
cat >expected <<\EOF &&
335-
a/b/c bam foo l * master n o/p r
336-
abc bar j/k m/m master2 o/o q
340+
a/b/c bam foo l * master mb o/o q
341+
abc bar j/k m/m master2 n o/p r
337342
EOF
338343
test_cmp expected actual
339344
'
@@ -357,6 +362,7 @@ test_expect_success 'git branch -v with column.ui ignored' '
357362
m/m
358363
* master
359364
master2
365+
mb
360366
n
361367
o/o
362368
o/p

0 commit comments

Comments
 (0)