From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>, 乙酸鋰 <ch3cooli@gmail.com>,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH] checkout: verify new branch name's validity early
Date: Tue, 28 Aug 2012 09:29:41 +0700 [thread overview]
Message-ID: <1346120981-27234-1-git-send-email-pclouds@gmail.com> (raw)
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
next reply other threads:[~2012-08-28 2:30 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-08-28 2:29 Nguyễn Thái Ngọc Duy [this message]
2012-08-28 3:55 ` [PATCH] checkout: verify new branch name's validity early 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1346120981-27234-1-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=ch3cooli@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).