1
# (C) 2005 Canonical Development Ltd
1
# Copyright (C) 2005, 2006, 2009 Canonical Ltd
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.
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.
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
17
17
"""Serializer factory for reading and writing bundles.
22
from bzrlib.bundle.serializer import (BundleSerializer,
26
from bzrlib.bundle.serializer import (BundleSerializer,
27
29
from bzrlib.bundle.serializer import binary_diff
28
from bzrlib.delta import compare_trees
30
from bzrlib.bundle.bundle_data import (RevisionInfo, BundleInfo, BundleTree)
29
31
from bzrlib.diff import internal_diff
30
import bzrlib.errors as errors
31
32
from bzrlib.osutils import pathjoin
32
from bzrlib.progress import DummyProgress
33
33
from bzrlib.revision import NULL_REVISION
34
from bzrlib.rio import RioWriter, read_stanzas
36
34
from bzrlib.testament import StrictTestament
35
from bzrlib.timestamp import (
37
39
from bzrlib.textfile import text_file
40
from bzrlib.trace import mutter
39
42
bool_text = {True: 'yes', False: 'no'}
107
116
self.revision_ids = revision_ids
108
117
self.forced_bases = forced_bases
119
self.check_compatible()
110
120
source.lock_read()
112
122
self._write_main_header()
123
pb = ui.ui_factory.nested_progress_bar()
115
125
self._write_revisions(pb)
131
def write_bundle(self, repository, target, base, fileobj):
132
return self._write_bundle(repository, target, base, fileobj)
122
134
def _write_main_header(self):
123
135
"""Write the header for the changes"""
125
f.write(BUNDLE_HEADER)
137
f.write(_get_bundle_header('0.8'))
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'
140
def _write(self, key, value, indent=1, trailing_space_when_empty=False):
141
"""Write out meta information, with proper indenting, etc.
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.
150
raise ValueError('indentation must be greater than 0')
133
152
f.write('#' + (' ' * indent))
134
153
f.write(key.encode('utf-8'))
137
elif isinstance(value, basestring):
155
if trailing_space_when_empty and value == '':
159
elif isinstance(value, str):
163
elif isinstance(value, unicode):
139
165
f.write(value.encode('utf-8'))
276
raise errors.BinaryFile()
242
277
old_lines = tree_lines(old_tree, require_text=True)
243
278
new_lines = tree_lines(new_tree, require_text=True)
244
279
action.write(self.to_file)
245
internal_diff(old_path, old_lines, new_path, new_lines,
280
internal_diff(old_path, old_lines, new_path, new_lines,
247
282
except errors.BinaryFile:
248
283
old_lines = tree_lines(old_tree, require_text=False)
249
284
new_lines = tree_lines(new_tree, require_text=False)
250
285
action.add_property('encoding', 'base64')
251
286
action.write(self.to_file)
252
binary_diff(old_path, old_lines, new_path, new_lines,
287
binary_diff(old_path, old_lines, new_path, new_lines,
255
290
def finish_action(action, file_id, kind, meta_modified, text_modified,
256
291
old_path, new_path):
257
292
entry = new_tree.inventory[file_id]
258
293
if entry.revision != default_revision_id:
259
action.add_property('last-changed', entry.revision)
294
action.add_utf8_property('last-changed', entry.revision)
260
295
if meta_modified:
261
296
action.add_bool_property('executable', entry.executable)
262
297
if text_modified and kind == "symlink":
263
298
action.add_property('target', entry.symlink_target)
264
299
if text_modified and kind == "file":
265
do_diff(file_id, old_path, new_path, action)
300
do_diff(file_id, old_path, new_path, action, force_binary)
267
302
action.write(self.to_file)
269
delta = compare_trees(old_tree, new_tree, want_unchanged=True)
304
delta = new_tree.changes_from(old_tree, want_unchanged=True,
270
306
for path, file_id, kind in delta.removed:
271
307
action = Action('removed', [kind, path]).write(self.to_file)
273
309
for path, file_id, kind in delta.added:
274
310
action = Action('added', [kind, path], [('file-id', file_id)])
275
meta_modified = (kind=='file' and
311
meta_modified = (kind=='file' and
276
312
new_tree.is_executable(file_id))
277
313
finish_action(action, file_id, kind, meta_modified, True,
297
333
old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
298
334
if new_rev != old_rev:
299
action = Action('modified', [ie.kind,
335
action = Action('modified', [ie.kind,
300
336
new_tree.id2path(ie.file_id)])
301
action.add_property('last-changed', ie.revision)
337
action.add_utf8_property('last-changed', ie.revision)
302
338
action.write(self.to_file)
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.
345
def __init__(self, from_file):
346
"""Read in the bundle from the file.
348
:param from_file: A file-like object (must have iterator support).
350
object.__init__(self)
351
self.from_file = iter(from_file)
352
self._next_line = None
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.
363
return BundleInfo08()
367
while self._next_line is not None:
368
if not self._read_revision_header():
370
if self._next_line is None:
376
"""Make sure that the information read in makes sense
377
and passes appropriate checksums.
379
# Fill in all the missing blanks for the revisions
380
# and generate the real_revisions list.
381
self.info.complete_info()
384
"""yield the next line, but secretly
385
keep 1 extra line for peeking.
387
for line in self.from_file:
388
last = self._next_line
389
self._next_line = line
391
#mutter('yielding line: %r' % last)
393
last = self._next_line
394
self._next_line = None
395
#mutter('yielding line: %r' % last)
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':
406
if not line.startswith('#'):
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
415
def _read_next_entry(self, line, indent=1):
416
"""Read in a key-value pair
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:
424
return None, None# Ignore blank lines
426
loc = line.find(': ')
431
value = self._read_many(indent=indent+2)
432
elif line[-1:] == ':':
434
value = self._read_many(indent=indent+2)
436
raise errors.MalformedHeader('While looking for key: value pairs,'
437
' did not find the colon %r' % (line))
439
key = key.replace(' ', '_')
440
#mutter('found %s: %s' % (key, value))
443
def _handle_next(self, line):
446
key, value = self._read_next_entry(line, indent=1)
447
mutter('_handle_next %r => %r' % (key, value))
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)
460
raise errors.MalformedHeader('Duplicated Key: %s' % key)
462
# What do we do with a key we don't recognize
463
raise errors.MalformedHeader('Unknown Key: "%s"' % key)
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.
469
This detects the end of the list, because it will be a line that
470
does not start properly indented.
473
start = '#' + (' '*indent)
475
if self._next_line is None or self._next_line[:len(start)] != start:
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:
484
def _read_one_patch(self):
485
"""Read in one patch, return the complete patch, along with
488
:return: action, lines, do_continue
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
497
for line in self._next():
499
if not line.startswith('==='):
500
raise errors.MalformedPatches('The first line of all patches'
501
' should be a bzr meta line "==="'
503
action = line[4:-1].decode('utf-8')
504
elif line.startswith('... '):
505
action += line[len('... '):-1].decode('utf-8')
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
515
elif not line.startswith('... '):
518
return action, lines, False
520
def _read_patches(self):
522
revision_actions = []
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
531
def _read_footer(self):
532
"""Read the rest of the meta information.
534
:param first_line: The previous step iterates past what it
535
can handle. That extra line is given here.
537
for line in self._next():
538
self._handle_next(line)
539
if self._next_line is None:
541
if not self._next_line.startswith('#'):
542
# Consume the trailing \n and stop processing
546
class BundleInfo08(BundleInfo):
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)
552
def _testament_sha1_from_revision(self, repository, revision_id):
553
testament = StrictTestament.from_revision(repository, revision_id)
554
return testament.as_sha1()
556
def _testament_sha1(self, revision, tree):
557
return StrictTestament(revision, tree).as_sha1()