##// END OF EJS Templates
py3: bytestr() bytes to get bytechar while iterating on it...
Pulkit Goyal -
r38096:e887381e default
parent child Browse files
Show More
@@ -1,2950 +1,2950 b''
1 # patch.py - patch file parsing routines
1 # patch.py - patch file parsing routines
2 #
2 #
3 # Copyright 2006 Brendan Cully <brendan@kublai.com>
3 # Copyright 2006 Brendan Cully <brendan@kublai.com>
4 # Copyright 2007 Chris Mason <chris.mason@oracle.com>
4 # Copyright 2007 Chris Mason <chris.mason@oracle.com>
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9 from __future__ import absolute_import, print_function
9 from __future__ import absolute_import, print_function
10
10
11 import collections
11 import collections
12 import contextlib
12 import contextlib
13 import copy
13 import copy
14 import email
14 import email
15 import errno
15 import errno
16 import hashlib
16 import hashlib
17 import os
17 import os
18 import posixpath
18 import posixpath
19 import re
19 import re
20 import shutil
20 import shutil
21 import tempfile
21 import tempfile
22 import zlib
22 import zlib
23
23
24 from .i18n import _
24 from .i18n import _
25 from .node import (
25 from .node import (
26 hex,
26 hex,
27 short,
27 short,
28 )
28 )
29 from . import (
29 from . import (
30 copies,
30 copies,
31 diffhelper,
31 diffhelper,
32 encoding,
32 encoding,
33 error,
33 error,
34 mail,
34 mail,
35 mdiff,
35 mdiff,
36 pathutil,
36 pathutil,
37 pycompat,
37 pycompat,
38 scmutil,
38 scmutil,
39 similar,
39 similar,
40 util,
40 util,
41 vfs as vfsmod,
41 vfs as vfsmod,
42 )
42 )
43 from .utils import (
43 from .utils import (
44 dateutil,
44 dateutil,
45 procutil,
45 procutil,
46 stringutil,
46 stringutil,
47 )
47 )
48
48
49 stringio = util.stringio
49 stringio = util.stringio
50
50
51 gitre = re.compile(br'diff --git a/(.*) b/(.*)')
51 gitre = re.compile(br'diff --git a/(.*) b/(.*)')
52 tabsplitter = re.compile(br'(\t+|[^\t]+)')
52 tabsplitter = re.compile(br'(\t+|[^\t]+)')
53 wordsplitter = re.compile(br'(\t+| +|[a-zA-Z0-9_\x80-\xff]+|'
53 wordsplitter = re.compile(br'(\t+| +|[a-zA-Z0-9_\x80-\xff]+|'
54 b'[^ \ta-zA-Z0-9_\x80-\xff])')
54 b'[^ \ta-zA-Z0-9_\x80-\xff])')
55
55
56 PatchError = error.PatchError
56 PatchError = error.PatchError
57
57
58 # public functions
58 # public functions
59
59
60 def split(stream):
60 def split(stream):
61 '''return an iterator of individual patches from a stream'''
61 '''return an iterator of individual patches from a stream'''
62 def isheader(line, inheader):
62 def isheader(line, inheader):
63 if inheader and line.startswith((' ', '\t')):
63 if inheader and line.startswith((' ', '\t')):
64 # continuation
64 # continuation
65 return True
65 return True
66 if line.startswith((' ', '-', '+')):
66 if line.startswith((' ', '-', '+')):
67 # diff line - don't check for header pattern in there
67 # diff line - don't check for header pattern in there
68 return False
68 return False
69 l = line.split(': ', 1)
69 l = line.split(': ', 1)
70 return len(l) == 2 and ' ' not in l[0]
70 return len(l) == 2 and ' ' not in l[0]
71
71
72 def chunk(lines):
72 def chunk(lines):
73 return stringio(''.join(lines))
73 return stringio(''.join(lines))
74
74
75 def hgsplit(stream, cur):
75 def hgsplit(stream, cur):
76 inheader = True
76 inheader = True
77
77
78 for line in stream:
78 for line in stream:
79 if not line.strip():
79 if not line.strip():
80 inheader = False
80 inheader = False
81 if not inheader and line.startswith('# HG changeset patch'):
81 if not inheader and line.startswith('# HG changeset patch'):
82 yield chunk(cur)
82 yield chunk(cur)
83 cur = []
83 cur = []
84 inheader = True
84 inheader = True
85
85
86 cur.append(line)
86 cur.append(line)
87
87
88 if cur:
88 if cur:
89 yield chunk(cur)
89 yield chunk(cur)
90
90
91 def mboxsplit(stream, cur):
91 def mboxsplit(stream, cur):
92 for line in stream:
92 for line in stream:
93 if line.startswith('From '):
93 if line.startswith('From '):
94 for c in split(chunk(cur[1:])):
94 for c in split(chunk(cur[1:])):
95 yield c
95 yield c
96 cur = []
96 cur = []
97
97
98 cur.append(line)
98 cur.append(line)
99
99
100 if cur:
100 if cur:
101 for c in split(chunk(cur[1:])):
101 for c in split(chunk(cur[1:])):
102 yield c
102 yield c
103
103
104 def mimesplit(stream, cur):
104 def mimesplit(stream, cur):
105 def msgfp(m):
105 def msgfp(m):
106 fp = stringio()
106 fp = stringio()
107 g = email.Generator.Generator(fp, mangle_from_=False)
107 g = email.Generator.Generator(fp, mangle_from_=False)
108 g.flatten(m)
108 g.flatten(m)
109 fp.seek(0)
109 fp.seek(0)
110 return fp
110 return fp
111
111
112 for line in stream:
112 for line in stream:
113 cur.append(line)
113 cur.append(line)
114 c = chunk(cur)
114 c = chunk(cur)
115
115
116 m = pycompat.emailparser().parse(c)
116 m = pycompat.emailparser().parse(c)
117 if not m.is_multipart():
117 if not m.is_multipart():
118 yield msgfp(m)
118 yield msgfp(m)
119 else:
119 else:
120 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
120 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
121 for part in m.walk():
121 for part in m.walk():
122 ct = part.get_content_type()
122 ct = part.get_content_type()
123 if ct not in ok_types:
123 if ct not in ok_types:
124 continue
124 continue
125 yield msgfp(part)
125 yield msgfp(part)
126
126
127 def headersplit(stream, cur):
127 def headersplit(stream, cur):
128 inheader = False
128 inheader = False
129
129
130 for line in stream:
130 for line in stream:
131 if not inheader and isheader(line, inheader):
131 if not inheader and isheader(line, inheader):
132 yield chunk(cur)
132 yield chunk(cur)
133 cur = []
133 cur = []
134 inheader = True
134 inheader = True
135 if inheader and not isheader(line, inheader):
135 if inheader and not isheader(line, inheader):
136 inheader = False
136 inheader = False
137
137
138 cur.append(line)
138 cur.append(line)
139
139
140 if cur:
140 if cur:
141 yield chunk(cur)
141 yield chunk(cur)
142
142
143 def remainder(cur):
143 def remainder(cur):
144 yield chunk(cur)
144 yield chunk(cur)
145
145
146 class fiter(object):
146 class fiter(object):
147 def __init__(self, fp):
147 def __init__(self, fp):
148 self.fp = fp
148 self.fp = fp
149
149
150 def __iter__(self):
150 def __iter__(self):
151 return self
151 return self
152
152
153 def next(self):
153 def next(self):
154 l = self.fp.readline()
154 l = self.fp.readline()
155 if not l:
155 if not l:
156 raise StopIteration
156 raise StopIteration
157 return l
157 return l
158
158
159 __next__ = next
159 __next__ = next
160
160
161 inheader = False
161 inheader = False
162 cur = []
162 cur = []
163
163
164 mimeheaders = ['content-type']
164 mimeheaders = ['content-type']
165
165
166 if not util.safehasattr(stream, 'next'):
166 if not util.safehasattr(stream, 'next'):
167 # http responses, for example, have readline but not next
167 # http responses, for example, have readline but not next
168 stream = fiter(stream)
168 stream = fiter(stream)
169
169
170 for line in stream:
170 for line in stream:
171 cur.append(line)
171 cur.append(line)
172 if line.startswith('# HG changeset patch'):
172 if line.startswith('# HG changeset patch'):
173 return hgsplit(stream, cur)
173 return hgsplit(stream, cur)
174 elif line.startswith('From '):
174 elif line.startswith('From '):
175 return mboxsplit(stream, cur)
175 return mboxsplit(stream, cur)
176 elif isheader(line, inheader):
176 elif isheader(line, inheader):
177 inheader = True
177 inheader = True
178 if line.split(':', 1)[0].lower() in mimeheaders:
178 if line.split(':', 1)[0].lower() in mimeheaders:
179 # let email parser handle this
179 # let email parser handle this
180 return mimesplit(stream, cur)
180 return mimesplit(stream, cur)
181 elif line.startswith('--- ') and inheader:
181 elif line.startswith('--- ') and inheader:
182 # No evil headers seen by diff start, split by hand
182 # No evil headers seen by diff start, split by hand
183 return headersplit(stream, cur)
183 return headersplit(stream, cur)
184 # Not enough info, keep reading
184 # Not enough info, keep reading
185
185
186 # if we are here, we have a very plain patch
186 # if we are here, we have a very plain patch
187 return remainder(cur)
187 return remainder(cur)
188
188
189 ## Some facility for extensible patch parsing:
189 ## Some facility for extensible patch parsing:
190 # list of pairs ("header to match", "data key")
190 # list of pairs ("header to match", "data key")
191 patchheadermap = [('Date', 'date'),
191 patchheadermap = [('Date', 'date'),
192 ('Branch', 'branch'),
192 ('Branch', 'branch'),
193 ('Node ID', 'nodeid'),
193 ('Node ID', 'nodeid'),
194 ]
194 ]
195
195
196 @contextlib.contextmanager
196 @contextlib.contextmanager
197 def extract(ui, fileobj):
197 def extract(ui, fileobj):
198 '''extract patch from data read from fileobj.
198 '''extract patch from data read from fileobj.
199
199
200 patch can be a normal patch or contained in an email message.
200 patch can be a normal patch or contained in an email message.
201
201
202 return a dictionary. Standard keys are:
202 return a dictionary. Standard keys are:
203 - filename,
203 - filename,
204 - message,
204 - message,
205 - user,
205 - user,
206 - date,
206 - date,
207 - branch,
207 - branch,
208 - node,
208 - node,
209 - p1,
209 - p1,
210 - p2.
210 - p2.
211 Any item can be missing from the dictionary. If filename is missing,
211 Any item can be missing from the dictionary. If filename is missing,
212 fileobj did not contain a patch. Caller must unlink filename when done.'''
212 fileobj did not contain a patch. Caller must unlink filename when done.'''
213
213
214 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
214 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
215 tmpfp = os.fdopen(fd, r'wb')
215 tmpfp = os.fdopen(fd, r'wb')
216 try:
216 try:
217 yield _extract(ui, fileobj, tmpname, tmpfp)
217 yield _extract(ui, fileobj, tmpname, tmpfp)
218 finally:
218 finally:
219 tmpfp.close()
219 tmpfp.close()
220 os.unlink(tmpname)
220 os.unlink(tmpname)
221
221
222 def _extract(ui, fileobj, tmpname, tmpfp):
222 def _extract(ui, fileobj, tmpname, tmpfp):
223
223
224 # attempt to detect the start of a patch
224 # attempt to detect the start of a patch
225 # (this heuristic is borrowed from quilt)
225 # (this heuristic is borrowed from quilt)
226 diffre = re.compile(br'^(?:Index:[ \t]|diff[ \t]-|RCS file: |'
226 diffre = re.compile(br'^(?:Index:[ \t]|diff[ \t]-|RCS file: |'
227 br'retrieving revision [0-9]+(\.[0-9]+)*$|'
227 br'retrieving revision [0-9]+(\.[0-9]+)*$|'
228 br'---[ \t].*?^\+\+\+[ \t]|'
228 br'---[ \t].*?^\+\+\+[ \t]|'
229 br'\*\*\*[ \t].*?^---[ \t])',
229 br'\*\*\*[ \t].*?^---[ \t])',
230 re.MULTILINE | re.DOTALL)
230 re.MULTILINE | re.DOTALL)
231
231
232 data = {}
232 data = {}
233
233
234 msg = pycompat.emailparser().parse(fileobj)
234 msg = pycompat.emailparser().parse(fileobj)
235
235
236 subject = msg[r'Subject'] and mail.headdecode(msg[r'Subject'])
236 subject = msg[r'Subject'] and mail.headdecode(msg[r'Subject'])
237 data['user'] = msg[r'From'] and mail.headdecode(msg[r'From'])
237 data['user'] = msg[r'From'] and mail.headdecode(msg[r'From'])
238 if not subject and not data['user']:
238 if not subject and not data['user']:
239 # Not an email, restore parsed headers if any
239 # Not an email, restore parsed headers if any
240 subject = '\n'.join(': '.join(map(encoding.strtolocal, h))
240 subject = '\n'.join(': '.join(map(encoding.strtolocal, h))
241 for h in msg.items()) + '\n'
241 for h in msg.items()) + '\n'
242
242
243 # should try to parse msg['Date']
243 # should try to parse msg['Date']
244 parents = []
244 parents = []
245
245
246 if subject:
246 if subject:
247 if subject.startswith('[PATCH'):
247 if subject.startswith('[PATCH'):
248 pend = subject.find(']')
248 pend = subject.find(']')
249 if pend >= 0:
249 if pend >= 0:
250 subject = subject[pend + 1:].lstrip()
250 subject = subject[pend + 1:].lstrip()
251 subject = re.sub(br'\n[ \t]+', ' ', subject)
251 subject = re.sub(br'\n[ \t]+', ' ', subject)
252 ui.debug('Subject: %s\n' % subject)
252 ui.debug('Subject: %s\n' % subject)
253 if data['user']:
253 if data['user']:
254 ui.debug('From: %s\n' % data['user'])
254 ui.debug('From: %s\n' % data['user'])
255 diffs_seen = 0
255 diffs_seen = 0
256 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
256 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
257 message = ''
257 message = ''
258 for part in msg.walk():
258 for part in msg.walk():
259 content_type = pycompat.bytestr(part.get_content_type())
259 content_type = pycompat.bytestr(part.get_content_type())
260 ui.debug('Content-Type: %s\n' % content_type)
260 ui.debug('Content-Type: %s\n' % content_type)
261 if content_type not in ok_types:
261 if content_type not in ok_types:
262 continue
262 continue
263 payload = part.get_payload(decode=True)
263 payload = part.get_payload(decode=True)
264 m = diffre.search(payload)
264 m = diffre.search(payload)
265 if m:
265 if m:
266 hgpatch = False
266 hgpatch = False
267 hgpatchheader = False
267 hgpatchheader = False
268 ignoretext = False
268 ignoretext = False
269
269
270 ui.debug('found patch at byte %d\n' % m.start(0))
270 ui.debug('found patch at byte %d\n' % m.start(0))
271 diffs_seen += 1
271 diffs_seen += 1
272 cfp = stringio()
272 cfp = stringio()
273 for line in payload[:m.start(0)].splitlines():
273 for line in payload[:m.start(0)].splitlines():
274 if line.startswith('# HG changeset patch') and not hgpatch:
274 if line.startswith('# HG changeset patch') and not hgpatch:
275 ui.debug('patch generated by hg export\n')
275 ui.debug('patch generated by hg export\n')
276 hgpatch = True
276 hgpatch = True
277 hgpatchheader = True
277 hgpatchheader = True
278 # drop earlier commit message content
278 # drop earlier commit message content
279 cfp.seek(0)
279 cfp.seek(0)
280 cfp.truncate()
280 cfp.truncate()
281 subject = None
281 subject = None
282 elif hgpatchheader:
282 elif hgpatchheader:
283 if line.startswith('# User '):
283 if line.startswith('# User '):
284 data['user'] = line[7:]
284 data['user'] = line[7:]
285 ui.debug('From: %s\n' % data['user'])
285 ui.debug('From: %s\n' % data['user'])
286 elif line.startswith("# Parent "):
286 elif line.startswith("# Parent "):
287 parents.append(line[9:].lstrip())
287 parents.append(line[9:].lstrip())
288 elif line.startswith("# "):
288 elif line.startswith("# "):
289 for header, key in patchheadermap:
289 for header, key in patchheadermap:
290 prefix = '# %s ' % header
290 prefix = '# %s ' % header
291 if line.startswith(prefix):
291 if line.startswith(prefix):
292 data[key] = line[len(prefix):]
292 data[key] = line[len(prefix):]
293 else:
293 else:
294 hgpatchheader = False
294 hgpatchheader = False
295 elif line == '---':
295 elif line == '---':
296 ignoretext = True
296 ignoretext = True
297 if not hgpatchheader and not ignoretext:
297 if not hgpatchheader and not ignoretext:
298 cfp.write(line)
298 cfp.write(line)
299 cfp.write('\n')
299 cfp.write('\n')
300 message = cfp.getvalue()
300 message = cfp.getvalue()
301 if tmpfp:
301 if tmpfp:
302 tmpfp.write(payload)
302 tmpfp.write(payload)
303 if not payload.endswith('\n'):
303 if not payload.endswith('\n'):
304 tmpfp.write('\n')
304 tmpfp.write('\n')
305 elif not diffs_seen and message and content_type == 'text/plain':
305 elif not diffs_seen and message and content_type == 'text/plain':
306 message += '\n' + payload
306 message += '\n' + payload
307
307
308 if subject and not message.startswith(subject):
308 if subject and not message.startswith(subject):
309 message = '%s\n%s' % (subject, message)
309 message = '%s\n%s' % (subject, message)
310 data['message'] = message
310 data['message'] = message
311 tmpfp.close()
311 tmpfp.close()
312 if parents:
312 if parents:
313 data['p1'] = parents.pop(0)
313 data['p1'] = parents.pop(0)
314 if parents:
314 if parents:
315 data['p2'] = parents.pop(0)
315 data['p2'] = parents.pop(0)
316
316
317 if diffs_seen:
317 if diffs_seen:
318 data['filename'] = tmpname
318 data['filename'] = tmpname
319
319
320 return data
320 return data
321
321
322 class patchmeta(object):
322 class patchmeta(object):
323 """Patched file metadata
323 """Patched file metadata
324
324
325 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
325 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
326 or COPY. 'path' is patched file path. 'oldpath' is set to the
326 or COPY. 'path' is patched file path. 'oldpath' is set to the
327 origin file when 'op' is either COPY or RENAME, None otherwise. If
327 origin file when 'op' is either COPY or RENAME, None otherwise. If
328 file mode is changed, 'mode' is a tuple (islink, isexec) where
328 file mode is changed, 'mode' is a tuple (islink, isexec) where
329 'islink' is True if the file is a symlink and 'isexec' is True if
329 'islink' is True if the file is a symlink and 'isexec' is True if
330 the file is executable. Otherwise, 'mode' is None.
330 the file is executable. Otherwise, 'mode' is None.
331 """
331 """
332 def __init__(self, path):
332 def __init__(self, path):
333 self.path = path
333 self.path = path
334 self.oldpath = None
334 self.oldpath = None
335 self.mode = None
335 self.mode = None
336 self.op = 'MODIFY'
336 self.op = 'MODIFY'
337 self.binary = False
337 self.binary = False
338
338
339 def setmode(self, mode):
339 def setmode(self, mode):
340 islink = mode & 0o20000
340 islink = mode & 0o20000
341 isexec = mode & 0o100
341 isexec = mode & 0o100
342 self.mode = (islink, isexec)
342 self.mode = (islink, isexec)
343
343
344 def copy(self):
344 def copy(self):
345 other = patchmeta(self.path)
345 other = patchmeta(self.path)
346 other.oldpath = self.oldpath
346 other.oldpath = self.oldpath
347 other.mode = self.mode
347 other.mode = self.mode
348 other.op = self.op
348 other.op = self.op
349 other.binary = self.binary
349 other.binary = self.binary
350 return other
350 return other
351
351
352 def _ispatchinga(self, afile):
352 def _ispatchinga(self, afile):
353 if afile == '/dev/null':
353 if afile == '/dev/null':
354 return self.op == 'ADD'
354 return self.op == 'ADD'
355 return afile == 'a/' + (self.oldpath or self.path)
355 return afile == 'a/' + (self.oldpath or self.path)
356
356
357 def _ispatchingb(self, bfile):
357 def _ispatchingb(self, bfile):
358 if bfile == '/dev/null':
358 if bfile == '/dev/null':
359 return self.op == 'DELETE'
359 return self.op == 'DELETE'
360 return bfile == 'b/' + self.path
360 return bfile == 'b/' + self.path
361
361
362 def ispatching(self, afile, bfile):
362 def ispatching(self, afile, bfile):
363 return self._ispatchinga(afile) and self._ispatchingb(bfile)
363 return self._ispatchinga(afile) and self._ispatchingb(bfile)
364
364
365 def __repr__(self):
365 def __repr__(self):
366 return "<patchmeta %s %r>" % (self.op, self.path)
366 return "<patchmeta %s %r>" % (self.op, self.path)
367
367
368 def readgitpatch(lr):
368 def readgitpatch(lr):
369 """extract git-style metadata about patches from <patchname>"""
369 """extract git-style metadata about patches from <patchname>"""
370
370
371 # Filter patch for git information
371 # Filter patch for git information
372 gp = None
372 gp = None
373 gitpatches = []
373 gitpatches = []
374 for line in lr:
374 for line in lr:
375 line = line.rstrip(' \r\n')
375 line = line.rstrip(' \r\n')
376 if line.startswith('diff --git a/'):
376 if line.startswith('diff --git a/'):
377 m = gitre.match(line)
377 m = gitre.match(line)
378 if m:
378 if m:
379 if gp:
379 if gp:
380 gitpatches.append(gp)
380 gitpatches.append(gp)
381 dst = m.group(2)
381 dst = m.group(2)
382 gp = patchmeta(dst)
382 gp = patchmeta(dst)
383 elif gp:
383 elif gp:
384 if line.startswith('--- '):
384 if line.startswith('--- '):
385 gitpatches.append(gp)
385 gitpatches.append(gp)
386 gp = None
386 gp = None
387 continue
387 continue
388 if line.startswith('rename from '):
388 if line.startswith('rename from '):
389 gp.op = 'RENAME'
389 gp.op = 'RENAME'
390 gp.oldpath = line[12:]
390 gp.oldpath = line[12:]
391 elif line.startswith('rename to '):
391 elif line.startswith('rename to '):
392 gp.path = line[10:]
392 gp.path = line[10:]
393 elif line.startswith('copy from '):
393 elif line.startswith('copy from '):
394 gp.op = 'COPY'
394 gp.op = 'COPY'
395 gp.oldpath = line[10:]
395 gp.oldpath = line[10:]
396 elif line.startswith('copy to '):
396 elif line.startswith('copy to '):
397 gp.path = line[8:]
397 gp.path = line[8:]
398 elif line.startswith('deleted file'):
398 elif line.startswith('deleted file'):
399 gp.op = 'DELETE'
399 gp.op = 'DELETE'
400 elif line.startswith('new file mode '):
400 elif line.startswith('new file mode '):
401 gp.op = 'ADD'
401 gp.op = 'ADD'
402 gp.setmode(int(line[-6:], 8))
402 gp.setmode(int(line[-6:], 8))
403 elif line.startswith('new mode '):
403 elif line.startswith('new mode '):
404 gp.setmode(int(line[-6:], 8))
404 gp.setmode(int(line[-6:], 8))
405 elif line.startswith('GIT binary patch'):
405 elif line.startswith('GIT binary patch'):
406 gp.binary = True
406 gp.binary = True
407 if gp:
407 if gp:
408 gitpatches.append(gp)
408 gitpatches.append(gp)
409
409
410 return gitpatches
410 return gitpatches
411
411
412 class linereader(object):
412 class linereader(object):
413 # simple class to allow pushing lines back into the input stream
413 # simple class to allow pushing lines back into the input stream
414 def __init__(self, fp):
414 def __init__(self, fp):
415 self.fp = fp
415 self.fp = fp
416 self.buf = []
416 self.buf = []
417
417
418 def push(self, line):
418 def push(self, line):
419 if line is not None:
419 if line is not None:
420 self.buf.append(line)
420 self.buf.append(line)
421
421
422 def readline(self):
422 def readline(self):
423 if self.buf:
423 if self.buf:
424 l = self.buf[0]
424 l = self.buf[0]
425 del self.buf[0]
425 del self.buf[0]
426 return l
426 return l
427 return self.fp.readline()
427 return self.fp.readline()
428
428
429 def __iter__(self):
429 def __iter__(self):
430 return iter(self.readline, '')
430 return iter(self.readline, '')
431
431
432 class abstractbackend(object):
432 class abstractbackend(object):
433 def __init__(self, ui):
433 def __init__(self, ui):
434 self.ui = ui
434 self.ui = ui
435
435
436 def getfile(self, fname):
436 def getfile(self, fname):
437 """Return target file data and flags as a (data, (islink,
437 """Return target file data and flags as a (data, (islink,
438 isexec)) tuple. Data is None if file is missing/deleted.
438 isexec)) tuple. Data is None if file is missing/deleted.
439 """
439 """
440 raise NotImplementedError
440 raise NotImplementedError
441
441
442 def setfile(self, fname, data, mode, copysource):
442 def setfile(self, fname, data, mode, copysource):
443 """Write data to target file fname and set its mode. mode is a
443 """Write data to target file fname and set its mode. mode is a
444 (islink, isexec) tuple. If data is None, the file content should
444 (islink, isexec) tuple. If data is None, the file content should
445 be left unchanged. If the file is modified after being copied,
445 be left unchanged. If the file is modified after being copied,
446 copysource is set to the original file name.
446 copysource is set to the original file name.
447 """
447 """
448 raise NotImplementedError
448 raise NotImplementedError
449
449
450 def unlink(self, fname):
450 def unlink(self, fname):
451 """Unlink target file."""
451 """Unlink target file."""
452 raise NotImplementedError
452 raise NotImplementedError
453
453
454 def writerej(self, fname, failed, total, lines):
454 def writerej(self, fname, failed, total, lines):
455 """Write rejected lines for fname. total is the number of hunks
455 """Write rejected lines for fname. total is the number of hunks
456 which failed to apply and total the total number of hunks for this
456 which failed to apply and total the total number of hunks for this
457 files.
457 files.
458 """
458 """
459
459
460 def exists(self, fname):
460 def exists(self, fname):
461 raise NotImplementedError
461 raise NotImplementedError
462
462
463 def close(self):
463 def close(self):
464 raise NotImplementedError
464 raise NotImplementedError
465
465
466 class fsbackend(abstractbackend):
466 class fsbackend(abstractbackend):
467 def __init__(self, ui, basedir):
467 def __init__(self, ui, basedir):
468 super(fsbackend, self).__init__(ui)
468 super(fsbackend, self).__init__(ui)
469 self.opener = vfsmod.vfs(basedir)
469 self.opener = vfsmod.vfs(basedir)
470
470
471 def getfile(self, fname):
471 def getfile(self, fname):
472 if self.opener.islink(fname):
472 if self.opener.islink(fname):
473 return (self.opener.readlink(fname), (True, False))
473 return (self.opener.readlink(fname), (True, False))
474
474
475 isexec = False
475 isexec = False
476 try:
476 try:
477 isexec = self.opener.lstat(fname).st_mode & 0o100 != 0
477 isexec = self.opener.lstat(fname).st_mode & 0o100 != 0
478 except OSError as e:
478 except OSError as e:
479 if e.errno != errno.ENOENT:
479 if e.errno != errno.ENOENT:
480 raise
480 raise
481 try:
481 try:
482 return (self.opener.read(fname), (False, isexec))
482 return (self.opener.read(fname), (False, isexec))
483 except IOError as e:
483 except IOError as e:
484 if e.errno != errno.ENOENT:
484 if e.errno != errno.ENOENT:
485 raise
485 raise
486 return None, None
486 return None, None
487
487
488 def setfile(self, fname, data, mode, copysource):
488 def setfile(self, fname, data, mode, copysource):
489 islink, isexec = mode
489 islink, isexec = mode
490 if data is None:
490 if data is None:
491 self.opener.setflags(fname, islink, isexec)
491 self.opener.setflags(fname, islink, isexec)
492 return
492 return
493 if islink:
493 if islink:
494 self.opener.symlink(data, fname)
494 self.opener.symlink(data, fname)
495 else:
495 else:
496 self.opener.write(fname, data)
496 self.opener.write(fname, data)
497 if isexec:
497 if isexec:
498 self.opener.setflags(fname, False, True)
498 self.opener.setflags(fname, False, True)
499
499
500 def unlink(self, fname):
500 def unlink(self, fname):
501 self.opener.unlinkpath(fname, ignoremissing=True)
501 self.opener.unlinkpath(fname, ignoremissing=True)
502
502
503 def writerej(self, fname, failed, total, lines):
503 def writerej(self, fname, failed, total, lines):
504 fname = fname + ".rej"
504 fname = fname + ".rej"
505 self.ui.warn(
505 self.ui.warn(
506 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
506 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
507 (failed, total, fname))
507 (failed, total, fname))
508 fp = self.opener(fname, 'w')
508 fp = self.opener(fname, 'w')
509 fp.writelines(lines)
509 fp.writelines(lines)
510 fp.close()
510 fp.close()
511
511
512 def exists(self, fname):
512 def exists(self, fname):
513 return self.opener.lexists(fname)
513 return self.opener.lexists(fname)
514
514
515 class workingbackend(fsbackend):
515 class workingbackend(fsbackend):
516 def __init__(self, ui, repo, similarity):
516 def __init__(self, ui, repo, similarity):
517 super(workingbackend, self).__init__(ui, repo.root)
517 super(workingbackend, self).__init__(ui, repo.root)
518 self.repo = repo
518 self.repo = repo
519 self.similarity = similarity
519 self.similarity = similarity
520 self.removed = set()
520 self.removed = set()
521 self.changed = set()
521 self.changed = set()
522 self.copied = []
522 self.copied = []
523
523
524 def _checkknown(self, fname):
524 def _checkknown(self, fname):
525 if self.repo.dirstate[fname] == '?' and self.exists(fname):
525 if self.repo.dirstate[fname] == '?' and self.exists(fname):
526 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
526 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
527
527
528 def setfile(self, fname, data, mode, copysource):
528 def setfile(self, fname, data, mode, copysource):
529 self._checkknown(fname)
529 self._checkknown(fname)
530 super(workingbackend, self).setfile(fname, data, mode, copysource)
530 super(workingbackend, self).setfile(fname, data, mode, copysource)
531 if copysource is not None:
531 if copysource is not None:
532 self.copied.append((copysource, fname))
532 self.copied.append((copysource, fname))
533 self.changed.add(fname)
533 self.changed.add(fname)
534
534
535 def unlink(self, fname):
535 def unlink(self, fname):
536 self._checkknown(fname)
536 self._checkknown(fname)
537 super(workingbackend, self).unlink(fname)
537 super(workingbackend, self).unlink(fname)
538 self.removed.add(fname)
538 self.removed.add(fname)
539 self.changed.add(fname)
539 self.changed.add(fname)
540
540
541 def close(self):
541 def close(self):
542 wctx = self.repo[None]
542 wctx = self.repo[None]
543 changed = set(self.changed)
543 changed = set(self.changed)
544 for src, dst in self.copied:
544 for src, dst in self.copied:
545 scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst)
545 scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst)
546 if self.removed:
546 if self.removed:
547 wctx.forget(sorted(self.removed))
547 wctx.forget(sorted(self.removed))
548 for f in self.removed:
548 for f in self.removed:
549 if f not in self.repo.dirstate:
549 if f not in self.repo.dirstate:
550 # File was deleted and no longer belongs to the
550 # File was deleted and no longer belongs to the
551 # dirstate, it was probably marked added then
551 # dirstate, it was probably marked added then
552 # deleted, and should not be considered by
552 # deleted, and should not be considered by
553 # marktouched().
553 # marktouched().
554 changed.discard(f)
554 changed.discard(f)
555 if changed:
555 if changed:
556 scmutil.marktouched(self.repo, changed, self.similarity)
556 scmutil.marktouched(self.repo, changed, self.similarity)
557 return sorted(self.changed)
557 return sorted(self.changed)
558
558
559 class filestore(object):
559 class filestore(object):
560 def __init__(self, maxsize=None):
560 def __init__(self, maxsize=None):
561 self.opener = None
561 self.opener = None
562 self.files = {}
562 self.files = {}
563 self.created = 0
563 self.created = 0
564 self.maxsize = maxsize
564 self.maxsize = maxsize
565 if self.maxsize is None:
565 if self.maxsize is None:
566 self.maxsize = 4*(2**20)
566 self.maxsize = 4*(2**20)
567 self.size = 0
567 self.size = 0
568 self.data = {}
568 self.data = {}
569
569
570 def setfile(self, fname, data, mode, copied=None):
570 def setfile(self, fname, data, mode, copied=None):
571 if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize:
571 if self.maxsize < 0 or (len(data) + self.size) <= self.maxsize:
572 self.data[fname] = (data, mode, copied)
572 self.data[fname] = (data, mode, copied)
573 self.size += len(data)
573 self.size += len(data)
574 else:
574 else:
575 if self.opener is None:
575 if self.opener is None:
576 root = tempfile.mkdtemp(prefix='hg-patch-')
576 root = tempfile.mkdtemp(prefix='hg-patch-')
577 self.opener = vfsmod.vfs(root)
577 self.opener = vfsmod.vfs(root)
578 # Avoid filename issues with these simple names
578 # Avoid filename issues with these simple names
579 fn = '%d' % self.created
579 fn = '%d' % self.created
580 self.opener.write(fn, data)
580 self.opener.write(fn, data)
581 self.created += 1
581 self.created += 1
582 self.files[fname] = (fn, mode, copied)
582 self.files[fname] = (fn, mode, copied)
583
583
584 def getfile(self, fname):
584 def getfile(self, fname):
585 if fname in self.data:
585 if fname in self.data:
586 return self.data[fname]
586 return self.data[fname]
587 if not self.opener or fname not in self.files:
587 if not self.opener or fname not in self.files:
588 return None, None, None
588 return None, None, None
589 fn, mode, copied = self.files[fname]
589 fn, mode, copied = self.files[fname]
590 return self.opener.read(fn), mode, copied
590 return self.opener.read(fn), mode, copied
591
591
592 def close(self):
592 def close(self):
593 if self.opener:
593 if self.opener:
594 shutil.rmtree(self.opener.base)
594 shutil.rmtree(self.opener.base)
595
595
596 class repobackend(abstractbackend):
596 class repobackend(abstractbackend):
597 def __init__(self, ui, repo, ctx, store):
597 def __init__(self, ui, repo, ctx, store):
598 super(repobackend, self).__init__(ui)
598 super(repobackend, self).__init__(ui)
599 self.repo = repo
599 self.repo = repo
600 self.ctx = ctx
600 self.ctx = ctx
601 self.store = store
601 self.store = store
602 self.changed = set()
602 self.changed = set()
603 self.removed = set()
603 self.removed = set()
604 self.copied = {}
604 self.copied = {}
605
605
606 def _checkknown(self, fname):
606 def _checkknown(self, fname):
607 if fname not in self.ctx:
607 if fname not in self.ctx:
608 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
608 raise PatchError(_('cannot patch %s: file is not tracked') % fname)
609
609
610 def getfile(self, fname):
610 def getfile(self, fname):
611 try:
611 try:
612 fctx = self.ctx[fname]
612 fctx = self.ctx[fname]
613 except error.LookupError:
613 except error.LookupError:
614 return None, None
614 return None, None
615 flags = fctx.flags()
615 flags = fctx.flags()
616 return fctx.data(), ('l' in flags, 'x' in flags)
616 return fctx.data(), ('l' in flags, 'x' in flags)
617
617
618 def setfile(self, fname, data, mode, copysource):
618 def setfile(self, fname, data, mode, copysource):
619 if copysource:
619 if copysource:
620 self._checkknown(copysource)
620 self._checkknown(copysource)
621 if data is None:
621 if data is None:
622 data = self.ctx[fname].data()
622 data = self.ctx[fname].data()
623 self.store.setfile(fname, data, mode, copysource)
623 self.store.setfile(fname, data, mode, copysource)
624 self.changed.add(fname)
624 self.changed.add(fname)
625 if copysource:
625 if copysource:
626 self.copied[fname] = copysource
626 self.copied[fname] = copysource
627
627
628 def unlink(self, fname):
628 def unlink(self, fname):
629 self._checkknown(fname)
629 self._checkknown(fname)
630 self.removed.add(fname)
630 self.removed.add(fname)
631
631
632 def exists(self, fname):
632 def exists(self, fname):
633 return fname in self.ctx
633 return fname in self.ctx
634
634
635 def close(self):
635 def close(self):
636 return self.changed | self.removed
636 return self.changed | self.removed
637
637
638 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
638 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
639 unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
639 unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
640 contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
640 contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
641 eolmodes = ['strict', 'crlf', 'lf', 'auto']
641 eolmodes = ['strict', 'crlf', 'lf', 'auto']
642
642
643 class patchfile(object):
643 class patchfile(object):
644 def __init__(self, ui, gp, backend, store, eolmode='strict'):
644 def __init__(self, ui, gp, backend, store, eolmode='strict'):
645 self.fname = gp.path
645 self.fname = gp.path
646 self.eolmode = eolmode
646 self.eolmode = eolmode
647 self.eol = None
647 self.eol = None
648 self.backend = backend
648 self.backend = backend
649 self.ui = ui
649 self.ui = ui
650 self.lines = []
650 self.lines = []
651 self.exists = False
651 self.exists = False
652 self.missing = True
652 self.missing = True
653 self.mode = gp.mode
653 self.mode = gp.mode
654 self.copysource = gp.oldpath
654 self.copysource = gp.oldpath
655 self.create = gp.op in ('ADD', 'COPY', 'RENAME')
655 self.create = gp.op in ('ADD', 'COPY', 'RENAME')
656 self.remove = gp.op == 'DELETE'
656 self.remove = gp.op == 'DELETE'
657 if self.copysource is None:
657 if self.copysource is None:
658 data, mode = backend.getfile(self.fname)
658 data, mode = backend.getfile(self.fname)
659 else:
659 else:
660 data, mode = store.getfile(self.copysource)[:2]
660 data, mode = store.getfile(self.copysource)[:2]
661 if data is not None:
661 if data is not None:
662 self.exists = self.copysource is None or backend.exists(self.fname)
662 self.exists = self.copysource is None or backend.exists(self.fname)
663 self.missing = False
663 self.missing = False
664 if data:
664 if data:
665 self.lines = mdiff.splitnewlines(data)
665 self.lines = mdiff.splitnewlines(data)
666 if self.mode is None:
666 if self.mode is None:
667 self.mode = mode
667 self.mode = mode
668 if self.lines:
668 if self.lines:
669 # Normalize line endings
669 # Normalize line endings
670 if self.lines[0].endswith('\r\n'):
670 if self.lines[0].endswith('\r\n'):
671 self.eol = '\r\n'
671 self.eol = '\r\n'
672 elif self.lines[0].endswith('\n'):
672 elif self.lines[0].endswith('\n'):
673 self.eol = '\n'
673 self.eol = '\n'
674 if eolmode != 'strict':
674 if eolmode != 'strict':
675 nlines = []
675 nlines = []
676 for l in self.lines:
676 for l in self.lines:
677 if l.endswith('\r\n'):
677 if l.endswith('\r\n'):
678 l = l[:-2] + '\n'
678 l = l[:-2] + '\n'
679 nlines.append(l)
679 nlines.append(l)
680 self.lines = nlines
680 self.lines = nlines
681 else:
681 else:
682 if self.create:
682 if self.create:
683 self.missing = False
683 self.missing = False
684 if self.mode is None:
684 if self.mode is None:
685 self.mode = (False, False)
685 self.mode = (False, False)
686 if self.missing:
686 if self.missing:
687 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
687 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
688 self.ui.warn(_("(use '--prefix' to apply patch relative to the "
688 self.ui.warn(_("(use '--prefix' to apply patch relative to the "
689 "current directory)\n"))
689 "current directory)\n"))
690
690
691 self.hash = {}
691 self.hash = {}
692 self.dirty = 0
692 self.dirty = 0
693 self.offset = 0
693 self.offset = 0
694 self.skew = 0
694 self.skew = 0
695 self.rej = []
695 self.rej = []
696 self.fileprinted = False
696 self.fileprinted = False
697 self.printfile(False)
697 self.printfile(False)
698 self.hunks = 0
698 self.hunks = 0
699
699
700 def writelines(self, fname, lines, mode):
700 def writelines(self, fname, lines, mode):
701 if self.eolmode == 'auto':
701 if self.eolmode == 'auto':
702 eol = self.eol
702 eol = self.eol
703 elif self.eolmode == 'crlf':
703 elif self.eolmode == 'crlf':
704 eol = '\r\n'
704 eol = '\r\n'
705 else:
705 else:
706 eol = '\n'
706 eol = '\n'
707
707
708 if self.eolmode != 'strict' and eol and eol != '\n':
708 if self.eolmode != 'strict' and eol and eol != '\n':
709 rawlines = []
709 rawlines = []
710 for l in lines:
710 for l in lines:
711 if l and l[-1] == '\n':
711 if l and l[-1] == '\n':
712 l = l[:-1] + eol
712 l = l[:-1] + eol
713 rawlines.append(l)
713 rawlines.append(l)
714 lines = rawlines
714 lines = rawlines
715
715
716 self.backend.setfile(fname, ''.join(lines), mode, self.copysource)
716 self.backend.setfile(fname, ''.join(lines), mode, self.copysource)
717
717
718 def printfile(self, warn):
718 def printfile(self, warn):
719 if self.fileprinted:
719 if self.fileprinted:
720 return
720 return
721 if warn or self.ui.verbose:
721 if warn or self.ui.verbose:
722 self.fileprinted = True
722 self.fileprinted = True
723 s = _("patching file %s\n") % self.fname
723 s = _("patching file %s\n") % self.fname
724 if warn:
724 if warn:
725 self.ui.warn(s)
725 self.ui.warn(s)
726 else:
726 else:
727 self.ui.note(s)
727 self.ui.note(s)
728
728
729
729
730 def findlines(self, l, linenum):
730 def findlines(self, l, linenum):
731 # looks through the hash and finds candidate lines. The
731 # looks through the hash and finds candidate lines. The
732 # result is a list of line numbers sorted based on distance
732 # result is a list of line numbers sorted based on distance
733 # from linenum
733 # from linenum
734
734
735 cand = self.hash.get(l, [])
735 cand = self.hash.get(l, [])
736 if len(cand) > 1:
736 if len(cand) > 1:
737 # resort our list of potentials forward then back.
737 # resort our list of potentials forward then back.
738 cand.sort(key=lambda x: abs(x - linenum))
738 cand.sort(key=lambda x: abs(x - linenum))
739 return cand
739 return cand
740
740
741 def write_rej(self):
741 def write_rej(self):
742 # our rejects are a little different from patch(1). This always
742 # our rejects are a little different from patch(1). This always
743 # creates rejects in the same form as the original patch. A file
743 # creates rejects in the same form as the original patch. A file
744 # header is inserted so that you can run the reject through patch again
744 # header is inserted so that you can run the reject through patch again
745 # without having to type the filename.
745 # without having to type the filename.
746 if not self.rej:
746 if not self.rej:
747 return
747 return
748 base = os.path.basename(self.fname)
748 base = os.path.basename(self.fname)
749 lines = ["--- %s\n+++ %s\n" % (base, base)]
749 lines = ["--- %s\n+++ %s\n" % (base, base)]
750 for x in self.rej:
750 for x in self.rej:
751 for l in x.hunk:
751 for l in x.hunk:
752 lines.append(l)
752 lines.append(l)
753 if l[-1:] != '\n':
753 if l[-1:] != '\n':
754 lines.append("\n\ No newline at end of file\n")
754 lines.append("\n\ No newline at end of file\n")
755 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines)
755 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines)
756
756
757 def apply(self, h):
757 def apply(self, h):
758 if not h.complete():
758 if not h.complete():
759 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") %
759 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") %
760 (h.number, h.desc, len(h.a), h.lena, len(h.b),
760 (h.number, h.desc, len(h.a), h.lena, len(h.b),
761 h.lenb))
761 h.lenb))
762
762
763 self.hunks += 1
763 self.hunks += 1
764
764
765 if self.missing:
765 if self.missing:
766 self.rej.append(h)
766 self.rej.append(h)
767 return -1
767 return -1
768
768
769 if self.exists and self.create:
769 if self.exists and self.create:
770 if self.copysource:
770 if self.copysource:
771 self.ui.warn(_("cannot create %s: destination already "
771 self.ui.warn(_("cannot create %s: destination already "
772 "exists\n") % self.fname)
772 "exists\n") % self.fname)
773 else:
773 else:
774 self.ui.warn(_("file %s already exists\n") % self.fname)
774 self.ui.warn(_("file %s already exists\n") % self.fname)
775 self.rej.append(h)
775 self.rej.append(h)
776 return -1
776 return -1
777
777
778 if isinstance(h, binhunk):
778 if isinstance(h, binhunk):
779 if self.remove:
779 if self.remove:
780 self.backend.unlink(self.fname)
780 self.backend.unlink(self.fname)
781 else:
781 else:
782 l = h.new(self.lines)
782 l = h.new(self.lines)
783 self.lines[:] = l
783 self.lines[:] = l
784 self.offset += len(l)
784 self.offset += len(l)
785 self.dirty = True
785 self.dirty = True
786 return 0
786 return 0
787
787
788 horig = h
788 horig = h
789 if (self.eolmode in ('crlf', 'lf')
789 if (self.eolmode in ('crlf', 'lf')
790 or self.eolmode == 'auto' and self.eol):
790 or self.eolmode == 'auto' and self.eol):
791 # If new eols are going to be normalized, then normalize
791 # If new eols are going to be normalized, then normalize
792 # hunk data before patching. Otherwise, preserve input
792 # hunk data before patching. Otherwise, preserve input
793 # line-endings.
793 # line-endings.
794 h = h.getnormalized()
794 h = h.getnormalized()
795
795
796 # fast case first, no offsets, no fuzz
796 # fast case first, no offsets, no fuzz
797 old, oldstart, new, newstart = h.fuzzit(0, False)
797 old, oldstart, new, newstart = h.fuzzit(0, False)
798 oldstart += self.offset
798 oldstart += self.offset
799 orig_start = oldstart
799 orig_start = oldstart
800 # if there's skew we want to emit the "(offset %d lines)" even
800 # if there's skew we want to emit the "(offset %d lines)" even
801 # when the hunk cleanly applies at start + skew, so skip the
801 # when the hunk cleanly applies at start + skew, so skip the
802 # fast case code
802 # fast case code
803 if self.skew == 0 and diffhelper.testhunk(old, self.lines, oldstart):
803 if self.skew == 0 and diffhelper.testhunk(old, self.lines, oldstart):
804 if self.remove:
804 if self.remove:
805 self.backend.unlink(self.fname)
805 self.backend.unlink(self.fname)
806 else:
806 else:
807 self.lines[oldstart:oldstart + len(old)] = new
807 self.lines[oldstart:oldstart + len(old)] = new
808 self.offset += len(new) - len(old)
808 self.offset += len(new) - len(old)
809 self.dirty = True
809 self.dirty = True
810 return 0
810 return 0
811
811
812 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it
812 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it
813 self.hash = {}
813 self.hash = {}
814 for x, s in enumerate(self.lines):
814 for x, s in enumerate(self.lines):
815 self.hash.setdefault(s, []).append(x)
815 self.hash.setdefault(s, []).append(x)
816
816
817 for fuzzlen in xrange(self.ui.configint("patch", "fuzz") + 1):
817 for fuzzlen in xrange(self.ui.configint("patch", "fuzz") + 1):
818 for toponly in [True, False]:
818 for toponly in [True, False]:
819 old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly)
819 old, oldstart, new, newstart = h.fuzzit(fuzzlen, toponly)
820 oldstart = oldstart + self.offset + self.skew
820 oldstart = oldstart + self.offset + self.skew
821 oldstart = min(oldstart, len(self.lines))
821 oldstart = min(oldstart, len(self.lines))
822 if old:
822 if old:
823 cand = self.findlines(old[0][1:], oldstart)
823 cand = self.findlines(old[0][1:], oldstart)
824 else:
824 else:
825 # Only adding lines with no or fuzzed context, just
825 # Only adding lines with no or fuzzed context, just
826 # take the skew in account
826 # take the skew in account
827 cand = [oldstart]
827 cand = [oldstart]
828
828
829 for l in cand:
829 for l in cand:
830 if not old or diffhelper.testhunk(old, self.lines, l):
830 if not old or diffhelper.testhunk(old, self.lines, l):
831 self.lines[l : l + len(old)] = new
831 self.lines[l : l + len(old)] = new
832 self.offset += len(new) - len(old)
832 self.offset += len(new) - len(old)
833 self.skew = l - orig_start
833 self.skew = l - orig_start
834 self.dirty = True
834 self.dirty = True
835 offset = l - orig_start - fuzzlen
835 offset = l - orig_start - fuzzlen
836 if fuzzlen:
836 if fuzzlen:
837 msg = _("Hunk #%d succeeded at %d "
837 msg = _("Hunk #%d succeeded at %d "
838 "with fuzz %d "
838 "with fuzz %d "
839 "(offset %d lines).\n")
839 "(offset %d lines).\n")
840 self.printfile(True)
840 self.printfile(True)
841 self.ui.warn(msg %
841 self.ui.warn(msg %
842 (h.number, l + 1, fuzzlen, offset))
842 (h.number, l + 1, fuzzlen, offset))
843 else:
843 else:
844 msg = _("Hunk #%d succeeded at %d "
844 msg = _("Hunk #%d succeeded at %d "
845 "(offset %d lines).\n")
845 "(offset %d lines).\n")
846 self.ui.note(msg % (h.number, l + 1, offset))
846 self.ui.note(msg % (h.number, l + 1, offset))
847 return fuzzlen
847 return fuzzlen
848 self.printfile(True)
848 self.printfile(True)
849 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
849 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
850 self.rej.append(horig)
850 self.rej.append(horig)
851 return -1
851 return -1
852
852
853 def close(self):
853 def close(self):
854 if self.dirty:
854 if self.dirty:
855 self.writelines(self.fname, self.lines, self.mode)
855 self.writelines(self.fname, self.lines, self.mode)
856 self.write_rej()
856 self.write_rej()
857 return len(self.rej)
857 return len(self.rej)
858
858
859 class header(object):
859 class header(object):
860 """patch header
860 """patch header
861 """
861 """
862 diffgit_re = re.compile('diff --git a/(.*) b/(.*)$')
862 diffgit_re = re.compile('diff --git a/(.*) b/(.*)$')
863 diff_re = re.compile('diff -r .* (.*)$')
863 diff_re = re.compile('diff -r .* (.*)$')
864 allhunks_re = re.compile('(?:index|deleted file) ')
864 allhunks_re = re.compile('(?:index|deleted file) ')
865 pretty_re = re.compile('(?:new file|deleted file) ')
865 pretty_re = re.compile('(?:new file|deleted file) ')
866 special_re = re.compile('(?:index|deleted|copy|rename) ')
866 special_re = re.compile('(?:index|deleted|copy|rename) ')
867 newfile_re = re.compile('(?:new file)')
867 newfile_re = re.compile('(?:new file)')
868
868
869 def __init__(self, header):
869 def __init__(self, header):
870 self.header = header
870 self.header = header
871 self.hunks = []
871 self.hunks = []
872
872
873 def binary(self):
873 def binary(self):
874 return any(h.startswith('index ') for h in self.header)
874 return any(h.startswith('index ') for h in self.header)
875
875
876 def pretty(self, fp):
876 def pretty(self, fp):
877 for h in self.header:
877 for h in self.header:
878 if h.startswith('index '):
878 if h.startswith('index '):
879 fp.write(_('this modifies a binary file (all or nothing)\n'))
879 fp.write(_('this modifies a binary file (all or nothing)\n'))
880 break
880 break
881 if self.pretty_re.match(h):
881 if self.pretty_re.match(h):
882 fp.write(h)
882 fp.write(h)
883 if self.binary():
883 if self.binary():
884 fp.write(_('this is a binary file\n'))
884 fp.write(_('this is a binary file\n'))
885 break
885 break
886 if h.startswith('---'):
886 if h.startswith('---'):
887 fp.write(_('%d hunks, %d lines changed\n') %
887 fp.write(_('%d hunks, %d lines changed\n') %
888 (len(self.hunks),
888 (len(self.hunks),
889 sum([max(h.added, h.removed) for h in self.hunks])))
889 sum([max(h.added, h.removed) for h in self.hunks])))
890 break
890 break
891 fp.write(h)
891 fp.write(h)
892
892
893 def write(self, fp):
893 def write(self, fp):
894 fp.write(''.join(self.header))
894 fp.write(''.join(self.header))
895
895
896 def allhunks(self):
896 def allhunks(self):
897 return any(self.allhunks_re.match(h) for h in self.header)
897 return any(self.allhunks_re.match(h) for h in self.header)
898
898
899 def files(self):
899 def files(self):
900 match = self.diffgit_re.match(self.header[0])
900 match = self.diffgit_re.match(self.header[0])
901 if match:
901 if match:
902 fromfile, tofile = match.groups()
902 fromfile, tofile = match.groups()
903 if fromfile == tofile:
903 if fromfile == tofile:
904 return [fromfile]
904 return [fromfile]
905 return [fromfile, tofile]
905 return [fromfile, tofile]
906 else:
906 else:
907 return self.diff_re.match(self.header[0]).groups()
907 return self.diff_re.match(self.header[0]).groups()
908
908
909 def filename(self):
909 def filename(self):
910 return self.files()[-1]
910 return self.files()[-1]
911
911
912 def __repr__(self):
912 def __repr__(self):
913 return '<header %s>' % (' '.join(map(repr, self.files())))
913 return '<header %s>' % (' '.join(map(repr, self.files())))
914
914
915 def isnewfile(self):
915 def isnewfile(self):
916 return any(self.newfile_re.match(h) for h in self.header)
916 return any(self.newfile_re.match(h) for h in self.header)
917
917
918 def special(self):
918 def special(self):
919 # Special files are shown only at the header level and not at the hunk
919 # Special files are shown only at the header level and not at the hunk
920 # level for example a file that has been deleted is a special file.
920 # level for example a file that has been deleted is a special file.
921 # The user cannot change the content of the operation, in the case of
921 # The user cannot change the content of the operation, in the case of
922 # the deleted file he has to take the deletion or not take it, he
922 # the deleted file he has to take the deletion or not take it, he
923 # cannot take some of it.
923 # cannot take some of it.
924 # Newly added files are special if they are empty, they are not special
924 # Newly added files are special if they are empty, they are not special
925 # if they have some content as we want to be able to change it
925 # if they have some content as we want to be able to change it
926 nocontent = len(self.header) == 2
926 nocontent = len(self.header) == 2
927 emptynewfile = self.isnewfile() and nocontent
927 emptynewfile = self.isnewfile() and nocontent
928 return emptynewfile or \
928 return emptynewfile or \
929 any(self.special_re.match(h) for h in self.header)
929 any(self.special_re.match(h) for h in self.header)
930
930
931 class recordhunk(object):
931 class recordhunk(object):
932 """patch hunk
932 """patch hunk
933
933
934 XXX shouldn't we merge this with the other hunk class?
934 XXX shouldn't we merge this with the other hunk class?
935 """
935 """
936
936
937 def __init__(self, header, fromline, toline, proc, before, hunk, after,
937 def __init__(self, header, fromline, toline, proc, before, hunk, after,
938 maxcontext=None):
938 maxcontext=None):
939 def trimcontext(lines, reverse=False):
939 def trimcontext(lines, reverse=False):
940 if maxcontext is not None:
940 if maxcontext is not None:
941 delta = len(lines) - maxcontext
941 delta = len(lines) - maxcontext
942 if delta > 0:
942 if delta > 0:
943 if reverse:
943 if reverse:
944 return delta, lines[delta:]
944 return delta, lines[delta:]
945 else:
945 else:
946 return delta, lines[:maxcontext]
946 return delta, lines[:maxcontext]
947 return 0, lines
947 return 0, lines
948
948
949 self.header = header
949 self.header = header
950 trimedbefore, self.before = trimcontext(before, True)
950 trimedbefore, self.before = trimcontext(before, True)
951 self.fromline = fromline + trimedbefore
951 self.fromline = fromline + trimedbefore
952 self.toline = toline + trimedbefore
952 self.toline = toline + trimedbefore
953 _trimedafter, self.after = trimcontext(after, False)
953 _trimedafter, self.after = trimcontext(after, False)
954 self.proc = proc
954 self.proc = proc
955 self.hunk = hunk
955 self.hunk = hunk
956 self.added, self.removed = self.countchanges(self.hunk)
956 self.added, self.removed = self.countchanges(self.hunk)
957
957
958 def __eq__(self, v):
958 def __eq__(self, v):
959 if not isinstance(v, recordhunk):
959 if not isinstance(v, recordhunk):
960 return False
960 return False
961
961
962 return ((v.hunk == self.hunk) and
962 return ((v.hunk == self.hunk) and
963 (v.proc == self.proc) and
963 (v.proc == self.proc) and
964 (self.fromline == v.fromline) and
964 (self.fromline == v.fromline) and
965 (self.header.files() == v.header.files()))
965 (self.header.files() == v.header.files()))
966
966
967 def __hash__(self):
967 def __hash__(self):
968 return hash((tuple(self.hunk),
968 return hash((tuple(self.hunk),
969 tuple(self.header.files()),
969 tuple(self.header.files()),
970 self.fromline,
970 self.fromline,
971 self.proc))
971 self.proc))
972
972
973 def countchanges(self, hunk):
973 def countchanges(self, hunk):
974 """hunk -> (n+,n-)"""
974 """hunk -> (n+,n-)"""
975 add = len([h for h in hunk if h.startswith('+')])
975 add = len([h for h in hunk if h.startswith('+')])
976 rem = len([h for h in hunk if h.startswith('-')])
976 rem = len([h for h in hunk if h.startswith('-')])
977 return add, rem
977 return add, rem
978
978
979 def reversehunk(self):
979 def reversehunk(self):
980 """return another recordhunk which is the reverse of the hunk
980 """return another recordhunk which is the reverse of the hunk
981
981
982 If this hunk is diff(A, B), the returned hunk is diff(B, A). To do
982 If this hunk is diff(A, B), the returned hunk is diff(B, A). To do
983 that, swap fromline/toline and +/- signs while keep other things
983 that, swap fromline/toline and +/- signs while keep other things
984 unchanged.
984 unchanged.
985 """
985 """
986 m = {'+': '-', '-': '+', '\\': '\\'}
986 m = {'+': '-', '-': '+', '\\': '\\'}
987 hunk = ['%s%s' % (m[l[0:1]], l[1:]) for l in self.hunk]
987 hunk = ['%s%s' % (m[l[0:1]], l[1:]) for l in self.hunk]
988 return recordhunk(self.header, self.toline, self.fromline, self.proc,
988 return recordhunk(self.header, self.toline, self.fromline, self.proc,
989 self.before, hunk, self.after)
989 self.before, hunk, self.after)
990
990
991 def write(self, fp):
991 def write(self, fp):
992 delta = len(self.before) + len(self.after)
992 delta = len(self.before) + len(self.after)
993 if self.after and self.after[-1] == '\\ No newline at end of file\n':
993 if self.after and self.after[-1] == '\\ No newline at end of file\n':
994 delta -= 1
994 delta -= 1
995 fromlen = delta + self.removed
995 fromlen = delta + self.removed
996 tolen = delta + self.added
996 tolen = delta + self.added
997 fp.write('@@ -%d,%d +%d,%d @@%s\n' %
997 fp.write('@@ -%d,%d +%d,%d @@%s\n' %
998 (self.fromline, fromlen, self.toline, tolen,
998 (self.fromline, fromlen, self.toline, tolen,
999 self.proc and (' ' + self.proc)))
999 self.proc and (' ' + self.proc)))
1000 fp.write(''.join(self.before + self.hunk + self.after))
1000 fp.write(''.join(self.before + self.hunk + self.after))
1001
1001
1002 pretty = write
1002 pretty = write
1003
1003
1004 def filename(self):
1004 def filename(self):
1005 return self.header.filename()
1005 return self.header.filename()
1006
1006
1007 def __repr__(self):
1007 def __repr__(self):
1008 return '<hunk %r@%d>' % (self.filename(), self.fromline)
1008 return '<hunk %r@%d>' % (self.filename(), self.fromline)
1009
1009
1010 def getmessages():
1010 def getmessages():
1011 return {
1011 return {
1012 'multiple': {
1012 'multiple': {
1013 'apply': _("apply change %d/%d to '%s'?"),
1013 'apply': _("apply change %d/%d to '%s'?"),
1014 'discard': _("discard change %d/%d to '%s'?"),
1014 'discard': _("discard change %d/%d to '%s'?"),
1015 'record': _("record change %d/%d to '%s'?"),
1015 'record': _("record change %d/%d to '%s'?"),
1016 },
1016 },
1017 'single': {
1017 'single': {
1018 'apply': _("apply this change to '%s'?"),
1018 'apply': _("apply this change to '%s'?"),
1019 'discard': _("discard this change to '%s'?"),
1019 'discard': _("discard this change to '%s'?"),
1020 'record': _("record this change to '%s'?"),
1020 'record': _("record this change to '%s'?"),
1021 },
1021 },
1022 'help': {
1022 'help': {
1023 'apply': _('[Ynesfdaq?]'
1023 'apply': _('[Ynesfdaq?]'
1024 '$$ &Yes, apply this change'
1024 '$$ &Yes, apply this change'
1025 '$$ &No, skip this change'
1025 '$$ &No, skip this change'
1026 '$$ &Edit this change manually'
1026 '$$ &Edit this change manually'
1027 '$$ &Skip remaining changes to this file'
1027 '$$ &Skip remaining changes to this file'
1028 '$$ Apply remaining changes to this &file'
1028 '$$ Apply remaining changes to this &file'
1029 '$$ &Done, skip remaining changes and files'
1029 '$$ &Done, skip remaining changes and files'
1030 '$$ Apply &all changes to all remaining files'
1030 '$$ Apply &all changes to all remaining files'
1031 '$$ &Quit, applying no changes'
1031 '$$ &Quit, applying no changes'
1032 '$$ &? (display help)'),
1032 '$$ &? (display help)'),
1033 'discard': _('[Ynesfdaq?]'
1033 'discard': _('[Ynesfdaq?]'
1034 '$$ &Yes, discard this change'
1034 '$$ &Yes, discard this change'
1035 '$$ &No, skip this change'
1035 '$$ &No, skip this change'
1036 '$$ &Edit this change manually'
1036 '$$ &Edit this change manually'
1037 '$$ &Skip remaining changes to this file'
1037 '$$ &Skip remaining changes to this file'
1038 '$$ Discard remaining changes to this &file'
1038 '$$ Discard remaining changes to this &file'
1039 '$$ &Done, skip remaining changes and files'
1039 '$$ &Done, skip remaining changes and files'
1040 '$$ Discard &all changes to all remaining files'
1040 '$$ Discard &all changes to all remaining files'
1041 '$$ &Quit, discarding no changes'
1041 '$$ &Quit, discarding no changes'
1042 '$$ &? (display help)'),
1042 '$$ &? (display help)'),
1043 'record': _('[Ynesfdaq?]'
1043 'record': _('[Ynesfdaq?]'
1044 '$$ &Yes, record this change'
1044 '$$ &Yes, record this change'
1045 '$$ &No, skip this change'
1045 '$$ &No, skip this change'
1046 '$$ &Edit this change manually'
1046 '$$ &Edit this change manually'
1047 '$$ &Skip remaining changes to this file'
1047 '$$ &Skip remaining changes to this file'
1048 '$$ Record remaining changes to this &file'
1048 '$$ Record remaining changes to this &file'
1049 '$$ &Done, skip remaining changes and files'
1049 '$$ &Done, skip remaining changes and files'
1050 '$$ Record &all changes to all remaining files'
1050 '$$ Record &all changes to all remaining files'
1051 '$$ &Quit, recording no changes'
1051 '$$ &Quit, recording no changes'
1052 '$$ &? (display help)'),
1052 '$$ &? (display help)'),
1053 }
1053 }
1054 }
1054 }
1055
1055
1056 def filterpatch(ui, headers, operation=None):
1056 def filterpatch(ui, headers, operation=None):
1057 """Interactively filter patch chunks into applied-only chunks"""
1057 """Interactively filter patch chunks into applied-only chunks"""
1058 messages = getmessages()
1058 messages = getmessages()
1059
1059
1060 if operation is None:
1060 if operation is None:
1061 operation = 'record'
1061 operation = 'record'
1062
1062
1063 def prompt(skipfile, skipall, query, chunk):
1063 def prompt(skipfile, skipall, query, chunk):
1064 """prompt query, and process base inputs
1064 """prompt query, and process base inputs
1065
1065
1066 - y/n for the rest of file
1066 - y/n for the rest of file
1067 - y/n for the rest
1067 - y/n for the rest
1068 - ? (help)
1068 - ? (help)
1069 - q (quit)
1069 - q (quit)
1070
1070
1071 Return True/False and possibly updated skipfile and skipall.
1071 Return True/False and possibly updated skipfile and skipall.
1072 """
1072 """
1073 newpatches = None
1073 newpatches = None
1074 if skipall is not None:
1074 if skipall is not None:
1075 return skipall, skipfile, skipall, newpatches
1075 return skipall, skipfile, skipall, newpatches
1076 if skipfile is not None:
1076 if skipfile is not None:
1077 return skipfile, skipfile, skipall, newpatches
1077 return skipfile, skipfile, skipall, newpatches
1078 while True:
1078 while True:
1079 resps = messages['help'][operation]
1079 resps = messages['help'][operation]
1080 r = ui.promptchoice("%s %s" % (query, resps))
1080 r = ui.promptchoice("%s %s" % (query, resps))
1081 ui.write("\n")
1081 ui.write("\n")
1082 if r == 8: # ?
1082 if r == 8: # ?
1083 for c, t in ui.extractchoices(resps)[1]:
1083 for c, t in ui.extractchoices(resps)[1]:
1084 ui.write('%s - %s\n' % (c, encoding.lower(t)))
1084 ui.write('%s - %s\n' % (c, encoding.lower(t)))
1085 continue
1085 continue
1086 elif r == 0: # yes
1086 elif r == 0: # yes
1087 ret = True
1087 ret = True
1088 elif r == 1: # no
1088 elif r == 1: # no
1089 ret = False
1089 ret = False
1090 elif r == 2: # Edit patch
1090 elif r == 2: # Edit patch
1091 if chunk is None:
1091 if chunk is None:
1092 ui.write(_('cannot edit patch for whole file'))
1092 ui.write(_('cannot edit patch for whole file'))
1093 ui.write("\n")
1093 ui.write("\n")
1094 continue
1094 continue
1095 if chunk.header.binary():
1095 if chunk.header.binary():
1096 ui.write(_('cannot edit patch for binary file'))
1096 ui.write(_('cannot edit patch for binary file'))
1097 ui.write("\n")
1097 ui.write("\n")
1098 continue
1098 continue
1099 # Patch comment based on the Git one (based on comment at end of
1099 # Patch comment based on the Git one (based on comment at end of
1100 # https://mercurial-scm.org/wiki/RecordExtension)
1100 # https://mercurial-scm.org/wiki/RecordExtension)
1101 phelp = '---' + _("""
1101 phelp = '---' + _("""
1102 To remove '-' lines, make them ' ' lines (context).
1102 To remove '-' lines, make them ' ' lines (context).
1103 To remove '+' lines, delete them.
1103 To remove '+' lines, delete them.
1104 Lines starting with # will be removed from the patch.
1104 Lines starting with # will be removed from the patch.
1105
1105
1106 If the patch applies cleanly, the edited hunk will immediately be
1106 If the patch applies cleanly, the edited hunk will immediately be
1107 added to the record list. If it does not apply cleanly, a rejects
1107 added to the record list. If it does not apply cleanly, a rejects
1108 file will be generated: you can use that when you try again. If
1108 file will be generated: you can use that when you try again. If
1109 all lines of the hunk are removed, then the edit is aborted and
1109 all lines of the hunk are removed, then the edit is aborted and
1110 the hunk is left unchanged.
1110 the hunk is left unchanged.
1111 """)
1111 """)
1112 (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
1112 (patchfd, patchfn) = tempfile.mkstemp(prefix="hg-editor-",
1113 suffix=".diff")
1113 suffix=".diff")
1114 ncpatchfp = None
1114 ncpatchfp = None
1115 try:
1115 try:
1116 # Write the initial patch
1116 # Write the initial patch
1117 f = util.nativeeolwriter(os.fdopen(patchfd, r'wb'))
1117 f = util.nativeeolwriter(os.fdopen(patchfd, r'wb'))
1118 chunk.header.write(f)
1118 chunk.header.write(f)
1119 chunk.write(f)
1119 chunk.write(f)
1120 f.write('\n'.join(['# ' + i for i in phelp.splitlines()]))
1120 f.write('\n'.join(['# ' + i for i in phelp.splitlines()]))
1121 f.close()
1121 f.close()
1122 # Start the editor and wait for it to complete
1122 # Start the editor and wait for it to complete
1123 editor = ui.geteditor()
1123 editor = ui.geteditor()
1124 ret = ui.system("%s \"%s\"" % (editor, patchfn),
1124 ret = ui.system("%s \"%s\"" % (editor, patchfn),
1125 environ={'HGUSER': ui.username()},
1125 environ={'HGUSER': ui.username()},
1126 blockedtag='filterpatch')
1126 blockedtag='filterpatch')
1127 if ret != 0:
1127 if ret != 0:
1128 ui.warn(_("editor exited with exit code %d\n") % ret)
1128 ui.warn(_("editor exited with exit code %d\n") % ret)
1129 continue
1129 continue
1130 # Remove comment lines
1130 # Remove comment lines
1131 patchfp = open(patchfn, r'rb')
1131 patchfp = open(patchfn, r'rb')
1132 ncpatchfp = stringio()
1132 ncpatchfp = stringio()
1133 for line in util.iterfile(patchfp):
1133 for line in util.iterfile(patchfp):
1134 line = util.fromnativeeol(line)
1134 line = util.fromnativeeol(line)
1135 if not line.startswith('#'):
1135 if not line.startswith('#'):
1136 ncpatchfp.write(line)
1136 ncpatchfp.write(line)
1137 patchfp.close()
1137 patchfp.close()
1138 ncpatchfp.seek(0)
1138 ncpatchfp.seek(0)
1139 newpatches = parsepatch(ncpatchfp)
1139 newpatches = parsepatch(ncpatchfp)
1140 finally:
1140 finally:
1141 os.unlink(patchfn)
1141 os.unlink(patchfn)
1142 del ncpatchfp
1142 del ncpatchfp
1143 # Signal that the chunk shouldn't be applied as-is, but
1143 # Signal that the chunk shouldn't be applied as-is, but
1144 # provide the new patch to be used instead.
1144 # provide the new patch to be used instead.
1145 ret = False
1145 ret = False
1146 elif r == 3: # Skip
1146 elif r == 3: # Skip
1147 ret = skipfile = False
1147 ret = skipfile = False
1148 elif r == 4: # file (Record remaining)
1148 elif r == 4: # file (Record remaining)
1149 ret = skipfile = True
1149 ret = skipfile = True
1150 elif r == 5: # done, skip remaining
1150 elif r == 5: # done, skip remaining
1151 ret = skipall = False
1151 ret = skipall = False
1152 elif r == 6: # all
1152 elif r == 6: # all
1153 ret = skipall = True
1153 ret = skipall = True
1154 elif r == 7: # quit
1154 elif r == 7: # quit
1155 raise error.Abort(_('user quit'))
1155 raise error.Abort(_('user quit'))
1156 return ret, skipfile, skipall, newpatches
1156 return ret, skipfile, skipall, newpatches
1157
1157
1158 seen = set()
1158 seen = set()
1159 applied = {} # 'filename' -> [] of chunks
1159 applied = {} # 'filename' -> [] of chunks
1160 skipfile, skipall = None, None
1160 skipfile, skipall = None, None
1161 pos, total = 1, sum(len(h.hunks) for h in headers)
1161 pos, total = 1, sum(len(h.hunks) for h in headers)
1162 for h in headers:
1162 for h in headers:
1163 pos += len(h.hunks)
1163 pos += len(h.hunks)
1164 skipfile = None
1164 skipfile = None
1165 fixoffset = 0
1165 fixoffset = 0
1166 hdr = ''.join(h.header)
1166 hdr = ''.join(h.header)
1167 if hdr in seen:
1167 if hdr in seen:
1168 continue
1168 continue
1169 seen.add(hdr)
1169 seen.add(hdr)
1170 if skipall is None:
1170 if skipall is None:
1171 h.pretty(ui)
1171 h.pretty(ui)
1172 msg = (_('examine changes to %s?') %
1172 msg = (_('examine changes to %s?') %
1173 _(' and ').join("'%s'" % f for f in h.files()))
1173 _(' and ').join("'%s'" % f for f in h.files()))
1174 r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None)
1174 r, skipfile, skipall, np = prompt(skipfile, skipall, msg, None)
1175 if not r:
1175 if not r:
1176 continue
1176 continue
1177 applied[h.filename()] = [h]
1177 applied[h.filename()] = [h]
1178 if h.allhunks():
1178 if h.allhunks():
1179 applied[h.filename()] += h.hunks
1179 applied[h.filename()] += h.hunks
1180 continue
1180 continue
1181 for i, chunk in enumerate(h.hunks):
1181 for i, chunk in enumerate(h.hunks):
1182 if skipfile is None and skipall is None:
1182 if skipfile is None and skipall is None:
1183 chunk.pretty(ui)
1183 chunk.pretty(ui)
1184 if total == 1:
1184 if total == 1:
1185 msg = messages['single'][operation] % chunk.filename()
1185 msg = messages['single'][operation] % chunk.filename()
1186 else:
1186 else:
1187 idx = pos - len(h.hunks) + i
1187 idx = pos - len(h.hunks) + i
1188 msg = messages['multiple'][operation] % (idx, total,
1188 msg = messages['multiple'][operation] % (idx, total,
1189 chunk.filename())
1189 chunk.filename())
1190 r, skipfile, skipall, newpatches = prompt(skipfile,
1190 r, skipfile, skipall, newpatches = prompt(skipfile,
1191 skipall, msg, chunk)
1191 skipall, msg, chunk)
1192 if r:
1192 if r:
1193 if fixoffset:
1193 if fixoffset:
1194 chunk = copy.copy(chunk)
1194 chunk = copy.copy(chunk)
1195 chunk.toline += fixoffset
1195 chunk.toline += fixoffset
1196 applied[chunk.filename()].append(chunk)
1196 applied[chunk.filename()].append(chunk)
1197 elif newpatches is not None:
1197 elif newpatches is not None:
1198 for newpatch in newpatches:
1198 for newpatch in newpatches:
1199 for newhunk in newpatch.hunks:
1199 for newhunk in newpatch.hunks:
1200 if fixoffset:
1200 if fixoffset:
1201 newhunk.toline += fixoffset
1201 newhunk.toline += fixoffset
1202 applied[newhunk.filename()].append(newhunk)
1202 applied[newhunk.filename()].append(newhunk)
1203 else:
1203 else:
1204 fixoffset += chunk.removed - chunk.added
1204 fixoffset += chunk.removed - chunk.added
1205 return (sum([h for h in applied.itervalues()
1205 return (sum([h for h in applied.itervalues()
1206 if h[0].special() or len(h) > 1], []), {})
1206 if h[0].special() or len(h) > 1], []), {})
1207 class hunk(object):
1207 class hunk(object):
1208 def __init__(self, desc, num, lr, context):
1208 def __init__(self, desc, num, lr, context):
1209 self.number = num
1209 self.number = num
1210 self.desc = desc
1210 self.desc = desc
1211 self.hunk = [desc]
1211 self.hunk = [desc]
1212 self.a = []
1212 self.a = []
1213 self.b = []
1213 self.b = []
1214 self.starta = self.lena = None
1214 self.starta = self.lena = None
1215 self.startb = self.lenb = None
1215 self.startb = self.lenb = None
1216 if lr is not None:
1216 if lr is not None:
1217 if context:
1217 if context:
1218 self.read_context_hunk(lr)
1218 self.read_context_hunk(lr)
1219 else:
1219 else:
1220 self.read_unified_hunk(lr)
1220 self.read_unified_hunk(lr)
1221
1221
1222 def getnormalized(self):
1222 def getnormalized(self):
1223 """Return a copy with line endings normalized to LF."""
1223 """Return a copy with line endings normalized to LF."""
1224
1224
1225 def normalize(lines):
1225 def normalize(lines):
1226 nlines = []
1226 nlines = []
1227 for line in lines:
1227 for line in lines:
1228 if line.endswith('\r\n'):
1228 if line.endswith('\r\n'):
1229 line = line[:-2] + '\n'
1229 line = line[:-2] + '\n'
1230 nlines.append(line)
1230 nlines.append(line)
1231 return nlines
1231 return nlines
1232
1232
1233 # Dummy object, it is rebuilt manually
1233 # Dummy object, it is rebuilt manually
1234 nh = hunk(self.desc, self.number, None, None)
1234 nh = hunk(self.desc, self.number, None, None)
1235 nh.number = self.number
1235 nh.number = self.number
1236 nh.desc = self.desc
1236 nh.desc = self.desc
1237 nh.hunk = self.hunk
1237 nh.hunk = self.hunk
1238 nh.a = normalize(self.a)
1238 nh.a = normalize(self.a)
1239 nh.b = normalize(self.b)
1239 nh.b = normalize(self.b)
1240 nh.starta = self.starta
1240 nh.starta = self.starta
1241 nh.startb = self.startb
1241 nh.startb = self.startb
1242 nh.lena = self.lena
1242 nh.lena = self.lena
1243 nh.lenb = self.lenb
1243 nh.lenb = self.lenb
1244 return nh
1244 return nh
1245
1245
1246 def read_unified_hunk(self, lr):
1246 def read_unified_hunk(self, lr):
1247 m = unidesc.match(self.desc)
1247 m = unidesc.match(self.desc)
1248 if not m:
1248 if not m:
1249 raise PatchError(_("bad hunk #%d") % self.number)
1249 raise PatchError(_("bad hunk #%d") % self.number)
1250 self.starta, self.lena, self.startb, self.lenb = m.groups()
1250 self.starta, self.lena, self.startb, self.lenb = m.groups()
1251 if self.lena is None:
1251 if self.lena is None:
1252 self.lena = 1
1252 self.lena = 1
1253 else:
1253 else:
1254 self.lena = int(self.lena)
1254 self.lena = int(self.lena)
1255 if self.lenb is None:
1255 if self.lenb is None:
1256 self.lenb = 1
1256 self.lenb = 1
1257 else:
1257 else:
1258 self.lenb = int(self.lenb)
1258 self.lenb = int(self.lenb)
1259 self.starta = int(self.starta)
1259 self.starta = int(self.starta)
1260 self.startb = int(self.startb)
1260 self.startb = int(self.startb)
1261 try:
1261 try:
1262 diffhelper.addlines(lr, self.hunk, self.lena, self.lenb,
1262 diffhelper.addlines(lr, self.hunk, self.lena, self.lenb,
1263 self.a, self.b)
1263 self.a, self.b)
1264 except error.ParseError as e:
1264 except error.ParseError as e:
1265 raise PatchError(_("bad hunk #%d: %s") % (self.number, e))
1265 raise PatchError(_("bad hunk #%d: %s") % (self.number, e))
1266 # if we hit eof before finishing out the hunk, the last line will
1266 # if we hit eof before finishing out the hunk, the last line will
1267 # be zero length. Lets try to fix it up.
1267 # be zero length. Lets try to fix it up.
1268 while len(self.hunk[-1]) == 0:
1268 while len(self.hunk[-1]) == 0:
1269 del self.hunk[-1]
1269 del self.hunk[-1]
1270 del self.a[-1]
1270 del self.a[-1]
1271 del self.b[-1]
1271 del self.b[-1]
1272 self.lena -= 1
1272 self.lena -= 1
1273 self.lenb -= 1
1273 self.lenb -= 1
1274 self._fixnewline(lr)
1274 self._fixnewline(lr)
1275
1275
1276 def read_context_hunk(self, lr):
1276 def read_context_hunk(self, lr):
1277 self.desc = lr.readline()
1277 self.desc = lr.readline()
1278 m = contextdesc.match(self.desc)
1278 m = contextdesc.match(self.desc)
1279 if not m:
1279 if not m:
1280 raise PatchError(_("bad hunk #%d") % self.number)
1280 raise PatchError(_("bad hunk #%d") % self.number)
1281 self.starta, aend = m.groups()
1281 self.starta, aend = m.groups()
1282 self.starta = int(self.starta)
1282 self.starta = int(self.starta)
1283 if aend is None:
1283 if aend is None:
1284 aend = self.starta
1284 aend = self.starta
1285 self.lena = int(aend) - self.starta
1285 self.lena = int(aend) - self.starta
1286 if self.starta:
1286 if self.starta:
1287 self.lena += 1
1287 self.lena += 1
1288 for x in xrange(self.lena):
1288 for x in xrange(self.lena):
1289 l = lr.readline()
1289 l = lr.readline()
1290 if l.startswith('---'):
1290 if l.startswith('---'):
1291 # lines addition, old block is empty
1291 # lines addition, old block is empty
1292 lr.push(l)
1292 lr.push(l)
1293 break
1293 break
1294 s = l[2:]
1294 s = l[2:]
1295 if l.startswith('- ') or l.startswith('! '):
1295 if l.startswith('- ') or l.startswith('! '):
1296 u = '-' + s
1296 u = '-' + s
1297 elif l.startswith(' '):
1297 elif l.startswith(' '):
1298 u = ' ' + s
1298 u = ' ' + s
1299 else:
1299 else:
1300 raise PatchError(_("bad hunk #%d old text line %d") %
1300 raise PatchError(_("bad hunk #%d old text line %d") %
1301 (self.number, x))
1301 (self.number, x))
1302 self.a.append(u)
1302 self.a.append(u)
1303 self.hunk.append(u)
1303 self.hunk.append(u)
1304
1304
1305 l = lr.readline()
1305 l = lr.readline()
1306 if l.startswith('\ '):
1306 if l.startswith('\ '):
1307 s = self.a[-1][:-1]
1307 s = self.a[-1][:-1]
1308 self.a[-1] = s
1308 self.a[-1] = s
1309 self.hunk[-1] = s
1309 self.hunk[-1] = s
1310 l = lr.readline()
1310 l = lr.readline()
1311 m = contextdesc.match(l)
1311 m = contextdesc.match(l)
1312 if not m:
1312 if not m:
1313 raise PatchError(_("bad hunk #%d") % self.number)
1313 raise PatchError(_("bad hunk #%d") % self.number)
1314 self.startb, bend = m.groups()
1314 self.startb, bend = m.groups()
1315 self.startb = int(self.startb)
1315 self.startb = int(self.startb)
1316 if bend is None:
1316 if bend is None:
1317 bend = self.startb
1317 bend = self.startb
1318 self.lenb = int(bend) - self.startb
1318 self.lenb = int(bend) - self.startb
1319 if self.startb:
1319 if self.startb:
1320 self.lenb += 1
1320 self.lenb += 1
1321 hunki = 1
1321 hunki = 1
1322 for x in xrange(self.lenb):
1322 for x in xrange(self.lenb):
1323 l = lr.readline()
1323 l = lr.readline()
1324 if l.startswith('\ '):
1324 if l.startswith('\ '):
1325 # XXX: the only way to hit this is with an invalid line range.
1325 # XXX: the only way to hit this is with an invalid line range.
1326 # The no-eol marker is not counted in the line range, but I
1326 # The no-eol marker is not counted in the line range, but I
1327 # guess there are diff(1) out there which behave differently.
1327 # guess there are diff(1) out there which behave differently.
1328 s = self.b[-1][:-1]
1328 s = self.b[-1][:-1]
1329 self.b[-1] = s
1329 self.b[-1] = s
1330 self.hunk[hunki - 1] = s
1330 self.hunk[hunki - 1] = s
1331 continue
1331 continue
1332 if not l:
1332 if not l:
1333 # line deletions, new block is empty and we hit EOF
1333 # line deletions, new block is empty and we hit EOF
1334 lr.push(l)
1334 lr.push(l)
1335 break
1335 break
1336 s = l[2:]
1336 s = l[2:]
1337 if l.startswith('+ ') or l.startswith('! '):
1337 if l.startswith('+ ') or l.startswith('! '):
1338 u = '+' + s
1338 u = '+' + s
1339 elif l.startswith(' '):
1339 elif l.startswith(' '):
1340 u = ' ' + s
1340 u = ' ' + s
1341 elif len(self.b) == 0:
1341 elif len(self.b) == 0:
1342 # line deletions, new block is empty
1342 # line deletions, new block is empty
1343 lr.push(l)
1343 lr.push(l)
1344 break
1344 break
1345 else:
1345 else:
1346 raise PatchError(_("bad hunk #%d old text line %d") %
1346 raise PatchError(_("bad hunk #%d old text line %d") %
1347 (self.number, x))
1347 (self.number, x))
1348 self.b.append(s)
1348 self.b.append(s)
1349 while True:
1349 while True:
1350 if hunki >= len(self.hunk):
1350 if hunki >= len(self.hunk):
1351 h = ""
1351 h = ""
1352 else:
1352 else:
1353 h = self.hunk[hunki]
1353 h = self.hunk[hunki]
1354 hunki += 1
1354 hunki += 1
1355 if h == u:
1355 if h == u:
1356 break
1356 break
1357 elif h.startswith('-'):
1357 elif h.startswith('-'):
1358 continue
1358 continue
1359 else:
1359 else:
1360 self.hunk.insert(hunki - 1, u)
1360 self.hunk.insert(hunki - 1, u)
1361 break
1361 break
1362
1362
1363 if not self.a:
1363 if not self.a:
1364 # this happens when lines were only added to the hunk
1364 # this happens when lines were only added to the hunk
1365 for x in self.hunk:
1365 for x in self.hunk:
1366 if x.startswith('-') or x.startswith(' '):
1366 if x.startswith('-') or x.startswith(' '):
1367 self.a.append(x)
1367 self.a.append(x)
1368 if not self.b:
1368 if not self.b:
1369 # this happens when lines were only deleted from the hunk
1369 # this happens when lines were only deleted from the hunk
1370 for x in self.hunk:
1370 for x in self.hunk:
1371 if x.startswith('+') or x.startswith(' '):
1371 if x.startswith('+') or x.startswith(' '):
1372 self.b.append(x[1:])
1372 self.b.append(x[1:])
1373 # @@ -start,len +start,len @@
1373 # @@ -start,len +start,len @@
1374 self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
1374 self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
1375 self.startb, self.lenb)
1375 self.startb, self.lenb)
1376 self.hunk[0] = self.desc
1376 self.hunk[0] = self.desc
1377 self._fixnewline(lr)
1377 self._fixnewline(lr)
1378
1378
1379 def _fixnewline(self, lr):
1379 def _fixnewline(self, lr):
1380 l = lr.readline()
1380 l = lr.readline()
1381 if l.startswith('\ '):
1381 if l.startswith('\ '):
1382 diffhelper.fixnewline(self.hunk, self.a, self.b)
1382 diffhelper.fixnewline(self.hunk, self.a, self.b)
1383 else:
1383 else:
1384 lr.push(l)
1384 lr.push(l)
1385
1385
1386 def complete(self):
1386 def complete(self):
1387 return len(self.a) == self.lena and len(self.b) == self.lenb
1387 return len(self.a) == self.lena and len(self.b) == self.lenb
1388
1388
1389 def _fuzzit(self, old, new, fuzz, toponly):
1389 def _fuzzit(self, old, new, fuzz, toponly):
1390 # this removes context lines from the top and bottom of list 'l'. It
1390 # this removes context lines from the top and bottom of list 'l'. It
1391 # checks the hunk to make sure only context lines are removed, and then
1391 # checks the hunk to make sure only context lines are removed, and then
1392 # returns a new shortened list of lines.
1392 # returns a new shortened list of lines.
1393 fuzz = min(fuzz, len(old))
1393 fuzz = min(fuzz, len(old))
1394 if fuzz:
1394 if fuzz:
1395 top = 0
1395 top = 0
1396 bot = 0
1396 bot = 0
1397 hlen = len(self.hunk)
1397 hlen = len(self.hunk)
1398 for x in xrange(hlen - 1):
1398 for x in xrange(hlen - 1):
1399 # the hunk starts with the @@ line, so use x+1
1399 # the hunk starts with the @@ line, so use x+1
1400 if self.hunk[x + 1].startswith(' '):
1400 if self.hunk[x + 1].startswith(' '):
1401 top += 1
1401 top += 1
1402 else:
1402 else:
1403 break
1403 break
1404 if not toponly:
1404 if not toponly:
1405 for x in xrange(hlen - 1):
1405 for x in xrange(hlen - 1):
1406 if self.hunk[hlen - bot - 1].startswith(' '):
1406 if self.hunk[hlen - bot - 1].startswith(' '):
1407 bot += 1
1407 bot += 1
1408 else:
1408 else:
1409 break
1409 break
1410
1410
1411 bot = min(fuzz, bot)
1411 bot = min(fuzz, bot)
1412 top = min(fuzz, top)
1412 top = min(fuzz, top)
1413 return old[top:len(old) - bot], new[top:len(new) - bot], top
1413 return old[top:len(old) - bot], new[top:len(new) - bot], top
1414 return old, new, 0
1414 return old, new, 0
1415
1415
1416 def fuzzit(self, fuzz, toponly):
1416 def fuzzit(self, fuzz, toponly):
1417 old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly)
1417 old, new, top = self._fuzzit(self.a, self.b, fuzz, toponly)
1418 oldstart = self.starta + top
1418 oldstart = self.starta + top
1419 newstart = self.startb + top
1419 newstart = self.startb + top
1420 # zero length hunk ranges already have their start decremented
1420 # zero length hunk ranges already have their start decremented
1421 if self.lena and oldstart > 0:
1421 if self.lena and oldstart > 0:
1422 oldstart -= 1
1422 oldstart -= 1
1423 if self.lenb and newstart > 0:
1423 if self.lenb and newstart > 0:
1424 newstart -= 1
1424 newstart -= 1
1425 return old, oldstart, new, newstart
1425 return old, oldstart, new, newstart
1426
1426
1427 class binhunk(object):
1427 class binhunk(object):
1428 'A binary patch file.'
1428 'A binary patch file.'
1429 def __init__(self, lr, fname):
1429 def __init__(self, lr, fname):
1430 self.text = None
1430 self.text = None
1431 self.delta = False
1431 self.delta = False
1432 self.hunk = ['GIT binary patch\n']
1432 self.hunk = ['GIT binary patch\n']
1433 self._fname = fname
1433 self._fname = fname
1434 self._read(lr)
1434 self._read(lr)
1435
1435
1436 def complete(self):
1436 def complete(self):
1437 return self.text is not None
1437 return self.text is not None
1438
1438
1439 def new(self, lines):
1439 def new(self, lines):
1440 if self.delta:
1440 if self.delta:
1441 return [applybindelta(self.text, ''.join(lines))]
1441 return [applybindelta(self.text, ''.join(lines))]
1442 return [self.text]
1442 return [self.text]
1443
1443
1444 def _read(self, lr):
1444 def _read(self, lr):
1445 def getline(lr, hunk):
1445 def getline(lr, hunk):
1446 l = lr.readline()
1446 l = lr.readline()
1447 hunk.append(l)
1447 hunk.append(l)
1448 return l.rstrip('\r\n')
1448 return l.rstrip('\r\n')
1449
1449
1450 size = 0
1450 size = 0
1451 while True:
1451 while True:
1452 line = getline(lr, self.hunk)
1452 line = getline(lr, self.hunk)
1453 if not line:
1453 if not line:
1454 raise PatchError(_('could not extract "%s" binary data')
1454 raise PatchError(_('could not extract "%s" binary data')
1455 % self._fname)
1455 % self._fname)
1456 if line.startswith('literal '):
1456 if line.startswith('literal '):
1457 size = int(line[8:].rstrip())
1457 size = int(line[8:].rstrip())
1458 break
1458 break
1459 if line.startswith('delta '):
1459 if line.startswith('delta '):
1460 size = int(line[6:].rstrip())
1460 size = int(line[6:].rstrip())
1461 self.delta = True
1461 self.delta = True
1462 break
1462 break
1463 dec = []
1463 dec = []
1464 line = getline(lr, self.hunk)
1464 line = getline(lr, self.hunk)
1465 while len(line) > 1:
1465 while len(line) > 1:
1466 l = line[0:1]
1466 l = line[0:1]
1467 if l <= 'Z' and l >= 'A':
1467 if l <= 'Z' and l >= 'A':
1468 l = ord(l) - ord('A') + 1
1468 l = ord(l) - ord('A') + 1
1469 else:
1469 else:
1470 l = ord(l) - ord('a') + 27
1470 l = ord(l) - ord('a') + 27
1471 try:
1471 try:
1472 dec.append(util.b85decode(line[1:])[:l])
1472 dec.append(util.b85decode(line[1:])[:l])
1473 except ValueError as e:
1473 except ValueError as e:
1474 raise PatchError(_('could not decode "%s" binary patch: %s')
1474 raise PatchError(_('could not decode "%s" binary patch: %s')
1475 % (self._fname, stringutil.forcebytestr(e)))
1475 % (self._fname, stringutil.forcebytestr(e)))
1476 line = getline(lr, self.hunk)
1476 line = getline(lr, self.hunk)
1477 text = zlib.decompress(''.join(dec))
1477 text = zlib.decompress(''.join(dec))
1478 if len(text) != size:
1478 if len(text) != size:
1479 raise PatchError(_('"%s" length is %d bytes, should be %d')
1479 raise PatchError(_('"%s" length is %d bytes, should be %d')
1480 % (self._fname, len(text), size))
1480 % (self._fname, len(text), size))
1481 self.text = text
1481 self.text = text
1482
1482
1483 def parsefilename(str):
1483 def parsefilename(str):
1484 # --- filename \t|space stuff
1484 # --- filename \t|space stuff
1485 s = str[4:].rstrip('\r\n')
1485 s = str[4:].rstrip('\r\n')
1486 i = s.find('\t')
1486 i = s.find('\t')
1487 if i < 0:
1487 if i < 0:
1488 i = s.find(' ')
1488 i = s.find(' ')
1489 if i < 0:
1489 if i < 0:
1490 return s
1490 return s
1491 return s[:i]
1491 return s[:i]
1492
1492
1493 def reversehunks(hunks):
1493 def reversehunks(hunks):
1494 '''reverse the signs in the hunks given as argument
1494 '''reverse the signs in the hunks given as argument
1495
1495
1496 This function operates on hunks coming out of patch.filterpatch, that is
1496 This function operates on hunks coming out of patch.filterpatch, that is
1497 a list of the form: [header1, hunk1, hunk2, header2...]. Example usage:
1497 a list of the form: [header1, hunk1, hunk2, header2...]. Example usage:
1498
1498
1499 >>> rawpatch = b"""diff --git a/folder1/g b/folder1/g
1499 >>> rawpatch = b"""diff --git a/folder1/g b/folder1/g
1500 ... --- a/folder1/g
1500 ... --- a/folder1/g
1501 ... +++ b/folder1/g
1501 ... +++ b/folder1/g
1502 ... @@ -1,7 +1,7 @@
1502 ... @@ -1,7 +1,7 @@
1503 ... +firstline
1503 ... +firstline
1504 ... c
1504 ... c
1505 ... 1
1505 ... 1
1506 ... 2
1506 ... 2
1507 ... + 3
1507 ... + 3
1508 ... -4
1508 ... -4
1509 ... 5
1509 ... 5
1510 ... d
1510 ... d
1511 ... +lastline"""
1511 ... +lastline"""
1512 >>> hunks = parsepatch([rawpatch])
1512 >>> hunks = parsepatch([rawpatch])
1513 >>> hunkscomingfromfilterpatch = []
1513 >>> hunkscomingfromfilterpatch = []
1514 >>> for h in hunks:
1514 >>> for h in hunks:
1515 ... hunkscomingfromfilterpatch.append(h)
1515 ... hunkscomingfromfilterpatch.append(h)
1516 ... hunkscomingfromfilterpatch.extend(h.hunks)
1516 ... hunkscomingfromfilterpatch.extend(h.hunks)
1517
1517
1518 >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch)
1518 >>> reversedhunks = reversehunks(hunkscomingfromfilterpatch)
1519 >>> from . import util
1519 >>> from . import util
1520 >>> fp = util.stringio()
1520 >>> fp = util.stringio()
1521 >>> for c in reversedhunks:
1521 >>> for c in reversedhunks:
1522 ... c.write(fp)
1522 ... c.write(fp)
1523 >>> fp.seek(0) or None
1523 >>> fp.seek(0) or None
1524 >>> reversedpatch = fp.read()
1524 >>> reversedpatch = fp.read()
1525 >>> print(pycompat.sysstr(reversedpatch))
1525 >>> print(pycompat.sysstr(reversedpatch))
1526 diff --git a/folder1/g b/folder1/g
1526 diff --git a/folder1/g b/folder1/g
1527 --- a/folder1/g
1527 --- a/folder1/g
1528 +++ b/folder1/g
1528 +++ b/folder1/g
1529 @@ -1,4 +1,3 @@
1529 @@ -1,4 +1,3 @@
1530 -firstline
1530 -firstline
1531 c
1531 c
1532 1
1532 1
1533 2
1533 2
1534 @@ -2,6 +1,6 @@
1534 @@ -2,6 +1,6 @@
1535 c
1535 c
1536 1
1536 1
1537 2
1537 2
1538 - 3
1538 - 3
1539 +4
1539 +4
1540 5
1540 5
1541 d
1541 d
1542 @@ -6,3 +5,2 @@
1542 @@ -6,3 +5,2 @@
1543 5
1543 5
1544 d
1544 d
1545 -lastline
1545 -lastline
1546
1546
1547 '''
1547 '''
1548
1548
1549 newhunks = []
1549 newhunks = []
1550 for c in hunks:
1550 for c in hunks:
1551 if util.safehasattr(c, 'reversehunk'):
1551 if util.safehasattr(c, 'reversehunk'):
1552 c = c.reversehunk()
1552 c = c.reversehunk()
1553 newhunks.append(c)
1553 newhunks.append(c)
1554 return newhunks
1554 return newhunks
1555
1555
1556 def parsepatch(originalchunks, maxcontext=None):
1556 def parsepatch(originalchunks, maxcontext=None):
1557 """patch -> [] of headers -> [] of hunks
1557 """patch -> [] of headers -> [] of hunks
1558
1558
1559 If maxcontext is not None, trim context lines if necessary.
1559 If maxcontext is not None, trim context lines if necessary.
1560
1560
1561 >>> rawpatch = b'''diff --git a/folder1/g b/folder1/g
1561 >>> rawpatch = b'''diff --git a/folder1/g b/folder1/g
1562 ... --- a/folder1/g
1562 ... --- a/folder1/g
1563 ... +++ b/folder1/g
1563 ... +++ b/folder1/g
1564 ... @@ -1,8 +1,10 @@
1564 ... @@ -1,8 +1,10 @@
1565 ... 1
1565 ... 1
1566 ... 2
1566 ... 2
1567 ... -3
1567 ... -3
1568 ... 4
1568 ... 4
1569 ... 5
1569 ... 5
1570 ... 6
1570 ... 6
1571 ... +6.1
1571 ... +6.1
1572 ... +6.2
1572 ... +6.2
1573 ... 7
1573 ... 7
1574 ... 8
1574 ... 8
1575 ... +9'''
1575 ... +9'''
1576 >>> out = util.stringio()
1576 >>> out = util.stringio()
1577 >>> headers = parsepatch([rawpatch], maxcontext=1)
1577 >>> headers = parsepatch([rawpatch], maxcontext=1)
1578 >>> for header in headers:
1578 >>> for header in headers:
1579 ... header.write(out)
1579 ... header.write(out)
1580 ... for hunk in header.hunks:
1580 ... for hunk in header.hunks:
1581 ... hunk.write(out)
1581 ... hunk.write(out)
1582 >>> print(pycompat.sysstr(out.getvalue()))
1582 >>> print(pycompat.sysstr(out.getvalue()))
1583 diff --git a/folder1/g b/folder1/g
1583 diff --git a/folder1/g b/folder1/g
1584 --- a/folder1/g
1584 --- a/folder1/g
1585 +++ b/folder1/g
1585 +++ b/folder1/g
1586 @@ -2,3 +2,2 @@
1586 @@ -2,3 +2,2 @@
1587 2
1587 2
1588 -3
1588 -3
1589 4
1589 4
1590 @@ -6,2 +5,4 @@
1590 @@ -6,2 +5,4 @@
1591 6
1591 6
1592 +6.1
1592 +6.1
1593 +6.2
1593 +6.2
1594 7
1594 7
1595 @@ -8,1 +9,2 @@
1595 @@ -8,1 +9,2 @@
1596 8
1596 8
1597 +9
1597 +9
1598 """
1598 """
1599 class parser(object):
1599 class parser(object):
1600 """patch parsing state machine"""
1600 """patch parsing state machine"""
1601 def __init__(self):
1601 def __init__(self):
1602 self.fromline = 0
1602 self.fromline = 0
1603 self.toline = 0
1603 self.toline = 0
1604 self.proc = ''
1604 self.proc = ''
1605 self.header = None
1605 self.header = None
1606 self.context = []
1606 self.context = []
1607 self.before = []
1607 self.before = []
1608 self.hunk = []
1608 self.hunk = []
1609 self.headers = []
1609 self.headers = []
1610
1610
1611 def addrange(self, limits):
1611 def addrange(self, limits):
1612 fromstart, fromend, tostart, toend, proc = limits
1612 fromstart, fromend, tostart, toend, proc = limits
1613 self.fromline = int(fromstart)
1613 self.fromline = int(fromstart)
1614 self.toline = int(tostart)
1614 self.toline = int(tostart)
1615 self.proc = proc
1615 self.proc = proc
1616
1616
1617 def addcontext(self, context):
1617 def addcontext(self, context):
1618 if self.hunk:
1618 if self.hunk:
1619 h = recordhunk(self.header, self.fromline, self.toline,
1619 h = recordhunk(self.header, self.fromline, self.toline,
1620 self.proc, self.before, self.hunk, context, maxcontext)
1620 self.proc, self.before, self.hunk, context, maxcontext)
1621 self.header.hunks.append(h)
1621 self.header.hunks.append(h)
1622 self.fromline += len(self.before) + h.removed
1622 self.fromline += len(self.before) + h.removed
1623 self.toline += len(self.before) + h.added
1623 self.toline += len(self.before) + h.added
1624 self.before = []
1624 self.before = []
1625 self.hunk = []
1625 self.hunk = []
1626 self.context = context
1626 self.context = context
1627
1627
1628 def addhunk(self, hunk):
1628 def addhunk(self, hunk):
1629 if self.context:
1629 if self.context:
1630 self.before = self.context
1630 self.before = self.context
1631 self.context = []
1631 self.context = []
1632 self.hunk = hunk
1632 self.hunk = hunk
1633
1633
1634 def newfile(self, hdr):
1634 def newfile(self, hdr):
1635 self.addcontext([])
1635 self.addcontext([])
1636 h = header(hdr)
1636 h = header(hdr)
1637 self.headers.append(h)
1637 self.headers.append(h)
1638 self.header = h
1638 self.header = h
1639
1639
1640 def addother(self, line):
1640 def addother(self, line):
1641 pass # 'other' lines are ignored
1641 pass # 'other' lines are ignored
1642
1642
1643 def finished(self):
1643 def finished(self):
1644 self.addcontext([])
1644 self.addcontext([])
1645 return self.headers
1645 return self.headers
1646
1646
1647 transitions = {
1647 transitions = {
1648 'file': {'context': addcontext,
1648 'file': {'context': addcontext,
1649 'file': newfile,
1649 'file': newfile,
1650 'hunk': addhunk,
1650 'hunk': addhunk,
1651 'range': addrange},
1651 'range': addrange},
1652 'context': {'file': newfile,
1652 'context': {'file': newfile,
1653 'hunk': addhunk,
1653 'hunk': addhunk,
1654 'range': addrange,
1654 'range': addrange,
1655 'other': addother},
1655 'other': addother},
1656 'hunk': {'context': addcontext,
1656 'hunk': {'context': addcontext,
1657 'file': newfile,
1657 'file': newfile,
1658 'range': addrange},
1658 'range': addrange},
1659 'range': {'context': addcontext,
1659 'range': {'context': addcontext,
1660 'hunk': addhunk},
1660 'hunk': addhunk},
1661 'other': {'other': addother},
1661 'other': {'other': addother},
1662 }
1662 }
1663
1663
1664 p = parser()
1664 p = parser()
1665 fp = stringio()
1665 fp = stringio()
1666 fp.write(''.join(originalchunks))
1666 fp.write(''.join(originalchunks))
1667 fp.seek(0)
1667 fp.seek(0)
1668
1668
1669 state = 'context'
1669 state = 'context'
1670 for newstate, data in scanpatch(fp):
1670 for newstate, data in scanpatch(fp):
1671 try:
1671 try:
1672 p.transitions[state][newstate](p, data)
1672 p.transitions[state][newstate](p, data)
1673 except KeyError:
1673 except KeyError:
1674 raise PatchError('unhandled transition: %s -> %s' %
1674 raise PatchError('unhandled transition: %s -> %s' %
1675 (state, newstate))
1675 (state, newstate))
1676 state = newstate
1676 state = newstate
1677 del fp
1677 del fp
1678 return p.finished()
1678 return p.finished()
1679
1679
1680 def pathtransform(path, strip, prefix):
1680 def pathtransform(path, strip, prefix):
1681 '''turn a path from a patch into a path suitable for the repository
1681 '''turn a path from a patch into a path suitable for the repository
1682
1682
1683 prefix, if not empty, is expected to be normalized with a / at the end.
1683 prefix, if not empty, is expected to be normalized with a / at the end.
1684
1684
1685 Returns (stripped components, path in repository).
1685 Returns (stripped components, path in repository).
1686
1686
1687 >>> pathtransform(b'a/b/c', 0, b'')
1687 >>> pathtransform(b'a/b/c', 0, b'')
1688 ('', 'a/b/c')
1688 ('', 'a/b/c')
1689 >>> pathtransform(b' a/b/c ', 0, b'')
1689 >>> pathtransform(b' a/b/c ', 0, b'')
1690 ('', ' a/b/c')
1690 ('', ' a/b/c')
1691 >>> pathtransform(b' a/b/c ', 2, b'')
1691 >>> pathtransform(b' a/b/c ', 2, b'')
1692 ('a/b/', 'c')
1692 ('a/b/', 'c')
1693 >>> pathtransform(b'a/b/c', 0, b'd/e/')
1693 >>> pathtransform(b'a/b/c', 0, b'd/e/')
1694 ('', 'd/e/a/b/c')
1694 ('', 'd/e/a/b/c')
1695 >>> pathtransform(b' a//b/c ', 2, b'd/e/')
1695 >>> pathtransform(b' a//b/c ', 2, b'd/e/')
1696 ('a//b/', 'd/e/c')
1696 ('a//b/', 'd/e/c')
1697 >>> pathtransform(b'a/b/c', 3, b'')
1697 >>> pathtransform(b'a/b/c', 3, b'')
1698 Traceback (most recent call last):
1698 Traceback (most recent call last):
1699 PatchError: unable to strip away 1 of 3 dirs from a/b/c
1699 PatchError: unable to strip away 1 of 3 dirs from a/b/c
1700 '''
1700 '''
1701 pathlen = len(path)
1701 pathlen = len(path)
1702 i = 0
1702 i = 0
1703 if strip == 0:
1703 if strip == 0:
1704 return '', prefix + path.rstrip()
1704 return '', prefix + path.rstrip()
1705 count = strip
1705 count = strip
1706 while count > 0:
1706 while count > 0:
1707 i = path.find('/', i)
1707 i = path.find('/', i)
1708 if i == -1:
1708 if i == -1:
1709 raise PatchError(_("unable to strip away %d of %d dirs from %s") %
1709 raise PatchError(_("unable to strip away %d of %d dirs from %s") %
1710 (count, strip, path))
1710 (count, strip, path))
1711 i += 1
1711 i += 1
1712 # consume '//' in the path
1712 # consume '//' in the path
1713 while i < pathlen - 1 and path[i:i + 1] == '/':
1713 while i < pathlen - 1 and path[i:i + 1] == '/':
1714 i += 1
1714 i += 1
1715 count -= 1
1715 count -= 1
1716 return path[:i].lstrip(), prefix + path[i:].rstrip()
1716 return path[:i].lstrip(), prefix + path[i:].rstrip()
1717
1717
1718 def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip, prefix):
1718 def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip, prefix):
1719 nulla = afile_orig == "/dev/null"
1719 nulla = afile_orig == "/dev/null"
1720 nullb = bfile_orig == "/dev/null"
1720 nullb = bfile_orig == "/dev/null"
1721 create = nulla and hunk.starta == 0 and hunk.lena == 0
1721 create = nulla and hunk.starta == 0 and hunk.lena == 0
1722 remove = nullb and hunk.startb == 0 and hunk.lenb == 0
1722 remove = nullb and hunk.startb == 0 and hunk.lenb == 0
1723 abase, afile = pathtransform(afile_orig, strip, prefix)
1723 abase, afile = pathtransform(afile_orig, strip, prefix)
1724 gooda = not nulla and backend.exists(afile)
1724 gooda = not nulla and backend.exists(afile)
1725 bbase, bfile = pathtransform(bfile_orig, strip, prefix)
1725 bbase, bfile = pathtransform(bfile_orig, strip, prefix)
1726 if afile == bfile:
1726 if afile == bfile:
1727 goodb = gooda
1727 goodb = gooda
1728 else:
1728 else:
1729 goodb = not nullb and backend.exists(bfile)
1729 goodb = not nullb and backend.exists(bfile)
1730 missing = not goodb and not gooda and not create
1730 missing = not goodb and not gooda and not create
1731
1731
1732 # some diff programs apparently produce patches where the afile is
1732 # some diff programs apparently produce patches where the afile is
1733 # not /dev/null, but afile starts with bfile
1733 # not /dev/null, but afile starts with bfile
1734 abasedir = afile[:afile.rfind('/') + 1]
1734 abasedir = afile[:afile.rfind('/') + 1]
1735 bbasedir = bfile[:bfile.rfind('/') + 1]
1735 bbasedir = bfile[:bfile.rfind('/') + 1]
1736 if (missing and abasedir == bbasedir and afile.startswith(bfile)
1736 if (missing and abasedir == bbasedir and afile.startswith(bfile)
1737 and hunk.starta == 0 and hunk.lena == 0):
1737 and hunk.starta == 0 and hunk.lena == 0):
1738 create = True
1738 create = True
1739 missing = False
1739 missing = False
1740
1740
1741 # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the
1741 # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the
1742 # diff is between a file and its backup. In this case, the original
1742 # diff is between a file and its backup. In this case, the original
1743 # file should be patched (see original mpatch code).
1743 # file should be patched (see original mpatch code).
1744 isbackup = (abase == bbase and bfile.startswith(afile))
1744 isbackup = (abase == bbase and bfile.startswith(afile))
1745 fname = None
1745 fname = None
1746 if not missing:
1746 if not missing:
1747 if gooda and goodb:
1747 if gooda and goodb:
1748 if isbackup:
1748 if isbackup:
1749 fname = afile
1749 fname = afile
1750 else:
1750 else:
1751 fname = bfile
1751 fname = bfile
1752 elif gooda:
1752 elif gooda:
1753 fname = afile
1753 fname = afile
1754
1754
1755 if not fname:
1755 if not fname:
1756 if not nullb:
1756 if not nullb:
1757 if isbackup:
1757 if isbackup:
1758 fname = afile
1758 fname = afile
1759 else:
1759 else:
1760 fname = bfile
1760 fname = bfile
1761 elif not nulla:
1761 elif not nulla:
1762 fname = afile
1762 fname = afile
1763 else:
1763 else:
1764 raise PatchError(_("undefined source and destination files"))
1764 raise PatchError(_("undefined source and destination files"))
1765
1765
1766 gp = patchmeta(fname)
1766 gp = patchmeta(fname)
1767 if create:
1767 if create:
1768 gp.op = 'ADD'
1768 gp.op = 'ADD'
1769 elif remove:
1769 elif remove:
1770 gp.op = 'DELETE'
1770 gp.op = 'DELETE'
1771 return gp
1771 return gp
1772
1772
1773 def scanpatch(fp):
1773 def scanpatch(fp):
1774 """like patch.iterhunks, but yield different events
1774 """like patch.iterhunks, but yield different events
1775
1775
1776 - ('file', [header_lines + fromfile + tofile])
1776 - ('file', [header_lines + fromfile + tofile])
1777 - ('context', [context_lines])
1777 - ('context', [context_lines])
1778 - ('hunk', [hunk_lines])
1778 - ('hunk', [hunk_lines])
1779 - ('range', (-start,len, +start,len, proc))
1779 - ('range', (-start,len, +start,len, proc))
1780 """
1780 """
1781 lines_re = re.compile(br'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)')
1781 lines_re = re.compile(br'@@ -(\d+),(\d+) \+(\d+),(\d+) @@\s*(.*)')
1782 lr = linereader(fp)
1782 lr = linereader(fp)
1783
1783
1784 def scanwhile(first, p):
1784 def scanwhile(first, p):
1785 """scan lr while predicate holds"""
1785 """scan lr while predicate holds"""
1786 lines = [first]
1786 lines = [first]
1787 for line in iter(lr.readline, ''):
1787 for line in iter(lr.readline, ''):
1788 if p(line):
1788 if p(line):
1789 lines.append(line)
1789 lines.append(line)
1790 else:
1790 else:
1791 lr.push(line)
1791 lr.push(line)
1792 break
1792 break
1793 return lines
1793 return lines
1794
1794
1795 for line in iter(lr.readline, ''):
1795 for line in iter(lr.readline, ''):
1796 if line.startswith('diff --git a/') or line.startswith('diff -r '):
1796 if line.startswith('diff --git a/') or line.startswith('diff -r '):
1797 def notheader(line):
1797 def notheader(line):
1798 s = line.split(None, 1)
1798 s = line.split(None, 1)
1799 return not s or s[0] not in ('---', 'diff')
1799 return not s or s[0] not in ('---', 'diff')
1800 header = scanwhile(line, notheader)
1800 header = scanwhile(line, notheader)
1801 fromfile = lr.readline()
1801 fromfile = lr.readline()
1802 if fromfile.startswith('---'):
1802 if fromfile.startswith('---'):
1803 tofile = lr.readline()
1803 tofile = lr.readline()
1804 header += [fromfile, tofile]
1804 header += [fromfile, tofile]
1805 else:
1805 else:
1806 lr.push(fromfile)
1806 lr.push(fromfile)
1807 yield 'file', header
1807 yield 'file', header
1808 elif line.startswith(' '):
1808 elif line.startswith(' '):
1809 cs = (' ', '\\')
1809 cs = (' ', '\\')
1810 yield 'context', scanwhile(line, lambda l: l.startswith(cs))
1810 yield 'context', scanwhile(line, lambda l: l.startswith(cs))
1811 elif line.startswith(('-', '+')):
1811 elif line.startswith(('-', '+')):
1812 cs = ('-', '+', '\\')
1812 cs = ('-', '+', '\\')
1813 yield 'hunk', scanwhile(line, lambda l: l.startswith(cs))
1813 yield 'hunk', scanwhile(line, lambda l: l.startswith(cs))
1814 else:
1814 else:
1815 m = lines_re.match(line)
1815 m = lines_re.match(line)
1816 if m:
1816 if m:
1817 yield 'range', m.groups()
1817 yield 'range', m.groups()
1818 else:
1818 else:
1819 yield 'other', line
1819 yield 'other', line
1820
1820
1821 def scangitpatch(lr, firstline):
1821 def scangitpatch(lr, firstline):
1822 """
1822 """
1823 Git patches can emit:
1823 Git patches can emit:
1824 - rename a to b
1824 - rename a to b
1825 - change b
1825 - change b
1826 - copy a to c
1826 - copy a to c
1827 - change c
1827 - change c
1828
1828
1829 We cannot apply this sequence as-is, the renamed 'a' could not be
1829 We cannot apply this sequence as-is, the renamed 'a' could not be
1830 found for it would have been renamed already. And we cannot copy
1830 found for it would have been renamed already. And we cannot copy
1831 from 'b' instead because 'b' would have been changed already. So
1831 from 'b' instead because 'b' would have been changed already. So
1832 we scan the git patch for copy and rename commands so we can
1832 we scan the git patch for copy and rename commands so we can
1833 perform the copies ahead of time.
1833 perform the copies ahead of time.
1834 """
1834 """
1835 pos = 0
1835 pos = 0
1836 try:
1836 try:
1837 pos = lr.fp.tell()
1837 pos = lr.fp.tell()
1838 fp = lr.fp
1838 fp = lr.fp
1839 except IOError:
1839 except IOError:
1840 fp = stringio(lr.fp.read())
1840 fp = stringio(lr.fp.read())
1841 gitlr = linereader(fp)
1841 gitlr = linereader(fp)
1842 gitlr.push(firstline)
1842 gitlr.push(firstline)
1843 gitpatches = readgitpatch(gitlr)
1843 gitpatches = readgitpatch(gitlr)
1844 fp.seek(pos)
1844 fp.seek(pos)
1845 return gitpatches
1845 return gitpatches
1846
1846
1847 def iterhunks(fp):
1847 def iterhunks(fp):
1848 """Read a patch and yield the following events:
1848 """Read a patch and yield the following events:
1849 - ("file", afile, bfile, firsthunk): select a new target file.
1849 - ("file", afile, bfile, firsthunk): select a new target file.
1850 - ("hunk", hunk): a new hunk is ready to be applied, follows a
1850 - ("hunk", hunk): a new hunk is ready to be applied, follows a
1851 "file" event.
1851 "file" event.
1852 - ("git", gitchanges): current diff is in git format, gitchanges
1852 - ("git", gitchanges): current diff is in git format, gitchanges
1853 maps filenames to gitpatch records. Unique event.
1853 maps filenames to gitpatch records. Unique event.
1854 """
1854 """
1855 afile = ""
1855 afile = ""
1856 bfile = ""
1856 bfile = ""
1857 state = None
1857 state = None
1858 hunknum = 0
1858 hunknum = 0
1859 emitfile = newfile = False
1859 emitfile = newfile = False
1860 gitpatches = None
1860 gitpatches = None
1861
1861
1862 # our states
1862 # our states
1863 BFILE = 1
1863 BFILE = 1
1864 context = None
1864 context = None
1865 lr = linereader(fp)
1865 lr = linereader(fp)
1866
1866
1867 for x in iter(lr.readline, ''):
1867 for x in iter(lr.readline, ''):
1868 if state == BFILE and (
1868 if state == BFILE and (
1869 (not context and x.startswith('@'))
1869 (not context and x.startswith('@'))
1870 or (context is not False and x.startswith('***************'))
1870 or (context is not False and x.startswith('***************'))
1871 or x.startswith('GIT binary patch')):
1871 or x.startswith('GIT binary patch')):
1872 gp = None
1872 gp = None
1873 if (gitpatches and
1873 if (gitpatches and
1874 gitpatches[-1].ispatching(afile, bfile)):
1874 gitpatches[-1].ispatching(afile, bfile)):
1875 gp = gitpatches.pop()
1875 gp = gitpatches.pop()
1876 if x.startswith('GIT binary patch'):
1876 if x.startswith('GIT binary patch'):
1877 h = binhunk(lr, gp.path)
1877 h = binhunk(lr, gp.path)
1878 else:
1878 else:
1879 if context is None and x.startswith('***************'):
1879 if context is None and x.startswith('***************'):
1880 context = True
1880 context = True
1881 h = hunk(x, hunknum + 1, lr, context)
1881 h = hunk(x, hunknum + 1, lr, context)
1882 hunknum += 1
1882 hunknum += 1
1883 if emitfile:
1883 if emitfile:
1884 emitfile = False
1884 emitfile = False
1885 yield 'file', (afile, bfile, h, gp and gp.copy() or None)
1885 yield 'file', (afile, bfile, h, gp and gp.copy() or None)
1886 yield 'hunk', h
1886 yield 'hunk', h
1887 elif x.startswith('diff --git a/'):
1887 elif x.startswith('diff --git a/'):
1888 m = gitre.match(x.rstrip(' \r\n'))
1888 m = gitre.match(x.rstrip(' \r\n'))
1889 if not m:
1889 if not m:
1890 continue
1890 continue
1891 if gitpatches is None:
1891 if gitpatches is None:
1892 # scan whole input for git metadata
1892 # scan whole input for git metadata
1893 gitpatches = scangitpatch(lr, x)
1893 gitpatches = scangitpatch(lr, x)
1894 yield 'git', [g.copy() for g in gitpatches
1894 yield 'git', [g.copy() for g in gitpatches
1895 if g.op in ('COPY', 'RENAME')]
1895 if g.op in ('COPY', 'RENAME')]
1896 gitpatches.reverse()
1896 gitpatches.reverse()
1897 afile = 'a/' + m.group(1)
1897 afile = 'a/' + m.group(1)
1898 bfile = 'b/' + m.group(2)
1898 bfile = 'b/' + m.group(2)
1899 while gitpatches and not gitpatches[-1].ispatching(afile, bfile):
1899 while gitpatches and not gitpatches[-1].ispatching(afile, bfile):
1900 gp = gitpatches.pop()
1900 gp = gitpatches.pop()
1901 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1901 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1902 if not gitpatches:
1902 if not gitpatches:
1903 raise PatchError(_('failed to synchronize metadata for "%s"')
1903 raise PatchError(_('failed to synchronize metadata for "%s"')
1904 % afile[2:])
1904 % afile[2:])
1905 gp = gitpatches[-1]
1905 gp = gitpatches[-1]
1906 newfile = True
1906 newfile = True
1907 elif x.startswith('---'):
1907 elif x.startswith('---'):
1908 # check for a unified diff
1908 # check for a unified diff
1909 l2 = lr.readline()
1909 l2 = lr.readline()
1910 if not l2.startswith('+++'):
1910 if not l2.startswith('+++'):
1911 lr.push(l2)
1911 lr.push(l2)
1912 continue
1912 continue
1913 newfile = True
1913 newfile = True
1914 context = False
1914 context = False
1915 afile = parsefilename(x)
1915 afile = parsefilename(x)
1916 bfile = parsefilename(l2)
1916 bfile = parsefilename(l2)
1917 elif x.startswith('***'):
1917 elif x.startswith('***'):
1918 # check for a context diff
1918 # check for a context diff
1919 l2 = lr.readline()
1919 l2 = lr.readline()
1920 if not l2.startswith('---'):
1920 if not l2.startswith('---'):
1921 lr.push(l2)
1921 lr.push(l2)
1922 continue
1922 continue
1923 l3 = lr.readline()
1923 l3 = lr.readline()
1924 lr.push(l3)
1924 lr.push(l3)
1925 if not l3.startswith("***************"):
1925 if not l3.startswith("***************"):
1926 lr.push(l2)
1926 lr.push(l2)
1927 continue
1927 continue
1928 newfile = True
1928 newfile = True
1929 context = True
1929 context = True
1930 afile = parsefilename(x)
1930 afile = parsefilename(x)
1931 bfile = parsefilename(l2)
1931 bfile = parsefilename(l2)
1932
1932
1933 if newfile:
1933 if newfile:
1934 newfile = False
1934 newfile = False
1935 emitfile = True
1935 emitfile = True
1936 state = BFILE
1936 state = BFILE
1937 hunknum = 0
1937 hunknum = 0
1938
1938
1939 while gitpatches:
1939 while gitpatches:
1940 gp = gitpatches.pop()
1940 gp = gitpatches.pop()
1941 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1941 yield 'file', ('a/' + gp.path, 'b/' + gp.path, None, gp.copy())
1942
1942
1943 def applybindelta(binchunk, data):
1943 def applybindelta(binchunk, data):
1944 """Apply a binary delta hunk
1944 """Apply a binary delta hunk
1945 The algorithm used is the algorithm from git's patch-delta.c
1945 The algorithm used is the algorithm from git's patch-delta.c
1946 """
1946 """
1947 def deltahead(binchunk):
1947 def deltahead(binchunk):
1948 i = 0
1948 i = 0
1949 for c in binchunk:
1949 for c in pycompat.bytestr(binchunk):
1950 i += 1
1950 i += 1
1951 if not (ord(c) & 0x80):
1951 if not (ord(c) & 0x80):
1952 return i
1952 return i
1953 return i
1953 return i
1954 out = ""
1954 out = ""
1955 s = deltahead(binchunk)
1955 s = deltahead(binchunk)
1956 binchunk = binchunk[s:]
1956 binchunk = binchunk[s:]
1957 s = deltahead(binchunk)
1957 s = deltahead(binchunk)
1958 binchunk = binchunk[s:]
1958 binchunk = binchunk[s:]
1959 i = 0
1959 i = 0
1960 while i < len(binchunk):
1960 while i < len(binchunk):
1961 cmd = ord(binchunk[i:i + 1])
1961 cmd = ord(binchunk[i:i + 1])
1962 i += 1
1962 i += 1
1963 if (cmd & 0x80):
1963 if (cmd & 0x80):
1964 offset = 0
1964 offset = 0
1965 size = 0
1965 size = 0
1966 if (cmd & 0x01):
1966 if (cmd & 0x01):
1967 offset = ord(binchunk[i:i + 1])
1967 offset = ord(binchunk[i:i + 1])
1968 i += 1
1968 i += 1
1969 if (cmd & 0x02):
1969 if (cmd & 0x02):
1970 offset |= ord(binchunk[i:i + 1]) << 8
1970 offset |= ord(binchunk[i:i + 1]) << 8
1971 i += 1
1971 i += 1
1972 if (cmd & 0x04):
1972 if (cmd & 0x04):
1973 offset |= ord(binchunk[i:i + 1]) << 16
1973 offset |= ord(binchunk[i:i + 1]) << 16
1974 i += 1
1974 i += 1
1975 if (cmd & 0x08):
1975 if (cmd & 0x08):
1976 offset |= ord(binchunk[i:i + 1]) << 24
1976 offset |= ord(binchunk[i:i + 1]) << 24
1977 i += 1
1977 i += 1
1978 if (cmd & 0x10):
1978 if (cmd & 0x10):
1979 size = ord(binchunk[i:i + 1])
1979 size = ord(binchunk[i:i + 1])
1980 i += 1
1980 i += 1
1981 if (cmd & 0x20):
1981 if (cmd & 0x20):
1982 size |= ord(binchunk[i:i + 1]) << 8
1982 size |= ord(binchunk[i:i + 1]) << 8
1983 i += 1
1983 i += 1
1984 if (cmd & 0x40):
1984 if (cmd & 0x40):
1985 size |= ord(binchunk[i:i + 1]) << 16
1985 size |= ord(binchunk[i:i + 1]) << 16
1986 i += 1
1986 i += 1
1987 if size == 0:
1987 if size == 0:
1988 size = 0x10000
1988 size = 0x10000
1989 offset_end = offset + size
1989 offset_end = offset + size
1990 out += data[offset:offset_end]
1990 out += data[offset:offset_end]
1991 elif cmd != 0:
1991 elif cmd != 0:
1992 offset_end = i + cmd
1992 offset_end = i + cmd
1993 out += binchunk[i:offset_end]
1993 out += binchunk[i:offset_end]
1994 i += cmd
1994 i += cmd
1995 else:
1995 else:
1996 raise PatchError(_('unexpected delta opcode 0'))
1996 raise PatchError(_('unexpected delta opcode 0'))
1997 return out
1997 return out
1998
1998
1999 def applydiff(ui, fp, backend, store, strip=1, prefix='', eolmode='strict'):
1999 def applydiff(ui, fp, backend, store, strip=1, prefix='', eolmode='strict'):
2000 """Reads a patch from fp and tries to apply it.
2000 """Reads a patch from fp and tries to apply it.
2001
2001
2002 Returns 0 for a clean patch, -1 if any rejects were found and 1 if
2002 Returns 0 for a clean patch, -1 if any rejects were found and 1 if
2003 there was any fuzz.
2003 there was any fuzz.
2004
2004
2005 If 'eolmode' is 'strict', the patch content and patched file are
2005 If 'eolmode' is 'strict', the patch content and patched file are
2006 read in binary mode. Otherwise, line endings are ignored when
2006 read in binary mode. Otherwise, line endings are ignored when
2007 patching then normalized according to 'eolmode'.
2007 patching then normalized according to 'eolmode'.
2008 """
2008 """
2009 return _applydiff(ui, fp, patchfile, backend, store, strip=strip,
2009 return _applydiff(ui, fp, patchfile, backend, store, strip=strip,
2010 prefix=prefix, eolmode=eolmode)
2010 prefix=prefix, eolmode=eolmode)
2011
2011
2012 def _canonprefix(repo, prefix):
2012 def _canonprefix(repo, prefix):
2013 if prefix:
2013 if prefix:
2014 prefix = pathutil.canonpath(repo.root, repo.getcwd(), prefix)
2014 prefix = pathutil.canonpath(repo.root, repo.getcwd(), prefix)
2015 if prefix != '':
2015 if prefix != '':
2016 prefix += '/'
2016 prefix += '/'
2017 return prefix
2017 return prefix
2018
2018
2019 def _applydiff(ui, fp, patcher, backend, store, strip=1, prefix='',
2019 def _applydiff(ui, fp, patcher, backend, store, strip=1, prefix='',
2020 eolmode='strict'):
2020 eolmode='strict'):
2021 prefix = _canonprefix(backend.repo, prefix)
2021 prefix = _canonprefix(backend.repo, prefix)
2022 def pstrip(p):
2022 def pstrip(p):
2023 return pathtransform(p, strip - 1, prefix)[1]
2023 return pathtransform(p, strip - 1, prefix)[1]
2024
2024
2025 rejects = 0
2025 rejects = 0
2026 err = 0
2026 err = 0
2027 current_file = None
2027 current_file = None
2028
2028
2029 for state, values in iterhunks(fp):
2029 for state, values in iterhunks(fp):
2030 if state == 'hunk':
2030 if state == 'hunk':
2031 if not current_file:
2031 if not current_file:
2032 continue
2032 continue
2033 ret = current_file.apply(values)
2033 ret = current_file.apply(values)
2034 if ret > 0:
2034 if ret > 0:
2035 err = 1
2035 err = 1
2036 elif state == 'file':
2036 elif state == 'file':
2037 if current_file:
2037 if current_file:
2038 rejects += current_file.close()
2038 rejects += current_file.close()
2039 current_file = None
2039 current_file = None
2040 afile, bfile, first_hunk, gp = values
2040 afile, bfile, first_hunk, gp = values
2041 if gp:
2041 if gp:
2042 gp.path = pstrip(gp.path)
2042 gp.path = pstrip(gp.path)
2043 if gp.oldpath:
2043 if gp.oldpath:
2044 gp.oldpath = pstrip(gp.oldpath)
2044 gp.oldpath = pstrip(gp.oldpath)
2045 else:
2045 else:
2046 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip,
2046 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip,
2047 prefix)
2047 prefix)
2048 if gp.op == 'RENAME':
2048 if gp.op == 'RENAME':
2049 backend.unlink(gp.oldpath)
2049 backend.unlink(gp.oldpath)
2050 if not first_hunk:
2050 if not first_hunk:
2051 if gp.op == 'DELETE':
2051 if gp.op == 'DELETE':
2052 backend.unlink(gp.path)
2052 backend.unlink(gp.path)
2053 continue
2053 continue
2054 data, mode = None, None
2054 data, mode = None, None
2055 if gp.op in ('RENAME', 'COPY'):
2055 if gp.op in ('RENAME', 'COPY'):
2056 data, mode = store.getfile(gp.oldpath)[:2]
2056 data, mode = store.getfile(gp.oldpath)[:2]
2057 if data is None:
2057 if data is None:
2058 # This means that the old path does not exist
2058 # This means that the old path does not exist
2059 raise PatchError(_("source file '%s' does not exist")
2059 raise PatchError(_("source file '%s' does not exist")
2060 % gp.oldpath)
2060 % gp.oldpath)
2061 if gp.mode:
2061 if gp.mode:
2062 mode = gp.mode
2062 mode = gp.mode
2063 if gp.op == 'ADD':
2063 if gp.op == 'ADD':
2064 # Added files without content have no hunk and
2064 # Added files without content have no hunk and
2065 # must be created
2065 # must be created
2066 data = ''
2066 data = ''
2067 if data or mode:
2067 if data or mode:
2068 if (gp.op in ('ADD', 'RENAME', 'COPY')
2068 if (gp.op in ('ADD', 'RENAME', 'COPY')
2069 and backend.exists(gp.path)):
2069 and backend.exists(gp.path)):
2070 raise PatchError(_("cannot create %s: destination "
2070 raise PatchError(_("cannot create %s: destination "
2071 "already exists") % gp.path)
2071 "already exists") % gp.path)
2072 backend.setfile(gp.path, data, mode, gp.oldpath)
2072 backend.setfile(gp.path, data, mode, gp.oldpath)
2073 continue
2073 continue
2074 try:
2074 try:
2075 current_file = patcher(ui, gp, backend, store,
2075 current_file = patcher(ui, gp, backend, store,
2076 eolmode=eolmode)
2076 eolmode=eolmode)
2077 except PatchError as inst:
2077 except PatchError as inst:
2078 ui.warn(str(inst) + '\n')
2078 ui.warn(str(inst) + '\n')
2079 current_file = None
2079 current_file = None
2080 rejects += 1
2080 rejects += 1
2081 continue
2081 continue
2082 elif state == 'git':
2082 elif state == 'git':
2083 for gp in values:
2083 for gp in values:
2084 path = pstrip(gp.oldpath)
2084 path = pstrip(gp.oldpath)
2085 data, mode = backend.getfile(path)
2085 data, mode = backend.getfile(path)
2086 if data is None:
2086 if data is None:
2087 # The error ignored here will trigger a getfile()
2087 # The error ignored here will trigger a getfile()
2088 # error in a place more appropriate for error
2088 # error in a place more appropriate for error
2089 # handling, and will not interrupt the patching
2089 # handling, and will not interrupt the patching
2090 # process.
2090 # process.
2091 pass
2091 pass
2092 else:
2092 else:
2093 store.setfile(path, data, mode)
2093 store.setfile(path, data, mode)
2094 else:
2094 else:
2095 raise error.Abort(_('unsupported parser state: %s') % state)
2095 raise error.Abort(_('unsupported parser state: %s') % state)
2096
2096
2097 if current_file:
2097 if current_file:
2098 rejects += current_file.close()
2098 rejects += current_file.close()
2099
2099
2100 if rejects:
2100 if rejects:
2101 return -1
2101 return -1
2102 return err
2102 return err
2103
2103
2104 def _externalpatch(ui, repo, patcher, patchname, strip, files,
2104 def _externalpatch(ui, repo, patcher, patchname, strip, files,
2105 similarity):
2105 similarity):
2106 """use <patcher> to apply <patchname> to the working directory.
2106 """use <patcher> to apply <patchname> to the working directory.
2107 returns whether patch was applied with fuzz factor."""
2107 returns whether patch was applied with fuzz factor."""
2108
2108
2109 fuzz = False
2109 fuzz = False
2110 args = []
2110 args = []
2111 cwd = repo.root
2111 cwd = repo.root
2112 if cwd:
2112 if cwd:
2113 args.append('-d %s' % procutil.shellquote(cwd))
2113 args.append('-d %s' % procutil.shellquote(cwd))
2114 cmd = ('%s %s -p%d < %s'
2114 cmd = ('%s %s -p%d < %s'
2115 % (patcher, ' '.join(args), strip, procutil.shellquote(patchname)))
2115 % (patcher, ' '.join(args), strip, procutil.shellquote(patchname)))
2116 fp = procutil.popen(cmd, 'rb')
2116 fp = procutil.popen(cmd, 'rb')
2117 try:
2117 try:
2118 for line in util.iterfile(fp):
2118 for line in util.iterfile(fp):
2119 line = line.rstrip()
2119 line = line.rstrip()
2120 ui.note(line + '\n')
2120 ui.note(line + '\n')
2121 if line.startswith('patching file '):
2121 if line.startswith('patching file '):
2122 pf = util.parsepatchoutput(line)
2122 pf = util.parsepatchoutput(line)
2123 printed_file = False
2123 printed_file = False
2124 files.add(pf)
2124 files.add(pf)
2125 elif line.find('with fuzz') >= 0:
2125 elif line.find('with fuzz') >= 0:
2126 fuzz = True
2126 fuzz = True
2127 if not printed_file:
2127 if not printed_file:
2128 ui.warn(pf + '\n')
2128 ui.warn(pf + '\n')
2129 printed_file = True
2129 printed_file = True
2130 ui.warn(line + '\n')
2130 ui.warn(line + '\n')
2131 elif line.find('saving rejects to file') >= 0:
2131 elif line.find('saving rejects to file') >= 0:
2132 ui.warn(line + '\n')
2132 ui.warn(line + '\n')
2133 elif line.find('FAILED') >= 0:
2133 elif line.find('FAILED') >= 0:
2134 if not printed_file:
2134 if not printed_file:
2135 ui.warn(pf + '\n')
2135 ui.warn(pf + '\n')
2136 printed_file = True
2136 printed_file = True
2137 ui.warn(line + '\n')
2137 ui.warn(line + '\n')
2138 finally:
2138 finally:
2139 if files:
2139 if files:
2140 scmutil.marktouched(repo, files, similarity)
2140 scmutil.marktouched(repo, files, similarity)
2141 code = fp.close()
2141 code = fp.close()
2142 if code:
2142 if code:
2143 raise PatchError(_("patch command failed: %s") %
2143 raise PatchError(_("patch command failed: %s") %
2144 procutil.explainexit(code))
2144 procutil.explainexit(code))
2145 return fuzz
2145 return fuzz
2146
2146
2147 def patchbackend(ui, backend, patchobj, strip, prefix, files=None,
2147 def patchbackend(ui, backend, patchobj, strip, prefix, files=None,
2148 eolmode='strict'):
2148 eolmode='strict'):
2149 if files is None:
2149 if files is None:
2150 files = set()
2150 files = set()
2151 if eolmode is None:
2151 if eolmode is None:
2152 eolmode = ui.config('patch', 'eol')
2152 eolmode = ui.config('patch', 'eol')
2153 if eolmode.lower() not in eolmodes:
2153 if eolmode.lower() not in eolmodes:
2154 raise error.Abort(_('unsupported line endings type: %s') % eolmode)
2154 raise error.Abort(_('unsupported line endings type: %s') % eolmode)
2155 eolmode = eolmode.lower()
2155 eolmode = eolmode.lower()
2156
2156
2157 store = filestore()
2157 store = filestore()
2158 try:
2158 try:
2159 fp = open(patchobj, 'rb')
2159 fp = open(patchobj, 'rb')
2160 except TypeError:
2160 except TypeError:
2161 fp = patchobj
2161 fp = patchobj
2162 try:
2162 try:
2163 ret = applydiff(ui, fp, backend, store, strip=strip, prefix=prefix,
2163 ret = applydiff(ui, fp, backend, store, strip=strip, prefix=prefix,
2164 eolmode=eolmode)
2164 eolmode=eolmode)
2165 finally:
2165 finally:
2166 if fp != patchobj:
2166 if fp != patchobj:
2167 fp.close()
2167 fp.close()
2168 files.update(backend.close())
2168 files.update(backend.close())
2169 store.close()
2169 store.close()
2170 if ret < 0:
2170 if ret < 0:
2171 raise PatchError(_('patch failed to apply'))
2171 raise PatchError(_('patch failed to apply'))
2172 return ret > 0
2172 return ret > 0
2173
2173
2174 def internalpatch(ui, repo, patchobj, strip, prefix='', files=None,
2174 def internalpatch(ui, repo, patchobj, strip, prefix='', files=None,
2175 eolmode='strict', similarity=0):
2175 eolmode='strict', similarity=0):
2176 """use builtin patch to apply <patchobj> to the working directory.
2176 """use builtin patch to apply <patchobj> to the working directory.
2177 returns whether patch was applied with fuzz factor."""
2177 returns whether patch was applied with fuzz factor."""
2178 backend = workingbackend(ui, repo, similarity)
2178 backend = workingbackend(ui, repo, similarity)
2179 return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode)
2179 return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode)
2180
2180
2181 def patchrepo(ui, repo, ctx, store, patchobj, strip, prefix, files=None,
2181 def patchrepo(ui, repo, ctx, store, patchobj, strip, prefix, files=None,
2182 eolmode='strict'):
2182 eolmode='strict'):
2183 backend = repobackend(ui, repo, ctx, store)
2183 backend = repobackend(ui, repo, ctx, store)
2184 return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode)
2184 return patchbackend(ui, backend, patchobj, strip, prefix, files, eolmode)
2185
2185
2186 def patch(ui, repo, patchname, strip=1, prefix='', files=None, eolmode='strict',
2186 def patch(ui, repo, patchname, strip=1, prefix='', files=None, eolmode='strict',
2187 similarity=0):
2187 similarity=0):
2188 """Apply <patchname> to the working directory.
2188 """Apply <patchname> to the working directory.
2189
2189
2190 'eolmode' specifies how end of lines should be handled. It can be:
2190 'eolmode' specifies how end of lines should be handled. It can be:
2191 - 'strict': inputs are read in binary mode, EOLs are preserved
2191 - 'strict': inputs are read in binary mode, EOLs are preserved
2192 - 'crlf': EOLs are ignored when patching and reset to CRLF
2192 - 'crlf': EOLs are ignored when patching and reset to CRLF
2193 - 'lf': EOLs are ignored when patching and reset to LF
2193 - 'lf': EOLs are ignored when patching and reset to LF
2194 - None: get it from user settings, default to 'strict'
2194 - None: get it from user settings, default to 'strict'
2195 'eolmode' is ignored when using an external patcher program.
2195 'eolmode' is ignored when using an external patcher program.
2196
2196
2197 Returns whether patch was applied with fuzz factor.
2197 Returns whether patch was applied with fuzz factor.
2198 """
2198 """
2199 patcher = ui.config('ui', 'patch')
2199 patcher = ui.config('ui', 'patch')
2200 if files is None:
2200 if files is None:
2201 files = set()
2201 files = set()
2202 if patcher:
2202 if patcher:
2203 return _externalpatch(ui, repo, patcher, patchname, strip,
2203 return _externalpatch(ui, repo, patcher, patchname, strip,
2204 files, similarity)
2204 files, similarity)
2205 return internalpatch(ui, repo, patchname, strip, prefix, files, eolmode,
2205 return internalpatch(ui, repo, patchname, strip, prefix, files, eolmode,
2206 similarity)
2206 similarity)
2207
2207
2208 def changedfiles(ui, repo, patchpath, strip=1, prefix=''):
2208 def changedfiles(ui, repo, patchpath, strip=1, prefix=''):
2209 backend = fsbackend(ui, repo.root)
2209 backend = fsbackend(ui, repo.root)
2210 prefix = _canonprefix(repo, prefix)
2210 prefix = _canonprefix(repo, prefix)
2211 with open(patchpath, 'rb') as fp:
2211 with open(patchpath, 'rb') as fp:
2212 changed = set()
2212 changed = set()
2213 for state, values in iterhunks(fp):
2213 for state, values in iterhunks(fp):
2214 if state == 'file':
2214 if state == 'file':
2215 afile, bfile, first_hunk, gp = values
2215 afile, bfile, first_hunk, gp = values
2216 if gp:
2216 if gp:
2217 gp.path = pathtransform(gp.path, strip - 1, prefix)[1]
2217 gp.path = pathtransform(gp.path, strip - 1, prefix)[1]
2218 if gp.oldpath:
2218 if gp.oldpath:
2219 gp.oldpath = pathtransform(gp.oldpath, strip - 1,
2219 gp.oldpath = pathtransform(gp.oldpath, strip - 1,
2220 prefix)[1]
2220 prefix)[1]
2221 else:
2221 else:
2222 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip,
2222 gp = makepatchmeta(backend, afile, bfile, first_hunk, strip,
2223 prefix)
2223 prefix)
2224 changed.add(gp.path)
2224 changed.add(gp.path)
2225 if gp.op == 'RENAME':
2225 if gp.op == 'RENAME':
2226 changed.add(gp.oldpath)
2226 changed.add(gp.oldpath)
2227 elif state not in ('hunk', 'git'):
2227 elif state not in ('hunk', 'git'):
2228 raise error.Abort(_('unsupported parser state: %s') % state)
2228 raise error.Abort(_('unsupported parser state: %s') % state)
2229 return changed
2229 return changed
2230
2230
2231 class GitDiffRequired(Exception):
2231 class GitDiffRequired(Exception):
2232 pass
2232 pass
2233
2233
2234 def diffallopts(ui, opts=None, untrusted=False, section='diff'):
2234 def diffallopts(ui, opts=None, untrusted=False, section='diff'):
2235 '''return diffopts with all features supported and parsed'''
2235 '''return diffopts with all features supported and parsed'''
2236 return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section,
2236 return difffeatureopts(ui, opts=opts, untrusted=untrusted, section=section,
2237 git=True, whitespace=True, formatchanging=True)
2237 git=True, whitespace=True, formatchanging=True)
2238
2238
2239 diffopts = diffallopts
2239 diffopts = diffallopts
2240
2240
2241 def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False,
2241 def difffeatureopts(ui, opts=None, untrusted=False, section='diff', git=False,
2242 whitespace=False, formatchanging=False):
2242 whitespace=False, formatchanging=False):
2243 '''return diffopts with only opted-in features parsed
2243 '''return diffopts with only opted-in features parsed
2244
2244
2245 Features:
2245 Features:
2246 - git: git-style diffs
2246 - git: git-style diffs
2247 - whitespace: whitespace options like ignoreblanklines and ignorews
2247 - whitespace: whitespace options like ignoreblanklines and ignorews
2248 - formatchanging: options that will likely break or cause correctness issues
2248 - formatchanging: options that will likely break or cause correctness issues
2249 with most diff parsers
2249 with most diff parsers
2250 '''
2250 '''
2251 def get(key, name=None, getter=ui.configbool, forceplain=None):
2251 def get(key, name=None, getter=ui.configbool, forceplain=None):
2252 if opts:
2252 if opts:
2253 v = opts.get(key)
2253 v = opts.get(key)
2254 # diffopts flags are either None-default (which is passed
2254 # diffopts flags are either None-default (which is passed
2255 # through unchanged, so we can identify unset values), or
2255 # through unchanged, so we can identify unset values), or
2256 # some other falsey default (eg --unified, which defaults
2256 # some other falsey default (eg --unified, which defaults
2257 # to an empty string). We only want to override the config
2257 # to an empty string). We only want to override the config
2258 # entries from hgrc with command line values if they
2258 # entries from hgrc with command line values if they
2259 # appear to have been set, which is any truthy value,
2259 # appear to have been set, which is any truthy value,
2260 # True, or False.
2260 # True, or False.
2261 if v or isinstance(v, bool):
2261 if v or isinstance(v, bool):
2262 return v
2262 return v
2263 if forceplain is not None and ui.plain():
2263 if forceplain is not None and ui.plain():
2264 return forceplain
2264 return forceplain
2265 return getter(section, name or key, untrusted=untrusted)
2265 return getter(section, name or key, untrusted=untrusted)
2266
2266
2267 # core options, expected to be understood by every diff parser
2267 # core options, expected to be understood by every diff parser
2268 buildopts = {
2268 buildopts = {
2269 'nodates': get('nodates'),
2269 'nodates': get('nodates'),
2270 'showfunc': get('show_function', 'showfunc'),
2270 'showfunc': get('show_function', 'showfunc'),
2271 'context': get('unified', getter=ui.config),
2271 'context': get('unified', getter=ui.config),
2272 }
2272 }
2273 buildopts['worddiff'] = ui.configbool('experimental', 'worddiff')
2273 buildopts['worddiff'] = ui.configbool('experimental', 'worddiff')
2274 buildopts['xdiff'] = ui.configbool('experimental', 'xdiff')
2274 buildopts['xdiff'] = ui.configbool('experimental', 'xdiff')
2275
2275
2276 if git:
2276 if git:
2277 buildopts['git'] = get('git')
2277 buildopts['git'] = get('git')
2278
2278
2279 # since this is in the experimental section, we need to call
2279 # since this is in the experimental section, we need to call
2280 # ui.configbool directory
2280 # ui.configbool directory
2281 buildopts['showsimilarity'] = ui.configbool('experimental',
2281 buildopts['showsimilarity'] = ui.configbool('experimental',
2282 'extendedheader.similarity')
2282 'extendedheader.similarity')
2283
2283
2284 # need to inspect the ui object instead of using get() since we want to
2284 # need to inspect the ui object instead of using get() since we want to
2285 # test for an int
2285 # test for an int
2286 hconf = ui.config('experimental', 'extendedheader.index')
2286 hconf = ui.config('experimental', 'extendedheader.index')
2287 if hconf is not None:
2287 if hconf is not None:
2288 hlen = None
2288 hlen = None
2289 try:
2289 try:
2290 # the hash config could be an integer (for length of hash) or a
2290 # the hash config could be an integer (for length of hash) or a
2291 # word (e.g. short, full, none)
2291 # word (e.g. short, full, none)
2292 hlen = int(hconf)
2292 hlen = int(hconf)
2293 if hlen < 0 or hlen > 40:
2293 if hlen < 0 or hlen > 40:
2294 msg = _("invalid length for extendedheader.index: '%d'\n")
2294 msg = _("invalid length for extendedheader.index: '%d'\n")
2295 ui.warn(msg % hlen)
2295 ui.warn(msg % hlen)
2296 except ValueError:
2296 except ValueError:
2297 # default value
2297 # default value
2298 if hconf == 'short' or hconf == '':
2298 if hconf == 'short' or hconf == '':
2299 hlen = 12
2299 hlen = 12
2300 elif hconf == 'full':
2300 elif hconf == 'full':
2301 hlen = 40
2301 hlen = 40
2302 elif hconf != 'none':
2302 elif hconf != 'none':
2303 msg = _("invalid value for extendedheader.index: '%s'\n")
2303 msg = _("invalid value for extendedheader.index: '%s'\n")
2304 ui.warn(msg % hconf)
2304 ui.warn(msg % hconf)
2305 finally:
2305 finally:
2306 buildopts['index'] = hlen
2306 buildopts['index'] = hlen
2307
2307
2308 if whitespace:
2308 if whitespace:
2309 buildopts['ignorews'] = get('ignore_all_space', 'ignorews')
2309 buildopts['ignorews'] = get('ignore_all_space', 'ignorews')
2310 buildopts['ignorewsamount'] = get('ignore_space_change',
2310 buildopts['ignorewsamount'] = get('ignore_space_change',
2311 'ignorewsamount')
2311 'ignorewsamount')
2312 buildopts['ignoreblanklines'] = get('ignore_blank_lines',
2312 buildopts['ignoreblanklines'] = get('ignore_blank_lines',
2313 'ignoreblanklines')
2313 'ignoreblanklines')
2314 buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol')
2314 buildopts['ignorewseol'] = get('ignore_space_at_eol', 'ignorewseol')
2315 if formatchanging:
2315 if formatchanging:
2316 buildopts['text'] = opts and opts.get('text')
2316 buildopts['text'] = opts and opts.get('text')
2317 binary = None if opts is None else opts.get('binary')
2317 binary = None if opts is None else opts.get('binary')
2318 buildopts['nobinary'] = (not binary if binary is not None
2318 buildopts['nobinary'] = (not binary if binary is not None
2319 else get('nobinary', forceplain=False))
2319 else get('nobinary', forceplain=False))
2320 buildopts['noprefix'] = get('noprefix', forceplain=False)
2320 buildopts['noprefix'] = get('noprefix', forceplain=False)
2321
2321
2322 return mdiff.diffopts(**pycompat.strkwargs(buildopts))
2322 return mdiff.diffopts(**pycompat.strkwargs(buildopts))
2323
2323
2324 def diff(repo, node1=None, node2=None, match=None, changes=None,
2324 def diff(repo, node1=None, node2=None, match=None, changes=None,
2325 opts=None, losedatafn=None, prefix='', relroot='', copy=None,
2325 opts=None, losedatafn=None, prefix='', relroot='', copy=None,
2326 hunksfilterfn=None):
2326 hunksfilterfn=None):
2327 '''yields diff of changes to files between two nodes, or node and
2327 '''yields diff of changes to files between two nodes, or node and
2328 working directory.
2328 working directory.
2329
2329
2330 if node1 is None, use first dirstate parent instead.
2330 if node1 is None, use first dirstate parent instead.
2331 if node2 is None, compare node1 with working directory.
2331 if node2 is None, compare node1 with working directory.
2332
2332
2333 losedatafn(**kwarg) is a callable run when opts.upgrade=True and
2333 losedatafn(**kwarg) is a callable run when opts.upgrade=True and
2334 every time some change cannot be represented with the current
2334 every time some change cannot be represented with the current
2335 patch format. Return False to upgrade to git patch format, True to
2335 patch format. Return False to upgrade to git patch format, True to
2336 accept the loss or raise an exception to abort the diff. It is
2336 accept the loss or raise an exception to abort the diff. It is
2337 called with the name of current file being diffed as 'fn'. If set
2337 called with the name of current file being diffed as 'fn'. If set
2338 to None, patches will always be upgraded to git format when
2338 to None, patches will always be upgraded to git format when
2339 necessary.
2339 necessary.
2340
2340
2341 prefix is a filename prefix that is prepended to all filenames on
2341 prefix is a filename prefix that is prepended to all filenames on
2342 display (used for subrepos).
2342 display (used for subrepos).
2343
2343
2344 relroot, if not empty, must be normalized with a trailing /. Any match
2344 relroot, if not empty, must be normalized with a trailing /. Any match
2345 patterns that fall outside it will be ignored.
2345 patterns that fall outside it will be ignored.
2346
2346
2347 copy, if not empty, should contain mappings {dst@y: src@x} of copy
2347 copy, if not empty, should contain mappings {dst@y: src@x} of copy
2348 information.
2348 information.
2349
2349
2350 hunksfilterfn, if not None, should be a function taking a filectx and
2350 hunksfilterfn, if not None, should be a function taking a filectx and
2351 hunks generator that may yield filtered hunks.
2351 hunks generator that may yield filtered hunks.
2352 '''
2352 '''
2353 for fctx1, fctx2, hdr, hunks in diffhunks(
2353 for fctx1, fctx2, hdr, hunks in diffhunks(
2354 repo, node1=node1, node2=node2,
2354 repo, node1=node1, node2=node2,
2355 match=match, changes=changes, opts=opts,
2355 match=match, changes=changes, opts=opts,
2356 losedatafn=losedatafn, prefix=prefix, relroot=relroot, copy=copy,
2356 losedatafn=losedatafn, prefix=prefix, relroot=relroot, copy=copy,
2357 ):
2357 ):
2358 if hunksfilterfn is not None:
2358 if hunksfilterfn is not None:
2359 # If the file has been removed, fctx2 is None; but this should
2359 # If the file has been removed, fctx2 is None; but this should
2360 # not occur here since we catch removed files early in
2360 # not occur here since we catch removed files early in
2361 # logcmdutil.getlinerangerevs() for 'hg log -L'.
2361 # logcmdutil.getlinerangerevs() for 'hg log -L'.
2362 assert fctx2 is not None, \
2362 assert fctx2 is not None, \
2363 'fctx2 unexpectly None in diff hunks filtering'
2363 'fctx2 unexpectly None in diff hunks filtering'
2364 hunks = hunksfilterfn(fctx2, hunks)
2364 hunks = hunksfilterfn(fctx2, hunks)
2365 text = ''.join(sum((list(hlines) for hrange, hlines in hunks), []))
2365 text = ''.join(sum((list(hlines) for hrange, hlines in hunks), []))
2366 if hdr and (text or len(hdr) > 1):
2366 if hdr and (text or len(hdr) > 1):
2367 yield '\n'.join(hdr) + '\n'
2367 yield '\n'.join(hdr) + '\n'
2368 if text:
2368 if text:
2369 yield text
2369 yield text
2370
2370
2371 def diffhunks(repo, node1=None, node2=None, match=None, changes=None,
2371 def diffhunks(repo, node1=None, node2=None, match=None, changes=None,
2372 opts=None, losedatafn=None, prefix='', relroot='', copy=None):
2372 opts=None, losedatafn=None, prefix='', relroot='', copy=None):
2373 """Yield diff of changes to files in the form of (`header`, `hunks`) tuples
2373 """Yield diff of changes to files in the form of (`header`, `hunks`) tuples
2374 where `header` is a list of diff headers and `hunks` is an iterable of
2374 where `header` is a list of diff headers and `hunks` is an iterable of
2375 (`hunkrange`, `hunklines`) tuples.
2375 (`hunkrange`, `hunklines`) tuples.
2376
2376
2377 See diff() for the meaning of parameters.
2377 See diff() for the meaning of parameters.
2378 """
2378 """
2379
2379
2380 if opts is None:
2380 if opts is None:
2381 opts = mdiff.defaultopts
2381 opts = mdiff.defaultopts
2382
2382
2383 if not node1 and not node2:
2383 if not node1 and not node2:
2384 node1 = repo.dirstate.p1()
2384 node1 = repo.dirstate.p1()
2385
2385
2386 def lrugetfilectx():
2386 def lrugetfilectx():
2387 cache = {}
2387 cache = {}
2388 order = collections.deque()
2388 order = collections.deque()
2389 def getfilectx(f, ctx):
2389 def getfilectx(f, ctx):
2390 fctx = ctx.filectx(f, filelog=cache.get(f))
2390 fctx = ctx.filectx(f, filelog=cache.get(f))
2391 if f not in cache:
2391 if f not in cache:
2392 if len(cache) > 20:
2392 if len(cache) > 20:
2393 del cache[order.popleft()]
2393 del cache[order.popleft()]
2394 cache[f] = fctx.filelog()
2394 cache[f] = fctx.filelog()
2395 else:
2395 else:
2396 order.remove(f)
2396 order.remove(f)
2397 order.append(f)
2397 order.append(f)
2398 return fctx
2398 return fctx
2399 return getfilectx
2399 return getfilectx
2400 getfilectx = lrugetfilectx()
2400 getfilectx = lrugetfilectx()
2401
2401
2402 ctx1 = repo[node1]
2402 ctx1 = repo[node1]
2403 ctx2 = repo[node2]
2403 ctx2 = repo[node2]
2404
2404
2405 relfiltered = False
2405 relfiltered = False
2406 if relroot != '' and match.always():
2406 if relroot != '' and match.always():
2407 # as a special case, create a new matcher with just the relroot
2407 # as a special case, create a new matcher with just the relroot
2408 pats = [relroot]
2408 pats = [relroot]
2409 match = scmutil.match(ctx2, pats, default='path')
2409 match = scmutil.match(ctx2, pats, default='path')
2410 relfiltered = True
2410 relfiltered = True
2411
2411
2412 if not changes:
2412 if not changes:
2413 changes = repo.status(ctx1, ctx2, match=match)
2413 changes = repo.status(ctx1, ctx2, match=match)
2414 modified, added, removed = changes[:3]
2414 modified, added, removed = changes[:3]
2415
2415
2416 if not modified and not added and not removed:
2416 if not modified and not added and not removed:
2417 return []
2417 return []
2418
2418
2419 if repo.ui.debugflag:
2419 if repo.ui.debugflag:
2420 hexfunc = hex
2420 hexfunc = hex
2421 else:
2421 else:
2422 hexfunc = short
2422 hexfunc = short
2423 revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node]
2423 revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node]
2424
2424
2425 if copy is None:
2425 if copy is None:
2426 copy = {}
2426 copy = {}
2427 if opts.git or opts.upgrade:
2427 if opts.git or opts.upgrade:
2428 copy = copies.pathcopies(ctx1, ctx2, match=match)
2428 copy = copies.pathcopies(ctx1, ctx2, match=match)
2429
2429
2430 if relroot is not None:
2430 if relroot is not None:
2431 if not relfiltered:
2431 if not relfiltered:
2432 # XXX this would ideally be done in the matcher, but that is
2432 # XXX this would ideally be done in the matcher, but that is
2433 # generally meant to 'or' patterns, not 'and' them. In this case we
2433 # generally meant to 'or' patterns, not 'and' them. In this case we
2434 # need to 'and' all the patterns from the matcher with relroot.
2434 # need to 'and' all the patterns from the matcher with relroot.
2435 def filterrel(l):
2435 def filterrel(l):
2436 return [f for f in l if f.startswith(relroot)]
2436 return [f for f in l if f.startswith(relroot)]
2437 modified = filterrel(modified)
2437 modified = filterrel(modified)
2438 added = filterrel(added)
2438 added = filterrel(added)
2439 removed = filterrel(removed)
2439 removed = filterrel(removed)
2440 relfiltered = True
2440 relfiltered = True
2441 # filter out copies where either side isn't inside the relative root
2441 # filter out copies where either side isn't inside the relative root
2442 copy = dict(((dst, src) for (dst, src) in copy.iteritems()
2442 copy = dict(((dst, src) for (dst, src) in copy.iteritems()
2443 if dst.startswith(relroot)
2443 if dst.startswith(relroot)
2444 and src.startswith(relroot)))
2444 and src.startswith(relroot)))
2445
2445
2446 modifiedset = set(modified)
2446 modifiedset = set(modified)
2447 addedset = set(added)
2447 addedset = set(added)
2448 removedset = set(removed)
2448 removedset = set(removed)
2449 for f in modified:
2449 for f in modified:
2450 if f not in ctx1:
2450 if f not in ctx1:
2451 # Fix up added, since merged-in additions appear as
2451 # Fix up added, since merged-in additions appear as
2452 # modifications during merges
2452 # modifications during merges
2453 modifiedset.remove(f)
2453 modifiedset.remove(f)
2454 addedset.add(f)
2454 addedset.add(f)
2455 for f in removed:
2455 for f in removed:
2456 if f not in ctx1:
2456 if f not in ctx1:
2457 # Merged-in additions that are then removed are reported as removed.
2457 # Merged-in additions that are then removed are reported as removed.
2458 # They are not in ctx1, so We don't want to show them in the diff.
2458 # They are not in ctx1, so We don't want to show them in the diff.
2459 removedset.remove(f)
2459 removedset.remove(f)
2460 modified = sorted(modifiedset)
2460 modified = sorted(modifiedset)
2461 added = sorted(addedset)
2461 added = sorted(addedset)
2462 removed = sorted(removedset)
2462 removed = sorted(removedset)
2463 for dst, src in list(copy.items()):
2463 for dst, src in list(copy.items()):
2464 if src not in ctx1:
2464 if src not in ctx1:
2465 # Files merged in during a merge and then copied/renamed are
2465 # Files merged in during a merge and then copied/renamed are
2466 # reported as copies. We want to show them in the diff as additions.
2466 # reported as copies. We want to show them in the diff as additions.
2467 del copy[dst]
2467 del copy[dst]
2468
2468
2469 prefetchmatch = scmutil.matchfiles(
2469 prefetchmatch = scmutil.matchfiles(
2470 repo, list(modifiedset | addedset | removedset))
2470 repo, list(modifiedset | addedset | removedset))
2471 scmutil.prefetchfiles(repo, [ctx1.rev(), ctx2.rev()], prefetchmatch)
2471 scmutil.prefetchfiles(repo, [ctx1.rev(), ctx2.rev()], prefetchmatch)
2472
2472
2473 def difffn(opts, losedata):
2473 def difffn(opts, losedata):
2474 return trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
2474 return trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
2475 copy, getfilectx, opts, losedata, prefix, relroot)
2475 copy, getfilectx, opts, losedata, prefix, relroot)
2476 if opts.upgrade and not opts.git:
2476 if opts.upgrade and not opts.git:
2477 try:
2477 try:
2478 def losedata(fn):
2478 def losedata(fn):
2479 if not losedatafn or not losedatafn(fn=fn):
2479 if not losedatafn or not losedatafn(fn=fn):
2480 raise GitDiffRequired
2480 raise GitDiffRequired
2481 # Buffer the whole output until we are sure it can be generated
2481 # Buffer the whole output until we are sure it can be generated
2482 return list(difffn(opts.copy(git=False), losedata))
2482 return list(difffn(opts.copy(git=False), losedata))
2483 except GitDiffRequired:
2483 except GitDiffRequired:
2484 return difffn(opts.copy(git=True), None)
2484 return difffn(opts.copy(git=True), None)
2485 else:
2485 else:
2486 return difffn(opts, None)
2486 return difffn(opts, None)
2487
2487
2488 def diffsinglehunk(hunklines):
2488 def diffsinglehunk(hunklines):
2489 """yield tokens for a list of lines in a single hunk"""
2489 """yield tokens for a list of lines in a single hunk"""
2490 for line in hunklines:
2490 for line in hunklines:
2491 # chomp
2491 # chomp
2492 chompline = line.rstrip('\n')
2492 chompline = line.rstrip('\n')
2493 # highlight tabs and trailing whitespace
2493 # highlight tabs and trailing whitespace
2494 stripline = chompline.rstrip()
2494 stripline = chompline.rstrip()
2495 if line.startswith('-'):
2495 if line.startswith('-'):
2496 label = 'diff.deleted'
2496 label = 'diff.deleted'
2497 elif line.startswith('+'):
2497 elif line.startswith('+'):
2498 label = 'diff.inserted'
2498 label = 'diff.inserted'
2499 else:
2499 else:
2500 raise error.ProgrammingError('unexpected hunk line: %s' % line)
2500 raise error.ProgrammingError('unexpected hunk line: %s' % line)
2501 for token in tabsplitter.findall(stripline):
2501 for token in tabsplitter.findall(stripline):
2502 if token.startswith('\t'):
2502 if token.startswith('\t'):
2503 yield (token, 'diff.tab')
2503 yield (token, 'diff.tab')
2504 else:
2504 else:
2505 yield (token, label)
2505 yield (token, label)
2506
2506
2507 if chompline != stripline:
2507 if chompline != stripline:
2508 yield (chompline[len(stripline):], 'diff.trailingwhitespace')
2508 yield (chompline[len(stripline):], 'diff.trailingwhitespace')
2509 if chompline != line:
2509 if chompline != line:
2510 yield (line[len(chompline):], '')
2510 yield (line[len(chompline):], '')
2511
2511
2512 def diffsinglehunkinline(hunklines):
2512 def diffsinglehunkinline(hunklines):
2513 """yield tokens for a list of lines in a single hunk, with inline colors"""
2513 """yield tokens for a list of lines in a single hunk, with inline colors"""
2514 # prepare deleted, and inserted content
2514 # prepare deleted, and inserted content
2515 a = ''
2515 a = ''
2516 b = ''
2516 b = ''
2517 for line in hunklines:
2517 for line in hunklines:
2518 if line[0] == '-':
2518 if line[0] == '-':
2519 a += line[1:]
2519 a += line[1:]
2520 elif line[0] == '+':
2520 elif line[0] == '+':
2521 b += line[1:]
2521 b += line[1:]
2522 else:
2522 else:
2523 raise error.ProgrammingError('unexpected hunk line: %s' % line)
2523 raise error.ProgrammingError('unexpected hunk line: %s' % line)
2524 # fast path: if either side is empty, use diffsinglehunk
2524 # fast path: if either side is empty, use diffsinglehunk
2525 if not a or not b:
2525 if not a or not b:
2526 for t in diffsinglehunk(hunklines):
2526 for t in diffsinglehunk(hunklines):
2527 yield t
2527 yield t
2528 return
2528 return
2529 # re-split the content into words
2529 # re-split the content into words
2530 al = wordsplitter.findall(a)
2530 al = wordsplitter.findall(a)
2531 bl = wordsplitter.findall(b)
2531 bl = wordsplitter.findall(b)
2532 # re-arrange the words to lines since the diff algorithm is line-based
2532 # re-arrange the words to lines since the diff algorithm is line-based
2533 aln = [s if s == '\n' else s + '\n' for s in al]
2533 aln = [s if s == '\n' else s + '\n' for s in al]
2534 bln = [s if s == '\n' else s + '\n' for s in bl]
2534 bln = [s if s == '\n' else s + '\n' for s in bl]
2535 an = ''.join(aln)
2535 an = ''.join(aln)
2536 bn = ''.join(bln)
2536 bn = ''.join(bln)
2537 # run the diff algorithm, prepare atokens and btokens
2537 # run the diff algorithm, prepare atokens and btokens
2538 atokens = []
2538 atokens = []
2539 btokens = []
2539 btokens = []
2540 blocks = mdiff.allblocks(an, bn, lines1=aln, lines2=bln)
2540 blocks = mdiff.allblocks(an, bn, lines1=aln, lines2=bln)
2541 for (a1, a2, b1, b2), btype in blocks:
2541 for (a1, a2, b1, b2), btype in blocks:
2542 changed = btype == '!'
2542 changed = btype == '!'
2543 for token in mdiff.splitnewlines(''.join(al[a1:a2])):
2543 for token in mdiff.splitnewlines(''.join(al[a1:a2])):
2544 atokens.append((changed, token))
2544 atokens.append((changed, token))
2545 for token in mdiff.splitnewlines(''.join(bl[b1:b2])):
2545 for token in mdiff.splitnewlines(''.join(bl[b1:b2])):
2546 btokens.append((changed, token))
2546 btokens.append((changed, token))
2547
2547
2548 # yield deleted tokens, then inserted ones
2548 # yield deleted tokens, then inserted ones
2549 for prefix, label, tokens in [('-', 'diff.deleted', atokens),
2549 for prefix, label, tokens in [('-', 'diff.deleted', atokens),
2550 ('+', 'diff.inserted', btokens)]:
2550 ('+', 'diff.inserted', btokens)]:
2551 nextisnewline = True
2551 nextisnewline = True
2552 for changed, token in tokens:
2552 for changed, token in tokens:
2553 if nextisnewline:
2553 if nextisnewline:
2554 yield (prefix, label)
2554 yield (prefix, label)
2555 nextisnewline = False
2555 nextisnewline = False
2556 # special handling line end
2556 # special handling line end
2557 isendofline = token.endswith('\n')
2557 isendofline = token.endswith('\n')
2558 if isendofline:
2558 if isendofline:
2559 chomp = token[:-1] # chomp
2559 chomp = token[:-1] # chomp
2560 token = chomp.rstrip() # detect spaces at the end
2560 token = chomp.rstrip() # detect spaces at the end
2561 endspaces = chomp[len(token):]
2561 endspaces = chomp[len(token):]
2562 # scan tabs
2562 # scan tabs
2563 for maybetab in tabsplitter.findall(token):
2563 for maybetab in tabsplitter.findall(token):
2564 if '\t' == maybetab[0]:
2564 if '\t' == maybetab[0]:
2565 currentlabel = 'diff.tab'
2565 currentlabel = 'diff.tab'
2566 else:
2566 else:
2567 if changed:
2567 if changed:
2568 currentlabel = label + '.changed'
2568 currentlabel = label + '.changed'
2569 else:
2569 else:
2570 currentlabel = label + '.unchanged'
2570 currentlabel = label + '.unchanged'
2571 yield (maybetab, currentlabel)
2571 yield (maybetab, currentlabel)
2572 if isendofline:
2572 if isendofline:
2573 if endspaces:
2573 if endspaces:
2574 yield (endspaces, 'diff.trailingwhitespace')
2574 yield (endspaces, 'diff.trailingwhitespace')
2575 yield ('\n', '')
2575 yield ('\n', '')
2576 nextisnewline = True
2576 nextisnewline = True
2577
2577
2578 def difflabel(func, *args, **kw):
2578 def difflabel(func, *args, **kw):
2579 '''yields 2-tuples of (output, label) based on the output of func()'''
2579 '''yields 2-tuples of (output, label) based on the output of func()'''
2580 if kw.get(r'opts') and kw[r'opts'].worddiff:
2580 if kw.get(r'opts') and kw[r'opts'].worddiff:
2581 dodiffhunk = diffsinglehunkinline
2581 dodiffhunk = diffsinglehunkinline
2582 else:
2582 else:
2583 dodiffhunk = diffsinglehunk
2583 dodiffhunk = diffsinglehunk
2584 headprefixes = [('diff', 'diff.diffline'),
2584 headprefixes = [('diff', 'diff.diffline'),
2585 ('copy', 'diff.extended'),
2585 ('copy', 'diff.extended'),
2586 ('rename', 'diff.extended'),
2586 ('rename', 'diff.extended'),
2587 ('old', 'diff.extended'),
2587 ('old', 'diff.extended'),
2588 ('new', 'diff.extended'),
2588 ('new', 'diff.extended'),
2589 ('deleted', 'diff.extended'),
2589 ('deleted', 'diff.extended'),
2590 ('index', 'diff.extended'),
2590 ('index', 'diff.extended'),
2591 ('similarity', 'diff.extended'),
2591 ('similarity', 'diff.extended'),
2592 ('---', 'diff.file_a'),
2592 ('---', 'diff.file_a'),
2593 ('+++', 'diff.file_b')]
2593 ('+++', 'diff.file_b')]
2594 textprefixes = [('@', 'diff.hunk'),
2594 textprefixes = [('@', 'diff.hunk'),
2595 # - and + are handled by diffsinglehunk
2595 # - and + are handled by diffsinglehunk
2596 ]
2596 ]
2597 head = False
2597 head = False
2598
2598
2599 # buffers a hunk, i.e. adjacent "-", "+" lines without other changes.
2599 # buffers a hunk, i.e. adjacent "-", "+" lines without other changes.
2600 hunkbuffer = []
2600 hunkbuffer = []
2601 def consumehunkbuffer():
2601 def consumehunkbuffer():
2602 if hunkbuffer:
2602 if hunkbuffer:
2603 for token in dodiffhunk(hunkbuffer):
2603 for token in dodiffhunk(hunkbuffer):
2604 yield token
2604 yield token
2605 hunkbuffer[:] = []
2605 hunkbuffer[:] = []
2606
2606
2607 for chunk in func(*args, **kw):
2607 for chunk in func(*args, **kw):
2608 lines = chunk.split('\n')
2608 lines = chunk.split('\n')
2609 linecount = len(lines)
2609 linecount = len(lines)
2610 for i, line in enumerate(lines):
2610 for i, line in enumerate(lines):
2611 if head:
2611 if head:
2612 if line.startswith('@'):
2612 if line.startswith('@'):
2613 head = False
2613 head = False
2614 else:
2614 else:
2615 if line and not line.startswith((' ', '+', '-', '@', '\\')):
2615 if line and not line.startswith((' ', '+', '-', '@', '\\')):
2616 head = True
2616 head = True
2617 diffline = False
2617 diffline = False
2618 if not head and line and line.startswith(('+', '-')):
2618 if not head and line and line.startswith(('+', '-')):
2619 diffline = True
2619 diffline = True
2620
2620
2621 prefixes = textprefixes
2621 prefixes = textprefixes
2622 if head:
2622 if head:
2623 prefixes = headprefixes
2623 prefixes = headprefixes
2624 if diffline:
2624 if diffline:
2625 # buffered
2625 # buffered
2626 bufferedline = line
2626 bufferedline = line
2627 if i + 1 < linecount:
2627 if i + 1 < linecount:
2628 bufferedline += "\n"
2628 bufferedline += "\n"
2629 hunkbuffer.append(bufferedline)
2629 hunkbuffer.append(bufferedline)
2630 else:
2630 else:
2631 # unbuffered
2631 # unbuffered
2632 for token in consumehunkbuffer():
2632 for token in consumehunkbuffer():
2633 yield token
2633 yield token
2634 stripline = line.rstrip()
2634 stripline = line.rstrip()
2635 for prefix, label in prefixes:
2635 for prefix, label in prefixes:
2636 if stripline.startswith(prefix):
2636 if stripline.startswith(prefix):
2637 yield (stripline, label)
2637 yield (stripline, label)
2638 if line != stripline:
2638 if line != stripline:
2639 yield (line[len(stripline):],
2639 yield (line[len(stripline):],
2640 'diff.trailingwhitespace')
2640 'diff.trailingwhitespace')
2641 break
2641 break
2642 else:
2642 else:
2643 yield (line, '')
2643 yield (line, '')
2644 if i + 1 < linecount:
2644 if i + 1 < linecount:
2645 yield ('\n', '')
2645 yield ('\n', '')
2646 for token in consumehunkbuffer():
2646 for token in consumehunkbuffer():
2647 yield token
2647 yield token
2648
2648
2649 def diffui(*args, **kw):
2649 def diffui(*args, **kw):
2650 '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
2650 '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
2651 return difflabel(diff, *args, **kw)
2651 return difflabel(diff, *args, **kw)
2652
2652
2653 def _filepairs(modified, added, removed, copy, opts):
2653 def _filepairs(modified, added, removed, copy, opts):
2654 '''generates tuples (f1, f2, copyop), where f1 is the name of the file
2654 '''generates tuples (f1, f2, copyop), where f1 is the name of the file
2655 before and f2 is the the name after. For added files, f1 will be None,
2655 before and f2 is the the name after. For added files, f1 will be None,
2656 and for removed files, f2 will be None. copyop may be set to None, 'copy'
2656 and for removed files, f2 will be None. copyop may be set to None, 'copy'
2657 or 'rename' (the latter two only if opts.git is set).'''
2657 or 'rename' (the latter two only if opts.git is set).'''
2658 gone = set()
2658 gone = set()
2659
2659
2660 copyto = dict([(v, k) for k, v in copy.items()])
2660 copyto = dict([(v, k) for k, v in copy.items()])
2661
2661
2662 addedset, removedset = set(added), set(removed)
2662 addedset, removedset = set(added), set(removed)
2663
2663
2664 for f in sorted(modified + added + removed):
2664 for f in sorted(modified + added + removed):
2665 copyop = None
2665 copyop = None
2666 f1, f2 = f, f
2666 f1, f2 = f, f
2667 if f in addedset:
2667 if f in addedset:
2668 f1 = None
2668 f1 = None
2669 if f in copy:
2669 if f in copy:
2670 if opts.git:
2670 if opts.git:
2671 f1 = copy[f]
2671 f1 = copy[f]
2672 if f1 in removedset and f1 not in gone:
2672 if f1 in removedset and f1 not in gone:
2673 copyop = 'rename'
2673 copyop = 'rename'
2674 gone.add(f1)
2674 gone.add(f1)
2675 else:
2675 else:
2676 copyop = 'copy'
2676 copyop = 'copy'
2677 elif f in removedset:
2677 elif f in removedset:
2678 f2 = None
2678 f2 = None
2679 if opts.git:
2679 if opts.git:
2680 # have we already reported a copy above?
2680 # have we already reported a copy above?
2681 if (f in copyto and copyto[f] in addedset
2681 if (f in copyto and copyto[f] in addedset
2682 and copy[copyto[f]] == f):
2682 and copy[copyto[f]] == f):
2683 continue
2683 continue
2684 yield f1, f2, copyop
2684 yield f1, f2, copyop
2685
2685
2686 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
2686 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
2687 copy, getfilectx, opts, losedatafn, prefix, relroot):
2687 copy, getfilectx, opts, losedatafn, prefix, relroot):
2688 '''given input data, generate a diff and yield it in blocks
2688 '''given input data, generate a diff and yield it in blocks
2689
2689
2690 If generating a diff would lose data like flags or binary data and
2690 If generating a diff would lose data like flags or binary data and
2691 losedatafn is not None, it will be called.
2691 losedatafn is not None, it will be called.
2692
2692
2693 relroot is removed and prefix is added to every path in the diff output.
2693 relroot is removed and prefix is added to every path in the diff output.
2694
2694
2695 If relroot is not empty, this function expects every path in modified,
2695 If relroot is not empty, this function expects every path in modified,
2696 added, removed and copy to start with it.'''
2696 added, removed and copy to start with it.'''
2697
2697
2698 def gitindex(text):
2698 def gitindex(text):
2699 if not text:
2699 if not text:
2700 text = ""
2700 text = ""
2701 l = len(text)
2701 l = len(text)
2702 s = hashlib.sha1('blob %d\0' % l)
2702 s = hashlib.sha1('blob %d\0' % l)
2703 s.update(text)
2703 s.update(text)
2704 return hex(s.digest())
2704 return hex(s.digest())
2705
2705
2706 if opts.noprefix:
2706 if opts.noprefix:
2707 aprefix = bprefix = ''
2707 aprefix = bprefix = ''
2708 else:
2708 else:
2709 aprefix = 'a/'
2709 aprefix = 'a/'
2710 bprefix = 'b/'
2710 bprefix = 'b/'
2711
2711
2712 def diffline(f, revs):
2712 def diffline(f, revs):
2713 revinfo = ' '.join(["-r %s" % rev for rev in revs])
2713 revinfo = ' '.join(["-r %s" % rev for rev in revs])
2714 return 'diff %s %s' % (revinfo, f)
2714 return 'diff %s %s' % (revinfo, f)
2715
2715
2716 def isempty(fctx):
2716 def isempty(fctx):
2717 return fctx is None or fctx.size() == 0
2717 return fctx is None or fctx.size() == 0
2718
2718
2719 date1 = dateutil.datestr(ctx1.date())
2719 date1 = dateutil.datestr(ctx1.date())
2720 date2 = dateutil.datestr(ctx2.date())
2720 date2 = dateutil.datestr(ctx2.date())
2721
2721
2722 gitmode = {'l': '120000', 'x': '100755', '': '100644'}
2722 gitmode = {'l': '120000', 'x': '100755', '': '100644'}
2723
2723
2724 if relroot != '' and (repo.ui.configbool('devel', 'all-warnings')
2724 if relroot != '' and (repo.ui.configbool('devel', 'all-warnings')
2725 or repo.ui.configbool('devel', 'check-relroot')):
2725 or repo.ui.configbool('devel', 'check-relroot')):
2726 for f in modified + added + removed + list(copy) + list(copy.values()):
2726 for f in modified + added + removed + list(copy) + list(copy.values()):
2727 if f is not None and not f.startswith(relroot):
2727 if f is not None and not f.startswith(relroot):
2728 raise AssertionError(
2728 raise AssertionError(
2729 "file %s doesn't start with relroot %s" % (f, relroot))
2729 "file %s doesn't start with relroot %s" % (f, relroot))
2730
2730
2731 for f1, f2, copyop in _filepairs(modified, added, removed, copy, opts):
2731 for f1, f2, copyop in _filepairs(modified, added, removed, copy, opts):
2732 content1 = None
2732 content1 = None
2733 content2 = None
2733 content2 = None
2734 fctx1 = None
2734 fctx1 = None
2735 fctx2 = None
2735 fctx2 = None
2736 flag1 = None
2736 flag1 = None
2737 flag2 = None
2737 flag2 = None
2738 if f1:
2738 if f1:
2739 fctx1 = getfilectx(f1, ctx1)
2739 fctx1 = getfilectx(f1, ctx1)
2740 if opts.git or losedatafn:
2740 if opts.git or losedatafn:
2741 flag1 = ctx1.flags(f1)
2741 flag1 = ctx1.flags(f1)
2742 if f2:
2742 if f2:
2743 fctx2 = getfilectx(f2, ctx2)
2743 fctx2 = getfilectx(f2, ctx2)
2744 if opts.git or losedatafn:
2744 if opts.git or losedatafn:
2745 flag2 = ctx2.flags(f2)
2745 flag2 = ctx2.flags(f2)
2746 # if binary is True, output "summary" or "base85", but not "text diff"
2746 # if binary is True, output "summary" or "base85", but not "text diff"
2747 if opts.text:
2747 if opts.text:
2748 binary = False
2748 binary = False
2749 else:
2749 else:
2750 binary = any(f.isbinary() for f in [fctx1, fctx2] if f is not None)
2750 binary = any(f.isbinary() for f in [fctx1, fctx2] if f is not None)
2751
2751
2752 if losedatafn and not opts.git:
2752 if losedatafn and not opts.git:
2753 if (binary or
2753 if (binary or
2754 # copy/rename
2754 # copy/rename
2755 f2 in copy or
2755 f2 in copy or
2756 # empty file creation
2756 # empty file creation
2757 (not f1 and isempty(fctx2)) or
2757 (not f1 and isempty(fctx2)) or
2758 # empty file deletion
2758 # empty file deletion
2759 (isempty(fctx1) and not f2) or
2759 (isempty(fctx1) and not f2) or
2760 # create with flags
2760 # create with flags
2761 (not f1 and flag2) or
2761 (not f1 and flag2) or
2762 # change flags
2762 # change flags
2763 (f1 and f2 and flag1 != flag2)):
2763 (f1 and f2 and flag1 != flag2)):
2764 losedatafn(f2 or f1)
2764 losedatafn(f2 or f1)
2765
2765
2766 path1 = f1 or f2
2766 path1 = f1 or f2
2767 path2 = f2 or f1
2767 path2 = f2 or f1
2768 path1 = posixpath.join(prefix, path1[len(relroot):])
2768 path1 = posixpath.join(prefix, path1[len(relroot):])
2769 path2 = posixpath.join(prefix, path2[len(relroot):])
2769 path2 = posixpath.join(prefix, path2[len(relroot):])
2770 header = []
2770 header = []
2771 if opts.git:
2771 if opts.git:
2772 header.append('diff --git %s%s %s%s' %
2772 header.append('diff --git %s%s %s%s' %
2773 (aprefix, path1, bprefix, path2))
2773 (aprefix, path1, bprefix, path2))
2774 if not f1: # added
2774 if not f1: # added
2775 header.append('new file mode %s' % gitmode[flag2])
2775 header.append('new file mode %s' % gitmode[flag2])
2776 elif not f2: # removed
2776 elif not f2: # removed
2777 header.append('deleted file mode %s' % gitmode[flag1])
2777 header.append('deleted file mode %s' % gitmode[flag1])
2778 else: # modified/copied/renamed
2778 else: # modified/copied/renamed
2779 mode1, mode2 = gitmode[flag1], gitmode[flag2]
2779 mode1, mode2 = gitmode[flag1], gitmode[flag2]
2780 if mode1 != mode2:
2780 if mode1 != mode2:
2781 header.append('old mode %s' % mode1)
2781 header.append('old mode %s' % mode1)
2782 header.append('new mode %s' % mode2)
2782 header.append('new mode %s' % mode2)
2783 if copyop is not None:
2783 if copyop is not None:
2784 if opts.showsimilarity:
2784 if opts.showsimilarity:
2785 sim = similar.score(ctx1[path1], ctx2[path2]) * 100
2785 sim = similar.score(ctx1[path1], ctx2[path2]) * 100
2786 header.append('similarity index %d%%' % sim)
2786 header.append('similarity index %d%%' % sim)
2787 header.append('%s from %s' % (copyop, path1))
2787 header.append('%s from %s' % (copyop, path1))
2788 header.append('%s to %s' % (copyop, path2))
2788 header.append('%s to %s' % (copyop, path2))
2789 elif revs and not repo.ui.quiet:
2789 elif revs and not repo.ui.quiet:
2790 header.append(diffline(path1, revs))
2790 header.append(diffline(path1, revs))
2791
2791
2792 # fctx.is | diffopts | what to | is fctx.data()
2792 # fctx.is | diffopts | what to | is fctx.data()
2793 # binary() | text nobinary git index | output? | outputted?
2793 # binary() | text nobinary git index | output? | outputted?
2794 # ------------------------------------|----------------------------
2794 # ------------------------------------|----------------------------
2795 # yes | no no no * | summary | no
2795 # yes | no no no * | summary | no
2796 # yes | no no yes * | base85 | yes
2796 # yes | no no yes * | base85 | yes
2797 # yes | no yes no * | summary | no
2797 # yes | no yes no * | summary | no
2798 # yes | no yes yes 0 | summary | no
2798 # yes | no yes yes 0 | summary | no
2799 # yes | no yes yes >0 | summary | semi [1]
2799 # yes | no yes yes >0 | summary | semi [1]
2800 # yes | yes * * * | text diff | yes
2800 # yes | yes * * * | text diff | yes
2801 # no | * * * * | text diff | yes
2801 # no | * * * * | text diff | yes
2802 # [1]: hash(fctx.data()) is outputted. so fctx.data() cannot be faked
2802 # [1]: hash(fctx.data()) is outputted. so fctx.data() cannot be faked
2803 if binary and (not opts.git or (opts.git and opts.nobinary and not
2803 if binary and (not opts.git or (opts.git and opts.nobinary and not
2804 opts.index)):
2804 opts.index)):
2805 # fast path: no binary content will be displayed, content1 and
2805 # fast path: no binary content will be displayed, content1 and
2806 # content2 are only used for equivalent test. cmp() could have a
2806 # content2 are only used for equivalent test. cmp() could have a
2807 # fast path.
2807 # fast path.
2808 if fctx1 is not None:
2808 if fctx1 is not None:
2809 content1 = b'\0'
2809 content1 = b'\0'
2810 if fctx2 is not None:
2810 if fctx2 is not None:
2811 if fctx1 is not None and not fctx1.cmp(fctx2):
2811 if fctx1 is not None and not fctx1.cmp(fctx2):
2812 content2 = b'\0' # not different
2812 content2 = b'\0' # not different
2813 else:
2813 else:
2814 content2 = b'\0\0'
2814 content2 = b'\0\0'
2815 else:
2815 else:
2816 # normal path: load contents
2816 # normal path: load contents
2817 if fctx1 is not None:
2817 if fctx1 is not None:
2818 content1 = fctx1.data()
2818 content1 = fctx1.data()
2819 if fctx2 is not None:
2819 if fctx2 is not None:
2820 content2 = fctx2.data()
2820 content2 = fctx2.data()
2821
2821
2822 if binary and opts.git and not opts.nobinary:
2822 if binary and opts.git and not opts.nobinary:
2823 text = mdiff.b85diff(content1, content2)
2823 text = mdiff.b85diff(content1, content2)
2824 if text:
2824 if text:
2825 header.append('index %s..%s' %
2825 header.append('index %s..%s' %
2826 (gitindex(content1), gitindex(content2)))
2826 (gitindex(content1), gitindex(content2)))
2827 hunks = (None, [text]),
2827 hunks = (None, [text]),
2828 else:
2828 else:
2829 if opts.git and opts.index > 0:
2829 if opts.git and opts.index > 0:
2830 flag = flag1
2830 flag = flag1
2831 if flag is None:
2831 if flag is None:
2832 flag = flag2
2832 flag = flag2
2833 header.append('index %s..%s %s' %
2833 header.append('index %s..%s %s' %
2834 (gitindex(content1)[0:opts.index],
2834 (gitindex(content1)[0:opts.index],
2835 gitindex(content2)[0:opts.index],
2835 gitindex(content2)[0:opts.index],
2836 gitmode[flag]))
2836 gitmode[flag]))
2837
2837
2838 uheaders, hunks = mdiff.unidiff(content1, date1,
2838 uheaders, hunks = mdiff.unidiff(content1, date1,
2839 content2, date2,
2839 content2, date2,
2840 path1, path2,
2840 path1, path2,
2841 binary=binary, opts=opts)
2841 binary=binary, opts=opts)
2842 header.extend(uheaders)
2842 header.extend(uheaders)
2843 yield fctx1, fctx2, header, hunks
2843 yield fctx1, fctx2, header, hunks
2844
2844
2845 def diffstatsum(stats):
2845 def diffstatsum(stats):
2846 maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False
2846 maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False
2847 for f, a, r, b in stats:
2847 for f, a, r, b in stats:
2848 maxfile = max(maxfile, encoding.colwidth(f))
2848 maxfile = max(maxfile, encoding.colwidth(f))
2849 maxtotal = max(maxtotal, a + r)
2849 maxtotal = max(maxtotal, a + r)
2850 addtotal += a
2850 addtotal += a
2851 removetotal += r
2851 removetotal += r
2852 binary = binary or b
2852 binary = binary or b
2853
2853
2854 return maxfile, maxtotal, addtotal, removetotal, binary
2854 return maxfile, maxtotal, addtotal, removetotal, binary
2855
2855
2856 def diffstatdata(lines):
2856 def diffstatdata(lines):
2857 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
2857 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
2858
2858
2859 results = []
2859 results = []
2860 filename, adds, removes, isbinary = None, 0, 0, False
2860 filename, adds, removes, isbinary = None, 0, 0, False
2861
2861
2862 def addresult():
2862 def addresult():
2863 if filename:
2863 if filename:
2864 results.append((filename, adds, removes, isbinary))
2864 results.append((filename, adds, removes, isbinary))
2865
2865
2866 # inheader is used to track if a line is in the
2866 # inheader is used to track if a line is in the
2867 # header portion of the diff. This helps properly account
2867 # header portion of the diff. This helps properly account
2868 # for lines that start with '--' or '++'
2868 # for lines that start with '--' or '++'
2869 inheader = False
2869 inheader = False
2870
2870
2871 for line in lines:
2871 for line in lines:
2872 if line.startswith('diff'):
2872 if line.startswith('diff'):
2873 addresult()
2873 addresult()
2874 # starting a new file diff
2874 # starting a new file diff
2875 # set numbers to 0 and reset inheader
2875 # set numbers to 0 and reset inheader
2876 inheader = True
2876 inheader = True
2877 adds, removes, isbinary = 0, 0, False
2877 adds, removes, isbinary = 0, 0, False
2878 if line.startswith('diff --git a/'):
2878 if line.startswith('diff --git a/'):
2879 filename = gitre.search(line).group(2)
2879 filename = gitre.search(line).group(2)
2880 elif line.startswith('diff -r'):
2880 elif line.startswith('diff -r'):
2881 # format: "diff -r ... -r ... filename"
2881 # format: "diff -r ... -r ... filename"
2882 filename = diffre.search(line).group(1)
2882 filename = diffre.search(line).group(1)
2883 elif line.startswith('@@'):
2883 elif line.startswith('@@'):
2884 inheader = False
2884 inheader = False
2885 elif line.startswith('+') and not inheader:
2885 elif line.startswith('+') and not inheader:
2886 adds += 1
2886 adds += 1
2887 elif line.startswith('-') and not inheader:
2887 elif line.startswith('-') and not inheader:
2888 removes += 1
2888 removes += 1
2889 elif (line.startswith('GIT binary patch') or
2889 elif (line.startswith('GIT binary patch') or
2890 line.startswith('Binary file')):
2890 line.startswith('Binary file')):
2891 isbinary = True
2891 isbinary = True
2892 addresult()
2892 addresult()
2893 return results
2893 return results
2894
2894
2895 def diffstat(lines, width=80):
2895 def diffstat(lines, width=80):
2896 output = []
2896 output = []
2897 stats = diffstatdata(lines)
2897 stats = diffstatdata(lines)
2898 maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats)
2898 maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats)
2899
2899
2900 countwidth = len(str(maxtotal))
2900 countwidth = len(str(maxtotal))
2901 if hasbinary and countwidth < 3:
2901 if hasbinary and countwidth < 3:
2902 countwidth = 3
2902 countwidth = 3
2903 graphwidth = width - countwidth - maxname - 6
2903 graphwidth = width - countwidth - maxname - 6
2904 if graphwidth < 10:
2904 if graphwidth < 10:
2905 graphwidth = 10
2905 graphwidth = 10
2906
2906
2907 def scale(i):
2907 def scale(i):
2908 if maxtotal <= graphwidth:
2908 if maxtotal <= graphwidth:
2909 return i
2909 return i
2910 # If diffstat runs out of room it doesn't print anything,
2910 # If diffstat runs out of room it doesn't print anything,
2911 # which isn't very useful, so always print at least one + or -
2911 # which isn't very useful, so always print at least one + or -
2912 # if there were at least some changes.
2912 # if there were at least some changes.
2913 return max(i * graphwidth // maxtotal, int(bool(i)))
2913 return max(i * graphwidth // maxtotal, int(bool(i)))
2914
2914
2915 for filename, adds, removes, isbinary in stats:
2915 for filename, adds, removes, isbinary in stats:
2916 if isbinary:
2916 if isbinary:
2917 count = 'Bin'
2917 count = 'Bin'
2918 else:
2918 else:
2919 count = '%d' % (adds + removes)
2919 count = '%d' % (adds + removes)
2920 pluses = '+' * scale(adds)
2920 pluses = '+' * scale(adds)
2921 minuses = '-' * scale(removes)
2921 minuses = '-' * scale(removes)
2922 output.append(' %s%s | %*s %s%s\n' %
2922 output.append(' %s%s | %*s %s%s\n' %
2923 (filename, ' ' * (maxname - encoding.colwidth(filename)),
2923 (filename, ' ' * (maxname - encoding.colwidth(filename)),
2924 countwidth, count, pluses, minuses))
2924 countwidth, count, pluses, minuses))
2925
2925
2926 if stats:
2926 if stats:
2927 output.append(_(' %d files changed, %d insertions(+), '
2927 output.append(_(' %d files changed, %d insertions(+), '
2928 '%d deletions(-)\n')
2928 '%d deletions(-)\n')
2929 % (len(stats), totaladds, totalremoves))
2929 % (len(stats), totaladds, totalremoves))
2930
2930
2931 return ''.join(output)
2931 return ''.join(output)
2932
2932
2933 def diffstatui(*args, **kw):
2933 def diffstatui(*args, **kw):
2934 '''like diffstat(), but yields 2-tuples of (output, label) for
2934 '''like diffstat(), but yields 2-tuples of (output, label) for
2935 ui.write()
2935 ui.write()
2936 '''
2936 '''
2937
2937
2938 for line in diffstat(*args, **kw).splitlines():
2938 for line in diffstat(*args, **kw).splitlines():
2939 if line and line[-1] in '+-':
2939 if line and line[-1] in '+-':
2940 name, graph = line.rsplit(' ', 1)
2940 name, graph = line.rsplit(' ', 1)
2941 yield (name + ' ', '')
2941 yield (name + ' ', '')
2942 m = re.search(br'\++', graph)
2942 m = re.search(br'\++', graph)
2943 if m:
2943 if m:
2944 yield (m.group(0), 'diffstat.inserted')
2944 yield (m.group(0), 'diffstat.inserted')
2945 m = re.search(br'-+', graph)
2945 m = re.search(br'-+', graph)
2946 if m:
2946 if m:
2947 yield (m.group(0), 'diffstat.deleted')
2947 yield (m.group(0), 'diffstat.deleted')
2948 else:
2948 else:
2949 yield (line, '')
2949 yield (line, '')
2950 yield ('\n', '')
2950 yield ('\n', '')
@@ -1,923 +1,924 b''
1 # templater.py - template expansion for output
1 # templater.py - template expansion for output
2 #
2 #
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7
7
8 """Slightly complicated template engine for commands and hgweb
8 """Slightly complicated template engine for commands and hgweb
9
9
10 This module provides low-level interface to the template engine. See the
10 This module provides low-level interface to the template engine. See the
11 formatter and cmdutil modules if you are looking for high-level functions
11 formatter and cmdutil modules if you are looking for high-level functions
12 such as ``cmdutil.rendertemplate(ctx, tmpl)``.
12 such as ``cmdutil.rendertemplate(ctx, tmpl)``.
13
13
14 Internal Data Types
14 Internal Data Types
15 -------------------
15 -------------------
16
16
17 Template keywords and functions take a dictionary of current symbols and
17 Template keywords and functions take a dictionary of current symbols and
18 resources (a "mapping") and return result. Inputs and outputs must be one
18 resources (a "mapping") and return result. Inputs and outputs must be one
19 of the following data types:
19 of the following data types:
20
20
21 bytes
21 bytes
22 a byte string, which is generally a human-readable text in local encoding.
22 a byte string, which is generally a human-readable text in local encoding.
23
23
24 generator
24 generator
25 a lazily-evaluated byte string, which is a possibly nested generator of
25 a lazily-evaluated byte string, which is a possibly nested generator of
26 values of any printable types, and will be folded by ``stringify()``
26 values of any printable types, and will be folded by ``stringify()``
27 or ``flatten()``.
27 or ``flatten()``.
28
28
29 BUG: hgweb overloads this type for mappings (i.e. some hgweb keywords
29 BUG: hgweb overloads this type for mappings (i.e. some hgweb keywords
30 returns a generator of dicts.)
30 returns a generator of dicts.)
31
31
32 None
32 None
33 sometimes represents an empty value, which can be stringified to ''.
33 sometimes represents an empty value, which can be stringified to ''.
34
34
35 True, False, int, float
35 True, False, int, float
36 can be stringified as such.
36 can be stringified as such.
37
37
38 date tuple
38 date tuple
39 a (unixtime, offset) tuple, which produces no meaningful output by itself.
39 a (unixtime, offset) tuple, which produces no meaningful output by itself.
40
40
41 hybrid
41 hybrid
42 represents a list/dict of printable values, which can also be converted
42 represents a list/dict of printable values, which can also be converted
43 to mappings by % operator.
43 to mappings by % operator.
44
44
45 mappable
45 mappable
46 represents a scalar printable value, also supports % operator.
46 represents a scalar printable value, also supports % operator.
47
47
48 mappinggenerator, mappinglist
48 mappinggenerator, mappinglist
49 represents mappings (i.e. a list of dicts), which may have default
49 represents mappings (i.e. a list of dicts), which may have default
50 output format.
50 output format.
51
51
52 mappedgenerator
52 mappedgenerator
53 a lazily-evaluated list of byte strings, which is e.g. a result of %
53 a lazily-evaluated list of byte strings, which is e.g. a result of %
54 operation.
54 operation.
55 """
55 """
56
56
57 from __future__ import absolute_import, print_function
57 from __future__ import absolute_import, print_function
58
58
59 import abc
59 import abc
60 import os
60 import os
61
61
62 from .i18n import _
62 from .i18n import _
63 from . import (
63 from . import (
64 config,
64 config,
65 encoding,
65 encoding,
66 error,
66 error,
67 parser,
67 parser,
68 pycompat,
68 pycompat,
69 templatefilters,
69 templatefilters,
70 templatefuncs,
70 templatefuncs,
71 templateutil,
71 templateutil,
72 util,
72 util,
73 )
73 )
74 from .utils import (
74 from .utils import (
75 stringutil,
75 stringutil,
76 )
76 )
77
77
78 # template parsing
78 # template parsing
79
79
80 elements = {
80 elements = {
81 # token-type: binding-strength, primary, prefix, infix, suffix
81 # token-type: binding-strength, primary, prefix, infix, suffix
82 "(": (20, None, ("group", 1, ")"), ("func", 1, ")"), None),
82 "(": (20, None, ("group", 1, ")"), ("func", 1, ")"), None),
83 ".": (18, None, None, (".", 18), None),
83 ".": (18, None, None, (".", 18), None),
84 "%": (15, None, None, ("%", 15), None),
84 "%": (15, None, None, ("%", 15), None),
85 "|": (15, None, None, ("|", 15), None),
85 "|": (15, None, None, ("|", 15), None),
86 "*": (5, None, None, ("*", 5), None),
86 "*": (5, None, None, ("*", 5), None),
87 "/": (5, None, None, ("/", 5), None),
87 "/": (5, None, None, ("/", 5), None),
88 "+": (4, None, None, ("+", 4), None),
88 "+": (4, None, None, ("+", 4), None),
89 "-": (4, None, ("negate", 19), ("-", 4), None),
89 "-": (4, None, ("negate", 19), ("-", 4), None),
90 "=": (3, None, None, ("keyvalue", 3), None),
90 "=": (3, None, None, ("keyvalue", 3), None),
91 ",": (2, None, None, ("list", 2), None),
91 ",": (2, None, None, ("list", 2), None),
92 ")": (0, None, None, None, None),
92 ")": (0, None, None, None, None),
93 "integer": (0, "integer", None, None, None),
93 "integer": (0, "integer", None, None, None),
94 "symbol": (0, "symbol", None, None, None),
94 "symbol": (0, "symbol", None, None, None),
95 "string": (0, "string", None, None, None),
95 "string": (0, "string", None, None, None),
96 "template": (0, "template", None, None, None),
96 "template": (0, "template", None, None, None),
97 "end": (0, None, None, None, None),
97 "end": (0, None, None, None, None),
98 }
98 }
99
99
100 def tokenize(program, start, end, term=None):
100 def tokenize(program, start, end, term=None):
101 """Parse a template expression into a stream of tokens, which must end
101 """Parse a template expression into a stream of tokens, which must end
102 with term if specified"""
102 with term if specified"""
103 pos = start
103 pos = start
104 program = pycompat.bytestr(program)
104 program = pycompat.bytestr(program)
105 while pos < end:
105 while pos < end:
106 c = program[pos]
106 c = program[pos]
107 if c.isspace(): # skip inter-token whitespace
107 if c.isspace(): # skip inter-token whitespace
108 pass
108 pass
109 elif c in "(=,).%|+-*/": # handle simple operators
109 elif c in "(=,).%|+-*/": # handle simple operators
110 yield (c, None, pos)
110 yield (c, None, pos)
111 elif c in '"\'': # handle quoted templates
111 elif c in '"\'': # handle quoted templates
112 s = pos + 1
112 s = pos + 1
113 data, pos = _parsetemplate(program, s, end, c)
113 data, pos = _parsetemplate(program, s, end, c)
114 yield ('template', data, s)
114 yield ('template', data, s)
115 pos -= 1
115 pos -= 1
116 elif c == 'r' and program[pos:pos + 2] in ("r'", 'r"'):
116 elif c == 'r' and program[pos:pos + 2] in ("r'", 'r"'):
117 # handle quoted strings
117 # handle quoted strings
118 c = program[pos + 1]
118 c = program[pos + 1]
119 s = pos = pos + 2
119 s = pos = pos + 2
120 while pos < end: # find closing quote
120 while pos < end: # find closing quote
121 d = program[pos]
121 d = program[pos]
122 if d == '\\': # skip over escaped characters
122 if d == '\\': # skip over escaped characters
123 pos += 2
123 pos += 2
124 continue
124 continue
125 if d == c:
125 if d == c:
126 yield ('string', program[s:pos], s)
126 yield ('string', program[s:pos], s)
127 break
127 break
128 pos += 1
128 pos += 1
129 else:
129 else:
130 raise error.ParseError(_("unterminated string"), s)
130 raise error.ParseError(_("unterminated string"), s)
131 elif c.isdigit():
131 elif c.isdigit():
132 s = pos
132 s = pos
133 while pos < end:
133 while pos < end:
134 d = program[pos]
134 d = program[pos]
135 if not d.isdigit():
135 if not d.isdigit():
136 break
136 break
137 pos += 1
137 pos += 1
138 yield ('integer', program[s:pos], s)
138 yield ('integer', program[s:pos], s)
139 pos -= 1
139 pos -= 1
140 elif (c == '\\' and program[pos:pos + 2] in (br"\'", br'\"')
140 elif (c == '\\' and program[pos:pos + 2] in (br"\'", br'\"')
141 or c == 'r' and program[pos:pos + 3] in (br"r\'", br'r\"')):
141 or c == 'r' and program[pos:pos + 3] in (br"r\'", br'r\"')):
142 # handle escaped quoted strings for compatibility with 2.9.2-3.4,
142 # handle escaped quoted strings for compatibility with 2.9.2-3.4,
143 # where some of nested templates were preprocessed as strings and
143 # where some of nested templates were preprocessed as strings and
144 # then compiled. therefore, \"...\" was allowed. (issue4733)
144 # then compiled. therefore, \"...\" was allowed. (issue4733)
145 #
145 #
146 # processing flow of _evalifliteral() at 5ab28a2e9962:
146 # processing flow of _evalifliteral() at 5ab28a2e9962:
147 # outer template string -> stringify() -> compiletemplate()
147 # outer template string -> stringify() -> compiletemplate()
148 # ------------------------ ------------ ------------------
148 # ------------------------ ------------ ------------------
149 # {f("\\\\ {g(\"\\\"\")}"} \\ {g("\"")} [r'\\', {g("\"")}]
149 # {f("\\\\ {g(\"\\\"\")}"} \\ {g("\"")} [r'\\', {g("\"")}]
150 # ~~~~~~~~
150 # ~~~~~~~~
151 # escaped quoted string
151 # escaped quoted string
152 if c == 'r':
152 if c == 'r':
153 pos += 1
153 pos += 1
154 token = 'string'
154 token = 'string'
155 else:
155 else:
156 token = 'template'
156 token = 'template'
157 quote = program[pos:pos + 2]
157 quote = program[pos:pos + 2]
158 s = pos = pos + 2
158 s = pos = pos + 2
159 while pos < end: # find closing escaped quote
159 while pos < end: # find closing escaped quote
160 if program.startswith('\\\\\\', pos, end):
160 if program.startswith('\\\\\\', pos, end):
161 pos += 4 # skip over double escaped characters
161 pos += 4 # skip over double escaped characters
162 continue
162 continue
163 if program.startswith(quote, pos, end):
163 if program.startswith(quote, pos, end):
164 # interpret as if it were a part of an outer string
164 # interpret as if it were a part of an outer string
165 data = parser.unescapestr(program[s:pos])
165 data = parser.unescapestr(program[s:pos])
166 if token == 'template':
166 if token == 'template':
167 data = _parsetemplate(data, 0, len(data))[0]
167 data = _parsetemplate(data, 0, len(data))[0]
168 yield (token, data, s)
168 yield (token, data, s)
169 pos += 1
169 pos += 1
170 break
170 break
171 pos += 1
171 pos += 1
172 else:
172 else:
173 raise error.ParseError(_("unterminated string"), s)
173 raise error.ParseError(_("unterminated string"), s)
174 elif c.isalnum() or c in '_':
174 elif c.isalnum() or c in '_':
175 s = pos
175 s = pos
176 pos += 1
176 pos += 1
177 while pos < end: # find end of symbol
177 while pos < end: # find end of symbol
178 d = program[pos]
178 d = program[pos]
179 if not (d.isalnum() or d == "_"):
179 if not (d.isalnum() or d == "_"):
180 break
180 break
181 pos += 1
181 pos += 1
182 sym = program[s:pos]
182 sym = program[s:pos]
183 yield ('symbol', sym, s)
183 yield ('symbol', sym, s)
184 pos -= 1
184 pos -= 1
185 elif c == term:
185 elif c == term:
186 yield ('end', None, pos)
186 yield ('end', None, pos)
187 return
187 return
188 else:
188 else:
189 raise error.ParseError(_("syntax error"), pos)
189 raise error.ParseError(_("syntax error"), pos)
190 pos += 1
190 pos += 1
191 if term:
191 if term:
192 raise error.ParseError(_("unterminated template expansion"), start)
192 raise error.ParseError(_("unterminated template expansion"), start)
193 yield ('end', None, pos)
193 yield ('end', None, pos)
194
194
195 def _parsetemplate(tmpl, start, stop, quote=''):
195 def _parsetemplate(tmpl, start, stop, quote=''):
196 r"""
196 r"""
197 >>> _parsetemplate(b'foo{bar}"baz', 0, 12)
197 >>> _parsetemplate(b'foo{bar}"baz', 0, 12)
198 ([('string', 'foo'), ('symbol', 'bar'), ('string', '"baz')], 12)
198 ([('string', 'foo'), ('symbol', 'bar'), ('string', '"baz')], 12)
199 >>> _parsetemplate(b'foo{bar}"baz', 0, 12, quote=b'"')
199 >>> _parsetemplate(b'foo{bar}"baz', 0, 12, quote=b'"')
200 ([('string', 'foo'), ('symbol', 'bar')], 9)
200 ([('string', 'foo'), ('symbol', 'bar')], 9)
201 >>> _parsetemplate(b'foo"{bar}', 0, 9, quote=b'"')
201 >>> _parsetemplate(b'foo"{bar}', 0, 9, quote=b'"')
202 ([('string', 'foo')], 4)
202 ([('string', 'foo')], 4)
203 >>> _parsetemplate(br'foo\"bar"baz', 0, 12, quote=b'"')
203 >>> _parsetemplate(br'foo\"bar"baz', 0, 12, quote=b'"')
204 ([('string', 'foo"'), ('string', 'bar')], 9)
204 ([('string', 'foo"'), ('string', 'bar')], 9)
205 >>> _parsetemplate(br'foo\\"bar', 0, 10, quote=b'"')
205 >>> _parsetemplate(br'foo\\"bar', 0, 10, quote=b'"')
206 ([('string', 'foo\\')], 6)
206 ([('string', 'foo\\')], 6)
207 """
207 """
208 parsed = []
208 parsed = []
209 for typ, val, pos in _scantemplate(tmpl, start, stop, quote):
209 for typ, val, pos in _scantemplate(tmpl, start, stop, quote):
210 if typ == 'string':
210 if typ == 'string':
211 parsed.append((typ, val))
211 parsed.append((typ, val))
212 elif typ == 'template':
212 elif typ == 'template':
213 parsed.append(val)
213 parsed.append(val)
214 elif typ == 'end':
214 elif typ == 'end':
215 return parsed, pos
215 return parsed, pos
216 else:
216 else:
217 raise error.ProgrammingError('unexpected type: %s' % typ)
217 raise error.ProgrammingError('unexpected type: %s' % typ)
218 raise error.ProgrammingError('unterminated scanning of template')
218 raise error.ProgrammingError('unterminated scanning of template')
219
219
220 def scantemplate(tmpl, raw=False):
220 def scantemplate(tmpl, raw=False):
221 r"""Scan (type, start, end) positions of outermost elements in template
221 r"""Scan (type, start, end) positions of outermost elements in template
222
222
223 If raw=True, a backslash is not taken as an escape character just like
223 If raw=True, a backslash is not taken as an escape character just like
224 r'' string in Python. Note that this is different from r'' literal in
224 r'' string in Python. Note that this is different from r'' literal in
225 template in that no template fragment can appear in r'', e.g. r'{foo}'
225 template in that no template fragment can appear in r'', e.g. r'{foo}'
226 is a literal '{foo}', but ('{foo}', raw=True) is a template expression
226 is a literal '{foo}', but ('{foo}', raw=True) is a template expression
227 'foo'.
227 'foo'.
228
228
229 >>> list(scantemplate(b'foo{bar}"baz'))
229 >>> list(scantemplate(b'foo{bar}"baz'))
230 [('string', 0, 3), ('template', 3, 8), ('string', 8, 12)]
230 [('string', 0, 3), ('template', 3, 8), ('string', 8, 12)]
231 >>> list(scantemplate(b'outer{"inner"}outer'))
231 >>> list(scantemplate(b'outer{"inner"}outer'))
232 [('string', 0, 5), ('template', 5, 14), ('string', 14, 19)]
232 [('string', 0, 5), ('template', 5, 14), ('string', 14, 19)]
233 >>> list(scantemplate(b'foo\\{escaped}'))
233 >>> list(scantemplate(b'foo\\{escaped}'))
234 [('string', 0, 5), ('string', 5, 13)]
234 [('string', 0, 5), ('string', 5, 13)]
235 >>> list(scantemplate(b'foo\\{escaped}', raw=True))
235 >>> list(scantemplate(b'foo\\{escaped}', raw=True))
236 [('string', 0, 4), ('template', 4, 13)]
236 [('string', 0, 4), ('template', 4, 13)]
237 """
237 """
238 last = None
238 last = None
239 for typ, val, pos in _scantemplate(tmpl, 0, len(tmpl), raw=raw):
239 for typ, val, pos in _scantemplate(tmpl, 0, len(tmpl), raw=raw):
240 if last:
240 if last:
241 yield last + (pos,)
241 yield last + (pos,)
242 if typ == 'end':
242 if typ == 'end':
243 return
243 return
244 else:
244 else:
245 last = (typ, pos)
245 last = (typ, pos)
246 raise error.ProgrammingError('unterminated scanning of template')
246 raise error.ProgrammingError('unterminated scanning of template')
247
247
248 def _scantemplate(tmpl, start, stop, quote='', raw=False):
248 def _scantemplate(tmpl, start, stop, quote='', raw=False):
249 """Parse template string into chunks of strings and template expressions"""
249 """Parse template string into chunks of strings and template expressions"""
250 sepchars = '{' + quote
250 sepchars = '{' + quote
251 unescape = [parser.unescapestr, pycompat.identity][raw]
251 unescape = [parser.unescapestr, pycompat.identity][raw]
252 pos = start
252 pos = start
253 p = parser.parser(elements)
253 p = parser.parser(elements)
254 try:
254 try:
255 while pos < stop:
255 while pos < stop:
256 n = min((tmpl.find(c, pos, stop) for c in sepchars),
256 n = min((tmpl.find(c, pos, stop)
257 for c in pycompat.bytestr(sepchars)),
257 key=lambda n: (n < 0, n))
258 key=lambda n: (n < 0, n))
258 if n < 0:
259 if n < 0:
259 yield ('string', unescape(tmpl[pos:stop]), pos)
260 yield ('string', unescape(tmpl[pos:stop]), pos)
260 pos = stop
261 pos = stop
261 break
262 break
262 c = tmpl[n:n + 1]
263 c = tmpl[n:n + 1]
263 bs = 0 # count leading backslashes
264 bs = 0 # count leading backslashes
264 if not raw:
265 if not raw:
265 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
266 bs = (n - pos) - len(tmpl[pos:n].rstrip('\\'))
266 if bs % 2 == 1:
267 if bs % 2 == 1:
267 # escaped (e.g. '\{', '\\\{', but not '\\{')
268 # escaped (e.g. '\{', '\\\{', but not '\\{')
268 yield ('string', unescape(tmpl[pos:n - 1]) + c, pos)
269 yield ('string', unescape(tmpl[pos:n - 1]) + c, pos)
269 pos = n + 1
270 pos = n + 1
270 continue
271 continue
271 if n > pos:
272 if n > pos:
272 yield ('string', unescape(tmpl[pos:n]), pos)
273 yield ('string', unescape(tmpl[pos:n]), pos)
273 if c == quote:
274 if c == quote:
274 yield ('end', None, n + 1)
275 yield ('end', None, n + 1)
275 return
276 return
276
277
277 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}'))
278 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop, '}'))
278 if not tmpl.startswith('}', pos):
279 if not tmpl.startswith('}', pos):
279 raise error.ParseError(_("invalid token"), pos)
280 raise error.ParseError(_("invalid token"), pos)
280 yield ('template', parseres, n)
281 yield ('template', parseres, n)
281 pos += 1
282 pos += 1
282
283
283 if quote:
284 if quote:
284 raise error.ParseError(_("unterminated string"), start)
285 raise error.ParseError(_("unterminated string"), start)
285 except error.ParseError as inst:
286 except error.ParseError as inst:
286 if len(inst.args) > 1: # has location
287 if len(inst.args) > 1: # has location
287 loc = inst.args[1]
288 loc = inst.args[1]
288 # Offset the caret location by the number of newlines before the
289 # Offset the caret location by the number of newlines before the
289 # location of the error, since we will replace one-char newlines
290 # location of the error, since we will replace one-char newlines
290 # with the two-char literal r'\n'.
291 # with the two-char literal r'\n'.
291 offset = tmpl[:loc].count('\n')
292 offset = tmpl[:loc].count('\n')
292 tmpl = tmpl.replace('\n', br'\n')
293 tmpl = tmpl.replace('\n', br'\n')
293 # We want the caret to point to the place in the template that
294 # We want the caret to point to the place in the template that
294 # failed to parse, but in a hint we get a open paren at the
295 # failed to parse, but in a hint we get a open paren at the
295 # start. Therefore, we print "loc + 1" spaces (instead of "loc")
296 # start. Therefore, we print "loc + 1" spaces (instead of "loc")
296 # to line up the caret with the location of the error.
297 # to line up the caret with the location of the error.
297 inst.hint = (tmpl + '\n'
298 inst.hint = (tmpl + '\n'
298 + ' ' * (loc + 1 + offset) + '^ ' + _('here'))
299 + ' ' * (loc + 1 + offset) + '^ ' + _('here'))
299 raise
300 raise
300 yield ('end', None, pos)
301 yield ('end', None, pos)
301
302
302 def _unnesttemplatelist(tree):
303 def _unnesttemplatelist(tree):
303 """Expand list of templates to node tuple
304 """Expand list of templates to node tuple
304
305
305 >>> def f(tree):
306 >>> def f(tree):
306 ... print(pycompat.sysstr(prettyformat(_unnesttemplatelist(tree))))
307 ... print(pycompat.sysstr(prettyformat(_unnesttemplatelist(tree))))
307 >>> f((b'template', []))
308 >>> f((b'template', []))
308 (string '')
309 (string '')
309 >>> f((b'template', [(b'string', b'foo')]))
310 >>> f((b'template', [(b'string', b'foo')]))
310 (string 'foo')
311 (string 'foo')
311 >>> f((b'template', [(b'string', b'foo'), (b'symbol', b'rev')]))
312 >>> f((b'template', [(b'string', b'foo'), (b'symbol', b'rev')]))
312 (template
313 (template
313 (string 'foo')
314 (string 'foo')
314 (symbol 'rev'))
315 (symbol 'rev'))
315 >>> f((b'template', [(b'symbol', b'rev')])) # template(rev) -> str
316 >>> f((b'template', [(b'symbol', b'rev')])) # template(rev) -> str
316 (template
317 (template
317 (symbol 'rev'))
318 (symbol 'rev'))
318 >>> f((b'template', [(b'template', [(b'string', b'foo')])]))
319 >>> f((b'template', [(b'template', [(b'string', b'foo')])]))
319 (string 'foo')
320 (string 'foo')
320 """
321 """
321 if not isinstance(tree, tuple):
322 if not isinstance(tree, tuple):
322 return tree
323 return tree
323 op = tree[0]
324 op = tree[0]
324 if op != 'template':
325 if op != 'template':
325 return (op,) + tuple(_unnesttemplatelist(x) for x in tree[1:])
326 return (op,) + tuple(_unnesttemplatelist(x) for x in tree[1:])
326
327
327 assert len(tree) == 2
328 assert len(tree) == 2
328 xs = tuple(_unnesttemplatelist(x) for x in tree[1])
329 xs = tuple(_unnesttemplatelist(x) for x in tree[1])
329 if not xs:
330 if not xs:
330 return ('string', '') # empty template ""
331 return ('string', '') # empty template ""
331 elif len(xs) == 1 and xs[0][0] == 'string':
332 elif len(xs) == 1 and xs[0][0] == 'string':
332 return xs[0] # fast path for string with no template fragment "x"
333 return xs[0] # fast path for string with no template fragment "x"
333 else:
334 else:
334 return (op,) + xs
335 return (op,) + xs
335
336
336 def parse(tmpl):
337 def parse(tmpl):
337 """Parse template string into tree"""
338 """Parse template string into tree"""
338 parsed, pos = _parsetemplate(tmpl, 0, len(tmpl))
339 parsed, pos = _parsetemplate(tmpl, 0, len(tmpl))
339 assert pos == len(tmpl), 'unquoted template should be consumed'
340 assert pos == len(tmpl), 'unquoted template should be consumed'
340 return _unnesttemplatelist(('template', parsed))
341 return _unnesttemplatelist(('template', parsed))
341
342
342 def _parseexpr(expr):
343 def _parseexpr(expr):
343 """Parse a template expression into tree
344 """Parse a template expression into tree
344
345
345 >>> _parseexpr(b'"foo"')
346 >>> _parseexpr(b'"foo"')
346 ('string', 'foo')
347 ('string', 'foo')
347 >>> _parseexpr(b'foo(bar)')
348 >>> _parseexpr(b'foo(bar)')
348 ('func', ('symbol', 'foo'), ('symbol', 'bar'))
349 ('func', ('symbol', 'foo'), ('symbol', 'bar'))
349 >>> _parseexpr(b'foo(')
350 >>> _parseexpr(b'foo(')
350 Traceback (most recent call last):
351 Traceback (most recent call last):
351 ...
352 ...
352 ParseError: ('not a prefix: end', 4)
353 ParseError: ('not a prefix: end', 4)
353 >>> _parseexpr(b'"foo" "bar"')
354 >>> _parseexpr(b'"foo" "bar"')
354 Traceback (most recent call last):
355 Traceback (most recent call last):
355 ...
356 ...
356 ParseError: ('invalid token', 7)
357 ParseError: ('invalid token', 7)
357 """
358 """
358 p = parser.parser(elements)
359 p = parser.parser(elements)
359 tree, pos = p.parse(tokenize(expr, 0, len(expr)))
360 tree, pos = p.parse(tokenize(expr, 0, len(expr)))
360 if pos != len(expr):
361 if pos != len(expr):
361 raise error.ParseError(_('invalid token'), pos)
362 raise error.ParseError(_('invalid token'), pos)
362 return _unnesttemplatelist(tree)
363 return _unnesttemplatelist(tree)
363
364
364 def prettyformat(tree):
365 def prettyformat(tree):
365 return parser.prettyformat(tree, ('integer', 'string', 'symbol'))
366 return parser.prettyformat(tree, ('integer', 'string', 'symbol'))
366
367
367 def compileexp(exp, context, curmethods):
368 def compileexp(exp, context, curmethods):
368 """Compile parsed template tree to (func, data) pair"""
369 """Compile parsed template tree to (func, data) pair"""
369 if not exp:
370 if not exp:
370 raise error.ParseError(_("missing argument"))
371 raise error.ParseError(_("missing argument"))
371 t = exp[0]
372 t = exp[0]
372 if t in curmethods:
373 if t in curmethods:
373 return curmethods[t](exp, context)
374 return curmethods[t](exp, context)
374 raise error.ParseError(_("unknown method '%s'") % t)
375 raise error.ParseError(_("unknown method '%s'") % t)
375
376
376 # template evaluation
377 # template evaluation
377
378
378 def getsymbol(exp):
379 def getsymbol(exp):
379 if exp[0] == 'symbol':
380 if exp[0] == 'symbol':
380 return exp[1]
381 return exp[1]
381 raise error.ParseError(_("expected a symbol, got '%s'") % exp[0])
382 raise error.ParseError(_("expected a symbol, got '%s'") % exp[0])
382
383
383 def getlist(x):
384 def getlist(x):
384 if not x:
385 if not x:
385 return []
386 return []
386 if x[0] == 'list':
387 if x[0] == 'list':
387 return getlist(x[1]) + [x[2]]
388 return getlist(x[1]) + [x[2]]
388 return [x]
389 return [x]
389
390
390 def gettemplate(exp, context):
391 def gettemplate(exp, context):
391 """Compile given template tree or load named template from map file;
392 """Compile given template tree or load named template from map file;
392 returns (func, data) pair"""
393 returns (func, data) pair"""
393 if exp[0] in ('template', 'string'):
394 if exp[0] in ('template', 'string'):
394 return compileexp(exp, context, methods)
395 return compileexp(exp, context, methods)
395 if exp[0] == 'symbol':
396 if exp[0] == 'symbol':
396 # unlike runsymbol(), here 'symbol' is always taken as template name
397 # unlike runsymbol(), here 'symbol' is always taken as template name
397 # even if it exists in mapping. this allows us to override mapping
398 # even if it exists in mapping. this allows us to override mapping
398 # by web templates, e.g. 'changelogtag' is redefined in map file.
399 # by web templates, e.g. 'changelogtag' is redefined in map file.
399 return context._load(exp[1])
400 return context._load(exp[1])
400 raise error.ParseError(_("expected template specifier"))
401 raise error.ParseError(_("expected template specifier"))
401
402
402 def _runrecursivesymbol(context, mapping, key):
403 def _runrecursivesymbol(context, mapping, key):
403 raise error.Abort(_("recursive reference '%s' in template") % key)
404 raise error.Abort(_("recursive reference '%s' in template") % key)
404
405
405 def buildtemplate(exp, context):
406 def buildtemplate(exp, context):
406 ctmpl = [compileexp(e, context, methods) for e in exp[1:]]
407 ctmpl = [compileexp(e, context, methods) for e in exp[1:]]
407 return (templateutil.runtemplate, ctmpl)
408 return (templateutil.runtemplate, ctmpl)
408
409
409 def buildfilter(exp, context):
410 def buildfilter(exp, context):
410 n = getsymbol(exp[2])
411 n = getsymbol(exp[2])
411 if n in context._filters:
412 if n in context._filters:
412 filt = context._filters[n]
413 filt = context._filters[n]
413 arg = compileexp(exp[1], context, methods)
414 arg = compileexp(exp[1], context, methods)
414 return (templateutil.runfilter, (arg, filt))
415 return (templateutil.runfilter, (arg, filt))
415 if n in context._funcs:
416 if n in context._funcs:
416 f = context._funcs[n]
417 f = context._funcs[n]
417 args = _buildfuncargs(exp[1], context, methods, n, f._argspec)
418 args = _buildfuncargs(exp[1], context, methods, n, f._argspec)
418 return (f, args)
419 return (f, args)
419 raise error.ParseError(_("unknown function '%s'") % n)
420 raise error.ParseError(_("unknown function '%s'") % n)
420
421
421 def buildmap(exp, context):
422 def buildmap(exp, context):
422 darg = compileexp(exp[1], context, methods)
423 darg = compileexp(exp[1], context, methods)
423 targ = gettemplate(exp[2], context)
424 targ = gettemplate(exp[2], context)
424 return (templateutil.runmap, (darg, targ))
425 return (templateutil.runmap, (darg, targ))
425
426
426 def buildmember(exp, context):
427 def buildmember(exp, context):
427 darg = compileexp(exp[1], context, methods)
428 darg = compileexp(exp[1], context, methods)
428 memb = getsymbol(exp[2])
429 memb = getsymbol(exp[2])
429 return (templateutil.runmember, (darg, memb))
430 return (templateutil.runmember, (darg, memb))
430
431
431 def buildnegate(exp, context):
432 def buildnegate(exp, context):
432 arg = compileexp(exp[1], context, exprmethods)
433 arg = compileexp(exp[1], context, exprmethods)
433 return (templateutil.runnegate, arg)
434 return (templateutil.runnegate, arg)
434
435
435 def buildarithmetic(exp, context, func):
436 def buildarithmetic(exp, context, func):
436 left = compileexp(exp[1], context, exprmethods)
437 left = compileexp(exp[1], context, exprmethods)
437 right = compileexp(exp[2], context, exprmethods)
438 right = compileexp(exp[2], context, exprmethods)
438 return (templateutil.runarithmetic, (func, left, right))
439 return (templateutil.runarithmetic, (func, left, right))
439
440
440 def buildfunc(exp, context):
441 def buildfunc(exp, context):
441 n = getsymbol(exp[1])
442 n = getsymbol(exp[1])
442 if n in context._funcs:
443 if n in context._funcs:
443 f = context._funcs[n]
444 f = context._funcs[n]
444 args = _buildfuncargs(exp[2], context, exprmethods, n, f._argspec)
445 args = _buildfuncargs(exp[2], context, exprmethods, n, f._argspec)
445 return (f, args)
446 return (f, args)
446 if n in context._filters:
447 if n in context._filters:
447 args = _buildfuncargs(exp[2], context, exprmethods, n, argspec=None)
448 args = _buildfuncargs(exp[2], context, exprmethods, n, argspec=None)
448 if len(args) != 1:
449 if len(args) != 1:
449 raise error.ParseError(_("filter %s expects one argument") % n)
450 raise error.ParseError(_("filter %s expects one argument") % n)
450 f = context._filters[n]
451 f = context._filters[n]
451 return (templateutil.runfilter, (args[0], f))
452 return (templateutil.runfilter, (args[0], f))
452 raise error.ParseError(_("unknown function '%s'") % n)
453 raise error.ParseError(_("unknown function '%s'") % n)
453
454
454 def _buildfuncargs(exp, context, curmethods, funcname, argspec):
455 def _buildfuncargs(exp, context, curmethods, funcname, argspec):
455 """Compile parsed tree of function arguments into list or dict of
456 """Compile parsed tree of function arguments into list or dict of
456 (func, data) pairs
457 (func, data) pairs
457
458
458 >>> context = engine(lambda t: (templateutil.runsymbol, t))
459 >>> context = engine(lambda t: (templateutil.runsymbol, t))
459 >>> def fargs(expr, argspec):
460 >>> def fargs(expr, argspec):
460 ... x = _parseexpr(expr)
461 ... x = _parseexpr(expr)
461 ... n = getsymbol(x[1])
462 ... n = getsymbol(x[1])
462 ... return _buildfuncargs(x[2], context, exprmethods, n, argspec)
463 ... return _buildfuncargs(x[2], context, exprmethods, n, argspec)
463 >>> list(fargs(b'a(l=1, k=2)', b'k l m').keys())
464 >>> list(fargs(b'a(l=1, k=2)', b'k l m').keys())
464 ['l', 'k']
465 ['l', 'k']
465 >>> args = fargs(b'a(opts=1, k=2)', b'**opts')
466 >>> args = fargs(b'a(opts=1, k=2)', b'**opts')
466 >>> list(args.keys()), list(args[b'opts'].keys())
467 >>> list(args.keys()), list(args[b'opts'].keys())
467 (['opts'], ['opts', 'k'])
468 (['opts'], ['opts', 'k'])
468 """
469 """
469 def compiledict(xs):
470 def compiledict(xs):
470 return util.sortdict((k, compileexp(x, context, curmethods))
471 return util.sortdict((k, compileexp(x, context, curmethods))
471 for k, x in xs.iteritems())
472 for k, x in xs.iteritems())
472 def compilelist(xs):
473 def compilelist(xs):
473 return [compileexp(x, context, curmethods) for x in xs]
474 return [compileexp(x, context, curmethods) for x in xs]
474
475
475 if not argspec:
476 if not argspec:
476 # filter or function with no argspec: return list of positional args
477 # filter or function with no argspec: return list of positional args
477 return compilelist(getlist(exp))
478 return compilelist(getlist(exp))
478
479
479 # function with argspec: return dict of named args
480 # function with argspec: return dict of named args
480 _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec)
481 _poskeys, varkey, _keys, optkey = argspec = parser.splitargspec(argspec)
481 treeargs = parser.buildargsdict(getlist(exp), funcname, argspec,
482 treeargs = parser.buildargsdict(getlist(exp), funcname, argspec,
482 keyvaluenode='keyvalue', keynode='symbol')
483 keyvaluenode='keyvalue', keynode='symbol')
483 compargs = util.sortdict()
484 compargs = util.sortdict()
484 if varkey:
485 if varkey:
485 compargs[varkey] = compilelist(treeargs.pop(varkey))
486 compargs[varkey] = compilelist(treeargs.pop(varkey))
486 if optkey:
487 if optkey:
487 compargs[optkey] = compiledict(treeargs.pop(optkey))
488 compargs[optkey] = compiledict(treeargs.pop(optkey))
488 compargs.update(compiledict(treeargs))
489 compargs.update(compiledict(treeargs))
489 return compargs
490 return compargs
490
491
491 def buildkeyvaluepair(exp, content):
492 def buildkeyvaluepair(exp, content):
492 raise error.ParseError(_("can't use a key-value pair in this context"))
493 raise error.ParseError(_("can't use a key-value pair in this context"))
493
494
494 # methods to interpret function arguments or inner expressions (e.g. {_(x)})
495 # methods to interpret function arguments or inner expressions (e.g. {_(x)})
495 exprmethods = {
496 exprmethods = {
496 "integer": lambda e, c: (templateutil.runinteger, e[1]),
497 "integer": lambda e, c: (templateutil.runinteger, e[1]),
497 "string": lambda e, c: (templateutil.runstring, e[1]),
498 "string": lambda e, c: (templateutil.runstring, e[1]),
498 "symbol": lambda e, c: (templateutil.runsymbol, e[1]),
499 "symbol": lambda e, c: (templateutil.runsymbol, e[1]),
499 "template": buildtemplate,
500 "template": buildtemplate,
500 "group": lambda e, c: compileexp(e[1], c, exprmethods),
501 "group": lambda e, c: compileexp(e[1], c, exprmethods),
501 ".": buildmember,
502 ".": buildmember,
502 "|": buildfilter,
503 "|": buildfilter,
503 "%": buildmap,
504 "%": buildmap,
504 "func": buildfunc,
505 "func": buildfunc,
505 "keyvalue": buildkeyvaluepair,
506 "keyvalue": buildkeyvaluepair,
506 "+": lambda e, c: buildarithmetic(e, c, lambda a, b: a + b),
507 "+": lambda e, c: buildarithmetic(e, c, lambda a, b: a + b),
507 "-": lambda e, c: buildarithmetic(e, c, lambda a, b: a - b),
508 "-": lambda e, c: buildarithmetic(e, c, lambda a, b: a - b),
508 "negate": buildnegate,
509 "negate": buildnegate,
509 "*": lambda e, c: buildarithmetic(e, c, lambda a, b: a * b),
510 "*": lambda e, c: buildarithmetic(e, c, lambda a, b: a * b),
510 "/": lambda e, c: buildarithmetic(e, c, lambda a, b: a // b),
511 "/": lambda e, c: buildarithmetic(e, c, lambda a, b: a // b),
511 }
512 }
512
513
513 # methods to interpret top-level template (e.g. {x}, {x|_}, {x % "y"})
514 # methods to interpret top-level template (e.g. {x}, {x|_}, {x % "y"})
514 methods = exprmethods.copy()
515 methods = exprmethods.copy()
515 methods["integer"] = exprmethods["symbol"] # '{1}' as variable
516 methods["integer"] = exprmethods["symbol"] # '{1}' as variable
516
517
517 class _aliasrules(parser.basealiasrules):
518 class _aliasrules(parser.basealiasrules):
518 """Parsing and expansion rule set of template aliases"""
519 """Parsing and expansion rule set of template aliases"""
519 _section = _('template alias')
520 _section = _('template alias')
520 _parse = staticmethod(_parseexpr)
521 _parse = staticmethod(_parseexpr)
521
522
522 @staticmethod
523 @staticmethod
523 def _trygetfunc(tree):
524 def _trygetfunc(tree):
524 """Return (name, args) if tree is func(...) or ...|filter; otherwise
525 """Return (name, args) if tree is func(...) or ...|filter; otherwise
525 None"""
526 None"""
526 if tree[0] == 'func' and tree[1][0] == 'symbol':
527 if tree[0] == 'func' and tree[1][0] == 'symbol':
527 return tree[1][1], getlist(tree[2])
528 return tree[1][1], getlist(tree[2])
528 if tree[0] == '|' and tree[2][0] == 'symbol':
529 if tree[0] == '|' and tree[2][0] == 'symbol':
529 return tree[2][1], [tree[1]]
530 return tree[2][1], [tree[1]]
530
531
531 def expandaliases(tree, aliases):
532 def expandaliases(tree, aliases):
532 """Return new tree of aliases are expanded"""
533 """Return new tree of aliases are expanded"""
533 aliasmap = _aliasrules.buildmap(aliases)
534 aliasmap = _aliasrules.buildmap(aliases)
534 return _aliasrules.expand(aliasmap, tree)
535 return _aliasrules.expand(aliasmap, tree)
535
536
536 # template engine
537 # template engine
537
538
538 def unquotestring(s):
539 def unquotestring(s):
539 '''unwrap quotes if any; otherwise returns unmodified string'''
540 '''unwrap quotes if any; otherwise returns unmodified string'''
540 if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]:
541 if len(s) < 2 or s[0] not in "'\"" or s[0] != s[-1]:
541 return s
542 return s
542 return s[1:-1]
543 return s[1:-1]
543
544
544 class resourcemapper(object):
545 class resourcemapper(object):
545 """Mapper of internal template resources"""
546 """Mapper of internal template resources"""
546
547
547 __metaclass__ = abc.ABCMeta
548 __metaclass__ = abc.ABCMeta
548
549
549 @abc.abstractmethod
550 @abc.abstractmethod
550 def availablekeys(self, context, mapping):
551 def availablekeys(self, context, mapping):
551 """Return a set of available resource keys based on the given mapping"""
552 """Return a set of available resource keys based on the given mapping"""
552
553
553 @abc.abstractmethod
554 @abc.abstractmethod
554 def knownkeys(self):
555 def knownkeys(self):
555 """Return a set of supported resource keys"""
556 """Return a set of supported resource keys"""
556
557
557 @abc.abstractmethod
558 @abc.abstractmethod
558 def lookup(self, context, mapping, key):
559 def lookup(self, context, mapping, key):
559 """Return a resource for the key if available; otherwise None"""
560 """Return a resource for the key if available; otherwise None"""
560
561
561 @abc.abstractmethod
562 @abc.abstractmethod
562 def populatemap(self, context, origmapping, newmapping):
563 def populatemap(self, context, origmapping, newmapping):
563 """Return a dict of additional mapping items which should be paired
564 """Return a dict of additional mapping items which should be paired
564 with the given new mapping"""
565 with the given new mapping"""
565
566
566 class nullresourcemapper(resourcemapper):
567 class nullresourcemapper(resourcemapper):
567 def availablekeys(self, context, mapping):
568 def availablekeys(self, context, mapping):
568 return set()
569 return set()
569
570
570 def knownkeys(self):
571 def knownkeys(self):
571 return set()
572 return set()
572
573
573 def lookup(self, context, mapping, key):
574 def lookup(self, context, mapping, key):
574 return None
575 return None
575
576
576 def populatemap(self, context, origmapping, newmapping):
577 def populatemap(self, context, origmapping, newmapping):
577 return {}
578 return {}
578
579
579 class engine(object):
580 class engine(object):
580 '''template expansion engine.
581 '''template expansion engine.
581
582
582 template expansion works like this. a map file contains key=value
583 template expansion works like this. a map file contains key=value
583 pairs. if value is quoted, it is treated as string. otherwise, it
584 pairs. if value is quoted, it is treated as string. otherwise, it
584 is treated as name of template file.
585 is treated as name of template file.
585
586
586 templater is asked to expand a key in map. it looks up key, and
587 templater is asked to expand a key in map. it looks up key, and
587 looks for strings like this: {foo}. it expands {foo} by looking up
588 looks for strings like this: {foo}. it expands {foo} by looking up
588 foo in map, and substituting it. expansion is recursive: it stops
589 foo in map, and substituting it. expansion is recursive: it stops
589 when there is no more {foo} to replace.
590 when there is no more {foo} to replace.
590
591
591 expansion also allows formatting and filtering.
592 expansion also allows formatting and filtering.
592
593
593 format uses key to expand each item in list. syntax is
594 format uses key to expand each item in list. syntax is
594 {key%format}.
595 {key%format}.
595
596
596 filter uses function to transform value. syntax is
597 filter uses function to transform value. syntax is
597 {key|filter1|filter2|...}.'''
598 {key|filter1|filter2|...}.'''
598
599
599 def __init__(self, loader, filters=None, defaults=None, resources=None,
600 def __init__(self, loader, filters=None, defaults=None, resources=None,
600 aliases=()):
601 aliases=()):
601 self._loader = loader
602 self._loader = loader
602 if filters is None:
603 if filters is None:
603 filters = {}
604 filters = {}
604 self._filters = filters
605 self._filters = filters
605 self._funcs = templatefuncs.funcs # make this a parameter if needed
606 self._funcs = templatefuncs.funcs # make this a parameter if needed
606 if defaults is None:
607 if defaults is None:
607 defaults = {}
608 defaults = {}
608 if resources is None:
609 if resources is None:
609 resources = nullresourcemapper()
610 resources = nullresourcemapper()
610 self._defaults = defaults
611 self._defaults = defaults
611 self._resources = resources
612 self._resources = resources
612 self._aliasmap = _aliasrules.buildmap(aliases)
613 self._aliasmap = _aliasrules.buildmap(aliases)
613 self._cache = {} # key: (func, data)
614 self._cache = {} # key: (func, data)
614 self._tmplcache = {} # literal template: (func, data)
615 self._tmplcache = {} # literal template: (func, data)
615
616
616 def overlaymap(self, origmapping, newmapping):
617 def overlaymap(self, origmapping, newmapping):
617 """Create combined mapping from the original mapping and partial
618 """Create combined mapping from the original mapping and partial
618 mapping to override the original"""
619 mapping to override the original"""
619 # do not copy symbols which overrides the defaults depending on
620 # do not copy symbols which overrides the defaults depending on
620 # new resources, so the defaults will be re-evaluated (issue5612)
621 # new resources, so the defaults will be re-evaluated (issue5612)
621 knownres = self._resources.knownkeys()
622 knownres = self._resources.knownkeys()
622 newres = self._resources.availablekeys(self, newmapping)
623 newres = self._resources.availablekeys(self, newmapping)
623 mapping = {k: v for k, v in origmapping.iteritems()
624 mapping = {k: v for k, v in origmapping.iteritems()
624 if (k in knownres # not a symbol per self.symbol()
625 if (k in knownres # not a symbol per self.symbol()
625 or newres.isdisjoint(self._defaultrequires(k)))}
626 or newres.isdisjoint(self._defaultrequires(k)))}
626 mapping.update(newmapping)
627 mapping.update(newmapping)
627 mapping.update(
628 mapping.update(
628 self._resources.populatemap(self, origmapping, newmapping))
629 self._resources.populatemap(self, origmapping, newmapping))
629 return mapping
630 return mapping
630
631
631 def _defaultrequires(self, key):
632 def _defaultrequires(self, key):
632 """Resource keys required by the specified default symbol function"""
633 """Resource keys required by the specified default symbol function"""
633 v = self._defaults.get(key)
634 v = self._defaults.get(key)
634 if v is None or not callable(v):
635 if v is None or not callable(v):
635 return ()
636 return ()
636 return getattr(v, '_requires', ())
637 return getattr(v, '_requires', ())
637
638
638 def symbol(self, mapping, key):
639 def symbol(self, mapping, key):
639 """Resolve symbol to value or function; None if nothing found"""
640 """Resolve symbol to value or function; None if nothing found"""
640 v = None
641 v = None
641 if key not in self._resources.knownkeys():
642 if key not in self._resources.knownkeys():
642 v = mapping.get(key)
643 v = mapping.get(key)
643 if v is None:
644 if v is None:
644 v = self._defaults.get(key)
645 v = self._defaults.get(key)
645 return v
646 return v
646
647
647 def availableresourcekeys(self, mapping):
648 def availableresourcekeys(self, mapping):
648 """Return a set of available resource keys based on the given mapping"""
649 """Return a set of available resource keys based on the given mapping"""
649 return self._resources.availablekeys(self, mapping)
650 return self._resources.availablekeys(self, mapping)
650
651
651 def knownresourcekeys(self):
652 def knownresourcekeys(self):
652 """Return a set of supported resource keys"""
653 """Return a set of supported resource keys"""
653 return self._resources.knownkeys()
654 return self._resources.knownkeys()
654
655
655 def resource(self, mapping, key):
656 def resource(self, mapping, key):
656 """Return internal data (e.g. cache) used for keyword/function
657 """Return internal data (e.g. cache) used for keyword/function
657 evaluation"""
658 evaluation"""
658 v = self._resources.lookup(self, mapping, key)
659 v = self._resources.lookup(self, mapping, key)
659 if v is None:
660 if v is None:
660 raise templateutil.ResourceUnavailable(
661 raise templateutil.ResourceUnavailable(
661 _('template resource not available: %s') % key)
662 _('template resource not available: %s') % key)
662 return v
663 return v
663
664
664 def _load(self, t):
665 def _load(self, t):
665 '''load, parse, and cache a template'''
666 '''load, parse, and cache a template'''
666 if t not in self._cache:
667 if t not in self._cache:
667 # put poison to cut recursion while compiling 't'
668 # put poison to cut recursion while compiling 't'
668 self._cache[t] = (_runrecursivesymbol, t)
669 self._cache[t] = (_runrecursivesymbol, t)
669 try:
670 try:
670 x = parse(self._loader(t))
671 x = parse(self._loader(t))
671 if self._aliasmap:
672 if self._aliasmap:
672 x = _aliasrules.expand(self._aliasmap, x)
673 x = _aliasrules.expand(self._aliasmap, x)
673 self._cache[t] = compileexp(x, self, methods)
674 self._cache[t] = compileexp(x, self, methods)
674 except: # re-raises
675 except: # re-raises
675 del self._cache[t]
676 del self._cache[t]
676 raise
677 raise
677 return self._cache[t]
678 return self._cache[t]
678
679
679 def _parse(self, tmpl):
680 def _parse(self, tmpl):
680 """Parse and cache a literal template"""
681 """Parse and cache a literal template"""
681 if tmpl not in self._tmplcache:
682 if tmpl not in self._tmplcache:
682 x = parse(tmpl)
683 x = parse(tmpl)
683 self._tmplcache[tmpl] = compileexp(x, self, methods)
684 self._tmplcache[tmpl] = compileexp(x, self, methods)
684 return self._tmplcache[tmpl]
685 return self._tmplcache[tmpl]
685
686
686 def preload(self, t):
687 def preload(self, t):
687 """Load, parse, and cache the specified template if available"""
688 """Load, parse, and cache the specified template if available"""
688 try:
689 try:
689 self._load(t)
690 self._load(t)
690 return True
691 return True
691 except templateutil.TemplateNotFound:
692 except templateutil.TemplateNotFound:
692 return False
693 return False
693
694
694 def process(self, t, mapping):
695 def process(self, t, mapping):
695 '''Perform expansion. t is name of map element to expand.
696 '''Perform expansion. t is name of map element to expand.
696 mapping contains added elements for use during expansion. Is a
697 mapping contains added elements for use during expansion. Is a
697 generator.'''
698 generator.'''
698 func, data = self._load(t)
699 func, data = self._load(t)
699 return self._expand(func, data, mapping)
700 return self._expand(func, data, mapping)
700
701
701 def expand(self, tmpl, mapping):
702 def expand(self, tmpl, mapping):
702 """Perform expansion over a literal template
703 """Perform expansion over a literal template
703
704
704 No user aliases will be expanded since this is supposed to be called
705 No user aliases will be expanded since this is supposed to be called
705 with an internal template string.
706 with an internal template string.
706 """
707 """
707 func, data = self._parse(tmpl)
708 func, data = self._parse(tmpl)
708 return self._expand(func, data, mapping)
709 return self._expand(func, data, mapping)
709
710
710 def _expand(self, func, data, mapping):
711 def _expand(self, func, data, mapping):
711 # populate additional items only if they don't exist in the given
712 # populate additional items only if they don't exist in the given
712 # mapping. this is slightly different from overlaymap() because the
713 # mapping. this is slightly different from overlaymap() because the
713 # initial 'revcache' may contain pre-computed items.
714 # initial 'revcache' may contain pre-computed items.
714 extramapping = self._resources.populatemap(self, {}, mapping)
715 extramapping = self._resources.populatemap(self, {}, mapping)
715 if extramapping:
716 if extramapping:
716 extramapping.update(mapping)
717 extramapping.update(mapping)
717 mapping = extramapping
718 mapping = extramapping
718 return templateutil.flatten(self, mapping, func(self, mapping, data))
719 return templateutil.flatten(self, mapping, func(self, mapping, data))
719
720
720 engines = {'default': engine}
721 engines = {'default': engine}
721
722
722 def stylelist():
723 def stylelist():
723 paths = templatepaths()
724 paths = templatepaths()
724 if not paths:
725 if not paths:
725 return _('no templates found, try `hg debuginstall` for more info')
726 return _('no templates found, try `hg debuginstall` for more info')
726 dirlist = os.listdir(paths[0])
727 dirlist = os.listdir(paths[0])
727 stylelist = []
728 stylelist = []
728 for file in dirlist:
729 for file in dirlist:
729 split = file.split(".")
730 split = file.split(".")
730 if split[-1] in ('orig', 'rej'):
731 if split[-1] in ('orig', 'rej'):
731 continue
732 continue
732 if split[0] == "map-cmdline":
733 if split[0] == "map-cmdline":
733 stylelist.append(split[1])
734 stylelist.append(split[1])
734 return ", ".join(sorted(stylelist))
735 return ", ".join(sorted(stylelist))
735
736
736 def _readmapfile(mapfile):
737 def _readmapfile(mapfile):
737 """Load template elements from the given map file"""
738 """Load template elements from the given map file"""
738 if not os.path.exists(mapfile):
739 if not os.path.exists(mapfile):
739 raise error.Abort(_("style '%s' not found") % mapfile,
740 raise error.Abort(_("style '%s' not found") % mapfile,
740 hint=_("available styles: %s") % stylelist())
741 hint=_("available styles: %s") % stylelist())
741
742
742 base = os.path.dirname(mapfile)
743 base = os.path.dirname(mapfile)
743 conf = config.config(includepaths=templatepaths())
744 conf = config.config(includepaths=templatepaths())
744 conf.read(mapfile, remap={'': 'templates'})
745 conf.read(mapfile, remap={'': 'templates'})
745
746
746 cache = {}
747 cache = {}
747 tmap = {}
748 tmap = {}
748 aliases = []
749 aliases = []
749
750
750 val = conf.get('templates', '__base__')
751 val = conf.get('templates', '__base__')
751 if val and val[0] not in "'\"":
752 if val and val[0] not in "'\"":
752 # treat as a pointer to a base class for this style
753 # treat as a pointer to a base class for this style
753 path = util.normpath(os.path.join(base, val))
754 path = util.normpath(os.path.join(base, val))
754
755
755 # fallback check in template paths
756 # fallback check in template paths
756 if not os.path.exists(path):
757 if not os.path.exists(path):
757 for p in templatepaths():
758 for p in templatepaths():
758 p2 = util.normpath(os.path.join(p, val))
759 p2 = util.normpath(os.path.join(p, val))
759 if os.path.isfile(p2):
760 if os.path.isfile(p2):
760 path = p2
761 path = p2
761 break
762 break
762 p3 = util.normpath(os.path.join(p2, "map"))
763 p3 = util.normpath(os.path.join(p2, "map"))
763 if os.path.isfile(p3):
764 if os.path.isfile(p3):
764 path = p3
765 path = p3
765 break
766 break
766
767
767 cache, tmap, aliases = _readmapfile(path)
768 cache, tmap, aliases = _readmapfile(path)
768
769
769 for key, val in conf['templates'].items():
770 for key, val in conf['templates'].items():
770 if not val:
771 if not val:
771 raise error.ParseError(_('missing value'),
772 raise error.ParseError(_('missing value'),
772 conf.source('templates', key))
773 conf.source('templates', key))
773 if val[0] in "'\"":
774 if val[0] in "'\"":
774 if val[0] != val[-1]:
775 if val[0] != val[-1]:
775 raise error.ParseError(_('unmatched quotes'),
776 raise error.ParseError(_('unmatched quotes'),
776 conf.source('templates', key))
777 conf.source('templates', key))
777 cache[key] = unquotestring(val)
778 cache[key] = unquotestring(val)
778 elif key != '__base__':
779 elif key != '__base__':
779 val = 'default', val
780 val = 'default', val
780 if ':' in val[1]:
781 if ':' in val[1]:
781 val = val[1].split(':', 1)
782 val = val[1].split(':', 1)
782 tmap[key] = val[0], os.path.join(base, val[1])
783 tmap[key] = val[0], os.path.join(base, val[1])
783 aliases.extend(conf['templatealias'].items())
784 aliases.extend(conf['templatealias'].items())
784 return cache, tmap, aliases
785 return cache, tmap, aliases
785
786
786 class templater(object):
787 class templater(object):
787
788
788 def __init__(self, filters=None, defaults=None, resources=None,
789 def __init__(self, filters=None, defaults=None, resources=None,
789 cache=None, aliases=(), minchunk=1024, maxchunk=65536):
790 cache=None, aliases=(), minchunk=1024, maxchunk=65536):
790 """Create template engine optionally with preloaded template fragments
791 """Create template engine optionally with preloaded template fragments
791
792
792 - ``filters``: a dict of functions to transform a value into another.
793 - ``filters``: a dict of functions to transform a value into another.
793 - ``defaults``: a dict of symbol values/functions; may be overridden
794 - ``defaults``: a dict of symbol values/functions; may be overridden
794 by a ``mapping`` dict.
795 by a ``mapping`` dict.
795 - ``resources``: a resourcemapper object to look up internal data
796 - ``resources``: a resourcemapper object to look up internal data
796 (e.g. cache), inaccessible from user template.
797 (e.g. cache), inaccessible from user template.
797 - ``cache``: a dict of preloaded template fragments.
798 - ``cache``: a dict of preloaded template fragments.
798 - ``aliases``: a list of alias (name, replacement) pairs.
799 - ``aliases``: a list of alias (name, replacement) pairs.
799
800
800 self.cache may be updated later to register additional template
801 self.cache may be updated later to register additional template
801 fragments.
802 fragments.
802 """
803 """
803 if filters is None:
804 if filters is None:
804 filters = {}
805 filters = {}
805 if defaults is None:
806 if defaults is None:
806 defaults = {}
807 defaults = {}
807 if cache is None:
808 if cache is None:
808 cache = {}
809 cache = {}
809 self.cache = cache.copy()
810 self.cache = cache.copy()
810 self.map = {}
811 self.map = {}
811 self.filters = templatefilters.filters.copy()
812 self.filters = templatefilters.filters.copy()
812 self.filters.update(filters)
813 self.filters.update(filters)
813 self.defaults = defaults
814 self.defaults = defaults
814 self._resources = resources
815 self._resources = resources
815 self._aliases = aliases
816 self._aliases = aliases
816 self.minchunk, self.maxchunk = minchunk, maxchunk
817 self.minchunk, self.maxchunk = minchunk, maxchunk
817 self.ecache = {}
818 self.ecache = {}
818
819
819 @classmethod
820 @classmethod
820 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None,
821 def frommapfile(cls, mapfile, filters=None, defaults=None, resources=None,
821 cache=None, minchunk=1024, maxchunk=65536):
822 cache=None, minchunk=1024, maxchunk=65536):
822 """Create templater from the specified map file"""
823 """Create templater from the specified map file"""
823 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk)
824 t = cls(filters, defaults, resources, cache, [], minchunk, maxchunk)
824 cache, tmap, aliases = _readmapfile(mapfile)
825 cache, tmap, aliases = _readmapfile(mapfile)
825 t.cache.update(cache)
826 t.cache.update(cache)
826 t.map = tmap
827 t.map = tmap
827 t._aliases = aliases
828 t._aliases = aliases
828 return t
829 return t
829
830
830 def __contains__(self, key):
831 def __contains__(self, key):
831 return key in self.cache or key in self.map
832 return key in self.cache or key in self.map
832
833
833 def load(self, t):
834 def load(self, t):
834 '''Get the template for the given template name. Use a local cache.'''
835 '''Get the template for the given template name. Use a local cache.'''
835 if t not in self.cache:
836 if t not in self.cache:
836 try:
837 try:
837 self.cache[t] = util.readfile(self.map[t][1])
838 self.cache[t] = util.readfile(self.map[t][1])
838 except KeyError as inst:
839 except KeyError as inst:
839 raise templateutil.TemplateNotFound(
840 raise templateutil.TemplateNotFound(
840 _('"%s" not in template map') % inst.args[0])
841 _('"%s" not in template map') % inst.args[0])
841 except IOError as inst:
842 except IOError as inst:
842 reason = (_('template file %s: %s')
843 reason = (_('template file %s: %s')
843 % (self.map[t][1],
844 % (self.map[t][1],
844 stringutil.forcebytestr(inst.args[1])))
845 stringutil.forcebytestr(inst.args[1])))
845 raise IOError(inst.args[0], encoding.strfromlocal(reason))
846 raise IOError(inst.args[0], encoding.strfromlocal(reason))
846 return self.cache[t]
847 return self.cache[t]
847
848
848 def renderdefault(self, mapping):
849 def renderdefault(self, mapping):
849 """Render the default unnamed template and return result as string"""
850 """Render the default unnamed template and return result as string"""
850 return self.render('', mapping)
851 return self.render('', mapping)
851
852
852 def render(self, t, mapping):
853 def render(self, t, mapping):
853 """Render the specified named template and return result as string"""
854 """Render the specified named template and return result as string"""
854 return b''.join(self.generate(t, mapping))
855 return b''.join(self.generate(t, mapping))
855
856
856 def generate(self, t, mapping):
857 def generate(self, t, mapping):
857 """Return a generator that renders the specified named template and
858 """Return a generator that renders the specified named template and
858 yields chunks"""
859 yields chunks"""
859 ttype = t in self.map and self.map[t][0] or 'default'
860 ttype = t in self.map and self.map[t][0] or 'default'
860 if ttype not in self.ecache:
861 if ttype not in self.ecache:
861 try:
862 try:
862 ecls = engines[ttype]
863 ecls = engines[ttype]
863 except KeyError:
864 except KeyError:
864 raise error.Abort(_('invalid template engine: %s') % ttype)
865 raise error.Abort(_('invalid template engine: %s') % ttype)
865 self.ecache[ttype] = ecls(self.load, self.filters, self.defaults,
866 self.ecache[ttype] = ecls(self.load, self.filters, self.defaults,
866 self._resources, self._aliases)
867 self._resources, self._aliases)
867 proc = self.ecache[ttype]
868 proc = self.ecache[ttype]
868
869
869 stream = proc.process(t, mapping)
870 stream = proc.process(t, mapping)
870 if self.minchunk:
871 if self.minchunk:
871 stream = util.increasingchunks(stream, min=self.minchunk,
872 stream = util.increasingchunks(stream, min=self.minchunk,
872 max=self.maxchunk)
873 max=self.maxchunk)
873 return stream
874 return stream
874
875
875 def templatepaths():
876 def templatepaths():
876 '''return locations used for template files.'''
877 '''return locations used for template files.'''
877 pathsrel = ['templates']
878 pathsrel = ['templates']
878 paths = [os.path.normpath(os.path.join(util.datapath, f))
879 paths = [os.path.normpath(os.path.join(util.datapath, f))
879 for f in pathsrel]
880 for f in pathsrel]
880 return [p for p in paths if os.path.isdir(p)]
881 return [p for p in paths if os.path.isdir(p)]
881
882
882 def templatepath(name):
883 def templatepath(name):
883 '''return location of template file. returns None if not found.'''
884 '''return location of template file. returns None if not found.'''
884 for p in templatepaths():
885 for p in templatepaths():
885 f = os.path.join(p, name)
886 f = os.path.join(p, name)
886 if os.path.exists(f):
887 if os.path.exists(f):
887 return f
888 return f
888 return None
889 return None
889
890
890 def stylemap(styles, paths=None):
891 def stylemap(styles, paths=None):
891 """Return path to mapfile for a given style.
892 """Return path to mapfile for a given style.
892
893
893 Searches mapfile in the following locations:
894 Searches mapfile in the following locations:
894 1. templatepath/style/map
895 1. templatepath/style/map
895 2. templatepath/map-style
896 2. templatepath/map-style
896 3. templatepath/map
897 3. templatepath/map
897 """
898 """
898
899
899 if paths is None:
900 if paths is None:
900 paths = templatepaths()
901 paths = templatepaths()
901 elif isinstance(paths, bytes):
902 elif isinstance(paths, bytes):
902 paths = [paths]
903 paths = [paths]
903
904
904 if isinstance(styles, bytes):
905 if isinstance(styles, bytes):
905 styles = [styles]
906 styles = [styles]
906
907
907 for style in styles:
908 for style in styles:
908 # only plain name is allowed to honor template paths
909 # only plain name is allowed to honor template paths
909 if (not style
910 if (not style
910 or style in (pycompat.oscurdir, pycompat.ospardir)
911 or style in (pycompat.oscurdir, pycompat.ospardir)
911 or pycompat.ossep in style
912 or pycompat.ossep in style
912 or pycompat.osaltsep and pycompat.osaltsep in style):
913 or pycompat.osaltsep and pycompat.osaltsep in style):
913 continue
914 continue
914 locations = [os.path.join(style, 'map'), 'map-' + style]
915 locations = [os.path.join(style, 'map'), 'map-' + style]
915 locations.append('map')
916 locations.append('map')
916
917
917 for path in paths:
918 for path in paths:
918 for location in locations:
919 for location in locations:
919 mapfile = os.path.join(path, location)
920 mapfile = os.path.join(path, location)
920 if os.path.isfile(mapfile):
921 if os.path.isfile(mapfile):
921 return style, mapfile
922 return style, mapfile
922
923
923 raise RuntimeError("No hgweb templates found in %r" % paths)
924 raise RuntimeError("No hgweb templates found in %r" % paths)
General Comments 0
You need to be logged in to leave comments. Login now