How to undo hg add

If you did hg add and mistakenly added some files, then do not worry. As long as you have not committed them, this operation can be undone easily by using revert.

For example:

$ hg add foobar

To undo this:

$ hg revert foobar

Tried with: Mercurial 2.8.2 and Ubuntu 14.04

VCPrompt

Extensions like Hg-Prompt help print the version control status to the command prompt. But, it is written in Python and launching the Python interpreter on every command is too slow at the command prompt. That is the last place where you need things to be slow.

As with most performance problems in computers, the solution ends up being C/C++. VCPrompt is a tool that is written in C/C++ that can be used to print status of version control in the shell prompt. It is extremely fast due to being a binary. It can handle Mercurial, Git and Subversion repositories.

To install from PPA:

$ sudo add-apt-repository ppa:voronov84/andreyv
$ sudo apt-get update
$ sudo apt install vcprompt

To install it from source:

$ hg clone https://bitbucket.org/gward/vcprompt
$ cd vcprompt
$ autoconf
$ ./configure
$ make
$ sudo make install

To use vcprompt, specify the information you need in its format specifier:

$ vcprompt -f "%n:%r:%u%m"
hg:123:+

It can be easily incorporated into a shell prompt, by grabbing its output.

Tried with: Ubuntu 14.04

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:

[extensions]
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}}")

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 this post. If you need to view or generate traditional line graphs of your activities on a Mercurial repository, then the Activity extension might be appropriate for you.

Clone the repository of the Activity extension:

$ hg clone http://sources.freehackers.org/Hgactivity/

Add these lines to your hgrc:

[extensions]
activity=/path/to/Hgactivity/activity

That is it! To view line graphs of activities on your repository in an interactive GUI:

$ hg activity --mode=gui

You can view your activity, activities of all contributors, activities across different files and directories by choosing the various 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:

[alias]
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 and Ubuntu 12.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. Though Bitbucket does not yet have such a feature, you can display a similar chart for a Mercurial repository using the Punchcard extension.

  1. To begin, first clone the Punchcard repository in a suitable directory:
$ hg clone https://bitbucket.org/birkenfeld/hgpunchcard
  1. Modify your hgrc and add this line:
[extensions]
hgpunchcard = /path/to/hgpunchcard.py
  1. 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 12.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:
[extensions]
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:
[extdiff]
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