Search code examples
gitmercurialbranchhookpuppet

Detect a closed branch in Mercurial


I'm trying to write a post-commit hook in my Puppet repository that works like this git-based setup. I have some of the basics ready, such as detecting when a new branch is created and changing to the proper directory to do a clone (or pull if it exists) with the right branch name, but I want to add a bit that cleans up when a branch is closed. Thus, I'm trying to find out how to detect via a changeset that a branch is closed, other than relying on the commit message. Maybe I'm missing it, but I don't see anything obvious that says "this commit was to close this branch" - I could perhaps try to detect an empty changeset, but that seems like it might have false positives.


Solution

  • Here's an example of a hook which detects a closed branch:

    from mercurial import context, ui
    def run(ui, repo, node, **kwargs):
        ctx = repo[node]
        for rev in xrange(ctx.rev(), len(repo)):
            ctx = context.changectx(repo, rev)
            parent1 = ctx.parents()[0]
            if parent1 != None and parent1.extra().get('close'):
                ui.warn("Commit to closed branch!\n")
                return True
        return False
    

    The hook can run in pretxnchangegroup mode (checked when changesets added from external repo) with the following hgrc

    [hooks]
    pretxnchangegroup.closed_branch = python:/path/to/closed_branch.py:run