]> git.ipfire.org Git - thirdparty/git.git/commitdiff
branch: make create_branch accept a merge base rev
authorDenton Liu <liu.denton@gmail.com>
Sat, 27 Apr 2019 12:02:22 +0000 (05:02 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 May 2019 04:23:13 +0000 (13:23 +0900)
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 <gitster@pobox.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-branch.txt
Documentation/git-checkout.txt
branch.c
t/t2018-checkout-branch.sh
t/t3200-branch.sh

index 3bd83a7cbdd9d62c9b10057bd4bf3f03d57b1085..8719e4cab9bee72ccf49169250d1969b69f3db55 100644 (file)
@@ -45,7 +45,11 @@ argument is missing it defaults to `HEAD` (i.e. the tip of the current
 branch).
 
 The command's second form creates a new branch head named <branchname>
-which points to the current `HEAD`, or <start-point> if given.
+which points to the current `HEAD`, or <start-point> if given. As a
+special case, for <start-point>, you may use `"A...B"` as a shortcut for
+the merge base of `A` and `B` if there is exactly one merge base. You
+can leave out at most one of `A` and `B`, in which case it defaults to
+`HEAD`.
 
 Note that this will create the new branch, but it will not switch the
 working tree to it; use "git checkout <newbranch>" to switch to the
index 8c3d4128c2871274967cd30029e2390e48d0dcc1..d32f2eb611fe894dc4d99895fbab9527255d23c9 100644 (file)
@@ -301,6 +301,10 @@ leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
 <start_point>::
        The name of a commit at which to start the new branch; see
        linkgit:git-branch[1] for details. Defaults to HEAD.
++
+As a special case, you may use `"A...B"` as a shortcut for the
+merge base of `A` and `B` if there is exactly one merge base. You can
+leave out at most one of `A` and `B`, in which case it defaults to `HEAD`.
 
 <tree-ish>::
        Tree to checkout from (when paths are given). If not specified,
index 28b81a7e0256b2923c0fe296d9618d130821cdc4..a84c8aaca2d3c563d167e25c6a3add950ec85a6c 100644 (file)
--- a/branch.c
+++ b/branch.c
@@ -268,7 +268,7 @@ void create_branch(struct repository *r,
        }
 
        real_ref = NULL;
-       if (get_oid(start_name, &oid)) {
+       if (get_oid_mb(start_name, &oid)) {
                if (explicit_tracking) {
                        if (advice_set_upstream_failure) {
                                error(_(upstream_missing), start_name);
index f1c7023e1ad51ca8bc3a3cf0dc6eeaa3a8f18dff..822381dd9df67f7a392e7697c21d9aade2eb56e1 100755 (executable)
@@ -66,6 +66,13 @@ test_expect_success 'checkout -b to a new branch, set to HEAD' '
        do_checkout branch2
 '
 
+test_expect_success 'checkout -b to a merge base' '
+       test_when_finished "
+               git checkout branch1 &&
+               test_might_fail git branch -D branch2" &&
+       git checkout -b branch2 branch1...
+'
+
 test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
        test_when_finished "
                git checkout branch1 &&
@@ -126,6 +133,12 @@ test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
        do_checkout branch2 "" -B
 '
 
+test_expect_success 'checkout -B to a merge base' '
+       git checkout branch1 &&
+
+       git checkout -B branch2 branch1...
+'
+
 test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
        git checkout $(git rev-parse --verify HEAD) &&
 
index 478b82cf9b513a8f45f250de59d519847227f794..acb16b62dda3abd87bfaad7658390c683b15e176 100755 (executable)
@@ -42,6 +42,10 @@ test_expect_success 'git branch a/b/c should create a branch' '
        git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
 '
 
+test_expect_success 'git branch mb master... should create a branch' '
+       git branch mb master... && test_path_is_file .git/refs/heads/mb
+'
+
 test_expect_success 'git branch HEAD should fail' '
        test_must_fail git branch HEAD
 '
@@ -292,8 +296,8 @@ test_expect_success 'git branch --list -v with --abbrev' '
 test_expect_success 'git branch --column' '
        COLUMNS=81 git branch --column=column >actual &&
        cat >expected <<\EOF &&
-  a/b/c     bam       foo       l       * master    n         o/p       r
-  abc       bar       j/k       m/m       master2   o/o       q
+  a/b/c     bam       foo       l       * master    mb        o/o       q
+  abc       bar       j/k       m/m       master2   n         o/p       r
 EOF
        test_cmp expected actual
 '
@@ -315,6 +319,7 @@ test_expect_success 'git branch --column with an extremely long branch name' '
   m/m
 * master
   master2
+  mb
   n
   o/o
   o/p
@@ -332,8 +337,8 @@ test_expect_success 'git branch with column.*' '
        git config --unset column.branch &&
        git config --unset column.ui &&
        cat >expected <<\EOF &&
-  a/b/c   bam   foo   l   * master    n     o/p   r
-  abc     bar   j/k   m/m   master2   o/o   q
+  a/b/c   bam   foo   l   * master    mb   o/o   q
+  abc     bar   j/k   m/m   master2   n    o/p   r
 EOF
        test_cmp expected actual
 '
@@ -357,6 +362,7 @@ test_expect_success 'git branch -v with column.ui ignored' '
   m/m
 * master
   master2
+  mb
   n
   o/o
   o/p