~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/bundle/serializer/v07.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-06-06 22:59:58 UTC
  • mfrom: (1740.2.6 bzr.checkout)
  • Revision ID: pqm@pqm.ubuntu.com-20060606225958-17ab4431da6b44f6
Speed up checkout by using existing revision text when possible

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
2
 
#
 
1
# (C) 2005 Canonical Development Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
19
 
20
20
import os
21
21
 
22
 
from bzrlib import errors
23
 
from bzrlib.bundle.serializer import (BundleSerializer,
24
 
                                      _get_bundle_header,
 
22
from bzrlib.bundle.serializer import (BundleSerializer, 
 
23
                                      BUNDLE_HEADER, 
 
24
                                      format_highres_date,
 
25
                                      unpack_highres_date,
25
26
                                     )
26
27
from bzrlib.bundle.serializer import binary_diff
27
 
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
 
28
from bzrlib.delta import compare_trees
28
29
from bzrlib.diff import internal_diff
 
30
import bzrlib.errors as errors
29
31
from bzrlib.osutils import pathjoin
30
32
from bzrlib.progress import DummyProgress
31
33
from bzrlib.revision import NULL_REVISION
 
34
from bzrlib.rio import RioWriter, read_stanzas
32
35
import bzrlib.ui
33
36
from bzrlib.testament import StrictTestament
34
 
from bzrlib.timestamp import (
35
 
    format_highres_date,
36
 
    unpack_highres_date,
37
 
)
38
37
from bzrlib.textfile import text_file
39
 
from bzrlib.trace import mutter
40
38
 
41
39
bool_text = {True: 'yes', False: 'no'}
42
40
 
55
53
        else:
56
54
            self.properties = properties
57
55
 
58
 
    def add_utf8_property(self, name, value):
59
 
        """Add a property whose value is currently utf8 to the action."""
60
 
        self.properties.append((name, value.decode('utf8')))
61
 
 
62
56
    def add_property(self, name, value):
63
57
        """Add a property to the action"""
64
58
        self.properties.append((name, value))
90
84
        to_file.write(text_line+'\n')
91
85
 
92
86
 
93
 
class BundleSerializerV08(BundleSerializer):
 
87
class BundleSerializerV07(BundleSerializer):
94
88
    def read(self, f):
95
89
        """Read the rest of the bundles from the supplied file.
96
90
 
97
91
        :param f: The file to read from
98
92
        :return: A list of bundles
99
93
        """
100
 
        return BundleReader(f).info
101
 
 
102
 
    def check_compatible(self):
103
 
        if self.source.supports_rich_root():
104
 
            raise errors.IncompatibleBundleFormat('0.8', repr(self.source))
 
94
        assert self.version == '0.7'
 
95
        # The first line of the header should have been read
 
96
        raise NotImplementedError
105
97
 
106
98
    def write(self, source, revision_ids, forced_bases, f):
107
99
        """Write the bundless to the supplied files.
115
107
        self.revision_ids = revision_ids
116
108
        self.forced_bases = forced_bases
117
109
        self.to_file = f
118
 
        self.check_compatible()
119
110
        source.lock_read()
120
111
        try:
121
112
            self._write_main_header()
128
119
        finally:
129
120
            source.unlock()
130
121
 
131
 
    def write_bundle(self, repository, target, base, fileobj):
132
 
        return self._write_bundle(repository, target, base, fileobj)
133
 
 
134
122
    def _write_main_header(self):
135
123
        """Write the header for the changes"""
136
124
        f = self.to_file
137
 
        f.write(_get_bundle_header('0.8'))
 
125
        f.write(BUNDLE_HEADER)
 
126
        f.write('0.7\n')
138
127
        f.write('#\n')
139
128
 
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')
 
129
    def _write(self, key, value, indent=1):
 
130
        """Write out meta information, with proper indenting, etc"""
 
131
        assert indent > 0, 'indentation must be greater than 0'
151
132
        f = self.to_file
152
133
        f.write('#' + (' ' * indent))
153
134
        f.write(key.encode('utf-8'))
154
135
        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):
 
136
            f.write(':\n')
 
137
        elif isinstance(value, basestring):
164
138
            f.write(': ')
165
139
            f.write(value.encode('utf-8'))
166
140
            f.write('\n')
168
142
            f.write(':\n')
169
143
            for entry in value:
170
144
                f.write('#' + (' ' * (indent+2)))
171
 
                if isinstance(entry, str):
172
 
                    f.write(entry)
173
 
                else:
174
 
                    f.write(entry.encode('utf-8'))
 
145
                f.write(entry.encode('utf-8'))
175
146
                f.write('\n')
176
147
 
177
148
    def _write_revisions(self, pb):
181
152
        last_rev_id = None
182
153
        last_rev_tree = None
183
154
 
184
 
        i_max = len(self.revision_ids)
 
155
        i_max = len(self.revision_ids) 
185
156
        for i, rev_id in enumerate(self.revision_ids):
186
157
            pb.update("Generating revsion data", i, i_max)
187
158
            rev = self.source.get_revision(rev_id)
188
159
            if rev_id == last_rev_id:
189
160
                rev_tree = last_rev_tree
190
161
            else:
191
 
                rev_tree = self.source.revision_tree(rev_id)
 
162
                base_tree = self.source.revision_tree(rev_id)
 
163
            rev_tree = self.source.revision_tree(rev_id)
192
164
            if rev_id in self.forced_bases:
193
165
                explicit_base = True
194
166
                base_id = self.forced_bases[rev_id]
205
177
                base_tree = last_rev_tree
206
178
            else:
207
179
                base_tree = self.source.revision_tree(base_id)
208
 
            force_binary = (i != 0)
 
180
 
209
181
            self._write_revision(rev, rev_tree, base_id, base_tree, 
210
 
                                 explicit_base, force_binary)
 
182
                                 explicit_base)
211
183
 
212
184
            last_rev_id = base_id
213
185
            last_rev_tree = base_tree
214
186
 
215
 
    def _testament_sha1(self, revision_id):
216
 
        return StrictTestament.from_revision(self.source, 
217
 
                                             revision_id).as_sha1()
218
 
 
219
187
    def _write_revision(self, rev, rev_tree, base_rev, base_tree, 
220
 
                        explicit_base, force_binary):
 
188
                        explicit_base):
221
189
        """Write out the information for a revision."""
222
190
        def w(key, value):
223
191
            self._write(key, value, indent=1)
227
195
        w('date', format_highres_date(rev.timestamp, rev.timezone))
228
196
        self.to_file.write('\n')
229
197
 
230
 
        self._write_delta(rev_tree, base_tree, rev.revision_id, force_binary)
 
198
        self._write_delta(rev_tree, base_tree, rev.revision_id)
231
199
 
232
200
        w('revision id', rev.revision_id)
233
 
        w('sha1', self._testament_sha1(rev.revision_id))
 
201
        w('sha1', StrictTestament.from_revision(self.source, 
 
202
                                                rev.revision_id).as_sha1())
234
203
        w('inventory sha1', rev.inventory_sha1)
235
204
        if rev.parent_ids:
236
205
            w('parent ids', rev.parent_ids)
238
207
            w('base id', base_rev)
239
208
        if rev.properties:
240
209
            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)
 
210
            for name, value in rev.properties.items():
 
211
                self._write(name, value, indent=3)
244
212
        
245
213
        # Add an extra blank space at the end
246
214
        self.to_file.write('\n')
254
222
        self.to_file.write(' // '.join(p_texts).encode('utf-8'))
255
223
        self.to_file.write('\n')
256
224
 
257
 
    def _write_delta(self, new_tree, old_tree, default_revision_id, 
258
 
                     force_binary):
 
225
    def _write_delta(self, new_tree, old_tree, default_revision_id):
259
226
        """Write out the changes between the trees."""
260
227
        DEVNULL = '/dev/null'
261
228
        old_label = ''
262
229
        new_label = ''
263
230
 
264
 
        def do_diff(file_id, old_path, new_path, action, force_binary):
 
231
        def do_diff(file_id, old_path, new_path, action):
265
232
            def tree_lines(tree, require_text=False):
266
233
                if file_id in tree:
267
234
                    tree_file = tree.get_file(file_id)
272
239
                    return []
273
240
 
274
241
            try:
275
 
                if force_binary:
276
 
                    raise errors.BinaryFile()
277
242
                old_lines = tree_lines(old_tree, require_text=True)
278
243
                new_lines = tree_lines(new_tree, require_text=True)
279
244
                action.write(self.to_file)
291
256
                          old_path, new_path):
292
257
            entry = new_tree.inventory[file_id]
293
258
            if entry.revision != default_revision_id:
294
 
                action.add_utf8_property('last-changed', entry.revision)
 
259
                action.add_property('last-changed', entry.revision)
295
260
            if meta_modified:
296
261
                action.add_bool_property('executable', entry.executable)
297
262
            if text_modified and kind == "symlink":
298
263
                action.add_property('target', entry.symlink_target)
299
264
            if text_modified and kind == "file":
300
 
                do_diff(file_id, old_path, new_path, action, force_binary)
 
265
                do_diff(file_id, old_path, new_path, action)
301
266
            else:
302
267
                action.write(self.to_file)
303
268
 
304
 
        delta = new_tree.changes_from(old_tree, want_unchanged=True,
305
 
                                      include_root=True)
 
269
        delta = compare_trees(old_tree, new_tree, want_unchanged=True)
306
270
        for path, file_id, kind in delta.removed:
307
271
            action = Action('removed', [kind, path]).write(self.to_file)
308
272
 
334
298
            if new_rev != old_rev:
335
299
                action = Action('modified', [ie.kind, 
336
300
                                             new_tree.id2path(ie.file_id)])
337
 
                action.add_utf8_property('last-changed', ie.revision)
 
301
                action.add_property('last-changed', ie.revision)
338
302
                action.write(self.to_file)
339
 
 
340
 
 
341
 
class BundleReader(object):
342
 
    """This class reads in a bundle from a file, and returns
343
 
    a Bundle object, which can then be applied against a tree.
344
 
    """
345
 
    def __init__(self, from_file):
346
 
        """Read in the bundle from the file.
347
 
 
348
 
        :param from_file: A file-like object (must have iterator support).
349
 
        """
350
 
        object.__init__(self)
351
 
        self.from_file = iter(from_file)
352
 
        self._next_line = None
353
 
        
354
 
        self.info = self._get_info()
355
 
        # We put the actual inventory ids in the footer, so that the patch
356
 
        # is easier to read for humans.
357
 
        # Unfortunately, that means we need to read everything before we
358
 
        # can create a proper bundle.
359
 
        self._read()
360
 
        self._validate()
361
 
 
362
 
    def _get_info(self):
363
 
        return BundleInfo08()
364
 
 
365
 
    def _read(self):
366
 
        self._next().next()
367
 
        while self._next_line is not None:
368
 
            if not self._read_revision_header():
369
 
                break
370
 
            if self._next_line is None:
371
 
                break
372
 
            self._read_patches()
373
 
            self._read_footer()
374
 
 
375
 
    def _validate(self):
376
 
        """Make sure that the information read in makes sense
377
 
        and passes appropriate checksums.
378
 
        """
379
 
        # Fill in all the missing blanks for the revisions
380
 
        # and generate the real_revisions list.
381
 
        self.info.complete_info()
382
 
 
383
 
    def _next(self):
384
 
        """yield the next line, but secretly
385
 
        keep 1 extra line for peeking.
386
 
        """
387
 
        for line in self.from_file:
388
 
            last = self._next_line
389
 
            self._next_line = line
390
 
            if last is not None:
391
 
                #mutter('yielding line: %r' % last)
392
 
                yield last
393
 
        last = self._next_line
394
 
        self._next_line = None
395
 
        #mutter('yielding line: %r' % last)
396
 
        yield last
397
 
 
398
 
    def _read_revision_header(self):
399
 
        found_something = False
400
 
        self.info.revisions.append(RevisionInfo(None))
401
 
        for line in self._next():
402
 
            # The bzr header is terminated with a blank line
403
 
            # which does not start with '#'
404
 
            if line is None or line == '\n':
405
 
                break
406
 
            if not line.startswith('#'):
407
 
                continue
408
 
            found_something = True
409
 
            self._handle_next(line)
410
 
        if not found_something:
411
 
            # Nothing was there, so remove the added revision
412
 
            self.info.revisions.pop()
413
 
        return found_something
414
 
 
415
 
    def _read_next_entry(self, line, indent=1):
416
 
        """Read in a key-value pair
417
 
        """
418
 
        if not line.startswith('#'):
419
 
            raise errors.MalformedHeader('Bzr header did not start with #')
420
 
        line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
421
 
        if line[:indent] == ' '*indent:
422
 
            line = line[indent:]
423
 
        if not line:
424
 
            return None, None# Ignore blank lines
425
 
 
426
 
        loc = line.find(': ')
427
 
        if loc != -1:
428
 
            key = line[:loc]
429
 
            value = line[loc+2:]
430
 
            if not value:
431
 
                value = self._read_many(indent=indent+2)
432
 
        elif line[-1:] == ':':
433
 
            key = line[:-1]
434
 
            value = self._read_many(indent=indent+2)
435
 
        else:
436
 
            raise errors.MalformedHeader('While looking for key: value pairs,'
437
 
                    ' did not find the colon %r' % (line))
438
 
 
439
 
        key = key.replace(' ', '_')
440
 
        #mutter('found %s: %s' % (key, value))
441
 
        return key, value
442
 
 
443
 
    def _handle_next(self, line):
444
 
        if line is None:
445
 
            return
446
 
        key, value = self._read_next_entry(line, indent=1)
447
 
        mutter('_handle_next %r => %r' % (key, value))
448
 
        if key is None:
449
 
            return
450
 
 
451
 
        revision_info = self.info.revisions[-1]
452
 
        if key in revision_info.__dict__:
453
 
            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
 
                setattr(revision_info, key, value)
459
 
            else:
460
 
                raise errors.MalformedHeader('Duplicated Key: %s' % key)
461
 
        else:
462
 
            # What do we do with a key we don't recognize
463
 
            raise errors.MalformedHeader('Unknown Key: "%s"' % key)
464
 
    
465
 
    def _read_many(self, indent):
466
 
        """If a line ends with no entry, that means that it should be
467
 
        followed with multiple lines of values.
468
 
 
469
 
        This detects the end of the list, because it will be a line that
470
 
        does not start properly indented.
471
 
        """
472
 
        values = []
473
 
        start = '#' + (' '*indent)
474
 
 
475
 
        if self._next_line is None or self._next_line[:len(start)] != start:
476
 
            return values
477
 
 
478
 
        for line in self._next():
479
 
            values.append(line[len(start):-1].decode('utf-8'))
480
 
            if self._next_line is None or self._next_line[:len(start)] != start:
481
 
                break
482
 
        return values
483
 
 
484
 
    def _read_one_patch(self):
485
 
        """Read in one patch, return the complete patch, along with
486
 
        the next line.
487
 
 
488
 
        :return: action, lines, do_continue
489
 
        """
490
 
        #mutter('_read_one_patch: %r' % self._next_line)
491
 
        # Peek and see if there are no patches
492
 
        if self._next_line is None or self._next_line.startswith('#'):
493
 
            return None, [], False
494
 
 
495
 
        first = True
496
 
        lines = []
497
 
        for line in self._next():
498
 
            if first:
499
 
                if not line.startswith('==='):
500
 
                    raise errors.MalformedPatches('The first line of all patches'
501
 
                        ' should be a bzr meta line "==="'
502
 
                        ': %r' % line)
503
 
                action = line[4:-1].decode('utf-8')
504
 
            elif line.startswith('... '):
505
 
                action += line[len('... '):-1].decode('utf-8')
506
 
 
507
 
            if (self._next_line is not None and 
508
 
                self._next_line.startswith('===')):
509
 
                return action, lines, True
510
 
            elif self._next_line is None or self._next_line.startswith('#'):
511
 
                return action, lines, False
512
 
 
513
 
            if first:
514
 
                first = False
515
 
            elif not line.startswith('... '):
516
 
                lines.append(line)
517
 
 
518
 
        return action, lines, False
519
 
            
520
 
    def _read_patches(self):
521
 
        do_continue = True
522
 
        revision_actions = []
523
 
        while do_continue:
524
 
            action, lines, do_continue = self._read_one_patch()
525
 
            if action is not None:
526
 
                revision_actions.append((action, lines))
527
 
        if self.info.revisions[-1].tree_actions is not None:
528
 
            raise AssertionError()
529
 
        self.info.revisions[-1].tree_actions = revision_actions
530
 
 
531
 
    def _read_footer(self):
532
 
        """Read the rest of the meta information.
533
 
 
534
 
        :param first_line:  The previous step iterates past what it
535
 
                            can handle. That extra line is given here.
536
 
        """
537
 
        for line in self._next():
538
 
            self._handle_next(line)
539
 
            if self._next_line is None:
540
 
                break
541
 
            if not self._next_line.startswith('#'):
542
 
                # Consume the trailing \n and stop processing
543
 
                self._next().next()
544
 
                break
545
 
 
546
 
class BundleInfo08(BundleInfo):
547
 
 
548
 
    def _update_tree(self, bundle_tree, revision_id):
549
 
        bundle_tree.note_last_changed('', revision_id)
550
 
        BundleInfo._update_tree(self, bundle_tree, revision_id)
551
 
 
552
 
    def _testament_sha1_from_revision(self, repository, revision_id):
553
 
        testament = StrictTestament.from_revision(repository, revision_id)
554
 
        return testament.as_sha1()
555
 
 
556
 
    def _testament_sha1(self, revision, inventory):
557
 
        return StrictTestament(revision, inventory).as_sha1()