Truncate commit messages

I know that you can trim git commit messages in pretty-printed form with something like this:

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

But this seems to impose commit messages that are shorter than 70 characters with a space (so %cn will always be pressed right).

Is there a way to stop a commit message with a space if it is less than 70 characters?

+7
git git-log pretty-print
source share
1 answer

According to git -log manual , ltrunc , mtrunc and trunc is an additional argument for %<(<N>) placeholder, the main purpose of which is to populate:

%<(<N>[,trunc|ltrunc|mtrunc]) : make the next placeholder at least N columns, fill in the spaces if necessary on the right. It is not necessary to trim at the beginning (ltrunc), middle (mtrunc) or end (trunc) if the output is longer than N columns. Note that truncation only works with N> = 2.

At the moment, the git log pretty formats do not seem to have an option that just truncates. I think this looks like a β€œfairly printed” one, usually used to render text in tabular form by an easily readable person.

You can remove the extra spaces from git log fairly printed result with some further processing, for example. 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" 
+4
source share

All Articles