~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2009 Canonical 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
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 errors
 
22
from bzrlib import (
 
23
    errors,
 
24
    ui,
 
25
    )
23
26
from bzrlib.bundle.serializer import (BundleSerializer,
24
27
                                      _get_bundle_header,
25
28
                                     )
26
29
from bzrlib.bundle.serializer import binary_diff
27
 
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
 
30
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo)
28
31
from bzrlib.diff import internal_diff
29
 
from bzrlib.osutils import pathjoin
30
 
from bzrlib.progress import DummyProgress
31
32
from bzrlib.revision import NULL_REVISION
32
 
import bzrlib.ui
33
33
from bzrlib.testament import StrictTestament
34
34
from bzrlib.timestamp import (
35
35
    format_highres_date,
36
 
    unpack_highres_date,
37
 
)
 
36
    )
38
37
from bzrlib.textfile import text_file
39
38
from bzrlib.trace import mutter
40
39
 
119
118
        source.lock_read()
120
119
        try:
121
120
            self._write_main_header()
122
 
            pb = DummyProgress()
 
121
            pb = ui.ui_factory.nested_progress_bar()
123
122
            try:
124
123
                self._write_revisions(pb)
125
124
            finally:
126
 
                pass
127
 
                #pb.finished()
 
125
                pb.finished()
128
126
        finally:
129
127
            source.unlock()
130
128
 
146
144
            If this parameter is True, and value is the empty string, we will
147
145
            write an extra space.
