patch.py
1347 lines
| 44.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / patch.py
|
r2861 | # patch.py - patch file parsing routines | ||
# | ||||
|
r2865 | # Copyright 2006 Brendan Cully <brendan@kublai.com> | ||
|
r4897 | # Copyright 2007 Chris Mason <chris.mason@oracle.com> | ||
|
r2865 | # | ||
|
r2861 | # This software may be used and distributed according to the terms | ||
# of the GNU General Public License, incorporated herein by reference. | ||||
|
r3891 | from i18n import _ | ||
|
r6211 | from node import hex, nullid, short | ||
|
r6738 | import base85, cmdutil, mdiff, util, revlog, diffhelpers, copies | ||
|
r6470 | import cStringIO, email.Parser, os, popen2, re, errno | ||
|
r3877 | import sys, tempfile, zlib | ||
|
r2866 | |||
|
r4897 | class PatchError(Exception): | ||
pass | ||||
|
r4900 | class NoHunks(PatchError): | ||
pass | ||||
|
r2933 | # helper functions | ||
def copyfile(src, dst, basedir=None): | ||||
if not basedir: | ||||
basedir = os.getcwd() | ||||
abssrc, absdst = [os.path.join(basedir, n) for n in (src, dst)] | ||||
if os.path.exists(absdst): | ||||
raise util.Abort(_("cannot create %s: destination already exists") % | ||||
dst) | ||||
targetdir = os.path.dirname(absdst) | ||||
if not os.path.isdir(targetdir): | ||||
os.makedirs(targetdir) | ||||
|
r3629 | |||
util.copyfile(abssrc, absdst) | ||||
|
r2933 | |||
# public functions | ||||
|
r2866 | def extract(ui, fileobj): | ||
'''extract patch from data read from fileobj. | ||||
|
r4263 | patch can be a normal patch or contained in an email message. | ||
|
r2866 | |||
|
r4263 | return tuple (filename, message, user, date, node, p1, p2). | ||
Any item in the returned tuple can be None. If filename is None, | ||||
fileobj did not contain a patch. Caller must unlink filename when done.''' | ||||
|
r2866 | |||
# attempt to detect the start of a patch | ||||
# (this heuristic is borrowed from quilt) | ||||
diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |' + | ||||
'retrieving revision [0-9]+(\.[0-9]+)*$|' + | ||||
'(---|\*\*\*)[ \t])', re.MULTILINE) | ||||
fd, tmpname = tempfile.mkstemp(prefix='hg-patch-') | ||||
tmpfp = os.fdopen(fd, 'w') | ||||
try: | ||||
msg = email.Parser.Parser().parse(fileobj) | ||||
|
r4777 | subject = msg['Subject'] | ||
|
r2866 | user = msg['From'] | ||
|
r5418 | gitsendmail = 'git-send-email' in msg.get('X-Mailer', '') | ||
|
r2866 | # should try to parse msg['Date'] | ||
date = None | ||||
|
r4263 | nodeid = None | ||
|
r4443 | branch = None | ||
|
r4263 | parents = [] | ||
|
r2866 | |||
|
r4777 | if subject: | ||
if subject.startswith('[PATCH'): | ||||
pend = subject.find(']') | ||||
|
r4208 | if pend >= 0: | ||
|
r4777 | subject = subject[pend+1:].lstrip() | ||
subject = subject.replace('\n\t', ' ') | ||||
ui.debug('Subject: %s\n' % subject) | ||||
|
r2866 | if user: | ||
ui.debug('From: %s\n' % user) | ||||
diffs_seen = 0 | ||||
ok_types = ('text/plain', 'text/x-diff', 'text/x-patch') | ||||
|
r4900 | message = '' | ||
|
r2866 | for part in msg.walk(): | ||
content_type = part.get_content_type() | ||||
ui.debug('Content-Type: %s\n' % content_type) | ||||
if content_type not in ok_types: | ||||
continue | ||||
payload = part.get_payload(decode=True) | ||||
m = diffre.search(payload) | ||||
if m: | ||||
|
r4220 | hgpatch = False | ||
ignoretext = False | ||||
|
r2866 | ui.debug(_('found patch at byte %d\n') % m.start(0)) | ||
diffs_seen += 1 | ||||
cfp = cStringIO.StringIO() | ||||
for line in payload[:m.start(0)].splitlines(): | ||||
if line.startswith('# HG changeset patch'): | ||||
ui.debug(_('patch generated by hg export\n')) | ||||
hgpatch = True | ||||
# drop earlier commit message content | ||||
cfp.seek(0) | ||||
cfp.truncate() | ||||
|
r4778 | subject = None | ||
|
r2866 | elif hgpatch: | ||
if line.startswith('# User '): | ||||
user = line[7:] | ||||
ui.debug('From: %s\n' % user) | ||||
elif line.startswith("# Date "): | ||||
date = line[7:] | ||||
|
r4443 | elif line.startswith("# Branch "): | ||
branch = line[9:] | ||||
|
r4263 | elif line.startswith("# Node ID "): | ||
nodeid = line[10:] | ||||
elif line.startswith("# Parent "): | ||||
parents.append(line[10:]) | ||||
|
r5418 | elif line == '---' and gitsendmail: | ||
|
r4220 | ignoretext = True | ||
if not line.startswith('# ') and not ignoretext: | ||||
|
r2866 | cfp.write(line) | ||
cfp.write('\n') | ||||
message = cfp.getvalue() | ||||
if tmpfp: | ||||
tmpfp.write(payload) | ||||
if not payload.endswith('\n'): | ||||
tmpfp.write('\n') | ||||
elif not diffs_seen and message and content_type == 'text/plain': | ||||
message += '\n' + payload | ||||
except: | ||||
tmpfp.close() | ||||
os.unlink(tmpname) | ||||
raise | ||||
|
r4777 | if subject and not message.startswith(subject): | ||
message = '%s\n%s' % (subject, message) | ||||
|
r2866 | tmpfp.close() | ||
if not diffs_seen: | ||||
os.unlink(tmpname) | ||||
|
r4443 | return None, message, user, date, branch, None, None, None | ||
|
r4263 | p1 = parents and parents.pop(0) or None | ||
p2 = parents and parents.pop(0) or None | ||||
|
r4443 | return tmpname, message, user, date, branch, nodeid, p1, p2 | ||
|
r2861 | |||
|
r3716 | GP_PATCH = 1 << 0 # we have to run patch | ||
GP_FILTER = 1 << 1 # there's some copy/rename operation | ||||
GP_BINARY = 1 << 2 # there's a binary patch | ||||
|
r5035 | def readgitpatch(fp, firstline=None): | ||
|
r2861 | """extract git-style metadata about patches from <patchname>""" | ||
class gitpatch: | ||||
"op is one of ADD, DELETE, RENAME, MODIFY or COPY" | ||||
def __init__(self, path): | ||||
self.path = path | ||||
self.oldpath = None | ||||
self.mode = None | ||||
self.op = 'MODIFY' | ||||
self.lineno = 0 | ||||
|
r3367 | self.binary = False | ||
|
r3223 | |||
|
r4897 | def reader(fp, firstline): | ||
|
r5035 | if firstline is not None: | ||
yield firstline | ||||
|
r4897 | for line in fp: | ||
yield line | ||||
|
r2861 | # Filter patch for git information | ||
gitre = re.compile('diff --git a/(.*) b/(.*)') | ||||
gp = None | ||||
gitpatches = [] | ||||
# Can have a git patch with only metadata, causing patch to complain | ||||
|
r3716 | dopatch = 0 | ||
|
r2861 | |||
lineno = 0 | ||||
|
r4897 | for line in reader(fp, firstline): | ||
|
r2861 | lineno += 1 | ||
if line.startswith('diff --git'): | ||||
m = gitre.match(line) | ||||
if m: | ||||
if gp: | ||||
gitpatches.append(gp) | ||||
|
r3673 | src, dst = m.group(1, 2) | ||
|
r2861 | gp = gitpatch(dst) | ||
gp.lineno = lineno | ||||
elif gp: | ||||
if line.startswith('--- '): | ||||
if gp.op in ('COPY', 'RENAME'): | ||||
|
r3716 | dopatch |= GP_FILTER | ||
|
r2861 | gitpatches.append(gp) | ||
gp = None | ||||
|
r3716 | dopatch |= GP_PATCH | ||
|
r2861 | continue | ||
if line.startswith('rename from '): | ||||
gp.op = 'RENAME' | ||||
gp.oldpath = line[12:].rstrip() | ||||
elif line.startswith('rename to '): | ||||
gp.path = line[10:].rstrip() | ||||
elif line.startswith('copy from '): | ||||
gp.op = 'COPY' | ||||
gp.oldpath = line[10:].rstrip() | ||||
elif line.startswith('copy to '): | ||||
gp.path = line[8:].rstrip() | ||||
elif line.startswith('deleted file'): | ||||
gp.op = 'DELETE' | ||||
elif line.startswith('new file mode '): | ||||
gp.op = 'ADD' | ||||
|
r5116 | gp.mode = int(line.rstrip()[-6:], 8) | ||
|
r2861 | elif line.startswith('new mode '): | ||
|
r5116 | gp.mode = int(line.rstrip()[-6:], 8) | ||
|
r3367 | elif line.startswith('GIT binary patch'): | ||
|
r3716 | dopatch |= GP_BINARY | ||
|
r3367 | gp.binary = True | ||
|
r2861 | if gp: | ||
gitpatches.append(gp) | ||||
if not gitpatches: | ||||
|
r3716 | dopatch = GP_PATCH | ||
|
r2861 | |||
return (dopatch, gitpatches) | ||||
|
r4897 | def patch(patchname, ui, strip=1, cwd=None, files={}): | ||
|
r4900 | """apply <patchname> to the working directory. | ||
returns whether patch was applied with fuzz factor.""" | ||||
patcher = ui.config('ui', 'patch') | ||||
args = [] | ||||
try: | ||||
if patcher: | ||||
return externalpatch(patcher, args, patchname, ui, strip, cwd, | ||||
files) | ||||
else: | ||||
try: | ||||
return internalpatch(patchname, ui, strip, cwd, files) | ||||
except NoHunks: | ||||
patcher = util.find_exe('gpatch') or util.find_exe('patch') | ||||
ui.debug('no valid hunks found; trying with %r instead\n' % | ||||
patcher) | ||||
if util.needbinarypatch(): | ||||
args.append('--binary') | ||||
return externalpatch(patcher, args, patchname, ui, strip, cwd, | ||||
files) | ||||
except PatchError, err: | ||||
s = str(err) | ||||
if s: | ||||
raise util.Abort(s) | ||||
else: | ||||
raise util.Abort(_('patch failed to apply')) | ||||
def externalpatch(patcher, args, patchname, ui, strip, cwd, files): | ||||
"""use <patcher> to apply <patchname> to the working directory. | ||||
returns whether patch was applied with fuzz factor.""" | ||||
fuzz = False | ||||
if cwd: | ||||
args.append('-d %s' % util.shellquote(cwd)) | ||||
|
r5481 | fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, | ||
|
r4900 | util.shellquote(patchname))) | ||
for line in fp: | ||||
line = line.rstrip() | ||||
ui.note(line + '\n') | ||||
if line.startswith('patching file '): | ||||
pf = util.parse_patch_output(line) | ||||
printed_file = False | ||||
files.setdefault(pf, (None, None)) | ||||
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') | ||||
code = fp.close() | ||||
if code: | ||||
raise PatchError(_("patch command failed: %s") % | ||||
util.explain_exit(code)[0]) | ||||
return fuzz | ||||
|
r5035 | def internalpatch(patchobj, ui, strip, cwd, files={}): | ||
"""use builtin patch to apply <patchobj> to the working directory. | ||||
|
r4900 | returns whether patch was applied with fuzz factor.""" | ||
|
r5035 | try: | ||
fp = file(patchobj, 'rb') | ||||
except TypeError: | ||||
fp = patchobj | ||||
|
r4897 | if cwd: | ||
curdir = os.getcwd() | ||||
os.chdir(cwd) | ||||
try: | ||||
ret = applydiff(ui, fp, files, strip=strip) | ||||
|
r4900 | finally: | ||
if cwd: | ||||
os.chdir(curdir) | ||||
|
r4897 | if ret < 0: | ||
|
r4900 | raise PatchError | ||
return ret > 0 | ||||
|
r4897 | |||
# @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 | ||||
unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@') | ||||
contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)') | ||||
class patchfile: | ||||
|
r5652 | def __init__(self, ui, fname, missing=False): | ||
|
r4897 | self.fname = fname | ||
self.ui = ui | ||||
|
r5652 | self.lines = [] | ||
self.exists = False | ||||
self.missing = missing | ||||
if not missing: | ||||
try: | ||||
fp = file(fname, 'rb') | ||||
self.lines = fp.readlines() | ||||
self.exists = True | ||||
except IOError: | ||||
pass | ||||
else: | ||||
self.ui.warn(_("unable to find '%s' for patching\n") % self.fname) | ||||
if not self.exists: | ||||
|
r4897 | dirname = os.path.dirname(fname) | ||
if dirname and not os.path.isdir(dirname): | ||||
|
r5653 | os.makedirs(dirname) | ||
|
r5143 | |||
|
r4897 | self.hash = {} | ||
self.dirty = 0 | ||||
self.offset = 0 | ||||
self.rej = [] | ||||
self.fileprinted = False | ||||
self.printfile(False) | ||||
self.hunks = 0 | ||||
def printfile(self, warn): | ||||
if self.fileprinted: | ||||
return | ||||
if warn or self.ui.verbose: | ||||
self.fileprinted = True | ||||
|
r4898 | s = _("patching file %s\n") % self.fname | ||
|
r4897 | if warn: | ||
self.ui.warn(s) | ||||
else: | ||||
self.ui.note(s) | ||||
def findlines(self, l, linenum): | ||||
# looks through the hash and finds candidate lines. The | ||||
# result is a list of line numbers sorted based on distance | ||||
# from linenum | ||||
def sorter(a, b): | ||||
vala = abs(a - linenum) | ||||
valb = abs(b - linenum) | ||||
return cmp(vala, valb) | ||||
|
r5143 | |||
|
r4897 | try: | ||
cand = self.hash[l] | ||||
except: | ||||
return [] | ||||
if len(cand) > 1: | ||||
# resort our list of potentials forward then back. | ||||
|
r5547 | cand.sort(sorter) | ||
|
r4897 | return cand | ||
def hashlines(self): | ||||
self.hash = {} | ||||
for x in xrange(len(self.lines)): | ||||
s = self.lines[x] | ||||
self.hash.setdefault(s, []).append(x) | ||||
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 | ||||
if self.hunks != 1: | ||||
hunkstr = "s" | ||||
else: | ||||
hunkstr = "" | ||||
fname = self.fname + ".rej" | ||||
self.ui.warn( | ||||
|
r4898 | _("%d out of %d hunk%s FAILED -- saving rejects to file %s\n") % | ||
(len(self.rej), self.hunks, hunkstr, fname)) | ||||
|
r4897 | try: os.unlink(fname) | ||
except: | ||||
pass | ||||
|
r4923 | fp = file(fname, 'wb') | ||
|
r4897 | base = os.path.basename(self.fname) | ||
fp.write("--- %s\n+++ %s\n" % (base, base)) | ||||
for x in self.rej: | ||||
for l in x.hunk: | ||||
fp.write(l) | ||||
if l[-1] != '\n': | ||||
fp.write("\n\ No newline at end of file\n") | ||||
def write(self, dest=None): | ||||
if self.dirty: | ||||
if not dest: | ||||
dest = self.fname | ||||
st = None | ||||
try: | ||||
st = os.lstat(dest) | ||||
|
r5477 | except OSError, inst: | ||
if inst.errno != errno.ENOENT: | ||||
raise | ||||
if st and st.st_nlink > 1: | ||||
os.unlink(dest) | ||||
|
r4923 | fp = file(dest, 'wb') | ||
|
r5477 | if st and st.st_nlink > 1: | ||
|
r4897 | os.chmod(dest, st.st_mode) | ||
fp.writelines(self.lines) | ||||
fp.close() | ||||
def close(self): | ||||
self.write() | ||||
self.write_rej() | ||||
def apply(self, h, reverse): | ||||
if not h.complete(): | ||||
|
r4898 | raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") % | ||
|
r4897 | (h.number, h.desc, len(h.a), h.lena, len(h.b), | ||
h.lenb)) | ||||
self.hunks += 1 | ||||
if reverse: | ||||
h.reverse() | ||||
|
r5652 | if self.missing: | ||
self.rej.append(h) | ||||
return -1 | ||||
|
r4897 | if self.exists and h.createfile(): | ||
|
r4898 | self.ui.warn(_("file %s already exists\n") % self.fname) | ||
|
r4897 | self.rej.append(h) | ||
return -1 | ||||
if isinstance(h, binhunk): | ||||
if h.rmfile(): | ||||
os.unlink(self.fname) | ||||
else: | ||||
self.lines[:] = h.new() | ||||
self.offset += len(h.new()) | ||||
self.dirty = 1 | ||||
return 0 | ||||
# fast case first, no offsets, no fuzz | ||||
old = h.old() | ||||
# patch starts counting at 1 unless we are adding the file | ||||
if h.starta == 0: | ||||
start = 0 | ||||
else: | ||||
start = h.starta + self.offset - 1 | ||||
orig_start = start | ||||
if diffhelpers.testhunk(old, self.lines, start) == 0: | ||||
if h.rmfile(): | ||||
os.unlink(self.fname) | ||||
else: | ||||
self.lines[start : start + h.lena] = h.new() | ||||
self.offset += h.lenb - h.lena | ||||
self.dirty = 1 | ||||
return 0 | ||||
# ok, we couldn't match the hunk. Lets look for offsets and fuzz it | ||||
self.hashlines() | ||||
if h.hunk[-1][0] != ' ': | ||||
# if the hunk tried to put something at the bottom of the file | ||||
# override the start line and use eof here | ||||
search_start = len(self.lines) | ||||
else: | ||||
search_start = orig_start | ||||
for fuzzlen in xrange(3): | ||||
for toponly in [ True, False ]: | ||||
old = h.old(fuzzlen, toponly) | ||||
cand = self.findlines(old[0][1:], search_start) | ||||
for l in cand: | ||||
if diffhelpers.testhunk(old, self.lines, l) == 0: | ||||
newlines = h.new(fuzzlen, toponly) | ||||
self.lines[l : l + len(old)] = newlines | ||||
self.offset += len(newlines) - len(old) | ||||
self.dirty = 1 | ||||
if fuzzlen: | ||||
fuzzstr = "with fuzz %d " % fuzzlen | ||||
f = self.ui.warn | ||||
self.printfile(True) | ||||
else: | ||||
fuzzstr = "" | ||||
f = self.ui.note | ||||
offset = l - orig_start - fuzzlen | ||||
if offset == 1: | ||||
linestr = "line" | ||||
else: | ||||
linestr = "lines" | ||||
|
r4898 | f(_("Hunk #%d succeeded at %d %s(offset %d %s).\n") % | ||
(h.number, l+1, fuzzstr, offset, linestr)) | ||||
|
r4897 | return fuzzlen | ||
self.printfile(True) | ||||
|
r4898 | self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start)) | ||
|
r4897 | self.rej.append(h) | ||
return -1 | ||||
class hunk: | ||||
|
r6280 | def __init__(self, desc, num, lr, context, create=False, remove=False): | ||
|
r4897 | self.number = num | ||
self.desc = desc | ||||
self.hunk = [ desc ] | ||||
self.a = [] | ||||
self.b = [] | ||||
if context: | ||||
self.read_context_hunk(lr) | ||||
else: | ||||
self.read_unified_hunk(lr) | ||||
|
r6280 | self.create = create | ||
self.remove = remove and not create | ||||
|
r4897 | |||
def read_unified_hunk(self, lr): | ||||
m = unidesc.match(self.desc) | ||||
if not m: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d") % self.number) | ||
|
r4897 | self.starta, foo, self.lena, self.startb, foo2, self.lenb = m.groups() | ||
if self.lena == None: | ||||
self.lena = 1 | ||||
else: | ||||
self.lena = int(self.lena) | ||||
if self.lenb == None: | ||||
self.lenb = 1 | ||||
else: | ||||
self.lenb = int(self.lenb) | ||||
self.starta = int(self.starta) | ||||
self.startb = int(self.startb) | ||||
diffhelpers.addlines(lr.fp, self.hunk, self.lena, self.lenb, self.a, self.b) | ||||
# 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: | ||||
del self.hunk[-1] | ||||
del self.a[-1] | ||||
del self.b[-1] | ||||
self.lena -= 1 | ||||
self.lenb -= 1 | ||||
def read_context_hunk(self, lr): | ||||
self.desc = lr.readline() | ||||
m = contextdesc.match(self.desc) | ||||
if not m: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d") % self.number) | ||
|
r4897 | foo, self.starta, foo2, aend, foo3 = m.groups() | ||
self.starta = int(self.starta) | ||||
if aend == None: | ||||
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('---'): | ||||
lr.push(l) | ||||
break | ||||
s = l[2:] | ||||
if l.startswith('- ') or l.startswith('! '): | ||||
u = '-' + s | ||||
elif l.startswith(' '): | ||||
u = ' ' + s | ||||
else: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d old text line %d") % | ||
(self.number, x)) | ||||
|
r4897 | self.a.append(u) | ||
self.hunk.append(u) | ||||
l = lr.readline() | ||||
if l.startswith('\ '): | ||||
s = self.a[-1][:-1] | ||||
self.a[-1] = s | ||||
self.hunk[-1] = s | ||||
l = lr.readline() | ||||
m = contextdesc.match(l) | ||||
if not m: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d") % self.number) | ||
|
r4897 | foo, self.startb, foo2, bend, foo3 = m.groups() | ||
self.startb = int(self.startb) | ||||
if bend == None: | ||||
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('\ '): | ||||
s = self.b[-1][:-1] | ||||
self.b[-1] = s | ||||
self.hunk[hunki-1] = s | ||||
continue | ||||
if not l: | ||||
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: | ||||
# this can happen when the hunk does not add any lines | ||||
lr.push(l) | ||||
break | ||||
else: | ||||
|
r4898 | raise PatchError(_("bad hunk #%d old text line %d") % | ||
(self.number, x)) | ||||
|
r4897 | self.b.append(s) | ||
while True: | ||||
if hunki >= len(self.hunk): | ||||
h = "" | ||||
else: | ||||
h = self.hunk[hunki] | ||||
hunki += 1 | ||||
if h == u: | ||||
break | ||||
elif h.startswith('-'): | ||||
continue | ||||
else: | ||||
self.hunk.insert(hunki-1, u) | ||||
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 | ||||
def reverse(self): | ||||
|
r6280 | self.create, self.remove = self.remove, self.create | ||
|
r4897 | origlena = self.lena | ||
origstarta = self.starta | ||||
self.lena = self.lenb | ||||
self.starta = self.startb | ||||
self.lenb = origlena | ||||
self.startb = origstarta | ||||
self.a = [] | ||||
self.b = [] | ||||
# self.hunk[0] is the @@ description | ||||
for x in xrange(1, len(self.hunk)): | ||||
o = self.hunk[x] | ||||
if o.startswith('-'): | ||||
n = '+' + o[1:] | ||||
self.b.append(o[1:]) | ||||
elif o.startswith('+'): | ||||
n = '-' + o[1:] | ||||
self.a.append(n) | ||||
else: | ||||
n = o | ||||
self.b.append(o[1:]) | ||||
self.a.append(o) | ||||
self.hunk[x] = o | ||||
def fix_newline(self): | ||||
diffhelpers.fix_newline(self.hunk, self.a, self.b) | ||||
def complete(self): | ||||
return len(self.a) == self.lena and len(self.b) == self.lenb | ||||
def createfile(self): | ||||
|
r6280 | return self.starta == 0 and self.lena == 0 and self.create | ||
|
r4897 | |||
def rmfile(self): | ||||
|
r6280 | return self.startb == 0 and self.lenb == 0 and self.remove | ||
|
r4897 | |||
def fuzzit(self, l, fuzz, toponly): | ||||
# 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. | ||||
fuzz = min(fuzz, len(l)-1) | ||||
if fuzz: | ||||
top = 0 | ||||
bot = 0 | ||||
hlen = len(self.hunk) | ||||
for x in xrange(hlen-1): | ||||
# the hunk starts with the @@ line, so use x+1 | ||||
if self.hunk[x+1][0] == ' ': | ||||
top += 1 | ||||
else: | ||||
break | ||||
if not toponly: | ||||
for x in xrange(hlen-1): | ||||
if self.hunk[hlen-bot-1][0] == ' ': | ||||
bot += 1 | ||||
else: | ||||
break | ||||
# top and bot now count context in the hunk | ||||
# adjust them if either one is short | ||||
context = max(top, bot, 3) | ||||
if bot < context: | ||||
bot = max(0, fuzz - (context - bot)) | ||||
else: | ||||
bot = min(fuzz, bot) | ||||
if top < context: | ||||
top = max(0, fuzz - (context - top)) | ||||
else: | ||||
top = min(fuzz, top) | ||||
return l[top:len(l)-bot] | ||||
return l | ||||
def old(self, fuzz=0, toponly=False): | ||||
return self.fuzzit(self.a, fuzz, toponly) | ||||
|
r5143 | |||
|
r4897 | def newctrl(self): | ||
res = [] | ||||
for x in self.hunk: | ||||
c = x[0] | ||||
if c == ' ' or c == '+': | ||||
res.append(x) | ||||
return res | ||||
def new(self, fuzz=0, toponly=False): | ||||
return self.fuzzit(self.b, fuzz, toponly) | ||||
class binhunk: | ||||
'A binary patch file. Only understands literals so far.' | ||||
def __init__(self, gitpatch): | ||||
self.gitpatch = gitpatch | ||||
self.text = None | ||||
self.hunk = ['GIT binary patch\n'] | ||||
def createfile(self): | ||||
return self.gitpatch.op in ('ADD', 'RENAME', 'COPY') | ||||
def rmfile(self): | ||||
return self.gitpatch.op == 'DELETE' | ||||
def complete(self): | ||||
return self.text is not None | ||||
def new(self): | ||||
return [self.text] | ||||
def extract(self, fp): | ||||
line = fp.readline() | ||||
self.hunk.append(line) | ||||
|
r3367 | while line and not line.startswith('literal '): | ||
|
r4897 | line = fp.readline() | ||
self.hunk.append(line) | ||||
|
r3367 | if not line: | ||
|
r4898 | raise PatchError(_('could not extract binary patch')) | ||
|
r4897 | size = int(line[8:].rstrip()) | ||
|
r3367 | dec = [] | ||
|
r4897 | line = fp.readline() | ||
self.hunk.append(line) | ||||
while len(line) > 1: | ||||
|
r3374 | l = line[0] | ||
if l <= 'Z' and l >= 'A': | ||||
l = ord(l) - ord('A') + 1 | ||||
else: | ||||
l = ord(l) - ord('a') + 27 | ||||
|
r4897 | dec.append(base85.b85decode(line[1:-1])[:l]) | ||
line = fp.readline() | ||||
self.hunk.append(line) | ||||
|
r3367 | text = zlib.decompress(''.join(dec)) | ||
if len(text) != size: | ||||
|
r4898 | raise PatchError(_('binary patch is %d bytes, not %d') % | ||
|
r4897 | len(text), size) | ||
self.text = text | ||||
|
r3367 | |||
|
r4897 | def parsefilename(str): | ||
# --- filename \t|space stuff | ||||
|
r5851 | s = str[4:].rstrip('\r\n') | ||
|
r4897 | i = s.find('\t') | ||
if i < 0: | ||||
i = s.find(' ') | ||||
if i < 0: | ||||
return s | ||||
return s[:i] | ||||
|
r2861 | |||
|
r4897 | def selectfile(afile_orig, bfile_orig, hunk, strip, reverse): | ||
def pathstrip(path, count=1): | ||||
pathlen = len(path) | ||||
i = 0 | ||||
if count == 0: | ||||
|
r6520 | return '', path.rstrip() | ||
|
r4897 | while count > 0: | ||
|
r4922 | i = path.find('/', i) | ||
|
r4897 | if i == -1: | ||
|
r4898 | raise PatchError(_("unable to strip away %d dirs from %s") % | ||
|
r4897 | (count, path)) | ||
i += 1 | ||||
# consume '//' in the path | ||||
|
r4922 | while i < pathlen - 1 and path[i] == '/': | ||
|
r4897 | i += 1 | ||
count -= 1 | ||||
|
r6295 | return path[:i].lstrip(), path[i:].rstrip() | ||
|
r3367 | |||
|
r4897 | nulla = afile_orig == "/dev/null" | ||
nullb = bfile_orig == "/dev/null" | ||||
|
r6295 | abase, afile = pathstrip(afile_orig, strip) | ||
|
r5651 | gooda = not nulla and os.path.exists(afile) | ||
|
r6295 | bbase, bfile = pathstrip(bfile_orig, strip) | ||
|
r4897 | if afile == bfile: | ||
goodb = gooda | ||||
else: | ||||
|
r5651 | goodb = not nullb and os.path.exists(bfile) | ||
|
r4897 | createfunc = hunk.createfile | ||
if reverse: | ||||
createfunc = hunk.rmfile | ||||
|
r5652 | missing = not goodb and not gooda and not createfunc() | ||
|
r6295 | # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the | ||
# diff is between a file and its backup. In this case, the original | ||||
# file should be patched (see original mpatch code). | ||||
isbackup = (abase == bbase and bfile.startswith(afile)) | ||||
|
r5652 | fname = None | ||
if not missing: | ||||
if gooda and goodb: | ||||
|
r6295 | fname = isbackup and afile or bfile | ||
|
r5652 | elif gooda: | ||
|
r4897 | fname = afile | ||
|
r5760 | |||
|
r5652 | if not fname: | ||
if not nullb: | ||||
|
r6295 | fname = isbackup and afile or bfile | ||
|
r5652 | elif not nulla: | ||
|
r4897 | fname = afile | ||
|
r5652 | else: | ||
raise PatchError(_("undefined source and destination files")) | ||||
|
r5760 | |||
|
r5652 | return fname, missing | ||
|
r4897 | |||
class linereader: | ||||
# simple class to allow pushing lines back into the input stream | ||||
def __init__(self, fp): | ||||
self.fp = fp | ||||
self.buf = [] | ||||
def push(self, line): | ||||
self.buf.append(line) | ||||
|
r2861 | |||
|
r4897 | def readline(self): | ||
if self.buf: | ||||
l = self.buf[0] | ||||
del self.buf[0] | ||||
return l | ||||
return self.fp.readline() | ||||
|
r5650 | def iterhunks(ui, fp, sourcefile=None): | ||
"""Read a patch and yield the following events: | ||||
- ("file", afile, bfile, firsthunk): select a new target file. | ||||
- ("hunk", hunk): a new hunk is ready to be applied, follows a | ||||
"file" event. | ||||
- ("git", gitchanges): current diff is in git format, gitchanges | ||||
maps filenames to gitpatch records. Unique event. | ||||
""" | ||||
|
r4897 | |||
|
r5650 | def scangitpatch(fp, firstline): | ||
|
r4897 | '''git patches can modify a file, then copy that file to | ||
a new file, but expect the source to be the unmodified form. | ||||
So we scan the patch looking for that case so we can do | ||||
the copies ahead of time.''' | ||||
|
r2861 | |||
|
r4897 | pos = 0 | ||
try: | ||||
pos = fp.tell() | ||||
except IOError: | ||||
fp = cStringIO.StringIO(fp.read()) | ||||
(dopatch, gitpatches) = readgitpatch(fp, firstline) | ||||
fp.seek(pos) | ||||
|
r2861 | |||
|
r4897 | return fp, dopatch, gitpatches | ||
|
r5650 | changed = {} | ||
|
r4897 | current_hunk = None | ||
afile = "" | ||||
bfile = "" | ||||
state = None | ||||
hunknum = 0 | ||||
|
r5650 | emitfile = False | ||
|
r4897 | |||
git = False | ||||
gitre = re.compile('diff --git (a/.*) (b/.*)') | ||||
|
r2861 | |||
|
r4897 | # our states | ||
BFILE = 1 | ||||
context = None | ||||
lr = linereader(fp) | ||||
dopatch = True | ||||
|
r6179 | # gitworkdone is True if a git operation (copy, rename, ...) was | ||
# performed already for the current file. Useful when the file | ||||
# section may have no hunk. | ||||
|
r4897 | gitworkdone = False | ||
|
r2861 | |||
|
r4897 | while True: | ||
newfile = False | ||||
x = lr.readline() | ||||
if not x: | ||||
break | ||||
if current_hunk: | ||||
if x.startswith('\ '): | ||||
current_hunk.fix_newline() | ||||
|
r5650 | yield 'hunk', current_hunk | ||
|
r4897 | current_hunk = None | ||
gitworkdone = False | ||||
if ((sourcefile or state == BFILE) and ((not context and x[0] == '@') or | ||||
((context or context == None) and x.startswith('***************')))): | ||||
try: | ||||
if context == None and x.startswith('***************'): | ||||
context = True | ||||
|
r5852 | gpatch = changed.get(bfile[2:], (None, None))[1] | ||
|
r6280 | create = afile == '/dev/null' or gpatch and gpatch.op == 'ADD' | ||
remove = bfile == '/dev/null' or gpatch and gpatch.op == 'DELETE' | ||||
current_hunk = hunk(x, hunknum + 1, lr, context, create, remove) | ||||
|
r4898 | except PatchError, err: | ||
ui.debug(err) | ||||
|
r4897 | current_hunk = None | ||
continue | ||||
hunknum += 1 | ||||
|
r5650 | if emitfile: | ||
emitfile = False | ||||
yield 'file', (afile, bfile, current_hunk) | ||||
|
r4897 | elif state == BFILE and x.startswith('GIT binary patch'): | ||
current_hunk = binhunk(changed[bfile[2:]][1]) | ||||
|
r5581 | hunknum += 1 | ||
|
r5650 | if emitfile: | ||
emitfile = False | ||||
yield 'file', (afile, bfile, current_hunk) | ||||
|
r4897 | current_hunk.extract(fp) | ||
elif x.startswith('diff --git'): | ||||
# check for git diff, scanning the whole patch file if needed | ||||
m = gitre.match(x) | ||||
if m: | ||||
afile, bfile = m.group(1, 2) | ||||
if not git: | ||||
git = True | ||||
fp, dopatch, gitpatches = scangitpatch(fp, x) | ||||
|
r5650 | yield 'git', gitpatches | ||
|
r4897 | for gp in gitpatches: | ||
changed[gp.path] = (gp.op, gp) | ||||
# else error? | ||||
# copy/rename + modify should modify target, not source | ||||
|
r6179 | gitop = changed.get(bfile[2:], (None, None))[0] | ||
if gitop in ('COPY', 'DELETE', 'RENAME'): | ||||
|
r4897 | afile = bfile | ||
gitworkdone = True | ||||
newfile = True | ||||
elif x.startswith('---'): | ||||
# check for a unified diff | ||||
l2 = lr.readline() | ||||
if not l2.startswith('+++'): | ||||
lr.push(l2) | ||||
continue | ||||
newfile = True | ||||
context = False | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
elif x.startswith('***'): | ||||
# check for a context diff | ||||
l2 = lr.readline() | ||||
if not l2.startswith('---'): | ||||
lr.push(l2) | ||||
continue | ||||
l3 = lr.readline() | ||||
lr.push(l3) | ||||
if not l3.startswith("***************"): | ||||
lr.push(l2) | ||||
continue | ||||
newfile = True | ||||
context = True | ||||
afile = parsefilename(x) | ||||
bfile = parsefilename(l2) | ||||
|
r3057 | |||
|
r4897 | if newfile: | ||
|
r5650 | emitfile = True | ||
|
r4897 | state = BFILE | ||
hunknum = 0 | ||||
if current_hunk: | ||||
if current_hunk.complete(): | ||||
|
r5650 | yield 'hunk', current_hunk | ||
else: | ||||
raise PatchError(_("malformed patch %s %s") % (afile, | ||||
current_hunk.desc)) | ||||
if hunknum == 0 and dopatch and not gitworkdone: | ||||
raise NoHunks | ||||
def applydiff(ui, fp, changed, strip=1, sourcefile=None, reverse=False, | ||||
rejmerge=None, updatedir=None): | ||||
"""reads a patch from fp and tries to apply it. The dict 'changed' is | ||||
filled in with all of the filenames changed by the patch. Returns 0 | ||||
for a clean patch, -1 if any rejects were found and 1 if there was | ||||
any fuzz.""" | ||||
rejects = 0 | ||||
err = 0 | ||||
current_file = None | ||||
gitpatches = None | ||||
def closefile(): | ||||
if not current_file: | ||||
return 0 | ||||
current_file.close() | ||||
if rejmerge: | ||||
rejmerge(current_file) | ||||
return len(current_file.rej) | ||||
for state, values in iterhunks(ui, fp, sourcefile): | ||||
if state == 'hunk': | ||||
if not current_file: | ||||
continue | ||||
current_hunk = values | ||||
|
r4897 | ret = current_file.apply(current_hunk, reverse) | ||
|
r4899 | if ret >= 0: | ||
changed.setdefault(current_file.fname, (None, None)) | ||||
if ret > 0: | ||||
err = 1 | ||||
|
r5650 | elif state == 'file': | ||
rejects += closefile() | ||||
afile, bfile, first_hunk = values | ||||
try: | ||||
if sourcefile: | ||||
current_file = patchfile(ui, sourcefile) | ||||
else: | ||||
|
r5652 | current_file, missing = selectfile(afile, bfile, first_hunk, | ||
|
r5650 | strip, reverse) | ||
|
r5652 | current_file = patchfile(ui, current_file, missing) | ||
|
r5650 | except PatchError, err: | ||
ui.warn(str(err) + '\n') | ||||
current_file, current_hunk = None, None | ||||
rejects += 1 | ||||
continue | ||||
elif state == 'git': | ||||
gitpatches = values | ||||
for gp in gitpatches: | ||||
if gp.op in ('COPY', 'RENAME'): | ||||
copyfile(gp.oldpath, gp.path) | ||||
|
r5760 | changed[gp.path] = (gp.op, gp) | ||
|
r4897 | else: | ||
|
r5650 | raise util.Abort(_('unsupported parser state: %s') % state) | ||
|
r5649 | |||
|
r5650 | rejects += closefile() | ||
if updatedir and gitpatches: | ||||
|
r4897 | updatedir(gitpatches) | ||
if rejects: | ||||
return -1 | ||||
return err | ||||
|
r2874 | |||
|
r3554 | def diffopts(ui, opts={}, untrusted=False): | ||
|
r6467 | def get(key, name=None, getter=ui.configbool): | ||
|
r3554 | return (opts.get(key) or | ||
|
r6467 | getter('diff', name or key, None, untrusted=untrusted)) | ||
|
r2888 | return mdiff.diffopts( | ||
text=opts.get('text'), | ||||
|
r3554 | git=get('git'), | ||
nodates=get('nodates'), | ||||
showfunc=get('show_function', 'showfunc'), | ||||
ignorews=get('ignore_all_space', 'ignorews'), | ||||
ignorewsamount=get('ignore_space_change', 'ignorewsamount'), | ||||
|
r6040 | ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'), | ||
|
r6467 | context=get('unified', getter=ui.config)) | ||
|
r2888 | |||
|
r4917 | def updatedir(ui, repo, patches): | ||
|
r2933 | '''Update dirstate after patch application according to metadata''' | ||
if not patches: | ||||
return | ||||
copies = [] | ||||
|
r3701 | removes = {} | ||
|
r2933 | cfiles = patches.keys() | ||
cwd = repo.getcwd() | ||||
if cwd: | ||||
|
r4229 | cfiles = [util.pathto(repo.root, cwd, f) for f in patches.keys()] | ||
|
r2933 | for f in patches: | ||
ctype, gp = patches[f] | ||||
if ctype == 'RENAME': | ||||
|
r5403 | copies.append((gp.oldpath, gp.path)) | ||
|
r3701 | removes[gp.oldpath] = 1 | ||
|
r2933 | elif ctype == 'COPY': | ||
|
r5403 | copies.append((gp.oldpath, gp.path)) | ||
|
r2933 | elif ctype == 'DELETE': | ||
|
r3701 | removes[gp.path] = 1 | ||
|
r5403 | for src, dst in copies: | ||
|
r4917 | repo.copy(src, dst) | ||
|
r3701 | removes = removes.keys() | ||
|
r2933 | if removes: | ||
|
r3701 | removes.sort() | ||
|
r4917 | repo.remove(removes, True) | ||
|
r2933 | for f in patches: | ||
ctype, gp = patches[f] | ||||
if gp and gp.mode: | ||||
|
r5706 | flags = '' | ||
if gp.mode & 0100: | ||||
flags = 'x' | ||||
elif gp.mode & 020000: | ||||
flags = 'l' | ||||
|
r2933 | dst = os.path.join(repo.root, gp.path) | ||
|
r3588 | # patch won't create empty files | ||
if ctype == 'ADD' and not os.path.exists(dst): | ||||
|
r5706 | repo.wwrite(gp.path, '', flags) | ||
|
r4006 | else: | ||
|
r5706 | util.set_flags(dst, flags) | ||
|
r4917 | cmdutil.addremove(repo, cfiles) | ||
|
r2933 | files = patches.keys() | ||
files.extend([r for r in removes if r not in files]) | ||||
files.sort() | ||||
return files | ||||
|
r5033 | def b85diff(to, tn): | ||
|
r3367 | '''print base85-encoded binary diff''' | ||
def gitindex(text): | ||||
if not text: | ||||
return '0' * 40 | ||||
l = len(text) | ||||
|
r6470 | s = util.sha1('blob %d\0' % l) | ||
|
r3367 | 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: | ||||
yield text[i:i+csize] | ||||
i += csize | ||||
|
r4105 | tohash = gitindex(to) | ||
tnhash = gitindex(tn) | ||||
if tohash == tnhash: | ||||
|
r4106 | return "" | ||
|
r3367 | # TODO: deltas | ||
|
r4106 | 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) | ||||
|
r3367 | |||
|
r6602 | def diff(repo, node1=None, node2=None, match=None, | ||
|
r2874 | fp=None, changes=None, opts=None): | ||
'''print diff of changes to files between two nodes, or node and | ||||
working directory. | ||||
if node1 is None, use first dirstate parent instead. | ||||
if node2 is None, compare node1 with working directory.''' | ||||
|
r6602 | if not match: | ||
match = cmdutil.matchall(repo) | ||||
|
r2874 | if opts is None: | ||
opts = mdiff.defaultopts | ||||
if fp is None: | ||||
fp = repo.ui | ||||
if not node1: | ||||
node1 = repo.dirstate.parents()[0] | ||||
|
r2934 | |||
|
r3967 | flcache = {} | ||
def getfilectx(f, ctx): | ||||
flctx = ctx.filectx(f, filelog=flcache.get(f)) | ||||
if f not in flcache: | ||||
flcache[f] = flctx._filelog | ||||
return flctx | ||||
|
r2934 | |||
|
r2874 | # reading the data for node1 early allows it to play nicely | ||
|
r2875 | # with repo.status and the revlog cache. | ||
|
r6738 | ctx1 = repo.changectx(node1) | ||
|
r3967 | # force manifest reading | ||
man1 = ctx1.manifest() | ||||
date1 = util.datestr(ctx1.date()) | ||||
|
r2874 | |||
if not changes: | ||||
|
r6603 | changes = repo.status(node1, node2, match=match)[:5] | ||
|
r2874 | modified, added, removed, deleted, unknown = changes | ||
if not modified and not added and not removed: | ||||
return | ||||
|
r6740 | ctx2 = repo.changectx(node2) | ||
|
r3967 | if node2: | ||
|
r4496 | execf2 = ctx2.manifest().execf | ||
|
r5116 | linkf2 = ctx2.manifest().linkf | ||
|
r3967 | else: | ||
|
r4496 | execf2 = util.execfunc(repo.root, None) | ||
|
r5116 | linkf2 = util.linkfunc(repo.root, None) | ||
|
r4496 | if execf2 is None: | ||
|
r5116 | mc = ctx2.parents()[0].manifest().copy() | ||
execf2 = mc.execf | ||||
linkf2 = mc.linkf | ||||
|
r3967 | |||
|
r2874 | if repo.ui.quiet: | ||
r = None | ||||
else: | ||||
|
r3387 | hexfunc = repo.ui.debugflag and hex or short | ||
|
r2874 | r = [hexfunc(node) for node in [node1, node2] if node] | ||
|
r2907 | if opts.git: | ||
|
r6275 | copy, diverge = copies.copies(repo, ctx1, ctx2, repo.changectx(nullid)) | ||
for k, v in copy.items(): | ||||
copy[v] = k | ||||
|
r2907 | |||
|
r2874 | all = modified + added + removed | ||
all.sort() | ||||
|
r3702 | gone = {} | ||
|
r3996 | |||
|
r2874 | for f in all: | ||
to = None | ||||
tn = None | ||||
|
r2907 | dodiff = True | ||
|
r3329 | header = [] | ||
|
r3967 | if f in man1: | ||
to = getfilectx(f, ctx1).data() | ||||
|
r2874 | if f not in removed: | ||
|
r3967 | tn = getfilectx(f, ctx2).data() | ||
|
r5482 | a, b = f, f | ||
|
r2907 | if opts.git: | ||
|
r5116 | def gitmode(x, l): | ||
return l and '120000' or (x and '100755' or '100644') | ||||
|
r2907 | def addmodehdr(header, omode, nmode): | ||
if omode != nmode: | ||||
header.append('old mode %s\n' % omode) | ||||
header.append('new mode %s\n' % nmode) | ||||
if f in added: | ||||
|
r5116 | mode = gitmode(execf2(f), linkf2(f)) | ||
|
r6275 | if f in copy: | ||
a = copy[f] | ||||
|
r5116 | omode = gitmode(man1.execf(a), man1.linkf(a)) | ||
|
r2907 | addmodehdr(header, omode, mode) | ||
|
r3702 | if a in removed and a not in gone: | ||
op = 'rename' | ||||
gone[a] = 1 | ||||
else: | ||||
op = 'copy' | ||||
|
r2907 | header.append('%s from %s\n' % (op, a)) | ||
header.append('%s to %s\n' % (op, f)) | ||||
|
r3967 | to = getfilectx(a, ctx1).data() | ||
|
r2907 | else: | ||
header.append('new file mode %s\n' % mode) | ||||
|
r4092 | if util.binary(tn): | ||
dodiff = 'binary' | ||||
|
r2907 | elif f in removed: | ||
|
r6275 | # have we already reported a copy above? | ||
if f in copy and copy[f] in added and copy[copy[f]] == f: | ||||
|
r2907 | dodiff = False | ||
else: | ||||
|
r5116 | mode = gitmode(man1.execf(f), man1.linkf(f)) | ||
|
r2907 | header.append('deleted file mode %s\n' % mode) | ||
else: | ||||
|
r5116 | omode = gitmode(man1.execf(f), man1.linkf(f)) | ||
nmode = gitmode(execf2(f), linkf2(f)) | ||||
|
r2907 | addmodehdr(header, omode, nmode) | ||
|
r3367 | if util.binary(to) or util.binary(tn): | ||
dodiff = 'binary' | ||||
|
r2907 | r = None | ||
|
r3329 | header.insert(0, 'diff --git a/%s b/%s\n' % (a, b)) | ||
|
r4106 | if dodiff: | ||
if dodiff == 'binary': | ||||
|
r5033 | text = b85diff(to, tn) | ||
|
r4106 | else: | ||
|
r4108 | text = mdiff.unidiff(to, date1, | ||
# ctx2 date may be dynamic | ||||
tn, util.datestr(ctx2.date()), | ||||
|
r5482 | a, b, r, opts=opts) | ||
|
r3329 | if text or len(header) > 1: | ||
|
r2907 | fp.write(''.join(header)) | ||
|
r3329 | fp.write(text) | ||
|
r2874 | |||
def export(repo, revs, template='hg-%h.patch', fp=None, switch_parent=False, | ||||
opts=None): | ||||
'''export changesets as hg patches.''' | ||||
total = len(revs) | ||||
|
r3900 | revwidth = max([len(str(rev)) for rev in revs]) | ||
|
r2874 | |||
|
r3970 | def single(rev, seqno, fp): | ||
ctx = repo.changectx(rev) | ||||
node = ctx.node() | ||||
parents = [p.node() for p in ctx.parents() if p] | ||||
|
r4436 | branch = ctx.branch() | ||
|
r2874 | if switch_parent: | ||
parents.reverse() | ||||
prev = (parents and parents[0]) or nullid | ||||
if not fp: | ||||
fp = cmdutil.make_file(repo, template, node, total=total, | ||||
seqno=seqno, revwidth=revwidth) | ||||
|
r4125 | if fp != sys.stdout and hasattr(fp, 'name'): | ||
|
r2874 | repo.ui.note("%s\n" % fp.name) | ||
fp.write("# HG changeset patch\n") | ||||
|
r3970 | fp.write("# User %s\n" % ctx.user()) | ||
fp.write("# Date %d %d\n" % ctx.date()) | ||||
|
r4436 | if branch and (branch != 'default'): | ||
fp.write("# Branch %s\n" % branch) | ||||
|
r2874 | fp.write("# Node ID %s\n" % hex(node)) | ||
fp.write("# Parent %s\n" % hex(prev)) | ||||
if len(parents) > 1: | ||||
fp.write("# Parent %s\n" % hex(parents[1])) | ||||
|
r3970 | fp.write(ctx.description().rstrip()) | ||
|
r2874 | fp.write("\n\n") | ||
diff(repo, prev, node, fp=fp, opts=opts) | ||||
if fp not in (sys.stdout, repo.ui): | ||||
fp.close() | ||||
|
r3900 | for seqno, rev in enumerate(revs): | ||
|
r3970 | single(rev, seqno+1, fp) | ||
|
r3096 | |||
def diffstat(patchlines): | ||||
|
r4488 | if not util.find_exe('diffstat'): | ||
|
r4316 | return | ||
|
r3096 | fd, name = tempfile.mkstemp(prefix="hg-patchbomb-", suffix=".txt") | ||
try: | ||||
p = popen2.Popen3('diffstat -p1 -w79 2>/dev/null > ' + name) | ||||
try: | ||||
|
r5878 | for line in patchlines: | ||
p.tochild.write(line + "\n") | ||||
|
r3096 | p.tochild.close() | ||
if p.wait(): return | ||||
fp = os.fdopen(fd, 'r') | ||||
stat = [] | ||||
for line in fp: stat.append(line.lstrip()) | ||||
last = stat.pop() | ||||
stat.insert(0, last) | ||||
stat = ''.join(stat) | ||||
return stat | ||||
except: raise | ||||
finally: | ||||
try: os.unlink(name) | ||||
except: pass | ||||