Browse Source

Handle 'Binary files ... differ'

Matthew Wang 11 years ago
parent
commit
a413a0351d

+ 15
- 5
cdiff.py View File

@@ -169,6 +169,11 @@ class Diff(object):
169 169
     def is_only_in_dir(self, line):
170 170
         return False
171 171
 
172
+    def is_binary_differ(self, line):
173
+        return False
174
+
175
+    # Followings are not suppose to override
176
+    #
172 177
     def markup_traditional(self):
173 178
         """Returns a generator"""
174 179
         for line in self._headers:
@@ -421,6 +426,9 @@ class Udiff(Diff):
421 426
     def is_only_in_dir(self, line):
422 427
         return line.startswith('Only in ')
423 428
 
429
+    def is_binary_differ(self, line):
430
+        return re.match('^Binary files .* differ$', line.rstrip())
431
+
424 432
 
425 433
 class PatchStream(object):
426 434
 
@@ -529,14 +537,16 @@ class DiffParser(object):
529 537
                 # ignore
530 538
                 pass
531 539
 
532
-            elif difflet.is_only_in_dir(line):
533
-                # 'Only in foo: ' is considered a separate diff, so yield
534
-                # current diff, then this line
540
+            elif difflet.is_only_in_dir(line) or difflet.is_binary_differ(line):
541
+                # 'Only in foo:' and 'Binary files ... differ' are considered as
542
+                # separate diffs, so yield current diff, then this line
535 543
                 #
536 544
                 if diff._old_path and diff._new_path and len(diff._hunks) > 0:
537
-                    # One diff constructed
545
+                    # Current diff is comppletely constructed
538 546
                     yield diff
539
-                yield Diff([line], '', '', [])
547
+                headers.append(line)
548
+                yield Diff(headers, '', '', [])
549
+                headers = []
540 550
                 diff = Diff([], None, None, [])
541 551
 
542 552
             else:

+ 20
- 0
tests/diff-ru-bin/in.diff View File

@@ -0,0 +1,20 @@
1
+diff -ru a/README b/README
2
+--- a/README	2013-02-23 19:51:41.000000000 +0800
3
++++ b/README	2013-02-23 19:52:06.000000000 +0800
4
+@@ -1,6 +1,6 @@
5
+-# To generate expected output, chdir to a subdir and use following command, then
6
++# To generate expected output, cd to a subdir and use following command, then
7
+ # review with `less -R`
8
+-#
9
+ ../../cdiff.py -c always < in.diff > out.normal
10
+ ../../cdiff.py -c always -s < in.diff > out.side-by-side
11
+ ../../cdiff.py -c always -s < in.diff -w70 > out.w70
12
++# EOF
13
+Binary files a/foo.pdf and b/foo.pdf differ
14
+diff -ru a/foo.txt b/foo.txt
15
+--- a/foo.txt	2013-02-23 19:55:03.000000000 +0800
16
++++ b/foo.txt	2013-02-23 19:55:10.000000000 +0800
17
+@@ -1,2 +1,2 @@
18
+-Hello
19
++hella
20
+ world

+ 21
- 0
tests/diff-ru-bin/out.normal View File

@@ -0,0 +1,21 @@
1
+diff -ru a/README b/README
2
+--- a/README	2013-02-23 19:51:41.000000000 +0800
3
++++ b/README	2013-02-23 19:52:06.000000000 +0800
4
+@@ -1,6 +1,6 @@
5
+-# To generate expected output, chdir to a subdir and use following command, then
6
++# To generate expected output, cd to a subdir and use following command, then
7
+ # review with `less -R`
8
+-#
9
+ ../../cdiff.py -c always < in.diff > out.normal
10
+ ../../cdiff.py -c always -s < in.diff > out.side-by-side
11
+ ../../cdiff.py -c always -s < in.diff -w70 > out.w70
12
++# EOF
13
+Binary files a/foo.pdf and b/foo.pdf differ
14
+diff -ru a/foo.txt b/foo.txt
15
+--- a/foo.txt	2013-02-23 19:55:03.000000000 +0800
16
++++ b/foo.txt	2013-02-23 19:55:10.000000000 +0800
17
+@@ -1,2 +1,2 @@
18
+-Hello
19
++hella
20
+ world
21
+

+ 18
- 0
tests/diff-ru-bin/out.side-by-side View File

@@ -0,0 +1,18 @@
1
+diff -ru a/README b/README
2
+--- a/README	2013-02-23 19:51:41.000000000 +0800
3
++++ b/README	2013-02-23 19:52:06.000000000 +0800
4
+@@ -1,6 +1,6 @@
5
+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
6
+2 # review with `less -R`                                                          2 # review with `less -R`
7
+3 #   
8
+4 ../../cdiff.py -c always < in.diff > out.normal                                  3 ../../cdiff.py -c always < in.diff > out.normal
9
+5 ../../cdiff.py -c always -s < in.diff > out.side-by-side                         4 ../../cdiff.py -c always -s < in.diff > out.side-by-side
10
+6 ../../cdiff.py -c always -s < in.diff -w70 > out.w70                             5 ../../cdiff.py -c always -s < in.diff -w70 > out.w70
11
+                                                                                   6 # EOF
12
+Binary files a/foo.pdf and b/foo.pdf differ
13
+diff -ru a/foo.txt b/foo.txt
14
+--- a/foo.txt	2013-02-23 19:55:03.000000000 +0800
15
++++ b/foo.txt	2013-02-23 19:55:10.000000000 +0800
16
+@@ -1,2 +1,2 @@
17
+1 Hello                                                                            1 hella
18
+2 world                                                                            2 world

