Make unified diffs group changed lines

The current version of view/unified_diff.py simply zips the lines
together. Meaning when you look at the the diff it's hard to sometimes
see exactly what has changed.

This change changes the unified make_lines logic to group together
differences and then append them in chunks, which more closely matches
a unified diff. For example:

  177  179         def fake_sleep(amount):
  178  180             sleep_calls.append(amount)
  179  181
  180              def fake_audit_location_generator(*args, **kwargs):
  181                  audit_location_generator_calls[0] += 1
  182                  # Makes .container_sync() short-circuit
  183                  yield 'container.db', 'device', 'partition'
  184                  return
       182         class FakeSyncedContainersStore(object):
       183             def synced_containers_generator(self):
       184                 synced_containers_calls[0] += 1
       185                 # Makes .container_sync() short-circuit
       186                 yield 'container.db'
       187                 return
  185  188
  186  189         orig_time = sync.time
  187  190         orig_sleep = sync.sleep
  188  191         orig_ContainerBroker = sync.ContainerBroker

Change-Id: I8cb09bec11fa1746f6b5ebacff51acf1ec6ea376
This commit is contained in:
Matthew Oliver 2016-05-13 17:12:03 +10:00
parent 2efa0606f7
commit d0cc41b49c
1 changed files with 38 additions and 12 deletions

View File

@ -125,47 +125,73 @@ class UnifiedFileReminder(BaseFileReminder):
class UnifiedDiffView(BaseDiffView):
def makeLines(self, diff, lines_to_add, comment_lists):
lines = []
old_cache = []
new_cache = []
for old, new in lines_to_add:
context = self.makeContext(diff, old[0], new[0])
if context.old_ln is not None:
lines.append(UnifiedDiffLine(self.app, context, gitrepo.OLD, old, new,
callback=self.onSelect))
old_cache.append(UnifiedDiffLine(self.app, context, gitrepo.OLD, old, new,
callback=self.onSelect))
else:
lines.extend(old_cache)
lines.extend(new_cache)
old_cache = []
new_cache = []
# see if there are any comments for this line
key = 'old-%s-%s' % (old[0], diff.oldname)
old_list = comment_lists.pop(key, [])
while old_list:
(old_comment_key, old_comment) = old_list.pop(0)
lines.append(UnifiedDiffComment(context, gitrepo.OLD, old_comment))
old_cache.append(UnifiedDiffComment(context, gitrepo.OLD, old_comment))
# see if there are any draft comments for this line
key = 'olddraft-%s-%s' % (old[0], diff.oldname)
old_list = comment_lists.pop(key, [])
while old_list:
(old_comment_key, old_comment) = old_list.pop(0)
lines.append(UnifiedDiffCommentEdit(self.app,
old_cache.append(UnifiedDiffCommentEdit(self.app,
context,
gitrepo.OLD,
old_comment_key,
old_comment))
# new line
if context.new_ln is not None and new[1] != ' ':
lines.append(UnifiedDiffLine(self.app, context, gitrepo.NEW, old, new,
callback=self.onSelect))
if old_cache:
new_cache.append(UnifiedDiffLine(self.app, context, gitrepo.NEW, old, new,
callback=self.onSelect))
else:
lines.append(UnifiedDiffLine(self.app, context, gitrepo.NEW, old, new,
callback=self.onSelect))
# see if there are any comments for this line
key = 'new-%s-%s' % (new[0], diff.newname)
new_list = comment_lists.pop(key, [])
while new_list:
(new_comment_key, new_comment) = new_list.pop(0)
lines.append(UnifiedDiffComment(context, gitrepo.NEW, new_comment))
if old_cache:
new_cache.append(UnifiedDiffComment(context, gitrepo.NEW, new_comment))
else:
lines.append(UnifiedDiffComment(context, gitrepo.NEW, new_comment))
# see if there are any draft comments for this line
key = 'newdraft-%s-%s' % (new[0], diff.newname)
new_list = comment_lists.pop(key, [])
while new_list:
(new_comment_key, new_comment) = new_list.pop(0)
lines.append(UnifiedDiffCommentEdit(self.app,
context,
gitrepo.NEW,
new_comment_key,
new_comment))
if old_cache:
new_cache.append(UnifiedDiffCommentEdit(self.app,
context,
gitrepo.NEW,
new_comment_key,
new_comment))
else:
lines.append(UnifiedDiffCommentEdit(self.app,
context,
gitrepo.NEW,
new_comment_key,
new_comment))
else:
if old_cache:
lines.extend(old_cache)
if new_cache:
lines.extend(new_cache)
return lines
def makeFileReminder(self):