Browse Source

Fix issue #25 w/ logic to recogonize old/new path

Matthew Wang 8 years ago
parent
commit
f246c518de
1 changed files with 21 additions and 7 deletions
  1. 21
    7
      cdiff.py

+ 21
- 7
cdiff.py View File

@@ -132,6 +132,11 @@ class Hunk(object):
132 132
                 out.append(line)
133 133
         return out
134 134
 
135
+    def is_completed(self):
136
+        old_completed = self._old_addr[1] == len(self._get_old_text())
137
+        new_completed = self._new_addr[1] == len(self._get_new_text())
138
+        return old_completed and new_completed
139
+
135 140
 
136 141
 class UnifiedDiff(object):
137 142
 
@@ -321,15 +326,24 @@ class DiffParser(object):
321 326
             line = decode(line)
322 327
 
323 328
             if diff.is_old_path(line):
324
-                # FIXME: '--- ' breaks here, better to probe next line
325
-                if diff._old_path and diff._new_path and diff._hunks:
326
-                    # See a new diff, yield previous diff if exists
327
-                    yield diff
328
-                diff = UnifiedDiff(headers, line, None, [])
329
-                headers = []
329
+                # This is a new diff when current hunk is not yet genreated or
330
+                # is completed.  We yield previous diff if exists and construct
331
+                # a new one for this case.  Otherwise it's acutally an 'old'
332
+                # line starts with '--- '.
333
+                #
334
+                if (not diff._hunks or diff._hunks[-1].is_completed()):
335
+                    if diff._old_path and diff._new_path and diff._hunks:
336
+                        yield diff
337
+                    diff = UnifiedDiff(headers, line, None, [])
338
+                    headers = []
339
+                else:
340
+                    diff._hunks[-1].append(diff.parse_hunk_line(line))
330 341
 
331 342
             elif diff.is_new_path(line) and diff._old_path:
332
-                diff._new_path = line
343
+                if not diff._new_path:
344
+                    diff._new_path = line
345
+                else:
346
+                    diff._hunks[-1].append(diff.parse_hunk_line(line))
333 347
 
334 348
             elif diff.is_hunk_meta(line):
335 349
                 hunk_meta = line