Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,36 @@ def test_index_line_mode(self):
# the index line is preserved so the diff still round-trips
self.assertEqual(str(res), diff)

def test_parse_format_patch_hunkless_rename(self):
# regression test for issues #73 / #74: git format-patch output where a
# hunkless file (a pure rename) is followed by the "-- " email
# signature and a trailing blank line must not raise.
lines = [
'From 82dd164 Mon Sep 17 00:00:00 2001\n',
'From: Someone <someone@example.com>\n',
'Subject: [PATCH] Rename JSONHelper to JSONHelper.java\n',
'\n',
'---\n',
' JSONHelper => JSONHelper.java | 0\n',
' 1 file changed, 0 insertions(+), 0 deletions(-)\n',
' rename JSONHelper => JSONHelper.java (100%)\n',
'\n',
'diff --git a/JSONHelper b/JSONHelper.java\n',
'similarity index 100%\n',
'rename from JSONHelper\n',
'rename to JSONHelper.java\n',
'-- \n',
'2.17.1\n',
'\n',
]

res = PatchSet(lines)

self.assertEqual(len(res), 1)
self.assertTrue(res[0].is_rename)
self.assertEqual(res[0].path, 'JSONHelper.java')
self.assertEqual(len(res[0]), 0)

def test_diff_lines_linenos(self):
with open(self.sample_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')
Expand Down
7 changes: 5 additions & 2 deletions unidiff/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,8 +575,11 @@ def _parse(self, diff: Iterable, encoding: Optional[str],
current_file._add_no_newline_marker_to_last_hunk()
continue

# sometimes hunks can be followed by empty lines
if line == '\n' and current_file is not None:
# sometimes hunks can be followed by empty lines; only attach the
# empty line to the current file when it actually has hunks,
# otherwise (e.g. a hunkless rename in git format-patch output) it
# is just a separator and belongs to the surrounding patch info
if line == '\n' and current_file:
current_file._append_trailing_empty_line()
continue

Expand Down
Loading