What does the git documentation mean by 'unwrapped subject and body' for %B pretty print specifier
Asked Answered
G

3

5

If you search for %b and %B in the documentation, there is no explanation of the difference between them apart from the cryptic "(unwrapped subject and body)".

Here is an example commit from a repo (using git version 2.9.3) and the differing result printed from using %b vs. %B. The commit command was git commit -m 'Automated version update from Jenkins.'

$ git log -1 origin/master
commit 30ac57e...
Author: Jenkins <[email protected]>
Date:   Wed Jul 12 16:28:41 2017 +0000

    Automated version update from Jenkins.
$ git log -1 --format=%B origin/master
Automated version update from Jenkins.

$ git log -1 --format=%b origin/master

$ 

I do not understand why %b fails to produce the commit message body, nor why %B (if it contains "subject and body" in some sense) only provides the message body.

What is the underlying difference between %b and %B for pretty printing from logs?

If you want to reliably print just the most recent commit message (message only), how should you do so? I thought it should be git log -1 --format=%b origin/master but this example seems to suggest otherwise. Will %B work reliably, or does the phrase "(unwrapped subject and body)" mean it may somehow include the subject in some situations?

Generalization answered 12/7, 2017 at 17:19 Comment(0)
U
7

The commit that you're looking at doesn't have a body, so %b is correctly printing nothing. The "subject", also called the "title line" elsewhere in the same doc, is everything between the headers and the next blank line, and that's the only kind of message your commit has. The body is everything from that first blank line onwards.

Illustrating with a different commit:

$ git log -1 51e6467fdc073a9a5149b4c12ca9d79d6ac46872^!
commit 51e6467fdc073a9a5149b4c12ca9d79d6ac46872
Author: Andrew Rodland <[email protected]>
Date:   Fri Mar 14 14:22:02 2014 -0400

    Make the event data be the master that owns the socket, instead of the socket

    Then we will be able to access the master's domains and other info later
    on

$ git log --format=%B 51e6467fdc073a9a5149b4c12ca9d79d6ac46872^!
Make the event data be the master that owns the socket, instead of the socket

Then we will be able to access the master's domains and other info later
on

$ git log --format=%b 51e6467fdc073a9a5149b4c12ca9d79d6ac46872^!
Then we will be able to access the master's domains and other info later
on

"Unwrapped" simply means that the subject will have any line-wrapping removed (although convention is that it shouldn't be longer than a line anyway, git doesn't enforce that).

Unadvised answered 12/7, 2017 at 17:29 Comment(2)
I see, thanks. My mistake was assuming that for git commit -m '...' commits, the message string would be treated as body if it doesn't have any newlines, rather than subject.Generalization
@Generalization yeah, more the other way around. If there's any message at all, then there's a subject, but not necessarily a body :)Unadvised
B
1

Although you can use any format when editing commit messages, the usual form contains a subject on the first line, then optionally a blank line and body after that. In your case, you only have a single line. This means that line is the subject, printable with %s.

The effect of this convention is very visible when using --format=email. If you do not use this convention or anything which relies on it, if you just want the complete commit message, ignore %s and %b.

This is mentioned in the git commit documentation:

DISCUSSION

Though not required, it’s a good idea to begin the commit message with a single short (less than 50 character) line summarizing the change, followed by a blank line and then a more thorough description. The text up to the first blank line in a commit message is treated as the commit title, and that title is used throughout Git. For example, git-format-patch[1] turns a commit into email, and it uses the title on the Subject line and the rest of the commit in the body.

Bendicty answered 12/7, 2017 at 17:28 Comment(0)
B
1

The difference between %B and %b can be seen when the commit message have Subject and Body.

Like

git commit

Then enter commit message with Subject and Body enter image description here

git log -1 --format=%B origin/master

This provides the subject and body of the commit message

git log -1 --format=%b origin/master

This provides only the body of the commit message.

When commit message is given using the command

git commit -m "Automated version update from Jenkins."

This message "Automated version update from Jenkins." will be considered as subject and using %b will fail to produce body.

Brandes answered 12/7, 2017 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.