hgext3rd/topic/discovery.py
changeset 2653 13313d0cab71
parent 2567 6eb87513024b
child 2673 1014341c637b
equal deleted inserted replaced
2652:839c2879edcc 2653:13313d0cab71
     2 
     2 
     3 import weakref
     3 import weakref
     4 
     4 
     5 from mercurial.i18n import _
     5 from mercurial.i18n import _
     6 from mercurial import (
     6 from mercurial import (
     7     branchmap,
       
     8     bundle2,
     7     bundle2,
     9     discovery,
     8     discovery,
    10     error,
     9     error,
    11     exchange,
    10     exchange,
    12     extensions,
    11     extensions,
    13     wireproto,
    12     wireproto,
    14 )
    13 )
    15 
    14 
    16 from . import topicmap
       
    17 
       
    18 def _headssummary(orig, *args):
    15 def _headssummary(orig, *args):
    19     # In mercurial < 4.2, we receive repo, remote and outgoing as arguments
    16     # In mercurial < 4.2, we receive repo, remote and outgoing as arguments
    20     if len(args) == 3:
    17     if len(args) == 3:
       
    18         pushoparg = False
    21         repo, remote, outgoing = args
    19         repo, remote, outgoing = args
    22 
    20 
    23     # In mercurial > 4.3, we receive the pushop as arguments
    21     # In mercurial > 4.3, we receive the pushop as arguments
    24     elif len(args) == 1:
    22     elif len(args) == 1:
       
    23         pushoparg = True
    25         pushop = args[0]
    24         pushop = args[0]
    26         repo = pushop.repo.unfiltered()
    25         repo = pushop.repo.unfiltered()
    27         remote = pushop.remote
    26         remote = pushop.remote
    28     else:
    27     else:
    29         msg = 'topic-ext _headssummary() takes 1 or 3 arguments (%d given)'
    28         msg = 'topic-ext _headssummary() takes 1 or 3 arguments (%d given)'
    31 
    30 
    32     publishing = ('phases' not in remote.listkeys('namespaces')
    31     publishing = ('phases' not in remote.listkeys('namespaces')
    33                   or bool(remote.listkeys('phases').get('publishing', False)))
    32                   or bool(remote.listkeys('phases').get('publishing', False)))
    34     if publishing or not remote.capable('topics'):
    33     if publishing or not remote.capable('topics'):
    35         return orig(*args)
    34         return orig(*args)
       
    35 
       
    36     class repocls(repo.__class__):
       
    37         def __getitem__(self, key):
       
    38             ctx = super(repocls, self).__getitem__(key)
       
    39             oldbranch = ctx.branch
       
    40 
       
    41             def branch():
       
    42                 branch = oldbranch()
       
    43                 topic = ctx.topic()
       
    44                 if topic:
       
    45                     branch = "%s:%s" % (branch, topic)
       
    46                 return branch
       
    47 
       
    48             ctx.branch = branch
       
    49             return ctx
       
    50 
    36     oldrepo = repo.__class__
    51     oldrepo = repo.__class__
    37     oldbranchcache = branchmap.branchcache
       
    38     oldfilename = branchmap._filename
       
    39     try:
    52     try:
    40         class repocls(repo.__class__):
       
    41             def __getitem__(self, key):
       
    42                 ctx = super(repocls, self).__getitem__(key)
       
    43                 oldbranch = ctx.branch
       
    44 
       
    45                 def branch():
       
    46                     branch = oldbranch()
       
    47                     topic = ctx.topic()
       
    48                     if topic:
       
    49                         branch = "%s:%s" % (branch, topic)
       
    50                     return branch
       
    51 
       
    52                 ctx.branch = branch
       
    53                 return ctx
       
    54 
       
    55         repo.__class__ = repocls
    53         repo.__class__ = repocls
    56         branchmap.branchcache = topicmap.topiccache
    54         unxx = repo.filtered('unfiltered-topic')
    57         branchmap._filename = topicmap._filename
    55         repo.unfiltered = lambda: unxx
    58         summary = orig(*args)
    56         if pushoparg:
       
    57             try:
       
    58                 pushop.repo = repo
       
    59                 summary = orig(pushop)
       
    60             finally:
       
    61                 pushop.repo = repo
       
    62         else:
       
    63             summary = orig(repo, remote, outgoing)
    59         for key, value in summary.iteritems():
    64         for key, value in summary.iteritems():
    60             if ':' in key: # This is a topic
    65             if ':' in key: # This is a topic
    61                 if value[0] is None and value[1]:
    66                 if value[0] is None and value[1]:
    62                     summary[key] = ([value[1].pop(0)], ) + value[1:]
    67                     summary[key] = ([value[1].pop(0)], ) + value[1:]
    63         return summary
    68         return summary
    64     finally:
    69     finally:
       
    70         if 'unfiltered' in vars(repo):
       
    71             del repo.unfiltered
    65         repo.__class__ = oldrepo
    72         repo.__class__ = oldrepo
    66         branchmap.branchcache = oldbranchcache
       
    67         branchmap._filename = oldfilename
       
    68 
    73 
    69 def wireprotobranchmap(orig, repo, proto):
    74 def wireprotobranchmap(orig, repo, proto):
    70     oldrepo = repo.__class__
    75     oldrepo = repo.__class__
    71     try:
    76     try:
    72         class repocls(repo.__class__):
    77         class repocls(repo.__class__):