patch.py
3262 lines
| 101.6 KiB
| text/x-python
|
PythonLexer
/ mercurial / patch.py
|
r2861 | # patch.py - patch file parsing routines | ||
# | ||||
|
r2865 | # Copyright 2006 Brendan Cully <brendan@kublai.com> | ||
|
r4897 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> | ||
|
r2865 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2861 | |||
|
r34139 | from __future__ import absolute_import, print_function | ||
|
r27485 | |||
|
r25113 | import collections | ||
|
r37639 | import contextlib | ||
|
r27485 | import copy | ||
import errno | ||||
import os | ||||
import re | ||||
import shutil | ||||
import zlib | ||||
|
r10965 | |||
|
r27485 | from .i18n import _ | ||
from .node import ( | ||||
hex, | ||||
|
r47771 | sha1nodeconstants, | ||
|
r27485 | short, | ||
) | ||||
|
r43355 | from .pycompat import open | ||
|
r27485 | from . import ( | ||
copies, | ||||
|
r37821 | diffhelper, | ||
|
r38607 | diffutil, | ||
|
r27485 | encoding, | ||
error, | ||||
|
r28341 | mail, | ||
|
r27485 | mdiff, | ||
pathutil, | ||||
|
r30924 | pycompat, | ||
|
r27485 | scmutil, | ||
|
r30807 | similar, | ||
|
r27485 | util, | ||
|
r31233 | vfs as vfsmod, | ||
|
r27485 | ) | ||
|
r37102 | from .utils import ( | ||
dateutil, | ||||
|
r44517 | hashutil, | ||
|
r37138 | procutil, | ||
|
r37102 | stringutil, | ||
) | ||||
|
r32370 | |||
|
r28861 | stringio = util.stringio | ||
|
r2866 | |||
|
r31630 | gitre = re.compile(br'diff --git a/(.*) b/(.*)') | ||
tabsplitter = re.compile(br'(\t+|[^\t]+)') | ||||
|
r43346 | wordsplitter = re.compile( | ||
|
r43387 | br'(\t+| +|[a-zA-Z0-9_\x80-\xff]+|[^ \ta-zA-Z0-9_\x80-\xff])' | ||
|
r43346 | ) | ||
|
r7199 | |||
|
r34252 | PatchError = error.PatchError | ||
|
r2933 | |||
# public functions | ||||
|
r43346 | |||
|
r10384 | def split(stream): | ||
'''return an iterator of individual patches from a stream''' | ||||
|
r43346 | |||
|
r10384 | def isheader(line, inheader): | ||
|
r43347 | if inheader and line.startswith((b' ', b'\t')): | ||
|
r10384 | # continuation | ||
return True | ||||
|
r43347 | if line.startswith((b' ', b'-', b'+')): | ||
|
r10883 | # diff line - don't check for header pattern in there | ||
return False | ||||
|
r43347 | l = line.split(b': ', 1) | ||
return len(l) == 2 and b' ' not in l[0] | ||||
|
r10384 | |||
def chunk(lines): | ||||
|
r43347 | return stringio(b''.join(lines)) | ||
|
r10384 | |||
def hgsplit(stream, cur): | ||||
inheader = True | ||||
for line in stream: | ||||
if not line.strip(): | ||||
inheader = False | ||||
|
r43347 | if not inheader and line.startswith(b'# HG changeset patch'): | ||
|
r10384 | yield chunk(cur) | ||
cur = [] | ||||
inheader = True | ||||
cur.append(line) | ||||
if cur: | ||||
yield chunk(cur) | ||||
def mboxsplit(stream, cur): | ||||
for line in stream: | ||||
|
r43347 | if line.startswith(b'From '): | ||
|
r10384 | for c in split(chunk(cur[1:])): | ||
yield c | ||||
cur = [] | ||||
cur.append(line) | ||||
if cur: | ||||
for c in split(chunk(cur[1:])): | ||||
yield c | ||||
def mimesplit(stream, cur): | ||||
def msgfp(m): | ||||
|
r28861 | fp = stringio() | ||
|
r43426 | g = mail.Generator(fp, mangle_from_=False) | ||
|
r10384 | g.flatten(m) | ||
fp.seek(0) | ||||
return fp | ||||
for line in stream: | ||||
cur.append(line) | ||||
c = chunk(cur) | ||||
|
r38354 | m = mail.parse(c) | ||
|
r10384 | if not m.is_multipart(): | ||
yield msgfp(m) | ||||
else: | ||||
|
r43347 | ok_types = (b'text/plain', b'text/x-diff', b'text/x-patch') | ||
|
r10384 | for part in m.walk(): | ||
ct = part.get_content_type() | ||||
if ct not in ok_types: | ||||
continue | ||||
yield msgfp(part) | ||||
def headersplit(stream, cur): | ||||
inheader = False | ||||
for line in stream: | ||||
if not inheader and isheader(line, inheader): | ||||
yield chunk(cur) | ||||
cur = [] | ||||
inheader = True | ||||
if inheader and not isheader(line, inheader): | ||||
inheader = False | ||||
cur.append(line) | ||||
if cur: | ||||
yield chunk(cur) | ||||
def remainder(cur): | ||||
yield chunk(cur) | ||||
class fiter(object): | ||||
def __init__(self, fp): | ||||
self.fp = fp | ||||
def __iter__(self): | ||||
return self | ||||
def next(self): | ||||
l = self.fp.readline() | ||||
if not l: | ||||
raise StopIteration | ||||
return l | ||||
|
r35192 | __next__ = next | ||
|
r10384 | inheader = False | ||
cur = [] | ||||
|
r43347 | mimeheaders = [b'content-type'] | ||
if not util.safehasattr(stream, b'next'): | ||||
|
r10384 | # http responses, for example, have readline but not next | ||
stream = fiter(stream) | ||||
for line in stream: | ||||
cur.append(line) | ||||
|
r43347 | if line.startswith(b'# HG changeset patch'): | ||
|
r10384 | return hgsplit(stream, cur) | ||
|
r43347 | elif line.startswith(b'From '): | ||
|
r10384 | return mboxsplit(stream, cur) | ||
elif isheader(line, inheader): | ||||
inheader = True | ||||
|
r43347 | if line.split(b':', 1)[0].lower() in mimeheaders: | ||
|
r10384 | # let email parser handle this | ||
return mimesplit(stream, cur) | ||||
|
r43347 | elif line.startswith(b'--- ') and inheader: | ||
|
r10501 | # No evil headers seen by diff start, split by hand | ||
|
r10384 | return headersplit(stream, cur) | ||
# Not enough info, keep reading | ||||
# if we are here, we have a very plain patch | ||||
return remainder(cur) | ||||
|
r43346 | |||
|
r26557 | ## Some facility for extensible patch parsing: | ||
# list of pairs ("header to match", "data key") | ||||
|
r43346 | patchheadermap = [ | ||
|
r43347 | (b'Date', b'date'), | ||
(b'Branch', b'branch'), | ||||
(b'Node ID', b'nodeid'), | ||||
|
r43346 | ] | ||
|
r26557 | |||
|
r37639 | @contextlib.contextmanager | ||
|
r2866 | def extract(ui, fileobj): | ||
|
r46554 | """extract patch from data read from fileobj. | ||
|
r2866 | |||
|
r4263 | patch can be a normal patch or contained in an email message. | ||
|
r2866 | |||
|
r26781 | return a dictionary. Standard keys are: | ||
|
r26547 | - filename, | ||
- message, | ||||
- user, | ||||
- date, | ||||
- branch, | ||||
- node, | ||||
- p1, | ||||
- p2. | ||||
|
r26781 | Any item can be missing from the dictionary. If filename is missing, | ||
|
r46554 | fileobj did not contain a patch. Caller must unlink filename when done.""" | ||
|
r2866 | |||
|
r43347 | fd, tmpname = pycompat.mkstemp(prefix=b'hg-patch-') | ||
|
r43906 | tmpfp = os.fdopen(fd, 'wb') | ||
|
r37639 | try: | ||
yield _extract(ui, fileobj, tmpname, tmpfp) | ||||
finally: | ||||
tmpfp.close() | ||||
os.unlink(tmpname) | ||||
|
r43346 | |||
|
r37639 | def _extract(ui, fileobj, tmpname, tmpfp): | ||
|
r2866 | # attempt to detect the start of a patch | ||
# (this heuristic is borrowed from quilt) | ||||
|
r43346 | diffre = re.compile( | ||
br'^(?:Index:[ \t]|diff[ \t]-|RCS file: |' | ||||
br'retrieving revision [0-9]+(\.[0-9]+)*$|' | ||||
br'---[ \t].*?^\+\+\+[ \t]|' | ||||
br'\*\*\*[ \t].*?^---[ \t])', | ||||
re.MULTILINE | re.DOTALL, | ||||
) | ||||
|
r2866 | |||
|
r26547 | data = {} | ||
|
r37639 | |||
|
r38354 | msg = mail.parse(fileobj) | ||
|
r2866 | |||
|
r43906 | subject = msg['Subject'] and mail.headdecode(msg['Subject']) | ||
data[b'user'] = msg['From'] and mail.headdecode(msg['From']) | ||||
|
r43347 | if not subject and not data[b'user']: | ||
|
r37639 | # Not an email, restore parsed headers if any | ||
|
r43346 | subject = ( | ||
|
r43347 | b'\n'.join( | ||
b': '.join(map(encoding.strtolocal, h)) for h in msg.items() | ||||
|
r43346 | ) | ||
|
r43347 | + b'\n' | ||
|
r43346 | ) | ||
|
r9573 | |||
|
r37639 | # should try to parse msg['Date'] | ||
parents = [] | ||||
|
r2866 | |||
|
r43906 | nodeid = msg['X-Mercurial-Node'] | ||
|
r43367 | if nodeid: | ||
data[b'nodeid'] = nodeid = mail.headdecode(nodeid) | ||||
ui.debug(b'Node ID: %s\n' % nodeid) | ||||
|
r37639 | if subject: | ||
|
r43347 | if subject.startswith(b'[PATCH'): | ||
pend = subject.find(b']') | ||||
|
r37639 | if pend >= 0: | ||
|
r43346 | subject = subject[pend + 1 :].lstrip() | ||
|
r43347 | subject = re.sub(br'\n[ \t]+', b' ', subject) | ||
ui.debug(b'Subject: %s\n' % subject) | ||||
if data[b'user']: | ||||
ui.debug(b'From: %s\n' % data[b'user']) | ||||
|
r37639 | diffs_seen = 0 | ||
|
r43347 | ok_types = (b'text/plain', b'text/x-diff', b'text/x-patch') | ||
message = b'' | ||||
|
r37639 | for part in msg.walk(): | ||
content_type = pycompat.bytestr(part.get_content_type()) | ||||
|
r43347 | ui.debug(b'Content-Type: %s\n' % content_type) | ||
|
r37639 | if content_type not in ok_types: | ||
continue | ||||
payload = part.get_payload(decode=True) | ||||
m = diffre.search(payload) | ||||
if m: | ||||
hgpatch = False | ||||
hgpatchheader = False | ||||
ignoretext = False | ||||
|
r4220 | |||
|
r43347 | ui.debug(b'found patch at byte %d\n' % m.start(0)) | ||
|
r37639 | diffs_seen += 1 | ||
cfp = stringio() | ||||
|
r43346 | for line in payload[: m.start(0)].splitlines(): | ||
|
r43347 | if line.startswith(b'# HG changeset patch') and not hgpatch: | ||
ui.debug(b'patch generated by hg export\n') | ||||
|
r37639 | hgpatch = True | ||
hgpatchheader = True | ||||
# drop earlier commit message content | ||||
cfp.seek(0) | ||||
cfp.truncate() | ||||
subject = None | ||||
elif hgpatchheader: | ||||
|
r43347 | if line.startswith(b'# User '): | ||
data[b'user'] = line[7:] | ||||
ui.debug(b'From: %s\n' % data[b'user']) | ||||
elif line.startswith(b"# Parent "): | ||||
|
r37639 | parents.append(line[9:].lstrip()) | ||
|
r43347 | elif line.startswith(b"# "): | ||
|
r37639 | for header, key in patchheadermap: | ||
|
r43347 | prefix = b'# %s ' % header | ||
|
r37639 | if line.startswith(prefix): | ||
|
r43346 | data[key] = line[len(prefix) :] | ||
|
r43347 | ui.debug(b'%s: %s\n' % (header, data[key])) | ||
|
r37639 | else: | ||
hgpatchheader = False | ||||
|
r43347 | elif line == b'---': | ||
|
r37639 | ignoretext = True | ||
if not hgpatchheader and not ignoretext: | ||||
cfp.write(line) | ||||
|
r43347 | cfp.write(b'\n') | ||
|
r37639 | message = cfp.getvalue() | ||
if tmpfp: | ||||
tmpfp.write(payload) | ||||
|
r43347 | if not payload.endswith(b'\n'): | ||
tmpfp.write(b'\n') | ||||
elif not diffs_seen and message and content_type == b'text/plain': | ||||
message += b'\n' + payload | ||||
|
r2866 | |||
|
r4777 | if subject and not message.startswith(subject): | ||
|
r43347 | message = b'%s\n%s' % (subject, message) | ||
data[b'message'] = message | ||||
|
r2866 | tmpfp.close() | ||
|
r24306 | if parents: | ||
|
r43347 | data[b'p1'] = parents.pop(0) | ||
|
r26548 | if parents: | ||
|
r43347 | data[b'p2'] = parents.pop(0) | ||
|
r24306 | |||
|
r26555 | if diffs_seen: | ||
|
r43347 | data[b'filename'] = tmpname | ||
|
r37639 | |||
|
r26547 | return data | ||
|
r2861 | |||
|
r43346 | |||
|
r8778 | class patchmeta(object): | ||
|
r7148 | """Patched file metadata | ||
'op' is the performed operation within ADD, DELETE, RENAME, MODIFY | ||||
or COPY. 'path' is patched file path. 'oldpath' is set to the | ||||
|
r7149 | origin file when 'op' is either COPY or RENAME, None otherwise. If | ||
file mode is changed, 'mode' is a tuple (islink, isexec) where | ||||
'islink' is True if the file is a symlink and 'isexec' is True if | ||||
the file is executable. Otherwise, 'mode' is None. | ||||
|
r7148 | """ | ||
|
r43346 | |||
|
r7148 | def __init__(self, path): | ||
self.path = path | ||||
self.oldpath = None | ||||
self.mode = None | ||||
|
r43347 | self.op = b'MODIFY' | ||
|
r7148 | self.binary = False | ||
|
r7149 | def setmode(self, mode): | ||
|
r25658 | islink = mode & 0o20000 | ||
isexec = mode & 0o100 | ||||
|
r7149 | self.mode = (islink, isexec) | ||
|
r14566 | def copy(self): | ||
other = patchmeta(self.path) | ||||
other.oldpath = self.oldpath | ||||
other.mode = self.mode | ||||
other.op = self.op | ||||
other.binary = self.binary | ||||
return other | ||||
|
r16506 | def _ispatchinga(self, afile): | ||
|
r43347 | if afile == b'/dev/null': | ||
return self.op == b'ADD' | ||||
return afile == b'a/' + (self.oldpath or self.path) | ||||
|
r16506 | |||
def _ispatchingb(self, bfile): | ||||
|
r43347 | if bfile == b'/dev/null': | ||
return self.op == b'DELETE' | ||||
return bfile == b'b/' + self.path | ||||
|
r16506 | |||
def ispatching(self, afile, bfile): | ||||
return self._ispatchinga(afile) and self._ispatchingb(bfile) | ||||
|
r11018 | def __repr__(self): | ||
|
r43809 | return "<patchmeta %s %r>" % (self.op, self.path) | ||
|
r11018 | |||
|
r43346 | |||
|
r7152 | def readgitpatch(lr): | ||
|
r2861 | """extract git-style metadata about patches from <patchname>""" | ||
|
r3223 | |||
|
r2861 | # Filter patch for git information | ||
gp = None | ||||
gitpatches = [] | ||||
|
r7152 | for line in lr: | ||
|
r46863 | line = line.rstrip(b'\r\n') | ||
|
r43347 | if line.startswith(b'diff --git a/'): | ||
|
r2861 | m = gitre.match(line) | ||
if m: | ||||
if gp: | ||||
gitpatches.append(gp) | ||||
|
r9392 | dst = m.group(2) | ||
|
r7148 | gp = patchmeta(dst) | ||
|
r2861 | elif gp: | ||
|
r43347 | if line.startswith(b'--- '): | ||
|
r2861 | gitpatches.append(gp) | ||
gp = None | ||||
continue | ||||
|
r43347 | if line.startswith(b'rename from '): | ||
gp.op = b'RENAME' | ||||
|
r9243 | gp.oldpath = line[12:] | ||
|
r43347 | elif line.startswith(b'rename to '): | ||
|
r9243 | gp.path = line[10:] | ||
|
r43347 | elif line.startswith(b'copy from '): | ||
gp.op = b'COPY' | ||||
|
r9243 | gp.oldpath = line[10:] | ||
|
r43347 | elif line.startswith(b'copy to '): | ||
|
r9243 | gp.path = line[8:] | ||
|
r43347 | elif line.startswith(b'deleted file'): | ||
gp.op = b'DELETE' | ||||
elif line.startswith(b'new file mode '): | ||||
gp.op = b'ADD' | ||||
|
r9243 | gp.setmode(int(line[-6:], 8)) | ||
|
r43347 | elif line.startswith(b'new mode '): | ||
|
r9243 | gp.setmode(int(line[-6:], 8)) | ||
|
r43347 | elif line.startswith(b'GIT binary patch'): | ||
|
r3367 | gp.binary = True | ||
|
r2861 | if gp: | ||
gitpatches.append(gp) | ||||
|
r12669 | return gitpatches | ||
|
r2861 | |||
|
r43346 | |||
|
r8891 | class linereader(object): | ||
|
r8810 | # simple class to allow pushing lines back into the input stream | ||
|
r14418 | def __init__(self, fp): | ||
|
r8810 | self.fp = fp | ||
self.buf = [] | ||||
def push(self, line): | ||||
if line is not None: | ||||
self.buf.append(line) | ||||
def readline(self): | ||||
if self.buf: | ||||
l = self.buf[0] | ||||
del self.buf[0] | ||||
return l | ||||
|
r14418 | return self.fp.readline() | ||
|
r8810 | |||
def __iter__(self): | ||||
|
r43347 | return iter(self.readline, b'') | ||
|
r8810 | |||
|
r43346 | |||
|
r14348 | class abstractbackend(object): | ||
def __init__(self, ui): | ||||
|
r4897 | self.ui = ui | ||
|
r14348 | |||
|
r14391 | def getfile(self, fname): | ||
"""Return target file data and flags as a (data, (islink, | ||||
|
r22296 | isexec)) tuple. Data is None if file is missing/deleted. | ||
|
r14348 | """ | ||
raise NotImplementedError | ||||
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
|
r14391 | """Write data to target file fname and set its mode. mode is a | ||
(islink, isexec) tuple. If data is None, the file content should | ||||
|
r14452 | be left unchanged. If the file is modified after being copied, | ||
copysource is set to the original file name. | ||||
|
r14367 | """ | ||
|
r14348 | raise NotImplementedError | ||
|
r5652 | |||
|
r14348 | def unlink(self, fname): | ||
"""Unlink target file.""" | ||||
raise NotImplementedError | ||||
def writerej(self, fname, failed, total, lines): | ||||
"""Write rejected lines for fname. total is the number of hunks | ||||
which failed to apply and total the total number of hunks for this | ||||
files. | ||||
""" | ||||
|
r14351 | def exists(self, fname): | ||
raise NotImplementedError | ||||
|
r33156 | def close(self): | ||
raise NotImplementedError | ||||
|
r43346 | |||
|
r14348 | class fsbackend(abstractbackend): | ||
|
r14350 | def __init__(self, ui, basedir): | ||
|
r14348 | super(fsbackend, self).__init__(ui) | ||
|
r31233 | self.opener = vfsmod.vfs(basedir) | ||
|
r4897 | |||
|
r14391 | def getfile(self, fname): | ||
|
r21717 | if self.opener.islink(fname): | ||
return (self.opener.readlink(fname), (True, False)) | ||||
|
r14531 | isexec = False | ||
|
r7392 | try: | ||
|
r25658 | isexec = self.opener.lstat(fname).st_mode & 0o100 != 0 | ||
|
r25660 | except OSError as e: | ||
|
r14391 | if e.errno != errno.ENOENT: | ||
raise | ||||
|
r22296 | try: | ||
return (self.opener.read(fname), (False, isexec)) | ||||
|
r25660 | except IOError as e: | ||
|
r22296 | if e.errno != errno.ENOENT: | ||
raise | ||||
return None, None | ||||
|
r7392 | |||
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
|
r14391 | islink, isexec = mode | ||
if data is None: | ||||
|
r21717 | self.opener.setflags(fname, islink, isexec) | ||
|
r14390 | return | ||
|
r14391 | if islink: | ||
self.opener.symlink(data, fname) | ||||
|
r14367 | else: | ||
|
r14391 | self.opener.write(fname, data) | ||
|
r14367 | if isexec: | ||
|
r21717 | self.opener.setflags(fname, False, True) | ||
|
r7392 | |||
def unlink(self, fname): | ||||
|
r43347 | rmdir = self.ui.configbool(b'experimental', b'removeemptydirs') | ||
|
r38512 | self.opener.unlinkpath(fname, ignoremissing=True, rmdir=rmdir) | ||
|
r7392 | |||
|
r14348 | def writerej(self, fname, failed, total, lines): | ||
|
r43347 | fname = fname + b".rej" | ||
|
r14348 | self.ui.warn( | ||
|
r43347 | _(b"%d out of %d hunks FAILED -- saving rejects to file %s\n") | ||
|
r43346 | % (failed, total, fname) | ||
) | ||||
|
r43347 | fp = self.opener(fname, b'w') | ||
|
r14348 | fp.writelines(lines) | ||
fp.close() | ||||
|
r14351 | def exists(self, fname): | ||
|
r21717 | return self.opener.lexists(fname) | ||
|
r14351 | |||
|
r43346 | |||
|
r14370 | class workingbackend(fsbackend): | ||
def __init__(self, ui, repo, similarity): | ||||
super(workingbackend, self).__init__(ui, repo.root) | ||||
self.repo = repo | ||||
self.similarity = similarity | ||||
self.removed = set() | ||||
self.changed = set() | ||||
self.copied = [] | ||||
|
r14453 | def _checkknown(self, fname): | ||
r48911 | if not self.repo.dirstate.get_entry(fname).any_tracked and self.exists( | |||
fname | ||||
): | ||||
|
r43347 | raise PatchError(_(b'cannot patch %s: file is not tracked') % fname) | ||
|
r14453 | |||
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
|
r14453 | self._checkknown(fname) | ||
|
r14452 | super(workingbackend, self).setfile(fname, data, mode, copysource) | ||
if copysource is not None: | ||||
self.copied.append((copysource, fname)) | ||||
|
r14370 | self.changed.add(fname) | ||
def unlink(self, fname): | ||||
|
r14453 | self._checkknown(fname) | ||
|
r14370 | super(workingbackend, self).unlink(fname) | ||
self.removed.add(fname) | ||||
self.changed.add(fname) | ||||
def close(self): | ||||
wctx = self.repo[None] | ||||
|
r19155 | changed = set(self.changed) | ||
|
r14370 | for src, dst in self.copied: | ||
scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst) | ||||
|
r16112 | if self.removed: | ||
|
r14435 | wctx.forget(sorted(self.removed)) | ||
|
r16112 | for f in self.removed: | ||
if f not in self.repo.dirstate: | ||||
# File was deleted and no longer belongs to the | ||||
# dirstate, it was probably marked added then | ||||
# deleted, and should not be considered by | ||||
|
r19155 | # marktouched(). | ||
changed.discard(f) | ||||
if changed: | ||||
scmutil.marktouched(self.repo, changed, self.similarity) | ||||
|
r14370 | return sorted(self.changed) | ||
|
r43346 | |||
|
r14452 | class filestore(object): | ||
|
r14658 | def __init__(self, maxsize=None): | ||
|
r14452 | self.opener = None | ||
self.files = {} | ||||
self.created = 0 | ||||
|
r14658 | self.maxsize = maxsize | ||
if self.maxsize is None: | ||||
|
r43346 | self.maxsize = 4 * (2 ** 20) | ||
|
r14658 | self.size = 0 | ||
self.data = {} | ||||
|
r14452 | |||
|
r14609 | def setfile(self, fname, data, mode, copied=None): | ||
|
r14658 | if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize: | ||
self.data[fname] = (data, mode, copied) | ||||
self.size += len(data) | ||||
else: | ||||
if self.opener is None: | ||||
|
r43347 | root = pycompat.mkdtemp(prefix=b'hg-patch-') | ||
|
r31233 | self.opener = vfsmod.vfs(root) | ||
|
r14658 | # Avoid filename issues with these simple names | ||
|
r43347 | fn = b'%d' % self.created | ||
|
r14658 | self.opener.write(fn, data) | ||
self.created += 1 | ||||
self.files[fname] = (fn, mode, copied) | ||||
|
r14452 | |||
def getfile(self, fname): | ||||
|
r14658 | if fname in self.data: | ||
return self.data[fname] | ||||
if not self.opener or fname not in self.files: | ||||
|
r22296 | return None, None, None | ||
|
r14609 | fn, mode, copied = self.files[fname] | ||
return self.opener.read(fn), mode, copied | ||||
|
r14452 | |||
def close(self): | ||||
if self.opener: | ||||
shutil.rmtree(self.opener.base) | ||||
|
r43346 | |||
|
r14611 | class repobackend(abstractbackend): | ||
def __init__(self, ui, repo, ctx, store): | ||||
super(repobackend, self).__init__(ui) | ||||
self.repo = repo | ||||
self.ctx = ctx | ||||
self.store = store | ||||
self.changed = set() | ||||
self.removed = set() | ||||
self.copied = {} | ||||
def _checkknown(self, fname): | ||||
if fname not in self.ctx: | ||||
|
r43347 | raise PatchError(_(b'cannot patch %s: file is not tracked') % fname) | ||
|
r14611 | |||
def getfile(self, fname): | ||||
try: | ||||
fctx = self.ctx[fname] | ||||
except error.LookupError: | ||||
|
r22296 | return None, None | ||
|
r14611 | flags = fctx.flags() | ||
|
r43347 | return fctx.data(), (b'l' in flags, b'x' in flags) | ||
|
r14611 | |||
def setfile(self, fname, data, mode, copysource): | ||||
if copysource: | ||||
self._checkknown(copysource) | ||||
if data is None: | ||||
data = self.ctx[fname].data() | ||||
self.store.setfile(fname, data, mode, copysource) | ||||
self.changed.add(fname) | ||||
if copysource: | ||||
self.copied[fname] = copysource | ||||
def unlink(self, fname): | ||||
self._checkknown(fname) | ||||
self.removed.add(fname) | ||||
def exists(self, fname): | ||||
return fname in self.ctx | ||||
def close(self): | ||||
return self.changed | self.removed | ||||
|
r43346 | |||
|
r14348 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 | ||
|
r41673 | unidesc = re.compile(br'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') | ||
contextdesc = re.compile(br'(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') | ||||
|
r43347 | eolmodes = [b'strict', b'crlf', b'lf', b'auto'] | ||
|
r14348 | |||
|
r43346 | |||
|
r14348 | class patchfile(object): | ||
|
r43347 | def __init__(self, ui, gp, backend, store, eolmode=b'strict'): | ||
|
r14566 | self.fname = gp.path | ||
|
r14348 | self.eolmode = eolmode | ||
self.eol = None | ||||
self.backend = backend | ||||
self.ui = ui | ||||
self.lines = [] | ||||
self.exists = False | ||||
|
r14452 | self.missing = True | ||
|
r14566 | self.mode = gp.mode | ||
self.copysource = gp.oldpath | ||||
|
r43347 | self.create = gp.op in (b'ADD', b'COPY', b'RENAME') | ||
self.remove = gp.op == b'DELETE' | ||||
|
r22296 | if self.copysource is None: | ||
data, mode = backend.getfile(self.fname) | ||||
else: | ||||
data, mode = store.getfile(self.copysource)[:2] | ||||
if data is not None: | ||||
self.exists = self.copysource is None or backend.exists(self.fname) | ||||
|
r14452 | self.missing = False | ||
if data: | ||||
|
r14832 | self.lines = mdiff.splitnewlines(data) | ||
|
r14452 | if self.mode is None: | ||
self.mode = mode | ||||
if self.lines: | ||||
# Normalize line endings | ||||
|
r43347 | if self.lines[0].endswith(b'\r\n'): | ||
self.eol = b'\r\n' | ||||
elif self.lines[0].endswith(b'\n'): | ||||
self.eol = b'\n' | ||||
if eolmode != b'strict': | ||||
|
r14452 | nlines = [] | ||
for l in self.lines: | ||||
|
r43347 | if l.endswith(b'\r\n'): | ||
l = l[:-2] + b'\n' | ||||
|
r14452 | nlines.append(l) | ||
self.lines = nlines | ||||
|
r22296 | else: | ||
|
r14566 | if self.create: | ||
|
r14452 | self.missing = False | ||
if self.mode is None: | ||||
self.mode = (False, False) | ||||
if self.missing: | ||||
|
r43347 | self.ui.warn(_(b"unable to find '%s' for patching\n") % self.fname) | ||
|
r43346 | self.ui.warn( | ||
_( | ||||
|
r43347 | b"(use '--prefix' to apply patch relative to the " | ||
b"current directory)\n" | ||||
|
r43346 | ) | ||
) | ||||
|
r14348 | |||
self.hash = {} | ||||
self.dirty = 0 | ||||
self.offset = 0 | ||||
self.skew = 0 | ||||
self.rej = [] | ||||
self.fileprinted = False | ||||
self.printfile(False) | ||||
self.hunks = 0 | ||||
|
r14367 | def writelines(self, fname, lines, mode): | ||
|
r43347 | if self.eolmode == b'auto': | ||
|
r14348 | eol = self.eol | ||
|
r43347 | elif self.eolmode == b'crlf': | ||
eol = b'\r\n' | ||||
|
r14348 | else: | ||
|
r43347 | eol = b'\n' | ||
if self.eolmode != b'strict' and eol and eol != b'\n': | ||||
|
r14348 | rawlines = [] | ||
for l in lines: | ||||
|
r43347 | if l and l.endswith(b'\n'): | ||
|
r14348 | l = l[:-1] + eol | ||
rawlines.append(l) | ||||
lines = rawlines | ||||
|
r43347 | self.backend.setfile(fname, b''.join(lines), mode, self.copysource) | ||
|
r14348 | |||
|
r4897 | def printfile(self, warn): | ||
if self.fileprinted: | ||||
return | ||||
if warn or self.ui.verbose: | ||||
self.fileprinted = True | ||||
|
r43347 | s = _(b"patching file %s\n") % self.fname | ||
|
r4897 | if warn: | ||
self.ui.warn(s) | ||||
else: | ||||
self.ui.note(s) | ||||
def findlines(self, l, linenum): | ||||
# looks through the hash and finds candidate lines. The | ||||
# result is a list of line numbers sorted based on distance | ||||
# from linenum | ||||
|
r5143 | |||
|
r9681 | cand = self.hash.get(l, []) | ||
|
r4897 | if len(cand) > 1: | ||
# resort our list of potentials forward then back. | ||||
|
r9032 | cand.sort(key=lambda x: abs(x - linenum)) | ||
|
r4897 | return cand | ||
def write_rej(self): | ||||
# our rejects are a little different from patch(1). This always | ||||
# creates rejects in the same form as the original patch. A file | ||||
# header is inserted so that you can run the reject through patch again | ||||
# without having to type the filename. | ||||
if not self.rej: | ||||
return | ||||
|
r14349 | base = os.path.basename(self.fname) | ||
|
r43347 | lines = [b"--- %s\n+++ %s\n" % (base, base)] | ||
|
r14349 | for x in self.rej: | ||
for l in x.hunk: | ||||
lines.append(l) | ||||
|
r43347 | if l[-1:] != b'\n': | ||
|
r45714 | lines.append(b'\n' + diffhelper.MISSING_NEWLINE_MARKER) | ||
|
r14349 | self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) | ||
|
r4897 | |||
|
r9393 | def apply(self, h): | ||
|
r4897 | if not h.complete(): | ||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b"bad hunk #%d %s (%d %d %d %d)") | ||
|
r43346 | % (h.number, h.desc, len(h.a), h.lena, len(h.b), h.lenb) | ||
) | ||||
|
r4897 | |||
self.hunks += 1 | ||||
|
r5652 | if self.missing: | ||
self.rej.append(h) | ||||
return -1 | ||||
|
r14451 | if self.exists and self.create: | ||
|
r14452 | if self.copysource: | ||
|
r43346 | self.ui.warn( | ||
|
r43387 | _(b"cannot create %s: destination already exists\n") | ||
|
r43346 | % self.fname | ||
) | ||||
|
r14452 | else: | ||
|
r43347 | self.ui.warn(_(b"file %s already exists\n") % self.fname) | ||
|
r4897 | self.rej.append(h) | ||
return -1 | ||||
|
r9585 | if isinstance(h, binhunk): | ||
|
r14451 | if self.remove: | ||
|
r14348 | self.backend.unlink(self.fname) | ||
|
r4897 | else: | ||
|
r20137 | l = h.new(self.lines) | ||
self.lines[:] = l | ||||
self.offset += len(l) | ||||
|
r14217 | self.dirty = True | ||
|
r4897 | return 0 | ||
|
r10127 | horig = h | ||
|
r43346 | if ( | ||
|
r43347 | self.eolmode in (b'crlf', b'lf') | ||
or self.eolmode == b'auto' | ||||
|
r43346 | and self.eol | ||
): | ||||
|
r10128 | # If new eols are going to be normalized, then normalize | ||
# hunk data before patching. Otherwise, preserve input | ||||
# line-endings. | ||||
|
r10127 | h = h.getnormalized() | ||
|
r4897 | # fast case first, no offsets, no fuzz | ||
|
r16122 | old, oldstart, new, newstart = h.fuzzit(0, False) | ||
oldstart += self.offset | ||||
orig_start = oldstart | ||||
|
r10135 | # if there's skew we want to emit the "(offset %d lines)" even | ||
# when the hunk cleanly applies at start + skew, so skip the | ||||
# fast case code | ||||
|
r37821 | if self.skew == 0 and diffhelper.testhunk(old, self.lines, oldstart): | ||
|
r14451 | if self.remove: | ||
|
r14348 | self.backend.unlink(self.fname) | ||
|
r4897 | else: | ||
|
r43346 | self.lines[oldstart : oldstart + len(old)] = new | ||
|
r16122 | self.offset += len(new) - len(old) | ||
|
r14217 | self.dirty = True | ||
|
r4897 | return 0 | ||
|
r13700 | # ok, we couldn't match the hunk. Lets look for offsets and fuzz it | ||
self.hash = {} | ||||
for x, s in enumerate(self.lines): | ||||
self.hash.setdefault(s, []).append(x) | ||||
|
r4897 | |||
|
r43347 | for fuzzlen in pycompat.xrange( | ||
self.ui.configint(b"patch", b"fuzz") + 1 | ||||
): | ||||
|
r10282 | for toponly in [True, False]: | ||
|
r16122 | old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) | ||
|
r16123 | oldstart = oldstart + self.offset + self.skew | ||
oldstart = min(oldstart, len(self.lines)) | ||||
if old: | ||||
cand = self.findlines(old[0][1:], oldstart) | ||||
else: | ||||
# Only adding lines with no or fuzzed context, just | ||||
# take the skew in account | ||||
cand = [oldstart] | ||||
|
r4897 | |||
for l in cand: | ||||
|
r37821 | if not old or diffhelper.testhunk(old, self.lines, l): | ||
|
r16121 | self.lines[l : l + len(old)] = new | ||
self.offset += len(new) - len(old) | ||||
|
r10135 | self.skew = l - orig_start | ||
|
r14217 | self.dirty = True | ||
|
r10518 | offset = l - orig_start - fuzzlen | ||
|
r4897 | if fuzzlen: | ||
|
r43346 | msg = _( | ||
|
r43347 | b"Hunk #%d succeeded at %d " | ||
b"with fuzz %d " | ||||
b"(offset %d lines).\n" | ||||
|
r43346 | ) | ||
|
r4897 | self.printfile(True) | ||
|
r43346 | self.ui.warn( | ||
msg % (h.number, l + 1, fuzzlen, offset) | ||||
) | ||||
|
r4897 | else: | ||
|
r43346 | msg = _( | ||
|
r43347 | b"Hunk #%d succeeded at %d " | ||
b"(offset %d lines).\n" | ||||
|
r43346 | ) | ||
|
r10518 | self.ui.note(msg % (h.number, l + 1, offset)) | ||
|
r4897 | return fuzzlen | ||
self.printfile(True) | ||||
|
r43347 | self.ui.warn(_(b"Hunk #%d FAILED at %d\n") % (h.number, orig_start)) | ||
|
r10127 | self.rej.append(horig) | ||
|
r4897 | return -1 | ||
|
r13701 | def close(self): | ||
if self.dirty: | ||||
|
r14367 | self.writelines(self.fname, self.lines, self.mode) | ||
|
r13701 | self.write_rej() | ||
return len(self.rej) | ||||
|
r43346 | |||
|
r24261 | class header(object): | ||
|
r46554 | """patch header""" | ||
|
r43346 | |||
|
r43347 | diffgit_re = re.compile(b'diff --git a/(.*) b/(.*)$') | ||
diff_re = re.compile(b'diff -r .* (.*)$') | ||||
allhunks_re = re.compile(b'(?:index|deleted file) ') | ||||
pretty_re = re.compile(b'(?:new file|deleted file) ') | ||||
special_re = re.compile(b'(?:index|deleted|copy|rename|new mode) ') | ||||
newfile_re = re.compile(b'(?:new file|copy to|rename to)') | ||||
|
r24261 | |||
def __init__(self, header): | ||||
self.header = header | ||||
self.hunks = [] | ||||
def binary(self): | ||||
|
r43347 | return any(h.startswith(b'index ') for h in self.header) | ||
|
r24261 | |||
def pretty(self, fp): | ||||
for h in self.header: | ||||
|
r43347 | if h.startswith(b'index '): | ||
fp.write(_(b'this modifies a binary file (all or nothing)\n')) | ||||
|
r24261 | break | ||
if self.pretty_re.match(h): | ||||
fp.write(h) | ||||
if self.binary(): | ||||
|
r43347 | fp.write(_(b'this is a binary file\n')) | ||
|
r24261 | break | ||
|
r43347 | if h.startswith(b'---'): | ||
|
r43346 | fp.write( | ||
|
r43347 | _(b'%d hunks, %d lines changed\n') | ||
|
r43346 | % ( | ||
len(self.hunks), | ||||
sum([max(h.added, h.removed) for h in self.hunks]), | ||||
) | ||||
) | ||||
|
r24261 | break | ||
fp.write(h) | ||||
def write(self, fp): | ||||
|
r43347 | fp.write(b''.join(self.header)) | ||
|
r24261 | |||
def allhunks(self): | ||||
|
r25149 | return any(self.allhunks_re.match(h) for h in self.header) | ||
|
r24261 | |||
def files(self): | ||||
match = self.diffgit_re.match(self.header[0]) | ||||
if match: | ||||
fromfile, tofile = match.groups() | ||||
if fromfile == tofile: | ||||
return [fromfile] | ||||
return [fromfile, tofile] | ||||
else: | ||||
return self.diff_re.match(self.header[0]).groups() | ||||
def filename(self): | ||||
return self.files()[-1] | ||||
def __repr__(self): | ||||
|
r44396 | return '<header %s>' % ( | ||
' '.join(pycompat.rapply(pycompat.fsdecode, self.files())) | ||||
) | ||||
|
r24261 | |||
|
r24845 | def isnewfile(self): | ||
|
r25149 | return any(self.newfile_re.match(h) for h in self.header) | ||
|
r24845 | |||
|
r24261 | def special(self): | ||
|
r24845 | # Special files are shown only at the header level and not at the hunk | ||
# level for example a file that has been deleted is a special file. | ||||
# The user cannot change the content of the operation, in the case of | ||||
# the deleted file he has to take the deletion or not take it, he | ||||
# cannot take some of it. | ||||
# Newly added files are special if they are empty, they are not special | ||||
# if they have some content as we want to be able to change it | ||||
nocontent = len(self.header) == 2 | ||||
emptynewfile = self.isnewfile() and nocontent | ||||
|
r43346 | return emptynewfile or any( | ||
self.special_re.match(h) for h in self.header | ||||
) | ||||
|
r24261 | |||
|
r24263 | class recordhunk(object): | ||
"""patch hunk | ||||
XXX shouldn't we merge this with the other hunk class? | ||||
""" | ||||
|
r43346 | def __init__( | ||
self, | ||||
header, | ||||
fromline, | ||||
toline, | ||||
proc, | ||||
before, | ||||
hunk, | ||||
after, | ||||
maxcontext=None, | ||||
): | ||||
|
r33270 | def trimcontext(lines, reverse=False): | ||
if maxcontext is not None: | ||||
delta = len(lines) - maxcontext | ||||
if delta > 0: | ||||
if reverse: | ||||
return delta, lines[delta:] | ||||
else: | ||||
return delta, lines[:maxcontext] | ||||
return 0, lines | ||||
|
r24263 | |||
self.header = header | ||||
|
r33270 | trimedbefore, self.before = trimcontext(before, True) | ||
self.fromline = fromline + trimedbefore | ||||
self.toline = toline + trimedbefore | ||||
_trimedafter, self.after = trimcontext(after, False) | ||||
|
r24263 | self.proc = proc | ||
self.hunk = hunk | ||||
self.added, self.removed = self.countchanges(self.hunk) | ||||
|
r24346 | def __eq__(self, v): | ||
if not isinstance(v, recordhunk): | ||||
return False | ||||
|
r43346 | return ( | ||
(v.hunk == self.hunk) | ||||
and (v.proc == self.proc) | ||||
and (self.fromline == v.fromline) | ||||
and (self.header.files() == v.header.files()) | ||||
) | ||||
|
r24346 | |||
def __hash__(self): | ||||
|
r43346 | return hash( | ||
( | ||||
tuple(self.hunk), | ||||
tuple(self.header.files()), | ||||
self.fromline, | ||||
self.proc, | ||||
) | ||||
) | ||||
|
r24346 | |||
|
r24263 | def countchanges(self, hunk): | ||
"""hunk -> (n+,n-)""" | ||||
|
r43347 | add = len([h for h in hunk if h.startswith(b'+')]) | ||
rem = len([h for h in hunk if h.startswith(b'-')]) | ||||
|
r24263 | return add, rem | ||
|
r32979 | def reversehunk(self): | ||
"""return another recordhunk which is the reverse of the hunk | ||||
If this hunk is diff(A, B), the returned hunk is diff(B, A). To do | ||||
that, swap fromline/toline and +/- signs while keep other things | ||||
unchanged. | ||||
""" | ||||
|
r43347 | m = {b'+': b'-', b'-': b'+', b'\\': b'\\'} | ||
hunk = [b'%s%s' % (m[l[0:1]], l[1:]) for l in self.hunk] | ||||
|
r43346 | return recordhunk( | ||
self.header, | ||||
self.toline, | ||||
self.fromline, | ||||
self.proc, | ||||
self.before, | ||||
hunk, | ||||
self.after, | ||||
) | ||||
|
r32979 | |||
|
r24263 | def write(self, fp): | ||
delta = len(self.before) + len(self.after) | ||||
|
r45714 | if self.after and self.after[-1] == diffhelper.MISSING_NEWLINE_MARKER: | ||
|
r24263 | delta -= 1 | ||
fromlen = delta + self.removed | ||||
tolen = delta + self.added | ||||
|
r43346 | fp.write( | ||
|
r43347 | b'@@ -%d,%d +%d,%d @@%s\n' | ||
|
r43346 | % ( | ||
self.fromline, | ||||
fromlen, | ||||
self.toline, | ||||
tolen, | ||||
|
r43347 | self.proc and (b' ' + self.proc), | ||
|
r43346 | ) | ||
) | ||||
|
r43347 | fp.write(b''.join(self.before + self.hunk + self.after)) | ||
|
r24263 | |||
pretty = write | ||||
def filename(self): | ||||
return self.header.filename() | ||||
|
r44742 | @encoding.strmethod | ||
|
r24263 | def __repr__(self): | ||
|
r43347 | return b'<hunk %r@%d>' % (self.filename(), self.fromline) | ||
|
r24263 | |||
|
r43346 | |||
|
r34567 | def getmessages(): | ||
return { | ||||
|
r43347 | b'multiple': { | ||
b'apply': _(b"apply change %d/%d to '%s'?"), | ||||
b'discard': _(b"discard change %d/%d to '%s'?"), | ||||
b'keep': _(b"keep change %d/%d to '%s'?"), | ||||
b'record': _(b"record change %d/%d to '%s'?"), | ||||
|
r34567 | }, | ||
|
r43347 | b'single': { | ||
b'apply': _(b"apply this change to '%s'?"), | ||||
b'discard': _(b"discard this change to '%s'?"), | ||||
b'keep': _(b"keep this change to '%s'?"), | ||||
b'record': _(b"record this change to '%s'?"), | ||||
|
r34567 | }, | ||
|
r43347 | b'help': { | ||
b'apply': _( | ||||
b'[Ynesfdaq?]' | ||||
b'$$ &Yes, apply this change' | ||||
b'$$ &No, skip this change' | ||||
b'$$ &Edit this change manually' | ||||
b'$$ &Skip remaining changes to this file' | ||||
b'$$ Apply remaining changes to this &file' | ||||
b'$$ &Done, skip remaining changes and files' | ||||
b'$$ Apply &all changes to all remaining files' | ||||
b'$$ &Quit, applying no changes' | ||||
b'$$ &? (display help)' | ||||
|
r43346 | ), | ||
|
r43347 | b'discard': _( | ||
b'[Ynesfdaq?]' | ||||
b'$$ &Yes, discard this change' | ||||
b'$$ &No, skip this change' | ||||
b'$$ &Edit this change manually' | ||||
b'$$ &Skip remaining changes to this file' | ||||
b'$$ Discard remaining changes to this &file' | ||||
b'$$ &Done, skip remaining changes and files' | ||||
b'$$ Discard &all changes to all remaining files' | ||||
b'$$ &Quit, discarding no changes' | ||||
b'$$ &? (display help)' | ||||
|
r43346 | ), | ||
|
r43347 | b'keep': _( | ||
b'[Ynesfdaq?]' | ||||
b'$$ &Yes, keep this change' | ||||
b'$$ &No, skip this change' | ||||
b'$$ &Edit this change manually' | ||||
b'$$ &Skip remaining changes to this file' | ||||
b'$$ Keep remaining changes to this &file' | ||||
b'$$ &Done, skip remaining changes and files' | ||||
b'$$ Keep &all changes to all remaining files' | ||||
b'$$ &Quit, keeping all changes' | ||||
b'$$ &? (display help)' | ||||
|
r43346 | ), | ||
|
r43347 | b'record': _( | ||
b'[Ynesfdaq?]' | ||||
b'$$ &Yes, record this change' | ||||
b'$$ &No, skip this change' | ||||
b'$$ &Edit this change manually' | ||||
b'$$ &Skip remaining changes to this file' | ||||
b'$$ Record remaining changes to this &file' | ||||
b'$$ &Done, skip remaining changes and files' | ||||
b'$$ Record &all changes to all remaining files' | ||||
b'$$ &Quit, recording no changes' | ||||
b'$$ &? (display help)' | ||||
|
r43346 | ), | ||
}, | ||||
|
r34044 | } | ||
|
r43346 | |||
|
r42238 | def filterpatch(ui, headers, match, operation=None): | ||
|
r24269 | """Interactively filter patch chunks into applied-only chunks""" | ||
|
r34567 | messages = getmessages() | ||
|
r25359 | if operation is None: | ||
|
r43347 | operation = b'record' | ||
|
r24269 | |||
def prompt(skipfile, skipall, query, chunk): | ||||
"""prompt query, and process base inputs | ||||
- y/n for the rest of file | ||||
- y/n for the rest | ||||
- ? (help) | ||||
- q (quit) | ||||
Return True/False and possibly updated skipfile and skipall. | ||||
""" | ||||
newpatches = None | ||||
if skipall is not None: | ||||
return skipall, skipfile, skipall, newpatches | ||||
if skipfile is not None: | ||||
return skipfile, skipfile, skipall, newpatches | ||||
while True: | ||||
|
r43347 | resps = messages[b'help'][operation] | ||
|
r42766 | # IMPORTANT: keep the last line of this prompt short (<40 english | ||
# chars is a good target) because of issue6158. | ||||
|
r43347 | r = ui.promptchoice(b"%s\n(enter ? for help) %s" % (query, resps)) | ||
ui.write(b"\n") | ||||
|
r43346 | if r == 8: # ? | ||
|
r24269 | for c, t in ui.extractchoices(resps)[1]: | ||
|
r43347 | ui.write(b'%s - %s\n' % (c, encoding.lower(t))) | ||
|
r24269 | continue | ||
|
r43346 | elif r == 0: # yes | ||
|
r24269 | ret = True | ||
|
r43346 | elif r == 1: # no | ||
|
r24269 | ret = False | ||
|
r43346 | elif r == 2: # Edit patch | ||
|
r24269 | if chunk is None: | ||
|
r43347 | ui.write(_(b'cannot edit patch for whole file')) | ||
ui.write(b"\n") | ||||
|
r24269 | continue | ||
if chunk.header.binary(): | ||||
|
r43347 | ui.write(_(b'cannot edit patch for binary file')) | ||
ui.write(b"\n") | ||||
|
r24269 | continue | ||
# Patch comment based on the Git one (based on comment at end of | ||||
|
r26421 | # https://mercurial-scm.org/wiki/RecordExtension) | ||
|
r43347 | phelp = b'---' + _( | ||
|
r47520 | b""" | ||
|
r24269 | To remove '-' lines, make them ' ' lines (context). | ||
To remove '+' lines, delete them. | ||||
Lines starting with # will be removed from the patch. | ||||
If the patch applies cleanly, the edited hunk will immediately be | ||||
added to the record list. If it does not apply cleanly, a rejects | ||||
file will be generated: you can use that when you try again. If | ||||
all lines of the hunk are removed, then the edit is aborted and | ||||
the hunk is left unchanged. | ||||
|
r43346 | """ | ||
) | ||||
(patchfd, patchfn) = pycompat.mkstemp( | ||||
|
r43347 | prefix=b"hg-editor-", suffix=b".diff" | ||
|
r43346 | ) | ||
|
r24269 | ncpatchfp = None | ||
try: | ||||
# Write the initial patch | ||||
|
r43906 | f = util.nativeeolwriter(os.fdopen(patchfd, 'wb')) | ||
|
r24269 | chunk.header.write(f) | ||
chunk.write(f) | ||||
|
r43346 | f.write( | ||
|
r43347 | b''.join( | ||
[b'# ' + i + b'\n' for i in phelp.splitlines()] | ||||
) | ||||
|
r43346 | ) | ||
|
r24269 | f.close() | ||
# Start the editor and wait for it to complete | ||||
editor = ui.geteditor() | ||||
|
r43346 | ret = ui.system( | ||
|
r43347 | b"%s \"%s\"" % (editor, patchfn), | ||
environ={b'HGUSER': ui.username()}, | ||||
blockedtag=b'filterpatch', | ||||
|
r43346 | ) | ||
|
r25483 | if ret != 0: | ||
|
r43347 | ui.warn(_(b"editor exited with exit code %d\n") % ret) | ||
|
r25483 | continue | ||
|
r24269 | # Remove comment lines | ||
|
r43906 | patchfp = open(patchfn, 'rb') | ||
|
r28861 | ncpatchfp = stringio() | ||
|
r30397 | for line in util.iterfile(patchfp): | ||
|
r36856 | line = util.fromnativeeol(line) | ||
|
r43347 | if not line.startswith(b'#'): | ||
|
r24269 | ncpatchfp.write(line) | ||
patchfp.close() | ||||
ncpatchfp.seek(0) | ||||
newpatches = parsepatch(ncpatchfp) | ||||
finally: | ||||
os.unlink(patchfn) | ||||
del ncpatchfp | ||||
# Signal that the chunk shouldn't be applied as-is, but | ||||
# provide the new patch to be used instead. | ||||
ret = False | ||||
|
r43346 | elif r == 3: # Skip | ||
|
r24269 | ret = skipfile = False | ||
|
r43346 | elif r == 4: # file (Record remaining) | ||
|
r24269 | ret = skipfile = True | ||
|
r43346 | elif r == 5: # done, skip remaining | ||
|
r24269 | ret = skipall = False | ||
|
r43346 | elif r == 6: # all | ||
|
r24269 | ret = skipall = True | ||
|
r43346 | elif r == 7: # quit | ||
|
r46489 | raise error.CanceledError(_(b'user quit')) | ||
|
r24269 | return ret, skipfile, skipall, newpatches | ||
seen = set() | ||||
|
r43346 | applied = {} # 'filename' -> [] of chunks | ||
|
r24269 | skipfile, skipall = None, None | ||
pos, total = 1, sum(len(h.hunks) for h in headers) | ||||
for h in headers: | ||||
pos += len(h.hunks) | ||||
skipfile = None | ||||
fixoffset = 0 | ||||
|
r43347 | hdr = b''.join(h.header) | ||
|
r24269 | if hdr in seen: | ||
continue | ||||
seen.add(hdr) | ||||
if skipall is None: | ||||
h.pretty(ui) | ||||
|
r42238 | files = h.files() | ||
|
r43347 | msg = _(b'examine changes to %s?') % _(b' and ').join( | ||
b"'%s'" % f for f in files | ||||
|
r43346 | ) | ||
|
r42238 | if all(match.exact(f) for f in files): | ||
r, skipall, np = True, None, None | ||||
else: | ||||
r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None) | ||||
|
r24269 | if not r: | ||
continue | ||||
applied[h.filename()] = [h] | ||||
if h.allhunks(): | ||||
applied[h.filename()] += h.hunks | ||||
continue | ||||
for i, chunk in enumerate(h.hunks): | ||||
if skipfile is None and skipall is None: | ||||
chunk.pretty(ui) | ||||
if total == 1: | ||||
|
r43347 | msg = messages[b'single'][operation] % chunk.filename() | ||
|
r24269 | else: | ||
idx = pos - len(h.hunks) + i | ||||
|
r43347 | msg = messages[b'multiple'][operation] % ( | ||
|
r43346 | idx, | ||
total, | ||||
chunk.filename(), | ||||
) | ||||
r, skipfile, skipall, newpatches = prompt( | ||||
skipfile, skipall, msg, chunk | ||||
) | ||||
|
r24269 | if r: | ||
if fixoffset: | ||||
chunk = copy.copy(chunk) | ||||
chunk.toline += fixoffset | ||||
applied[chunk.filename()].append(chunk) | ||||
elif newpatches is not None: | ||||
for newpatch in newpatches: | ||||
for newhunk in newpatch.hunks: | ||||
if fixoffset: | ||||
newhunk.toline += fixoffset | ||||
applied[newhunk.filename()].append(newhunk) | ||||
else: | ||||
fixoffset += chunk.removed - chunk.added | ||||
|
r43346 | return ( | ||
sum( | ||||
|
r43374 | [ | ||
h | ||||
for h in pycompat.itervalues(applied) | ||||
if h[0].special() or len(h) > 1 | ||||
], | ||||
|
r43346 | [], | ||
), | ||||
{}, | ||||
) | ||||
|
r8778 | class hunk(object): | ||
|
r14451 | def __init__(self, desc, num, lr, context): | ||
|
r4897 | self.number = num | ||
self.desc = desc | ||||
|
r10282 | self.hunk = [desc] | ||
|
r4897 | self.a = [] | ||
self.b = [] | ||||
|
r9682 | self.starta = self.lena = None | ||
self.startb = self.lenb = None | ||||
|
r10127 | if lr is not None: | ||
if context: | ||||
self.read_context_hunk(lr) | ||||
else: | ||||
self.read_unified_hunk(lr) | ||||
|
r4897 | |||
|
r10127 | def getnormalized(self): | ||
"""Return a copy with line endings normalized to LF.""" | ||||
def normalize(lines): | ||||
nlines = [] | ||||
for line in lines: | ||||
|
r43347 | if line.endswith(b'\r\n'): | ||
line = line[:-2] + b'\n' | ||||
|
r10127 | nlines.append(line) | ||
return nlines | ||||
# Dummy object, it is rebuilt manually | ||||
|
r14451 | nh = hunk(self.desc, self.number, None, None) | ||
|
r10127 | nh.number = self.number | ||
nh.desc = self.desc | ||||
|
r10524 | nh.hunk = self.hunk | ||
|
r10127 | nh.a = normalize(self.a) | ||
nh.b = normalize(self.b) | ||||
nh.starta = self.starta | ||||
nh.startb = self.startb | ||||
nh.lena = self.lena | ||||
nh.lenb = self.lenb | ||||
return nh | ||||
|
r4897 | def read_unified_hunk(self, lr): | ||
m = unidesc.match(self.desc) | ||||
if not m: | ||||
|
r43347 | raise PatchError(_(b"bad hunk #%d") % self.number) | ||
|
r15510 | self.starta, self.lena, self.startb, self.lenb = m.groups() | ||
|
r8527 | if self.lena is None: | ||
|
r4897 | self.lena = 1 | ||
else: | ||||
self.lena = int(self.lena) | ||||
|
r8527 | if self.lenb is None: | ||
|
r4897 | self.lenb = 1 | ||
else: | ||||
self.lenb = int(self.lenb) | ||||
self.starta = int(self.starta) | ||||
self.startb = int(self.startb) | ||||
|
r37591 | try: | ||
|
r43346 | diffhelper.addlines( | ||
lr, self.hunk, self.lena, self.lenb, self.a, self.b | ||||
) | ||||
|
r37591 | except error.ParseError as e: | ||
|
r43347 | raise PatchError(_(b"bad hunk #%d: %s") % (self.number, e)) | ||
|
r4897 | # if we hit eof before finishing out the hunk, the last line will | ||
# be zero length. Lets try to fix it up. | ||||
while len(self.hunk[-1]) == 0: | ||||
|
r6948 | del self.hunk[-1] | ||
del self.a[-1] | ||||
del self.b[-1] | ||||
self.lena -= 1 | ||||
self.lenb -= 1 | ||||
|
r13699 | self._fixnewline(lr) | ||
|
r4897 | |||
def read_context_hunk(self, lr): | ||||
self.desc = lr.readline() | ||||
m = contextdesc.match(self.desc) | ||||
if not m: | ||||
|
r43347 | raise PatchError(_(b"bad hunk #%d") % self.number) | ||
|
r15510 | self.starta, aend = m.groups() | ||
|
r4897 | self.starta = int(self.starta) | ||
|
r8527 | if aend is None: | ||
|
r4897 | aend = self.starta | ||
self.lena = int(aend) - self.starta | ||||
if self.starta: | ||||
self.lena += 1 | ||||
|
r38806 | for x in pycompat.xrange(self.lena): | ||
|
r4897 | l = lr.readline() | ||
|
r43347 | if l.startswith(b'---'): | ||
|
r12825 | # lines addition, old block is empty | ||
|
r4897 | lr.push(l) | ||
break | ||||
s = l[2:] | ||||
|
r43347 | if l.startswith(b'- ') or l.startswith(b'! '): | ||
u = b'-' + s | ||||
elif l.startswith(b' '): | ||||
u = b' ' + s | ||||
|
r4897 | else: | ||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b"bad hunk #%d old text line %d") % (self.number, x) | ||
|
r43346 | ) | ||
|
r4897 | self.a.append(u) | ||
self.hunk.append(u) | ||||
l = lr.readline() | ||||
|
r41675 | if l.startswith(br'\ '): | ||
|
r4897 | s = self.a[-1][:-1] | ||
self.a[-1] = s | ||||
self.hunk[-1] = s | ||||
l = lr.readline() | ||||
m = contextdesc.match(l) | ||||
if not m: | ||||
|
r43347 | raise PatchError(_(b"bad hunk #%d") % self.number) | ||
|
r15510 | self.startb, bend = m.groups() | ||
|
r4897 | self.startb = int(self.startb) | ||
|
r8527 | if bend is None: | ||
|
r4897 | bend = self.startb | ||
self.lenb = int(bend) - self.startb | ||||
if self.startb: | ||||
self.lenb += 1 | ||||
hunki = 1 | ||||
|
r38806 | for x in pycompat.xrange(self.lenb): | ||
|
r4897 | l = lr.readline() | ||
|
r41675 | if l.startswith(br'\ '): | ||
|
r12825 | # XXX: the only way to hit this is with an invalid line range. | ||
# The no-eol marker is not counted in the line range, but I | ||||
# guess there are diff(1) out there which behave differently. | ||||
|
r4897 | s = self.b[-1][:-1] | ||
self.b[-1] = s | ||||
|
r10282 | self.hunk[hunki - 1] = s | ||
|
r4897 | continue | ||
if not l: | ||||
|
r12825 | # line deletions, new block is empty and we hit EOF | ||
|
r4897 | lr.push(l) | ||
break | ||||
s = l[2:] | ||||
|
r43347 | if l.startswith(b'+ ') or l.startswith(b'! '): | ||
u = b'+' + s | ||||
elif l.startswith(b' '): | ||||
u = b' ' + s | ||||
|
r4897 | elif len(self.b) == 0: | ||
|
r12825 | # line deletions, new block is empty | ||
|
r4897 | lr.push(l) | ||
break | ||||
else: | ||||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b"bad hunk #%d old text line %d") % (self.number, x) | ||
|
r43346 | ) | ||
|
r4897 | self.b.append(s) | ||
while True: | ||||
if hunki >= len(self.hunk): | ||||
|
r43347 | h = b"" | ||
|
r4897 | else: | ||
h = self.hunk[hunki] | ||||
hunki += 1 | ||||
if h == u: | ||||
break | ||||
|
r43347 | elif h.startswith(b'-'): | ||
|
r4897 | continue | ||
else: | ||||
|
r10282 | self.hunk.insert(hunki - 1, u) | ||
|
r4897 | break | ||
if not self.a: | ||||
# this happens when lines were only added to the hunk | ||||
for x in self.hunk: | ||||
|
r43347 | if x.startswith(b'-') or x.startswith(b' '): | ||
|
r4897 | self.a.append(x) | ||
if not self.b: | ||||
# this happens when lines were only deleted from the hunk | ||||
for x in self.hunk: | ||||
|
r43347 | if x.startswith(b'+') or x.startswith(b' '): | ||
|
r4897 | self.b.append(x[1:]) | ||
# @@ -start,len +start,len @@ | ||||
|
r43347 | self.desc = b"@@ -%d,%d +%d,%d @@\n" % ( | ||
|
r43346 | self.starta, | ||
self.lena, | ||||
self.startb, | ||||
self.lenb, | ||||
) | ||||
|
r4897 | self.hunk[0] = self.desc | ||
|
r13699 | self._fixnewline(lr) | ||
|
r4897 | |||
|
r13699 | def _fixnewline(self, lr): | ||
l = lr.readline() | ||||
|
r41675 | if l.startswith(br'\ '): | ||
|
r37821 | diffhelper.fixnewline(self.hunk, self.a, self.b) | ||
|
r13699 | else: | ||
lr.push(l) | ||||
|
r4897 | |||
def complete(self): | ||||
return len(self.a) == self.lena and len(self.b) == self.lenb | ||||
|
r16121 | def _fuzzit(self, old, new, fuzz, toponly): | ||
|
r4897 | # this removes context lines from the top and bottom of list 'l'. It | ||
# checks the hunk to make sure only context lines are removed, and then | ||||
# returns a new shortened list of lines. | ||||
|
r16124 | fuzz = min(fuzz, len(old)) | ||
|
r4897 | if fuzz: | ||
top = 0 | ||||
bot = 0 | ||||
hlen = len(self.hunk) | ||||
|
r38806 | for x in pycompat.xrange(hlen - 1): | ||
|
r4897 | # the hunk starts with the @@ line, so use x+1 | ||
|
r43347 | if self.hunk[x + 1].startswith(b' '): | ||
|
r4897 | top += 1 | ||
else: | ||||
break | ||||
if not toponly: | ||||
|
r38806 | for x in pycompat.xrange(hlen - 1): | ||
|
r43347 | if self.hunk[hlen - bot - 1].startswith(b' '): | ||
|
r4897 | bot += 1 | ||
else: | ||||
break | ||||
|
r16124 | bot = min(fuzz, bot) | ||
top = min(fuzz, top) | ||||
|
r43346 | return old[top : len(old) - bot], new[top : len(new) - bot], top | ||
|
r16122 | return old, new, 0 | ||
|
r4897 | |||
|
r16121 | def fuzzit(self, fuzz, toponly): | ||
|
r16122 | old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly) | ||
oldstart = self.starta + top | ||||
newstart = self.startb + top | ||||
# zero length hunk ranges already have their start decremented | ||||
|
r16650 | if self.lena and oldstart > 0: | ||
|
r16122 | oldstart -= 1 | ||
|
r16650 | if self.lenb and newstart > 0: | ||
|
r16122 | newstart -= 1 | ||
return old, oldstart, new, newstart | ||||
|
r4897 | |||
|
r43346 | |||
|
r14764 | class binhunk(object): | ||
|
r44226 | """A binary patch file.""" | ||
|
r43346 | |||
|
r16523 | def __init__(self, lr, fname): | ||
|
r4897 | self.text = None | ||
|
r20137 | self.delta = False | ||
|
r43347 | self.hunk = [b'GIT binary patch\n'] | ||
|
r16523 | self._fname = fname | ||
|
r14384 | self._read(lr) | ||
|
r4897 | |||
def complete(self): | ||||
return self.text is not None | ||||
|
r20137 | def new(self, lines): | ||
if self.delta: | ||||
|
r43347 | return [applybindelta(self.text, b''.join(lines))] | ||
|
r4897 | return [self.text] | ||
|
r14384 | def _read(self, lr): | ||
|
r16524 | def getline(lr, hunk): | ||
l = lr.readline() | ||||
hunk.append(l) | ||||
|
r43347 | return l.rstrip(b'\r\n') | ||
|
r16524 | |||
|
r16567 | while True: | ||
|
r16524 | line = getline(lr, self.hunk) | ||
|
r16567 | if not line: | ||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b'could not extract "%s" binary data') % self._fname | ||
|
r43346 | ) | ||
|
r43347 | if line.startswith(b'literal '): | ||
|
r20137 | size = int(line[8:].rstrip()) | ||
|
r16567 | break | ||
|
r43347 | if line.startswith(b'delta '): | ||
|
r20137 | size = int(line[6:].rstrip()) | ||
self.delta = True | ||||
break | ||||
|
r3367 | dec = [] | ||
|
r16524 | line = getline(lr, self.hunk) | ||
|
r4897 | while len(line) > 1: | ||
|
r36210 | l = line[0:1] | ||
|
r43347 | if l <= b'Z' and l >= b'A': | ||
l = ord(l) - ord(b'A') + 1 | ||||
|
r3374 | else: | ||
|
r43347 | l = ord(l) - ord(b'a') + 27 | ||
|
r16522 | try: | ||
|
r32200 | dec.append(util.b85decode(line[1:])[:l]) | ||
|
r25660 | except ValueError as e: | ||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b'could not decode "%s" binary patch: %s') | ||
|
r43346 | % (self._fname, stringutil.forcebytestr(e)) | ||
) | ||||
|
r16524 | line = getline(lr, self.hunk) | ||
|
r43347 | text = zlib.decompress(b''.join(dec)) | ||
|
r3367 | if len(text) != size: | ||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b'"%s" length is %d bytes, should be %d') | ||
|
r43346 | % (self._fname, len(text), size) | ||
) | ||||
|
r4897 | self.text = text | ||
|
r3367 | |||
|
r43346 | |||
|
r4897 | def parsefilename(str): | ||
# --- filename \t|space stuff | ||||
|
r43347 | s = str[4:].rstrip(b'\r\n') | ||
i = s.find(b'\t') | ||||
|
r4897 | if i < 0: | ||
|
r43347 | i = s.find(b' ') | ||
|
r4897 | if i < 0: | ||
return s | ||||
return s[:i] | ||||
|
r2861 | |||
|
r43346 | |||
|
r25424 | def reversehunks(hunks): | ||
'''reverse the signs in the hunks given as argument | ||||
This function operates on hunks coming out of patch.filterpatch, that is | ||||
a list of the form: [header1, hunk1, hunk2, header2...]. Example usage: | ||||
|
r34133 | >>> rawpatch = b"""diff --git a/folder1/g b/folder1/g | ||
|
r25424 | ... --- a/folder1/g | ||
... +++ b/folder1/g | ||||
... @@ -1,7 +1,7 @@ | ||||
... +firstline | ||||
... c | ||||
... 1 | ||||
... 2 | ||||
... + 3 | ||||
... -4 | ||||
... 5 | ||||
... d | ||||
... +lastline""" | ||||
|
r34254 | >>> hunks = parsepatch([rawpatch]) | ||
|
r25424 | >>> hunkscomingfromfilterpatch = [] | ||
>>> for h in hunks: | ||||
... hunkscomingfromfilterpatch.append(h) | ||||
... hunkscomingfromfilterpatch.extend(h.hunks) | ||||
>>> reversedhunks = reversehunks(hunkscomingfromfilterpatch) | ||||
|
r28861 | >>> from . import util | ||
>>> fp = util.stringio() | ||||
|
r25424 | >>> for c in reversedhunks: | ||
... c.write(fp) | ||||
|
r34254 | >>> fp.seek(0) or None | ||
|
r25424 | >>> reversedpatch = fp.read() | ||
|
r34139 | >>> print(pycompat.sysstr(reversedpatch)) | ||
|
r25424 | diff --git a/folder1/g b/folder1/g | ||
--- a/folder1/g | ||||
+++ b/folder1/g | ||||
@@ -1,4 +1,3 @@ | ||||
-firstline | ||||
c | ||||
1 | ||||
2 | ||||
|
r32979 | @@ -2,6 +1,6 @@ | ||
|
r25424 | c | ||
1 | ||||
2 | ||||
- 3 | ||||
+4 | ||||
5 | ||||
d | ||||
|
r32979 | @@ -6,3 +5,2 @@ | ||
|
r25424 | 5 | ||
d | ||||
-lastline | ||||
''' | ||||
newhunks = [] | ||||
for c in hunks: | ||||
|
r43347 | if util.safehasattr(c, b'reversehunk'): | ||
|
r32979 | c = c.reversehunk() | ||
|
r25424 | newhunks.append(c) | ||
return newhunks | ||||
|
r43346 | |||
|
r33270 | def parsepatch(originalchunks, maxcontext=None): | ||
"""patch -> [] of headers -> [] of hunks | ||||
If maxcontext is not None, trim context lines if necessary. | ||||
|
r34133 | >>> rawpatch = b'''diff --git a/folder1/g b/folder1/g | ||
|
r33270 | ... --- a/folder1/g | ||
... +++ b/folder1/g | ||||
... @@ -1,8 +1,10 @@ | ||||
... 1 | ||||
... 2 | ||||
... -3 | ||||
... 4 | ||||
... 5 | ||||
... 6 | ||||
... +6.1 | ||||
... +6.2 | ||||
... 7 | ||||
... 8 | ||||
... +9''' | ||||
>>> out = util.stringio() | ||||
>>> headers = parsepatch([rawpatch], maxcontext=1) | ||||
>>> for header in headers: | ||||
... header.write(out) | ||||
... for hunk in header.hunks: | ||||
... hunk.write(out) | ||||
|
r34139 | >>> print(pycompat.sysstr(out.getvalue())) | ||
|
r33270 | diff --git a/folder1/g b/folder1/g | ||
--- a/folder1/g | ||||
+++ b/folder1/g | ||||
@@ -2,3 +2,2 @@ | ||||
2 | ||||
-3 | ||||
4 | ||||
@@ -6,2 +5,4 @@ | ||||
6 | ||||
+6.1 | ||||
+6.2 | ||||
7 | ||||
@@ -8,1 +9,2 @@ | ||||
8 | ||||
+9 | ||||
""" | ||||
|
r43346 | |||
|
r24265 | class parser(object): | ||
"""patch parsing state machine""" | ||||
|
r43346 | |||
|
r24265 | def __init__(self): | ||
self.fromline = 0 | ||||
self.toline = 0 | ||||
|
r43347 | self.proc = b'' | ||
|
r24265 | self.header = None | ||
self.context = [] | ||||
self.before = [] | ||||
self.hunk = [] | ||||
self.headers = [] | ||||
def addrange(self, limits): | ||||
|
r41497 | self.addcontext([]) | ||
|
r24265 | fromstart, fromend, tostart, toend, proc = limits | ||
self.fromline = int(fromstart) | ||||
self.toline = int(tostart) | ||||
self.proc = proc | ||||
def addcontext(self, context): | ||||
if self.hunk: | ||||
|
r43346 | h = recordhunk( | ||
self.header, | ||||
self.fromline, | ||||
self.toline, | ||||
self.proc, | ||||
self.before, | ||||
self.hunk, | ||||
context, | ||||
maxcontext, | ||||
) | ||||
|
r24265 | self.header.hunks.append(h) | ||
self.fromline += len(self.before) + h.removed | ||||
self.toline += len(self.before) + h.added | ||||
self.before = [] | ||||
self.hunk = [] | ||||
self.context = context | ||||
def addhunk(self, hunk): | ||||
if self.context: | ||||
self.before = self.context | ||||
self.context = [] | ||||
|
r41497 | if self.hunk: | ||
self.addcontext([]) | ||||
|
r24265 | self.hunk = hunk | ||
def newfile(self, hdr): | ||||
self.addcontext([]) | ||||
h = header(hdr) | ||||
self.headers.append(h) | ||||
self.header = h | ||||
def addother(self, line): | ||||
|
r43346 | pass # 'other' lines are ignored | ||
|
r24265 | |||
def finished(self): | ||||
self.addcontext([]) | ||||
return self.headers | ||||
transitions = { | ||||
|
r43347 | b'file': { | ||
b'context': addcontext, | ||||
b'file': newfile, | ||||
b'hunk': addhunk, | ||||
b'range': addrange, | ||||
|
r43346 | }, | ||
|
r43347 | b'context': { | ||
b'file': newfile, | ||||
b'hunk': addhunk, | ||||
b'range': addrange, | ||||
b'other': addother, | ||||
|
r43346 | }, | ||
|
r43347 | b'hunk': { | ||
b'context': addcontext, | ||||
b'file': newfile, | ||||
b'range': addrange, | ||||
}, | ||||
b'range': {b'context': addcontext, b'hunk': addhunk}, | ||||
b'other': {b'other': addother}, | ||||
|
r43346 | } | ||
|
r24265 | |||
p = parser() | ||||
|
r28861 | fp = stringio() | ||
|
r43347 | fp.write(b''.join(originalchunks)) | ||
|
r24341 | fp.seek(0) | ||
|
r24265 | |||
|
r43347 | state = b'context' | ||
|
r24265 | for newstate, data in scanpatch(fp): | ||
try: | ||||
p.transitions[state][newstate](p, data) | ||||
except KeyError: | ||||
|
r43346 | raise PatchError( | ||
|
r43347 | b'unhandled transition: %s -> %s' % (state, newstate) | ||
|
r43346 | ) | ||
|
r24265 | state = newstate | ||
|
r24341 | del fp | ||
|
r24265 | return p.finished() | ||
|
r43346 | |||
|
r24244 | def pathtransform(path, strip, prefix): | ||
|
r46554 | """turn a path from a patch into a path suitable for the repository | ||
|
r24243 | |||
|
r24244 | prefix, if not empty, is expected to be normalized with a / at the end. | ||
|
r24243 | Returns (stripped components, path in repository). | ||
|
r34133 | >>> pathtransform(b'a/b/c', 0, b'') | ||
|
r24243 | ('', 'a/b/c') | ||
|
r34133 | >>> pathtransform(b' a/b/c ', 0, b'') | ||
|
r24243 | ('', ' a/b/c') | ||
|
r34133 | >>> pathtransform(b' a/b/c ', 2, b'') | ||
|
r24243 | ('a/b/', 'c') | ||
|
r34133 | >>> pathtransform(b'a/b/c', 0, b'd/e/') | ||
|
r24385 | ('', 'd/e/a/b/c') | ||
|
r34133 | >>> pathtransform(b' a//b/c ', 2, b'd/e/') | ||
|
r24244 | ('a//b/', 'd/e/c') | ||
|
r34133 | >>> pathtransform(b'a/b/c', 3, b'') | ||
|
r24243 | Traceback (most recent call last): | ||
PatchError: unable to strip away 1 of 3 dirs from a/b/c | ||||
|
r46554 | """ | ||
|
r11022 | pathlen = len(path) | ||
i = 0 | ||||
if strip == 0: | ||||
|
r43347 | return b'', prefix + path.rstrip() | ||
|
r11022 | count = strip | ||
while count > 0: | ||||
|
r43347 | i = path.find(b'/', i) | ||
|
r11022 | if i == -1: | ||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b"unable to strip away %d of %d dirs from %s") | ||
|
r43346 | % (count, strip, path) | ||
) | ||||
|
r11022 | i += 1 | ||
# consume '//' in the path | ||||
|
r43347 | while i < pathlen - 1 and path[i : i + 1] == b'/': | ||
|
r11022 | i += 1 | ||
count -= 1 | ||||
|
r24244 | return path[:i].lstrip(), prefix + path[i:].rstrip() | ||
|
r11022 | |||
|
r43346 | |||
|
r24245 | def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip, prefix): | ||
|
r43347 | nulla = afile_orig == b"/dev/null" | ||
nullb = bfile_orig == b"/dev/null" | ||||
|
r14451 | create = nulla and hunk.starta == 0 and hunk.lena == 0 | ||
remove = nullb and hunk.startb == 0 and hunk.lenb == 0 | ||||
|
r24245 | abase, afile = pathtransform(afile_orig, strip, prefix) | ||
|
r14351 | gooda = not nulla and backend.exists(afile) | ||
|
r24245 | bbase, bfile = pathtransform(bfile_orig, strip, prefix) | ||
|
r4897 | if afile == bfile: | ||
goodb = gooda | ||||
else: | ||||
|
r14351 | goodb = not nullb and backend.exists(bfile) | ||
|
r14451 | missing = not goodb and not gooda and not create | ||
|
r9328 | |||
|
r11820 | # some diff programs apparently produce patches where the afile is | ||
# not /dev/null, but afile starts with bfile | ||||
|
r43347 | abasedir = afile[: afile.rfind(b'/') + 1] | ||
bbasedir = bfile[: bfile.rfind(b'/') + 1] | ||||
|
r43346 | if ( | ||
missing | ||||
and abasedir == bbasedir | ||||
and afile.startswith(bfile) | ||||
and hunk.starta == 0 | ||||
and hunk.lena == 0 | ||||
): | ||||
|
r14451 | create = True | ||
missing = False | ||||
|
r9328 | |||
|
r6295 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the | ||
# diff is between a file and its backup. In this case, the original | ||||
# file should be patched (see original mpatch code). | ||||
|
r43346 | isbackup = abase == bbase and bfile.startswith(afile) | ||
|
r5652 | fname = None | ||
if not missing: | ||||
if gooda and goodb: | ||||
|
r24306 | if isbackup: | ||
fname = afile | ||||
else: | ||||
fname = bfile | ||||
|
r5652 | elif gooda: | ||
|
r4897 | fname = afile | ||
|
r5760 | |||
|
r5652 | if not fname: | ||
if not nullb: | ||||
|
r24306 | if isbackup: | ||
fname = afile | ||||
else: | ||||
fname = bfile | ||||
|
r5652 | elif not nulla: | ||
|
r4897 | fname = afile | ||
|
r5652 | else: | ||
|
r43347 | raise PatchError(_(b"undefined source and destination files")) | ||
|
r5760 | |||
|
r14566 | gp = patchmeta(fname) | ||
if create: | ||||
|
r43347 | gp.op = b'ADD' | ||
|
r14566 | elif remove: | ||
|
r43347 | gp.op = b'DELETE' | ||
|
r14566 | return gp | ||
|
r4897 | |||
|
r43346 | |||
|
r24264 | def scanpatch(fp): | ||
"""like patch.iterhunks, but yield different events | ||||
- ('file', [header_lines + fromfile + tofile]) | ||||
- ('context', [context_lines]) | ||||
- ('hunk', [hunk_lines]) | ||||
- ('range', (-start,len, +start,len, proc)) | ||||
""" | ||||
|
r34068 | lines_re = re.compile(br'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)') | ||
|
r24264 | lr = linereader(fp) | ||
def scanwhile(first, p): | ||||
"""scan lr while predicate holds""" | ||||
lines = [first] | ||||
|
r43347 | for line in iter(lr.readline, b''): | ||
|
r24264 | if p(line): | ||
lines.append(line) | ||||
else: | ||||
lr.push(line) | ||||
break | ||||
return lines | ||||
|
r43347 | for line in iter(lr.readline, b''): | ||
if line.startswith(b'diff --git a/') or line.startswith(b'diff -r '): | ||||
|
r43346 | |||
|
r24264 | def notheader(line): | ||
s = line.split(None, 1) | ||||
|
r43347 | return not s or s[0] not in (b'---', b'diff') | ||
|
r43346 | |||
|
r24264 | header = scanwhile(line, notheader) | ||
fromfile = lr.readline() | ||||
|
r43347 | if fromfile.startswith(b'---'): | ||
|
r24264 | tofile = lr.readline() | ||
header += [fromfile, tofile] | ||||
else: | ||||
lr.push(fromfile) | ||||
|
r43347 | yield b'file', header | ||
elif line.startswith(b' '): | ||||
cs = (b' ', b'\\') | ||||
yield b'context', scanwhile(line, lambda l: l.startswith(cs)) | ||||
elif line.startswith((b'-', b'+')): | ||||
cs = (b'-', b'+', b'\\') | ||||
yield b'hunk', scanwhile(line, lambda l: l.startswith(cs)) | ||||
|
r24264 | else: | ||
m = lines_re.match(line) | ||||
if m: | ||||
|
r43347 | yield b'range', m.groups() | ||
|
r24264 | else: | ||
|
r43347 | yield b'other', line | ||
|
r24264 | |||
|
r43346 | |||
|
r7152 | def scangitpatch(lr, firstline): | ||
|
r7186 | """ | ||
|
r7152 | Git patches can emit: | ||
- rename a to b | ||||
- change b | ||||
- copy a to c | ||||
- change c | ||||
|
r7186 | |||
|
r7152 | We cannot apply this sequence as-is, the renamed 'a' could not be | ||
found for it would have been renamed already. And we cannot copy | ||||
from 'b' instead because 'b' would have been changed already. So | ||||
we scan the git patch for copy and rename commands so we can | ||||
perform the copies ahead of time. | ||||
""" | ||||
pos = 0 | ||||
try: | ||||
pos = lr.fp.tell() | ||||
fp = lr.fp | ||||
except IOError: | ||||
|
r28861 | fp = stringio(lr.fp.read()) | ||
|
r14418 | gitlr = linereader(fp) | ||
|
r7152 | gitlr.push(firstline) | ||
|
r12669 | gitpatches = readgitpatch(gitlr) | ||
|
r7152 | fp.seek(pos) | ||
|
r12669 | return gitpatches | ||
|
r7152 | |||
|
r43346 | |||
|
r14240 | def iterhunks(fp): | ||
|
r5650 | """Read a patch and yield the following events: | ||
- ("file", afile, bfile, firsthunk): select a new target file. | ||||
- ("hunk", hunk): a new hunk is ready to be applied, follows a | ||||
"file" event. | ||||
- ("git", gitchanges): current diff is in git format, gitchanges | ||||
maps filenames to gitpatch records. Unique event. | ||||
""" | ||||
|
r43347 | afile = b"" | ||
bfile = b"" | ||||
|
r4897 | state = None | ||
hunknum = 0 | ||||
|
r14017 | emitfile = newfile = False | ||
|
r14388 | gitpatches = None | ||
|
r2861 | |||
|
r4897 | # our states | ||
BFILE = 1 | ||||
context = None | ||||
|
r10128 | lr = linereader(fp) | ||
|
r2861 | |||
|
r43347 | for x in iter(lr.readline, b''): | ||
|
r14383 | if state == BFILE and ( | ||
|
r43347 | (not context and x.startswith(b'@')) | ||
or (context is not False and x.startswith(b'***************')) | ||||
or x.startswith(b'GIT binary patch') | ||||
|
r43346 | ): | ||
|
r14388 | gp = None | ||
|
r43346 | if gitpatches and gitpatches[-1].ispatching(afile, bfile): | ||
|
r16506 | gp = gitpatches.pop() | ||
|
r43347 | if x.startswith(b'GIT binary patch'): | ||
|
r16523 | h = binhunk(lr, gp.path) | ||
|
r14383 | else: | ||
|
r43347 | if context is None and x.startswith(b'***************'): | ||
|
r14383 | context = True | ||
|
r14451 | h = hunk(x, hunknum + 1, lr, context) | ||
|
r4897 | hunknum += 1 | ||
|
r5650 | if emitfile: | ||
emitfile = False | ||||
|
r43347 | yield b'file', (afile, bfile, h, gp and gp.copy() or None) | ||
yield b'hunk', h | ||||
elif x.startswith(b'diff --git a/'): | ||||
|
r46863 | m = gitre.match(x.rstrip(b'\r\n')) | ||
|
r14387 | if not m: | ||
continue | ||||
|
r16506 | if gitpatches is None: | ||
|
r14387 | # scan whole input for git metadata | ||
|
r16506 | gitpatches = scangitpatch(lr, x) | ||
|
r43347 | yield b'git', [ | ||
g.copy() for g in gitpatches if g.op in (b'COPY', b'RENAME') | ||||
|
r43346 | ] | ||
|
r14388 | gitpatches.reverse() | ||
|
r43347 | afile = b'a/' + m.group(1) | ||
bfile = b'b/' + m.group(2) | ||||
|
r16506 | while gitpatches and not gitpatches[-1].ispatching(afile, bfile): | ||
gp = gitpatches.pop() | ||||
|
r43347 | yield b'file', ( | ||
b'a/' + gp.path, | ||||
b'b/' + gp.path, | ||||
None, | ||||
gp.copy(), | ||||
) | ||||
|
r16506 | if not gitpatches: | ||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b'failed to synchronize metadata for "%s"') % afile[2:] | ||
|
r43346 | ) | ||
|
r14387 | newfile = True | ||
|
r43347 | elif x.startswith(b'---'): | ||
|
r4897 | # check for a unified diff | ||
l2 = lr.readline() | ||||
|
r43347 | if not l2.startswith(b'+++'): | ||
|
r4897 | lr.push(l2) | ||
continue | ||||
newfile = True | ||||
context = False | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
|
r43347 | elif x.startswith(b'***'): | ||
|
r4897 | # check for a context diff | ||
l2 = lr.readline() | ||||
|
r43347 | if not l2.startswith(b'---'): | ||
|
r4897 | lr.push(l2) | ||
continue | ||||
l3 = lr.readline() | ||||
lr.push(l3) | ||||
|
r43347 | if not l3.startswith(b"***************"): | ||
|
r4897 | lr.push(l2) | ||
continue | ||||
newfile = True | ||||
context = True | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
|
r3057 | |||
|
r14017 | if newfile: | ||
newfile = False | ||||
|
r5650 | emitfile = True | ||
|
r4897 | state = BFILE | ||
hunknum = 0 | ||||
|
r5650 | |||
|
r14388 | while gitpatches: | ||
|
r16506 | gp = gitpatches.pop() | ||
|
r43347 | yield b'file', (b'a/' + gp.path, b'b/' + gp.path, None, gp.copy()) | ||
|
r14388 | |||
|
r43346 | |||
|
r20137 | def applybindelta(binchunk, data): | ||
"""Apply a binary delta hunk | ||||
The algorithm used is the algorithm from git's patch-delta.c | ||||
""" | ||||
|
r43346 | |||
|
r20137 | def deltahead(binchunk): | ||
i = 0 | ||||
|
r38096 | for c in pycompat.bytestr(binchunk): | ||
|
r20137 | i += 1 | ||
if not (ord(c) & 0x80): | ||||
return i | ||||
return i | ||||
|
r43346 | |||
|
r43347 | out = b"" | ||
|
r20137 | s = deltahead(binchunk) | ||
binchunk = binchunk[s:] | ||||
s = deltahead(binchunk) | ||||
binchunk = binchunk[s:] | ||||
i = 0 | ||||
while i < len(binchunk): | ||||
|
r43346 | cmd = ord(binchunk[i : i + 1]) | ||
|
r20137 | i += 1 | ||
|
r43346 | if cmd & 0x80: | ||
|
r20137 | offset = 0 | ||
size = 0 | ||||
|
r43346 | if cmd & 0x01: | ||
offset = ord(binchunk[i : i + 1]) | ||||
|
r20137 | i += 1 | ||
|
r43346 | if cmd & 0x02: | ||
offset |= ord(binchunk[i : i + 1]) << 8 | ||||
|
r20137 | i += 1 | ||
|
r43346 | if cmd & 0x04: | ||
offset |= ord(binchunk[i : i + 1]) << 16 | ||||
|
r20137 | i += 1 | ||
|
r43346 | if cmd & 0x08: | ||
offset |= ord(binchunk[i : i + 1]) << 24 | ||||
|
r20137 | i += 1 | ||
|
r43346 | if cmd & 0x10: | ||
size = ord(binchunk[i : i + 1]) | ||||
|
r20137 | i += 1 | ||
|
r43346 | if cmd & 0x20: | ||
size |= ord(binchunk[i : i + 1]) << 8 | ||||
|
r20137 | i += 1 | ||
|
r43346 | if cmd & 0x40: | ||
size |= ord(binchunk[i : i + 1]) << 16 | ||||
|
r20137 | i += 1 | ||
if size == 0: | ||||
size = 0x10000 | ||||
offset_end = offset + size | ||||
out += data[offset:offset_end] | ||||
elif cmd != 0: | ||||
offset_end = i + cmd | ||||
out += binchunk[i:offset_end] | ||||
i += cmd | ||||
else: | ||||
|
r43347 | raise PatchError(_(b'unexpected delta opcode 0')) | ||
|
r20137 | return out | ||
|
r43346 | |||
|
r43347 | def applydiff(ui, fp, backend, store, strip=1, prefix=b'', eolmode=b'strict'): | ||
|
r10966 | """Reads a patch from fp and tries to apply it. | ||
|
r5650 | |||
|
r14565 | Returns 0 for a clean patch, -1 if any rejects were found and 1 if | ||
there was any fuzz. | ||||
|
r8810 | |||
|
r10101 | If 'eolmode' is 'strict', the patch content and patched file are | ||
read in binary mode. Otherwise, line endings are ignored when | ||||
patching then normalized according to 'eolmode'. | ||||
|
r8810 | """ | ||
|
r43346 | return _applydiff( | ||
ui, | ||||
fp, | ||||
patchfile, | ||||
backend, | ||||
store, | ||||
strip=strip, | ||||
prefix=prefix, | ||||
eolmode=eolmode, | ||||
) | ||||
|
r10966 | |||
|
r35053 | def _canonprefix(repo, prefix): | ||
if prefix: | ||||
prefix = pathutil.canonpath(repo.root, repo.getcwd(), prefix) | ||||
|
r43347 | if prefix != b'': | ||
prefix += b'/' | ||||
|
r35053 | return prefix | ||
|
r43346 | |||
def _applydiff( | ||||
|
r43347 | ui, fp, patcher, backend, store, strip=1, prefix=b'', eolmode=b'strict' | ||
|
r43346 | ): | ||
|
r35053 | prefix = _canonprefix(backend.repo, prefix) | ||
|
r43346 | |||
|
r14389 | def pstrip(p): | ||
|
r24246 | return pathtransform(p, strip - 1, prefix)[1] | ||
|
r14389 | |||
|
r5650 | rejects = 0 | ||
err = 0 | ||||
current_file = None | ||||
|
r14240 | for state, values in iterhunks(fp): | ||
|
r43347 | if state == b'hunk': | ||
|
r5650 | if not current_file: | ||
continue | ||||
|
r11021 | ret = current_file.apply(values) | ||
|
r14565 | if ret > 0: | ||
err = 1 | ||||
|
r43347 | elif state == b'file': | ||
|
r13701 | if current_file: | ||
rejects += current_file.close() | ||||
|
r14388 | current_file = None | ||
afile, bfile, first_hunk, gp = values | ||||
if gp: | ||||
|
r14566 | gp.path = pstrip(gp.path) | ||
|
r14452 | if gp.oldpath: | ||
|
r14566 | gp.oldpath = pstrip(gp.oldpath) | ||
else: | ||||
|
r43346 | gp = makepatchmeta( | ||
backend, afile, bfile, first_hunk, strip, prefix | ||||
) | ||||
|
r43347 | if gp.op == b'RENAME': | ||
|
r14566 | backend.unlink(gp.oldpath) | ||
|
r14388 | if not first_hunk: | ||
|
r43347 | if gp.op == b'DELETE': | ||
|
r14566 | backend.unlink(gp.path) | ||
continue | ||||
data, mode = None, None | ||||
|
r43347 | if gp.op in (b'RENAME', b'COPY'): | ||
|
r14609 | data, mode = store.getfile(gp.oldpath)[:2] | ||
|
r30078 | if data is None: | ||
# This means that the old path does not exist | ||||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b"source file '%s' does not exist") % gp.oldpath | ||
|
r43346 | ) | ||
|
r14566 | if gp.mode: | ||
mode = gp.mode | ||||
|
r43347 | if gp.op == b'ADD': | ||
|
r14566 | # Added files without content have no hunk and | ||
# must be created | ||||
|
r43347 | data = b'' | ||
|
r14566 | if data or mode: | ||
|
r43347 | if gp.op in (b'ADD', b'RENAME', b'COPY') and backend.exists( | ||
|
r43346 | gp.path | ||
): | ||||
raise PatchError( | ||||
|
r43347 | _( | ||
b"cannot create %s: destination " | ||||
b"already exists" | ||||
) | ||||
|
r43346 | % gp.path | ||
) | ||||
|
r14566 | backend.setfile(gp.path, data, mode, gp.oldpath) | ||
|
r14388 | continue | ||
|
r5650 | try: | ||
|
r43346 | current_file = patcher(ui, gp, backend, store, eolmode=eolmode) | ||
|
r25660 | except PatchError as inst: | ||
|
r44127 | ui.warn(stringutil.forcebytestr(inst) + b'\n') | ||
|
r11021 | current_file = None | ||
|
r5650 | rejects += 1 | ||
continue | ||||
|
r43347 | elif state == b'git': | ||
|
r11021 | for gp in values: | ||
|
r14452 | path = pstrip(gp.oldpath) | ||
|
r22296 | data, mode = backend.getfile(path) | ||
if data is None: | ||||
|
r16813 | # The error ignored here will trigger a getfile() | ||
# error in a place more appropriate for error | ||||
# handling, and will not interrupt the patching | ||||
# process. | ||||
|
r22296 | pass | ||
|
r16813 | else: | ||
store.setfile(path, data, mode) | ||||
|
r4897 | else: | ||
|
r43347 | raise error.Abort(_(b'unsupported parser state: %s') % state) | ||
|
r5649 | |||
|
r13701 | if current_file: | ||
rejects += current_file.close() | ||||
|
r5650 | |||
|
r4897 | if rejects: | ||
return -1 | ||||
return err | ||||
|
r2874 | |||
|
r43346 | |||
def _externalpatch(ui, repo, patcher, patchname, strip, files, similarity): | ||||
|
r7151 | """use <patcher> to apply <patchname> to the working directory. | ||
returns whether patch was applied with fuzz factor.""" | ||||
fuzz = False | ||||
|
r12673 | args = [] | ||
|
r14382 | cwd = repo.root | ||
|
r7151 | if cwd: | ||
|
r43347 | args.append(b'-d %s' % procutil.shellquote(cwd)) | ||
cmd = b'%s %s -p%d < %s' % ( | ||||
|
r43346 | patcher, | ||
|
r43347 | b' '.join(args), | ||
|
r43346 | strip, | ||
procutil.shellquote(patchname), | ||||
) | ||||
|
r43347 | ui.debug(b'Using external patch tool: %s\n' % cmd) | ||
fp = procutil.popen(cmd, b'rb') | ||||
|
r14381 | try: | ||
|
r30397 | for line in util.iterfile(fp): | ||
|
r14381 | line = line.rstrip() | ||
|
r43347 | ui.note(line + b'\n') | ||
if line.startswith(b'patching file '): | ||||
|
r14381 | pf = util.parsepatchoutput(line) | ||
printed_file = False | ||||
|
r14564 | files.add(pf) | ||
|
r43347 | elif line.find(b'with fuzz') >= 0: | ||
|
r14381 | fuzz = True | ||
if not printed_file: | ||||
|
r43347 | ui.warn(pf + b'\n') | ||
|
r14381 | printed_file = True | ||
|
r43347 | ui.warn(line + b'\n') | ||
elif line.find(b'saving rejects to file') >= 0: | ||||
ui.warn(line + b'\n') | ||||
elif line.find(b'FAILED') >= 0: | ||||
|
r14381 | if not printed_file: | ||
|
r43347 | ui.warn(pf + b'\n') | ||
|
r14381 | printed_file = True | ||
|
r43347 | ui.warn(line + b'\n') | ||
|
r14381 | finally: | ||
if files: | ||||
|
r19155 | scmutil.marktouched(repo, files, similarity) | ||
|
r7151 | code = fp.close() | ||
if code: | ||||
|
r43346 | raise PatchError( | ||
|
r43347 | _(b"patch command failed: %s") % procutil.explainexit(code) | ||
|
r43346 | ) | ||
|
r7151 | return fuzz | ||
|
r43346 | |||
def patchbackend( | ||||
|
r43347 | ui, backend, patchobj, strip, prefix, files=None, eolmode=b'strict' | ||
|
r43346 | ): | ||
|
r9683 | if files is None: | ||
|
r14564 | files = set() | ||
|
r8810 | if eolmode is None: | ||
|
r43347 | eolmode = ui.config(b'patch', b'eol') | ||
|
r10101 | if eolmode.lower() not in eolmodes: | ||
|
r43347 | raise error.Abort(_(b'unsupported line endings type: %s') % eolmode) | ||
|
r10101 | eolmode = eolmode.lower() | ||
|
r8843 | |||
|
r14452 | store = filestore() | ||
|
r7151 | try: | ||
|
r43347 | fp = open(patchobj, b'rb') | ||
|
r7151 | except TypeError: | ||
fp = patchobj | ||||
try: | ||||
|
r43346 | ret = applydiff( | ||
ui, fp, backend, store, strip=strip, prefix=prefix, eolmode=eolmode | ||||
) | ||||
|
r7151 | finally: | ||
|
r10203 | if fp != patchobj: | ||
fp.close() | ||||
|
r14564 | files.update(backend.close()) | ||
|
r14452 | store.close() | ||
|
r7151 | if ret < 0: | ||
|
r43347 | raise PatchError(_(b'patch failed to apply')) | ||
|
r7151 | return ret > 0 | ||
|
r43346 | |||
def internalpatch( | ||||
ui, | ||||
repo, | ||||
patchobj, | ||||
strip, | ||||
|
r43347 | prefix=b'', | ||
|
r43346 | files=None, | ||
|
r43347 | eolmode=b'strict', | ||
|
r43346 | similarity=0, | ||
): | ||||
|
r14611 | """use builtin patch to apply <patchobj> to the working directory. | ||
returns whether patch was applied with fuzz factor.""" | ||||
backend = workingbackend(ui, repo, similarity) | ||||
|
r24254 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) | ||
|
r14611 | |||
|
r43346 | |||
def patchrepo( | ||||
|
r43347 | ui, repo, ctx, store, patchobj, strip, prefix, files=None, eolmode=b'strict' | ||
|
r43346 | ): | ||
|
r14611 | backend = repobackend(ui, repo, ctx, store) | ||
|
r24260 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) | ||
|
r14611 | |||
|
r43346 | |||
def patch( | ||||
ui, | ||||
repo, | ||||
patchname, | ||||
strip=1, | ||||
|
r43347 | prefix=b'', | ||
|
r43346 | files=None, | ||
|
r43347 | eolmode=b'strict', | ||
|
r43346 | similarity=0, | ||
): | ||||
|
r8810 | """Apply <patchname> to the working directory. | ||
'eolmode' specifies how end of lines should be handled. It can be: | ||||
- 'strict': inputs are read in binary mode, EOLs are preserved | ||||
- 'crlf': EOLs are ignored when patching and reset to CRLF | ||||
- 'lf': EOLs are ignored when patching and reset to LF | ||||
- None: get it from user settings, default to 'strict' | ||||
'eolmode' is ignored when using an external patcher program. | ||||
Returns whether patch was applied with fuzz factor. | ||||
""" | ||||
|
r43347 | patcher = ui.config(b'ui', b'patch') | ||
|
r9683 | if files is None: | ||
|
r14564 | files = set() | ||
|
r21553 | if patcher: | ||
|
r43346 | return _externalpatch( | ||
ui, repo, patcher, patchname, strip, files, similarity | ||||
) | ||||
return internalpatch( | ||||
ui, repo, patchname, strip, prefix, files, eolmode, similarity | ||||
) | ||||
|
r7151 | |||
|
r43347 | def changedfiles(ui, repo, patchpath, strip=1, prefix=b''): | ||
|
r14351 | backend = fsbackend(ui, repo.root) | ||
|
r35053 | prefix = _canonprefix(repo, prefix) | ||
|
r43347 | with open(patchpath, b'rb') as fp: | ||
|
r14255 | changed = set() | ||
for state, values in iterhunks(fp): | ||||
|
r43347 | if state == b'file': | ||
|
r14388 | afile, bfile, first_hunk, gp = values | ||
if gp: | ||||
|
r35053 | gp.path = pathtransform(gp.path, strip - 1, prefix)[1] | ||
|
r14566 | if gp.oldpath: | ||
|
r43346 | gp.oldpath = pathtransform( | ||
gp.oldpath, strip - 1, prefix | ||||
)[1] | ||||
|
r14566 | else: | ||
|
r43346 | gp = makepatchmeta( | ||
backend, afile, bfile, first_hunk, strip, prefix | ||||
) | ||||
|
r14566 | changed.add(gp.path) | ||
|
r43347 | if gp.op == b'RENAME': | ||
|
r14566 | changed.add(gp.oldpath) | ||
|
r43347 | elif state not in (b'hunk', b'git'): | ||
raise error.Abort(_(b'unsupported parser state: %s') % state) | ||||
|
r14255 | return changed | ||
|
r43346 | |||
|
r10189 | class GitDiffRequired(Exception): | ||
pass | ||||
|
r7198 | |||
|
r43346 | |||
|
r38606 | diffopts = diffutil.diffallopts | ||
|
r38581 | diffallopts = diffutil.diffallopts | ||
difffeatureopts = diffutil.difffeatureopts | ||||
|
r10615 | |||
|
r43346 | |||
def diff( | ||||
repo, | ||||
node1=None, | ||||
node2=None, | ||||
match=None, | ||||
changes=None, | ||||
opts=None, | ||||
losedatafn=None, | ||||
pathfn=None, | ||||
copy=None, | ||||
copysourcematch=None, | ||||
hunksfilterfn=None, | ||||
): | ||||
|
r46554 | """yields diff of changes to files between two nodes, or node and | ||
|
r2874 | working directory. | ||
if node1 is None, use first dirstate parent instead. | ||||
|
r10189 | if node2 is None, compare node1 with working directory. | ||
losedatafn(**kwarg) is a callable run when opts.upgrade=True and | ||||
every time some change cannot be represented with the current | ||||
patch format. Return False to upgrade to git patch format, True to | ||||
accept the loss or raise an exception to abort the diff. It is | ||||
called with the name of current file being diffed as 'fn'. If set | ||||
to None, patches will always be upgraded to git format when | ||||
necessary. | ||||
|
r12167 | |||
prefix is a filename prefix that is prepended to all filenames on | ||||
display (used for subrepos). | ||||
|
r24417 | |||
relroot, if not empty, must be normalized with a trailing /. Any match | ||||
|
r29422 | patterns that fall outside it will be ignored. | ||
copy, if not empty, should contain mappings {dst@y: src@x} of copy | ||||
|
r34857 | information. | ||
|
r41769 | if copysourcematch is not None, then copy sources will be filtered by this | ||
matcher | ||||
|
r34857 | hunksfilterfn, if not None, should be a function taking a filectx and | ||
hunks generator that may yield filtered hunks. | ||||
|
r46554 | """ | ||
|
r41767 | if not node1 and not node2: | ||
node1 = repo.dirstate.p1() | ||||
ctx1 = repo[node1] | ||||
ctx2 = repo[node2] | ||||
|
r34856 | for fctx1, fctx2, hdr, hunks in diffhunks( | ||
|
r43346 | repo, | ||
ctx1=ctx1, | ||||
ctx2=ctx2, | ||||
match=match, | ||||
changes=changes, | ||||
opts=opts, | ||||
losedatafn=losedatafn, | ||||
pathfn=pathfn, | ||||
copy=copy, | ||||
copysourcematch=copysourcematch, | ||||
): | ||||
|
r34857 | if hunksfilterfn is not None: | ||
|
r34909 | # If the file has been removed, fctx2 is None; but this should | ||
# not occur here since we catch removed files early in | ||||
|
r35906 | # logcmdutil.getlinerangerevs() for 'hg log -L'. | ||
|
r43346 | assert ( | ||
fctx2 is not None | ||||
|
r43347 | ), b'fctx2 unexpectly None in diff hunks filtering' | ||
|
r34857 | hunks = hunksfilterfn(fctx2, hunks) | ||
|
r45266 | text = b''.join(b''.join(hlines) for hrange, hlines in hunks) | ||
|
r34562 | if hdr and (text or len(hdr) > 1): | ||
|
r43347 | yield b'\n'.join(hdr) + b'\n' | ||
|
r31274 | if text: | ||
yield text | ||||
|
r43346 | |||
def diffhunks( | ||||
repo, | ||||
ctx1, | ||||
ctx2, | ||||
match=None, | ||||
changes=None, | ||||
opts=None, | ||||
losedatafn=None, | ||||
pathfn=None, | ||||
copy=None, | ||||
copysourcematch=None, | ||||
): | ||||
|
r31274 | """Yield diff of changes to files in the form of (`header`, `hunks`) tuples | ||
where `header` is a list of diff headers and `hunks` is an iterable of | ||||
(`hunkrange`, `hunklines`) tuples. | ||||
See diff() for the meaning of parameters. | ||||
""" | ||||
|
r2874 | |||
if opts is None: | ||||
opts = mdiff.defaultopts | ||||
|
r9123 | def lrugetfilectx(): | ||
cache = {} | ||||
|
r25113 | order = collections.deque() | ||
|
r43346 | |||
|
r9123 | def getfilectx(f, ctx): | ||
fctx = ctx.filectx(f, filelog=cache.get(f)) | ||||
if f not in cache: | ||||
if len(cache) > 20: | ||||
|
r16803 | del cache[order.popleft()] | ||
|
r9684 | cache[f] = fctx.filelog() | ||
|
r9123 | else: | ||
order.remove(f) | ||||
order.append(f) | ||||
return fctx | ||||
|
r43346 | |||
|
r9123 | return getfilectx | ||
|
r43346 | |||
|
r9123 | getfilectx = lrugetfilectx() | ||
|
r2934 | |||
|
r2874 | if not changes: | ||
|
r38796 | changes = ctx1.status(ctx2, match=match) | ||
|
r44048 | if isinstance(changes, list): | ||
modified, added, removed = changes[:3] | ||||
else: | ||||
modified, added, removed = ( | ||||
changes.modified, | ||||
changes.added, | ||||
changes.removed, | ||||
) | ||||
|
r2874 | |||
if not modified and not added and not removed: | ||||
|
r10189 | return [] | ||
|
r24306 | if repo.ui.debugflag: | ||
hexfunc = hex | ||||
else: | ||||
hexfunc = short | ||||
|
r21833 | revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node] | ||
|
r10189 | |||
|
r29422 | if copy is None: | ||
copy = {} | ||||
if opts.git or opts.upgrade: | ||||
copy = copies.pathcopies(ctx1, ctx2, match=match) | ||||
|
r10189 | |||
|
r41769 | if copysourcematch: | ||
# filter out copies where source side isn't inside the matcher | ||||
|
r41640 | # (copies.pathcopies() already filtered out the destination) | ||
|
r43346 | copy = { | ||
|
r43376 | dst: src | ||
for dst, src in pycompat.iteritems(copy) | ||||
if copysourcematch(src) | ||||
|
r43346 | } | ||
|
r24417 | |||
|
r27900 | modifiedset = set(modified) | ||
addedset = set(added) | ||||
|
r27901 | removedset = set(removed) | ||
|
r27900 | for f in modified: | ||
if f not in ctx1: | ||||
# Fix up added, since merged-in additions appear as | ||||
# modifications during merges | ||||
modifiedset.remove(f) | ||||
addedset.add(f) | ||||
|
r27901 | for f in removed: | ||
if f not in ctx1: | ||||
# Merged-in additions that are then removed are reported as removed. | ||||
# They are not in ctx1, so We don't want to show them in the diff. | ||||
removedset.remove(f) | ||||
|
r27900 | modified = sorted(modifiedset) | ||
added = sorted(addedset) | ||||
|
r27901 | removed = sorted(removedset) | ||
|
r35607 | for dst, src in list(copy.items()): | ||
|
r27902 | if src not in ctx1: | ||
# Files merged in during a merge and then copied/renamed are | ||||
# reported as copies. We want to show them in the diff as additions. | ||||
del copy[dst] | ||||
|
r27900 | |||
|
r37782 | prefetchmatch = scmutil.matchfiles( | ||
|
r43346 | repo, list(modifiedset | addedset | removedset) | ||
) | ||||
|
r45632 | revmatches = [ | ||
(ctx1.rev(), prefetchmatch), | ||||
(ctx2.rev(), prefetchmatch), | ||||
] | ||||
scmutil.prefetchfiles(repo, revmatches) | ||||
|
r37782 | |||
|
r17299 | def difffn(opts, losedata): | ||
|
r43346 | return trydiff( | ||
repo, | ||||
revs, | ||||
ctx1, | ||||
ctx2, | ||||
modified, | ||||
added, | ||||
removed, | ||||
copy, | ||||
getfilectx, | ||||
opts, | ||||
losedata, | ||||
pathfn, | ||||
) | ||||
|
r10189 | if opts.upgrade and not opts.git: | ||
try: | ||||
|
r43346 | |||
|
r10189 | def losedata(fn): | ||
if not losedatafn or not losedatafn(fn=fn): | ||||
|
r16687 | raise GitDiffRequired | ||
|
r43346 | |||
|
r10189 | # Buffer the whole output until we are sure it can be generated | ||
return list(difffn(opts.copy(git=False), losedata)) | ||||
except GitDiffRequired: | ||||
return difffn(opts.copy(git=True), None) | ||||
else: | ||||
return difffn(opts, None) | ||||
|
r43346 | |||
|
r37749 | def diffsinglehunk(hunklines): | ||
"""yield tokens for a list of lines in a single hunk""" | ||||
for line in hunklines: | ||||
# chomp | ||||
|
r43347 | chompline = line.rstrip(b'\r\n') | ||
|
r37749 | # highlight tabs and trailing whitespace | ||
stripline = chompline.rstrip() | ||||
|
r43347 | if line.startswith(b'-'): | ||
label = b'diff.deleted' | ||||
elif line.startswith(b'+'): | ||||
label = b'diff.inserted' | ||||
|
r37749 | else: | ||
|
r43347 | raise error.ProgrammingError(b'unexpected hunk line: %s' % line) | ||
|
r37749 | for token in tabsplitter.findall(stripline): | ||
|
r43347 | if token.startswith(b'\t'): | ||
yield (token, b'diff.tab') | ||||
|
r37749 | else: | ||
yield (token, label) | ||||
if chompline != stripline: | ||||
|
r43347 | yield (chompline[len(stripline) :], b'diff.trailingwhitespace') | ||
|
r37749 | if chompline != line: | ||
|
r43347 | yield (line[len(chompline) :], b'') | ||
|
r43346 | |||
|
r37749 | |||
|
r37750 | def diffsinglehunkinline(hunklines): | ||
"""yield tokens for a list of lines in a single hunk, with inline colors""" | ||||
# prepare deleted, and inserted content | ||||
|
r46624 | a = bytearray() | ||
b = bytearray() | ||||
|
r37750 | for line in hunklines: | ||
|
r43347 | if line[0:1] == b'-': | ||
|
r37750 | a += line[1:] | ||
|
r43347 | elif line[0:1] == b'+': | ||
|
r37750 | b += line[1:] | ||
else: | ||||
|
r43347 | raise error.ProgrammingError(b'unexpected hunk line: %s' % line) | ||
|
r37750 | # fast path: if either side is empty, use diffsinglehunk | ||
if not a or not b: | ||||
for t in diffsinglehunk(hunklines): | ||||
yield t | ||||
return | ||||
# re-split the content into words | ||||
|
r46624 | al = wordsplitter.findall(bytes(a)) | ||
bl = wordsplitter.findall(bytes(b)) | ||||
|
r37750 | # re-arrange the words to lines since the diff algorithm is line-based | ||
|
r43347 | aln = [s if s == b'\n' else s + b'\n' for s in al] | ||
bln = [s if s == b'\n' else s + b'\n' for s in bl] | ||||
an = b''.join(aln) | ||||
bn = b''.join(bln) | ||||
|
r37750 | # run the diff algorithm, prepare atokens and btokens | ||
atokens = [] | ||||
btokens = [] | ||||
blocks = mdiff.allblocks(an, bn, lines1=aln, lines2=bln) | ||||
for (a1, a2, b1, b2), btype in blocks: | ||||
|
r43347 | changed = btype == b'!' | ||
for token in mdiff.splitnewlines(b''.join(al[a1:a2])): | ||||
|
r37750 | atokens.append((changed, token)) | ||
|
r43347 | for token in mdiff.splitnewlines(b''.join(bl[b1:b2])): | ||
|
r37750 | btokens.append((changed, token)) | ||
# yield deleted tokens, then inserted ones | ||||
|
r43346 | for prefix, label, tokens in [ | ||
|
r43347 | (b'-', b'diff.deleted', atokens), | ||
(b'+', b'diff.inserted', btokens), | ||||
|
r43346 | ]: | ||
|
r37750 | nextisnewline = True | ||
for changed, token in tokens: | ||||
if nextisnewline: | ||||
yield (prefix, label) | ||||
nextisnewline = False | ||||
# special handling line end | ||||
|
r43347 | isendofline = token.endswith(b'\n') | ||
|
r37750 | if isendofline: | ||
|
r43346 | chomp = token[:-1] # chomp | ||
|
r43347 | if chomp.endswith(b'\r'): | ||
|
r38649 | chomp = chomp[:-1] | ||
|
r43346 | endofline = token[len(chomp) :] | ||
token = chomp.rstrip() # detect spaces at the end | ||||
endspaces = chomp[len(token) :] | ||||
|
r37750 | # scan tabs | ||
for maybetab in tabsplitter.findall(token): | ||||
|
r40318 | if b'\t' == maybetab[0:1]: | ||
|
r43347 | currentlabel = b'diff.tab' | ||
|
r37750 | else: | ||
if changed: | ||||
|
r43347 | currentlabel = label + b'.changed' | ||
|
r37750 | else: | ||
|
r43347 | currentlabel = label + b'.unchanged' | ||
|
r37750 | yield (maybetab, currentlabel) | ||
if isendofline: | ||||
if endspaces: | ||||
|
r43347 | yield (endspaces, b'diff.trailingwhitespace') | ||
yield (endofline, b'') | ||||
|
r37750 | nextisnewline = True | ||
|
r43346 | |||
|
r10818 | def difflabel(func, *args, **kw): | ||
'''yields 2-tuples of (output, label) based on the output of func()''' | ||||
|
r43906 | if kw.get('opts') and kw['opts'].worddiff: | ||
|
r37750 | dodiffhunk = diffsinglehunkinline | ||
else: | ||||
dodiffhunk = diffsinglehunk | ||||
|
r43346 | headprefixes = [ | ||
|
r43347 | (b'diff', b'diff.diffline'), | ||
(b'copy', b'diff.extended'), | ||||
(b'rename', b'diff.extended'), | ||||
(b'old', b'diff.extended'), | ||||
(b'new', b'diff.extended'), | ||||
(b'deleted', b'diff.extended'), | ||||
(b'index', b'diff.extended'), | ||||
(b'similarity', b'diff.extended'), | ||||
(b'---', b'diff.file_a'), | ||||
(b'+++', b'diff.file_b'), | ||||
|
r43346 | ] | ||
textprefixes = [ | ||||
|
r43347 | (b'@', b'diff.hunk'), | ||
|
r43346 | # - and + are handled by diffsinglehunk | ||
] | ||||
|
r15201 | head = False | ||
|
r37749 | |||
# buffers a hunk, i.e. adjacent "-", "+" lines without other changes. | ||||
hunkbuffer = [] | ||||
|
r43346 | |||
|
r37749 | def consumehunkbuffer(): | ||
if hunkbuffer: | ||||
|
r37750 | for token in dodiffhunk(hunkbuffer): | ||
|
r37749 | yield token | ||
hunkbuffer[:] = [] | ||||
|
r10818 | for chunk in func(*args, **kw): | ||
|
r43347 | lines = chunk.split(b'\n') | ||
|
r37748 | linecount = len(lines) | ||
|
r10818 | for i, line in enumerate(lines): | ||
|
r15201 | if head: | ||
|
r43347 | if line.startswith(b'@'): | ||
|
r15201 | head = False | ||
else: | ||||
|
r43347 | if line and not line.startswith( | ||
(b' ', b'+', b'-', b'@', b'\\') | ||||
): | ||||
|
r15201 | head = True | ||
|
r22460 | diffline = False | ||
|
r43347 | if not head and line and line.startswith((b'+', b'-')): | ||
|
r22460 | diffline = True | ||
|
r15201 | prefixes = textprefixes | ||
if head: | ||||
prefixes = headprefixes | ||||
|
r37749 | if diffline: | ||
# buffered | ||||
bufferedline = line | ||||
if i + 1 < linecount: | ||||
|
r43347 | bufferedline += b"\n" | ||
|
r37749 | hunkbuffer.append(bufferedline) | ||
|
r10818 | else: | ||
|
r37749 | # unbuffered | ||
for token in consumehunkbuffer(): | ||||
yield token | ||||
stripline = line.rstrip() | ||||
for prefix, label in prefixes: | ||||
if stripline.startswith(prefix): | ||||
yield (stripline, label) | ||||
if line != stripline: | ||||
|
r43346 | yield ( | ||
line[len(stripline) :], | ||||
|
r43347 | b'diff.trailingwhitespace', | ||
|
r43346 | ) | ||
|
r35278 | break | ||
|
r37749 | else: | ||
|
r43347 | yield (line, b'') | ||
|
r37749 | if i + 1 < linecount: | ||
|
r43347 | yield (b'\n', b'') | ||
|
r37749 | for token in consumehunkbuffer(): | ||
yield token | ||||
|
r35278 | |||
|
r43346 | |||
|
r10818 | def diffui(*args, **kw): | ||
'''like diff(), but yields 2-tuples of (output, label) for ui.write()''' | ||||
return difflabel(diff, *args, **kw) | ||||
|
r43346 | |||
|
r27900 | def _filepairs(modified, added, removed, copy, opts): | ||
|
r46554 | """generates tuples (f1, f2, copyop), where f1 is the name of the file | ||
|
r24106 | before and f2 is the the name after. For added files, f1 will be None, | ||
and for removed files, f2 will be None. copyop may be set to None, 'copy' | ||||
|
r46554 | or 'rename' (the latter two only if opts.git is set).""" | ||
|
r24106 | gone = set() | ||
|
r44937 | copyto = {v: k for k, v in copy.items()} | ||
|
r24106 | |||
addedset, removedset = set(added), set(removed) | ||||
for f in sorted(modified + added + removed): | ||||
copyop = None | ||||
f1, f2 = f, f | ||||
if f in addedset: | ||||
f1 = None | ||||
if f in copy: | ||||
if opts.git: | ||||
f1 = copy[f] | ||||
if f1 in removedset and f1 not in gone: | ||||
|
r43347 | copyop = b'rename' | ||
|
r24106 | gone.add(f1) | ||
else: | ||||
|
r43347 | copyop = b'copy' | ||
|
r24106 | elif f in removedset: | ||
f2 = None | ||||
if opts.git: | ||||
# have we already reported a copy above? | ||||
|
r43346 | if ( | ||
f in copyto | ||||
and copyto[f] in addedset | ||||
and copy[copyto[f]] == f | ||||
): | ||||
|
r24106 | continue | ||
yield f1, f2, copyop | ||||
|
r43346 | |||
|
r45658 | def _gitindex(text): | ||
if not text: | ||||
text = b"" | ||||
l = len(text) | ||||
s = hashutil.sha1(b'blob %d\0' % l) | ||||
s.update(text) | ||||
return hex(s.digest()) | ||||
_gitmode = {b'l': b'120000', b'x': b'100755', b'': b'100644'} | ||||
|
r43346 | def trydiff( | ||
repo, | ||||
revs, | ||||
ctx1, | ||||
ctx2, | ||||
modified, | ||||
added, | ||||
removed, | ||||
copy, | ||||
getfilectx, | ||||
opts, | ||||
losedatafn, | ||||
pathfn, | ||||
): | ||||
|
r46554 | """given input data, generate a diff and yield it in blocks | ||
|
r24371 | |||
If generating a diff would lose data like flags or binary data and | ||||
losedatafn is not None, it will be called. | ||||
|
r41795 | pathfn is applied to every path in the diff output. | ||
|
r46554 | """ | ||
|
r12167 | |||
|
r23300 | if opts.noprefix: | ||
|
r43347 | aprefix = bprefix = b'' | ||
|
r23300 | else: | ||
|
r43347 | aprefix = b'a/' | ||
bprefix = b'b/' | ||||
|
r23300 | |||
|
r24021 | def diffline(f, revs): | ||
|
r43347 | revinfo = b' '.join([b"-r %s" % rev for rev in revs]) | ||
return b'diff %s %s' % (revinfo, f) | ||||
|
r17941 | |||
|
r32188 | def isempty(fctx): | ||
return fctx is None or fctx.size() == 0 | ||||
|
r36625 | date1 = dateutil.datestr(ctx1.date()) | ||
date2 = dateutil.datestr(ctx2.date()) | ||||
|
r3967 | |||
|
r41795 | if not pathfn: | ||
pathfn = lambda f: f | ||||
|
r24416 | |||
|
r27900 | for f1, f2, copyop in _filepairs(modified, added, removed, copy, opts): | ||
|
r24105 | content1 = None | ||
content2 = None | ||||
|
r32187 | fctx1 = None | ||
fctx2 = None | ||||
|
r24103 | flag1 = None | ||
flag2 = None | ||||
|
r24107 | if f1: | ||
|
r32187 | fctx1 = getfilectx(f1, ctx1) | ||
|
r24107 | if opts.git or losedatafn: | ||
flag1 = ctx1.flags(f1) | ||||
if f2: | ||||
|
r32187 | fctx2 = getfilectx(f2, ctx2) | ||
|
r24107 | if opts.git or losedatafn: | ||
flag2 = ctx2.flags(f2) | ||||
|
r32190 | # if binary is True, output "summary" or "base85", but not "text diff" | ||
|
r35868 | if opts.text: | ||
binary = False | ||||
else: | ||||
|
r35969 | binary = any(f.isbinary() for f in [fctx1, fctx2] if f is not None) | ||
|
r24057 | |||
if losedatafn and not opts.git: | ||||
|
r43346 | if ( | ||
binary | ||||
or | ||||
|
r24057 | # copy/rename | ||
|
r43346 | f2 in copy | ||
or | ||||
|
r24057 | # empty file creation | ||
|
r43346 | (not f1 and isempty(fctx2)) | ||
or | ||||
|
r24057 | # empty file deletion | ||
|
r43346 | (isempty(fctx1) and not f2) | ||
or | ||||
|
r24057 | # create with flags | ||
|
r43346 | (not f1 and flag2) | ||
or | ||||
|
r24057 | # change flags | ||
|
r43346 | (f1 and f2 and flag1 != flag2) | ||
): | ||||
|
r24106 | losedatafn(f2 or f1) | ||
|
r10189 | |||
|
r41795 | path1 = pathfn(f1 or f2) | ||
path2 = pathfn(f2 or f1) | ||||
|
r23998 | header = [] | ||
|
r24022 | if opts.git: | ||
|
r43346 | header.append( | ||
|
r43347 | b'diff --git %s%s %s%s' % (aprefix, path1, bprefix, path2) | ||
|
r43346 | ) | ||
if not f1: # added | ||||
|
r45658 | header.append(b'new file mode %s' % _gitmode[flag2]) | ||
|
r43346 | elif not f2: # removed | ||
|
r45658 | header.append(b'deleted file mode %s' % _gitmode[flag1]) | ||
|
r23998 | else: # modified/copied/renamed | ||
|
r45658 | mode1, mode2 = _gitmode[flag1], _gitmode[flag2] | ||
|
r24000 | if mode1 != mode2: | ||
|
r43347 | header.append(b'old mode %s' % mode1) | ||
header.append(b'new mode %s' % mode2) | ||||
|
r24055 | if copyop is not None: | ||
|
r30807 | if opts.showsimilarity: | ||
sim = similar.score(ctx1[path1], ctx2[path2]) * 100 | ||||
|
r43347 | header.append(b'similarity index %d%%' % sim) | ||
header.append(b'%s from %s' % (copyop, path1)) | ||||
header.append(b'%s to %s' % (copyop, path2)) | ||||
|
r41950 | elif revs: | ||
|
r24022 | header.append(diffline(path1, revs)) | ||
|
r23998 | |||
|
r32189 | # fctx.is | diffopts | what to | is fctx.data() | ||
# binary() | text nobinary git index | output? | outputted? | ||||
# ------------------------------------|---------------------------- | ||||
# yes | no no no * | summary | no | ||||
# yes | no no yes * | base85 | yes | ||||
# yes | no yes no * | summary | no | ||||
# yes | no yes yes 0 | summary | no | ||||
# yes | no yes yes >0 | summary | semi [1] | ||||
# yes | yes * * * | text diff | yes | ||||
# no | * * * * | text diff | yes | ||||
# [1]: hash(fctx.data()) is outputted. so fctx.data() cannot be faked | ||||
|
r43346 | if binary and ( | ||
not opts.git or (opts.git and opts.nobinary and not opts.index) | ||||
): | ||||
|
r32191 | # fast path: no binary content will be displayed, content1 and | ||
# content2 are only used for equivalent test. cmp() could have a | ||||
# fast path. | ||||
if fctx1 is not None: | ||||
content1 = b'\0' | ||||
if fctx2 is not None: | ||||
if fctx1 is not None and not fctx1.cmp(fctx2): | ||||
|
r43346 | content2 = b'\0' # not different | ||
|
r32191 | else: | ||
content2 = b'\0\0' | ||||
else: | ||||
# normal path: load contents | ||||
if fctx1 is not None: | ||||
content1 = fctx1.data() | ||||
if fctx2 is not None: | ||||
content2 = fctx2.data() | ||||
|
r45658 | data1 = (ctx1, fctx1, path1, flag1, content1, date1) | ||
data2 = (ctx2, fctx2, path2, flag2, content2, date2) | ||||
yield diffcontent(data1, data2, header, binary, opts) | ||||
def diffcontent(data1, data2, header, binary, opts): | ||||
|
r46554 | """diffs two versions of a file. | ||
|
r45658 | |||
data1 and data2 are tuples containg: | ||||
* ctx: changeset for the file | ||||
* fctx: file context for that file | ||||
* path1: name of the file | ||||
* flag: flags of the file | ||||
* content: full content of the file (can be null in case of binary) | ||||
* date: date of the changeset | ||||
header: the patch header | ||||
binary: whether the any of the version of file is binary or not | ||||
opts: user passed options | ||||
It exists as a separate function so that extensions like extdiff can wrap | ||||
it and use the file content directly. | ||||
""" | ||||
ctx1, fctx1, path1, flag1, content1, date1 = data1 | ||||
ctx2, fctx2, path2, flag2, content2, date2 = data2 | ||||
|
r47771 | index1 = _gitindex(content1) if path1 in ctx1 else sha1nodeconstants.nullhex | ||
index2 = _gitindex(content2) if path2 in ctx2 else sha1nodeconstants.nullhex | ||||
|
r45658 | if binary and opts.git and not opts.nobinary: | ||
text = mdiff.b85diff(content1, content2) | ||||
if text: | ||||
|
r47364 | header.append(b'index %s..%s' % (index1, index2)) | ||
|
r45658 | hunks = ((None, [text]),) | ||
else: | ||||
if opts.git and opts.index > 0: | ||||
flag = flag1 | ||||
if flag is None: | ||||
flag = flag2 | ||||
header.append( | ||||
b'index %s..%s %s' | ||||
% ( | ||||
|
r47279 | index1[0 : opts.index], | ||
index2[0 : opts.index], | ||||
|
r45658 | _gitmode[flag], | ||
|
r43346 | ) | ||
) | ||||
|
r45658 | |||
uheaders, hunks = mdiff.unidiff( | ||||
content1, | ||||
date1, | ||||
content2, | ||||
date2, | ||||
path1, | ||||
path2, | ||||
binary=binary, | ||||
opts=opts, | ||||
) | ||||
header.extend(uheaders) | ||||
return fctx1, fctx2, header, hunks | ||||
|
r2874 | |||
|
r43346 | |||
|
r14401 | def diffstatsum(stats): | ||
|
r14437 | maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False | ||
|
r14401 | for f, a, r, b in stats: | ||
maxfile = max(maxfile, encoding.colwidth(f)) | ||||
|
r14437 | maxtotal = max(maxtotal, a + r) | ||
|
r14401 | addtotal += a | ||
removetotal += r | ||||
binary = binary or b | ||||
|
r14437 | return maxfile, maxtotal, addtotal, removetotal, binary | ||
|
r14401 | |||
|
r43346 | |||
|
r7547 | def diffstatdata(lines): | ||
|
r41673 | diffre = re.compile(br'^diff .*-r [a-z0-9]+\s(.*)$') | ||
|
r13395 | |||
|
r14400 | results = [] | ||
|
r15363 | filename, adds, removes, isbinary = None, 0, 0, False | ||
|
r14400 | |||
def addresult(): | ||||
if filename: | ||||
results.append((filename, adds, removes, isbinary)) | ||||
|
r32321 | # inheader is used to track if a line is in the | ||
# header portion of the diff. This helps properly account | ||||
# for lines that start with '--' or '++' | ||||
inheader = False | ||||
|
r7547 | for line in lines: | ||
|
r43347 | if line.startswith(b'diff'): | ||
|
r14400 | addresult() | ||
|
r32321 | # starting a new file diff | ||
# set numbers to 0 and reset inheader | ||||
inheader = True | ||||
|
r15363 | adds, removes, isbinary = 0, 0, False | ||
|
r43347 | if line.startswith(b'diff --git a/'): | ||
|
r20972 | filename = gitre.search(line).group(2) | ||
|
r43347 | elif line.startswith(b'diff -r'): | ||
|
r8761 | # format: "diff -r ... -r ... filename" | ||
|
r13395 | filename = diffre.search(line).group(1) | ||
|
r43347 | elif line.startswith(b'@@'): | ||
|
r32321 | inheader = False | ||
|
r43347 | elif line.startswith(b'+') and not inheader: | ||
|
r7547 | adds += 1 | ||
|
r43347 | elif line.startswith(b'-') and not inheader: | ||
|
r7547 | removes += 1 | ||
|
r43347 | elif line.startswith(b'GIT binary patch') or line.startswith( | ||
b'Binary file' | ||||
|
r43346 | ): | ||
|
r15363 | isbinary = True | ||
|
r43347 | elif line.startswith(b'rename from'): | ||
|
r41446 | filename = line[12:] | ||
|
r43347 | elif line.startswith(b'rename to'): | ||
filename += b' => %s' % line[10:] | ||||
|
r14400 | addresult() | ||
return results | ||||
|
r7547 | |||
|
r43346 | |||
|
r30407 | def diffstat(lines, width=80): | ||
|
r7547 | output = [] | ||
|
r14402 | stats = diffstatdata(lines) | ||
|
r14437 | maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats) | ||
|
r7547 | |||
countwidth = len(str(maxtotal)) | ||||
|
r9642 | if hasbinary and countwidth < 3: | ||
countwidth = 3 | ||||
|
r9330 | graphwidth = width - countwidth - maxname - 6 | ||
|
r7547 | if graphwidth < 10: | ||
graphwidth = 10 | ||||
|
r9330 | def scale(i): | ||
if maxtotal <= graphwidth: | ||||
return i | ||||
# If diffstat runs out of room it doesn't print anything, | ||||
# which isn't very useful, so always print at least one + or - | ||||
# if there were at least some changes. | ||||
return max(i * graphwidth // maxtotal, int(bool(i))) | ||||
|
r7547 | |||
|
r14402 | for filename, adds, removes, isbinary in stats: | ||
|
r15363 | if isbinary: | ||
|
r43347 | count = b'Bin' | ||
|
r9642 | else: | ||
|
r43347 | count = b'%d' % (adds + removes) | ||
pluses = b'+' * scale(adds) | ||||
minuses = b'-' * scale(removes) | ||||
|
r43346 | output.append( | ||
|
r43347 | b' %s%s | %*s %s%s\n' | ||
|
r43346 | % ( | ||
filename, | ||||
|
r43347 | b' ' * (maxname - encoding.colwidth(filename)), | ||
|
r43346 | countwidth, | ||
count, | ||||
pluses, | ||||
minuses, | ||||
) | ||||
) | ||||
|
r7547 | |||
if stats: | ||||
|
r43346 | output.append( | ||
|
r43387 | _(b' %d files changed, %d insertions(+), %d deletions(-)\n') | ||
|
r43346 | % (len(stats), totaladds, totalremoves) | ||
) | ||||
|
r7547 | |||
|
r43347 | return b''.join(output) | ||
|
r10818 | |||
|
r43346 | |||
|
r10818 | def diffstatui(*args, **kw): | ||
|
r46554 | """like diffstat(), but yields 2-tuples of (output, label) for | ||
|
r10818 | ui.write() | ||
|
r46554 | """ | ||
|
r10818 | |||
for line in diffstat(*args, **kw).splitlines(): | ||||
|
r43347 | if line and line[-1] in b'+-': | ||
name, graph = line.rsplit(b' ', 1) | ||||
yield (name + b' ', b'') | ||||
|
r33096 | m = re.search(br'\++', graph) | ||
|
r10818 | if m: | ||
|
r43347 | yield (m.group(0), b'diffstat.inserted') | ||
|
r33096 | m = re.search(br'-+', graph) | ||
|
r10818 | if m: | ||
|
r43347 | yield (m.group(0), b'diffstat.deleted') | ||
|
r10818 | else: | ||
|
r43347 | yield (line, b'') | ||
yield (b'\n', b'') | ||||