Shortlog for Fun and Profit

posted by Steve Losh on October 7, 2009

Many Mercurial commands that print out some output are customizable through the --template option. For an in-depth look at what you can do with templates take a look at hg help templating.

For this tip, we’re going to use a very small template that will turn out to be very powerful.

A Shortlog Template

Let’s look at a very simple example of template, used with hg log:

hg log --template '{rev}:{node|short} {desc|firstline}\n'

This command will print out a log of all the changesets in the repository, one per line, with each line having the revision number, hash identifier, and summary. It should look something like this:

81:966ff708eb3a Add the Shortcuts for Specifying Revisions beginner tip.
80:3dca217e76a2 Change the title to something better.
79:4edca6026448 Change the title to something more description.
78:bea31254eba5 Add the Graphlog beginner tip.
77:5ef6bf0c5081 Add scrollbars to code blocks if needed.
76:dd1fa17eaa1f Reduce the number of tips on the front page to 4 per category.
75:f3d5a6d9d5a5 Fix the nested <p> tag issue.
74:e6a76edf8032 Add the Creating Repositories Over SSH advanced tip.
73:a4f1c617f01f Reword "N.B." to "Note" because it looks better before the colon.
72:e44939482007 Mention the feature requirement for HTTP User Auth.
... a lot more lines ...

This can be handy if you want to see what’s happened but you don’t need a lot of information. Go ahead and add an alias for this command by editing your ~/.hgrc file to contain:

[alias]
slog = log --template '{rev}:{node|short} {desc|firstline}\n'

Now you can use hg slog to output this very compact log. We’re not quite done yet though.

Counting

There’s something very special about this template: every log entry takes up one line. That means we can use the UNIX wc (wordcount) utility to count the number of lines, and therefore the number of changesets.

For example, we can count the number of changesets in the repository:

$ hg slog | wc -l
  82

That’s fairly useless because we can just look at the revision number of the newest changeset. Where it starts to get interesting is when we use hg log‘s filtering options to select the changesets we want to count.

To count the number of merges:

hg slog --only-merges | wc -l

To count the number of non-merges:

hg slog --no-merges | wc -l

To count the number of commits ‘Steve’ made:

hg slog --user Steve | wc -l

To count the number of commits in the past three days:

hg slog --date -3 | wc -l

To count the number of commits since February 1:

hg slog --date '>Feb 1' | wc -l

To count the number of non-merge commits Steve made since March:

hg slog --no-merges --user Steve --date '>Mar 1' | wc -l

Using a template like this (one changeset per line) makes it easy to count anything you can filter with hg log. Check out hg help log for more ways to filter.

Adding an hg shortlog alias will let you do a lot of nifty things.