1
# Copyright (C) 2007 Canonical Ltd
1
# Copyright (C) 2007-2010 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
17
from bzrlib.lazy_import import lazy_import
23
23
from StringIO import StringIO
25
25
from bzrlib import (
30
from bzrlib.util import bencode
31
from bzrlib import bencode
32
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)
35
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):
39
versions = vf.versions()
40
51
def pending_parents(version):
41
return [v for v in vf.get_parents(version) if v in versions and
52
if parents[version] is None:
54
return [v for v in parents[version] if v in versions and
43
56
for version_id in versions:
44
for parent_id in vf.get_parents(version_id):
57
if parents[version_id] is None:
60
for parent_id in parents[version_id]:
45
61
descendants.setdefault(parent_id, []).append(version_id)
46
62
cur = [v for v in versions if len(pending_parents(v)) == 0]
47
63
while len(cur) > 0:
56
72
seen.add(version_id)
58
assert len(seen) == len(versions)
61
76
class MultiParent(object):
62
77
"""A multi-parent diff"""
64
81
def __init__(self, hunks=None):
65
82
if hunks is not None:
193
210
elif cur_line[0] == '\n':
194
211
hunks[-1].lines[-1] += '\n'
196
assert cur_line[0] == 'c', cur_line[0]
213
if not (cur_line[0] == 'c'):
214
raise AssertionError(cur_line[0])
197
215
parent, parent_pos, child_pos, num_lines =\
198
216
[int(v) for v in cur_line.split(' ')[1:]]
199
217
hunks.append(ParentText(parent, parent_pos, child_pos,
263
283
class ParentText(object):
264
284
"""A reference to text present in a parent text"""
286
__slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
266
288
def __init__(self, parent, parent_pos, child_pos, num_lines):
267
289
self.parent = parent
268
290
self.parent_pos = parent_pos
269
291
self.child_pos = child_pos
270
292
self.num_lines = num_lines
295
return dict(parent=self.parent, parent_pos=self.parent_pos,
296
child_pos=self.child_pos, num_lines=self.num_lines)
272
298
def __repr__(self):
273
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
274
' %(num_lines)r)' % self.__dict__
299
return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
300
' %(num_lines)r)' % self._as_dict())
276
302
def __eq__(self, other):
277
if self.__class__ != other.__class__:
303
if self.__class__ is not other.__class__:
279
return (self.__dict__ == other.__dict__)
305
return self._as_dict() == other._as_dict()
281
307
def to_patch(self):
282
yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
308
yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
286
312
class BaseVersionedFile(object):
300
326
return version in self._parents
302
328
def do_snapshot(self, version_id, parent_ids):
303
"""Determine whether to perform a a snapshot for this version"""
329
"""Determine whether to perform a snapshot for this version"""
304
330
if self.snapshot_interval is None:
306
332
if self.max_snapshots is not None and\
368
394
:param single_parent: If true, omit all but one parent text, (but
369
395
retain parent metadata).
371
assert no_cache or not verify
397
if not (no_cache or not verify):
372
399
revisions = set(vf.versions())
373
400
total = len(revisions)
374
401
pb = ui.ui_factory.nested_progress_bar()
392
419
self.clear_cache()
395
assert lines == self.get_line_list([revision])[0]
422
if not (lines == self.get_line_list([revision])[0]):
423
raise AssertionError()
396
424
self.clear_cache()
397
425
pb.update('Importing revisions',
398
426
(total - len(revisions)) + len(added), total)
508
536
self._parents[version_id] = parent_ids
510
538
def get_diff(self, version_id):
511
return self._diffs[version_id]
540
return self._diffs[version_id]
542
raise errors.RevisionNotPresent(version_id, self)
513
544
def destroy(self):
540
571
def add_diff(self, diff, version_id, parent_ids):
541
572
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
543
577
start = outfile.tell()
545
579
zipfile = GzipFile(None, mode='ab', fileobj=outfile)