mercurial/pure/mpatch.py
changeset 49599 94a797032fc4
parent 48875 6000f5b25c9b
equal deleted inserted replaced
49598:594fc56c0af7 49599:94a797032fc4
     6 # GNU General Public License version 2 or any later version.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 
     8 
     9 import io
     9 import io
    10 import struct
    10 import struct
       
    11 
       
    12 from typing import (
       
    13     List,
       
    14     Tuple,
       
    15 )
    11 
    16 
    12 
    17 
    13 stringio = io.BytesIO
    18 stringio = io.BytesIO
    14 
    19 
    15 
    20 
    26 # efficiently, we do all our operations inside a buffer created by
    31 # efficiently, we do all our operations inside a buffer created by
    27 # mmap and simply use memmove. This avoids creating a bunch of large
    32 # mmap and simply use memmove. This avoids creating a bunch of large
    28 # temporary string buffers.
    33 # temporary string buffers.
    29 
    34 
    30 
    35 
    31 def _pull(dst, src, l):  # pull l bytes from src
    36 def _pull(
       
    37     dst: List[Tuple[int, int]], src: List[Tuple[int, int]], l: int
       
    38 ) -> None:  # pull l bytes from src
    32     while l:
    39     while l:
    33         f = src.pop()
    40         f = src.pop()
    34         if f[0] > l:  # do we need to split?
    41         if f[0] > l:  # do we need to split?
    35             src.append((f[0] - l, f[1] + l))
    42             src.append((f[0] - l, f[1] + l))
    36             dst.append((l, f[1]))
    43             dst.append((l, f[1]))
    37             return
    44             return
    38         dst.append(f)
    45         dst.append(f)
    39         l -= f[0]
    46         l -= f[0]
    40 
    47 
    41 
    48 
    42 def _move(m, dest, src, count):
    49 def _move(m: stringio, dest: int, src: int, count: int) -> None:
    43     """move count bytes from src to dest
    50     """move count bytes from src to dest
    44 
    51 
    45     The file pointer is left at the end of dest.
    52     The file pointer is left at the end of dest.
    46     """
    53     """
    47     m.seek(src)
    54     m.seek(src)
    48     buf = m.read(count)
    55     buf = m.read(count)
    49     m.seek(dest)
    56     m.seek(dest)
    50     m.write(buf)
    57     m.write(buf)
    51 
    58 
    52 
    59 
    53 def _collect(m, buf, list):
    60 def _collect(
       
    61     m: stringio, buf: int, list: List[Tuple[int, int]]
       
    62 ) -> Tuple[int, int]:
    54     start = buf
    63     start = buf
    55     for l, p in reversed(list):
    64     for l, p in reversed(list):
    56         _move(m, buf, p, l)
    65         _move(m, buf, p, l)
    57         buf += l
    66         buf += l
    58     return (buf - start, start)
    67     return (buf - start, start)
    59 
    68 
    60 
    69 
    61 def patches(a, bins):
    70 def patches(a: bytes, bins: List[bytes]) -> bytes:
    62     if not bins:
    71     if not bins:
    63         return a
    72         return a
    64 
    73 
    65     plens = [len(x) for x in bins]
    74     plens = [len(x) for x in bins]
    66     pl = sum(plens)
    75     pl = sum(plens)
   109 
   118 
   110     m.seek(t[1])
   119     m.seek(t[1])
   111     return m.read(t[0])
   120     return m.read(t[0])
   112 
   121 
   113 
   122 
   114 def patchedsize(orig, delta):
   123 def patchedsize(orig: int, delta: bytes) -> int:
   115     outlen, last, bin = 0, 0, 0
   124     outlen, last, bin = 0, 0, 0
   116     binend = len(delta)
   125     binend = len(delta)
   117     data = 12
   126     data = 12
   118 
   127 
   119     while data <= binend:
   128     while data <= binend: