/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/bundle/serializer/v08.py

  • Committer: Aaron Bentley
  • Date: 2006-09-21 23:23:03 UTC
  • mto: This revision was merged to the branch mainline in revision 2048.
  • Revision ID: aaron.bentley@utoronto.ca-20060921232303-456dc6bfcc66f870
Got 0.9 bundles working, with root included by changes_from

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2009 Canonical Ltd
 
1
# (C) 2005 Canonical Development Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Serializer factory for reading and writing bundles.
18
18
"""
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import (
23
 
    errors,
24
 
    ui,
25
 
    )
 
22
from bzrlib import errors
26
23
from bzrlib.bundle.serializer import (BundleSerializer,
27
 
                                      _get_bundle_header,
 
24
                                      BUNDLE_HEADER,
 
25
                                      format_highres_date,
 
26
                                      unpack_highres_date,
28
27
                                     )
29
28
from bzrlib.bundle.serializer import binary_diff
30
29
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
31
30
from bzrlib.diff import internal_diff
32
31
from bzrlib.osutils import pathjoin
 
32
from bzrlib.progress import DummyProgress
33
33
from bzrlib.revision import NULL_REVISION
 
34
from bzrlib.rio import RioWriter, read_stanzas
 
35
import bzrlib.ui
34
36
from bzrlib.testament import StrictTestament
35
 
from bzrlib.timestamp import (
36
 
    format_highres_date,
37
 
    unpack_highres_date,
38
 
)
39
37
from bzrlib.textfile import text_file
40
38
from bzrlib.trace import mutter
41
39
 
56
54
        else:
57
55
            self.properties = properties
58
56
 
59
 
    def add_utf8_property(self, name, value):
60
 
        """Add a property whose value is currently utf8 to the action."""
61
 
        self.properties.append((name, value.decode('utf8')))
62
 
 
63
57
    def add_property(self, name, value):
64
58
        """Add a property to the action"""
65
59
        self.properties.append((name, value))
101
95
        return BundleReader(f).info
102
96
 
103
97
    def check_compatible(self):
104
 
        if self.source.supports_rich_root():
105
 
            raise errors.IncompatibleBundleFormat('0.8', repr(self.source))
 
98
        if self.source._format.rich_root_data:
 
99
            raise errors.IncompatibleFormat('0.8', 
 
100
                self.source._format.get_format_description())
106
101
 
107
102
    def write(self, source, revision_ids, forced_bases, f):
108
103
        """Write the bundless to the supplied files.
120
115
        source.lock_read()
121
116
        try:
122
117
            self._write_main_header()
123
 
            pb = ui.ui_factory.nested_progress_bar()
 
118
            pb = DummyProgress()
124
119
            try:
125
120
                self._write_revisions(pb)
126
121
            finally:
127
 
                pb.finished()
 
122
                pass
 
123
                #pb.finished()
128
124
        finally:
129
125
            source.unlock()
130
126
 
131
 
    def write_bundle(self, repository, target, base, fileobj):
132
 
        return self._write_bundle(repository, target, base, fileobj)
133
 
 
134
127
    def _write_main_header(self):
135
128
        """Write the header for the changes"""
136
129
        f = self.to_file
137
 
        f.write(_get_bundle_header('0.8'))
 
130
        f.write(BUNDLE_HEADER)
 
131
        f.write('0.8\n')
138
132
        f.write('#\n')
139
133
 
140
 
    def _write(self, key, value, indent=1, trailing_space_when_empty=False):
141
 
        """Write out meta information, with proper indenting, etc.
142
 
 
143
 
        :param trailing_space_when_empty: To work around a bug in earlier
144
 
            bundle readers, when writing an empty property, we use "prop: \n"
145
 
            rather than writing "prop:\n".
146
 
            If this parameter is True, and value is the empty string, we will
147
 
            write an extra space.
148
 
        """
149
 
        if indent < 1:
150
 
            raise ValueError('indentation must be greater than 0')
 
134
    def _write(self, key, value, indent=1):
 
135
        """Write out meta information, with proper indenting, etc"""
 
136
        assert indent > 0, 'indentation must be greater than 0'
151
137
        f = self.to_file
152
138
        f.write('#' + (' ' * indent))
153
139
        f.write(key.encode('utf-8'))
