Browse Source

Make patch parser more solid

Matthew Wang 11 years ago
parent
commit
6e60a56c0c
1 changed files with 10 additions and 3 deletions
  1. 10
    3
      cdiff.py

+ 10
- 3
cdiff.py View File

377
         return line.startswith('+++ ')
377
         return line.startswith('+++ ')
378
 
378
 
379
     def is_hunk_meta(self, line):
379
     def is_hunk_meta(self, line):
380
-        return line.startswith('@@ -') or line.startswith('## -')
380
+        """Minimal valid hunk meta is like '@@ -1 +1 @@', note extra chars might
381
+        occur after the ending @@, e.g. in git log
382
+        """
383
+        return (line.startswith('@@ -') and line.find(' @@') >= 8) or \
384
+               (line.startswith('## -') and line.find(' ##') >= 8)
381
 
385
 
382
     def parse_hunk_meta(self, hunk_meta):
386
     def parse_hunk_meta(self, hunk_meta):
383
         # @@ -3,7 +3,6 @@
387
         # @@ -3,7 +3,6 @@
401
         return (line[0], line[1:])
405
         return (line[0], line[1:])
402
 
406
 
403
     def is_old(self, line):
407
     def is_old(self, line):
404
-        """Exclude header line from svn log --diff output"""
408
+        """Exclude old path and header line from svn log --diff output, allow
409
+        '----' likely to see in diff from yaml file
410
+        """
405
         return line.startswith('-') and not self.is_old_path(line) and \
411
         return line.startswith('-') and not self.is_old_path(line) and \
406
-                not re.match(r'^-{4,}$', line.rstrip())
412
+                not re.match(r'^-{5,}$', line.rstrip())
407
 
413
 
408
     def is_new(self, line):
414
     def is_new(self, line):
409
         return line.startswith('+') and not self.is_new_path(line)
415
         return line.startswith('+') and not self.is_new_path(line)
496
             line = decode(line)
502
             line = decode(line)
497
 
503
 
498
             if difflet.is_old_path(line):
504
             if difflet.is_old_path(line):
505
+                # FIXME: '--- ' breaks here, need to probe next 3 lines
499
                 if diff._old_path and diff._new_path and len(diff._hunks) > 0:
506
                 if diff._old_path and diff._new_path and len(diff._hunks) > 0:
500
                     # One diff constructed
507
                     # One diff constructed
501
                     yield diff
508
                     yield diff