bzr branch
http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
|
0.9.2
by Aaron Bentley
Get single-parent comparison working |
1 |
from difflib import SequenceMatcher |
|
0.9.30
by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile |
2 |
from itertools import chain |
|
0.9.25
by Aaron Bentley
More messy hacking |
3 |
from StringIO import StringIO |
|
0.9.19
by Aaron Bentley
More tweakage |
4 |
import sys |
5 |
||
|
0.9.25
by Aaron Bentley
More messy hacking |
6 |
from bzrlib import ( |
7 |
patiencediff, |
|
8 |
trace, |
|
9 |
ui, |
|
10 |
)
|
|
11 |
||
12 |
from bzrlib.tuned_gzip import GzipFile |
|
|
0.9.3
by Aaron Bentley
Get three-parent comparisions under test |
13 |
|
|
0.9.26
by Aaron Bentley
Move topological iteration into an iterator |
14 |
def topo_iter(vf): |
15 |
seen = set() |
|
16 |
descendants = {} |
|
17 |
for version_id in vf.versions(): |
|
18 |
for parent_id in vf.get_parents(version_id): |
|
19 |
descendants.setdefault(parent_id, []).append(version_id) |
|
20 |
cur = [v for v in vf.versions() if len(vf.get_parents(v)) == 0] |
|
21 |
while len(cur) > 0: |
|
22 |
next = [] |
|
23 |
for version_id in cur: |
|
24 |
if version_id in seen: |
|
25 |
continue
|
|
26 |
parents = vf.get_parents(version_id) |
|
27 |
if not seen.issuperset(parents): |
|
28 |
continue
|
|
29 |
next.extend(descendants.get(version_id, [])) |
|
30 |
yield version_id |
|
31 |
seen.add(version_id) |
|
32 |
cur = next |
|
33 |
||
34 |
||
|
0.9.1
by Aaron Bentley
Get trivial case passing |
35 |
class MultiParent(object): |
36 |
||
|
0.9.2
by Aaron Bentley
Get single-parent comparison working |
37 |
def __init__(self, hunks=None): |
38 |
if hunks is not None: |
|
39 |
self.hunks = hunks |
|
40 |
else: |
|
41 |
self.hunks = [] |
|
42 |
||
43 |
def __repr__(self): |
|
44 |
return "MultiParent(%r)" % self.hunks |
|
45 |
||
46 |
def __eq__(self, other): |
|
47 |
if self.__class__ is not other.__class__: |
|
48 |
return False |
|
49 |
return (self.hunks == other.hunks) |
|
|
0.9.1
by Aaron Bentley
Get trivial case passing |
50 |
|
51 |
@staticmethod
|
|
52 |
def from_lines(text, parents=()): |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
53 |
"""Produce a MultiParent from a list of lines and parents""" |
|
0.9.2
by Aaron Bentley
Get single-parent comparison working |
54 |
def compare(parent): |
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
55 |
matcher = patiencediff.PatienceSequenceMatcher(None, parent, |
56 |
text) |
|
57 |
return matcher.get_matching_blocks() |
|
|
0.9.2
by Aaron Bentley
Get single-parent comparison working |
58 |
parent_comparisons = [compare(p) for p in parents] |
59 |
cur_line = 0 |
|
60 |
new_text = NewText([]) |
|
61 |
parent_text = [] |
|
62 |
block_iter = [iter(i) for i in parent_comparisons] |
|
63 |
diff = MultiParent([]) |
|
64 |
def next_block(p): |
|
65 |
try: |
|
66 |
return block_iter[p].next() |
|
67 |
except StopIteration: |
|
68 |
return None |
|
69 |
cur_block = [next_block(p) for p, i in enumerate(block_iter)] |
|
70 |
while cur_line < len(text): |
|
71 |
best_match = None |
|
72 |
for p, block in enumerate(cur_block): |
|
73 |
if block is None: |
|
74 |
continue
|
|
75 |
i, j, n = block |
|
76 |
while j + n < cur_line: |
|
77 |
block = cur_block[p] = next_block(p) |
|
78 |
if block is None: |
|
79 |
break
|
|
80 |
i, j, n = block |
|
81 |
if block is None: |
|
82 |
continue
|
|
83 |
if j > cur_line: |
|
84 |
continue
|
|
85 |
offset = cur_line - j |
|
86 |
i += offset |
|
87 |
j = cur_line |
|
88 |
n -= offset |
|
89 |
if n == 0: |
|
90 |
continue
|
|
91 |
if best_match is None or n > best_match.num_lines: |
|
92 |
best_match = ParentText(p, i, j, n) |
|
93 |
if best_match is None: |
|
94 |
new_text.lines.append(text[cur_line]) |
|
95 |
cur_line += 1 |
|
96 |
else: |
|
97 |
if len(new_text.lines) > 0: |
|
98 |
diff.hunks.append(new_text) |
|
99 |
new_text = NewText([]) |
|
100 |
diff.hunks.append(best_match) |
|
101 |
cur_line += best_match.num_lines |
|
102 |
if len(new_text.lines) > 0: |
|
103 |
diff.hunks.append(new_text) |
|
|
0.9.1
by Aaron Bentley
Get trivial case passing |
104 |
return diff |
105 |
||
106 |
@classmethod
|
|
107 |
def from_texts(cls, text, parents=()): |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
108 |
"""Produce a MultiParent from a text and list of parent text""" |
|
0.9.1
by Aaron Bentley
Get trivial case passing |
109 |
return cls.from_lines(text.splitlines(True), |
110 |
[p.splitlines(True) for p in parents]) |
|
111 |
||
|
0.9.4
by Aaron Bentley
Start supporting serialization |
112 |
def to_patch(self): |
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
113 |
"""Yield text lines for a patch""" |
|
0.9.4
by Aaron Bentley
Start supporting serialization |
114 |
for hunk in self.hunks: |
115 |
for line in hunk.to_patch(): |
|
116 |
yield line |
|
117 |
||
|
0.9.25
by Aaron Bentley
More messy hacking |
118 |
def patch_len(self): |
119 |
return len(''.join(self.to_patch())) |
|
120 |
||
121 |
def zipped_patch_len(self): |
|
122 |
return len(gzip_string(self.to_patch())) |
|
123 |
||
|
0.9.18
by Aaron Bentley
Implement from_patch |
124 |
@staticmethod
|
125 |
def from_patch(lines): |
|
|
0.9.19
by Aaron Bentley
More tweakage |
126 |
"""Produce a MultiParent from a sequence of lines""" |
|
0.9.18
by Aaron Bentley
Implement from_patch |
127 |
line_iter = iter(lines) |
128 |
hunks = [] |
|
129 |
cur_line = None |
|
130 |
while(True): |
|
131 |
try: |
|
132 |
cur_line = line_iter.next() |
|
133 |
except StopIteration: |
|
134 |
break
|
|
135 |
if cur_line[0] == 'i': |
|
136 |
num_lines = int(cur_line.split(' ')[1]) |
|
137 |
hunk_lines = [line_iter.next() for x in xrange(num_lines)] |
|
138 |
hunk_lines[-1] = hunk_lines[-1][:-1] |
|
139 |
hunks.append(NewText(hunk_lines)) |
|
140 |
elif cur_line[0] == '\n': |
|
141 |
hunks[-1].lines[-1] += '\n' |
|
142 |
else: |
|
143 |
assert cur_line[0] == 'c', cur_line[0] |
|
144 |
parent, parent_pos, child_pos, num_lines =\ |
|
145 |
[int(v) for v in cur_line.split(' ')[1:]] |
|
146 |
hunks.append(ParentText(parent, parent_pos, child_pos, |
|
147 |
num_lines)) |
|
148 |
return MultiParent(hunks) |
|
149 |
||
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
150 |
def range_iterator(self): |
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
151 |
"""Iterate through the hunks, with range indicated |
152 |
||
153 |
kind is "new" or "parent".
|
|
154 |
for "new", data is a list of lines.
|
|
155 |
for "parent", data is (parent, parent_start, parent_end)
|
|
156 |
:return: a generator of (start, end, kind, data)
|
|
157 |
"""
|
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
158 |
start = 0 |
159 |
for hunk in self.hunks: |
|
160 |
if isinstance(hunk, NewText): |
|
161 |
kind = 'new' |
|
162 |
end = start + len(hunk.lines) |
|
163 |
data = hunk.lines |
|
164 |
else: |
|
165 |
kind = 'parent' |
|
166 |
start = hunk.child_pos |
|
167 |
end = start + hunk.num_lines |
|
168 |
data = (hunk.parent, hunk.parent_pos, hunk.parent_pos + |
|
169 |
hunk.num_lines) |
|
170 |
yield start, end, kind, data |
|
171 |
start = end |
|
172 |
||
|
0.9.11
by Aaron Bentley
Implement reconstruct_version, handle all hunks through that |
173 |
def num_lines(self): |
174 |
extra_n = 0 |
|
175 |
for hunk in reversed(self.hunks): |
|
176 |
if isinstance(hunk, ParentText): |
|
177 |
return hunk.child_pos + hunk.num_lines + extra_n |
|
178 |
extra_n += len(hunk.lines) |
|
179 |
return extra_n |
|
180 |
||
|
0.9.25
by Aaron Bentley
More messy hacking |
181 |
def is_snapshot(self): |
182 |
if len(self.hunks) != 1: |
|
183 |
return False |
|
184 |
return (isinstance(self.hunks[0], NewText)) |
|
185 |
||
|
0.9.1
by Aaron Bentley
Get trivial case passing |
186 |
|
187 |
class NewText(object): |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
188 |
"""The contents of text that is introduced by this text""" |
|
0.9.1
by Aaron Bentley
Get trivial case passing |
189 |
|
190 |
def __init__(self, lines): |
|
191 |
self.lines = lines |
|
192 |
||
193 |
def __eq__(self, other): |
|
194 |
if self.__class__ is not other.__class__: |
|
195 |
return False |
|
196 |
return (other.lines == self.lines) |
|
|
0.9.2
by Aaron Bentley
Get single-parent comparison working |
197 |
|
198 |
def __repr__(self): |
|
199 |
return 'NewText(%r)' % self.lines |
|
200 |
||
|
0.9.4
by Aaron Bentley
Start supporting serialization |
201 |
def to_patch(self): |
202 |
yield 'i %d\n' % len(self.lines) |
|
203 |
for line in self.lines: |
|
204 |
yield line |
|
205 |
yield '\n' |
|
206 |
||
|
0.9.2
by Aaron Bentley
Get single-parent comparison working |
207 |
|
208 |
class ParentText(object): |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
209 |
"""A reference to text present in a parent text""" |
|
0.9.2
by Aaron Bentley
Get single-parent comparison working |
210 |
|
211 |
def __init__(self, parent, parent_pos, child_pos, num_lines): |
|
212 |
self.parent = parent |
|
213 |
self.parent_pos = parent_pos |
|
214 |
self.child_pos = child_pos |
|
215 |
self.num_lines = num_lines |
|
216 |
||
217 |
def __repr__(self): |
|
218 |
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\ |
|
219 |
' %(num_lines)r)' % self.__dict__ |
|
220 |
||
221 |
def __eq__(self, other): |
|
222 |
if self.__class__ != other.__class__: |
|
223 |
return False |
|
224 |
return (self.__dict__ == other.__dict__) |
|
|
0.9.4
by Aaron Bentley
Start supporting serialization |
225 |
|
226 |
def to_patch(self): |
|
227 |
yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\ |
|
228 |
% self.__dict__ |
|
|
0.9.8
by Aaron Bentley
get add_version working |
229 |
|
230 |
||
|
0.9.30
by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile |
231 |
class BaseVersionedFile(object): |
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
232 |
"""VersionedFile skeleton for MultiParent""" |
|
0.9.8
by Aaron Bentley
get add_version working |
233 |
|
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
234 |
def __init__(self, snapshot_interval=25, max_snapshots=None): |
|
0.9.8
by Aaron Bentley
get add_version working |
235 |
self._lines = {} |
236 |
self._parents = {} |
|
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
237 |
self._snapshots = set() |
|
0.9.12
by Aaron Bentley
Make benchmarks for mp |
238 |
self.snapshot_interval = snapshot_interval |
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
239 |
self.max_snapshots = max_snapshots |
|
0.9.12
by Aaron Bentley
Make benchmarks for mp |
240 |
|
|
0.9.30
by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile |
241 |
def versions(self): |
242 |
return iter(self._parents) |
|
243 |
||
|
0.9.12
by Aaron Bentley
Make benchmarks for mp |
244 |
def do_snapshot(self, version_id, parent_ids): |
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
245 |
if self.snapshot_interval is None: |
246 |
return False |
|
247 |
if self.max_snapshots is not None and\ |
|
248 |
len(self._snapshots) == self.max_snapshots: |
|
|
0.9.14
by Aaron Bentley
Temporarily force snapshots to 44 |
249 |
return False |
|
0.9.12
by Aaron Bentley
Make benchmarks for mp |
250 |
if len(parent_ids) == 0: |
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
251 |
return True |
252 |
for ignored in xrange(self.snapshot_interval): |
|
|
0.9.12
by Aaron Bentley
Make benchmarks for mp |
253 |
if len(parent_ids) == 0: |
254 |
return False |
|
|
0.9.17
by Aaron Bentley
Dynamically select snapshots based on all parents |
255 |
version_ids = parent_ids |
256 |
parent_ids = [] |
|
257 |
for version_id in version_ids: |
|
258 |
if version_id not in self._snapshots: |
|
259 |
parent_ids.extend(self._parents[version_id]) |
|
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
260 |
else: |
261 |
return True |
|
|
0.9.8
by Aaron Bentley
get add_version working |
262 |
|
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
263 |
def add_version(self, lines, version_id, parent_ids, |
|
0.9.20
by Aaron Bentley
Convert to a plugin |
264 |
force_snapshot=None, single_parent=False): |
|
0.9.16
by Aaron Bentley
More control over snapshotting, disable caching for inventory |
265 |
if force_snapshot is None: |
266 |
do_snapshot = self.do_snapshot(version_id, parent_ids) |
|
267 |
else: |
|
268 |
do_snapshot = force_snapshot |
|
269 |
if do_snapshot: |
|
270 |
self._snapshots.add(version_id) |
|
|
0.9.12
by Aaron Bentley
Make benchmarks for mp |
271 |
diff = MultiParent([NewText(lines)]) |
272 |
else: |
|
|
0.9.20
by Aaron Bentley
Convert to a plugin |
273 |
if single_parent: |
274 |
parent_lines = self.get_line_list(parent_ids[:1]) |
|
275 |
else: |
|
276 |
parent_lines = self.get_line_list(parent_ids) |
|
|
0.9.12
by Aaron Bentley
Make benchmarks for mp |
277 |
diff = MultiParent.from_lines(lines, parent_lines) |
|
0.9.25
by Aaron Bentley
More messy hacking |
278 |
snapdiff = MultiParent([NewText(lines)]) |
279 |
if diff.is_snapshot(): |
|
280 |
self._snapshots.add(version_id) |
|
281 |
elif diff.patch_len() >= snapdiff.patch_len(): |
|
282 |
trace.note("Forcing snapshot") |
|
283 |
self._snapshots.add(version_id) |
|
|
0.9.8
by Aaron Bentley
get add_version working |
284 |
self.add_diff(diff, version_id, parent_ids) |
285 |
self._lines[version_id] = lines |
|
286 |
||
|
0.9.20
by Aaron Bentley
Convert to a plugin |
287 |
def import_versionedfile(self, vf, snapshots, no_cache=True, |
|
0.9.22
by Aaron Bentley
Fix restoration bug |
288 |
single_parent=False, verify=False): |
|
0.9.20
by Aaron Bentley
Convert to a plugin |
289 |
"""Import all revisions of a versionedfile |
290 |
||
291 |
:param vf: The versionedfile to import
|
|
292 |
:param snapshots: If provided, the revisions to make snapshots of.
|
|
293 |
Otherwise, this will be auto-determined
|
|
294 |
:param no_cache: If true, clear the cache after every add.
|
|
295 |
:param single_parent: If true, omit all but one parent text, (but
|
|
296 |
retain parent metadata).
|
|
297 |
"""
|
|
|
0.9.22
by Aaron Bentley
Fix restoration bug |
298 |
assert no_cache or not verify |
|
0.9.19
by Aaron Bentley
More tweakage |
299 |
revisions = set(vf.versions()) |
300 |
total = len(revisions) |
|
|
0.9.20
by Aaron Bentley
Convert to a plugin |
301 |
pb = ui.ui_factory.nested_progress_bar() |
302 |
try: |
|
303 |
while len(revisions) > 0: |
|
304 |
added = set() |
|
305 |
for revision in revisions: |
|
306 |
parents = vf.get_parents(revision) |
|
|
0.9.30
by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile |
307 |
if [p for p in parents if p not in self._parents] != []: |
|
0.9.20
by Aaron Bentley
Convert to a plugin |
308 |
continue
|
309 |
lines = [a + ' ' + l for a, l in |
|
310 |
vf.annotate_iter(revision)] |
|
|
0.9.21
by Aaron Bentley
finish converting ft_ to snapshots |
311 |
if snapshots is None: |
|
0.9.20
by Aaron Bentley
Convert to a plugin |
312 |
force_snapshot = None |
313 |
else: |
|
|
0.9.21
by Aaron Bentley
finish converting ft_ to snapshots |
314 |
force_snapshot = (revision in snapshots) |
|
0.9.20
by Aaron Bentley
Convert to a plugin |
315 |
self.add_version(lines, revision, parents, force_snapshot, |
316 |
single_parent) |
|
317 |
added.add(revision) |
|
318 |
if no_cache: |
|
319 |
self.clear_cache() |
|
|
0.9.25
by Aaron Bentley
More messy hacking |
320 |
vf.clear_cache() |
|
0.9.22
by Aaron Bentley
Fix restoration bug |
321 |
if verify: |
322 |
assert lines == self.get_line_list([revision])[0] |
|
323 |
self.clear_cache() |
|
|
0.9.20
by Aaron Bentley
Convert to a plugin |
324 |
pb.update('Importing revisions', |
325 |
(total - len(revisions)) + len(added), total) |
|
326 |
revisions = [r for r in revisions if r not in added] |
|
327 |
finally: |
|
328 |
pb.finished() |
|
|
0.9.19
by Aaron Bentley
More tweakage |
329 |
|
|
0.9.23
by Aaron Bentley
handle snapshots all at once |
330 |
def select_snapshots(self, vf): |
|
0.9.28
by Aaron Bentley
Update snapshot-picking to use sets of ancestors |
331 |
build_ancestors = {} |
|
0.9.23
by Aaron Bentley
handle snapshots all at once |
332 |
descendants = {} |
333 |
snapshots = set() |
|
|
0.9.26
by Aaron Bentley
Move topological iteration into an iterator |
334 |
for version_id in topo_iter(vf): |
|
0.9.28
by Aaron Bentley
Update snapshot-picking to use sets of ancestors |
335 |
potential_build_ancestors = set(vf.get_parents(version_id)) |
336 |
parents = vf.get_parents(version_id) |
|
337 |
if len(parents) == 0: |
|
|
0.9.26
by Aaron Bentley
Move topological iteration into an iterator |
338 |
snapshots.add(version_id) |
|
0.9.28
by Aaron Bentley
Update snapshot-picking to use sets of ancestors |
339 |
build_ancestors[version_id] = set() |
|
0.9.26
by Aaron Bentley
Move topological iteration into an iterator |
340 |
else: |
|
0.9.28
by Aaron Bentley
Update snapshot-picking to use sets of ancestors |
341 |
for parent in vf.get_parents(version_id): |
342 |
potential_build_ancestors.update(build_ancestors[parent]) |
|
343 |
if len(potential_build_ancestors) > self.snapshot_interval: |
|
344 |
snapshots.add(version_id) |
|
345 |
build_ancestors[version_id] = set() |
|
|
0.9.23
by Aaron Bentley
handle snapshots all at once |
346 |
else: |
|
0.9.28
by Aaron Bentley
Update snapshot-picking to use sets of ancestors |
347 |
build_ancestors[version_id] = potential_build_ancestors |
|
0.9.23
by Aaron Bentley
handle snapshots all at once |
348 |
return snapshots |
349 |
||
350 |
||
|
0.9.8
by Aaron Bentley
get add_version working |
351 |
def clear_cache(self): |
352 |
self._lines.clear() |
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
353 |
|
354 |
def get_line_list(self, version_ids): |
|
355 |
return [self.cache_version(v) for v in version_ids] |
|
356 |
||
357 |
def cache_version(self, version_id): |
|
358 |
try: |
|
359 |
return self._lines[version_id] |
|
360 |
except KeyError: |
|
361 |
pass
|
|
|
0.9.29
by Aaron Bentley
Support using disk for knit reconstruction |
362 |
diff = self.get_diff(version_id) |
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
363 |
lines = [] |
|
0.9.29
by Aaron Bentley
Support using disk for knit reconstruction |
364 |
reconstructor = _Reconstructor(self, self._lines, |
|
0.9.17
by Aaron Bentley
Dynamically select snapshots based on all parents |
365 |
self._parents) |
|
0.9.11
by Aaron Bentley
Implement reconstruct_version, handle all hunks through that |
366 |
reconstructor.reconstruct_version(lines, version_id) |
|
0.9.25
by Aaron Bentley
More messy hacking |
367 |
#self._lines[version_id] = lines
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
368 |
return lines |
369 |
||
|
0.9.30
by Aaron Bentley
Split into MultiVersionedFile and MultiMemoryVersionedFile |
370 |
class MultiMemoryVersionedFile(BaseVersionedFile): |
371 |
||
372 |
def __init__(self, snapshot_interval=25, max_snapshots=None): |
|
373 |
BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots) |
|
374 |
self._diffs = {} |
|
375 |
||
376 |
def add_diff(self, diff, version_id, parent_ids): |
|
377 |
self._diffs[version_id] = diff |
|
378 |
self._parents[version_id] = parent_ids |
|
379 |
||
380 |
def get_diff(self, version_id): |
|
381 |
return self._diffs[version_id] |
|
382 |
||
383 |
||
384 |
class MultiVersionedFile(BaseVersionedFile): |
|
385 |
||
386 |
def __init__(self, filename, snapshot_interval=25, max_snapshots=None): |
|
387 |
BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots) |
|
388 |
self._filename = filename |
|
389 |
self._diff_offset = {} |
|
390 |
||
391 |
def get_diff(self, version_id): |
|
392 |
start, count = self._diff_offset[version_id] |
|
393 |
infile = open(self._filename, 'rb') |
|
394 |
try: |
|
395 |
infile.seek(start) |
|
396 |
sio = StringIO(infile.read(count)) |
|
397 |
finally: |
|
398 |
infile.close() |
|
399 |
zip_file = GzipFile(None, mode='rb', fileobj=sio) |
|
400 |
try: |
|
401 |
file_version_id = zip_file.readline() |
|
402 |
return MultiParent.from_patch(zip_file.readlines()) |
|
403 |
finally: |
|
404 |
zip_file.close() |
|
405 |
||
406 |
def add_diff(self, diff, version_id, parent_ids): |
|
407 |
outfile = open(self._filename, 'ab') |
|
408 |
try: |
|
409 |
start = outfile.tell() |
|
410 |
try: |
|
411 |
zipfile = GzipFile(None, mode='ab', fileobj=outfile) |
|
412 |
zipfile.writelines(chain(['version %s\n' % version_id], |
|
413 |
diff.to_patch())) |
|
414 |
finally: |
|
415 |
zipfile.close() |
|
416 |
end = outfile.tell() |
|
417 |
finally: |
|
418 |
outfile.close() |
|
419 |
self._diff_offset[version_id] = (start, end-start) |
|
420 |
self._parents[version_id] = parent_ids |
|
421 |
||
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
422 |
|
423 |
class _Reconstructor(object): |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
424 |
"""Build a text from the diffs, ancestry graph and cached lines""" |
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
425 |
|
426 |
def __init__(self, diffs, lines, parents): |
|
427 |
self.diffs = diffs |
|
428 |
self.lines = lines |
|
429 |
self.parents = parents |
|
430 |
self.cursor = {} |
|
431 |
||
432 |
def reconstruct(self, lines, parent_text, version_id): |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
433 |
"""Append the lines referred to by a ParentText to lines""" |
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
434 |
parent_id = self.parents[version_id][parent_text.parent] |
435 |
end = parent_text.parent_pos + parent_text.num_lines |
|
|
0.9.17
by Aaron Bentley
Dynamically select snapshots based on all parents |
436 |
return self._reconstruct(lines, parent_id, parent_text.parent_pos, |
437 |
end) |
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
438 |
|
439 |
def _reconstruct(self, lines, req_version_id, req_start, req_end): |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
440 |
"""Append lines for the requested version_id range""" |
441 |
# stack of pending range requests
|
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
442 |
pending_reqs = [(req_version_id, req_start, req_end)] |
443 |
while len(pending_reqs) > 0: |
|
444 |
req_version_id, req_start, req_end = pending_reqs.pop() |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
445 |
# lazily allocate cursors for versions
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
446 |
try: |
447 |
start, end, kind, data, iterator = self.cursor[req_version_id] |
|
448 |
except KeyError: |
|
|
0.9.29
by Aaron Bentley
Support using disk for knit reconstruction |
449 |
iterator = self.diffs.get_diff(req_version_id).range_iterator() |
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
450 |
start, end, kind, data = iterator.next() |
|
0.9.22
by Aaron Bentley
Fix restoration bug |
451 |
if start > req_start: |
|
0.9.29
by Aaron Bentley
Support using disk for knit reconstruction |
452 |
iterator = self.diffs.get_diff(req_version_id).range_iterator() |
|
0.9.22
by Aaron Bentley
Fix restoration bug |
453 |
start, end, kind, data = iterator.next() |
454 |
||
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
455 |
# find the first hunk relevant to the request
|
456 |
while end <= req_start: |
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
457 |
start, end, kind, data = iterator.next() |
458 |
self.cursor[req_version_id] = start, end, kind, data, iterator |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
459 |
# if the hunk can't satisfy the whole request, split it in two,
|
460 |
# and leave the second half for later.
|
|
461 |
if req_end > end: |
|
462 |
pending_reqs.append((req_version_id, end, req_end)) |
|
463 |
req_end = end |
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
464 |
if kind == 'new': |
465 |
lines.extend(data[req_start - start: (req_end - start)]) |
|
466 |
else: |
|
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
467 |
# If the hunk is a ParentText, rewrite it as a range request
|
468 |
# for the parent, and make it the next pending request.
|
|
|
0.9.9
by Aaron Bentley
Much progress on non-naive text reconstruction |
469 |
parent, parent_start, parent_end = data |
|
0.9.10
by Aaron Bentley
Text reconstruction seems to work |
470 |
new_version_id = self.parents[req_version_id][parent] |
471 |
new_start = parent_start + req_start - start |
|
472 |
new_end = parent_end + req_end - end |
|
473 |
pending_reqs.append((new_version_id, new_start, new_end)) |
|
|
0.9.11
by Aaron Bentley
Implement reconstruct_version, handle all hunks through that |
474 |
|
475 |
def reconstruct_version(self, lines, version_id): |
|
|
0.9.29
by Aaron Bentley
Support using disk for knit reconstruction |
476 |
length = self.diffs.get_diff(version_id).num_lines() |
|
0.9.11
by Aaron Bentley
Implement reconstruct_version, handle all hunks through that |
477 |
return self._reconstruct(lines, version_id, 0, length) |
|
0.9.25
by Aaron Bentley
More messy hacking |
478 |
|
479 |
def gzip_string(lines): |
|
480 |
sio = StringIO() |
|
481 |
data_file = GzipFile(None, mode='wb', fileobj=sio) |
|
482 |
data_file.writelines(lines) |
|
483 |
data_file.close() |
|
484 |
return sio.getvalue() |