unknown
unknown

Reputation: 1893

check head mercurial python hook is not working in version 4.6.1

I am working on Hg python hook.These hooks are currently running on Hg version 2.2.1 fine but now we are planning to upgrade Hg to Higher version 4.6.1 and I found the issue with below hook on higher version with error

def chkheads(ui, repo, **kwargs):
        ui.status(_('chkheads hook\n'))
        for b in repo.branchtags():
            if len(repo.branchheads(b)) > 1:
                ui.status(_("Two heads found n branch '%s'\n" % b))
                ui.status(_('Only one head is allowed\n'))
                return 1

Error: for b in repo.branchtags():
AttributeError: 'lfilesrepo' object has no attribute 'branchtags'

Has this branchtags() method been removed from 4.6.1 hg version?If yes, is there any way to do the checkheads in hg 4.6.1 version?

Upvotes: 2

Views: 160

Answers (3)

Pierre-Yves David
Pierre-Yves David

Reputation: 396

The best way to access this kind of information is now the branchmap object.

def checkheads(ui, repo, **kwargs):
    """Only allow pushing a single head"""
    ui.status(_('checkheads hook\n'))
    branchdata = repo.branchmap()
    for b in branchdata.iterbranches():
        heads = branchdata.branchheads(b)
        if 1 < len(heads):
            ui.status(_("Two heads detected on branch '%s'\n") % b)
            ui.status(_('Only one head per branch is allowed\n'))
        return 1

Upvotes: 1

unknown
unknown

Reputation: 1893

Now if I print b then it gives me the list of all branches and the value of test variable is 0 for all closed branches whereas 1 for all opened branches

def checkheads(ui, repo, **kwargs):
    """Only allow pushing a single head"""
    ui.status(_('checkheads hook\n'))
    for b in repo.branchmap():
        print b
        test = len(repo.branchheads(b))
        print test
        if len(repo.branchheads(b)) > 1:
            ui.status(_("Two heads detected on branch '%s'\n" % b))
            ui.status(_('Only one head per branch is allowed\n'))
            return 1

Upvotes: 0

Boris Feld
Boris Feld

Reputation: 845

I think you can now the branchmap method on the repository instance. According to the source (https://www.mercurial-scm.org/repo/hg/file/4.6.1/mercurial/localrepo.py#l1038), it should returns a dictionary: {branch: [branchheads]}.

Upvotes: 1

Related Questions