mercurial/diffhelpers.py
changeset 37884 86e7a57449fa
parent 37787 92213f6745ed
parent 37835 1f65d7d46545
child 37885 e0f30c91dfd8
equal deleted inserted replaced
37787:92213f6745ed 37884:86e7a57449fa
     1 # diffhelpers.py - helper routines for patch
       
     2 #
       
     3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
       
     4 #
       
     5 # This software may be used and distributed according to the terms of the
       
     6 # GNU General Public License version 2 or any later version.
       
     7 
       
     8 from __future__ import absolute_import
       
     9 
       
    10 from .i18n import _
       
    11 
       
    12 from . import (
       
    13     error,
       
    14 )
       
    15 
       
    16 def addlines(fp, hunk, lena, lenb, a, b):
       
    17     """Read lines from fp into the hunk
       
    18 
       
    19     The hunk is parsed into two arrays, a and b. a gets the old state of
       
    20     the text, b gets the new state. The control char from the hunk is saved
       
    21     when inserting into a, but not b (for performance while deleting files.)
       
    22     """
       
    23     while True:
       
    24         todoa = lena - len(a)
       
    25         todob = lenb - len(b)
       
    26         num = max(todoa, todob)
       
    27         if num == 0:
       
    28             break
       
    29         for i in xrange(num):
       
    30             s = fp.readline()
       
    31             if not s:
       
    32                 raise error.ParseError(_('incomplete hunk'))
       
    33             if s == "\\ No newline at end of file\n":
       
    34                 fixnewline(hunk, a, b)
       
    35                 continue
       
    36             if s == '\n' or s == '\r\n':
       
    37                 # Some patches may be missing the control char
       
    38                 # on empty lines. Supply a leading space.
       
    39                 s = ' ' + s
       
    40             hunk.append(s)
       
    41             if s.startswith('+'):
       
    42                 b.append(s[1:])
       
    43             elif s.startswith('-'):
       
    44                 a.append(s)
       
    45             else:
       
    46                 b.append(s[1:])
       
    47                 a.append(s)
       
    48 
       
    49 def fixnewline(hunk, a, b):
       
    50     """Fix up the last lines of a and b when the patch has no newline at EOF"""
       
    51     l = hunk[-1]
       
    52     # tolerate CRLF in last line
       
    53     if l.endswith('\r\n'):
       
    54         hline = l[:-2]
       
    55     else:
       
    56         hline = l[:-1]
       
    57 
       
    58     if hline.startswith((' ', '+')):
       
    59         b[-1] = hline[1:]
       
    60     if hline.startswith((' ', '-')):
       
    61         a[-1] = hline
       
    62     hunk[-1] = hline
       
    63 
       
    64 def testhunk(a, b, bstart):
       
    65     """Compare the lines in a with the lines in b
       
    66 
       
    67     a is assumed to have a control char at the start of each line, this char
       
    68     is ignored in the compare.
       
    69     """
       
    70     alen = len(a)
       
    71     blen = len(b)
       
    72     if alen > blen - bstart:
       
    73         return False
       
    74     for i in xrange(alen):
       
    75         if a[i][1:] != b[i + bstart]:
       
    76             return False
       
    77     return True