diff options
-rw-r--r-- | builtin-checkout.c | 12 | ||||
-rwxr-xr-x | t/t7201-co.sh | 10 |
2 files changed, 22 insertions, 0 deletions
diff --git a/builtin-checkout.c b/builtin-checkout.c index 08c6d8614a..1ee23468ff 100644 --- a/builtin-checkout.c +++ b/builtin-checkout.c @@ -565,6 +565,18 @@ no_reference: return checkout_paths(source_tree, pathspec); } + if (opts.new_branch) { + struct strbuf buf; + strbuf_init(&buf, 0); + strbuf_addstr(&buf, "refs/heads/"); + strbuf_addstr(&buf, opts.new_branch); + if (!get_sha1(buf.buf, rev)) + die("git checkout: branch %s already exists", opts.new_branch); + if (check_ref_format(buf.buf)) + die("git checkout: we do not like '%s' as a branch name.", opts.new_branch); + strbuf_release(&buf); + } + if (new.name && !new.commit) { die("Cannot switch branch to a non-commit."); } diff --git a/t/t7201-co.sh b/t/t7201-co.sh index fbec70d3c6..0679abd29d 100755 --- a/t/t7201-co.sh +++ b/t/t7201-co.sh @@ -359,4 +359,14 @@ test_expect_success 'checkout an unmerged path should fail' ' test_cmp sample file ' +test_expect_success 'failing checkout -b should not break working tree' ' + git reset --hard master && + git symbolic-ref HEAD refs/heads/master && + test_must_fail git checkout -b renamer side^ && + test $(git symbolic-ref HEAD) = refs/heads/master && + git diff --exit-code && + git diff --cached --exit-code + +' + test_done |