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

Skip to content

Commit 81b40de

Browse files
committed
git diff support --name-only option
1 parent 85cf7e8 commit 81b40de

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

git/diff.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,15 @@ def diff(self, other=Index, paths=None, create_patch=False, **kwargs):
146146
kwargs['as_process'] = True
147147
proc = diff_cmd(*self._process_diff_args(args), **kwargs)
148148

149-
diff_method = (Diff._index_from_patch_format
150-
if create_patch
151-
else Diff._index_from_raw_format)
149+
cmdline = getattr(proc, 'args', '') # PY3+ only
150+
151+
if '--name-only' in cmdline:
152+
diff_method = Diff._index_from_name_only_format
153+
elif create_patch:
154+
diff_method = Diff._index_from_patch_format
155+
else:
156+
diff_method = Diff._index_from_raw_format
157+
152158
index = diff_method(self.repo, proc)
153159

154160
proc.wait()
@@ -478,6 +484,34 @@ def _index_from_patch_format(cls, repo, proc):
478484

479485
return index
480486

487+
@classmethod
488+
def _index_from_name_only_format(cls, repo, proc):
489+
"""Create a new DiffIndex from the given text which must be in name only format
490+
:param repo: is the repository we are operating on - it is required
491+
:param stream: result of 'git diff' as a stream (supporting file protocol)
492+
:return: git.DiffIndex """
493+
494+
cls.is_first = True
495+
496+
index = DiffIndex()
497+
498+
def handle_diff_line_name_only(line):
499+
path = line.decode(defenc)
500+
if cls.is_first:
501+
cls.is_first = False
502+
return
503+
504+
path = path.strip()
505+
a_path = path.encode(defenc)
506+
b_path = path.encode(defenc)
507+
index.append(Diff(repo, a_path, b_path, None, None, None, None,
508+
False, False, None, None, '',
509+
None, None, None))
510+
511+
handle_process_output(proc, handle_diff_line_name_only, None, finalize_process, decode_streams=False)
512+
513+
return index
514+
481515
@classmethod
482516
def _index_from_raw_format(cls, repo, proc):
483517
"""Create a new DiffIndex from the given stream which must be in raw format.

git/test/test_diff.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@ def test_diff_initial_commit(self):
234234
self.assertIsNotNone(diff_index[0].new_file)
235235
self.assertEqual(diff_index[0].diff, fixture('diff_initial'))
236236

237+
def test_diff_initial_commit_name_only(self):
238+
initial_commit = self.rorepo.commit('33ebe7acec14b25c5f84f35a664803fcab2f7781')
239+
240+
# Without creating a patch...
241+
diff_index = initial_commit.diff(NULL_TREE, **{'name-only': True})
242+
self.assertEqual(diff_index[0].a_path, 'CHANGES')
243+
self.assertEqual(diff_index[0].b_path, 'CHANGES')
244+
245+
# ...and with creating a patch
246+
diff_index = initial_commit.diff(NULL_TREE, create_patch=True, **{'name-only': True})
247+
self.assertEqual(diff_index[0].a_path, 'CHANGES')
248+
self.assertEqual(diff_index[0].b_path, 'CHANGES')
249+
237250
def test_diff_unsafe_paths(self):
238251
output = StringProcessAdapter(fixture('diff_patch_unsafe_paths'))
239252
res = Diff._index_from_patch_format(None, output)

0 commit comments

Comments
 (0)