Skip to content

Commit

Permalink
Handle 'Binary files ... differ'
Browse files Browse the repository at this point in the history
  • Loading branch information
ymattw committed Feb 23, 2013
1 parent c929b5f commit a413a03
Show file tree
Hide file tree
Showing 14 changed files with 263 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cdiff.py
Expand Up @@ -169,6 +169,11 @@ def is_eof(self, line):
def is_only_in_dir(self, line):
return False

def is_binary_differ(self, line):
return False

# Followings are not suppose to override
#
def markup_traditional(self):
"""Returns a generator"""
for line in self._headers:
Expand Down Expand Up @@ -421,6 +426,9 @@ def is_eof(self, line):
def is_only_in_dir(self, line):
return line.startswith('Only in ')

def is_binary_differ(self, line):
return re.match('^Binary files .* differ$', line.rstrip())


class PatchStream(object):

Expand Down Expand Up @@ -529,14 +537,16 @@ def _parse(self):
# ignore
pass

elif difflet.is_only_in_dir(line):
# 'Only in foo: ' is considered a separate diff, so yield
# current diff, then this line
elif difflet.is_only_in_dir(line) or difflet.is_binary_differ(line):
# 'Only in foo:' and 'Binary files ... differ' are considered as
# separate diffs, so yield current diff, then this line
#
if diff._old_path and diff._new_path and len(diff._hunks) > 0:
# One diff constructed
# Current diff is comppletely constructed
yield diff
yield Diff([line], '', '', [])
headers.append(line)
yield Diff(headers, '', '', [])
headers = []
diff = Diff([], None, None, [])

else:
Expand Down
20 changes: 20 additions & 0 deletions tests/diff-ru-bin/in.diff
@@ -0,0 +1,20 @@
diff -ru a/README b/README
--- a/README 2013-02-23 19:51:41.000000000 +0800
+++ b/README 2013-02-23 19:52:06.000000000 +0800
@@ -1,6 +1,6 @@
-# To generate expected output, chdir to a subdir and use following command, then
+# To generate expected output, cd to a subdir and use following command, then
# review with `less -R`
-#
../../cdiff.py -c always < in.diff > out.normal
../../cdiff.py -c always -s < in.diff > out.side-by-side
../../cdiff.py -c always -s < in.diff -w70 > out.w70
+# EOF
Binary files a/foo.pdf and b/foo.pdf differ
diff -ru a/foo.txt b/foo.txt
--- a/foo.txt 2013-02-23 19:55:03.000000000 +0800
+++ b/foo.txt 2013-02-23 19:55:10.000000000 +0800
@@ -1,2 +1,2 @@
-Hello
+hella
world
21 changes: 21 additions & 0 deletions tests/diff-ru-bin/out.normal
@@ -0,0 +1,21 @@
diff -ru a/README b/README
--- a/README 2013-02-23 19:51:41.000000000 +0800
+++ b/README 2013-02-23 19:52:06.000000000 +0800
@@ -1,6 +1,6 @@
-# To generate expected output, chdir to a subdir and use following command, then
+# To generate expected output, cd to a subdir and use following command, then
 # review with `less -R`
-#
 ../../cdiff.py -c always < in.diff > out.normal
 ../../cdiff.py -c always -s < in.diff > out.side-by-side
 ../../cdiff.py -c always -s < in.diff -w70 > out.w70
+# EOF
Binary files a/foo.pdf and b/foo.pdf differ
diff -ru a/foo.txt b/foo.txt
--- a/foo.txt 2013-02-23 19:55:03.000000000 +0800
+++ b/foo.txt 2013-02-23 19:55:10.000000000 +0800
@@ -1,2 +1,2 @@
-Hello
+hella
 world

18 changes: 18 additions & 0 deletions tests/diff-ru-bin/out.side-by-side
@@ -0,0 +1,18 @@
diff -ru a/README b/README
--- a/README 2013-02-23 19:51:41.000000000 +0800
+++ b/README 2013-02-23 19:52:06.000000000 +0800
@@ -1,6 +1,6 @@
1 # To generate expected output, chdir to a subdir and use following command, then 1 # To generate expected output, cd to a subdir and use following command, then
2 # review with `less -R` 2 # review with `less -R`
3 #  
4 ../../cdiff.py -c always < in.diff > out.normal 3 ../../cdiff.py -c always < in.diff > out.normal
5 ../../cdiff.py -c always -s < in.diff > out.side-by-side 4 ../../cdiff.py -c always -s < in.diff > out.side-by-side
6 ../../cdiff.py -c always -s < in.diff -w70 > out.w70 5 ../../cdiff.py -c always -s < in.diff -w70 > out.w70
  6 # EOF
