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

Re: [PATCH 2/2] bisect: rewrite `check_term_format` shell function in C

From
Pranit Bauva <pranit.bauva@gmail.com>
Date
May 4, 2016, 07:36 UTC
Message-ID
<CAFZEwPNKug1pvGC1fTvZzVPBGKy71fw6S3qcx_fx98nYZasR3w@mail.gmail.com>
In-Reply-To
<CAPig+cRL7QkQHpSmeKEYECd9JQO8B29OOJoGx2AQORPfmW7QQQ@mail.gmail.com>
On Wed, May 4, 2016 at 12:22 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
Show 10 quoted lines
> On Wed, May 4, 2016 at 1:07 AM, Pranit Bauva <pranit.bauva@gmail.com> wrote:
>> This reimplements the `check_term_format` shell function in C and adds
>
> s/This reimplements/Reimplement/
> s/adds/add/
>
>> a `--check-term-format` subcommand to `git bisect--helper` to call it
>> from git-bisect.sh
>
> s/$/./
Sure.
Show 13 quoted lines
> Okay, I'll bite: Why is this a good idea? What does it buy you?
>
> It's not as if the rewrite is especially faster or more easily
> expressed in C; quite the contrary, the shell code is more concise and
> probably about equally as fast (not that execution speed matters in
> this case).
>
> I could understand this functionality being ported to C in the form of
> a static function as a minor part of porting "git bisect terms" in its
> entirety to C, but I'm not imaginative enough to see why this
> functionality is useful as a standalone git-bisect--helper subcommand,
> and the commit message doesn't enlighten. Consequently, it seems like
> unnecessary complexity.

It is important to understand that the subcommand is just a **temporary** measure. Yes, I agree that making it a subcommand increases unnecessary complexity. As a part of complete rewrite of git-bisect.sh, I am converting one function individually to C. The functionality of subcommand is useful so that I can use the existing test suite to verify whether I have done the conversion properly. As more functions get ported (which I intend to finish this summers), previously existing subcommands will be removed. For eg. After this patch, I will now convert the function write_terms(). So in that patch, I will remove the subcommand for check-term-format and instead use the check_term_format() method and then introduce a new subcommand for write_terms(). Verifying the function conversion was suggested by Stefan Beller[1] and Christian Couder[2] gave a hint of how to go about with using the existing test suite. As for the current situation, git-bisect.sh calls `--next-all` in a similar way which was the hint for me of how to go about with this project.

Show 9 quoted lines
>> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
>> ---
>> diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
>> @@ -2,16 +2,66 @@
>>  static const char * const git_bisect_helper_usage[] = {
>>         N_("git bisect--helper --next-all [--no-checkout]"),
>> +       N_("git bisect--helper --check-term-format <term> <orig_term>"),
>
> Could this be shortened to --check-term or would that be undesirable?

I guess --check-term-format would be more appropriate and as this is temporary, it wouldn't matter afterwards.

Show 18 quoted lines
>>         NULL
>>  };
>>
>>  enum sub_commands {
>> -       NEXT_ALL = 1
>> +       NEXT_ALL = 1,
>> +       CHECK_TERM_FMT
>>  };
>>
>> +/*
>> + * To check whether the string `term` belongs to the set of strings
>> + * included in the variable arguments so as to make the code look
>> + * clean and avoid having long lines in the `if` statement.
>> + */
>
> Is this a (long) sentence fragment? Code cleanliness is an obviously
> desirable trait, thus talking about it in the comment adds no value;
> it's just noise.
I will keep the initial part of the comment.
Show 11 quoted lines
>> +static int one_of(const char *term, ...)
>> +{
>> +       va_list matches;
>> +       const char *match;
>> +
>> +       va_start(matches, term);
>> +       while ((match = va_arg(matches, const char *)) != NULL)
>> +               if (!strcmp(term, match))
>> +                       return 1;
>
> Is it wise to return here without invoking va_end()?

I guess since it already checks for NULL, invoking va_end() will make it redundant[3].

Show 9 quoted lines
>> +       va_end(matches);
>> +
>> +       return 0;
>> +}
>> +
>> +static int check_term_format(const char *term, const char *orig_term,
>> +                            int flag)
>
> What is 'flag' for? The single caller only ever passes 0, so why is this needed?

Well, currently the subcommand does not use this flag but this flag is present in the method check_refname_format() so it would be better to use it. This flag might be useful in further parts of conversion since as I previously mentioned check-term-format isn't a permanent solution.

