1843200
  8   -ˆl–äY>”JeJe@†à¸Û*bÑ¿�þZlO,Ü¡p¬ˆ$‘¦�üÿÞ„®�¯ÝÂÈÛ ?÷     -# mdiff.py - diff and patch routines for mercurial
#
# Copyright 2005, 2006 Olivia Mackall <olivia@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.


import re
import struct
import zlib

from .i18n import _
from .pycompat import (
    getattr,
    setattr,
)
from . import (
    diffhelper,
    encoding,
    error,
    policy,
    pycompat,
    util,
)
from .utils import dateutil

bdiff = policy.importmod('bdiff')
mpatch = policy.importmod('mpatch')

blocks = bdiff.blocks
fixws = bdiff.fixws
patches = mpatch.patches
patchedsize = mpatch.patchedsize
textdiff = bdiff.bdiff
splitnewlines = bdiff.splitnewlines


# TODO: this looks like it could be an attrs, which might help pytype
class diffopts:
    """context is the number of context lines
    text treats all files as text
    showfunc enables diff -p output
    git enables the git extended patch format
    nodates removes dates from diff headers
    nobinary ignores binary files
    noprefix disables the 'a/' and 'b/' prefixes (ignored in plain mode)
    ignorews ignores all whitespace changes in the diff
    ignorewsamount ignores changes in the amount of whitespace
    ignoreblanklines ignores changes whose lines are all blank
    upgrade generates git diffs to avoid data loss
    """

    _HAS_DYNAMIC_ATTRIBUTES = True

    defaults = {
        b'context': 3,
        b'text': False,
        b'showfunc': False,
        b'git': False,
        b'nodates': False,
        b'nobinary': False,
        b'noprefix': False,
        b'index': 0,
        b'ignorews': False,
        b'ignorewsamount': False,
        b'ignorewseol': False,
        b'ignoreblanklines': False,
        b'upgrade': False,
        b'showsimilarity': False,
        b'worddiff': False,
        b'xdiff': False,
    }

    def __init__(self, **opts):
        opts = pycompat.byteskwargs(opts)
        for k in self.defaults.keys():
            v = opts.get(k)
            if v is None:
                v = self.defaults[k]
            setattr(self, k, v)

        try:
            self.context = int(self.context)
        except ValueError:
            raise error.InputError(
                _(b'diff context lines count must be an integer, not %r')
                % pycompat.bytestr(self.context)
            )

    def copy(self, **kwargs):
        opts = {k: getattr(self, k) for k in self.defaults}
        opts = pycompat.strkwargs(opts)
        opts.update(kwargs)
        return diffopts(**opts)


defaultopts = diffopts()


def wsclean(opts, text, blank=True):
    if opts.ignorews:
        text = bdiff.fixws(text, 1)
    elif opts.ignorewsamount:
        text = bdiff.fixws(text, 0)
    if blank and opts.ignoreblanklines:
        text = re.sub(b'\n+', b'\n', text).strip(b'\n')
    if opts.ignorewseol:
        text = re.sub(br'[ \t\r\f]+\n', br'\n', text)
    return text


def splitblock(base1, lines1, base2, lines2, opts):
    # The input lines matches except for interwoven blank lines. We
    # transform it into a sequence of matching blocks and blank blocks.
    lines1 = [(wsclean(opts, l) and 1 or 0) for l in lines1]
    lines2 = [(wsclean(opts, l) and 1 or 0) for l in lines2]
    s1, e1 = 0, len(lines1)
    s2, e2 = 0, len(lines2)
    while s1 < e1 or s2 < e2:
        i1, i2, btype = s1, s2, b'='
        if i1 >= e1 or lines1[i1] == 0 or i2 >= e2 or lines2[i2] == 0:
            # Consume the block of blank lines
            btype = b'~'
            while i1 < e1 and lines1[i1] == 0:
                i1 += 1
            while i2 < e2 and lines2[i2] == 0:
                i2 += 1
        else:
            # Consume the matching lines
            while i1 < e1 and lines1[i1] == 1 and lines2[i2] == 1:
                i1 += 1
                i2 += 1
        yield [base1 + s1, base1 + i1, base2 + s2, base2 + i2], btype
        s1 = i1
        s2 = i2


def hunkinrange(hunk, linerange):
    """Return True if `hunk` defined as (start, lengt