comparison mercurial/patch.py @ 10818:d14d45fae927

diff: make use of output labeling
author Brodie Rao <brodie@bitheap.org>
date Fri, 02 Apr 2010 15:22:06 -0500
parents 440786f7f18b
children 4fb1bafd43e7
comparison
equal deleted inserted replaced
10817:2096496b40ec 10818:d14d45fae927
1464 except GitDiffRequired: 1464 except GitDiffRequired:
1465 return difffn(opts.copy(git=True), None) 1465 return difffn(opts.copy(git=True), None)
1466 else: 1466 else:
1467 return difffn(opts, None) 1467 return difffn(opts, None)
1468 1468
1469 def difflabel(func, *args, **kw):
1470 '''yields 2-tuples of (output, label) based on the output of func()'''
1471 prefixes = [('diff', 'diff.diffline'),
1472 ('copy', 'diff.extended'),
1473 ('rename', 'diff.extended'),
1474 ('old', 'diff.extended'),
1475 ('new', 'diff.extended'),
1476 ('deleted', 'diff.extended'),
1477 ('---', 'diff.file_a'),
1478 ('+++', 'diff.file_b'),
1479 ('@@', 'diff.hunk'),
1480 ('-', 'diff.deleted'),
1481 ('+', 'diff.inserted')]
1482
1483 for chunk in func(*args, **kw):
1484 lines = chunk.split('\n')
1485 for i, line in enumerate(lines):
1486 if i != 0:
1487 yield ('\n', '')
1488 stripline = line
1489 if line and line[0] in '+-':
1490 # highlight trailing whitespace, but only in changed lines
1491 stripline = line.rstrip()
1492 for prefix, label in prefixes:
1493 if stripline.startswith(prefix):
1494 yield (stripline, label)
1495 break
1496 else:
1497 yield (line, '')
1498 if line != stripline:
1499 yield (line[len(stripline):], 'diff.trailingwhitespace')
1500
1501 def diffui(*args, **kw):
1502 '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
1503 return difflabel(diff, *args, **kw)
1504
1505
1469 def _addmodehdr(header, omode, nmode): 1506 def _addmodehdr(header, omode, nmode):
1470 if omode != nmode: 1507 if omode != nmode:
1471 header.append('old mode %s\n' % omode) 1508 header.append('old mode %s\n' % omode)
1472 header.append('new mode %s\n' % nmode) 1509 header.append('new mode %s\n' % nmode)
1473 1510
1634 if stats: 1671 if stats:
1635 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n') 1672 output.append(_(' %d files changed, %d insertions(+), %d deletions(-)\n')
1636 % (len(stats), totaladds, totalremoves)) 1673 % (len(stats), totaladds, totalremoves))
1637 1674
1638 return ''.join(output) 1675 return ''.join(output)
1676
1677 def diffstatui(*args, **kw):
1678 '''like diffstat(), but yields 2-tuples of (output, label) for
1679 ui.write()
1680 '''
1681
1682 for line in diffstat(*args, **kw).splitlines():
1683 if line and line[-1] in '+-':
1684 name, graph = line.rsplit(' ', 1)
1685 yield (name + ' ', '')
1686 m = re.search(r'\++', graph)
1687 if m:
1688 yield (m.group(0), 'diffstat.inserted')
1689 m = re.search(r'-+', graph)
1690 if m:
1691 yield (m.group(0), 'diffstat.deleted')
1692 else:
1693 yield (line, '')
1694 yield ('\n', '')