/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/revision.py

  • Committer: Martin Pool
  • Date: 2005-06-28 03:02:31 UTC
  • Revision ID: mbp@sourcefrog.net-20050628030231-d311e4ebcd467ef4
Merge John's import-speedup branch:

                                                                                         
  777 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:32 -0500
      revision-id: john@arbash-meinel.com-20050627032031-e82a50db3863b18e
      bzr selftest was not using the correct bzr

  776 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:22 -0500
      revision-id: john@arbash-meinel.com-20050627032021-c9f21fde989ddaee
      Add was using an old mutter

  775 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:02:33 -0500
      revision-id: john@arbash-meinel.com-20050627030233-9165cfe98fc63298
      Cleaned up to be less different

  774 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:54:53 -0500
      revision-id: john@arbash-meinel.com-20050627025452-4260d0e744edef43
      Allow BZR_PLUGIN_PATH='' to negate plugin loading.

  773 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:49:34 -0500
      revision-id: john@arbash-meinel.com-20050627024933-b7158f67b7b9eae5
      Finished the previous cleanup (allowing load_plugins to be called twice)

  772 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:45:08 -0500
      revision-id: john@arbash-meinel.com-20050627024508-723b1df510d196fc
      Work on making the tests pass. versioning.py is calling run_cmd directly, but plugins have been loaded.

  771 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:32:29 -0500
      revision-id: john@arbash-meinel.com-20050627023228-79972744d7c53e15
      Got it down a little bit more by removing import of tree and inventory.

  770 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:26:05 -0500
      revision-id: john@arbash-meinel.com-20050627022604-350b9773ef622f95
      Reducing the number of import from bzrlib/__init__.py and bzrlib/branch.py

  769 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:32:25 -0500
      revision-id: john@arbash-meinel.com-20050627013225-32dd044f10d23948
      Updated revision.py and xml.py to include SubElement.

  768 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:56 -0500
      revision-id: john@arbash-meinel.com-20050627010356-ee66919e1c377faf
      Minor typo

  767 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:13 -0500
      revision-id: john@arbash-meinel.com-20050627010312-40d024007eb85051
      Caching the import

  766 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:51:47 -0500
      revision-id: john@arbash-meinel.com-20050627005147-5281c99e48ed1834
      Created wrapper functions for lazy import of ElementTree

  765 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:46:37 -0500
      revision-id: john@arbash-meinel.com-20050627004636-bf432902004a94c5
      Removed all of the test imports of cElementTree

  764 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:43:59 -0500
      revision-id: john@arbash-meinel.com-20050627004358-d137fbe9570dd71b
      Trying to make bzr startup faster.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# (C) 2005 Canonical
 
2
 
 
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.
 
7
 
 
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.
 
12
 
 
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
 
16
 
 
17
 
 
18
 
 
19
 
 
20
from xml import XMLMixin, Element, SubElement
 
21
 
 
22
from errors import BzrError
 
23
 
 
24
 
 
25
class RevisionReference:
 
26
    """
 
27
    Reference to a stored revision.
 
28
 
 
29
    Includes the revision_id and revision_sha1.
 
30
    """
 
31
    revision_id = None
 
32
    revision_sha1 = None
 
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
 
37
        else:
 
38
            raise ValueError('bad revision_id %r' % revision_id)
 
39
 
 
40
        if revision_sha1 != None:
 
41
            if isinstance(revision_sha1, basestring) \
 
42
               and len(revision_sha1) == 40:
 
43
                self.revision_sha1 = revision_sha1
 
44
            else:
 
45
                raise ValueError('bad revision_sha1 %r' % revision_sha1)
 
46
                
 
47
 
 
48
 
 
49
class Revision(XMLMixin):
 
50
    """Single revision on a branch.
 
51
 
 
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.
 
55
 
 
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
 
59
    be dropped.
 
60
 
 
61
    parents
 
62
        List of parent revisions, each is a RevisionReference.
 
63
    """
 
64
    inventory_id = None
 
65
    inventory_sha1 = None
 
66
    revision_id = None
 
67
    timestamp = None
 
68
    message = None
 
69
    timezone = None
 
70
    committer = None
 
71
    
 
72
    def __init__(self, **args):
 
73
        self.__dict__.update(args)
 
74
        self.parents = []
 
75
 
 
76
    def _get_precursor(self):
 
77
        from warnings import warn
 
78
        warn("Revision.precursor is deprecated", stacklevel=2)
 
79
        if self.parents:
 
80
            return self.parents[0].revision_id
 
81
        else:
 
82
            return None
 
83
 
 
84
 
 
85
    def _get_precursor_sha1(self):
 
86
        from warnings import warn
 
87
        warn("Revision.precursor_sha1 is deprecated", stacklevel=2)
 
88
        if self.parents:
 
89
            return self.parents[0].revision_sha1
 
90
        else:
 
91
            return None    
 
92
 
 
93
 
 
94
    def _fail(self):
 
95
        raise Exception("can't assign to precursor anymore")
 
96
 
 
97
 
 
98
    precursor = property(_get_precursor, _fail, _fail)
 
99
    precursor_sha1 = property(_get_precursor_sha1, _fail, _fail)
 
100
 
 
101
 
 
102
 
 
103
    def __repr__(self):
 
104
        return "<Revision id %s>" % self.revision_id
 
105
 
 
106
        
 
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,
 
114
                       )
 
115
        if self.timezone:
 
116
            root.set('timezone', str(self.timezone))
 
117
        root.text = '\n'
 
118
        
 
119
        msg = SubElement(root, 'message')
 
120
        msg.text = self.message
 
121
        msg.tail = '\n'
 
122
 
 
123
        if self.parents:
 
124
            # first parent stored as precursor for compatability with 0.0.5 and
 
125
            # earlier
 
126
            pr = self.parents[0]
 
127
            assert pr.revision_id
 
128
            root.set('precursor', pr.revision_id)
 
129
            if pr.revision_sha1:
 
130
                root.set('precursor_sha1', pr.revision_sha1)
 
131
                
 
132
        if self.parents:
 
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')
 
138
                p.tail = '\n'
 
139
                assert rr.revision_id
 
140
                p.set('revision_id', rr.revision_id)
 
141
                if rr.revision_sha1:
 
142
                    p.set('revision_sha1', rr.revision_sha1)
 
143
 
 
144
        return root
 
145
 
 
146
 
 
147
    def from_element(cls, elt):
 
148
        return unpack_revision(elt)
 
149
 
 
150
    from_element = classmethod(from_element)
 
151
 
 
152
 
 
153
 
 
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)
 
159
 
 
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')
 
165
                   )
 
166
 
 
167
    precursor = elt.get('precursor')
 
168
    precursor_sha1 = elt.get('precursor_sha1')
 
169
 
 
170
    pelts = elt.find('parents')
 
171
 
 
172
    if precursor:
 
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)
 
177
    elif pelts:
 
178
        for p in pelts:
 
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)
 
184
 
 
185
    v = elt.get('timezone')
 
186
    rev.timezone = v and int(v)
 
187
 
 
188
    rev.message = elt.findtext('message') # text of <message>
 
189
    return rev