Skip to content

Commit 4006c43

Browse files
committed
fix(diff): use explicit change-type if possible
That way, we do not have to figure the change type out by examining the diff object. It's implemented in a way that should yield more desireable results as we keep the change-type that git is providing us with. Fixes #493
1 parent 1ec4389 commit 4006c43

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

doc/source/changes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
Changelog
33
=========
44

5+
2.0.8 - Bugfixes
6+
================
7+
8+
* `DiffIndex.iter_change_type(...)` produces better results when diffing
9+
an index against the working tree.
10+
511
2.0.7 - New Features
612
====================
713

git/diff.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ def iter_change_type(self, change_type):
183183
raise ValueError("Invalid change type: %s" % change_type)
184184

185185
for diff in self:
186-
if change_type == "A" and diff.new_file:
186+
if diff.change_type == change_type:
187+
yield diff
188+
elif change_type == "A" and diff.new_file:
187189
yield diff
188190
elif change_type == "D" and diff.deleted_file:
189191
yield diff
@@ -247,11 +249,12 @@ class Diff(object):
247249
NULL_BIN_SHA = b"\0" * 20
248250

249251
__slots__ = ("a_blob", "b_blob", "a_mode", "b_mode", "a_rawpath", "b_rawpath",
250-
"new_file", "deleted_file", "raw_rename_from", "raw_rename_to", "diff")
252+
"new_file", "deleted_file", "raw_rename_from", "raw_rename_to",
253+
"diff", "change_type")
251254

252255
def __init__(self, repo, a_rawpath, b_rawpath, a_blob_id, b_blob_id, a_mode,
253256
b_mode, new_file, deleted_file, raw_rename_from,
254-
raw_rename_to, diff):
257+
raw_rename_to, diff, change_type):
255258

256259
self.a_mode = a_mode
257260
self.b_mode = b_mode
@@ -286,6 +289,7 @@ def __init__(self, repo, a_rawpath, b_rawpath, a_blob_id, b_blob_id, a_mode,
286289
self.raw_rename_to = raw_rename_to or None
287290

288291
self.diff = diff
292+
self.change_type = change_type
289293

290294
def __eq__(self, other):
291295
for name in self.__slots__:
@@ -435,7 +439,7 @@ def _index_from_patch_format(cls, repo, stream):
435439
new_file, deleted_file,
436440
rename_from,
437441
rename_to,
438-
None))
442+
None, None))
439443

440444
previous_header = header
441445
# end for each header we parse
@@ -483,7 +487,7 @@ def _index_from_raw_format(cls, repo, stream):
483487
# END add/remove handling
484488

485489
diff = Diff(repo, a_path, b_path, a_blob_id, b_blob_id, old_mode, new_mode,
486-
new_file, deleted_file, rename_from, rename_to, '')
490+
new_file, deleted_file, rename_from, rename_to, '', change_type)
487491
index.append(diff)
488492
# END for each line
489493

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:100644 100644 739bc65220ad90e9ebfa2d6af1723b97555569a4 0000000000000000000000000000000000000000 M README.md

git/test/test_diff.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ def test_diff_with_rename(self):
104104
assert diff.rename_to == 'that'
105105
assert len(list(diffs.iter_change_type('R'))) == 1
106106

107+
def test_diff_of_modified_files_not_added_to_the_index(self):
108+
output = StringProcessAdapter(fixture('diff_abbrev-40_full-index_M_raw_no-color'))
109+
diffs = Diff._index_from_raw_format(self.rorepo, output.stdout)
110+
111+
assert len(diffs) == 1, 'one modification'
112+
assert len(list(diffs.iter_change_type('M'))) == 1, 'one modification'
113+
assert diffs[0].change_type == 'M'
114+
assert diffs[0].b_blob is None
115+
107116
def test_binary_diff(self):
108117
for method, file_name in ((Diff._index_from_patch_format, 'diff_patch_binary'),
109118
(Diff._index_from_raw_format, 'diff_raw_binary')):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy