1
from StringIO import StringIO
8
revision as _mod_revision,
12
from bzrlib.bundle import serializer as bundle_serializer
15
class MergeDirective(object):
17
_format_string = 'Bazaar merge directive format experimental-1'
19
def __init__(self, revision_id, testament_sha1, time, timezone,
20
target_branch, patch=None, patch_type=None,
22
assert isinstance(time, float)
23
assert patch_type in (None, 'diff', 'bundle')
24
if patch_type != 'bundle' and source_branch is None:
25
raise errors.NoMergeSource()
26
if patch_type is not None and patch is None:
27
raise errors.PatchMissing(patch_type)
28
self.revision_id = revision_id
29
self.testament_sha1 = testament_sha1
31
self.timezone = timezone
32
self.target_branch = target_branch
34
self.patch_type = patch_type
35
self.source_branch = source_branch
38
def from_lines(klass, lines):
39
assert lines[0].startswith('# ' + klass._format_string + '\n')
40
line_iter = iter(lines[1:])
41
stanza = rio.read_patch_stanza(line_iter)
42
patch_lines = list(line_iter)
43
if len(patch_lines) == 0:
46
patch = ''.join(patch_lines)
48
bundle_serializer.read_bundle(StringIO(patch))
49
except errors.NotABundle:
53
time, timezone = bundle_serializer.unpack_highres_date(
54
stanza.get('timestamp'))
56
for key in ('revision_id', 'testament_sha1', 'target_branch',
59
kwargs[key] = stanza.get(key)
62
return MergeDirective(time=time, timezone=timezone,
63
patch_type=patch_type, patch=patch, **kwargs)
66
timestamp = bundle_serializer.format_highres_date(self.time,
68
stanza = rio.Stanza(revision_id=self.revision_id, timestamp=timestamp,
69
target_branch=self.target_branch,
70
testament_sha1=self.testament_sha1)
71
for key in ('source_branch',):
72
if self.__dict__[key] is not None:
73
stanza.add(key, self.__dict__[key])
74
lines = ['# ' + self._format_string + '\n']
75
lines.extend(rio.to_patch_lines(stanza))
77
if self.patch is not None:
78
lines.extend(self.patch.splitlines(True))
81
def to_signed(self, branch):
82
my_gpg = gpg.GPGStrategy(branch.get_config())
83
return my_gpg.sign(''.join(self.to_lines()))
86
def from_objects(klass, repository, revision_id, time, timezone,
87
target_branch, patch_type='bundle',
88
local_target_branch=None, public_branch=None):
89
if public_branch is not None:
90
source_branch = public_branch.base
91
if not public_branch.repository.has_revision(revision_id):
92
raise errors.PublicBranchOutOfDate(source_branch,
96
t = testament.StrictTestament3.from_revision(repository, revision_id)
97
if patch_type is None:
100
submit_branch = _mod_branch.Branch.open(target_branch)
101
submit_revision_id = submit_branch.last_revision()
102
repository.fetch(submit_branch.repository, submit_revision_id)
103
ancestor_id = _mod_revision.common_ancestor(revision_id,
106
if patch_type == 'bundle':
108
bundle_serializer.write_bundle(repository, revision_id,
111
elif patch_type == 'diff':
112
patch = klass._generate_diff(repository, revision_id,
114
return MergeDirective(revision_id, t.as_sha1(), time, timezone,
115
target_branch, patch, patch_type, source_branch)
118
def _generate_diff(repository, revision_id, ancestor_id):
119
tree_1 = repository.revision_tree(ancestor_id)
120
tree_2 = repository.revision_tree(revision_id)
122
diff.show_diff_trees(tree_1, tree_2, s)