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

  • Committer: Jelmer Vernooij
  • Date: 2018-11-21 22:07:46 UTC
  • mto: This revision was merged to the branch mainline in revision 7208.
  • Revision ID: jelmer@jelmer.uk-20181121220746-l1tynnionjpjrdzn
Handle overflow errors on Python 2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Serializer factory for reading and writing bundles.
18
18
"""
19
19
 
20
 
import os
 
20
from __future__ import absolute_import
21
21
 
22
 
from bzrlib import (
 
22
from breezy import (
23
23
    errors,
24
24
    ui,
25
25
    )
26
 
from bzrlib.bundle.serializer import (BundleSerializer,
27
 
                                      _get_bundle_header,
28
 
                                     )
29
 
from bzrlib.bundle.serializer import binary_diff
30
 
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
31
 
from bzrlib.diff import internal_diff
32
 
from bzrlib.osutils import pathjoin
33
 
from bzrlib.revision import NULL_REVISION
34
 
from bzrlib.testament import StrictTestament
35
 
from bzrlib.timestamp import (
 
26
from breezy.bundle.serializer import (
 
27
    BundleSerializer,
 
28
    _get_bundle_header,
 
29
    )
 
30
from breezy.bundle.serializer import binary_diff
 
31
from breezy.bundle.bundle_data import (
 
32
    RevisionInfo,
 
33
    BundleInfo,
 
34
    )
 
35
from breezy.diff import internal_diff
 
36
from breezy.revision import NULL_REVISION
 
37
from breezy.sixish import text_type
 
38
from breezy.testament import StrictTestament
 
39
from breezy.timestamp import (
36
40
    format_highres_date,
37
 
    unpack_highres_date,
38
 
)
39
 
from bzrlib.textfile import text_file
40
 
from bzrlib.trace import mutter
 
41
    )
 
42
from breezy.textfile import text_file
 
43
from breezy.trace import mutter
41
44
 
42
45
bool_text = {True: 'yes', False: 'no'}
43
46
 
70
73
 
71
74
    def write(self, to_file):
72
75
        """Write action as to a file"""
73
 
        p_texts = [' '.join([self.name]+self.parameters)]
 
76
        p_texts = [' '.join([self.name] + self.parameters)]
74
77
        for prop in self.properties:
75
78
            if len(prop) == 1:
76
79
                p_texts.append(prop[0])
86
89
        while len(text_line) > available:
87
90
            to_file.write(text_line[:available])
88
91
            text_line = text_line[available:]
89
 
            to_file.write('\n... ')
90
 
            available = 79 - len('... ')
91
 
        to_file.write(text_line+'\n')
 
92
            to_file.write(b'\n... ')
 
93
            available = 79 - len(b'... ')
 
94
        to_file.write(text_line + b'\n')
92
95
 
93
96
 
94
97
class BundleSerializerV08(BundleSerializer):
 
98
 
95
99
    def read(self, f):
96
100
        """Read the rest of the bundles from the supplied file.
97
101
 
117
121
        self.forced_bases = forced_bases
118
122
        self.to_file = f
119
123
        self.check_compatible()
120
 
        source.lock_read()
121
 
        try:
 
124
        with source.lock_read():
122
125
            self._write_main_header()
123
 
            pb = ui.ui_factory.nested_progress_bar()
124
 
            try:
 
126
            with ui.ui_factory.nested_progress_bar() as pb:
125
127
                self._write_revisions(pb)
126
 
            finally:
127
 
                pb.finished()
128
 
        finally:
129
 
            source.unlock()
130
128
 
131
 
    def write_bundle(self, repository, target, base, fileobj):
132
 
        return self._write_bundle(repository, target, base, fileobj)
 
129
    def write_bundle(self, repository, revision_id, base_revision_id, out):
 
130
        """Helper function for translating write_bundle to write"""
 
131
        forced_bases = {revision_id: base_revision_id}
 
132
        if base_revision_id is NULL_REVISION:
 
133
            base_revision_id = None
 
134
        graph = repository.get_graph()
 
135
        revision_ids = graph.find_unique_ancestors(revision_id,
 
136
                                                   [base_revision_id])
 
137
        revision_ids = list(repository.get_graph().iter_topo_order(
 
138
            revision_ids))
 
139
        revision_ids.reverse()
 
140
        self.write(repository, revision_ids, forced_bases, out)
 
