[PATCH] checkout: verify new branch name's validity early

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



If the new branch name to -b/-B happens to be missing, the next
argument may be mistaken as branch name and no longer recognized by
checkout as argument. This may lead to confusing error messages.

By checking branch name early and printing out the invalid name, users
may realize they forgot to specify branch name.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 On Tue, Aug 28, 2012 at 12:03 AM, Junio C Hamano <gitster@xxxxxxxxx> wrote:
 > Ideally you would want
 >
 >         fatal: "-t" is not an acceptable name for a branch
 >
 > in this case; if it is cumbersome to arrange, at the very least,
 >
 >         updating paths is incompatible with checking out the branch "-t".
 >
 > would be clearer.

 Fair enough. It turns out we do check branch name's validity. It's
 just too late.

 builtin/checkout.c         | 20 ++++++++++----------
 t/t2018-checkout-branch.sh |  5 +++++
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index d812219..03b0f25 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1049,6 +1049,16 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (opts.track == BRANCH_TRACK_UNSPECIFIED)
 		opts.track = git_branch_track;
 
+	if (opts.new_branch) {
+		struct strbuf buf = STRBUF_INIT;
+
+		opts.branch_exists = validate_new_branchname(opts.new_branch, &buf,
+							     !!opts.new_branch_force,
+							     !!opts.new_branch_force);
+
+		strbuf_release(&buf);
+	}
+
 	if (argc) {
 		const char **pathspec = get_pathspec(prefix, argv);
 
@@ -1079,16 +1089,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (patch_mode)
 		return interactive_checkout(new.name, NULL, &opts);
 
-	if (opts.new_branch) {
-		struct strbuf buf = STRBUF_INIT;
-
-		opts.branch_exists = validate_new_branchname(opts.new_branch, &buf,
-							     !!opts.new_branch_force,
-							     !!opts.new_branch_force);
-
-		strbuf_release(&buf);
-	}
-
 	if (new.name && !new.commit) {
 		die(_("Cannot switch branch to a non-commit."));
 	}
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index 2741262..48ab6a2 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -198,4 +198,9 @@ test_expect_success 'checkout -B to the current branch works' '
 	test_dirty_mergeable
 '
 
+test_expect_success 'checkout -b checks branch validitity early' '
+	test_must_fail git checkout -b -t origin/master 2>err &&
+	grep "not a valid branch name" err
+'
+
 test_done
-- 
1.7.12.rc2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]