From f410d963cd346e03d30e60baa9647b7588a86765 Mon Sep 17 00:00:00 2001 From: James Walmsley Date: Mon, 8 Feb 2021 19:53:35 +0000 Subject: [PATCH] Fix bug when processing diff with multiple renames. --- tests/samples/git_rename.diff | 14 ++++++++++++++ tests/test_parser.py | 20 ++++++++++++++++++-- unidiff/patch.py | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/samples/git_rename.diff b/tests/samples/git_rename.diff index 6a28228..a51f4d3 100644 --- a/tests/samples/git_rename.diff +++ b/tests/samples/git_rename.diff @@ -11,3 +11,17 @@ index a071991..4dbab21 100644 Some content -Some content +Some modified content + +diff --git a/oldfile b/newfile +similarity index 85% +rename from oldfile +rename to newfile +index a071991..4dbab21 100644 +--- a/oldfile ++++ b/newfile +@@ -9,4 +9,4 @@ Some content + Some content + Some content + Some content +-Some content ++Some modified content diff --git a/tests/test_parser.py b/tests/test_parser.py index 841c1c1..6403519 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -411,13 +411,29 @@ def test_git_renaming(self): with codecs.open(file_path, 'r', encoding='utf-8') as diff_file: res = PatchSet(diff_file) - self.assertEqual(len(res), 1) + self.assertEqual(len(res), 4) patch = res[0] self.assertTrue(patch.is_rename) + self.assertEqual(patch.added, 0) + self.assertEqual(patch.removed, 0) + + patch = res[1] + self.assertFalse(patch.is_rename) self.assertEqual(patch.added, 1) self.assertEqual(patch.removed, 1) - self.assertEqual(len(res.modified_files), 1) + + patch = res[2] + self.assertTrue(patch.is_rename) + self.assertEqual(patch.added, 0) + self.assertEqual(patch.removed, 0) + + patch = res[3] + self.assertFalse(patch.is_rename) + self.assertEqual(patch.added, 1) + self.assertEqual(patch.removed, 1) + + self.assertEqual(len(res.modified_files), 4) self.assertEqual(len(res.added_files), 0) self.assertEqual(len(res.removed_files), 0) diff --git a/unidiff/patch.py b/unidiff/patch.py index 009b74b..7762969 100644 --- a/unidiff/patch.py +++ b/unidiff/patch.py @@ -462,6 +462,7 @@ def _parse(self, diff, encoding, metadata_only): patch_info, source_file, target_file, None, None, is_rename=True) self.append(current_file) + patch_info = None continue # check for source file header