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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
from bzrlib.lazy_import import lazy_import
19
19
lazy_import(globals(), """
24
23
from StringIO import StringIO
26
25
from bzrlib import (
30
from bzrlib.util import bencode
35
def topo_iter_keys(vf, keys=None):
38
parents = vf.get_parent_map(keys)
39
return _topo_iter(parents, keys)
32
from bzrlib.tuned_gzip import GzipFile
41
35
def topo_iter(vf, versions=None):
42
38
if versions is None:
43
39
versions = vf.versions()
44
parents = vf.get_parent_map(versions)
45
return _topo_iter(parents, versions)
47
def _topo_iter(parents, versions):
50
40
def pending_parents(version):
51
if parents[version] is None:
53
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
55
43
for version_id in versions:
56
if parents[version_id] is None:
59
for parent_id in parents[version_id]:
44
for parent_id in vf.get_parents(version_id):
60
45
descendants.setdefault(parent_id, []).append(version_id)
61
46
cur = [v for v in versions if len(pending_parents(v)) == 0]
62
47
while len(cur) > 0:
71
56
seen.add(version_id)
58
assert len(seen) == len(versions)
75
61
class MultiParent(object):
76
62
"""A multi-parent diff"""
80
64
def __init__(self, hunks=None):
81
65
if hunks is not None:
152
136
diff.hunks.append(new_text)
155
def get_matching_blocks(self, parent, parent_len):
156
for hunk in self.hunks:
157
if not isinstance(hunk, ParentText) or hunk.parent != parent:
159
yield (hunk.parent_pos, hunk.child_pos, hunk.num_lines)
160
yield parent_len, self.num_lines(), 0
162
139
def to_lines(self, parents=()):
163
140
"""Contruct a fulltext from this diff and its parents"""
164
141
mpvf = MultiMemoryVersionedFile()
209
186
elif cur_line[0] == '\n':
210
187
hunks[-1].lines[-1] += '\n'
212
if not (cur_line[0] == 'c'):
213
raise AssertionError(cur_line[0])
189
assert cur_line[0] == 'c', cur_line[0]
214
190
parent, parent_pos, child_pos, num_lines =\
215
191
[int(v) for v in cur_line.split(' ')[1:]]
216
192
hunks.append(ParentText(parent, parent_pos, child_pos,
282
256
class ParentText(object):
283
257
"""A reference to text present in a parent text"""
285
__slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
287
259
def __init__(self, parent, parent_pos, child_pos, num_lines):
288
260
self.parent = parent
289
261
self.parent_pos = parent_pos
290
262
self.child_pos = child_pos
291
263
self.num_lines = num_lines
294
return dict(parent=self.parent, parent_pos=self.parent_pos,
295
child_pos=self.child_pos, num_lines=self.num_lines)
297
265
def __repr__(self):
298
return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
299
' %(num_lines)r)' % self._as_dict())
266
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
267
' %(num_lines)r)' % self.__dict__
301
269
def __eq__(self, other):
302
if self.__class__ is not other.__class__:
270
if self.__class__ != other.__class__:
304
return self._as_dict() == other._as_dict()
272
return (self.__dict__ == other.__dict__)
306
274
def to_patch(self):
307
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'\
311
279
class BaseVersionedFile(object):
325
293
return version in self._parents
327
295
def do_snapshot(self, version_id, parent_ids):
328
"""Determine whether to perform a snapshot for this version"""
296
"""Determine whether to perform a a snapshot for this version"""
329
297
if self.snapshot_interval is None:
331
299
if self.max_snapshots is not None and\
393
361
:param single_parent: If true, omit all but one parent text, (but
394
362
retain parent metadata).
396
if not (no_cache or not verify):
364
assert no_cache or not verify
398
365
revisions = set(vf.versions())
399
366
total = len(revisions)
400
367
pb = ui.ui_factory.nested_progress_bar()
418
385
self.clear_cache()
421
if not (lines == self.get_line_list([revision])[0]):
422
raise AssertionError()
388
assert lines == self.get_line_list([revision])[0]
423
389
self.clear_cache()
424
pb.update(gettext('Importing revisions'),
390
pb.update('Importing revisions',
425
391
(total - len(revisions)) + len(added), total)
426
392
revisions = [r for r in revisions if r not in added]
518
484
diff = self.get_diff(version_id)
520
reconstructor = _Reconstructor(self, self._lines, self._parents)
486
reconstructor = _Reconstructor(self, self._lines,
521
488
reconstructor.reconstruct_version(lines, version_id)
522
489
self._lines[version_id] = lines
535
502
self._parents[version_id] = parent_ids
537
504
def get_diff(self, version_id):
539
return self._diffs[version_id]
541
raise errors.RevisionNotPresent(version_id, self)
505
return self._diffs[version_id]
543
507
def destroy(self):
560
524
sio = StringIO(infile.read(count))
563
zip_file = gzip.GzipFile(None, mode='rb', fileobj=sio)
527
zip_file = GzipFile(None, mode='rb', fileobj=sio)
565
529
file_version_id = zip_file.readline()
566
content = zip_file.read()
567
return MultiParent.from_patch(content)
530
return MultiParent.from_patch(zip_file.read())
571
534
def add_diff(self, diff, version_id, parent_ids):
572
535
outfile = open(self._filename + '.mpknit', 'ab')
574
outfile.seek(0, 2) # workaround for windows bug:
575
# .tell() for files opened in 'ab' mode
576
# before any write returns 0
577
537
start = outfile.tell()
579
zipfile = gzip.GzipFile(None, mode='ab', fileobj=outfile)
539
zipfile = GzipFile(None, mode='ab', fileobj=outfile)
580
540
zipfile.writelines(itertools.chain(
581
541
['version %s\n' % version_id], diff.to_patch()))
634
594
while len(pending_reqs) > 0:
635
595
req_version_id, req_start, req_end = pending_reqs.pop()
636
596
# lazily allocate cursors for versions
637
if req_version_id in self.lines:
638
lines.extend(self.lines[req_version_id][req_start:req_end])
641
598
start, end, kind, data, iterator = self.cursor[req_version_id]
674
631
def gzip_string(lines):
676
data_file = gzip.GzipFile(None, mode='wb', fileobj=sio)
633
data_file = GzipFile(None, mode='wb', fileobj=sio)
677
634
data_file.writelines(lines)
678
635
data_file.close()
679
636
return sio.getvalue()