Skip to content

Commit

Permalink
Merge branch 'es/do-not-let-rebase-switch-to-protected-branch'
Browse files Browse the repository at this point in the history
"git rebase BASE BRANCH" rebased/updated the tip of BRANCH and
checked it out, even when the BRANCH is checked out in a different
worktree.  This has been corrected.

* es/do-not-let-rebase-switch-to-protected-branch:
  rebase: refuse to switch to branch already checked out elsewhere
  t3400: make test clean up after itself
  • Loading branch information
gitster committed Mar 5, 2020
2 parents 4a2e91d + b5cabb4 commit a0ab37d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
5 changes: 3 additions & 2 deletions builtin/rebase.c
Expand Up @@ -2044,10 +2044,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'"),
Expand Down
20 changes: 19 additions & 1 deletion t/t3400-rebase.sh
Expand Up @@ -143,11 +143,11 @@ test_expect_success 'setup: recover' '

test_expect_success 'Show verbose error when HEAD could not be detached' '
>B &&
test_when_finished "rm -f B" &&
test_must_fail git rebase topic 2>output.err >output.out &&
test_i18ngrep "The following untracked working tree files would be overwritten by checkout:" output.err &&
test_i18ngrep B output.err
'
rm -f B

test_expect_success 'fail when upstream arg is missing and not on branch' '
git checkout topic &&
Expand Down Expand Up @@ -401,4 +401,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

0 comments on commit a0ab37d

Please sign in to comment.