Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc767b52

Browse files
committed
Now Diff.__str__ works correctly in all python versions.
Additionally, unicode handling was improved to the point where we dealwith all diff(create_path=True) data as binary.Therefore we don't claim to know all encodings of all textfiles in the world,even though we still assume that everything git throws at us is utf-8 encoded.Fixesgitpython-developers#113
1 parent85a5a8c commitc767b52

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

‎git/diff.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
from .objects.blobimportBlob
1111
from .objects.utilimportmode_str_to_int
1212

13-
fromgit.compatimportdefenc
13+
fromgit.compatimport (
14+
defenc,
15+
PY3
16+
)
1417

1518

1619
__all__= ('Diffable','DiffIndex','Diff')
@@ -194,7 +197,7 @@ class Diff(object):
194197
(?:^deleted[ ]file[ ]mode[ ](?P<deleted_file_mode>.+)(?:\n|$))?
195198
(?:^index[ ](?P<a_blob_id>[0-9A-Fa-f]+)
196199
\.\.(?P<b_blob_id>[0-9A-Fa-f]+)[ ]?(?P<b_mode>.+)?(?:\n|$))?
197-
""",re.VERBOSE|re.MULTILINE)
200+
""".encode('ascii'),re.VERBOSE|re.MULTILINE)
198201
# can be used for comparisons
199202
NULL_HEX_SHA="0"*40
200203
NULL_BIN_SHA=b"\0"*20
@@ -280,11 +283,21 @@ def __str__(self):
280283
msg+='\nfile renamed to %r'%self.rename_to
281284
ifself.diff:
282285
msg+='\n---'
283-
msg+=self.diff
286+
try:
287+
msg+=self.diff.decode(defenc)
288+
exceptUnicodeDecodeError:
289+
msg+='OMITTED BINARY DATA'
290+
# end handle encoding
284291
msg+='\n---'
285292
# END diff info
286293

287-
returnh+msg
294+
# Python2 sillyness: have to assure we convert our likely to be unicode object to a string with the
295+
# right encoding. Otherwise it tries to convert it using ascii, which may fail ungracefully
296+
res=h+msg
297+
ifnotPY3:
298+
res=res.encode(defenc)
299+
# end
300+
returnres
288301

289302
@property
290303
defrenamed(self):
@@ -298,7 +311,7 @@ def _index_from_patch_format(cls, repo, stream):
298311
:param stream: result of 'git diff' as a stream (supporting file protocol)
299312
:return: git.DiffIndex """
300313
# for now, we have to bake the stream
301-
text=stream.read().decode(defenc)
314+
text=stream.read()
302315
index=DiffIndex()
303316
previous_header=None
304317
forheaderincls.re_header.finditer(text):
@@ -317,8 +330,17 @@ def _index_from_patch_format(cls, repo, stream):
317330
# We just use the one mode we should have parsed
318331
a_mode=old_modeordeleted_file_modeor (a_pathand (b_modeornew_modeornew_file_mode))
319332
b_mode=b_modeornew_modeornew_file_modeor (b_pathanda_mode)
320-
index.append(Diff(repo,a_path,b_path,a_blob_id,b_blob_id,a_mode,b_mode,
321-
new_file,deleted_file,rename_from,rename_to,None))
333+
index.append(Diff(repo,
334+
a_pathanda_path.decode(defenc),
335+
b_pathandb_path.decode(defenc),
336+
a_blob_idanda_blob_id.decode(defenc),
337+
b_blob_idandb_blob_id.decode(defenc),
338+
a_modeanda_mode.decode(defenc),
339+
b_modeandb_mode.decode(defenc),
340+
new_file,deleted_file,
341+
rename_fromandrename_from.decode(defenc),
342+
rename_toandrename_to.decode(defenc),
343+
None))
322344

323345
previous_header=header
324346
# end for each header we parse

‎git/test/test_diff.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_binary_diff(self):
7272
assertlen(res)==1
7373
assertlen(list(res.iter_change_type('M')))==1
7474
ifres[0].diff:
75-
assertres[0].diff=="Binary files a/rps and b/rps differ\n","in patch mode, we get a diff text"
75+
assertres[0].diff==b"Binary files a/rps and b/rps differ\n","in patch mode, we get a diff text"
7676
assertstr(res[0]),"This call should just work"
7777
# end for each method to test
7878

@@ -86,7 +86,7 @@ def test_diff_index(self):
8686
# end for each diff
8787

8888
dr=res[3]
89-
assertdr.diff.endswith("+Binary files a/rps and b/rps differ\n")
89+
assertdr.diff.endswith(b"+Binary files a/rps and b/rps differ\n")
9090

9191
deftest_diff_patch_format(self):
9292
# test all of the 'old' format diffs for completness - it should at least

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp