git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Phillip Wood <phillip.wood@talktalk.net>
Cc: Git Mailing List <git@vger.kernel.org>,
	Eric Sunshine <sunshine@sunshineco.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: Re: [PATCH v2 2/5] am: improve author-script error reporting
Date: Thu, 25 Oct 2018 17:55:51 +0900	[thread overview]
Message-ID: <xmqqy3amh0bs.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <20181018100023.7327-3-phillip.wood@talktalk.net> (Phillip Wood's message of "Thu, 18 Oct 2018 11:00:20 +0100")

Phillip Wood <phillip.wood@talktalk.net> writes:

> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> If there are errors in a user edited author-script there was no
> indication of what was wrong. This commit adds some specific error messages
> depending on the problem. It also relaxes the requirement that the
> variables appear in a specific order in the file to match the behavior
> of 'rebase --interactive'.

That relaxing is sensible; there is no reason to insist that the
file we are reading was written by exactly the same writer as we
have.

> diff --git a/builtin/am.c b/builtin/am.c
> index b68578bc3f..d42b725273 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -270,8 +270,11 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
>  		struct string_list_item *item;
>  		char *np;
>  		char *cp = strchr(buf, '=');
> -		if (!cp)
> -			return -1;
> +		if (!cp) {
> +			np = strchrnul(buf, '\n');
> +			return error(_("unable to parse '%.*s'"),
> +				     (int) (np - buf), buf);
> +		}

We are unable to parse because it is not of KEY='VALUE' form.  Is
that something worth reporting, e.g. "no key present in '%.*s'"?

> @@ -280,7 +283,8 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
>  		*np = '\0';
>  		cp = sq_dequote(cp);
>  		if (!cp)
> -			return -1;
> +			return error(_("unable to dequote value of '%s'"),
> +				     item->string);

At this point, item->string is what we earlier found on the left
hand side of the '=', i.e. the key.  The message makes sense.

> @@ -308,6 +312,7 @@ static int read_author_script(struct am_state *state)
>  	struct strbuf buf = STRBUF_INIT;
>  	struct string_list kv = STRING_LIST_INIT_DUP;
>  	int retval = -1; /* assume failure */
> +	int i, name_i = -2, email_i = -2, date_i = -2, err = 0;

That -2 is somewhat cute.  If we find a dup, then it will become -1
so later check for missing field would not trigger.

> @@ -326,14 +331,38 @@ static int read_author_script(struct am_state *state)
>  	if (parse_key_value_squoted(buf.buf, &kv))
>  		goto finish;
>  
> -	if (kv.nr != 3 ||
> -	    strcmp(kv.items[0].string, "GIT_AUTHOR_NAME") ||
> -	    strcmp(kv.items[1].string, "GIT_AUTHOR_EMAIL") ||
> -	    strcmp(kv.items[2].string, "GIT_AUTHOR_DATE"))
> +	for (i = 0; i < kv.nr; i++) {
> +		if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) {
> +			if (name_i >= 0)
> +				name_i = error(_("'GIT_AUTHOR_NAME' already given"));
> +			else
> +				name_i = i;

However, if you have three instances of GIT_AUTHOR_NAME, then

 - the first one makes name_i point at it
 - the second one triggers an error and name_i becomes -1
 - the third one makes name_i point at it again

And name_i is not -2 so we won't give "missing" error, which is OK,
but we end up having a usable name even though we said we detected
duplicate!

You can probably compare name_i with -2 when detecting the dup to
fix it, i.e.

	if (name_i != -2)
		name_i = error("found dup");
	else
		name_i = i;

> +		} else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) {
> +			if (email_i >= 0)
> +				email_i = error(_("'GIT_AUTHOR_EMAIL' already given"));
> +			else
> +				email_i = i;
> +		} else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) {
> +			if (date_i >= 0)
> +				date_i = error(_("'GIT_AUTHOR_DATE' already given"));
> +			else
> +				date_i = i;
> +		} else {
> +			err = error(_("unknown variable '%s'"),
> +				    kv.items[i].string);
> +		}
> +	}
> +	if (name_i == -2)
> +		error(_("missing 'GIT_AUTHOR_NAME'"));
> +	if (email_i == -2)
> +		error(_("missing 'GIT_AUTHOR_EMAIL'"));
> +	if (date_i == -2)
> +		error(_("missing 'GIT_AUTHOR_DATE'"));
> +	if (date_i < 0 || email_i < 0 || date_i < 0 || err)
>  		goto finish;
> -	state->author_name = kv.items[0].util;
> -	state->author_email = kv.items[1].util;
> -	state->author_date = kv.items[2].util;
> +	state->author_name = kv.items[name_i].util;
> +	state->author_email = kv.items[email_i].util;
> +	state->author_date = kv.items[date_i].util;
>  	retval = 0;
>  finish:
>  	string_list_clear(&kv, !!retval);

  reply	other threads:[~2018-10-25  8:55 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-12 10:10 [PATCH 0/3] am/rebase: share read_author_script() Phillip Wood
2018-09-12 10:10 ` [PATCH 1/3] am: rename read_author_script() Phillip Wood
2018-09-12 10:10 ` [PATCH 2/3] add read_author_script() to libgit Phillip Wood
2018-09-13 23:49   ` Eric Sunshine
2018-10-10 10:14     ` Phillip Wood
2018-10-10 10:22       ` Eric Sunshine
2018-09-12 10:10 ` [PATCH 3/3] sequencer: use read_author_script() Phillip Wood
2018-09-12 12:14   ` Eric Sunshine
2018-09-14  0:31   ` Eric Sunshine
2018-10-10 10:35     ` Phillip Wood
2018-10-18 10:00 ` [PATCH v2 0/5] am/rebase: share read_author_script() Phillip Wood
2018-10-18 10:00   ` [PATCH v2 1/5] am: don't die in read_author_script() Phillip Wood
2018-10-25  8:44     ` Junio C Hamano
2018-10-18 10:00   ` [PATCH v2 2/5] am: improve author-script error reporting Phillip Wood
2018-10-25  8:55     ` Junio C Hamano [this message]
2018-10-18 10:00   ` [PATCH v2 3/5] am: rename read_author_script() Phillip Wood
2018-10-18 10:00   ` [PATCH v2 4/5] add read_author_script() to libgit Phillip Wood
2018-10-18 10:00   ` [PATCH v2 5/5] sequencer: use read_author_script() Phillip Wood
2018-10-25  8:59   ` [PATCH v2 0/5] am/rebase: share read_author_script() Junio C Hamano
2018-10-26  9:00     ` Phillip Wood
2018-10-26 13:32       ` Junio C Hamano
2018-10-30 10:39 ` [PATCH v3 " Phillip Wood
2018-10-30 10:39   ` [PATCH v3 1/5] am: don't die in read_author_script() Phillip Wood
2018-10-30 10:39   ` [PATCH v3 2/5] am: improve author-script error reporting Phillip Wood
2018-10-30 10:39   ` [PATCH v3 3/5] am: rename read_author_script() Phillip Wood
2018-10-30 10:39   ` [PATCH v3 4/5] add read_author_script() to libgit Phillip Wood
2018-10-30 10:39   ` [PATCH v3 5/5] sequencer: use read_author_script() Phillip Wood
2018-10-31  2:50   ` [PATCH v3 0/5] am/rebase: share read_author_script() Junio C Hamano
2018-10-31 10:14     ` Phillip Wood
2018-10-31 10:15 ` [PATCH v4 " Phillip Wood
2018-10-31 10:15   ` [PATCH v4 1/5] am: don't die in read_author_script() Phillip Wood
2018-10-31 10:15   ` [PATCH v4 2/5] am: improve author-script error reporting Phillip Wood
2018-11-04  4:12     ` Eric Sunshine
2018-11-05  1:53       ` Junio C Hamano
2018-10-31 10:15   ` [PATCH v4 3/5] am: rename read_author_script() Phillip Wood
2018-10-31 10:15   ` [PATCH v4 4/5] add read_author_script() to libgit Phillip Wood
2018-10-31 10:15   ` [PATCH v4 5/5] sequencer: use read_author_script() Phillip Wood
2018-11-01  3:01   ` [PATCH v4 0/5] am/rebase: share read_author_script() Junio C Hamano
2018-11-01  3:12     ` [PATCH v4fixed 2/5] am: improve author-script error reporting Junio C Hamano
2018-11-01  3:12       ` [PATCH v4fixed 4/5] add read_author_script() to libgit Junio C Hamano
2018-11-01 16:27     ` [PATCH v4 0/5] am/rebase: share read_author_script() Phillip Wood

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=xmqqy3amh0bs.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    --cc=phillip.wood@talktalk.net \
    --cc=sunshine@sunshineco.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).