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

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

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 5, 2018, 01:53 UTC
Message-ID
<xmqqy3a8jnmw.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<CAPig+cRBwkKGtzY6CwZiKb1mef0a-9F+O24d0R8ZfsRb5ssUbA@mail.gmail.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 5 quoted lines
> Junio labeled the "-2" trick "cute", and while it is optimal in that
> it only traverses the key/value list once, it also increases cognitive
> load since the reader has to spend a good deal more brain cycles
> understanding what is going on than would be the case with simpler
> (and less noisily repetitive) code.

... and update three copies of very similar looking code that have to stay similar. If this were parsing unbounded and customizable set of variables, I think the approach you suggest to use a helper that iterates over the whole array for each key makes sense, but for now I think what was queued would be OK.

Show 39 quoted lines
> An alternative would be to make the code trivially easy to understand,
> though a bit more costly, but that expense should be negligible since
> this file should always be tiny, containing very few key/value pairs,
> and it's not timing critical code anyhow. For instance:
>
> static char *find(struct string_list *kv, const char *key)
> {
>     const char *val = NULL;
>     int i;
>     for (i = 0; i < kv.nr; i++) {
>         if (!strcmp(kv.items[i].string, key)) {
>             if (val) {
>                 error(_("duplicate %s"), key);
>                 return NULL;
>             }
>             val = kv.items[i].util;
>         }
>     }
>     if (!val)
>         error(_("missing %s"), key);
>     return val;
> }
>
> static int read_author_script(struct am_state *state)
> {
>     ...
>     char *name, *email, *date;
>     ...
>     name = find(&kv, "GIT_AUTHOR_NAME");
>     email = find(&kv, "GIT_AUTHOR_EMAIL");
>     date = find(&kv, "GIT_AUTHOR_DATE");
>     if (name && email && date) {
>         state->author_name = name;
>         state->author_email = email;
>         state->author_date = date;
>         retval = 0;
>     }
>     string_list_clear&kv, !!retval);
>     ...
Previous: Eric SunshineNext: Phillip Wood
Message 33 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.