141
        return revision_ids
133
142
 
134
143
    def _write_main_header(self):
135
144
        """Write the header for the changes"""
136
145
        f = self.to_file
137
146
        f.write(_get_bundle_header('0.8'))
138
 
        f.write('#\n')
 
147
        f.write(b'#\n')
139
148
 
140
149
    def _write(self, key, value, indent=1, trailing_space_when_empty=False):
141
150
        """Write out meta information, with proper indenting, etc.
149
158
        if indent < 1:
150
159
            raise ValueError('indentation must be greater than 0')
151
160
        f = self.to_file
152
 
        f.write('#' + (' ' * indent))
 
161
        f.write(b'#' + (b' ' * indent))
153
162
        f.write(key.encode('utf-8'))
154
163
        if not value:
155
164
            if trailing_space_when_empty and value == '':
156
 
                f.write(': \n')
 
165
                f.write(b': \n')
157
166
            else:
158
 
                f.write(':\n')
159
 
        elif isinstance(value, str):
160
 
            f.write(': ')
 
167
                f.write(b':\n')
 
168
        elif isinstance(value, bytes):
 
169
            f.write(b': ')
161
170
            f.write(value)
162
 
            f.write('\n')
163
 
        elif isinstance(value, unicode):
164
 
            f.write(': ')
 
171
            f.write(b'\n')
 
172
        elif isinstance(value, text_type):
 
173
            f.write(b': ')
165
174
            f.write(value.encode('utf-8'))
166
 
            f.write('\n')
 
175
            f.write(b'\n')
167
176
        else:
168
 
            f.write(':\n')
 
177
            f.write(b':\n')
169
178
            for entry in value:
170
 
                f.write('#' + (' ' * (indent+2)))
171
 
                if isinstance(entry, str):
 
179
                f.write(b'#' + (b' ' * (indent + 2)))
 
180
                if isinstance(entry, bytes):
172
181
                    f.write(entry)
173
182
                else:
174
183
                    f.write(entry.encode('utf-8'))
175
 
                f.write('\n')
 
184
                f.write(b'\n')
176
185
 
177
186
    def _write_revisions(self, pb):
178
187
        """Write the information for all of the revisions."""
225
234
        w('message', rev.message.split('\n'))
226
235
        w('committer', rev.committer)
227
236
        w('date', format_highres_date(rev.timestamp, rev.timezone))
228
 
        self.to_file.write('\n')
 
237
        self.to_file.write(b'\n')
229
238
 
230
239
        self._write_delta(rev_tree, base_tree, rev.revision_id, force_binary)
231
240
 
243
252
                            trailing_space_when_empty=True)
244
253
 
245
254
        # Add an extra blank space at the end
246
 
        self.to_file.write('\n')
 
255
        self.to_file.write(b'\n')
247
256
 
248
257
    def _write_action(self, name, parameters, properties=None):
249
258
        if properties is None:
250
259
            properties = []
251
260
        p_texts = ['%s:%s' % v for v in properties]
252
 
        self.to_file.write('=== ')
253
 
        self.to_file.write(' '.join([name]+parameters).encode('utf-8'))
 
261
        self.to_file.write(b'=== ')
 
262
        self.to_file.write(' '.join([name] + parameters).encode('utf-8'))
254
263
        self.to_file.write(' // '.join(p_texts).encode('utf-8'))
255
 
        self.to_file.write('\n')
 
264
        self.to_file.write(b'\n')
256
265
 
257
266
    def _write_delta(self, new_tree, old_tree, default_revision_id,
258
267
                     force_binary):
262
271
        new_label = ''
263
272
 
264
273
        def do_diff(file_id, old_path, new_path, action, force_binary):
265
 
            def tree_lines(tree, require_text=False):
266
 
                if file_id in tree:
267
 
                    tree_file = tree.get_file(file_id)
 
274
            def tree_lines(tree, path, require_text=False):
 
275
                if tree.has_id(file_id):
 
276
                    tree_file = tree.get_file(path)
268
277
                    if require_text is True:
269
278
                        tree_file = text_file(tree_file)
270
279
                    return tree_file.readlines()
274
283
            try:
275
284
                if force_binary:
276
285
                    raise errors.BinaryFile()
277
 
                old_lines = tree_lines(old_tree, require_text=True)
278
 
                new_lines = tree_lines(new_tree, require_text=True)
 
286
                old_lines = tree_lines(old_tree, old_path, require_text=True)
 
287
                new_lines = tree_lines(new_tree, new_path, require_text=True)
279
288
                action.write(self.to_file)
280
289
                internal_diff(old_path, old_lines, new_path, new_lines,
281
290
                              self.to_file)
282
291
            except errors.BinaryFile:
283
 
                old_lines = tree_lines(old_tree, require_text=False)
284
 
                new_lines = tree_lines(new_tree, require_text=False)
 
292
                old_lines = tree_lines(old_tree, old_path, require_text=False)
 
293
                new_lines = tree_lines(new_tree, new_path, require_text=False)
285
294
                action.add_property('encoding', 'base64')
286
295
                action.write(self.to_file)
287
296
                binary_diff(old_path, old_lines, new_path, new_lines,
289
298
 
290
299
        def finish_action(action, file_id, kind, meta_modified, text_modified,
291
300
                          old_path, new_path):
292
 
            entry = new_tree.inventory[file_id]
 
301
            entry = new_tree.root_inventory.get_entry(file_id)
293
302
            if entry.revision != default_revision_id:
294
303
                action.add_utf8_property('last-changed', entry.revision)
295
304
            if meta_modified:
307
316
            action = Action('removed', [kind, path]).write(self.to_file)
308
317
 
309
318
        for path, file_id, kind in delta.added:
310
 
            action = Action('added', [kind, path], [('file-id', file_id)])
311
 
            meta_modified = (kind=='file' and
312
 
                             new_tree.is_executable(file_id))
 
319
            action = Action(
 
320
                'added', [kind, path], [('file-id', file_id.decode('utf-8'))])
 
321
            meta_modified = (kind == 'file' and
 
322
                             new_tree.is_executable(path))
313
323
            finish_action(action, file_id, kind, meta_modified, True,
314
324
                          DEVNULL, path)
315
325
 
326
336
                          path, path)
327
337
 
328
338
        for path, file_id, kind in delta.unchanged:
329
 
            ie = new_tree.inventory[file_id]
330
 
            new_rev = getattr(ie, 'revision', None)
 
339
            new_rev = new_tree.get_file_revision(path)
331
340
            if new_rev is None:
332
341
                continue
333
 
            old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
 
342
            old_rev = old_tree.get_file_revision(old_tree.id2path(file_id))
334
343
            if new_rev != old_rev:
335
 
                action = Action('modified', [ie.kind,
336
 
                                             new_tree.id2path(ie.file_id)])
337
 
                action.add_utf8_property('last-changed', ie.revision)
 
344
                action = Action('modified', [new_tree.kind(path), path])
 
345
                action.add_utf8_property('last-changed', new_rev)
338
346
                action.write(self.to_file)
339
347
 
340
348
 
342
350
    """This class reads in a bundle from a file, and returns
