2006-08-12 02:50:16 +04:00
|
|
|
# patch.py - patch file parsing routines
|
|
|
|
#
|
2006-08-12 23:47:18 +04:00
|
|
|
# Copyright 2006 Brendan Cully <brendan@kublai.com>
|
2007-07-17 20:39:30 +04:00
|
|
|
# Copyright 2007 Chris Mason <chris.mason@oracle.com>
|
2006-08-12 23:47:18 +04:00
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2006-08-12 02:50:16 +04:00
|
|
|
|
2010-12-10 17:14:05 +03:00
|
|
|
import cStringIO, email.Parser, os, errno, re
|
2011-05-27 23:50:10 +04:00
|
|
|
import tempfile, zlib, shutil
|
2010-04-17 22:13:57 +04:00
|
|
|
|
2006-12-15 05:25:19 +03:00
|
|
|
from i18n import _
|
2008-03-07 00:23:26 +03:00
|
|
|
from node import hex, nullid, short
|
2011-06-15 01:26:35 +04:00
|
|
|
import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
|
2012-06-02 04:05:31 +04:00
|
|
|
import context
|
2006-08-13 00:16:48 +04:00
|
|
|
|
2008-10-22 14:56:28 +04:00
|
|
|
gitre = re.compile('diff --git a/(.*) b/(.*)')
|
|
|
|
|
2007-07-17 20:39:30 +04:00
|
|
|
class PatchError(Exception):
|
|
|
|
pass
|
|
|
|
|
2006-08-17 06:46:18 +04:00
|
|
|
|
|
|
|
# public functions
|
|
|
|
|
2010-02-07 20:06:52 +03:00
|
|
|
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
|
2010-04-09 22:34:05 +04:00
|
|
|
if line[0] in (' ', '-', '+'):
|
|
|
|
# diff line - don't check for header pattern in there
|
|
|
|
return False
|
2010-02-07 20:06:52 +03:00
|
|
|
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']
|
|
|
|
|
2011-07-26 01:02:27 +04:00
|
|
|
if not util.safehasattr(stream, 'next'):
|
2010-02-07 20:06:52 +03:00
|
|
|
# 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)
|
2010-02-18 21:46:01 +03:00
|
|
|
elif line.startswith('--- ') and inheader:
|
|
|
|
# No evil headers seen by diff start, split by hand
|
2010-02-07 20:06:52 +03:00
|
|
|
return headersplit(stream, cur)
|
|
|
|
# Not enough info, keep reading
|
|
|
|
|
|
|
|
# if we are here, we have a very plain patch
|
|
|
|
return remainder(cur)
|
|
|
|
|
2006-08-13 00:16:48 +04:00
|
|
|
def extract(ui, fileobj):
|
|
|
|
'''extract patch from data read from fileobj.
|
|
|
|
|
2007-03-22 20:44:59 +03:00
|
|
|
patch can be a normal patch or contained in an email message.
|
2006-08-13 00:16:48 +04:00
|
|
|
|
2010-07-22 09:30:27 +04:00
|
|
|
return tuple (filename, message, user, date, branch, node, p1, p2).
|
2007-03-22 20:44:59 +03:00
|
|
|
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.'''
|
2006-08-13 00:16:48 +04:00
|
|
|
|
|
|
|
# attempt to detect the start of a patch
|
|
|
|
# (this heuristic is borrowed from quilt)
|
2009-02-04 22:53:38 +03:00
|
|
|
diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |'
|
|
|
|
r'retrieving revision [0-9]+(\.[0-9]+)*$|'
|
2010-03-19 14:45:39 +03:00
|
|
|
r'---[ \t].*?^\+\+\+[ \t]|'
|
|
|
|
r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL)
|
2006-08-13 00:16:48 +04:00
|
|
|
|
|
|
|
fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
|
|
|
|
tmpfp = os.fdopen(fd, 'w')
|
|
|
|
try:
|
|
|
|
msg = email.Parser.Parser().parse(fileobj)
|
|
|
|
|
2007-07-02 21:59:16 +04:00
|
|
|
subject = msg['Subject']
|
2006-08-13 00:16:48 +04:00
|
|
|
user = msg['From']
|
2009-10-09 01:42:33 +04:00
|
|
|
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'
|
|
|
|
|
2007-10-09 00:20:23 +04:00
|
|
|
gitsendmail = 'git-send-email' in msg.get('X-Mailer', '')
|
2006-08-13 00:16:48 +04:00
|
|
|
# should try to parse msg['Date']
|
|
|
|
date = None
|
2007-03-22 20:44:59 +03:00
|
|
|
nodeid = None
|
2007-05-18 07:09:47 +04:00
|
|
|
branch = None
|
2007-03-22 20:44:59 +03:00
|
|
|
parents = []
|
2006-08-13 00:16:48 +04:00
|
|
|
|
2007-07-02 21:59:16 +04:00
|
|
|
if subject:
|
|
|
|
if subject.startswith('[PATCH'):
|
|
|
|
pend = subject.find(']')
|
2007-03-14 07:54:34 +03:00
|
|
|
if pend >= 0:
|
2010-01-25 09:05:27 +03:00
|
|
|
subject = subject[pend + 1:].lstrip()
|
2011-09-28 03:41:09 +04:00
|
|
|
subject = re.sub(r'\n[ \t]+', ' ', subject)
|
2007-07-02 21:59:16 +04:00
|
|
|
ui.debug('Subject: %s\n' % subject)
|
2006-08-13 00:16:48 +04:00
|
|
|
if user:
|
|
|
|
ui.debug('From: %s\n' % user)
|
|
|
|
diffs_seen = 0
|
|
|
|
ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
|
2007-07-17 20:39:30 +04:00
|
|
|
message = ''
|
2006-08-13 00:16:48 +04:00
|
|
|
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:
|
2007-03-14 21:46:07 +03:00
|
|
|
hgpatch = False
|
2010-10-09 08:39:44 +04:00
|
|
|
hgpatchheader = False
|
2007-03-14 21:46:07 +03:00
|
|
|
ignoretext = False
|
|
|
|
|
2009-09-19 03:15:38 +04:00
|
|
|
ui.debug('found patch at byte %d\n' % m.start(0))
|
2006-08-13 00:16:48 +04:00
|
|
|
diffs_seen += 1
|
|
|
|
cfp = cStringIO.StringIO()
|
|
|
|
for line in payload[:m.start(0)].splitlines():
|
2010-10-14 03:28:29 +04:00
|
|
|
if line.startswith('# HG changeset patch') and not hgpatch:
|
2009-09-19 03:15:38 +04:00
|
|
|
ui.debug('patch generated by hg export\n')
|
2010-10-14 03:28:29 +04:00
|
|
|
hgpatch = True
|
2010-10-09 08:39:44 +04:00
|
|
|
hgpatchheader = True
|
2006-08-13 00:16:48 +04:00
|
|
|
# drop earlier commit message content
|
|
|
|
cfp.seek(0)
|
|
|
|
cfp.truncate()
|
2007-07-03 00:26:12 +04:00
|
|
|
subject = None
|
2010-10-09 08:39:44 +04:00
|
|
|
elif hgpatchheader:
|
2006-08-13 00:16:48 +04:00
|
|
|
if line.startswith('# User '):
|
|
|
|
user = line[7:]
|
|
|
|
ui.debug('From: %s\n' % user)
|
|
|
|
elif line.startswith("# Date "):
|
|
|
|
date = line[7:]
|
2007-05-18 07:09:47 +04:00
|
|
|
elif line.startswith("# Branch "):
|
|
|
|
branch = line[9:]
|
2007-03-22 20:44:59 +03:00
|
|
|
elif line.startswith("# Node ID "):
|
|
|
|
nodeid = line[10:]
|
|
|
|
elif line.startswith("# Parent "):
|
2012-04-20 21:11:54 +04:00
|
|
|
parents.append(line[9:].lstrip())
|
2010-10-09 08:39:44 +04:00
|
|
|
elif not line.startswith("# "):
|
|
|
|
hgpatchheader = False
|
2007-10-09 00:20:23 +04:00
|
|
|
elif line == '---' and gitsendmail:
|
2007-03-14 21:46:07 +03:00
|
|
|
ignoretext = True
|
2010-10-09 08:39:44 +04:00
|
|
|
if not hgpatchheader and not ignoretext:
|
2006-08-13 00:16:48 +04:00
|
|
|
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
|
2012-05-13 15:18:06 +04:00
|
|
|
except: # re-raises
|
2006-08-13 00:16:48 +04:00
|
|
|
tmpfp.close()
|
|
|
|
os.unlink(tmpname)
|
|
|
|
raise
|
|
|
|
|
2007-07-02 21:59:16 +04:00
|
|
|
if subject and not message.startswith(subject):
|
|
|
|
message = '%s\n%s' % (subject, message)
|
2006-08-13 00:16:48 +04:00
|
|
|
tmpfp.close()
|
|
|
|
if not diffs_seen:
|
|
|
|
os.unlink(tmpname)
|
2007-05-18 07:09:47 +04:00
|
|
|
return None, message, user, date, branch, None, None, None
|
2007-03-22 20:44:59 +03:00
|
|
|
p1 = parents and parents.pop(0) or None
|
|
|
|
p2 = parents and parents.pop(0) or None
|
2007-05-18 07:09:47 +04:00
|
|
|
return tmpname, message, user, date, branch, nodeid, p1, p2
|
2006-08-12 02:50:16 +04:00
|
|
|
|
2009-06-10 17:10:21 +04:00
|
|
|
class patchmeta(object):
|
2008-10-19 01:45:45 +04:00
|
|
|
"""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
|
2008-10-19 01:45:45 +04:00
|
|
|
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.
|
2008-10-19 01:45:45 +04:00
|
|
|
"""
|
|
|
|
def __init__(self, path):
|
|
|
|
self.path = path
|
|
|
|
self.oldpath = None
|
|
|
|
self.mode = None
|
|
|
|
self.op = 'MODIFY'
|
|
|
|
self.binary = False
|
|
|
|
|
2008-10-19 01:45:45 +04:00
|
|
|
def setmode(self, mode):
|
|
|
|
islink = mode & 020000
|
|
|
|
isexec = mode & 0100
|
|
|
|
self.mode = (islink, isexec)
|
|
|
|
|
2011-06-11 16:17:25 +04:00
|
|
|
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
|
|
|
|
|
patch: fix patch hunk/metdata synchronization (issue3384)
Git patches are parsed in two phases: 1) extract metadata, 2) parse actual
deltas and merge them with the previous metadata. We do this to avoid
dependency issues like "modify a; copy a to b", where "b" must be copied from
the unmodified "a".
Issue3384 is caused by flaky code I wrote to synchronize the patch metadata
with the emitted hunk:
if (gitpatches and
(gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)):
gp = gitpatches.pop()[2]
With a patch like:
diff --git a/a b/c
copy from a
copy to c
--- a/a
+++ b/c
@@ -1,1 +1,2 @@
a
+a
@@ -2,1 +2,2 @@
a
+a
diff --git a/a b/a
--- a/a
+++ b/a
@@ -1,1 +1,2 @@
a
+b
the first hunk of the first block is matched with the metadata for the block
"diff --git a/a b/c", then the second hunk of the first block is matched with
the metadata of the second block "diff --git a/a b/a", because of the "or" in
the code paste above. Turning the "or" into an "and" is not enough as we have
to deal with /dev/null cases for each file.
We I remove this broken piece of code:
# copy/rename + modify should modify target, not source
if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode:
afile = bfile
because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file",
and because this only happens for git patches, which afile/bfile are ignored
anyway by applydiff().
v2:
- Avoid a traceback on git metadata desynchronization
2012-04-21 23:40:25 +04:00
|
|
|
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)
|
|
|
|
|
2010-04-26 15:21:02 +04:00
|
|
|
def __repr__(self):
|
|
|
|
return "<patchmeta %s %r>" % (self.op, self.path)
|
|
|
|
|
2008-10-19 01:45:46 +04:00
|
|
|
def readgitpatch(lr):
|
2006-08-12 02:50:16 +04:00
|
|
|
"""extract git-style metadata about patches from <patchname>"""
|
2006-10-01 21:26:33 +04:00
|
|
|
|
2006-08-12 02:50:16 +04:00
|
|
|
# Filter patch for git information
|
|
|
|
gp = None
|
|
|
|
gitpatches = []
|
2008-10-19 01:45:46 +04:00
|
|
|
for line in lr:
|
2009-07-25 01:53:40 +04:00
|
|
|
line = line.rstrip(' \r\n')
|
2006-08-12 02:50:16 +04:00
|
|
|
if line.startswith('diff --git'):
|
|
|
|
m = gitre.match(line)
|
|
|
|
if m:
|
|
|
|
if gp:
|
|
|
|
gitpatches.append(gp)
|
2009-08-24 16:40:21 +04:00
|
|
|
dst = m.group(2)
|
2008-10-19 01:45:45 +04:00
|
|
|
gp = patchmeta(dst)
|
2006-08-12 02:50:16 +04:00
|
|
|
elif gp:
|
|
|
|
if line.startswith('--- '):
|
|
|
|
gitpatches.append(gp)
|
|
|
|
gp = None
|
|
|
|
continue
|
|
|
|
if line.startswith('rename from '):
|
|
|
|
gp.op = 'RENAME'
|
2009-07-25 01:53:40 +04:00
|
|
|
gp.oldpath = line[12:]
|
2006-08-12 02:50:16 +04:00
|
|
|
elif line.startswith('rename to '):
|
2009-07-25 01:53:40 +04:00
|
|
|
gp.path = line[10:]
|
2006-08-12 02:50:16 +04:00
|
|
|
elif line.startswith('copy from '):
|
|
|
|
gp.op = 'COPY'
|
2009-07-25 01:53:40 +04:00
|
|
|
gp.oldpath = line[10:]
|
2006-08-12 02:50:16 +04:00
|
|
|
elif line.startswith('copy to '):
|
2009-07-25 01:53:40 +04:00
|
|
|
gp.path = line[8:]
|
2006-08-12 02:50:16 +04:00
|
|
|
elif line.startswith('deleted file'):
|
|
|
|
gp.op = 'DELETE'
|
|
|
|
elif line.startswith('new file mode '):
|
|
|
|
gp.op = 'ADD'
|
2009-07-25 01:53:40 +04:00
|
|
|
gp.setmode(int(line[-6:], 8))
|
2006-08-12 02:50:16 +04:00
|
|
|
elif line.startswith('new mode '):
|
2009-07-25 01:53:40 +04:00
|
|
|
gp.setmode(int(line[-6:], 8))
|
2006-10-12 20:17:16 +04:00
|
|
|
elif line.startswith('GIT binary patch'):
|
|
|
|
gp.binary = True
|
2006-08-12 02:50:16 +04:00
|
|
|
if gp:
|
|
|
|
gitpatches.append(gp)
|
|
|
|
|
2010-10-10 00:13:08 +04:00
|
|
|
return gitpatches
|
2006-08-12 02:50:16 +04:00
|
|
|
|
2009-06-22 14:05:11 +04:00
|
|
|
class linereader(object):
|
2009-06-15 02:03:26 +04:00
|
|
|
# simple class to allow pushing lines back into the input stream
|
2011-05-24 16:21:04 +04:00
|
|
|
def __init__(self, fp):
|
2009-06-15 02:03:26 +04:00
|
|
|
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
|
2011-05-24 16:21:04 +04:00
|
|
|
return self.fp.readline()
|
2009-06-15 02:03:26 +04:00
|
|
|
|
|
|
|
def __iter__(self):
|
2011-06-01 14:38:46 +04:00
|
|
|
while True:
|
2009-06-15 02:03:26 +04:00
|
|
|
l = self.readline()
|
|
|
|
if not l:
|
|
|
|
break
|
|
|
|
yield l
|
|
|
|
|
2011-05-18 01:46:15 +04:00
|
|
|
class abstractbackend(object):
|
|
|
|
def __init__(self, ui):
|
2007-07-17 20:39:30 +04:00
|
|
|
self.ui = ui
|
2007-12-18 01:42:46 +03:00
|
|
|
|
2011-05-20 00:49:43 +04:00
|
|
|
def getfile(self, fname):
|
|
|
|
"""Return target file data and flags as a (data, (islink,
|
|
|
|
isexec)) tuple.
|
2011-05-18 01:46:15 +04:00
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2011-05-27 23:50:10 +04:00
|
|
|
def setfile(self, fname, data, mode, copysource):
|
2011-05-20 00:49:43 +04:00
|
|
|
"""Write data to target file fname and set its mode. mode is a
|
|
|
|
(islink, isexec) tuple. If data is None, the file content should
|
2011-05-27 23:50:10 +04:00
|
|
|
be left unchanged. If the file is modified after being copied,
|
|
|
|
copysource is set to the original file name.
|
2011-05-19 01:48:13 +04:00
|
|
|
"""
|
2011-05-18 01:46:15 +04:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2011-05-18 01:46:38 +04:00
|
|
|
def exists(self, fname):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2011-05-18 01:46:15 +04:00
|
|
|
class fsbackend(abstractbackend):
|
2011-05-18 01:46:38 +04:00
|
|
|
def __init__(self, ui, basedir):
|
2011-05-18 01:46:15 +04:00
|
|
|
super(fsbackend, self).__init__(ui)
|
2011-05-18 01:46:38 +04:00
|
|
|
self.opener = scmutil.opener(basedir)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2011-05-19 01:48:13 +04:00
|
|
|
def _join(self, f):
|
|
|
|
return os.path.join(self.opener.base, f)
|
|
|
|
|
2011-05-20 00:49:43 +04:00
|
|
|
def getfile(self, fname):
|
|
|
|
path = self._join(fname)
|
|
|
|
if os.path.islink(path):
|
|
|
|
return (os.readlink(path), (True, False))
|
2011-06-05 15:27:06 +04:00
|
|
|
isexec = False
|
2008-11-19 15:27:57 +03:00
|
|
|
try:
|
2011-05-20 00:49:43 +04:00
|
|
|
isexec = os.lstat(path).st_mode & 0100 != 0
|
|
|
|
except OSError, e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
2011-06-05 15:27:06 +04:00
|
|
|
return (self.opener.read(fname), (False, isexec))
|
2011-05-20 00:49:43 +04:00
|
|
|
|
2011-05-27 23:50:10 +04:00
|
|
|
def setfile(self, fname, data, mode, copysource):
|
2011-05-20 00:49:43 +04:00
|
|
|
islink, isexec = mode
|
|
|
|
if data is None:
|
|
|
|
util.setflags(self._join(fname), islink, isexec)
|
2011-05-20 00:44:01 +04:00
|
|
|
return
|
2011-05-19 01:48:13 +04:00
|
|
|
if islink:
|
2011-05-20 00:49:43 +04:00
|
|
|
self.opener.symlink(data, fname)
|
2011-05-19 01:48:13 +04:00
|
|
|
else:
|
2011-05-20 00:49:43 +04:00
|
|
|
self.opener.write(fname, data)
|
2011-05-19 01:48:13 +04:00
|
|
|
if isexec:
|
|
|
|
util.setflags(self._join(fname), False, True)
|
2008-11-19 15:27:57 +03:00
|
|
|
|
|
|
|
def unlink(self, fname):
|
2011-05-19 01:48:13 +04:00
|
|
|
try:
|
|
|
|
util.unlinkpath(self._join(fname))
|
|
|
|
except OSError, inst:
|
|
|
|
if inst.errno != errno.ENOENT:
|
|
|
|
raise
|
2008-11-19 15:27:57 +03:00
|
|
|
|
2011-05-18 01:46:15 +04:00
|
|
|
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()
|
|
|
|
|
2011-05-18 01:46:38 +04:00
|
|
|
def exists(self, fname):
|
2011-05-19 01:48:13 +04:00
|
|
|
return os.path.lexists(self._join(fname))
|
2011-05-18 01:46:38 +04:00
|
|
|
|
2011-05-19 01:48:17 +04:00
|
|
|
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 = []
|
|
|
|
|
2011-05-27 23:50:11 +04:00
|
|
|
def _checkknown(self, fname):
|
|
|
|
if self.repo.dirstate[fname] == '?' and self.exists(fname):
|
|
|
|
raise PatchError(_('cannot patch %s: file is not tracked') % fname)
|
|
|
|
|
2011-05-27 23:50:10 +04:00
|
|
|
def setfile(self, fname, data, mode, copysource):
|
2011-05-27 23:50:11 +04:00
|
|
|
self._checkknown(fname)
|
2011-05-27 23:50:10 +04:00
|
|
|
super(workingbackend, self).setfile(fname, data, mode, copysource)
|
|
|
|
if copysource is not None:
|
|
|
|
self.copied.append((copysource, fname))
|
2011-05-19 01:48:17 +04:00
|
|
|
self.changed.add(fname)
|
|
|
|
|
|
|
|
def unlink(self, fname):
|
2011-05-27 23:50:11 +04:00
|
|
|
self._checkknown(fname)
|
2011-05-19 01:48:17 +04:00
|
|
|
super(workingbackend, self).unlink(fname)
|
|
|
|
self.removed.add(fname)
|
|
|
|
self.changed.add(fname)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
wctx = self.repo[None]
|
|
|
|
addremoved = set(self.changed)
|
|
|
|
for src, dst in self.copied:
|
|
|
|
scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst)
|
2012-02-16 16:03:42 +04:00
|
|
|
if self.removed:
|
2011-05-27 02:15:35 +04:00
|
|
|
wctx.forget(sorted(self.removed))
|
2012-02-16 16:03:42 +04:00
|
|
|
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
|
|
|
|
# addremove().
|
|
|
|
addremoved.discard(f)
|
2011-05-19 01:48:17 +04:00
|
|
|
if addremoved:
|
|
|
|
cwd = self.repo.getcwd()
|
|
|
|
if cwd:
|
|
|
|
addremoved = [util.pathto(self.repo.root, cwd, f)
|
|
|
|
for f in addremoved]
|
|
|
|
scmutil.addremove(self.repo, addremoved, similarity=self.similarity)
|
|
|
|
return sorted(self.changed)
|
|
|
|
|
2011-05-27 23:50:10 +04:00
|
|
|
class filestore(object):
|
2011-06-17 22:33:02 +04:00
|
|
|
def __init__(self, maxsize=None):
|
2011-05-27 23:50:10 +04:00
|
|
|
self.opener = None
|
|
|
|
self.files = {}
|
|
|
|
self.created = 0
|
2011-06-17 22:33:02 +04:00
|
|
|
self.maxsize = maxsize
|
|
|
|
if self.maxsize is None:
|
|
|
|
self.maxsize = 4*(2**20)
|
|
|
|
self.size = 0
|
|
|
|
self.data = {}
|
2011-05-27 23:50:10 +04:00
|
|
|
|
2011-06-15 01:24:34 +04:00
|
|
|
def setfile(self, fname, data, mode, copied=None):
|
2011-06-17 22:33:02 +04:00
|
|
|
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)
|
2011-05-27 23:50:10 +04:00
|
|
|
|
|
|
|
def getfile(self, fname):
|
2011-06-17 22:33:02 +04:00
|
|
|
if fname in self.data:
|
|
|
|
return self.data[fname]
|
|
|
|
if not self.opener or fname not in self.files:
|
2012-05-12 18:00:58 +04:00
|
|
|
raise IOError
|
2011-06-15 01:24:34 +04:00
|
|
|
fn, mode, copied = self.files[fname]
|
|
|
|
return self.opener.read(fn), mode, copied
|
2011-05-27 23:50:10 +04:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.opener:
|
|
|
|
shutil.rmtree(self.opener.base)
|
|
|
|
|
2011-06-15 01:26:35 +04:00
|
|
|
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:
|
2012-05-12 18:00:58 +04:00
|
|
|
raise IOError
|
2011-06-15 01:26:35 +04:00
|
|
|
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
|
|
|
|
|
2011-05-18 01:46:15 +04:00
|
|
|
# @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
|
2011-11-14 21:16:01 +04:00
|
|
|
unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
|
|
|
|
contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
|
2011-05-18 01:46:15 +04:00
|
|
|
eolmodes = ['strict', 'crlf', 'lf', 'auto']
|
|
|
|
|
|
|
|
class patchfile(object):
|
2011-06-11 16:17:25 +04:00
|
|
|
def __init__(self, ui, gp, backend, store, eolmode='strict'):
|
|
|
|
self.fname = gp.path
|
2011-05-18 01:46:15 +04:00
|
|
|
self.eolmode = eolmode
|
|
|
|
self.eol = None
|
|
|
|
self.backend = backend
|
|
|
|
self.ui = ui
|
|
|
|
self.lines = []
|
|
|
|
self.exists = False
|
2011-05-27 23:50:10 +04:00
|
|
|
self.missing = True
|
2011-06-11 16:17:25 +04:00
|
|
|
self.mode = gp.mode
|
|
|
|
self.copysource = gp.oldpath
|
|
|
|
self.create = gp.op in ('ADD', 'COPY', 'RENAME')
|
|
|
|
self.remove = gp.op == 'DELETE'
|
2011-05-27 23:50:10 +04:00
|
|
|
try:
|
2011-06-11 16:17:25 +04:00
|
|
|
if self.copysource is None:
|
|
|
|
data, mode = backend.getfile(self.fname)
|
2011-05-18 01:46:15 +04:00
|
|
|
self.exists = True
|
2011-05-27 23:50:10 +04:00
|
|
|
else:
|
2011-06-15 01:24:34 +04:00
|
|
|
data, mode = store.getfile(self.copysource)[:2]
|
2011-06-11 16:17:25 +04:00
|
|
|
self.exists = backend.exists(self.fname)
|
2011-05-27 23:50:10 +04:00
|
|
|
self.missing = False
|
|
|
|
if data:
|
2011-07-05 02:53:39 +04:00
|
|
|
self.lines = mdiff.splitnewlines(data)
|
2011-05-27 23:50:10 +04:00
|
|
|
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
|
|
|
|
except IOError:
|
2011-06-11 16:17:25 +04:00
|
|
|
if self.create:
|
2011-05-27 23:50:10 +04:00
|
|
|
self.missing = False
|
|
|
|
if self.mode is None:
|
|
|
|
self.mode = (False, False)
|
|
|
|
if self.missing:
|
2012-07-31 05:30:42 +04:00
|
|
|
self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
|
2011-05-18 01:46:15 +04:00
|
|
|
|
|
|
|
self.hash = {}
|
|
|
|
self.dirty = 0
|
|
|
|
self.offset = 0
|
|
|
|
self.skew = 0
|
|
|
|
self.rej = []
|
|
|
|
self.fileprinted = False
|
|
|
|
self.printfile(False)
|
|
|
|
self.hunks = 0
|
|
|
|
|
2011-05-19 01:48:13 +04:00
|
|
|
def writelines(self, fname, lines, mode):
|
2011-05-18 01:46:15 +04:00
|
|
|
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
|
|
|
|
|
2011-05-27 23:50:10 +04:00
|
|
|
self.backend.setfile(fname, ''.join(lines), mode, self.copysource)
|
2011-05-18 01:46:15 +04:00
|
|
|
|
2007-07-17 20:39:30 +04:00
|
|
|
def printfile(self, warn):
|
|
|
|
if self.fileprinted:
|
|
|
|
return
|
|
|
|
if warn or self.ui.verbose:
|
|
|
|
self.fileprinted = True
|
2007-07-17 20:39:30 +04:00
|
|
|
s = _("patching file %s\n") % self.fname
|
2007-07-17 20:39:30 +04:00
|
|
|
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
|
2007-08-07 12:28:43 +04:00
|
|
|
|
2009-10-31 20:00:05 +03:00
|
|
|
cand = self.hash.get(l, [])
|
2007-07-17 20:39:30 +04:00
|
|
|
if len(cand) > 1:
|
|
|
|
# resort our list of potentials forward then back.
|
2009-07-05 13:02:00 +04:00
|
|
|
cand.sort(key=lambda x: abs(x - linenum))
|
2007-07-17 20:39:30 +04:00
|
|
|
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
|
2011-05-18 01:46:37 +04:00
|
|
|
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)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2009-08-23 16:32:58 +04:00
|
|
|
def apply(self, h):
|
2007-07-17 20:39:30 +04:00
|
|
|
if not h.complete():
|
2007-07-17 20:39:30 +04:00
|
|
|
raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") %
|
2007-07-17 20:39:30 +04:00
|
|
|
(h.number, h.desc, len(h.a), h.lena, len(h.b),
|
|
|
|
h.lenb))
|
|
|
|
|
|
|
|
self.hunks += 1
|
|
|
|
|
2007-12-18 01:42:46 +03:00
|
|
|
if self.missing:
|
|
|
|
self.rej.append(h)
|
|
|
|
return -1
|
|
|
|
|
2011-05-27 23:50:09 +04:00
|
|
|
if self.exists and self.create:
|
2011-05-27 23:50:10 +04:00
|
|
|
if self.copysource:
|
|
|
|
self.ui.warn(_("cannot create %s: destination already "
|
|
|
|
"exists\n" % self.fname))
|
|
|
|
else:
|
|
|
|
self.ui.warn(_("file %s already exists\n") % self.fname)
|
2007-07-17 20:39:30 +04:00
|
|
|
self.rej.append(h)
|
|
|
|
return -1
|
|
|
|
|
2009-10-16 01:15:30 +04:00
|
|
|
if isinstance(h, binhunk):
|
2011-05-27 23:50:09 +04:00
|
|
|
if self.remove:
|
2011-05-18 01:46:15 +04:00
|
|
|
self.backend.unlink(self.fname)
|
2007-07-17 20:39:30 +04:00
|
|
|
else:
|
|
|
|
self.lines[:] = h.new()
|
|
|
|
self.offset += len(h.new())
|
2011-05-06 13:12:55 +04:00
|
|
|
self.dirty = True
|
2007-07-17 20:39:30 +04:00
|
|
|
return 0
|
|
|
|
|
2009-12-23 21:31:47 +03:00
|
|
|
horig = h
|
2009-12-23 21:31:48 +03:00
|
|
|
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.
|
2009-12-23 21:31:47 +03:00
|
|
|
h = h.getnormalized()
|
|
|
|
|
2007-07-17 20:39:30 +04:00
|
|
|
# fast case first, no offsets, no fuzz
|
2012-02-13 16:51:38 +04:00
|
|
|
old, oldstart, new, newstart = h.fuzzit(0, False)
|
|
|
|
oldstart += self.offset
|
|
|
|
orig_start = oldstart
|
2009-12-10 03:56:00 +03:00
|
|
|
# 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
|
2012-02-13 16:51:38 +04:00
|
|
|
if (self.skew == 0 and
|
|
|
|
diffhelpers.testhunk(old, self.lines, oldstart) == 0):
|
2011-05-27 23:50:09 +04:00
|
|
|
if self.remove:
|
2011-05-18 01:46:15 +04:00
|
|
|
self.backend.unlink(self.fname)
|
2007-07-17 20:39:30 +04:00
|
|
|
else:
|
2012-02-13 16:51:38 +04:00
|
|
|
self.lines[oldstart:oldstart + len(old)] = new
|
|
|
|
self.offset += len(new) - len(old)
|
2011-05-06 13:12:55 +04:00
|
|
|
self.dirty = True
|
2007-07-17 20:39:30 +04:00
|
|
|
return 0
|
|
|
|
|
2011-03-20 02:09:44 +03:00
|
|
|
# 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)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
|
|
|
for fuzzlen in xrange(3):
|
2010-01-25 09:05:27 +03:00
|
|
|
for toponly in [True, False]:
|
2012-02-13 16:51:38 +04:00
|
|
|
old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly)
|
2012-02-13 19:47:31 +04:00
|
|
|
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]
|
2007-07-17 20:39:30 +04:00
|
|
|
|
|
|
|
for l in cand:
|
2012-02-13 19:47:31 +04:00
|
|
|
if not old or diffhelpers.testhunk(old, self.lines, l) == 0:
|
2012-02-13 16:21:00 +04:00
|
|
|
self.lines[l : l + len(old)] = new
|
|
|
|
self.offset += len(new) - len(old)
|
2009-12-10 03:56:00 +03:00
|
|
|
self.skew = l - orig_start
|
2011-05-06 13:12:55 +04:00
|
|
|
self.dirty = True
|
2010-02-19 18:04:11 +03:00
|
|
|
offset = l - orig_start - fuzzlen
|
2007-07-17 20:39:30 +04:00
|
|
|
if fuzzlen:
|
2010-02-19 18:04:11 +03:00
|
|
|
msg = _("Hunk #%d succeeded at %d "
|
|
|
|
"with fuzz %d "
|
|
|
|
"(offset %d lines).\n")
|
2007-07-17 20:39:30 +04:00
|
|
|
self.printfile(True)
|
2010-02-19 18:04:11 +03:00
|
|
|
self.ui.warn(msg %
|
|
|
|
(h.number, l + 1, fuzzlen, offset))
|
2007-07-17 20:39:30 +04:00
|
|
|
else:
|
2010-02-19 18:04:11 +03:00
|
|
|
msg = _("Hunk #%d succeeded at %d "
|
2009-04-21 04:16:14 +04:00
|
|
|
"(offset %d lines).\n")
|
2010-02-19 18:04:11 +03:00
|
|
|
self.ui.note(msg % (h.number, l + 1, offset))
|
2007-07-17 20:39:30 +04:00
|
|
|
return fuzzlen
|
|
|
|
self.printfile(True)
|
2007-07-17 20:39:30 +04:00
|
|
|
self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
|
2009-12-23 21:31:47 +03:00
|
|
|
self.rej.append(horig)
|
2007-07-17 20:39:30 +04:00
|
|
|
return -1
|
|
|
|
|
2011-03-20 02:22:47 +03:00
|
|
|
def close(self):
|
|
|
|
if self.dirty:
|
2011-05-19 01:48:13 +04:00
|
|
|
self.writelines(self.fname, self.lines, self.mode)
|
2011-03-20 02:22:47 +03:00
|
|
|
self.write_rej()
|
|
|
|
return len(self.rej)
|
|
|
|
|
2009-06-10 17:10:21 +04:00
|
|
|
class hunk(object):
|
2011-05-27 23:50:09 +04:00
|
|
|
def __init__(self, desc, num, lr, context):
|
2007-07-17 20:39:30 +04:00
|
|
|
self.number = num
|
|
|
|
self.desc = desc
|
2010-01-25 09:05:27 +03:00
|
|
|
self.hunk = [desc]
|
2007-07-17 20:39:30 +04:00
|
|
|
self.a = []
|
|
|
|
self.b = []
|
2009-10-31 20:01:08 +03:00
|
|
|
self.starta = self.lena = None
|
|
|
|
self.startb = self.lenb = None
|
2009-12-23 21:31:47 +03:00
|
|
|
if lr is not None:
|
|
|
|
if context:
|
|
|
|
self.read_context_hunk(lr)
|
|
|
|
else:
|
|
|
|
self.read_unified_hunk(lr)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2009-12-23 21:31:47 +03:00
|
|
|
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
|
2011-05-27 23:50:09 +04:00
|
|
|
nh = hunk(self.desc, self.number, None, None)
|
2009-12-23 21:31:47 +03:00
|
|
|
nh.number = self.number
|
|
|
|
nh.desc = self.desc
|
2010-02-23 00:55:58 +03:00
|
|
|
nh.hunk = self.hunk
|
2009-12-23 21:31:47 +03:00
|
|
|
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
|
|
|
|
|
2007-07-17 20:39:30 +04:00
|
|
|
def read_unified_hunk(self, lr):
|
|
|
|
m = unidesc.match(self.desc)
|
|
|
|
if not m:
|
2007-07-17 20:39:30 +04:00
|
|
|
raise PatchError(_("bad hunk #%d") % self.number)
|
2011-11-14 21:16:01 +04:00
|
|
|
self.starta, self.lena, self.startb, self.lenb = m.groups()
|
2009-05-20 02:52:46 +04:00
|
|
|
if self.lena is None:
|
2007-07-17 20:39:30 +04:00
|
|
|
self.lena = 1
|
|
|
|
else:
|
|
|
|
self.lena = int(self.lena)
|
2009-05-20 02:52:46 +04:00
|
|
|
if self.lenb is None:
|
2007-07-17 20:39:30 +04:00
|
|
|
self.lenb = 1
|
|
|
|
else:
|
|
|
|
self.lenb = int(self.lenb)
|
|
|
|
self.starta = int(self.starta)
|
|
|
|
self.startb = int(self.startb)
|
2012-05-12 17:54:54 +04:00
|
|
|
diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a,
|
|
|
|
self.b)
|
2007-07-17 20:39:30 +04:00
|
|
|
# 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:
|
2008-08-31 13:41:52 +04:00
|
|
|
del self.hunk[-1]
|
|
|
|
del self.a[-1]
|
|
|
|
del self.b[-1]
|
|
|
|
self.lena -= 1
|
|
|
|
self.lenb -= 1
|
2011-03-20 02:08:44 +03:00
|
|
|
self._fixnewline(lr)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
|
|
|
def read_context_hunk(self, lr):
|
|
|
|
self.desc = lr.readline()
|
|
|
|
m = contextdesc.match(self.desc)
|
|
|
|
if not m:
|
2007-07-17 20:39:30 +04:00
|
|
|
raise PatchError(_("bad hunk #%d") % self.number)
|
2011-11-14 21:16:01 +04:00
|
|
|
self.starta, aend = m.groups()
|
2007-07-17 20:39:30 +04:00
|
|
|
self.starta = int(self.starta)
|
2009-05-20 02:52:46 +04:00
|
|
|
if aend is None:
|
2007-07-17 20:39:30 +04:00
|
|
|
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('---'):
|
2010-10-24 14:56:38 +04:00
|
|
|
# lines addition, old block is empty
|
2007-07-17 20:39:30 +04:00
|
|
|
lr.push(l)
|
|
|
|
break
|
|
|
|
s = l[2:]
|
|
|
|
if l.startswith('- ') or l.startswith('! '):
|
|
|
|
u = '-' + s
|
|
|
|
elif l.startswith(' '):
|
|
|
|
u = ' ' + s
|
|
|
|
else:
|
2007-07-17 20:39:30 +04:00
|
|
|
raise PatchError(_("bad hunk #%d old text line %d") %
|
|
|
|
(self.number, x))
|
2007-07-17 20:39:30 +04:00
|
|
|
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:
|
2007-07-17 20:39:30 +04:00
|
|
|
raise PatchError(_("bad hunk #%d") % self.number)
|
2011-11-14 21:16:01 +04:00
|
|
|
self.startb, bend = m.groups()
|
2007-07-17 20:39:30 +04:00
|
|
|
self.startb = int(self.startb)
|
2009-05-20 02:52:46 +04:00
|
|
|
if bend is None:
|
2007-07-17 20:39:30 +04:00
|
|
|
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('\ '):
|
2010-10-24 14:56:38 +04:00
|
|
|
# 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.
|
2007-07-17 20:39:30 +04:00
|
|
|
s = self.b[-1][:-1]
|
|
|
|
self.b[-1] = s
|
2010-01-25 09:05:27 +03:00
|
|
|
self.hunk[hunki - 1] = s
|
2007-07-17 20:39:30 +04:00
|
|
|
continue
|
|
|
|
if not l:
|
2010-10-24 14:56:38 +04:00
|
|
|
# line deletions, new block is empty and we hit EOF
|
2007-07-17 20:39:30 +04:00
|
|
|
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:
|
2010-10-24 14:56:38 +04:00
|
|
|
# line deletions, new block is empty
|
2007-07-17 20:39:30 +04:00
|
|
|
lr.push(l)
|
|
|
|
break
|
|
|
|
else:
|
2007-07-17 20:39:30 +04:00
|
|
|
raise PatchError(_("bad hunk #%d old text line %d") %
|
|
|
|
(self.number, x))
|
2007-07-17 20:39:30 +04:00
|
|
|
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:
|
2010-01-25 09:05:27 +03:00
|
|
|
self.hunk.insert(hunki - 1, u)
|
2007-07-17 20:39:30 +04:00
|
|
|
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
|
2011-03-20 02:08:44 +03:00
|
|
|
self._fixnewline(lr)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2011-03-20 02:08:44 +03:00
|
|
|
def _fixnewline(self, lr):
|
|
|
|
l = lr.readline()
|
|
|
|
if l.startswith('\ '):
|
|
|
|
diffhelpers.fix_newline(self.hunk, self.a, self.b)
|
|
|
|
else:
|
|
|
|
lr.push(l)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
|
|
|
def complete(self):
|
|
|
|
return len(self.a) == self.lena and len(self.b) == self.lenb
|
|
|
|
|
2012-02-13 16:21:00 +04:00
|
|
|
def _fuzzit(self, old, new, fuzz, toponly):
|
2007-07-17 20:39:30 +04:00
|
|
|
# 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.
|
2012-02-13 20:22:35 +04:00
|
|
|
fuzz = min(fuzz, len(old))
|
2007-07-17 20:39:30 +04:00
|
|
|
if fuzz:
|
|
|
|
top = 0
|
|
|
|
bot = 0
|
|
|
|
hlen = len(self.hunk)
|
2010-01-25 09:05:27 +03:00
|
|
|
for x in xrange(hlen - 1):
|
2007-07-17 20:39:30 +04:00
|
|
|
# the hunk starts with the @@ line, so use x+1
|
2010-01-25 09:05:27 +03:00
|
|
|
if self.hunk[x + 1][0] == ' ':
|
2007-07-17 20:39:30 +04:00
|
|
|
top += 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
if not toponly:
|
2010-01-25 09:05:27 +03:00
|
|
|
for x in xrange(hlen - 1):
|
|
|
|
if self.hunk[hlen - bot - 1][0] == ' ':
|
2007-07-17 20:39:30 +04:00
|
|
|
bot += 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2012-02-13 20:22:35 +04:00
|
|
|
bot = min(fuzz, bot)
|
|
|
|
top = min(fuzz, top)
|
2012-02-13 16:51:38 +04:00
|
|
|
return old[top:len(old)-bot], new[top:len(new)-bot], top
|
|
|
|
return old, new, 0
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2012-02-13 16:21:00 +04:00
|
|
|
def fuzzit(self, fuzz, toponly):
|
2012-02-13 16:51:38 +04:00
|
|
|
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
|
2012-05-12 11:10:01 +04:00
|
|
|
if self.lena and oldstart > 0:
|
2012-02-13 16:51:38 +04:00
|
|
|
oldstart -= 1
|
2012-05-12 11:10:01 +04:00
|
|
|
if self.lenb and newstart > 0:
|
2012-02-13 16:51:38 +04:00
|
|
|
newstart -= 1
|
|
|
|
return old, oldstart, new, newstart
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2011-06-29 17:00:00 +04:00
|
|
|
class binhunk(object):
|
2009-10-16 01:15:30 +04:00
|
|
|
'A binary patch file. Only understands literals so far.'
|
2012-04-26 23:44:00 +04:00
|
|
|
def __init__(self, lr, fname):
|
2007-07-17 20:39:30 +04:00
|
|
|
self.text = None
|
2009-10-16 01:15:30 +04:00
|
|
|
self.hunk = ['GIT binary patch\n']
|
2012-04-26 23:44:00 +04:00
|
|
|
self._fname = fname
|
2011-05-20 00:44:01 +04:00
|
|
|
self._read(lr)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
|
|
|
def complete(self):
|
|
|
|
return self.text is not None
|
|
|
|
|
|
|
|
def new(self):
|
|
|
|
return [self.text]
|
|
|
|
|
2011-05-20 00:44:01 +04:00
|
|
|
def _read(self, lr):
|
2012-04-26 23:44:02 +04:00
|
|
|
def getline(lr, hunk):
|
|
|
|
l = lr.readline()
|
|
|
|
hunk.append(l)
|
|
|
|
return l.rstrip('\r\n')
|
|
|
|
|
2012-04-29 13:19:51 +04:00
|
|
|
while True:
|
2012-04-26 23:44:02 +04:00
|
|
|
line = getline(lr, self.hunk)
|
2012-04-29 13:19:51 +04:00
|
|
|
if not line:
|
|
|
|
raise PatchError(_('could not extract "%s" binary data')
|
|
|
|
% self._fname)
|
|
|
|
if line.startswith('literal '):
|
|
|
|
break
|
2007-07-17 20:39:30 +04:00
|
|
|
size = int(line[8:].rstrip())
|
2006-10-12 20:17:16 +04:00
|
|
|
dec = []
|
2012-04-26 23:44:02 +04:00
|
|
|
line = getline(lr, self.hunk)
|
2007-07-17 20:39:30 +04:00
|
|
|
while len(line) > 1:
|
2006-10-13 00:39:14 +04:00
|
|
|
l = line[0]
|
|
|
|
if l <= 'Z' and l >= 'A':
|
|
|
|
l = ord(l) - ord('A') + 1
|
|
|
|
else:
|
|
|
|
l = ord(l) - ord('a') + 27
|
2012-04-21 21:58:18 +04:00
|
|
|
try:
|
2012-04-26 23:44:02 +04:00
|
|
|
dec.append(base85.b85decode(line[1:])[:l])
|
2012-04-21 21:58:18 +04:00
|
|
|
except ValueError, e:
|
2012-04-26 23:44:00 +04:00
|
|
|
raise PatchError(_('could not decode "%s" binary patch: %s')
|
|
|
|
% (self._fname, str(e)))
|
2012-04-26 23:44:02 +04:00
|
|
|
line = getline(lr, self.hunk)
|
2006-10-12 20:17:16 +04:00
|
|
|
text = zlib.decompress(''.join(dec))
|
|
|
|
if len(text) != size:
|
2012-04-26 23:44:00 +04:00
|
|
|
raise PatchError(_('"%s" length is %d bytes, should be %d')
|
|
|
|
% (self._fname, len(text), size))
|
2007-07-17 20:39:30 +04:00
|
|
|
self.text = text
|
|
|
|
|
|
|
|
def parsefilename(str):
|
|
|
|
# --- filename \t|space stuff
|
2008-01-12 22:43:09 +03:00
|
|
|
s = str[4:].rstrip('\r\n')
|
2007-07-17 20:39:30 +04:00
|
|
|
i = s.find('\t')
|
|
|
|
if i < 0:
|
|
|
|
i = s.find(' ')
|
|
|
|
if i < 0:
|
|
|
|
return s
|
|
|
|
return s[:i]
|
|
|
|
|
2010-04-26 15:21:03 +04:00
|
|
|
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] == '/':
|
2007-07-17 20:39:30 +04:00
|
|
|
i += 1
|
2010-04-26 15:21:03 +04:00
|
|
|
count -= 1
|
|
|
|
return path[:i].lstrip(), path[i:].rstrip()
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2011-06-11 16:17:25 +04:00
|
|
|
def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip):
|
2007-07-17 20:39:30 +04:00
|
|
|
nulla = afile_orig == "/dev/null"
|
|
|
|
nullb = bfile_orig == "/dev/null"
|
2011-05-27 23:50:09 +04:00
|
|
|
create = nulla and hunk.starta == 0 and hunk.lena == 0
|
|
|
|
remove = nullb and hunk.startb == 0 and hunk.lenb == 0
|
2008-03-18 01:36:45 +03:00
|
|
|
abase, afile = pathstrip(afile_orig, strip)
|
2011-05-18 01:46:38 +04:00
|
|
|
gooda = not nulla and backend.exists(afile)
|
2008-03-18 01:36:45 +03:00
|
|
|
bbase, bfile = pathstrip(bfile_orig, strip)
|
2007-07-17 20:39:30 +04:00
|
|
|
if afile == bfile:
|
|
|
|
goodb = gooda
|
|
|
|
else:
|
2011-05-18 01:46:38 +04:00
|
|
|
goodb = not nullb and backend.exists(bfile)
|
2011-05-27 23:50:09 +04:00
|
|
|
missing = not goodb and not gooda and not create
|
2009-08-08 06:27:54 +04:00
|
|
|
|
2010-08-12 19:58:03 +04:00
|
|
|
# some diff programs apparently produce patches where the afile is
|
|
|
|
# not /dev/null, but afile starts with bfile
|
2010-03-20 00:52:38 +03:00
|
|
|
abasedir = afile[:afile.rfind('/') + 1]
|
|
|
|
bbasedir = bfile[:bfile.rfind('/') + 1]
|
2011-05-27 23:50:09 +04:00
|
|
|
if (missing and abasedir == bbasedir and afile.startswith(bfile)
|
|
|
|
and hunk.starta == 0 and hunk.lena == 0):
|
|
|
|
create = True
|
|
|
|
missing = False
|
2009-08-08 06:27:54 +04:00
|
|
|
|
2008-03-18 01:36:45 +03:00
|
|
|
# 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))
|
2007-12-18 01:42:46 +03:00
|
|
|
fname = None
|
|
|
|
if not missing:
|
|
|
|
if gooda and goodb:
|
2008-03-18 01:36:45 +03:00
|
|
|
fname = isbackup and afile or bfile
|
2007-12-18 01:42:46 +03:00
|
|
|
elif gooda:
|
2007-07-17 20:39:30 +04:00
|
|
|
fname = afile
|
2007-12-29 21:49:48 +03:00
|
|
|
|
2007-12-18 01:42:46 +03:00
|
|
|
if not fname:
|
|
|
|
if not nullb:
|
2008-03-18 01:36:45 +03:00
|
|
|
fname = isbackup and afile or bfile
|
2007-12-18 01:42:46 +03:00
|
|
|
elif not nulla:
|
2007-07-17 20:39:30 +04:00
|
|
|
fname = afile
|
2007-12-18 01:42:46 +03:00
|
|
|
else:
|
|
|
|
raise PatchError(_("undefined source and destination files"))
|
2007-12-29 21:49:48 +03:00
|
|
|
|
2011-06-11 16:17:25 +04:00
|
|
|
gp = patchmeta(fname)
|
|
|
|
if create:
|
|
|
|
gp.op = 'ADD'
|
|
|
|
elif remove:
|
|
|
|
gp.op = 'DELETE'
|
|
|
|
return gp
|
2007-07-17 20:39:30 +04:00
|
|
|
|
2008-10-19 01:45:46 +04:00
|
|
|
def scangitpatch(lr, firstline):
|
2008-10-20 16:57:04 +04:00
|
|
|
"""
|
2008-10-19 01:45:46 +04:00
|
|
|
Git patches can emit:
|
|
|
|
- rename a to b
|
|
|
|
- change b
|
|
|
|
- copy a to c
|
|
|
|
- change c
|
2008-10-20 16:57:04 +04:00
|
|
|
|
2008-10-19 01:45:46 +04:00
|
|
|
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())
|
2011-05-24 16:21:04 +04:00
|
|
|
gitlr = linereader(fp)
|
2008-10-19 01:45:46 +04:00
|
|
|
gitlr.push(firstline)
|
2010-10-10 00:13:08 +04:00
|
|
|
gitpatches = readgitpatch(gitlr)
|
2008-10-19 01:45:46 +04:00
|
|
|
fp.seek(pos)
|
2010-10-10 00:13:08 +04:00
|
|
|
return gitpatches
|
2008-10-19 01:45:46 +04:00
|
|
|
|
2011-05-06 19:45:12 +04:00
|
|
|
def iterhunks(fp):
|
2007-12-18 01:06:01 +03:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-07-17 20:39:30 +04:00
|
|
|
afile = ""
|
|
|
|
bfile = ""
|
|
|
|
state = None
|
|
|
|
hunknum = 0
|
2011-04-26 23:22:14 +04:00
|
|
|
emitfile = newfile = False
|
2011-05-20 00:44:01 +04:00
|
|
|
gitpatches = None
|
2007-07-17 20:39:30 +04:00
|
|
|
|
|
|
|
# our states
|
|
|
|
BFILE = 1
|
|
|
|
context = None
|
2009-12-23 21:31:48 +03:00
|
|
|
lr = linereader(fp)
|
2007-07-17 20:39:30 +04:00
|
|
|
|
|
|
|
while True:
|
|
|
|
x = lr.readline()
|
|
|
|
if not x:
|
|
|
|
break
|
2011-05-20 00:44:01 +04:00
|
|
|
if state == BFILE and (
|
|
|
|
(not context and x[0] == '@')
|
|
|
|
or (context is not False and x.startswith('***************'))
|
|
|
|
or x.startswith('GIT binary patch')):
|
2011-05-20 00:44:01 +04:00
|
|
|
gp = None
|
2011-06-06 00:24:19 +04:00
|
|
|
if (gitpatches and
|
patch: fix patch hunk/metdata synchronization (issue3384)
Git patches are parsed in two phases: 1) extract metadata, 2) parse actual
deltas and merge them with the previous metadata. We do this to avoid
dependency issues like "modify a; copy a to b", where "b" must be copied from
the unmodified "a".
Issue3384 is caused by flaky code I wrote to synchronize the patch metadata
with the emitted hunk:
if (gitpatches and
(gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)):
gp = gitpatches.pop()[2]
With a patch like:
diff --git a/a b/c
copy from a
copy to c
--- a/a
+++ b/c
@@ -1,1 +1,2 @@
a
+a
@@ -2,1 +2,2 @@
a
+a
diff --git a/a b/a
--- a/a
+++ b/a
@@ -1,1 +1,2 @@
a
+b
the first hunk of the first block is matched with the metadata for the block
"diff --git a/a b/c", then the second hunk of the first block is matched with
the metadata of the second block "diff --git a/a b/a", because of the "or" in
the code paste above. Turning the "or" into an "and" is not enough as we have
to deal with /dev/null cases for each file.
We I remove this broken piece of code:
# copy/rename + modify should modify target, not source
if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode:
afile = bfile
because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file",
and because this only happens for git patches, which afile/bfile are ignored
anyway by applydiff().
v2:
- Avoid a traceback on git metadata desynchronization
2012-04-21 23:40:25 +04:00
|
|
|
gitpatches[-1].ispatching(afile, bfile)):
|
|
|
|
gp = gitpatches.pop()
|
2011-05-20 00:44:01 +04:00
|
|
|
if x.startswith('GIT binary patch'):
|
2012-04-26 23:44:00 +04:00
|
|
|
h = binhunk(lr, gp.path)
|
2011-05-20 00:44:01 +04:00
|
|
|
else:
|
|
|
|
if context is None and x.startswith('***************'):
|
|
|
|
context = True
|
2011-05-27 23:50:09 +04:00
|
|
|
h = hunk(x, hunknum + 1, lr, context)
|
2007-07-17 20:39:30 +04:00
|
|
|
hunknum += 1
|
2007-12-18 01:06:01 +03:00
|
|
|
if emitfile:
|
|
|
|
emitfile = False
|
2011-06-11 16:17:25 +04:00
|
|
|
yield 'file', (afile, bfile, h, gp and gp.copy() or None)
|
2011-03-20 02:08:44 +03:00
|
|
|
yield 'hunk', h
|
2007-07-17 20:39:30 +04:00
|
|
|
elif x.startswith('diff --git'):
|
2012-04-26 23:44:02 +04:00
|
|
|
m = gitre.match(x.rstrip(' \r\n'))
|
2011-05-20 00:44:01 +04:00
|
|
|
if not m:
|
|
|
|
continue
|
patch: fix patch hunk/metdata synchronization (issue3384)
Git patches are parsed in two phases: 1) extract metadata, 2) parse actual
deltas and merge them with the previous metadata. We do this to avoid
dependency issues like "modify a; copy a to b", where "b" must be copied from
the unmodified "a".
Issue3384 is caused by flaky code I wrote to synchronize the patch metadata
with the emitted hunk:
if (gitpatches and
(gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)):
gp = gitpatches.pop()[2]
With a patch like:
diff --git a/a b/c
copy from a
copy to c
--- a/a
+++ b/c
@@ -1,1 +1,2 @@
a
+a
@@ -2,1 +2,2 @@
a
+a
diff --git a/a b/a
--- a/a
+++ b/a
@@ -1,1 +1,2 @@
a
+b
the first hunk of the first block is matched with the metadata for the block
"diff --git a/a b/c", then the second hunk of the first block is matched with
the metadata of the second block "diff --git a/a b/a", because of the "or" in
the code paste above. Turning the "or" into an "and" is not enough as we have
to deal with /dev/null cases for each file.
We I remove this broken piece of code:
# copy/rename + modify should modify target, not source
if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode:
afile = bfile
because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file",
and because this only happens for git patches, which afile/bfile are ignored
anyway by applydiff().
v2:
- Avoid a traceback on git metadata desynchronization
2012-04-21 23:40:25 +04:00
|
|
|
if gitpatches is None:
|
2011-05-20 00:44:01 +04:00
|
|
|
# scan whole input for git metadata
|
patch: fix patch hunk/metdata synchronization (issue3384)
Git patches are parsed in two phases: 1) extract metadata, 2) parse actual
deltas and merge them with the previous metadata. We do this to avoid
dependency issues like "modify a; copy a to b", where "b" must be copied from
the unmodified "a".
Issue3384 is caused by flaky code I wrote to synchronize the patch metadata
with the emitted hunk:
if (gitpatches and
(gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)):
gp = gitpatches.pop()[2]
With a patch like:
diff --git a/a b/c
copy from a
copy to c
--- a/a
+++ b/c
@@ -1,1 +1,2 @@
a
+a
@@ -2,1 +2,2 @@
a
+a
diff --git a/a b/a
--- a/a
+++ b/a
@@ -1,1 +1,2 @@
a
+b
the first hunk of the first block is matched with the metadata for the block
"diff --git a/a b/c", then the second hunk of the first block is matched with
the metadata of the second block "diff --git a/a b/a", because of the "or" in
the code paste above. Turning the "or" into an "and" is not enough as we have
to deal with /dev/null cases for each file.
We I remove this broken piece of code:
# copy/rename + modify should modify target, not source
if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode:
afile = bfile
because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file",
and because this only happens for git patches, which afile/bfile are ignored
anyway by applydiff().
v2:
- Avoid a traceback on git metadata desynchronization
2012-04-21 23:40:25 +04:00
|
|
|
gitpatches = scangitpatch(lr, x)
|
|
|
|
yield 'git', [g.copy() for g in gitpatches
|
|
|
|
if g.op in ('COPY', 'RENAME')]
|
2011-05-20 00:44:01 +04:00
|
|
|
gitpatches.reverse()
|
2011-05-20 00:44:01 +04:00
|
|
|
afile = 'a/' + m.group(1)
|
|
|
|
bfile = 'b/' + m.group(2)
|
patch: fix patch hunk/metdata synchronization (issue3384)
Git patches are parsed in two phases: 1) extract metadata, 2) parse actual
deltas and merge them with the previous metadata. We do this to avoid
dependency issues like "modify a; copy a to b", where "b" must be copied from
the unmodified "a".
Issue3384 is caused by flaky code I wrote to synchronize the patch metadata
with the emitted hunk:
if (gitpatches and
(gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)):
gp = gitpatches.pop()[2]
With a patch like:
diff --git a/a b/c
copy from a
copy to c
--- a/a
+++ b/c
@@ -1,1 +1,2 @@
a
+a
@@ -2,1 +2,2 @@
a
+a
diff --git a/a b/a
--- a/a
+++ b/a
@@ -1,1 +1,2 @@
a
+b
the first hunk of the first block is matched with the metadata for the block
"diff --git a/a b/c", then the second hunk of the first block is matched with
the metadata of the second block "diff --git a/a b/a", because of the "or" in
the code paste above. Turning the "or" into an "and" is not enough as we have
to deal with /dev/null cases for each file.
We I remove this broken piece of code:
# copy/rename + modify should modify target, not source
if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode:
afile = bfile
because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file",
and because this only happens for git patches, which afile/bfile are ignored
anyway by applydiff().
v2:
- Avoid a traceback on git metadata desynchronization
2012-04-21 23:40:25 +04:00
|
|
|
while gitpatches and not gitpatches[-1].ispatching(afile, bfile):
|
|
|
|
gp = gitpatches.pop()
|
2011-06-11 16:17:25 +04:00
|
|
|
yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
|
patch: fix patch hunk/metdata synchronization (issue3384)
Git patches are parsed in two phases: 1) extract metadata, 2) parse actual
deltas and merge them with the previous metadata. We do this to avoid
dependency issues like "modify a; copy a to b", where "b" must be copied from
the unmodified "a".
Issue3384 is caused by flaky code I wrote to synchronize the patch metadata
with the emitted hunk:
if (gitpatches and
(gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)):
gp = gitpatches.pop()[2]
With a patch like:
diff --git a/a b/c
copy from a
copy to c
--- a/a
+++ b/c
@@ -1,1 +1,2 @@
a
+a
@@ -2,1 +2,2 @@
a
+a
diff --git a/a b/a
--- a/a
+++ b/a
@@ -1,1 +1,2 @@
a
+b
the first hunk of the first block is matched with the metadata for the block
"diff --git a/a b/c", then the second hunk of the first block is matched with
the metadata of the second block "diff --git a/a b/a", because of the "or" in
the code paste above. Turning the "or" into an "and" is not enough as we have
to deal with /dev/null cases for each file.
We I remove this broken piece of code:
# copy/rename + modify should modify target, not source
if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode:
afile = bfile
because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file",
and because this only happens for git patches, which afile/bfile are ignored
anyway by applydiff().
v2:
- Avoid a traceback on git metadata desynchronization
2012-04-21 23:40:25 +04:00
|
|
|
if not gitpatches:
|
|
|
|
raise PatchError(_('failed to synchronize metadata for "%s"')
|
|
|
|
% afile[2:])
|
|
|
|
gp = gitpatches[-1]
|
2011-05-20 00:44:01 +04:00
|
|
|
newfile = True
|
2007-07-17 20:39:30 +04:00
|
|
|
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)
|
|
|
|
|
2011-04-26 23:22:14 +04:00
|
|
|
if newfile:
|
|
|
|
newfile = False
|
2007-12-18 01:06:01 +03:00
|
|
|
emitfile = True
|
2007-07-17 20:39:30 +04:00
|
|
|
state = BFILE
|
|
|
|
hunknum = 0
|
2007-12-18 01:06:01 +03:00
|
|
|
|
2011-05-20 00:44:01 +04:00
|
|
|
while gitpatches:
|
patch: fix patch hunk/metdata synchronization (issue3384)
Git patches are parsed in two phases: 1) extract metadata, 2) parse actual
deltas and merge them with the previous metadata. We do this to avoid
dependency issues like "modify a; copy a to b", where "b" must be copied from
the unmodified "a".
Issue3384 is caused by flaky code I wrote to synchronize the patch metadata
with the emitted hunk:
if (gitpatches and
(gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)):
gp = gitpatches.pop()[2]
With a patch like:
diff --git a/a b/c
copy from a
copy to c
--- a/a
+++ b/c
@@ -1,1 +1,2 @@
a
+a
@@ -2,1 +2,2 @@
a
+a
diff --git a/a b/a
--- a/a
+++ b/a
@@ -1,1 +1,2 @@
a
+b
the first hunk of the first block is matched with the metadata for the block
"diff --git a/a b/c", then the second hunk of the first block is matched with
the metadata of the second block "diff --git a/a b/a", because of the "or" in
the code paste above. Turning the "or" into an "and" is not enough as we have
to deal with /dev/null cases for each file.
We I remove this broken piece of code:
# copy/rename + modify should modify target, not source
if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode:
afile = bfile
because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file",
and because this only happens for git patches, which afile/bfile are ignored
anyway by applydiff().
v2:
- Avoid a traceback on git metadata desynchronization
2012-04-21 23:40:25 +04:00
|
|
|
gp = gitpatches.pop()
|
2011-06-11 16:17:25 +04:00
|
|
|
yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
|
2011-05-20 00:44:01 +04:00
|
|
|
|
2011-06-11 16:14:13 +04:00
|
|
|
def applydiff(ui, fp, backend, store, strip=1, eolmode='strict'):
|
2010-04-17 22:23:24 +04:00
|
|
|
"""Reads a patch from fp and tries to apply it.
|
2009-06-15 02:03:26 +04:00
|
|
|
|
2011-06-11 16:14:13 +04:00
|
|
|
Returns 0 for a clean patch, -1 if any rejects were found and 1 if
|
|
|
|
there was any fuzz.
|
2007-12-18 01:06:01 +03:00
|
|
|
|
2009-12-20 19:18:02 +03:00
|
|
|
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'.
|
2009-06-15 02:03:26 +04:00
|
|
|
"""
|
2011-06-11 16:14:13 +04:00
|
|
|
return _applydiff(ui, fp, patchfile, backend, store, strip=strip,
|
2010-11-03 23:11:07 +03:00
|
|
|
eolmode=eolmode)
|
2010-04-17 22:23:24 +04:00
|
|
|
|
2011-06-11 16:14:13 +04:00
|
|
|
def _applydiff(ui, fp, patcher, backend, store, strip=1,
|
2011-05-27 23:50:10 +04:00
|
|
|
eolmode='strict'):
|
2011-05-20 00:44:01 +04:00
|
|
|
|
|
|
|
def pstrip(p):
|
|
|
|
return pathstrip(p, strip - 1)[1]
|
|
|
|
|
2007-12-18 01:06:01 +03:00
|
|
|
rejects = 0
|
|
|
|
err = 0
|
|
|
|
current_file = None
|
|
|
|
|
2011-05-06 19:45:12 +04:00
|
|
|
for state, values in iterhunks(fp):
|
2007-12-18 01:06:01 +03:00
|
|
|
if state == 'hunk':
|
|
|
|
if not current_file:
|
|
|
|
continue
|
2010-04-26 15:21:03 +04:00
|
|
|
ret = current_file.apply(values)
|
2011-06-11 16:14:13 +04:00
|
|
|
if ret > 0:
|
|
|
|
err = 1
|
2007-12-18 01:06:01 +03:00
|
|
|
elif state == 'file':
|
2011-03-20 02:22:47 +03:00
|
|
|
if current_file:
|
|
|
|
rejects += current_file.close()
|
2011-05-20 00:44:01 +04:00
|
|
|
current_file = None
|
|
|
|
afile, bfile, first_hunk, gp = values
|
|
|
|
if gp:
|
2011-06-11 16:17:25 +04:00
|
|
|
gp.path = pstrip(gp.path)
|
2011-05-27 23:50:10 +04:00
|
|
|
if gp.oldpath:
|
2011-06-11 16:17:25 +04:00
|
|
|
gp.oldpath = pstrip(gp.oldpath)
|
|
|
|
else:
|
|
|
|
gp = makepatchmeta(backend, afile, bfile, first_hunk, strip)
|
|
|
|
if gp.op == 'RENAME':
|
|
|
|
backend.unlink(gp.oldpath)
|
2011-05-20 00:44:01 +04:00
|
|
|
if not first_hunk:
|
2011-06-11 16:17:25 +04:00
|
|
|
if gp.op == 'DELETE':
|
|
|
|
backend.unlink(gp.path)
|
|
|
|
continue
|
|
|
|
data, mode = None, None
|
|
|
|
if gp.op in ('RENAME', 'COPY'):
|
2011-06-15 01:24:34 +04:00
|
|
|
data, mode = store.getfile(gp.oldpath)[:2]
|
2011-06-11 16:17:25 +04:00
|
|
|
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)
|
2011-05-20 00:44:01 +04:00
|
|
|
continue
|
2007-12-18 01:06:01 +03:00
|
|
|
try:
|
2011-06-11 16:17:25 +04:00
|
|
|
current_file = patcher(ui, gp, backend, store,
|
|
|
|
eolmode=eolmode)
|
2011-05-06 13:31:40 +04:00
|
|
|
except PatchError, inst:
|
|
|
|
ui.warn(str(inst) + '\n')
|
2010-04-26 15:21:03 +04:00
|
|
|
current_file = None
|
2007-12-18 01:06:01 +03:00
|
|
|
rejects += 1
|
|
|
|
continue
|
|
|
|
elif state == 'git':
|
2010-04-26 15:21:03 +04:00
|
|
|
for gp in values:
|
2011-05-27 23:50:10 +04:00
|
|
|
path = pstrip(gp.oldpath)
|
2012-06-01 19:37:56 +04:00
|
|
|
try:
|
|
|
|
data, mode = backend.getfile(path)
|
|
|
|
except IOError, e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
# The error ignored here will trigger a getfile()
|
|
|
|
# error in a place more appropriate for error
|
|
|
|
# handling, and will not interrupt the patching
|
|
|
|
# process.
|
|
|
|
else:
|
|
|
|
store.setfile(path, data, mode)
|
2007-07-17 20:39:30 +04:00
|
|
|
else:
|
2007-12-18 01:06:01 +03:00
|
|
|
raise util.Abort(_('unsupported parser state: %s') % state)
|
2007-12-18 00:19:21 +03:00
|
|
|
|
2011-03-20 02:22:47 +03:00
|
|
|
if current_file:
|
|
|
|
rejects += current_file.close()
|
2007-12-18 01:06:01 +03:00
|
|
|
|
2007-07-17 20:39:30 +04:00
|
|
|
if rejects:
|
|
|
|
return -1
|
|
|
|
return err
|
2006-08-13 03:13:27 +04:00
|
|
|
|
2011-05-20 00:44:01 +04:00
|
|
|
def _externalpatch(ui, repo, patcher, patchname, strip, files,
|
2011-05-20 00:44:01 +04:00
|
|
|
similarity):
|
2008-10-19 01:45:45 +04:00
|
|
|
"""use <patcher> to apply <patchname> to the working directory.
|
|
|
|
returns whether patch was applied with fuzz factor."""
|
|
|
|
|
|
|
|
fuzz = False
|
2010-10-10 00:13:08 +04:00
|
|
|
args = []
|
2011-05-20 00:44:01 +04:00
|
|
|
cwd = repo.root
|
2008-10-19 01:45:45 +04:00
|
|
|
if cwd:
|
|
|
|
args.append('-d %s' % util.shellquote(cwd))
|
|
|
|
fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip,
|
|
|
|
util.shellquote(patchname)))
|
2011-05-20 00:44:01 +04:00
|
|
|
try:
|
|
|
|
for line in fp:
|
|
|
|
line = line.rstrip()
|
|
|
|
ui.note(line + '\n')
|
|
|
|
if line.startswith('patching file '):
|
|
|
|
pf = util.parsepatchoutput(line)
|
|
|
|
printed_file = False
|
2011-06-11 16:14:11 +04:00
|
|
|
files.add(pf)
|
2011-05-20 00:44:01 +04:00
|
|
|
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:
|
|
|
|
cfiles = list(files)
|
|
|
|
cwd = repo.getcwd()
|
|
|
|
if cwd:
|
|
|
|
cfiles = [util.pathto(repo.root, cwd, f)
|
2011-06-20 11:30:03 +04:00
|
|
|
for f in cfiles]
|
2011-05-20 00:44:01 +04:00
|
|
|
scmutil.addremove(repo, cfiles, similarity=similarity)
|
2008-10-19 01:45:45 +04:00
|
|
|
code = fp.close()
|
|
|
|
if code:
|
|
|
|
raise PatchError(_("patch command failed: %s") %
|
2011-05-06 17:31:09 +04:00
|
|
|
util.explainexit(code)[0])
|
2008-10-19 01:45:45 +04:00
|
|
|
return fuzz
|
|
|
|
|
2011-06-15 01:26:35 +04:00
|
|
|
def patchbackend(ui, backend, patchobj, strip, files=None, eolmode='strict'):
|
2009-10-31 20:02:13 +03:00
|
|
|
if files is None:
|
2011-06-11 16:14:11 +04:00
|
|
|
files = set()
|
2009-06-15 02:03:26 +04:00
|
|
|
if eolmode is None:
|
|
|
|
eolmode = ui.config('patch', 'eol', 'strict')
|
2009-12-20 19:18:02 +03:00
|
|
|
if eolmode.lower() not in eolmodes:
|
2010-08-30 00:37:58 +04:00
|
|
|
raise util.Abort(_('unsupported line endings type: %s') % eolmode)
|
2009-12-20 19:18:02 +03:00
|
|
|
eolmode = eolmode.lower()
|
2009-06-19 15:47:50 +04:00
|
|
|
|
2011-05-27 23:50:10 +04:00
|
|
|
store = filestore()
|
2008-10-19 01:45:45 +04:00
|
|
|
try:
|
2009-07-05 13:01:30 +04:00
|
|
|
fp = open(patchobj, 'rb')
|
2008-10-19 01:45:45 +04:00
|
|
|
except TypeError:
|
|
|
|
fp = patchobj
|
|
|
|
try:
|
2011-06-11 16:14:13 +04:00
|
|
|
ret = applydiff(ui, fp, backend, store, strip=strip,
|
2011-05-27 23:50:10 +04:00
|
|
|
eolmode=eolmode)
|
2008-10-19 01:45:45 +04:00
|
|
|
finally:
|
2010-01-03 21:47:07 +03:00
|
|
|
if fp != patchobj:
|
|
|
|
fp.close()
|
2011-06-11 16:14:11 +04:00
|
|
|
files.update(backend.close())
|
2011-05-27 23:50:10 +04:00
|
|
|
store.close()
|
2008-10-19 01:45:45 +04:00
|
|
|
if ret < 0:
|
2010-10-10 00:13:08 +04:00
|
|
|
raise PatchError(_('patch failed to apply'))
|
2008-10-19 01:45:45 +04:00
|
|
|
return ret > 0
|
|
|
|
|
2011-06-15 01:26:35 +04:00
|
|
|
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)
|
|
|
|
|
|
|
|
def makememctx(repo, parents, text, user, date, branch, files, store,
|
|
|
|
editor=None):
|
|
|
|
def getfilectx(repo, memctx, path):
|
|
|
|
data, (islink, isexec), copied = store.getfile(path)
|
|
|
|
return context.memfilectx(path, data, islink=islink, isexec=isexec,
|
|
|
|
copied=copied)
|
|
|
|
extra = {}
|
|
|
|
if branch:
|
|
|
|
extra['branch'] = encoding.fromlocal(branch)
|
|
|
|
ctx = context.memctx(repo, parents, text, files, getfilectx, user,
|
|
|
|
date, extra)
|
|
|
|
if editor:
|
|
|
|
ctx._text = editor(repo, ctx, [])
|
|
|
|
return ctx
|
|
|
|
|
2011-05-20 00:44:01 +04:00
|
|
|
def patch(ui, repo, patchname, strip=1, files=None, eolmode='strict',
|
2011-05-08 19:48:31 +04:00
|
|
|
similarity=0):
|
2009-06-15 02:03:26 +04:00
|
|
|
"""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.
|
|
|
|
"""
|
2008-10-19 01:45:45 +04:00
|
|
|
patcher = ui.config('ui', 'patch')
|
2009-10-31 20:02:13 +03:00
|
|
|
if files is None:
|
2011-06-11 16:14:11 +04:00
|
|
|
files = set()
|
2008-10-19 01:45:45 +04:00
|
|
|
try:
|
|
|
|
if patcher:
|
2011-05-20 00:44:01 +04:00
|
|
|
return _externalpatch(ui, repo, patcher, patchname, strip,
|
2011-05-20 00:44:01 +04:00
|
|
|
files, similarity)
|
2011-05-19 01:48:17 +04:00
|
|
|
return internalpatch(ui, repo, patchname, strip, files, eolmode,
|
2011-05-08 19:48:31 +04:00
|
|
|
similarity)
|
2008-10-19 01:45:45 +04:00
|
|
|
except PatchError, err:
|
2010-10-10 00:13:08 +04:00
|
|
|
raise util.Abort(str(err))
|
2008-10-19 01:45:45 +04:00
|
|
|
|
2011-05-18 01:46:38 +04:00
|
|
|
def changedfiles(ui, repo, patchpath, strip=1):
|
|
|
|
backend = fsbackend(ui, repo.root)
|
2011-05-06 20:03:41 +04:00
|
|
|
fp = open(patchpath, 'rb')
|
|
|
|
try:
|
|
|
|
changed = set()
|
|
|
|
for state, values in iterhunks(fp):
|
2011-05-20 00:44:01 +04:00
|
|
|
if state == 'file':
|
2011-05-20 00:44:01 +04:00
|
|
|
afile, bfile, first_hunk, gp = values
|
|
|
|
if gp:
|
2011-06-11 16:17:25 +04:00
|
|
|
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)
|
2011-05-20 00:44:01 +04:00
|
|
|
elif state not in ('hunk', 'git'):
|
2011-05-06 20:03:41 +04:00
|
|
|
raise util.Abort(_('unsupported parser state: %s') % state)
|
|
|
|
return changed
|
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
|
2007-07-31 07:49:08 +04:00
|
|
|
def b85diff(to, tn):
|
2006-10-12 20:17:16 +04:00
|
|
|
'''print base85-encoded binary diff'''
|
|
|
|
def gitindex(text):
|
|
|
|
if not text:
|
2010-09-02 14:08:13 +04:00
|
|
|
return hex(nullid)
|
2006-10-12 20:17:16 +04:00
|
|
|
l = len(text)
|
2008-04-05 00:36:40 +04:00
|
|
|
s = util.sha1('blob %d\0' % l)
|
2006-10-12 20:17:16 +04:00
|
|
|
s.update(text)
|
|
|
|
return s.hexdigest()
|
|
|
|
|
|
|
|
def fmtline(line):
|
|
|
|
l = len(line)
|
|
|
|
if l <= 26:
|
|
|
|
l = chr(ord('A') + l - 1)
|
|
|
|
else:
|
|
|
|
l = chr(l - 26 + ord('a') - 1)
|
|
|
|
return '%c%s\n' % (l, base85.b85encode(line, True))
|
|
|
|
|
|
|
|
def chunk(text, csize=52):
|
|
|
|
l = len(text)
|
|
|
|
i = 0
|
|
|
|
while i < l:
|
2010-01-25 09:05:27 +03:00
|
|
|
yield text[i:i + csize]
|
2006-10-12 20:17:16 +04:00
|
|
|
i += csize
|
|
|
|
|
2007-02-17 14:54:59 +03:00
|
|
|
tohash = gitindex(to)
|
|
|
|
tnhash = gitindex(tn)
|
|
|
|
if tohash == tnhash:
|
2007-02-17 14:55:00 +03:00
|
|
|
return ""
|
2006-10-12 20:17:16 +04:00
|
|
|
|
2007-02-17 14:55:00 +03:00
|
|
|
# TODO: deltas
|
|
|
|
ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' %
|
|
|
|
(tohash, tnhash, len(tn))]
|
|
|
|
for l in chunk(zlib.compress(tn)):
|
|
|
|
ret.append(fmtline(l))
|
|
|
|
ret.append('\n')
|
|
|
|
return ''.join(ret)
|
2006-10-12 20:17:16 +04:00
|
|
|
|
2010-01-01 22:54:05 +03:00
|
|
|
class GitDiffRequired(Exception):
|
|
|
|
pass
|
2008-10-22 11:29:26 +04:00
|
|
|
|
2011-11-18 15:04:31 +04:00
|
|
|
def diffopts(ui, opts=None, untrusted=False, section='diff'):
|
2010-03-09 21:04:18 +03:00
|
|
|
def get(key, name=None, getter=ui.configbool):
|
|
|
|
return ((opts and opts.get(key)) or
|
2011-11-18 15:04:31 +04:00
|
|
|
getter(section, name or key, None, untrusted=untrusted))
|
2010-03-09 21:04:18 +03:00
|
|
|
return mdiff.diffopts(
|
|
|
|
text=opts and opts.get('text'),
|
|
|
|
git=get('git'),
|
|
|
|
nodates=get('nodates'),
|
|
|
|
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))
|
|
|
|
|
2010-01-01 22:54:05 +03:00
|
|
|
def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
|
2010-09-03 14:58:51 +04:00
|
|
|
losedatafn=None, prefix=''):
|
2008-11-03 18:48:23 +03:00
|
|
|
'''yields diff of changes to files between two nodes, or node and
|
2006-08-13 03:13:27 +04:00
|
|
|
working directory.
|
|
|
|
|
|
|
|
if node1 is None, use first dirstate parent instead.
|
2010-01-01 22:54:05 +03:00
|
|
|
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.
|
2010-09-03 14:58:51 +04:00
|
|
|
|
|
|
|
prefix is a filename prefix that is prepended to all filenames on
|
|
|
|
display (used for subrepos).
|
2010-01-01 22:54:05 +03:00
|
|
|
'''
|
2006-08-13 03:13:27 +04:00
|
|
|
|
|
|
|
if opts is None:
|
|
|
|
opts = mdiff.defaultopts
|
|
|
|
|
2009-11-05 17:18:56 +03:00
|
|
|
if not node1 and not node2:
|
2011-04-05 01:21:59 +04:00
|
|
|
node1 = repo.dirstate.p1()
|
2006-08-17 06:49:45 +04:00
|
|
|
|
2009-07-15 03:50:37 +04:00
|
|
|
def lrugetfilectx():
|
|
|
|
cache = {}
|
2012-06-02 04:05:31 +04:00
|
|
|
order = util.deque()
|
2009-07-15 03:50:37 +04:00
|
|
|
def getfilectx(f, ctx):
|
|
|
|
fctx = ctx.filectx(f, filelog=cache.get(f))
|
|
|
|
if f not in cache:
|
|
|
|
if len(cache) > 20:
|
2012-05-15 21:46:23 +04:00
|
|
|
del cache[order.popleft()]
|
2009-10-31 20:02:34 +03:00
|
|
|
cache[f] = fctx.filelog()
|
2009-07-15 03:50:37 +04:00
|
|
|
else:
|
|
|
|
order.remove(f)
|
|
|
|
order.append(f)
|
|
|
|
return fctx
|
|
|
|
return getfilectx
|
|
|
|
getfilectx = lrugetfilectx()
|
2006-08-17 06:49:45 +04:00
|
|
|
|
2008-06-26 23:35:46 +04:00
|
|
|
ctx1 = repo[node1]
|
2008-10-13 00:21:08 +04:00
|
|
|
ctx2 = repo[node2]
|
2006-08-13 03:13:27 +04:00
|
|
|
|
|
|
|
if not changes:
|
2008-10-13 00:21:08 +04:00
|
|
|
changes = repo.status(ctx1, ctx2, match=match)
|
2008-06-27 22:43:29 +04:00
|
|
|
modified, added, removed = changes[:3]
|
2006-08-13 03:13:27 +04:00
|
|
|
|
|
|
|
if not modified and not added and not removed:
|
2010-01-01 22:54:05 +03:00
|
|
|
return []
|
2006-12-25 19:43:49 +03:00
|
|
|
|
2009-12-29 18:00:38 +03:00
|
|
|
revs = None
|
2010-01-01 22:54:05 +03:00
|
|
|
if not repo.ui.quiet:
|
2006-10-13 22:34:35 +04:00
|
|
|
hexfunc = repo.ui.debugflag and hex or short
|
2009-12-29 18:00:38 +03:00
|
|
|
revs = [hexfunc(node) for node in [node1, node2] if node]
|
2006-08-13 03:13:27 +04:00
|
|
|
|
2010-01-01 22:54:05 +03:00
|
|
|
copy = {}
|
|
|
|
if opts.git or opts.upgrade:
|
2012-01-05 01:48:02 +04:00
|
|
|
copy = copies.pathcopies(ctx1, ctx2)
|
2006-08-15 09:48:03 +04:00
|
|
|
|
2012-07-31 05:30:42 +04:00
|
|
|
def difffn(opts, losedata):
|
|
|
|
return trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
|
|
|
|
copy, getfilectx, opts, losedata, prefix)
|
2010-01-01 22:54:05 +03:00
|
|
|
if opts.upgrade and not opts.git:
|
|
|
|
try:
|
|
|
|
def losedata(fn):
|
|
|
|
if not losedatafn or not losedatafn(fn=fn):
|
2012-05-12 18:00:58 +04:00
|
|
|
raise GitDiffRequired
|
2010-01-01 22:54:05 +03:00
|
|
|
# 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)
|
|
|
|
|
2010-04-03 00:22:06 +04:00
|
|
|
def difflabel(func, *args, **kw):
|
|
|
|
'''yields 2-tuples of (output, label) based on the output of func()'''
|
2011-10-05 10:20:38 +04:00
|
|
|
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
|
2010-04-03 00:22:06 +04:00
|
|
|
for chunk in func(*args, **kw):
|
|
|
|
lines = chunk.split('\n')
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
if i != 0:
|
|
|
|
yield ('\n', '')
|
2011-10-05 10:20:38 +04:00
|
|
|
if head:
|
|
|
|
if line.startswith('@'):
|
|
|
|
head = False
|
|
|
|
else:
|
2012-05-12 18:00:57 +04:00
|
|
|
if line and line[0] not in ' +-@\\':
|
2011-10-05 10:20:38 +04:00
|
|
|
head = True
|
2010-04-03 00:22:06 +04:00
|
|
|
stripline = line
|
2011-10-05 10:20:38 +04:00
|
|
|
if not head and line and line[0] in '+-':
|
2010-04-03 00:22:06 +04:00
|
|
|
# highlight trailing whitespace, but only in changed lines
|
|
|
|
stripline = line.rstrip()
|
2011-10-05 10:20:38 +04:00
|
|
|
prefixes = textprefixes
|
|
|
|
if head:
|
|
|
|
prefixes = headprefixes
|
2010-04-03 00:22:06 +04:00
|
|
|
for prefix, label in prefixes:
|
|
|
|
if stripline.startswith(prefix):
|
|
|
|
yield (stripline, label)
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2010-01-01 22:54:05 +03:00
|
|
|
def _addmodehdr(header, omode, nmode):
|
|
|
|
if omode != nmode:
|
|
|
|
header.append('old mode %s\n' % omode)
|
|
|
|
header.append('new mode %s\n' % nmode)
|
|
|
|
|
|
|
|
def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
|
2010-09-03 14:58:51 +04:00
|
|
|
copy, getfilectx, opts, losedatafn, prefix):
|
|
|
|
|
|
|
|
def join(f):
|
|
|
|
return os.path.join(prefix, f)
|
2010-01-01 22:54:05 +03:00
|
|
|
|
|
|
|
date1 = util.datestr(ctx1.date())
|
|
|
|
man1 = ctx1.manifest()
|
|
|
|
|
2009-05-17 05:28:49 +04:00
|
|
|
gone = set()
|
2008-06-26 22:46:34 +04:00
|
|
|
gitmode = {'l': '120000', 'x': '100755', '': '100644'}
|
|
|
|
|
2010-02-11 16:22:57 +03:00
|
|
|
copyto = dict([(v, k) for k, v in copy.items()])
|
|
|
|
|
2010-01-01 22:54:05 +03:00
|
|
|
if opts.git:
|
|
|
|
revs = None
|
|
|
|
|
2009-04-27 01:50:44 +04:00
|
|
|
for f in sorted(modified + added + removed):
|
2006-08-13 03:13:27 +04:00
|
|
|
to = None
|
|
|
|
tn = None
|
2006-08-15 09:48:03 +04:00
|
|
|
dodiff = True
|
2006-10-11 01:48:26 +04:00
|
|
|
header = []
|
2006-12-25 19:43:49 +03:00
|
|
|
if f in man1:
|
|
|
|
to = getfilectx(f, ctx1).data()
|
2006-08-13 03:13:27 +04:00
|
|
|
if f not in removed:
|
2006-12-25 19:43:49 +03:00
|
|
|
tn = getfilectx(f, ctx2).data()
|
2007-11-01 22:17:59 +03:00
|
|
|
a, b = f, f
|
2010-01-01 22:54:05 +03:00
|
|
|
if opts.git or losedatafn:
|
2006-08-15 09:48:03 +04:00
|
|
|
if f in added:
|
2008-06-26 22:46:34 +04:00
|
|
|
mode = gitmode[ctx2.flags(f)]
|
2010-02-11 16:22:57 +03:00
|
|
|
if f in copy or f in copyto:
|
2010-01-01 22:54:05 +03:00
|
|
|
if opts.git:
|
2010-02-11 16:22:57 +03:00
|
|
|
if f in copy:
|
|
|
|
a = copy[f]
|
|
|
|
else:
|
|
|
|
a = copyto[f]
|
2010-01-01 22:54:05 +03:00
|
|
|
omode = gitmode[man1.flags(a)]
|
|
|
|
_addmodehdr(header, omode, mode)
|
|
|
|
if a in removed and a not in gone:
|
|
|
|
op = 'rename'
|
|
|
|
gone.add(a)
|
|
|
|
else:
|
|
|
|
op = 'copy'
|
2010-09-03 14:58:51 +04:00
|
|
|
header.append('%s from %s\n' % (op, join(a)))
|
|
|
|
header.append('%s to %s\n' % (op, join(f)))
|
2010-01-01 22:54:05 +03:00
|
|
|
to = getfilectx(a, ctx1).data()
|
2006-11-21 00:32:46 +03:00
|
|
|
else:
|
2010-01-01 22:54:05 +03:00
|
|
|
losedatafn(f)
|
2006-08-15 09:48:03 +04:00
|
|
|
else:
|
2010-01-01 22:54:05 +03:00
|
|
|
if opts.git:
|
|
|
|
header.append('new file mode %s\n' % mode)
|
|
|
|
elif ctx2.flags(f):
|
|
|
|
losedatafn(f)
|
2010-09-28 02:41:08 +04:00
|
|
|
# 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.
|
2007-02-16 09:54:46 +03:00
|
|
|
if util.binary(tn):
|
2010-01-01 22:54:05 +03:00
|
|
|
if opts.git:
|
|
|
|
dodiff = 'binary'
|
|
|
|
else:
|
|
|
|
losedatafn(f)
|
|
|
|
if not opts.git and not tn:
|
|
|
|
# regular diffs cannot represent new empty file
|
|
|
|
losedatafn(f)
|
2006-08-15 09:48:03 +04:00
|
|
|
elif f in removed:
|
2010-01-01 22:54:05 +03:00
|
|
|
if opts.git:
|
|
|
|
# have we already reported a copy above?
|
2010-02-14 19:53:23 +03:00
|
|
|
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)):
|
2010-01-01 22:54:05 +03:00
|
|
|
dodiff = False
|
|
|
|
else:
|
|
|
|
header.append('deleted file mode %s\n' %
|
|
|
|
gitmode[man1.flags(f)])
|
2010-09-28 02:41:07 +04:00
|
|
|
elif not to or util.binary(to):
|
2010-01-01 22:54:05 +03:00
|
|
|
# regular diffs cannot represent empty file deletion
|
|
|
|
losedatafn(f)
|
2006-08-15 09:48:03 +04:00
|
|
|
else:
|
2010-01-01 22:54:05 +03:00
|
|
|
oflag = man1.flags(f)
|
|
|
|
nflag = ctx2.flags(f)
|
|
|
|
binary = util.binary(to) or util.binary(tn)
|
|
|
|
if opts.git:
|
|
|
|
_addmodehdr(header, gitmode[oflag], gitmode[nflag])
|
|
|
|
if binary:
|
|
|
|
dodiff = 'binary'
|
|
|
|
elif binary or nflag != oflag:
|
|
|
|
losedatafn(f)
|
|
|
|
if opts.git:
|
2010-09-03 14:58:51 +04:00
|
|
|
header.insert(0, mdiff.diffline(revs, join(a), join(b), opts))
|
2010-01-01 22:54:05 +03:00
|
|
|
|
2007-02-17 14:55:00 +03:00
|
|
|
if dodiff:
|
|
|
|
if dodiff == 'binary':
|
2007-07-31 07:49:08 +04:00
|
|
|
text = b85diff(to, tn)
|
2007-02-17 14:55:00 +03:00
|
|
|
else:
|
2007-02-20 22:55:23 +03:00
|
|
|
text = mdiff.unidiff(to, date1,
|
|
|
|
# ctx2 date may be dynamic
|
|
|
|
tn, util.datestr(ctx2.date()),
|
2010-09-03 14:58:51 +04:00
|
|
|
join(a), join(b), revs, opts=opts)
|
2008-11-03 18:48:23 +03:00
|
|
|
if header and (text or len(header) > 1):
|
|
|
|
yield ''.join(header)
|
|
|
|
if text:
|
|
|
|
yield text
|
2006-08-13 03:13:27 +04:00
|
|
|
|
2011-05-22 00:06:36 +04:00
|
|
|
def diffstatsum(stats):
|
2011-05-26 18:51:02 +04:00
|
|
|
maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False
|
2011-05-22 00:06:36 +04:00
|
|
|
for f, a, r, b in stats:
|
|
|
|
maxfile = max(maxfile, encoding.colwidth(f))
|
2011-05-26 18:51:02 +04:00
|
|
|
maxtotal = max(maxtotal, a + r)
|
2011-05-22 00:06:36 +04:00
|
|
|
addtotal += a
|
|
|
|
removetotal += r
|
|
|
|
binary = binary or b
|
|
|
|
|
2011-05-26 18:51:02 +04:00
|
|
|
return maxfile, maxtotal, addtotal, removetotal, binary
|
2011-05-22 00:06:36 +04:00
|
|
|
|
2008-12-25 11:48:24 +03:00
|
|
|
def diffstatdata(lines):
|
diffstat: fix parsing of filenames with spaces
The patch changes the output of "hg diff --stat" when one file whose filename
has spaces has changed, making it get the full filename instead of just the
substring between the last space and the end of the filename.
It also changes the diffstat generated by "hg email -d" when one of the commit
messages starts with "diff". Because of the regex used to parse the filename,
the diffstat generated by "hg email -d" will still be not correct if a commit
message starts with "diff -r ".
Before the patch Mercurial has the following behavior:
$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
After the patch:
$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Before the patch:
$ hg add mercurial/patch.py tests/tests-diffstat.t
$ hg commit -m "diffstat: fix parsing of filenames"
$ hg email -d --test tip
This patch series consists of 1 patches.
diffstat: fix parsing of filenames
[...]
filenames | 0
mercurial/patch.py | 6 ++++--
tests/test-diffstat.t | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 2 deletions(-)
[...]
After the patch:
$ hg email -d --test tip
This patch series consists of 1 patches.
diffstat: fix parsing of filenames
[...]
mercurial/patch.py | 6 ++++--
tests/test-diffstat.t | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 2 deletions(-)
[...]
2011-02-04 22:32:14 +03:00
|
|
|
diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
|
|
|
|
|
2011-05-22 00:01:28 +04:00
|
|
|
results = []
|
2011-10-24 15:41:19 +04:00
|
|
|
filename, adds, removes, isbinary = None, 0, 0, False
|
2011-05-22 00:01:28 +04:00
|
|
|
|
|
|
|
def addresult():
|
|
|
|
if filename:
|
|
|
|
results.append((filename, adds, removes, isbinary))
|
|
|
|
|
2008-12-25 11:48:24 +03:00
|
|
|
for line in lines:
|
|
|
|
if line.startswith('diff'):
|
2011-05-22 00:01:28 +04:00
|
|
|
addresult()
|
2008-12-25 11:48:24 +03:00
|
|
|
# set numbers to 0 anyway when starting new file
|
2011-10-24 15:41:19 +04:00
|
|
|
adds, removes, isbinary = 0, 0, False
|
2008-12-25 11:48:24 +03:00
|
|
|
if line.startswith('diff --git'):
|
|
|
|
filename = gitre.search(line).group(1)
|
diffstat: fix parsing of filenames with spaces
The patch changes the output of "hg diff --stat" when one file whose filename
has spaces has changed, making it get the full filename instead of just the
substring between the last space and the end of the filename.
It also changes the diffstat generated by "hg email -d" when one of the commit
messages starts with "diff". Because of the regex used to parse the filename,
the diffstat generated by "hg email -d" will still be not correct if a commit
message starts with "diff -r ".
Before the patch Mercurial has the following behavior:
$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
After the patch:
$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Before the patch:
$ hg add mercurial/patch.py tests/tests-diffstat.t
$ hg commit -m "diffstat: fix parsing of filenames"
$ hg email -d --test tip
This patch series consists of 1 patches.
diffstat: fix parsing of filenames
[...]
filenames | 0
mercurial/patch.py | 6 ++++--
tests/test-diffstat.t | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 2 deletions(-)
[...]
After the patch:
$ hg email -d --test tip
This patch series consists of 1 patches.
diffstat: fix parsing of filenames
[...]
mercurial/patch.py | 6 ++++--
tests/test-diffstat.t | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 2 deletions(-)
[...]
2011-02-04 22:32:14 +03:00
|
|
|
elif line.startswith('diff -r'):
|
2009-06-09 17:25:17 +04:00
|
|
|
# format: "diff -r ... -r ... filename"
|
diffstat: fix parsing of filenames with spaces
The patch changes the output of "hg diff --stat" when one file whose filename
has spaces has changed, making it get the full filename instead of just the
substring between the last space and the end of the filename.
It also changes the diffstat generated by "hg email -d" when one of the commit
messages starts with "diff". Because of the regex used to parse the filename,
the diffstat generated by "hg email -d" will still be not correct if a commit
message starts with "diff -r ".
Before the patch Mercurial has the following behavior:
$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
After the patch:
$ echo "foobar">"file with spaces"
$ hg add "file with spaces"
$ hg diff --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ hg diff --git --stat
file with spaces | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
Before the patch:
$ hg add mercurial/patch.py tests/tests-diffstat.t
$ hg commit -m "diffstat: fix parsing of filenames"
$ hg email -d --test tip
This patch series consists of 1 patches.
diffstat: fix parsing of filenames
[...]
filenames | 0
mercurial/patch.py | 6 ++++--
tests/test-diffstat.t | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 2 deletions(-)
[...]
After the patch:
$ hg email -d --test tip
This patch series consists of 1 patches.
diffstat: fix parsing of filenames
[...]
mercurial/patch.py | 6 ++++--
tests/test-diffstat.t | 17 +++++++++++++++++
3 files changed, 21 insertions(+), 2 deletions(-)
[...]
2011-02-04 22:32:14 +03:00
|
|
|
filename = diffre.search(line).group(1)
|
2012-01-22 02:50:58 +04:00
|
|
|
elif line.startswith('+') and not line.startswith('+++ '):
|
2008-12-25 11:48:24 +03:00
|
|
|
adds += 1
|
2012-01-22 02:50:58 +04:00
|
|
|
elif line.startswith('-') and not line.startswith('--- '):
|
2008-12-25 11:48:24 +03:00
|
|
|
removes += 1
|
2011-10-24 15:41:19 +04:00
|
|
|
elif (line.startswith('GIT binary patch') or
|
|
|
|
line.startswith('Binary file')):
|
|
|
|
isbinary = True
|
2011-05-22 00:01:28 +04:00
|
|
|
addresult()
|
|
|
|
return results
|
2008-12-25 11:48:24 +03:00
|
|
|
|
2009-10-25 03:53:33 +03:00
|
|
|
def diffstat(lines, width=80, git=False):
|
2008-12-25 11:48:24 +03:00
|
|
|
output = []
|
2011-05-22 00:06:38 +04:00
|
|
|
stats = diffstatdata(lines)
|
2011-05-26 18:51:02 +04:00
|
|
|
maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats)
|
2008-12-25 11:48:24 +03:00
|
|
|
|
|
|
|
countwidth = len(str(maxtotal))
|
2009-10-25 03:53:33 +03:00
|
|
|
if hasbinary and countwidth < 3:
|
|
|
|
countwidth = 3
|
2009-08-11 00:59:29 +04:00
|
|
|
graphwidth = width - countwidth - maxname - 6
|
2008-12-25 11:48:24 +03:00
|
|
|
if graphwidth < 10:
|
|
|
|
graphwidth = 10
|
|
|
|
|
2009-08-11 00:59:29 +04:00
|
|
|
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)))
|
2008-12-25 11:48:24 +03:00
|
|
|
|
2011-05-22 00:06:38 +04:00
|
|
|
for filename, adds, removes, isbinary in stats:
|
2011-10-24 15:41:19 +04:00
|
|
|
if isbinary:
|
2009-10-25 03:53:33 +03:00
|
|
|
count = 'Bin'
|
|
|
|
else:
|
|
|
|
count = adds + removes
|
2009-08-11 00:59:29 +04:00
|
|
|
pluses = '+' * scale(adds)
|
|
|
|
minuses = '-' * scale(removes)
|
2010-07-17 20:06:50 +04:00
|
|
|
output.append(' %s%s | %*s %s%s\n' %
|
2011-05-22 00:06:38 +04:00
|
|
|
(filename, ' ' * (maxname - encoding.colwidth(filename)),
|
|
|
|
countwidth, count, pluses, minuses))
|
2008-12-25 11:48:24 +03:00
|
|
|
|
|
|
|
if stats:
|
2012-05-12 17:54:54 +04:00
|
|
|
output.append(_(' %d files changed, %d insertions(+), '
|
|
|
|
'%d deletions(-)\n')
|
2009-03-20 22:38:50 +03:00
|
|
|
% (len(stats), totaladds, totalremoves))
|
2008-12-25 11:48:24 +03:00
|
|
|
|
|
|
|
return ''.join(output)
|
2010-04-03 00:22:06 +04:00
|
|
|
|
|
|
|
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', '')
|