298
319
if new_rev != old_rev:
299
320
action = Action('modified', [ie.kind,
300
321
new_tree.id2path(ie.file_id)])
301
action.add_property('last-changed', ie.revision)
322
action.add_utf8_property('last-changed', ie.revision)
302
323
action.write(self.to_file)
326
class BundleReader(object):
327
"""This class reads in a bundle from a file, and returns
328
a Bundle object, which can then be applied against a tree.
330
def __init__(self, from_file):
331
"""Read in the bundle from the file.
333
:param from_file: A file-like object (must have iterator support).
335
object.__init__(self)
336
self.from_file = iter(from_file)
337
self._next_line = None
339
self.info = self._get_info()
340
# We put the actual inventory ids in the footer, so that the patch
341
# is easier to read for humans.
342
# Unfortunately, that means we need to read everything before we
343
# can create a proper bundle.
348
return BundleInfo08()
352
while self._next_line is not None:
353
if not self._read_revision_header():
355
if self._next_line is None:
361
"""Make sure that the information read in makes sense
362
and passes appropriate checksums.
364
# Fill in all the missing blanks for the revisions
365
# and generate the real_revisions list.
366
self.info.complete_info()
369
"""yield the next line, but secretly
370
keep 1 extra line for peeking.
372
for line in self.from_file:
373
last = self._next_line
374
self._next_line = line
376
#mutter('yielding line: %r' % last)
378
last = self._next_line
379
self._next_line = None
380
#mutter('yielding line: %r' % last)
383
def _read_revision_header(self):
384
found_something = False
385
self.info.revisions.append(RevisionInfo(None))
386
for line in self._next():
387
# The bzr header is terminated with a blank line
388
# which does not start with '#'
389
if line is None or line == '\n':
391
if not line.startswith('#'):
393
found_something = True
394
self._handle_next(line)
395
if not found_something:
396
# Nothing was there, so remove the added revision
397
self.info.revisions.pop()
398
return found_something
400
def _read_next_entry(self, line, indent=1):
401
"""Read in a key-value pair
403
if not line.startswith('#'):
404
raise errors.MalformedHeader('Bzr header did not start with #')
405
line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
406
if line[:indent] == ' '*indent:
409
return None, None# Ignore blank lines
411
loc = line.find(': ')
416
value = self._read_many(indent=indent+2)
417
elif line[-1:] == ':':
419
value = self._read_many(indent=indent+2)
421
raise errors.MalformedHeader('While looking for key: value pairs,'
422
' did not find the colon %r' % (line))
424
key = key.replace(' ', '_')
425
#mutter('found %s: %s' % (key, value))
428
def _handle_next(self, line):
431
key, value = self._read_next_entry(line, indent=1)
432
mutter('_handle_next %r => %r' % (key, value))
436
revision_info = self.info.revisions[-1]
437
if key in revision_info.__dict__:
438
if getattr(revision_info, key) is None:
439
if key in ('file_id', 'revision_id', 'base_id'):
440
value = value.encode('utf8')
441
elif key in ('parent_ids'):
442
value = [v.encode('utf8') for v in value]
443
setattr(revision_info, key, value)
445
raise errors.MalformedHeader('Duplicated Key: %s' % key)
447
# What do we do with a key we don't recognize
448
raise errors.MalformedHeader('Unknown Key: "%s"' % key)
450
def _read_many(self, indent):
451
"""If a line ends with no entry, that means that it should be
452
followed with multiple lines of values.
454
This detects the end of the list, because it will be a line that
455
does not start properly indented.
458
start = '#' + (' '*indent)
460
if self._next_line is None or self._next_line[:len(start)] != start:
463
for line in self._next():
464
values.append(line[len(start):-1].decode('utf-8'))
465
if self._next_line is None or self._next_line[:len(start)] != start:
469
def _read_one_patch(self):
470
"""Read in one patch, return the complete patch, along with
473
:return: action, lines, do_continue
475
#mutter('_read_one_patch: %r' % self._next_line)
476
# Peek and see if there are no patches
477
if self._next_line is None or self._next_line.startswith('#'):
478
return None, [], False
482
for line in self._next():
484
if not line.startswith('==='):
485
raise errors.MalformedPatches('The first line of all patches'
486
' should be a bzr meta line "==="'
488
action = line[4:-1].decode('utf-8')
489
elif line.startswith('... '):
490
action += line[len('... '):-1].decode('utf-8')
492
if (self._next_line is not None and
493
self._next_line.startswith('===')):
494
return action, lines, True
495
elif self._next_line is None or self._next_line.startswith('#'):
496
return action, lines, False
500
elif not line.startswith('... '):
503
return action, lines, False
505
def _read_patches(self):
507
revision_actions = []
509
action, lines, do_continue = self._read_one_patch()
510
if action is not None:
511
revision_actions.append((action, lines))
512
assert self.info.revisions[-1].tree_actions is None
513
self.info.revisions[-1].tree_actions = revision_actions
515
def _read_footer(self):
516
"""Read the rest of the meta information.
518
:param first_line: The previous step iterates past what it
519
can handle. That extra line is given here.
521
for line in self._next():
522
self._handle_next(line)
523
if self._next_line is None:
525
if not self._next_line.startswith('#'):
526
# Consume the trailing \n and stop processing
531
class BundleInfo08(BundleInfo):
533
def _update_tree(self, bundle_tree, revision_id):
534
bundle_tree.note_last_changed('', revision_id)
535
BundleInfo._update_tree(self, bundle_tree, revision_id)
537
def _testament_sha1_from_revision(self, repository, revision_id):
538
testament = StrictTestament.from_revision(repository, revision_id)
539
return testament.as_sha1()
541
def _testament_sha1(self, revision, inventory):
542
return StrictTestament(revision, inventory).as_sha1()