##// END OF EJS Templates
patch: merge _updatedir() into externalpatch()
Patrick Mezard -
r14381:d4192500 default
parent child Browse files
Show More
@@ -1,1807 +1,1779 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 import cStringIO, email.Parser, os, errno, re
9 import cStringIO, email.Parser, os, errno, re
10 import tempfile, zlib
10 import tempfile, zlib
11
11
12 from i18n import _
12 from i18n import _
13 from node import hex, nullid, short
13 from node import hex, nullid, short
14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding
14 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding
15
15
16 gitre = re.compile('diff --git a/(.*) b/(.*)')
16 gitre = re.compile('diff --git a/(.*) b/(.*)')
17
17
18 class PatchError(Exception):
18 class PatchError(Exception):
19 pass
19 pass
20
20
21
21
22 # public functions
22 # public functions
23
23
24 def split(stream):
24 def split(stream):
25 '''return an iterator of individual patches from a stream'''
25 '''return an iterator of individual patches from a stream'''
26 def isheader(line, inheader):
26 def isheader(line, inheader):
27 if inheader and line[0] in (' ', '\t'):
27 if inheader and line[0] in (' ', '\t'):
28 # continuation
28 # continuation
29 return True
29 return True
30 if line[0] in (' ', '-', '+'):
30 if line[0] in (' ', '-', '+'):
31 # diff line - don't check for header pattern in there
31 # diff line - don't check for header pattern in there
32 return False
32 return False
33 l = line.split(': ', 1)
33 l = line.split(': ', 1)
34 return len(l) == 2 and ' ' not in l[0]
34 return len(l) == 2 and ' ' not in l[0]
35
35
36 def chunk(lines):
36 def chunk(lines):
37 return cStringIO.StringIO(''.join(lines))
37 return cStringIO.StringIO(''.join(lines))
38
38
39 def hgsplit(stream, cur):
39 def hgsplit(stream, cur):
40 inheader = True
40 inheader = True
41
41
42 for line in stream:
42 for line in stream:
43 if not line.strip():
43 if not line.strip():
44 inheader = False
44 inheader = False
45 if not inheader and line.startswith('# HG changeset patch'):
45 if not inheader and line.startswith('# HG changeset patch'):
46 yield chunk(cur)
46 yield chunk(cur)
47 cur = []
47 cur = []
48 inheader = True
48 inheader = True
49
49
50 cur.append(line)
50 cur.append(line)
51
51
52 if cur:
52 if cur:
53 yield chunk(cur)
53 yield chunk(cur)
54
54
55 def mboxsplit(stream, cur):
55 def mboxsplit(stream, cur):
56 for line in stream:
56 for line in stream:
57 if line.startswith('From '):
57 if line.startswith('From '):
58 for c in split(chunk(cur[1:])):
58 for c in split(chunk(cur[1:])):
59 yield c
59 yield c
60 cur = []
60 cur = []
61
61
62 cur.append(line)
62 cur.append(line)
63
63
64 if cur:
64 if cur:
65 for c in split(chunk(cur[1:])):
65 for c in split(chunk(cur[1:])):
66 yield c
66 yield c
67
67
68 def mimesplit(stream, cur):
68 def mimesplit(stream, cur):
69 def msgfp(m):
69 def msgfp(m):
70 fp = cStringIO.StringIO()
70 fp = cStringIO.StringIO()
71 g = email.Generator.Generator(fp, mangle_from_=False)
71 g = email.Generator.Generator(fp, mangle_from_=False)
72 g.flatten(m)
72 g.flatten(m)
73 fp.seek(0)
73 fp.seek(0)
74 return fp
74 return fp
75
75
76 for line in stream:
76 for line in stream:
77 cur.append(line)
77 cur.append(line)
78 c = chunk(cur)
78 c = chunk(cur)
79
79
80 m = email.Parser.Parser().parse(c)
80 m = email.Parser.Parser().parse(c)
81 if not m.is_multipart():
81 if not m.is_multipart():
82 yield msgfp(m)
82 yield msgfp(m)
83 else:
83 else:
84 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
84 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
85 for part in m.walk():
85 for part in m.walk():
86 ct = part.get_content_type()
86 ct = part.get_content_type()
87 if ct not in ok_types:
87 if ct not in ok_types:
88 continue
88 continue
89 yield msgfp(part)
89 yield msgfp(part)
90
90
91 def headersplit(stream, cur):
91 def headersplit(stream, cur):
92 inheader = False
92 inheader = False
93
93
94 for line in stream:
94 for line in stream:
95 if not inheader and isheader(line, inheader):
95 if not inheader and isheader(line, inheader):
96 yield chunk(cur)
96 yield chunk(cur)
97 cur = []
97 cur = []
98 inheader = True
98 inheader = True
99 if inheader and not isheader(line, inheader):
99 if inheader and not isheader(line, inheader):
100 inheader = False
100 inheader = False
101
101
102 cur.append(line)
102 cur.append(line)
103
103
104 if cur:
104 if cur:
105 yield chunk(cur)
105 yield chunk(cur)
106
106
107 def remainder(cur):
107 def remainder(cur):
108 yield chunk(cur)
108 yield chunk(cur)
109
109
110 class fiter(object):
110 class fiter(object):
111 def __init__(self, fp):
111 def __init__(self, fp):
112 self.fp = fp
112 self.fp = fp
113
113
114 def __iter__(self):
114 def __iter__(self):
115 return self
115 return self
116
116
117 def next(self):
117 def next(self):
118 l = self.fp.readline()
118 l = self.fp.readline()
119 if not l:
119 if not l:
120 raise StopIteration
120 raise StopIteration
121 return l
121 return l
122
122
123 inheader = False
123 inheader = False
124 cur = []
124 cur = []
125
125
126 mimeheaders = ['content-type']
126 mimeheaders = ['content-type']
127
127
128 if not hasattr(stream, 'next'):
128 if not hasattr(stream, 'next'):
129 # http responses, for example, have readline but not next
129 # http responses, for example, have readline but not next
130 stream = fiter(stream)
130 stream = fiter(stream)
131
131
132 for line in stream:
132 for line in stream:
133 cur.append(line)
133 cur.append(line)
134 if line.startswith('# HG changeset patch'):
134 if line.startswith('# HG changeset patch'):
135 return hgsplit(stream, cur)
135 return hgsplit(stream, cur)
136 elif line.startswith('From '):
136 elif line.startswith('From '):
137 return mboxsplit(stream, cur)
137 return mboxsplit(stream, cur)
138 elif isheader(line, inheader):
138 elif isheader(line, inheader):
139 inheader = True
139 inheader = True
140 if line.split(':', 1)[0].lower() in mimeheaders:
140 if line.split(':', 1)[0].lower() in mimeheaders:
141 # let email parser handle this
141 # let email parser handle this
142 return mimesplit(stream, cur)
142 return mimesplit(stream, cur)
143 elif line.startswith('--- ') and inheader:
143 elif line.startswith('--- ') and inheader:
144 # No evil headers seen by diff start, split by hand
144 # No evil headers seen by diff start, split by hand
145 return headersplit(stream, cur)
145 return headersplit(stream, cur)
146 # Not enough info, keep reading
146 # Not enough info, keep reading
147
147
148 # if we are here, we have a very plain patch
148 # if we are here, we have a very plain patch
149 return remainder(cur)
149 return remainder(cur)
150
150
151 def extract(ui, fileobj):
151 def extract(ui, fileobj):
152 '''extract patch from data read from fileobj.
152 '''extract patch from data read from fileobj.
153
153
154 patch can be a normal patch or contained in an email message.
154 patch can be a normal patch or contained in an email message.
155
155
156 return tuple (filename, message, user, date, branch, node, p1, p2).
156 return tuple (filename, message, user, date, branch, node, p1, p2).
157 Any item in the returned tuple can be None. If filename is None,
157 Any item in the returned tuple can be None. If filename is None,
158 fileobj did not contain a patch. Caller must unlink filename when done.'''
158 fileobj did not contain a patch. Caller must unlink filename when done.'''
159
159
160 # attempt to detect the start of a patch
160 # attempt to detect the start of a patch
161 # (this heuristic is borrowed from quilt)
161 # (this heuristic is borrowed from quilt)
162 diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |'
162 diffre = re.compile(r'^(?:Index:[ \t]|diff[ \t]|RCS file: |'
163 r'retrieving revision [0-9]+(\.[0-9]+)*$|'
163 r'retrieving revision [0-9]+(\.[0-9]+)*$|'
164 r'---[ \t].*?^\+\+\+[ \t]|'
164 r'---[ \t].*?^\+\+\+[ \t]|'
165 r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL)
165 r'\*\*\*[ \t].*?^---[ \t])', re.MULTILINE|re.DOTALL)
166
166
167 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
167 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
168 tmpfp = os.fdopen(fd, 'w')
168 tmpfp = os.fdopen(fd, 'w')
169 try:
169 try:
170 msg = email.Parser.Parser().parse(fileobj)
170 msg = email.Parser.Parser().parse(fileobj)
171
171
172 subject = msg['Subject']
172 subject = msg['Subject']
173 user = msg['From']
173 user = msg['From']
174 if not subject and not user:
174 if not subject and not user:
175 # Not an email, restore parsed headers if any
175 # Not an email, restore parsed headers if any
176 subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n'
176 subject = '\n'.join(': '.join(h) for h in msg.items()) + '\n'
177
177
178 gitsendmail = 'git-send-email' in msg.get('X-Mailer', '')
178 gitsendmail = 'git-send-email' in msg.get('X-Mailer', '')
179 # should try to parse msg['Date']
179 # should try to parse msg['Date']
180 date = None
180 date = None
181 nodeid = None
181 nodeid = None
182 branch = None
182 branch = None
183 parents = []
183 parents = []
184
184
185 if subject:
185 if subject:
186 if subject.startswith('[PATCH'):
186 if subject.startswith('[PATCH'):
187 pend = subject.find(']')
187 pend = subject.find(']')
188 if pend >= 0:
188 if pend >= 0:
189 subject = subject[pend + 1:].lstrip()
189 subject = subject[pend + 1:].lstrip()
190 subject = subject.replace('\n\t', ' ')
190 subject = subject.replace('\n\t', ' ')
191 ui.debug('Subject: %s\n' % subject)
191 ui.debug('Subject: %s\n' % subject)
192 if user:
192 if user:
193 ui.debug('From: %s\n' % user)
193 ui.debug('From: %s\n' % user)
194 diffs_seen = 0
194 diffs_seen = 0
195 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
195 ok_types = ('text/plain', 'text/x-diff', 'text/x-patch')
196 message = ''
196 message = ''
197 for part in msg.walk():
197 for part in msg.walk():
198 content_type = part.get_content_type()
198 content_type = part.get_content_type()
199 ui.debug('Content-Type: %s\n' % content_type)
199 ui.debug('Content-Type: %s\n' % content_type)
200 if content_type not in ok_types:
200 if content_type not in ok_types:
201 continue
201 continue
202 payload = part.get_payload(decode=True)
202 payload = part.get_payload(decode=True)
203 m = diffre.search(payload)
203 m = diffre.search(payload)
204 if m:
204 if m:
205 hgpatch = False
205 hgpatch = False
206 hgpatchheader = False
206 hgpatchheader = False
207 ignoretext = False
207 ignoretext = False
208
208
209 ui.debug('found patch at byte %d\n' % m.start(0))
209 ui.debug('found patch at byte %d\n' % m.start(0))
210 diffs_seen += 1
210 diffs_seen += 1
211 cfp = cStringIO.StringIO()
211 cfp = cStringIO.StringIO()
212 for line in payload[:m.start(0)].splitlines():
212 for line in payload[:m.start(0)].splitlines():
213 if line.startswith('# HG changeset patch') and not hgpatch:
213 if line.startswith('# HG changeset patch') and not hgpatch:
214 ui.debug('patch generated by hg export\n')
214 ui.debug('patch generated by hg export\n')
215 hgpatch = True
215 hgpatch = True
216 hgpatchheader = True
216 hgpatchheader = True
217 # drop earlier commit message content
217 # drop earlier commit message content
218 cfp.seek(0)
218 cfp.seek(0)
219 cfp.truncate()
219 cfp.truncate()
220 subject = None
220 subject = None
221 elif hgpatchheader:
221 elif hgpatchheader:
222 if line.startswith('# User '):
222 if line.startswith('# User '):
223 user = line[7:]
223 user = line[7:]
224 ui.debug('From: %s\n' % user)
224 ui.debug('From: %s\n' % user)
225 elif line.startswith("# Date "):
225 elif line.startswith("# Date "):
226 date = line[7:]
226 date = line[7:]
227 elif line.startswith("# Branch "):
227 elif line.startswith("# Branch "):
228 branch = line[9:]
228 branch = line[9:]
229 elif line.startswith("# Node ID "):
229 elif line.startswith("# Node ID "):
230 nodeid = line[10:]
230 nodeid = line[10:]
231 elif line.startswith("# Parent "):
231 elif line.startswith("# Parent "):
232 parents.append(line[10:])
232 parents.append(line[10:])
233 elif not line.startswith("# "):
233 elif not line.startswith("# "):
234 hgpatchheader = False
234 hgpatchheader = False
235 elif line == '---' and gitsendmail:
235 elif line == '---' and gitsendmail:
236 ignoretext = True
236 ignoretext = True
237 if not hgpatchheader and not ignoretext:
237 if not hgpatchheader and not ignoretext:
238 cfp.write(line)
238 cfp.write(line)
239 cfp.write('\n')
239 cfp.write('\n')
240 message = cfp.getvalue()
240 message = cfp.getvalue()
241 if tmpfp:
241 if tmpfp:
242 tmpfp.write(payload)
242 tmpfp.write(payload)
243 if not payload.endswith('\n'):
243 if not payload.endswith('\n'):
244 tmpfp.write('\n')
244 tmpfp.write('\n')
245 elif not diffs_seen and message and content_type == 'text/plain':
245 elif not diffs_seen and message and content_type == 'text/plain':
246 message += '\n' + payload
246 message += '\n' + payload
247 except:
247 except:
248 tmpfp.close()
248 tmpfp.close()
249 os.unlink(tmpname)
249 os.unlink(tmpname)
250 raise
250 raise
251
251
252 if subject and not message.startswith(subject):
252 if subject and not message.startswith(subject):
253 message = '%s\n%s' % (subject, message)
253 message = '%s\n%s' % (subject, message)
254 tmpfp.close()
254 tmpfp.close()
255 if not diffs_seen:
255 if not diffs_seen:
256 os.unlink(tmpname)
256 os.unlink(tmpname)
257 return None, message, user, date, branch, None, None, None
257 return None, message, user, date, branch, None, None, None
258 p1 = parents and parents.pop(0) or None
258 p1 = parents and parents.pop(0) or None
259 p2 = parents and parents.pop(0) or None
259 p2 = parents and parents.pop(0) or None
260 return tmpname, message, user, date, branch, nodeid, p1, p2
260 return tmpname, message, user, date, branch, nodeid, p1, p2
261
261
262 class patchmeta(object):
262 class patchmeta(object):
263 """Patched file metadata
263 """Patched file metadata
264
264
265 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
265 'op' is the performed operation within ADD, DELETE, RENAME, MODIFY
266 or COPY. 'path' is patched file path. 'oldpath' is set to the
266 or COPY. 'path' is patched file path. 'oldpath' is set to the
267 origin file when 'op' is either COPY or RENAME, None otherwise. If
267 origin file when 'op' is either COPY or RENAME, None otherwise. If
268 file mode is changed, 'mode' is a tuple (islink, isexec) where
268 file mode is changed, 'mode' is a tuple (islink, isexec) where
269 'islink' is True if the file is a symlink and 'isexec' is True if
269 'islink' is True if the file is a symlink and 'isexec' is True if
270 the file is executable. Otherwise, 'mode' is None.
270 the file is executable. Otherwise, 'mode' is None.
271 """
271 """
272 def __init__(self, path):
272 def __init__(self, path):
273 self.path = path
273 self.path = path
274 self.oldpath = None
274 self.oldpath = None
275 self.mode = None
275 self.mode = None
276 self.op = 'MODIFY'
276 self.op = 'MODIFY'
277 self.binary = False
277 self.binary = False
278
278
279 def setmode(self, mode):
279 def setmode(self, mode):
280 islink = mode & 020000
280 islink = mode & 020000
281 isexec = mode & 0100
281 isexec = mode & 0100
282 self.mode = (islink, isexec)
282 self.mode = (islink, isexec)
283
283
284 def __repr__(self):
284 def __repr__(self):
285 return "<patchmeta %s %r>" % (self.op, self.path)
285 return "<patchmeta %s %r>" % (self.op, self.path)
286
286
287 def readgitpatch(lr):
287 def readgitpatch(lr):
288 """extract git-style metadata about patches from <patchname>"""
288 """extract git-style metadata about patches from <patchname>"""
289
289
290 # Filter patch for git information
290 # Filter patch for git information
291 gp = None
291 gp = None
292 gitpatches = []
292 gitpatches = []
293 for line in lr:
293 for line in lr:
294 line = line.rstrip(' \r\n')
294 line = line.rstrip(' \r\n')
295 if line.startswith('diff --git'):
295 if line.startswith('diff --git'):
296 m = gitre.match(line)
296 m = gitre.match(line)
297 if m:
297 if m:
298 if gp:
298 if gp:
299 gitpatches.append(gp)
299 gitpatches.append(gp)
300 dst = m.group(2)
300 dst = m.group(2)
301 gp = patchmeta(dst)
301 gp = patchmeta(dst)
302 elif gp:
302 elif gp:
303 if line.startswith('--- '):
303 if line.startswith('--- '):
304 gitpatches.append(gp)
304 gitpatches.append(gp)
305 gp = None
305 gp = None
306 continue
306 continue
307 if line.startswith('rename from '):
307 if line.startswith('rename from '):
308 gp.op = 'RENAME'
308 gp.op = 'RENAME'
309 gp.oldpath = line[12:]
309 gp.oldpath = line[12:]
310 elif line.startswith('rename to '):
310 elif line.startswith('rename to '):
311 gp.path = line[10:]
311 gp.path = line[10:]
312 elif line.startswith('copy from '):
312 elif line.startswith('copy from '):
313 gp.op = 'COPY'
313 gp.op = 'COPY'
314 gp.oldpath = line[10:]
314 gp.oldpath = line[10:]
315 elif line.startswith('copy to '):
315 elif line.startswith('copy to '):
316 gp.path = line[8:]
316 gp.path = line[8:]
317 elif line.startswith('deleted file'):
317 elif line.startswith('deleted file'):
318 gp.op = 'DELETE'
318 gp.op = 'DELETE'
319 elif line.startswith('new file mode '):
319 elif line.startswith('new file mode '):
320 gp.op = 'ADD'
320 gp.op = 'ADD'
321 gp.setmode(int(line[-6:], 8))
321 gp.setmode(int(line[-6:], 8))
322 elif line.startswith('new mode '):
322 elif line.startswith('new mode '):
323 gp.setmode(int(line[-6:], 8))
323 gp.setmode(int(line[-6:], 8))
324 elif line.startswith('GIT binary patch'):
324 elif line.startswith('GIT binary patch'):
325 gp.binary = True
325 gp.binary = True
326 if gp:
326 if gp:
327 gitpatches.append(gp)
327 gitpatches.append(gp)
328
328
329 return gitpatches
329 return gitpatches
330
330
331 class linereader(object):
331 class linereader(object):
332 # simple class to allow pushing lines back into the input stream
332 # simple class to allow pushing lines back into the input stream
333 def __init__(self, fp, textmode=False):
333 def __init__(self, fp, textmode=False):
334 self.fp = fp
334 self.fp = fp
335 self.buf = []
335 self.buf = []
336 self.textmode = textmode
336 self.textmode = textmode
337 self.eol = None
337 self.eol = None
338
338
339 def push(self, line):
339 def push(self, line):
340 if line is not None:
340 if line is not None:
341 self.buf.append(line)
341 self.buf.append(line)
342
342
343 def readline(self):
343 def readline(self):
344 if self.buf:
344 if self.buf:
345 l = self.buf[0]
345 l = self.buf[0]
346 del self.buf[0]
346 del self.buf[0]
347 return l
347 return l
348 l = self.fp.readline()
348 l = self.fp.readline()
349 if not self.eol:
349 if not self.eol:
350 if l.endswith('\r\n'):
350 if l.endswith('\r\n'):
351 self.eol = '\r\n'
351 self.eol = '\r\n'
352 elif l.endswith('\n'):
352 elif l.endswith('\n'):
353 self.eol = '\n'
353 self.eol = '\n'
354 if self.textmode and l.endswith('\r\n'):
354 if self.textmode and l.endswith('\r\n'):
355 l = l[:-2] + '\n'
355 l = l[:-2] + '\n'
356 return l
356 return l
357
357
358 def __iter__(self):
358 def __iter__(self):
359 while 1:
359 while 1:
360 l = self.readline()
360 l = self.readline()
361 if not l:
361 if not l:
362 break
362 break
363 yield l
363 yield l
364
364
365 class abstractbackend(object):
365 class abstractbackend(object):
366 def __init__(self, ui):
366 def __init__(self, ui):
367 self.ui = ui
367 self.ui = ui
368
368
369 def readlines(self, fname):
369 def readlines(self, fname):
370 """Return target file lines, or its content as a single line
370 """Return target file lines, or its content as a single line
371 for symlinks.
371 for symlinks.
372 """
372 """
373 raise NotImplementedError
373 raise NotImplementedError
374
374
375 def writelines(self, fname, lines, mode):
375 def writelines(self, fname, lines, mode):
376 """Write lines to target file. mode is a (islink, isexec)
376 """Write lines to target file. mode is a (islink, isexec)
377 tuple, or None if there is no mode information.
377 tuple, or None if there is no mode information.
378 """
378 """
379 raise NotImplementedError
379 raise NotImplementedError
380
380
381 def unlink(self, fname):
381 def unlink(self, fname):
382 """Unlink target file."""
382 """Unlink target file."""
383 raise NotImplementedError
383 raise NotImplementedError
384
384
385 def writerej(self, fname, failed, total, lines):
385 def writerej(self, fname, failed, total, lines):
386 """Write rejected lines for fname. total is the number of hunks
386 """Write rejected lines for fname. total is the number of hunks
387 which failed to apply and total the total number of hunks for this
387 which failed to apply and total the total number of hunks for this
388 files.
388 files.
389 """
389 """
390 pass
390 pass
391
391
392 def copy(self, src, dst):
392 def copy(self, src, dst):
393 """Copy src file into dst file. Create intermediate directories if
393 """Copy src file into dst file. Create intermediate directories if
394 necessary. Files are specified relatively to the patching base
394 necessary. Files are specified relatively to the patching base
395 directory.
395 directory.
396 """
396 """
397 raise NotImplementedError
397 raise NotImplementedError
398
398
399 def exists(self, fname):
399 def exists(self, fname):
400 raise NotImplementedError
400 raise NotImplementedError
401
401
402 def setmode(self, fname, islink, isexec):
402 def setmode(self, fname, islink, isexec):
403 """Change target file mode."""
403 """Change target file mode."""
404 raise NotImplementedError
404 raise NotImplementedError
405
405
406 class fsbackend(abstractbackend):
406 class fsbackend(abstractbackend):
407 def __init__(self, ui, basedir):
407 def __init__(self, ui, basedir):
408 super(fsbackend, self).__init__(ui)
408 super(fsbackend, self).__init__(ui)
409 self.opener = scmutil.opener(basedir)
409 self.opener = scmutil.opener(basedir)
410
410
411 def _join(self, f):
411 def _join(self, f):
412 return os.path.join(self.opener.base, f)
412 return os.path.join(self.opener.base, f)
413
413
414 def readlines(self, fname):
414 def readlines(self, fname):
415 if os.path.islink(self._join(fname)):
415 if os.path.islink(self._join(fname)):
416 return [os.readlink(self._join(fname))]
416 return [os.readlink(self._join(fname))]
417 fp = self.opener(fname, 'r')
417 fp = self.opener(fname, 'r')
418 try:
418 try:
419 return list(fp)
419 return list(fp)
420 finally:
420 finally:
421 fp.close()
421 fp.close()
422
422
423 def writelines(self, fname, lines, mode):
423 def writelines(self, fname, lines, mode):
424 if not mode:
424 if not mode:
425 # Preserve mode information
425 # Preserve mode information
426 isexec, islink = False, False
426 isexec, islink = False, False
427 try:
427 try:
428 isexec = os.lstat(self._join(fname)).st_mode & 0100 != 0
428 isexec = os.lstat(self._join(fname)).st_mode & 0100 != 0
429 islink = os.path.islink(self._join(fname))
429 islink = os.path.islink(self._join(fname))
430 except OSError, e:
430 except OSError, e:
431 if e.errno != errno.ENOENT:
431 if e.errno != errno.ENOENT:
432 raise
432 raise
433 else:
433 else:
434 islink, isexec = mode
434 islink, isexec = mode
435 if islink:
435 if islink:
436 self.opener.symlink(''.join(lines), fname)
436 self.opener.symlink(''.join(lines), fname)
437 else:
437 else:
438 self.opener(fname, 'w').writelines(lines)
438 self.opener(fname, 'w').writelines(lines)
439 if isexec:
439 if isexec:
440 util.setflags(self._join(fname), False, True)
440 util.setflags(self._join(fname), False, True)
441
441
442 def unlink(self, fname):
442 def unlink(self, fname):
443 try:
443 try:
444 util.unlinkpath(self._join(fname))
444 util.unlinkpath(self._join(fname))
445 except OSError, inst:
445 except OSError, inst:
446 if inst.errno != errno.ENOENT:
446 if inst.errno != errno.ENOENT:
447 raise
447 raise
448
448
449 def writerej(self, fname, failed, total, lines):
449 def writerej(self, fname, failed, total, lines):
450 fname = fname + ".rej"
450 fname = fname + ".rej"
451 self.ui.warn(
451 self.ui.warn(
452 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
452 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
453 (failed, total, fname))
453 (failed, total, fname))
454 fp = self.opener(fname, 'w')
454 fp = self.opener(fname, 'w')
455 fp.writelines(lines)
455 fp.writelines(lines)
456 fp.close()
456 fp.close()
457
457
458 def copy(self, src, dst):
458 def copy(self, src, dst):
459 basedir = self.opener.base
459 basedir = self.opener.base
460 abssrc, absdst = [scmutil.canonpath(basedir, basedir, x)
460 abssrc, absdst = [scmutil.canonpath(basedir, basedir, x)
461 for x in [src, dst]]
461 for x in [src, dst]]
462 if os.path.lexists(absdst):
462 if os.path.lexists(absdst):
463 raise util.Abort(_("cannot create %s: destination already exists")
463 raise util.Abort(_("cannot create %s: destination already exists")
464 % dst)
464 % dst)
465 dstdir = os.path.dirname(absdst)
465 dstdir = os.path.dirname(absdst)
466 if dstdir and not os.path.isdir(dstdir):
466 if dstdir and not os.path.isdir(dstdir):
467 try:
467 try:
468 os.makedirs(dstdir)
468 os.makedirs(dstdir)
469 except IOError:
469 except IOError:
470 raise util.Abort(
470 raise util.Abort(
471 _("cannot create %s: unable to create destination directory")
471 _("cannot create %s: unable to create destination directory")
472 % dst)
472 % dst)
473 util.copyfile(abssrc, absdst)
473 util.copyfile(abssrc, absdst)
474
474
475 def exists(self, fname):
475 def exists(self, fname):
476 return os.path.lexists(self._join(fname))
476 return os.path.lexists(self._join(fname))
477
477
478 def setmode(self, fname, islink, isexec):
478 def setmode(self, fname, islink, isexec):
479 util.setflags(self._join(fname), islink, isexec)
479 util.setflags(self._join(fname), islink, isexec)
480
480
481 class workingbackend(fsbackend):
481 class workingbackend(fsbackend):
482 def __init__(self, ui, repo, similarity):
482 def __init__(self, ui, repo, similarity):
483 super(workingbackend, self).__init__(ui, repo.root)
483 super(workingbackend, self).__init__(ui, repo.root)
484 self.repo = repo
484 self.repo = repo
485 self.similarity = similarity
485 self.similarity = similarity
486 self.removed = set()
486 self.removed = set()
487 self.changed = set()
487 self.changed = set()
488 self.copied = []
488 self.copied = []
489
489
490 def writelines(self, fname, lines, mode):
490 def writelines(self, fname, lines, mode):
491 super(workingbackend, self).writelines(fname, lines, mode)
491 super(workingbackend, self).writelines(fname, lines, mode)
492 self.changed.add(fname)
492 self.changed.add(fname)
493
493
494 def unlink(self, fname):
494 def unlink(self, fname):
495 super(workingbackend, self).unlink(fname)
495 super(workingbackend, self).unlink(fname)
496 self.removed.add(fname)
496 self.removed.add(fname)
497 self.changed.add(fname)
497 self.changed.add(fname)
498
498
499 def copy(self, src, dst):
499 def copy(self, src, dst):
500 super(workingbackend, self).copy(src, dst)
500 super(workingbackend, self).copy(src, dst)
501 self.copied.append((src, dst))
501 self.copied.append((src, dst))
502 self.changed.add(dst)
502 self.changed.add(dst)
503
503
504 def setmode(self, fname, islink, isexec):
504 def setmode(self, fname, islink, isexec):
505 super(workingbackend, self).setmode(fname, islink, isexec)
505 super(workingbackend, self).setmode(fname, islink, isexec)
506 self.changed.add(fname)
506 self.changed.add(fname)
507
507
508 def close(self):
508 def close(self):
509 wctx = self.repo[None]
509 wctx = self.repo[None]
510 addremoved = set(self.changed)
510 addremoved = set(self.changed)
511 for src, dst in self.copied:
511 for src, dst in self.copied:
512 scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst)
512 scmutil.dirstatecopy(self.ui, self.repo, wctx, src, dst)
513 addremoved.discard(src)
513 addremoved.discard(src)
514 if (not self.similarity) and self.removed:
514 if (not self.similarity) and self.removed:
515 wctx.remove(sorted(self.removed))
515 wctx.remove(sorted(self.removed))
516 if addremoved:
516 if addremoved:
517 cwd = self.repo.getcwd()
517 cwd = self.repo.getcwd()
518 if cwd:
518 if cwd:
519 addremoved = [util.pathto(self.repo.root, cwd, f)
519 addremoved = [util.pathto(self.repo.root, cwd, f)
520 for f in addremoved]
520 for f in addremoved]
521 scmutil.addremove(self.repo, addremoved, similarity=self.similarity)
521 scmutil.addremove(self.repo, addremoved, similarity=self.similarity)
522 return sorted(self.changed)
522 return sorted(self.changed)
523
523
524 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
524 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
525 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@')
525 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@')
526 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
526 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
527 eolmodes = ['strict', 'crlf', 'lf', 'auto']
527 eolmodes = ['strict', 'crlf', 'lf', 'auto']
528
528
529 class patchfile(object):
529 class patchfile(object):
530 def __init__(self, ui, fname, backend, mode, missing=False,
530 def __init__(self, ui, fname, backend, mode, missing=False,
531 eolmode='strict'):
531 eolmode='strict'):
532 self.fname = fname
532 self.fname = fname
533 self.eolmode = eolmode
533 self.eolmode = eolmode
534 self.eol = None
534 self.eol = None
535 self.backend = backend
535 self.backend = backend
536 self.ui = ui
536 self.ui = ui
537 self.lines = []
537 self.lines = []
538 self.exists = False
538 self.exists = False
539 self.missing = missing
539 self.missing = missing
540 self.mode = mode
540 self.mode = mode
541 if not missing:
541 if not missing:
542 try:
542 try:
543 self.lines = self.backend.readlines(fname)
543 self.lines = self.backend.readlines(fname)
544 if self.lines:
544 if self.lines:
545 # Normalize line endings
545 # Normalize line endings
546 if self.lines[0].endswith('\r\n'):
546 if self.lines[0].endswith('\r\n'):
547 self.eol = '\r\n'
547 self.eol = '\r\n'
548 elif self.lines[0].endswith('\n'):
548 elif self.lines[0].endswith('\n'):
549 self.eol = '\n'
549 self.eol = '\n'
550 if eolmode != 'strict':
550 if eolmode != 'strict':
551 nlines = []
551 nlines = []
552 for l in self.lines:
552 for l in self.lines:
553 if l.endswith('\r\n'):
553 if l.endswith('\r\n'):
554 l = l[:-2] + '\n'
554 l = l[:-2] + '\n'
555 nlines.append(l)
555 nlines.append(l)
556 self.lines = nlines
556 self.lines = nlines
557 self.exists = True
557 self.exists = True
558 except IOError:
558 except IOError:
559 pass
559 pass
560 else:
560 else:
561 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
561 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
562
562
563 self.hash = {}
563 self.hash = {}
564 self.dirty = 0
564 self.dirty = 0
565 self.offset = 0
565 self.offset = 0
566 self.skew = 0
566 self.skew = 0
567 self.rej = []
567 self.rej = []
568 self.fileprinted = False
568 self.fileprinted = False
569 self.printfile(False)
569 self.printfile(False)
570 self.hunks = 0
570 self.hunks = 0
571
571
572 def writelines(self, fname, lines, mode):
572 def writelines(self, fname, lines, mode):
573 if self.eolmode == 'auto':
573 if self.eolmode == 'auto':
574 eol = self.eol
574 eol = self.eol
575 elif self.eolmode == 'crlf':
575 elif self.eolmode == 'crlf':
576 eol = '\r\n'
576 eol = '\r\n'
577 else:
577 else:
578 eol = '\n'
578 eol = '\n'
579
579
580 if self.eolmode != 'strict' and eol and eol != '\n':
580 if self.eolmode != 'strict' and eol and eol != '\n':
581 rawlines = []
581 rawlines = []
582 for l in lines:
582 for l in lines:
583 if l and l[-1] == '\n':
583 if l and l[-1] == '\n':
584 l = l[:-1] + eol
584 l = l[:-1] + eol
585 rawlines.append(l)
585 rawlines.append(l)
586 lines = rawlines
586 lines = rawlines
587
587
588 self.backend.writelines(fname, lines, mode)
588 self.backend.writelines(fname, lines, mode)
589
589
590 def printfile(self, warn):
590 def printfile(self, warn):
591 if self.fileprinted:
591 if self.fileprinted:
592 return
592 return
593 if warn or self.ui.verbose:
593 if warn or self.ui.verbose:
594 self.fileprinted = True
594 self.fileprinted = True
595 s = _("patching file %s\n") % self.fname
595 s = _("patching file %s\n") % self.fname
596 if warn:
596 if warn:
597 self.ui.warn(s)
597 self.ui.warn(s)
598 else:
598 else:
599 self.ui.note(s)
599 self.ui.note(s)
600
600
601
601
602 def findlines(self, l, linenum):
602 def findlines(self, l, linenum):
603 # looks through the hash and finds candidate lines. The
603 # looks through the hash and finds candidate lines. The
604 # result is a list of line numbers sorted based on distance
604 # result is a list of line numbers sorted based on distance
605 # from linenum
605 # from linenum
606
606
607 cand = self.hash.get(l, [])
607 cand = self.hash.get(l, [])
608 if len(cand) > 1:
608 if len(cand) > 1:
609 # resort our list of potentials forward then back.
609 # resort our list of potentials forward then back.
610 cand.sort(key=lambda x: abs(x - linenum))
610 cand.sort(key=lambda x: abs(x - linenum))
611 return cand
611 return cand
612
612
613 def write_rej(self):
613 def write_rej(self):
614 # our rejects are a little different from patch(1). This always
614 # our rejects are a little different from patch(1). This always
615 # creates rejects in the same form as the original patch. A file
615 # creates rejects in the same form as the original patch. A file
616 # header is inserted so that you can run the reject through patch again
616 # header is inserted so that you can run the reject through patch again
617 # without having to type the filename.
617 # without having to type the filename.
618 if not self.rej:
618 if not self.rej:
619 return
619 return
620 base = os.path.basename(self.fname)
620 base = os.path.basename(self.fname)
621 lines = ["--- %s\n+++ %s\n" % (base, base)]
621 lines = ["--- %s\n+++ %s\n" % (base, base)]
622 for x in self.rej:
622 for x in self.rej:
623 for l in x.hunk:
623 for l in x.hunk:
624 lines.append(l)
624 lines.append(l)
625 if l[-1] != '\n':
625 if l[-1] != '\n':
626 lines.append("\n\ No newline at end of file\n")
626 lines.append("\n\ No newline at end of file\n")
627 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines)
627 self.backend.writerej(self.fname, len(self.rej), self.hunks, lines)
628
628
629 def apply(self, h):
629 def apply(self, h):
630 if not h.complete():
630 if not h.complete():
631 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") %
631 raise PatchError(_("bad hunk #%d %s (%d %d %d %d)") %
632 (h.number, h.desc, len(h.a), h.lena, len(h.b),
632 (h.number, h.desc, len(h.a), h.lena, len(h.b),
633 h.lenb))
633 h.lenb))
634
634
635 self.hunks += 1
635 self.hunks += 1
636
636
637 if self.missing:
637 if self.missing:
638 self.rej.append(h)
638 self.rej.append(h)
639 return -1
639 return -1
640
640
641 if self.exists and h.createfile():
641 if self.exists and h.createfile():
642 self.ui.warn(_("file %s already exists\n") % self.fname)
642 self.ui.warn(_("file %s already exists\n") % self.fname)
643 self.rej.append(h)
643 self.rej.append(h)
644 return -1
644 return -1
645
645
646 if isinstance(h, binhunk):
646 if isinstance(h, binhunk):
647 if h.rmfile():
647 if h.rmfile():
648 self.backend.unlink(self.fname)
648 self.backend.unlink(self.fname)
649 else:
649 else:
650 self.lines[:] = h.new()
650 self.lines[:] = h.new()
651 self.offset += len(h.new())
651 self.offset += len(h.new())
652 self.dirty = True
652 self.dirty = True
653 return 0
653 return 0
654
654
655 horig = h
655 horig = h
656 if (self.eolmode in ('crlf', 'lf')
656 if (self.eolmode in ('crlf', 'lf')
657 or self.eolmode == 'auto' and self.eol):
657 or self.eolmode == 'auto' and self.eol):
658 # If new eols are going to be normalized, then normalize
658 # If new eols are going to be normalized, then normalize
659 # hunk data before patching. Otherwise, preserve input
659 # hunk data before patching. Otherwise, preserve input
660 # line-endings.
660 # line-endings.
661 h = h.getnormalized()
661 h = h.getnormalized()
662
662
663 # fast case first, no offsets, no fuzz
663 # fast case first, no offsets, no fuzz
664 old = h.old()
664 old = h.old()
665 # patch starts counting at 1 unless we are adding the file
665 # patch starts counting at 1 unless we are adding the file
666 if h.starta == 0:
666 if h.starta == 0:
667 start = 0
667 start = 0
668 else:
668 else:
669 start = h.starta + self.offset - 1
669 start = h.starta + self.offset - 1
670 orig_start = start
670 orig_start = start
671 # if there's skew we want to emit the "(offset %d lines)" even
671 # if there's skew we want to emit the "(offset %d lines)" even
672 # when the hunk cleanly applies at start + skew, so skip the
672 # when the hunk cleanly applies at start + skew, so skip the
673 # fast case code
673 # fast case code
674 if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0:
674 if self.skew == 0 and diffhelpers.testhunk(old, self.lines, start) == 0:
675 if h.rmfile():
675 if h.rmfile():
676 self.backend.unlink(self.fname)
676 self.backend.unlink(self.fname)
677 else:
677 else:
678 self.lines[start : start + h.lena] = h.new()
678 self.lines[start : start + h.lena] = h.new()
679 self.offset += h.lenb - h.lena
679 self.offset += h.lenb - h.lena
680 self.dirty = True
680 self.dirty = True
681 return 0
681 return 0
682
682
683 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it
683 # ok, we couldn't match the hunk. Lets look for offsets and fuzz it
684 self.hash = {}
684 self.hash = {}
685 for x, s in enumerate(self.lines):
685 for x, s in enumerate(self.lines):
686 self.hash.setdefault(s, []).append(x)
686 self.hash.setdefault(s, []).append(x)
687 if h.hunk[-1][0] != ' ':
687 if h.hunk[-1][0] != ' ':
688 # if the hunk tried to put something at the bottom of the file
688 # if the hunk tried to put something at the bottom of the file
689 # override the start line and use eof here
689 # override the start line and use eof here
690 search_start = len(self.lines)
690 search_start = len(self.lines)
691 else:
691 else:
692 search_start = orig_start + self.skew
692 search_start = orig_start + self.skew
693
693
694 for fuzzlen in xrange(3):
694 for fuzzlen in xrange(3):
695 for toponly in [True, False]:
695 for toponly in [True, False]:
696 old = h.old(fuzzlen, toponly)
696 old = h.old(fuzzlen, toponly)
697
697
698 cand = self.findlines(old[0][1:], search_start)
698 cand = self.findlines(old[0][1:], search_start)
699 for l in cand:
699 for l in cand:
700 if diffhelpers.testhunk(old, self.lines, l) == 0:
700 if diffhelpers.testhunk(old, self.lines, l) == 0:
701 newlines = h.new(fuzzlen, toponly)
701 newlines = h.new(fuzzlen, toponly)
702 self.lines[l : l + len(old)] = newlines
702 self.lines[l : l + len(old)] = newlines
703 self.offset += len(newlines) - len(old)
703 self.offset += len(newlines) - len(old)
704 self.skew = l - orig_start
704 self.skew = l - orig_start
705 self.dirty = True
705 self.dirty = True
706 offset = l - orig_start - fuzzlen
706 offset = l - orig_start - fuzzlen
707 if fuzzlen:
707 if fuzzlen:
708 msg = _("Hunk #%d succeeded at %d "
708 msg = _("Hunk #%d succeeded at %d "
709 "with fuzz %d "
709 "with fuzz %d "
710 "(offset %d lines).\n")
710 "(offset %d lines).\n")
711 self.printfile(True)
711 self.printfile(True)
712 self.ui.warn(msg %
712 self.ui.warn(msg %
713 (h.number, l + 1, fuzzlen, offset))
713 (h.number, l + 1, fuzzlen, offset))
714 else:
714 else:
715 msg = _("Hunk #%d succeeded at %d "
715 msg = _("Hunk #%d succeeded at %d "
716 "(offset %d lines).\n")
716 "(offset %d lines).\n")
717 self.ui.note(msg % (h.number, l + 1, offset))
717 self.ui.note(msg % (h.number, l + 1, offset))
718 return fuzzlen
718 return fuzzlen
719 self.printfile(True)
719 self.printfile(True)
720 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
720 self.ui.warn(_("Hunk #%d FAILED at %d\n") % (h.number, orig_start))
721 self.rej.append(horig)
721 self.rej.append(horig)
722 return -1
722 return -1
723
723
724 def close(self):
724 def close(self):
725 if self.dirty:
725 if self.dirty:
726 self.writelines(self.fname, self.lines, self.mode)
726 self.writelines(self.fname, self.lines, self.mode)
727 self.write_rej()
727 self.write_rej()
728 return len(self.rej)
728 return len(self.rej)
729
729
730 class hunk(object):
730 class hunk(object):
731 def __init__(self, desc, num, lr, context, create=False, remove=False):
731 def __init__(self, desc, num, lr, context, create=False, remove=False):
732 self.number = num
732 self.number = num
733 self.desc = desc
733 self.desc = desc
734 self.hunk = [desc]
734 self.hunk = [desc]
735 self.a = []
735 self.a = []
736 self.b = []
736 self.b = []
737 self.starta = self.lena = None
737 self.starta = self.lena = None
738 self.startb = self.lenb = None
738 self.startb = self.lenb = None
739 if lr is not None:
739 if lr is not None:
740 if context:
740 if context:
741 self.read_context_hunk(lr)
741 self.read_context_hunk(lr)
742 else:
742 else:
743 self.read_unified_hunk(lr)
743 self.read_unified_hunk(lr)
744 self.create = create
744 self.create = create
745 self.remove = remove and not create
745 self.remove = remove and not create
746
746
747 def getnormalized(self):
747 def getnormalized(self):
748 """Return a copy with line endings normalized to LF."""
748 """Return a copy with line endings normalized to LF."""
749
749
750 def normalize(lines):
750 def normalize(lines):
751 nlines = []
751 nlines = []
752 for line in lines:
752 for line in lines:
753 if line.endswith('\r\n'):
753 if line.endswith('\r\n'):
754 line = line[:-2] + '\n'
754 line = line[:-2] + '\n'
755 nlines.append(line)
755 nlines.append(line)
756 return nlines
756 return nlines
757
757
758 # Dummy object, it is rebuilt manually
758 # Dummy object, it is rebuilt manually
759 nh = hunk(self.desc, self.number, None, None, False, False)
759 nh = hunk(self.desc, self.number, None, None, False, False)
760 nh.number = self.number
760 nh.number = self.number
761 nh.desc = self.desc
761 nh.desc = self.desc
762 nh.hunk = self.hunk
762 nh.hunk = self.hunk
763 nh.a = normalize(self.a)
763 nh.a = normalize(self.a)
764 nh.b = normalize(self.b)
764 nh.b = normalize(self.b)
765 nh.starta = self.starta
765 nh.starta = self.starta
766 nh.startb = self.startb
766 nh.startb = self.startb
767 nh.lena = self.lena
767 nh.lena = self.lena
768 nh.lenb = self.lenb
768 nh.lenb = self.lenb
769 nh.create = self.create
769 nh.create = self.create
770 nh.remove = self.remove
770 nh.remove = self.remove
771 return nh
771 return nh
772
772
773 def read_unified_hunk(self, lr):
773 def read_unified_hunk(self, lr):
774 m = unidesc.match(self.desc)
774 m = unidesc.match(self.desc)
775 if not m:
775 if not m:
776 raise PatchError(_("bad hunk #%d") % self.number)
776 raise PatchError(_("bad hunk #%d") % self.number)
777 self.starta, foo, self.lena, self.startb, foo2, self.lenb = m.groups()
777 self.starta, foo, self.lena, self.startb, foo2, self.lenb = m.groups()
778 if self.lena is None:
778 if self.lena is None:
779 self.lena = 1
779 self.lena = 1
780 else:
780 else:
781 self.lena = int(self.lena)
781 self.lena = int(self.lena)
782 if self.lenb is None:
782 if self.lenb is None:
783 self.lenb = 1
783 self.lenb = 1
784 else:
784 else:
785 self.lenb = int(self.lenb)
785 self.lenb = int(self.lenb)
786 self.starta = int(self.starta)
786 self.starta = int(self.starta)
787 self.startb = int(self.startb)
787 self.startb = int(self.startb)
788 diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, self.b)
788 diffhelpers.addlines(lr, self.hunk, self.lena, self.lenb, self.a, self.b)
789 # if we hit eof before finishing out the hunk, the last line will
789 # if we hit eof before finishing out the hunk, the last line will
790 # be zero length. Lets try to fix it up.
790 # be zero length. Lets try to fix it up.
791 while len(self.hunk[-1]) == 0:
791 while len(self.hunk[-1]) == 0:
792 del self.hunk[-1]
792 del self.hunk[-1]
793 del self.a[-1]
793 del self.a[-1]
794 del self.b[-1]
794 del self.b[-1]
795 self.lena -= 1
795 self.lena -= 1
796 self.lenb -= 1
796 self.lenb -= 1
797 self._fixnewline(lr)
797 self._fixnewline(lr)
798
798
799 def read_context_hunk(self, lr):
799 def read_context_hunk(self, lr):
800 self.desc = lr.readline()
800 self.desc = lr.readline()
801 m = contextdesc.match(self.desc)
801 m = contextdesc.match(self.desc)
802 if not m:
802 if not m:
803 raise PatchError(_("bad hunk #%d") % self.number)
803 raise PatchError(_("bad hunk #%d") % self.number)
804 foo, self.starta, foo2, aend, foo3 = m.groups()
804 foo, self.starta, foo2, aend, foo3 = m.groups()
805 self.starta = int(self.starta)
805 self.starta = int(self.starta)
806 if aend is None:
806 if aend is None:
807 aend = self.starta
807 aend = self.starta
808 self.lena = int(aend) - self.starta
808 self.lena = int(aend) - self.starta
809 if self.starta:
809 if self.starta:
810 self.lena += 1
810 self.lena += 1
811 for x in xrange(self.lena):
811 for x in xrange(self.lena):
812 l = lr.readline()
812 l = lr.readline()
813 if l.startswith('---'):
813 if l.startswith('---'):
814 # lines addition, old block is empty
814 # lines addition, old block is empty
815 lr.push(l)
815 lr.push(l)
816 break
816 break
817 s = l[2:]
817 s = l[2:]
818 if l.startswith('- ') or l.startswith('! '):
818 if l.startswith('- ') or l.startswith('! '):
819 u = '-' + s
819 u = '-' + s
820 elif l.startswith(' '):
820 elif l.startswith(' '):
821 u = ' ' + s
821 u = ' ' + s
822 else:
822 else:
823 raise PatchError(_("bad hunk #%d old text line %d") %
823 raise PatchError(_("bad hunk #%d old text line %d") %
824 (self.number, x))
824 (self.number, x))
825 self.a.append(u)
825 self.a.append(u)
826 self.hunk.append(u)
826 self.hunk.append(u)
827
827
828 l = lr.readline()
828 l = lr.readline()
829 if l.startswith('\ '):
829 if l.startswith('\ '):
830 s = self.a[-1][:-1]
830 s = self.a[-1][:-1]
831 self.a[-1] = s
831 self.a[-1] = s
832 self.hunk[-1] = s
832 self.hunk[-1] = s
833 l = lr.readline()
833 l = lr.readline()
834 m = contextdesc.match(l)
834 m = contextdesc.match(l)
835 if not m:
835 if not m:
836 raise PatchError(_("bad hunk #%d") % self.number)
836 raise PatchError(_("bad hunk #%d") % self.number)
837 foo, self.startb, foo2, bend, foo3 = m.groups()
837 foo, self.startb, foo2, bend, foo3 = m.groups()
838 self.startb = int(self.startb)
838 self.startb = int(self.startb)
839 if bend is None:
839 if bend is None:
840 bend = self.startb
840 bend = self.startb
841 self.lenb = int(bend) - self.startb
841 self.lenb = int(bend) - self.startb
842 if self.startb:
842 if self.startb:
843 self.lenb += 1
843 self.lenb += 1
844 hunki = 1
844 hunki = 1
845 for x in xrange(self.lenb):
845 for x in xrange(self.lenb):
846 l = lr.readline()
846 l = lr.readline()
847 if l.startswith('\ '):
847 if l.startswith('\ '):
848 # XXX: the only way to hit this is with an invalid line range.
848 # XXX: the only way to hit this is with an invalid line range.
849 # The no-eol marker is not counted in the line range, but I
849 # The no-eol marker is not counted in the line range, but I
850 # guess there are diff(1) out there which behave differently.
850 # guess there are diff(1) out there which behave differently.
851 s = self.b[-1][:-1]
851 s = self.b[-1][:-1]
852 self.b[-1] = s
852 self.b[-1] = s
853 self.hunk[hunki - 1] = s
853 self.hunk[hunki - 1] = s
854 continue
854 continue
855 if not l:
855 if not l:
856 # line deletions, new block is empty and we hit EOF
856 # line deletions, new block is empty and we hit EOF
857 lr.push(l)
857 lr.push(l)
858 break
858 break
859 s = l[2:]
859 s = l[2:]
860 if l.startswith('+ ') or l.startswith('! '):
860 if l.startswith('+ ') or l.startswith('! '):
861 u = '+' + s
861 u = '+' + s
862 elif l.startswith(' '):
862 elif l.startswith(' '):
863 u = ' ' + s
863 u = ' ' + s
864 elif len(self.b) == 0:
864 elif len(self.b) == 0:
865 # line deletions, new block is empty
865 # line deletions, new block is empty
866 lr.push(l)
866 lr.push(l)
867 break
867 break
868 else:
868 else:
869 raise PatchError(_("bad hunk #%d old text line %d") %
869 raise PatchError(_("bad hunk #%d old text line %d") %
870 (self.number, x))
870 (self.number, x))
871 self.b.append(s)
871 self.b.append(s)
872 while True:
872 while True:
873 if hunki >= len(self.hunk):
873 if hunki >= len(self.hunk):
874 h = ""
874 h = ""
875 else:
875 else:
876 h = self.hunk[hunki]
876 h = self.hunk[hunki]
877 hunki += 1
877 hunki += 1
878 if h == u:
878 if h == u:
879 break
879 break
880 elif h.startswith('-'):
880 elif h.startswith('-'):
881 continue
881 continue
882 else:
882 else:
883 self.hunk.insert(hunki - 1, u)
883 self.hunk.insert(hunki - 1, u)
884 break
884 break
885
885
886 if not self.a:
886 if not self.a:
887 # this happens when lines were only added to the hunk
887 # this happens when lines were only added to the hunk
888 for x in self.hunk:
888 for x in self.hunk:
889 if x.startswith('-') or x.startswith(' '):
889 if x.startswith('-') or x.startswith(' '):
890 self.a.append(x)
890 self.a.append(x)
891 if not self.b:
891 if not self.b:
892 # this happens when lines were only deleted from the hunk
892 # this happens when lines were only deleted from the hunk
893 for x in self.hunk:
893 for x in self.hunk:
894 if x.startswith('+') or x.startswith(' '):
894 if x.startswith('+') or x.startswith(' '):
895 self.b.append(x[1:])
895 self.b.append(x[1:])
896 # @@ -start,len +start,len @@
896 # @@ -start,len +start,len @@
897 self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
897 self.desc = "@@ -%d,%d +%d,%d @@\n" % (self.starta, self.lena,
898 self.startb, self.lenb)
898 self.startb, self.lenb)
899 self.hunk[0] = self.desc
899 self.hunk[0] = self.desc
900 self._fixnewline(lr)
900 self._fixnewline(lr)
901
901
902 def _fixnewline(self, lr):
902 def _fixnewline(self, lr):
903 l = lr.readline()
903 l = lr.readline()
904 if l.startswith('\ '):
904 if l.startswith('\ '):
905 diffhelpers.fix_newline(self.hunk, self.a, self.b)
905 diffhelpers.fix_newline(self.hunk, self.a, self.b)
906 else:
906 else:
907 lr.push(l)
907 lr.push(l)
908
908
909 def complete(self):
909 def complete(self):
910 return len(self.a) == self.lena and len(self.b) == self.lenb
910 return len(self.a) == self.lena and len(self.b) == self.lenb
911
911
912 def createfile(self):
912 def createfile(self):
913 return self.starta == 0 and self.lena == 0 and self.create
913 return self.starta == 0 and self.lena == 0 and self.create
914
914
915 def rmfile(self):
915 def rmfile(self):
916 return self.startb == 0 and self.lenb == 0 and self.remove
916 return self.startb == 0 and self.lenb == 0 and self.remove
917
917
918 def fuzzit(self, l, fuzz, toponly):
918 def fuzzit(self, l, fuzz, toponly):
919 # this removes context lines from the top and bottom of list 'l'. It
919 # this removes context lines from the top and bottom of list 'l'. It
920 # checks the hunk to make sure only context lines are removed, and then
920 # checks the hunk to make sure only context lines are removed, and then
921 # returns a new shortened list of lines.
921 # returns a new shortened list of lines.
922 fuzz = min(fuzz, len(l)-1)
922 fuzz = min(fuzz, len(l)-1)
923 if fuzz:
923 if fuzz:
924 top = 0
924 top = 0
925 bot = 0
925 bot = 0
926 hlen = len(self.hunk)
926 hlen = len(self.hunk)
927 for x in xrange(hlen - 1):
927 for x in xrange(hlen - 1):
928 # the hunk starts with the @@ line, so use x+1
928 # the hunk starts with the @@ line, so use x+1
929 if self.hunk[x + 1][0] == ' ':
929 if self.hunk[x + 1][0] == ' ':
930 top += 1
930 top += 1
931 else:
931 else:
932 break
932 break
933 if not toponly:
933 if not toponly:
934 for x in xrange(hlen - 1):
934 for x in xrange(hlen - 1):
935 if self.hunk[hlen - bot - 1][0] == ' ':
935 if self.hunk[hlen - bot - 1][0] == ' ':
936 bot += 1
936 bot += 1
937 else:
937 else:
938 break
938 break
939
939
940 # top and bot now count context in the hunk
940 # top and bot now count context in the hunk
941 # adjust them if either one is short
941 # adjust them if either one is short
942 context = max(top, bot, 3)
942 context = max(top, bot, 3)
943 if bot < context:
943 if bot < context:
944 bot = max(0, fuzz - (context - bot))
944 bot = max(0, fuzz - (context - bot))
945 else:
945 else:
946 bot = min(fuzz, bot)
946 bot = min(fuzz, bot)
947 if top < context:
947 if top < context:
948 top = max(0, fuzz - (context - top))
948 top = max(0, fuzz - (context - top))
949 else:
949 else:
950 top = min(fuzz, top)
950 top = min(fuzz, top)
951
951
952 return l[top:len(l)-bot]
952 return l[top:len(l)-bot]
953 return l
953 return l
954
954
955 def old(self, fuzz=0, toponly=False):
955 def old(self, fuzz=0, toponly=False):
956 return self.fuzzit(self.a, fuzz, toponly)
956 return self.fuzzit(self.a, fuzz, toponly)
957
957
958 def new(self, fuzz=0, toponly=False):
958 def new(self, fuzz=0, toponly=False):
959 return self.fuzzit(self.b, fuzz, toponly)
959 return self.fuzzit(self.b, fuzz, toponly)
960
960
961 class binhunk:
961 class binhunk:
962 'A binary patch file. Only understands literals so far.'
962 'A binary patch file. Only understands literals so far.'
963 def __init__(self, gitpatch):
963 def __init__(self, gitpatch):
964 self.gitpatch = gitpatch
964 self.gitpatch = gitpatch
965 self.text = None
965 self.text = None
966 self.hunk = ['GIT binary patch\n']
966 self.hunk = ['GIT binary patch\n']
967
967
968 def createfile(self):
968 def createfile(self):
969 return self.gitpatch.op == 'ADD'
969 return self.gitpatch.op == 'ADD'
970
970
971 def rmfile(self):
971 def rmfile(self):
972 return self.gitpatch.op == 'DELETE'
972 return self.gitpatch.op == 'DELETE'
973
973
974 def complete(self):
974 def complete(self):
975 return self.text is not None
975 return self.text is not None
976
976
977 def new(self):
977 def new(self):
978 return [self.text]
978 return [self.text]
979
979
980 def extract(self, lr):
980 def extract(self, lr):
981 line = lr.readline()
981 line = lr.readline()
982 self.hunk.append(line)
982 self.hunk.append(line)
983 while line and not line.startswith('literal '):
983 while line and not line.startswith('literal '):
984 line = lr.readline()
984 line = lr.readline()
985 self.hunk.append(line)
985 self.hunk.append(line)
986 if not line:
986 if not line:
987 raise PatchError(_('could not extract binary patch'))
987 raise PatchError(_('could not extract binary patch'))
988 size = int(line[8:].rstrip())
988 size = int(line[8:].rstrip())
989 dec = []
989 dec = []
990 line = lr.readline()
990 line = lr.readline()
991 self.hunk.append(line)
991 self.hunk.append(line)
992 while len(line) > 1:
992 while len(line) > 1:
993 l = line[0]
993 l = line[0]
994 if l <= 'Z' and l >= 'A':
994 if l <= 'Z' and l >= 'A':
995 l = ord(l) - ord('A') + 1
995 l = ord(l) - ord('A') + 1
996 else:
996 else:
997 l = ord(l) - ord('a') + 27
997 l = ord(l) - ord('a') + 27
998 dec.append(base85.b85decode(line[1:-1])[:l])
998 dec.append(base85.b85decode(line[1:-1])[:l])
999 line = lr.readline()
999 line = lr.readline()
1000 self.hunk.append(line)
1000 self.hunk.append(line)
1001 text = zlib.decompress(''.join(dec))
1001 text = zlib.decompress(''.join(dec))
1002 if len(text) != size:
1002 if len(text) != size:
1003 raise PatchError(_('binary patch is %d bytes, not %d') %
1003 raise PatchError(_('binary patch is %d bytes, not %d') %
1004 len(text), size)
1004 len(text), size)
1005 self.text = text
1005 self.text = text
1006
1006
1007 def parsefilename(str):
1007 def parsefilename(str):
1008 # --- filename \t|space stuff
1008 # --- filename \t|space stuff
1009 s = str[4:].rstrip('\r\n')
1009 s = str[4:].rstrip('\r\n')
1010 i = s.find('\t')
1010 i = s.find('\t')
1011 if i < 0:
1011 if i < 0:
1012 i = s.find(' ')
1012 i = s.find(' ')
1013 if i < 0:
1013 if i < 0:
1014 return s
1014 return s
1015 return s[:i]
1015 return s[:i]
1016
1016
1017 def pathstrip(path, strip):
1017 def pathstrip(path, strip):
1018 pathlen = len(path)
1018 pathlen = len(path)
1019 i = 0
1019 i = 0
1020 if strip == 0:
1020 if strip == 0:
1021 return '', path.rstrip()
1021 return '', path.rstrip()
1022 count = strip
1022 count = strip
1023 while count > 0:
1023 while count > 0:
1024 i = path.find('/', i)
1024 i = path.find('/', i)
1025 if i == -1:
1025 if i == -1:
1026 raise PatchError(_("unable to strip away %d of %d dirs from %s") %
1026 raise PatchError(_("unable to strip away %d of %d dirs from %s") %
1027 (count, strip, path))
1027 (count, strip, path))
1028 i += 1
1028 i += 1
1029 # consume '//' in the path
1029 # consume '//' in the path
1030 while i < pathlen - 1 and path[i] == '/':
1030 while i < pathlen - 1 and path[i] == '/':
1031 i += 1
1031 i += 1
1032 count -= 1
1032 count -= 1
1033 return path[:i].lstrip(), path[i:].rstrip()
1033 return path[:i].lstrip(), path[i:].rstrip()
1034
1034
1035 def selectfile(backend, afile_orig, bfile_orig, hunk, strip):
1035 def selectfile(backend, afile_orig, bfile_orig, hunk, strip):
1036 nulla = afile_orig == "/dev/null"
1036 nulla = afile_orig == "/dev/null"
1037 nullb = bfile_orig == "/dev/null"
1037 nullb = bfile_orig == "/dev/null"
1038 abase, afile = pathstrip(afile_orig, strip)
1038 abase, afile = pathstrip(afile_orig, strip)
1039 gooda = not nulla and backend.exists(afile)
1039 gooda = not nulla and backend.exists(afile)
1040 bbase, bfile = pathstrip(bfile_orig, strip)
1040 bbase, bfile = pathstrip(bfile_orig, strip)
1041 if afile == bfile:
1041 if afile == bfile:
1042 goodb = gooda
1042 goodb = gooda
1043 else:
1043 else:
1044 goodb = not nullb and backend.exists(bfile)
1044 goodb = not nullb and backend.exists(bfile)
1045 createfunc = hunk.createfile
1045 createfunc = hunk.createfile
1046 missing = not goodb and not gooda and not createfunc()
1046 missing = not goodb and not gooda and not createfunc()
1047
1047
1048 # some diff programs apparently produce patches where the afile is
1048 # some diff programs apparently produce patches where the afile is
1049 # not /dev/null, but afile starts with bfile
1049 # not /dev/null, but afile starts with bfile
1050 abasedir = afile[:afile.rfind('/') + 1]
1050 abasedir = afile[:afile.rfind('/') + 1]
1051 bbasedir = bfile[:bfile.rfind('/') + 1]
1051 bbasedir = bfile[:bfile.rfind('/') + 1]
1052 if missing and abasedir == bbasedir and afile.startswith(bfile):
1052 if missing and abasedir == bbasedir and afile.startswith(bfile):
1053 # this isn't very pretty
1053 # this isn't very pretty
1054 hunk.create = True
1054 hunk.create = True
1055 if createfunc():
1055 if createfunc():
1056 missing = False
1056 missing = False
1057 else:
1057 else:
1058 hunk.create = False
1058 hunk.create = False
1059
1059
1060 # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the
1060 # If afile is "a/b/foo" and bfile is "a/b/foo.orig" we assume the
1061 # diff is between a file and its backup. In this case, the original
1061 # diff is between a file and its backup. In this case, the original
1062 # file should be patched (see original mpatch code).
1062 # file should be patched (see original mpatch code).
1063 isbackup = (abase == bbase and bfile.startswith(afile))
1063 isbackup = (abase == bbase and bfile.startswith(afile))
1064 fname = None
1064 fname = None
1065 if not missing:
1065 if not missing:
1066 if gooda and goodb:
1066 if gooda and goodb:
1067 fname = isbackup and afile or bfile
1067 fname = isbackup and afile or bfile
1068 elif gooda:
1068 elif gooda:
1069 fname = afile
1069 fname = afile
1070
1070
1071 if not fname:
1071 if not fname:
1072 if not nullb:
1072 if not nullb:
1073 fname = isbackup and afile or bfile
1073 fname = isbackup and afile or bfile
1074 elif not nulla:
1074 elif not nulla:
1075 fname = afile
1075 fname = afile
1076 else:
1076 else:
1077 raise PatchError(_("undefined source and destination files"))
1077 raise PatchError(_("undefined source and destination files"))
1078
1078
1079 return fname, missing
1079 return fname, missing
1080
1080
1081 def scangitpatch(lr, firstline):
1081 def scangitpatch(lr, firstline):
1082 """
1082 """
1083 Git patches can emit:
1083 Git patches can emit:
1084 - rename a to b
1084 - rename a to b
1085 - change b
1085 - change b
1086 - copy a to c
1086 - copy a to c
1087 - change c
1087 - change c
1088
1088
1089 We cannot apply this sequence as-is, the renamed 'a' could not be
1089 We cannot apply this sequence as-is, the renamed 'a' could not be
1090 found for it would have been renamed already. And we cannot copy
1090 found for it would have been renamed already. And we cannot copy
1091 from 'b' instead because 'b' would have been changed already. So
1091 from 'b' instead because 'b' would have been changed already. So
1092 we scan the git patch for copy and rename commands so we can
1092 we scan the git patch for copy and rename commands so we can
1093 perform the copies ahead of time.
1093 perform the copies ahead of time.
1094 """
1094 """
1095 pos = 0
1095 pos = 0
1096 try:
1096 try:
1097 pos = lr.fp.tell()
1097 pos = lr.fp.tell()
1098 fp = lr.fp
1098 fp = lr.fp
1099 except IOError:
1099 except IOError:
1100 fp = cStringIO.StringIO(lr.fp.read())
1100 fp = cStringIO.StringIO(lr.fp.read())
1101 gitlr = linereader(fp, lr.textmode)
1101 gitlr = linereader(fp, lr.textmode)
1102 gitlr.push(firstline)
1102 gitlr.push(firstline)
1103 gitpatches = readgitpatch(gitlr)
1103 gitpatches = readgitpatch(gitlr)
1104 fp.seek(pos)
1104 fp.seek(pos)
1105 return gitpatches
1105 return gitpatches
1106
1106
1107 def iterhunks(fp):
1107 def iterhunks(fp):
1108 """Read a patch and yield the following events:
1108 """Read a patch and yield the following events:
1109 - ("file", afile, bfile, firsthunk): select a new target file.
1109 - ("file", afile, bfile, firsthunk): select a new target file.
1110 - ("hunk", hunk): a new hunk is ready to be applied, follows a
1110 - ("hunk", hunk): a new hunk is ready to be applied, follows a
1111 "file" event.
1111 "file" event.
1112 - ("git", gitchanges): current diff is in git format, gitchanges
1112 - ("git", gitchanges): current diff is in git format, gitchanges
1113 maps filenames to gitpatch records. Unique event.
1113 maps filenames to gitpatch records. Unique event.
1114 """
1114 """
1115 changed = {}
1115 changed = {}
1116 afile = ""
1116 afile = ""
1117 bfile = ""
1117 bfile = ""
1118 state = None
1118 state = None
1119 hunknum = 0
1119 hunknum = 0
1120 emitfile = newfile = False
1120 emitfile = newfile = False
1121 git = False
1121 git = False
1122
1122
1123 # our states
1123 # our states
1124 BFILE = 1
1124 BFILE = 1
1125 context = None
1125 context = None
1126 lr = linereader(fp)
1126 lr = linereader(fp)
1127
1127
1128 while True:
1128 while True:
1129 x = lr.readline()
1129 x = lr.readline()
1130 if not x:
1130 if not x:
1131 break
1131 break
1132 if (state == BFILE and ((not context and x[0] == '@') or
1132 if (state == BFILE and ((not context and x[0] == '@') or
1133 ((context is not False) and x.startswith('***************')))):
1133 ((context is not False) and x.startswith('***************')))):
1134 if context is None and x.startswith('***************'):
1134 if context is None and x.startswith('***************'):
1135 context = True
1135 context = True
1136 gpatch = changed.get(bfile)
1136 gpatch = changed.get(bfile)
1137 create = afile == '/dev/null' or gpatch and gpatch.op == 'ADD'
1137 create = afile == '/dev/null' or gpatch and gpatch.op == 'ADD'
1138 remove = bfile == '/dev/null' or gpatch and gpatch.op == 'DELETE'
1138 remove = bfile == '/dev/null' or gpatch and gpatch.op == 'DELETE'
1139 h = hunk(x, hunknum + 1, lr, context, create, remove)
1139 h = hunk(x, hunknum + 1, lr, context, create, remove)
1140 hunknum += 1
1140 hunknum += 1
1141 if emitfile:
1141 if emitfile:
1142 emitfile = False
1142 emitfile = False
1143 yield 'file', (afile, bfile, h, gpatch and gpatch.mode or None)
1143 yield 'file', (afile, bfile, h, gpatch and gpatch.mode or None)
1144 yield 'hunk', h
1144 yield 'hunk', h
1145 elif state == BFILE and x.startswith('GIT binary patch'):
1145 elif state == BFILE and x.startswith('GIT binary patch'):
1146 gpatch = changed[bfile]
1146 gpatch = changed[bfile]
1147 h = binhunk(gpatch)
1147 h = binhunk(gpatch)
1148 hunknum += 1
1148 hunknum += 1
1149 if emitfile:
1149 if emitfile:
1150 emitfile = False
1150 emitfile = False
1151 yield 'file', ('a/' + afile, 'b/' + bfile, h,
1151 yield 'file', ('a/' + afile, 'b/' + bfile, h,
1152 gpatch and gpatch.mode or None)
1152 gpatch and gpatch.mode or None)
1153 h.extract(lr)
1153 h.extract(lr)
1154 yield 'hunk', h
1154 yield 'hunk', h
1155 elif x.startswith('diff --git'):
1155 elif x.startswith('diff --git'):
1156 # check for git diff, scanning the whole patch file if needed
1156 # check for git diff, scanning the whole patch file if needed
1157 m = gitre.match(x)
1157 m = gitre.match(x)
1158 if m:
1158 if m:
1159 afile, bfile = m.group(1, 2)
1159 afile, bfile = m.group(1, 2)
1160 if not git:
1160 if not git:
1161 git = True
1161 git = True
1162 gitpatches = scangitpatch(lr, x)
1162 gitpatches = scangitpatch(lr, x)
1163 yield 'git', gitpatches
1163 yield 'git', gitpatches
1164 for gp in gitpatches:
1164 for gp in gitpatches:
1165 changed[gp.path] = gp
1165 changed[gp.path] = gp
1166 # else error?
1166 # else error?
1167 # copy/rename + modify should modify target, not source
1167 # copy/rename + modify should modify target, not source
1168 gp = changed.get(bfile)
1168 gp = changed.get(bfile)
1169 if gp and (gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD')
1169 if gp and (gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD')
1170 or gp.mode):
1170 or gp.mode):
1171 afile = bfile
1171 afile = bfile
1172 newfile = True
1172 newfile = True
1173 elif x.startswith('---'):
1173 elif x.startswith('---'):
1174 # check for a unified diff
1174 # check for a unified diff
1175 l2 = lr.readline()
1175 l2 = lr.readline()
1176 if not l2.startswith('+++'):
1176 if not l2.startswith('+++'):
1177 lr.push(l2)
1177 lr.push(l2)
1178 continue
1178 continue
1179 newfile = True
1179 newfile = True
1180 context = False
1180 context = False
1181 afile = parsefilename(x)
1181 afile = parsefilename(x)
1182 bfile = parsefilename(l2)
1182 bfile = parsefilename(l2)
1183 elif x.startswith('***'):
1183 elif x.startswith('***'):
1184 # check for a context diff
1184 # check for a context diff
1185 l2 = lr.readline()
1185 l2 = lr.readline()
1186 if not l2.startswith('---'):
1186 if not l2.startswith('---'):
1187 lr.push(l2)
1187 lr.push(l2)
1188 continue
1188 continue
1189 l3 = lr.readline()
1189 l3 = lr.readline()
1190 lr.push(l3)
1190 lr.push(l3)
1191 if not l3.startswith("***************"):
1191 if not l3.startswith("***************"):
1192 lr.push(l2)
1192 lr.push(l2)
1193 continue
1193 continue
1194 newfile = True
1194 newfile = True
1195 context = True
1195 context = True
1196 afile = parsefilename(x)
1196 afile = parsefilename(x)
1197 bfile = parsefilename(l2)
1197 bfile = parsefilename(l2)
1198
1198
1199 if newfile:
1199 if newfile:
1200 newfile = False
1200 newfile = False
1201 emitfile = True
1201 emitfile = True
1202 state = BFILE
1202 state = BFILE
1203 hunknum = 0
1203 hunknum = 0
1204
1204
1205 def applydiff(ui, fp, changed, backend, strip=1, eolmode='strict'):
1205 def applydiff(ui, fp, changed, backend, strip=1, eolmode='strict'):
1206 """Reads a patch from fp and tries to apply it.
1206 """Reads a patch from fp and tries to apply it.
1207
1207
1208 The dict 'changed' is filled in with all of the filenames changed
1208 The dict 'changed' is filled in with all of the filenames changed
1209 by the patch. Returns 0 for a clean patch, -1 if any rejects were
1209 by the patch. Returns 0 for a clean patch, -1 if any rejects were
1210 found and 1 if there was any fuzz.
1210 found and 1 if there was any fuzz.
1211
1211
1212 If 'eolmode' is 'strict', the patch content and patched file are
1212 If 'eolmode' is 'strict', the patch content and patched file are
1213 read in binary mode. Otherwise, line endings are ignored when
1213 read in binary mode. Otherwise, line endings are ignored when
1214 patching then normalized according to 'eolmode'.
1214 patching then normalized according to 'eolmode'.
1215 """
1215 """
1216 return _applydiff(ui, fp, patchfile, backend, changed, strip=strip,
1216 return _applydiff(ui, fp, patchfile, backend, changed, strip=strip,
1217 eolmode=eolmode)
1217 eolmode=eolmode)
1218
1218
1219 def _applydiff(ui, fp, patcher, backend, changed, strip=1, eolmode='strict'):
1219 def _applydiff(ui, fp, patcher, backend, changed, strip=1, eolmode='strict'):
1220 rejects = 0
1220 rejects = 0
1221 err = 0
1221 err = 0
1222 current_file = None
1222 current_file = None
1223
1223
1224 for state, values in iterhunks(fp):
1224 for state, values in iterhunks(fp):
1225 if state == 'hunk':
1225 if state == 'hunk':
1226 if not current_file:
1226 if not current_file:
1227 continue
1227 continue
1228 ret = current_file.apply(values)
1228 ret = current_file.apply(values)
1229 if ret >= 0:
1229 if ret >= 0:
1230 changed.setdefault(current_file.fname, None)
1230 changed.setdefault(current_file.fname, None)
1231 if ret > 0:
1231 if ret > 0:
1232 err = 1
1232 err = 1
1233 elif state == 'file':
1233 elif state == 'file':
1234 if current_file:
1234 if current_file:
1235 rejects += current_file.close()
1235 rejects += current_file.close()
1236 afile, bfile, first_hunk, mode = values
1236 afile, bfile, first_hunk, mode = values
1237 try:
1237 try:
1238 current_file, missing = selectfile(backend, afile, bfile,
1238 current_file, missing = selectfile(backend, afile, bfile,
1239 first_hunk, strip)
1239 first_hunk, strip)
1240 current_file = patcher(ui, current_file, backend, mode,
1240 current_file = patcher(ui, current_file, backend, mode,
1241 missing=missing, eolmode=eolmode)
1241 missing=missing, eolmode=eolmode)
1242 except PatchError, inst:
1242 except PatchError, inst:
1243 ui.warn(str(inst) + '\n')
1243 ui.warn(str(inst) + '\n')
1244 current_file = None
1244 current_file = None
1245 rejects += 1
1245 rejects += 1
1246 continue
1246 continue
1247 elif state == 'git':
1247 elif state == 'git':
1248 for gp in values:
1248 for gp in values:
1249 gp.path = pathstrip(gp.path, strip - 1)[1]
1249 gp.path = pathstrip(gp.path, strip - 1)[1]
1250 if gp.oldpath:
1250 if gp.oldpath:
1251 gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1]
1251 gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1]
1252 if gp.op in ('COPY', 'RENAME'):
1252 if gp.op in ('COPY', 'RENAME'):
1253 backend.copy(gp.oldpath, gp.path)
1253 backend.copy(gp.oldpath, gp.path)
1254 changed[gp.path] = gp
1254 changed[gp.path] = gp
1255 else:
1255 else:
1256 raise util.Abort(_('unsupported parser state: %s') % state)
1256 raise util.Abort(_('unsupported parser state: %s') % state)
1257
1257
1258 if current_file:
1258 if current_file:
1259 rejects += current_file.close()
1259 rejects += current_file.close()
1260
1260
1261 # Handle mode changes without hunk
1261 # Handle mode changes without hunk
1262 removed = set()
1262 removed = set()
1263 for gp in changed.itervalues():
1263 for gp in changed.itervalues():
1264 if not gp:
1264 if not gp:
1265 continue
1265 continue
1266 if gp.op == 'DELETE':
1266 if gp.op == 'DELETE':
1267 removed.add(gp.path)
1267 removed.add(gp.path)
1268 continue
1268 continue
1269 if gp.op == 'RENAME':
1269 if gp.op == 'RENAME':
1270 removed.add(gp.oldpath)
1270 removed.add(gp.oldpath)
1271 if gp.mode:
1271 if gp.mode:
1272 if gp.op == 'ADD' and not backend.exists(gp.path):
1272 if gp.op == 'ADD' and not backend.exists(gp.path):
1273 # Added files without content have no hunk and must be created
1273 # Added files without content have no hunk and must be created
1274 backend.writelines(gp.path, [], gp.mode)
1274 backend.writelines(gp.path, [], gp.mode)
1275 else:
1275 else:
1276 backend.setmode(gp.path, gp.mode[0], gp.mode[1])
1276 backend.setmode(gp.path, gp.mode[0], gp.mode[1])
1277 for path in sorted(removed):
1277 for path in sorted(removed):
1278 backend.unlink(path)
1278 backend.unlink(path)
1279
1279
1280 if rejects:
1280 if rejects:
1281 return -1
1281 return -1
1282 return err
1282 return err
1283
1283
1284 def _updatedir(ui, repo, patches, similarity=0):
1284 def _externalpatch(ui, repo, patcher, patchname, strip, cwd, files,
1285 '''Update dirstate after patch application according to metadata'''
1285 similarity):
1286 if not patches:
1287 return []
1288 copies = []
1289 removes = set()
1290 cfiles = patches.keys()
1291 cwd = repo.getcwd()
1292 if cwd:
1293 cfiles = [util.pathto(repo.root, cwd, f) for f in patches.keys()]
1294 for f in patches:
1295 gp = patches[f]
1296 if not gp:
1297 continue
1298 if gp.op == 'RENAME':
1299 copies.append((gp.oldpath, gp.path))
1300 removes.add(gp.oldpath)
1301 elif gp.op == 'COPY':
1302 copies.append((gp.oldpath, gp.path))
1303 elif gp.op == 'DELETE':
1304 removes.add(gp.path)
1305
1306 wctx = repo[None]
1307 for src, dst in copies:
1308 scmutil.dirstatecopy(ui, repo, wctx, src, dst, cwd=cwd)
1309 if (not similarity) and removes:
1310 wctx.remove(sorted(removes))
1311
1312 scmutil.addremove(repo, cfiles, similarity=similarity)
1313 files = patches.keys()
1314 files.extend([r for r in removes if r not in files])
1315 return sorted(files)
1316
1317 def _externalpatch(patcher, patchname, ui, strip, cwd, files):
1318 """use <patcher> to apply <patchname> to the working directory.
1286 """use <patcher> to apply <patchname> to the working directory.
1319 returns whether patch was applied with fuzz factor."""
1287 returns whether patch was applied with fuzz factor."""
1320
1288
1321 fuzz = False
1289 fuzz = False
1322 args = []
1290 args = []
1323 if cwd:
1291 if cwd:
1324 args.append('-d %s' % util.shellquote(cwd))
1292 args.append('-d %s' % util.shellquote(cwd))
1325 fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip,
1293 fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip,
1326 util.shellquote(patchname)))
1294 util.shellquote(patchname)))
1327
1295 try:
1328 for line in fp:
1296 for line in fp:
1329 line = line.rstrip()
1297 line = line.rstrip()
1330 ui.note(line + '\n')
1298 ui.note(line + '\n')
1331 if line.startswith('patching file '):
1299 if line.startswith('patching file '):
1332 pf = util.parsepatchoutput(line)
1300 pf = util.parsepatchoutput(line)
1333 printed_file = False
1301 printed_file = False
1334 files.setdefault(pf, None)
1302 files.setdefault(pf, None)
1335 elif line.find('with fuzz') >= 0:
1303 elif line.find('with fuzz') >= 0:
1336 fuzz = True
1304 fuzz = True
1337 if not printed_file:
1305 if not printed_file:
1338 ui.warn(pf + '\n')
1306 ui.warn(pf + '\n')
1339 printed_file = True
1307 printed_file = True
1340 ui.warn(line + '\n')
1308 ui.warn(line + '\n')
1341 elif line.find('saving rejects to file') >= 0:
1309 elif line.find('saving rejects to file') >= 0:
1342 ui.warn(line + '\n')
1310 ui.warn(line + '\n')
1343 elif line.find('FAILED') >= 0:
1311 elif line.find('FAILED') >= 0:
1344 if not printed_file:
1312 if not printed_file:
1345 ui.warn(pf + '\n')
1313 ui.warn(pf + '\n')
1346 printed_file = True
1314 printed_file = True
1347 ui.warn(line + '\n')
1315 ui.warn(line + '\n')
1316 finally:
1317 if files:
1318 cfiles = list(files)
1319 cwd = repo.getcwd()
1320 if cwd:
1321 cfiles = [util.pathto(repo.root, cwd, f)
1322 for f in cfile]
1323 scmutil.addremove(repo, cfiles, similarity=similarity)
1348 code = fp.close()
1324 code = fp.close()
1349 if code:
1325 if code:
1350 raise PatchError(_("patch command failed: %s") %
1326 raise PatchError(_("patch command failed: %s") %
1351 util.explainexit(code)[0])
1327 util.explainexit(code)[0])
1352 return fuzz
1328 return fuzz
1353
1329
1354 def internalpatch(ui, repo, patchobj, strip, files=None, eolmode='strict',
1330 def internalpatch(ui, repo, patchobj, strip, files=None, eolmode='strict',
1355 similarity=0):
1331 similarity=0):
1356 """use builtin patch to apply <patchobj> to the working directory.
1332 """use builtin patch to apply <patchobj> to the working directory.
1357 returns whether patch was applied with fuzz factor."""
1333 returns whether patch was applied with fuzz factor."""
1358
1334
1359 if files is None:
1335 if files is None:
1360 files = {}
1336 files = {}
1361 if eolmode is None:
1337 if eolmode is None:
1362 eolmode = ui.config('patch', 'eol', 'strict')
1338 eolmode = ui.config('patch', 'eol', 'strict')
1363 if eolmode.lower() not in eolmodes:
1339 if eolmode.lower() not in eolmodes:
1364 raise util.Abort(_('unsupported line endings type: %s') % eolmode)
1340 raise util.Abort(_('unsupported line endings type: %s') % eolmode)
1365 eolmode = eolmode.lower()
1341 eolmode = eolmode.lower()
1366
1342
1367 backend = workingbackend(ui, repo, similarity)
1343 backend = workingbackend(ui, repo, similarity)
1368 try:
1344 try:
1369 fp = open(patchobj, 'rb')
1345 fp = open(patchobj, 'rb')
1370 except TypeError:
1346 except TypeError:
1371 fp = patchobj
1347 fp = patchobj
1372 try:
1348 try:
1373 ret = applydiff(ui, fp, files, backend, strip=strip, eolmode=eolmode)
1349 ret = applydiff(ui, fp, files, backend, strip=strip, eolmode=eolmode)
1374 finally:
1350 finally:
1375 if fp != patchobj:
1351 if fp != patchobj:
1376 fp.close()
1352 fp.close()
1377 files.update(dict.fromkeys(backend.close()))
1353 files.update(dict.fromkeys(backend.close()))
1378 if ret < 0:
1354 if ret < 0:
1379 raise PatchError(_('patch failed to apply'))
1355 raise PatchError(_('patch failed to apply'))
1380 return ret > 0
1356 return ret > 0
1381
1357
1382 def patch(ui, repo, patchname, strip=1, cwd=None, files=None, eolmode='strict',
1358 def patch(ui, repo, patchname, strip=1, cwd=None, files=None, eolmode='strict',
1383 similarity=0):
1359 similarity=0):
1384 """Apply <patchname> to the working directory.
1360 """Apply <patchname> to the working directory.
1385
1361
1386 'eolmode' specifies how end of lines should be handled. It can be:
1362 'eolmode' specifies how end of lines should be handled. It can be:
1387 - 'strict': inputs are read in binary mode, EOLs are preserved
1363 - 'strict': inputs are read in binary mode, EOLs are preserved
1388 - 'crlf': EOLs are ignored when patching and reset to CRLF
1364 - 'crlf': EOLs are ignored when patching and reset to CRLF
1389 - 'lf': EOLs are ignored when patching and reset to LF
1365 - 'lf': EOLs are ignored when patching and reset to LF
1390 - None: get it from user settings, default to 'strict'
1366 - None: get it from user settings, default to 'strict'
1391 'eolmode' is ignored when using an external patcher program.
1367 'eolmode' is ignored when using an external patcher program.
1392
1368
1393 Returns whether patch was applied with fuzz factor.
1369 Returns whether patch was applied with fuzz factor.
1394 """
1370 """
1395 patcher = ui.config('ui', 'patch')
1371 patcher = ui.config('ui', 'patch')
1396 if files is None:
1372 if files is None:
1397 files = {}
1373 files = {}
1398 try:
1374 try:
1399 if patcher:
1375 if patcher:
1400 try:
1376 return _externalpatch(ui, repo, patcher, patchname, strip,
1401 return _externalpatch(patcher, patchname, ui, strip, cwd,
1377 cwd, files, similarity)
1402 files)
1403 finally:
1404 touched = _updatedir(ui, repo, files, similarity)
1405 files.update(dict.fromkeys(touched))
1406 return internalpatch(ui, repo, patchname, strip, files, eolmode,
1378 return internalpatch(ui, repo, patchname, strip, files, eolmode,
1407 similarity)
1379 similarity)
1408 except PatchError, err:
1380 except PatchError, err:
1409 raise util.Abort(str(err))
1381 raise util.Abort(str(err))
1410
1382
1411 def changedfiles(ui, repo, patchpath, strip=1):
1383 def changedfiles(ui, repo, patchpath, strip=1):
1412 backend = fsbackend(ui, repo.root)
1384 backend = fsbackend(ui, repo.root)
1413 fp = open(patchpath, 'rb')
1385 fp = open(patchpath, 'rb')
1414 try:
1386 try:
1415 changed = set()
1387 changed = set()
1416 for state, values in iterhunks(fp):
1388 for state, values in iterhunks(fp):
1417 if state == 'hunk':
1389 if state == 'hunk':
1418 continue
1390 continue
1419 elif state == 'file':
1391 elif state == 'file':
1420 afile, bfile, first_hunk, mode = values
1392 afile, bfile, first_hunk, mode = values
1421 current_file, missing = selectfile(backend, afile, bfile,
1393 current_file, missing = selectfile(backend, afile, bfile,
1422 first_hunk, strip)
1394 first_hunk, strip)
1423 changed.add(current_file)
1395 changed.add(current_file)
1424 elif state == 'git':
1396 elif state == 'git':
1425 for gp in values:
1397 for gp in values:
1426 gp.path = pathstrip(gp.path, strip - 1)[1]
1398 gp.path = pathstrip(gp.path, strip - 1)[1]
1427 changed.add(gp.path)
1399 changed.add(gp.path)
1428 if gp.oldpath:
1400 if gp.oldpath:
1429 gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1]
1401 gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1]
1430 if gp.op == 'RENAME':
1402 if gp.op == 'RENAME':
1431 changed.add(gp.oldpath)
1403 changed.add(gp.oldpath)
1432 else:
1404 else:
1433 raise util.Abort(_('unsupported parser state: %s') % state)
1405 raise util.Abort(_('unsupported parser state: %s') % state)
1434 return changed
1406 return changed
1435 finally:
1407 finally:
1436 fp.close()
1408 fp.close()
1437
1409
1438 def b85diff(to, tn):
1410 def b85diff(to, tn):
1439 '''print base85-encoded binary diff'''
1411 '''print base85-encoded binary diff'''
1440 def gitindex(text):
1412 def gitindex(text):
1441 if not text:
1413 if not text:
1442 return hex(nullid)
1414 return hex(nullid)
1443 l = len(text)
1415 l = len(text)
1444 s = util.sha1('blob %d\0' % l)
1416 s = util.sha1('blob %d\0' % l)
1445 s.update(text)
1417 s.update(text)
1446 return s.hexdigest()
1418 return s.hexdigest()
1447
1419
1448 def fmtline(line):
1420 def fmtline(line):
1449 l = len(line)
1421 l = len(line)
1450 if l <= 26:
1422 if l <= 26:
1451 l = chr(ord('A') + l - 1)
1423 l = chr(ord('A') + l - 1)
1452 else:
1424 else:
1453 l = chr(l - 26 + ord('a') - 1)
1425 l = chr(l - 26 + ord('a') - 1)
1454 return '%c%s\n' % (l, base85.b85encode(line, True))
1426 return '%c%s\n' % (l, base85.b85encode(line, True))
1455
1427
1456 def chunk(text, csize=52):
1428 def chunk(text, csize=52):
1457 l = len(text)
1429 l = len(text)
1458 i = 0
1430 i = 0
1459 while i < l:
1431 while i < l:
1460 yield text[i:i + csize]
1432 yield text[i:i + csize]
1461 i += csize
1433 i += csize
1462
1434
1463 tohash = gitindex(to)
1435 tohash = gitindex(to)
1464 tnhash = gitindex(tn)
1436 tnhash = gitindex(tn)
1465 if tohash == tnhash:
1437 if tohash == tnhash:
1466 return ""
1438 return ""
1467
1439
1468 # TODO: deltas
1440 # TODO: deltas
1469 ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' %
1441 ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' %
1470 (tohash, tnhash, len(tn))]
1442 (tohash, tnhash, len(tn))]
1471 for l in chunk(zlib.compress(tn)):
1443 for l in chunk(zlib.compress(tn)):
1472 ret.append(fmtline(l))
1444 ret.append(fmtline(l))
1473 ret.append('\n')
1445 ret.append('\n')
1474 return ''.join(ret)
1446 return ''.join(ret)
1475
1447
1476 class GitDiffRequired(Exception):
1448 class GitDiffRequired(Exception):
1477 pass
1449 pass
1478
1450
1479 def diffopts(ui, opts=None, untrusted=False):
1451 def diffopts(ui, opts=None, untrusted=False):
1480 def get(key, name=None, getter=ui.configbool):
1452 def get(key, name=None, getter=ui.configbool):
1481 return ((opts and opts.get(key)) or
1453 return ((opts and opts.get(key)) or
1482 getter('diff', name or key, None, untrusted=untrusted))
1454 getter('diff', name or key, None, untrusted=untrusted))
1483 return mdiff.diffopts(
1455 return mdiff.diffopts(
1484 text=opts and opts.get('text'),
1456 text=opts and opts.get('text'),
1485 git=get('git'),
1457 git=get('git'),
1486 nodates=get('nodates'),
1458 nodates=get('nodates'),
1487 showfunc=get('show_function', 'showfunc'),
1459 showfunc=get('show_function', 'showfunc'),
1488 ignorews=get('ignore_all_space', 'ignorews'),
1460 ignorews=get('ignore_all_space', 'ignorews'),
1489 ignorewsamount=get('ignore_space_change', 'ignorewsamount'),
1461 ignorewsamount=get('ignore_space_change', 'ignorewsamount'),
1490 ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'),
1462 ignoreblanklines=get('ignore_blank_lines', 'ignoreblanklines'),
1491 context=get('unified', getter=ui.config))
1463 context=get('unified', getter=ui.config))
1492
1464
1493 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
1465 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
1494 losedatafn=None, prefix=''):
1466 losedatafn=None, prefix=''):
1495 '''yields diff of changes to files between two nodes, or node and
1467 '''yields diff of changes to files between two nodes, or node and
1496 working directory.
1468 working directory.
1497
1469
1498 if node1 is None, use first dirstate parent instead.
1470 if node1 is None, use first dirstate parent instead.
1499 if node2 is None, compare node1 with working directory.
1471 if node2 is None, compare node1 with working directory.
1500
1472
1501 losedatafn(**kwarg) is a callable run when opts.upgrade=True and
1473 losedatafn(**kwarg) is a callable run when opts.upgrade=True and
1502 every time some change cannot be represented with the current
1474 every time some change cannot be represented with the current
1503 patch format. Return False to upgrade to git patch format, True to
1475 patch format. Return False to upgrade to git patch format, True to
1504 accept the loss or raise an exception to abort the diff. It is
1476 accept the loss or raise an exception to abort the diff. It is
1505 called with the name of current file being diffed as 'fn'. If set
1477 called with the name of current file being diffed as 'fn'. If set
1506 to None, patches will always be upgraded to git format when
1478 to None, patches will always be upgraded to git format when
1507 necessary.
1479 necessary.
1508
1480
1509 prefix is a filename prefix that is prepended to all filenames on
1481 prefix is a filename prefix that is prepended to all filenames on
1510 display (used for subrepos).
1482 display (used for subrepos).
1511 '''
1483 '''
1512
1484
1513 if opts is None:
1485 if opts is None:
1514 opts = mdiff.defaultopts
1486 opts = mdiff.defaultopts
1515
1487
1516 if not node1 and not node2:
1488 if not node1 and not node2:
1517 node1 = repo.dirstate.p1()
1489 node1 = repo.dirstate.p1()
1518
1490
1519 def lrugetfilectx():
1491 def lrugetfilectx():
1520 cache = {}
1492 cache = {}
1521 order = []
1493 order = []
1522 def getfilectx(f, ctx):
1494 def getfilectx(f, ctx):
1523 fctx = ctx.filectx(f, filelog=cache.get(f))
1495 fctx = ctx.filectx(f, filelog=cache.get(f))
1524 if f not in cache:
1496 if f not in cache:
1525 if len(cache) > 20:
1497 if len(cache) > 20:
1526 del cache[order.pop(0)]
1498 del cache[order.pop(0)]
1527 cache[f] = fctx.filelog()
1499 cache[f] = fctx.filelog()
1528 else:
1500 else:
1529 order.remove(f)
1501 order.remove(f)
1530 order.append(f)
1502 order.append(f)
1531 return fctx
1503 return fctx
1532 return getfilectx
1504 return getfilectx
1533 getfilectx = lrugetfilectx()
1505 getfilectx = lrugetfilectx()
1534
1506
1535 ctx1 = repo[node1]
1507 ctx1 = repo[node1]
1536 ctx2 = repo[node2]
1508 ctx2 = repo[node2]
1537
1509
1538 if not changes:
1510 if not changes:
1539 changes = repo.status(ctx1, ctx2, match=match)
1511 changes = repo.status(ctx1, ctx2, match=match)
1540 modified, added, removed = changes[:3]
1512 modified, added, removed = changes[:3]
1541
1513
1542 if not modified and not added and not removed:
1514 if not modified and not added and not removed:
1543 return []
1515 return []
1544
1516
1545 revs = None
1517 revs = None
1546 if not repo.ui.quiet:
1518 if not repo.ui.quiet:
1547 hexfunc = repo.ui.debugflag and hex or short
1519 hexfunc = repo.ui.debugflag and hex or short
1548 revs = [hexfunc(node) for node in [node1, node2] if node]
1520 revs = [hexfunc(node) for node in [node1, node2] if node]
1549
1521
1550 copy = {}
1522 copy = {}
1551 if opts.git or opts.upgrade:
1523 if opts.git or opts.upgrade:
1552 copy = copies.copies(repo, ctx1, ctx2, repo[nullid])[0]
1524 copy = copies.copies(repo, ctx1, ctx2, repo[nullid])[0]
1553
1525
1554 difffn = lambda opts, losedata: trydiff(repo, revs, ctx1, ctx2,
1526 difffn = lambda opts, losedata: trydiff(repo, revs, ctx1, ctx2,
1555 modified, added, removed, copy, getfilectx, opts, losedata, prefix)
1527 modified, added, removed, copy, getfilectx, opts, losedata, prefix)
1556 if opts.upgrade and not opts.git:
1528 if opts.upgrade and not opts.git:
1557 try:
1529 try:
1558 def losedata(fn):
1530 def losedata(fn):
1559 if not losedatafn or not losedatafn(fn=fn):
1531 if not losedatafn or not losedatafn(fn=fn):
1560 raise GitDiffRequired()
1532 raise GitDiffRequired()
1561 # Buffer the whole output until we are sure it can be generated
1533 # Buffer the whole output until we are sure it can be generated
1562 return list(difffn(opts.copy(git=False), losedata))
1534 return list(difffn(opts.copy(git=False), losedata))
1563 except GitDiffRequired:
1535 except GitDiffRequired:
1564 return difffn(opts.copy(git=True), None)
1536 return difffn(opts.copy(git=True), None)
1565 else:
1537 else:
1566 return difffn(opts, None)
1538 return difffn(opts, None)
1567
1539
1568 def difflabel(func, *args, **kw):
1540 def difflabel(func, *args, **kw):
1569 '''yields 2-tuples of (output, label) based on the output of func()'''
1541 '''yields 2-tuples of (output, label) based on the output of func()'''
1570 prefixes = [('diff', 'diff.diffline'),
1542 prefixes = [('diff', 'diff.diffline'),
1571 ('copy', 'diff.extended'),
1543 ('copy', 'diff.extended'),
1572 ('rename', 'diff.extended'),
1544 ('rename', 'diff.extended'),
1573 ('old', 'diff.extended'),
1545 ('old', 'diff.extended'),
1574 ('new', 'diff.extended'),
1546 ('new', 'diff.extended'),
1575 ('deleted', 'diff.extended'),
1547 ('deleted', 'diff.extended'),
1576 ('---', 'diff.file_a'),
1548 ('---', 'diff.file_a'),
1577 ('+++', 'diff.file_b'),
1549 ('+++', 'diff.file_b'),
1578 ('@@', 'diff.hunk'),
1550 ('@@', 'diff.hunk'),
1579 ('-', 'diff.deleted'),
1551 ('-', 'diff.deleted'),
1580 ('+', 'diff.inserted')]
1552 ('+', 'diff.inserted')]
1581
1553
1582 for chunk in func(*args, **kw):
1554 for chunk in func(*args, **kw):
1583 lines = chunk.split('\n')
1555 lines = chunk.split('\n')
1584 for i, line in enumerate(lines):
1556 for i, line in enumerate(lines):
1585 if i != 0:
1557 if i != 0:
1586 yield ('\n', '')
1558 yield ('\n', '')
1587 stripline = line
1559 stripline = line
1588 if line and line[0] in '+-':
1560 if line and line[0] in '+-':
1589 # highlight trailing whitespace, but only in changed lines
1561 # highlight trailing whitespace, but only in changed lines
1590 stripline = line.rstrip()
1562 stripline = line.rstrip()
1591 for prefix, label in prefixes:
1563 for prefix, label in prefixes:
1592 if stripline.startswith(prefix):
1564 if stripline.startswith(prefix):
1593 yield (stripline, label)
1565 yield (stripline, label)
1594 break
1566 break
1595 else:
1567 else:
1596 yield (line, '')
1568 yield (line, '')
1597 if line != stripline:
1569 if line != stripline:
1598 yield (line[len(stripline):], 'diff.trailingwhitespace')
1570 yield (line[len(stripline):], 'diff.trailingwhitespace')
1599
1571
1600 def diffui(*args, **kw):
1572 def diffui(*args, **kw):
1601 '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
1573 '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
1602 return difflabel(diff, *args, **kw)
1574 return difflabel(diff, *args, **kw)
1603
1575
1604
1576
1605 def _addmodehdr(header, omode, nmode):
1577 def _addmodehdr(header, omode, nmode):
1606 if omode != nmode:
1578 if omode != nmode:
1607 header.append('old mode %s\n' % omode)
1579 header.append('old mode %s\n' % omode)
1608 header.append('new mode %s\n' % nmode)
1580 header.append('new mode %s\n' % nmode)
1609
1581
1610 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
1582 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
1611 copy, getfilectx, opts, losedatafn, prefix):
1583 copy, getfilectx, opts, losedatafn, prefix):
1612
1584
1613 def join(f):
1585 def join(f):
1614 return os.path.join(prefix, f)
1586 return os.path.join(prefix, f)
1615
1587
1616 date1 = util.datestr(ctx1.date())
1588 date1 = util.datestr(ctx1.date())
1617 man1 = ctx1.manifest()
1589 man1 = ctx1.manifest()
1618
1590
1619 gone = set()
1591 gone = set()
1620 gitmode = {'l': '120000', 'x': '100755', '': '100644'}
1592 gitmode = {'l': '120000', 'x': '100755', '': '100644'}
1621
1593
1622 copyto = dict([(v, k) for k, v in copy.items()])
1594 copyto = dict([(v, k) for k, v in copy.items()])
1623
1595
1624 if opts.git:
1596 if opts.git:
1625 revs = None
1597 revs = None
1626
1598
1627 for f in sorted(modified + added + removed):
1599 for f in sorted(modified + added + removed):
1628 to = None
1600 to = None
1629 tn = None
1601 tn = None
1630 dodiff = True
1602 dodiff = True
1631 header = []
1603 header = []
1632 if f in man1:
1604 if f in man1:
1633 to = getfilectx(f, ctx1).data()
1605 to = getfilectx(f, ctx1).data()
1634 if f not in removed:
1606 if f not in removed:
1635 tn = getfilectx(f, ctx2).data()
1607 tn = getfilectx(f, ctx2).data()
1636 a, b = f, f
1608 a, b = f, f
1637 if opts.git or losedatafn:
1609 if opts.git or losedatafn:
1638 if f in added:
1610 if f in added:
1639 mode = gitmode[ctx2.flags(f)]
1611 mode = gitmode[ctx2.flags(f)]
1640 if f in copy or f in copyto:
1612 if f in copy or f in copyto:
1641 if opts.git:
1613 if opts.git:
1642 if f in copy:
1614 if f in copy:
1643 a = copy[f]
1615 a = copy[f]
1644 else:
1616 else:
1645 a = copyto[f]
1617 a = copyto[f]
1646 omode = gitmode[man1.flags(a)]
1618 omode = gitmode[man1.flags(a)]
1647 _addmodehdr(header, omode, mode)
1619 _addmodehdr(header, omode, mode)
1648 if a in removed and a not in gone:
1620 if a in removed and a not in gone:
1649 op = 'rename'
1621 op = 'rename'
1650 gone.add(a)
1622 gone.add(a)
1651 else:
1623 else:
1652 op = 'copy'
1624 op = 'copy'
1653 header.append('%s from %s\n' % (op, join(a)))
1625 header.append('%s from %s\n' % (op, join(a)))
1654 header.append('%s to %s\n' % (op, join(f)))
1626 header.append('%s to %s\n' % (op, join(f)))
1655 to = getfilectx(a, ctx1).data()
1627 to = getfilectx(a, ctx1).data()
1656 else:
1628 else:
1657 losedatafn(f)
1629 losedatafn(f)
1658 else:
1630 else:
1659 if opts.git:
1631 if opts.git:
1660 header.append('new file mode %s\n' % mode)
1632 header.append('new file mode %s\n' % mode)
1661 elif ctx2.flags(f):
1633 elif ctx2.flags(f):
1662 losedatafn(f)
1634 losedatafn(f)
1663 # In theory, if tn was copied or renamed we should check
1635 # In theory, if tn was copied or renamed we should check
1664 # if the source is binary too but the copy record already
1636 # if the source is binary too but the copy record already
1665 # forces git mode.
1637 # forces git mode.
1666 if util.binary(tn):
1638 if util.binary(tn):
1667 if opts.git:
1639 if opts.git:
1668 dodiff = 'binary'
1640 dodiff = 'binary'
1669 else:
1641 else:
1670 losedatafn(f)
1642 losedatafn(f)
1671 if not opts.git and not tn:
1643 if not opts.git and not tn:
1672 # regular diffs cannot represent new empty file
1644 # regular diffs cannot represent new empty file
1673 losedatafn(f)
1645 losedatafn(f)
1674 elif f in removed:
1646 elif f in removed:
1675 if opts.git:
1647 if opts.git:
1676 # have we already reported a copy above?
1648 # have we already reported a copy above?
1677 if ((f in copy and copy[f] in added
1649 if ((f in copy and copy[f] in added
1678 and copyto[copy[f]] == f) or
1650 and copyto[copy[f]] == f) or
1679 (f in copyto and copyto[f] in added
1651 (f in copyto and copyto[f] in added
1680 and copy[copyto[f]] == f)):
1652 and copy[copyto[f]] == f)):
1681 dodiff = False
1653 dodiff = False
1682 else:
1654 else:
1683 header.append('deleted file mode %s\n' %
1655 header.append('deleted file mode %s\n' %
1684 gitmode[man1.flags(f)])
1656 gitmode[man1.flags(f)])
1685 elif not to or util.binary(to):
1657 elif not to or util.binary(to):
1686 # regular diffs cannot represent empty file deletion
1658 # regular diffs cannot represent empty file deletion
1687 losedatafn(f)
1659 losedatafn(f)
1688 else:
1660 else:
1689 oflag = man1.flags(f)
1661 oflag = man1.flags(f)
1690 nflag = ctx2.flags(f)
1662 nflag = ctx2.flags(f)
1691 binary = util.binary(to) or util.binary(tn)
1663 binary = util.binary(to) or util.binary(tn)
1692 if opts.git:
1664 if opts.git:
1693 _addmodehdr(header, gitmode[oflag], gitmode[nflag])
1665 _addmodehdr(header, gitmode[oflag], gitmode[nflag])
1694 if binary:
1666 if binary:
1695 dodiff = 'binary'
1667 dodiff = 'binary'
1696 elif binary or nflag != oflag:
1668 elif binary or nflag != oflag:
1697 losedatafn(f)
1669 losedatafn(f)
1698 if opts.git:
1670 if opts.git:
1699 header.insert(0, mdiff.diffline(revs, join(a), join(b), opts))
1671 header.insert(0, mdiff.diffline(revs, join(a), join(b), opts))
1700
1672
1701 if dodiff:
1673 if dodiff:
1702 if dodiff == 'binary':
1674 if dodiff == 'binary':
1703 text = b85diff(to, tn)
1675 text = b85diff(to, tn)
1704 else:
1676 else:
1705 text = mdiff.unidiff(to, date1,
1677 text = mdiff.unidiff(to, date1,
1706 # ctx2 date may be dynamic
1678 # ctx2 date may be dynamic
1707 tn, util.datestr(ctx2.date()),
1679 tn, util.datestr(ctx2.date()),
1708 join(a), join(b), revs, opts=opts)
1680 join(a), join(b), revs, opts=opts)
1709 if header and (text or len(header) > 1):
1681 if header and (text or len(header) > 1):
1710 yield ''.join(header)
1682 yield ''.join(header)
1711 if text:
1683 if text:
1712 yield text
1684 yield text
1713
1685
1714 def diffstatdata(lines):
1686 def diffstatdata(lines):
1715 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
1687 diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
1716
1688
1717 filename, adds, removes = None, 0, 0
1689 filename, adds, removes = None, 0, 0
1718 for line in lines:
1690 for line in lines:
1719 if line.startswith('diff'):
1691 if line.startswith('diff'):
1720 if filename:
1692 if filename:
1721 isbinary = adds == 0 and removes == 0
1693 isbinary = adds == 0 and removes == 0
1722 yield (filename, adds, removes, isbinary)
1694 yield (filename, adds, removes, isbinary)
1723 # set numbers to 0 anyway when starting new file
1695 # set numbers to 0 anyway when starting new file
1724 adds, removes = 0, 0
1696 adds, removes = 0, 0
1725 if line.startswith('diff --git'):
1697 if line.startswith('diff --git'):
1726 filename = gitre.search(line).group(1)
1698 filename = gitre.search(line).group(1)
1727 elif line.startswith('diff -r'):
1699 elif line.startswith('diff -r'):
1728 # format: "diff -r ... -r ... filename"
1700 # format: "diff -r ... -r ... filename"
1729 filename = diffre.search(line).group(1)
1701 filename = diffre.search(line).group(1)
1730 elif line.startswith('+') and not line.startswith('+++'):
1702 elif line.startswith('+') and not line.startswith('+++'):
1731 adds += 1
1703 adds += 1
1732 elif line.startswith('-') and not line.startswith('---'):
1704 elif line.startswith('-') and not line.startswith('---'):
1733 removes += 1
1705 removes += 1
1734 if filename:
1706 if filename:
1735 isbinary = adds == 0 and removes == 0
1707 isbinary = adds == 0 and removes == 0
1736 yield (filename, adds, removes, isbinary)
1708 yield (filename, adds, removes, isbinary)
1737
1709
1738 def diffstat(lines, width=80, git=False):
1710 def diffstat(lines, width=80, git=False):
1739 output = []
1711 output = []
1740 stats = list(diffstatdata(lines))
1712 stats = list(diffstatdata(lines))
1741
1713
1742 maxtotal, maxname = 0, 0
1714 maxtotal, maxname = 0, 0
1743 totaladds, totalremoves = 0, 0
1715 totaladds, totalremoves = 0, 0
1744 hasbinary = False
1716 hasbinary = False
1745
1717
1746 sized = [(filename, adds, removes, isbinary, encoding.colwidth(filename))
1718 sized = [(filename, adds, removes, isbinary, encoding.colwidth(filename))
1747 for filename, adds, removes, isbinary in stats]
1719 for filename, adds, removes, isbinary in stats]
1748
1720
1749 for filename, adds, removes, isbinary, namewidth in sized:
1721 for filename, adds, removes, isbinary, namewidth in sized:
1750 totaladds += adds
1722 totaladds += adds
1751 totalremoves += removes
1723 totalremoves += removes
1752 maxname = max(maxname, namewidth)
1724 maxname = max(maxname, namewidth)
1753 maxtotal = max(maxtotal, adds + removes)
1725 maxtotal = max(maxtotal, adds + removes)
1754 if isbinary:
1726 if isbinary:
1755 hasbinary = True
1727 hasbinary = True
1756
1728
1757 countwidth = len(str(maxtotal))
1729 countwidth = len(str(maxtotal))
1758 if hasbinary and countwidth < 3:
1730 if hasbinary and countwidth < 3:
1759 countwidth = 3
1731 countwidth = 3
1760 graphwidth = width - countwidth - maxname - 6
1732 graphwidth = width - countwidth - maxname - 6
1761 if graphwidth < 10:
1733 if graphwidth < 10:
1762 graphwidth = 10
1734 graphwidth = 10
1763
1735
1764 def scale(i):
1736 def scale(i):
1765 if maxtotal <= graphwidth:
1737 if maxtotal <= graphwidth:
1766 return i
1738 return i
1767 # If diffstat runs out of room it doesn't print anything,
1739 # If diffstat runs out of room it doesn't print anything,
1768 # which isn't very useful, so always print at least one + or -
1740 # which isn't very useful, so always print at least one + or -
1769 # if there were at least some changes.
1741 # if there were at least some changes.
1770 return max(i * graphwidth // maxtotal, int(bool(i)))
1742 return max(i * graphwidth // maxtotal, int(bool(i)))
1771
1743
1772 for filename, adds, removes, isbinary, namewidth in sized:
1744 for filename, adds, removes, isbinary, namewidth in sized:
1773 if git and isbinary:
1745 if git and isbinary:
1774 count = 'Bin'
1746 count = 'Bin'
1775 else:
1747 else:
1776 count = adds + removes
1748 count = adds + removes
1777 pluses = '+' * scale(adds)
1749 pluses = '+' * scale(adds)
1778 minuses = '-' * scale(removes)
1750 minuses = '-' * scale(removes)
1779 output.append(' %s%s | %*s %s%s\n' %
1751 output.append(' %s%s | %*s %s%s\n' %
1780 (filename, ' ' * (maxname - namewidth),
1752 (filename, ' ' * (maxname - namewidth),
1781 countwidth, count,
1753 countwidth, count,
1782 pluses, minuses))
1754 pluses, minuses))
1783
1755
1784 if stats:
1756 if stats:
1785 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
1757 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
1786 % (len(stats), totaladds, totalremoves))
1758 % (len(stats), totaladds, totalremoves))
1787
1759
1788 return ''.join(output)
1760 return ''.join(output)
1789
1761
1790 def diffstatui(*args, **kw):
1762 def diffstatui(*args, **kw):
1791 '''like diffstat(), but yields 2-tuples of (output, label) for
1763 '''like diffstat(), but yields 2-tuples of (output, label) for
1792 ui.write()
1764 ui.write()
1793 '''
1765 '''
1794
1766
1795 for line in diffstat(*args, **kw).splitlines():
1767 for line in diffstat(*args, **kw).splitlines():
1796 if line and line[-1] in '+-':
1768 if line and line[-1] in '+-':
1797 name, graph = line.rsplit(' ', 1)
1769 name, graph = line.rsplit(' ', 1)
1798 yield (name + ' ', '')
1770 yield (name + ' ', '')
1799 m = re.search(r'\++', graph)
1771 m = re.search(r'\++', graph)
1800 if m:
1772 if m:
1801 yield (m.group(0), 'diffstat.inserted')
1773 yield (m.group(0), 'diffstat.inserted')
1802 m = re.search(r'-+', graph)
1774 m = re.search(r'-+', graph)
1803 if m:
1775 if m:
1804 yield (m.group(0), 'diffstat.deleted')
1776 yield (m.group(0), 'diffstat.deleted')
1805 else:
1777 else:
1806 yield (line, '')
1778 yield (line, '')
1807 yield ('\n', '')
1779 yield ('\n', '')
General Comments 0
You need to be logged in to leave comments. Login now