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

  • Committer: Jelmer Vernooij
  • Date: 2020-05-05 23:32:39 UTC
  • mto: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200505233239-kdmnmscn8eisltk6
Add a breezy.__main__ module.

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