Truncating commit messages

git, git-log, pretty-print

Solution

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"

Problem

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?

Original source