changeset 6131:6b723a49a9a1

diff: only highlight of difference between del and add line for one-liners Comparing a single del line with the first of several add lines would often be quite arbitrary and misleading. Note: the non-gitdiff code paths in diffs.py are incomplete and unused and unusable.
author Mads Kiilerich <madski@unity3d.com>
date Fri, 12 Aug 2016 03:13:59 +0200
parents 59a38ec5ad8b
children 190cb30841de
files kallithea/lib/diffs.py kallithea/tests/fixtures/markuptest.diff kallithea/tests/models/test_diff_parsers.py
diffstat 3 files changed, 35 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/diffs.py	Fri Aug 12 03:04:48 2016 +0200
+++ b/kallithea/lib/diffs.py	Fri Aug 12 03:13:59 2016 +0200
@@ -283,15 +283,13 @@
             self.removes += 1
         return safe_unicode(l)
 
-    def _highlight_line_difflib(self, line, next_):
+    def _highlight_line_difflib(self, old, new):
         """
         Highlight inline changes in both lines.
         """
 
-        if line['action'] == 'del':
-            old, new = line, next_
-        else:
-            old, new = next_, line
+        assert old['action'] == 'del'
+        assert new['action'] == 'add'
 
         oldwords = self._token_re.split(old['line'])
         newwords = self._token_re.split(new['line'])
@@ -484,19 +482,34 @@
         if not inline_diff:
             return diff_container(_files)
 
-        # highlight inline changes
+        # highlight inline changes when one del is followed by one add
         for diff_data in _files:
             for chunk in diff_data['chunks']:
                 lineiter = iter(chunk)
                 try:
-                    while 1:
-                        line = lineiter.next()
-                        if line['action'] not in ['unmod', 'context']:
-                            nextline = lineiter.next()
-                            if nextline['action'] in ['unmod', 'context'] or \
-                               nextline['action'] == line['action']:
-                                continue
-                            self.differ(line, nextline)
+                    peekline = lineiter.next()
+                    while True:
+                        # find a first del line
+                        while peekline['action'] != 'del':
+                            peekline = lineiter.next()
+                        delline = peekline
+                        peekline = lineiter.next()
+                        # if not followed by add, eat all following del lines
+                        if peekline['action'] != 'add':
+                            while peekline['action'] == 'del':
+                                peekline = lineiter.next()
+                            continue
+                        # found an add - make sure it is the only one
+                        addline = peekline
+                        try:
+                            peekline = lineiter.next()
+                        except StopIteration:
+                            # add was last line - ok
+                            self.differ(delline, addline)
+                            raise
+                        if peekline['action'] != 'add':
+                            # there was only one add line - ok
+                            self.differ(delline, addline)
                 except StopIteration:
                     pass
 
--- a/kallithea/tests/fixtures/markuptest.diff	Fri Aug 12 03:04:48 2016 +0200
+++ b/kallithea/tests/fixtures/markuptest.diff	Fri Aug 12 03:13:59 2016 +0200
@@ -1,7 +1,7 @@
 diff --git a/f b/f
 --- a/f	
 +++ b/f	
-@@ -51,5 +51,12 @@
+@@ -51,6 +51,13 @@
  	begin();
  	
 +	int foo;
@@ -15,3 +15,5 @@
 +	
 +	#define MAX_STEPS (64)
  
+-	#define MIN_STEPS (48)
++	#define MIN_STEPS (42)
--- a/kallithea/tests/models/test_diff_parsers.py	Fri Aug 12 03:04:48 2016 +0200
+++ b/kallithea/tests/models/test_diff_parsers.py	Fri Aug 12 03:13:59 2016 +0200
@@ -298,7 +298,7 @@
         s = ''.join(l)
         print s
         assert s == r'''
-context ... ... u'@@ -51,5 +51,12 @@\n'
+context ... ... u'@@ -51,6 +51,13 @@\n'
 unmod    51  51 u'<u>\t</u>begin();\n'
 unmod    52  52 u'<u>\t</u>\n'
 add      53     u'<u>\t</u>int foo;<u class="cr"></u>\n'
@@ -308,8 +308,10 @@
 add      57     u'<u>\t</u>int tab;<u>\t</u>\n'
 add      58     u'<u>\t</u>\n'
 unmod    59  53 u' <i></i>'
-del          54 u'<u>\t</u><del>#define MAX_STEPS (48)</del>\n'
-add      60     u'<u>\t</u><ins><u class="cr"></u></ins>\n'
+del          54 u'<u>\t</u>#define MAX_STEPS (48)\n'
+add      60     u'<u>\t</u><u class="cr"></u>\n'
 add      61     u'<u>\t</u>#define MAX_STEPS (64)<u class="cr"></u>\n'
 unmod    62  55 u'\n'
+del          56 u'<u>\t</u>#define MIN_STEPS (<del>48</del>)\n'
+add      63     u'<u>\t</u>#define MIN_STEPS (<ins>42</ins>)\n'
 '''