~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Martin Pool
  • Date: 2005-06-20 04:24:35 UTC
  • Revision ID: mbp@sourcefrog.net-20050620042435-7c315b5a93001b89
- add jk's patchwork client

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
            root.set('precursor', pr.revision_id)
 
133
            if pr.revision_sha1:
 
134
                root.set('precursor_sha1', pr.revision_sha1)
 
135
                
 
136
        if self.parents:
92
137
            pelts = SubElement(root, 'parents')
93
138
            pelts.tail = pelts.text = '\n'
94
139
            for rr in self.parents:
95
140
                assert isinstance(rr, RevisionReference)
96
141
                p = SubElement(pelts, 'revision_ref')
97
142
                p.tail = '\n'
98
 
                assert rr.revision_id
99
143
                p.set('revision_id', rr.revision_id)
100
144
                if rr.revision_sha1:
101
145
                    p.set('revision_sha1', rr.revision_sha1)
113
157
def unpack_revision(elt):
114
158
    """Convert XML element into Revision object."""
115
159
    # <changeset> is deprecated...
116
 
    from bzrlib.errors import BzrError
117
 
    
118
160
    if elt.tag not in ('revision', 'changeset'):
119
161
        raise BzrError("unexpected tag in revision file: %r" % elt)
120
162
 
130
172
 
131
173
    pelts = elt.find('parents')
132
174
 
133
 
    if pelts:
 
175
    if precursor:
 
176
        # revisions written prior to 0.0.5 have a single precursor
 
177
        # give as an attribute
 
178
        rev_ref = RevisionReference(precursor, precursor_sha1)
 
179
        rev.parents.append(rev_ref)
 
180
    elif pelts:
134
181
        for p in pelts:
135
182
            assert p.tag == 'revision_ref', \
136
183
                   "bad parent node tag %r" % p.tag
138
185
                                        p.get('revision_sha1'))
139
186
            rev.parents.append(rev_ref)
140
187
 
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
188
    v = elt.get('timezone')
152
189
    rev.timezone = v and int(v)
153
190
 
154
191
    rev.message = elt.findtext('message') # text of <message>
155
192
    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