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

Re: [PATCH v4 5/5] notes.c: introduce "--separator" option

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Jan 15, 2023, 22:04 UTC
Message-ID
<CAPig+cSF7Fp3oM4TRU1QbiSzTeKNd1qGtqU7goPc1r-p4g8mkg@mail.gmail.com>
In-Reply-To
<230112.86y1q812y4.gmgdl@evledraar.gmail.com>

On Thu, Jan 12, 2023 at 5:07 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:

Show 20 quoted lines
> On Thu, Jan 12 2023, Teng Long wrote:
> > When appending to a given notes object and the appended note is not
> > empty too, we will insert a blank line at first which separates the
> > existing note and the appended one, which as the separator.
> > [...]
> > Signed-off-by: Teng Long <dyroneteng@gmail.com>
> > ---
> > diff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt
> > @@ -159,6 +163,16 @@ OPTIONS
> > +--separator <text>::
> > +     Specify the <text> to be inserted between existing note and appended
> > +     message, the <text> acts as a separator.
>
> Maybe let's use '<string>' or '<separator>' here instead? e.g.:
>         Specifies the <string> ...
> Maybe "<text>" just looks odd to me.
>
> More generally, let's say something like:
>         When invoking "git notes append", specify the...
> I.e. this is only for "append", but nothing here says so.
Agreed on these points.
Show 27 quoted lines
> > +     If <text> is empty (`--separator=''`), will append the message to
> > +     existing note directly without insert any separator.
> > +     If <text> is nonempty, will use as-is. One thing to notice is if
> > +     the <text> lacks newline charactor, will add the newline automatically.
> > +     If not specify this option, a blank line will be inserted as the
> > +     separator.
>
> We're spending a lot of text here on a pretty simple concept if I
> understand it correctly, I.e. just (pseudocode):
>
>         int sep_extra_nl = 0;
>         const char *sep = opt_sep ? opt_sep : "\n";
>         if (!strstr(sep, '\n'))
>                 sep_extra_nl = 1;
>         [...]
>
> Except that was written after I read your explanation, but looking at
> the code it's incorrect, it's whether the "*last*" character contains a
> newline or not.
>
> So all in all, I think we should just say:
>
>         --separator <separator>:
>                 The '<separator>' inserted between the note and message
>                 by 'append', "\n" by default. A custom separator can be
>                 provided, if it doesn't end in a "\n" one will be added
>                 implicitly .

Unfortunately, this misses the point. The original reason Teng Long started on this patch series was to be able to _suppress_ the blank line added unconditionally between notes. In the original submission, this was done via a --no-blankline option, but that met with resistance from some reviewers as being potentially confusing and too specialized. (The commit message of this patch should probably do a better job of explaining that one purpose of this change is to support the case of no-separator.)

A generalized --separator= option was suggested[1] as a possibly more palatable alternative, with which an empty string (meaning "no separator") would cover the case for which the original --no-blankline was meant to handle. So, at the very least, the documentation needs to call out the empty string as being a special case for which automatic appending of "\n" does not occur.

