From 1c234b96d80e374e20a519fd833704cdb7530411 Mon Sep 17 00:00:00 2001
From: Jonas Haag <jonas@lophus.org>
Date: Fri, 17 Apr 2015 12:04:52 +0200
Subject: [PATCH] Fix #113: Fix whitespace in filenames in diff

The 'diff.py' module used to parse unified diff lines like

  +++ a/foo/bar /spam/eggs

as

  "/foo/bar"

i.e. stripping anything behind whitespace. This is because it tried to
recognize the part after whitespace as a commit revision id. As dulwich
never uses the latter, I simply removed that feature. Whitespace isn't
special-cased anymore now.
---
 klaus/diff.py | 29 ++++++++++++-----------------
 1 file changed, 12 insertions(+), 17 deletions(-)

diff --git a/klaus/diff.py b/klaus/diff.py
index fefdb1c..7788f8f 100644
--- a/klaus/diff.py
+++ b/klaus/diff.py
@@ -27,18 +27,16 @@ class DiffRenderer(object):
         """:param udiff:   a text in udiff format"""
         self.lines = [escape(line) for line in udiff.splitlines()]
 
-    def _extract_rev(self, line1, line2):
-        def _extract(line):
-            parts = line.split(None, 1)
-            if parts[0].startswith(('a/', 'b/')):
-                parts[0] = parts[0][2:]
-            return parts[0], (len(parts) == 2 and parts[1] or None)
-        try:
-            if line1.startswith('--- ') and line2.startswith('+++ '):
-                return _extract(line1[4:]), _extract(line2[4:])
-        except (ValueError, IndexError):
-            pass
-        return (None, None), (None, None)
+    def _extract_filename(self, line):
+        """
+        Extract file name from unified diff line:
+            --- a/foo/bar   ==>   foo/bar
+            +++ b/foo/bar   ==>   foo/bar
+        """
+        if line.startswith(("--- /dev/null", "+++ /dev/null")):
+            return line[len("--- "):]
+        else:
+            return line[len("--- a/"):]
 
     def _highlight_line(self, line, next):
         """Highlight inline changes in both lines."""
@@ -91,14 +89,11 @@ class DiffRenderer(object):
 
                 in_header = False
                 chunks = []
-                old, new = self._extract_rev(line, lineiter.next())
                 adds, dels = 0, 0
                 files.append({
                     'is_header':        False,
-                    'old_filename':     old[0],
-                    'old_revision':     old[1],
-                    'new_filename':     new[0],
-                    'new_revision':     new[1],
+                    'old_filename':     self._extract_filename(line),
+                    'new_filename':     self._extract_filename(lineiter.next()),
                     'additions':        adds,
                     'deletions':        dels,
                     'chunks':           chunks
-- 
GitLab