Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`git notes append` creates additional blank line

Tags:

git

git-notes

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.

like image 571
Zuku Avatar asked Oct 17 '25 11:10

Zuku


2 Answers

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>"
like image 73
CodeWizard Avatar answered Oct 19 '25 01:10

CodeWizard


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.


Also, "git notes add -m '' --allow-empty"(man)" and friends that take prepared data to create notes should not invoke an editor, but it started doing so since Git 2.42, which has been corrected with Git 2.47 (Q4 2024), batch 3.

See commit 8b426c8 (29 Jul 2024) by David Disseldorp (ddiss).
(Merged by Junio C Hamano -- gitster -- in commit 028cf22, 08 Aug 2024)

notes: do not trigger editor when adding an empty note

Signed-off-by: David Disseldorp

With "git notes add -C $blob"(man)", the given blob contents are to be made into a note without involving an editor.
But when "--allow-empty" is given, the editor is invoked, which can cause problems for non-interactive callers.

This behaviour started with 90bc19b ("notes.c: introduce '--separator=<paragraph-break>' option", 2023-05-27, Git v2.42.0-rc0 -- merge listed in batch #8), which changed editor invocation logic to check for a zero length note_data buffer.

Restore the original behaviour of git note that takes the contents given via the -m, "-C", "-F" options without invoking an editor, by checking for any prior parameter callbacks, indicated by a non-zero note_data.msg_nr.
Remove the now-unneeded note_data.given flag.

Add a test for this regression by checking whether GIT_EDITOR is invoked alongside "git notes add -C" $empty_blob --allow-empty".

like image 33
VonC Avatar answered Oct 19 '25 00:10

VonC