Browse Source

Enhance diff parser on handling hunk links, fix #20

Matthew Wang 11 years ago
parent
commit
ab9a9981c6
5 changed files with 157 additions and 11 deletions
  1. 8
    11
      cdiff.py
  2. 37
    0
      tests/git-log/in.diff
  3. 38
    0
      tests/git-log/out.normal
  4. 37
    0
      tests/git-log/out.side-by-side
  5. 37
    0
      tests/git-log/out.w70

+ 8
- 11
cdiff.py View File

@@ -560,17 +560,14 @@ class DiffParser(object):
560 560
             line = decode(line)
561 561
 
562 562
             if difflet.is_old_path(line):
563
-                # FIXME: '--- ' breaks here, need to probe next 3 lines,
564
-                # reproducible with github raw patch
565
-                #
566
-                if diff._old_path and diff._new_path and len(diff._hunks) > 0:
567
-                    # One diff constructed
563
+                # FIXME: '--- ' breaks here, better to probe next line
564
+                if diff._old_path and diff._new_path and diff._hunks:
565
+                    # See a new diff, yield previous diff if exists
568 566
                     yield diff
569
-                    diff = Diff([], None, None, [])
570 567
                 diff = Diff(headers, line, None, [])
571 568
                 headers = []
572 569
 
573
-            elif difflet.is_new_path(line):
570
+            elif difflet.is_new_path(line) and diff._old_path:
574 571
                 diff._new_path = line
575 572
 
576 573
             elif difflet.is_hunk_meta(line):
@@ -583,9 +580,9 @@ class DiffParser(object):
583 580
                 headers = []
584 581
                 diff._hunks.append(hunk)
585 582
 
586
-            elif len(diff._hunks) > 0 and (difflet.is_old(line) or
587
-                                           difflet.is_new(line) or
588
-                                           difflet.is_common(line)):
583
+            elif diff._hunks and not headers and (difflet.is_old(line) or
584
+                                                  difflet.is_new(line) or
585
+                                                  difflet.is_common(line)):
589 586
                 diff._hunks[-1].append(difflet.parse_hunk_line(line))
590 587
 
591 588
             elif difflet.is_eof(line):
@@ -597,7 +594,7 @@ class DiffParser(object):
597 594
                 # 'Only in foo:' and 'Binary files ... differ' are considered
598 595
                 # as separate diffs, so yield current diff, then this line
599 596
                 #
600
-                if diff._old_path and diff._new_path and len(diff._hunks) > 0:
597
+                if diff._old_path and diff._new_path and diff._hunks:
601 598
                     # Current diff is comppletely constructed
602 599
                     yield diff
603 600
                 headers.append(line)

+ 37
- 0
tests/git-log/in.diff View File

