hgext/pushexperiment.py
changeset 727 cb907cf3b556
child 728 5d368ae3d5a0
equal deleted inserted replaced
726:06cd220141ba 727:cb907cf3b556
       
     1 """Small extension altering some push behavior
       
     2 
       
     3 - Add a new wire protocol command to exchange obsolescence marker. Sending the
       
     4   raw file as a binary instead of using pushkey hack.
       
     5 
       
     6 """
       
     7 
       
     8 import errno
       
     9 from StringIO import StringIO
       
    10 
       
    11 from mercurial.i18n import _
       
    12 from mercurial import extensions
       
    13 from mercurial import wireproto
       
    14 from mercurial import obsolete
       
    15 
       
    16 
       
    17 def client_pushobsmarkers(self, obsfile):
       
    18     """wireprotocol peer method"""
       
    19     self.requirecap('_push_experiment_pushobsmarkers_0',
       
    20                     _('push obsolete markers faster'))
       
    21     ret, output = self._callpush('push_experiment_pushobsmarkers_0', obsfile)
       
    22     for l in output.splitlines(True):
       
    23         self.ui.status(_('remote: '), l)
       
    24     return ret
       
    25 
       
    26 
       
    27 def srv_pushobsmarkers(repo, proto):
       
    28     """wireprotocol command"""
       
    29     fp = StringIO()
       
    30     proto.redirect()
       
    31     proto.getfile(fp)
       
    32     data = fp.getvalue()
       
    33     fp.close()
       
    34     lock = repo.lock()
       
    35     try:
       
    36         tr = repo.transaction('pushkey: obsolete markers')
       
    37         try:
       
    38             repo.obsstore.mergemarkers(tr, data)
       
    39             tr.close()
       
    40         finally:
       
    41             tr.release()
       
    42     finally:
       
    43         lock.release()
       
    44     return wireproto.pushres(0)
       
    45 
       
    46 
       
    47 def syncpush(orig, repo, remote):
       
    48     """wraper for obsolete.syncpush to use the fast way if possible"""
       
    49     if not (obsolete._enabled and repo.obsstore):
       
    50         return
       
    51     if remote.capable('_push_experiment_pushobsmarkers_0'):
       
    52         try:
       
    53             obsfp = repo.sopener('obsstore')
       
    54         except IOError as e:
       
    55             if e.errno != errno.ENOENT:
       
    56                 raise
       
    57             return
       
    58         remote.push_experiment_pushobsmarkers_0(obsfp)
       
    59         return
       
    60     return orig(repo, remote)
       
    61 
       
    62 
       
    63 def capabilities(orig, repo, proto):
       
    64     """wrapper to advertise new capability"""
       
    65     caps = orig(repo, proto)
       
    66     if obsolete._enabled:
       
    67         caps += ' _push_experiment_pushobsmarkers_0'
       
    68     return caps
       
    69 
       
    70 
       
    71 def extsetup(ui):
       
    72     wireproto.wirepeer.push_experiment_pushobsmarkers_0 = client_pushobsmarkers
       
    73     wireproto.commands['push_experiment_pushobsmarkers_0'] = (srv_pushobsmarkers, '')
       
    74     extensions.wrapfunction(wireproto, 'capabilities', capabilities)
       
    75     extensions.wrapfunction(obsolete, 'syncpush', syncpush)
       
    76 
       
    77