154
140
        if not value:
155
 
            if trailing_space_when_empty and value == '':
156
 
                f.write(': \n')
157
 
            else:
158
 
                f.write(':\n')
159
 
        elif isinstance(value, str):
160
 
            f.write(': ')
161
 
            f.write(value)
162
 
            f.write('\n')
163
 
        elif isinstance(value, unicode):
 
141
            f.write(':\n')
 
142
        elif isinstance(value, basestring):
164
143
            f.write(': ')
165
144
            f.write(value.encode('utf-8'))
166
145
            f.write('\n')
168
147
            f.write(':\n')
169
148
            for entry in value:
170
149
                f.write('#' + (' ' * (indent+2)))
171
 
                if isinstance(entry, str):
172
 
                    f.write(entry)
173
 
                else:
174
 
                    f.write(entry.encode('utf-8'))
 
150
                f.write(entry.encode('utf-8'))
175
151
                f.write('\n')
176
152
 
177
153
    def _write_revisions(self, pb):
181
157
        last_rev_id = None
182
158
        last_rev_tree = None
183
159
 
184
 
        i_max = len(self.revision_ids)
 
160
        i_max = len(self.revision_ids) 
185
161
        for i, rev_id in enumerate(self.revision_ids):
186
 
            pb.update("Generating revision data", i, i_max)
 
162
            pb.update("Generating revsion data", i, i_max)
187
163
            rev = self.source.get_revision(rev_id)
188
164
            if rev_id == last_rev_id:
189
165
                rev_tree = last_rev_tree
206
182
            else:
207
183
                base_tree = self.source.revision_tree(base_id)
208
184
            force_binary = (i != 0)
