Browse Source

Merge remote-tracking branch 'origin/context-diff'

Matthew Wang 11 years ago
parent
commit
d81f755e4e
2 changed files with 28 additions and 1 deletions
  1. 12
    0
      cdiff.py
  2. 16
    1
      tests/test_cdiff.py

+ 12
- 0
cdiff.py View File

429
         return re.match('^Binary files .* differ$', line.rstrip())
429
         return re.match('^Binary files .* differ$', line.rstrip())
430
 
430
 
431
 
431
 
432
+class ContextDiff(Diff):
433
+    pass
434
+
435
+
432
 class PatchStream(object):
436
 class PatchStream(object):
433
 
437
 
434
     def __init__(self, diff_hdl):
438
     def __init__(self, diff_hdl):
474
                   self._stream.read_stream_header(100)]
478
                   self._stream.read_stream_header(100)]
475
         size = len(header)
479
         size = len(header)
476
 
480
 
481
+        if size >= 4 and (header[0].startswith('*** ') and
482
+                          header[1].startswith('--- ') and
483
+                          header[2].rstrip() == '***************' and
484
+                          header[3].startswith('*** ') and
485
+                          header[3].rstrip().endswith(' ****')):
486
+            self._type = 'context'
487
+            return
488
+
477
         for n in range(size):
489
         for n in range(size):
478
             if header[n].startswith('--- ') and (n < size - 1) and \
490
             if header[n].startswith('--- ') and (n < size - 1) and \
479
                     header[n+1].startswith('+++ '):
491
                     header[n+1].startswith('+++ '):

+ 16
- 1
tests/test_cdiff.py View File

423
 
423
 
424
 class DiffParserTest(unittest.TestCase):
424
 class DiffParserTest(unittest.TestCase):
425
 
425
 
426
-    def test_type_detect(self):
426
+    def test_type_detect_unified(self):
427
         patch = """\
427
         patch = """\
428
 spam
428
 spam
429
 --- a
429
 --- a
435
         parser = cdiff.DiffParser(stream)
435
         parser = cdiff.DiffParser(stream)
436
         self.assertEqual(parser._type, 'unified')
436
         self.assertEqual(parser._type, 'unified')
437
 
437
 
438
+    def test_type_detect_context(self):
439
+        patch = """\
440
+*** /path/to/original timestamp
441
+--- /path/to/new timestamp
442
+***************
443
+*** 1,1 ****
444
+--- 1,2 ----
445
++ This is an important
446
+  This part of the
447
+"""
448
+        items = patch.splitlines(True)
449
+        stream = cdiff.PatchStream(Sequential(items))
450
+        parser = cdiff.DiffParser(stream)
451
+        self.assertEqual(parser._type, 'context')
452
+
438
     def test_type_detect_neg(self):
453
     def test_type_detect_neg(self):
439
         patch = """\
454
         patch = """\
440
 spam
455
 spam