+ 18
- 0
tests/diff-ru-bin/out.w70 View File

@@ -0,0 +1,18 @@
1
+diff -ru a/README b/README
2
+--- a/README	2013-02-23 19:51:41.000000000 +0800
3
++++ b/README	2013-02-23 19:52:06.000000000 +0800
4
+@@ -1,6 +1,6 @@
5
+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>
6
+2 # review with `less -R`                                                2 # review with `less -R`
7
+3 #   
8
+4 ../../cdiff.py -c always < in.diff > out.normal                        3 ../../cdiff.py -c always < in.diff > out.normal
9
+5 ../../cdiff.py -c always -s < in.diff > out.side-by-side               4 ../../cdiff.py -c always -s < in.diff > out.side-by-side
10
+6 ../../cdiff.py -c always -s < in.diff -w70 > out.w70                   5 ../../cdiff.py -c always -s < in.diff -w70 > out.w70
11
+                                                                         6 # EOF
12
+Binary files a/foo.pdf and b/foo.pdf differ
13
+diff -ru a/foo.txt b/foo.txt
14
+--- a/foo.txt	2013-02-23 19:55:03.000000000 +0800
15
++++ b/foo.txt	2013-02-23 19:55:10.000000000 +0800
16
+@@ -1,2 +1,2 @@
17
+1 Hello                                                                  1 hella
18
+2 world                                                                  2 world

tests/dir/in.diff → tests/diff-ru/in.diff View File


tests/dir/out.normal → tests/diff-ru/out.normal View File


tests/dir/out.side-by-side → tests/diff-ru/out.side-by-side View File


tests/dir/out.w70 → tests/diff-ru/out.w70 View File


+ 26
- 0
tests/git-bin/in.diff View File

@@ -0,0 +1,26 @@
1
+diff --git a/2pdf.sh b/2pdf.sh
2
+index 529d8a3..ad71911 100755
3
+--- a/2pdf.sh
4
++++ b/2pdf.sh
5
+@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
6
+ OUTPUT=${INPUT%.html}.pdf
7
+ 
8
+ wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments ends here
9
++
10
++# EOF
11
+diff --git a/example.pdf b/example.pdf
12
+index 1eacfd8..3696851 100644
13
+Binary files a/example.pdf and b/example.pdf differ
14
+diff --git a/foo.html b/foo.html
15
+index d2fd3fb..13afa6e 100644
16
+--- a/foo.html
17
++++ b/foo.html
18
+@@ -1,4 +1,4 @@
19
+-<!doctype html>
20
++<!DOCTYPE html>
21
+ <html>
22
+ 
23
+ <head>
24
+diff --git a/foo.pdf b/foo.pdf
25
+index 0e90017..3c3b90d 100644
26
+Binary files a/foo.pdf and b/foo.pdf differ

+ 27
- 0
tests/git-bin/out.normal View File

@@ -0,0 +1,27 @@
1
+diff --git a/2pdf.sh b/2pdf.sh
2
+index 529d8a3..ad71911 100755
3
+--- a/2pdf.sh
4
++++ b/2pdf.sh
5
+@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
6
+ OUTPUT=${INPUT%.html}.pdf
7
+ 
8
+ wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments ends here
9
++
10
++# EOF
11
+diff --git a/example.pdf b/example.pdf
12
+index 1eacfd8..3696851 100644
13
+Binary files a/example.pdf and b/example.pdf differ
14
+diff --git a/foo.html b/foo.html
15
+index d2fd3fb..13afa6e 100644
16
+--- a/foo.html
17
++++ b/foo.html
18
+@@ -1,4 +1,4 @@
19
+-<!doctype html>
20
++<!DOCTYPE html>
21
+ <html>
22
+ 
23
+ <head>
24
+diff --git a/foo.pdf b/foo.pdf
25
+index 0e90017..3c3b90d 100644
26
+Binary files a/foo.pdf and b/foo.pdf differ
27
+

+ 26
- 0
tests/git-bin/out.side-by-side View File

@@ -0,0 +1,26 @@
1
+diff --git a/2pdf.sh b/2pdf.sh
2
+index 529d8a3..ad71911 100755
3
+--- a/2pdf.sh
4
++++ b/2pdf.sh
5
+@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
6
+13 OUTPUT=${INPUT%.html}.pdf                                                        13 OUTPUT=${INPUT%.html}.pdf
7
+14                                                                                  14 
8
+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
9
+                                                                                    16 
10
+                                                                                    17 # EOF
11
+diff --git a/example.pdf b/example.pdf
12
+index 1eacfd8..3696851 100644
13
+Binary files a/example.pdf and b/example.pdf differ
14
+diff --git a/foo.html b/foo.html
15
+index d2fd3fb..13afa6e 100644
16
+--- a/foo.html
17
++++ b/foo.html
18
+@@ -1,4 +1,4 @@
19
+1 <!doctype html>                                                                  1 <!DOCTYPE html>
20
+2 <html>                                                                           2 <html>
21
+3                                                                                  3 
22
+4 <head>                                                                           4 <head>
23
+diff --git a/foo.pdf b/foo.pdf
24
+index 0e90017..3c3b90d 100644
25
+Binary files a/foo.pdf and b/foo.pdf differ
26
+

