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

Re: [PATCH v2 1/3] pretty.c: rework describe options parsing for better extensibility

From
Eli Schwartz <eschwartz@archlinux.org>
Date
Oct 26, 2021, 20:05 UTC
Message-ID
<7bd2ef6f-9afe-2c44-db51-9307b0cd5f0f@archlinux.org>
In-Reply-To
<CAPig+cTWeN9_Z1jNLyyMsbRS4oOoyrPAWa3+JdCtsgE2B-rKFg@mail.gmail.com>
On 10/26/21 1:18 AM, Eric Sunshine wrote:
Show 49 quoted lines
> On Mon, Oct 25, 2021 at 9:36 PM Eli Schwartz <eschwartz@archlinux.org> wrote:
>> It contains option arguments only, not options. We would like to add
>> option support here too, but to do that we need to distinguish between
>> different types of options.
>>
>> Lay out the groundwork for distinguishing between bools, strings, etc.
>> and move the central logic (validating values and pushing new arguments
>> to *args) into the successful match, because that will be fairly
>> conditional on what type of argument is being parsed.
>>
>> Signed-off-by: Eli Schwartz <eschwartz@archlinux.org>
>> ---
>> diff --git a/pretty.c b/pretty.c
>> @@ -1216,28 +1216,37 @@ int format_set_trailers_options(struct process_trailer_options *opts,
>>  static size_t parse_describe_args(const char *start, struct strvec *args)
>>  {
>> +       struct {
>> +               char *name;
>> +               enum { OPT_STRING } type;
>> +       }  option[] = {
>> +               { "exclude", OPT_STRING },
>> +               { "match", OPT_STRING },
>> +       };
>>         const char *arg = start;
>>
>>         for (;;) {
>> +               int found = 0;
>>                 const char *argval;
>>                 size_t arglen = 0;
>>                 int i;
>>
>> +               for (i = 0; !found && i < ARRAY_SIZE(option); i++) {
>> +                       switch(option[i].type) {
>> +                       case OPT_STRING:
>> +                               if (match_placeholder_arg_value(arg, option[i].name, &arg,
>> +                                                               &argval, &arglen) && arglen) {
>> +                                       if (!arglen)
>> +                                               return 0;
> 
> I may be missing something obvious, but how will it be possible for:
> 
>     if (!arglen)
>         return 0;
> 
> to trigger if the `if` immediately above it:
> 
>     if (... && arglen) {
> 
>  has already asserted that `arglen` is not 0?

I don't think you are missing anything here, I simply forgot that halfway through I added a second check to the if, and later moved the code from down below.

I think returning 0 is correct here, to avoid pointlessly checking the rest of option[]. So I'll (re-)remove the first check.

Show 17 quoted lines
>> +                                       strvec_pushf(args, "--%s=%.*s", option[i].name, (int)arglen, argval);
>> +                                       found = 1;
>> +                               }
>>                                 break;
>>                         }
>>                 }
>> +               if (!found)
>>                         break;
> 
> The use of `found` to break out of a loop from within a `switch` seems
> a bit clunky. An alternative would be to `goto` a label...
> 
>>         }
>>         return arg - start;
> 
> ... which could be introduced just before the `return`. Of course,
> this is highly subjective, so not necessarily worth changing.

Keeping in mind that this for (;;) { .... break; } was there before me :D I just switched the name/type of the variable it checks...

IMO changing to goto is not my business to change (at least not in this patch), and given the "common wisdom" is "goto is evil" I'm not strongly inclined to get into the business of rewriting someone else's code for that. It's too subjective for my taste.

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User
Previous: Eric SunshineNext: Eli Schwartz
Message 14 of 46 in “Add some more options to the pretty-formats”
  1. 0/3 Add some more options to the pretty-formatsEli Schwartz, Oct 24, 2021
  2. 1/3 pretty.c: rename describe options variable to more descriptive nameEli Schwartz, Oct 24, 2021
  3. Junio C HamanoOct 24, 2021
  4. Eli SchwartzOct 24, 2021
  5. 3/3 pretty: add abbrev option to %(describe)Eli Schwartz, Oct 24, 2021
  6. Junio C HamanoOct 24, 2021
  7. Eli SchwartzOct 24, 2021
  8. 2/3 pretty: add tag option to %(describe)Eli Schwartz, Oct 24, 2021
  9. Junio C HamanoOct 24, 2021
  10. Eli SchwartzOct 24, 2021
  11. 0/3 Add some more options to the pretty-formatsEli Schwartz, Oct 26, 2021
  12. 1/3 pretty.c: rework describe options parsing for better extensibilityEli Schwartz, Oct 26, 2021
  13. Eric SunshineOct 26, 2021
  14. Eli SchwartzOct 26, 2021
  15. 2/3 pretty: add tag option to %(describe)Eli Schwartz, Oct 26, 2021
  16. Eric SunshineOct 26, 2021
  17. Eli SchwartzOct 26, 2021
  18. 3/3 pretty: add abbrev option to %(describe)Eli Schwartz, Oct 26, 2021
  19. Eric SunshineOct 26, 2021
  20. Đoàn Trần Công DanhOct 26, 2021
  21. Eric SunshineOct 26, 2021
  22. Eli SchwartzOct 26, 2021
  23. Carlo ArenasOct 27, 2021
  24. Johannes SchindelinNov 3, 2021
  25. Johannes SchindelinNov 4, 2021
  26. Eli SchwartzNov 7, 2021
  27. 0/3 Add some more options to the pretty-formatsEli Schwartz, Oct 29, 2021
  28. 2/3 pretty: add tag option to %(describe)Eli Schwartz, Oct 29, 2021
  29. Junio C HamanoOct 29, 2021
  30. Eli SchwartzOct 29, 2021
  31. Junio C HamanoOct 29, 2021
  32. Junio C HamanoOct 29, 2021
  33. Eli SchwartzOct 29, 2021
  34. 3/3 pretty: add abbrev option to %(describe)Eli Schwartz, Oct 29, 2021
  35. Eric SunshineOct 29, 2021
  36. Eli SchwartzOct 29, 2021
  37. 1/3 pretty.c: rework describe options parsing for better extensibilityEli Schwartz, Oct 29, 2021
  38. Junio C HamanoOct 29, 2021
  39. Eli SchwartzOct 29, 2021
  40. Junio C HamanoOct 29, 2021
  41. 0/3 Add some more options to the pretty-formatsEli Schwartz, Oct 31, 2021
  42. 1/3 pretty.c: rework describe options parsing for better extensibilityEli Schwartz, Oct 31, 2021
  43. 2/3 pretty: add tag option to %(describe)Eli Schwartz, Oct 31, 2021
  44. Junio C HamanoOct 31, 2021
  45. Eli SchwartzOct 31, 2021
  46. 3/3 pretty: add abbrev option to %(describe)Eli Schwartz, Oct 31, 2021

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.