Skip to content

Commit 4d6629d

Browse files
committed
Fix unified diff parse error
1 parent 73290df commit 4d6629d

File tree

6 files changed

+68
-2
lines changed

6 files changed

+68
-2
lines changed

tests/casefiles/abc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The Nameless is the origin of Heaven and Earth;

tests/casefiles/diff-unified2.diff

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
--- abc 2013-01-05 16:56:19.000000000 -0600
2+
+++ efg 2013-01-05 16:56:35.000000000 -0600
3+
@@ -1 +1,2 @@
4+
The Nameless is the origin of Heaven and Earth;
5+
+The named is the mother of all things.

tests/casefiles/efg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The Nameless is the origin of Heaven and Earth;
2+
The named is the mother of all things.

tests/test_apply.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def setUp(self):
2828
with open("tests/casefiles/tzu") as f:
2929
self.tzu = f.read().splitlines()
3030

31+
with open("tests/casefiles/abc") as f:
32+
self.abc = f.read().splitlines()
33+
34+
with open("tests/casefiles/efg") as f:
35+
self.efg = f.read().splitlines()
36+
3137
def test_truth(self):
3238
self.assertEqual(type(self.lao), list)
3339
self.assertEqual(type(self.tzu), list)
@@ -55,6 +61,13 @@ def test_diff_unified(self):
5561
self.assertEqual(_apply(self.lao, diff_text), self.tzu)
5662
self.assertEqual(_apply_r(self.tzu, diff_text), self.lao)
5763

64+
def test_diff_unified2(self):
65+
with open("tests/casefiles/diff-unified2.diff") as f:
66+
diff_text = f.read()
67+
68+
self.assertEqual(_apply(self.abc, diff_text), self.efg)
69+
self.assertEqual(_apply_r(self.efg, diff_text), self.abc)
70+
5871
def test_diff_unified_bad(self):
5972
with open("tests/casefiles/diff-unified-bad.diff") as f:
6073
diff_text = f.read()
@@ -129,6 +142,22 @@ def test_diff_unified_patchutil(self):
129142
with pytest.raises(exceptions.ApplyException):
130143
_apply([""] + self.lao, diff_text, use_patch=True)
131144

145+
def test_diff_unified2_patchutil(self):
146+
with open("tests/casefiles/diff-unified2.diff") as f:
147+
diff_text = f.read()
148+
149+
if not which("patch"):
150+
raise SkipTest()
151+
152+
self.assertEqual(_apply(self.abc, diff_text, use_patch=True),
153+
(self.efg, None))
154+
self.assertEqual(_apply(self.abc, diff_text, use_patch=True),
155+
(_apply(self.abc, diff_text), None))
156+
self.assertEqual(_apply_r(self.efg, diff_text, use_patch=True),
157+
(self.abc, None))
158+
self.assertEqual(_apply_r(self.efg, diff_text, use_patch=True),
159+
(_apply_r(self.efg, diff_text), None))
160+
132161
def test_diff_rcs(self):
133162
with open("tests/casefiles/diff-rcs.diff") as f:
134163
diff_text = f.read()

tests/test_patch.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,34 @@ def test_unified_diff(self):
857857
results_main = next(wtp.patch.parse_patch(text))
858858
self.assert_diffs_equal(results_main, expected_main)
859859

860+
def test_unified2_diff(self):
861+
with open(datapath("diff-unified2.diff")) as f:
862+
text = f.read()
863+
864+
# off with your head!
865+
text_diff = "\n".join(text.splitlines()[2:]) + "\n"
866+
867+
expected = [
868+
(None, 2, "The named is the mother of all things."),
869+
]
870+
871+
results = list(wtp.patch.parse_unified_diff(text_diff))
872+
self.assert_diffs_equal(results, expected)
873+
874+
expected_main = diffobj(
875+
header=headerobj(
876+
index_path=None,
877+
old_path="abc",
878+
old_version="2013-01-05 16:56:19.000000000 -0600",
879+
new_path="efg",
880+
new_version="2013-01-05 16:56:35.000000000 -0600",
881+
),
882+
changes=expected,
883+
text=text,
884+
)
885+
results_main = next(wtp.patch.parse_patch(text))
886+
self.assert_diffs_equal(results_main, expected_main)
887+
860888
def test_diff_unified_with_does_not_include_extra_lines(self):
861889
with open("tests/casefiles/diff-unified-blah.diff") as f:
862890
text = f.read()

whatthepatch/patch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,9 @@ def parse_unified_diff(text):
622622
elif kind == "+" and (i != new_len or i == 0):
623623
changes.append(Change(None, new + i, line, hunk_n))
624624
i += 1
625-
elif kind == " " and r != old_len and i != new_len:
626-
changes.append(Change(old + r, new + i, line, hunk_n))
625+
elif kind == " ":
626+
if r != old_len and i != new_len:
627+
changes.append(Change(old + r, new + i, line, hunk_n))
627628
r += 1
628629
i += 1
629630

0 commit comments

Comments
 (0)