Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9432e43

Browse files
committed
git diff support --name-only option
1 parent f253335 commit 9432e43

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

git/diff.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,14 @@ def diff(
187187
kwargs["as_process"] = True
188188
proc = diff_cmd(*self._process_diff_args(args), **kwargs)
189189

190-
diff_method = Diff._index_from_patch_format if create_patch else Diff._index_from_raw_format
190+
cmdline = getattr(proc, 'args', '') # PY3+ only
191+
192+
if '--name-only' in cmdline:
193+
diff_method = Diff._index_from_name_only_format
194+
elif create_patch:
195+
diff_method = Diff._index_from_patch_format
196+
else:
197+
diff_method = Diff._index_from_raw_format
191198
index = diff_method(self.repo, proc)
192199

193200
proc.wait()
@@ -569,6 +576,34 @@ def _index_from_patch_format(cls, repo: "Repo", proc: Union["Popen", "Git.AutoIn
569576

570577
return index
571578

579+
@classmethod
580+
def _index_from_name_only_format(cls, repo, proc):
581+
"""Create a new DiffIndex from the given text which must be in name only format
582+
:param repo: is the repository we are operating on - it is required
583+
:param stream: result of 'git diff' as a stream (supporting file protocol)
584+
:return: git.DiffIndex """
585+
586+
cls.is_first = True
587+
588+
index = DiffIndex()
589+
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))
602+
603+
handle_process_output(proc, handle_diff_line_name_only, None, finalize_process, decode_streams=False)
604+
605+
return index
606+
572607
@staticmethod
573608
def _handle_diff_line(lines_bytes: bytes, repo: "Repo", index: DiffIndex) -> None:
574609
lines = lines_bytes.decode(defenc)

test/test_diff.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ def test_diff_initial_commit(self):
263263
self.assertIsNotNone(diff_index[0].new_file)
264264
self.assertEqual(diff_index[0].diff, fixture("diff_initial"))
265265

266+
def test_diff_initial_commit_name_only(self):
267+
initial_commit = self.rorepo.commit('33ebe7acec14b25c5f84f35a664803fcab2f7781')
268+
269+
# Without creating a patch...
270+
diff_index = initial_commit.diff(NULL_TREE, **{'name-only': True})
271+
self.assertEqual(diff_index[0].a_path, 'CHANGES')
272+
self.assertEqual(diff_index[0].b_path, 'CHANGES')
273+
274+
# ...and with creating a patch
275+
diff_index = initial_commit.diff(NULL_TREE, create_patch=True, **{'name-only': True})
276+
self.assertEqual(diff_index[0].a_path, 'CHANGES')
277+
self.assertEqual(diff_index[0].b_path, 'CHANGES')
278+
266279
def test_diff_unsafe_paths(self):
267280
output = StringProcessAdapter(fixture("diff_patch_unsafe_paths"))
268281
res = Diff._index_from_patch_format(None, output)

0 commit comments

Comments
 (0)