Binary files a/foo.pdf and b/foo.pdf differ
diff -ru a/foo.txt b/foo.txt
--- a/foo.txt 2013-02-23 19:55:03.000000000 +0800
+++ b/foo.txt 2013-02-23 19:55:10.000000000 +0800
@@ -1,2 +1,2 @@
1 Hello 1 hella
2 world 2 world
18 changes: 18 additions & 0 deletions tests/diff-ru-bin/out.w70
@@ -0,0 +1,18 @@
diff -ru a/README b/README
--- a/README 2013-02-23 19:51:41.000000000 +0800
+++ b/README 2013-02-23 19:52:06.000000000 +0800
@@ -1,6 +1,6 @@
1 # To generate expected output, chdir to a subdir and use following co> 1 # To generate expected output, cd to a subdir and use following comma>
2 # review with `less -R` 2 # review with `less -R`
3 #  
4 ../../cdiff.py -c always < in.diff > out.normal 3 ../../cdiff.py -c always < in.diff > out.normal
5 ../../cdiff.py -c always -s < in.diff > out.side-by-side 4 ../../cdiff.py -c always -s < in.diff > out.side-by-side
6 ../../cdiff.py -c always -s < in.diff -w70 > out.w70 5 ../../cdiff.py -c always -s < in.diff -w70 > out.w70
  6 # EOF
Binary files a/foo.pdf and b/foo.pdf differ
diff -ru a/foo.txt b/foo.txt
--- a/foo.txt 2013-02-23 19:55:03.000000000 +0800
+++ b/foo.txt 2013-02-23 19:55:10.000000000 +0800
@@ -1,2 +1,2 @@
1 Hello 1 hella
2 world 2 world
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions tests/git-bin/in.diff
@@ -0,0 +1,26 @@
diff --git a/2pdf.sh b/2pdf.sh
index 529d8a3..ad71911 100755
--- a/2pdf.sh
+++ b/2pdf.sh
@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
OUTPUT=${INPUT%.html}.pdf

wkhtmltopdf --page-size A4 $INPUT $OUTPUT # very very long comments ends here
+
+# EOF
diff --git a/example.pdf b/example.pdf
index 1eacfd8..3696851 100644
Binary files a/example.pdf and b/example.pdf differ
diff --git a/foo.html b/foo.html
index d2fd3fb..13afa6e 100644
--- a/foo.html
+++ b/foo.html
@@ -1,4 +1,4 @@
-<!doctype html>
+<!DOCTYPE html>
<html>

<head>
diff --git a/foo.pdf b/foo.pdf
index 0e90017..3c3b90d 100644
Binary files a/foo.pdf and b/foo.pdf differ
27 changes: 27 additions & 0 deletions tests/git-bin/out.normal
@@ -0,0 +1,27 @@
diff --git a/2pdf.sh b/2pdf.sh
index 529d8a3..ad71911 100755
--- a/2pdf.sh
+++ b/2pdf.sh
@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
 OUTPUT=${INPUT%.html}.pdf

 wkhtmltopdf --page-size A4 $INPUT $OUTPUT # very very long comments ends here
+
+# EOF
diff --git a/example.pdf b/example.pdf
index 1eacfd8..3696851 100644
Binary files a/example.pdf and b/example.pdf differ
diff --git a/foo.html b/foo.html
index d2fd3fb..13afa6e 100644
--- a/foo.html
+++ b/foo.html
@@ -1,4 +1,4 @@
-<!doctype html>
+<!DOCTYPE html>
 <html>

 <head>
diff --git a/foo.pdf b/foo.pdf
index 0e90017..3c3b90d 100644
Binary files a/foo.pdf and b/foo.pdf differ

