From: Junio C Hamano <gitster@pobox.com>
To: Matthieu Moy <Matthieu.Moy@imag.fr>
Cc: git@vger.kernel.org, pclouds@gmail.com, jc@sahnwaldt.de,
jrnieder@gmail.com
Subject: Re: [PATCH v3 1/2] checkout: allow dwim for branch creation for "git checkout $branch --"
Date: Thu, 17 Oct 2013 11:09:37 -0700 [thread overview]
Message-ID: <xmqqr4bj22hq.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <1380186486-8220-1-git-send-email-Matthieu.Moy@imag.fr> (Matthieu Moy's message of "Thu, 26 Sep 2013 11:08:05 +0200")
Matthieu Moy <Matthieu.Moy@imag.fr> writes:
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index 0f57397..9edd9c3 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -863,6 +863,13 @@ static const char *unique_tracking_name(const char *name, unsigned char *sha1)
> return NULL;
> }
>
> +static int error_invalid_ref(const char *arg, int has_dash_dash, int argcount)
> +{
> + if (has_dash_dash)
> + die(_("invalid reference: %s"), arg);
> + return argcount;
> +}
This is somewhat unfortunate; it pretends to be a reusable helper by
being a separate function, but it is not very reusable (see below).
> @@ -917,19 +934,32 @@ static int parse_branchname_arg(int argc, const char **argv,
> arg = "@{-1}";
>
> if (get_sha1_mb(arg, rev)) {
> + /*
> + * Either case (3) or (4), with <something> not being
> + * a commit, or an attempt to use case (1) with an
> + * invalid ref.
> + */
> + int try_dwim = dwim_new_local_branch_ok;
> +
> + if (check_filename(NULL, arg) && !has_dash_dash)
> + try_dwim = 0;
> + /*
> + * Accept "git checkout foo" and "git checkout foo --"
> + * as candidates for dwim.
> + */
> + if (!(argc == 1 && !has_dash_dash) &&
> + !(argc == 2 && has_dash_dash))
> + try_dwim = 0;
> +
> + if (try_dwim) {
> const char *remote = unique_tracking_name(arg, rev);
Up to this point, the updated code makes very good sense.
> if (!remote)
> - return argcount;
> + return error_invalid_ref(arg, has_dash_dash, argcount);
The original that returned "argcount" from here were unnecessarily
misleading in the first place. It saw "git checkout foo" where "foo"
does not refer to an object nor a filesystem entity and there was no
unique "refs/remotes/*/foo"; it wanted to return 0 to tell the
caller that it consumed zero arguments as branch names.
And the updated code is even more obscure. This calling site makes
it look as if it is an error to have no unique "refs/remotes/*/foo"
at this point of the code by naming the helper function "error_*()",
but it is an error in some case and not in others.
if (!remote) {
if (has_dash_dash)
die(_("..."));
return 0;
}
would be a lot more understandable.
The only reason you have conditional die() here (and on the "else"
side of this "if" statement) is because you delayed the die that was
at a much earlier point in the original. And the only reason you
created the unfortunate helper function is because you need to deal
with that delayed decision to die now in two places.
So it may be even cleaner to read if you did it this way:
if (get_sha1_mb(...)) {
/*
* The first token is not a valid rev; we should
* ordinarily error out if "git checkout foo --"
* if foo is not a valid rev, but first see if
* we can auto-create foo to continue...
*/
int recover_with_dwim = dwim_new_local_branch_ok;
... decide if we want to recover_with_dwim ...
if (recover_with_dwim) {
const char *remote = unique_tracking_name(arg, rev);
if (remote) {
*new_branch = arg;
arg = remote;
} else {
/* no; arg cannot be salvaged */
recover_with_dwim = 0;
}
}
if (!recover_with_dwim) {
if (has_dash_dash)
die(_("invalid ref %s", arg);
return 0; /* we saw no branch/commit */
}
/* otherwise we made a successful recovery */
}
prev parent reply other threads:[~2013-10-17 18:09 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-26 9:08 [PATCH v3 1/2] checkout: allow dwim for branch creation for "git checkout $branch --" Matthieu Moy
2013-09-26 9:08 ` [PATCH v3 2/2] checkout: proper error message on 'git checkout foo bar --' Matthieu Moy
2013-10-17 18:09 ` Junio C Hamano [this message]
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=xmqqr4bj22hq.fsf@gitster.dls.corp.google.com \
--to=gitster@pobox.com \
--cc=Matthieu.Moy@imag.fr \
--cc=git@vger.kernel.org \
--cc=jc@sahnwaldt.de \
--cc=jrnieder@gmail.com \
--cc=pclouds@gmail.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).