4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
4 # Copyright 2005-2007 Matt Mackall <mpm@selenic.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 os, mimetypes, copy |
9 import os, copy |
10 from mercurial import match, patch, scmutil, error, ui, util |
10 from mercurial import match, patch, scmutil, error, ui, util |
11 from mercurial.i18n import _ |
11 from mercurial.i18n import _ |
12 from mercurial.node import hex, nullid |
12 from mercurial.node import hex, nullid |
13 from common import ErrorResponse |
13 from common import ErrorResponse |
14 from common import HTTP_NOT_FOUND |
14 from common import HTTP_NOT_FOUND |
225 block.append(chunk) |
225 block.append(chunk) |
226 blockno = blockcount.next() |
226 blockno = blockcount.next() |
227 yield tmpl('diffblock', parity=parity.next(), blockno=blockno, |
227 yield tmpl('diffblock', parity=parity.next(), blockno=blockno, |
228 lines=prettyprintlines(''.join(block), blockno)) |
228 lines=prettyprintlines(''.join(block), blockno)) |
229 |
229 |
230 def compare(tmpl, ctx, path, context): |
230 def compare(tmpl, context, leftlines, rightlines): |
231 '''Generator function that provides side-by-side comparison data.''' |
231 '''Generator function that provides side-by-side comparison data.''' |
232 |
|
233 def filelines(f): |
|
234 if util.binary(f.data()): |
|
235 mt = mimetypes.guess_type(f.path())[0] |
|
236 if not mt: |
|
237 mt = 'application/octet-stream' |
|
238 return [_('(binary file %s, hash: %s)') % (mt, hex(f.filenode()))] |
|
239 return f.data().splitlines() |
|
240 |
232 |
241 def compline(type, leftlineno, leftline, rightlineno, rightline): |
233 def compline(type, leftlineno, leftline, rightlineno, rightline): |
242 lineid = leftlineno and ("l%s" % leftlineno) or '' |
234 lineid = leftlineno and ("l%s" % leftlineno) or '' |
243 lineid += rightlineno and ("r%s" % rightlineno) or '' |
235 lineid += rightlineno and ("r%s" % rightlineno) or '' |
244 return tmpl('comparisonline', |
236 return tmpl('comparisonline', |
273 leftlineno=None, |
265 leftlineno=None, |
274 leftline=None, |
266 leftline=None, |
275 rightlineno=i + 1, |
267 rightlineno=i + 1, |
276 rightline=rightlines[i]) |
268 rightline=rightlines[i]) |
277 |
269 |
278 if path in ctx: |
|
279 fctx = ctx[path] |
|
280 rightrev = fctx.filerev() |
|
281 rightnode = fctx.filenode() |
|
282 rightlines = filelines(fctx) |
|
283 parents = fctx.parents() |
|
284 if not parents: |
|
285 leftrev = -1 |
|
286 leftnode = nullid |
|
287 leftlines = () |
|
288 else: |
|
289 pfctx = parents[0] |
|
290 leftrev = pfctx.filerev() |
|
291 leftnode = pfctx.filenode() |
|
292 leftlines = filelines(pfctx) |
|
293 else: |
|
294 rightrev = -1 |
|
295 rightnode = nullid |
|
296 rightlines = () |
|
297 fctx = ctx.parents()[0][path] |
|
298 leftrev = fctx.filerev() |
|
299 leftnode = fctx.filenode() |
|
300 leftlines = filelines(fctx) |
|
301 |
|
302 s = difflib.SequenceMatcher(None, leftlines, rightlines) |
270 s = difflib.SequenceMatcher(None, leftlines, rightlines) |
303 if context < 0: |
271 if context < 0: |
304 blocks = [tmpl('comparisonblock', lines=getblock(s.get_opcodes()))] |
272 yield tmpl('comparisonblock', lines=getblock(s.get_opcodes())) |
305 else: |
273 else: |
306 blocks = (tmpl('comparisonblock', lines=getblock(oc)) |
274 for oc in s.get_grouped_opcodes(n=context): |
307 for oc in s.get_grouped_opcodes(n=context)) |
275 yield tmpl('comparisonblock', lines=getblock(oc)) |
308 |
|
309 yield tmpl('comparison', |
|
310 leftrev=leftrev, |
|
311 leftnode=hex(leftnode), |
|
312 rightrev=rightrev, |
|
313 rightnode=hex(rightnode), |
|
314 blocks=blocks) |
|
315 |
276 |
316 def diffstatgen(ctx): |
277 def diffstatgen(ctx): |
317 '''Generator function that provides the diffstat data.''' |
278 '''Generator function that provides the diffstat data.''' |
318 |
279 |
319 stats = patch.diffstatdata(util.iterlines(ctx.diff())) |
280 stats = patch.diffstatdata(util.iterlines(ctx.diff())) |