patch.py
3260 lines
| 101.4 KiB
| text/x-python
|
PythonLexer
/ mercurial / patch.py
Brendan Cully
|
r2861 | # patch.py - patch file parsing routines | ||
# | ||||
Vadim Gelfer
|
r2865 | # Copyright 2006 Brendan Cully <brendan@kublai.com> | ||
Bryan O'Sullivan
|
r4897 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> | ||
Vadim Gelfer
|
r2865 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Brendan Cully
|
r2861 | |||
Yuya Nishihara
|
r34139 | from __future__ import absolute_import, print_function | ||
Gregory Szorc
|
r27485 | |||
Martin von Zweigbergk
|
r25113 | import collections | ||
Gregory Szorc
|
r37639 | import contextlib | ||
Gregory Szorc
|
r27485 | import copy | ||
import errno | ||||
import os | ||||
import re | ||||
import shutil | ||||
import zlib | ||||
Augie Fackler
|
r10965 | |||
Gregory Szorc
|
r27485 | from .i18n import _ | ||
from .node import ( | ||||
hex, | ||||
short, | ||||
) | ||||
Gregory Szorc
|
r43355 | from .pycompat import open | ||
Gregory Szorc
|
r27485 | from . import ( | ||
copies, | ||||
Yuya Nishihara
|
r37821 | diffhelper, | ||
Yuya Nishihara
|
r38607 | diffutil, | ||
Gregory Szorc
|
r27485 | encoding, | ||
error, | ||||
Julien Cristau
|
r28341 | mail, | ||
Gregory Szorc
|
r27485 | mdiff, | ||
pathutil, | ||||
Pulkit Goyal
|
r30924 | pycompat, | ||
Gregory Szorc
|
r27485 | scmutil, | ||
Sean Farley
|
r30807 | similar, | ||
Gregory Szorc
|
r27485 | util, | ||
Pierre-Yves David
|
r31233 | vfs as vfsmod, | ||
Gregory Szorc
|
r27485 | ) | ||
Yuya Nishihara
|
r37102 | from .utils import ( | ||
dateutil, | ||||
Augie Fackler
|
r44517 | hashutil, | ||
Yuya Nishihara
|
r37138 | procutil, | ||
Yuya Nishihara
|
r37102 | stringutil, | ||
) | ||||
Yuya Nishihara
|
r32370 | |||
timeless
|
r28861 | stringio = util.stringio | ||
Vadim Gelfer
|
r2866 | |||
Pulkit Goyal
|
r31630 | gitre = re.compile(br'diff --git a/(.*) b/(.*)') | ||
tabsplitter = re.compile(br'(\t+|[^\t]+)') | ||||
Augie Fackler
|
r43346 | wordsplitter = re.compile( | ||
Martin von Zweigbergk
|
r43387 | br'(\t+| +|[a-zA-Z0-9_\x80-\xff]+|[^ \ta-zA-Z0-9_\x80-\xff])' | ||
Augie Fackler
|
r43346 | ) | ||
Dirkjan Ochtman
|
r7199 | |||
Yuya Nishihara
|
r34252 | PatchError = error.PatchError | ||
Brendan Cully
|
r2933 | |||
# public functions | ||||
Augie Fackler
|
r43346 | |||
Brendan Cully
|
r10384 | def split(stream): | ||
'''return an iterator of individual patches from a stream''' | ||||
Augie Fackler
|
r43346 | |||
Brendan Cully
|
r10384 | def isheader(line, inheader): | ||
Augie Fackler
|
r43347 | if inheader and line.startswith((b' ', b'\t')): | ||
Brendan Cully
|
r10384 | # continuation | ||
return True | ||||
Augie Fackler
|
r43347 | if line.startswith((b' ', b'-', b'+')): | ||
Peter Arrenbrecht
|
r10883 | # diff line - don't check for header pattern in there | ||
return False | ||||
Augie Fackler
|
r43347 | l = line.split(b': ', 1) | ||
return len(l) == 2 and b' ' not in l[0] | ||||
Brendan Cully
|
r10384 | |||
def chunk(lines): | ||||
Augie Fackler
|
r43347 | return stringio(b''.join(lines)) | ||
Brendan Cully
|
r10384 | |||
def hgsplit(stream, cur): | ||||
inheader = True | ||||
for line in stream: | ||||
if not line.strip(): | ||||
inheader = False | ||||
Augie Fackler
|
r43347 | if not inheader and line.startswith(b'# HG changeset patch'): | ||
Brendan Cully
|
r10384 | yield chunk(cur) | ||
cur = [] | ||||
inheader = True | ||||
cur.append(line) | ||||
if cur: | ||||
yield chunk(cur) | ||||
def mboxsplit(stream, cur): | ||||
for line in stream: | ||||
Augie Fackler
|
r43347 | if line.startswith(b'From '): | ||
Brendan Cully
|
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): | ||||
timeless
|
r28861 | fp = stringio() | ||
Denis Laxalde
|
r43426 | g = mail.Generator(fp, mangle_from_=False) | ||
Brendan Cully
|
r10384 | g.flatten(m) | ||
fp.seek(0) | ||||
return fp | ||||
for line in stream: | ||||
cur.append(line) | ||||
c = chunk(cur) | ||||
Yuya Nishihara
|
r38354 | m = mail.parse(c) | ||
Brendan Cully
|
r10384 | if not m.is_multipart(): | ||
yield msgfp(m) | ||||
else: | ||||
Augie Fackler
|
r43347 | ok_types = (b'text/plain', b'text/x-diff', b'text/x-patch') | ||
Brendan Cully
|
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 | ||||
Gregory Szorc
|
r35192 | __next__ = next | ||
Brendan Cully
|
r10384 | inheader = False | ||
cur = [] | ||||
Augie Fackler
|
r43347 | mimeheaders = [b'content-type'] | ||
if not util.safehasattr(stream, b'next'): | ||||
Brendan Cully
|
r10384 | # http responses, for example, have readline but not next | ||
stream = fiter(stream) | ||||
for line in stream: | ||||
cur.append(line) | ||||
Augie Fackler
|
r43347 | if line.startswith(b'# HG changeset patch'): | ||
Brendan Cully
|
r10384 | return hgsplit(stream, cur) | ||
Augie Fackler
|
r43347 | elif line.startswith(b'From '): | ||
Brendan Cully
|
r10384 | return mboxsplit(stream, cur) | ||
elif isheader(line, inheader): | ||||
inheader = True | ||||
Augie Fackler
|
r43347 | if line.split(b':', 1)[0].lower() in mimeheaders: | ||
Brendan Cully
|
r10384 | # let email parser handle this | ||
return mimesplit(stream, cur) | ||||
Augie Fackler
|
r43347 | elif line.startswith(b'--- ') and inheader: | ||
Brendan Cully
|
r10501 | # No evil headers seen by diff start, split by hand | ||
Brendan Cully
|
r10384 | return headersplit(stream, cur) | ||
# Not enough info, keep reading | ||||
# if we are here, we have a very plain patch | ||||
return remainder(cur) | ||||
Augie Fackler
|
r43346 | |||
Pierre-Yves David
|
r26557 | ## Some facility for extensible patch parsing: | ||
# list of pairs ("header to match", "data key") | ||||
Augie Fackler
|
r43346 | patchheadermap = [ | ||
Augie Fackler
|
r43347 | (b'Date', b'date'), | ||
(b'Branch', b'branch'), | ||||
(b'Node ID', b'nodeid'), | ||||
Augie Fackler
|
r43346 | ] | ||
Pierre-Yves David
|
r26557 | |||
Gregory Szorc
|
r37639 | @contextlib.contextmanager | ||
Vadim Gelfer
|
r2866 | def extract(ui, fileobj): | ||
'''extract patch from data read from fileobj. | ||||
Brendan Cully
|
r4263 | patch can be a normal patch or contained in an email message. | ||
Vadim Gelfer
|
r2866 | |||
Mads Kiilerich
|
r26781 | return a dictionary. Standard keys are: | ||
Pierre-Yves David
|
r26547 | - filename, | ||
- message, | ||||
- user, | ||||
- date, | ||||
- branch, | ||||
- node, | ||||
- p1, | ||||
- p2. | ||||
Mads Kiilerich
|
r26781 | Any item can be missing from the dictionary. If filename is missing, | ||
Brendan Cully
|
r4263 | fileobj did not contain a patch. Caller must unlink filename when done.''' | ||
Vadim Gelfer
|
r2866 | |||
Augie Fackler
|
r43347 | fd, tmpname = pycompat.mkstemp(prefix=b'hg-patch-') | ||
Augie Fackler
|
r43906 | tmpfp = os.fdopen(fd, 'wb') | ||
Gregory Szorc
|
r37639 | try: | ||
yield _extract(ui, fileobj, tmpname, tmpfp) | ||||
finally: | ||||
tmpfp.close() | ||||
os.unlink(tmpname) | ||||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r37639 | def _extract(ui, fileobj, tmpname, tmpfp): | ||
Vadim Gelfer
|
r2866 | # attempt to detect the start of a patch | ||
# (this heuristic is borrowed from quilt) | ||||
Augie Fackler
|
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, | ||||
) | ||||
Vadim Gelfer
|
r2866 | |||
Pierre-Yves David
|
r26547 | data = {} | ||
Gregory Szorc
|
r37639 | |||
Yuya Nishihara
|
r38354 | msg = mail.parse(fileobj) | ||
Vadim Gelfer
|
r2866 | |||
Augie Fackler
|
r43906 | subject = msg['Subject'] and mail.headdecode(msg['Subject']) | ||
data[b'user'] = msg['From'] and mail.headdecode(msg['From']) | ||||
Augie Fackler
|
r43347 | if not subject and not data[b'user']: | ||
Gregory Szorc
|
r37639 | # Not an email, restore parsed headers if any | ||
Augie Fackler
|
r43346 | subject = ( | ||
Augie Fackler
|
r43347 | b'\n'.join( | ||
b': '.join(map(encoding.strtolocal, h)) for h in msg.items() | ||||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r43347 | + b'\n' | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r9573 | |||
Gregory Szorc
|
r37639 | # should try to parse msg['Date'] | ||
parents = [] | ||||
Vadim Gelfer
|
r2866 | |||
Augie Fackler
|
r43906 | nodeid = msg['X-Mercurial-Node'] | ||
Denis Laxalde
|
r43367 | if nodeid: | ||
data[b'nodeid'] = nodeid = mail.headdecode(nodeid) | ||||
ui.debug(b'Node ID: %s\n' % nodeid) | ||||
Gregory Szorc
|
r37639 | if subject: | ||
Augie Fackler
|
r43347 | if subject.startswith(b'[PATCH'): | ||
pend = subject.find(b']') | ||||
Gregory Szorc
|
r37639 | if pend >= 0: | ||
Augie Fackler
|
r43346 | subject = subject[pend + 1 :].lstrip() | ||
Augie Fackler
|
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']) | ||||
Gregory Szorc
|
r37639 | diffs_seen = 0 | ||
Augie Fackler
|
r43347 | ok_types = (b'text/plain', b'text/x-diff', b'text/x-patch') | ||
message = b'' | ||||
Gregory Szorc
|
r37639 | for part in msg.walk(): | ||
content_type = pycompat.bytestr(part.get_content_type()) | ||||
Augie Fackler
|
r43347 | ui.debug(b'Content-Type: %s\n' % content_type) | ||
Gregory Szorc
|
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 | ||||
Brendan Cully
|
r4220 | |||
Augie Fackler
|
r43347 | ui.debug(b'found patch at byte %d\n' % m.start(0)) | ||
Gregory Szorc
|
r37639 | diffs_seen += 1 | ||
cfp = stringio() | ||||
Augie Fackler
|
r43346 | for line in payload[: m.start(0)].splitlines(): | ||
Augie Fackler
|
r43347 | if line.startswith(b'# HG changeset patch') and not hgpatch: | ||
ui.debug(b'patch generated by hg export\n') | ||||
Gregory Szorc
|
r37639 | hgpatch = True | ||
hgpatchheader = True | ||||
# drop earlier commit message content | ||||
cfp.seek(0) | ||||
cfp.truncate() | ||||
subject = None | ||||
elif hgpatchheader: | ||||
Augie Fackler
|
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 "): | ||||
Gregory Szorc
|
r37639 | parents.append(line[9:].lstrip()) | ||
Augie Fackler
|
r43347 | elif line.startswith(b"# "): | ||
Gregory Szorc
|
r37639 | for header, key in patchheadermap: | ||
Augie Fackler
|
r43347 | prefix = b'# %s ' % header | ||
Gregory Szorc
|
r37639 | if line.startswith(prefix): | ||
Augie Fackler
|
r43346 | data[key] = line[len(prefix) :] | ||
Augie Fackler
|
r43347 | ui.debug(b'%s: %s\n' % (header, data[key])) | ||
Gregory Szorc
|
r37639 | else: | ||
hgpatchheader = False | ||||
Augie Fackler
|
r43347 | elif line == b'---': | ||
Gregory Szorc
|
r37639 | ignoretext = True | ||
if not hgpatchheader and not ignoretext: | ||||
cfp.write(line) | ||||
Augie Fackler
|
r43347 | cfp.write(b'\n') | ||
Gregory Szorc
|
r37639 | message = cfp.getvalue() | ||
if tmpfp: | ||||
tmpfp.write(payload) | ||||
Augie Fackler
|
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 | ||||
Vadim Gelfer
|
r2866 | |||
Brendan Cully
|
r4777 | if subject and not message.startswith(subject): | ||
Augie Fackler
|
r43347 | message = b'%s\n%s' % (subject, message) | ||
data[b'message'] = message | ||||
Vadim Gelfer
|
r2866 | tmpfp.close() | ||
Jordi Gutiérrez Hermoso
|
r24306 | if parents: | ||
Augie Fackler
|
r43347 | data[b'p1'] = parents.pop(0) | ||
Pierre-Yves David
|
r26548 | if parents: | ||
Augie Fackler
|
r43347 | data[b'p2'] = parents.pop(0) | ||
Jordi Gutiérrez Hermoso
|
r24306 | |||
Pierre-Yves David
|
r26555 | if diffs_seen: | ||
Augie Fackler
|
r43347 | data[b'filename'] = tmpname | ||
Gregory Szorc
|
r37639 | |||
Pierre-Yves David
|
r26547 | return data | ||
Brendan Cully
|
r2861 | |||
Augie Fackler
|
r43346 | |||
Benoit Boissinot
|
r8778 | class patchmeta(object): | ||
Patrick Mezard
|
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 | ||||
Patrick Mezard
|
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. | ||||
Patrick Mezard
|
r7148 | """ | ||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r7148 | def __init__(self, path): | ||
self.path = path | ||||
self.oldpath = None | ||||
self.mode = None | ||||
Augie Fackler
|
r43347 | self.op = b'MODIFY' | ||
Patrick Mezard
|
r7148 | self.binary = False | ||
Patrick Mezard
|
r7149 | def setmode(self, mode): | ||
Gregory Szorc
|
r25658 | islink = mode & 0o20000 | ||
isexec = mode & 0o100 | ||||
Patrick Mezard
|
r7149 | self.mode = (islink, isexec) | ||
Patrick Mezard
|
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 | ||||
Patrick Mezard
|
r16506 | def _ispatchinga(self, afile): | ||
Augie Fackler
|
r43347 | if afile == b'/dev/null': | ||
return self.op == b'ADD' | ||||
return afile == b'a/' + (self.oldpath or self.path) | ||||
Patrick Mezard
|
r16506 | |||
def _ispatchingb(self, bfile): | ||||
Augie Fackler
|
r43347 | if bfile == b'/dev/null': | ||
return self.op == b'DELETE' | ||||
return bfile == b'b/' + self.path | ||||
Patrick Mezard
|
r16506 | |||
def ispatching(self, afile, bfile): | ||||
return self._ispatchinga(afile) and self._ispatchingb(bfile) | ||||
Mads Kiilerich
|
r11018 | def __repr__(self): | ||
Augie Fackler
|
r43809 | return "<patchmeta %s %r>" % (self.op, self.path) | ||
Mads Kiilerich
|
r11018 | |||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r7152 | def readgitpatch(lr): | ||
Brendan Cully
|
r2861 | """extract git-style metadata about patches from <patchname>""" | ||
Thomas Arendsen Hein
|
r3223 | |||
Brendan Cully
|
r2861 | # Filter patch for git information | ||
gp = None | ||||
gitpatches = [] | ||||
Patrick Mezard
|
r7152 | for line in lr: | ||
Augie Fackler
|
r43347 | line = line.rstrip(b' \r\n') | ||
if line.startswith(b'diff --git a/'): | ||||
Brendan Cully
|
r2861 | m = gitre.match(line) | ||
if m: | ||||
if gp: | ||||
gitpatches.append(gp) | ||||
Nicolas Dumazet
|
r9392 | dst = m.group(2) | ||
Patrick Mezard
|
r7148 | gp = patchmeta(dst) | ||
Brendan Cully
|
r2861 | elif gp: | ||
Augie Fackler
|
r43347 | if line.startswith(b'--- '): | ||
Brendan Cully
|
r2861 | gitpatches.append(gp) | ||
gp = None | ||||
continue | ||||
Augie Fackler
|
r43347 | if line.startswith(b'rename from '): | ||
gp.op = b'RENAME' | ||||
Bill Barry
|
r9243 | gp.oldpath = line[12:] | ||
Augie Fackler
|
r43347 | elif line.startswith(b'rename to '): | ||
Bill Barry
|
r9243 | gp.path = line[10:] | ||
Augie Fackler
|
r43347 | elif line.startswith(b'copy from '): | ||
gp.op = b'COPY' | ||||
Bill Barry
|
r9243 | gp.oldpath = line[10:] | ||
Augie Fackler
|
r43347 | elif line.startswith(b'copy to '): | ||
Bill Barry
|
r9243 | gp.path = line[8:] | ||
Augie Fackler
|
r43347 | elif line.startswith(b'deleted file'): | ||
gp.op = b'DELETE' | ||||
elif line.startswith(b'new file mode '): | ||||
gp.op = b'ADD' | ||||
Bill Barry
|
r9243 | gp.setmode(int(line[-6:], 8)) | ||
Augie Fackler
|
r43347 | elif line.startswith(b'new mode '): | ||
Bill Barry
|
r9243 | gp.setmode(int(line[-6:], 8)) | ||
Augie Fackler
|
r43347 | elif line.startswith(b'GIT binary patch'): | ||
Brendan Cully
|
r3367 | gp.binary = True | ||
Brendan Cully
|
r2861 | if gp: | ||
gitpatches.append(gp) | ||||
Patrick Mezard
|
r12669 | return gitpatches | ||
Brendan Cully
|
r2861 | |||
Augie Fackler
|
r43346 | |||
Simon Heimberg
|
r8891 | class linereader(object): | ||
Patrick Mezard
|
r8810 | # simple class to allow pushing lines back into the input stream | ||
Patrick Mezard
|
r14418 | def __init__(self, fp): | ||
Patrick Mezard
|
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 | ||||
Patrick Mezard
|
r14418 | return self.fp.readline() | ||
Patrick Mezard
|
r8810 | |||
def __iter__(self): | ||||
Augie Fackler
|
r43347 | return iter(self.readline, b'') | ||
Patrick Mezard
|
r8810 | |||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r14348 | class abstractbackend(object): | ||
def __init__(self, ui): | ||||
Bryan O'Sullivan
|
r4897 | self.ui = ui | ||
Patrick Mezard
|
r14348 | |||
Patrick Mezard
|
r14391 | def getfile(self, fname): | ||
"""Return target file data and flags as a (data, (islink, | ||||
Mads Kiilerich
|
r22296 | isexec)) tuple. Data is None if file is missing/deleted. | ||
Patrick Mezard
|
r14348 | """ | ||
raise NotImplementedError | ||||
Patrick Mezard
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
Patrick Mezard
|
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 | ||||
Patrick Mezard
|
r14452 | be left unchanged. If the file is modified after being copied, | ||
copysource is set to the original file name. | ||||
Patrick Mezard
|
r14367 | """ | ||
Patrick Mezard
|
r14348 | raise NotImplementedError | ||
Patrick Mezard
|
r5652 | |||
Patrick Mezard
|
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. | ||||
""" | ||||
Patrick Mezard
|
r14351 | def exists(self, fname): | ||
raise NotImplementedError | ||||
Martin von Zweigbergk
|
r33156 | def close(self): | ||
raise NotImplementedError | ||||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r14348 | class fsbackend(abstractbackend): | ||
Patrick Mezard
|
r14350 | def __init__(self, ui, basedir): | ||
Patrick Mezard
|
r14348 | super(fsbackend, self).__init__(ui) | ||
Pierre-Yves David
|
r31233 | self.opener = vfsmod.vfs(basedir) | ||
Bryan O'Sullivan
|
r4897 | |||
Patrick Mezard
|
r14391 | def getfile(self, fname): | ||
Chinmay Joshi
|
r21717 | if self.opener.islink(fname): | ||
return (self.opener.readlink(fname), (True, False)) | ||||
Patrick Mezard
|
r14531 | isexec = False | ||
Patrick Mezard
|
r7392 | try: | ||
Gregory Szorc
|
r25658 | isexec = self.opener.lstat(fname).st_mode & 0o100 != 0 | ||
Gregory Szorc
|
r25660 | except OSError as e: | ||
Patrick Mezard
|
r14391 | if e.errno != errno.ENOENT: | ||
raise | ||||
Mads Kiilerich
|
r22296 | try: | ||
return (self.opener.read(fname), (False, isexec)) | ||||
Gregory Szorc
|
r25660 | except IOError as e: | ||
Mads Kiilerich
|
r22296 | if e.errno != errno.ENOENT: | ||
raise | ||||
return None, None | ||||
Patrick Mezard
|
r7392 | |||
Patrick Mezard
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
Patrick Mezard
|
r14391 | islink, isexec = mode | ||
if data is None: | ||||
Chinmay Joshi
|
r21717 | self.opener.setflags(fname, islink, isexec) | ||
Patrick Mezard
|
r14390 | return | ||
Patrick Mezard
|
r14391 | if islink: | ||
self.opener.symlink(data, fname) | ||||
Patrick Mezard
|
r14367 | else: | ||
Patrick Mezard
|
r14391 | self.opener.write(fname, data) | ||
Patrick Mezard
|
r14367 | if isexec: | ||
Chinmay Joshi
|
r21717 | self.opener.setflags(fname, False, True) | ||
Patrick Mezard
|
r7392 | |||
def unlink(self, fname): | ||||
Augie Fackler
|
r43347 | rmdir = self.ui.configbool(b'experimental', b'removeemptydirs') | ||
Kyle Lippincott
|
r38512 | self.opener.unlinkpath(fname, ignoremissing=True, rmdir=rmdir) | ||
Patrick Mezard
|
r7392 | |||
Patrick Mezard
|
r14348 | def writerej(self, fname, failed, total, lines): | ||
Augie Fackler
|
r43347 | fname = fname + b".rej" | ||
Patrick Mezard
|
r14348 | self.ui.warn( | ||
Augie Fackler
|
r43347 | _(b"%d out of %d hunks FAILED -- saving rejects to file %s\n") | ||
Augie Fackler
|
r43346 | % (failed, total, fname) | ||
) | ||||
Augie Fackler
|
r43347 | fp = self.opener(fname, b'w') | ||
Patrick Mezard
|
r14348 | fp.writelines(lines) | ||
fp.close() | ||||
Patrick Mezard
|
r14351 | def exists(self, fname): | ||
Chinmay Joshi
|
r21717 | return self.opener.lexists(fname) | ||
Patrick Mezard
|
r14351 | |||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
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 = [] | ||||
Patrick Mezard
|
r14453 | def _checkknown(self, fname): | ||
Augie Fackler
|
r43347 | if self.repo.dirstate[fname] == b'?' and self.exists(fname): | ||
raise PatchError(_(b'cannot patch %s: file is not tracked') % fname) | ||||
Patrick Mezard
|
r14453 | |||
Patrick Mezard
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
Patrick Mezard
|
r14453 | self._checkknown(fname) | ||
Patrick Mezard
|
r14452 | super(workingbackend, self).setfile(fname, data, mode, copysource) | ||
if copysource is not None: | ||||
self.copied.append((copysource, fname)) | ||||
Patrick Mezard
|
r14370 | self.changed.add(fname) | ||
def unlink(self, fname): | ||||
Patrick Mezard
|
r14453 | self._checkknown(fname) | ||
Patrick Mezard
|
r14370 | super(workingbackend, self).unlink(fname) | ||
self.removed.add(fname) | ||||
self.changed.add(fname) | ||||
def close(self): | ||||
wctx = self.repo[None] | ||||
Siddharth Agarwal
|
r19155 | changed = set(self.changed) | ||
Patrick Mezard
|
r14370 | for src, dst in self.copied: | ||
scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst) | ||||
Patrick Mezard
|
r16112 | if self.removed: | ||
Matt Mackall
|
r14435 | wctx.forget(sorted(self.removed)) | ||
Patrick Mezard
|
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 | ||||
Siddharth Agarwal
|
r19155 | # marktouched(). | ||
changed.discard(f) | ||||
if changed: | ||||
scmutil.marktouched(self.repo, changed, self.similarity) | ||||
Patrick Mezard
|
r14370 | return sorted(self.changed) | ||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r14452 | class filestore(object): | ||
Patrick Mezard
|
r14658 | def __init__(self, maxsize=None): | ||
Patrick Mezard
|
r14452 | self.opener = None | ||
self.files = {} | ||||
self.created = 0 | ||||
Patrick Mezard
|
r14658 | self.maxsize = maxsize | ||
if self.maxsize is None: | ||||
Augie Fackler
|
r43346 | self.maxsize = 4 * (2 ** 20) | ||
Patrick Mezard
|
r14658 | self.size = 0 | ||
self.data = {} | ||||
Patrick Mezard
|
r14452 | |||
Patrick Mezard
|
r14609 | def setfile(self, fname, data, mode, copied=None): | ||
Patrick Mezard
|
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: | ||||
Augie Fackler
|
r43347 | root = pycompat.mkdtemp(prefix=b'hg-patch-') | ||
Pierre-Yves David
|
r31233 | self.opener = vfsmod.vfs(root) | ||
Patrick Mezard
|
r14658 | # Avoid filename issues with these simple names | ||
Augie Fackler
|
r43347 | fn = b'%d' % self.created | ||
Patrick Mezard
|
r14658 | self.opener.write(fn, data) | ||
self.created += 1 | ||||
self.files[fname] = (fn, mode, copied) | ||||
Patrick Mezard
|
r14452 | |||
def getfile(self, fname): | ||||
Patrick Mezard
|
r14658 | if fname in self.data: | ||
return self.data[fname] | ||||
if not self.opener or fname not in self.files: | ||||
Mads Kiilerich
|
r22296 | return None, None, None | ||
Patrick Mezard
|
r14609 | fn, mode, copied = self.files[fname] | ||
return self.opener.read(fn), mode, copied | ||||
Patrick Mezard
|
r14452 | |||
def close(self): | ||||
if self.opener: | ||||
shutil.rmtree(self.opener.base) | ||||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
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: | ||||
Augie Fackler
|
r43347 | raise PatchError(_(b'cannot patch %s: file is not tracked') % fname) | ||
Patrick Mezard
|
r14611 | |||
def getfile(self, fname): | ||||
try: | ||||
fctx = self.ctx[fname] | ||||
except error.LookupError: | ||||
Mads Kiilerich
|
r22296 | return None, None | ||
Patrick Mezard
|
r14611 | flags = fctx.flags() | ||
Augie Fackler
|
r43347 | return fctx.data(), (b'l' in flags, b'x' in flags) | ||
Patrick Mezard
|
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 | ||||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r14348 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 | ||
Gregory Szorc
|
r41673 | unidesc = re.compile(br'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') | ||
contextdesc = re.compile(br'(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') | ||||
Augie Fackler
|
r43347 | eolmodes = [b'strict', b'crlf', b'lf', b'auto'] | ||
Patrick Mezard
|
r14348 | |||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r14348 | class patchfile(object): | ||
Augie Fackler
|
r43347 | def __init__(self, ui, gp, backend, store, eolmode=b'strict'): | ||
Patrick Mezard
|
r14566 | self.fname = gp.path | ||
Patrick Mezard
|
r14348 | self.eolmode = eolmode | ||
self.eol = None | ||||
self.backend = backend | ||||
self.ui = ui | ||||
self.lines = [] | ||||
self.exists = False | ||||
Patrick Mezard
|
r14452 | self.missing = True | ||
Patrick Mezard
|
r14566 | self.mode = gp.mode | ||
self.copysource = gp.oldpath | ||||
Augie Fackler
|
r43347 | self.create = gp.op in (b'ADD', b'COPY', b'RENAME') | ||
self.remove = gp.op == b'DELETE' | ||||
Mads Kiilerich
|
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) | ||||
Patrick Mezard
|
r14452 | self.missing = False | ||
if data: | ||||
Wagner Bruna
|
r14832 | self.lines = mdiff.splitnewlines(data) | ||
Patrick Mezard
|
r14452 | if self.mode is None: | ||
self.mode = mode | ||||
if self.lines: | ||||
# Normalize line endings | ||||
Augie Fackler
|
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': | ||||
Patrick Mezard
|
r14452 | nlines = [] | ||
for l in self.lines: | ||||
Augie Fackler
|
r43347 | if l.endswith(b'\r\n'): | ||
l = l[:-2] + b'\n' | ||||
Patrick Mezard
|
r14452 | nlines.append(l) | ||
self.lines = nlines | ||||
Mads Kiilerich
|
r22296 | else: | ||
Patrick Mezard
|
r14566 | if self.create: | ||
Patrick Mezard
|
r14452 | self.missing = False | ||
if self.mode is None: | ||||
self.mode = (False, False) | ||||
if self.missing: | ||||
Augie Fackler
|
r43347 | self.ui.warn(_(b"unable to find '%s' for patching\n") % self.fname) | ||
Augie Fackler
|
r43346 | self.ui.warn( | ||
_( | ||||
Augie Fackler
|
r43347 | b"(use '--prefix' to apply patch relative to the " | ||
b"current directory)\n" | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Patrick Mezard
|
r14348 | |||
self.hash = {} | ||||
self.dirty = 0 | ||||
self.offset = 0 | ||||
self.skew = 0 | ||||
self.rej = [] | ||||
self.fileprinted = False | ||||
self.printfile(False) | ||||
self.hunks = 0 | ||||
Patrick Mezard
|
r14367 | def writelines(self, fname, lines, mode): | ||
Augie Fackler
|
r43347 | if self.eolmode == b'auto': | ||
Patrick Mezard
|
r14348 | eol = self.eol | ||
Augie Fackler
|
r43347 | elif self.eolmode == b'crlf': | ||
eol = b'\r\n' | ||||
Patrick Mezard
|
r14348 | else: | ||
Augie Fackler
|
r43347 | eol = b'\n' | ||
if self.eolmode != b'strict' and eol and eol != b'\n': | ||||
Patrick Mezard
|
r14348 | rawlines = [] | ||
for l in lines: | ||||
Augie Fackler
|
r43347 | if l and l.endswith(b'\n'): | ||
Patrick Mezard
|
r14348 | l = l[:-1] + eol | ||
rawlines.append(l) | ||||
lines = rawlines | ||||
Augie Fackler
|
r43347 | self.backend.setfile(fname, b''.join(lines), mode, self.copysource) | ||
Patrick Mezard
|
r14348 | |||
Bryan O'Sullivan
|
r4897 | def printfile(self, warn): | ||
if self.fileprinted: | ||||
return | ||||
if warn or self.ui.verbose: | ||||
self.fileprinted = True | ||||
Augie Fackler
|
r43347 | s = _(b"patching file %s\n") % self.fname | ||
Bryan O'Sullivan
|
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 | ||||
Thomas Arendsen Hein
|
r5143 | |||
Benoit Boissinot
|
r9681 | cand = self.hash.get(l, []) | ||
Bryan O'Sullivan
|
r4897 | if len(cand) > 1: | ||
# resort our list of potentials forward then back. | ||||
Alejandro Santos
|
r9032 | cand.sort(key=lambda x: abs(x - linenum)) | ||
Bryan O'Sullivan
|
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 | ||||
Patrick Mezard
|
r14349 | base = os.path.basename(self.fname) | ||
Augie Fackler
|
r43347 | lines = [b"--- %s\n+++ %s\n" % (base, base)] | ||
Patrick Mezard
|
r14349 | for x in self.rej: | ||
for l in x.hunk: | ||||
lines.append(l) | ||||
Augie Fackler
|
r43347 | if l[-1:] != b'\n': | ||
Rodrigo Damazio Bovendorp
|
r45714 | lines.append(b'\n' + diffhelper.MISSING_NEWLINE_MARKER) | ||
Patrick Mezard
|
r14349 | self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) | ||
Bryan O'Sullivan
|
r4897 | |||
Nicolas Dumazet
|
r9393 | def apply(self, h): | ||
Bryan O'Sullivan
|
r4897 | if not h.complete(): | ||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b"bad hunk #%d %s (%d %d %d %d)") | ||
Augie Fackler
|
r43346 | % (h.number, h.desc, len(h.a), h.lena, len(h.b), h.lenb) | ||
) | ||||
Bryan O'Sullivan
|
r4897 | |||
self.hunks += 1 | ||||
Patrick Mezard
|
r5652 | if self.missing: | ||
self.rej.append(h) | ||||
return -1 | ||||
Patrick Mezard
|
r14451 | if self.exists and self.create: | ||
Patrick Mezard
|
r14452 | if self.copysource: | ||
Augie Fackler
|
r43346 | self.ui.warn( | ||
Martin von Zweigbergk
|
r43387 | _(b"cannot create %s: destination already exists\n") | ||
Augie Fackler
|
r43346 | % self.fname | ||
) | ||||
Patrick Mezard
|
r14452 | else: | ||
Augie Fackler
|
r43347 | self.ui.warn(_(b"file %s already exists\n") % self.fname) | ||
Bryan O'Sullivan
|
r4897 | self.rej.append(h) | ||
return -1 | ||||
Patrick Mezard
|
r9585 | if isinstance(h, binhunk): | ||
Patrick Mezard
|
r14451 | if self.remove: | ||
Patrick Mezard
|
r14348 | self.backend.unlink(self.fname) | ||
Bryan O'Sullivan
|
r4897 | else: | ||
Nicolas Vigier
|
r20137 | l = h.new(self.lines) | ||
self.lines[:] = l | ||||
self.offset += len(l) | ||||
Martin Geisler
|
r14217 | self.dirty = True | ||
Bryan O'Sullivan
|
r4897 | return 0 | ||
Patrick Mezard
|
r10127 | horig = h | ||
Augie Fackler
|
r43346 | if ( | ||
Augie Fackler
|
r43347 | self.eolmode in (b'crlf', b'lf') | ||
or self.eolmode == b'auto' | ||||
Augie Fackler
|
r43346 | and self.eol | ||
): | ||||
Patrick Mezard
|
r10128 | # If new eols are going to be normalized, then normalize | ||
# hunk data before patching. Otherwise, preserve input | ||||
# line-endings. | ||||
Patrick Mezard
|
r10127 | h = h.getnormalized() | ||
Bryan O'Sullivan
|
r4897 | # fast case first, no offsets, no fuzz | ||
Patrick Mezard
|
r16122 | old, oldstart, new, newstart = h.fuzzit(0, False) | ||
oldstart += self.offset | ||||
orig_start = oldstart | ||||
Greg Onufer
|
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 | ||||
Yuya Nishihara
|
r37821 | if self.skew == 0 and diffhelper.testhunk(old, self.lines, oldstart): | ||
Patrick Mezard
|
r14451 | if self.remove: | ||
Patrick Mezard
|
r14348 | self.backend.unlink(self.fname) | ||
Bryan O'Sullivan
|
r4897 | else: | ||
Augie Fackler
|
r43346 | self.lines[oldstart : oldstart + len(old)] = new | ||
Patrick Mezard
|
r16122 | self.offset += len(new) - len(old) | ||
Martin Geisler
|
r14217 | self.dirty = True | ||
Bryan O'Sullivan
|
r4897 | return 0 | ||
Patrick Mezard
|
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) | ||||
Bryan O'Sullivan
|
r4897 | |||
Augie Fackler
|
r43347 | for fuzzlen in pycompat.xrange( | ||
self.ui.configint(b"patch", b"fuzz") + 1 | ||||
): | ||||
Matt Mackall
|
r10282 | for toponly in [True, False]: | ||
Patrick Mezard
|
r16122 | old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) | ||
Patrick Mezard
|
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] | ||||
Bryan O'Sullivan
|
r4897 | |||
for l in cand: | ||||
Yuya Nishihara
|
r37821 | if not old or diffhelper.testhunk(old, self.lines, l): | ||
Patrick Mezard
|
r16121 | self.lines[l : l + len(old)] = new | ||
self.offset += len(new) - len(old) | ||||
Greg Onufer
|
r10135 | self.skew = l - orig_start | ||
Martin Geisler
|
r14217 | self.dirty = True | ||
Wagner Bruna
|
r10518 | offset = l - orig_start - fuzzlen | ||
Bryan O'Sullivan
|
r4897 | if fuzzlen: | ||
Augie Fackler
|
r43346 | msg = _( | ||
Augie Fackler
|
r43347 | b"Hunk #%d succeeded at %d " | ||
b"with fuzz %d " | ||||
b"(offset %d lines).\n" | ||||
Augie Fackler
|
r43346 | ) | ||
Bryan O'Sullivan
|
r4897 | self.printfile(True) | ||
Augie Fackler
|
r43346 | self.ui.warn( | ||
msg % (h.number, l + 1, fuzzlen, offset) | ||||
) | ||||
Bryan O'Sullivan
|
r4897 | else: | ||
Augie Fackler
|
r43346 | msg = _( | ||
Augie Fackler
|
r43347 | b"Hunk #%d succeeded at %d " | ||
b"(offset %d lines).\n" | ||||
Augie Fackler
|
r43346 | ) | ||
Wagner Bruna
|
r10518 | self.ui.note(msg % (h.number, l + 1, offset)) | ||
Bryan O'Sullivan
|
r4897 | return fuzzlen | ||
self.printfile(True) | ||||
Augie Fackler
|
r43347 | self.ui.warn(_(b"Hunk #%d FAILED at %d\n") % (h.number, orig_start)) | ||
Patrick Mezard
|
r10127 | self.rej.append(horig) | ||
Bryan O'Sullivan
|
r4897 | return -1 | ||
Patrick Mezard
|
r13701 | def close(self): | ||
if self.dirty: | ||||
Patrick Mezard
|
r14367 | self.writelines(self.fname, self.lines, self.mode) | ||
Patrick Mezard
|
r13701 | self.write_rej() | ||
return len(self.rej) | ||||
Augie Fackler
|
r43346 | |||
Laurent Charignon
|
r24261 | class header(object): | ||
"""patch header | ||||
""" | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
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)') | ||||
Laurent Charignon
|
r24261 | |||
def __init__(self, header): | ||||
self.header = header | ||||
self.hunks = [] | ||||
def binary(self): | ||||
Augie Fackler
|
r43347 | return any(h.startswith(b'index ') for h in self.header) | ||
Laurent Charignon
|
r24261 | |||
def pretty(self, fp): | ||||
for h in self.header: | ||||
Augie Fackler
|
r43347 | if h.startswith(b'index '): | ||
fp.write(_(b'this modifies a binary file (all or nothing)\n')) | ||||
Laurent Charignon
|
r24261 | break | ||
if self.pretty_re.match(h): | ||||
fp.write(h) | ||||
if self.binary(): | ||||
Augie Fackler
|
r43347 | fp.write(_(b'this is a binary file\n')) | ||
Laurent Charignon
|
r24261 | break | ||
Augie Fackler
|
r43347 | if h.startswith(b'---'): | ||
Augie Fackler
|
r43346 | fp.write( | ||
Augie Fackler
|
r43347 | _(b'%d hunks, %d lines changed\n') | ||
Augie Fackler
|
r43346 | % ( | ||
len(self.hunks), | ||||
sum([max(h.added, h.removed) for h in self.hunks]), | ||||
) | ||||
) | ||||
Laurent Charignon
|
r24261 | break | ||
fp.write(h) | ||||
def write(self, fp): | ||||
Augie Fackler
|
r43347 | fp.write(b''.join(self.header)) | ||
Laurent Charignon
|
r24261 | |||
def allhunks(self): | ||||
Augie Fackler
|
r25149 | return any(self.allhunks_re.match(h) for h in self.header) | ||
Laurent Charignon
|
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): | ||||
Matt Harbison
|
r44396 | return '<header %s>' % ( | ||
' '.join(pycompat.rapply(pycompat.fsdecode, self.files())) | ||||
) | ||||
Laurent Charignon
|
r24261 | |||
Laurent Charignon
|
r24845 | def isnewfile(self): | ||
Augie Fackler
|
r25149 | return any(self.newfile_re.match(h) for h in self.header) | ||
Laurent Charignon
|
r24845 | |||
Laurent Charignon
|
r24261 | def special(self): | ||
Laurent Charignon
|
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 | ||||
Augie Fackler
|
r43346 | return emptynewfile or any( | ||
self.special_re.match(h) for h in self.header | ||||
) | ||||
Laurent Charignon
|
r24261 | |||
Laurent Charignon
|
r24263 | class recordhunk(object): | ||
"""patch hunk | ||||
XXX shouldn't we merge this with the other hunk class? | ||||
""" | ||||
Augie Fackler
|
r43346 | def __init__( | ||
self, | ||||
header, | ||||
fromline, | ||||
toline, | ||||
proc, | ||||
before, | ||||
hunk, | ||||
after, | ||||
maxcontext=None, | ||||
): | ||||
Jun Wu
|
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 | ||||
Laurent Charignon
|
r24263 | |||
self.header = header | ||||
Jun Wu
|
r33270 | trimedbefore, self.before = trimcontext(before, True) | ||
self.fromline = fromline + trimedbefore | ||||
self.toline = toline + trimedbefore | ||||
_trimedafter, self.after = trimcontext(after, False) | ||||
Laurent Charignon
|
r24263 | self.proc = proc | ||
self.hunk = hunk | ||||
self.added, self.removed = self.countchanges(self.hunk) | ||||
Laurent Charignon
|
r24346 | def __eq__(self, v): | ||
if not isinstance(v, recordhunk): | ||||
return False | ||||
Augie Fackler
|
r43346 | return ( | ||
(v.hunk == self.hunk) | ||||
and (v.proc == self.proc) | ||||
and (self.fromline == v.fromline) | ||||
and (self.header.files() == v.header.files()) | ||||
) | ||||
Laurent Charignon
|
r24346 | |||
def __hash__(self): | ||||
Augie Fackler
|
r43346 | return hash( | ||
( | ||||
tuple(self.hunk), | ||||
tuple(self.header.files()), | ||||
self.fromline, | ||||
self.proc, | ||||
) | ||||
) | ||||
Laurent Charignon
|
r24346 | |||
Laurent Charignon
|
r24263 | def countchanges(self, hunk): | ||
"""hunk -> (n+,n-)""" | ||||
Augie Fackler
|
r43347 | add = len([h for h in hunk if h.startswith(b'+')]) | ||
rem = len([h for h in hunk if h.startswith(b'-')]) | ||||
Laurent Charignon
|
r24263 | return add, rem | ||
Jun Wu
|
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. | ||||
""" | ||||
Augie Fackler
|
r43347 | m = {b'+': b'-', b'-': b'+', b'\\': b'\\'} | ||
hunk = [b'%s%s' % (m[l[0:1]], l[1:]) for l in self.hunk] | ||||
Augie Fackler
|
r43346 | return recordhunk( | ||
self.header, | ||||
self.toline, | ||||
self.fromline, | ||||
self.proc, | ||||
self.before, | ||||
hunk, | ||||
self.after, | ||||
) | ||||
Jun Wu
|
r32979 | |||
Laurent Charignon
|
r24263 | def write(self, fp): | ||
delta = len(self.before) + len(self.after) | ||||
Rodrigo Damazio Bovendorp
|
r45714 | if self.after and self.after[-1] == diffhelper.MISSING_NEWLINE_MARKER: | ||
Laurent Charignon
|
r24263 | delta -= 1 | ||
fromlen = delta + self.removed | ||||
tolen = delta + self.added | ||||
Augie Fackler
|
r43346 | fp.write( | ||
Augie Fackler
|
r43347 | b'@@ -%d,%d +%d,%d @@%s\n' | ||
Augie Fackler
|
r43346 | % ( | ||
self.fromline, | ||||
fromlen, | ||||
self.toline, | ||||
tolen, | ||||
Augie Fackler
|
r43347 | self.proc and (b' ' + self.proc), | ||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Augie Fackler
|
r43347 | fp.write(b''.join(self.before + self.hunk + self.after)) | ||
Laurent Charignon
|
r24263 | |||
pretty = write | ||||
def filename(self): | ||||
return self.header.filename() | ||||
Kyle Lippincott
|
r44742 | @encoding.strmethod | ||
Laurent Charignon
|
r24263 | def __repr__(self): | ||
Augie Fackler
|
r43347 | return b'<hunk %r@%d>' % (self.filename(), self.fromline) | ||
Laurent Charignon
|
r24263 | |||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r34567 | def getmessages(): | ||
return { | ||||
Augie Fackler
|
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'?"), | ||||
Jun Wu
|
r34567 | }, | ||
Augie Fackler
|
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'?"), | ||||
Jun Wu
|
r34567 | }, | ||
Augie Fackler
|
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)' | ||||
Augie Fackler
|
r43346 | ), | ||
Augie Fackler
|
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)' | ||||
Augie Fackler
|
r43346 | ), | ||
Augie Fackler
|
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)' | ||||
Augie Fackler
|
r43346 | ), | ||
Augie Fackler
|
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)' | ||||
Augie Fackler
|
r43346 | ), | ||
}, | ||||
Pulkit Goyal
|
r34044 | } | ||
Augie Fackler
|
r43346 | |||
Denis Laxalde
|
r42238 | def filterpatch(ui, headers, match, operation=None): | ||
Laurent Charignon
|
r24269 | """Interactively filter patch chunks into applied-only chunks""" | ||
Jun Wu
|
r34567 | messages = getmessages() | ||
Laurent Charignon
|
r25359 | if operation is None: | ||
Augie Fackler
|
r43347 | operation = b'record' | ||
Laurent Charignon
|
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: | ||||
Augie Fackler
|
r43347 | resps = messages[b'help'][operation] | ||
Kyle Lippincott
|
r42766 | # IMPORTANT: keep the last line of this prompt short (<40 english | ||
# chars is a good target) because of issue6158. | ||||
Augie Fackler
|
r43347 | r = ui.promptchoice(b"%s\n(enter ? for help) %s" % (query, resps)) | ||
ui.write(b"\n") | ||||
Augie Fackler
|
r43346 | if r == 8: # ? | ||
Laurent Charignon
|
r24269 | for c, t in ui.extractchoices(resps)[1]: | ||
Augie Fackler
|
r43347 | ui.write(b'%s - %s\n' % (c, encoding.lower(t))) | ||
Laurent Charignon
|
r24269 | continue | ||
Augie Fackler
|
r43346 | elif r == 0: # yes | ||
Laurent Charignon
|
r24269 | ret = True | ||
Augie Fackler
|
r43346 | elif r == 1: # no | ||
Laurent Charignon
|
r24269 | ret = False | ||
Augie Fackler
|
r43346 | elif r == 2: # Edit patch | ||
Laurent Charignon
|
r24269 | if chunk is None: | ||
Augie Fackler
|
r43347 | ui.write(_(b'cannot edit patch for whole file')) | ||
ui.write(b"\n") | ||||
Laurent Charignon
|
r24269 | continue | ||
if chunk.header.binary(): | ||||
Augie Fackler
|
r43347 | ui.write(_(b'cannot edit patch for binary file')) | ||
ui.write(b"\n") | ||||
Laurent Charignon
|
r24269 | continue | ||
# Patch comment based on the Git one (based on comment at end of | ||||
Matt Mackall
|
r26421 | # https://mercurial-scm.org/wiki/RecordExtension) | ||
Augie Fackler
|
r43347 | phelp = b'---' + _( | ||
Augie Fackler
|
r43346 | """ | ||
Laurent Charignon
|
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. | ||||
Augie Fackler
|
r43346 | """ | ||
) | ||||
(patchfd, patchfn) = pycompat.mkstemp( | ||||
Augie Fackler
|
r43347 | prefix=b"hg-editor-", suffix=b".diff" | ||
Augie Fackler
|
r43346 | ) | ||
Laurent Charignon
|
r24269 | ncpatchfp = None | ||
try: | ||||
# Write the initial patch | ||||
Augie Fackler
|
r43906 | f = util.nativeeolwriter(os.fdopen(patchfd, 'wb')) | ||
Laurent Charignon
|
r24269 | chunk.header.write(f) | ||
chunk.write(f) | ||||
Augie Fackler
|
r43346 | f.write( | ||
Augie Fackler
|
r43347 | b''.join( | ||
[b'# ' + i + b'\n' for i in phelp.splitlines()] | ||||
) | ||||
Augie Fackler
|
r43346 | ) | ||
Laurent Charignon
|
r24269 | f.close() | ||
# Start the editor and wait for it to complete | ||||
editor = ui.geteditor() | ||||
Augie Fackler
|
r43346 | ret = ui.system( | ||
Augie Fackler
|
r43347 | b"%s \"%s\"" % (editor, patchfn), | ||
environ={b'HGUSER': ui.username()}, | ||||
blockedtag=b'filterpatch', | ||||
Augie Fackler
|
r43346 | ) | ||
Laurent Charignon
|
r25483 | if ret != 0: | ||
Augie Fackler
|
r43347 | ui.warn(_(b"editor exited with exit code %d\n") % ret) | ||
Laurent Charignon
|
r25483 | continue | ||
Laurent Charignon
|
r24269 | # Remove comment lines | ||
Augie Fackler
|
r43906 | patchfp = open(patchfn, 'rb') | ||
timeless
|
r28861 | ncpatchfp = stringio() | ||
Jun Wu
|
r30397 | for line in util.iterfile(patchfp): | ||
Yuya Nishihara
|
r36856 | line = util.fromnativeeol(line) | ||
Augie Fackler
|
r43347 | if not line.startswith(b'#'): | ||
Laurent Charignon
|
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 | ||||
Augie Fackler
|
r43346 | elif r == 3: # Skip | ||
Laurent Charignon
|
r24269 | ret = skipfile = False | ||
Augie Fackler
|
r43346 | elif r == 4: # file (Record remaining) | ||
Laurent Charignon
|
r24269 | ret = skipfile = True | ||
Augie Fackler
|
r43346 | elif r == 5: # done, skip remaining | ||
Laurent Charignon
|
r24269 | ret = skipall = False | ||
Augie Fackler
|
r43346 | elif r == 6: # all | ||
Laurent Charignon
|
r24269 | ret = skipall = True | ||
Augie Fackler
|
r43346 | elif r == 7: # quit | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'user quit')) | ||
Laurent Charignon
|
r24269 | return ret, skipfile, skipall, newpatches | ||
seen = set() | ||||
Augie Fackler
|
r43346 | applied = {} # 'filename' -> [] of chunks | ||
Laurent Charignon
|
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 | ||||
Augie Fackler
|
r43347 | hdr = b''.join(h.header) | ||
Laurent Charignon
|
r24269 | if hdr in seen: | ||
continue | ||||
seen.add(hdr) | ||||
if skipall is None: | ||||
h.pretty(ui) | ||||
Denis Laxalde
|
r42238 | files = h.files() | ||
Augie Fackler
|
r43347 | msg = _(b'examine changes to %s?') % _(b' and ').join( | ||
b"'%s'" % f for f in files | ||||
Augie Fackler
|
r43346 | ) | ||
Denis Laxalde
|
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) | ||||
Laurent Charignon
|
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: | ||||
Augie Fackler
|
r43347 | msg = messages[b'single'][operation] % chunk.filename() | ||
Laurent Charignon
|
r24269 | else: | ||
idx = pos - len(h.hunks) + i | ||||
Augie Fackler
|
r43347 | msg = messages[b'multiple'][operation] % ( | ||
Augie Fackler
|
r43346 | idx, | ||
total, | ||||
chunk.filename(), | ||||
) | ||||
r, skipfile, skipall, newpatches = prompt( | ||||
skipfile, skipall, msg, chunk | ||||
) | ||||
Laurent Charignon
|
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 | ||||
Augie Fackler
|
r43346 | return ( | ||
sum( | ||||
Gregory Szorc
|
r43374 | [ | ||
h | ||||
for h in pycompat.itervalues(applied) | ||||
if h[0].special() or len(h) > 1 | ||||
], | ||||
Augie Fackler
|
r43346 | [], | ||
), | ||||
{}, | ||||
) | ||||
Benoit Boissinot
|
r8778 | class hunk(object): | ||
Patrick Mezard
|
r14451 | def __init__(self, desc, num, lr, context): | ||
Bryan O'Sullivan
|
r4897 | self.number = num | ||
self.desc = desc | ||||
Matt Mackall
|
r10282 | self.hunk = [desc] | ||
Bryan O'Sullivan
|
r4897 | self.a = [] | ||
self.b = [] | ||||
Benoit Boissinot
|
r9682 | self.starta = self.lena = None | ||
self.startb = self.lenb = None | ||||
Patrick Mezard
|
r10127 | if lr is not None: | ||
if context: | ||||
self.read_context_hunk(lr) | ||||
else: | ||||
self.read_unified_hunk(lr) | ||||
Bryan O'Sullivan
|
r4897 | |||
Patrick Mezard
|
r10127 | def getnormalized(self): | ||
"""Return a copy with line endings normalized to LF.""" | ||||
def normalize(lines): | ||||
nlines = [] | ||||
for line in lines: | ||||
Augie Fackler
|
r43347 | if line.endswith(b'\r\n'): | ||
line = line[:-2] + b'\n' | ||||
Patrick Mezard
|
r10127 | nlines.append(line) | ||
return nlines | ||||
# Dummy object, it is rebuilt manually | ||||
Patrick Mezard
|
r14451 | nh = hunk(self.desc, self.number, None, None) | ||
Patrick Mezard
|
r10127 | nh.number = self.number | ||
nh.desc = self.desc | ||||
Patrick Mezard
|
r10524 | nh.hunk = self.hunk | ||
Patrick Mezard
|
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 | ||||
Bryan O'Sullivan
|
r4897 | def read_unified_hunk(self, lr): | ||
m = unidesc.match(self.desc) | ||||
if not m: | ||||
Augie Fackler
|
r43347 | raise PatchError(_(b"bad hunk #%d") % self.number) | ||
Patrick Mezard
|
r15510 | self.starta, self.lena, self.startb, self.lenb = m.groups() | ||
Martin Geisler
|
r8527 | if self.lena is None: | ||
Bryan O'Sullivan
|
r4897 | self.lena = 1 | ||
else: | ||||
self.lena = int(self.lena) | ||||
Martin Geisler
|
r8527 | if self.lenb is None: | ||
Bryan O'Sullivan
|
r4897 | self.lenb = 1 | ||
else: | ||||
self.lenb = int(self.lenb) | ||||
self.starta = int(self.starta) | ||||
self.startb = int(self.startb) | ||||
Yuya Nishihara
|
r37591 | try: | ||
Augie Fackler
|
r43346 | diffhelper.addlines( | ||
lr, self.hunk, self.lena, self.lenb, self.a, self.b | ||||
) | ||||
Yuya Nishihara
|
r37591 | except error.ParseError as e: | ||
Augie Fackler
|
r43347 | raise PatchError(_(b"bad hunk #%d: %s") % (self.number, e)) | ||
Bryan O'Sullivan
|
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: | ||||
Dirkjan Ochtman
|
r6948 | del self.hunk[-1] | ||
del self.a[-1] | ||||
del self.b[-1] | ||||
self.lena -= 1 | ||||
self.lenb -= 1 | ||||
Patrick Mezard
|
r13699 | self._fixnewline(lr) | ||
Bryan O'Sullivan
|
r4897 | |||
def read_context_hunk(self, lr): | ||||
self.desc = lr.readline() | ||||
m = contextdesc.match(self.desc) | ||||
if not m: | ||||
Augie Fackler
|
r43347 | raise PatchError(_(b"bad hunk #%d") % self.number) | ||
Patrick Mezard
|
r15510 | self.starta, aend = m.groups() | ||
Bryan O'Sullivan
|
r4897 | self.starta = int(self.starta) | ||
Martin Geisler
|
r8527 | if aend is None: | ||
Bryan O'Sullivan
|
r4897 | aend = self.starta | ||
self.lena = int(aend) - self.starta | ||||
if self.starta: | ||||
self.lena += 1 | ||||
Gregory Szorc
|
r38806 | for x in pycompat.xrange(self.lena): | ||
Bryan O'Sullivan
|
r4897 | l = lr.readline() | ||
Augie Fackler
|
r43347 | if l.startswith(b'---'): | ||
Patrick Mezard
|
r12825 | # lines addition, old block is empty | ||
Bryan O'Sullivan
|
r4897 | lr.push(l) | ||
break | ||||
s = l[2:] | ||||
Augie Fackler
|
r43347 | if l.startswith(b'- ') or l.startswith(b'! '): | ||
u = b'-' + s | ||||
elif l.startswith(b' '): | ||||
u = b' ' + s | ||||
Bryan O'Sullivan
|
r4897 | else: | ||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b"bad hunk #%d old text line %d") % (self.number, x) | ||
Augie Fackler
|
r43346 | ) | ||
Bryan O'Sullivan
|
r4897 | self.a.append(u) | ||
self.hunk.append(u) | ||||
l = lr.readline() | ||||
Gregory Szorc
|
r41675 | if l.startswith(br'\ '): | ||
Bryan O'Sullivan
|
r4897 | s = self.a[-1][:-1] | ||
self.a[-1] = s | ||||
self.hunk[-1] = s | ||||
l = lr.readline() | ||||
m = contextdesc.match(l) | ||||
if not m: | ||||
Augie Fackler
|
r43347 | raise PatchError(_(b"bad hunk #%d") % self.number) | ||
Patrick Mezard
|
r15510 | self.startb, bend = m.groups() | ||
Bryan O'Sullivan
|
r4897 | self.startb = int(self.startb) | ||
Martin Geisler
|
r8527 | if bend is None: | ||
Bryan O'Sullivan
|
r4897 | bend = self.startb | ||
self.lenb = int(bend) - self.startb | ||||
if self.startb: | ||||
self.lenb += 1 | ||||
hunki = 1 | ||||
Gregory Szorc
|
r38806 | for x in pycompat.xrange(self.lenb): | ||
Bryan O'Sullivan
|
r4897 | l = lr.readline() | ||
Gregory Szorc
|
r41675 | if l.startswith(br'\ '): | ||
Patrick Mezard
|
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. | ||||
Bryan O'Sullivan
|
r4897 | s = self.b[-1][:-1] | ||
self.b[-1] = s | ||||
Matt Mackall
|
r10282 | self.hunk[hunki - 1] = s | ||
Bryan O'Sullivan
|
r4897 | continue | ||
if not l: | ||||
Patrick Mezard
|
r12825 | # line deletions, new block is empty and we hit EOF | ||
Bryan O'Sullivan
|
r4897 | lr.push(l) | ||
break | ||||
s = l[2:] | ||||
Augie Fackler
|
r43347 | if l.startswith(b'+ ') or l.startswith(b'! '): | ||
u = b'+' + s | ||||
elif l.startswith(b' '): | ||||
u = b' ' + s | ||||
Bryan O'Sullivan
|
r4897 | elif len(self.b) == 0: | ||
Patrick Mezard
|
r12825 | # line deletions, new block is empty | ||
Bryan O'Sullivan
|
r4897 | lr.push(l) | ||
break | ||||
else: | ||||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b"bad hunk #%d old text line %d") % (self.number, x) | ||
Augie Fackler
|
r43346 | ) | ||
Bryan O'Sullivan
|
r4897 | self.b.append(s) | ||
while True: | ||||
if hunki >= len(self.hunk): | ||||
Augie Fackler
|
r43347 | h = b"" | ||
Bryan O'Sullivan
|
r4897 | else: | ||
h = self.hunk[hunki] | ||||
hunki += 1 | ||||
if h == u: | ||||
break | ||||
Augie Fackler
|
r43347 | elif h.startswith(b'-'): | ||
Bryan O'Sullivan
|
r4897 | continue | ||
else: | ||||
Matt Mackall
|
r10282 | self.hunk.insert(hunki - 1, u) | ||
Bryan O'Sullivan
|
r4897 | break | ||
if not self.a: | ||||
# this happens when lines were only added to the hunk | ||||
for x in self.hunk: | ||||
Augie Fackler
|
r43347 | if x.startswith(b'-') or x.startswith(b' '): | ||
Bryan O'Sullivan
|
r4897 | self.a.append(x) | ||
if not self.b: | ||||
# this happens when lines were only deleted from the hunk | ||||
for x in self.hunk: | ||||
Augie Fackler
|
r43347 | if x.startswith(b'+') or x.startswith(b' '): | ||
Bryan O'Sullivan
|
r4897 | self.b.append(x[1:]) | ||
# @@ -start,len +start,len @@ | ||||
Augie Fackler
|
r43347 | self.desc = b"@@ -%d,%d +%d,%d @@\n" % ( | ||
Augie Fackler
|
r43346 | self.starta, | ||
self.lena, | ||||
self.startb, | ||||
self.lenb, | ||||
) | ||||
Bryan O'Sullivan
|
r4897 | self.hunk[0] = self.desc | ||
Patrick Mezard
|
r13699 | self._fixnewline(lr) | ||
Bryan O'Sullivan
|
r4897 | |||
Patrick Mezard
|
r13699 | def _fixnewline(self, lr): | ||
l = lr.readline() | ||||
Gregory Szorc
|
r41675 | if l.startswith(br'\ '): | ||
Yuya Nishihara
|
r37821 | diffhelper.fixnewline(self.hunk, self.a, self.b) | ||
Patrick Mezard
|
r13699 | else: | ||
lr.push(l) | ||||
Bryan O'Sullivan
|
r4897 | |||
def complete(self): | ||||
return len(self.a) == self.lena and len(self.b) == self.lenb | ||||
Patrick Mezard
|
r16121 | def _fuzzit(self, old, new, fuzz, toponly): | ||
Bryan O'Sullivan
|
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. | ||||
Patrick Mezard
|
r16124 | fuzz = min(fuzz, len(old)) | ||
Bryan O'Sullivan
|
r4897 | if fuzz: | ||
top = 0 | ||||
bot = 0 | ||||
hlen = len(self.hunk) | ||||
Gregory Szorc
|
r38806 | for x in pycompat.xrange(hlen - 1): | ||
Bryan O'Sullivan
|
r4897 | # the hunk starts with the @@ line, so use x+1 | ||
Augie Fackler
|
r43347 | if self.hunk[x + 1].startswith(b' '): | ||
Bryan O'Sullivan
|
r4897 | top += 1 | ||
else: | ||||
break | ||||
if not toponly: | ||||
Gregory Szorc
|
r38806 | for x in pycompat.xrange(hlen - 1): | ||
Augie Fackler
|
r43347 | if self.hunk[hlen - bot - 1].startswith(b' '): | ||
Bryan O'Sullivan
|
r4897 | bot += 1 | ||
else: | ||||
break | ||||
Patrick Mezard
|
r16124 | bot = min(fuzz, bot) | ||
top = min(fuzz, top) | ||||
Augie Fackler
|
r43346 | return old[top : len(old) - bot], new[top : len(new) - bot], top | ||
Patrick Mezard
|
r16122 | return old, new, 0 | ||
Bryan O'Sullivan
|
r4897 | |||
Patrick Mezard
|
r16121 | def fuzzit(self, fuzz, toponly): | ||
Patrick Mezard
|
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 | ||||
Yuya Nishihara
|
r16650 | if self.lena and oldstart > 0: | ||
Patrick Mezard
|
r16122 | oldstart -= 1 | ||
Yuya Nishihara
|
r16650 | if self.lenb and newstart > 0: | ||
Patrick Mezard
|
r16122 | newstart -= 1 | ||
return old, oldstart, new, newstart | ||||
Bryan O'Sullivan
|
r4897 | |||
Augie Fackler
|
r43346 | |||
Thomas Arendsen Hein
|
r14764 | class binhunk(object): | ||
Matt Harbison
|
r44226 | """A binary patch file.""" | ||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r16523 | def __init__(self, lr, fname): | ||
Bryan O'Sullivan
|
r4897 | self.text = None | ||
Nicolas Vigier
|
r20137 | self.delta = False | ||
Augie Fackler
|
r43347 | self.hunk = [b'GIT binary patch\n'] | ||
Patrick Mezard
|
r16523 | self._fname = fname | ||
Patrick Mezard
|
r14384 | self._read(lr) | ||
Bryan O'Sullivan
|
r4897 | |||
def complete(self): | ||||
return self.text is not None | ||||
Nicolas Vigier
|
r20137 | def new(self, lines): | ||
if self.delta: | ||||
Augie Fackler
|
r43347 | return [applybindelta(self.text, b''.join(lines))] | ||
Bryan O'Sullivan
|
r4897 | return [self.text] | ||
Patrick Mezard
|
r14384 | def _read(self, lr): | ||
Patrick Mezard
|
r16524 | def getline(lr, hunk): | ||
l = lr.readline() | ||||
hunk.append(l) | ||||
Augie Fackler
|
r43347 | return l.rstrip(b'\r\n') | ||
Patrick Mezard
|
r16524 | |||
Patrick Mezard
|
r16567 | while True: | ||
Patrick Mezard
|
r16524 | line = getline(lr, self.hunk) | ||
Patrick Mezard
|
r16567 | if not line: | ||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b'could not extract "%s" binary data') % self._fname | ||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r43347 | if line.startswith(b'literal '): | ||
Nicolas Vigier
|
r20137 | size = int(line[8:].rstrip()) | ||
Patrick Mezard
|
r16567 | break | ||
Augie Fackler
|
r43347 | if line.startswith(b'delta '): | ||
Nicolas Vigier
|
r20137 | size = int(line[6:].rstrip()) | ||
self.delta = True | ||||
break | ||||
Brendan Cully
|
r3367 | dec = [] | ||
Patrick Mezard
|
r16524 | line = getline(lr, self.hunk) | ||
Bryan O'Sullivan
|
r4897 | while len(line) > 1: | ||
Pulkit Goyal
|
r36210 | l = line[0:1] | ||
Augie Fackler
|
r43347 | if l <= b'Z' and l >= b'A': | ||
l = ord(l) - ord(b'A') + 1 | ||||
Brendan Cully
|
r3374 | else: | ||
Augie Fackler
|
r43347 | l = ord(l) - ord(b'a') + 27 | ||
Patrick Mezard
|
r16522 | try: | ||
Yuya Nishihara
|
r32200 | dec.append(util.b85decode(line[1:])[:l]) | ||
Gregory Szorc
|
r25660 | except ValueError as e: | ||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b'could not decode "%s" binary patch: %s') | ||
Augie Fackler
|
r43346 | % (self._fname, stringutil.forcebytestr(e)) | ||
) | ||||
Patrick Mezard
|
r16524 | line = getline(lr, self.hunk) | ||
Augie Fackler
|
r43347 | text = zlib.decompress(b''.join(dec)) | ||
Brendan Cully
|
r3367 | if len(text) != size: | ||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b'"%s" length is %d bytes, should be %d') | ||
Augie Fackler
|
r43346 | % (self._fname, len(text), size) | ||
) | ||||
Bryan O'Sullivan
|
r4897 | self.text = text | ||
Brendan Cully
|
r3367 | |||
Augie Fackler
|
r43346 | |||
Bryan O'Sullivan
|
r4897 | def parsefilename(str): | ||
# --- filename \t|space stuff | ||||
Augie Fackler
|
r43347 | s = str[4:].rstrip(b'\r\n') | ||
i = s.find(b'\t') | ||||
Bryan O'Sullivan
|
r4897 | if i < 0: | ||
Augie Fackler
|
r43347 | i = s.find(b' ') | ||
Bryan O'Sullivan
|
r4897 | if i < 0: | ||
return s | ||||
return s[:i] | ||||
Brendan Cully
|
r2861 | |||
Augie Fackler
|
r43346 | |||
Laurent Charignon
|
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: | ||||
Yuya Nishihara
|
r34133 | >>> rawpatch = b"""diff --git a/folder1/g b/folder1/g | ||
Laurent Charignon
|
r25424 | ... --- a/folder1/g | ||
... +++ b/folder1/g | ||||
... @@ -1,7 +1,7 @@ | ||||
... +firstline | ||||
... c | ||||
... 1 | ||||
... 2 | ||||
... + 3 | ||||
... -4 | ||||
... 5 | ||||
... d | ||||
... +lastline""" | ||||
Yuya Nishihara
|
r34254 | >>> hunks = parsepatch([rawpatch]) | ||
Laurent Charignon
|
r25424 | >>> hunkscomingfromfilterpatch = [] | ||
>>> for h in hunks: | ||||
... hunkscomingfromfilterpatch.append(h) | ||||
... hunkscomingfromfilterpatch.extend(h.hunks) | ||||
>>> reversedhunks = reversehunks(hunkscomingfromfilterpatch) | ||||
timeless
|
r28861 | >>> from . import util | ||
>>> fp = util.stringio() | ||||
Laurent Charignon
|
r25424 | >>> for c in reversedhunks: | ||
... c.write(fp) | ||||
Yuya Nishihara
|
r34254 | >>> fp.seek(0) or None | ||
Laurent Charignon
|
r25424 | >>> reversedpatch = fp.read() | ||
Yuya Nishihara
|
r34139 | >>> print(pycompat.sysstr(reversedpatch)) | ||
Laurent Charignon
|
r25424 | diff --git a/folder1/g b/folder1/g | ||
--- a/folder1/g | ||||
+++ b/folder1/g | ||||
@@ -1,4 +1,3 @@ | ||||
-firstline | ||||
c | ||||
1 | ||||
2 | ||||
Jun Wu
|
r32979 | @@ -2,6 +1,6 @@ | ||
Laurent Charignon
|
r25424 | c | ||
1 | ||||
2 | ||||
- 3 | ||||
+4 | ||||
5 | ||||
d | ||||
Jun Wu
|
r32979 | @@ -6,3 +5,2 @@ | ||
Laurent Charignon
|
r25424 | 5 | ||
d | ||||
-lastline | ||||
''' | ||||
newhunks = [] | ||||
for c in hunks: | ||||
Augie Fackler
|
r43347 | if util.safehasattr(c, b'reversehunk'): | ||
Jun Wu
|
r32979 | c = c.reversehunk() | ||
Laurent Charignon
|
r25424 | newhunks.append(c) | ||
return newhunks | ||||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r33270 | def parsepatch(originalchunks, maxcontext=None): | ||
"""patch -> [] of headers -> [] of hunks | ||||
If maxcontext is not None, trim context lines if necessary. | ||||
Yuya Nishihara
|
r34133 | >>> rawpatch = b'''diff --git a/folder1/g b/folder1/g | ||
Jun Wu
|
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) | ||||
Yuya Nishihara
|
r34139 | >>> print(pycompat.sysstr(out.getvalue())) | ||
Jun Wu
|
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 | ||||
""" | ||||
Augie Fackler
|
r43346 | |||
Laurent Charignon
|
r24265 | class parser(object): | ||
"""patch parsing state machine""" | ||||
Augie Fackler
|
r43346 | |||
Laurent Charignon
|
r24265 | def __init__(self): | ||
self.fromline = 0 | ||||
self.toline = 0 | ||||
Augie Fackler
|
r43347 | self.proc = b'' | ||
Laurent Charignon
|
r24265 | self.header = None | ||
self.context = [] | ||||
self.before = [] | ||||
self.hunk = [] | ||||
self.headers = [] | ||||
def addrange(self, limits): | ||||
Kyle Lippincott
|
r41497 | self.addcontext([]) | ||
Laurent Charignon
|
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: | ||||
Augie Fackler
|
r43346 | h = recordhunk( | ||
self.header, | ||||
self.fromline, | ||||
self.toline, | ||||
self.proc, | ||||
self.before, | ||||
self.hunk, | ||||
context, | ||||
maxcontext, | ||||
) | ||||
Laurent Charignon
|
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 = [] | ||||
Kyle Lippincott
|
r41497 | if self.hunk: | ||
self.addcontext([]) | ||||
Laurent Charignon
|
r24265 | self.hunk = hunk | ||
def newfile(self, hdr): | ||||
self.addcontext([]) | ||||
h = header(hdr) | ||||
self.headers.append(h) | ||||
self.header = h | ||||
def addother(self, line): | ||||
Augie Fackler
|
r43346 | pass # 'other' lines are ignored | ||
Laurent Charignon
|
r24265 | |||
def finished(self): | ||||
self.addcontext([]) | ||||
return self.headers | ||||
transitions = { | ||||
Augie Fackler
|
r43347 | b'file': { | ||
b'context': addcontext, | ||||
b'file': newfile, | ||||
b'hunk': addhunk, | ||||
b'range': addrange, | ||||
Augie Fackler
|
r43346 | }, | ||
Augie Fackler
|
r43347 | b'context': { | ||
b'file': newfile, | ||||
b'hunk': addhunk, | ||||
b'range': addrange, | ||||
b'other': addother, | ||||
Augie Fackler
|
r43346 | }, | ||
Augie Fackler
|
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}, | ||||
Augie Fackler
|
r43346 | } | ||
Laurent Charignon
|
r24265 | |||
p = parser() | ||||
timeless
|
r28861 | fp = stringio() | ||
Augie Fackler
|
r43347 | fp.write(b''.join(originalchunks)) | ||
Laurent Charignon
|
r24341 | fp.seek(0) | ||
Laurent Charignon
|
r24265 | |||
Augie Fackler
|
r43347 | state = b'context' | ||
Laurent Charignon
|
r24265 | for newstate, data in scanpatch(fp): | ||
try: | ||||
p.transitions[state][newstate](p, data) | ||||
except KeyError: | ||||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | b'unhandled transition: %s -> %s' % (state, newstate) | ||
Augie Fackler
|
r43346 | ) | ||
Laurent Charignon
|
r24265 | state = newstate | ||
Laurent Charignon
|
r24341 | del fp | ||
Laurent Charignon
|
r24265 | return p.finished() | ||
Augie Fackler
|
r43346 | |||
Siddharth Agarwal
|
r24244 | def pathtransform(path, strip, prefix): | ||
Siddharth Agarwal
|
r24243 | '''turn a path from a patch into a path suitable for the repository | ||
Siddharth Agarwal
|
r24244 | prefix, if not empty, is expected to be normalized with a / at the end. | ||
Siddharth Agarwal
|
r24243 | Returns (stripped components, path in repository). | ||
Yuya Nishihara
|
r34133 | >>> pathtransform(b'a/b/c', 0, b'') | ||
Siddharth Agarwal
|
r24243 | ('', 'a/b/c') | ||
Yuya Nishihara
|
r34133 | >>> pathtransform(b' a/b/c ', 0, b'') | ||
Siddharth Agarwal
|
r24243 | ('', ' a/b/c') | ||
Yuya Nishihara
|
r34133 | >>> pathtransform(b' a/b/c ', 2, b'') | ||
Siddharth Agarwal
|
r24243 | ('a/b/', 'c') | ||
Yuya Nishihara
|
r34133 | >>> pathtransform(b'a/b/c', 0, b'd/e/') | ||
Siddharth Agarwal
|
r24385 | ('', 'd/e/a/b/c') | ||
Yuya Nishihara
|
r34133 | >>> pathtransform(b' a//b/c ', 2, b'd/e/') | ||
Siddharth Agarwal
|
r24244 | ('a//b/', 'd/e/c') | ||
Yuya Nishihara
|
r34133 | >>> pathtransform(b'a/b/c', 3, b'') | ||
Siddharth Agarwal
|
r24243 | Traceback (most recent call last): | ||
PatchError: unable to strip away 1 of 3 dirs from a/b/c | ||||
''' | ||||
Mads Kiilerich
|
r11022 | pathlen = len(path) | ||
i = 0 | ||||
if strip == 0: | ||||
Augie Fackler
|
r43347 | return b'', prefix + path.rstrip() | ||
Mads Kiilerich
|
r11022 | count = strip | ||
while count > 0: | ||||
Augie Fackler
|
r43347 | i = path.find(b'/', i) | ||
Mads Kiilerich
|
r11022 | if i == -1: | ||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b"unable to strip away %d of %d dirs from %s") | ||
Augie Fackler
|
r43346 | % (count, strip, path) | ||
) | ||||
Mads Kiilerich
|
r11022 | i += 1 | ||
# consume '//' in the path | ||||
Augie Fackler
|
r43347 | while i < pathlen - 1 and path[i : i + 1] == b'/': | ||
Mads Kiilerich
|
r11022 | i += 1 | ||
count -= 1 | ||||
Siddharth Agarwal
|
r24244 | return path[:i].lstrip(), prefix + path[i:].rstrip() | ||
Mads Kiilerich
|
r11022 | |||
Augie Fackler
|
r43346 | |||
Siddharth Agarwal
|
r24245 | def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip, prefix): | ||
Augie Fackler
|
r43347 | nulla = afile_orig == b"/dev/null" | ||
nullb = bfile_orig == b"/dev/null" | ||||
Patrick Mezard
|
r14451 | create = nulla and hunk.starta == 0 and hunk.lena == 0 | ||
remove = nullb and hunk.startb == 0 and hunk.lenb == 0 | ||||
Siddharth Agarwal
|
r24245 | abase, afile = pathtransform(afile_orig, strip, prefix) | ||
Patrick Mezard
|
r14351 | gooda = not nulla and backend.exists(afile) | ||
Siddharth Agarwal
|
r24245 | bbase, bfile = pathtransform(bfile_orig, strip, prefix) | ||
Bryan O'Sullivan
|
r4897 | if afile == bfile: | ||
goodb = gooda | ||||
else: | ||||
Patrick Mezard
|
r14351 | goodb = not nullb and backend.exists(bfile) | ||
Patrick Mezard
|
r14451 | missing = not goodb and not gooda and not create | ||
Brendan Cully
|
r9328 | |||
Martin Geisler
|
r11820 | # some diff programs apparently produce patches where the afile is | ||
# not /dev/null, but afile starts with bfile | ||||
Augie Fackler
|
r43347 | abasedir = afile[: afile.rfind(b'/') + 1] | ||
bbasedir = bfile[: bfile.rfind(b'/') + 1] | ||||
Augie Fackler
|
r43346 | if ( | ||
missing | ||||
and abasedir == bbasedir | ||||
and afile.startswith(bfile) | ||||
and hunk.starta == 0 | ||||
and hunk.lena == 0 | ||||
): | ||||
Patrick Mezard
|
r14451 | create = True | ||
missing = False | ||||
Brendan Cully
|
r9328 | |||
Patrick Mezard
|
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). | ||||
Augie Fackler
|
r43346 | isbackup = abase == bbase and bfile.startswith(afile) | ||
Patrick Mezard
|
r5652 | fname = None | ||
if not missing: | ||||
if gooda and goodb: | ||||
Jordi Gutiérrez Hermoso
|
r24306 | if isbackup: | ||
fname = afile | ||||
else: | ||||
fname = bfile | ||||
Patrick Mezard
|
r5652 | elif gooda: | ||
Bryan O'Sullivan
|
r4897 | fname = afile | ||
Thomas Arendsen Hein
|
r5760 | |||
Patrick Mezard
|
r5652 | if not fname: | ||
if not nullb: | ||||
Jordi Gutiérrez Hermoso
|
r24306 | if isbackup: | ||
fname = afile | ||||
else: | ||||
fname = bfile | ||||
Patrick Mezard
|
r5652 | elif not nulla: | ||
Bryan O'Sullivan
|
r4897 | fname = afile | ||
Patrick Mezard
|
r5652 | else: | ||
Augie Fackler
|
r43347 | raise PatchError(_(b"undefined source and destination files")) | ||
Thomas Arendsen Hein
|
r5760 | |||
Patrick Mezard
|
r14566 | gp = patchmeta(fname) | ||
if create: | ||||
Augie Fackler
|
r43347 | gp.op = b'ADD' | ||
Patrick Mezard
|
r14566 | elif remove: | ||
Augie Fackler
|
r43347 | gp.op = b'DELETE' | ||
Patrick Mezard
|
r14566 | return gp | ||
Bryan O'Sullivan
|
r4897 | |||
Augie Fackler
|
r43346 | |||
Laurent Charignon
|
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)) | ||||
""" | ||||
Yuya Nishihara
|
r34068 | lines_re = re.compile(br'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)') | ||
Laurent Charignon
|
r24264 | lr = linereader(fp) | ||
def scanwhile(first, p): | ||||
"""scan lr while predicate holds""" | ||||
lines = [first] | ||||
Augie Fackler
|
r43347 | for line in iter(lr.readline, b''): | ||
Laurent Charignon
|
r24264 | if p(line): | ||
lines.append(line) | ||||
else: | ||||
lr.push(line) | ||||
break | ||||
return lines | ||||
Augie Fackler
|
r43347 | for line in iter(lr.readline, b''): | ||
if line.startswith(b'diff --git a/') or line.startswith(b'diff -r '): | ||||
Augie Fackler
|
r43346 | |||
Laurent Charignon
|
r24264 | def notheader(line): | ||
s = line.split(None, 1) | ||||
Augie Fackler
|
r43347 | return not s or s[0] not in (b'---', b'diff') | ||
Augie Fackler
|
r43346 | |||
Laurent Charignon
|
r24264 | header = scanwhile(line, notheader) | ||
fromfile = lr.readline() | ||||
Augie Fackler
|
r43347 | if fromfile.startswith(b'---'): | ||
Laurent Charignon
|
r24264 | tofile = lr.readline() | ||
header += [fromfile, tofile] | ||||
else: | ||||
lr.push(fromfile) | ||||
Augie Fackler
|
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)) | ||||
Laurent Charignon
|
r24264 | else: | ||
m = lines_re.match(line) | ||||
if m: | ||||
Augie Fackler
|
r43347 | yield b'range', m.groups() | ||
Laurent Charignon
|
r24264 | else: | ||
Augie Fackler
|
r43347 | yield b'other', line | ||
Laurent Charignon
|
r24264 | |||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r7152 | def scangitpatch(lr, firstline): | ||
Dirkjan Ochtman
|
r7186 | """ | ||
Patrick Mezard
|
r7152 | Git patches can emit: | ||
- rename a to b | ||||
- change b | ||||
- copy a to c | ||||
- change c | ||||
Dirkjan Ochtman
|
r7186 | |||
Patrick Mezard
|
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: | ||||
timeless
|
r28861 | fp = stringio(lr.fp.read()) | ||
Patrick Mezard
|
r14418 | gitlr = linereader(fp) | ||
Patrick Mezard
|
r7152 | gitlr.push(firstline) | ||
Patrick Mezard
|
r12669 | gitpatches = readgitpatch(gitlr) | ||
Patrick Mezard
|
r7152 | fp.seek(pos) | ||
Patrick Mezard
|
r12669 | return gitpatches | ||
Patrick Mezard
|
r7152 | |||
Augie Fackler
|
r43346 | |||
Idan Kamara
|
r14240 | def iterhunks(fp): | ||
Patrick Mezard
|
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. | ||||
""" | ||||
Augie Fackler
|
r43347 | afile = b"" | ||
bfile = b"" | ||||
Bryan O'Sullivan
|
r4897 | state = None | ||
hunknum = 0 | ||||
Patrick Mezard
|
r14017 | emitfile = newfile = False | ||
Patrick Mezard
|
r14388 | gitpatches = None | ||
Brendan Cully
|
r2861 | |||
Bryan O'Sullivan
|
r4897 | # our states | ||
BFILE = 1 | ||||
context = None | ||||
Patrick Mezard
|
r10128 | lr = linereader(fp) | ||
Brendan Cully
|
r2861 | |||
Augie Fackler
|
r43347 | for x in iter(lr.readline, b''): | ||
Patrick Mezard
|
r14383 | if state == BFILE and ( | ||
Augie Fackler
|
r43347 | (not context and x.startswith(b'@')) | ||
or (context is not False and x.startswith(b'***************')) | ||||
or x.startswith(b'GIT binary patch') | ||||
Augie Fackler
|
r43346 | ): | ||
Patrick Mezard
|
r14388 | gp = None | ||
Augie Fackler
|
r43346 | if gitpatches and gitpatches[-1].ispatching(afile, bfile): | ||
Patrick Mezard
|
r16506 | gp = gitpatches.pop() | ||
Augie Fackler
|
r43347 | if x.startswith(b'GIT binary patch'): | ||
Patrick Mezard
|
r16523 | h = binhunk(lr, gp.path) | ||
Patrick Mezard
|
r14383 | else: | ||
Augie Fackler
|
r43347 | if context is None and x.startswith(b'***************'): | ||
Patrick Mezard
|
r14383 | context = True | ||
Patrick Mezard
|
r14451 | h = hunk(x, hunknum + 1, lr, context) | ||
Bryan O'Sullivan
|
r4897 | hunknum += 1 | ||
Patrick Mezard
|
r5650 | if emitfile: | ||
emitfile = False | ||||
Augie Fackler
|
r43347 | yield b'file', (afile, bfile, h, gp and gp.copy() or None) | ||
yield b'hunk', h | ||||
elif x.startswith(b'diff --git a/'): | ||||
m = gitre.match(x.rstrip(b' \r\n')) | ||||
Patrick Mezard
|
r14387 | if not m: | ||
continue | ||||
Patrick Mezard
|
r16506 | if gitpatches is None: | ||
Patrick Mezard
|
r14387 | # scan whole input for git metadata | ||
Patrick Mezard
|
r16506 | gitpatches = scangitpatch(lr, x) | ||
Augie Fackler
|
r43347 | yield b'git', [ | ||
g.copy() for g in gitpatches if g.op in (b'COPY', b'RENAME') | ||||
Augie Fackler
|
r43346 | ] | ||
Patrick Mezard
|
r14388 | gitpatches.reverse() | ||
Augie Fackler
|
r43347 | afile = b'a/' + m.group(1) | ||
bfile = b'b/' + m.group(2) | ||||
Patrick Mezard
|
r16506 | while gitpatches and not gitpatches[-1].ispatching(afile, bfile): | ||
gp = gitpatches.pop() | ||||
Augie Fackler
|
r43347 | yield b'file', ( | ||
b'a/' + gp.path, | ||||
b'b/' + gp.path, | ||||
None, | ||||
gp.copy(), | ||||
) | ||||
Patrick Mezard
|
r16506 | if not gitpatches: | ||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b'failed to synchronize metadata for "%s"') % afile[2:] | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r14387 | newfile = True | ||
Augie Fackler
|
r43347 | elif x.startswith(b'---'): | ||
Bryan O'Sullivan
|
r4897 | # check for a unified diff | ||
l2 = lr.readline() | ||||
Augie Fackler
|
r43347 | if not l2.startswith(b'+++'): | ||
Bryan O'Sullivan
|
r4897 | lr.push(l2) | ||
continue | ||||
newfile = True | ||||
context = False | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
Augie Fackler
|
r43347 | elif x.startswith(b'***'): | ||
Bryan O'Sullivan
|
r4897 | # check for a context diff | ||
l2 = lr.readline() | ||||
Augie Fackler
|
r43347 | if not l2.startswith(b'---'): | ||
Bryan O'Sullivan
|
r4897 | lr.push(l2) | ||
continue | ||||
l3 = lr.readline() | ||||
lr.push(l3) | ||||
Augie Fackler
|
r43347 | if not l3.startswith(b"***************"): | ||
Bryan O'Sullivan
|
r4897 | lr.push(l2) | ||
continue | ||||
newfile = True | ||||
context = True | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
Benoit Boissinot
|
r3057 | |||
Patrick Mezard
|
r14017 | if newfile: | ||
newfile = False | ||||
Patrick Mezard
|
r5650 | emitfile = True | ||
Bryan O'Sullivan
|
r4897 | state = BFILE | ||
hunknum = 0 | ||||
Patrick Mezard
|
r5650 | |||
Patrick Mezard
|
r14388 | while gitpatches: | ||
Patrick Mezard
|
r16506 | gp = gitpatches.pop() | ||
Augie Fackler
|
r43347 | yield b'file', (b'a/' + gp.path, b'b/' + gp.path, None, gp.copy()) | ||
Patrick Mezard
|
r14388 | |||
Augie Fackler
|
r43346 | |||
Nicolas Vigier
|
r20137 | def applybindelta(binchunk, data): | ||
"""Apply a binary delta hunk | ||||
The algorithm used is the algorithm from git's patch-delta.c | ||||
""" | ||||
Augie Fackler
|
r43346 | |||
Nicolas Vigier
|
r20137 | def deltahead(binchunk): | ||
i = 0 | ||||
Pulkit Goyal
|
r38096 | for c in pycompat.bytestr(binchunk): | ||
Nicolas Vigier
|
r20137 | i += 1 | ||
if not (ord(c) & 0x80): | ||||
return i | ||||
return i | ||||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | out = b"" | ||
Nicolas Vigier
|
r20137 | s = deltahead(binchunk) | ||
binchunk = binchunk[s:] | ||||
s = deltahead(binchunk) | ||||
binchunk = binchunk[s:] | ||||
i = 0 | ||||
while i < len(binchunk): | ||||
Augie Fackler
|
r43346 | cmd = ord(binchunk[i : i + 1]) | ||
Nicolas Vigier
|
r20137 | i += 1 | ||
Augie Fackler
|
r43346 | if cmd & 0x80: | ||
Nicolas Vigier
|
r20137 | offset = 0 | ||
size = 0 | ||||
Augie Fackler
|
r43346 | if cmd & 0x01: | ||
offset = ord(binchunk[i : i + 1]) | ||||
Nicolas Vigier
|
r20137 | i += 1 | ||
Augie Fackler
|
r43346 | if cmd & 0x02: | ||
offset |= ord(binchunk[i : i + 1]) << 8 | ||||
Nicolas Vigier
|
r20137 | i += 1 | ||
Augie Fackler
|
r43346 | if cmd & 0x04: | ||
offset |= ord(binchunk[i : i + 1]) << 16 | ||||
Nicolas Vigier
|
r20137 | i += 1 | ||
Augie Fackler
|
r43346 | if cmd & 0x08: | ||
offset |= ord(binchunk[i : i + 1]) << 24 | ||||
Nicolas Vigier
|
r20137 | i += 1 | ||
Augie Fackler
|
r43346 | if cmd & 0x10: | ||
size = ord(binchunk[i : i + 1]) | ||||
Nicolas Vigier
|
r20137 | i += 1 | ||
Augie Fackler
|
r43346 | if cmd & 0x20: | ||
size |= ord(binchunk[i : i + 1]) << 8 | ||||
Nicolas Vigier
|
r20137 | i += 1 | ||
Augie Fackler
|
r43346 | if cmd & 0x40: | ||
size |= ord(binchunk[i : i + 1]) << 16 | ||||
Nicolas Vigier
|
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: | ||||
Augie Fackler
|
r43347 | raise PatchError(_(b'unexpected delta opcode 0')) | ||
Nicolas Vigier
|
r20137 | return out | ||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | def applydiff(ui, fp, backend, store, strip=1, prefix=b'', eolmode=b'strict'): | ||
Augie Fackler
|
r10966 | """Reads a patch from fp and tries to apply it. | ||
Patrick Mezard
|
r5650 | |||
Patrick Mezard
|
r14565 | Returns 0 for a clean patch, -1 if any rejects were found and 1 if | ||
there was any fuzz. | ||||
Patrick Mezard
|
r8810 | |||
Martin Geisler
|
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'. | ||||
Patrick Mezard
|
r8810 | """ | ||
Augie Fackler
|
r43346 | return _applydiff( | ||
ui, | ||||
fp, | ||||
patchfile, | ||||
backend, | ||||
store, | ||||
strip=strip, | ||||
prefix=prefix, | ||||
eolmode=eolmode, | ||||
) | ||||
Augie Fackler
|
r10966 | |||
Martin von Zweigbergk
|
r35053 | def _canonprefix(repo, prefix): | ||
if prefix: | ||||
prefix = pathutil.canonpath(repo.root, repo.getcwd(), prefix) | ||||
Augie Fackler
|
r43347 | if prefix != b'': | ||
prefix += b'/' | ||||
Martin von Zweigbergk
|
r35053 | return prefix | ||
Augie Fackler
|
r43346 | |||
def _applydiff( | ||||
Augie Fackler
|
r43347 | ui, fp, patcher, backend, store, strip=1, prefix=b'', eolmode=b'strict' | ||
Augie Fackler
|
r43346 | ): | ||
Martin von Zweigbergk
|
r35053 | prefix = _canonprefix(backend.repo, prefix) | ||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r14389 | def pstrip(p): | ||
Siddharth Agarwal
|
r24246 | return pathtransform(p, strip - 1, prefix)[1] | ||
Patrick Mezard
|
r14389 | |||
Patrick Mezard
|
r5650 | rejects = 0 | ||
err = 0 | ||||
current_file = None | ||||
Idan Kamara
|
r14240 | for state, values in iterhunks(fp): | ||
Augie Fackler
|
r43347 | if state == b'hunk': | ||
Patrick Mezard
|
r5650 | if not current_file: | ||
continue | ||||
Mads Kiilerich
|
r11021 | ret = current_file.apply(values) | ||
Patrick Mezard
|
r14565 | if ret > 0: | ||
err = 1 | ||||
Augie Fackler
|
r43347 | elif state == b'file': | ||
Patrick Mezard
|
r13701 | if current_file: | ||
rejects += current_file.close() | ||||
Patrick Mezard
|
r14388 | current_file = None | ||
afile, bfile, first_hunk, gp = values | ||||
if gp: | ||||
Patrick Mezard
|
r14566 | gp.path = pstrip(gp.path) | ||
Patrick Mezard
|
r14452 | if gp.oldpath: | ||
Patrick Mezard
|
r14566 | gp.oldpath = pstrip(gp.oldpath) | ||
else: | ||||
Augie Fackler
|
r43346 | gp = makepatchmeta( | ||
backend, afile, bfile, first_hunk, strip, prefix | ||||
) | ||||
Augie Fackler
|
r43347 | if gp.op == b'RENAME': | ||
Patrick Mezard
|
r14566 | backend.unlink(gp.oldpath) | ||
Patrick Mezard
|
r14388 | if not first_hunk: | ||
Augie Fackler
|
r43347 | if gp.op == b'DELETE': | ||
Patrick Mezard
|
r14566 | backend.unlink(gp.path) | ||
continue | ||||
data, mode = None, None | ||||
Augie Fackler
|
r43347 | if gp.op in (b'RENAME', b'COPY'): | ||
Patrick Mezard
|
r14609 | data, mode = store.getfile(gp.oldpath)[:2] | ||
Ryan McElroy
|
r30078 | if data is None: | ||
# This means that the old path does not exist | ||||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b"source file '%s' does not exist") % gp.oldpath | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r14566 | if gp.mode: | ||
mode = gp.mode | ||||
Augie Fackler
|
r43347 | if gp.op == b'ADD': | ||
Patrick Mezard
|
r14566 | # Added files without content have no hunk and | ||
# must be created | ||||
Augie Fackler
|
r43347 | data = b'' | ||
Patrick Mezard
|
r14566 | if data or mode: | ||
Augie Fackler
|
r43347 | if gp.op in (b'ADD', b'RENAME', b'COPY') and backend.exists( | ||
Augie Fackler
|
r43346 | gp.path | ||
): | ||||
raise PatchError( | ||||
Augie Fackler
|
r43347 | _( | ||
b"cannot create %s: destination " | ||||
b"already exists" | ||||
) | ||||
Augie Fackler
|
r43346 | % gp.path | ||
) | ||||
Patrick Mezard
|
r14566 | backend.setfile(gp.path, data, mode, gp.oldpath) | ||
Patrick Mezard
|
r14388 | continue | ||
Patrick Mezard
|
r5650 | try: | ||
Augie Fackler
|
r43346 | current_file = patcher(ui, gp, backend, store, eolmode=eolmode) | ||
Gregory Szorc
|
r25660 | except PatchError as inst: | ||
Matt Harbison
|
r44127 | ui.warn(stringutil.forcebytestr(inst) + b'\n') | ||
Mads Kiilerich
|
r11021 | current_file = None | ||
Patrick Mezard
|
r5650 | rejects += 1 | ||
continue | ||||
Augie Fackler
|
r43347 | elif state == b'git': | ||
Mads Kiilerich
|
r11021 | for gp in values: | ||
Patrick Mezard
|
r14452 | path = pstrip(gp.oldpath) | ||
Mads Kiilerich
|
r22296 | data, mode = backend.getfile(path) | ||
if data is None: | ||||
Patrick Mezard
|
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. | ||||
Mads Kiilerich
|
r22296 | pass | ||
Patrick Mezard
|
r16813 | else: | ||
store.setfile(path, data, mode) | ||||
Bryan O'Sullivan
|
r4897 | else: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'unsupported parser state: %s') % state) | ||
Patrick Mezard
|
r5649 | |||
Patrick Mezard
|
r13701 | if current_file: | ||
rejects += current_file.close() | ||||
Patrick Mezard
|
r5650 | |||
Bryan O'Sullivan
|
r4897 | if rejects: | ||
return -1 | ||||
return err | ||||
Vadim Gelfer
|
r2874 | |||
Augie Fackler
|
r43346 | |||
def _externalpatch(ui, repo, patcher, patchname, strip, files, similarity): | ||||
Patrick Mezard
|
r7151 | """use <patcher> to apply <patchname> to the working directory. | ||
returns whether patch was applied with fuzz factor.""" | ||||
fuzz = False | ||||
Patrick Mezard
|
r12673 | args = [] | ||
Patrick Mezard
|
r14382 | cwd = repo.root | ||
Patrick Mezard
|
r7151 | if cwd: | ||
Augie Fackler
|
r43347 | args.append(b'-d %s' % procutil.shellquote(cwd)) | ||
cmd = b'%s %s -p%d < %s' % ( | ||||
Augie Fackler
|
r43346 | patcher, | ||
Augie Fackler
|
r43347 | b' '.join(args), | ||
Augie Fackler
|
r43346 | strip, | ||
procutil.shellquote(patchname), | ||||
) | ||||
Augie Fackler
|
r43347 | ui.debug(b'Using external patch tool: %s\n' % cmd) | ||
fp = procutil.popen(cmd, b'rb') | ||||
Patrick Mezard
|
r14381 | try: | ||
Jun Wu
|
r30397 | for line in util.iterfile(fp): | ||
Patrick Mezard
|
r14381 | line = line.rstrip() | ||
Augie Fackler
|
r43347 | ui.note(line + b'\n') | ||
if line.startswith(b'patching file '): | ||||
Patrick Mezard
|
r14381 | pf = util.parsepatchoutput(line) | ||
printed_file = False | ||||
Patrick Mezard
|
r14564 | files.add(pf) | ||
Augie Fackler
|
r43347 | elif line.find(b'with fuzz') >= 0: | ||
Patrick Mezard
|
r14381 | fuzz = True | ||
if not printed_file: | ||||
Augie Fackler
|
r43347 | ui.warn(pf + b'\n') | ||
Patrick Mezard
|
r14381 | printed_file = True | ||
Augie Fackler
|
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: | ||||
Patrick Mezard
|
r14381 | if not printed_file: | ||
Augie Fackler
|
r43347 | ui.warn(pf + b'\n') | ||
Patrick Mezard
|
r14381 | printed_file = True | ||
Augie Fackler
|
r43347 | ui.warn(line + b'\n') | ||
Patrick Mezard
|
r14381 | finally: | ||
if files: | ||||
Siddharth Agarwal
|
r19155 | scmutil.marktouched(repo, files, similarity) | ||
Patrick Mezard
|
r7151 | code = fp.close() | ||
if code: | ||||
Augie Fackler
|
r43346 | raise PatchError( | ||
Augie Fackler
|
r43347 | _(b"patch command failed: %s") % procutil.explainexit(code) | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r7151 | return fuzz | ||
Augie Fackler
|
r43346 | |||
def patchbackend( | ||||
Augie Fackler
|
r43347 | ui, backend, patchobj, strip, prefix, files=None, eolmode=b'strict' | ||
Augie Fackler
|
r43346 | ): | ||
Benoit Boissinot
|
r9683 | if files is None: | ||
Patrick Mezard
|
r14564 | files = set() | ||
Patrick Mezard
|
r8810 | if eolmode is None: | ||
Augie Fackler
|
r43347 | eolmode = ui.config(b'patch', b'eol') | ||
Martin Geisler
|
r10101 | if eolmode.lower() not in eolmodes: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'unsupported line endings type: %s') % eolmode) | ||
Martin Geisler
|
r10101 | eolmode = eolmode.lower() | ||
Dirkjan Ochtman
|
r8843 | |||
Patrick Mezard
|
r14452 | store = filestore() | ||
Patrick Mezard
|
r7151 | try: | ||
Augie Fackler
|
r43347 | fp = open(patchobj, b'rb') | ||
Patrick Mezard
|
r7151 | except TypeError: | ||
fp = patchobj | ||||
try: | ||||
Augie Fackler
|
r43346 | ret = applydiff( | ||
ui, fp, backend, store, strip=strip, prefix=prefix, eolmode=eolmode | ||||
) | ||||
Patrick Mezard
|
r7151 | finally: | ||
Patrick Mezard
|
r10203 | if fp != patchobj: | ||
fp.close() | ||||
Patrick Mezard
|
r14564 | files.update(backend.close()) | ||
Patrick Mezard
|
r14452 | store.close() | ||
Patrick Mezard
|
r7151 | if ret < 0: | ||
Augie Fackler
|
r43347 | raise PatchError(_(b'patch failed to apply')) | ||
Patrick Mezard
|
r7151 | return ret > 0 | ||
Augie Fackler
|
r43346 | |||
def internalpatch( | ||||
ui, | ||||
repo, | ||||
patchobj, | ||||
strip, | ||||
Augie Fackler
|
r43347 | prefix=b'', | ||
Augie Fackler
|
r43346 | files=None, | ||
Augie Fackler
|
r43347 | eolmode=b'strict', | ||
Augie Fackler
|
r43346 | similarity=0, | ||
): | ||||
Patrick Mezard
|
r14611 | """use builtin patch to apply <patchobj> to the working directory. | ||
returns whether patch was applied with fuzz factor.""" | ||||
backend = workingbackend(ui, repo, similarity) | ||||
Siddharth Agarwal
|
r24254 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) | ||
Patrick Mezard
|
r14611 | |||
Augie Fackler
|
r43346 | |||
def patchrepo( | ||||
Augie Fackler
|
r43347 | ui, repo, ctx, store, patchobj, strip, prefix, files=None, eolmode=b'strict' | ||
Augie Fackler
|
r43346 | ): | ||
Patrick Mezard
|
r14611 | backend = repobackend(ui, repo, ctx, store) | ||
Siddharth Agarwal
|
r24260 | return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode) | ||
Patrick Mezard
|
r14611 | |||
Augie Fackler
|
r43346 | |||
def patch( | ||||
ui, | ||||
repo, | ||||
patchname, | ||||
strip=1, | ||||
Augie Fackler
|
r43347 | prefix=b'', | ||
Augie Fackler
|
r43346 | files=None, | ||
Augie Fackler
|
r43347 | eolmode=b'strict', | ||
Augie Fackler
|
r43346 | similarity=0, | ||
): | ||||
Patrick Mezard
|
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. | ||||
""" | ||||
Augie Fackler
|
r43347 | patcher = ui.config(b'ui', b'patch') | ||
Benoit Boissinot
|
r9683 | if files is None: | ||
Patrick Mezard
|
r14564 | files = set() | ||
Pierre-Yves David
|
r21553 | if patcher: | ||
Augie Fackler
|
r43346 | return _externalpatch( | ||
ui, repo, patcher, patchname, strip, files, similarity | ||||
) | ||||
return internalpatch( | ||||
ui, repo, patchname, strip, prefix, files, eolmode, similarity | ||||
) | ||||
Patrick Mezard
|
r7151 | |||
Augie Fackler
|
r43347 | def changedfiles(ui, repo, patchpath, strip=1, prefix=b''): | ||
Patrick Mezard
|
r14351 | backend = fsbackend(ui, repo.root) | ||
Martin von Zweigbergk
|
r35053 | prefix = _canonprefix(repo, prefix) | ||
Augie Fackler
|
r43347 | with open(patchpath, b'rb') as fp: | ||
Idan Kamara
|
r14255 | changed = set() | ||
for state, values in iterhunks(fp): | ||||
Augie Fackler
|
r43347 | if state == b'file': | ||
Patrick Mezard
|
r14388 | afile, bfile, first_hunk, gp = values | ||
if gp: | ||||
Martin von Zweigbergk
|
r35053 | gp.path = pathtransform(gp.path, strip - 1, prefix)[1] | ||
Patrick Mezard
|
r14566 | if gp.oldpath: | ||
Augie Fackler
|
r43346 | gp.oldpath = pathtransform( | ||
gp.oldpath, strip - 1, prefix | ||||
)[1] | ||||
Patrick Mezard
|
r14566 | else: | ||
Augie Fackler
|
r43346 | gp = makepatchmeta( | ||
backend, afile, bfile, first_hunk, strip, prefix | ||||
) | ||||
Patrick Mezard
|
r14566 | changed.add(gp.path) | ||
Augie Fackler
|
r43347 | if gp.op == b'RENAME': | ||
Patrick Mezard
|
r14566 | changed.add(gp.oldpath) | ||
Augie Fackler
|
r43347 | elif state not in (b'hunk', b'git'): | ||
raise error.Abort(_(b'unsupported parser state: %s') % state) | ||||
Idan Kamara
|
r14255 | return changed | ||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r10189 | class GitDiffRequired(Exception): | ||
pass | ||||
Dirkjan Ochtman
|
r7198 | |||
Augie Fackler
|
r43346 | |||
Yuya Nishihara
|
r38606 | diffopts = diffutil.diffallopts | ||
Boris Feld
|
r38581 | diffallopts = diffutil.diffallopts | ||
difffeatureopts = diffutil.difffeatureopts | ||||
Benoit Boissinot
|
r10615 | |||
Augie Fackler
|
r43346 | |||
def diff( | ||||
repo, | ||||
node1=None, | ||||
node2=None, | ||||
match=None, | ||||
changes=None, | ||||
opts=None, | ||||
losedatafn=None, | ||||
pathfn=None, | ||||
copy=None, | ||||
copysourcematch=None, | ||||
hunksfilterfn=None, | ||||
): | ||||
Dirkjan Ochtman
|
r7308 | '''yields diff of changes to files between two nodes, or node and | ||
Vadim Gelfer
|
r2874 | working directory. | ||
if node1 is None, use first dirstate parent instead. | ||||
Patrick Mezard
|
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. | ||||
Martin Geisler
|
r12167 | |||
prefix is a filename prefix that is prepended to all filenames on | ||||
display (used for subrepos). | ||||
Siddharth Agarwal
|
r24417 | |||
relroot, if not empty, must be normalized with a trailing /. Any match | ||||
Henrik Stuart
|
r29422 | patterns that fall outside it will be ignored. | ||
copy, if not empty, should contain mappings {dst@y: src@x} of copy | ||||
Denis Laxalde
|
r34857 | information. | ||
Martin von Zweigbergk
|
r41769 | if copysourcematch is not None, then copy sources will be filtered by this | ||
matcher | ||||
Denis Laxalde
|
r34857 | hunksfilterfn, if not None, should be a function taking a filectx and | ||
hunks generator that may yield filtered hunks. | ||||
''' | ||||
Martin von Zweigbergk
|
r41767 | if not node1 and not node2: | ||
node1 = repo.dirstate.p1() | ||||
ctx1 = repo[node1] | ||||
ctx2 = repo[node2] | ||||
Denis Laxalde
|
r34856 | for fctx1, fctx2, hdr, hunks in diffhunks( | ||
Augie Fackler
|
r43346 | repo, | ||
ctx1=ctx1, | ||||
ctx2=ctx2, | ||||
match=match, | ||||
changes=changes, | ||||
opts=opts, | ||||
losedatafn=losedatafn, | ||||
pathfn=pathfn, | ||||
copy=copy, | ||||
copysourcematch=copysourcematch, | ||||
): | ||||
Denis Laxalde
|
r34857 | if hunksfilterfn is not None: | ||
Denis Laxalde
|
r34909 | # If the file has been removed, fctx2 is None; but this should | ||
# not occur here since we catch removed files early in | ||||
Yuya Nishihara
|
r35906 | # logcmdutil.getlinerangerevs() for 'hg log -L'. | ||
Augie Fackler
|
r43346 | assert ( | ||
fctx2 is not None | ||||
Augie Fackler
|
r43347 | ), b'fctx2 unexpectly None in diff hunks filtering' | ||
Denis Laxalde
|
r34857 | hunks = hunksfilterfn(fctx2, hunks) | ||
Elmar Bartel
|
r45266 | text = b''.join(b''.join(hlines) for hrange, hlines in hunks) | ||
Denis Laxalde
|
r34562 | if hdr and (text or len(hdr) > 1): | ||
Augie Fackler
|
r43347 | yield b'\n'.join(hdr) + b'\n' | ||
Denis Laxalde
|
r31274 | if text: | ||
yield text | ||||
Augie Fackler
|
r43346 | |||
def diffhunks( | ||||
repo, | ||||
ctx1, | ||||
ctx2, | ||||
match=None, | ||||
changes=None, | ||||
opts=None, | ||||
losedatafn=None, | ||||
pathfn=None, | ||||
copy=None, | ||||
copysourcematch=None, | ||||
): | ||||
Denis Laxalde
|
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. | ||||
""" | ||||
Vadim Gelfer
|
r2874 | |||
if opts is None: | ||||
opts = mdiff.defaultopts | ||||
Brendan Cully
|
r9123 | def lrugetfilectx(): | ||
cache = {} | ||||
Martin von Zweigbergk
|
r25113 | order = collections.deque() | ||
Augie Fackler
|
r43346 | |||
Brendan Cully
|
r9123 | def getfilectx(f, ctx): | ||
fctx = ctx.filectx(f, filelog=cache.get(f)) | ||||
if f not in cache: | ||||
if len(cache) > 20: | ||||
Bryan O'Sullivan
|
r16803 | del cache[order.popleft()] | ||
Benoit Boissinot
|
r9684 | cache[f] = fctx.filelog() | ||
Brendan Cully
|
r9123 | else: | ||
order.remove(f) | ||||
order.append(f) | ||||
return fctx | ||||
Augie Fackler
|
r43346 | |||
Brendan Cully
|
r9123 | return getfilectx | ||
Augie Fackler
|
r43346 | |||
Brendan Cully
|
r9123 | getfilectx = lrugetfilectx() | ||
Brendan Cully
|
r2934 | |||
Vadim Gelfer
|
r2874 | if not changes: | ||
Martin von Zweigbergk
|
r38796 | changes = ctx1.status(ctx2, match=match) | ||
Augie Fackler
|
r44048 | if isinstance(changes, list): | ||
modified, added, removed = changes[:3] | ||||
else: | ||||
modified, added, removed = ( | ||||
changes.modified, | ||||
changes.added, | ||||
changes.removed, | ||||
) | ||||
Vadim Gelfer
|
r2874 | |||
if not modified and not added and not removed: | ||||
Patrick Mezard
|
r10189 | return [] | ||
Jordi Gutiérrez Hermoso
|
r24306 | if repo.ui.debugflag: | ||
hexfunc = hex | ||||
else: | ||||
hexfunc = short | ||||
Sean Farley
|
r21833 | revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node] | ||
Patrick Mezard
|
r10189 | |||
Henrik Stuart
|
r29422 | if copy is None: | ||
copy = {} | ||||
if opts.git or opts.upgrade: | ||||
copy = copies.pathcopies(ctx1, ctx2, match=match) | ||||
Patrick Mezard
|
r10189 | |||
Martin von Zweigbergk
|
r41769 | if copysourcematch: | ||
# filter out copies where source side isn't inside the matcher | ||||
Martin von Zweigbergk
|
r41640 | # (copies.pathcopies() already filtered out the destination) | ||
Augie Fackler
|
r43346 | copy = { | ||
Gregory Szorc
|
r43376 | dst: src | ||
for dst, src in pycompat.iteritems(copy) | ||||
if copysourcematch(src) | ||||
Augie Fackler
|
r43346 | } | ||
Siddharth Agarwal
|
r24417 | |||
Martin von Zweigbergk
|
r27900 | modifiedset = set(modified) | ||
addedset = set(added) | ||||
Martin von Zweigbergk
|
r27901 | removedset = set(removed) | ||
Martin von Zweigbergk
|
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) | ||||
Martin von Zweigbergk
|
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) | ||||
Martin von Zweigbergk
|
r27900 | modified = sorted(modifiedset) | ||
added = sorted(addedset) | ||||
Martin von Zweigbergk
|
r27901 | removed = sorted(removedset) | ||
Pulkit Goyal
|
r35607 | for dst, src in list(copy.items()): | ||
Martin von Zweigbergk
|
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] | ||||
Martin von Zweigbergk
|
r27900 | |||
Matt Harbison
|
r37782 | prefetchmatch = scmutil.matchfiles( | ||
Augie Fackler
|
r43346 | repo, list(modifiedset | addedset | removedset) | ||
) | ||||
Rodrigo Damazio Bovendorp
|
r45632 | revmatches = [ | ||
(ctx1.rev(), prefetchmatch), | ||||
(ctx2.rev(), prefetchmatch), | ||||
] | ||||
scmutil.prefetchfiles(repo, revmatches) | ||||
Matt Harbison
|
r37782 | |||
Mads Kiilerich
|
r17299 | def difffn(opts, losedata): | ||
Augie Fackler
|
r43346 | return trydiff( | ||
repo, | ||||
revs, | ||||
ctx1, | ||||
ctx2, | ||||
modified, | ||||
added, | ||||
removed, | ||||
copy, | ||||
getfilectx, | ||||
opts, | ||||
losedata, | ||||
pathfn, | ||||
) | ||||
Patrick Mezard
|
r10189 | if opts.upgrade and not opts.git: | ||
try: | ||||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
r10189 | def losedata(fn): | ||
if not losedatafn or not losedatafn(fn=fn): | ||||
Brodie Rao
|
r16687 | raise GitDiffRequired | ||
Augie Fackler
|
r43346 | |||
Patrick Mezard
|
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) | ||||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r37749 | def diffsinglehunk(hunklines): | ||
"""yield tokens for a list of lines in a single hunk""" | ||||
for line in hunklines: | ||||
# chomp | ||||
Augie Fackler
|
r43347 | chompline = line.rstrip(b'\r\n') | ||
Jun Wu
|
r37749 | # highlight tabs and trailing whitespace | ||
stripline = chompline.rstrip() | ||||
Augie Fackler
|
r43347 | if line.startswith(b'-'): | ||
label = b'diff.deleted' | ||||
elif line.startswith(b'+'): | ||||
label = b'diff.inserted' | ||||
Jun Wu
|
r37749 | else: | ||
Augie Fackler
|
r43347 | raise error.ProgrammingError(b'unexpected hunk line: %s' % line) | ||
Jun Wu
|
r37749 | for token in tabsplitter.findall(stripline): | ||
Augie Fackler
|
r43347 | if token.startswith(b'\t'): | ||
yield (token, b'diff.tab') | ||||
Jun Wu
|
r37749 | else: | ||
yield (token, label) | ||||
if chompline != stripline: | ||||
Augie Fackler
|
r43347 | yield (chompline[len(stripline) :], b'diff.trailingwhitespace') | ||
Jun Wu
|
r37749 | if chompline != line: | ||
Augie Fackler
|
r43347 | yield (line[len(chompline) :], b'') | ||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r37749 | |||
Jun Wu
|
r37750 | def diffsinglehunkinline(hunklines): | ||
"""yield tokens for a list of lines in a single hunk, with inline colors""" | ||||
# prepare deleted, and inserted content | ||||
Augie Fackler
|
r43347 | a = b'' | ||
b = b'' | ||||
Jun Wu
|
r37750 | for line in hunklines: | ||
Augie Fackler
|
r43347 | if line[0:1] == b'-': | ||
Jun Wu
|
r37750 | a += line[1:] | ||
Augie Fackler
|
r43347 | elif line[0:1] == b'+': | ||
Jun Wu
|
r37750 | b += line[1:] | ||
else: | ||||
Augie Fackler
|
r43347 | raise error.ProgrammingError(b'unexpected hunk line: %s' % line) | ||
Jun Wu
|
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 | ||||
al = wordsplitter.findall(a) | ||||
bl = wordsplitter.findall(b) | ||||
# re-arrange the words to lines since the diff algorithm is line-based | ||||
Augie Fackler
|
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) | ||||
Jun Wu
|
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: | ||||
Augie Fackler
|
r43347 | changed = btype == b'!' | ||
for token in mdiff.splitnewlines(b''.join(al[a1:a2])): | ||||
Jun Wu
|
r37750 | atokens.append((changed, token)) | ||
Augie Fackler
|
r43347 | for token in mdiff.splitnewlines(b''.join(bl[b1:b2])): | ||
Jun Wu
|
r37750 | btokens.append((changed, token)) | ||
# yield deleted tokens, then inserted ones | ||||
Augie Fackler
|
r43346 | for prefix, label, tokens in [ | ||
Augie Fackler
|
r43347 | (b'-', b'diff.deleted', atokens), | ||
(b'+', b'diff.inserted', btokens), | ||||
Augie Fackler
|
r43346 | ]: | ||
Jun Wu
|
r37750 | nextisnewline = True | ||
for changed, token in tokens: | ||||
if nextisnewline: | ||||
yield (prefix, label) | ||||
nextisnewline = False | ||||
# special handling line end | ||||
Augie Fackler
|
r43347 | isendofline = token.endswith(b'\n') | ||
Jun Wu
|
r37750 | if isendofline: | ||
Augie Fackler
|
r43346 | chomp = token[:-1] # chomp | ||
Augie Fackler
|
r43347 | if chomp.endswith(b'\r'): | ||
Sune Foldager
|
r38649 | chomp = chomp[:-1] | ||
Augie Fackler
|
r43346 | endofline = token[len(chomp) :] | ||
token = chomp.rstrip() # detect spaces at the end | ||||
endspaces = chomp[len(token) :] | ||||
Jun Wu
|
r37750 | # scan tabs | ||
for maybetab in tabsplitter.findall(token): | ||||
Mark Thomas
|
r40318 | if b'\t' == maybetab[0:1]: | ||
Augie Fackler
|
r43347 | currentlabel = b'diff.tab' | ||
Jun Wu
|
r37750 | else: | ||
if changed: | ||||
Augie Fackler
|
r43347 | currentlabel = label + b'.changed' | ||
Jun Wu
|
r37750 | else: | ||
Augie Fackler
|
r43347 | currentlabel = label + b'.unchanged' | ||
Jun Wu
|
r37750 | yield (maybetab, currentlabel) | ||
if isendofline: | ||||
if endspaces: | ||||
Augie Fackler
|
r43347 | yield (endspaces, b'diff.trailingwhitespace') | ||
yield (endofline, b'') | ||||
Jun Wu
|
r37750 | nextisnewline = True | ||
Augie Fackler
|
r43346 | |||
Brodie Rao
|
r10818 | def difflabel(func, *args, **kw): | ||
'''yields 2-tuples of (output, label) based on the output of func()''' | ||||
Augie Fackler
|
r43906 | if kw.get('opts') and kw['opts'].worddiff: | ||
Jun Wu
|
r37750 | dodiffhunk = diffsinglehunkinline | ||
else: | ||||
dodiffhunk = diffsinglehunk | ||||
Augie Fackler
|
r43346 | headprefixes = [ | ||
Augie Fackler
|
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'), | ||||
Augie Fackler
|
r43346 | ] | ||
textprefixes = [ | ||||
Augie Fackler
|
r43347 | (b'@', b'diff.hunk'), | ||
Augie Fackler
|
r43346 | # - and + are handled by diffsinglehunk | ||
] | ||||
Kirill Elagin
|
r15201 | head = False | ||
Jun Wu
|
r37749 | |||
# buffers a hunk, i.e. adjacent "-", "+" lines without other changes. | ||||
hunkbuffer = [] | ||||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r37749 | def consumehunkbuffer(): | ||
if hunkbuffer: | ||||
Jun Wu
|
r37750 | for token in dodiffhunk(hunkbuffer): | ||
Jun Wu
|
r37749 | yield token | ||
hunkbuffer[:] = [] | ||||
Brodie Rao
|
r10818 | for chunk in func(*args, **kw): | ||
Augie Fackler
|
r43347 | lines = chunk.split(b'\n') | ||
Jun Wu
|
r37748 | linecount = len(lines) | ||
Brodie Rao
|
r10818 | for i, line in enumerate(lines): | ||
Kirill Elagin
|
r15201 | if head: | ||
Augie Fackler
|
r43347 | if line.startswith(b'@'): | ||
Kirill Elagin
|
r15201 | head = False | ||
else: | ||||
Augie Fackler
|
r43347 | if line and not line.startswith( | ||
(b' ', b'+', b'-', b'@', b'\\') | ||||
): | ||||
Kirill Elagin
|
r15201 | head = True | ||
Jordi Gutiérrez Hermoso
|
r22460 | diffline = False | ||
Augie Fackler
|
r43347 | if not head and line and line.startswith((b'+', b'-')): | ||
Jordi Gutiérrez Hermoso
|
r22460 | diffline = True | ||
Kirill Elagin
|
r15201 | prefixes = textprefixes | ||
if head: | ||||
prefixes = headprefixes | ||||
Jun Wu
|
r37749 | if diffline: | ||
# buffered | ||||
bufferedline = line | ||||
if i + 1 < linecount: | ||||
Augie Fackler
|
r43347 | bufferedline += b"\n" | ||
Jun Wu
|
r37749 | hunkbuffer.append(bufferedline) | ||
Brodie Rao
|
r10818 | else: | ||
Jun Wu
|
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: | ||||
Augie Fackler
|
r43346 | yield ( | ||
line[len(stripline) :], | ||||
Augie Fackler
|
r43347 | b'diff.trailingwhitespace', | ||
Augie Fackler
|
r43346 | ) | ||
Matthieu Laneuville
|
r35278 | break | ||
Jun Wu
|
r37749 | else: | ||
Augie Fackler
|
r43347 | yield (line, b'') | ||
Jun Wu
|
r37749 | if i + 1 < linecount: | ||
Augie Fackler
|
r43347 | yield (b'\n', b'') | ||
Jun Wu
|
r37749 | for token in consumehunkbuffer(): | ||
yield token | ||||
Matthieu Laneuville
|
r35278 | |||
Augie Fackler
|
r43346 | |||
Brodie Rao
|
r10818 | def diffui(*args, **kw): | ||
'''like diff(), but yields 2-tuples of (output, label) for ui.write()''' | ||||
return difflabel(diff, *args, **kw) | ||||
Augie Fackler
|
r43346 | |||
Martin von Zweigbergk
|
r27900 | def _filepairs(modified, added, removed, copy, opts): | ||
Martin von Zweigbergk
|
r24106 | '''generates tuples (f1, f2, copyop), where f1 is the name of the file | ||
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' | ||||
or 'rename' (the latter two only if opts.git is set).''' | ||||
gone = set() | ||||
Augie Fackler
|
r44937 | copyto = {v: k for k, v in copy.items()} | ||
Martin von Zweigbergk
|
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: | ||||
Augie Fackler
|
r43347 | copyop = b'rename' | ||
Martin von Zweigbergk
|
r24106 | gone.add(f1) | ||
else: | ||||
Augie Fackler
|
r43347 | copyop = b'copy' | ||
Martin von Zweigbergk
|
r24106 | elif f in removedset: | ||
f2 = None | ||||
if opts.git: | ||||
# have we already reported a copy above? | ||||
Augie Fackler
|
r43346 | if ( | ||
f in copyto | ||||
and copyto[f] in addedset | ||||
and copy[copyto[f]] == f | ||||
): | ||||
Martin von Zweigbergk
|
r24106 | continue | ||
yield f1, f2, copyop | ||||
Augie Fackler
|
r43346 | |||
Pulkit Goyal
|
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'} | ||||
Augie Fackler
|
r43346 | def trydiff( | ||
repo, | ||||
revs, | ||||
ctx1, | ||||
ctx2, | ||||
modified, | ||||
added, | ||||
removed, | ||||
copy, | ||||
getfilectx, | ||||
opts, | ||||
losedatafn, | ||||
pathfn, | ||||
): | ||||
Siddharth Agarwal
|
r24371 | '''given input data, generate a diff and yield it in blocks | ||
If generating a diff would lose data like flags or binary data and | ||||
losedatafn is not None, it will be called. | ||||
Martin von Zweigbergk
|
r41795 | pathfn is applied to every path in the diff output. | ||
''' | ||||
Martin Geisler
|
r12167 | |||
Siddharth Agarwal
|
r23300 | if opts.noprefix: | ||
Augie Fackler
|
r43347 | aprefix = bprefix = b'' | ||
Siddharth Agarwal
|
r23300 | else: | ||
Augie Fackler
|
r43347 | aprefix = b'a/' | ||
bprefix = b'b/' | ||||
Siddharth Agarwal
|
r23300 | |||
Martin von Zweigbergk
|
r24021 | def diffline(f, revs): | ||
Augie Fackler
|
r43347 | revinfo = b' '.join([b"-r %s" % rev for rev in revs]) | ||
return b'diff %s %s' % (revinfo, f) | ||||
Guillermo Pérez
|
r17941 | |||
Jun Wu
|
r32188 | def isempty(fctx): | ||
return fctx is None or fctx.size() == 0 | ||||
Boris Feld
|
r36625 | date1 = dateutil.datestr(ctx1.date()) | ||
date2 = dateutil.datestr(ctx2.date()) | ||||
Benoit Boissinot
|
r3967 | |||
Martin von Zweigbergk
|
r41795 | if not pathfn: | ||
pathfn = lambda f: f | ||||
Siddharth Agarwal
|
r24416 | |||
Martin von Zweigbergk
|
r27900 | for f1, f2, copyop in _filepairs(modified, added, removed, copy, opts): | ||
Martin von Zweigbergk
|
r24105 | content1 = None | ||
content2 = None | ||||
Jun Wu
|
r32187 | fctx1 = None | ||
fctx2 = None | ||||
Martin von Zweigbergk
|
r24103 | flag1 = None | ||
flag2 = None | ||||
Martin von Zweigbergk
|
r24107 | if f1: | ||
Jun Wu
|
r32187 | fctx1 = getfilectx(f1, ctx1) | ||
Martin von Zweigbergk
|
r24107 | if opts.git or losedatafn: | ||
flag1 = ctx1.flags(f1) | ||||
if f2: | ||||
Jun Wu
|
r32187 | fctx2 = getfilectx(f2, ctx2) | ||
Martin von Zweigbergk
|
r24107 | if opts.git or losedatafn: | ||
flag2 = ctx2.flags(f2) | ||||
Jun Wu
|
r32190 | # if binary is True, output "summary" or "base85", but not "text diff" | ||
Joerg Sonnenberger
|
r35868 | if opts.text: | ||
binary = False | ||||
else: | ||||
Yuya Nishihara
|
r35969 | binary = any(f.isbinary() for f in [fctx1, fctx2] if f is not None) | ||
Martin von Zweigbergk
|
r24057 | |||
if losedatafn and not opts.git: | ||||
Augie Fackler
|
r43346 | if ( | ||
binary | ||||
or | ||||
Martin von Zweigbergk
|
r24057 | # copy/rename | ||
Augie Fackler
|
r43346 | f2 in copy | ||
or | ||||
Martin von Zweigbergk
|
r24057 | # empty file creation | ||
Augie Fackler
|
r43346 | (not f1 and isempty(fctx2)) | ||
or | ||||
Martin von Zweigbergk
|
r24057 | # empty file deletion | ||
Augie Fackler
|
r43346 | (isempty(fctx1) and not f2) | ||
or | ||||
Martin von Zweigbergk
|
r24057 | # create with flags | ||
Augie Fackler
|
r43346 | (not f1 and flag2) | ||
or | ||||
Martin von Zweigbergk
|
r24057 | # change flags | ||
Augie Fackler
|
r43346 | (f1 and f2 and flag1 != flag2) | ||
): | ||||
Martin von Zweigbergk
|
r24106 | losedatafn(f2 or f1) | ||
Patrick Mezard
|
r10189 | |||
Martin von Zweigbergk
|
r41795 | path1 = pathfn(f1 or f2) | ||
path2 = pathfn(f2 or f1) | ||||
Martin von Zweigbergk
|
r23998 | header = [] | ||
Martin von Zweigbergk
|
r24022 | if opts.git: | ||
Augie Fackler
|
r43346 | header.append( | ||
Augie Fackler
|
r43347 | b'diff --git %s%s %s%s' % (aprefix, path1, bprefix, path2) | ||
Augie Fackler
|
r43346 | ) | ||
if not f1: # added | ||||
Pulkit Goyal
|
r45658 | header.append(b'new file mode %s' % _gitmode[flag2]) | ||
Augie Fackler
|
r43346 | elif not f2: # removed | ||
Pulkit Goyal
|
r45658 | header.append(b'deleted file mode %s' % _gitmode[flag1]) | ||
Martin von Zweigbergk
|
r23998 | else: # modified/copied/renamed | ||
Pulkit Goyal
|
r45658 | mode1, mode2 = _gitmode[flag1], _gitmode[flag2] | ||
Martin von Zweigbergk
|
r24000 | if mode1 != mode2: | ||
Augie Fackler
|
r43347 | header.append(b'old mode %s' % mode1) | ||
header.append(b'new mode %s' % mode2) | ||||
Martin von Zweigbergk
|
r24055 | if copyop is not None: | ||
Sean Farley
|
r30807 | if opts.showsimilarity: | ||
sim = similar.score(ctx1[path1], ctx2[path2]) * 100 | ||||
Augie Fackler
|
r43347 | header.append(b'similarity index %d%%' % sim) | ||
header.append(b'%s from %s' % (copyop, path1)) | ||||
header.append(b'%s to %s' % (copyop, path2)) | ||||
Pulkit Goyal
|
r41950 | elif revs: | ||
Martin von Zweigbergk
|
r24022 | header.append(diffline(path1, revs)) | ||
Martin von Zweigbergk
|
r23998 | |||
Jun Wu
|
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 | ||||
Augie Fackler
|
r43346 | if binary and ( | ||
not opts.git or (opts.git and opts.nobinary and not opts.index) | ||||
): | ||||
Jun Wu
|
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): | ||||
Augie Fackler
|
r43346 | content2 = b'\0' # not different | ||
Jun Wu
|
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() | ||||
Pulkit Goyal
|
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): | ||||
""" diffs two versions of a file. | ||||
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 | ||||
if binary and opts.git and not opts.nobinary: | ||||
text = mdiff.b85diff(content1, content2) | ||||
if text: | ||||
header.append( | ||||
b'index %s..%s' % (_gitindex(content1), _gitindex(content2)) | ||||
) | ||||
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' | ||||
% ( | ||||
_gitindex(content1)[0 : opts.index], | ||||
_gitindex(content2)[0 : opts.index], | ||||
_gitmode[flag], | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Pulkit Goyal
|
r45658 | |||
uheaders, hunks = mdiff.unidiff( | ||||
content1, | ||||
date1, | ||||
content2, | ||||
date2, | ||||
path1, | ||||
path2, | ||||
binary=binary, | ||||
opts=opts, | ||||
) | ||||
header.extend(uheaders) | ||||
return fctx1, fctx2, header, hunks | ||||
Vadim Gelfer
|
r2874 | |||
Augie Fackler
|
r43346 | |||
Matt Mackall
|
r14401 | def diffstatsum(stats): | ||
Steven Brown
|
r14437 | maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False | ||
Matt Mackall
|
r14401 | for f, a, r, b in stats: | ||
maxfile = max(maxfile, encoding.colwidth(f)) | ||||
Steven Brown
|
r14437 | maxtotal = max(maxtotal, a + r) | ||
Matt Mackall
|
r14401 | addtotal += a | ||
removetotal += r | ||||
binary = binary or b | ||||
Steven Brown
|
r14437 | return maxfile, maxtotal, addtotal, removetotal, binary | ||
Matt Mackall
|
r14401 | |||
Augie Fackler
|
r43346 | |||
Alexander Solovyov
|
r7547 | def diffstatdata(lines): | ||
Gregory Szorc
|
r41673 | diffre = re.compile(br'^diff .*-r [a-z0-9]+\s(.*)$') | ||
Gastón Kleiman
|
r13395 | |||
Matt Mackall
|
r14400 | results = [] | ||
Patrick Mezard
|
r15363 | filename, adds, removes, isbinary = None, 0, 0, False | ||
Matt Mackall
|
r14400 | |||
def addresult(): | ||||
if filename: | ||||
results.append((filename, adds, removes, isbinary)) | ||||
Andrew Zwicky
|
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 | ||||
Alexander Solovyov
|
r7547 | for line in lines: | ||
Augie Fackler
|
r43347 | if line.startswith(b'diff'): | ||
Matt Mackall
|
r14400 | addresult() | ||
Andrew Zwicky
|
r32321 | # starting a new file diff | ||
# set numbers to 0 and reset inheader | ||||
inheader = True | ||||
Patrick Mezard
|
r15363 | adds, removes, isbinary = 0, 0, False | ||
Augie Fackler
|
r43347 | if line.startswith(b'diff --git a/'): | ||
Matt Mackall
|
r20972 | filename = gitre.search(line).group(2) | ||
Augie Fackler
|
r43347 | elif line.startswith(b'diff -r'): | ||
timeless
|
r8761 | # format: "diff -r ... -r ... filename" | ||
Gastón Kleiman
|
r13395 | filename = diffre.search(line).group(1) | ||
Augie Fackler
|
r43347 | elif line.startswith(b'@@'): | ||
Andrew Zwicky
|
r32321 | inheader = False | ||
Augie Fackler
|
r43347 | elif line.startswith(b'+') and not inheader: | ||
Alexander Solovyov
|
r7547 | adds += 1 | ||
Augie Fackler
|
r43347 | elif line.startswith(b'-') and not inheader: | ||
Alexander Solovyov
|
r7547 | removes += 1 | ||
Augie Fackler
|
r43347 | elif line.startswith(b'GIT binary patch') or line.startswith( | ||
b'Binary file' | ||||
Augie Fackler
|
r43346 | ): | ||
Patrick Mezard
|
r15363 | isbinary = True | ||
Augie Fackler
|
r43347 | elif line.startswith(b'rename from'): | ||
Navaneeth Suresh
|
r41446 | filename = line[12:] | ||
Augie Fackler
|
r43347 | elif line.startswith(b'rename to'): | ||
filename += b' => %s' % line[10:] | ||||
Matt Mackall
|
r14400 | addresult() | ||
return results | ||||
Alexander Solovyov
|
r7547 | |||
Augie Fackler
|
r43346 | |||
Henning Schild
|
r30407 | def diffstat(lines, width=80): | ||
Alexander Solovyov
|
r7547 | output = [] | ||
Matt Mackall
|
r14402 | stats = diffstatdata(lines) | ||
Steven Brown
|
r14437 | maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats) | ||
Alexander Solovyov
|
r7547 | |||
countwidth = len(str(maxtotal)) | ||||
Brodie Rao
|
r9642 | if hasbinary and countwidth < 3: | ||
countwidth = 3 | ||||
Brodie Rao
|
r9330 | graphwidth = width - countwidth - maxname - 6 | ||
Alexander Solovyov
|
r7547 | if graphwidth < 10: | ||
graphwidth = 10 | ||||
Brodie Rao
|
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))) | ||||
Alexander Solovyov
|
r7547 | |||
Matt Mackall
|
r14402 | for filename, adds, removes, isbinary in stats: | ||
Patrick Mezard
|
r15363 | if isbinary: | ||
Augie Fackler
|
r43347 | count = b'Bin' | ||
Brodie Rao
|
r9642 | else: | ||
Augie Fackler
|
r43347 | count = b'%d' % (adds + removes) | ||
pluses = b'+' * scale(adds) | ||||
minuses = b'-' * scale(removes) | ||||
Augie Fackler
|
r43346 | output.append( | ||
Augie Fackler
|
r43347 | b' %s%s | %*s %s%s\n' | ||
Augie Fackler
|
r43346 | % ( | ||
filename, | ||||
Augie Fackler
|
r43347 | b' ' * (maxname - encoding.colwidth(filename)), | ||
Augie Fackler
|
r43346 | countwidth, | ||
count, | ||||
pluses, | ||||
minuses, | ||||
) | ||||
) | ||||
Alexander Solovyov
|
r7547 | |||
if stats: | ||||
Augie Fackler
|
r43346 | output.append( | ||
Martin von Zweigbergk
|
r43387 | _(b' %d files changed, %d insertions(+), %d deletions(-)\n') | ||
Augie Fackler
|
r43346 | % (len(stats), totaladds, totalremoves) | ||
) | ||||
Alexander Solovyov
|
r7547 | |||
Augie Fackler
|
r43347 | return b''.join(output) | ||
Brodie Rao
|
r10818 | |||
Augie Fackler
|
r43346 | |||
Brodie Rao
|
r10818 | def diffstatui(*args, **kw): | ||
'''like diffstat(), but yields 2-tuples of (output, label) for | ||||
ui.write() | ||||
''' | ||||
for line in diffstat(*args, **kw).splitlines(): | ||||
Augie Fackler
|
r43347 | if line and line[-1] in b'+-': | ||
name, graph = line.rsplit(b' ', 1) | ||||
yield (name + b' ', b'') | ||||
Pulkit Goyal
|
r33096 | m = re.search(br'\++', graph) | ||
Brodie Rao
|
r10818 | if m: | ||
Augie Fackler
|
r43347 | yield (m.group(0), b'diffstat.inserted') | ||
Pulkit Goyal
|
r33096 | m = re.search(br'-+', graph) | ||
Brodie Rao
|
r10818 | if m: | ||
Augie Fackler
|
r43347 | yield (m.group(0), b'diffstat.deleted') | ||
Brodie Rao
|
r10818 | else: | ||
Augie Fackler
|
r43347 | yield (line, b'') | ||
yield (b'\n', b'') | ||||