bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.1.1
by Martin Pool
Check in old existing knit code. |
1 |
#! /usr/bin/python
|
2 |
||
3 |
# Copyright (C) 2005 Canonical Ltd
|
|
4 |
||
|
0.1.33
by Martin Pool
add gpl text |
5 |
# This program is free software; you can redistribute it and/or modify
|
6 |
# it under the terms of the GNU General Public License as published by
|
|
7 |
# the Free Software Foundation; either version 2 of the License, or
|
|
8 |
# (at your option) any later version.
|
|
9 |
||
10 |
# This program is distributed in the hope that it will be useful,
|
|
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
# GNU General Public License for more details.
|
|
14 |
||
15 |
# You should have received a copy of the GNU General Public License
|
|
16 |
# along with this program; if not, write to the Free Software
|
|
17 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
0.1.1
by Martin Pool
Check in old existing knit code. |
18 |
|
19 |
# Author: Martin Pool <mbp@canonical.com>
|
|
20 |
||
21 |
||
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
22 |
"""Weave - storage of related text file versions"""
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
23 |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
24 |
# TODO: Perhaps have copy and comparison methods of Weave instances?
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
25 |
|
|
0.1.58
by Martin Pool
doc |
26 |
# XXX: If we do weaves this way, will a merge still behave the same
|
27 |
# way if it's done in a different order? That's a pretty desirable
|
|
28 |
# property.
|
|
29 |
||
30 |
||
|
0.1.34
by Martin Pool
remove dead code |
31 |
|
|
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
32 |
class VerInfo(object): |
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
33 |
"""Information about a version in a Weave.""" |
|
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
34 |
included = frozenset() |
35 |
def __init__(self, included=None): |
|
36 |
if included: |
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
37 |
self.included = frozenset(included) |
|
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
38 |
|
|
0.1.18
by Martin Pool
Better Knit.dump method |
39 |
def __repr__(self): |
40 |
s = self.__class__.__name__ + '(' |
|
41 |
if self.included: |
|
42 |
s += 'included=%r' % (list(self.included)) |
|
43 |
s += ')' |
|
44 |
return s |
|
45 |
||
|
0.1.17
by Martin Pool
Use objects rather than tuples for tracking VerInfo for |
46 |
|
|
0.1.47
by Martin Pool
New WeaveError and WeaveFormatError rather than assertions. |
47 |
class WeaveError(Exception): |
48 |
"""Exception in processing weave""" |
|
49 |
||
50 |
||
51 |
class WeaveFormatError(WeaveError): |
|
52 |
"""Weave invariant violated""" |
|
53 |
||
54 |
||
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
55 |
class Weave(object): |
56 |
"""weave - versioned text file storage. |
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
57 |
|
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
58 |
A Weave manages versions of line-based text files, keeping track of the
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
59 |
originating version for each line.
|
60 |
||
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
61 |
Texts can be identified in either of two ways:
|
62 |
||
63 |
* a nonnegative index number.
|
|
64 |
||
65 |
* a version-id string.
|
|
66 |
||
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
67 |
Typically the index number will be valid only inside this weave and
|
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
68 |
the version-id is used to reference it in the larger world.
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
69 |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
70 |
The weave is represented as a list mixing edit instructions and
|
71 |
literal text. Each entry in _l can be either a string (or
|
|
72 |
unicode), or a tuple. If a string, it means that the given line
|
|
73 |
should be output in the currently active revisions.
|
|
74 |
||
75 |
If a tuple, it gives a processing instruction saying in which
|
|
76 |
revisions the enclosed lines are active. The tuple has the form
|
|
77 |
(instruction, version).
|
|
78 |
||
79 |
The instruction can be '{' or '}' for an insertion block, and '['
|
|
80 |
and ']' for a deletion block respectively. The version is the
|
|
|
0.1.45
by Martin Pool
doc |
81 |
integer version index. There is no replace operator, only deletes
|
82 |
and inserts.
|
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
83 |
|
|
0.1.41
by Martin Pool
Doc |
84 |
Constraints/notes:
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
85 |
|
86 |
* A later version can delete lines that were introduced by any
|
|
87 |
number of ancestor versions; this implies that deletion
|
|
88 |
instructions can span insertion blocks without regard to the
|
|
89 |
insertion block's nesting.
|
|
90 |
||
|
0.1.41
by Martin Pool
Doc |
91 |
* Similarly, deletions need not be properly nested with regard to
|
92 |
each other, because they might have been generated by
|
|
93 |
independent revisions.
|
|
94 |
||
|
0.1.45
by Martin Pool
doc |
95 |
* Insertions are always made by inserting a new bracketed block
|
96 |
into a single point in the previous weave. This implies they
|
|
97 |
can nest but not overlap, and the nesting must always have later
|
|
98 |
insertions on the inside.
|
|
99 |
||
|
0.1.41
by Martin Pool
Doc |
100 |
* It doesn't seem very useful to have an active insertion
|
101 |
inside an inactive insertion, but it might happen.
|
|
|
0.1.45
by Martin Pool
doc |
102 |
|
|
0.1.41
by Martin Pool
Doc |
103 |
* Therefore, all instructions are always"considered"; that
|
104 |
is passed onto and off the stack. An outer inactive block
|
|
105 |
doesn't disable an inner block.
|
|
106 |
||
107 |
* Lines are enabled if the most recent enclosing insertion is
|
|
108 |
active and none of the enclosing deletions are active.
|
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
109 |
|
|
0.1.49
by Martin Pool
Add another constraint: revisions should not delete text that they |
110 |
* There is no point having a deletion directly inside its own
|
111 |
insertion; you might as well just not write it. And there
|
|
112 |
should be no way to get an earlier version deleting a later
|
|
113 |
version.
|
|
114 |
||
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
115 |
_l
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
116 |
Text of the weave.
|
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
117 |
|
118 |
_v
|
|
|
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
119 |
List of versions, indexed by index number.
|
120 |
||
121 |
For each version we store the tuple (included_versions), which
|
|
122 |
lists the previous versions also considered active.
|
|
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
123 |
"""
|
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
124 |
def __init__(self): |
125 |
self._l = [] |
|
126 |
self._v = [] |
|
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
127 |
|
|
0.1.60
by Martin Pool
Weave eq and ne methods |
128 |
|
129 |
||
130 |
def __eq__(self, other): |
|
131 |
if not isinstance(other, Weave): |
|
132 |
return False |
|
133 |
return self._v == other._v \ |
|
134 |
and self._l == other._l |
|
135 |
||
136 |
||
137 |
def __ne__(self, other): |
|
138 |
return not self.__eq__(other) |
|
139 |
||
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
140 |
|
|
0.1.26
by Martin Pool
Refactor parameters to add command |
141 |
def add(self, parents, text): |
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
142 |
"""Add a single text on top of the weave. |
|
0.1.36
by Martin Pool
doc |
143 |
|
|
0.1.26
by Martin Pool
Refactor parameters to add command |
144 |
Returns the index number of the newly added version.
|
145 |
||
146 |
parents
|
|
147 |
List or set of parent version numbers.
|
|
148 |
||
149 |
text
|
|
150 |
Sequence of lines to be added in the new version."""
|
|
|
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
151 |
self._check_versions(parents) |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
152 |
self._check_lines(text) |
|
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
153 |
|
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
154 |
idx = len(self._v) |
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
155 |
|
|
0.1.26
by Martin Pool
Refactor parameters to add command |
156 |
if parents: |
157 |
parents = frozenset(parents) |
|
158 |
delta = self._delta(parents, text) |
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
159 |
|
|
0.1.31
by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go. |
160 |
# offset gives the number of lines that have been inserted
|
161 |
# into the weave up to the current point; if the original edit instruction
|
|
162 |
# says to change line A then we actually change (A+offset)
|
|
163 |
offset = 0 |
|
164 |
||
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
165 |
for i1, i2, newlines in delta: |
|
0.1.29
by Martin Pool
Better internal error |
166 |
assert 0 <= i1 |
167 |
assert i1 <= i2 |
|
168 |
assert i2 <= len(self._l) |
|
|
0.1.56
by Martin Pool
Handle deletion of lines by marking the region with a deletion |
169 |
|
170 |
# the deletion and insertion are handled separately.
|
|
171 |
# first delete the region.
|
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
172 |
if i1 != i2: |
|
0.1.56
by Martin Pool
Handle deletion of lines by marking the region with a deletion |
173 |
self._l.insert(i1+offset, ('[', idx)) |
174 |
self._l.insert(i2+offset+1, (']', idx)) |
|
175 |
offset += 2 |
|
176 |
# is this OK???
|
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
177 |
|
|
0.1.56
by Martin Pool
Handle deletion of lines by marking the region with a deletion |
178 |
if newlines: |
|
0.1.57
by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously |
179 |
# there may have been a deletion spanning up to
|
180 |
# i2; we want to insert after this region to make sure
|
|
181 |
# we don't destroy ourselves
|
|
182 |
i = i2 + offset |
|
|
0.1.56
by Martin Pool
Handle deletion of lines by marking the region with a deletion |
183 |
self._l[i:i] = [('{', idx)] \ |
184 |
+ newlines \ |
|
185 |
+ [('}', idx)] |
|
186 |
offset += 2 + len(newlines) |
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
187 |
|
|
0.1.26
by Martin Pool
Refactor parameters to add command |
188 |
self._v.append(VerInfo(parents)) |
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
189 |
else: |
|
0.1.26
by Martin Pool
Refactor parameters to add command |
190 |
# special case; adding with no parents revision; can do this
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
191 |
# more quickly by just appending unconditionally
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
192 |
self._l.append(('{', idx)) |
193 |
self._l += text |
|
194 |
self._l.append(('}', idx)) |
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
195 |
|
196 |
self._v.append(VerInfo()) |
|
197 |
||
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
198 |
return idx |
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
199 |
|
|
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
200 |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
201 |
def _check_lines(self, text): |
202 |
if not isinstance(text, list): |
|
203 |
raise ValueError("text should be a list, not %s" % type(text)) |
|
204 |
||
205 |
for l in text: |
|
206 |
if not isinstance(l, basestring): |
|
207 |
raise ValueError("text line should be a string or unicode, not %s" % type(l)) |
|
208 |
||
209 |
||
210 |
||
|
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
211 |
def _check_versions(self, indexes): |
212 |
"""Check everything in the sequence of indexes is valid""" |
|
213 |
for i in indexes: |
|
214 |
try: |
|
215 |
self._v[i] |
|
216 |
except IndexError: |
|
217 |
raise IndexError("invalid version number %r" % i) |
|
218 |
||
|
0.1.2
by Martin Pool
Import testsweet module adapted from bzr. |
219 |
|
|
0.1.7
by Martin Pool
Add trivial annotate text |
220 |
def annotate(self, index): |
221 |
return list(self.annotate_iter(index)) |
|
222 |
||
223 |
||
224 |
def annotate_iter(self, index): |
|
225 |
"""Yield list of (index-id, line) pairs for the specified version. |
|
226 |
||
227 |
The index indicates when the line originated in the weave."""
|
|
|
0.1.25
by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis |
228 |
try: |
229 |
vi = self._v[index] |
|
230 |
except IndexError: |
|
231 |
raise IndexError('version index %d out of range' % index) |
|
|
0.1.20
by Martin Pool
Factor out Knit.extract() method |
232 |
included = set(vi.included) |
233 |
included.add(index) |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
234 |
for origin, lineno, text in self._extract(included): |
235 |
yield origin, text |
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
236 |
|
237 |
||
238 |
def _extract(self, included): |
|
|
0.1.20
by Martin Pool
Factor out Knit.extract() method |
239 |
"""Yield annotation of lines in included set. |
240 |
||
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
241 |
Yields a sequence of tuples (origin, lineno, text), where
|
242 |
origin is the origin version, lineno the index in the weave,
|
|
243 |
and text the text of the line.
|
|
244 |
||
|
0.1.20
by Martin Pool
Factor out Knit.extract() method |
245 |
The set typically but not necessarily corresponds to a version.
|
246 |
"""
|
|
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
247 |
istack = [] # versions for which an insertion block is current |
248 |
||
249 |
dset = set() # versions for which a deletion block is current |
|
250 |
||
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
251 |
isactive = False |
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
252 |
|
253 |
lineno = 0 # line of weave, 0-based |
|
|
0.1.53
by Martin Pool
doc |
254 |
|
255 |
# TODO: Probably only need to put included revisions in the istack
|
|
256 |
||
257 |
# TODO: Could split this into two functions, one that updates
|
|
258 |
# the stack and the other that processes the results -- but
|
|
259 |
# I'm not sure it's really needed.
|
|
|
0.1.20
by Martin Pool
Factor out Knit.extract() method |
260 |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
261 |
for l in self._l: |
262 |
if isinstance(l, tuple): |
|
263 |
c, v = l |
|
264 |
if c == '{': |
|
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
265 |
if istack and (istack[-1] >= v): |
|
0.1.47
by Martin Pool
New WeaveError and WeaveFormatError rather than assertions. |
266 |
raise WeaveFormatError("improperly nested insertions %d>=%d on line %d" |
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
267 |
% (istack[-1], v, lineno)) |
268 |
istack.append(v) |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
269 |
elif c == '}': |
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
270 |
try: |
271 |
oldv = istack.pop() |
|
272 |
except IndexError: |
|
273 |
raise WeaveFormatError("unmatched close of insertion %d on line %d" |
|
274 |
% (v, lineno)) |
|
275 |
if oldv != v: |
|
276 |
raise WeaveFormatError("mismatched close of insertion %d!=%d on line %d" |
|
277 |
% (oldv, v, lineno)) |
|
278 |
elif c == '[': |
|
279 |
# block deleted in v
|
|
280 |
if v in dset: |
|
281 |
raise WeaveFormatError("repeated deletion marker for version %d on line %d" |
|
282 |
% (v, lineno)) |
|
|
0.1.49
by Martin Pool
Add another constraint: revisions should not delete text that they |
283 |
if istack: |
284 |
if istack[-1] == v: |
|
285 |
raise WeaveFormatError("version %d deletes own text on line %d" |
|
286 |
% (v, lineno)) |
|
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
287 |
dset.add(v) |
288 |
elif c == ']': |
|
289 |
if v in dset: |
|
290 |
dset.remove(v) |
|
291 |
else: |
|
292 |
raise WeaveFormatError("unmatched close of deletion %d on line %d" |
|
293 |
% (v, lineno)) |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
294 |
else: |
|
0.1.47
by Martin Pool
New WeaveError and WeaveFormatError rather than assertions. |
295 |
raise WeaveFormatError("invalid processing instruction %r on line %d" |
296 |
% (l, lineno)) |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
297 |
else: |
298 |
assert isinstance(l, basestring) |
|
|
0.1.46
by Martin Pool
More constraints on structure of weave, and checks that they work |
299 |
if not istack: |
|
0.1.47
by Martin Pool
New WeaveError and WeaveFormatError rather than assertions. |
300 |
raise WeaveFormatError("literal at top level on line %d" |
301 |
% lineno) |
|
|
0.1.50
by Martin Pool
Basic implementation of deletion markers |
302 |
isactive = (istack[-1] in included) \ |
303 |
and not included.intersection(dset) |
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
304 |
if isactive: |
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
305 |
origin = istack[-1] |
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
306 |
yield origin, lineno, l |
307 |
lineno += 1 |
|
|
0.1.7
by Martin Pool
Add trivial annotate text |
308 |
|
|
0.1.46
by Martin Pool
More constraints on structure of weave, and checks that they work |
309 |
if istack: |
|
0.1.47
by Martin Pool
New WeaveError and WeaveFormatError rather than assertions. |
310 |
raise WeaveFormatError("unclosed insertion blocks at end of weave", |
311 |
istack) |
|
|
0.1.48
by Martin Pool
Basic parsing of delete instructions. |
312 |
if dset: |
313 |
raise WeaveFormatError("unclosed deletion blocks at end of weave", |
|
314 |
dset) |
|
|
0.1.40
by Martin Pool
Add test for extracting from weave with nested insertions |
315 |
|
|
0.1.7
by Martin Pool
Add trivial annotate text |
316 |
|
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
317 |
def getiter(self, index): |
318 |
"""Yield lines for the specified version.""" |
|
|
0.1.8
by Martin Pool
Unify get/annotate code |
319 |
for origin, line in self.annotate_iter(index): |
320 |
yield line |
|
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
321 |
|
322 |
||
|
0.1.4
by Martin Pool
Start indexing knits by both integer and version string. |
323 |
def get(self, index): |
|
0.1.5
by Martin Pool
Add test for storing two text versions. |
324 |
return list(self.getiter(index)) |
|
0.1.1
by Martin Pool
Check in old existing knit code. |
325 |
|
326 |
||
|
0.1.11
by Martin Pool
Add Knit.dump method |
327 |
def dump(self, to_file): |
328 |
from pprint import pprint |
|
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
329 |
print >>to_file, "Weave._l = ", |
|
0.1.11
by Martin Pool
Add Knit.dump method |
330 |
pprint(self._l, to_file) |
|
0.1.38
by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.) |
331 |
print >>to_file, "Weave._v = ", |
|
0.1.18
by Martin Pool
Better Knit.dump method |
332 |
pprint(self._v, to_file) |
|
0.1.11
by Martin Pool
Add Knit.dump method |
333 |
|
334 |
||
|
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
335 |
def check(self): |
336 |
for vers_info in self._v: |
|
337 |
included = set() |
|
338 |
for vi in vers_info[0]: |
|
339 |
if vi < 0 or vi >= index: |
|
|
0.1.47
by Martin Pool
New WeaveError and WeaveFormatError rather than assertions. |
340 |
raise WeaveFormatError("invalid included version %d for index %d" |
341 |
% (vi, index)) |
|
|
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
342 |
if vi in included: |
|
0.1.47
by Martin Pool
New WeaveError and WeaveFormatError rather than assertions. |
343 |
raise WeaveFormatError("repeated included version %d for index %d" |
344 |
% (vi, index)) |
|
|
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
345 |
included.add(vi) |
|
0.1.18
by Martin Pool
Better Knit.dump method |
346 |
|
|
0.1.13
by Martin Pool
Knit structure now allows for versions to include the lines present in other |
347 |
|
348 |
||
|
0.1.21
by Martin Pool
Start computing a delta to insert a new revision |
349 |
def _delta(self, included, lines): |
350 |
"""Return changes from basis to new revision. |
|
351 |
||
352 |
The old text for comparison is the union of included revisions.
|
|
353 |
||
354 |
This is used in inserting a new text.
|
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
355 |
|
|
0.1.55
by Martin Pool
doc |
356 |
Delta is returned as a sequence of
|
357 |
(weave1, weave2, newlines).
|
|
358 |
||
359 |
This indicates that weave1:weave2 of the old weave should be
|
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
360 |
replaced by the sequence of lines in newlines. Note that
|
361 |
these line numbers are positions in the total weave and don't
|
|
362 |
correspond to the lines in any extracted version, or even the
|
|
363 |
extracted union of included versions.
|
|
364 |
||
365 |
If line1=line2, this is a pure insert; if newlines=[] this is a
|
|
366 |
pure delete. (Similar to difflib.)
|
|
|
0.1.21
by Martin Pool
Start computing a delta to insert a new revision |
367 |
"""
|
368 |
||
|
0.1.27
by Martin Pool
Check that version numbers passed in are reasonable |
369 |
self._check_versions(included) |
370 |
||
|
0.1.23
by Martin Pool
tidy up |
371 |
##from pprint import pprint
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
372 |
|
373 |
# first get basis for comparison
|
|
374 |
# basis holds (lineno, origin, line)
|
|
375 |
basis = [] |
|
376 |
||
|
0.1.23
by Martin Pool
tidy up |
377 |
##print 'my lines:'
|
378 |
##pprint(self._l)
|
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
379 |
|
|
0.1.54
by Martin Pool
Fix weave line calculation when making deltas |
380 |
# basis a list of (origin, lineno, line)
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
381 |
basis = list(self._extract(included)) |
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
382 |
|
383 |
# now make a parallel list with only the text, to pass to the differ
|
|
|
0.1.39
by Martin Pool
Change to a more realistic weave structure which can represent insertions and |
384 |
basis_lines = [line for (origin, lineno, line) in basis] |
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
385 |
|
386 |
# add a sentinal, because we can also match against the final line
|
|
|
0.1.54
by Martin Pool
Fix weave line calculation when making deltas |
387 |
basis.append((None, len(self._l), None)) |
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
388 |
|
389 |
# XXX: which line of the weave should we really consider matches the end of the file?
|
|
390 |
# the current code says it's the last line of the weave?
|
|
391 |
||
392 |
from difflib import SequenceMatcher |
|
393 |
s = SequenceMatcher(None, basis_lines, lines) |
|
394 |
||
|
0.1.23
by Martin Pool
tidy up |
395 |
##print 'basis sequence:'
|
396 |
##pprint(basis)
|
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
397 |
|
|
0.1.55
by Martin Pool
doc |
398 |
# TODO: Perhaps return line numbers from composed weave as well?
|
399 |
||
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
400 |
for tag, i1, i2, j1, j2 in s.get_opcodes(): |
|
0.1.23
by Martin Pool
tidy up |
401 |
##print tag, i1, i2, j1, j2
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
402 |
|
403 |
if tag == 'equal': |
|
404 |
continue
|
|
405 |
||
406 |
# i1,i2 are given in offsets within basis_lines; we need to map them
|
|
407 |
# back to offsets within the entire weave
|
|
|
0.1.54
by Martin Pool
Fix weave line calculation when making deltas |
408 |
real_i1 = basis[i1][1] |
409 |
real_i2 = basis[i2][1] |
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
410 |
|
|
0.1.35
by Martin Pool
Clean up Knit._delta method |
411 |
assert 0 <= j1 |
412 |
assert j1 <= j2 |
|
413 |
assert j2 <= len(lines) |
|
|
0.1.22
by Martin Pool
Calculate delta for new versions relative to a set of parent versions. |
414 |
|
|
0.1.35
by Martin Pool
Clean up Knit._delta method |
415 |
yield real_i1, real_i2, lines[j1:j2] |
|
0.1.21
by Martin Pool
Start computing a delta to insert a new revision |
416 |
|
|
0.1.1
by Martin Pool
Check in old existing knit code. |
417 |