Skip to content

Commit b1ae544

Browse files
committed
Fix name only
1 parent 9432e43 commit b1ae544

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

git/diff.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ def diff(
148148
if not any(x in kwargs for x in ('find_renames', 'no_renames', 'M')):
149149
args.append("-M")
150150

151-
if create_patch:
151+
name_only = 'name-only' in kwargs
152+
if not name_only and create_patch:
152153
args.append("-p")
153154
else:
154155
args.append("--raw")
@@ -170,6 +171,7 @@ def diff(
170171
elif other is NULL_TREE:
171172
args.insert(0, "-r") # recursive diff-tree
172173
args.insert(0, "--root")
174+
Diff.is_first = True
173175
diff_cmd = self.repo.git.diff_tree
174176
elif other is not None:
175177
args.insert(0, "-r") # recursive diff-tree
@@ -576,29 +578,33 @@ def _index_from_patch_format(cls, repo: "Repo", proc: Union["Popen", "Git.AutoIn
576578

577579
return index
578580

581+
is_first = False
582+
579583
@classmethod
580584
def _index_from_name_only_format(cls, repo, proc):
581585
"""Create a new DiffIndex from the given text which must be in name only format
582586
:param repo: is the repository we are operating on - it is required
583587
:param stream: result of 'git diff' as a stream (supporting file protocol)
584588
:return: git.DiffIndex """
585589

586-
cls.is_first = True
587-
588590
index = DiffIndex()
589591

590-
def handle_diff_line_name_only(line):
591-
path = line.decode(defenc)
592-
if cls.is_first:
593-
cls.is_first = False
594-
return
595-
596-
path = path.strip()
597-
a_path = path.encode(defenc)
598-
b_path = path.encode(defenc)
599-
index.append(Diff(repo, a_path, b_path, None, None, None, None,
600-
False, False, None, None, None,
601-
'', None, None))
592+
def handle_diff_line_name_only(lines):
593+
lines = lines.decode(defenc)
594+
595+
for line in lines.split('\x00'):
596+
path = line.strip()
597+
if len(path) == 0:
598+
continue
599+
if cls.is_first:
600+
cls.is_first = False
601+
continue
602+
603+
a_path = path.encode(defenc)
604+
b_path = path.encode(defenc)
605+
index.append(Diff(repo, a_path, b_path, None, None, None, None,
606+
False, False, None, None, None,
607+
'', None, None))
602608

603609
handle_process_output(proc, handle_diff_line_name_only, None, finalize_process, decode_streams=False)
604610

test/test_diff.py

+17
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ def test_diff_initial_commit_name_only(self):
276276
self.assertEqual(diff_index[0].a_path, 'CHANGES')
277277
self.assertEqual(diff_index[0].b_path, 'CHANGES')
278278

279+
def test_diff_between_commit_name_only(self):
280+
initial_commit = self.rorepo.commit('33ebe7acec14b25c5f84f35a664803fcab2f7781')
281+
new_commit = self.rorepo.commit('1f66cfbbce58b4b552b041707a12d437cc5f400a')
282+
283+
# Without creating a patch...
284+
diff_index = initial_commit.diff(new_commit, **{'name-only': True})
285+
286+
files = {'lib/git_python/diff.py', 'lib/git_python/git.py', 'lib/git_python/repo.py',
287+
'test/git/test_repo.py', 'README'}
288+
289+
for diff in diff_index:
290+
self.assertTrue(diff.a_path in files)
291+
self.assertTrue(diff.b_path in files)
292+
files.remove(diff.b_path)
293+
294+
self.assertTrue(len(files) == 0)
295+
279296
def test_diff_unsafe_paths(self):
280297
output = StringProcessAdapter(fixture("diff_patch_unsafe_paths"))
281298
res = Diff._index_from_patch_format(None, output)

0 commit comments

Comments
 (0)