1
# Copyright (C) 2007-2011 Canonical Ltd
1
# Copyright (C) 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
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 __future__ import absolute_import
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
17
from bzrlib.lazy_import import lazy_import
21
19
lazy_import(globals(), """
26
23
from StringIO import StringIO
28
25
from bzrlib import (
30
from bzrlib.util import bencode
37
def topo_iter_keys(vf, keys=None):
40
parents = vf.get_parent_map(keys)
41
return _topo_iter(parents, keys)
32
from bzrlib.tuned_gzip import GzipFile
43
35
def topo_iter(vf, versions=None):
44
38
if versions is None:
45
39
versions = vf.versions()
46
parents = vf.get_parent_map(versions)
47
return _topo_iter(parents, versions)
49
def _topo_iter(parents, versions):
52
40
def pending_parents(version):
53
if parents[version] is None:
55
return [v for v in parents[version] if v in versions and
41
return [v for v in vf.get_parents(version) if v in versions and
57
43
for version_id in versions:
58
if parents[version_id] is None:
61
for parent_id in parents[version_id]:
44
for parent_id in vf.get_parents(version_id):
62
45
descendants.setdefault(parent_id, []).append(version_id)
63
46
cur = [v for v in versions if len(pending_parents(v)) == 0]
64
47
while len(cur) > 0:
73
56
seen.add(version_id)
58
assert len(seen) == len(versions)
77
61
class MultiParent(object):
78
62
"""A multi-parent diff"""
82
64
def __init__(self, hunks=None):
83
65
if hunks is not None:
154
136
diff.hunks.append(new_text)
157
def get_matching_blocks(self, parent, parent_len):
158
for hunk in self.hunks:
159
if not isinstance(hunk, ParentText) or hunk.parent != parent:
161
yield (hunk.parent_pos, hunk.child_pos, hunk.num_lines)
162
yield parent_len, self.num_lines(), 0
164
139
def to_lines(self, parents=()):
165
140
"""Contruct a fulltext from this diff and its parents"""
166
141
mpvf = MultiMemoryVersionedFile()
211
186
elif cur_line[0] == '\n':
212
187
hunks[-1].lines[-1] += '\n'
214
if not (cur_line[0] == 'c'):
215
raise AssertionError(cur_line[0])
189
assert cur_line[0] == 'c', cur_line[0]
216
190
parent, parent_pos, child_pos, num_lines =\
217
191
[int(v) for v in cur_line.split(' ')[1:]]
218
192
hunks.append(ParentText(parent, parent_pos, child_pos,
284
256
class ParentText(object):
285
257
"""A reference to text present in a parent text"""
287
__slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
289
259
def __init__(self, parent, parent_pos, child_pos, num_lines):
290
260
self.parent = parent
291
261
self.parent_pos = parent_pos
292
262
self.child_pos = child_pos
293
263
self.num_lines = num_lines
296
return dict(parent=self.parent, parent_pos=self.parent_pos,
297
child_pos=self.child_pos, num_lines=self.num_lines)
299
265
def __repr__(self):
300
return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
301
' %(num_lines)r)' % self._as_dict())
266
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
267
' %(num_lines)r)' % self.__dict__
303
269
def __eq__(self, other):
304
if self.__class__ is not other.__class__:
270
if self.__class__ != other.__class__:
306
return self._as_dict() == other._as_dict()
272
return (self.__dict__ == other.__dict__)
308
274
def to_patch(self):
309
yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
275
yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
313
279
class BaseVersionedFile(object):
327
293
return version in self._parents
329
295
def do_snapshot(self, version_id, parent_ids):
330
"""Determine whether to perform a snapshot for this version"""
296
"""Determine whether to perform a a snapshot for this version"""
331
297
if self.snapshot_interval is None:
333
299
if self.max_snapshots is not None and\
395
361
:param single_parent: If true, omit all but one parent text, (but
396
362
retain parent metadata).
398
if not (no_cache or not verify):
364
assert no_cache or not verify
400
365
revisions = set(vf.versions())
401
366
total = len(revisions)
402
367
pb = ui.ui_factory.nested_progress_bar()
420
385
self.clear_cache()
423
if not (lines == self.get_line_list([revision])[0]):
424
raise AssertionError()
388
assert lines == self.get_line_list([revision])[0]
425
389
self.clear_cache()
426
pb.update(gettext('Importing revisions'),
390
pb.update('Importing revisions',
427
391
(total - len(revisions)) + len(added), total)
428
392
revisions = [r for r in revisions if r not in added]
520
484
diff = self.get_diff(version_id)
522
reconstructor = _Reconstructor(self, self._lines, self._parents)
486
reconstructor = _Reconstructor(self, self._lines,
523
488
reconstructor.reconstruct_version(lines, version_id)
524
489
self._lines[version_id] = lines
537
502
self._parents[version_id] = parent_ids
539
504
def get_diff(self, version_id):
541
return self._diffs[version_id]
543
raise errors.RevisionNotPresent(version_id, self)
505
return self._diffs[version_id]
545
507
def destroy(self):
562
524
sio = StringIO(infile.read(count))
565
zip_file = gzip.GzipFile(None, mode='rb', fileobj=sio)
527
zip_file = GzipFile(None, mode='rb', fileobj=sio)
567
529
file_version_id = zip_file.readline()
568
content = zip_file.read()
569
return MultiParent.from_patch(content)
530
return MultiParent.from_patch(zip_file.read())
573
534
def add_diff(self, diff, version_id, parent_ids):
574
535
outfile = open(self._filename + '.mpknit', 'ab')
576
outfile.seek(0, 2) # workaround for windows bug:
577
# .tell() for files opened in 'ab' mode
578
# before any write returns 0
579
537
start = outfile.tell()
581
zipfile = gzip.GzipFile(None, mode='ab', fileobj=outfile)
539
zipfile = GzipFile(None, mode='ab', fileobj=outfile)
582
540
zipfile.writelines(itertools.chain(
583
541
['version %s\n' % version_id], diff.to_patch()))
636
594
while len(pending_reqs) > 0:
637
595
req_version_id, req_start, req_end = pending_reqs.pop()
638
596
# lazily allocate cursors for versions
639
if req_version_id in self.lines:
640
lines.extend(self.lines[req_version_id][req_start:req_end])
643
598
start, end, kind, data, iterator = self.cursor[req_version_id]
676
631
def gzip_string(lines):
678
data_file = gzip.GzipFile(None, mode='wb', fileobj=sio)
633
data_file = GzipFile(None, mode='wb', fileobj=sio)
679
634
data_file.writelines(lines)
680
635
data_file.close()
681
636
return sio.getvalue()