3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from xml import XMLMixin, Element, SubElement
22
from errors import BzrError
25
class RevisionReference:
27
Reference to a stored revision.
29
Includes the revision_id and revision_sha1.
33
def __init__(self, revision_id, revision_sha1):
34
if revision_id == None \
35
or isinstance(revision_id, basestring):
36
self.revision_id = revision_id
38
raise ValueError('bad revision_id %r' % revision_id)
40
if revision_sha1 != None:
41
if isinstance(revision_sha1, basestring) \
42
and len(revision_sha1) == 40:
43
self.revision_sha1 = revision_sha1
45
raise ValueError('bad revision_sha1 %r' % revision_sha1)
49
class Revision(XMLMixin):
50
"""Single revision on a branch.
52
Revisions may know their revision_hash, but only once they've been
53
written out. This is not stored because you cannot write the hash
54
into the file it describes.
56
After bzr 0.0.5 revisions are allowed to have multiple parents.
57
To support old clients this is written out in a slightly redundant
58
form: the first parent as the predecessor. This will eventually
62
List of parent revisions, each is a RevisionReference.
72
def __init__(self, **args):
73
self.__dict__.update(args)
76
def _get_precursor(self):
77
from warnings import warn
78
warn("Revision.precursor is deprecated", stacklevel=2)
80
return self.parents[0].revision_id
85
def _get_precursor_sha1(self):
86
from warnings import warn
87
warn("Revision.precursor_sha1 is deprecated", stacklevel=2)
89
return self.parents[0].revision_sha1
95
raise Exception("can't assign to precursor anymore")
98
precursor = property(_get_precursor, _fail, _fail)
99
precursor_sha1 = property(_get_precursor_sha1, _fail, _fail)
104
return "<Revision id %s>" % self.revision_id
107
def to_element(self):
108
root = Element('revision',
109
committer = self.committer,
110
timestamp = '%.9f' % self.timestamp,
111
revision_id = self.revision_id,
112
inventory_id = self.inventory_id,
113
inventory_sha1 = self.inventory_sha1,
116
root.set('timezone', str(self.timezone))
119
msg = SubElement(root, 'message')
120
msg.text = self.message
124
# first parent stored as precursor for compatability with 0.0.5 and
127
assert pr.revision_id
128
root.set('precursor', pr.revision_id)
130
root.set('precursor_sha1', pr.revision_sha1)
133
pelts = SubElement(root, 'parents')
134
pelts.tail = pelts.text = '\n'
135
for rr in self.parents:
136
assert isinstance(rr, RevisionReference)
137
p = SubElement(pelts, 'revision_ref')
139
assert rr.revision_id
140
p.set('revision_id', rr.revision_id)
142
p.set('revision_sha1', rr.revision_sha1)
147
def from_element(cls, elt):
148
return unpack_revision(elt)
150
from_element = classmethod(from_element)
154
def unpack_revision(elt):
155
"""Convert XML element into Revision object."""
156
# <changeset> is deprecated...
157
if elt.tag not in ('revision', 'changeset'):
158
raise BzrError("unexpected tag in revision file: %r" % elt)
160
rev = Revision(committer = elt.get('committer'),
161
timestamp = float(elt.get('timestamp')),
162
revision_id = elt.get('revision_id'),
163
inventory_id = elt.get('inventory_id'),
164
inventory_sha1 = elt.get('inventory_sha1')
167
precursor = elt.get('precursor')
168
precursor_sha1 = elt.get('precursor_sha1')
170
pelts = elt.find('parents')
173
# revisions written prior to 0.0.5 have a single precursor
174
# give as an attribute
175
rev_ref = RevisionReference(precursor, precursor_sha1)
176
rev.parents.append(rev_ref)
179
assert p.tag == 'revision_ref', \
180
"bad parent node tag %r" % p.tag
181
rev_ref = RevisionReference(p.get('revision_id'),
182
p.get('revision_sha1'))
183
rev.parents.append(rev_ref)
185
v = elt.get('timezone')
186
rev.timezone = v and int(v)
188
rev.message = elt.findtext('message') # text of <message>