~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-09-17 21:19:56 UTC
  • mfrom: (1997.1.6 bind-does-not-push-or-pull)
  • Revision ID: pqm@pqm.ubuntu.com-20060917211956-6e30d07da410fd1a
(Robert Collins) Change the Branch bind method to just bind rather than binding and pushing (fixes #43744 and #39542)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
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
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.bundle.serializer import (BundleSerializer, 
23
 
                                      BUNDLE_HEADER, 
 
22
from bzrlib import errors
 
23
from bzrlib.bundle.serializer import (BundleSerializer,
 
24
                                      BUNDLE_HEADER,
24
25
                                      format_highres_date,
25
26
                                      unpack_highres_date,
26
27
                                     )
27
28
from bzrlib.bundle.serializer import binary_diff
28
 
from bzrlib.delta import compare_trees
 
29
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
29
30
from bzrlib.diff import internal_diff
30
 
import bzrlib.errors as errors
31
31
from bzrlib.osutils import pathjoin
32
32
from bzrlib.progress import DummyProgress
33
33
from bzrlib.revision import NULL_REVISION
35
35
import bzrlib.ui
36
36
from bzrlib.testament import StrictTestament
37
37
from bzrlib.textfile import text_file
 
38
from bzrlib.trace import mutter
38
39
 
39
40
bool_text = {True: 'yes', False: 'no'}
40
41
 
84
85
        to_file.write(text_line+'\n')
85
86
 
86
87
 
87
 
class BundleSerializerV07(BundleSerializer):
 
88
class BundleSerializerV08(BundleSerializer):
88
89
    def read(self, f):
89
90
        """Read the rest of the bundles from the supplied file.
90
91
 
91
92
        :param f: The file to read from
92
93
        :return: A list of bundles
93
94
        """
94
 
        assert self.version == '0.7'
95
 
        # The first line of the header should have been read
96
 
        raise NotImplementedError
 
95
        return BundleReader(f).info
97
96
 
98
97
    def write(self, source, revision_ids, forced_bases, f):
99
98
        """Write the bundless to the supplied files.
123
122
        """Write the header for the changes"""
124
123
        f = self.to_file
125
124
        f.write(BUNDLE_HEADER)
126
 
        f.write('0.7\n')
 
125
        f.write('0.8\n')
127
126
        f.write('#\n')
128
127
 
129
128
    def _write(self, key, value, indent=1):
159
158
            if rev_id == last_rev_id:
160
159
                rev_tree = last_rev_tree
161
160
            else:
162
 
                base_tree = self.source.revision_tree(rev_id)
163
 
            rev_tree = self.source.revision_tree(rev_id)
 
161
                rev_tree = self.source.revision_tree(rev_id)
164
162
            if rev_id in self.forced_bases:
165
163
                explicit_base = True
166
164
                base_id = self.forced_bases[rev_id]
269
267
            else:
270
268
                action.write(self.to_file)
271
269
 
272
 
        delta = compare_trees(old_tree, new_tree, want_unchanged=True)
 
270
        delta = new_tree.changes_from(old_tree, want_unchanged=True)
273
271
        for path, file_id, kind in delta.removed:
274
272
            action = Action('removed', [kind, path]).write(self.to_file)
275
273
 
303
301
                                             new_tree.id2path(ie.file_id)])
304
302
                action.add_property('last-changed', ie.revision)
305
303
                action.write(self.to_file)
 
304
 
 
305
 
 
306
class BundleReader(object):
 
307
    """This class reads in a bundle from a file, and returns
 
308
    a Bundle object, which can then be applied against a tree.
 
309
    """
 
310
    def __init__(self, from_file):
 
311
        """Read in the bundle from the file.
 
312
 
 
313
        :param from_file: A file-like object (must have iterator support).
 
314
        """
 
315
        object.__init__(self)
 
316
        self.from_file = iter(from_file)
 
317
        self._next_line = None
 
318
        
 
319
        self.info = BundleInfo08()
 
320
        # We put the actual inventory ids in the footer, so that the patch
 
321
        # is easier to read for humans.
 
322
        # Unfortunately, that means we need to read everything before we
 
323
        # can create a proper bundle.
 
324
        self._read()
 
325
        self._validate()
 
326
 
 
327
    def _read(self):
 
328
        self._next().next()
 
329
        while self._next_line is not None:
 
330
            if not self._read_revision_header():
 
331
                break
 
332
            if self._next_line is None:
 
333
                break
 
334
            self._read_patches()
 
335
            self._read_footer()
 
336
 
 
337
    def _validate(self):
 
338
        """Make sure that the information read in makes sense
 
339
        and passes appropriate checksums.
 
340
        """
 
341
        # Fill in all the missing blanks for the revisions
 
342
        # and generate the real_revisions list.
 
343
        self.info.complete_info()
 
344
 
 
345
    def _next(self):
 
346
        """yield the next line, but secretly
 
347
        keep 1 extra line for peeking.
 
348
        """
 
349
        for line in self.from_file:
 
350
            last = self._next_line
 
351
            self._next_line = line
 
352
            if last is not None:
 
353
                #mutter('yielding line: %r' % last)
 
354
                yield last
 
355
        last = self._next_line
 
356
        self._next_line = None
 
357
        #mutter('yielding line: %r' % last)
 
358
        yield last
 
359
 
 
360
    def _read_revision_header(self):
 
361
        found_something = False
 
362
        self.info.revisions.append(RevisionInfo(None))
 
363
        for line in self._next():
 
364
            # The bzr header is terminated with a blank line
 
365
            # which does not start with '#'
 
366
            if line is None or line == '\n':
 
367
                break
 
368
            if not line.startswith('#'):
 
369
                continue
 
370
            found_something = True
 
371
            self._handle_next(line)
 
372
        if not found_something:
 
373
            # Nothing was there, so remove the added revision
 
374
            self.info.revisions.pop()
 
375
        return found_something
 
376
 
 
377
    def _read_next_entry(self, line, indent=1):
 
378
        """Read in a key-value pair
 
379
        """
 
380
        if not line.startswith('#'):
 
381
            raise errors.MalformedHeader('Bzr header did not start with #')
 
382
        line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
 
383
        if line[:indent] == ' '*indent:
 
384
            line = line[indent:]
 
385
        if not line:
 
386
            return None, None# Ignore blank lines
 
387
 
 
388
        loc = line.find(': ')
 
389
        if loc != -1:
 
390
            key = line[:loc]
 
391
            value = line[loc+2:]
 
392
            if not value:
 
393
                value = self._read_many(indent=indent+2)
 
394
        elif line[-1:] == ':':
 
395
            key = line[:-1]
 
396
            value = self._read_many(indent=indent+2)
 
397
        else:
 
398
            raise errors.MalformedHeader('While looking for key: value pairs,'
 
399
                    ' did not find the colon %r' % (line))
 
400
 
 
401
        key = key.replace(' ', '_')
 
402
        #mutter('found %s: %s' % (key, value))
 
403
        return key, value
 
404
 
 
405
    def _handle_next(self, line):
 
406
        if line is None:
 
407
            return
 
408
        key, value = self._read_next_entry(line, indent=1)
 
409
        mutter('_handle_next %r => %r' % (key, value))
 
410
        if key is None:
 
411
            return
 
412
 
 
413
        revision_info = self.info.revisions[-1]
 
414
        if key in revision_info.__dict__:
 
415
            if getattr(revision_info, key) is None:
 
416
                setattr(revision_info, key, value)
 
417
            else:
 
418
                raise errors.MalformedHeader('Duplicated Key: %s' % key)
 
419
        else:
 
420
            # What do we do with a key we don't recognize
 
421
            raise errors.MalformedHeader('Unknown Key: "%s"' % key)
 
422
    
 
423
    def _read_many(self, indent):
 
424
        """If a line ends with no entry, that means that it should be
 
425
        followed with multiple lines of values.
 
426
 
 
427
        This detects the end of the list, because it will be a line that
 
428
        does not start properly indented.
 
429
        """
 
430
        values = []
 
431
        start = '#' + (' '*indent)
 
432
 
 
433
        if self._next_line is None or self._next_line[:len(start)] != start:
 
434
            return values
 
435
 
 
436
        for line in self._next():
 
437
            values.append(line[len(start):-1].decode('utf-8'))
 
438
            if self._next_line is None or self._next_line[:len(start)] != start:
 
439
                break
 
440
        return values
 
441
 
 
442
    def _read_one_patch(self):
 
443
        """Read in one patch, return the complete patch, along with
 
444
        the next line.
 
445
 
 
446
        :return: action, lines, do_continue
 
447
        """
 
448
        #mutter('_read_one_patch: %r' % self._next_line)
 
449
        # Peek and see if there are no patches
 
450
        if self._next_line is None or self._next_line.startswith('#'):
 
451
            return None, [], False
 
452
 
 
453
        first = True
 
454
        lines = []
 
455
        for line in self._next():
 
456
            if first:
 
457
                if not line.startswith('==='):
 
458
                    raise errors.MalformedPatches('The first line of all patches'
 
459
                        ' should be a bzr meta line "==="'
 
460
                        ': %r' % line)
 
461
                action = line[4:-1].decode('utf-8')
 
462
            elif line.startswith('... '):
 
463
                action += line[len('... '):-1].decode('utf-8')
 
464
 
 
465
            if (self._next_line is not None and 
 
466
                self._next_line.startswith('===')):
 
467
                return action, lines, True
 
468
            elif self._next_line is None or self._next_line.startswith('#'):
 
469
                return action, lines, False
 
470
 
 
471
            if first:
 
472
                first = False
 
473
            elif not line.startswith('... '):
 
474
                lines.append(line)
 
475
 
 
476
        return action, lines, False
 
477
            
 
478
    def _read_patches(self):
 
479
        do_continue = True
 
480
        revision_actions = []
 
481
        while do_continue:
 
482
            action, lines, do_continue = self._read_one_patch()
 
483
            if action is not None:
 
484
                revision_actions.append((action, lines))
 
485
        assert self.info.revisions[-1].tree_actions is None
 
486
        self.info.revisions[-1].tree_actions = revision_actions
 
487
 
 
488
    def _read_footer(self):
 
489
        """Read the rest of the meta information.
 
490
 
 
491
        :param first_line:  The previous step iterates past what it
 
492
                            can handle. That extra line is given here.
 
493
        """
 
494
        for line in self._next():
 
495
            self._handle_next(line)
 
496
            if self._next_line is None:
 
497
                break
 
498
            if not self._next_line.startswith('#'):
 
499
                # Consume the trailing \n and stop processing
 
500
                self._next().next()
 
501
                break
 
502
 
 
503
 
 
504
class BundleInfo08(BundleInfo):
 
505
    def _update_tree(self, bundle_tree, revision_id):
 
506
        bundle_tree.note_last_changed('', revision_id)
 
507
        BundleInfo._update_tree(self, bundle_tree, revision_id)