Mercurial features an extension mechanism for adding new commands. It allows you to create new features and use them directly from the main hg command line.

Using the hgk extension

The contrib directory includes an extension to add support for a port of gitk under Mercurial. This is named hgk.py, and will be used as an example here.

To load an extension, you add it to your .hgrc file. You can specify an absolute path:

[extensions]
hgk=/usr/local/lib/hgk.py

Mercurial can also scan the default python library path for a file named 'hgit':

[extensions]
hgk=

<!> In version 0.7, this extension is named "hgit" (without the .py) and does not provide the view command. You must invoke hgk directly.

hg help will now show the new commands provided by the hgk extension:

$ hg help status      show changed files in the working directory
...
 tag         add a tag for the current tip or a given revision
 tags        list repository tags
 tip         show the tip revision
 unbundle    apply a changegroup file
 undo        undo the last commit or pull
 update      update or merge working directory
 verify      verify the integrity of the repository
 version     output version and copyright information
 view        start interactive history viewer
$ hg view

Writing your own extension

To write your own extension, your python module needs to provide a dict with entries describing each command, and a callback named reposetup. reposetup is called after the main Mercurial repository initialization, and can be used to setup any local state the extension might need. Below is an example extension to help demonstrate how things work:

   1 #!/usr/bin/env python
   2 
   3 from mercurial import hg
   4 
   5 # every command must take a ui and and repo as arguments.
   6 # opts is a dict where you can find other command line flags
   7 #
   8 # Other parameters are taken in order from items on the command line that
   9 # don't start with a dash.  If no default value is given in the parameter list,
  10 # they are required.
  11 def print_parents(ui, repo, node, **opts):
  12     # The doc string below will show up in hg help
  13     """Print parent information"""
  14 
  15     # repo.lookup can lookup based on tags, an sha1, or a revision number
  16     node = repo.lookup(node)
  17     parents = repo.changelog.parents(node)
  18 
  19     if opts['short']:
  20         # hg.short will return a smaller portion of the sha1
  21         print "short %s %s" % (hg.short(parents[0]), hg.short(parents[1]))
  22     elif opts['long']:
  23         # hg.hex will return the full sha1 
  24         print "long %s %s" % (hg.hex(parents[0]), hg.hex(parents[1]))
  25     else:
  26         print "default %s %s" % (hg.short(parents[0]), hg.short(parents[1]))
  27 
  28 cmdtable = {
  29     # cmd name        function call
  30     "print-parents": (print_parents, 
  31                      # see mercurial/fancyopts.py for all of the command
  32                      # flag options.
  33                      [('s', 'short', None, 'print short form'),
  34                       ('l', 'long', None, 'print long form')],
  35                      "hg print-parents [options] node")
  36 }
  37 
  38 def reposetup(ui, repo):
  39     pass
  40     # extension specific setup can go here