Browse Source

Fall through unknown format to 'unified', fixed #18

Matthew Wang 11 years ago
parent
commit
2e49a7eb3f

+ 12
- 10
cdiff.py View File

@@ -377,7 +377,7 @@ class UnifiedDiff(Diff):
377 377
 
378 378
     def is_hunk_meta(self, line):
379 379
         """Minimal valid hunk meta is like '@@ -1 +1 @@', note extra chars
380
-        might occur after the ending @@, e.g. in git log.  '## ' uaually
380
+        might occur after the ending @@, e.g. in git log.  '## ' usually
381 381
         indicates svn property changes in output from `svn log --diff`
382 382
         """
383 383
         return (line.startswith('@@ -') and line.find(' @@') >= 8) or \
@@ -484,6 +484,12 @@ class DiffParser(object):
484 484
                           header[3].startswith('*** ') and
485 485
                           header[3].rstrip().endswith(' ****')):
486 486
             self._type = 'context'
487
+
488
+            # For context diff, try use `filterdiff` to translate it to unified
489
+            # format and provide a new stream
490
+            #
491
+            # TODO
492
+
487 493
             return
488 494
 
489 495
         for n in range(size):
@@ -492,15 +498,11 @@ class DiffParser(object):
492 498
                 self._type = 'unified'
493 499
                 break
494 500
         else:
495
-            if size < 5:
496
-                # It's safe to consider as udiff if patch stream contains no
497
-                # more than 4 lines. happens with `git diff` on a file that
498
-                # only has perm bits changes or `svn diff` with property
499
-                # changes.
500
-                #
501
-                self._type = 'unified'
502
-            else:
503
-                raise RuntimeError('unknown diff type')
501
+            # `filterdiff` translate unknown diff to nothing, fall through to
502
+            # unified diff give cdiff a chance to show everything as headers
503
+            #
504
+            sys.stderr.write("*** unknown format, fall through to 'unified'\n")
505
+            self._type = 'unified'
504 506
 
505 507
     def get_diff_generator(self):
506 508
         """parse all diff lines, construct a list of Diff objects"""

+ 6
- 3
tests/git-perm/in.diff View File

@@ -1,3 +1,6 @@
1
-diff --git a/CHANGES b/CHANGES
2
-old mode 100644
3
-new mode 100755
1
+diff --git a/cdiff b/cdiff
2
+old mode 100755
3
+new mode 100644
4
+diff --git a/cdiff.py b/cdiff.py
5
+old mode 100755
6
+new mode 100644

+ 6
- 3
tests/git-perm/out.normal View File

@@ -1,4 +1,7 @@
1
-diff --git a/CHANGES b/CHANGES
2
-old mode 100644
3
-new mode 100755
1
+diff --git a/cdiff b/cdiff
2
+old mode 100755
3
+new mode 100644
4
+diff --git a/cdiff.py b/cdiff.py
5
+old mode 100755
6
+new mode 100644
4 7
 

+ 6
- 3
tests/git-perm/out.side-by-side View File

@@ -1,4 +1,7 @@
1
-diff --git a/CHANGES b/CHANGES
2
-old mode 100644
3
-new mode 100755
1
+diff --git a/cdiff b/cdiff
2
+old mode 100755
3
+new mode 100644
4
+diff --git a/cdiff.py b/cdiff.py
5
+old mode 100755
6
+new mode 100644
4 7
 

+ 6
- 3
tests/git-perm/out.w70 View File

@@ -1,4 +1,7 @@
1
-diff --git a/CHANGES b/CHANGES
2
-old mode 100644
3
-new mode 100755
1
+diff --git a/cdiff b/cdiff
2
+old mode 100755
3
+new mode 100644
4
+diff --git a/cdiff.py b/cdiff.py
5
+old mode 100755
6
+new mode 100644
4 7
 

+ 1
- 1
tests/regression.sh View File

@@ -33,7 +33,7 @@ function cmp_output()
33 33
     local cdiff_opt=${3:-""}
34 34
     local cmd
35 35
 
36
-    cmd=$(printf "%-8s $CDIFF %-18s < %-25s " $PYTHON "$cdiff_opt" "$input")
36
+    cmd=$(printf "%-8s $CDIFF %-18s <  %-30s " $PYTHON "$cdiff_opt" "$input")
37 37
     printf "$cmd"
38 38
     if eval $cmd 2>/dev/null | diff -ubq $expected_out - >& /dev/null; then
39 39
         pass

+ 2
- 1
tests/test_cdiff.py View File

@@ -460,7 +460,8 @@ spam
460 460
 """
461 461
         items = patch.splitlines(True)
462 462
         stream = cdiff.PatchStream(Sequential(items))
463
-        self.assertRaises(RuntimeError, cdiff.DiffParser, stream)
463
+        parser = cdiff.DiffParser(stream)
464
+        self.assertEqual(parser._type, 'unified')
464 465
 
465 466
     def test_parse_invalid_hunk_meta(self):
466 467
         patch = """\