Show 10 quoted lines
> > diff --git a/builtin/notes.c b/builtin/notes.c
> > +static void insert_separator(struct strbuf *message)
> > +{
> > +     const char *insert;
> > +
> > +     if (!separator)
> > +             separator = "\n";
> > +     if (*separator == '\0')
>
> Style: Don't compare to 0, NULL, '\0' etc. Just use !*separator.
My fault[2]. Your suggestion is indeed more appropriate in this codebase.
Show 17 quoted lines
> > +             /* separator is empty; use as-is (no blank line) */
> > +             return;
> > +     else if (separator[strlen(separator) - 1] == '\n')
> > +             /* user supplied newline; use as-is */
> > +             insert = separator;
> > +     else
> > +             /* separator lacks newline; add it ourselves */
> > +             insert = xstrfmt("%s%s", separator,"\n");
>
> We're leaking memor here, and making it hard to fix that by conflating a
> const "insert" with this allocated version.
>
> I haven't read the whole context, but this seems really complex per the
> doc feedback above. Why can't we just keep track of if we're using the
> default value or not? I.e. just have the "--separator" option default to
> NULL, if it's not set y ou don't need to do this "\n" check, and just
> use the default, otherwise append etc.

That wouldn't work for the reason given above. The idea outlined in [2] is that an empty separator is treated specially as meaning "nothing-between-notes, not even a blank line".

Show 5 quoted lines
> > +     strbuf_insertstr(message, 0, insert);
>
> Maybe you were trying to get around using a more complex strbuf_splice()
> here, but let's just avoid teh xstrfmt() and splice() that "\n" in, if
> needed?

The code example I gave in [2] was meant to illustrate the suggested behavior as clearly as possible, not necessarily to be copied verbatim. Being able to do this without leaking memory should certainly be possible.

> Do we mean to strbuf_stripspace() here over the whole buffer, or just
> what we're appending?

That's a very good question. The note being appended might indeed have leading whitespace gunk which ought to be removed before the append operation. (Plus, it's a reasonable assumption that the existing note text has already been "stripspaced".)

[1]: https://lore.kernel.org/git/CAPig+cRcezSp4Rqt1Y9bD-FT6+7b0g9qHfbGRx65AOnw2FQXKg@mail.gmail.com/ [2]: https://lore.kernel.org/git/CAPig+cTFBVAL2gd3LqQEzS--cXqJXR+1OVerii-D6JqFvJwXqQ@mail.gmail.com/

