Collect new branches in pretxnchangegroup hook

Wujek Srujek wujek.srujek at gmail.com
Tue Sep 25 22:23:35 UTC 2012


Hi. I would like to write a hook that performs some action for every new
branch that comes in. Here is what I have to collect new branch names:

def branchhook(ui, repo, hooktype, node, **kwargs):
    if hooktype not in ['pretxnchangegroup', 'pretxncommit']:
        ui.write('Hook should be pretxncommit/pretxnchangegroup not "%s".'
% hooktype)
        return 1

    # print "================================"
    changegroup = set(repo[rev].node() for rev in xrange(repo[node],
len(repo)))
    incomingbranches = set(repo.branches(changegroup))
    newbranches  = set()
    for current, root, p1, p2 in incomingbranches:
        # print 'branch:', repo[current].branch(), 'current:',
repo[current].rev(), 'root:', repo[root].rev()
        branch = repo[current].branch()
        if branch == 'default':
            continue
        branchstart = [c for c in repo[root].children() if c.branch() ==
branch]
        # for c in branchstart:
        #    print '\tchild:', c.rev(), 'branch:', c.branch()
        if all(map(lambda c: c.node() in changegroup, branchstart)):
            newbranches.add(branch)
        #else:
        #    print 'nope'
        #print '-----'
    print newbranches
    print "================================"
    return 1

The idea is this:
1. for all incoming changesets I get the root changeset on the branch (with
repo.branches)
2. the root is not the first commit on the branch, it's a branch parent, I
retrieve all root's children that are on the same branch as currently
processed changeset (that should probably be only one commit, unless there
are multiple branches with the same name on the same root...)
3. if all such children (first commits on the branch) are in the
changegroup set, this means the branch is new in this changegroup and I add
it to the newbranches set

This seems to work, but my question is - is there something simpler than
that? I spent some time looking around and googling, and either I am doing
it wrong, or there is nothing easily found. The task is simple enough -
collect all new branches. Any suggestions?

wujek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial/attachments/20120926/86d80b93/attachment-0002.html>


More information about the Mercurial mailing list