comparison rhodecode/lib/diffs.py @ 2391:91fae60bf2b6 codereview

merge with beta
author Marcin Kuzminski <marcin@python-works.com>
date Tue, 05 Jun 2012 21:22:23 +0200
parents 9d61aad859bc a455b2c79c85
children 60dfc369df1d
comparison
equal deleted inserted replaced
2364:9d61aad859bc 2391:91fae60bf2b6
133 mentioned in the diff together with a dict of meta information that 133 mentioned in the diff together with a dict of meta information that
134 can be used to render it in a HTML template. 134 can be used to render it in a HTML template.
135 """ 135 """
136 _chunk_re = re.compile(r'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)') 136 _chunk_re = re.compile(r'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)')
137 137
138 def __init__(self, diff, differ='diff', format='udiff'): 138 def __init__(self, diff, differ='diff', format='gitdiff'):
139 """ 139 """
140 :param diff: a text in diff format or generator 140 :param diff: a text in diff format or generator
141 :param format: format of diff passed, `udiff` or `gitdiff` 141 :param format: format of diff passed, `udiff` or `gitdiff`
142 """ 142 """
143 if isinstance(diff, basestring): 143 if isinstance(diff, basestring):
287 l['line'][last:] 287 l['line'][last:]
288 ) 288 )
289 do(line) 289 do(line)
290 do(next_) 290 do(next_)
291 291
292 def _parse_udiff(self): 292 def _parse_udiff(self, inline_diff=True):
293 """ 293 """
294 Parse the diff an return data for the template. 294 Parse the diff an return data for the template.
295 """ 295 """
296 lineiter = self.lines 296 lineiter = self.lines
297 files = [] 297 files = []
384 'action': action, 384 'action': action,
385 'line': line 385 'line': line
386 }) 386 })
387 387
388 line = lineiter.next() 388 line = lineiter.next()
389
389 except StopIteration: 390 except StopIteration:
390 pass 391 pass
392
393 sorter = lambda info: {'A': 0, 'M': 1, 'D': 2}.get(info['operation'])
394 if inline_diff is False:
395 return sorted(files, key=sorter)
391 396
392 # highlight inline changes 397 # highlight inline changes
393 for diff_data in files: 398 for diff_data in files:
394 for chunk in diff_data['chunks']: 399 for chunk in diff_data['chunks']:
395 lineiter = iter(chunk) 400 lineiter = iter(chunk)
402 nextline['action'] == line['action']: 407 nextline['action'] == line['action']:
403 continue 408 continue
404 self.differ(line, nextline) 409 self.differ(line, nextline)
405 except StopIteration: 410 except StopIteration:
406 pass 411 pass
407 return files 412
408 413 return sorted(files, key=sorter)
409 def prepare(self): 414
415 def prepare(self, inline_diff=True):
410 """ 416 """
411 Prepare the passed udiff for HTML rendering. It'l return a list 417 Prepare the passed udiff for HTML rendering. It'l return a list
412 of dicts 418 of dicts
413 """ 419 """
414 return self._parse_udiff() 420 return self._parse_udiff(inline_diff=inline_diff)
415 421
416 def _safe_id(self, idstring): 422 def _safe_id(self, idstring):
417 """Make a string safe for including in an id attribute. 423 """Make a string safe for including in an id attribute.
418 424
419 The HTML spec says that id attributes 'must begin with 425 The HTML spec says that id attributes 'must begin with