blog

Mercurial: Persona extension

# August 27, 2011

I’ve recently switched (back) from Git to Mercurial. This post isn’t intended to be a recapitulation of pros and cons; both are great tools. Instead I’d like to mention a problem that came up in my workflow and a small extension written to address it.

Here’s the problem: I use Mercurial for both personal and work projects—and different usernames for each. This situation may well be familiar to some:

  1. You save your work (personal) username in the ui section of $HOME/.hgrc
  2. For personal (work) repositories you edit the .hg/hgrc and specify a local username
  3. You often forget to do (2) and end up committing changesets with the wrong username

The persona extension is designed to make this process slightly less of a headache: It provides an easy way to set ui.username on a repository by repository basis.

At its simplest, the extension lets you specify a username for a repository as follows:

# hg persona -n "Firstname Lastname <firstname@some.domain>"

To save repetition, you can configure individual personas in your ~/.hgrc:

[persona]
home        = Firstname Lastname <firstname@home.domain>
work        = Firstname Lastname <lastname@work.domain>

which allows for quick switching in a given repository:

# hg persona -n home
~ Setting username to 'Firstname Lastname <firstname@home.domain>'

# hg persona -n work
~ Setting username to 'Firstname Lastname <lastname@work.domain>'

Finally, you can specify the --persona option to clone:

# hg clone --persona work src dest

To use this extension, clone http://hg.0branch.com/hg-persona and add the following to your ~/.hgrc:

[extensions]
persona = /path/to/persona.py

Feedback appreciated!

Update: The following hook is also handy for displaying ui.username info on each commit (allowing you to catch repository-username mismatches early):

[hooks]
# ...
pre-commit = echo "(as '`hg showconfig ui.username`')"

comments powered by Disqus