git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/5] am: improve author-script error reporting

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 25, 2018, 08:55 UTC
Message-ID
<xmqqy3amh0bs.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20181018100023.7327-3-phillip.wood@talktalk.net>
Phillip Wood <phillip.wood@talktalk.net> writes:
Show 7 quoted lines
> 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.

Show 15 quoted lines
> 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'"?

Show 7 quoted lines
> @@ -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.

Show 5 quoted lines
> @@ -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.

Show 14 quoted lines
> @@ -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;
Show 32 quoted lines
> +		} 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);
Previous: Phillip WoodNext: Phillip Wood
Message 15 of 41 in “am/rebase: share read_author_script()”
  1. 0/3 am/rebase: share read_author_script()Phillip Wood, Sep 12, 2018
  2. 1/3 am: rename read_author_script()Phillip Wood, Sep 12, 2018
  3. 2/3 add read_author_script() to libgitPhillip Wood, Sep 12, 2018
  4. Eric SunshineSep 13, 2018
  5. Phillip WoodOct 10, 2018
  6. Eric SunshineOct 10, 2018
  7. 3/3 sequencer: use read_author_script()Phillip Wood, Sep 12, 2018
  8. Eric SunshineSep 12, 2018
  9. Eric SunshineSep 14, 2018
  10. Phillip WoodOct 10, 2018
  11. 0/5 am/rebase: share read_author_script()Phillip Wood, Oct 18, 2018
  12. 1/5 am: don't die in read_author_script()Phillip Wood, Oct 18, 2018
  13. Junio C HamanoOct 25, 2018
  14. 2/5 am: improve author-script error reportingPhillip Wood, Oct 18, 2018
  15. Junio C HamanoOct 25, 2018
  16. 3/5 am: rename read_author_script()Phillip Wood, Oct 18, 2018
  17. 5/5 sequencer: use read_author_script()Phillip Wood, Oct 18, 2018
  18. 4/5 add read_author_script() to libgitPhillip Wood, Oct 18, 2018
  19. Junio C HamanoOct 25, 2018
  20. Phillip WoodOct 26, 2018
  21. Junio C HamanoOct 26, 2018
  22. 0/5 am/rebase: share read_author_script()Phillip Wood, Oct 30, 2018
  23. 2/5 am: improve author-script error reportingPhillip Wood, Oct 30, 2018
  24. 1/5 am: don't die in read_author_script()Phillip Wood, Oct 30, 2018
  25. 4/5 add read_author_script() to libgitPhillip Wood, Oct 30, 2018
  26. 5/5 sequencer: use read_author_script()Phillip Wood, Oct 30, 2018
  27. 3/5 am: rename read_author_script()Phillip Wood, Oct 30, 2018
  28. Junio C HamanoOct 31, 2018
  29. Phillip WoodOct 31, 2018
  30. 0/5 am/rebase: share read_author_script()Phillip Wood, Oct 31, 2018
  31. 2/5 am: improve author-script error reportingPhillip Wood, Oct 31, 2018
  32. Eric SunshineNov 4, 2018
  33. Junio C HamanoNov 5, 2018
  34. 3/5 am: rename read_author_script()Phillip Wood, Oct 31, 2018
  35. 5/5 sequencer: use read_author_script()Phillip Wood, Oct 31, 2018
  36. 4/5 add read_author_script() to libgitPhillip Wood, Oct 31, 2018
  37. 1/5 am: don't die in read_author_script()Phillip Wood, Oct 31, 2018
  38. Junio C HamanoNov 1, 2018
  39. 2/5 am: improve author-script error reportingJunio C Hamano, Nov 1, 2018
  40. 4/5 add read_author_script() to libgitJunio C Hamano, Nov 1, 2018
  41. Phillip WoodNov 1, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.