{git, hg} Custom Log Output

Posted on in Software

cover image for article

The standard log output for both Git and Mercurial is a bit verbose for my liking. I keep my terminal at ~50 lines, which results in only getting about 8 to 10 log entries depending on how verbose the commit was. This isn't a big deal if you are just interested in the last few commits. Anything more than that and it becomes annoying. So, I use a custom log alias ({git,hg} lg) to give me a short one-liner for each commit with the following format (please note there might be some word-wrap at play):

<commit> - <first line/short description> (relative date) <author>

diff -r 3e47ac2c5281 Slaptijack/gitconfig
--- a/Slaptijack/gitconfig      Sun Oct 25 14:38:43 2015 -0500
+++ b/Slaptijack/gitconfig      Sun Oct 25 14:39:27 2015 -0500
@@ -19,3 +19,6 @@

        default = upstream
+       lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue) <%an>%Creset' --abbr

I've been using this a long time, and unfortunately I don't remember where I got the idea, but here's a likely source.

diff -r 57a5220f4c42 Slaptijack/hgrc
--- a/Slaptijack/hgrc   Sun Oct 25 14:42:12 2015 -0500
+++ b/Slaptijack/hgrc   Sun Oct 25 14:42:14 2015 -0500
@@ -6,3 +6,23 @@

 show = log --patch --rev
+lg = log --template=lg
+mode = terminfo
+color.orange = 202
+color.lightyellow = 191
+color.darkorange = 220
+color.brightyellow = 226
+changeset.public = red
+changeset.secret = orange
+changeset.draft = brightyellow
+grep.date = green
+grep.user = blue
+list.bookmarks = orange
+lg = "{label('changeset.{phase}', node|short)} - {if(bookmarks, label('list.bookmarks', '({bookmarks}) '))}{desc|firstline} {label('grep.date', '({date|age})')} {label('grep.user', '<{author|user}>')}\n"

There's a lot going on in this change. First, the template is set in the [templates] stanza. To get the colors, we've made use of the label() function. Not all the colors we're looking for are available or set, so we have to do that in the [color] stanza. FYI, the changeset.{phase} label colors are not set by default. Finally, adding lg to the [alias] stanza is necessary. Check here for more details on customizing Mercurial.

I use Git in my daily work, but prefer Mercurial for personal projects.

UPDATE: My current position uses Mercurial instead of Git. Because of this, I have made it a goal to improve my development habits. I talked about this briefly in my goals for 2016.

One of the new things I'm trying to do is use bookmarks in my daily work. Thus, I have updated my custom log template to show the bookmarks active on a particular revision ({if(bookmarks, label('list.bookmarks', '({bookmarks}) '))}).

My Bookshelf

Reading Now

Other Stuff