Skip to content

Commit 7b3d084

Browse files
committed
Issue #1: Fixes parsing old-style CVS diffs
1 parent 068ed57 commit 7b3d084

File tree

3 files changed

+117
-38
lines changed

3 files changed

+117
-38
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Index: mozilla/js/rhino/CHANGELOG
2+
diff -c mozilla/js/rhino/CHANGELOG mozilla/js/rhino/CHANGELOG
3+
*** mozilla/js/rhino/CHANGELOG Thu Jan 25 10:59:02 2007
4+
--- mozilla/js/rhino/CHANGELOG Thu Jan 25 10:59:02 2007
5+
***************
6+
*** 1,4 ****
7+
! This file version: $Id: CHANGELOG,v 1.1.1.1 2007/01/25 15:59:02 inonit Exp $
8+
9+
Changes since Rhino 1.6R5
10+
=========================
11+
--- 1,4 ----
12+
! This file version: $Id: CHANGELOG,v 1.1 2007/01/25 15:59:02 inonit Exp $
13+
14+
Changes since Rhino 1.6R5
15+
=========================

tests/test_patch.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,39 @@ def test_old_style_cvs(self):
10471047
results = wtp.patch.parse_cvs_header(text)
10481048
self.assertEqual(results, expected[0].header)
10491049

1050+
results = wtp.patch.parse_header(text)
1051+
self.assertEqual(results, expected[0].header)
1052+
1053+
results = list(wtp.patch.parse_patch(text))
1054+
self.assertEqual(results, expected)
1055+
1056+
def test_mozilla_252983_versionless(self):
1057+
with open('tests/casefiles/mozilla-252983-versionless.diff') as f:
1058+
text = f.read()
1059+
1060+
expected = [
1061+
wtp.patch.diffobj(
1062+
header=wtp.patch.header(
1063+
index_path='mozilla/js/rhino/CHANGELOG',
1064+
old_path='mozilla/js/rhino/CHANGELOG',
1065+
old_version=None,
1066+
new_path='mozilla/js/rhino/CHANGELOG',
1067+
new_version=None,
1068+
),
1069+
changes=[
1070+
(1, None, 'This file version: $Id: CHANGELOG,v 1.1.1.1 2007/01/25 15:59:02 inonit Exp $'),
1071+
(None, 1, 'This file version: $Id: CHANGELOG,v 1.1 2007/01/25 15:59:02 inonit Exp $'),
1072+
(2, 2, ''),
1073+
(3, 3, 'Changes since Rhino 1.6R5'),
1074+
(4, 4, '========================='),
1075+
],
1076+
text=text
1077+
),
1078+
]
1079+
1080+
results = wtp.patch.parse_header(text)
1081+
self.assertEqual(results, expected[0].header)
1082+
10501083
results = list(wtp.patch.parse_patch(text))
10511084
self.assertEqual(results, expected)
10521085

whatthepatch/patch.py

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
cvs_header_rcs = re.compile('^RCS file: (.+)(?:,\w{1}$|$)')
5959
cvs_header_timestamp = re.compile('(.+)\t([\d.]+)')
6060
cvs_header_timestamp_colon = re.compile(':([\d.]+)\t(.+)')
61-
old_cvs_diffcmd_header = re.compile('^diff.* (.+):?(.*) (.+):?(.*)$')
61+
old_cvs_diffcmd_header = re.compile('^diff.* (.+):(.*) (.+):(.*)$')
6262

6363

6464
def parse_patch(text):
@@ -106,6 +106,7 @@ def parse_scm_header(text):
106106

107107
check = [
108108
(git_header_index, parse_git_header),
109+
(old_cvs_diffcmd_header, parse_cvs_header),
109110
(cvs_header_rcs, parse_cvs_header),
110111
(svn_header_index, parse_svn_header),
111112
]
@@ -298,49 +299,79 @@ def parse_cvs_header(text):
298299
lines = text
299300

300301
headers = findall_regex(lines, cvs_header_rcs)
301-
headers2 = findall_regex(lines, old_cvs_diffcmd_header)
302-
if len(headers) == 0 and len(headers2) == 0:
303-
return None
302+
headers_old = findall_regex(lines, old_cvs_diffcmd_header)
304303

305-
while len(lines) > 0:
306-
i = cvs_header_index.match(lines[0])
307-
d = old_cvs_diffcmd_header.match(lines[0])
308-
del lines[0]
309-
if i:
310-
diff_header = parse_diff_header(lines)
311-
if diff_header:
312-
over = diff_header.old_version
313-
if over:
314-
oend = cvs_header_timestamp.match(over)
315-
oend_c = cvs_header_timestamp_colon.match(over)
316-
if oend:
317-
over = oend.group(2)
318-
elif oend_c:
319-
over = oend_c.group(1)
304+
if headers:
305+
# parse rcs style headers
306+
while len(lines) > 0:
307+
i = cvs_header_index.match(lines[0])
308+
del lines[0]
309+
if i:
310+
diff_header = parse_diff_header(lines)
311+
if diff_header:
312+
over = diff_header.old_version
313+
if over:
314+
oend = cvs_header_timestamp.match(over)
315+
oend_c = cvs_header_timestamp_colon.match(over)
316+
if oend:
317+
over = oend.group(2)
318+
elif oend_c:
319+
over = oend_c.group(1)
320+
321+
nver = diff_header.new_version
322+
if nver:
323+
nend = cvs_header_timestamp.match(nver)
324+
nend_c = cvs_header_timestamp_colon.match(nver)
325+
if nend:
326+
nver = nend.group(2)
327+
elif nend_c:
328+
nver = nend_c.group(1)
320329

321-
nver = diff_header.new_version
322-
if nver:
323-
nend = cvs_header_timestamp.match(nver)
324-
nend_c = cvs_header_timestamp_colon.match(nver)
325-
if nend:
326-
nver = nend.group(2)
327-
elif nend_c:
328-
nver = nend_c.group(1)
330+
return header(
331+
index_path = i.group(1),
332+
old_path = diff_header.old_path,
333+
old_version = over,
334+
new_path = diff_header.new_path,
335+
new_version = nver,
336+
)
337+
return header(
338+
index_path = i.group(1),
339+
old_path = i.group(1),
340+
old_version = None,
341+
new_path = i.group(1),
342+
new_version = None,
343+
)
344+
elif headers_old:
345+
# parse old style headers
346+
while len(lines) > 0:
347+
i = cvs_header_index.match(lines[0])
348+
del lines[0]
349+
if i:
350+
d = old_cvs_diffcmd_header.match(lines[0])
351+
if d:
352+
_ = parse_diff_header(lines) # will get rid of the useless stuff for us
353+
over = d.group(2)
354+
if not over:
355+
over = None
356+
357+
nver = d.group(4)
358+
if not nver:
359+
nver = None
360+
return header(
361+
index_path = i.group(1),
362+
old_path = d.group(1),
363+
old_version = over,
364+
new_path = d.group(3),
365+
new_version = nver,
366+
)
329367

330368
return header(
331369
index_path = i.group(1),
332-
old_path = diff_header.old_path,
333-
old_version = over,
334-
new_path = diff_header.new_path,
335-
new_version = nver,
370+
old_path = i.group(1),
371+
old_version = None,
372+
new_path = i.group(1),
373+
new_version = None,
336374
)
337-
return header(
338-
index_path = i.group(1),
339-
old_path = i.group(1),
340-
old_version = None,
341-
new_path = i.group(1),
342-
new_version = None,
343-
)
344375

345376
return None
346377

0 commit comments

Comments
 (0)