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