Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git log cut long comments?

Tags:

git

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.
like image 203
Marcus Ahlberg Avatar asked Feb 17 '14 13:02

Marcus Ahlberg


People also ask

How do I limit a git log?

The most basic filtering option for git log is to limit the number of commits that are displayed. When you're only interested in the last few commits, this saves you the trouble of viewing all the commits in a page. You can limit git log 's output by including the - option.

How do I view the full git log?

The git log command shows a list of all the commits made to a repository. You can see the hash of each Git commit , the message associated with each commit, and more metadata. This command is useful for displaying the history of a repository.

What is git log -- Oneline?

Git Log OnelineThe oneline option is used to display the output as one commit per line. It also shows the output in brief like the first seven characters of the commit SHA and the commit message. It will be used as follows: $ git log --oneline.


4 Answers

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:\src\msysgit>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
like image 163
patthoyts Avatar answered Oct 22 '22 17:10

patthoyts


Late to the party, but these options will do it too:

$ git config --global core.pager 'less -S'

or (e.g.)

$ echo $LESS
-R
$ export LESS=-RS
like image 38
Edward Falk Avatar answered Oct 22 '22 16:10

Edward Falk


Truncate and pad the commit message only

As per other answers, the format placeholders %<(50,trunc)%s will print the commit message truncated at 50 characters. But that will also pad shorter values to the same, and there's no way to tell it not to.

If that suits you, then you're done. If not, another approach is needed.

Truncate the entire line at terminal width

Also per other answers, you could configure less -S as the core.pager option globally or per-repository. This will trim the entire log string at the terminal width, avoiding wrapped lines.

But it will do that to all Git commands! (At least all the ones that produce paged output).

Improvement - truncate only for that specific command

You can do this with the -c option, e.g. git -c core.pager='less -S' log --graph --oneline

Even better, set this up as an alias so you don't have to type it every time:

git config --global alias.graph "-c core.pager='less -S' \
 log --graph --oneline`

Combining both

You can also combine this with formatting placeholders. Here's an example using the --graph flag, where the commit message is also padded/truncated to 50 characters, but since the --graph option creates a variable-width drawing of the commit graph, you need to combine both approaches. And you don't want to be typing this out every time:

git config --global alias.graph "-c core.pager='less -S' \
log --pretty='tformat:%C(bold cyan)%h %C(blue)%<(10,trunc)%aN \
%<(50,trunc)%C(white)%s %C(auto)%d %C(dim green)%ar' --graph"
like image 8
Andrew Spencer Avatar answered Oct 22 '22 16:10

Andrew Spencer


(I seem completely unable to format a comment appropriately so have posted this as an answer, but really is a comment to @patthoyts's response.)

What's lovely about trunc is that it pads, so you can use it like so:
git log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse
to produce an easier (at least for my eyes) overview.

$ git log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse
2015-06-15 initial commit 5099ede 2015-06-16 Layout - Responsive grid added. 6534242 2015-06-17 HTML - H1 / Title updated <title>Testpage</title.. 88ea464 2015-06-18 Updating the Hotfix changes a8fbc47

Tip - add an alias of, say, trunc to make it easy on yourself.
git config --global alias.trunc 'log --pretty=format:"%ad %<(50,trunc)%s %h" --date=short --reverse'

like image 2
liamvictor Avatar answered Oct 22 '22 17:10

liamvictor