rebase: refuse to switch to branch already checked out elsewhere
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>
Eric Sunshine committed
Feb 23, 2020 at 05:14 UTC
b5cabb4a967fa455378ee7ddfa831a9bf0244753
2 files changed
+21
-2
builtin/rebase.c
+3
-2
@@ -1953,10 +1953,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
1953
/* Is it a local branch? */
1954
strbuf_reset(&buf);
1955
strbuf_addf(&buf, "refs/heads/%s", branch_name);
1956
- if (!read_ref(buf.buf, &options.orig_head))
1956
+ if (!read_ref(buf.buf, &options.orig_head)) {
1957
+ die_if_checked_out(buf.buf, 1);
1958
options.head_name = xstrdup(buf.buf);
1959
/* If not is it a valid ref (branch or commit)? */
1959
- else if (!get_oid(branch_name, &options.orig_head))
1960
+ } else if (!get_oid(branch_name, &options.orig_head))
1961
options.head_name = NULL;
1962
else
1963
die(_("fatal: no such branch/commit '%s'"),
t/t3400-rebase.sh
+18
@@ -377,4 +377,22 @@ test_expect_success 'rebase -c rebase.useBuiltin=false warning' '
377
test_must_be_empty err
378
'
379
380
+test_expect_success 'switch to branch checked out here' '
381
+ git checkout master &&
382
+ git rebase master master
383
+'
384
+
385
+test_expect_success 'switch to branch not checked out' '
386
+ git checkout master &&
387
+ git branch other &&
388
+ git rebase master other
389
+'
390
+
391
+test_expect_success 'refuse to switch to branch checked out elsewhere' '
392
+ git checkout master &&
393
+ git worktree add wt &&
394
+ test_must_fail git -C wt rebase master master 2>err &&
395
+ test_i18ngrep "already checked out" err
396
+'
397
+
398
test_done