343
351
    a Bundle object, which can then be applied against a tree.
344
352
    """
 
353
 
345
354
    def __init__(self, from_file):
346
355
        """Read in the bundle from the file.
347
356
 
363
372
        return BundleInfo08()
364
373
 
365
374
    def _read(self):
366
 
        self._next().next()
 
375
        next(self._next())
367
376
        while self._next_line is not None:
368
377
            if not self._read_revision_header():
369
378
                break
401
410
        for line in self._next():
402
411
            # The bzr header is terminated with a blank line
403
412
            # which does not start with '#'
404
 
            if line is None or line == '\n':
 
413
            if line is None or line == b'\n':
405
414
                break
406
 
            if not line.startswith('#'):
 
415
            if not line.startswith(b'#'):
407
416
                continue
408
417
            found_something = True
409
418
            self._handle_next(line)
415
424
    def _read_next_entry(self, line, indent=1):
416
425
        """Read in a key-value pair
417
426
        """
418
 
        if not line.startswith('#'):
 
427
        if not line.startswith(b'#'):
419
428
            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:
 
429
        line = line[1:-1].decode('utf-8')  # Remove the '#' and '\n'
 
430
        if line[:indent] == ' ' * indent:
422
431
            line = line[indent:]
423
432
        if not line:
424
 
            return None, None# Ignore blank lines
 
433
            return None, None  # Ignore blank lines
425
434
 
426
435
        loc = line.find(': ')
427
436
        if loc != -1:
428
437
            key = line[:loc]
429
 
            value = line[loc+2:]
 
