From a413a0351dce689e1ed7ec09932e8ee8a1700f75 Mon Sep 17 00:00:00 2001 From: Matthew Wang Date: Sat, 23 Feb 2013 21:09:46 +0800 Subject: [PATCH] Handle 'Binary files ... differ' --- cdiff.py | 20 ++++++-- tests/diff-ru-bin/in.diff | 20 ++++++++ tests/diff-ru-bin/out.normal | 21 ++++++++ tests/diff-ru-bin/out.side-by-side | 18 +++++++ tests/diff-ru-bin/out.w70 | 18 +++++++ tests/{dir => diff-ru}/in.diff | 0 tests/{dir => diff-ru}/out.normal | 0 tests/{dir => diff-ru}/out.side-by-side | 0 tests/{dir => diff-ru}/out.w70 | 0 tests/git-bin/in.diff | 26 ++++++++++ tests/git-bin/out.normal | 27 ++++++++++ tests/git-bin/out.side-by-side | 26 ++++++++++ tests/git-bin/out.w70 | 26 ++++++++++ tests/test_cdiff.py | 66 +++++++++++++++++++++++++ 14 files changed, 263 insertions(+), 5 deletions(-) create mode 100644 tests/diff-ru-bin/in.diff create mode 100644 tests/diff-ru-bin/out.normal create mode 100644 tests/diff-ru-bin/out.side-by-side create mode 100644 tests/diff-ru-bin/out.w70 rename tests/{dir => diff-ru}/in.diff (100%) rename tests/{dir => diff-ru}/out.normal (100%) rename tests/{dir => diff-ru}/out.side-by-side (100%) rename tests/{dir => diff-ru}/out.w70 (100%) create mode 100644 tests/git-bin/in.diff create mode 100644 tests/git-bin/out.normal create mode 100644 tests/git-bin/out.side-by-side create mode 100644 tests/git-bin/out.w70 diff --git a/cdiff.py b/cdiff.py index d5e230b..19705ca 100755 --- a/cdiff.py +++ b/cdiff.py @@ -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: @@ -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): @@ -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: diff --git a/tests/diff-ru-bin/in.diff b/tests/diff-ru-bin/in.diff new file mode 100644 index 0000000..52f23e4 --- /dev/null +++ b/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 diff --git a/tests/diff-ru-bin/out.normal b/tests/diff-ru-bin/out.normal new file mode 100644 index 0000000..697f9de --- /dev/null +++ b/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 + \ No newline at end of file diff --git a/tests/diff-ru-bin/out.side-by-side b/tests/diff-ru-bin/out.side-by-side new file mode 100644 index 0000000..d78a771 --- /dev/null +++ b/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 diff --git a/tests/diff-ru-bin/out.w70 b/tests/diff-ru-bin/out.w70 new file mode 100644 index 0000000..684c08a --- /dev/null +++ b/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 diff --git a/tests/dir/in.diff b/tests/diff-ru/in.diff similarity index 100% rename from tests/dir/in.diff rename to tests/diff-ru/in.diff diff --git a/tests/dir/out.normal b/tests/diff-ru/out.normal similarity index 100% rename from tests/dir/out.normal rename to tests/diff-ru/out.normal diff --git a/tests/dir/out.side-by-side b/tests/diff-ru/out.side-by-side similarity index 100% rename from tests/dir/out.side-by-side rename to tests/diff-ru/out.side-by-side diff --git a/tests/dir/out.w70 b/tests/diff-ru/out.w70 similarity index 100% rename from tests/dir/out.w70 rename to tests/diff-ru/out.w70 diff --git a/tests/git-bin/in.diff b/tests/git-bin/in.diff new file mode 100644 index 0000000..1876678 --- /dev/null +++ b/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 @@ +- ++ + + + +diff --git a/foo.pdf b/foo.pdf +index 0e90017..3c3b90d 100644 +Binary files a/foo.pdf and b/foo.pdf differ diff --git a/tests/git-bin/out.normal b/tests/git-bin/out.normal new file mode 100644 index 0000000..bcc2468 --- /dev/null +++ b/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 @@ +- ++ + + + +diff --git a/foo.pdf b/foo.pdf +index 0e90017..3c3b90d 100644 +Binary files a/foo.pdf and b/foo.pdf differ + \ No newline at end of file diff --git a/tests/git-bin/out.side-by-side b/tests/git-bin/out.side-by-side new file mode 100644 index 0000000..e26d6e7 --- /dev/null +++ b/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  1  +2  2  +3  3  +4  4  +diff --git a/foo.pdf b/foo.pdf +index 0e90017..3c3b90d 100644 +Binary files a/foo.pdf and b/foo.pdf differ + \ No newline at end of file diff --git a/tests/git-bin/out.w70 b/tests/git-bin/out.w70 new file mode 100644 index 0000000..dda9028 --- /dev/null +++ b/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  1  +2  2  +3  3  +4  4  +diff --git a/foo.pdf b/foo.pdf +index 0e90017..3c3b90d 100644 +Binary files a/foo.pdf and b/foo.pdf differ + \ No newline at end of file diff --git a/tests/test_cdiff.py b/tests/test_cdiff.py index 3e84581..a9920c5 100755 --- a/tests/test_cdiff.py +++ b/tests/test_cdiff.py @@ -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