Debian Codegraph @

test_patches_data/diff-5

raw
1 --- orig-5 2005-09-23 16:25:00.000000000 -0500
2 +++ mod-5 2005-09-23 16:25:21.000000000 -0500
3 @@ -60,161 +60,6 @@
4 raise MalformedPatchHeader("No mod line", "")
5 return (orig_name, mod_name)
6
7 -def parse_range(textrange):
8 - """Parse a patch range, handling the "1" special-case
9 -
10 - :param textrange: The text to parse
11 - :type textrange: str
12 - :return: the position and range, as a tuple
13 - :rtype: (int, int)
14 - """
15 - tmp = textrange.split(',')
16 - if len(tmp) == 1:
17 - pos = tmp[0]
18 - range = "1"
19 - else:
20 - (pos, range) = tmp
21 - pos = int(pos)
22 - range = int(range)
23 - return (pos, range)
24 -
25 -
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)
30 - try:
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)
36 - try:
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)
44 -
45 -
46 -class HunkLine:
47 - def __init__(self, contents):
48 - self.contents = contents
49 -
50 - def get_str(self, leadchar):
51 - if self.contents == "\n" and leadchar == " " and False:
52 - return "\n"
53 - if not self.contents.endswith('\n'):
54 - terminator = '\n' + NO_NL
55 - else:
56 - terminator = ''
57 - return leadchar + self.contents + terminator
58 -
59 -
60 -class ContextLine(HunkLine):
61 - def __init__(self, contents):
62 - HunkLine.__init__(self, contents)
63 -
64 - def __str__(self):
65 - return self.get_str(" ")
66 -
67 -
68 -class InsertLine(HunkLine):
69 - def __init__(self, contents):
70 - HunkLine.__init__(self, contents)
71 -
72 - def __str__(self):
73 - return self.get_str("+")
74 -
75 -
76 -class RemoveLine(HunkLine):
77 - def __init__(self, contents):
78 - HunkLine.__init__(self, contents)
79 -
80 - def __str__(self):
81 - return self.get_str("-")
82 -
83 -NO_NL = '\\ No newline at end of file\n'
84 -__pychecker__="no-returnvalues"
85 -
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:])
95 - elif line == NO_NL:
96 - return NO_NL
97 - else:
98 - raise MalformedLine("Unknown line type", line)
99 -__pychecker__=""
100 -
101 -
102 -class Hunk:
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
108 - self.lines = []
109 -
110 - def get_header(self):
111 - return "@@ -%s +%s @@\n" % (self.range_str(self.orig_pos,
112 - self.orig_range),
113 - self.range_str(self.mod_pos,
114 - self.mod_range))
115 -
116 - def range_str(self, pos, range):
117 - """Return a file range, special-casing for 1-line files.
118 -
119 - :param pos: The position in the file
120 - :type pos: int
121 - :range: The range in the file
122 - :type range: int
123 - :return: a string in the format 1,4 except when range == pos == 1
124 - """
125 - if range == 1:
126 - return "%i" % pos
127 - else:
128 - return "%i,%i" % (pos, range)
129 -
130 - def __str__(self):
131 - lines = [self.get_header()]
132 - for line in self.lines:
133 - lines.append(str(line))
134 - return "".join(lines)
135 -
136 - def shift_to_mod(self, pos):
137 - if pos < self.orig_pos-1:
138 - return 0
139 - elif pos > self.orig_pos+self.orig_range:
140 - return self.mod_range - self.orig_range
141 - else:
142 - return self.shift_to_mod_lines(pos)
143 -
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
147 - shift = 0
148 - for line in self.lines:
149 - if isinstance(line, InsertLine):
150 - shift += 1
151 - elif isinstance(line, RemoveLine):
152 - if position == pos:
153 - return None
154 - shift -= 1
155 - position += 1
156 - elif isinstance(line, ContextLine):
157 - position += 1
158 - if position > pos:
159 - break
160 - return shift
161 -
162 def iter_hunks(iter_lines):
163 hunk = None
164 for line in iter_lines: