Skip to content

Commit 85a5a8c

Browse files
committed
Fixed mode-handling in Diff creation, and added assertions to catch this in future
There is still some work todo in terms of how we handle the encoding
1 parent 18caa61 commit 85a5a8c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

‎git/diff.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,12 @@ def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode,
217217
if a_blob_id is None:
218218
self.a_blob = None
219219
else:
220+
assert self.a_mode
220221
self.a_blob = Blob(repo, hex_to_bin(a_blob_id), mode=self.a_mode, path=a_path)
221222
if b_blob_id is None:
222223
self.b_blob = None
223224
else:
225+
assert self.b_mode
224226
self.b_blob = Blob(repo, hex_to_bin(b_blob_id), mode=self.b_mode, path=b_path)
225227

226228
self.new_file = new_file
@@ -313,8 +315,9 @@ def _index_from_patch_format(cls, repo, stream):
313315

314316
# Make sure the mode is set if the path is set. Otherwise the resulting blob is invalid
315317
# We just use the one mode we should have parsed
316-
index.append(Diff(repo, a_path, b_path, a_blob_id, b_blob_id,
317-
old_mode or deleted_file_mode or b_mode, new_mode or new_file_mode or b_mode,
318+
a_mode = old_mode or deleted_file_mode or (a_path and (b_mode or new_mode or new_file_mode))
319+
b_mode = b_mode or new_mode or new_file_mode or (b_path and a_mode)
320+
index.append(Diff(repo, a_path, b_path, a_blob_id, b_blob_id, a_mode, b_mode,
318321
new_file, deleted_file, rename_from, rename_to, None))
319322

320323
previous_header = header

‎git/test/test_diff.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_binary_diff(self):
7373
assert len(list(res.iter_change_type('M'))) == 1
7474
if res[0].diff:
7575
assert res[0].diff == "Binary files a/rps and b/rps differ\n", "in patch mode, we get a diff text"
76-
assert isinstance(str(res[0]), str), "This call should just work"
76+
assert str(res[0]), "This call should just work"
7777
# end for each method to test
7878

7979
def test_diff_index(self):
@@ -82,6 +82,7 @@ def test_diff_index(self):
8282
assert len(res) == 6
8383
for dr in res:
8484
assert dr.diff
85+
assert str(dr), "Diff to string conversion should be possible"
8586
# end for each diff
8687

8788
dr = res[3]
@@ -129,6 +130,9 @@ def test_diff_interface(self):
129130
assert len(diff_set) == 1
130131
assert diff_index[0] == diff_index[0]
131132
assert not (diff_index[0] != diff_index[0])
133+
134+
for dr in diff_index:
135+
assert str(dr), "Diff to string conversion should be possible"
132136
# END diff index checking
133137
# END for each patch option
134138
# END for each path option

0 commit comments

Comments
 (0)