Truncating commit messages

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?


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, .eg 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"
链接地址: http://www.djcxy.com/p/80308.html

上一篇: 从IncomingMessage获取网址和正文?

下一篇: 截断提交消息