annotate pylons_app/lib/differ.py @ 152:0c00fbaff55a

Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 15 May 2010 19:53:23 +0200
parents ffddbd80649e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
130
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 # original copyright: 2007-2008 by Armin Ronacher
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 # licensed under the BSD license.
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5 import re, difflib
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 def render_udiff(udiff, differ='udiff'):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 """Renders the udiff into multiple chunks of nice looking tables.
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 The return value is a list of those tables.
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 """
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 return DiffProcessor(udiff, differ).prepare()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13 class DiffProcessor(object):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 """Give it a unified diff and it returns a list of the files that were
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 mentioned in the diff together with a dict of meta information that
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 can be used to render it in a HTML template.
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17 """
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 _chunk_re = re.compile(r'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)')
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
20 def __init__(self, udiff, differ):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 """
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 :param udiff: a text in udiff format
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23 """
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 if isinstance(udiff, basestring):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 udiff = udiff.splitlines(1)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
26
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 self.lines = map(self.escaper, udiff)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
29 # Select a differ.
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
30 if differ == 'difflib':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
31 self.differ = self._highlight_line_difflib
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
32 else:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
33 self.differ = self._highlight_line_udiff
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
34
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
35
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
36 def escaper(self, string):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
37 return string.replace('<', '&lt;').replace('>', '&gt;')
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
38
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
39 def _extract_rev(self, line1, line2):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
40 """Extract the filename and revision hint from a line."""
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
41 try:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
42 if line1.startswith('--- ') and line2.startswith('+++ '):
152
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
43 l1 = line1[4:].split(None, 1)
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
44 old_filename = l1[0] if len(l1) >= 1 else None
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
45 old_rev = l1[1] if len(l1) == 2 else 'old'
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
46
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
47 l2 = line1[4:].split(None, 1)
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
48 new_filename = l2[0] if len(l2) >= 1 else None
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
49 new_rev = l2[1] if len(l2) == 2 else 'new'
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
50
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
51 return old_filename, new_rev, old_rev
130
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
52 except (ValueError, IndexError):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
53 pass
152
0c00fbaff55a Fixed differ to properly extract filenames, and dates from diff file. and swaped order of columns with lines nr in diff html
Marcin Kuzminski <marcin@python-works.com>
parents: 130
diff changeset
54
130
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
55 return None, None, None
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
56
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
57 def _highlight_line_difflib(self, line, next):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
58 """Highlight inline changes in both lines."""
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
59
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
60 if line['action'] == 'del':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
61 old, new = line, next
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
62 else:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
63 old, new = next, line
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
64
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
65 oldwords = re.split(r'(\W)', old['line'])
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
66 newwords = re.split(r'(\W)', new['line'])
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
67
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
68 sequence = difflib.SequenceMatcher(None, oldwords, newwords)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
69
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
70 oldfragments, newfragments = [], []
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
71 for tag, i1, i2, j1, j2 in sequence.get_opcodes():
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
72 oldfrag = ''.join(oldwords[i1:i2])
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
73 newfrag = ''.join(newwords[j1:j2])
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
74 if tag != 'equal':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
75 if oldfrag:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
76 oldfrag = '<del>%s</del>' % oldfrag
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
77 if newfrag:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
78 newfrag = '<ins>%s</ins>' % newfrag
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
79 oldfragments.append(oldfrag)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
80 newfragments.append(newfrag)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
81
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
82 old['line'] = "".join(oldfragments)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
83 new['line'] = "".join(newfragments)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
84
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
85 def _highlight_line_udiff(self, line, next):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
86 """Highlight inline changes in both lines."""
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
87 start = 0
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
88 limit = min(len(line['line']), len(next['line']))
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
89 while start < limit and line['line'][start] == next['line'][start]:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
90 start += 1
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
91 end = -1
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
92 limit -= start
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
93 while - end <= limit and line['line'][end] == next['line'][end]:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
94 end -= 1
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
95 end += 1
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
96 if start or end:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
97 def do(l):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
98 last = end + len(l['line'])
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
99 if l['action'] == 'add':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
100 tag = 'ins'
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
101 else:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
102 tag = 'del'
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
103 l['line'] = '%s<%s>%s</%s>%s' % (
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
104 l['line'][:start],
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
105 tag,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
106 l['line'][start:last],
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
107 tag,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
108 l['line'][last:]
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
109 )
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
110 do(line)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
111 do(next)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
112
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
113 def _parse_udiff(self):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
114 """Parse the diff an return data for the template."""
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
115 lineiter = iter(self.lines)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
116 files = []
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
117 try:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
118 line = lineiter.next()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
119 while 1:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
120 # continue until we found the old file
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 if not line.startswith('--- '):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122 line = lineiter.next()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 continue
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
125 chunks = []
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
126 filename, old_rev, new_rev = \
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
127 self._extract_rev(line, lineiter.next())
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
128 files.append({
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
129 'filename': filename,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
130 'old_revision': old_rev,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
131 'new_revision': new_rev,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
132 'chunks': chunks
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
133 })
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
134
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
135 line = lineiter.next()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
136 while line:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
137 match = self._chunk_re.match(line)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
138 if not match:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
139 break
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
140
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
141 lines = []
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
142 chunks.append(lines)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
143
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
144 old_line, old_end, new_line, new_end = \
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
145 [int(x or 1) for x in match.groups()[:-1]]
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
146 old_line -= 1
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
147 new_line -= 1
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
148 context = match.groups()[-1]
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
149 old_end += old_line
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
150 new_end += new_line
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
151
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
152 if context:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
153 lines.append({
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
154 'old_lineno': None,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
155 'new_lineno': None,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
156 'action': 'context',
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
157 'line': line,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
158 })
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
159
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
160 line = lineiter.next()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
161
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
162 while old_line < old_end or new_line < new_end:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
163 if line:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
164 command, line = line[0], line[1:]
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
165 else:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
166 command = ' '
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
167 affects_old = affects_new = False
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
168
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
169 # ignore those if we don't expect them
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
170 if command in '#@':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
171 continue
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
172 elif command == '+':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
173 affects_new = True
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
174 action = 'add'
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
175 elif command == '-':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
176 affects_old = True
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
177 action = 'del'
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
178 else:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
179 affects_old = affects_new = True
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
180 action = 'unmod'
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
181
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
182 old_line += affects_old
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
183 new_line += affects_new
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
184 lines.append({
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
185 'old_lineno': affects_old and old_line or '',
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
186 'new_lineno': affects_new and new_line or '',
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
187 'action': action,
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
188 'line': line
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
189 })
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
190 line = lineiter.next()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
191
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
192 except StopIteration:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
193 pass
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
194
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
195 # highlight inline changes
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
196 for file in files:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
197 for chunk in chunks:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
198 lineiter = iter(chunk)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
199 first = True
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
200 try:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
201 while 1:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
202 line = lineiter.next()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
203 if line['action'] != 'unmod':
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
204 nextline = lineiter.next()
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
205 if nextline['action'] == 'unmod' or \
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
206 nextline['action'] == line['action']:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
207 continue
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
208 self.differ(line, nextline)
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
209 except StopIteration:
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
210 pass
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
211
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
212 return files
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
213
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
214 def prepare(self):
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
215 """Prepare the passed udiff for HTML rendering."""
ffddbd80649e Added differ lib from mercurial.
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
216 return self._parse_udiff()