Skip to content

Commit

Permalink
Probe context diff for #15
Browse files Browse the repository at this point in the history
  • Loading branch information
ymattw committed Mar 16, 2013
1 parent 7881a41 commit 69e4b16
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cdiff.py
Expand Up @@ -429,6 +429,10 @@ def is_binary_differ(self, line):
return re.match('^Binary files .* differ$', line.rstrip())


class ContextDiff(Diff):
pass


class PatchStream(object):

def __init__(self, diff_hdl):
Expand Down Expand Up @@ -474,6 +478,14 @@ def __init__(self, stream):
self._stream.read_stream_header(100)]
size = len(header)

if size >= 4 and (header[0].startswith('*** ') and
header[1].startswith('--- ') and
header[2] == '***************\n' and
header[3].startswith('*** ') and
header[3].endswith(' ****\n')):
self._type = 'context'
return

for n in range(size):
if header[n].startswith('--- ') and (n < size - 1) and \
header[n+1].startswith('+++ '):
Expand Down
17 changes: 16 additions & 1 deletion tests/test_cdiff.py
Expand Up @@ -423,7 +423,7 @@ def test_is_new_neg(self):

class DiffParserTest(unittest.TestCase):

def test_type_detect(self):
def test_type_detect_unified(self):
patch = """\
spam
--- a
Expand All @@ -435,6 +435,21 @@ def test_type_detect(self):
parser = cdiff.DiffParser(stream)
self.assertEqual(parser._type, 'unified')

def test_type_detect_context(self):
patch = """\
*** /path/to/original timestamp
--- /path/to/new timestamp
***************
*** 1,1 ****
--- 1,2 ----
+ This is an important
This part of the
"""
items = patch.splitlines(True)
stream = cdiff.PatchStream(Sequential(items))
parser = cdiff.DiffParser(stream)
self.assertEqual(parser._type, 'context')

def test_type_detect_neg(self):
patch = """\
spam
Expand Down

0 comments on commit 69e4b16

Please sign in to comment.