~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/testament.py

  • Committer: John Arbash Meinel
  • Date: 2006-10-11 00:23:23 UTC
  • mfrom: (2070 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2071.
  • Revision ID: john@arbash-meinel.com-20061011002323-82ba88c293d7caff
[merge] bzr.dev 2070

Show diffs side-by-side

added added

removed removed

Lines of Context:
70
70
# revisions can be serialized.
71
71
 
72
72
from copy import copy
73
 
from cStringIO import StringIO
74
 
import string
75
73
from sha import sha
76
74
 
77
75
from bzrlib.osutils import contains_whitespace, contains_linebreaks
78
76
 
 
77
 
79
78
class Testament(object):
80
79
    """Reduced summary of a revision.
81
80
 
132
131
        for l in self.message.splitlines():
133
132
            a('  %s\n' % l)
134
133
        a('inventory:\n')
135
 
        for path, ie in self.inventory.iter_entries():
 
134
        for path, ie in self._get_entries():
136
135
            a(self._entry_to_line(path, ie))
137
136
        r.extend(self._revprops_to_lines())
138
137
        if __debug__:
141
140
                    '%r of type %s is not a plain string' % (l, type(l))
142
141
        return [line.encode('utf-8') for line in r]
143
142
 
 
143
    def _get_entries(self):
 
144
        entries = self.inventory.iter_entries()
 
145
        entries.next()
 
146
        return entries
 
147
 
144
148
    def _escape_path(self, path):
145
149
        assert not contains_linebreaks(path)
146
 
        return unicode(path.replace('\\', '/').replace(' ', '\ ')).encode('utf-8')
 
150
        return unicode(path.replace('\\', '/').replace(' ', '\ '))
147
151
 
148
152
    def _entry_to_line(self, path, ie):
149
153
        """Turn an inventory entry into a testament line"""
150
 
        l = '  ' + str(ie.kind)
151
 
        l += ' ' + self._escape_path(path)
152
154
        assert not contains_whitespace(ie.file_id)
153
 
        l += ' ' + unicode(ie.file_id).encode('utf-8')
 
155
 
 
156
        content = ''
 
157
        content_spacer=''
154
158
        if ie.kind == 'file':
155
159
            # TODO: avoid switching on kind
156
160
            assert ie.text_sha1
157
 
            l += ' ' + ie.text_sha1
 
161
            content = ie.text_sha1
 
162
            content_spacer = ' '
158
163
        elif ie.kind == 'symlink':
159
164
            assert ie.symlink_target
160
 
            l += ' ' + self._escape_path(ie.symlink_target)
161
 
        l += '\n'
162
 
        return l.decode('utf-8')
 
165
            content = self._escape_path(ie.symlink_target)
 
166
            content_spacer = ' '
 
167
 
 
168
        l = u'  %s %s %s%s%s\n' % (ie.kind, self._escape_path(path),
 
169
                                   unicode(ie.file_id),
 
170
                                   content_spacer, content)
 
171
        return l
163
172
 
164
173
    def as_text(self):
165
174
        return ''.join(self.as_text_lines())
181
190
            assert not contains_whitespace(name)
182
191
            r.append('  %s:\n' % name)
183
192
            for line in value.splitlines():
184
 
                if not isinstance(line, str):
185
 
                    line = line.encode('utf-8')
186
 
                r.append('    %s\n' % line)
 
193
                r.append(u'    %s\n' % line)
187
194
        return r
188
195
 
189
196
    def as_sha1(self):
193
200
 
194
201
 
195
202
class StrictTestament(Testament):
196
 
    """This testament format is for use as a checksum in changesets"""
 
203
    """This testament format is for use as a checksum in bundle format 0.8"""
197
204
 
198
205
    long_header = 'bazaar-ng testament version 2.1\n'
199
206
    short_header = 'bazaar-ng testament short form 2.1\n'
200
207
    def _entry_to_line(self, path, ie):
201
208
        l = Testament._entry_to_line(self, path, ie)[:-1]
202
 
        l += ' ' + ie.revision.decode('utf-8')
 
209
        l += ' ' + ie.revision
203
210
        l += {True: ' yes\n', False: ' no\n'}[ie.executable]
204
211
        return l
 
212
 
 
213
 
 
214
class StrictTestament3(StrictTestament):
 
215
    """This testament format is for use as a checksum in bundle format 0.9+
 
216
    
 
217
    It differs from StrictTestament by including data about the tree root.
 
218
    """
 
219
 
 
220
    long_header = 'bazaar testament version 3 strict\n'
 
221
    short_header = 'bazaar testament short form 3 strict\n'
 
222
    def _get_entries(self):
 
223
        return self.inventory.iter_entries()
 
224
 
 
225
    def _escape_path(self, path):
 
226
        assert not contains_linebreaks(path)
 
227
        if path == '':
 
228
            path = '.'
 
229
        return unicode(path.replace('\\', '/').replace(' ', '\ '))