Merge lp:~abentley/bzr/parse-binary-diff into lp:bzr/2.0

Proposed by Aaron Bentley
Status: Merged
Merged at revision: not available
Proposed branch: lp:~abentley/bzr/parse-binary-diff
Merge into: lp:bzr/2.0
Diff against target: 157 lines
4 files modified
NEWS (+2/-0)
bzrlib/patches.py (+31/-6)
bzrlib/tests/test_patches.py (+30/-0)
bzrlib/tests/test_patches_data/binary.patch (+6/-0)
To merge this branch: bzr merge lp:~abentley/bzr/parse-binary-diff
Reviewer Review Type Date Requested Status
Vincent Ladeuil Approve
Review via email: mp+13422@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Aaron Bentley (abentley) wrote :

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi all,

This branch teaches the patch parsing code to handle "Binary files
differ" sections, which may be generated by bzr's diffing code. Without
this patch, a MalformedHeader exception is raised.

It fixes the library side of Bug #436325.

Aaron
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkrXQ1EACgkQ0F+nu1YWqI0sLQCfUCaJZP53TRjir35i8CdqBNRj
TLsAnia62pm+bdWOHS9rSmt1zjyIXMZC
=yd6K
-----END PGP SIGNATURE-----

Revision history for this message
Vincent Ladeuil (vila) wrote :

Sounds good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS'
2--- NEWS 2009-10-16 15:11:52 +0000
3+++ NEWS 2009-10-16 19:28:11 +0000
4@@ -23,6 +23,8 @@
5 * Avoid "NoneType has no attribute st_mode" error when files disappear
6 from a directory while it's being read. (Martin Pool, #446033)
7
8+* Diff parsing handles "Binary files differ" hunks. (Aaron Bentley, #436325)
9+
10 * PreviewTree file names are not limited by the encoding of the temp
11 directory's filesystem. (Aaron Bentley, #436794)
12
13
14=== modified file 'bzrlib/patches.py'
15--- bzrlib/patches.py 2009-03-23 14:59:43 +0000
16+++ bzrlib/patches.py 2009-10-16 19:28:11 +0000
17@@ -14,6 +14,15 @@
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+import re
22+
23+
24+class BinaryFiles(Exception):
25+
26+ def __init__(self, orig_name, mod_name):
27+ self.orig_name = orig_name
28+ self.mod_name = mod_name
29+ Exception.__init__(self, 'Binary files section encountered.')
30
31
32 class PatchSyntax(Exception):
33@@ -57,6 +66,9 @@
34 def get_patch_names(iter_lines):
35 try:
36 line = iter_lines.next()
37+ match = re.match('Binary files (.*) and (.*) differ\n', line)
38+ if match is not None:
39+ raise BinaryFiles(match.group(1), match.group(2))
40 if not line.startswith("--- "):
41 raise MalformedPatchHeader("No orig name", line)
42 else:
43@@ -259,10 +271,19 @@
44 yield hunk
45
46
47-class Patch:
48+class BinaryPatch(object):
49 def __init__(self, oldname, newname):
50 self.oldname = oldname
51 self.newname = newname
52+
53+ def __str__(self):
54+ return 'Binary files %s and %s differ\n' % (self.oldname, self.newname)
55+
56+
57+class Patch(BinaryPatch):
58+
59+ def __init__(self, oldname, newname):
60+ BinaryPatch.__init__(self, oldname, newname)
61 self.hunks = []
62
63 def __str__(self):
64@@ -317,11 +338,15 @@
65
66 def parse_patch(iter_lines):
67 iter_lines = iter_lines_handle_nl(iter_lines)
68- (orig_name, mod_name) = get_patch_names(iter_lines)
69- patch = Patch(orig_name, mod_name)
70- for hunk in iter_hunks(iter_lines):
71- patch.hunks.append(hunk)
72- return patch
73+ try:
74+ (orig_name, mod_name) = get_patch_names(iter_lines)
75+ except BinaryFiles, e:
76+ return BinaryPatch(e.orig_name, e.mod_name)
77+ else:
78+ patch = Patch(orig_name, mod_name)
79+ for hunk in iter_hunks(iter_lines):
80+ patch.hunks.append(hunk)
81+ return patch
82
83
84 def iter_file_patch(iter_lines):
85
86=== modified file 'bzrlib/tests/test_patches.py'
87--- bzrlib/tests/test_patches.py 2009-03-23 14:59:43 +0000
88+++ bzrlib/tests/test_patches.py 2009-10-16 19:28:11 +0000
89@@ -24,6 +24,9 @@
90 from bzrlib.patches import (MalformedLine,
91 MalformedHunkHeader,
92 MalformedPatchHeader,
93+ BinaryPatch,
94+ BinaryFiles,
95+ Patch,
96 ContextLine,
97 InsertLine,
98 RemoveLine,
99@@ -45,6 +48,13 @@
100 "test_patches_data", filename)
101 return file(data_path, "rb")
102
103+ def data_lines(self, filename):
104+ datafile = self.datafile(filename)
105+ try:
106+ return datafile.readlines()
107+ finally:
108+ datafile.close()
109+
110 def testValidPatchHeader(self):
111 """Parse a valid patch header"""
112 lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
113@@ -136,6 +146,21 @@
114 patchtext = self.datafile("patchtext.patch").read()
115 self.compare_parsed(patchtext)
116
117+ def test_parse_binary(self):
118+ """Test parsing a whole patch"""
119+ patches = parse_patches(self.data_lines("binary.patch"))
120+ self.assertIs(BinaryPatch, patches[0].__class__)
121+ self.assertIs(Patch, patches[1].__class__)
122+ self.assertContainsRe(patches[0].oldname, '^bar\t')
123+ self.assertContainsRe(patches[0].newname, '^qux\t')
124+ self.assertContainsRe(str(patches[0]),
125+ 'Binary files bar\t.* and qux\t.* differ\n')
126+
127+ def test_roundtrip_binary(self):
128+ patchtext = ''.join(self.data_lines("binary.patch"))
129+ patches = parse_patches(patchtext.splitlines(True))
130+ self.assertEqual(patchtext, ''.join(str(p) for p in patches))
131+
132 def testInit(self):
133 """Handle patches missing half the position, range tuple"""
134 patchtext = \
135@@ -194,6 +219,11 @@
136 count += 1
137 self.assertEqual(count, len(mod_lines))
138
139+ def test_iter_patched_binary(self):
140+ binary_lines = self.data_lines('binary.patch')
141+ e = self.assertRaises(BinaryFiles, iter_patched, [], binary_lines)
142+
143+
144 def test_iter_patched_from_hunks(self):
145 """Test a few patch files, and make sure they work."""
146 files = [
147
148=== added file 'bzrlib/tests/test_patches_data/binary.patch'
149--- bzrlib/tests/test_patches_data/binary.patch 1970-01-01 00:00:00 +0000
150+++ bzrlib/tests/test_patches_data/binary.patch 2009-10-16 19:28:11 +0000
151@@ -0,0 +1,6 @@
152+Binary files bar 2009-10-14 19:49:59 +0000 and qux 2009-10-14 19:50:35 +0000 differ
153+--- baz 2009-10-14 19:49:59 +0000
154++++ quxx 2009-10-14 19:51:00 +0000
155+@@ -1 +1 @@
156+-hello
157++goodbye

Subscribers

People subscribed via source and target branches