I am trying to develop a format string to pass to git log --pretty
so that each log entry ends in a full commit message, yet each log entry is separated by exactly one empty line. The problem is that some full commit messages end in a newline, and some do not.
For example, let's say I have two commits, abc1234
and def5678
, but only abc1234
contains a newline at the end of the full commit message. Outputting the raw commit contents on the command line would look something like this:
[prompt]$ git cat-file commit abc1234
(...)
Title FOO
Full commit message FOO
[prompt]$ git cat-file commit def5678
(...)
Title BAR
Full commit message BAR[prompt]$
Note how the new shell prompt appears at the end of the last line of output, demonstrating that commit def5678
does not contain a newline at the end of the full commit message.
Let's say that def5678
is the parent of abc1234
and I want to output a simple log where each entry contains only the short commit hash, title line, and full commit message. I might try something like this:
[prompt]$ git log --graph --pretty='commit %h%n%B' abc1234
* commit abc1234
| Title FOO
|
| Full commit message FOO
|
* commit def5678
| Title BAR
|
| Full commit message BAR
* commit <parent of def5678>
(...)
Note the spacing between the log entries. The entries for abc1234
and def5678
are separated by a blank line (save for the graph character), yet the entries for def5678
and its parent are not.
How can I construct a format string so that the spacing is consistent, even with inconsistent termination of full commit messages? The builtin pretty formats of medium
, full
, fuller
, and email
already do that, but I want to be able to construct arbitrary format strings to do the same thing.
I've experimented with the %+B
, %-B
and % B
sequences (and their %b
and %n
equivalents), but I just can't seem to get consistent spacing.
I'm using Git 2.17.0 if that makes a difference.
git
directly or through some alternate implementation (that is, one that does not uselibgit2
)? IIRC,git
automatically fixes messages with missing EOLs, but some reimplementations do not. – Frankfortfilter-branch
to fix the commits, but then my local repo would be incapable of tracking upstream without grafts or some other cumbersome mechanism. – Scornfulgit log --graph --pretty='commit %h%n%B%-gd%n'
, you're not using reflog selectors so%gd
will expand empty and the-
will eat all preceding newlines. – Tannicgit log --graph --pretty='commit %h%n%B%-C()%n'
(%C()
being an empty color selector). Seems pretty kludgy but it works! – Scornful