~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Martin Pool
  • Date: 2005-06-22 07:30:45 UTC
  • Revision ID: mbp@sourcefrog.net-20050622073045-80d336b7e0b859aa
- redirect stdout/stderr while running tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
 
19
19
 
20
 
class RevisionReference(object):
 
20
from xml import XMLMixin
 
21
 
 
22
try:
 
23
    from cElementTree import Element, ElementTree, SubElement
 
24
except ImportError:
 
25
    from elementtree.ElementTree import Element, ElementTree, SubElement
 
26
 
 
27
from errors import BzrError
 
28
 
 
29
 
 
30
class RevisionReference:
21
31
    """
22
32
    Reference to a stored revision.
23
33
 
25
35
    """
26
36
    revision_id = None
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
41
51
                
42
52
 
43
53
 
44
 
class Revision(object):
 
54
class Revision(XMLMixin):
45
55
    """Single revision on a branch.
46
56
 
47
57
    Revisions may know their revision_hash, but only once they've been
49
59
    into the file it describes.
50
60
 
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
 
64
    be dropped.
52
65
 
53
66
    parents
54
67
        List of parent revisions, each is a RevisionReference.
65
78
        self.__dict__.update(args)
66
79
        self.parents = []
67
80
 
 
81
    def _get_precursor(self):
 
82
        from warnings import warn
 
83
        warn("Revision.precursor is deprecated", stacklevel=2)
 
84
        if self.parents:
 
85
            return self.parents[0].revision_id
 
86
        else:
 
87
            return None
 
88
 
 
89
 
 
90
    def _get_precursor_sha1(self):
 
91
        from warnings import warn
 
92
        warn("Revision.precursor_sha1 is deprecated", stacklevel=2)
 
93
        if self.parents:
 
94
            return self.parents[0].revision_sha1
 
95
        else:
 
96
            return None    
 
97
 
 
98
 
 
99
    def _fail(self):
 
100
        raise Exception("can't assign to precursor anymore")
 
101
 
 
102
 
 
103
    precursor = property(_get_precursor, _fail, _fail)
 
104
    precursor_sha1 = property(_get_precursor_sha1, _fail, _fail)
 
105
 
 
106
 
68
107
 
69
108
    def __repr__(self):
70
109
        return "<Revision id %s>" % self.revision_id
71
110
 
72
111
        
73
112
    def to_element(self):
74
 
        from bzrlib.xml import Element, SubElement
75
 
        
76
113
        root = Element('revision',
77
114
                       committer = self.committer,
78
115
                       timestamp = '%.9f' % self.timestamp,
89
126
        msg.tail = '\n'
90
127
 
91
128
        if self.parents:
 
129
            # first parent stored as precursor for compatability with 0.0.5 and
 
130
            # earlier
 
131
            pr = self.parents[0]
 
132
            assert pr.revision_id
 
133
            root.set('precursor', pr.revision_id)
 
134
            if pr.revision_sha1:
 
135
                root.set('precursor_sha1', pr.revision_sha1)
 
136
                
 
137
        if self.parents:
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
117
 
    
118
162
    if elt.tag not in ('revision', 'changeset'):
119
163
        raise BzrError("unexpected tag in revision file: %r" % elt)
120
164
 
130
174
 
131
175
    pelts = elt.find('parents')
132
176
 
133
 
    if pelts:
 
177
    if precursor:
 
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)
 
182
    elif pelts:
134
183
        for p in pelts:
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)
140
189
 
141
 
        if precursor:
142
 
            # must be consistent
143
 
            prec_parent = rev.parents[0].revision_id
144
 
            assert prec_parent == precursor
145
 
    elif 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)
150
 
 
151
190
    v = elt.get('timezone')
152
191
    rev.timezone = v and int(v)
153
192
 
154
193
    rev.message = elt.findtext('message') # text of <message>
155
194
    return rev
156
 
 
157
 
 
158
 
 
159
 
REVISION_ID_RE = None
160
 
 
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:
165
 
        import re
166
 
        REVISION_ID_RE = re.compile('[\w.-]+@[\w.-]+--?\d+--?[0-9a-f]+\Z')
167
 
 
168
 
    if not REVISION_ID_RE.match(rid):
169
 
        raise ValueError("malformed revision-id %r" % rid)
170