diff --git a/builtin/checkout.c b/builtin/checkout.c index 22fb6c0cae..7cd01f62be 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -725,7 +725,10 @@ static int merge_working_tree(const struct checkout_opts *opts, */ struct tree *result; struct tree *work; + struct tree *old_tree; struct merge_options o; + struct strbuf sb = STRBUF_INIT; + if (!opts->merge) return 1; @@ -735,6 +738,12 @@ static int merge_working_tree(const struct checkout_opts *opts, */ if (!old_branch_info->commit) return 1; + old_tree = get_commit_tree(old_branch_info->commit); + + if (repo_index_has_changes(the_repository, old_tree, &sb)) + die(_("cannot continue with staged changes in " + "the following files:\n%s"), sb.buf); + strbuf_release(&sb); /* Do more real merge */ @@ -772,7 +781,7 @@ static int merge_working_tree(const struct checkout_opts *opts, ret = merge_trees(&o, get_commit_tree(new_branch_info->commit), work, - get_commit_tree(old_branch_info->commit), + old_tree, &result); if (ret < 0) exit(128); diff --git a/t/t7201-co.sh b/t/t7201-co.sh index f165582019..5990299fc9 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -224,15 +224,7 @@ test_expect_success 'switch to another branch while carrying a deletion' ' test_i18ngrep overwritten errs && test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs && - test_must_be_empty errs && - - git checkout --merge simple 2>errs && - test_i18ngrep ! overwritten errs && - git ls-files -u && - test_must_fail git cat-file -t :0:two && - test "$(git cat-file -t :1:two)" = blob && - test "$(git cat-file -t :2:two)" = blob && - test_must_fail git cat-file -t :3:two + test_must_be_empty errs ' test_expect_success 'checkout to detach HEAD (with advice declined)' '