Help with extension writing

Greg Ward greg-hg at gerg.ca
Tue Apr 13 16:27:36 CDT 2010


On Tue, Apr 13, 2010 at 8:07 AM, Matthew Watson <mattw.watson at gmail.com> wrote:
> I'm pretty new to Mercurial and not a python guru by any stretch, but I've
> been trying to write and extension to get some information out of a hg repo
> that we can put into an external database.

*What* information do you want precisely?  And why?

> I've started off with http://mercurial.selenic.com/wiki/MercurialApi and
> defined an extension that pulls some info out of a changectx, then calls
> commands.diff(ui, repo, rev="%s:%s" % (ctx.parent.hash(), ctx.hash()),
> git=1) and for each file we see in the output, print some extra info

1) You generally don't want to call functions in commands directly --
they're usually a bit too high-level to be convenient.  The usual
procedure is to read the source of the command that you want to use,
and figure out which lower-level code you really want.  Quite often,
the lower-level code is either a function in cmdutil.py, a function in
hg.py, a method of changectx, or a method of localrepository.

2) In this case, calling commands.diff() (or patch.diff(), the lower
level code in this case) is useful if you want the diff between two
changesets -- e.g. ctx and its first parent.  But if you want some
*other* info, like the list of files changed in a given changeset,
this is not the right way to get it.

For example, if you want the list of files changed in a particular
changeset, there's a changectx method for that:

  cctx = repo[revid]
  for file in cctx.files():
      ...

(As mentioned in section 5 of the MercurialApi wiki page.)

> What I'd really like to get hold of is the current file revision and the
> preceeding file revision in the DAG (revisions if it's a merge commit).
> I can get the current file rev by calling node.hex(filectx.filenode()), but
> I have no idea how to get the preceeding revision(s).

Why do you want file revisions?  That's a pretty arcane feature of
Mercurial -- it's not actually exposed in the UI (except via "hg
manifest --debug"), so is not useful to anyone using the command-line
interface.

And what precisely do you mean by "preceding revision(s)"?  Are you
talking about the previous revision of this one file?  Or the first
parent of the changeset in question?

> I have been calling commands.log(ui, repo, file, limit=1, follow=1,
> branch=ctx.branch) to get the changectx it is in, but this seems
> inefficient.

Yes, very.

I think you need to back up a step and tell us what you're really
trying to accomplish, rather than precisely what bits of data you are
trying to extract from Mercurial's API.  It sounds to me like you
might be going about it -- whatever it is -- in an unnecessarily
difficult or obscure way.

Greg


More information about the Mercurial-devel mailing list