@@ -0,0 +1,37 @@
1
+commit 65f33a326fb1d81e9b56cfa9dbe3af887ed91c8d
2
+Author: Steven Myint
3
+Date:   Fri Mar 22 14:59:34 2013 -0700
4
+
5
+    Remove unused imports
6
+
7
+diff --git a/libmodernize/fixes/__init__.py b/libmodernize/fixes/__init__.py
8
+index f8628f1..2e06052 100644
9
+--- a/libmodernize/fixes/__init__.py
10
++++ b/libmodernize/fixes/__init__.py
11
+@@ -1,7 +1,3 @@
12
+-import sys
13
+-from lib2to3 import refactor
14
+-
15
+-
16
+ lib2to3_fix_names = set([
17
+     'lib2to3.fixes.fix_apply',
18
+     'lib2to3.fixes.fix_except',
19
+
20
+commit 2e80837bf815d0686138beec9a5bb94e3282204d
21
+Author: Steven Myint
22
+Date:   Fri Mar 22 14:33:55 2013 -0700
23
+
24
+    Format docstring
25
+
26
+diff --git a/libmodernize/main.py b/libmodernize/main.py
27
+index caf847d..b24db29 100644
28
+--- a/libmodernize/main.py
29
++++ b/libmodernize/main.py
30
+@@ -14,6 +14,7 @@ def main(args=None):
31
+     """Main program.
32
+ 
33
+     Returns a suggested exit status (0, 1, 2).
34
++
35
+     """
36
+     # Set up option parser
37
+     parser = optparse.OptionParser(usage="modernize [options] file|dir ...")

+ 38
- 0
tests/git-log/out.normal View File

@@ -0,0 +1,38 @@
1
+commit 65f33a326fb1d81e9b56cfa9dbe3af887ed91c8d
2
+Author: Steven Myint
3
+Date:   Fri Mar 22 14:59:34 2013 -0700
4
+
5
+    Remove unused imports
6
+
7
+diff --git a/libmodernize/fixes/__init__.py b/libmodernize/fixes/__init__.py
8
+index f8628f1..2e06052 100644
9
+--- a/libmodernize/fixes/__init__.py
10
++++ b/libmodernize/fixes/__init__.py
11
+@@ -1,7 +1,3 @@
12
+-import sys
13
+-from lib2to3 import refactor
14
+-
15
+-
16
+ lib2to3_fix_names = set([
17
+     'lib2to3.fixes.fix_apply',
18
+     'lib2to3.fixes.fix_except',
19
+
20
+commit 2e80837bf815d0686138beec9a5bb94e3282204d
21
+Author: Steven Myint
22
+Date:   Fri Mar 22 14:33:55 2013 -0700
23
+
24
+    Format docstring
25
+
26
+diff --git a/libmodernize/main.py b/libmodernize/main.py
27
+index caf847d..b24db29 100644
28
+--- a/libmodernize/main.py
29
++++ b/libmodernize/main.py
30
+@@ -14,6 +14,7 @@ def main(args=None):
31
+     """Main program.
32
+ 
33
+     Returns a suggested exit status (0, 1, 2).
34
++
35
+     """
36
+     # Set up option parser
37
+     parser = optparse.OptionParser(usage="modernize [options] file|dir ...")
38
+

+ 37
- 0
tests/git-log/out.side-by-side View File

@@ -0,0 +1,37 @@
1
+commit 65f33a326fb1d81e9b56cfa9dbe3af887ed91c8d
2
+Author: Steven Myint
3
+Date:   Fri Mar 22 14:59:34 2013 -0700
4
+
5
+    Remove unused imports
6
+
7
+diff --git a/libmodernize/fixes/__init__.py b/libmodernize/fixes/__init__.py
8
+index f8628f1..2e06052 100644
9
+--- a/libmodernize/fixes/__init__.py
10
++++ b/libmodernize/fixes/__init__.py
11
+@@ -1,7 +1,3 @@
12
+1 import sys   
13
+2 from lib2to3 import refactor   
14
+3    
15
+4    
16
+5 lib2to3_fix_names = set([                                                        1 lib2to3_fix_names = set([
17
+6     'lib2to3.fixes.fix_apply',                                                   2     'lib2to3.fixes.fix_apply',
18
+7     'lib2to3.fixes.fix_except',                                                  3     'lib2to3.fixes.fix_except',
19
+
20
+commit 2e80837bf815d0686138beec9a5bb94e3282204d
21
+Author: Steven Myint
22
+Date:   Fri Mar 22 14:33:55 2013 -0700
23
+
24
+    Format docstring
25
+
26
+diff --git a/libmodernize/main.py b/libmodernize/main.py
27
+index caf847d..b24db29 100644
28
+--- a/libmodernize/main.py
29
++++ b/libmodernize/main.py
30
+@@ -14,6 +14,7 @@ def main(args=None):
31
+14     """Main program.                                                             14     """Main program.
32
+15                                                                                  15 
33
+16     Returns a suggested exit status (0, 1, 2).                                   16     Returns a suggested exit status (0, 1, 2).
34
+                                                                                    17 
35
+17     """                                                                          18     """
36
+18     # Set up option parser                                                       19     # Set up option parser
37
+19     parser = optparse.OptionParser(usage="modernize [options] file|dir ...")     20     parser = optparse.OptionParser(usage="modernize [options] file|dir ...")

+ 37
- 0
tests/git-log/out.w70 View File

@@ -0,0 +1,37 @@
1
+commit 65f33a326fb1d81e9b56cfa9dbe3af887ed91c8d
2
+Author: Steven Myint
3
+Date:   Fri Mar 22 14:59:34 2013 -0700
4
+
5
+    Remove unused imports
6
+
7
+diff --git a/libmodernize/fixes/__init__.py b/libmodernize/fixes/__init__.py
8
+index f8628f1..2e06052 100644
9
+--- a/libmodernize/fixes/__init__.py
10
++++ b/libmodernize/fixes/__init__.py
11
+@@ -1,7 +1,3 @@
12
+1 import sys   
13
+2 from lib2to3 import refactor   
14
+3    
15
+4    
16
+5 lib2to3_fix_names = set([                                              1 lib2to3_fix_names = set([
17
+6     'lib2to3.fixes.fix_apply',                                         2     'lib2to3.fixes.fix_apply',
18
+7     'lib2to3.fixes.fix_except',                                        3     'lib2to3.fixes.fix_except',
19
+
20
+commit 2e80837bf815d0686138beec9a5bb94e3282204d
21
+Author: Steven Myint
22
+Date:   Fri Mar 22 14:33:55 2013 -0700
23
+
24
+    Format docstring
25
+
26
+diff --git a/libmodernize/main.py b/libmodernize/main.py
27
+index caf847d..b24db29 100644
28
+--- a/libmodernize/main.py
29
++++ b/libmodernize/main.py
30
+@@ -14,6 +14,7 @@ def main(args=None):
31
+14     """Main program.                                                   14     """Main program.
32
+15                                                                        15 
33
+16     Returns a suggested exit status (0, 1, 2).                         16     Returns a suggested exit status (0, 1, 2).
34
+                                                                          17 
35
+17     """                                                                18     """
36
+18     # Set up option parser                                             19     # Set up option parser
37
+19     parser = optparse.OptionParser(usage="modernize [options] file|di> 20     parser = optparse.OptionParser(usage="modernize [options] file|di>