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:
211
193
elif cur_line[0] == '\n':
212
194
hunks[-1].lines[-1] += '\n'
214
if not (cur_line[0] == 'c'):
215
raise AssertionError(cur_line[0])
196
assert cur_line[0] == 'c', cur_line[0]
216
197
parent, parent_pos, child_pos, num_lines =\
217
198
[int(v) for v in cur_line.split(' ')[1:]]
218
199
hunks.append(ParentText(parent, parent_pos, child_pos,
284
263
class ParentText(object):
285
264
"""A reference to text present in a parent text"""
287
__slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
289
266
def __init__(self, parent, parent_pos, child_pos, num_lines):
290
267
self.parent = parent
291
268
self.parent_pos = parent_pos
292
269
self.child_pos = child_pos
293
270
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
272
def __repr__(self):
300
return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
301
' %(num_lines)r)' % self._as_dict())
273
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
274
' %(num_lines)r)' % self.__dict__
303
276
def __eq__(self, other):
304
if self.__class__ is not other.__class__:
277
if self.__class__ != other.__class__:
306
return self._as_dict() == other._as_dict()
279
return (self.__dict__ == other.__dict__)
308
281
def to_patch(self):
309
yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
282
yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
313
286
class BaseVersionedFile(object):
327
300
return version in self._parents
329
302
def do_snapshot(self, version_id, parent_ids):
330
"""Determine whether to perform a snapshot for this version"""
303
"""Determine whether to perform a a snapshot for this version"""
331
304
if self.snapshot_interval is None:
333
306
if self.max_snapshots is not None and\
395
368
:param single_parent: If true, omit all but one parent text, (but
396
369
retain parent metadata).
398
if not (no_cache or not verify):
371
assert no_cache or not verify
400
372
revisions = set(vf.versions())
401
373
total = len(revisions)
402
374
pb = ui.ui_factory.nested_progress_bar()
420
392
self.clear_cache()
423
if not (lines == self.get_line_list([revision])[0]):
424
raise AssertionError()
395
assert lines == self.get_line_list([revision])[0]
425
396
self.clear_cache()
426
pb.update(gettext('Importing revisions'),
397
pb.update('Importing revisions',
427
398
(total - len(revisions)) + len(added), total)
428
399
revisions = [r for r in revisions if r not in added]
537
508
self._parents[version_id] = parent_ids
539
510
def get_diff(self, version_id):
541
return self._diffs[version_id]
543
raise errors.RevisionNotPresent(version_id, self)
511
return self._diffs[version_id]
545
513
def destroy(self):
562
530
sio = StringIO(infile.read(count))
565
zip_file = gzip.GzipFile(None, mode='rb', fileobj=sio)
533
zip_file = GzipFile(None, mode='rb', fileobj=sio)
567
535
file_version_id = zip_file.readline()
568
content = zip_file.read()
569
return MultiParent.from_patch(content)
536
return MultiParent.from_patch(zip_file.read())
578
545
# before any write returns 0
579
546
start = outfile.tell()
581
zipfile = gzip.GzipFile(None, mode='ab', fileobj=outfile)
548
zipfile = GzipFile(None, mode='ab', fileobj=outfile)
582
549
zipfile.writelines(itertools.chain(
583
550
['version %s\n' % version_id], diff.to_patch()))
676
643
def gzip_string(lines):
678
data_file = gzip.GzipFile(None, mode='wb', fileobj=sio)
645
data_file = GzipFile(None, mode='wb', fileobj=sio)
679
646
data_file.writelines(lines)
680
647
data_file.close()
681
648
return sio.getvalue()