git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] checkout: verify new branch name's validity early
@ 2012-08-28  2:29 Nguyễn Thái Ngọc Duy
  2012-08-28  3:55 ` Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2012-08-28  2:29 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, 乙酸鋰,
	Nguyễn Thái Ngọc Duy

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@gmail.com>
---
 On Tue, Aug 28, 2012 at 12:03 AM, Junio C Hamano <gitster@pobox.com> 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

^ permalink raw reply related	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2012-09-07 20:19 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-08-28  2:29 [PATCH] checkout: verify new branch name's validity early Nguyễn Thái Ngọc Duy
2012-08-28  3:55 ` Junio C Hamano
2012-08-28 13:49   ` [PATCH 0/3] Refactor checkout option handling Nguyễn Thái Ngọc Duy
2012-08-28 13:49     ` [PATCH 1/3] checkout: pass "struct checkout_opts *" as const pointer Nguyễn Thái Ngọc Duy
2012-08-28 13:49     ` [PATCH 2/3] checkout: reorder option handling Nguyễn Thái Ngọc Duy
2012-08-28 20:45       ` Junio C Hamano
2012-08-29 12:16         ` Nguyen Thai Ngoc Duy
2012-08-29 13:55           ` [PATCH v2 1/3] checkout: pass "struct checkout_opts *" as const pointer Nguyễn Thái Ngọc Duy
2012-08-29 13:55             ` [PATCH v2 2/3] checkout: move more parameters to struct checkout_opts Nguyễn Thái Ngọc Duy
2012-08-29 13:55             ` [PATCH v2 3/3] checkout: reorder option handling Nguyễn Thái Ngọc Duy
2012-08-29 18:37               ` Junio C Hamano
2012-08-29 18:55               ` Junio C Hamano
2012-08-30 12:45               ` [PATCH v3 " Nguyễn Thái Ngọc Duy
2012-08-30 16:10                 ` Junio C Hamano
2012-09-07 20:19                 ` Junio C Hamano
2012-08-28 13:49     ` [PATCH 3/3] checkout: move branch guessing code out as a separate function Nguyễn Thái Ngọc Duy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).