Truncating commit messages
Asked Answered
O

1

10

I know that it is possible to truncate git commit messages in pretty-print with something like this:

git log --oneline --format="%h %<(70,trunc)%s %cn"

But this seems to pad the commit messages which are shorter than 70 characters with white space (so %cn will always be pushed to the right).

Is there a way to stop the commit message being padded with space if it is shorter than 70 characters?

Oistrakh answered 7/7, 2014 at 6:15 Comment(2)
Hi there. what do you try to accomplish? you want to block the option to write commit message longer then 70 characters or just to view the first 70 characters?Fastness
just view the first 70 charactersOistrakh
P
7

As per the git-log manual, ltrunc, mtrunc and trunc is only an optional argument to the %<(<N>) placeholder, which main purpose is to do the padding:

%<(<N>[,trunc|ltrunc|mtrunc]): make the next placeholder take at least N columns, padding spaces on the right if necessary. Optionally truncate at the beginning (ltrunc), the middle (mtrunc) or the end (trunc) if the output is longer than N columns. Note that truncating only works correctly with N >=2.

As of right now the git log pretty formats don't seem to have an option that just does the truncation. I think this kinda goes along with "pretty printing" being generally used to tabularize the output to be easily human-readable.

You can remove extra whitespace from git log pretty print output with some post-processing, .e.g. using sed to replace two or more adjacent spaces with one:

git log --oneline --format="%h %<(70,trunc)%s %cn" | sed -e "s/[ ]\{2,\}/ /g"
Proscription answered 7/7, 2014 at 6:59 Comment(2)
Sed will not work in this case since the white spaces are added after the commit message if its less then 70 and then the author name, so there will be a message with padding spaces and then the author name so sed will not remove the spaces in the middleFastness
@jsexpert This exact command does it for me as described on both OS X and Cygwin. May need to be adjusted for different sed flavors.Proscription

© 2022 - 2024 — McMap. All rights reserved.