1
--- orig-5 2005-09-23 16:25:00.000000000 -0500
2
+++ mod-5 2005-09-23 16:25:21.000000000 -0500
4
raise MalformedPatchHeader("No mod line", "")
5
return (orig_name, mod_name)
7
-def parse_range(textrange):
8
- """Parse a patch range, handling the "1" special-case
10
- :param textrange: The text to parse
11
- :type textrange: str
12
- :return: the position and range, as a tuple
15
- tmp = textrange.split(',')
26
-def hunk_from_header(line):
27
- if not line.startswith("@@") or not line.endswith("@@\n") \
28
- or not len(line) > 4:
29
- raise MalformedHunkHeader("Does not start and end with @@.", line)
31
- (orig, mod) = line[3:-4].split(" ")
32
- except Exception, e:
33
- raise MalformedHunkHeader(str(e), line)
34
- if not orig.startswith('-') or not mod.startswith('+'):
35
- raise MalformedHunkHeader("Positions don't start with + or -.", line)
37
- (orig_pos, orig_range) = parse_range(orig[1:])
38
- (mod_pos, mod_range) = parse_range(mod[1:])
39
- except Exception, e:
40
- raise MalformedHunkHeader(str(e), line)
41
- if mod_range < 0 or orig_range < 0:
42
- raise MalformedHunkHeader("Hunk range is negative", line)
43
- return Hunk(orig_pos, orig_range, mod_pos, mod_range)
47
- def __init__(self, contents):
48
- self.contents = contents
50
- def get_str(self, leadchar):
51
- if self.contents == "\n" and leadchar == " " and False:
53
- if not self.contents.endswith('\n'):
54
- terminator = '\n' + NO_NL
57
- return leadchar + self.contents + terminator
60
-class ContextLine(HunkLine):
61
- def __init__(self, contents):
62
- HunkLine.__init__(self, contents)
65
- return self.get_str(" ")
68
-class InsertLine(HunkLine):
69
- def __init__(self, contents):
70
- HunkLine.__init__(self, contents)
73
- return self.get_str("+")
76
-class RemoveLine(HunkLine):
77
- def __init__(self, contents):
78
- HunkLine.__init__(self, contents)
81
- return self.get_str("-")
83
-NO_NL = '\\ No newline at end of file\n'
84
-__pychecker__="no-returnvalues"
86
-def parse_line(line):
87
- if line.startswith("\n"):
88
- return ContextLine(line)
89
- elif line.startswith(" "):
90
- return ContextLine(line[1:])
91
- elif line.startswith("+"):
92
- return InsertLine(line[1:])
93
- elif line.startswith("-"):
94
- return RemoveLine(line[1:])
98
- raise MalformedLine("Unknown line type", line)
103
- def __init__(self, orig_pos, orig_range, mod_pos, mod_range):
104
- self.orig_pos = orig_pos
105
- self.orig_range = orig_range
106
- self.mod_pos = mod_pos
107
- self.mod_range = mod_range
110
- def get_header(self):
111
- return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
113
- self.range_str(self.mod_pos,
116
- def range_str(self, pos, range):
117
- """Return a file range, special-casing for 1-line files.
119
- :param pos: The position in the file
121
- :range: The range in the file
123
- :return: a string in the format 1,4 except when range == pos == 1
128
- return "%i,%i" % (pos, range)
131
- lines = [self.get_header()]
132
- for line in self.lines:
133
- lines.append(str(line))
134
- return "".join(lines)
136
- def shift_to_mod(self, pos):
137
- if pos < self.orig_pos-1:
139
- elif pos > self.orig_pos+self.orig_range:
140
- return self.mod_range - self.orig_range
142
- return self.shift_to_mod_lines(pos)
144
- def shift_to_mod_lines(self, pos):
145
- assert (pos >= self.orig_pos-1 and pos <= self.orig_pos+self.orig_range)
146
- position = self.orig_pos-1
148
- for line in self.lines:
149
- if isinstance(line, InsertLine):
151
- elif isinstance(line, RemoveLine):
152
- if position == pos:
156
- elif isinstance(line, ContextLine):
162
def iter_hunks(iter_lines):
164
for line in iter_lines: