Hg-Prompt extension for Mercurial

You can add some Mercurial information to your shell prompt, but it can be quite a pain to do this using the commands in Mercurial. Steve Losh created the Hg-Prompt extension purely for the purpose of using it in shell prompts.

To install this extension, first clone the repository somewhere:

$ hg clone https://bitbucket.org/sjl/hg-prompt/

And add the path to the prompt.py in your hgrc file:

prompt = /path/to/hg-prompt/prompt.py

The extension comes with keywords which are listed here. For example, to get the count of incoming changes:

$ hg prompt "{No of incoming changes: {incoming|count}}"

The great thing about this is that if there is no incoming changes, none of the text is printed.

You can easily adapt Hg-Prompt into your shell. For example, in the fish shell, you can print out the incoming changes like this in its fish_prompt.fish:

echo (hg prompt "{No of in changes: {incoming|count}}")

Note: I found Hg-Prompt to be too slow for use as part of command prompt. I switched to VCPrompt.

Tried with: Mercurial 2.8.2 and Ubuntu 14.04


How to view specific revisions in Mercurial log

I typically check the Mercurial history of a repository using the log or the graphical log -G method. I sometimes find it convenient to view only the tags or heads in the same log format. This is as if the log was filtered out to show only the tags or heads log entries.

This can be done by passing the resulting list of revisions from the tag or head functions to the log option:

$ hg log -r "tag()"
$ hg log -G -r "tag()"
$ hg log -r "head()"
$ hg log -G -r "head()"

Tried with: Mercurial 2.8.2 and Ubuntu 14.04

How to use Activity extension of Mercurial

Display of activities on a Mercurial repository
Display of activities on a Mercurial repository

The Punchcard extension of Mercurial can be used to obtain a calendar chart of your contributions to a repository, as shown in here. If you need to view the classic line graph of activities in a Mercurial repository, then the Activity extension is a great solution.

  • This extension uses Matplotlib and requires a recent version. Matplotlib that ships with Ubuntu is typically ancient, so uninstall that and install the latest using pip:
$ sudo apt-get purge python-matplotlib python-matplotlib-data
$ sudo pip install --upgrade matplotlib
  • Clone the repository of the Activity extension to get the latest source code:
$ hg clone http://sources.freehackers.org/Hgactivity/
  • Add lines to your hgrc to use this extension:
  • To view line graph of activities on your repository in an interactive GUI:
$ hg activity --mode=gui

You can view commits, branches, tags and users. Activities across different files and directories can be viewed by choosing options in the GUI.

  • To generate an image file named activity.png of this line graph:
$ hg activity --mode=file
  • If you find yourself using one of these activity options a lot, you could create an alias for it by adding these lines to your hgrc:
gact = activity --mode=gui

After this, you can use the alias instead of the more verbose form:

$ hg gact

Tried with: Mercurial 2.8.2, Matplotlib 1.5.0 and Ubuntu 14.04

How to use Punchcard extension of Mercurial

Punchcard of a Mercurial repository
Punchcard of a Mercurial repository

One of the coolest feature of Github is the calendar chart that is shows for your contributions. You can get a similar chart for any Mercurial repository on your computer using the Punchcard extension.

  • First clone the Punchcard repository in a suitable directory:
$ hg clone https://bitbucket.org/birkenfeld/hgpunchcard
  • Modify your hgrc by adding these lines:
hgpunchcard = /path/to/hgpunchcard.py
  • To generate the punchcard for a repository, try this command from inside any of its directories:
$ hg punchcard

This saves a file named punchcard.png that a chart of your commits to this repository, as shown above.

Tried with: Mercurial 2.8.2 and Ubuntu 14.04

How to use ExtDiff extension of Mercurial

Mercurial is very convenient to use at the commandline. However, there are certain operations that are better with the GUI. For example, I prefer to use a GUI to go through the diff of uncommitted changes. The ExtDiff extension can be used to launch a GUI Diff tool to view the diff of a single file or across the entire repository.

  • To enable the ExtDiff extension, add this line to your hgrc:
hgext.extdiff =
  • To view the diff of a particular file using a GUI diff program, for example meld:
$ hg extdiff -p meld src/bar/foo.cpp
  • To view the diff of all uncommitted files using a GUI diff program, for example meld:
$ hg extdiff -p meld

Make sure that the GUI diff program you use has the ability to show the diff of multiple files in a directory hierarchy.

  • If you want to always launch a particular GUI diff program, then it is better to add a custom command for it. For example, to launch meld using a new command named vdiff, add this line to your hgrc:
cmd.vdiff = meld

After this, you can view the diff using the vdiff command:

$ hg vdiff src/bar/foo.cpp
$ hg vdiff

Tried with: Mercurial 2.8.2 and Ubuntu 12.04

How to use Churn extension of Mercurial

The Churn extension is useful to have on repositories shared by many authors. It shows a simple histogram of committed changes, along with author name.

  • To enable the Churn extension, add this line to your hgrc:
churn =
  • Using churn is straightforward:
$ hg churn
joe@mac.com 800 ********************************
foo@foo.com 420 *****************

Tried with: Mercurial 2.8.2 and Ubuntu 12.04

How to use Shelve extension of Mercurial

The Shelve extension of Mercurial is useful to temporarily store away your uncommitted changes and then restore them back. For example, if you are working on some changes, and before you commit it, someone comes in and asks you to test something on an earlier version. You can shelve away your changes, finish the testing and unshelve the changes and continue.

  • The Shelve extension needs Mercurial 2.8 or later. If you have an older version of Mercurial, install the latest version.

  • If your Mercurial installation does not include the Shelve extension, it can be installed from PyPI using PIP:

$ sudo pip install hgshelve
  • Enable the extension by adding this line to your hgrc:
shelve =
  • To shelve your uncommitted changes:
$ hg shelve

It will be stored using the name default. You can provide other names if you wish.

  • To unshelve these changes later:
$ hg unshelve

Tried with: Mercurial 2.8.2 and Ubuntu 12.04

How to add symbolic name for Mercurial repository

When you clone a repository in Mercurial, it creates a file .hg/hgrc in the new clone repository. Typically, this has the following lines:

default = https://joe@someserver.org/joe/foobar

That is, it has the login, the source location (directory or internet domain), the repository owner and finally the repository name. This assigns a symbolic name of default to this repository. After this any push or pull command is done with this repository.

If you created a new Mercurial repository and want to push it to another existing repository, then the command is:

$ hg push https://joe@someserver.org/joe/foobar

If you want to push or pull without having to specify the repository, then just create a .hg/hgrc file and add the above lines to it.

If you want to add other repositories to push-pull from, add them on separate lines with a different symbolic name, not default:

default = https://joe@someserver.org/joe/foobar
bkp = https://joe@bkpserver.org/joe/foobar

You can push-pull from that repository using the symbolic name you have given it:

$ hg pull bkp

Tried with: Mercurial 2.0.2 and Ubuntu 12.04 LTS

How to view files ignored by Mercurial

It is sometimes useful to see which files are being ignored in a Mercurial repository. For example, there might be some .hgignore rules that might be getting applied without your full knowledge. Such files will not be tracked or committed.

Seeing the list of ignored files in a local repository is easy:

$ hg status --ignored

Or if you prefer the short version:

$ hg st -i

Tried with: Mercurial 2.0.2 and Ubuntu 12.04 LTS