WARNING (July 2014)

This page is obsolete, as Sage is now using Git instead of Mercurial as a version control system.

There are 3 ways to modify Mercurial's behavior: writing "hooks" in a user's (or system-wide) hgrc file, writing and "extension" for hg and finally modifying hg itself. I will cover those briefly below.

Hooks

A hook is an action that gets executed before or after a command is issued. For example, to see the differences before committing changes, you could write it as a "precommit" hook:

[hooks]
# Before we commit, make sure we know what we are committing
precommit = hg diff|less

In this case, precommit says when to perform the action and hg diff|less says what to do.

To automatically merge and update your reposisory after updating it you could insert the following hook in your hgrc file:

[hooks]
# Execute hg merge and hg update after pull-ing
incoming = hg merge && hg update

Hooks have to be written in a file called hgrc located $INSTALL_ROOT/etc/mercurial/ . You can have multiple hgrc files and they all get loaded by hg. The man page for hgrc has more info if you'd like to tweak yours more: http://www.selenic.com/mercurial/hgrc.5.html

Extensions

Printing out more user-friendly error messages is requires somewhat modifying the behavior of hg and that's what extensions try to do. Extension are written in python and they are used to add new commands or modify/override existing ones. For example, here's an easy one that I've written that prints out a warning when committing fails:

def docommit(ui, repo, *pats, **opts):
  i = repo.commit(None)
      if None == i:
          print 'WARNING: Your changes have NOT been recorded!'
          print 'You must enter a message in order to record them.'

One limitation of extensions is that you depend on what functions provided by hg return. For example, the commit() function from above returns None for one other kind of failure, but you have no way of knowing what kind of failure it is.

Find more about writing your own extensions here: http://www.selenic.com/mercurial/wiki/index.cgi/WritingExtensions?action=show&redirect=ExtensionHowto

Modiying Mercurial

Modifying the source is not that hard either. In particular, $MERCURIAL/mercurial/commands.py is where you'll find all the operations (add, commit, etc) along with the sometimes cryptic error messages they output. It also helps to look at localrepos.py, if you're not sure what a command is doing.