20
class RevisionReference(object):
20
from xml import XMLMixin
23
from cElementTree import Element, ElementTree, SubElement
25
from elementtree.ElementTree import Element, ElementTree, SubElement
27
from errors import BzrError
30
class RevisionReference:
22
32
Reference to a stored revision.
27
37
revision_sha1 = None
28
def __init__(self, revision_id, revision_sha1=None):
38
def __init__(self, revision_id, revision_sha1):
29
39
if revision_id == None \
30
40
or isinstance(revision_id, basestring):
31
41
self.revision_id = revision_id
49
59
into the file it describes.
51
61
After bzr 0.0.5 revisions are allowed to have multiple parents.
62
To support old clients this is written out in a slightly redundant
63
form: the first parent as the predecessor. This will eventually
54
67
List of parent revisions, each is a RevisionReference.
65
78
self.__dict__.update(args)
81
def _get_precursor(self):
82
from warnings import warn
83
warn("Revision.precursor is deprecated", stacklevel=2)
85
return self.parents[0].revision_id
90
def _get_precursor_sha1(self):
91
from warnings import warn
92
warn("Revision.precursor_sha1 is deprecated", stacklevel=2)
94
return self.parents[0].revision_sha1
100
raise Exception("can't assign to precursor anymore")
103
precursor = property(_get_precursor, _fail, _fail)
104
precursor_sha1 = property(_get_precursor_sha1, _fail, _fail)
69
108
def __repr__(self):
70
109
return "<Revision id %s>" % self.revision_id
73
112
def to_element(self):
74
from bzrlib.xml import Element, SubElement
76
113
root = Element('revision',
77
114
committer = self.committer,
78
115
timestamp = '%.9f' % self.timestamp,
129
# first parent stored as precursor for compatability with 0.0.5 and
132
assert pr.revision_id
133
root.set('precursor', pr.revision_id)
135
root.set('precursor_sha1', pr.revision_sha1)
92
138
pelts = SubElement(root, 'parents')
93
139
pelts.tail = pelts.text = '\n'
94
140
for rr in self.parents:
113
159
def unpack_revision(elt):
114
160
"""Convert XML element into Revision object."""
115
161
# <changeset> is deprecated...
116
from bzrlib.errors import BzrError
118
162
if elt.tag not in ('revision', 'changeset'):
119
163
raise BzrError("unexpected tag in revision file: %r" % elt)
131
175
pelts = elt.find('parents')
178
# revisions written prior to 0.0.5 have a single precursor
179
# give as an attribute
180
rev_ref = RevisionReference(precursor, precursor_sha1)
181
rev.parents.append(rev_ref)
135
184
assert p.tag == 'revision_ref', \
136
185
"bad parent node tag %r" % p.tag
138
187
p.get('revision_sha1'))
139
188
rev.parents.append(rev_ref)
143
prec_parent = rev.parents[0].revision_id
144
assert prec_parent == precursor
146
# revisions written prior to 0.0.5 have a single precursor
147
# give as an attribute
148
rev_ref = RevisionReference(precursor, precursor_sha1)
149
rev.parents.append(rev_ref)
151
190
v = elt.get('timezone')
152
191
rev.timezone = v and int(v)
154
193
rev.message = elt.findtext('message') # text of <message>
159
REVISION_ID_RE = None
161
def validate_revision_id(rid):
162
"""Check rid is syntactically valid for a revision id."""
163
global REVISION_ID_RE
164
if not REVISION_ID_RE:
166
REVISION_ID_RE = re.compile('[\w.-]+@[\w.-]+--?\d+--?[0-9a-f]+\Z')
168
if not REVISION_ID_RE.match(rid):
169
raise ValueError("malformed revision-id %r" % rid)
171
def is_ancestor(revision_id, candidate_id, revision_source):
172
"""Return true if candidate_id is an ancestor of revision_id.
173
A false negative will be returned if any intermediate descendent of
174
candidate_id is not present in any of the revision_sources.
176
revisions_source is an object supporting a get_revision operation that
177
behaves like Branch's.
180
from bzrlib.branch import NoSuchRevision
181
ancestors = (revision_id,)
182
while len(ancestors) > 0:
184
for ancestor in ancestors:
185
if ancestor == candidate_id:
188
revision = revision_source.get_revision(ancestor)
189
except NoSuchRevision, e:
190
if e.revision == revision_id:
194
new_ancestors.extend([p.revision_id for p in revision.parents])
195
ancestors = new_ancestors
198
class MultipleRevisionSources(object):
199
def __init__(self, *args):
200
object.__init__(self)
201
assert len(args) != 0
202
self._revision_sources = args
204
def get_revision(self, revision_id):
205
from bzrlib.branch import NoSuchRevision
206
for source in self._revision_sources:
208
return source.get_revision(revision_id)
209
except NoSuchRevision, e: