70
70
def __repr__(self):
71
71
return "<Revision id %s>" % self.revision_id
75
from bzrlib.xml import Element, SubElement
77
root = Element('revision',
78
committer = self.committer,
79
timestamp = '%.9f' % self.timestamp,
80
revision_id = self.revision_id,
81
inventory_id = self.inventory_id,
82
inventory_sha1 = self.inventory_sha1,
85
root.set('timezone', str(self.timezone))
88
msg = SubElement(root, 'message')
89
msg.text = self.message
93
pelts = SubElement(root, 'parents')
94
pelts.tail = pelts.text = '\n'
95
for rr in self.parents:
96
assert isinstance(rr, RevisionReference)
97
p = SubElement(pelts, 'revision_ref')
100
p.set('revision_id', rr.revision_id)
102
p.set('revision_sha1', rr.revision_sha1)
107
def from_element(cls, elt):
108
return unpack_revision(elt)
110
from_element = classmethod(from_element)
114
def unpack_revision(elt):
115
"""Convert XML element into Revision object."""
116
# <changeset> is deprecated...
117
if elt.tag not in ('revision', 'changeset'):
118
raise bzrlib.errors.BzrError("unexpected tag in revision file: %r" % elt)
120
rev = Revision(committer = elt.get('committer'),
121
timestamp = float(elt.get('timestamp')),
122
revision_id = elt.get('revision_id'),
123
inventory_id = elt.get('inventory_id'),
124
inventory_sha1 = elt.get('inventory_sha1')
127
precursor = elt.get('precursor')
128
precursor_sha1 = elt.get('precursor_sha1')
130
pelts = elt.find('parents')
134
assert p.tag == 'revision_ref', \
135
"bad parent node tag %r" % p.tag
136
rev_ref = RevisionReference(p.get('revision_id'),
137
p.get('revision_sha1'))
138
rev.parents.append(rev_ref)
142
prec_parent = rev.parents[0].revision_id
143
assert prec_parent == precursor
145
# revisions written prior to 0.0.5 have a single precursor
146
# give as an attribute
147
rev_ref = RevisionReference(precursor, precursor_sha1)
148
rev.parents.append(rev_ref)
150
v = elt.get('timezone')
151
rev.timezone = v and int(v)
153
rev.message = elt.findtext('message') # text of <message>
73
def __eq__(self, other):
74
if not isinstance(other, Revision):
76
return (self.inventory_id == other.inventory_id
77
and self.inventory_sha1 == other.inventory_sha1
78
and self.revision_id == other.revision_id
79
and self.timestamp == other.timestamp
80
and self.message == other.message
81
and self.timezone == other.timezone
82
and self.committer == other.committer)
84
def __ne__(self, other):
85
return not self.__eq__(other)
158
89
REVISION_ID_RE = None
259
190
raise bzrlib.errors.AmbiguousBase((a_closest[0], b_closest[0]))
260
191
return a_closest[0]
262
def revision_graph(revision, revision_source):
263
"""Produce a graph of the ancestry of the specified revision.
264
Return root, ancestors map, descendants map
266
TODO: Produce graphs with the NULL revision as root, so that we can find
267
a common even when trees are not branches don't represent a single line
274
descendants[revision] = {}
275
while len(lines) > 0:
279
rev = revision_source.get_revision(line)
280
parents = [p.revision_id for p in rev.parents]
281
if len(parents) == 0:
283
except bzrlib.errors.NoSuchRevision:
285
if parents is not None:
286
for parent in parents:
287
if parent not in ancestors:
288
new_lines.add(parent)
289
if parent not in descendants:
290
descendants[parent] = {}
291
descendants[parent][line] = 1
292
if parents is not None:
293
ancestors[line] = set(parents)
295
assert root not in descendants[root]
296
assert root not in ancestors[root]
297
return root, ancestors, descendants
299
def combined_graph(revision_a, revision_b, revision_source):
300
"""Produce a combined ancestry graph.
301
Return graph root, ancestors map, descendants map, set of common nodes"""
302
root, ancestors, descendants = revision_graph(revision_a, revision_source)
303
root_b, ancestors_b, descendants_b = revision_graph(revision_b,
305
assert root == root_b
307
for node, node_anc in ancestors_b.iteritems():
308
if node in ancestors:
311
ancestors[node] = set()
312
ancestors[node].update(node_anc)
313
for node, node_dec in descendants_b.iteritems():
314
if node not in descendants:
315
descendants[node] = set()
316
descendants[node].update(node_dec)
317
return root, ancestors, descendants, common
319
def common_ancestor(revision_a, revision_b, revision_source):
320
root, ancestors, descendants, common = \
321
combined_graph(revision_a, revision_b, revision_source)
322
nodes = farthest_nodes(descendants, ancestors, root)
327
193
class MultipleRevisionSources(object):
328
194
"""Proxy that looks in multiple branches for revisions."""
329
195
def __init__(self, *args):