1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from bzrlib.lazy_import import lazy_import
19
lazy_import(globals(), """
23
from StringIO import StringIO
31
from bzrlib import bencode
33
from bzrlib.tuned_gzip import GzipFile
36
def topo_iter_keys(vf, keys=None):
39
parents = vf.get_parent_map(keys)
40
return _topo_iter(parents, keys)
42
def topo_iter(vf, versions=None):
44
versions = vf.versions()
45
parents = vf.get_parent_map(versions)
46
return _topo_iter(parents, versions)
48
def _topo_iter(parents, versions):
51
def pending_parents(version):
52
if parents[version] is None:
54
return [v for v in parents[version] if v in versions and
56
for version_id in versions:
57
if parents[version_id] is None:
60
for parent_id in parents[version_id]:
61
descendants.setdefault(parent_id, []).append(version_id)
62
cur = [v for v in versions if len(pending_parents(v)) == 0]
65
for version_id in cur:
66
if version_id in seen:
68
if len(pending_parents(version_id)) != 0:
70
next.extend(descendants.get(version_id, []))
76
class MultiParent(object):
77
"""A multi-parent diff"""
79
def __init__(self, hunks=None):
86
return "MultiParent(%r)" % self.hunks
88
def __eq__(self, other):
89
if self.__class__ is not other.__class__:
91
return (self.hunks == other.hunks)
94
def from_lines(text, parents=(), left_blocks=None):
95
"""Produce a MultiParent from a list of lines and parents"""
97
matcher = patiencediff.PatienceSequenceMatcher(None, parent,
99
return matcher.get_matching_blocks()
101
if left_blocks is None:
102
left_blocks = compare(parents[0])
103
parent_comparisons = [left_blocks] + [compare(p) for p in
106
parent_comparisons = []
108
new_text = NewText([])
110
block_iter = [iter(i) for i in parent_comparisons]
111
diff = MultiParent([])
114
return block_iter[p].next()
115
except StopIteration:
117
cur_block = [next_block(p) for p, i in enumerate(block_iter)]
118
while cur_line < len(text):
120
for p, block in enumerate(cur_block):
124
while j + n <= cur_line:
125
block = cur_block[p] = next_block(p)
133
offset = cur_line - j
139
if best_match is None or n > best_match.num_lines:
140
best_match = ParentText(p, i, j, n)
141
if best_match is None:
142
new_text.lines.append(text[cur_line])
145
if len(new_text.lines) > 0:
146
diff.hunks.append(new_text)
147
new_text = NewText([])
148
diff.hunks.append(best_match)
149
cur_line += best_match.num_lines
150
if len(new_text.lines) > 0:
151
diff.hunks.append(new_text)
154
def get_matching_blocks(self, parent, parent_len):
155
for hunk in self.hunks:
156
if not isinstance(hunk, ParentText) or hunk.parent != parent:
158
yield (hunk.parent_pos, hunk.child_pos, hunk.num_lines)
159
yield parent_len, self.num_lines(), 0
161
def to_lines(self, parents=()):
162
"""Contruct a fulltext from this diff and its parents"""
163
mpvf = MultiMemoryVersionedFile()
164
for num, parent in enumerate(parents):
165
mpvf.add_version(StringIO(parent).readlines(), num, [])
166
mpvf.add_diff(self, 'a', range(len(parents)))
167
return mpvf.get_line_list(['a'])[0]
170
def from_texts(cls, text, parents=()):
171
"""Produce a MultiParent from a text and list of parent text"""
172
return cls.from_lines(StringIO(text).readlines(),
173
[StringIO(p).readlines() for p in parents])
176
"""Yield text lines for a patch"""
177
for hunk in self.hunks:
178
for line in hunk.to_patch():
182
return len(''.join(self.to_patch()))
184
def zipped_patch_len(self):
185
return len(gzip_string(self.to_patch()))
188
def from_patch(cls, text):
189
"""Create a MultiParent from its string form"""
190
return cls._from_patch(StringIO(text))
193
def _from_patch(lines):
194
"""This is private because it is essential to split lines on \n only"""
195
line_iter = iter(lines)
200
cur_line = line_iter.next()
201
except StopIteration:
203
if cur_line[0] == 'i':
204
num_lines = int(cur_line.split(' ')[1])
205
hunk_lines = [line_iter.next() for x in xrange(num_lines)]
206
hunk_lines[-1] = hunk_lines[-1][:-1]
207
hunks.append(NewText(hunk_lines))
208
elif cur_line[0] == '\n':
209
hunks[-1].lines[-1] += '\n'
211
if not (cur_line[0] == 'c'):
212
raise AssertionError(cur_line[0])
213
parent, parent_pos, child_pos, num_lines =\
214
[int(v) for v in cur_line.split(' ')[1:]]
215
hunks.append(ParentText(parent, parent_pos, child_pos,
217
return MultiParent(hunks)
219
def range_iterator(self):
220
"""Iterate through the hunks, with range indicated
222
kind is "new" or "parent".
223
for "new", data is a list of lines.
224
for "parent", data is (parent, parent_start, parent_end)
225
:return: a generator of (start, end, kind, data)
228
for hunk in self.hunks:
229
if isinstance(hunk, NewText):
231
end = start + len(hunk.lines)
235
start = hunk.child_pos
236
end = start + hunk.num_lines
237
data = (hunk.parent, hunk.parent_pos, hunk.parent_pos +
239
yield start, end, kind, data
243
"""The number of lines in the output text"""
245
for hunk in reversed(self.hunks):
246
if isinstance(hunk, ParentText):
247
return hunk.child_pos + hunk.num_lines + extra_n
248
extra_n += len(hunk.lines)
251
def is_snapshot(self):
252
"""Return true of this hunk is effectively a fulltext"""
253
if len(self.hunks) != 1:
255
return (isinstance(self.hunks[0], NewText))
258
class NewText(object):
259
"""The contents of text that is introduced by this text"""
261
def __init__(self, lines):
264
def __eq__(self, other):
265
if self.__class__ is not other.__class__:
267
return (other.lines == self.lines)
270
return 'NewText(%r)' % self.lines
273
yield 'i %d\n' % len(self.lines)
274
for line in self.lines:
279
class ParentText(object):
280
"""A reference to text present in a parent text"""
282
def __init__(self, parent, parent_pos, child_pos, num_lines):
284
self.parent_pos = parent_pos
285
self.child_pos = child_pos
286
self.num_lines = num_lines
289
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
290
' %(num_lines)r)' % self.__dict__
292
def __eq__(self, other):
293
if self.__class__ is not other.__class__:
295
return (self.__dict__ == other.__dict__)
298
yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
302
class BaseVersionedFile(object):
303
"""Pseudo-VersionedFile skeleton for MultiParent"""
305
def __init__(self, snapshot_interval=25, max_snapshots=None):
308
self._snapshots = set()
309
self.snapshot_interval = snapshot_interval
310
self.max_snapshots = max_snapshots
313
return iter(self._parents)
315
def has_version(self, version):
316
return version in self._parents
318
def do_snapshot(self, version_id, parent_ids):
319
"""Determine whether to perform a snapshot for this version"""
320
if self.snapshot_interval is None:
322
if self.max_snapshots is not None and\
323
len(self._snapshots) == self.max_snapshots:
325
if len(parent_ids) == 0:
327
for ignored in xrange(self.snapshot_interval):
328
if len(parent_ids) == 0:
330
version_ids = parent_ids
332
for version_id in version_ids:
333
if version_id not in self._snapshots:
334
parent_ids.extend(self._parents[version_id])
338
def add_version(self, lines, version_id, parent_ids,
339
force_snapshot=None, single_parent=False):
340
"""Add a version to the versionedfile
342
:param lines: The list of lines to add. Must be split on '\n'.
343
:param version_id: The version_id of the version to add
344
:param force_snapshot: If true, force this version to be added as a
345
snapshot version. If false, force this version to be added as a
346
diff. If none, determine this automatically.
347
:param single_parent: If true, use a single parent, rather than
350
if force_snapshot is None:
351
do_snapshot = self.do_snapshot(version_id, parent_ids)
353
do_snapshot = force_snapshot
355
self._snapshots.add(version_id)
356
diff = MultiParent([NewText(lines)])
359
parent_lines = self.get_line_list(parent_ids[:1])
361
parent_lines = self.get_line_list(parent_ids)
362
diff = MultiParent.from_lines(lines, parent_lines)
363
if diff.is_snapshot():
364
self._snapshots.add(version_id)
365
self.add_diff(diff, version_id, parent_ids)
366
self._lines[version_id] = lines
368
def get_parents(self, version_id):
369
return self._parents[version_id]
371
def make_snapshot(self, version_id):
372
snapdiff = MultiParent([NewText(self.cache_version(version_id))])
373
self.add_diff(snapdiff, version_id, self._parents[version_id])
374
self._snapshots.add(version_id)
376
def import_versionedfile(self, vf, snapshots, no_cache=True,
377
single_parent=False, verify=False):
378
"""Import all revisions of a versionedfile
380
:param vf: The versionedfile to import
381
:param snapshots: If provided, the revisions to make snapshots of.
382
Otherwise, this will be auto-determined
383
:param no_cache: If true, clear the cache after every add.
384
:param single_parent: If true, omit all but one parent text, (but
385
retain parent metadata).
387
if not (no_cache or not verify):
389
revisions = set(vf.versions())
390
total = len(revisions)
391
pb = ui.ui_factory.nested_progress_bar()
393
while len(revisions) > 0:
395
for revision in revisions:
396
parents = vf.get_parents(revision)
397
if [p for p in parents if p not in self._parents] != []:
399
lines = [a + ' ' + l for a, l in
400
vf.annotate(revision)]
401
if snapshots is None:
402
force_snapshot = None
404
force_snapshot = (revision in snapshots)
405
self.add_version(lines, revision, parents, force_snapshot,
412
if not (lines == self.get_line_list([revision])[0]):
413
raise AssertionError()
415
pb.update('Importing revisions',
416
(total - len(revisions)) + len(added), total)
417
revisions = [r for r in revisions if r not in added]
421
def select_snapshots(self, vf):
422
"""Determine which versions to add as snapshots"""
426
for version_id in topo_iter(vf):
427
potential_build_ancestors = set(vf.get_parents(version_id))
428
parents = vf.get_parents(version_id)
429
if len(parents) == 0:
430
snapshots.add(version_id)
431
build_ancestors[version_id] = set()
433
for parent in vf.get_parents(version_id):
434
potential_build_ancestors.update(build_ancestors[parent])
435
if len(potential_build_ancestors) > self.snapshot_interval:
436
snapshots.add(version_id)
437
build_ancestors[version_id] = set()
439
build_ancestors[version_id] = potential_build_ancestors
442
def select_by_size(self, num):
443
"""Select snapshots for minimum output size"""
444
num -= len(self._snapshots)
445
new_snapshots = self.get_size_ranking()[-num:]
446
return [v for n, v in new_snapshots]
448
def get_size_ranking(self):
449
"""Get versions ranked by size"""
451
new_snapshots = set()
452
for version_id in self.versions():
453
if version_id in self._snapshots:
455
diff_len = self.get_diff(version_id).patch_len()
456
snapshot_len = MultiParent([NewText(
457
self.cache_version(version_id))]).patch_len()
458
versions.append((snapshot_len - diff_len, version_id))
462
def import_diffs(self, vf):
463
"""Import the diffs from another pseudo-versionedfile"""
464
for version_id in vf.versions():
465
self.add_diff(vf.get_diff(version_id), version_id,
466
vf._parents[version_id])
468
def get_build_ranking(self):
469
"""Return revisions sorted by how much they reduce build complexity"""
472
for version_id in topo_iter(self):
473
could_avoid[version_id] = set()
474
if version_id not in self._snapshots:
475
for parent_id in self._parents[version_id]:
476
could_avoid[version_id].update(could_avoid[parent_id])
477
could_avoid[version_id].update(self._parents)
478
could_avoid[version_id].discard(version_id)
479
for avoid_id in could_avoid[version_id]:
480
referenced_by.setdefault(avoid_id, set()).add(version_id)
481
available_versions = list(self.versions())
483
while len(available_versions) > 0:
484
available_versions.sort(key=lambda x:
485
len(could_avoid[x]) *
486
len(referenced_by.get(x, [])))
487
selected = available_versions.pop()
488
ranking.append(selected)
489
for version_id in referenced_by[selected]:
490
could_avoid[version_id].difference_update(
491
could_avoid[selected])
492
for version_id in could_avoid[selected]:
493
referenced_by[version_id].difference_update(
494
referenced_by[selected]
498
def clear_cache(self):
501
def get_line_list(self, version_ids):
502
return [self.cache_version(v) for v in version_ids]
504
def cache_version(self, version_id):
506
return self._lines[version_id]
509
diff = self.get_diff(version_id)
511
reconstructor = _Reconstructor(self, self._lines, self._parents)
512
reconstructor.reconstruct_version(lines, version_id)
513
self._lines[version_id] = lines
517
class MultiMemoryVersionedFile(BaseVersionedFile):
518
"""Memory-backed pseudo-versionedfile"""
520
def __init__(self, snapshot_interval=25, max_snapshots=None):
521
BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
524
def add_diff(self, diff, version_id, parent_ids):
525
self._diffs[version_id] = diff
526
self._parents[version_id] = parent_ids
528
def get_diff(self, version_id):
530
return self._diffs[version_id]
532
raise errors.RevisionNotPresent(version_id, self)
538
class MultiVersionedFile(BaseVersionedFile):
539
"""Disk-backed pseudo-versionedfile"""
541
def __init__(self, filename, snapshot_interval=25, max_snapshots=None):
542
BaseVersionedFile.__init__(self, snapshot_interval, max_snapshots)
543
self._filename = filename
544
self._diff_offset = {}
546
def get_diff(self, version_id):
547
start, count = self._diff_offset[version_id]
548
infile = open(self._filename + '.mpknit', 'rb')
551
sio = StringIO(infile.read(count))
554
zip_file = GzipFile(None, mode='rb', fileobj=sio)
556
file_version_id = zip_file.readline()
557
return MultiParent.from_patch(zip_file.read())
561
def add_diff(self, diff, version_id, parent_ids):
562
outfile = open(self._filename + '.mpknit', 'ab')
564
outfile.seek(0, 2) # workaround for windows bug:
565
# .tell() for files opened in 'ab' mode
566
# before any write returns 0
567
start = outfile.tell()
569
zipfile = GzipFile(None, mode='ab', fileobj=outfile)
570
zipfile.writelines(itertools.chain(
571
['version %s\n' % version_id], diff.to_patch()))
577
self._diff_offset[version_id] = (start, end-start)
578
self._parents[version_id] = parent_ids
582
os.unlink(self._filename + '.mpknit')
584
if e.errno != errno.ENOENT:
587
os.unlink(self._filename + '.mpidx')
589
if e.errno != errno.ENOENT:
593
open(self._filename + '.mpidx', 'wb').write(bencode.bencode(
594
(self._parents, list(self._snapshots), self._diff_offset)))
597
self._parents, snapshots, self._diff_offset = bencode.bdecode(
598
open(self._filename + '.mpidx', 'rb').read())
599
self._snapshots = set(snapshots)
602
class _Reconstructor(object):
603
"""Build a text from the diffs, ancestry graph and cached lines"""
605
def __init__(self, diffs, lines, parents):
608
self.parents = parents
611
def reconstruct(self, lines, parent_text, version_id):
612
"""Append the lines referred to by a ParentText to lines"""
613
parent_id = self.parents[version_id][parent_text.parent]
614
end = parent_text.parent_pos + parent_text.num_lines
615
return self._reconstruct(lines, parent_id, parent_text.parent_pos,
618
def _reconstruct(self, lines, req_version_id, req_start, req_end):
619
"""Append lines for the requested version_id range"""
620
# stack of pending range requests
621
if req_start == req_end:
623
pending_reqs = [(req_version_id, req_start, req_end)]
624
while len(pending_reqs) > 0:
625
req_version_id, req_start, req_end = pending_reqs.pop()
626
# lazily allocate cursors for versions
627
if req_version_id in self.lines:
628
lines.extend(self.lines[req_version_id][req_start:req_end])
631
start, end, kind, data, iterator = self.cursor[req_version_id]
633
iterator = self.diffs.get_diff(req_version_id).range_iterator()
634
start, end, kind, data = iterator.next()
635
if start > req_start:
636
iterator = self.diffs.get_diff(req_version_id).range_iterator()
637
start, end, kind, data = iterator.next()
639
# find the first hunk relevant to the request
640
while end <= req_start:
641
start, end, kind, data = iterator.next()
642
self.cursor[req_version_id] = start, end, kind, data, iterator
643
# if the hunk can't satisfy the whole request, split it in two,
644
# and leave the second half for later.
646
pending_reqs.append((req_version_id, end, req_end))
649
lines.extend(data[req_start - start: (req_end - start)])
651
# If the hunk is a ParentText, rewrite it as a range request
652
# for the parent, and make it the next pending request.
653
parent, parent_start, parent_end = data
654
new_version_id = self.parents[req_version_id][parent]
655
new_start = parent_start + req_start - start
656
new_end = parent_end + req_end - end
657
pending_reqs.append((new_version_id, new_start, new_end))
659
def reconstruct_version(self, lines, version_id):
660
length = self.diffs.get_diff(version_id).num_lines()
661
return self._reconstruct(lines, version_id, 0, length)
664
def gzip_string(lines):
666
data_file = GzipFile(None, mode='wb', fileobj=sio)
667
data_file.writelines(lines)
669
return sio.getvalue()