python to api vs. command line (was RE: comparing dates in a mercurial hook)

Joel B. Mohler joel at
Fri Oct 14 14:58:07 CDT 2011

On Friday, October 14, 2011 03:03:14 pm Matt Mackall wrote:
> On Fri, 2011-10-14 at 18:41 +0000, Haszlakiewicz, Eric wrote:
> > > -----Original Message-----
> > > From: Matt Mackall [mailto:mpm at]
> > > 
> > > Why does everyone want to write hooks in Python?
> > > 
> > > On the command line, you can do this with something like:
> > >  hg id -r "$SOMENODE and not date('-180')"
> > > 
> > > and get a non-zero return code if the set is empty.
> > 
> > I have pre-existing hookthat needs to work on both unix and windows.
> > They do more than just the date check (env variable check, regexp
> > check of the description, works for both commit and push, etc...), and
> > I thought the best practice for a portable hook was to use python.
> Well it's portable, sure. But definitely not completely documented and
> not guaranteed to work from release to release. So.. you can either
> write Unix and Windows hooks in shell scripts once or write in Python
> and take the hit every time you upgrade, I guess.
> Another approach is to treat Python as your 'portable shell' and talk to
> Mercurial vs os.system().

This sounds nice, but it would be a lot more viable (or at least enjoyable) if 
there was a better way to parse out change-sets.  The thing that obviously 
occurs to me is to use json and I thought that certainly I wasn't the only 
person to want that.  It's true that I wasn't as witnessed by .  However, 
that e-mail thread appeared to end in frustration and so I wrote my code in 
python accessing the changectx because I'd much rather do that than parse text 
logs.  Perhaps that was simply short-sighted.

So, is there a sensible way to get a dump of user, date, full commit 
description, files in the commit, etc through std-in/out?  Here "sensible" 
should be understood to mean that I don't feel like spending an hour parsing 
text (and likely finding that I didn't cover all the corner cases).

In pleasant contrast to using std-in/out, the mercurial api exposes lots of 
these goodies using great pythonic idioms with lots of pleasant iterations 
over list(like) objects.  To me, that's a pretty big advantage over the 
command line.

I haven't looked into it in detail, but I have gotten the impression that this 
is the big news of the command server (to me, at least).


More information about the Mercurial-devel mailing list