209
 
            self._write_revision(rev, rev_tree, base_id, base_tree,
 
185
            self._write_revision(rev, rev_tree, base_id, base_tree, 
210
186
                                 explicit_base, force_binary)
211
187
 
212
188
            last_rev_id = base_id
213
189
            last_rev_tree = base_tree
214
190
 
215
191
    def _testament_sha1(self, revision_id):
216
 
        return StrictTestament.from_revision(self.source,
 
192
        return StrictTestament.from_revision(self.source, 
217
193
                                             revision_id).as_sha1()
218
194
 
219
 
    def _write_revision(self, rev, rev_tree, base_rev, base_tree,
 
195
    def _write_revision(self, rev, rev_tree, base_rev, base_tree, 
220
196
                        explicit_base, force_binary):
221
197
        """Write out the information for a revision."""
222
198
        def w(key, value):
238
214
            w('base id', base_rev)
239
215
        if rev.properties:
240
216
            self._write('properties', None, indent=1)
241
 
            for name, value in sorted(rev.properties.items()):
242
 
                self._write(name, value, indent=3,
243
 
                            trailing_space_when_empty=True)
244
 
 
 
217
            for name, value in rev.properties.items():
 
218
                self._write(name, value, indent=3)
 
219
        
245
220
        # Add an extra blank space at the end
246
221
        self.to_file.write('\n')
247
222
 
254
229
        self.to_file.write(' // '.join(p_texts).encode('utf-8'))
255
230
        self.to_file.write('\n')
256
231
 
257
 
    def _write_delta(self, new_tree, old_tree, default_revision_id,
 
232
    def _write_delta(self, new_tree, old_tree, default_revision_id, 
258
233
                     force_binary):
259
234
        """Write out the changes between the trees."""
260
235
        DEVNULL = '/dev/null'
277
252
                old_lines = tree_lines(old_tree, require_text=True)
278
253
                new_lines = tree_lines(new_tree, require_text=True)
279
254
                action.write(self.to_file)
280
 
                internal_diff(old_path, old_lines, new_path, new_lines,
 
255
                internal_diff(old_path, old_lines, new_path, new_lines, 
281
256
                              self.to_file)
282
257
            except errors.BinaryFile:
283
258
                old_lines = tree_lines(old_tree, require_text=False)
284
259
                new_lines = tree_lines(new_tree, require_text=False)
285
260
                action.add_property('encoding', 'base64')
286
261
                action.write(self.to_file)
287
 
                binary_diff(old_path, old_lines, new_path, new_lines,
 
262
                binary_diff(old_path, old_lines, new_path, new_lines, 
288
263
                            self.to_file)
289
264
 
290
265
        def finish_action(action, file_id, kind, meta_modified, text_modified,
291
266
                          old_path, new_path):
292
267
            entry = new_tree.inventory[file_id]
293
268
            if entry.revision != default_revision_id:
294
 
                action.add_utf8_property('last-changed', entry.revision)
 
269
                action.add_property('last-changed', entry.revision)
295
270
            if meta_modified:
296
271
                action.add_bool_property('executable', entry.executable)
297
272
            if text_modified and kind == "symlink":
301
276
            else:
302
277
                action.write(self.to_file)
303
278
 
304
 
        delta = new_tree.changes_from(old_tree, want_unchanged=True,
 
279
        delta = new_tree.changes_from(old_tree, want_unchanged=True, 
305
280
                                      include_root=True)
306
281
        for path, file_id, kind in delta.removed:
307
282
            action = Action('removed', [kind, path]).write(self.to_file)
308
283
 
309
284
        for path, file_id, kind in delta.added:
310
285
            action = Action('added', [kind, path], [('file-id', file_id)])
311
 
            meta_modified = (kind=='file' and
 
286
            meta_modified = (kind=='file' and 
312
287
                             new_tree.is_executable(file_id))
313
288
            finish_action(action, file_id, kind, meta_modified, True,
314
289
                          DEVNULL, path)
332
307
                continue
333
308
            old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
334
309
            if new_rev != old_rev:
335
 
                action = Action('modified', [ie.kind,
 
310
                action = Action('modified', [ie.kind, 
336
311
                                             new_tree.id2path(ie.file_id)])
337
 
                action.add_utf8_property('last-changed', ie.revision)
 
312
                action.add_property('last-changed', ie.revision)
338
313
                action.write(self.to_file)
339
314
 
340
315
 
350
325
        object.__init__(self)
351
326
        self.from_file = iter(from_file)
352
327
        self._next_line = None
353
 
 
 
328
        
354
329
        self.info = self._get_info()
355
330
        # We put the actual inventory ids in the footer, so that the patch
356
331
        # is easier to read for humans.
451
426
        revision_info = self.info.revisions[-1]
452
427
        if key in revision_info.__dict__:
453
428
            if getattr(revision_info, key) is None:
454
 
                if key in ('file_id', 'revision_id', 'base_id'):
455
 
                    value = value.encode('utf8')
456
 
                elif key in ('parent_ids'):
457
 
                    value = [v.encode('utf8') for v in value]
458
429
                setattr(revision_info, key, value)
459
430
            else:
460
431
                raise errors.MalformedHeader('Duplicated Key: %s' % key)
461
432
        else:
462
433
            # What do we do with a key we don't recognize
463
434
            raise errors.MalformedHeader('Unknown Key: "%s"' % key)
464
 
 
 
435
    
465
436
    def _read_many(self, indent):
466
437
        """If a line ends with no entry, that means that it should be
467
438
        followed with multiple lines of values.
504
475
            elif line.startswith('... '):
505
476
                action += line[len('... '):-1].decode('utf-8')
506
477
 
507
 
            if (self._next_line is not None and
 
478
            if (self._next_line is not None and 
508
479
                self._next_line.startswith('===')):
509
480
                return action, lines, True
510
481
            elif self._next_line is None or self._next_line.startswith('#'):
516
487
                lines.append(line)
517
488
 
518
489
        return action, lines, False
519
 
 
 
490
            
520
491
    def _read_patches(self):
521
492
        do_continue = True
522
493
        revision_actions = []
524
495
            action, lines, do_continue = self._read_one_patch()
525
496
            if action is not None:
526
497
                revision_actions.append((action, lines))
527
 
        if self.info.revisions[-1].tree_actions is not None:
528
 
            raise AssertionError()
 
498
        assert self.info.revisions[-1].tree_actions is None
529
499
        self.info.revisions[-1].tree_actions = revision_actions
530
500
 
531
501
    def _read_footer(self):
543
513
                self._next().next()
544
514
                break
545
515
 
 
516
 
546
517
class BundleInfo08(BundleInfo):
547
518
 
548
519
    def _update_tree(self, bundle_tree, revision_id):