3

When running git log to list commits with notes, it seems like the notes end with an extra newline.

Example output with two commits where only the second one has a note:

$ git log --pretty="%ad %N %s"
<date>  <subject>
<date> <note>
<subject>

Whereas I would like the output to be:

<date>  <subject>
<date> <note> <subject>

Is it possible to trim this newline from the note in the output?

A very similar question was already asked and answered in Display Git log including Notes in oneline, but the accepted answer does include the newline that I want to get rid of from the output.

7
  • Notes are free text with any number of newline characters. They are not restricted to a single line. You seem to have only single-line notes. But this is a special case that Git is not prepared to handle, I would think. Commented Oct 25 at 11:51
  • @j6t it seems like it Commented Oct 25 at 12:21
  • stackoverflow.com/a/76123623 Commented Oct 28 at 14:23
  • 1
    Thanks @Guildenstern (it says: use %N%-C()) Commented Oct 29 at 12:51
  • Do you thnk this is a duplicate of that question? Or is there some nuance/difference I’m missing? Commented Oct 29 at 13:44

1 Answer 1

2

I think you can work around your issue by adding a special marker after the notes placeholder and then replacing any new line followed by that marker with a space.

git log --pretty="%ad %N-notes_marker-%s" \
| sed -zE "s/(\n-notes_marker-| -notes_marker-)/ /g"

Because by default sed processes the input one line at a time, the option z is necessary to treat the entire input as a single line, using null (\0) separators instead of newlines. Like so, newlines become part of the string so they can be matched and replaced.

Instead, the option E enables extended regular expression so that the parentheses are not interpreted as literal characters, providing alternation. This bit is necessary to match either a commit containing notes (\n-notes_marker-) or a "note-less" commit ( -notes_marker-).

If the command is too verbose, you could define an alias for it:

git config --local alias.note-log '!sh -c "git log --pretty=\"%ad %N-notes_marker-%s\" | sed -zE \"s/(\n-notes_marker-| -notes_marker-)/ /g\""'

and invoke it like so:

git note-log
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.