Skip to content

Commit fecae94

Browse files
author
techtonik
committed
Support hunk descriptions. Fixes issue techtonik#22.
When `diff` is called with `-p` argument (--show-c-function), it adds a comment after last @@ in chunk header string. * Found by Philippe Ombredanne
1 parent 13ac9da commit fecae94

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

patch.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""
1515

1616
__author__ = "anatoly techtonik <techtonik@gmail.com>"
17-
__version__ = "1.12.11"
17+
__version__ = "1.12.12dev"
1818

1919
import copy
2020
import logging
@@ -163,6 +163,7 @@ def __init__(self):
163163
self.starttgt=None
164164
self.linestgt=None
165165
self.invalid=False
166+
self.desc=''
166167
self.text=[]
167168

168169
# def apply(self, estream):
@@ -273,7 +274,7 @@ def lineno(self):
273274
hunkparsed = False # state after successfully parsed hunk
274275

275276
# regexp to match start of hunk, used groups - 1,3,4,6
276-
re_hunk_start = re.compile("^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))?")
277+
re_hunk_start = re.compile("^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@")
277278

278279
self.errors = 0
279280
# temp buffers for header and filenames info
@@ -478,7 +479,7 @@ def lineno(self):
478479
continue
479480

480481
if hunkhead:
481-
match = re.match("^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))?", line)
482+
match = re.match("^@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@(.*)", line)
482483
if not match:
483484
if not p.hunks:
484485
warning("skipping invalid patch with no hunks for file %s" % p.source)
@@ -502,6 +503,7 @@ def lineno(self):
502503
hunk.linestgt = 1
503504
if match.group(6): hunk.linestgt = int(match.group(6))
504505
hunk.invalid = False
506+
hunk.desc = match.group(7)[1:].rstrip()
505507
hunk.text = []
506508

507509
hunkactual["linessrc"] = hunkactual["linestgt"] = 0

tests/run_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ def test_header_for_second_file_in_svn_diff(self):
244244
self.assertEqual(pto.items[1].header[0], 'Index: updatedlg.h\r\n')
245245
self.assert_(pto.items[1].header[1].startswith('====='))
246246

247+
def test_hunk_desc(self):
248+
pto = patch.fromfile(testfile('git-changed-file.diff'))
249+
self.assertEqual(pto.items[0].hunks[0].desc, 'class JSONPluginMgr(object):')
250+
247251
def test_autofixed_absolute_path(self):
248252
pto = patch.fromfile(join(tests_dir, "data/autofix/absolute-path.diff"))
249253
self.assertEqual(pto.errors, 0)

0 commit comments

Comments
 (0)