438
            value = line[loc + 2:]
430
439
            if not value:
431
 
                value = self._read_many(indent=indent+2)
 
440
                value = self._read_many(indent=indent + 2)
432
441
        elif line[-1:] == ':':
433
442
            key = line[:-1]
434
 
            value = self._read_many(indent=indent+2)
 
443
            value = self._read_many(indent=indent + 2)
435
444
        else:
436
445
            raise errors.MalformedHeader('While looking for key: value pairs,'
437
 
                    ' did not find the colon %r' % (line))
 
446
                                         ' did not find the colon %r' % (line))
438
447
 
439
448
        key = key.replace(' ', '_')
440
449
        #mutter('found %s: %s' % (key, value))
455
464
                    value = value.encode('utf8')
456
465
                elif key in ('parent_ids'):
457
466
                    value = [v.encode('utf8') for v in value]
 
467
                elif key in ('testament_sha1', 'inventory_sha1', 'sha1'):
 
468
                    value = value.encode('ascii')
458
469
                setattr(revision_info, key, value)
459
470
            else:
460
471
                raise errors.MalformedHeader('Duplicated Key: %s' % key)
470
481
        does not start properly indented.
471
482
        """
472
483
        values = []
473
 
        start = '#' + (' '*indent)
 
484
        start = b'#' + (b' ' * indent)
474
485
 
475
 
        if self._next_line is None or self._next_line[:len(start)] != start:
 
486
        if self._next_line is None or not self._next_line.startswith(start):
476
487
            return values
477
488
 
478
489
        for line in self._next():
479
490
            values.append(line[len(start):-1].decode('utf-8'))
480
 
            if self._next_line is None or self._next_line[:len(start)] != start:
 
491
            if self._next_line is None or not self._next_line.startswith(start):
481
492
                break
482
493
        return values
483
494
 
489
500
        """
490
501
        #mutter('_read_one_patch: %r' % self._next_line)
491
502
        # Peek and see if there are no patches
492
 
        if self._next_line is None or self._next_line.startswith('#'):
 
503
        if self._next_line is None or self._next_line.startswith(b'#'):
493
504
            return None, [], False
494
505
 
495
506
        first = True
496
507
        lines = []
497
508
        for line in self._next():
498
509
            if first:
499
 
                if not line.startswith('==='):
 
510
                if not line.startswith(b'==='):
500
511
                    raise errors.MalformedPatches('The first line of all patches'
501
 
                        ' should be a bzr meta line "==="'
502
 
                        ': %r' % line)
 
512
                                                  ' should be a bzr meta line "==="'
 
513
                                                  ': %r' % line)
503
514
                action = line[4:-1].decode('utf-8')
504
 
            elif line.startswith('... '):
505
 
                action += line[len('... '):-1].decode('utf-8')
 
515
            elif line.startswith(b'... '):
 
516
                action += line[len(b'... '):-1].decode('utf-8')
506
517
 
507
518
            if (self._next_line is not None and
508
 
                self._next_line.startswith('===')):
 
519
                    self._next_line.startswith(b'===')):
509
520
                return action, lines, True
510
 
            elif self._next_line is None or self._next_line.startswith('#'):
 
521
            elif self._next_line is None or self._next_line.startswith(b'#'):
511
522
                return action, lines, False
512
523
 
513
524
            if first:
514
525
                first = False
515
 
            elif not line.startswith('... '):
 
526
            elif not line.startswith(b'... '):
516
527
                lines.append(line)
517
528
 
518
529
        return action, lines, False
538
549
            self._handle_next(line)
539
550
            if self._next_line is None:
540
551
                break
541
 
            if not self._next_line.startswith('#'):
 
552
            if not self._next_line.startswith(b'#'):
542
553
                # Consume the trailing \n and stop processing
543
 
                self._next().next()
 
554
                next(self._next())
544
555
                break
545
556
 
 
557
 
546
558
class BundleInfo08(BundleInfo):
547
559
 
548
560
    def _update_tree(self, bundle_tree, revision_id):
553
565
        testament = StrictTestament.from_revision(repository, revision_id)
554
566
        return testament.as_sha1()
555
567
 
556
 
    def _testament_sha1(self, revision, inventory):
557
 
        return StrictTestament(revision, inventory).as_sha1()
 
568
    def _testament_sha1(self, revision, tree):
 
569
        return StrictTestament(revision, tree).as_sha1()