`git notes append` creates additional blank line

182 Views Asked by At

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.

2

There are 2 best solutions below

0
CodeWizard On BEST ANSWER

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>"
2
VonC On

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/[email protected]/

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.