mercurial/mdiff.py
branchstable
changeset 49366 288de6f5d724
parent 49284 d44e3c45f0e4
child 49897 a78dfb1ad60e
equal deleted inserted replaced
49364:e8ea403b1c46 49366:288de6f5d724
     3 # Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
     3 # Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
     4 #
     4 #
     5 # This software may be used and distributed according to the terms of the
     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.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 from __future__ import absolute_import
       
     9 
     8 
    10 import re
     9 import re
    11 import struct
    10 import struct
    12 import zlib
    11 import zlib
    13 
    12 
    36 textdiff = bdiff.bdiff
    35 textdiff = bdiff.bdiff
    37 splitnewlines = bdiff.splitnewlines
    36 splitnewlines = bdiff.splitnewlines
    38 
    37 
    39 
    38 
    40 # TODO: this looks like it could be an attrs, which might help pytype
    39 # TODO: this looks like it could be an attrs, which might help pytype
    41 class diffopts(object):
    40 class diffopts:
    42     """context is the number of context lines
    41     """context is the number of context lines
    43     text treats all files as text
    42     text treats all files as text
    44     showfunc enables diff -p output
    43     showfunc enables diff -p output
    45     git enables the git extended patch format
    44     git enables the git extended patch format
    46     nodates removes dates from diff headers
    45     nodates removes dates from diff headers
   377         if opts.showfunc:
   376         if opts.showfunc:
   378             lastpos, func = lastfunc
   377             lastpos, func = lastfunc
   379             # walk backwards from the start of the context up to the start of
   378             # walk backwards from the start of the context up to the start of
   380             # the previous hunk context until we find a line starting with an
   379             # the previous hunk context until we find a line starting with an
   381             # alphanumeric char.
   380             # alphanumeric char.
   382             for i in pycompat.xrange(astart - 1, lastpos - 1, -1):
   381             for i in range(astart - 1, lastpos - 1, -1):
   383                 if l1[i][0:1].isalnum():
   382                 if l1[i][0:1].isalnum():
   384                     func = b' ' + l1[i].rstrip()
   383                     func = b' ' + l1[i].rstrip()
   385                     # split long function name if ASCII. otherwise we have no
   384                     # split long function name if ASCII. otherwise we have no
   386                     # idea where the multi-byte boundary is, so just leave it.
   385                     # idea where the multi-byte boundary is, so just leave it.
   387                     if encoding.isasciistr(func):
   386                     if encoding.isasciistr(func):
   401 
   400 
   402         hunkrange = astart, alen, bstart, blen
   401         hunkrange = astart, alen, bstart, blen
   403         hunklines = (
   402         hunklines = (
   404             [b"@@ -%d,%d +%d,%d @@%s\n" % (hunkrange + (func,))]
   403             [b"@@ -%d,%d +%d,%d @@%s\n" % (hunkrange + (func,))]
   405             + delta
   404             + delta
   406             + [b' ' + l1[x] for x in pycompat.xrange(a2, aend)]
   405             + [b' ' + l1[x] for x in range(a2, aend)]
   407         )
   406         )
   408         # If either file ends without a newline and the last line of
   407         # If either file ends without a newline and the last line of
   409         # that file is part of a hunk, a marker is printed. If the
   408         # that file is part of a hunk, a marker is printed. If the
   410         # last line of both files is identical and neither ends in
   409         # last line of both files is identical and neither ends in
   411         # a newline, print only one marker. That's the only case in
   410         # a newline, print only one marker. That's the only case in
   412         # which the hunk can end in a shared line without a newline.
   411         # which the hunk can end in a shared line without a newline.
   413         skip = False
   412         skip = False
   414         if not t1.endswith(b'\n') and astart + alen == len(l1) + 1:
   413         if not t1.endswith(b'\n') and astart + alen == len(l1) + 1:
   415             for i in pycompat.xrange(len(hunklines) - 1, -1, -1):
   414             for i in range(len(hunklines) - 1, -1, -1):
   416                 if hunklines[i].startswith((b'-', b' ')):
   415                 if hunklines[i].startswith((b'-', b' ')):
   417                     if hunklines[i].startswith(b' '):
   416                     if hunklines[i].startswith(b' '):
   418                         skip = True
   417                         skip = True
   419                     hunklines[i] += b'\n'
   418                     hunklines[i] += b'\n'
   420                     hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER)
   419                     hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER)
   421                     break
   420                     break
   422         if not skip and not t2.endswith(b'\n') and bstart + blen == len(l2) + 1:
   421         if not skip and not t2.endswith(b'\n') and bstart + blen == len(l2) + 1:
   423             for i in pycompat.xrange(len(hunklines) - 1, -1, -1):
   422             for i in range(len(hunklines) - 1, -1, -1):
   424                 if hunklines[i].startswith(b'+'):
   423                 if hunklines[i].startswith(b'+'):
   425                     hunklines[i] += b'\n'
   424                     hunklines[i] += b'\n'
   426                     hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER)
   425                     hunklines.insert(i + 1, diffhelper.MISSING_NEWLINE_MARKER)
   427                     break
   426                     break
   428         yield hunkrange, hunklines
   427         yield hunkrange, hunklines