Previous: Ævar Arnfjörð BjarmasonNext: Eric Sunshine
Message 78 of 91 in “notes.c: introduce "--no-blankline" option”
  1. 0/2 notes.c: introduce "--no-blankline" optionTeng Long, Oct 13, 2022
  2. 1/2 notes.c: introduce "--no-blankline" optionTeng Long, Oct 13, 2022
  3. Junio C HamanoOct 13, 2022
  4. Teng LongOct 17, 2022
  5. Ævar Arnfjörð BjarmasonOct 13, 2022
  6. Teng LongOct 17, 2022
  7. 2/2 notes.c: fixed tip when target and append note are both emptyTeng Long, Oct 13, 2022
  8. Ævar Arnfjörð BjarmasonOct 13, 2022
  9. Phillip WoodOct 13, 2022
  10. Ævar Arnfjörð BjarmasonOct 13, 2022
  11. Phillip WoodOct 15, 2022
  12. Teng LongOct 18, 2022
  13. Teng LongOct 18, 2022
  14. Teng LongOct 18, 2022
  15. Ævar Arnfjörð BjarmasonOct 18, 2022
  16. 0/3 notes.c: introduce "--blank-line" optionTeng Long, Nov 7, 2022
  17. 1/3 notes.c: introduce "--blank-line" optionTeng Long, Nov 7, 2022
  18. Ævar Arnfjörð BjarmasonNov 7, 2022
  19. Eric SunshineNov 7, 2022
  20. Ævar Arnfjörð BjarmasonNov 7, 2022
  21. Taylor BlauNov 7, 2022
  22. Ævar Arnfjörð BjarmasonNov 7, 2022
  23. Taylor BlauNov 8, 2022
  24. Teng LongNov 8, 2022
  25. Teng LongNov 8, 2022
  26. Ævar Arnfjörð BjarmasonNov 8, 2022
  27. Teng LongNov 9, 2022
  28. Ævar Arnfjörð BjarmasonNov 7, 2022
  29. Teng LongNov 8, 2022
  30. Taylor BlauNov 7, 2022
  31. Teng LongNov 8, 2022
  32. 2/3 notes.c: fixed tip when target and append note are both emptyTeng Long, Nov 7, 2022
  33. Ævar Arnfjörð BjarmasonNov 7, 2022
  34. Taylor BlauNov 7, 2022
  35. Ævar Arnfjörð BjarmasonNov 7, 2022
  36. Taylor BlauNov 7, 2022
  37. Teng LongNov 8, 2022
  38. 3/3 notes.c: drop unreachable code in "append_edit()"Teng Long, Nov 7, 2022
  39. Ævar Arnfjörð BjarmasonNov 7, 2022
  40. Ævar Arnfjörð BjarmasonNov 7, 2022
  41. Teng LongNov 9, 2022
  42. Teng LongNov 9, 2022
  43. 0/5 notes.c: introduce "--no-blank-line" optionTeng Long, Nov 9, 2022
  44. 1/5 notes.c: cleanup 'strbuf_grow' call in 'append_edit'Teng Long, Nov 9, 2022
  45. 2/5 notes.c: cleanup for "designated init" and "char ptr init"Teng Long, Nov 9, 2022
  46. 4/5 notes.c: provide tips when target and append note are both emptyTeng Long, Nov 9, 2022
  47. 3/5 notes.c: drop unreachable code in 'append_edit()'Teng Long, Nov 9, 2022
  48. 5/5 notes.c: introduce "--no-blank-line" optionTeng Long, Nov 9, 2022
  49. 0/5 notes.c: introduce "--no-blank-line" optionTeng Long, Nov 28, 2022
  50. Junio C HamanoNov 29, 2022
  51. Taylor BlauNov 29, 2022
  52. Teng LongNov 29, 2022
  53. Junio C HamanoNov 29, 2022
  54. Teng LongDec 15, 2022
  55. Eric SunshineDec 19, 2022
  56. Teng LongDec 21, 2022
  57. Junio C HamanoDec 21, 2022
  58. Teng LongDec 22, 2022
  59. Eric SunshineDec 23, 2022
  60. 0/5 notes.c: introduce "--separator" optioTeng Long, Jan 12, 2023
  61. 1/5 notes.c: cleanup 'strbuf_grow' call in 'append_edit'Teng Long, Jan 12, 2023
  62. Eric SunshineJan 15, 2023
  63. Teng LongJan 28, 2023
  64. 2/5 notes.c: cleanup for "designated init" and "char ptr init"Teng Long, Jan 12, 2023
  65. Ævar Arnfjörð BjarmasonJan 12, 2023
  66. Teng LongJan 28, 2023
  67. 4/5 notes.c: provide tips when target and append note are both emptyTeng Long, Jan 12, 2023
  68. Ævar Arnfjörð BjarmasonJan 12, 2023
  69. Eric SunshineJan 15, 2023
  70. 3/5 notes.c: drop unreachable code in 'append_edit()'Teng Long, Jan 12, 2023
  71. Eric SunshineJan 15, 2023
  72. Eric SunshineJan 15, 2023
  73. Teng LongJan 28, 2023
  74. Eric SunshineJan 30, 2023
  75. Teng LongFeb 1, 2023
  76. 5/5 notes.c: introduce "--separator" optionTeng Long, Jan 12, 2023
  77. Ævar Arnfjörð BjarmasonJan 12, 2023
  78. Eric SunshineJan 15, 2023
  79. Eric SunshineJan 15, 2023
  80. 0/3 notes.c: introduce "--separator" optionTeng Long, Feb 16, 2023
  81. 1/3 notes.c: cleanup 'strbuf_grow' call in 'append_edit'Teng Long, Feb 16, 2023
  82. Junio C HamanoFeb 16, 2023
  83. Teng LongFeb 20, 2023
  84. 2/3 notes.c: cleanup for "designated init"Teng Long, Feb 16, 2023
  85. Junio C HamanoFeb 16, 2023
  86. 3/3 notes.c: introduce "--separator" optionTeng Long, Feb 16, 2023
  87. Junio C HamanoFeb 16, 2023
  88. Teng LongFeb 20, 2023
  89. Junio C HamanoFeb 21, 2023
  90. Teng LongFeb 22, 2023
  91. Junio C HamanoFeb 22, 2023

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.