patch.py
1968 lines
| 66.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / patch.py
|
r2861 | # patch.py - patch file parsing routines | ||
# | ||||
|
r2865 | # Copyright 2006 Brendan Cully <brendan@kublai.com> | ||
|
r4897 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> | ||
|
r2865 | # | ||
|
r8225 | # This software may be used and distributed according to the terms of the | ||
|
r10263 | # GNU General Public License version 2 or any later version. | ||
|
r2861 | |||
|
r19789 | import cStringIO, email, os, errno, re, posixpath | ||
|
r14452 | import tempfile, zlib, shutil | ||
|
r19810 | # On python2.4 you have to import these by name or they fail to | ||
# load. This was not a problem on Python 2.7. | ||||
import email.Generator | ||||
import email.Parser | ||||
|
r10965 | |||
|
r3891 | from i18n import _ | ||
|
r19875 | from node import hex, short | ||
|
r14611 | import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error | ||
|
r2866 | |||
|
r7199 | gitre = re.compile('diff --git a/(.*) b/(.*)') | ||
|
r22460 | tabsplitter = re.compile(r'(\t+|[^\t]+)') | ||
|
r7199 | |||
|
r4897 | class PatchError(Exception): | ||
pass | ||||
|
r2933 | |||
# public functions | ||||
|
r10384 | def split(stream): | ||
'''return an iterator of individual patches from a stream''' | ||||
def isheader(line, inheader): | ||||
if inheader and line[0] in (' ', '\t'): | ||||
# continuation | ||||
return True | ||||
|
r10883 | if line[0] in (' ', '-', '+'): | ||
# diff line - don't check for header pattern in there | ||||
return False | ||||
|
r10384 | l = line.split(': ', 1) | ||
return len(l) == 2 and ' ' not in l[0] | ||||
def chunk(lines): | ||||
return cStringIO.StringIO(''.join(lines)) | ||||
def hgsplit(stream, cur): | ||||
inheader = True | ||||
for line in stream: | ||||
if not line.strip(): | ||||
inheader = False | ||||
if not inheader and line.startswith('# HG changeset patch'): | ||||
yield chunk(cur) | ||||
cur = [] | ||||
inheader = True | ||||
cur.append(line) | ||||
if cur: | ||||
yield chunk(cur) | ||||
def mboxsplit(stream, cur): | ||||
for line in stream: | ||||
if line.startswith('From '): | ||||
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): | ||||
fp = cStringIO.StringIO() | ||||
g = email.Generator.Generator(fp, mangle_from_=False) | ||||
g.flatten(m) | ||||
fp.seek(0) | ||||
return fp | ||||
for line in stream: | ||||
cur.append(line) | ||||
c = chunk(cur) | ||||
m = email.Parser.Parser().parse(c) | ||||
if not m.is_multipart(): | ||||
yield msgfp(m) | ||||
else: | ||||
ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') | ||||
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 | ||||
inheader = False | ||||
cur = [] | ||||
mimeheaders = ['content-type'] | ||||
|
r14966 | if not util.safehasattr(stream, 'next'): | ||
|
r10384 | # http responses, for example, have readline but not next | ||
stream = fiter(stream) | ||||
for line in stream: | ||||
cur.append(line) | ||||
if line.startswith('# HG changeset patch'): | ||||
return hgsplit(stream, cur) | ||||
elif line.startswith('From '): | ||||
return mboxsplit(stream, cur) | ||||
elif isheader(line, inheader): | ||||
inheader = True | ||||
if line.split(':', 1)[0].lower() in mimeheaders: | ||||
# let email parser handle this | ||||
return mimesplit(stream, cur) | ||||
|
r10501 | elif line.startswith('--- ') and inheader: | ||
# No evil headers seen by diff start, split by hand | ||||
|
r10384 | return headersplit(stream, cur) | ||
# Not enough info, keep reading | ||||
# if we are here, we have a very plain patch | ||||
return remainder(cur) | ||||
|
r2866 | def extract(ui, fileobj): | ||
'''extract patch from data read from fileobj. | ||||
|
r4263 | patch can be a normal patch or contained in an email message. | ||
|
r2866 | |||
|
r11645 | return tuple (filename, message, user, date, branch, node, p1, p2). | ||
|
r4263 | Any item in the returned tuple can be None. If filename is None, | ||
fileobj did not contain a patch. Caller must unlink filename when done.''' | ||||
|
r2866 | |||
# attempt to detect the start of a patch | ||||
# (this heuristic is borrowed from quilt) | ||||
|
r7736 | diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |' | ||
r'retrieving revision [0-9]+(\.[0-9]+)*$|' | ||||
|
r10736 | r'---[ \t].*?^\+\+\+[ \t]|' | ||
r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL) | ||||
|
r2866 | |||
fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') | ||||
tmpfp = os.fdopen(fd, 'w') | ||||
try: | ||||
msg = email.Parser.Parser().parse(fileobj) | ||||
|
r4777 | subject = msg['Subject'] | ||
|
r2866 | user = msg['From'] | ||
|
r9573 | if not subject and not user: | ||
# Not an email, restore parsed headers if any | ||||
subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n' | ||||
|
r2866 | # should try to parse msg['Date'] | ||
date = None | ||||
|
r4263 | nodeid = None | ||
|
r4443 | branch = None | ||
|
r4263 | parents = [] | ||
|
r2866 | |||
|
r4777 | if subject: | ||
if subject.startswith('[PATCH'): | ||||
pend = subject.find(']') | ||||
|
r4208 | if pend >= 0: | ||
|
r10282 | subject = subject[pend + 1:].lstrip() | ||
|
r15158 | subject = re.sub(r'\n[ \t]+', ' ', subject) | ||
|
r4777 | ui.debug('Subject: %s\n' % subject) | ||
|
r2866 | if user: | ||
ui.debug('From: %s\n' % user) | ||||
diffs_seen = 0 | ||||
ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') | ||||
|
r4900 | message = '' | ||
|
r2866 | for part in msg.walk(): | ||
content_type = part.get_content_type() | ||||
ui.debug('Content-Type: %s\n' % content_type) | ||||
if content_type not in ok_types: | ||||
continue | ||||
payload = part.get_payload(decode=True) | ||||
m = diffre.search(payload) | ||||
if m: | ||||
|
r4220 | hgpatch = False | ||
|
r12645 | hgpatchheader = False | ||
|
r4220 | ignoretext = False | ||
|
r9467 | ui.debug('found patch at byte %d\n' % m.start(0)) | ||
|
r2866 | diffs_seen += 1 | ||
cfp = cStringIO.StringIO() | ||||
for line in payload[:m.start(0)].splitlines(): | ||||
|
r12728 | if line.startswith('# HG changeset patch') and not hgpatch: | ||
|
r9467 | ui.debug('patch generated by hg export\n') | ||
|
r12728 | hgpatch = True | ||
|
r12645 | hgpatchheader = True | ||
|
r2866 | # drop earlier commit message content | ||
cfp.seek(0) | ||||
cfp.truncate() | ||||
|
r4778 | subject = None | ||
|
r12645 | elif hgpatchheader: | ||
|
r2866 | if line.startswith('# User '): | ||
user = line[7:] | ||||
ui.debug('From: %s\n' % user) | ||||
elif line.startswith("# Date "): | ||||
date = line[7:] | ||||
|
r4443 | elif line.startswith("# Branch "): | ||
branch = line[9:] | ||||
|
r4263 | elif line.startswith("# Node ID "): | ||
nodeid = line[10:] | ||||
elif line.startswith("# Parent "): | ||||
|
r16475 | parents.append(line[9:].lstrip()) | ||
|
r12645 | elif not line.startswith("# "): | ||
hgpatchheader = False | ||||
|
r19513 | elif line == '---': | ||
|
r4220 | ignoretext = True | ||
|
r12645 | if not hgpatchheader and not ignoretext: | ||
|
r2866 | cfp.write(line) | ||
cfp.write('\n') | ||||
message = cfp.getvalue() | ||||
if tmpfp: | ||||
tmpfp.write(payload) | ||||
if not payload.endswith('\n'): | ||||
tmpfp.write('\n') | ||||
elif not diffs_seen and message and content_type == 'text/plain': | ||||
message += '\n' + payload | ||||
|
r16705 | except: # re-raises | ||
|
r2866 | tmpfp.close() | ||
os.unlink(tmpname) | ||||
raise | ||||
|
r4777 | if subject and not message.startswith(subject): | ||
message = '%s\n%s' % (subject, message) | ||||
|
r2866 | tmpfp.close() | ||
if not diffs_seen: | ||||
os.unlink(tmpname) | ||||
|
r4443 | return None, message, user, date, branch, None, None, None | ||
|
r4263 | p1 = parents and parents.pop(0) or None | ||
p2 = parents and parents.pop(0) or None | ||||
|
r4443 | return tmpname, message, user, date, branch, nodeid, p1, p2 | ||
|
r2861 | |||
|
r8778 | class patchmeta(object): | ||
|
r7148 | """Patched file metadata | ||
'op' is the performed operation within ADD, DELETE, RENAME, MODIFY | ||||
or COPY. 'path' is patched file path. 'oldpath' is set to the | ||||
|
r7149 | origin file when 'op' is either COPY or RENAME, None otherwise. If | ||
file mode is changed, 'mode' is a tuple (islink, isexec) where | ||||
'islink' is True if the file is a symlink and 'isexec' is True if | ||||
the file is executable. Otherwise, 'mode' is None. | ||||
|
r7148 | """ | ||
def __init__(self, path): | ||||
self.path = path | ||||
self.oldpath = None | ||||
self.mode = None | ||||
self.op = 'MODIFY' | ||||
self.binary = False | ||||
|
r7149 | def setmode(self, mode): | ||
islink = mode & 020000 | ||||
isexec = mode & 0100 | ||||
self.mode = (islink, isexec) | ||||
|
r14566 | def copy(self): | ||
other = patchmeta(self.path) | ||||
other.oldpath = self.oldpath | ||||
other.mode = self.mode | ||||
other.op = self.op | ||||
other.binary = self.binary | ||||
return other | ||||
|
r16506 | def _ispatchinga(self, afile): | ||
if afile == '/dev/null': | ||||
return self.op == 'ADD' | ||||
return afile == 'a/' + (self.oldpath or self.path) | ||||
def _ispatchingb(self, bfile): | ||||
if bfile == '/dev/null': | ||||
return self.op == 'DELETE' | ||||
return bfile == 'b/' + self.path | ||||
def ispatching(self, afile, bfile): | ||||
return self._ispatchinga(afile) and self._ispatchingb(bfile) | ||||
|
r11018 | def __repr__(self): | ||
return "<patchmeta %s %r>" % (self.op, self.path) | ||||
|
r7152 | def readgitpatch(lr): | ||
|
r2861 | """extract git-style metadata about patches from <patchname>""" | ||
|
r3223 | |||
|
r2861 | # Filter patch for git information | ||
gp = None | ||||
gitpatches = [] | ||||
|
r7152 | for line in lr: | ||
|
r9243 | line = line.rstrip(' \r\n') | ||
|
r18830 | if line.startswith('diff --git a/'): | ||
|
r2861 | m = gitre.match(line) | ||
if m: | ||||
if gp: | ||||
gitpatches.append(gp) | ||||
|
r9392 | dst = m.group(2) | ||
|
r7148 | gp = patchmeta(dst) | ||
|
r2861 | elif gp: | ||
if line.startswith('--- '): | ||||
gitpatches.append(gp) | ||||
gp = None | ||||
continue | ||||
if line.startswith('rename from '): | ||||
gp.op = 'RENAME' | ||||
|
r9243 | gp.oldpath = line[12:] | ||
|
r2861 | elif line.startswith('rename to '): | ||
|
r9243 | gp.path = line[10:] | ||
|
r2861 | elif line.startswith('copy from '): | ||
gp.op = 'COPY' | ||||
|
r9243 | gp.oldpath = line[10:] | ||
|
r2861 | elif line.startswith('copy to '): | ||
|
r9243 | gp.path = line[8:] | ||
|
r2861 | elif line.startswith('deleted file'): | ||
gp.op = 'DELETE' | ||||
elif line.startswith('new file mode '): | ||||
gp.op = 'ADD' | ||||
|
r9243 | gp.setmode(int(line[-6:], 8)) | ||
|
r2861 | elif line.startswith('new mode '): | ||
|
r9243 | gp.setmode(int(line[-6:], 8)) | ||
|
r3367 | elif line.startswith('GIT binary patch'): | ||
gp.binary = True | ||||
|
r2861 | if gp: | ||
gitpatches.append(gp) | ||||
|
r12669 | return gitpatches | ||
|
r2861 | |||
|
r8891 | class linereader(object): | ||
|
r8810 | # simple class to allow pushing lines back into the input stream | ||
|
r14418 | def __init__(self, fp): | ||
|
r8810 | self.fp = fp | ||
self.buf = [] | ||||
def push(self, line): | ||||
if line is not None: | ||||
self.buf.append(line) | ||||
def readline(self): | ||||
if self.buf: | ||||
l = self.buf[0] | ||||
del self.buf[0] | ||||
return l | ||||
|
r14418 | return self.fp.readline() | ||
|
r8810 | |||
def __iter__(self): | ||||
|
r14494 | while True: | ||
|
r8810 | l = self.readline() | ||
if not l: | ||||
break | ||||
yield l | ||||
|
r14348 | class abstractbackend(object): | ||
def __init__(self, ui): | ||||
|
r4897 | self.ui = ui | ||
|
r14348 | |||
|
r14391 | def getfile(self, fname): | ||
"""Return target file data and flags as a (data, (islink, | ||||
|
r22296 | isexec)) tuple. Data is None if file is missing/deleted. | ||
|
r14348 | """ | ||
raise NotImplementedError | ||||
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
|
r14391 | """Write data to target file fname and set its mode. mode is a | ||
(islink, isexec) tuple. If data is None, the file content should | ||||
|
r14452 | be left unchanged. If the file is modified after being copied, | ||
copysource is set to the original file name. | ||||
|
r14367 | """ | ||
|
r14348 | raise NotImplementedError | ||
|
r5652 | |||
|
r14348 | def unlink(self, fname): | ||
"""Unlink target file.""" | ||||
raise NotImplementedError | ||||
def writerej(self, fname, failed, total, lines): | ||||
"""Write rejected lines for fname. total is the number of hunks | ||||
which failed to apply and total the total number of hunks for this | ||||
files. | ||||
""" | ||||
pass | ||||
|
r14351 | def exists(self, fname): | ||
raise NotImplementedError | ||||
|
r14348 | class fsbackend(abstractbackend): | ||
|
r14350 | def __init__(self, ui, basedir): | ||
|
r14348 | super(fsbackend, self).__init__(ui) | ||
|
r14350 | self.opener = scmutil.opener(basedir) | ||
|
r4897 | |||
|
r14366 | def _join(self, f): | ||
return os.path.join(self.opener.base, f) | ||||
|
r14391 | def getfile(self, fname): | ||
|
r21717 | if self.opener.islink(fname): | ||
return (self.opener.readlink(fname), (True, False)) | ||||
|
r14531 | isexec = False | ||
|
r7392 | try: | ||
|
r21717 | isexec = self.opener.lstat(fname).st_mode & 0100 != 0 | ||
|
r14391 | except OSError, e: | ||
if e.errno != errno.ENOENT: | ||||
raise | ||||
|
r22296 | try: | ||
return (self.opener.read(fname), (False, isexec)) | ||||
except IOError, e: | ||||
if e.errno != errno.ENOENT: | ||||
raise | ||||
return None, None | ||||
|
r7392 | |||
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
|
r14391 | islink, isexec = mode | ||
if data is None: | ||||
|
r21717 | self.opener.setflags(fname, islink, isexec) | ||
|
r14390 | return | ||
|
r14391 | if islink: | ||
self.opener.symlink(data, fname) | ||||
|
r14367 | else: | ||
|
r14391 | self.opener.write(fname, data) | ||
|
r14367 | if isexec: | ||
|
r21717 | self.opener.setflags(fname, False, True) | ||
|
r7392 | |||
def unlink(self, fname): | ||||
|
r21717 | self.opener.unlinkpath(fname, ignoremissing=True) | ||
|
r7392 | |||
|
r14348 | def writerej(self, fname, failed, total, lines): | ||
fname = fname + ".rej" | ||||
self.ui.warn( | ||||
_("%d out of %d hunks FAILED -- saving rejects to file %s\n") % | ||||
(failed, total, fname)) | ||||
fp = self.opener(fname, 'w') | ||||
fp.writelines(lines) | ||||
fp.close() | ||||
|
r14351 | def exists(self, fname): | ||
|
r21717 | return self.opener.lexists(fname) | ||
|
r14351 | |||
|
r14370 | class workingbackend(fsbackend): | ||
def __init__(self, ui, repo, similarity): | ||||
super(workingbackend, self).__init__(ui, repo.root) | ||||
self.repo = repo | ||||
self.similarity = similarity | ||||
self.removed = set() | ||||
self.changed = set() | ||||
self.copied = [] | ||||
|
r14453 | def _checkknown(self, fname): | ||
if self.repo.dirstate[fname] == '?' and self.exists(fname): | ||||
raise PatchError(_('cannot patch %s: file is not tracked') % fname) | ||||
|
r14452 | def setfile(self, fname, data, mode, copysource): | ||
|
r14453 | self._checkknown(fname) | ||
|
r14452 | super(workingbackend, self).setfile(fname, data, mode, copysource) | ||
if copysource is not None: | ||||
self.copied.append((copysource, fname)) | ||||
|
r14370 | self.changed.add(fname) | ||
def unlink(self, fname): | ||||
|
r14453 | self._checkknown(fname) | ||
|
r14370 | super(workingbackend, self).unlink(fname) | ||
self.removed.add(fname) | ||||
self.changed.add(fname) | ||||
def close(self): | ||||
wctx = self.repo[None] | ||||
|
r19155 | changed = set(self.changed) | ||
|
r14370 | for src, dst in self.copied: | ||
scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst) | ||||
|
r16112 | if self.removed: | ||
|
r14435 | wctx.forget(sorted(self.removed)) | ||
|
r16112 | for f in self.removed: | ||
if f not in self.repo.dirstate: | ||||
# File was deleted and no longer belongs to the | ||||
# dirstate, it was probably marked added then | ||||
# deleted, and should not be considered by | ||||
|
r19155 | # marktouched(). | ||
changed.discard(f) | ||||
if changed: | ||||
scmutil.marktouched(self.repo, changed, self.similarity) | ||||
|
r14370 | return sorted(self.changed) | ||
|
r14452 | class filestore(object): | ||
|
r14658 | def __init__(self, maxsize=None): | ||
|
r14452 | self.opener = None | ||
self.files = {} | ||||
self.created = 0 | ||||
|
r14658 | self.maxsize = maxsize | ||
if self.maxsize is None: | ||||
self.maxsize = 4*(2**20) | ||||
self.size = 0 | ||||
self.data = {} | ||||
|
r14452 | |||
|
r14609 | def setfile(self, fname, data, mode, copied=None): | ||
|
r14658 | if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize: | ||
self.data[fname] = (data, mode, copied) | ||||
self.size += len(data) | ||||
else: | ||||
if self.opener is None: | ||||
root = tempfile.mkdtemp(prefix='hg-patch-') | ||||
self.opener = scmutil.opener(root) | ||||
# Avoid filename issues with these simple names | ||||
fn = str(self.created) | ||||
self.opener.write(fn, data) | ||||
self.created += 1 | ||||
self.files[fname] = (fn, mode, copied) | ||||
|
r14452 | |||
def getfile(self, fname): | ||||
|
r14658 | if fname in self.data: | ||
return self.data[fname] | ||||
if not self.opener or fname not in self.files: | ||||
|
r22296 | return None, None, None | ||
|
r14609 | fn, mode, copied = self.files[fname] | ||
return self.opener.read(fn), mode, copied | ||||
|
r14452 | |||
def close(self): | ||||
if self.opener: | ||||
shutil.rmtree(self.opener.base) | ||||
|
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: | ||||
raise PatchError(_('cannot patch %s: file is not tracked') % fname) | ||||
def getfile(self, fname): | ||||
try: | ||||
fctx = self.ctx[fname] | ||||
except error.LookupError: | ||||
|
r22296 | return None, None | ||
|
r14611 | flags = fctx.flags() | ||
return fctx.data(), ('l' in flags, 'x' in flags) | ||||
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 | ||||
|
r14348 | # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 | ||
|
r15510 | unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@') | ||
contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)') | ||||
|
r14348 | eolmodes = ['strict', 'crlf', 'lf', 'auto'] | ||
class patchfile(object): | ||||
|
r14566 | def __init__(self, ui, gp, backend, store, eolmode='strict'): | ||
self.fname = gp.path | ||||
|
r14348 | self.eolmode = eolmode | ||
self.eol = None | ||||
self.backend = backend | ||||
self.ui = ui | ||||
self.lines = [] | ||||
self.exists = False | ||||
|
r14452 | self.missing = True | ||
|
r14566 | self.mode = gp.mode | ||
self.copysource = gp.oldpath | ||||
self.create = gp.op in ('ADD', 'COPY', 'RENAME') | ||||
self.remove = gp.op == 'DELETE' | ||||
|
r22296 | if self.copysource is None: | ||
data, mode = backend.getfile(self.fname) | ||||
else: | ||||
data, mode = store.getfile(self.copysource)[:2] | ||||
if data is not None: | ||||
self.exists = self.copysource is None or backend.exists(self.fname) | ||||
|
r14452 | self.missing = False | ||
if data: | ||||
|
r14832 | self.lines = mdiff.splitnewlines(data) | ||
|
r14452 | if self.mode is None: | ||
self.mode = mode | ||||
if self.lines: | ||||
# Normalize line endings | ||||
if self.lines[0].endswith('\r\n'): | ||||
self.eol = '\r\n' | ||||
elif self.lines[0].endswith('\n'): | ||||
self.eol = '\n' | ||||
if eolmode != 'strict': | ||||
nlines = [] | ||||
for l in self.lines: | ||||
if l.endswith('\r\n'): | ||||
l = l[:-2] + '\n' | ||||
nlines.append(l) | ||||
self.lines = nlines | ||||
|
r22296 | else: | ||
|
r14566 | if self.create: | ||
|
r14452 | self.missing = False | ||
if self.mode is None: | ||||
self.mode = (False, False) | ||||
if self.missing: | ||||
|
r17299 | self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) | ||
|
r14348 | |||
self.hash = {} | ||||
self.dirty = 0 | ||||
self.offset = 0 | ||||
self.skew = 0 | ||||
self.rej = [] | ||||
self.fileprinted = False | ||||
self.printfile(False) | ||||
self.hunks = 0 | ||||
|
r14367 | def writelines(self, fname, lines, mode): | ||
|
r14348 | if self.eolmode == 'auto': | ||
eol = self.eol | ||||
elif self.eolmode == 'crlf': | ||||
eol = '\r\n' | ||||
else: | ||||
eol = '\n' | ||||
if self.eolmode != 'strict' and eol and eol != '\n': | ||||
rawlines = [] | ||||
for l in lines: | ||||
if l and l[-1] == '\n': | ||||
l = l[:-1] + eol | ||||
rawlines.append(l) | ||||
lines = rawlines | ||||
|
r14452 | self.backend.setfile(fname, ''.join(lines), mode, self.copysource) | ||
|
r14348 | |||
|
r4897 | def printfile(self, warn): | ||
if self.fileprinted: | ||||
return | ||||
if warn or self.ui.verbose: | ||||
self.fileprinted = True | ||||
|
r4898 | s = _("patching file %s\n") % self.fname | ||
|
r4897 | if warn: | ||
self.ui.warn(s) | ||||
else: | ||||
self.ui.note(s) | ||||
def findlines(self, l, linenum): | ||||
# looks through the hash and finds candidate lines. The | ||||
# result is a list of line numbers sorted based on distance | ||||
# from linenum | ||||
|
r5143 | |||
|
r9681 | cand = self.hash.get(l, []) | ||
|
r4897 | if len(cand) > 1: | ||
# resort our list of potentials forward then back. | ||||
|
r9032 | cand.sort(key=lambda x: abs(x - linenum)) | ||
|
r4897 | return cand | ||
def write_rej(self): | ||||
# our rejects are a little different from patch(1). This always | ||||
# creates rejects in the same form as the original patch. A file | ||||
# header is inserted so that you can run the reject through patch again | ||||
# without having to type the filename. | ||||
if not self.rej: | ||||
return | ||||
|
r14349 | base = os.path.basename(self.fname) | ||
lines = ["--- %s\n+++ %s\n" % (base, base)] | ||||
for x in self.rej: | ||||
for l in x.hunk: | ||||
lines.append(l) | ||||
if l[-1] != '\n': | ||||
lines.append("\n\ No newline at end of file\n") | ||||
self.backend.writerej(self.fname, len(self.rej), self.hunks, lines) | ||||
|
r4897 | |||
|
r9393 | def apply(self, h): | ||
|
r4897 | if not h.complete(): | ||
|
r4898 | raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % | ||
|
r4897 | (h.number, h.desc, len(h.a), h.lena, len(h.b), | ||
h.lenb)) | ||||
self.hunks += 1 | ||||
|
r5652 | if self.missing: | ||
self.rej.append(h) | ||||
return -1 | ||||
|
r14451 | if self.exists and self.create: | ||
|
r14452 | if self.copysource: | ||
self.ui.warn(_("cannot create %s: destination already " | ||||
|
r20869 | "exists\n") % self.fname) | ||
|
r14452 | else: | ||
self.ui.warn(_("file %s already exists\n") % self.fname) | ||||
|
r4897 | self.rej.append(h) | ||
return -1 | ||||
|
r9585 | if isinstance(h, binhunk): | ||
|
r14451 | if self.remove: | ||
|
r14348 | self.backend.unlink(self.fname) | ||
|
r4897 | else: | ||
|
r20137 | l = h.new(self.lines) | ||
self.lines[:] = l | ||||
self.offset += len(l) | ||||
|
r14217 | self.dirty = True | ||
|
r4897 | return 0 | ||
|
r10127 | horig = h | ||
|
r10128 | if (self.eolmode in ('crlf', 'lf') | ||
or self.eolmode == 'auto' and self.eol): | ||||
# If new eols are going to be normalized, then normalize | ||||
# hunk data before patching. Otherwise, preserve input | ||||
# line-endings. | ||||
|
r10127 | h = h.getnormalized() | ||
|
r4897 | # fast case first, no offsets, no fuzz | ||
|
r16122 | old, oldstart, new, newstart = h.fuzzit(0, False) | ||
oldstart += self.offset | ||||
orig_start = oldstart | ||||
|
r10135 | # if there's skew we want to emit the "(offset %d lines)" even | ||
# when the hunk cleanly applies at start + skew, so skip the | ||||
# fast case code | ||||
|
r16122 | if (self.skew == 0 and | ||
diffhelpers.testhunk(old, self.lines, oldstart) == 0): | ||||
|
r14451 | if self.remove: | ||
|
r14348 | self.backend.unlink(self.fname) | ||
|
r4897 | else: | ||
|
r16122 | self.lines[oldstart:oldstart + len(old)] = new | ||
self.offset += len(new) - len(old) | ||||
|
r14217 | self.dirty = True | ||
|
r4897 | return 0 | ||
|
r13700 | # ok, we couldn't match the hunk. Lets look for offsets and fuzz it | ||
self.hash = {} | ||||
for x, s in enumerate(self.lines): | ||||
self.hash.setdefault(s, []).append(x) | ||||
|
r4897 | |||
for fuzzlen in xrange(3): | ||||
|
r10282 | for toponly in [True, False]: | ||
|
r16122 | old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly) | ||
|
r16123 | oldstart = oldstart + self.offset + self.skew | ||
oldstart = min(oldstart, len(self.lines)) | ||||
if old: | ||||
cand = self.findlines(old[0][1:], oldstart) | ||||
else: | ||||
# Only adding lines with no or fuzzed context, just | ||||
# take the skew in account | ||||
cand = [oldstart] | ||||
|
r4897 | |||
for l in cand: | ||||
|
r16123 | if not old or diffhelpers.testhunk(old, self.lines, l) == 0: | ||
|
r16121 | self.lines[l : l + len(old)] = new | ||
self.offset += len(new) - len(old) | ||||
|
r10135 | self.skew = l - orig_start | ||
|
r14217 | self.dirty = True | ||
|
r10518 | offset = l - orig_start - fuzzlen | ||
|
r4897 | if fuzzlen: | ||
|
r10518 | msg = _("Hunk #%d succeeded at %d " | ||
"with fuzz %d " | ||||
"(offset %d lines).\n") | ||||
|
r4897 | self.printfile(True) | ||
|
r10518 | self.ui.warn(msg % | ||
(h.number, l + 1, fuzzlen, offset)) | ||||
|
r4897 | else: | ||
|
r10518 | msg = _("Hunk #%d succeeded at %d " | ||
|
r8090 | "(offset %d lines).\n") | ||
|
r10518 | self.ui.note(msg % (h.number, l + 1, offset)) | ||
|
r4897 | return fuzzlen | ||
self.printfile(True) | ||||
|
r4898 | self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) | ||
|
r10127 | self.rej.append(horig) | ||
|
r4897 | return -1 | ||
|
r13701 | def close(self): | ||
if self.dirty: | ||||
|
r14367 | self.writelines(self.fname, self.lines, self.mode) | ||
|
r13701 | self.write_rej() | ||
return len(self.rej) | ||||
|
r8778 | class hunk(object): | ||
|
r14451 | def __init__(self, desc, num, lr, context): | ||
|
r4897 | self.number = num | ||
self.desc = desc | ||||
|
r10282 | self.hunk = [desc] | ||
|
r4897 | self.a = [] | ||
self.b = [] | ||||
|
r9682 | self.starta = self.lena = None | ||
self.startb = self.lenb = None | ||||
|
r10127 | if lr is not None: | ||
if context: | ||||
self.read_context_hunk(lr) | ||||
else: | ||||
self.read_unified_hunk(lr) | ||||
|
r4897 | |||
|
r10127 | def getnormalized(self): | ||
"""Return a copy with line endings normalized to LF.""" | ||||
def normalize(lines): | ||||
nlines = [] | ||||
for line in lines: | ||||
if line.endswith('\r\n'): | ||||
line = line[:-2] + '\n' | ||||
nlines.append(line) | ||||
return nlines | ||||
# Dummy object, it is rebuilt manually | ||||
|
r14451 | nh = hunk(self.desc, self.number, None, None) | ||
|
r10127 | nh.number = self.number | ||
nh.desc = self.desc | ||||
|
r10524 | nh.hunk = self.hunk | ||
|
r10127 | nh.a = normalize(self.a) | ||
nh.b = normalize(self.b) | ||||
nh.starta = self.starta | ||||
nh.startb = self.startb | ||||
nh.lena = self.lena | ||||
nh.lenb = self.lenb | ||||
return nh | ||||
|
r4897 | def read_unified_hunk(self, lr): | ||
m = unidesc.match(self.desc) | ||||
if not m: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d") % self.number) | ||
|
r15510 | self.starta, self.lena, self.startb, self.lenb = m.groups() | ||
|
r8527 | if self.lena is None: | ||
|
r4897 | self.lena = 1 | ||
else: | ||||
self.lena = int(self.lena) | ||||
|
r8527 | if self.lenb is None: | ||
|
r4897 | self.lenb = 1 | ||
else: | ||||
self.lenb = int(self.lenb) | ||||
self.starta = int(self.starta) | ||||
self.startb = int(self.startb) | ||||
|
r16683 | diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, | ||
self.b) | ||||
|
r4897 | # if we hit eof before finishing out the hunk, the last line will | ||
# be zero length. Lets try to fix it up. | ||||
while len(self.hunk[-1]) == 0: | ||||
|
r6948 | del self.hunk[-1] | ||
del self.a[-1] | ||||
del self.b[-1] | ||||
self.lena -= 1 | ||||
self.lenb -= 1 | ||||
|
r13699 | self._fixnewline(lr) | ||
|
r4897 | |||
def read_context_hunk(self, lr): | ||||
self.desc = lr.readline() | ||||
m = contextdesc.match(self.desc) | ||||
if not m: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d") % self.number) | ||
|
r15510 | self.starta, aend = m.groups() | ||
|
r4897 | self.starta = int(self.starta) | ||
|
r8527 | if aend is None: | ||
|
r4897 | aend = self.starta | ||
self.lena = int(aend) - self.starta | ||||
if self.starta: | ||||
self.lena += 1 | ||||
for x in xrange(self.lena): | ||||
l = lr.readline() | ||||
if l.startswith('---'): | ||||
|
r12825 | # lines addition, old block is empty | ||
|
r4897 | lr.push(l) | ||
break | ||||
s = l[2:] | ||||
if l.startswith('- ') or l.startswith('! '): | ||||
u = '-' + s | ||||
elif l.startswith(' '): | ||||
u = ' ' + s | ||||
else: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d old text line %d") % | ||
(self.number, x)) | ||||
|
r4897 | self.a.append(u) | ||
self.hunk.append(u) | ||||
l = lr.readline() | ||||
if l.startswith('\ '): | ||||
s = self.a[-1][:-1] | ||||
self.a[-1] = s | ||||
self.hunk[-1] = s | ||||
l = lr.readline() | ||||
m = contextdesc.match(l) | ||||
if not m: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d") % self.number) | ||
|
r15510 | self.startb, bend = m.groups() | ||
|
r4897 | self.startb = int(self.startb) | ||
|
r8527 | if bend is None: | ||
|
r4897 | bend = self.startb | ||
self.lenb = int(bend) - self.startb | ||||
if self.startb: | ||||
self.lenb += 1 | ||||
hunki = 1 | ||||
for x in xrange(self.lenb): | ||||
l = lr.readline() | ||||
if l.startswith('\ '): | ||||
|
r12825 | # XXX: the only way to hit this is with an invalid line range. | ||
# The no-eol marker is not counted in the line range, but I | ||||
# guess there are diff(1) out there which behave differently. | ||||
|
r4897 | s = self.b[-1][:-1] | ||
self.b[-1] = s | ||||
|
r10282 | self.hunk[hunki - 1] = s | ||
|
r4897 | continue | ||
if not l: | ||||
|
r12825 | # line deletions, new block is empty and we hit EOF | ||
|
r4897 | lr.push(l) | ||
break | ||||
s = l[2:] | ||||
if l.startswith('+ ') or l.startswith('! '): | ||||
u = '+' + s | ||||
elif l.startswith(' '): | ||||
u = ' ' + s | ||||
elif len(self.b) == 0: | ||||
|
r12825 | # line deletions, new block is empty | ||
|
r4897 | lr.push(l) | ||
break | ||||
else: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d old text line %d") % | ||
(self.number, x)) | ||||
|
r4897 | self.b.append(s) | ||
while True: | ||||
if hunki >= len(self.hunk): | ||||
h = "" | ||||
else: | ||||
h = self.hunk[hunki] | ||||
hunki += 1 | ||||
if h == u: | ||||
break | ||||
elif h.startswith('-'): | ||||
continue | ||||
else: | ||||
|
r10282 | self.hunk.insert(hunki - 1, u) | ||
|
r4897 | break | ||
if not self.a: | ||||
# this happens when lines were only added to the hunk | ||||
for x in self.hunk: | ||||
if x.startswith('-') or x.startswith(' '): | ||||
self.a.append(x) | ||||
if not self.b: | ||||
# this happens when lines were only deleted from the hunk | ||||
for x in self.hunk: | ||||
if x.startswith('+') or x.startswith(' '): | ||||
self.b.append(x[1:]) | ||||
# @@ -start,len +start,len @@ | ||||
self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena, | ||||
self.startb, self.lenb) | ||||
self.hunk[0] = self.desc | ||||
|
r13699 | self._fixnewline(lr) | ||
|
r4897 | |||
|
r13699 | def _fixnewline(self, lr): | ||
l = lr.readline() | ||||
if l.startswith('\ '): | ||||
diffhelpers.fix_newline(self.hunk, self.a, self.b) | ||||
else: | ||||
lr.push(l) | ||||
|
r4897 | |||
def complete(self): | ||||
return len(self.a) == self.lena and len(self.b) == self.lenb | ||||
|
r16121 | def _fuzzit(self, old, new, fuzz, toponly): | ||
|
r4897 | # this removes context lines from the top and bottom of list 'l'. It | ||
# checks the hunk to make sure only context lines are removed, and then | ||||
# returns a new shortened list of lines. | ||||
|
r16124 | fuzz = min(fuzz, len(old)) | ||
|
r4897 | if fuzz: | ||
top = 0 | ||||
bot = 0 | ||||
hlen = len(self.hunk) | ||||
|
r10282 | for x in xrange(hlen - 1): | ||
|
r4897 | # the hunk starts with the @@ line, so use x+1 | ||
|
r10282 | if self.hunk[x + 1][0] == ' ': | ||
|
r4897 | top += 1 | ||
else: | ||||
break | ||||
if not toponly: | ||||
|
r10282 | for x in xrange(hlen - 1): | ||
if self.hunk[hlen - bot - 1][0] == ' ': | ||||
|
r4897 | bot += 1 | ||
else: | ||||
break | ||||
|
r16124 | bot = min(fuzz, bot) | ||
top = min(fuzz, top) | ||||
|
r18054 | return old[top:len(old) - bot], new[top:len(new) - bot], top | ||
|
r16122 | return old, new, 0 | ||
|
r4897 | |||
|
r16121 | def fuzzit(self, fuzz, toponly): | ||
|
r16122 | old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly) | ||
oldstart = self.starta + top | ||||
newstart = self.startb + top | ||||
# zero length hunk ranges already have their start decremented | ||||
|
r16650 | if self.lena and oldstart > 0: | ||
|
r16122 | oldstart -= 1 | ||
|
r16650 | if self.lenb and newstart > 0: | ||
|
r16122 | newstart -= 1 | ||
return old, oldstart, new, newstart | ||||
|
r4897 | |||
|
r14764 | class binhunk(object): | ||
|
r20137 | 'A binary patch file.' | ||
|
r16523 | def __init__(self, lr, fname): | ||
|
r4897 | self.text = None | ||
|
r20137 | self.delta = False | ||
|
r9585 | self.hunk = ['GIT binary patch\n'] | ||
|
r16523 | self._fname = fname | ||
|
r14384 | self._read(lr) | ||
|
r4897 | |||
def complete(self): | ||||
return self.text is not None | ||||
|
r20137 | def new(self, lines): | ||
if self.delta: | ||||
return [applybindelta(self.text, ''.join(lines))] | ||||
|
r4897 | return [self.text] | ||
|
r14384 | def _read(self, lr): | ||
|
r16524 | def getline(lr, hunk): | ||
l = lr.readline() | ||||
hunk.append(l) | ||||
return l.rstrip('\r\n') | ||||
|
r20137 | size = 0 | ||
|
r16567 | while True: | ||
|
r16524 | line = getline(lr, self.hunk) | ||
|
r16567 | if not line: | ||
raise PatchError(_('could not extract "%s" binary data') | ||||
% self._fname) | ||||
if line.startswith('literal '): | ||||
|
r20137 | size = int(line[8:].rstrip()) | ||
|
r16567 | break | ||
|
r20137 | if line.startswith('delta '): | ||
size = int(line[6:].rstrip()) | ||||
self.delta = True | ||||
break | ||||
|
r3367 | dec = [] | ||
|
r16524 | line = getline(lr, self.hunk) | ||
|
r4897 | while len(line) > 1: | ||
|
r3374 | l = line[0] | ||
if l <= 'Z' and l >= 'A': | ||||
l = ord(l) - ord('A') + 1 | ||||
else: | ||||
l = ord(l) - ord('a') + 27 | ||||
|
r16522 | try: | ||
|
r16524 | dec.append(base85.b85decode(line[1:])[:l]) | ||
|
r16522 | except ValueError, e: | ||
|
r16523 | raise PatchError(_('could not decode "%s" binary patch: %s') | ||
% (self._fname, str(e))) | ||||
|
r16524 | line = getline(lr, self.hunk) | ||
|
r3367 | text = zlib.decompress(''.join(dec)) | ||
if len(text) != size: | ||||
|
r16523 | raise PatchError(_('"%s" length is %d bytes, should be %d') | ||
% (self._fname, len(text), size)) | ||||
|
r4897 | self.text = text | ||
|
r3367 | |||
|
r4897 | def parsefilename(str): | ||
# --- filename \t|space stuff | ||||
|
r5851 | s = str[4:].rstrip('\r\n') | ||
|
r4897 | i = s.find('\t') | ||
if i < 0: | ||||
i = s.find(' ') | ||||
if i < 0: | ||||
return s | ||||
return s[:i] | ||||
|
r2861 | |||
|
r11022 | def pathstrip(path, strip): | ||
pathlen = len(path) | ||||
i = 0 | ||||
if strip == 0: | ||||
return '', path.rstrip() | ||||
count = strip | ||||
while count > 0: | ||||
i = path.find('/', i) | ||||
if i == -1: | ||||
raise PatchError(_("unable to strip away %d of %d dirs from %s") % | ||||
(count, strip, path)) | ||||
i += 1 | ||||
# consume '//' in the path | ||||
while i < pathlen - 1 and path[i] == '/': | ||||
i += 1 | ||||
count -= 1 | ||||
return path[:i].lstrip(), path[i:].rstrip() | ||||
|
r14566 | def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip): | ||
|
r4897 | nulla = afile_orig == "/dev/null" | ||
nullb = bfile_orig == "/dev/null" | ||||
|
r14451 | create = nulla and hunk.starta == 0 and hunk.lena == 0 | ||
remove = nullb and hunk.startb == 0 and hunk.lenb == 0 | ||||
|
r6295 | abase, afile = pathstrip(afile_orig, strip) | ||
|
r14351 | gooda = not nulla and backend.exists(afile) | ||
|
r6295 | bbase, bfile = pathstrip(bfile_orig, strip) | ||
|
r4897 | if afile == bfile: | ||
goodb = gooda | ||||
else: | ||||
|
r14351 | goodb = not nullb and backend.exists(bfile) | ||
|
r14451 | missing = not goodb and not gooda and not create | ||
|
r9328 | |||
|
r11820 | # some diff programs apparently produce patches where the afile is | ||
# not /dev/null, but afile starts with bfile | ||||
|
r10745 | abasedir = afile[:afile.rfind('/') + 1] | ||
bbasedir = bfile[:bfile.rfind('/') + 1] | ||||
|
r14451 | if (missing and abasedir == bbasedir and afile.startswith(bfile) | ||
and hunk.starta == 0 and hunk.lena == 0): | ||||
create = True | ||||
missing = False | ||||
|
r9328 | |||
|
r6295 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the | ||
# diff is between a file and its backup. In this case, the original | ||||
# file should be patched (see original mpatch code). | ||||
isbackup = (abase == bbase and bfile.startswith(afile)) | ||||
|
r5652 | fname = None | ||
if not missing: | ||||
if gooda and goodb: | ||||
|
r6295 | fname = isbackup and afile or bfile | ||
|
r5652 | elif gooda: | ||
|
r4897 | fname = afile | ||
|
r5760 | |||
|
r5652 | if not fname: | ||
if not nullb: | ||||
|
r6295 | fname = isbackup and afile or bfile | ||
|
r5652 | elif not nulla: | ||
|
r4897 | fname = afile | ||
|
r5652 | else: | ||
raise PatchError(_("undefined source and destination files")) | ||||
|
r5760 | |||
|
r14566 | gp = patchmeta(fname) | ||
if create: | ||||
gp.op = 'ADD' | ||||
elif remove: | ||||
gp.op = 'DELETE' | ||||
return gp | ||||
|
r4897 | |||
|
r7152 | def scangitpatch(lr, firstline): | ||
|
r7186 | """ | ||
|
r7152 | Git patches can emit: | ||
- rename a to b | ||||
- change b | ||||
- copy a to c | ||||
- change c | ||||
|
r7186 | |||
|
r7152 | We cannot apply this sequence as-is, the renamed 'a' could not be | ||
found for it would have been renamed already. And we cannot copy | ||||
from 'b' instead because 'b' would have been changed already. So | ||||
we scan the git patch for copy and rename commands so we can | ||||
perform the copies ahead of time. | ||||
""" | ||||
pos = 0 | ||||
try: | ||||
pos = lr.fp.tell() | ||||
fp = lr.fp | ||||
except IOError: | ||||
fp = cStringIO.StringIO(lr.fp.read()) | ||||
|
r14418 | gitlr = linereader(fp) | ||
|
r7152 | gitlr.push(firstline) | ||
|
r12669 | gitpatches = readgitpatch(gitlr) | ||
|
r7152 | fp.seek(pos) | ||
|
r12669 | return gitpatches | ||
|
r7152 | |||
|
r14240 | def iterhunks(fp): | ||
|
r5650 | """Read a patch and yield the following events: | ||
- ("file", afile, bfile, firsthunk): select a new target file. | ||||
- ("hunk", hunk): a new hunk is ready to be applied, follows a | ||||
"file" event. | ||||
- ("git", gitchanges): current diff is in git format, gitchanges | ||||
maps filenames to gitpatch records. Unique event. | ||||
""" | ||||
|
r4897 | afile = "" | ||
bfile = "" | ||||
state = None | ||||
hunknum = 0 | ||||
|
r14017 | emitfile = newfile = False | ||
|
r14388 | gitpatches = None | ||
|
r2861 | |||
|
r4897 | # our states | ||
BFILE = 1 | ||||
context = None | ||||
|
r10128 | lr = linereader(fp) | ||
|
r2861 | |||
|
r4897 | while True: | ||
x = lr.readline() | ||||
if not x: | ||||
break | ||||
|
r14383 | if state == BFILE and ( | ||
(not context and x[0] == '@') | ||||
or (context is not False and x.startswith('***************')) | ||||
or x.startswith('GIT binary patch')): | ||||
|
r14388 | gp = None | ||
|
r14534 | if (gitpatches and | ||
|
r16506 | gitpatches[-1].ispatching(afile, bfile)): | ||
gp = gitpatches.pop() | ||||
|
r14383 | if x.startswith('GIT binary patch'): | ||
|
r16523 | h = binhunk(lr, gp.path) | ||
|
r14383 | else: | ||
if context is None and x.startswith('***************'): | ||||
context = True | ||||
|
r14451 | h = hunk(x, hunknum + 1, lr, context) | ||
|
r4897 | hunknum += 1 | ||
|
r5650 | if emitfile: | ||
emitfile = False | ||||
|
r14566 | yield 'file', (afile, bfile, h, gp and gp.copy() or None) | ||
|
r13699 | yield 'hunk', h | ||
|
r18830 | elif x.startswith('diff --git a/'): | ||
|
r16524 | m = gitre.match(x.rstrip(' \r\n')) | ||
|
r14387 | if not m: | ||
continue | ||||
|
r16506 | if gitpatches is None: | ||
|
r14387 | # scan whole input for git metadata | ||
|
r16506 | gitpatches = scangitpatch(lr, x) | ||
yield 'git', [g.copy() for g in gitpatches | ||||
if g.op in ('COPY', 'RENAME')] | ||||
|
r14388 | gitpatches.reverse() | ||
|
r14387 | afile = 'a/' + m.group(1) | ||
bfile = 'b/' + m.group(2) | ||||
|
r16506 | while gitpatches and not gitpatches[-1].ispatching(afile, bfile): | ||
gp = gitpatches.pop() | ||||
|
r14566 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) | ||
|
r16506 | if not gitpatches: | ||
raise PatchError(_('failed to synchronize metadata for "%s"') | ||||
% afile[2:]) | ||||
gp = gitpatches[-1] | ||||
|
r14387 | newfile = True | ||
|
r4897 | elif x.startswith('---'): | ||
# check for a unified diff | ||||
l2 = lr.readline() | ||||
if not l2.startswith('+++'): | ||||
lr.push(l2) | ||||
continue | ||||
newfile = True | ||||
context = False | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
elif x.startswith('***'): | ||||
# check for a context diff | ||||
l2 = lr.readline() | ||||
if not l2.startswith('---'): | ||||
lr.push(l2) | ||||
continue | ||||
l3 = lr.readline() | ||||
lr.push(l3) | ||||
if not l3.startswith("***************"): | ||||
lr.push(l2) | ||||
continue | ||||
newfile = True | ||||
context = True | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
|
r3057 | |||
|
r14017 | if newfile: | ||
newfile = False | ||||
|
r5650 | emitfile = True | ||
|
r4897 | state = BFILE | ||
hunknum = 0 | ||||
|
r5650 | |||
|
r14388 | while gitpatches: | ||
|
r16506 | gp = gitpatches.pop() | ||
|
r14566 | yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy()) | ||
|
r14388 | |||
|
r20137 | def applybindelta(binchunk, data): | ||
"""Apply a binary delta hunk | ||||
The algorithm used is the algorithm from git's patch-delta.c | ||||
""" | ||||
def deltahead(binchunk): | ||||
i = 0 | ||||
for c in binchunk: | ||||
i += 1 | ||||
if not (ord(c) & 0x80): | ||||
return i | ||||
return i | ||||
out = "" | ||||
s = deltahead(binchunk) | ||||
binchunk = binchunk[s:] | ||||
s = deltahead(binchunk) | ||||
binchunk = binchunk[s:] | ||||
i = 0 | ||||
while i < len(binchunk): | ||||
cmd = ord(binchunk[i]) | ||||
i += 1 | ||||
if (cmd & 0x80): | ||||
offset = 0 | ||||
size = 0 | ||||
if (cmd & 0x01): | ||||
offset = ord(binchunk[i]) | ||||
i += 1 | ||||
if (cmd & 0x02): | ||||
offset |= ord(binchunk[i]) << 8 | ||||
i += 1 | ||||
if (cmd & 0x04): | ||||
offset |= ord(binchunk[i]) << 16 | ||||
i += 1 | ||||
if (cmd & 0x08): | ||||
offset |= ord(binchunk[i]) << 24 | ||||
i += 1 | ||||
if (cmd & 0x10): | ||||
size = ord(binchunk[i]) | ||||
i += 1 | ||||
if (cmd & 0x20): | ||||
size |= ord(binchunk[i]) << 8 | ||||
i += 1 | ||||
if (cmd & 0x40): | ||||
size |= ord(binchunk[i]) << 16 | ||||
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: | ||||
raise PatchError(_('unexpected delta opcode 0')) | ||||
return out | ||||
|
r14565 | def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'): | ||
|
r10966 | """Reads a patch from fp and tries to apply it. | ||
|
r5650 | |||
|
r14565 | Returns 0 for a clean patch, -1 if any rejects were found and 1 if | ||
there was any fuzz. | ||||
|
r8810 | |||
|
r10101 | If 'eolmode' is 'strict', the patch content and patched file are | ||
read in binary mode. Otherwise, line endings are ignored when | ||||
patching then normalized according to 'eolmode'. | ||||
|
r8810 | """ | ||
|
r14565 | return _applydiff(ui, fp, patchfile, backend, store, strip=strip, | ||
|
r12916 | eolmode=eolmode) | ||
|
r10966 | |||
|
r14565 | def _applydiff(ui, fp, patcher, backend, store, strip=1, | ||
|
r14452 | eolmode='strict'): | ||
|
r14389 | |||
def pstrip(p): | ||||
return pathstrip(p, strip - 1)[1] | ||||
|
r5650 | rejects = 0 | ||
err = 0 | ||||
current_file = None | ||||
|
r14240 | for state, values in iterhunks(fp): | ||
|
r5650 | if state == 'hunk': | ||
if not current_file: | ||||
continue | ||||
|
r11021 | ret = current_file.apply(values) | ||
|
r14565 | if ret > 0: | ||
err = 1 | ||||
|
r5650 | elif state == 'file': | ||
|
r13701 | if current_file: | ||
rejects += current_file.close() | ||||
|
r14388 | current_file = None | ||
afile, bfile, first_hunk, gp = values | ||||
if gp: | ||||
|
r14566 | gp.path = pstrip(gp.path) | ||
|
r14452 | if gp.oldpath: | ||
|
r14566 | gp.oldpath = pstrip(gp.oldpath) | ||
else: | ||||
gp = makepatchmeta(backend, afile, bfile, first_hunk, strip) | ||||
if gp.op == 'RENAME': | ||||
backend.unlink(gp.oldpath) | ||||
|
r14388 | if not first_hunk: | ||
|
r14566 | if gp.op == 'DELETE': | ||
backend.unlink(gp.path) | ||||
continue | ||||
data, mode = None, None | ||||
if gp.op in ('RENAME', 'COPY'): | ||||
|
r14609 | data, mode = store.getfile(gp.oldpath)[:2] | ||
|
r22296 | # FIXME: failing getfile has never been handled here | ||
assert data is not None | ||||
|
r14566 | if gp.mode: | ||
mode = gp.mode | ||||
if gp.op == 'ADD': | ||||
# Added files without content have no hunk and | ||||
# must be created | ||||
data = '' | ||||
if data or mode: | ||||
if (gp.op in ('ADD', 'RENAME', 'COPY') | ||||
and backend.exists(gp.path)): | ||||
raise PatchError(_("cannot create %s: destination " | ||||
"already exists") % gp.path) | ||||
backend.setfile(gp.path, data, mode, gp.oldpath) | ||||
|
r14388 | continue | ||
|
r5650 | try: | ||
|
r14566 | current_file = patcher(ui, gp, backend, store, | ||
eolmode=eolmode) | ||||
|
r14218 | except PatchError, inst: | ||
ui.warn(str(inst) + '\n') | ||||
|
r11021 | current_file = None | ||
|
r5650 | rejects += 1 | ||
continue | ||||
elif state == 'git': | ||||
|
r11021 | for gp in values: | ||
|
r14452 | path = pstrip(gp.oldpath) | ||
|
r22296 | data, mode = backend.getfile(path) | ||
if data is None: | ||||
|
r16813 | # The error ignored here will trigger a getfile() | ||
# error in a place more appropriate for error | ||||
# handling, and will not interrupt the patching | ||||
# process. | ||||
|
r22296 | pass | ||
|
r16813 | else: | ||
store.setfile(path, data, mode) | ||||
|
r4897 | else: | ||
|
r5650 | raise util.Abort(_('unsupported parser state: %s') % state) | ||
|
r5649 | |||
|
r13701 | if current_file: | ||
rejects += current_file.close() | ||||
|
r5650 | |||
|
r4897 | if rejects: | ||
return -1 | ||||
return err | ||||
|
r2874 | |||
|
r14382 | def _externalpatch(ui, repo, patcher, patchname, strip, files, | ||
|
r14381 | similarity): | ||
|
r7151 | """use <patcher> to apply <patchname> to the working directory. | ||
returns whether patch was applied with fuzz factor.""" | ||||
fuzz = False | ||||
|
r12673 | args = [] | ||
|
r14382 | cwd = repo.root | ||
|
r7151 | if cwd: | ||
args.append('-d %s' % util.shellquote(cwd)) | ||||
fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, | ||||
util.shellquote(patchname))) | ||||
|
r14381 | try: | ||
for line in fp: | ||||
line = line.rstrip() | ||||
ui.note(line + '\n') | ||||
if line.startswith('patching file '): | ||||
pf = util.parsepatchoutput(line) | ||||
printed_file = False | ||||
|
r14564 | files.add(pf) | ||
|
r14381 | elif line.find('with fuzz') >= 0: | ||
fuzz = True | ||||
if not printed_file: | ||||
ui.warn(pf + '\n') | ||||
printed_file = True | ||||
ui.warn(line + '\n') | ||||
elif line.find('saving rejects to file') >= 0: | ||||
ui.warn(line + '\n') | ||||
elif line.find('FAILED') >= 0: | ||||
if not printed_file: | ||||
ui.warn(pf + '\n') | ||||
printed_file = True | ||||
ui.warn(line + '\n') | ||||
finally: | ||||
if files: | ||||
|
r19155 | scmutil.marktouched(repo, files, similarity) | ||
|
r7151 | code = fp.close() | ||
if code: | ||||
raise PatchError(_("patch command failed: %s") % | ||||
|
r14234 | util.explainexit(code)[0]) | ||
|
r7151 | return fuzz | ||
|
r14611 | def patchbackend(ui, backend, patchobj, strip, files=None, eolmode='strict'): | ||
|
r9683 | if files is None: | ||
|
r14564 | files = set() | ||
|
r8810 | if eolmode is None: | ||
eolmode = ui.config('patch', 'eol', 'strict') | ||||
|
r10101 | if eolmode.lower() not in eolmodes: | ||
|
r12067 | raise util.Abort(_('unsupported line endings type: %s') % eolmode) | ||
|
r10101 | eolmode = eolmode.lower() | ||
|
r8843 | |||
|
r14452 | store = filestore() | ||
|
r7151 | try: | ||
|
r9031 | fp = open(patchobj, 'rb') | ||
|
r7151 | except TypeError: | ||
fp = patchobj | ||||
try: | ||||
|
r14565 | ret = applydiff(ui, fp, backend, store, strip=strip, | ||
|
r14452 | eolmode=eolmode) | ||
|
r7151 | finally: | ||
|
r10203 | if fp != patchobj: | ||
fp.close() | ||||
|
r14564 | files.update(backend.close()) | ||
|
r14452 | store.close() | ||
|
r7151 | if ret < 0: | ||
|
r12674 | raise PatchError(_('patch failed to apply')) | ||
|
r7151 | return ret > 0 | ||
|
r14611 | def internalpatch(ui, repo, patchobj, strip, files=None, eolmode='strict', | ||
similarity=0): | ||||
"""use builtin patch to apply <patchobj> to the working directory. | ||||
returns whether patch was applied with fuzz factor.""" | ||||
backend = workingbackend(ui, repo, similarity) | ||||
return patchbackend(ui, backend, patchobj, strip, files, eolmode) | ||||
def patchrepo(ui, repo, ctx, store, patchobj, strip, files=None, | ||||
eolmode='strict'): | ||||
backend = repobackend(ui, repo, ctx, store) | ||||
return patchbackend(ui, backend, patchobj, strip, files, eolmode) | ||||
|
r14382 | def patch(ui, repo, patchname, strip=1, files=None, eolmode='strict', | ||
|
r14260 | similarity=0): | ||
|
r8810 | """Apply <patchname> to the working directory. | ||
'eolmode' specifies how end of lines should be handled. It can be: | ||||
- 'strict': inputs are read in binary mode, EOLs are preserved | ||||
- 'crlf': EOLs are ignored when patching and reset to CRLF | ||||
- 'lf': EOLs are ignored when patching and reset to LF | ||||
- None: get it from user settings, default to 'strict' | ||||
'eolmode' is ignored when using an external patcher program. | ||||
Returns whether patch was applied with fuzz factor. | ||||
""" | ||||
|
r7151 | patcher = ui.config('ui', 'patch') | ||
|
r9683 | if files is None: | ||
|
r14564 | files = set() | ||
|
r21553 | if patcher: | ||
return _externalpatch(ui, repo, patcher, patchname, strip, | ||||
files, similarity) | ||||
return internalpatch(ui, repo, patchname, strip, files, eolmode, | ||||
similarity) | ||||
|
r7151 | |||
|
r14351 | def changedfiles(ui, repo, patchpath, strip=1): | ||
backend = fsbackend(ui, repo.root) | ||||
|
r14255 | fp = open(patchpath, 'rb') | ||
try: | ||||
changed = set() | ||||
for state, values in iterhunks(fp): | ||||
|
r14389 | if state == 'file': | ||
|
r14388 | afile, bfile, first_hunk, gp = values | ||
if gp: | ||||
|
r14566 | gp.path = pathstrip(gp.path, strip - 1)[1] | ||
if gp.oldpath: | ||||
gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1] | ||||
else: | ||||
gp = makepatchmeta(backend, afile, bfile, first_hunk, strip) | ||||
changed.add(gp.path) | ||||
if gp.op == 'RENAME': | ||||
changed.add(gp.oldpath) | ||||
|
r14389 | elif state not in ('hunk', 'git'): | ||
|
r14255 | raise util.Abort(_('unsupported parser state: %s') % state) | ||
return changed | ||||
finally: | ||||
fp.close() | ||||
|
r10189 | class GitDiffRequired(Exception): | ||
pass | ||||
|
r7198 | |||
|
r23431 | def diffallopts(ui, opts=None, untrusted=False, section='diff'): | ||
|
r23430 | '''return diffopts with all features supported and parsed''' | ||
return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section) | ||||
|
r23431 | diffopts = diffallopts | ||
|
r23430 | def difffeatureopts(ui, opts=None, untrusted=False, section='diff'): | ||
'''return diffopts with only opted-in features parsed''' | ||||
|
r23295 | def get(key, name=None, getter=ui.configbool, forceplain=None): | ||
if opts: | ||||
v = opts.get(key) | ||||
if v: | ||||
return v | ||||
|
r23296 | if forceplain is not None and ui.plain(): | ||
return forceplain | ||||
|
r23295 | return getter(section, name or key, None, untrusted=untrusted) | ||
|
r23429 | buildopts = { | ||
'text': opts and opts.get('text'), | ||||
'git': get('git'), | ||||
'nodates': get('nodates'), | ||||
'nobinary': get('nobinary'), | ||||
'noprefix': get('noprefix', forceplain=False), | ||||
'showfunc': get('show_function', 'showfunc'), | ||||
'ignorews': get('ignore_all_space', 'ignorews'), | ||||
'ignorewsamount': get('ignore_space_change', 'ignorewsamount'), | ||||
'ignoreblanklines': get('ignore_blank_lines', 'ignoreblanklines'), | ||||
'context': get('unified', getter=ui.config), | ||||
} | ||||
return mdiff.diffopts(**buildopts) | ||||
|
r10615 | |||
|
r10189 | def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None, | ||
|
r12167 | losedatafn=None, prefix=''): | ||
|
r7308 | '''yields diff of changes to files between two nodes, or node and | ||
|
r2874 | working directory. | ||
if node1 is None, use first dirstate parent instead. | ||||
|
r10189 | if node2 is None, compare node1 with working directory. | ||
losedatafn(**kwarg) is a callable run when opts.upgrade=True and | ||||
every time some change cannot be represented with the current | ||||
patch format. Return False to upgrade to git patch format, True to | ||||
accept the loss or raise an exception to abort the diff. It is | ||||
called with the name of current file being diffed as 'fn'. If set | ||||
to None, patches will always be upgraded to git format when | ||||
necessary. | ||||
|
r12167 | |||
prefix is a filename prefix that is prepended to all filenames on | ||||
display (used for subrepos). | ||||
|
r10189 | ''' | ||
|
r2874 | |||
if opts is None: | ||||
opts = mdiff.defaultopts | ||||
|
r9725 | if not node1 and not node2: | ||
|
r13878 | node1 = repo.dirstate.p1() | ||
|
r2934 | |||
|
r9123 | def lrugetfilectx(): | ||
cache = {} | ||||
|
r16834 | order = util.deque() | ||
|
r9123 | def getfilectx(f, ctx): | ||
fctx = ctx.filectx(f, filelog=cache.get(f)) | ||||
if f not in cache: | ||||
if len(cache) > 20: | ||||
|
r16803 | del cache[order.popleft()] | ||
|
r9684 | cache[f] = fctx.filelog() | ||
|
r9123 | else: | ||
order.remove(f) | ||||
order.append(f) | ||||
return fctx | ||||
return getfilectx | ||||
getfilectx = lrugetfilectx() | ||||
|
r2934 | |||
|
r6747 | ctx1 = repo[node1] | ||
|
r7090 | ctx2 = repo[node2] | ||
|
r2874 | |||
if not changes: | ||||
|
r7090 | changes = repo.status(ctx1, ctx2, match=match) | ||
|
r6760 | modified, added, removed = changes[:3] | ||
|
r2874 | |||
if not modified and not added and not removed: | ||||
|
r10189 | return [] | ||
revs = None | ||||
|
r17942 | hexfunc = repo.ui.debugflag and hex or short | ||
|
r21833 | revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node] | ||
|
r10189 | |||
copy = {} | ||||
if opts.git or opts.upgrade: | ||||
|
r15774 | copy = copies.pathcopies(ctx1, ctx2) | ||
|
r10189 | |||
|
r17299 | def difffn(opts, losedata): | ||
return trydiff(repo, revs, ctx1, ctx2, modified, added, removed, | ||||
copy, getfilectx, opts, losedata, prefix) | ||||
|
r10189 | if opts.upgrade and not opts.git: | ||
try: | ||||
def losedata(fn): | ||||
if not losedatafn or not losedatafn(fn=fn): | ||||
|
r16687 | raise GitDiffRequired | ||
|
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) | ||||
|
r10818 | def difflabel(func, *args, **kw): | ||
'''yields 2-tuples of (output, label) based on the output of func()''' | ||||
|
r15201 | headprefixes = [('diff', 'diff.diffline'), | ||
('copy', 'diff.extended'), | ||||
('rename', 'diff.extended'), | ||||
('old', 'diff.extended'), | ||||
('new', 'diff.extended'), | ||||
('deleted', 'diff.extended'), | ||||
('---', 'diff.file_a'), | ||||
('+++', 'diff.file_b')] | ||||
textprefixes = [('@', 'diff.hunk'), | ||||
('-', 'diff.deleted'), | ||||
('+', 'diff.inserted')] | ||||
head = False | ||||
|
r10818 | for chunk in func(*args, **kw): | ||
lines = chunk.split('\n') | ||||
for i, line in enumerate(lines): | ||||
if i != 0: | ||||
yield ('\n', '') | ||||
|
r15201 | if head: | ||
if line.startswith('@'): | ||||
head = False | ||||
else: | ||||
|
r16686 | if line and line[0] not in ' +-@\\': | ||
|
r15201 | head = True | ||
|
r10818 | stripline = line | ||
|
r22460 | diffline = False | ||
|
r15201 | if not head and line and line[0] in '+-': | ||
|
r22460 | # highlight tabs and trailing whitespace, but only in | ||
# changed lines | ||||
|
r10818 | stripline = line.rstrip() | ||
|
r22460 | diffline = True | ||
|
r15201 | prefixes = textprefixes | ||
if head: | ||||
prefixes = headprefixes | ||||
|
r10818 | for prefix, label in prefixes: | ||
if stripline.startswith(prefix): | ||||
|
r22460 | if diffline: | ||
for token in tabsplitter.findall(stripline): | ||||
if '\t' == token[0]: | ||||
yield (token, 'diff.tab') | ||||
else: | ||||
yield (token, label) | ||||
else: | ||||
yield (stripline, label) | ||||
|
r10818 | break | ||
else: | ||||
yield (line, '') | ||||
if line != stripline: | ||||
yield (line[len(stripline):], 'diff.trailingwhitespace') | ||||
def diffui(*args, **kw): | ||||
'''like diff(), but yields 2-tuples of (output, label) for ui.write()''' | ||||
return difflabel(diff, *args, **kw) | ||||
|
r10189 | def trydiff(repo, revs, ctx1, ctx2, modified, added, removed, | ||
|
r12167 | copy, getfilectx, opts, losedatafn, prefix): | ||
def join(f): | ||||
|
r17968 | return posixpath.join(prefix, f) | ||
|
r2874 | |||
|
r17945 | def addmodehdr(header, omode, nmode): | ||
if omode != nmode: | ||||
header.append('old mode %s\n' % omode) | ||||
header.append('new mode %s\n' % nmode) | ||||
|
r17946 | def addindexmeta(meta, revs): | ||
if opts.git: | ||||
i = len(revs) | ||||
if i==2: | ||||
meta.append('index %s..%s\n' % tuple(revs)) | ||||
elif i==3: | ||||
meta.append('index %s,%s..%s\n' % tuple(revs)) | ||||
def gitindex(text): | ||||
if not text: | ||||
|
r19875 | text = "" | ||
|
r17946 | l = len(text) | ||
s = util.sha1('blob %d\0' % l) | ||||
s.update(text) | ||||
return s.hexdigest() | ||||
|
r23300 | if opts.noprefix: | ||
aprefix = bprefix = '' | ||||
else: | ||||
aprefix = 'a/' | ||||
bprefix = 'b/' | ||||
|
r17943 | def diffline(a, b, revs): | ||
|
r17941 | if opts.git: | ||
|
r23300 | line = 'diff --git %s%s %s%s\n' % (aprefix, a, bprefix, b) | ||
|
r17944 | elif not repo.ui.quiet: | ||
if revs: | ||||
revinfo = ' '.join(["-r %s" % rev for rev in revs]) | ||||
line = 'diff %s %s\n' % (revinfo, a) | ||||
else: | ||||
line = 'diff %s\n' % a | ||||
|
r17941 | else: | ||
|
r17944 | line = '' | ||
return line | ||||
|
r17941 | |||
|
r7090 | date1 = util.datestr(ctx1.date()) | ||
man1 = ctx1.manifest() | ||||
|
r3967 | |||
|
r10189 | gone = set() | ||
gitmode = {'l': '120000', 'x': '100755', '': '100644'} | ||||
|
r2874 | |||
|
r10466 | copyto = dict([(v, k) for k, v in copy.items()]) | ||
|
r2907 | if opts.git: | ||
|
r10189 | revs = None | ||
|
r3996 | |||
|
r8209 | for f in sorted(modified + added + removed): | ||
|
r2874 | to = None | ||
tn = None | ||||
|
r2907 | dodiff = True | ||
|
r3329 | header = [] | ||
|
r3967 | if f in man1: | ||
to = getfilectx(f, ctx1).data() | ||||
|
r2874 | if f not in removed: | ||
|
r3967 | tn = getfilectx(f, ctx2).data() | ||
|
r5482 | a, b = f, f | ||
|
r10189 | if opts.git or losedatafn: | ||
|
r19973 | if f in added or (f in modified and to is None): | ||
|
r6743 | mode = gitmode[ctx2.flags(f)] | ||
|
r10466 | if f in copy or f in copyto: | ||
|
r10189 | if opts.git: | ||
|
r10466 | if f in copy: | ||
a = copy[f] | ||||
else: | ||||
a = copyto[f] | ||||
|
r10189 | omode = gitmode[man1.flags(a)] | ||
|
r17945 | addmodehdr(header, omode, mode) | ||
|
r10189 | if a in removed and a not in gone: | ||
op = 'rename' | ||||
gone.add(a) | ||||
else: | ||||
op = 'copy' | ||||
|
r12167 | header.append('%s from %s\n' % (op, join(a))) | ||
header.append('%s to %s\n' % (op, join(f))) | ||||
|
r10189 | to = getfilectx(a, ctx1).data() | ||
|
r3702 | else: | ||
|
r10189 | losedatafn(f) | ||
|
r2907 | else: | ||
|
r10189 | if opts.git: | ||
header.append('new file mode %s\n' % mode) | ||||
elif ctx2.flags(f): | ||||
losedatafn(f) | ||||
|
r12576 | # In theory, if tn was copied or renamed we should check | ||
# if the source is binary too but the copy record already | ||||
# forces git mode. | ||||
|
r4092 | if util.binary(tn): | ||
|
r10189 | if opts.git: | ||
dodiff = 'binary' | ||||
else: | ||||
losedatafn(f) | ||||
if not opts.git and not tn: | ||||
# regular diffs cannot represent new empty file | ||||
losedatafn(f) | ||||
|
r19973 | elif f in removed or (f in modified and tn is None): | ||
|
r10189 | if opts.git: | ||
# have we already reported a copy above? | ||||
|
r10467 | if ((f in copy and copy[f] in added | ||
and copyto[copy[f]] == f) or | ||||
(f in copyto and copyto[f] in added | ||||
and copy[copyto[f]] == f)): | ||||
|
r10189 | dodiff = False | ||
else: | ||||
header.append('deleted file mode %s\n' % | ||||
gitmode[man1.flags(f)]) | ||||
|
r18824 | if util.binary(to): | ||
dodiff = 'binary' | ||||
|
r12575 | elif not to or util.binary(to): | ||
|
r10189 | # regular diffs cannot represent empty file deletion | ||
losedatafn(f) | ||||
|
r2907 | else: | ||
|
r10189 | oflag = man1.flags(f) | ||
nflag = ctx2.flags(f) | ||||
binary = util.binary(to) or util.binary(tn) | ||||
if opts.git: | ||||
|
r17945 | addmodehdr(header, gitmode[oflag], gitmode[nflag]) | ||
|
r10189 | if binary: | ||
dodiff = 'binary' | ||||
elif binary or nflag != oflag: | ||||
losedatafn(f) | ||||
|
r4106 | if dodiff: | ||
|
r17940 | if opts.git or revs: | ||
|
r17943 | header.insert(0, diffline(join(a), join(b), revs)) | ||
|
r21790 | if dodiff == 'binary' and not opts.nobinary: | ||
|
r17939 | text = mdiff.b85diff(to, tn) | ||
|
r17946 | if text: | ||
addindexmeta(header, [gitindex(to), gitindex(tn)]) | ||||
|
r4106 | else: | ||
|
r4108 | text = mdiff.unidiff(to, date1, | ||
# ctx2 date may be dynamic | ||||
tn, util.datestr(ctx2.date()), | ||||
|
r17940 | join(a), join(b), opts=opts) | ||
|
r7308 | if header and (text or len(header) > 1): | ||
yield ''.join(header) | ||||
if text: | ||||
yield text | ||||
|
r2874 | |||
|
r14401 | def diffstatsum(stats): | ||
|
r14437 | maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False | ||
|
r14401 | for f, a, r, b in stats: | ||
maxfile = max(maxfile, encoding.colwidth(f)) | ||||
|
r14437 | maxtotal = max(maxtotal, a + r) | ||
|
r14401 | addtotal += a | ||
removetotal += r | ||||
binary = binary or b | ||||
|
r14437 | return maxfile, maxtotal, addtotal, removetotal, binary | ||
|
r14401 | |||
|
r7547 | def diffstatdata(lines): | ||
|
r13395 | diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$') | ||
|
r14400 | results = [] | ||
|
r15363 | filename, adds, removes, isbinary = None, 0, 0, False | ||
|
r14400 | |||
def addresult(): | ||||
if filename: | ||||
results.append((filename, adds, removes, isbinary)) | ||||
|
r7547 | for line in lines: | ||
if line.startswith('diff'): | ||||
|
r14400 | addresult() | ||
|
r7547 | # set numbers to 0 anyway when starting new file | ||
|
r15363 | adds, removes, isbinary = 0, 0, False | ||
|
r18830 | if line.startswith('diff --git a/'): | ||
|
r20972 | filename = gitre.search(line).group(2) | ||
|
r13395 | elif line.startswith('diff -r'): | ||
|
r8761 | # format: "diff -r ... -r ... filename" | ||
|
r13395 | filename = diffre.search(line).group(1) | ||
|
r15971 | elif line.startswith('+') and not line.startswith('+++ '): | ||
|
r7547 | adds += 1 | ||
|
r15971 | elif line.startswith('-') and not line.startswith('--- '): | ||
|
r7547 | removes += 1 | ||
|
r15363 | elif (line.startswith('GIT binary patch') or | ||
line.startswith('Binary file')): | ||||
isbinary = True | ||||
|
r14400 | addresult() | ||
return results | ||||
|
r7547 | |||
|
r9642 | def diffstat(lines, width=80, git=False): | ||
|
r7547 | output = [] | ||
|
r14402 | stats = diffstatdata(lines) | ||
|
r14437 | maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats) | ||
|
r7547 | |||
countwidth = len(str(maxtotal)) | ||||
|
r9642 | if hasbinary and countwidth < 3: | ||
countwidth = 3 | ||||
|
r9330 | graphwidth = width - countwidth - maxname - 6 | ||
|
r7547 | if graphwidth < 10: | ||
graphwidth = 10 | ||||
|
r9330 | def scale(i): | ||
if maxtotal <= graphwidth: | ||||
return i | ||||
# If diffstat runs out of room it doesn't print anything, | ||||
# which isn't very useful, so always print at least one + or - | ||||
# if there were at least some changes. | ||||
return max(i * graphwidth // maxtotal, int(bool(i))) | ||||
|
r7547 | |||
|
r14402 | for filename, adds, removes, isbinary in stats: | ||
|
r15363 | if isbinary: | ||
|
r9642 | count = 'Bin' | ||
else: | ||||
count = adds + removes | ||||
|
r9330 | pluses = '+' * scale(adds) | ||
minuses = '-' * scale(removes) | ||||
|
r11611 | output.append(' %s%s | %*s %s%s\n' % | ||
|
r14402 | (filename, ' ' * (maxname - encoding.colwidth(filename)), | ||
countwidth, count, pluses, minuses)) | ||||
|
r7547 | |||
if stats: | ||||
|
r16683 | output.append(_(' %d files changed, %d insertions(+), ' | ||
'%d deletions(-)\n') | ||||
|
r7860 | % (len(stats), totaladds, totalremoves)) | ||
|
r7547 | |||
return ''.join(output) | ||||
|
r10818 | |||
def diffstatui(*args, **kw): | ||||
'''like diffstat(), but yields 2-tuples of (output, label) for | ||||
ui.write() | ||||
''' | ||||
for line in diffstat(*args, **kw).splitlines(): | ||||
if line and line[-1] in '+-': | ||||
name, graph = line.rsplit(' ', 1) | ||||
yield (name + ' ', '') | ||||
m = re.search(r'\++', graph) | ||||
if m: | ||||
yield (m.group(0), 'diffstat.inserted') | ||||
m = re.search(r'-+', graph) | ||||
if m: | ||||
yield (m.group(0), 'diffstat.deleted') | ||||
else: | ||||
yield (line, '') | ||||
yield ('\n', '') | ||||