mercurial/pure/mpatch.py
changeset 28782 f736f98e16ca
parent 28589 c4c7be9f0554
child 28861 86db5cb55d46
equal deleted inserted replaced
28781:c042b98a6ff8 28782:f736f98e16ca
     9 
     9 
    10 import cStringIO
    10 import cStringIO
    11 import struct
    11 import struct
    12 
    12 
    13 StringIO = cStringIO.StringIO
    13 StringIO = cStringIO.StringIO
       
    14 
       
    15 class mpatchError(Exception):
       
    16     """error raised when a delta cannot be decoded
       
    17     """
    14 
    18 
    15 # This attempts to apply a series of patches in time proportional to
    19 # This attempts to apply a series of patches in time proportional to
    16 # the total size of the patches, rather than patches * len(text). This
    20 # the total size of the patches, rather than patches * len(text). This
    17 # means rather than shuffling strings around, we shuffle around
    21 # means rather than shuffling strings around, we shuffle around
    18 # pointers to fragments with fragment lists.
    22 # pointers to fragments with fragment lists.
    82         new = []
    86         new = []
    83         end = pos + plen
    87         end = pos + plen
    84         last = 0
    88         last = 0
    85         while pos < end:
    89         while pos < end:
    86             m.seek(pos)
    90             m.seek(pos)
    87             p1, p2, l = struct.unpack(">lll", m.read(12))
    91             try:
       
    92                 p1, p2, l = struct.unpack(">lll", m.read(12))
       
    93             except struct.error:
       
    94                 raise mpatchError("patch cannot be decoded")
    88             _pull(new, frags, p1 - last) # what didn't change
    95             _pull(new, frags, p1 - last) # what didn't change
    89             _pull([], frags, p2 - p1)    # what got deleted
    96             _pull([], frags, p2 - p1)    # what got deleted
    90             new.append((l, pos + 12))   # what got added
    97             new.append((l, pos + 12))   # what got added
    91             pos += l + 12
    98             pos += l + 12
    92             last = p2
    99             last = p2
   112         outlen += start - last
   119         outlen += start - last
   113         last = end
   120         last = end
   114         outlen += length
   121         outlen += length
   115 
   122 
   116     if bin != binend:
   123     if bin != binend:
   117         raise ValueError("patch cannot be decoded")
   124         raise mpatchError("patch cannot be decoded")
   118 
   125 
   119     outlen += orig - last
   126     outlen += orig - last
   120     return outlen
   127     return outlen