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