26 changes: 26 additions & 0 deletions tests/git-bin/out.side-by-side
@@ -0,0 +1,26 @@
diff --git a/2pdf.sh b/2pdf.sh
index 529d8a3..ad71911 100755
--- a/2pdf.sh
+++ b/2pdf.sh
@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
13 OUTPUT=${INPUT%.html}.pdf 13 OUTPUT=${INPUT%.html}.pdf
14  14 
15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT # very very long comments ends here 15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT # very very long comments ends here
  16 
  17 # EOF
diff --git a/example.pdf b/example.pdf
index 1eacfd8..3696851 100644
Binary files a/example.pdf and b/example.pdf differ
diff --git a/foo.html b/foo.html
index d2fd3fb..13afa6e 100644
--- a/foo.html
+++ b/foo.html
@@ -1,4 +1,4 @@
1 <!doctype html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3  3 
4 <head> 4 <head>
diff --git a/foo.pdf b/foo.pdf
index 0e90017..3c3b90d 100644
Binary files a/foo.pdf and b/foo.pdf differ

26 changes: 26 additions & 0 deletions tests/git-bin/out.w70
@@ -0,0 +1,26 @@
diff --git a/2pdf.sh b/2pdf.sh
index 529d8a3..ad71911 100755
--- a/2pdf.sh
+++ b/2pdf.sh
@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
13 OUTPUT=${INPUT%.html}.pdf 13 OUTPUT=${INPUT%.html}.pdf
14  14 
15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT # very very long comments > 15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT # very very long comments >
  16 
  17 # EOF
diff --git a/example.pdf b/example.pdf
index 1eacfd8..3696851 100644
Binary files a/example.pdf and b/example.pdf differ
diff --git a/foo.html b/foo.html
index d2fd3fb..13afa6e 100644
--- a/foo.html
+++ b/foo.html
@@ -1,4 +1,4 @@
1 <!doctype html> 1 <!DOCTYPE html>
2 <html> 2 <html>
3  3 
4 <head> 4 <head>
diff --git a/foo.pdf b/foo.pdf
index 0e90017..3c3b90d 100644
Binary files a/foo.pdf and b/foo.pdf differ

66 changes: 66 additions & 0 deletions tests/test_cdiff.py
Expand Up @@ -397,6 +397,72 @@ def test_parse_only_in_dir_at_last(self):
self.assertEqual(len(out[1]._hunks), 0)
self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])

def test_parse_binary_differ_diff_ru(self):
patch = r"""\
--- a
+++ b
@@ -1,2 +1,2 @@
-foo
+bar
common
Binary files a/1.pdf and b/1.pdf differ
--- c
+++ d
@@ -1,2 +1,2 @@
-foo
+bar
common
"""
items = patch.splitlines(True)
stream = cdiff.PatchStream(Sequential(items))
parser = cdiff.DiffParser(stream)

out = list(parser._parse())
self.assertEqual(len(out), 3)
self.assertEqual(len(out[1]._hunks), 0)
self.assertEqual(out[1]._old_path, '')
self.assertEqual(out[1]._new_path, '')
self.assertEqual(len(out[1]._headers), 1)
self.assertTrue(out[1]._headers[0].startswith('Binary files'))
self.assertEqual(len(out[2]._hunks), 1)
self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)

def test_parse_binary_differ_git(self):
patch = r"""\
diff --git a/foo b/foo
index 529d8a3..ad71911 100755
--- a/foo
+++ b/foo
@@ -1,2 +1,2 @@
-foo
+bar
common
diff --git a/example.pdf b/example.pdf
index 1eacfd8..3696851 100644
Binary files a/example.pdf and b/example.pdf differ
diff --git a/bar b/bar
index 529e8a3..ad71921 100755
--- a/bar
+++ b/bar
@@ -1,2 +1,2 @@
-foo
+bar
common
"""
items = patch.splitlines(True)
stream = cdiff.PatchStream(Sequential(items))
parser = cdiff.DiffParser(stream)

out = list(parser._parse())
self.assertEqual(len(out), 3)
self.assertEqual(len(out[1]._hunks), 0)
self.assertEqual(out[1]._old_path, '')
self.assertEqual(out[1]._new_path, '')
self.assertEqual(len(out[1]._headers), 3)
self.assertTrue(out[1]._headers[2].startswith('Binary files'))
self.assertEqual(len(out[2]._hunks), 1)
self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)

def test_parse_svn_prop(self):
patch = r"""\
--- a
Expand Down

0 comments on commit a413a03

Please sign in to comment.