浏览代码

Probe context diff for #15

Matthew Wang 11 年前
父节点
当前提交
69e4b1683e
共有 2 个文件被更改,包括 28 次插入1 次删除
  1. 12
    0
      cdiff.py
  2. 16
    1
      tests/test_cdiff.py

+ 12
- 0
cdiff.py 查看文件

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] == '***************\n' and
484
+                          header[3].startswith('*** ') and
485
+                          header[3].endswith(' ****\n')):
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 查看文件

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