hgext3rd/topic/discovery.py
branchstable
changeset 5353 f32d4c7f8fb7
parent 5276 8431bb224862
equal deleted inserted replaced
5298:a828c7a7ace1 5353:f32d4c7f8fb7
    15 from . import (
    15 from . import (
    16     common,
    16     common,
    17     compat,
    17     compat,
    18 )
    18 )
    19 
    19 
    20 # hg <= 4.5 (b4d85bc122bd)
    20 from mercurial import wireprotov1server
    21 try:
       
    22     from mercurial import wireproto  # pytype: disable=import-error
       
    23     wireproto.branchmap
       
    24 except (AttributeError, ImportError):
       
    25     from mercurial import wireprotov1server as wireproto
       
    26 
    21 
    27 def _headssummary(orig, pushop, *args, **kwargs):
    22 def _headssummary(orig, pushop, *args, **kwargs):
    28     # In mercurial > 4.3, we receive the pushop as arguments
       
    29     repo = pushop.repo.unfiltered()
    23     repo = pushop.repo.unfiltered()
    30     remote = pushop.remote
    24     remote = pushop.remote
    31 
    25 
    32     publishing = (b'phases' not in remote.listkeys(b'namespaces')
    26     publishing = (b'phases' not in remote.listkeys(b'namespaces')
    33                   or bool(remote.listkeys(b'phases').get(b'publishing', False)))
    27                   or bool(remote.listkeys(b'phases').get(b'publishing', False)))
   203     tr = op.gettransaction()
   197     tr = op.gettransaction()
   204     if tr.hookargs[b'source'] not in (b'push', b'serve'): # not a push
   198     if tr.hookargs[b'source'] not in (b'push', b'serve'): # not a push
   205         return
   199         return
   206     tr._prepushheads = _nbheads(op.repo)
   200     tr._prepushheads = _nbheads(op.repo)
   207     reporef = weakref.ref(op.repo)
   201     reporef = weakref.ref(op.repo)
   208     if util.safehasattr(tr, 'validator'): # hg <= 4.7
   202     if util.safehasattr(tr, 'validator'): # hg <= 4.7 (ebbba3ba3f66)
   209         oldvalidator = tr.validator
   203         oldvalidator = tr.validator
   210     else:
   204     elif util.safehasattr(tr, '_validator'):
       
   205         # hg <= 5.3 (36f08ae87ef6)
   211         oldvalidator = tr._validator
   206         oldvalidator = tr._validator
   212 
   207 
   213     def validator(tr):
   208     def _validate(tr):
   214         repo = reporef()
   209         repo = reporef()
   215         if repo is not None:
   210         if repo is not None:
   216             repo.invalidatecaches()
   211             repo.invalidatecaches()
   217             finalheads = _nbheads(repo)
   212             finalheads = _nbheads(repo)
   218             for branch, oldnb in tr._prepushheads.items():
   213             for branch, oldnb in tr._prepushheads.items():
   223             for branch, newnb in finalheads.items():
   218             for branch, newnb in finalheads.items():
   224                 if 1 < newnb:
   219                 if 1 < newnb:
   225                     msg = _(b'push create more than 1 head on new branch "%s"'
   220                     msg = _(b'push create more than 1 head on new branch "%s"'
   226                             % branch)
   221                             % branch)
   227                     raise error.Abort(msg)
   222                     raise error.Abort(msg)
       
   223 
       
   224     def validator(tr):
       
   225         _validate(tr)
   228         return oldvalidator(tr)
   226         return oldvalidator(tr)
   229     if util.safehasattr(tr, 'validator'): # hg <= 4.7
   227 
       
   228     if util.safehasattr(tr, 'validator'): # hg <= 4.7 (ebbba3ba3f66)
   230         tr.validator = validator
   229         tr.validator = validator
       
   230     elif util.safehasattr(tr, '_validator'):
       
   231         # hg <= 5.3 (36f08ae87ef6)
       
   232         tr._validator = validator
   231     else:
   233     else:
   232         tr._validator = validator
   234         tr.addvalidator(b'000-new-head-check', _validate)
       
   235 
   233 handlecheckheads.params = frozenset()
   236 handlecheckheads.params = frozenset()
   234 
   237 
   235 def _pushb2phases(orig, pushop, bundler):
   238 def _pushb2phases(orig, pushop, bundler):
   236     if common.hastopicext(pushop.repo):
   239     if common.hastopicext(pushop.repo):
   237         checktypes = (b'check:heads', b'check:updated-heads')
   240         checktypes = (b'check:heads', b'check:updated-heads')
   247     return caps
   250     return caps
   248 
   251 
   249 def modsetup(ui):
   252 def modsetup(ui):
   250     """run at uisetup time to install all destinations wrapping"""
   253     """run at uisetup time to install all destinations wrapping"""
   251     extensions.wrapfunction(discovery, '_headssummary', _headssummary)
   254     extensions.wrapfunction(discovery, '_headssummary', _headssummary)
   252     extensions.wrapfunction(wireproto, 'branchmap', wireprotobranchmap)
   255     extensions.wrapfunction(wireprotov1server, 'branchmap', wireprotobranchmap)
   253     extensions.wrapfunction(wireproto, '_capabilities', wireprotocaps)
   256     extensions.wrapfunction(wireprotov1server, '_capabilities', wireprotocaps)
   254     # we need a proper wrap b2 part stuff
   257     # we need a proper wrap b2 part stuff
   255     extensions.wrapfunction(bundle2, 'handlecheckheads', handlecheckheads)
   258     extensions.wrapfunction(bundle2, 'handlecheckheads', handlecheckheads)
   256     bundle2.handlecheckheads.params = frozenset()
   259     bundle2.handlecheckheads.params = frozenset()
   257     bundle2.parthandlermapping[b'check:heads'] = bundle2.handlecheckheads
   260     bundle2.parthandlermapping[b'check:heads'] = bundle2.handlecheckheads
   258     if util.safehasattr(bundle2, 'handlecheckupdatedheads'):
   261     if util.safehasattr(bundle2, 'handlecheckupdatedheads'):