Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

How to make git log cut long comments?

I have a git log alias that prints each commit as a single line. Since some people write far too long one-liners in the commit log, many commits wrap to a new line. How can I format the git log output to cut the comment after 50 characters?

I found this in the git-log man page but it will only pad short comments, not cut long ones.

%<(<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.
question from:https://stackoverflow.com/questions/21830810/how-to-make-git-log-cut-long-comments

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is not that clear in the documentation just which characters are needed but the following example cuts the subject line to 50 characters:

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

The format specification is %< and the arguments for that need to be in parentheses. In this case, 50 chars and truncate the excess.

For instance, performing this on the msysGit repository yields:

C:srcmsysgit>git log -n 5 --format="%h [%<(12,trunc)%aN] [%<(12,trunc)%cN] %<(50,trunc)%s"

218ed04 [Sebastian ..] [Sebastian ..] Merge pull request #154 from csware/tortoisegitp..
8a920b9 [Sven Stric..] [Sven Stric..] Installer: Detect TortoiseGitPlink from Tortoise..
448e125 [dscho       ] [dscho       ] Merge pull request #152 from csware/syscommand
db8d1bf [Sven Stric..] [Sven Stric..] Perl readline creates empty sys$command files if..
753d3d6 [Johannes S..] [Johannes S..] Git for Windows 1.8.5.2-preview20131230

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...