2

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?

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
unknown
  • 1,815
  • 3
  • 26
  • 51

3 Answers3

1

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]}.

Boris Feld
  • 825
  • 7
  • 8
  • @othiraldan I used the branchmap and gives me the list of all branches.Please see above in answer – unknown Aug 13 '18 at 14:41
1

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
madprog
  • 275
  • 1
  • 4
  • 13
0

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
unknown
  • 1,815
  • 3
  • 26
  • 51