Show More
@@ -0,0 +1,209 b'' | |||||
|
1 | # -*- coding: utf-8 -*- | |||
|
2 | # original copyright: 2007-2008 by Armin Ronacher | |||
|
3 | # licensed under the BSD license. | |||
|
4 | ||||
|
5 | import re, difflib | |||
|
6 | ||||
|
7 | def render_udiff(udiff, differ='udiff'): | |||
|
8 | """Renders the udiff into multiple chunks of nice looking tables. | |||
|
9 | The return value is a list of those tables. | |||
|
10 | """ | |||
|
11 | return DiffProcessor(udiff, differ).prepare() | |||
|
12 | ||||
|
13 | class DiffProcessor(object): | |||
|
14 | """Give it a unified diff and it returns a list of the files that were | |||
|
15 | mentioned in the diff together with a dict of meta information that | |||
|
16 | can be used to render it in a HTML template. | |||
|
17 | """ | |||
|
18 | _chunk_re = re.compile(r'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)') | |||
|
19 | ||||
|
20 | def __init__(self, udiff, differ): | |||
|
21 | """ | |||
|
22 | :param udiff: a text in udiff format | |||
|
23 | """ | |||
|
24 | if isinstance(udiff, basestring): | |||
|
25 | udiff = udiff.splitlines(1) | |||
|
26 | ||||
|
27 | self.lines = map(self.escaper, udiff) | |||
|
28 | ||||
|
29 | # Select a differ. | |||
|
30 | if differ == 'difflib': | |||
|
31 | self.differ = self._highlight_line_difflib | |||
|
32 | else: | |||
|
33 | self.differ = self._highlight_line_udiff | |||
|
34 | ||||
|
35 | ||||
|
36 | def escaper(self, string): | |||
|
37 | return string.replace('<', '<').replace('>', '>') | |||
|
38 | ||||
|
39 | def _extract_rev(self, line1, line2): | |||
|
40 | """Extract the filename and revision hint from a line.""" | |||
|
41 | try: | |||
|
42 | if line1.startswith('--- ') and line2.startswith('+++ '): | |||
|
43 | filename, old_rev = line1[4:].split(None, 1) | |||
|
44 | new_rev = line2[4:].split(None, 1)[1] | |||
|
45 | return filename, 'old', 'new' | |||
|
46 | except (ValueError, IndexError): | |||
|
47 | pass | |||
|
48 | return None, None, None | |||
|
49 | ||||
|
50 | def _highlight_line_difflib(self, line, next): | |||
|
51 | """Highlight inline changes in both lines.""" | |||
|
52 | ||||
|
53 | if line['action'] == 'del': | |||
|
54 | old, new = line, next | |||
|
55 | else: | |||
|
56 | old, new = next, line | |||
|
57 | ||||
|
58 | oldwords = re.split(r'(\W)', old['line']) | |||
|
59 | newwords = re.split(r'(\W)', new['line']) | |||
|
60 | ||||
|
61 | sequence = difflib.SequenceMatcher(None, oldwords, newwords) | |||
|
62 | ||||
|
63 | oldfragments, newfragments = [], [] | |||
|
64 | for tag, i1, i2, j1, j2 in sequence.get_opcodes(): | |||
|
65 | oldfrag = ''.join(oldwords[i1:i2]) | |||
|
66 | newfrag = ''.join(newwords[j1:j2]) | |||
|
67 | if tag != 'equal': | |||
|
68 | if oldfrag: | |||
|
69 | oldfrag = '<del>%s</del>' % oldfrag | |||
|
70 | if newfrag: | |||
|
71 | newfrag = '<ins>%s</ins>' % newfrag | |||
|
72 | oldfragments.append(oldfrag) | |||
|
73 | newfragments.append(newfrag) | |||
|
74 | ||||
|
75 | old['line'] = "".join(oldfragments) | |||
|
76 | new['line'] = "".join(newfragments) | |||
|
77 | ||||
|
78 | def _highlight_line_udiff(self, line, next): | |||
|
79 | """Highlight inline changes in both lines.""" | |||
|
80 | start = 0 | |||
|
81 | limit = min(len(line['line']), len(next['line'])) | |||
|
82 | while start < limit and line['line'][start] == next['line'][start]: | |||
|
83 | start += 1 | |||
|
84 | end = -1 | |||
|
85 | limit -= start | |||
|
86 | while - end <= limit and line['line'][end] == next['line'][end]: | |||
|
87 | end -= 1 | |||
|
88 | end += 1 | |||
|
89 | if start or end: | |||
|
90 | def do(l): | |||
|
91 | last = end + len(l['line']) | |||
|
92 | if l['action'] == 'add': | |||
|
93 | tag = 'ins' | |||
|
94 | else: | |||
|
95 | tag = 'del' | |||
|
96 | l['line'] = '%s<%s>%s</%s>%s' % ( | |||
|
97 | l['line'][:start], | |||
|
98 | tag, | |||
|
99 | l['line'][start:last], | |||
|
100 | tag, | |||
|
101 | l['line'][last:] | |||
|
102 | ) | |||
|
103 | do(line) | |||
|
104 | do(next) | |||
|
105 | ||||
|
106 | def _parse_udiff(self): | |||
|
107 | """Parse the diff an return data for the template.""" | |||
|
108 | lineiter = iter(self.lines) | |||
|
109 | files = [] | |||
|
110 | try: | |||
|
111 | line = lineiter.next() | |||
|
112 | while 1: | |||
|
113 | # continue until we found the old file | |||
|
114 | if not line.startswith('--- '): | |||
|
115 | line = lineiter.next() | |||
|
116 | continue | |||
|
117 | ||||
|
118 | chunks = [] | |||
|
119 | filename, old_rev, new_rev = \ | |||
|
120 | self._extract_rev(line, lineiter.next()) | |||
|
121 | files.append({ | |||
|
122 | 'filename': filename, | |||
|
123 | 'old_revision': old_rev, | |||
|
124 | 'new_revision': new_rev, | |||
|
125 | 'chunks': chunks | |||
|
126 | }) | |||
|
127 | ||||
|
128 | line = lineiter.next() | |||
|
129 | while line: | |||
|
130 | match = self._chunk_re.match(line) | |||
|
131 | if not match: | |||
|
132 | break | |||
|
133 | ||||
|
134 | lines = [] | |||
|
135 | chunks.append(lines) | |||
|
136 | ||||
|
137 | old_line, old_end, new_line, new_end = \ | |||
|
138 | [int(x or 1) for x in match.groups()[:-1]] | |||
|
139 | old_line -= 1 | |||
|
140 | new_line -= 1 | |||
|
141 | context = match.groups()[-1] | |||
|
142 | old_end += old_line | |||
|
143 | new_end += new_line | |||
|
144 | ||||
|
145 | if context: | |||
|
146 | lines.append({ | |||
|
147 | 'old_lineno': None, | |||
|
148 | 'new_lineno': None, | |||
|
149 | 'action': 'context', | |||
|
150 | 'line': line, | |||
|
151 | }) | |||
|
152 | ||||
|
153 | line = lineiter.next() | |||
|
154 | ||||
|
155 | while old_line < old_end or new_line < new_end: | |||
|
156 | if line: | |||
|
157 | command, line = line[0], line[1:] | |||
|
158 | else: | |||
|
159 | command = ' ' | |||
|
160 | affects_old = affects_new = False | |||
|
161 | ||||
|
162 | # ignore those if we don't expect them | |||
|
163 | if command in '#@': | |||
|
164 | continue | |||
|
165 | elif command == '+': | |||
|
166 | affects_new = True | |||
|
167 | action = 'add' | |||
|
168 | elif command == '-': | |||
|
169 | affects_old = True | |||
|
170 | action = 'del' | |||
|
171 | else: | |||
|
172 | affects_old = affects_new = True | |||
|
173 | action = 'unmod' | |||
|
174 | ||||
|
175 | old_line += affects_old | |||
|
176 | new_line += affects_new | |||
|
177 | lines.append({ | |||
|
178 | 'old_lineno': affects_old and old_line or '', | |||
|
179 | 'new_lineno': affects_new and new_line or '', | |||
|
180 | 'action': action, | |||
|
181 | 'line': line | |||
|
182 | }) | |||
|
183 | line = lineiter.next() | |||
|
184 | ||||
|
185 | except StopIteration: | |||
|
186 | pass | |||
|
187 | ||||
|
188 | # highlight inline changes | |||
|
189 | for file in files: | |||
|
190 | for chunk in chunks: | |||
|
191 | lineiter = iter(chunk) | |||
|
192 | first = True | |||
|
193 | try: | |||
|
194 | while 1: | |||
|
195 | line = lineiter.next() | |||
|
196 | if line['action'] != 'unmod': | |||
|
197 | nextline = lineiter.next() | |||
|
198 | if nextline['action'] == 'unmod' or \ | |||
|
199 | nextline['action'] == line['action']: | |||
|
200 | continue | |||
|
201 | self.differ(line, nextline) | |||
|
202 | except StopIteration: | |||
|
203 | pass | |||
|
204 | ||||
|
205 | return files | |||
|
206 | ||||
|
207 | def prepare(self): | |||
|
208 | """Prepare the passed udiff for HTML rendering.""" | |||
|
209 | return self._parse_udiff() |
@@ -1,55 +1,60 b'' | |||||
1 | import logging |
|
1 | import logging | |
2 |
|
2 | |||
3 | from pylons import request, response, session, tmpl_context as c, url, config, app_globals as g |
|
3 | from pylons import request, response, session, tmpl_context as c, url, config, app_globals as g | |
4 | from pylons.controllers.util import abort, redirect |
|
4 | from pylons.controllers.util import abort, redirect | |
5 |
|
5 | |||
6 | from pylons_app.lib.base import BaseController, render |
|
6 | from pylons_app.lib.base import BaseController, render | |
7 | from pylons_app.lib.utils import get_repo_slug |
|
7 | from pylons_app.lib.utils import get_repo_slug | |
8 | from pylons_app.model.hg_model import HgModel |
|
8 | from pylons_app.model.hg_model import HgModel | |
9 | log = logging.getLogger(__name__) |
|
9 | log = logging.getLogger(__name__) | |
10 |
|
10 | |||
11 | class FilesController(BaseController): |
|
11 | class FilesController(BaseController): | |
12 | def __before__(self): |
|
12 | def __before__(self): | |
13 | c.repos_prefix = config['repos_name'] |
|
13 | c.repos_prefix = config['repos_name'] | |
14 | c.repo_name = get_repo_slug(request) |
|
14 | c.repo_name = get_repo_slug(request) | |
15 |
|
15 | |||
16 | def index(self, repo_name, revision, f_path): |
|
16 | def index(self, repo_name, revision, f_path): | |
17 | hg_model = HgModel() |
|
17 | hg_model = HgModel() | |
18 | c.repo = repo = hg_model.get_repo(c.repo_name) |
|
18 | c.repo = repo = hg_model.get_repo(c.repo_name) | |
19 | c.cur_rev = revision |
|
19 | c.cur_rev = revision | |
20 | c.f_path = f_path |
|
20 | c.f_path = f_path | |
21 | c.changeset = repo.get_changeset(repo._get_revision(revision)) |
|
21 | c.changeset = repo.get_changeset(repo._get_revision(revision)) | |
22 |
|
22 | |||
23 | c.files_list = c.changeset.get_node(f_path) |
|
23 | c.files_list = c.changeset.get_node(f_path) | |
24 |
|
24 | |||
25 | c.file_history = self._get_history(repo, c.files_list, f_path) |
|
25 | c.file_history = self._get_history(repo, c.files_list, f_path) | |
26 | return render('files/files.html') |
|
26 | return render('files/files.html') | |
27 |
|
27 | |||
28 | def diff(self, repo_name, f_path): |
|
28 | def diff(self, repo_name, f_path): | |
29 | hg_model = HgModel() |
|
29 | hg_model = HgModel() | |
30 | diff1 = request.GET.get('diff1') |
|
30 | diff1 = request.GET.get('diff1') | |
31 | diff2 = request.GET.get('diff2') |
|
31 | diff2 = request.GET.get('diff2') | |
32 | c.f_path = f_path |
|
32 | c.f_path = f_path | |
33 | c.repo = hg_model.get_repo(c.repo_name) |
|
33 | c.repo = hg_model.get_repo(c.repo_name) | |
34 | c.changeset_1 = c.repo.get_changeset(diff1) |
|
34 | c.changeset_1 = c.repo.get_changeset(diff1) | |
35 | c.changeset_2 = c.repo.get_changeset(diff2) |
|
35 | c.changeset_2 = c.repo.get_changeset(diff2) | |
36 |
|
36 | |||
37 | c.file_1 = c.changeset_1.get_node(f_path).content |
|
37 | c.file_1 = c.changeset_1.get_node(f_path).content | |
38 | c.file_2 = c.changeset_2.get_node(f_path).content |
|
38 | c.file_2 = c.changeset_2.get_node(f_path).content | |
39 | c.diff1 = 'r%s:%s' % (c.changeset_1.revision, c.changeset_1._short) |
|
39 | c.diff1 = 'r%s:%s' % (c.changeset_1.revision, c.changeset_1._short) | |
40 | c.diff2 = 'r%s:%s' % (c.changeset_2.revision, c.changeset_2._short) |
|
40 | c.diff2 = 'r%s:%s' % (c.changeset_2.revision, c.changeset_2._short) | |
41 | from difflib import unified_diff |
|
41 | from difflib import unified_diff | |
42 | d = unified_diff(c.file_1.splitlines(1), c.file_2.splitlines(1)) |
|
42 | d = unified_diff(c.file_1.splitlines(1), c.file_2.splitlines(1)) | |
43 | c.diff = ''.join(d) |
|
43 | c.diff = ''.join(d) | |
|
44 | ||||
|
45 | from pylons_app.lib.differ import render_udiff | |||
|
46 | d2 = unified_diff(c.file_1.splitlines(1), c.file_2.splitlines(1)) | |||
|
47 | c.diff_2 = render_udiff(udiff=d2) | |||
|
48 | ||||
44 | return render('files/file_diff.html') |
|
49 | return render('files/file_diff.html') | |
45 |
|
50 | |||
46 | def _get_history(self, repo, node, f_path): |
|
51 | def _get_history(self, repo, node, f_path): | |
47 | from vcs.nodes import NodeKind |
|
52 | from vcs.nodes import NodeKind | |
48 | if not node.kind is NodeKind.FILE: |
|
53 | if not node.kind is NodeKind.FILE: | |
49 | return [] |
|
54 | return [] | |
50 | changesets = node.history |
|
55 | changesets = node.history | |
51 | hist_l = [] |
|
56 | hist_l = [] | |
52 | for chs in changesets: |
|
57 | for chs in changesets: | |
53 | n_desc = 'r%s:%s' % (chs.revision, chs._short) |
|
58 | n_desc = 'r%s:%s' % (chs.revision, chs._short) | |
54 | hist_l.append((chs._short, n_desc,)) |
|
59 | hist_l.append((chs._short, n_desc,)) | |
55 | return hist_l |
|
60 | return hist_l |
@@ -1,35 +1,71 b'' | |||||
1 | <%inherit file="/base/base.html"/> |
|
1 | <%inherit file="/base/base.html"/> | |
2 |
|
2 | |||
3 | <%def name="title()"> |
|
3 | <%def name="title()"> | |
4 | ${_('Repository managment')} |
|
4 | ${_('Repository managment')} | |
5 | </%def> |
|
5 | </%def> | |
6 | <%def name="breadcrumbs()"> |
|
6 | <%def name="breadcrumbs()"> | |
7 | ${h.link_to(u'Home',h.url('/'))} |
|
7 | ${h.link_to(u'Home',h.url('/'))} | |
8 | / |
|
8 | / | |
9 | ${h.link_to(c.repo_name,h.url('files_home',repo_name=c.repo_name))} |
|
9 | ${h.link_to(c.repo_name,h.url('files_home',repo_name=c.repo_name))} | |
10 | / |
|
10 | / | |
11 | ${_('files')} |
|
11 | ${_('files')} | |
12 | </%def> |
|
12 | </%def> | |
13 | <%def name="page_nav()"> |
|
13 | <%def name="page_nav()"> | |
14 | <form action="log"> |
|
14 | <form action="log"> | |
15 | <dl class="search"> |
|
15 | <dl class="search"> | |
16 | <dt><label>Search: </label></dt> |
|
16 | <dt><label>Search: </label></dt> | |
17 | <dd><input type="text" name="rev" /></dd> |
|
17 | <dd><input type="text" name="rev" /></dd> | |
18 | </dl> |
|
18 | </dl> | |
19 | </form> |
|
19 | </form> | |
20 |
|
20 | |||
21 | ${self.menu('files')} |
|
21 | ${self.menu('files')} | |
22 | </%def> |
|
22 | </%def> | |
23 | <%def name="css()"> |
|
23 | <%def name="css()"> | |
24 | <link rel="stylesheet" href="/css/monoblue_custom.css" type="text/css" /> |
|
24 | <link rel="stylesheet" href="/css/monoblue_custom.css" type="text/css" /> | |
25 | <link rel="stylesheet" href="/css/pygments_diff.css" type="text/css" /> |
|
25 | <link rel="stylesheet" href="/css/pygments_diff.css" type="text/css" /> | |
26 | </%def> |
|
26 | </%def> | |
27 | <div></div> |
|
27 | <div></div> | |
28 | <%def name="main()"> |
|
28 | <%def name="main()"> | |
29 | <h2 class="no-link no-border">${'%s: %s@%s %s %s' % (_('File diff'),c.f_path,c.diff2,'→',c.diff1)|n}</h2> |
|
29 | <h2 class="no-link no-border">${'%s: %s@%s %s %s' % (_('File diff'),c.f_path,c.diff2,'→',c.diff1)|n}</h2> | |
30 | <div id="files_data"> |
|
30 | <div id="files_data"> | |
31 | <div id="body" class="codeblock"> |
|
31 | <div id="body" class="codeblock"> | |
32 | ${h.pygmentize(c.diff,linenos=True,anchorlinenos=True,cssclass="code-diff")} |
|
32 | ${h.pygmentize(c.diff,linenos=True,anchorlinenos=True,cssclass="code-diff")} | |
33 | </div> |
|
33 | </div> | |
34 | </div> |
|
34 | </div> | |
|
35 | <div style="clear:both"></div> | |||
|
36 | <div id="files_data"> | |||
|
37 | <div id="body" class="codeblock"> | |||
|
38 | <table class='highlighttable'> | |||
|
39 | %for x in c.diff_2[0]['chunks']: | |||
|
40 | %for y in x: | |||
|
41 | <tr class="line ${y['action']}"> | |||
|
42 | <td class="lineno_new"> | |||
|
43 | <div class="linenodiv"> | |||
|
44 | <pre> | |||
|
45 | ${y['new_lineno']} | |||
|
46 | </pre> | |||
|
47 | </div> | |||
|
48 | </td> | |||
|
49 | <td class="lineno_old"> | |||
|
50 | <div class="linenodiv"> | |||
|
51 | <pre> | |||
|
52 | ${y['old_lineno']} | |||
|
53 | </pre> | |||
|
54 | </div> | |||
|
55 | </td> | |||
|
56 | <td class="code"> | |||
|
57 | <div class="code-diff"> | |||
|
58 | <pre> | |||
|
59 | <span> | |||
|
60 | ${y} | |||
|
61 | </span> | |||
|
62 | </pre> | |||
|
63 | </div> | |||
|
64 | </td> | |||
|
65 | </tr> | |||
|
66 | %endfor$ | |||
|
67 | %endfor | |||
|
68 | </table> | |||
|
69 | </div> | |||
|
70 | </div> | |||
35 | </%def> No newline at end of file |
|
71 | </%def> |
General Comments 0
You need to be logged in to leave comments.
Login now