18
18
import bzrlib.errors
19
19
from bzrlib.graph import node_distances, select_farthest, all_descendants
20
from bzrlib.osutils import contains_whitespace
22
21
NULL_REVISION="null:"
23
class RevisionReference(object):
25
Reference to a stored revision.
27
Includes the revision_id and revision_sha1.
31
def __init__(self, revision_id, revision_sha1=None):
32
if revision_id == None \
33
or isinstance(revision_id, basestring):
34
self.revision_id = revision_id
36
raise ValueError('bad revision_id %r' % revision_id)
38
if revision_sha1 != None:
39
if isinstance(revision_sha1, basestring) \
40
and len(revision_sha1) == 40:
41
self.revision_sha1 = revision_sha1
43
raise ValueError('bad revision_sha1 %r' % revision_sha1)
24
47
class Revision(object):
25
48
"""Single revision on a branch.
31
54
After bzr 0.0.5 revisions are allowed to have multiple parents.
34
List of parent revision_ids
37
Dictionary of revision properties. These are attached to the
38
revision as extra metadata. The name must be a single
39
word; the value can be an arbitrary string.
57
List of parent revisions, each is a RevisionReference.
42
def __init__(self, revision_id, properties=None, **args):
43
self.revision_id = revision_id
44
self.properties = properties or {}
45
self._check_properties()
67
def __init__(self, **args):
46
68
self.__dict__.update(args)
48
self.parent_sha1s = []
50
72
def __repr__(self):
51
73
return "<Revision id %s>" % self.revision_id
53
75
def __eq__(self, other):
54
76
if not isinstance(other, Revision):
56
# FIXME: rbc 20050930 parent_ids are not being compared
58
self.inventory_sha1 == other.inventory_sha1
78
return (self.inventory_id == other.inventory_id
79
and self.inventory_sha1 == other.inventory_sha1
59
80
and self.revision_id == other.revision_id
60
81
and self.timestamp == other.timestamp
61
82
and self.message == other.message
62
83
and self.timezone == other.timezone
63
and self.committer == other.committer
64
and self.properties == other.properties)
84
and self.committer == other.committer)
66
86
def __ne__(self, other):
67
87
return not self.__eq__(other)
69
def _check_properties(self):
70
"""Verify that all revision properties are OK.
72
for name, value in self.properties.iteritems():
73
if not isinstance(name, basestring) or contains_whitespace(name):
74
raise ValueError("invalid property name %r" % name)
75
if not isinstance(value, basestring):
76
raise ValueError("invalid property value %r for %r" %
80
def is_ancestor(revision_id, candidate_id, branch):
93
def validate_revision_id(rid):
94
"""Check rid is syntactically valid for a revision id."""
96
if not REVISION_ID_RE:
98
REVISION_ID_RE = re.compile('[\w.-]+@[\w.-]+--?\d+--?[0-9a-f]+\Z')
100
if not REVISION_ID_RE.match(rid):
101
raise ValueError("malformed revision-id %r" % rid)
103
def is_ancestor(revision_id, candidate_id, revision_source):
81
104
"""Return true if candidate_id is an ancestor of revision_id.
83
105
A false negative will be returned if any intermediate descendent of
84
106
candidate_id is not present in any of the revision_sources.
86
108
revisions_source is an object supporting a get_revision operation that
87
109
behaves like Branch's.
89
return candidate_id in branch.get_ancestry(revision_id)
111
if candidate_id is None:
113
for ancestor_id, distance in iter_ancestors(revision_id, revision_source):
114
if ancestor_id == candidate_id:
92
118
def iter_ancestors(revision_id, revision_source, only_present=False):
93
119
ancestors = (revision_id,)
210
236
assert root not in ancestors[root]
211
237
return root, ancestors, descendants
214
239
def combined_graph(revision_a, revision_b, revision_source):
215
240
"""Produce a combined ancestry graph.
216
241
Return graph root, ancestors map, descendants map, set of common nodes"""
228
253
ancestors[node].update(node_anc)
229
254
for node, node_dec in descendants_b.iteritems():
230
255
if node not in descendants:
231
descendants[node] = {}
256
descendants[node] = set()
232
257
descendants[node].update(node_dec)
233
258
return root, ancestors, descendants, common
236
260
def common_ancestor(revision_a, revision_b, revision_source):
238
262
root, ancestors, descendants, common = \