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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from bzrlib.lazy_import import lazy_import
17
from __future__ import absolute_import
22
from .lazy_import import lazy_import
19
24
lazy_import(globals(), """
23
from StringIO import StringIO
31
from bzrlib import bencode
33
from bzrlib.tuned_gzip import GzipFile
36
41
def topo_iter_keys(vf, keys=None):
162
169
"""Contruct a fulltext from this diff and its parents"""
163
170
mpvf = MultiMemoryVersionedFile()
164
171
for num, parent in enumerate(parents):
165
mpvf.add_version(StringIO(parent).readlines(), num, [])
166
mpvf.add_diff(self, 'a', range(len(parents)))
172
mpvf.add_version(BytesIO(parent).readlines(), num, [])
173
mpvf.add_diff(self, 'a', list(range(len(parents))))
167
174
return mpvf.get_line_list(['a'])[0]
170
177
def from_texts(cls, text, parents=()):
171
178
"""Produce a MultiParent from a text and list of parent text"""
172
return cls.from_lines(StringIO(text).readlines(),
173
[StringIO(p).readlines() for p in parents])
179
return cls.from_lines(BytesIO(text).readlines(),
180
[BytesIO(p).readlines() for p in parents])
175
182
def to_patch(self):
176
183
"""Yield text lines for a patch"""
188
195
def from_patch(cls, text):
189
196
"""Create a MultiParent from its string form"""
190
return cls._from_patch(StringIO(text))
197
return cls._from_patch(BytesIO(text))
193
200
def _from_patch(lines):
200
cur_line = line_iter.next()
207
cur_line = next(line_iter)
201
208
except StopIteration:
203
210
if cur_line[0] == 'i':
204
211
num_lines = int(cur_line.split(' ')[1])
205
hunk_lines = [line_iter.next() for x in xrange(num_lines)]
212
hunk_lines = [next(line_iter) for _ in range(num_lines)]
206
213
hunk_lines[-1] = hunk_lines[-1][:-1]
207
214
hunks.append(NewText(hunk_lines))
208
215
elif cur_line[0] == '\n':
279
288
class ParentText(object):
280
289
"""A reference to text present in a parent text"""
291
__slots__ = ['parent', 'parent_pos', 'child_pos', 'num_lines']
282
293
def __init__(self, parent, parent_pos, child_pos, num_lines):
283
294
self.parent = parent
284
295
self.parent_pos = parent_pos
285
296
self.child_pos = child_pos
286
297
self.num_lines = num_lines
300
return dict(parent=self.parent, parent_pos=self.parent_pos,
301
child_pos=self.child_pos, num_lines=self.num_lines)
288
303
def __repr__(self):
289
return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
290
' %(num_lines)r)' % self.__dict__
304
return ('ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'
305
' %(num_lines)r)' % self._as_dict())
292
307
def __eq__(self, other):
293
308
if self.__class__ is not other.__class__:
295
return (self.__dict__ == other.__dict__)
310
return self._as_dict() == other._as_dict()
297
312
def to_patch(self):
298
yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
313
yield ('c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'
302
317
class BaseVersionedFile(object):
388
403
raise ValueError()
389
404
revisions = set(vf.versions())
390
405
total = len(revisions)
391
pb = ui.ui_factory.nested_progress_bar()
406
with ui.ui_factory.nested_progress_bar() as pb:
393
407
while len(revisions) > 0:
395
409
for revision in revisions:
412
426
if not (lines == self.get_line_list([revision])[0]):
413
427
raise AssertionError()
414
428
self.clear_cache()
415
pb.update('Importing revisions',
429
pb.update(gettext('Importing revisions'),
416
430
(total - len(revisions)) + len(added), total)
417
431
revisions = [r for r in revisions if r not in added]
421
433
def select_snapshots(self, vf):
422
434
"""Determine which versions to add as snapshots"""
546
558
def get_diff(self, version_id):
547
559
start, count = self._diff_offset[version_id]
548
infile = open(self._filename + '.mpknit', 'rb')
560
with open(self._filename + '.mpknit', 'rb') as infile:
550
561
infile.seek(start)
551
sio = StringIO(infile.read(count))
554
zip_file = GzipFile(None, mode='rb', fileobj=sio)
562
sio = BytesIO(infile.read(count))
563
with gzip.GzipFile(None, mode='rb', fileobj=sio) as zip_file:
556
564
file_version_id = zip_file.readline()
557
return MultiParent.from_patch(zip_file.read())
565
content = zip_file.read()
566
return MultiParent.from_patch(content)
561
568
def add_diff(self, diff, version_id, parent_ids):
562
outfile = open(self._filename + '.mpknit', 'ab')
569
with open(self._filename + '.mpknit', 'ab') as outfile:
564
570
outfile.seek(0, 2) # workaround for windows bug:
565
571
# .tell() for files opened in 'ab' mode
566
572
# before any write returns 0
567
573
start = outfile.tell()
569
zipfile = GzipFile(None, mode='ab', fileobj=outfile)
574
with gzip.GzipFile(None, mode='ab', fileobj=outfile) as zipfile:
570
575
zipfile.writelines(itertools.chain(
571
576
['version %s\n' % version_id], diff.to_patch()))
574
577
end = outfile.tell()
577
578
self._diff_offset[version_id] = (start, end-start)
578
579
self._parents[version_id] = parent_ids
580
581
def destroy(self):
582
583
os.unlink(self._filename + '.mpknit')
584
585
if e.errno != errno.ENOENT:
587
588
os.unlink(self._filename + '.mpidx')
589
590
if e.errno != errno.ENOENT:
631
632
start, end, kind, data, iterator = self.cursor[req_version_id]
633
634
iterator = self.diffs.get_diff(req_version_id).range_iterator()
634
start, end, kind, data = iterator.next()
635
start, end, kind, data = next(iterator)
635
636
if start > req_start:
636
637
iterator = self.diffs.get_diff(req_version_id).range_iterator()
637
start, end, kind, data = iterator.next()
638
start, end, kind, data = next(iterator)
639
640
# find the first hunk relevant to the request
640
641
while end <= req_start:
641
start, end, kind, data = iterator.next()
642
start, end, kind, data = next(iterator)
642
643
self.cursor[req_version_id] = start, end, kind, data, iterator
643
644
# if the hunk can't satisfy the whole request, split it in two,
644
645
# and leave the second half for later.
664
665
def gzip_string(lines):
666
data_file = GzipFile(None, mode='wb', fileobj=sio)
667
data_file.writelines(lines)
667
with gzip.GzipFile(None, mode='wb', fileobj=sio) as data_file:
668
data_file.writelines(lines)
669
669
return sio.getvalue()