[diffoscope] 01/03: Suppress decimal line numbers for xxd output

Ximin Luo infinity0 at debian.org
Tue Sep 6 22:58:32 CEST 2016


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository diffoscope.

commit 341be0776b7432e8e0fc94b85e2a9d18027eb6d3
Author: Ximin Luo <infinity0 at debian.org>
Date:   Thu Aug 25 02:31:37 2016 +0200

    Suppress decimal line numbers for xxd output
---
 diffoscope/comparators/binary.py |  4 +++-
 diffoscope/difference.py         | 12 +++++++++---
 diffoscope/presenters/html.py    | 32 +++++++++++++++++++-------------
 3 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/diffoscope/comparators/binary.py b/diffoscope/comparators/binary.py
index 55b152e..a72ad6f 100644
--- a/diffoscope/comparators/binary.py
+++ b/diffoscope/comparators/binary.py
@@ -56,7 +56,9 @@ def compare_binary_files(file1, file2, source=None):
     import diffoscope.comparators.utils
 
     try:
-        return Difference.from_command(diffoscope.comparators.utils.Xxd, file1.path, file2.path, source=[file1.name, file2.name])
+        return Difference.from_command(
+            diffoscope.comparators.utils.Xxd, file1.path, file2.path,
+            source=[file1.name, file2.name], has_internal_linenos=True)
     except RequiredToolNotFound:
         hexdump1 = hexdump_fallback(file1.path)
         hexdump2 = hexdump_fallback(file2.path)
diff --git a/diffoscope/difference.py b/diffoscope/difference.py
index b2ed8a7..edffc72 100644
--- a/diffoscope/difference.py
+++ b/diffoscope/difference.py
@@ -273,7 +273,7 @@ def diff(feeder1, feeder2):
 
 
 class Difference(object):
-    def __init__(self, unified_diff, path1, path2, source=None, comment=None):
+    def __init__(self, unified_diff, path1, path2, source=None, comment=None, has_internal_linenos=False):
         self._comments = []
         if comment:
             if type(comment) is list:
@@ -297,18 +297,20 @@ class Difference(object):
             raise TypeError("path1/source[0] is not a string")
         if not isinstance(self._source2, str):
             raise TypeError("path2/source[1] is not a string")
+        # Whether the unified_diff already contains line numbers inside itself
+        self._has_internal_linenos = has_internal_linenos
         self._details = []
 
     def __repr__(self):
         return '<Difference %s -- %s %s>' % (self._source1, self._source2, self._details)
 
     @staticmethod
-    def from_feeder(feeder1, feeder2, path1, path2, source=None, comment=None):
+    def from_feeder(feeder1, feeder2, path1, path2, source=None, comment=None, **kwargs):
         try:
             unified_diff = diff(feeder1, feeder2)
             if not unified_diff:
                 return None
-            return Difference(unified_diff, path1, path2, source, comment)
+            return Difference(unified_diff, path1, path2, source, comment, **kwargs)
         except RequiredToolNotFound:
             difference = Difference(None, path1, path2, source)
             difference.add_comment('diff is not available!')
@@ -391,6 +393,10 @@ class Difference(object):
         return self._unified_diff
 
     @property
+    def has_internal_linenos(self):
+        return self._has_internal_linenos
+
+    @property
     def details(self):
         return self._details
 
diff --git a/diffoscope/presenters/html.py b/diffoscope/presenters/html.py
index 6667e16..329e71f 100644
--- a/diffoscope/presenters/html.py
+++ b/diffoscope/presenters/html.py
@@ -197,7 +197,7 @@ def create_limited_print_func(print_func, max_page_size):
 
 buf = []
 add_cpt, del_cpt = 0, 0
-line1, line2 = 0, 0
+line1, line2, has_internal_linenos = 0, 0, True
 hunk_off1, hunk_size1, hunk_off2, hunk_size2 = 0, 0, 0, 0
 
 
