git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Matthieu Moy <Matthieu.Moy@imag.fr>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 2/2 RFC] verify_filename: ask the caller to chose the kind of diagnosis
Date: Sun, 17 Jun 2012 13:22:26 -0700	[thread overview]
Message-ID: <7vehpd7kot.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: <1339958341-22186-2-git-send-email-Matthieu.Moy@imag.fr> (Matthieu Moy's message of "Sun, 17 Jun 2012 20:39:01 +0200")

Matthieu Moy <Matthieu.Moy@imag.fr> writes:

> +/*
> + * Verify that "name" is a filename.
> + * The "diagnose_rev" is used to provide a user-friendly diagnosis. If
> + * 0, the diagnosis will try to diagnose "name" as an invalid object
> + * name (e.g. HEAD:foo). If non-zero, the diagnosis will only complain
> + * about an inexisting file.
> + */
> +extern void verify_filename(const char *prefix, const char *name, int diagnose_rev);

The whole point of verify_filename() is to make sure, because the
user did not have disambiguating "--" on the command line, that the
first non-rev argument is a path and also it cannot be interpreted
as a valid rev.  It somehow feels wrong to make it also responsible,
for a possibly misspelled rev.  The caller can mistakenly throw 0 or
1 at random but the _only_ right value for this parameter is to set
it to true only for the first non-rev, no?

Let's look at the patched sites.

> diff --git a/builtin/grep.c b/builtin/grep.c
> index fe1726f..41924dc 100644
> --- a/builtin/grep.c
> +++ b/builtin/grep.c
> @@ -927,8 +927,11 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>  	/* The rest are paths */
>  	if (!seen_dashdash) {
>  		int j;
> -		for (j = i; j < argc; j++)
> -			verify_filename(prefix, argv[j]);
> +		if (i < argc) {
> +			verify_filename(prefix, argv[i], 1);
> +			for (j = i + 1; j < argc; j++)
> +				verify_filename(prefix, argv[j], 0);
> +		}

This is exactly

	verify_filename(prefix, argv[j], j == first_non_rev)

> diff --git a/builtin/reset.c b/builtin/reset.c
> index 8c2c1d5..4cc34c9 100644
> --- a/builtin/reset.c
> +++ b/builtin/reset.c
> @@ -285,7 +285,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
>  			rev = argv[i++];
>  		} else {
>  			/* Otherwise we treat this as a filename */
> -			verify_filename(prefix, argv[i]);
> +			verify_filename(prefix, argv[i], 1);

This is also checking the first non-rev, too.  We just saw
"florbl^{triee}" in "git reset florbl^{triee} hello.c" is not a
valid rev.  If "florbl^{triee}" is indeed a file, we shouldn't
complain and die with "This may be a misspelled rev", but take it as
a path.

> diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
> index 733f626..13495b8 100644
> --- a/builtin/rev-parse.c
> +++ b/builtin/rev-parse.c
> @@ -486,7 +486,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
>  
>  		if (as_is) {
>  			if (show_file(arg) && as_is < 2)
> -				verify_filename(prefix, arg);
> +				verify_filename(prefix, arg, 0);
>  			continue;
>  		}
>  		if (!strcmp(arg,"-n")) {
> @@ -734,7 +734,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
>  		as_is = 1;
>  		if (!show_file(arg))
>  			continue;
> -		verify_filename(prefix, arg);
> +		verify_filename(prefix, arg, 1);
>  	}

These are, too.

> diff --git a/revision.c b/revision.c
> index 935e7a7..756196a 100644
> --- a/revision.c
> +++ b/revision.c
> @@ -1780,8 +1780,9 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
>  			 *     as a valid filename.
>  			 * but the latter we have checked in the main loop.
>  			 */
> -			for (j = i; j < argc; j++)
> -				verify_filename(revs->prefix, argv[j]);
> +			verify_filename(revs->prefix, arg, 1);			
> +			for (j = i + 1; j < argc; j++)
> +				verify_filename(revs->prefix, argv[j], 0);

Likewise.

> @@ -81,13 +83,13 @@ static void NORETURN die_verify_filename(const char *prefix, const char *arg)
>   * it to be preceded by the "--" marker (or we want the user to
>   * use a format like "./-filename")
>   */
> -void verify_filename(const char *prefix, const char *arg)
> +void verify_filename(const char *prefix, const char *arg, int diagnose_rev)
>  {
>  	if (*arg == '-')
>  		die("bad flag '%s' used after filename", arg);
>  	if (check_filename(prefix, arg))
>  		return;
> -	die_verify_filename(prefix, arg);
> +	die_verify_filename(prefix, arg, diagnose_rev);

And this implements the "if it is path, don't complain, but
otherwise diagnose misspelled rev if the caller asked us to".

I think the patch is not wrong per-se, but diagnose_rev is probably
misnamed.  It tells the callee what to do, but gives little hint to
the caller when to set it.  s/diagnose_rev/first_non_rev/ or
something might make it easier to understand for future callers.

Thanks.

  reply	other threads:[~2012-06-17 20:22 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-15  4:03 "Detailed diagnosis" perhaps broken Junio C Hamano
2012-06-17 18:34 ` Matthieu Moy
2012-06-17 18:39   ` [PATCH 1/2] sha1_name: don't trigger detailed diagnosis for file arguments Matthieu Moy
2012-06-17 18:39     ` [PATCH 2/2 RFC] verify_filename: ask the caller to chose the kind of diagnosis Matthieu Moy
2012-06-17 20:22       ` Junio C Hamano [this message]
2012-06-18  6:42         ` Matthieu Moy
2012-06-18 16:27           ` Junio C Hamano
2012-06-18 18:18             ` [PATCH 1/2 v2] sha1_name: don't trigger detailed diagnosis for file arguments Matthieu Moy
2012-06-18 18:18               ` [PATCH 2/2 v2] verify_filename: ask the caller to chose the kind of diagnosis Matthieu Moy
2012-06-18 22:25                 ` Junio C Hamano
2012-06-19 11:17                   ` Matthieu Moy
2012-06-18 17:23     ` [PATCH 1/2] sha1_name: don't trigger detailed diagnosis for file arguments Junio C Hamano
2012-06-18 17:42       ` Matthieu Moy
2012-06-18 17:50         ` Junio C Hamano
2012-06-18 17:56           ` Matthieu Moy
2012-06-18 18:01             ` Junio C Hamano

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=7vehpd7kot.fsf@alter.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=Matthieu.Moy@imag.fr \
    --cc=git@vger.kernel.org \
    /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).