瀏覽代碼

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,6 +429,10 @@ class UnifiedDiff(Diff):
429 429
         return re.match('^Binary files .* differ$', line.rstrip())
430 430
 
431 431
 
432
+class ContextDiff(Diff):
433
+    pass
434
+
435
+
432 436
 class PatchStream(object):
433 437
 
434 438
     def __init__(self, diff_hdl):
@@ -474,6 +478,14 @@ class DiffParser(object):
474 478
                   self._stream.read_stream_header(100)]
475 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 489
         for n in range(size):
478 490
             if header[n].startswith('--- ') and (n < size - 1) and \
479 491
                     header[n+1].startswith('+++ '):

+ 16
- 1
tests/test_cdiff.py 查看文件

@@ -423,7 +423,7 @@ class UnifiedDiffTest(unittest.TestCase):
423 423
 
424 424
 class DiffParserTest(unittest.TestCase):
425 425
 
426
-    def test_type_detect(self):
426
+    def test_type_detect_unified(self):
427 427
         patch = """\
428 428
 spam
429 429
 --- a
@@ -435,6 +435,21 @@ spam
435 435
         parser = cdiff.DiffParser(stream)
436 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 453
     def test_type_detect_neg(self):
439 454
         patch = """\
440 455
 spam