~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Martin Pool
  • Date: 2005-09-05 09:11:03 UTC
  • Revision ID: mbp@sourcefrog.net-20050905091103-1e51e146be0f08b4
- add test for deserialization from a canned XML inventory

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
    def __repr__(self):
71
71
        return "<Revision id %s>" % self.revision_id
72
72
 
73
 
    def __eq__(self, other):
74
 
        if not isinstance(other, Revision):
75
 
            return False
76
 
        return (self.inventory_id == other.inventory_id
77
 
                and self.inventory_sha1 == other.inventory_sha1
78
 
                and self.revision_id == other.revision_id
79
 
                and self.timestamp == other.timestamp
80
 
                and self.message == other.message
81
 
                and self.timezone == other.timezone
82
 
                and self.committer == other.committer)
83
 
 
84
 
    def __ne__(self, other):
85
 
        return not self.__eq__(other)
86
 
 
87
 
        
 
73
        
 
74
    def to_element(self):
 
75
        from bzrlib.xml import Element, SubElement
 
76
        
 
77
        root = Element('revision',
 
78
                       committer = self.committer,
 
79
                       timestamp = '%.9f' % self.timestamp,
 
80
                       revision_id = self.revision_id,
 
81
                       inventory_id = self.inventory_id,
 
82
                       inventory_sha1 = self.inventory_sha1,
 
83
                       )
 
84
        if self.timezone:
 
85
            root.set('timezone', str(self.timezone))
 
86
        root.text = '\n'
 
87
        
 
88
        msg = SubElement(root, 'message')
 
89
        msg.text = self.message
 
90
        msg.tail = '\n'
 
91
 
 
92
        if self.parents:
 
93
            pelts = SubElement(root, 'parents')
 
94
            pelts.tail = pelts.text = '\n'
 
95
            for rr in self.parents:
 
96
                assert isinstance(rr, RevisionReference)
 
97
                p = SubElement(pelts, 'revision_ref')
 
98
                p.tail = '\n'
 
99
                assert rr.revision_id
 
100
                p.set('revision_id', rr.revision_id)
 
101
                if rr.revision_sha1:
 
102
                    p.set('revision_sha1', rr.revision_sha1)
 
103
 
 
104
        return root
 
105
 
 
106
 
 
107
    def from_element(cls, elt):
 
108
        return unpack_revision(elt)
 
109
 
 
110
    from_element = classmethod(from_element)
 
111
 
 
112
 
 
113
 
 
114
def unpack_revision(elt):
 
115
    """Convert XML element into Revision object."""
 
116
    # <changeset> is deprecated...
 
117
    if elt.tag not in ('revision', 'changeset'):
 
118
        raise bzrlib.errors.BzrError("unexpected tag in revision file: %r" % elt)
 
119
 
 
120
    rev = Revision(committer = elt.get('committer'),
 
121
                   timestamp = float(elt.get('timestamp')),
 
122
                   revision_id = elt.get('revision_id'),
 
123
                   inventory_id = elt.get('inventory_id'),
 
124
                   inventory_sha1 = elt.get('inventory_sha1')
 
125
                   )
 
126
 
 
127
    precursor = elt.get('precursor')
 
128
    precursor_sha1 = elt.get('precursor_sha1')
 
129
 
 
130
    pelts = elt.find('parents')
 
131
 
 
132
    if pelts:
 
133
        for p in pelts:
 
134
            assert p.tag == 'revision_ref', \
 
135
                   "bad parent node tag %r" % p.tag
 
136
            rev_ref = RevisionReference(p.get('revision_id'),
 
137
                                        p.get('revision_sha1'))
 
138
            rev.parents.append(rev_ref)
 
139
 
 
140
        if precursor:
 
141
            # must be consistent
 
142
            prec_parent = rev.parents[0].revision_id
 
143
            assert prec_parent == precursor
 
144
    elif precursor:
 
145
        # revisions written prior to 0.0.5 have a single precursor
 
146
        # give as an attribute
 
147
        rev_ref = RevisionReference(precursor, precursor_sha1)
 
148
        rev.parents.append(rev_ref)
 
149
 
 
150
    v = elt.get('timezone')
 
151
    rev.timezone = v and int(v)
 
152
 
 
153
    rev.message = elt.findtext('message') # text of <message>
 
154
    return rev
 
155
 
 
156
 
88
157
 
89
158
REVISION_ID_RE = None
90
159
 
98
167
    if not REVISION_ID_RE.match(rid):
99
168
        raise ValueError("malformed revision-id %r" % rid)
100
169
 
101
 
 
102
 
def is_ancestor(revision_id, candidate_id, branch):
 
170
def is_ancestor(revision_id, candidate_id, revision_source):
103
171
    """Return true if candidate_id is an ancestor of revision_id.
104
 
 
105
172
    A false negative will be returned if any intermediate descendent of
106
173
    candidate_id is not present in any of the revision_sources.
107
174
    
108
175
    revisions_source is an object supporting a get_revision operation that
109
176
    behaves like Branch's.
110
177
    """
111
 
    return candidate_id in branch.get_ancestry(revision_id)
112
178
 
 
179
    for ancestor_id, distance in iter_ancestors(revision_id, revision_source):
 
180
        if ancestor_id == candidate_id:
 
181
            return True
 
182
    return False
113
183
 
114
184
def iter_ancestors(revision_id, revision_source, only_present=False):
115
185
    ancestors = (revision_id,)