3

Let's say I have git commit with git note:

commit 385f6c188a5b1cef25acb6412ba4acd7c25b0b9c (HEAD -> master)
Author: zuku
Date:   Tue Oct 8 14:14:31 2019 +0200

    Test commit

Notes:
    Test note

Now I want to add some more text to this note:

git notes append -m "Next line"
git notes append -m "Another line"

The problem is that every time git notes append adds also blank line:

commit 385f6c188a5b1cef25acb6412ba4acd7c25b0b9c (HEAD -> master)
Author: zuku
Date:   Tue Oct 8 14:14:31 2019 +0200

    Test commit

Notes:
    Test note

    Next line

    Another line

I do not see a purpose of that and really would like to avoid these empty lines. I know that I can use git notes edit and enter the text manually, but I need to do that from command line without using an editor. I didn't find any useful information in docs. Any ideas how to accomplish that? Thanks.

Zuku
  • 1,030
  • 13
  • 21
  • 1
    That's how git notes are supposed to work and the purpose is the same as any paragraph break in English. If you want to edit an existing note, the idiom is to use `git commit --amend --edit`. – msanford Oct 08 '19 at 13:38
  • 1
    @msanford Why is that “how it’s supposed to work”? You can use (plaintext) notes for anything, and I don’t know why paragraphs are apparently privileged. The commit that introduced the `append` subcommand used a trailer as an example, and trailers are not separated by blank lines. – Guildenstern May 11 '23 at 13:20
  • I dug a little more. Seems that it was introduced as a mirror of `git commit -m`, which has that same paragraph behavior. – Guildenstern May 11 '23 at 13:23

2 Answers2

3

Use this tiny script

# 1 line script:
notes=$(git notes show HEAD); git notes add -f -m"${notes}<YOUR MESSAGE>"

Explanation

# Get the current note's message and store it in notes variable
# In this sample I'm using HEAD but you can use any commit you wish
notes=$(git notes show HEAD)

# Use the previous not and append the desired extra message to it
# Update the current message using the -f flag so it will overwrite the existing note
git notes add -f -m"${notes}<YOUR MESSAGE>"
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
2

TLDR; use git notes add -m foo -m bar --no-separator

Or:

git notes append --no-separator -m "Next line"
git notes append --no-separator  -m "Another line"

You can see the separator illustrated with Git 2.42 (Q3 2023): 'git notes append'(man) was taught '--separator' to specify a string to insert between paragraphs.

See commit 3d6a316, commit c4e2aa7, commit b7d87ad, commit 90bc19b, commit 5958704, commit 3d27ae0, commit ef48fcc (27 May 2023) by Teng Long (dyrone).
(Merged by Junio C Hamano -- gitster -- in commit a9cc3b8, 06 Jul 2023)

notes.c: introduce '--separator=' option

Signed-off-by: Teng Long

When adding new notes or appending to an existing notes, we will insert a blank line between the paragraphs, like:

$ git notes add -m foo -m bar
$ git notes show HEAD
foo

bar

The default behavour sometimes is not enough, the user may want to use a custom delimiter between paragraphs, like when specifying '-m', '-F', '-C', '-c' options.
So this commit introduce a new '--separator' option for 'git notes add'(man) and 'git notes append'(man), for example when executing:

$ git notes add -m foo -m bar --separator="-"
$ git notes show HEAD
foo
-
bar

a newline is added to the value given to --separator if it does not end with one already.
So when executing:

$ git notes add -m foo -m bar --separator="-"

and $ export LF=" " $ git notes add -m foo -m bar --separator="-$LF"

Both the two exections produce the same result.

The reason we use a "strbuf" array to concat but not "string_list", is that the binary file content may contain '\0' in the middle, this will cause the corrupt result if using a string to save.

git notes now includes in its man page:

Append new message(s) given by -m or -F options to an existing note, or add them as a new note if one does not exist, for the object (defaults to HEAD). When appending to an existing note, a blank line is added before each new message as an inter-paragraph separator. The separator can be customized with the --separator option.

git notes now includes in its man page:

--separator <paragraph-break>

Specify a string used as a custom inter-paragraph separator (a newline is added at the end as needed). Defaults to a blank line.

But now:

notes: introduce "--no-separator" option

Signed-off-by: Teng Long

Sometimes, the user may want to add or append multiple notes without any separator to be added between them.

Disscussion:

https://public-inbox.org/git/3f86a553-246a-4626-b1bd-bacd8148318a@app.fastmail.com/

git notes now includes in its man page:

--[no-]separator, --separator=<paragraph-break>

git notes now includes in its man page:

If --no-separator, no separators will be added between paragraphs.
Defaults to a blank line.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • As a, umm, note?, :-) `git notes add` and `git notes append` are not interchangeable, I think sticking with the `git notes append` OP used would work better here. – jthill Jul 07 '23 at 06:39
  • @jthill True, thank you for the, umm, note. I have edited the TLDR to use `git notes append`. – VonC Jul 07 '23 at 07:51