>> +{
>> +       struct strbuf new_term = STRBUF_INIT;
>
> 'new_term' is being leaked at every 'return' statement in this function.
I will have to free this memory.
Show 8 quoted lines
>
>> +       strbuf_addf(&new_term, "refs/bisect/%s", term);
>> +
>> +       if (check_refname_format(new_term.buf, flag))
>> +               die(_("'%s' is not a valid term\n"), term);
>
> Why does this die() while the other "invalid" cases merely return
> error()? What makes this special?

This is because I felt that if check_refname_format() fails then its a fatal error while in other cases, it is not as fatal.

> Also, drop "\n" from the error string.
Sure!
>> +       else if (one_of(term, "help", "start", "skip", "next", "reset",
>
> s/else //

Agree since it would be a part of the switch which is not included with the check_refname_format().

Show 18 quoted lines
>> +                       "visualize", "replay", "log", "run", NULL))
>> +               return error("can't use the builtin command '%s' as a term\n", term);
>
> This should be wrapped in _(...). Also, drop the "\n".
>
>> +       /*
>> +        * In theory, nothing prevents swapping
>> +        * completely good and bad, but this situation
>> +        * could be confusing and hasn't been tested
>> +        * enough. Forbid it for now.
>> +        */
>
> This would be a bit easier to read if re-wrapped to fit within 80
> columns rather than 53 or so.
>
>> +       else if ((one_of(term, "bad", "new", NULL) && strcmp(orig_term, "bad")) ||
>
> s/else //

In the shell script a switch was used, thus `else if` would be a more appropriate choice over `if`. Also if the first if statement fails then it is unnecessary to go further.

Show 7 quoted lines
>
>> +                (one_of(term, "good", "old", NULL) && strcmp(orig_term, "good")))
>
> This can be more efficient by doing the strcmp() before the expensive one_of():
>
>     if ((strcmp(...) && one_of(...)) ||
>         strcmp(...) && one_of(...)))
Nice. Will include this.
>> +               return error("can't change the meaning of the term '%s'\n", term);
>
> This should be wrapped in _(...). Also, drop the "\n".
Sure
Show 15 quoted lines
>
>> +       return 0;
>> +}
>> +
>>  int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
>>  {
>>         int sub_command = 0;
>> @@ -19,6 +69,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
>>         struct option options[] = {
>>                 OPT_CMDMODE(0, "next-all", &sub_command,
>>                          N_("perform 'git bisect next'"), NEXT_ALL),
>> +               OPT_CMDMODE(0, "check-term-format", &sub_command,
>> +                        N_("check format of the ref"), CHECK_TERM_FMT),
>
> What "ref"?
The ref here means that ref (like HEAD).
Show 14 quoted lines
>>                 OPT_BOOL(0, "no-checkout", &no_checkout,
>>                          N_("update BISECT_HEAD instead of checking out the current commit")),
>>                 OPT_END()
>> @@ -33,6 +85,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
>>         switch (sub_command) {
>>         case NEXT_ALL:
>>                 return bisect_next_all(prefix, no_checkout);
>> +       case CHECK_TERM_FMT:
>> +               if (argc != 2)
>> +                       die(_("--check-term-format should be followed by exactly 2 arguments."));
>
> Drop the period. Possible reword:
>
>     --check-term-format requires two arguments
Seems better
>> +               return check_term_format(argv[0], argv[1], 0);
>>         default:
>>                 die(_("bug: unknown subcommand '%d'"), sub_command);
>>         }
[1]: http://article.gmane.org/gmane.comp.version-control.git/293489
[2]: http://article.gmane.org/gmane.comp.version-control.git/293489
[3]: http://article.gmane.org/gmane.comp.version-control.git/293489
Previous: Eric SunshineNext: Pranit Bauva
Message 43 of 110 in “bisect--helper: convert a function in shell to C”
  1. bisect--helper: convert a function in shell to CPranit Bauva, Mar 21, 2016
  2. Stefan BellerMar 22, 2016
  3. Christian CouderMar 22, 2016
  4. Pranit BauvaMar 22, 2016
  5. Pranit BauvaMar 22, 2016
  6. bisect--helper: convert a function in shell to CPranit Bauva, Mar 22, 2016
  7. Johannes SchindelinMar 22, 2016
  8. Johannes SchindelinMar 22, 2016
  9. Pranit BauvaMar 22, 2016
  10. Johannes SchindelinMar 23, 2016
  11. Junio C HamanoMar 22, 2016
  12. Johannes SchindelinMar 22, 2016
  13. Pranit BauvaMar 22, 2016
  14. Stefan BellerMar 22, 2016
  15. Johannes SchindelinMar 23, 2016
  16. Pranit BauvaMar 22, 2016
  17. Johannes SchindelinMar 23, 2016
  18. Pranit BauvaMar 23, 2016
  19. bisect--helper: convert a function in shell to CPranit Bauva, Mar 23, 2016
  20. Johannes SchindelinMar 23, 2016
  21. Pranit BauvaMar 23, 2016
  22. Junio C HamanoMar 23, 2016
  23. Pranit BauvaMar 23, 2016
  24. Junio C HamanoMar 23, 2016
  25. Pranit BauvaMar 23, 2016
  26. 0/2 bisect--helper: rewrite of check_term_format()Pranit Bauva, May 4, 2016
  27. 1/2 bisect--helper: use OPT_CMDMODE instead of OPT_BOOLPranit Bauva, May 4, 2016
  28. Pranit BauvaMay 4, 2016
  29. Eric SunshineMay 4, 2016
  30. Christian CouderMay 4, 2016
  31. Johannes SchindelinMay 4, 2016
  32. Pranit BauvaMay 4, 2016
  33. Christian CouderMay 4, 2016
  34. Johannes SchindelinMay 4, 2016
  35. Christian CouderMay 4, 2016
  36. Johannes SchindelinMay 4, 2016
  37. Christian CouderMay 4, 2016
  38. Johannes SchindelinMay 8, 2016
  39. Junio C HamanoMay 4, 2016
  40. Johannes SchindelinMay 4, 2016
  41. 2/2 bisect: rewrite `check_term_format` shell function in CPranit Bauva, May 4, 2016
  42. Eric SunshineMay 4, 2016
  43. Pranit BauvaMay 4, 2016
  44. Pranit BauvaMay 4, 2016
  45. Eric SunshineMay 4, 2016
  46. Christian CouderMay 4, 2016
  47. Pranit BauvaMay 4, 2016
  48. Eric SunshineMay 4, 2016
  49. Pranit BauvaMay 4, 2016
  50. Johannes SchindelinMay 4, 2016
  51. Pranit BauvaMay 4, 2016
  52. Christian CouderMay 4, 2016
  53. Junio C HamanoMay 4, 2016
  54. Christian CouderMay 4, 2016
  55. Pranit BauvaMay 4, 2016
  56. 0/2 bisect--helper: rewrite of check-term-format()Pranit Bauva, May 6, 2016
  57. 1/2 bisect--helper: use OPT_CMDMODE instead of OPT_BOOLPranit Bauva, May 6, 2016
  58. Johannes SchindelinMay 8, 2016
  59. Pranit BauvaMay 8, 2016
  60. Christian CouderMay 8, 2016
  61. Pranit BauvaMay 8, 2016
  62. Johannes SchindelinMay 9, 2016
  63. Pranit BauvaMay 9, 2016
  64. 2/2 bisect: rewrite `check_term_format` shell function in CPranit Bauva, May 6, 2016
  65. Eric SunshineMay 7, 2016
  66. Junio C HamanoMay 6, 2016
  67. Pranit BauvaMay 7, 2016
  68. Junio C HamanoMay 8, 2016
  69. Pranit BauvaMay 8, 2016
  70. Pranit BauvaMay 8, 2016
  71. Eric SunshineMay 16, 2016
  72. Christian CouderMay 16, 2016
  73. Pranit BauvaMay 20, 2016
  74. 0/3 bisect--helper: check_term_format() & write_terms()Pranit Bauva, May 12, 2016
  75. 1/3 bisect--helper: use OPT_CMDMODE instead of OPT_BOOLPranit Bauva, May 12, 2016
  76. Eric SunshineMay 16, 2016
  77. Pranit BauvaMay 20, 2016
  78. 2/3 bisect: rewrite `check_term_format` shell function in CPranit Bauva, May 12, 2016
  79. Junio C HamanoMay 12, 2016
  80. Pranit BauvaMay 13, 2016
  81. Junio C HamanoMay 13, 2016
  82. 3/3 bisect--helper: `write_terms` shell function in CPranit Bauva, May 12, 2016
  83. Eric SunshineMay 16, 2016
  84. Johannes SchindelinMay 16, 2016
  85. Eric SunshineMay 16, 2016
  86. Johannes SchindelinMay 16, 2016
  87. Eric SunshineMay 16, 2016
  88. Pranit BauvaMay 20, 2016
  89. Johannes SchindelinMay 23, 2016
  90. Christian CouderMay 23, 2016
  91. Johannes SchindelinMay 23, 2016
  92. Pranit BauvaMay 23, 2016
  93. Pranit BauvaMay 20, 2016
  94. 0/3 bisect--helper: check_term_format() & write_terms()Pranit Bauva, May 23, 2016
  95. 1/3 bisect--helper: use OPT_CMDMODE instead of OPT_BOOLPranit Bauva, May 23, 2016
  96. 2/3 bisect: rewrite `check_term_format` shell function in CPranit Bauva, May 23, 2016
  97. 3/3 bisect--helper: `write_terms` shell function in CPranit Bauva, May 23, 2016
  98. Eric SunshineMay 23, 2016
  99. Pranit BauvaMay 23, 2016
  100. 0/3 bisect--helper: check-term-format() & write_terms()Pranit Bauva, May 24, 2016
  101. 1/3 bisect--helper: use OPT_CMDMODE instead of OPT_BOOLPranit Bauva, May 24, 2016
  102. 2/3 bisect: rewrite `check_term_format` shell function in CPranit Bauva, May 24, 2016
  103. Johannes SchindelinMay 25, 2016
  104. Pranit BauvaMay 25, 2016
  105. Lars SchneiderJun 16, 2016
  106. Pranit BauvaJun 17, 2016
  107. 3/3 bisect--helper: `write_terms` shell function in CPranit Bauva, May 24, 2016
  108. Christian CouderMay 24, 2016
  109. Junio C HamanoMay 24, 2016
  110. Pranit BauvaMay 24, 2016

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.