Skip to content

Commit a0ab37d

Browse files
committed
Merge branch 'es/do-not-let-rebase-switch-to-protected-branch'
"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
2 parents 4a2e91d + b5cabb4 commit a0ab37d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

builtin/rebase.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,10 +2044,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
20442044
/* Is it a local branch? */
20452045
strbuf_reset(&buf);
20462046
strbuf_addf(&buf, "refs/heads/%s", branch_name);
2047-
if (!read_ref(buf.buf, &options.orig_head))
2047+
if (!read_ref(buf.buf, &options.orig_head)) {
2048+
die_if_checked_out(buf.buf, 1);
20482049
options.head_name = xstrdup(buf.buf);
20492050
/* If not is it a valid ref (branch or commit)? */
2050-
else if (!get_oid(branch_name, &options.orig_head))
2051+
} else if (!get_oid(branch_name, &options.orig_head))
20512052
options.head_name = NULL;
20522053
else
20532054
die(_("fatal: no such branch/commit '%s'"),

t/t3400-rebase.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ test_expect_success 'setup: recover' '
143143

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

152152
test_expect_success 'fail when upstream arg is missing and not on branch' '
153153
git checkout topic &&
@@ -401,4 +401,22 @@ test_expect_success 'rebase -c rebase.useBuiltin=false warning' '
401401
test_must_be_empty err
402402
'
403403

404+
test_expect_success 'switch to branch checked out here' '
405+
git checkout master &&
406+
git rebase master master
407+
'
408+
409+
test_expect_success 'switch to branch not checked out' '
410+
git checkout master &&
411+
git branch other &&
412+
git rebase master other
413+
'
414+
415+
test_expect_success 'refuse to switch to branch checked out elsewhere' '
416+
git checkout master &&
417+
git worktree add wt &&
418+
test_must_fail git -C wt rebase master master 2>err &&
419+
test_i18ngrep "already checked out" err
420+
'
421+
404422
test_done

0 commit comments

Comments
 (0)