Mercurial scripting with python Mercurial scripting with python python python

Mercurial scripting with python


It's true there's no official API, but you can get an idea about best practices by reading other extensions, particularly those bundled with hg. For this particular problem, I would do something like this:

from mercurial import ui, hgfrom mercurial.node import hexrepo = hg.repository('/path/to/repo/root', ui.ui())fctx = repo.filectx('/path/to/file', 'tip')hexnode = hex(fctx.node())

Update At some point the parameter order changed, now it's like this:

   repo = hg.repository(ui.ui(), '/path/to/repo/root' )


Do you mean this documentation?
Note that, as stated in that page, there is no official API, because they still reserve the right to change it at any time. But you can see the list of changes in the last few versions, it is not very extensive.


An updated, cleaner subprocess version (uses .check_output(), added in Python 2.7/3.1) that I use in my Django settings file for a crude end-to-end deployment check (I dump it into an HTML comment):

import subprocessHG_REV = subprocess.check_output(['hg', 'id', '--id']).strip()

You could wrap it in a try if you don't want some strange hiccup to prevent startup:

try:    HG_REV = subprocess.check_output(['hg', 'id', '--id']).strip()except OSError:    HG_REV = "? (Couldn't find hg)"except subprocess.CalledProcessError as e:    HG_REV = "? (Error {})".format(e.returncode)except Exception:  # don't use bare 'except', mis-catches Ctrl-C and more    # should never have to deal with a hangup     HG_REV = "???"