Question

I want to check the user names in a pre-commit-hook. From the command line, what I want to achieve looks like this:

hg log -r "$HG_NODE:tip" --template "{author}\n"

How do I achieve the same using the internal Mercurial API?

Was it helpful?

Solution

Presuming you've already figured out how to get a repo object, with the stable release you can do:

start = repo[node].rev()
end = repo['tip'].rev()

for r in xrange(start, end + 1):
    ctx = repo[r]
    print ctx.user()

In the development branch, you can do this:

for ctx in repo.set('%s:tip', node): # node here must be hex, use %n for binary
    print ctx.user()

Also note that 'node::tip' (two colons) might be a more useful definition of 'between': it includes all descendants of node and all ancestors of tip, rather than simply numerical ordering.

Lastly, make sure you've read all the caveats about using the internal API here:

https://www.mercurial-scm.org/wiki/MercurialApi

...and consider using python-hglib instead:

https://www.mercurial-scm.org/wiki/CommandServer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top