+ 26
- 0
tests/git-bin/out.w70 View File

@@ -0,0 +1,26 @@
1
+diff --git a/2pdf.sh b/2pdf.sh
2
+index 529d8a3..ad71911 100755
3
+--- a/2pdf.sh
4
++++ b/2pdf.sh
5
+@@ -13,3 +13,5 @@ INPUT=${1:-foo.html}
6
+13 OUTPUT=${INPUT%.html}.pdf                                              13 OUTPUT=${INPUT%.html}.pdf
7
+14                                                                        14 
8
+15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments > 15 wkhtmltopdf --page-size A4 $INPUT $OUTPUT  # very very long comments >
9
+                                                                          16 
10
+                                                                          17 # EOF
11
+diff --git a/example.pdf b/example.pdf
12
+index 1eacfd8..3696851 100644
13
+Binary files a/example.pdf and b/example.pdf differ
14
+diff --git a/foo.html b/foo.html
15
+index d2fd3fb..13afa6e 100644
16
+--- a/foo.html
17
++++ b/foo.html
18
+@@ -1,4 +1,4 @@
19
+1 <!doctype html>                                                        1 <!DOCTYPE html>
20
+2 <html>                                                                 2 <html>
21
+3                                                                        3 
22
+4 <head>                                                                 4 <head>
23
+diff --git a/foo.pdf b/foo.pdf
24
+index 0e90017..3c3b90d 100644
25
+Binary files a/foo.pdf and b/foo.pdf differ
26
+

+ 66
- 0
tests/test_cdiff.py View File

@@ -397,6 +397,72 @@ Only in foo: foo
397 397
         self.assertEqual(len(out[1]._hunks), 0)
398 398
         self.assertEqual(out[1]._headers, ['Only in foo: foo\n'])
399 399
 
400
+    def test_parse_binary_differ_diff_ru(self):
401
+        patch = r"""\
402
+--- a
403
++++ b
404
+@@ -1,2 +1,2 @@
405
+-foo
406
++bar
407
+ common
408
+Binary files a/1.pdf and b/1.pdf differ
409
+--- c
410
++++ d
411
+@@ -1,2 +1,2 @@
412
+-foo
413
++bar
414
+ common
415
+"""
416
+        items = patch.splitlines(True)
417
+        stream = cdiff.PatchStream(Sequential(items))
418
+        parser = cdiff.DiffParser(stream)
419
+
420
+        out = list(parser._parse())
421
+        self.assertEqual(len(out), 3)
422
+        self.assertEqual(len(out[1]._hunks), 0)
423
+        self.assertEqual(out[1]._old_path, '')
424
+        self.assertEqual(out[1]._new_path, '')
425
+        self.assertEqual(len(out[1]._headers), 1)
426
+        self.assertTrue(out[1]._headers[0].startswith('Binary files'))
427
+        self.assertEqual(len(out[2]._hunks), 1)
428
+        self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
429
+
430
+    def test_parse_binary_differ_git(self):
431
+        patch = r"""\
432
+diff --git a/foo b/foo
433
+index 529d8a3..ad71911 100755
434
+--- a/foo
435
++++ b/foo
436
+@@ -1,2 +1,2 @@
437
+-foo
438
++bar
439
+ common
440
+diff --git a/example.pdf b/example.pdf
441
+index 1eacfd8..3696851 100644
442
+Binary files a/example.pdf and b/example.pdf differ
443
+diff --git a/bar b/bar
444
+index 529e8a3..ad71921 100755
445
+--- a/bar
446
++++ b/bar
447
+@@ -1,2 +1,2 @@
448
+-foo
449
++bar
450
+ common
451
+"""
452
+        items = patch.splitlines(True)
453
+        stream = cdiff.PatchStream(Sequential(items))
454
+        parser = cdiff.DiffParser(stream)
455
+
456
+        out = list(parser._parse())
457
+        self.assertEqual(len(out), 3)
458
+        self.assertEqual(len(out[1]._hunks), 0)
459
+        self.assertEqual(out[1]._old_path, '')
460
+        self.assertEqual(out[1]._new_path, '')
461
+        self.assertEqual(len(out[1]._headers), 3)
462
+        self.assertTrue(out[1]._headers[2].startswith('Binary files'))
463
+        self.assertEqual(len(out[2]._hunks), 1)
464
+        self.assertEqual(len(out[2]._hunks[0]._hunk_list), 3)
465
+
400 466
     def test_parse_svn_prop(self):
401 467
         patch = r"""\
402 468
 --- a