summaryrefslogtreecommitdiff
path: root/builtin-checkout.c
diff options
context:
space:
mode:
authorDaniel Barkalow <barkalow@iabervon.org>2008-09-21 18:36:06 (GMT)
committerJunio C Hamano <gitster@pobox.com>2008-09-22 06:17:06 (GMT)
commit352eadc40024b141e1295693654ec20cc123844f (patch)
tree240eec2143c0d447bcea9aec3a56323abe4d909e /builtin-checkout.c
parentcc185a6a8ac24737a26ec4b40cc401c2db8b2e97 (diff)
downloadgit-352eadc40024b141e1295693654ec20cc123844f.zip
git-352eadc40024b141e1295693654ec20cc123844f.tar.gz
git-352eadc40024b141e1295693654ec20cc123844f.tar.bz2
Check early that a new branch is new and valid
If you fail to update refs to change branches in checkout, your index and working tree are left already updated. We don't have an easy way to undo this, but at least we can check things that would make the creation of a new branch fail. These checks were in the shell version, and were lost in the C conversion. The messages are from the shell version, and should probably be made nicer. [jc: added test to t7201] Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin-checkout.c')
-rw-r--r--builtin-checkout.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 08c6d86..1ee2346 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.");
}