/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/testament.py

  • Committer: John Ferlito
  • Date: 2009-09-02 04:31:45 UTC
  • mto: (4665.7.1 serve-init)
  • mto: This revision was merged to the branch mainline in revision 4913.
  • Revision ID: johnf@inodes.org-20090902043145-gxdsfw03ilcwbyn5
Add a debian init script for bzr --serve

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
 
72
72
from copy import copy
73
73
 
74
 
from ..osutils import (
 
74
from bzrlib.osutils import (
75
75
    contains_whitespace,
76
76
    contains_linebreaks,
77
 
    sha_strings,
 
77
    sha,
78
78
    )
79
 
from ..tree import Tree
80
79
 
81
80
 
82
81
class Testament(object):
92
91
 
93
92
    long_header = 'bazaar-ng testament version 1\n'
94
93
    short_header = 'bazaar-ng testament short form 1\n'
95
 
    include_root = False
96
94
 
97
95
    @classmethod
98
96
    def from_revision(cls, repository, revision_id):
99
 
        """Produce a new testament from a historical revision."""
 
97
        """Produce a new testament from a historical revision"""
100
98
        rev = repository.get_revision(revision_id)
101
 
        tree = repository.revision_tree(revision_id)
102
 
        return cls(rev, tree)
103
 
 
104
 
    @classmethod
105
 
    def from_revision_tree(cls, tree):
106
 
        """Produce a new testament from a revision tree."""
107
 
        rev = tree._repository.get_revision(tree.get_revision_id())
108
 
        return cls(rev, tree)
109
 
 
110
 
    def __init__(self, rev, tree):
111
 
        """Create a new testament for rev using tree."""
 
99
        inventory = repository.get_inventory(revision_id)
 
100
        return cls(rev, inventory)
 
101
 
 
102
    def __init__(self, rev, inventory):
 
103
        """Create a new testament for rev using inventory."""
112
104
        self.revision_id = rev.revision_id
113
105
        self.committer = rev.committer
114
106
        self.timezone = rev.timezone or 0
115
107
        self.timestamp = rev.timestamp
116
108
        self.message = rev.message
117
109
        self.parent_ids = rev.parent_ids[:]
118
 
        if not isinstance(tree, Tree):
119
 
            raise TypeError("As of bzr 2.4 Testament.__init__() takes a "
120
 
                            "Revision and a Tree.")
121
 
        self.tree = tree
 
110
        self.inventory = inventory
122
111
        self.revprops = copy(rev.properties)
123
112
        if contains_whitespace(self.revision_id):
124
113
            raise ValueError(self.revision_id)
134
123
        r = []
135
124
        a = r.append
136
125
        a(self.long_header)
137
 
        a('revision-id: %s\n' % self.revision_id.decode('utf-8'))
 
126
        a('revision-id: %s\n' % self.revision_id)
138
127
        a('committer: %s\n' % self.committer)
139
128
        a('timestamp: %d\n' % self.timestamp)
140
129
        a('timezone: %d\n' % self.timezone)
143
132
        for parent_id in sorted(self.parent_ids):
144
133
            if contains_whitespace(parent_id):
145
134
                raise ValueError(parent_id)
146
 
            a('  %s\n' % parent_id.decode('utf-8'))
 
135
            a('  %s\n' % parent_id)
147
136
        a('message:\n')
148
137
        for l in self.message.splitlines():
149
138
            a('  %s\n' % l)
154
143
        return [line.encode('utf-8') for line in r]
155
144
 
156
145
    def _get_entries(self):
157
 
        return ((path, ie) for (path, file_class, kind, ie) in
158
 
                self.tree.list_files(include_root=self.include_root))
 
146
        entries = self.inventory.iter_entries()
 
147
        entries.next()
 
148
        return entries
159
149
 
160
150
    def _escape_path(self, path):
161
151
        if contains_linebreaks(path):
162
152
            raise ValueError(path)
163
 
        if not isinstance(path, str):
164
 
            # TODO(jelmer): Clean this up for pad.lv/1696545
165
 
            path = path.decode('ascii')
166
 
        return path.replace(u'\\', u'/').replace(u' ', u'\\ ')
 
153
        return unicode(path.replace('\\', '/').replace(' ', '\ '))
167
154
 
168
155
    def _entry_to_line(self, path, ie):
169
156
        """Turn an inventory entry into a testament line"""
170
157
        if contains_whitespace(ie.file_id):
171
158
            raise ValueError(ie.file_id)
172
159
        content = ''
173
 
        content_spacer = ''
 
160
        content_spacer=''
174
161
        if ie.kind == 'file':
175
162
            # TODO: avoid switching on kind
176
163
            if not ie.text_sha1:
177
164
                raise AssertionError()
178
 
            content = ie.text_sha1.decode('ascii')
 
165
            content = ie.text_sha1
179
166
            content_spacer = ' '
180
167
        elif ie.kind == 'symlink':
181
168
            if not ie.symlink_target:
189
176
        return l
190
177
 
191
178
    def as_text(self):
192
 
        return b''.join(self.as_text_lines())
 
179
        return ''.join(self.as_text_lines())
193
180
 
194
181
    def as_short_text(self):
195
182
        """Return short digest-based testament."""
196
 
        return (self.short_header.encode('ascii') +
197
 
                b'revision-id: %s\n'
198
 
                b'sha1: %s\n'
 
183
        return (self.short_header +
 
184
                'revision-id: %s\n'
 
185
                'sha1: %s\n'
199
186
                % (self.revision_id, self.as_sha1()))
200
187
 
201
188
    def _revprops_to_lines(self):
212
199
        return r
213
200
 
214
201
    def as_sha1(self):
215
 
        return sha_strings(self.as_text_lines())
 
202
        s = sha()
 
203
        map(s.update, self.as_text_lines())
 
204
        return s.hexdigest()
216
205
 
217
206
 
218
207
class StrictTestament(Testament):
220
209
 
221
210
    long_header = 'bazaar-ng testament version 2.1\n'
222
211
    short_header = 'bazaar-ng testament short form 2.1\n'
223
 
    include_root = False
224
 
 
225
212
    def _entry_to_line(self, path, ie):
226
213
        l = Testament._entry_to_line(self, path, ie)[:-1]
227
 
        l += ' ' + ie.revision.decode('utf-8')
 
214
        l += ' ' + ie.revision
228
215
        l += {True: ' yes\n', False: ' no\n'}[ie.executable]
229
216
        return l
230
217
 
237
224
 
238
225
    long_header = 'bazaar testament version 3 strict\n'
239
226
    short_header = 'bazaar testament short form 3 strict\n'
240
 
    include_root = True
 
227
    def _get_entries(self):
 
228
        return self.inventory.iter_entries()
241
229
 
242
230
    def _escape_path(self, path):
243
231
        if contains_linebreaks(path):
244
232
            raise ValueError(path)
245
 
        if not isinstance(path, str):
246
 
            # TODO(jelmer): Clean this up for pad.lv/1696545
247
 
            path = path.decode('ascii')
248
 
        if path == u'':
249
 
            path = u'.'
250
 
        return path.replace(u'\\', u'/').replace(u' ', u'\\ ')
 
233
        if path == '':
 
234
            path = '.'
 
235
        return unicode(path.replace('\\', '/').replace(' ', '\ '))