148
146
        """
149
 
        assert indent > 0, 'indentation must be greater than 0'
 
147
        if indent < 1:
 
148
            raise ValueError('indentation must be greater than 0')
150
149
        f = self.to_file
151
150
        f.write('#' + (' ' * indent))
152
151
        f.write(key.encode('utf-8'))
182
181
 
183
182
        i_max = len(self.revision_ids)
184
183
        for i, rev_id in enumerate(self.revision_ids):
185
 
            pb.update("Generating revsion data", i, i_max)
 
184
            pb.update("Generating revision data", i, i_max)
186
185
            rev = self.source.get_revision(rev_id)
187
186
            if rev_id == last_rev_id:
188
187
                rev_tree = last_rev_tree
205
204
            else:
206
205
                base_tree = self.source.revision_tree(base_id)
207
206
            force_binary = (i != 0)
208
 
            self._write_revision(rev, rev_tree, base_id, base_tree, 
 
207
            self._write_revision(rev, rev_tree, base_id, base_tree,
209
208
                                 explicit_base, force_binary)
210
209
 
211
210
            last_rev_id = base_id
212
211
            last_rev_tree = base_tree
213
212
 
214
213
    def _testament_sha1(self, revision_id):
215
 
        return StrictTestament.from_revision(self.source, 
 
214
        return StrictTestament.from_revision(self.source,
216
215
                                             revision_id).as_sha1()
217
216
 
218
 
    def _write_revision(self, rev, rev_tree, base_rev, base_tree, 
 
217
    def _write_revision(self, rev, rev_tree, base_rev, base_tree,
219
218
                        explicit_base, force_binary):
220
219
        """Write out the information for a revision."""
221
220
        def w(key, value):
240
239
            for name, value in sorted(rev.properties.items()):
241
240
                self._write(name, value, indent=3,
242
241
                            trailing_space_when_empty=True)
243
 
        
 
242
 
244
243
        # Add an extra blank space at the end
245
244
        self.to_file.write('\n')
246
245
 
253
252
        self.to_file.write(' // '.join(p_texts).encode('utf-8'))
254
253
        self.to_file.write('\n')
255
254
 
256
 
    def _write_delta(self, new_tree, old_tree, default_revision_id, 
 
255
    def _write_delta(self, new_tree, old_tree, default_revision_id,
257
256
                     force_binary):
258
257
        """Write out the changes between the trees."""
259
258
        DEVNULL = '/dev/null'
262
261
 
263
262
        def do_diff(file_id, old_path, new_path, action, force_binary):
264
263
            def tree_lines(tree, require_text=False):
265
 
                if file_id in tree:
 
264
                if tree.has_id(file_id):
266
265
                    tree_file = tree.get_file(file_id)
267
266
                    if require_text is True:
268
267
                        tree_file = text_file(tree_file)
276
275
                old_lines = tree_lines(old_tree, require_text=True)
277
276
                new_lines = tree_lines(new_tree, require_text=True)
278
277
                action.write(self.to_file)
279
 
                internal_diff(old_path, old_lines, new_path, new_lines, 
 
278
                internal_diff(old_path, old_lines, new_path, new_lines,
280
279
                              self.to_file)
281
280
            except errors.BinaryFile:
282
281
                old_lines = tree_lines(old_tree, require_text=False)
283
282
                new_lines = tree_lines(new_tree, require_text=False)
284
283
                action.add_property('encoding', 'base64')
285
284
                action.write(self.to_file)
286
 
                binary_diff(old_path, old_lines, new_path, new_lines, 
 
285
                binary_diff(old_path, old_lines, new_path, new_lines,
287
286
                            self.to_file)
288
287
 
289
288
        def finish_action(action, file_id, kind, meta_modified, text_modified,
290
289
                          old_path, new_path):
291
 
            entry = new_tree.inventory[file_id]
 
290
            entry = new_tree.root_inventory[file_id]
292
291
            if entry.revision != default_revision_id:
293
292
                action.add_utf8_property('last-changed', entry.revision)
294
293
            if meta_modified:
307
306
 
308
307
        for path, file_id, kind in delta.added:
309
308
            action = Action('added', [kind, path], [('file-id', file_id)])
310
 
            meta_modified = (kind=='file' and 
 
309
            meta_modified = (kind=='file' and
311
310
                             new_tree.is_executable(file_id))
312
311
            finish_action(action, file_id, kind, meta_modified, True,
313
312
                          DEVNULL, path)
325
324
                          path, path)
326
325
 
327
326
        for path, file_id, kind in delta.unchanged:
328
 
            ie = new_tree.inventory[file_id]
329
 
            new_rev = getattr(ie, 'revision', None)
 
327
            new_rev = new_tree.get_file_revision(file_id)
330
328
            if new_rev is None:
331
329
                continue
332
 
            old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
 
330
            old_rev = old_tree.get_file_revision(file_id)
333
331
            if new_rev != old_rev:
334
 
                action = Action('modified', [ie.kind, 
335
 
                                             new_tree.id2path(ie.file_id)])
336
 
                action.add_utf8_property('last-changed', ie.revision)
 
332
                action = Action('modified', [new_tree.kind(file_id),
 
333
                                             new_tree.id2path(file_id)])
 
334
                action.add_utf8_property('last-changed', new_rev)
337
335
                action.write(self.to_file)
338
336
 
339
337
 
349
347
        object.__init__(self)
350
348
        self.from_file = iter(from_file)
351
349
        self._next_line = None
352
 
        
 
350
 
353
351
        self.info = self._get_info()
354
352
        # We put the actual inventory ids in the footer, so that the patch
355
353
        # is easier to read for humans.
460
458
        else:
461
459
            # What do we do with a key we don't recognize
462
460
            raise errors.MalformedHeader('Unknown Key: "%s"' % key)
463
 
    
 
461
 
464
462
    def _read_many(self, indent):
465
463
        """If a line ends with no entry, that means that it should be
466
464
        followed with multiple lines of values.
503
501
            elif line.startswith('... '):
504
502
                action += line[len('... '):-1].decode('utf-8')
505
503
 
506
 
            if (self._next_line is not None and 
 
504
            if (self._next_line is not None and
507
505
                self._next_line.startswith('===')):
508
506
                return action, lines, True
509
507
            elif self._next_line is None or self._next_line.startswith('#'):
515
513
                lines.append(line)
516
514
 
517
515
        return action, lines, False
518
 
            
 
516
 
519
517
    def _read_patches(self):
520
518
        do_continue = True
521
519
        revision_actions = []
523
521
            action, lines, do_continue = self._read_one_patch()
524
522
            if action is not None:
525
523
                revision_actions.append((action, lines))
526
 
        assert self.info.revisions[-1].tree_actions is None
 
524
        if self.info.revisions[-1].tree_actions is not None:
 
525
            raise AssertionError()
527
526
        self.info.revisions[-1].tree_actions = revision_actions
528
527
 
529
528
    def _read_footer(self):
551
550
        testament = StrictTestament.from_revision(repository, revision_id)
552
551
        return testament.as_sha1()
553
552
 
554
 
    def _testament_sha1(self, revision, inventory):
555
 
        return StrictTestament(revision, inventory).as_sha1()
 
553
    def _testament_sha1(self, revision, tree):
 
554
        return StrictTestament(revision, tree).as_sha1()