Mercurial tips and tricks

Setting your name and email

You've probably noticed by now that Mercurial issues a warning about "No username found" when you commit. And you may have also noticed that the email address Mercurial uses to label your commits isn't correct; it's something like "carljm@dj.goshen.edu" or "carljm@un004-imac25.goshen.edu" if you commit from a lab Mac.

Some of you may also have caught the bit in Chapter 2 of the Definitive Guide to Mercurial where he lists a number of options for fixing this. On dj.goshen.edu or on your own computer, I recommend solution #3 in his list, which is to create a .hgrc file in your home directory with the following contents:

[ui]
username = Carl Meyer <carljm@goshen.edu>

The only place where this solution is insufficient is on the lab Macs, where your home directory isn't persistent. If you're using hg on those machines (directly, not sshed in to dj.goshen.edu), you should use option 2 instead:

$ export HGUSER="Carl Meyer <carljm@goshen.edu>"

Unlike the .hgrc file solution, this command you have to run each time you open a shell.

Making a Mercurial working tree update itself automatically

As I mentioned in an email last week, if you have a Mercurial repo that exists for the sole purpose of showing changes to the outside world, and you don't want to have to manually run hg update every time you hg push to it, you can configure it to automatically update its working tree whenever the repo receives a new group of changesets:

You do this by creating a file hgrc inside the .hg/ directory of your repo (emacs .hg/hgrc) with the following contents:

[hooks]
changegroup = hg update

Whenever this repo gets new changesets pushed or pulled into it, it will automatically run "hg update" right afterwards. This only affects the one repo where you do this; it isn't a change that will "come along" with a new clone of that repo. Generally this isn't something you'd want to do on a repo where you do a lot of active editing (because you might leave uncommitted changes laying around in it, which could cause conflicts when "hg update" tries to bring your working directory up to date).

Note the difference between ~/.hgrc (where we put the username option) and .hg/hgrc inside a specific repo (where we put this changegroup hook). One is a "global" Mercurial config for your user, the other only applies to the single repo. Both actually accept exactly the same set of options (of which there are many many more than just these two); though some options (like changegroup hooks) make more sense for one repo, and some (like username) make more sense at the user level.