Skip to content

Commit

Permalink
Merge pull request #17 from sk-/master
Browse files Browse the repository at this point in the history
Add diff_line_no to Line.
  • Loading branch information
matiasb committed Jun 28, 2015
2 parents 5a08521 + b10f7af commit a7e46e0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Python
*.py[cod]
__pycache__
build
dist
unidiff.egg-info

# Vim
*.swp
55 changes: 55 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,61 @@ def test_parse_malformed_diff(self):
with open(self.sample_bad_file) as diff_file:
self.assertRaises(UnidiffParseError, PatchSet, diff_file)

def test_diff_lines_linenos(self):
with open(self.sample_file, 'rb') as diff_file:
res = PatchSet(diff_file, encoding='utf-8')

target_line_nos = []
source_line_nos = []
diff_line_nos = []
for diff_file in res:
for hunk in diff_file:
for line in hunk:
target_line_nos.append(line.target_line_no)
source_line_nos.append(line.source_line_no)
diff_line_nos.append(line.diff_line_no)

expected_target_line_nos = [
# File: 1, Hunk: 1
1, 2, 3, 4, 5, 6, 7, 8, 9,
# File: 1, Hunk: 2
11, 12, 13, None, None, None, None, None, None, None, 14, 15, 16, None, 17, 18, 19, 20,
# File: 1, Hunk: 3
22, 23, 24, 25, 26, 27, 28,
# File: 2, Hunk 1
1, 2, 3, 4, 5, 6, 7, 8, 9,
# File: 3, Hunk 1
None, None, None, None, None, None, None, None, None,
]
expected_source_line_nos = [
# File: 1, Hunk: 1
None, None, None, None, None, None, 1, 2, 3,
# File: 1, Hunk: 2
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, None, 15, 16, 17, None, 18, 19, 20,
# File: 1, Hunk: 3
22, 23, 24, None, None, None, None,
# File: 2, Hunk 1
None, None, None, None, None, None, None, None, None,
# File: 3, Hunk 1
1, 2, 3, 4, 5, 6, 7, 8, 9,
]
expected_diff_line_nos = [
# File: 1, Hunk: 1
4, 5, 6, 7, 8, 9, 10, 11, 12,
# File: 1, Hunk: 2
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
# File: 1, Hunk: 3
33, 34, 35, 36, 37, 38, 39,
# File: 2, Hunk 1
43, 44, 45, 46, 47, 48, 49, 50, 51,
# File: 3, Hunk 1
55, 56, 57, 58, 59, 60, 61, 62, 63,
]

self.assertEqual(target_line_nos, expected_target_line_nos)
self.assertEqual(source_line_nos, expected_source_line_nos)
self.assertEqual(diff_line_nos, expected_diff_line_nos)


class TestVCSSamples(unittest.TestCase):
"""Tests for real examples from VCS."""
Expand Down
9 changes: 6 additions & 3 deletions unidiff/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ class Line(object):
"""A diff line."""

def __init__(self, value, line_type,
source_line_no=None, target_line_no=None):
source_line_no=None, target_line_no=None, diff_line_no=None):
super(Line, self).__init__()
self.source_line_no = source_line_no
self.target_line_no = target_line_no
self.diff_line_no = diff_line_no
self.line_type = line_type
self.value = value

Expand Down Expand Up @@ -184,7 +185,7 @@ def _parse_hunk(self, header, diff, encoding):
expected_source_end = source_line_no + hunk.source_length
expected_target_end = target_line_no + hunk.target_length

for line in diff:
for diff_line_no, line in diff:
if encoding is not None:
line = line.decode(encoding)
valid_line = RE_HUNK_BODY_LINE.match(line)
Expand All @@ -211,6 +212,7 @@ def _parse_hunk(self, header, diff, encoding):
original_line = None

if original_line:
original_line.diff_line_no = diff_line_no
hunk.append(original_line)

# if hunk source/target lengths are ok, hunk is complete
Expand Down Expand Up @@ -284,7 +286,8 @@ def __str__(self):
def _parse(self, diff, encoding):
current_file = None

for line in diff:
diff = enumerate(diff, 1)
for unused_diff_line_no, line in diff:
if encoding is not None:
line = line.decode(encoding)
# check for source file header
Expand Down

0 comments on commit a7e46e0

Please sign in to comment.