DORMANTno replies

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

From: Nguyễn Thái Ngọc Duy <hidden>
Date: 2016-06-15 22:54:35
Subsystem: the rest · Maintainer: Linus Torvalds

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 <redacted>
---
 On Tue, Aug 28, 2012 at 12:03 AM, Junio C Hamano [off-list ref] 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help