Merge branch 'db/maint-checkout-b'
authorJunio C Hamano <gitster@pobox.com>
Mon, 22 Sep 2008 06:50:05 +0000 (23:50 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 22 Sep 2008 06:50:05 +0000 (23:50 -0700)
* db/maint-checkout-b:
Check early that a new branch is new and valid

builtin-checkout.c
t/t7201-co.sh
index 9377a1c71ea8571d3e36e9e19aa322db765e93c8..4497b7092d0e88aa731c64c0f22bf0d05ead0411 100644 (file)
@@ -580,6 +580,18 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
                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.");
        }
index 25181388f8a8f1730d05fce128e0e1fc66eb3c0e..62d73f934a0c0be2d41a30d11308430838f6c9c0 100755 (executable)
@@ -391,4 +391,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