1
# Copyright (C) 2004, 2005 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
class PatchSyntax(Exception):
19
def __init__(self, msg):
20
Exception.__init__(self, msg)
23
class MalformedPatchHeader(PatchSyntax):
24
def __init__(self, desc, line):
27
msg = "Malformed patch header. %s\n%r" % (self.desc, self.line)
28
PatchSyntax.__init__(self, msg)
30
class MalformedHunkHeader(PatchSyntax):
31
def __init__(self, desc, line):
34
msg = "Malformed hunk header. %s\n%r" % (self.desc, self.line)
35
PatchSyntax.__init__(self, msg)
37
class MalformedLine(PatchSyntax):
38
def __init__(self, desc, line):
41
msg = "Malformed line. %s\n%s" % (self.desc, self.line)
42
PatchSyntax.__init__(self, msg)
44
def get_patch_names(iter_lines):
46
line = iter_lines.next()
47
if not line.startswith("--- "):
48
raise MalformedPatchHeader("No orig name", line)
50
orig_name = line[4:].rstrip("\n")
52
raise MalformedPatchHeader("No orig line", "")
54
line = iter_lines.next()
55
if not line.startswith("+++ "):
56
raise PatchSyntax("No mod name")
58
mod_name = line[4:].rstrip("\n")
60
raise MalformedPatchHeader("No mod line", "")
61
return (orig_name, mod_name)
63
def parse_range(textrange):
64
"""Parse a patch range, handling the "1" special-case
66
:param textrange: The text to parse
68
:return: the position and range, as a tuple
71
tmp = textrange.split(',')
82
def hunk_from_header(line):
83
if not line.startswith("@@") or not line.endswith("@@\n") \
85
raise MalformedHunkHeader("Does not start and end with @@.", line)
87
(orig, mod) = line[3:-4].split(" ")
89
raise MalformedHunkHeader(str(e), line)
90
if not orig.startswith('-') or not mod.startswith('+'):
91
raise MalformedHunkHeader("Positions don't start with + or -.", line)
93
(orig_pos, orig_range) = parse_range(orig[1:])
94
(mod_pos, mod_range) = parse_range(mod[1:])
96
raise MalformedHunkHeader(str(e), line)
97
if mod_range < 0 or orig_range < 0:
98
raise MalformedHunkHeader("Hunk range is negative", line)
99
return Hunk(orig_pos, orig_range, mod_pos, mod_range)
103
def __init__(self, contents):
104
self.contents = contents
106
def get_str(self, leadchar):
107
if self.contents == "\n" and leadchar == " " and False:
109
if not self.contents.endswith('\n'):
110
terminator = '\n' + NO_NL
113
return leadchar + self.contents + terminator
116
assert self.contents.endswith('\n')
117
self.contents = self.contents[:-1]
119
class ContextLine(HunkLine):
120
def __init__(self, contents):
121
HunkLine.__init__(self, contents)
124
return self.get_str(" ")
127
class InsertLine(HunkLine):
128
def __init__(self, contents):
129
HunkLine.__init__(self, contents)
132
return self.get_str("+")
135
class RemoveLine(HunkLine):
136
def __init__(self, contents):
137
HunkLine.__init__(self, contents)
140
return self.get_str("-")
142
NO_NL = '\\ No newline at end of file\n'
143
__pychecker__="no-returnvalues"
145
def parse_line(line):
146
if line.startswith("\n"):
147
return ContextLine(line)
148
elif line.startswith(" "):
149
return ContextLine(line[1:])
150
elif line.startswith("+"):
151
return InsertLine(line[1:])
152
elif line.startswith("-"):
153
return RemoveLine(line[1:])
157
raise MalformedLine("Unknown line type", line)
162
def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
163
self.orig_pos = orig_pos
164
self.orig_range = orig_range
165
self.mod_pos = mod_pos
166
self.mod_range = mod_range
169
def get_header(self):
170
return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
172
self.range_str(self.mod_pos,
175
def range_str(self, pos, range):
176
"""Return a file range, special-casing for 1-line files.
178
:param pos: The position in the file
180
:range: The range in the file
182
:return: a string in the format 1,4 except when range == pos == 1
187
return "%i,%i" % (pos, range)
190
lines = [self.get_header()]
191
for line in self.lines:
192
lines.append(str(line))
193
return "".join(lines)
195
def shift_to_mod(self, pos):
196
if pos < self.orig_pos-1:
198
elif pos > self.orig_pos+self.orig_range:
199
return self.mod_range - self.orig_range
201
return self.shift_to_mod_lines(pos)
203
def shift_to_mod_lines(self, pos):
204
assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
205
position = self.orig_pos-1
207
for line in self.lines:
208
if isinstance(line, InsertLine):
210
elif isinstance(line, RemoveLine):
215
elif isinstance(line, ContextLine):
221
def iter_hunks(iter_lines):
223
for line in iter_lines:
225
hunk.lines[-1].no_nl()
236
hunk = hunk_from_header(line)
239
while orig_size < hunk.orig_range or mod_size < hunk.mod_range:
240
hunk_line = parse_line(iter_lines.next())
241
if hunk_line is NO_NL:
242
hunk.lines[-1].no_nl()
244
hunk.lines.append(hunk_line)
245
if isinstance(hunk_line, (RemoveLine, ContextLine)):
247
if isinstance(hunk_line, (InsertLine, ContextLine)):
253
def __init__(self, oldname, newname):
254
self.oldname = oldname
255
self.newname = newname
259
ret = "--- %s\n+++ %s\n" % (self.oldname, self.newname)
260
ret += "".join([str(h) for h in self.hunks])
264
"""Return a string of patch statistics"""
267
for hunk in self.hunks:
268
for line in hunk.lines:
269
if isinstance(line, InsertLine):
271
elif isinstance(line, RemoveLine):
273
return "%i inserts, %i removes in %i hunks" % \
274
(inserts, removes, len(self.hunks))
276
def pos_in_mod(self, position):
278
for hunk in self.hunks:
279
shift = hunk.shift_to_mod(position)
285
def iter_inserted(self):
286
"""Iteraties through inserted lines
288
:return: Pair of line number, line
289
:rtype: iterator of (int, InsertLine)
291
for hunk in self.hunks:
292
pos = hunk.mod_pos - 1;
293
for line in hunk.lines:
294
if isinstance(line, InsertLine):
297
if isinstance(line, ContextLine):
300
def parse_patch(iter_lines):
301
(orig_name, mod_name) = get_patch_names(iter_lines)
302
patch = Patch(orig_name, mod_name)
303
for hunk in iter_hunks(iter_lines):
304
patch.hunks.append(hunk)
308
def iter_file_patch(iter_lines):
310
for line in iter_lines:
311
if line.startswith('*** '):
313
elif line.startswith('--- '):
314
if len(saved_lines) > 0:
317
saved_lines.append(line)
318
if len(saved_lines) > 0:
322
def parse_patches(iter_lines):
323
return [parse_patch(f.__iter__()) for f in iter_file_patch(iter_lines)]
326
def difference_index(atext, btext):
327
"""Find the indext of the first character that differs betweeen two texts
329
:param atext: The first text
331
:param btext: The second text
333
:return: The index, or None if there are no differences within the range
334
:rtype: int or NoneType
337
if len(btext) < length:
339
for i in range(length):
340
if atext[i] != btext[i]:
344
class PatchConflict(Exception):
345
def __init__(self, line_no, orig_line, patch_line):
346
orig = orig_line.rstrip('\n')
347
patch = str(patch_line).rstrip('\n')
348
msg = 'Text contents mismatch at line %d. Original has "%s",'\
349
' but patch says it should be "%s"' % (line_no, orig, patch)
350
Exception.__init__(self, msg)
353
def iter_patched(orig_lines, patch_lines):
354
"""Iterate through a series of lines with a patch applied.
355
This handles a single file, and does exact, not fuzzy patching.
357
if orig_lines is not None:
358
orig_lines = orig_lines.__iter__()
360
patch_lines = patch_lines.__iter__()
361
get_patch_names(patch_lines)
363
for hunk in iter_hunks(patch_lines):
364
while line_no < hunk.orig_pos:
365
orig_line = orig_lines.next()
368
for hunk_line in hunk.lines:
369
seen_patch.append(str(hunk_line))
370
if isinstance(hunk_line, InsertLine):
371
yield hunk_line.contents
372
elif isinstance(hunk_line, (ContextLine, RemoveLine)):
373
orig_line = orig_lines.next()
374
if orig_line != hunk_line.contents:
375
raise PatchConflict(line_no, orig_line, "".join(seen_patch))
376
if isinstance(hunk_line, ContextLine):
379
assert isinstance(hunk_line, RemoveLine)
384
class PatchesTester(unittest.TestCase):
385
def datafile(self, filename):
386
data_path = os.path.join(os.path.dirname(__file__), "testdata",
388
return file(data_path, "rb")
390
def testValidPatchHeader(self):
391
"""Parse a valid patch header"""
392
lines = "--- orig/commands.py\n+++ mod/dommands.py\n".split('\n')
393
(orig, mod) = get_patch_names(lines.__iter__())
394
assert(orig == "orig/commands.py")
395
assert(mod == "mod/dommands.py")
397
def testInvalidPatchHeader(self):
398
"""Parse an invalid patch header"""
399
lines = "-- orig/commands.py\n+++ mod/dommands.py".split('\n')
400
self.assertRaises(MalformedPatchHeader, get_patch_names,
403
def testValidHunkHeader(self):
404
"""Parse a valid hunk header"""
405
header = "@@ -34,11 +50,6 @@\n"
406
hunk = hunk_from_header(header);
407
assert (hunk.orig_pos == 34)
408
assert (hunk.orig_range == 11)
409
assert (hunk.mod_pos == 50)
410
assert (hunk.mod_range == 6)
411
assert (str(hunk) == header)
413
def testValidHunkHeader2(self):
414
"""Parse a tricky, valid hunk header"""
415
header = "@@ -1 +0,0 @@\n"
416
hunk = hunk_from_header(header);
417
assert (hunk.orig_pos == 1)
418
assert (hunk.orig_range == 1)
419
assert (hunk.mod_pos == 0)
420
assert (hunk.mod_range == 0)
421
assert (str(hunk) == header)
423
def makeMalformed(self, header):
424
self.assertRaises(MalformedHunkHeader, hunk_from_header, header)
426
def testInvalidHeader(self):
427
"""Parse an invalid hunk header"""
428
self.makeMalformed(" -34,11 +50,6 \n")
429
self.makeMalformed("@@ +50,6 -34,11 @@\n")
430
self.makeMalformed("@@ -34,11 +50,6 @@")
431
self.makeMalformed("@@ -34.5,11 +50,6 @@\n")
432
self.makeMalformed("@@-34,11 +50,6@@\n")
433
self.makeMalformed("@@ 34,11 50,6 @@\n")
434
self.makeMalformed("@@ -34,11 @@\n")
435
self.makeMalformed("@@ -34,11 +50,6.5 @@\n")
436
self.makeMalformed("@@ -34,11 +50,-6 @@\n")
438
def lineThing(self,text, type):
439
line = parse_line(text)
440
assert(isinstance(line, type))
441
assert(str(line)==text)
443
def makeMalformedLine(self, text):
444
self.assertRaises(MalformedLine, parse_line, text)
446
def testValidLine(self):
447
"""Parse a valid hunk line"""
448
self.lineThing(" hello\n", ContextLine)
449
self.lineThing("+hello\n", InsertLine)
450
self.lineThing("-hello\n", RemoveLine)
452
def testMalformedLine(self):
453
"""Parse invalid valid hunk lines"""
454
self.makeMalformedLine("hello\n")
456
def compare_parsed(self, patchtext):
457
lines = patchtext.splitlines(True)
458
patch = parse_patch(lines.__iter__())
460
i = difference_index(patchtext, pstr)
462
print "%i: \"%s\" != \"%s\"" % (i, patchtext[i], pstr[i])
463
self.assertEqual (patchtext, str(patch))
466
"""Test parsing a whole patch"""
467
patchtext = """--- orig/commands.py
469
@@ -1337,7 +1337,8 @@
471
def set_title(self, command=None):
473
- version = self.tree.tree_version.nonarch
474
+ version = pylon.alias_or_version(self.tree.tree_version, self.tree,
477
version = "[no version]"
479
@@ -1983,7 +1984,11 @@
481
if len(new_merges) > 0:
482
if cmdutil.prompt("Log for merge"):
483
- mergestuff = cmdutil.log_for_merge(tree, comp_version)
484
+ if cmdutil.prompt("changelog for merge"):
485
+ mergestuff = "Patches applied:\\n"
486
+ mergestuff += pylon.changelog_for_merge(new_merges)
488
+ mergestuff = cmdutil.log_for_merge(tree, comp_version)
489
log.description += mergestuff
493
self.compare_parsed(patchtext)
496
"""Handle patches missing half the position, range tuple"""
498
"""--- orig/__init__.py
501
__docformat__ = "restructuredtext en"
502
+__doc__ = An alternate Arch commandline interface
504
self.compare_parsed(patchtext)
508
def testLineLookup(self):
510
"""Make sure we can accurately look up mod line from orig"""
511
patch = parse_patch(self.datafile("diff"))
512
orig = list(self.datafile("orig"))
513
mod = list(self.datafile("mod"))
515
for i in range(len(orig)):
516
mod_pos = patch.pos_in_mod(i)
518
removals.append(orig[i])
520
assert(mod[mod_pos]==orig[i])
521
rem_iter = removals.__iter__()
522
for hunk in patch.hunks:
523
for line in hunk.lines:
524
if isinstance(line, RemoveLine):
525
next = rem_iter.next()
526
if line.contents != next:
527
sys.stdout.write(" orig:%spatch:%s" % (next,
529
assert(line.contents == next)
530
self.assertRaises(StopIteration, rem_iter.next)
532
def testFirstLineRenumber(self):
533
"""Make sure we handle lines at the beginning of the hunk"""
534
patch = parse_patch(self.datafile("insert_top.patch"))
535
assert (patch.pos_in_mod(0)==1)
538
patchesTestSuite = unittest.makeSuite(PatchesTester,'test')
539
runner = unittest.TextTestRunner(verbosity=0)
540
return runner.run(patchesTestSuite)
543
if __name__ == "__main__":
545
# arch-tag: d1541a25-eac5-4de9-a476-08a7cecd5683