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