1
from StringIO import StringIO
7
revision as _mod_revision,
11
from bzrlib.bundle import serializer as bundle_serializer
14
class MergeDirective(object):
16
def __init__(self, revision_id, testament_sha1, time, timezone,
17
submit_location, patch=None, patch_type=None,
18
public_location=None):
19
assert patch_type in (None, 'diff', 'bundle')
20
if patch_type != 'bundle' and public_location is None:
21
raise errors.NoMergeSource()
22
if patch_type is not None and patch is None:
23
raise errors.PatchMissing(patch_type)
24
self.revision_id = revision_id
25
self.testament_sha1 = testament_sha1
27
self.timezone = timezone
28
self.submit_location = submit_location
30
self.patch_type = patch_type
31
self.public_location = public_location
34
def from_lines(lines):
35
line_iter = iter(lines)
36
stanza = rio.read_patch_stanza(line_iter)
37
patch_lines = list(line_iter)
38
if len(patch_lines) == 0:
41
patch = ''.join(patch_lines)
43
bundle_serializer.read_bundle(StringIO(patch))
44
except errors.NotABundle:
48
time, timezone = bundle_serializer.unpack_highres_date(
49
stanza.get('timestamp'))
51
for key in ('revision_id', 'testament_sha1', 'submit_location',
54
kwargs[key] = stanza.get(key)
57
return MergeDirective(time=time, timezone=timezone,
58
patch_type=patch_type, patch=patch, **kwargs)
61
timestamp = bundle_serializer.format_highres_date(self.time,
63
stanza = rio.Stanza(revision_id=self.revision_id, timestamp=timestamp,
64
submit_location=self.submit_location,
65
testament_sha1=self.testament_sha1)
66
for key in ('public_location',):
67
if self.__dict__[key] is not None:
68
stanza.add(key, self.__dict__[key])
69
lines = rio.to_patch_lines(stanza)
71
if self.patch is not None:
72
lines.extend(self.patch.splitlines(True))
76
def from_objects(klass, repository, revision_id, time, timezone,
77
submit_location, patch_type='bundle',
78
local_submit_location=None, public_branch=None):
79
if public_branch is not None:
80
public_location = public_branch.base
81
if not public_branch.repository.has_revision(revision_id):
82
raise errors.PublicBranchOutOfDate(public_location,
85
public_location = None
86
t = testament.StrictTestament3.from_revision(repository, revision_id)
87
if patch_type is None:
90
submit_branch = _mod_branch.Branch.open(submit_location)
91
submit_revision_id = submit_branch.last_revision()
92
repository.fetch(submit_branch.repository, submit_revision_id)
93
ancestor_id = _mod_revision.common_ancestor(revision_id,
96
if patch_type == 'bundle':
98
bundle_serializer.write_bundle(repository, revision_id,
101
elif patch_type == 'diff':
102
patch = klass._generate_diff(repository, revision_id,
104
return MergeDirective(revision_id, t.as_sha1(), time, timezone,
105
submit_location, patch, patch_type,
109
def _generate_diff(repository, revision_id, ancestor_id):
110
tree_1 = repository.revision_tree(ancestor_id)
111
tree_2 = repository.revision_tree(revision_id)
113
diff.show_diff_trees(tree_1, tree_2, s)