@@ -321,8 +321,7 @@ def output_hunk(print_func):
 
 
 def output_line(print_func, s1, s2):
-    global line1
-    global line2
+    global line1, line2, has_internal_linenos
 
     orig1 = s1
     orig2 = s2
@@ -349,16 +348,22 @@ def output_line(print_func, s1, s2):
     print_func(u'<tr class="diff%s">' % type_name)
     try:
         if s1:
-            print_func(u'<td class="diffline">%d </td>' % line1)
-            print_func(u'<td class="diffpresent">')
+            if has_internal_linenos:
+                print_func(u'<td colspan="2" class="diffpresent">')
+            else:
+                print_func(u'<td class="diffline">%d </td>' % line1)
+                print_func(u'<td class="diffpresent">')
             print_func(convert(s1, ponct=1, tag='del'))
             print_func(u'</td>')
         else:
             print_func(u'<td colspan="2">\xa0</td>')
 
         if s2:
-            print_func(u'<td class="diffline">%d </td>' % line2)
-            print_func(u'<td class="diffpresent">')
+            if has_internal_linenos:
+                print_func(u'<td colspan="2" class="diffpresent">')
+            else:
+                print_func(u'<td class="diffline">%d </td>' % line2)
+                print_func(u'<td class="diffpresent">')
             print_func(convert(s2, ponct=1, tag='ins'))
             print_func(u'</td>')
         else:
@@ -407,11 +412,12 @@ def empty_buffer(print_func):
     buf = []
 
 
-def output_unified_diff_table(print_func, unified_diff):
+def output_unified_diff_table(print_func, unified_diff, _has_internal_linenos):
     global add_cpt, del_cpt
-    global line1, line2
+    global line1, line2, has_internal_linenos
     global hunk_off1, hunk_size1, hunk_off2, hunk_size2
 
+    has_internal_linenos = _has_internal_linenos
     print_func(u'<table class="diff">\n')
     try:
         print_func(u'<colgroup><col style="width: 3em;"/><col style="99%"/>\n')
@@ -490,14 +496,14 @@ def output_unified_diff_table(print_func, unified_diff):
     finally:
         print_func(u"</table>", force=True)
 
-def output_unified_diff(print_func, css_url, directory, unified_diff):
+def output_unified_diff(print_func, css_url, directory, unified_diff, has_internal_linenos):
     if directory and len(unified_diff) > Config.general.separate_file_diff_size:
         # open a new file for this table
         filename="%s.html" % hashlib.md5(unified_diff.encode('utf-8')).hexdigest()
         logger.debug('separate html output for diff of size %d', len(unified_diff))
         with file_printer(directory, filename) as new_print_func:
             output_header(css_url, new_print_func)
-            output_unified_diff_table(new_print_func, unified_diff)
+            output_unified_diff_table(new_print_func, unified_diff, has_internal_linenos)
             output_footer(new_print_func)
 
         print_func("<div class='ondemand'>\n")
@@ -505,7 +511,7 @@ def output_unified_diff(print_func, css_url, directory, unified_diff):
         print_func("</div>\n")
 
     else:
-        output_unified_diff_table(print_func, unified_diff)
+        output_unified_diff_table(print_func, unified_diff, has_internal_linenos)
 
 def output_difference(difference, print_func, css_url, directory, parents):
     logger.debug('html output for %s', difference.source1)
@@ -529,7 +535,7 @@ def output_difference(difference, print_func, css_url, directory, parents):
                        % u'<br />'.join(map(html.escape, difference.comments)))
         print_func(u"</div>")
         if difference.unified_diff:
-            output_unified_diff(print_func, css_url, directory, difference.unified_diff)
+            output_unified_diff(print_func, css_url, directory, difference.unified_diff, difference.has_internal_linenos)
         for detail in difference.details:
             output_difference(detail, print_func, css_url, directory, sources)
     except PrintLimitReached:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list