]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rebase: refuse to switch to branch already checked out elsewhere
authorEric Sunshine <sunshine@sunshineco.com>
Sun, 23 Feb 2020 10:14:07 +0000 (05:14 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 24 Feb 2020 19:34:41 +0000 (11:34 -0800)
The invocation "git rebase <upstream> <branch>" switches to <branch>
before performing the rebase operation. However, unlike git-switch,
git-checkout, and git-worktree which all refuse to switch to a branch
that is already checked out in some other worktree, git-rebase switches
to <branch> unconditionally. Curb this careless behavior by making
git-rebase also refuse to switch to a branch checked out elsewhere.

Reported-by: Mike Hommey <mh@glandium.org>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/rebase.c
t/t3400-rebase.sh

index 8081741f8aac4e64010ae11871078917b5d535e8..1c7d250ba3683d33bd9cbb641bc4a9e855917238 100644 (file)
@@ -1953,10 +1953,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
                /* Is it a local branch? */
                strbuf_reset(&buf);
                strbuf_addf(&buf, "refs/heads/%s", branch_name);
-               if (!read_ref(buf.buf, &options.orig_head))
+               if (!read_ref(buf.buf, &options.orig_head)) {
+                       die_if_checked_out(buf.buf, 1);
                        options.head_name = xstrdup(buf.buf);
                /* If not is it a valid ref (branch or commit)? */
-               else if (!get_oid(branch_name, &options.orig_head))
+               else if (!get_oid(branch_name, &options.orig_head))
                        options.head_name = NULL;
                else
                        die(_("fatal: no such branch/commit '%s'"),
index 6e746dca003e42e9010bc03fc1afa9a8313c15eb..9aa5268a068bacaa21abb9040483ba4d808d29f1 100755 (executable)
@@ -377,4 +377,22 @@ test_expect_success 'rebase -c rebase.useBuiltin=false warning' '
        test_must_be_empty err
 '
 
+test_expect_success 'switch to branch checked out here' '
+       git checkout master &&
+       git rebase master master
+'
+
+test_expect_success 'switch to branch not checked out' '
+       git checkout master &&
+       git branch other &&
+       git rebase master other
+'
+
+test_expect_success 'refuse to switch to branch checked out elsewhere' '
+       git checkout master &&
+       git worktree add wt &&
+       test_must_fail git -C wt rebase master master 2>err &&
+       test_i18ngrep "already checked out" err
+'
+
 test_done