@@ -96,7 +96,7 @@ def test_patch_create_from_buffers(self):
9696 new_as_path = BLOB_NEW_PATH ,
9797 )
9898
99- assert patch .patch == BLOB_PATCH
99+ assert patch .text == BLOB_PATCH
100100
101101 def test_patch_create_from_blobs (self ):
102102 old_blob = self .repo [BLOB_OLD_SHA ]
@@ -109,7 +109,7 @@ def test_patch_create_from_blobs(self):
109109 new_as_path = BLOB_NEW_PATH ,
110110 )
111111
112- assert patch .patch == BLOB_PATCH2
112+ assert patch .text == BLOB_PATCH2
113113
114114 def test_patch_create_from_blob_buffer (self ):
115115 old_blob = self .repo [BLOB_OLD_SHA ]
@@ -120,7 +120,7 @@ def test_patch_create_from_blob_buffer(self):
120120 new_as_path = BLOB_NEW_PATH ,
121121 )
122122
123- assert patch .patch == BLOB_PATCH
123+ assert patch .text == BLOB_PATCH
124124
125125 def test_patch_create_from_blob_buffer_add (self ):
126126 patch = pygit2 .Patch .create_from (
@@ -130,7 +130,7 @@ def test_patch_create_from_blob_buffer_add(self):
130130 new_as_path = BLOB_NEW_PATH ,
131131 )
132132
133- assert patch .patch == BLOB_PATCH_ADDED
133+ assert patch .text == BLOB_PATCH_ADDED
134134
135135 def test_patch_create_from_blob_buffer_delete (self ):
136136 old_blob = self .repo [BLOB_OLD_SHA ]
@@ -142,7 +142,7 @@ def test_patch_create_from_blob_buffer_delete(self):
142142 new_as_path = BLOB_NEW_PATH ,
143143 )
144144
145- assert patch .patch == BLOB_PATCH_DELETED
145+ assert patch .text == BLOB_PATCH_DELETED
146146
147147 def test_patch_create_from_bad_old_type_arg (self ):
148148 with pytest .raises (TypeError ):
@@ -163,8 +163,8 @@ def test_context_lines(self):
163163 new_as_path = BLOB_NEW_PATH ,
164164 )
165165
166- context_count = (
167- len ( [line for line in patch .patch .splitlines () if line .startswith (" " )])
166+ context_count = len (
167+ [line for line in patch .text .splitlines () if line .startswith (" " )]
168168 )
169169
170170 assert context_count != 0
@@ -181,13 +181,12 @@ def test_no_context_lines(self):
181181 context_lines = 0 ,
182182 )
183183
184- context_count = (
185- len ( [line for line in patch .patch .splitlines () if line .startswith (" " )])
184+ context_count = len (
185+ [line for line in patch .text .splitlines () if line .startswith (" " )]
186186 )
187187
188188 assert context_count == 0
189189
190-
191190 def test_patch_create_blob_blobs (self ):
192191 old_blob = self .repo [self .repo .create_blob (BLOB_OLD_CONTENT )]
193192 new_blob = self .repo [self .repo .create_blob (BLOB_NEW_CONTENT )]
@@ -199,7 +198,7 @@ def test_patch_create_blob_blobs(self):
199198 new_as_path = BLOB_NEW_PATH ,
200199 )
201200
202- assert patch .patch == BLOB_PATCH
201+ assert patch .text == BLOB_PATCH
203202
204203 def test_patch_create_blob_buffer (self ):
205204 blob = self .repo [self .repo .create_blob (BLOB_OLD_CONTENT )]
@@ -210,7 +209,7 @@ def test_patch_create_blob_buffer(self):
210209 new_as_path = BLOB_NEW_PATH ,
211210 )
212211
213- assert patch .patch == BLOB_PATCH
212+ assert patch .text == BLOB_PATCH
214213
215214 def test_patch_create_blob_delete (self ):
216215 blob = self .repo [self .repo .create_blob (BLOB_OLD_CONTENT )]
@@ -221,7 +220,7 @@ def test_patch_create_blob_delete(self):
221220 new_as_path = BLOB_NEW_PATH ,
222221 )
223222
224- assert patch .patch == BLOB_PATCH_DELETED
223+ assert patch .text == BLOB_PATCH_DELETED
225224
226225 def test_patch_create_blob_add (self ):
227226 blob = self .repo [self .repo .create_blob (BLOB_NEW_CONTENT )]
@@ -232,7 +231,7 @@ def test_patch_create_blob_add(self):
232231 new_as_path = BLOB_NEW_PATH ,
233232 )
234233
235- assert patch .patch == BLOB_PATCH_ADDED
234+ assert patch .text == BLOB_PATCH_ADDED
236235
237236 def test_patch_delete_blob (self ):
238237 blob = self .repo [BLOB_OLD_SHA ]
@@ -246,24 +245,73 @@ def test_patch_delete_blob(self):
246245 # Make sure that even after deleting the blob the patch still has the
247246 # necessary references to generate its patch
248247 del blob
249- assert patch .patch == BLOB_PATCH_DELETED
248+ assert patch .text == BLOB_PATCH_DELETED
250249
251250 def test_patch_multi_blob (self ):
252251 blob = self .repo [BLOB_OLD_SHA ]
253252 patch = pygit2 .Patch .create_from (
254253 blob ,
255254 None
256255 )
257- patch_text = patch .patch
256+ patch_text = patch .text
258257
259258 blob = self .repo [BLOB_OLD_SHA ]
260259 patch2 = pygit2 .Patch .create_from (
261260 blob ,
262261 None
263262 )
264- patch_text2 = patch .patch
263+ patch_text2 = patch .text
265264
266265 assert patch_text == patch_text2
267- assert patch_text == patch .patch
268- assert patch_text2 == patch2 .patch
269- assert patch .patch == patch2 .patch
266+ assert patch_text == patch .text
267+ assert patch_text2 == patch2 .text
268+ assert patch .text == patch2 .text
269+
270+
271+ class PatchEncodingTest (utils .AutoRepoTestCase ):
272+ repo_spec = 'tar' , 'encoding'
273+ expected_diff = b"""diff --git a/iso-8859-1.txt b/iso-8859-1.txt
274+ index e84e339..201e0c9 100644
275+ --- a/iso-8859-1.txt
276+ +++ b/iso-8859-1.txt
277+ @@ -1 +1,2 @@
278+ Kristian H\xf8 gsberg
279+ +foo
280+ """
281+
282+ def test_patch_from_non_utf8 (self ):
283+ # blobs encoded in ISO-8859-1
284+ old_content = b'Kristian H\xf8 gsberg\n '
285+ new_content = old_content + b'foo\n '
286+ patch = pygit2 .Patch .create_from (
287+ old_content ,
288+ new_content ,
289+ old_as_path = 'iso-8859-1.txt' ,
290+ new_as_path = 'iso-8859-1.txt' ,
291+ )
292+
293+ self .assertEqual (patch .data , self .expected_diff )
294+
295+ self .assertEqual (
296+ patch .text , self .expected_diff .decode ('utf-8' , errors = 'replace' ))
297+
298+ # `patch.text` corrupted the ISO-8859-1 content as it forced UTF-8
299+ # decoding, so assert that we cannot get the original content back:
300+ self .assertNotEqual (patch .text .encode ('utf-8' ), self .expected_diff )
301+
302+ def test_patch_create_from_blobs (self ):
303+ patch = pygit2 .Patch .create_from (
304+ self .repo ['e84e339ac7fcc823106efa65a6972d7a20016c85' ],
305+ self .repo ['201e0c908e3d9f526659df3e556c3d06384ef0df' ],
306+ old_as_path = 'iso-8859-1.txt' ,
307+ new_as_path = 'iso-8859-1.txt' ,
308+ )
309+
310+ self .assertEqual (patch .data , self .expected_diff )
311+
312+ self .assertEqual (
313+ patch .text , self .expected_diff .decode ('utf-8' , errors = 'replace' ))
314+
315+ # `patch.text` corrupted the ISO-8859-1 content as it forced UTF-8
316+ # decoding, so assert that we cannot get the original content back:
317+ self .assertNotEqual (patch .text .encode ('utf-8' ), self .expected_diff )
0 commit comments