mercurial/pure/mpatch.py
changeset 28587 76d7cab13f04
parent 27337 9a17576103a4
child 28588 6546afde350e
equal deleted inserted replaced
28586:82cee85d5274 28587:76d7cab13f04
    19 #
    19 #
    20 # When the fragment lists get too long, we collapse them. To do this
    20 # When the fragment lists get too long, we collapse them. To do this
    21 # efficiently, we do all our operations inside a buffer created by
    21 # efficiently, we do all our operations inside a buffer created by
    22 # mmap and simply use memmove. This avoids creating a bunch of large
    22 # mmap and simply use memmove. This avoids creating a bunch of large
    23 # temporary string buffers.
    23 # temporary string buffers.
       
    24 
       
    25 def _pull(dst, src, l): # pull l bytes from src
       
    26     while l:
       
    27         f = src.pop()
       
    28         if f[0] > l: # do we need to split?
       
    29             src.append((f[0] - l, f[1] + l))
       
    30             dst.append((l, f[1]))
       
    31             return
       
    32         dst.append(f)
       
    33         l -= f[0]
    24 
    34 
    25 def patches(a, bins):
    35 def patches(a, bins):
    26     if not bins:
    36     if not bins:
    27         return a
    37         return a
    28 
    38 
    53     # copy all the patches into our segment so we can memmove from them
    63     # copy all the patches into our segment so we can memmove from them
    54     pos = b2 + bl
    64     pos = b2 + bl
    55     m.seek(pos)
    65     m.seek(pos)
    56     for p in bins: m.write(p)
    66     for p in bins: m.write(p)
    57 
    67 
    58     def pull(dst, src, l): # pull l bytes from src
       
    59         while l:
       
    60             f = src.pop()
       
    61             if f[0] > l: # do we need to split?
       
    62                 src.append((f[0] - l, f[1] + l))
       
    63                 dst.append((l, f[1]))
       
    64                 return
       
    65             dst.append(f)
       
    66             l -= f[0]
       
    67 
       
    68     def collect(buf, list):
    68     def collect(buf, list):
    69         start = buf
    69         start = buf
    70         for l, p in reversed(list):
    70         for l, p in reversed(list):
    71             move(buf, p, l)
    71             move(buf, p, l)
    72             buf += l
    72             buf += l
    82         end = pos + plen
    82         end = pos + plen
    83         last = 0
    83         last = 0
    84         while pos < end:
    84         while pos < end:
    85             m.seek(pos)
    85             m.seek(pos)
    86             p1, p2, l = struct.unpack(">lll", m.read(12))
    86             p1, p2, l = struct.unpack(">lll", m.read(12))
    87             pull(new, frags, p1 - last) # what didn't change
    87             _pull(new, frags, p1 - last) # what didn't change
    88             pull([], frags, p2 - p1)    # what got deleted
    88             _pull([], frags, p2 - p1)    # what got deleted
    89             new.append((l, pos + 12))   # what got added
    89             new.append((l, pos + 12))   # what got added
    90             pos += l + 12
    90             pos += l + 12
    91             last = p2
    91             last = p2
    92         frags.extend(reversed(new))     # what was left at the end
    92         frags.extend(reversed(new))     # what was left at the end
    93 
    93