17
17
"""Serializer factory for reading and writing bundles.
20
from __future__ import absolute_import
26
from bzrlib.bundle.serializer import (BundleSerializer,
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 (
30
from breezy.bundle.serializer import binary_diff
31
from breezy.bundle.bundle_data import (
35
from breezy.diff import internal_diff
36
from breezy.revision import NULL_REVISION
37
from breezy.sixish import text_type
38
from breezy.bzr.testament import StrictTestament
39
from breezy.timestamp import (
36
40
format_highres_date,
39
from bzrlib.textfile import text_file
40
from bzrlib.trace import mutter
42
from breezy.textfile import text_file
43
from breezy.trace import mutter
42
45
bool_text = {True: 'yes', False: 'no'}
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:
76
79
p_texts.append(prop[0])
79
p_texts.append('%s:%s' % prop)
81
p_texts.append('%s:%s' % prop)
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')
94
94
class BundleSerializerV08(BundleSerializer):
96
97
"""Read the rest of the bundles from the supplied file.
117
118
self.forced_bases = forced_bases
119
120
self.check_compatible()
121
with source.lock_read():
122
122
self._write_main_header()
123
pb = ui.ui_factory.nested_progress_bar()
123
with ui.ui_factory.nested_progress_bar() as pb:
125
124
self._write_revisions(pb)
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,
134
revision_ids = list(repository.get_graph().iter_topo_order(
136
revision_ids.reverse()
137
self.write(repository, revision_ids, forced_bases, out)
134
140
def _write_main_header(self):
135
141
"""Write the header for the changes"""
137
143
f.write(_get_bundle_header('0.8'))
140
146
def _write(self, key, value, indent=1, trailing_space_when_empty=False):
141
147
"""Write out meta information, with proper indenting, etc.
150
156
raise ValueError('indentation must be greater than 0')
152
f.write('#' + (' ' * indent))
158
f.write(b'#' + (b' ' * indent))
153
159
f.write(key.encode('utf-8'))
155
161
if trailing_space_when_empty and value == '':
159
elif isinstance(value, str):
165
elif isinstance(value, bytes):
163
elif isinstance(value, unicode):
169
elif isinstance(value, text_type):
165
171
f.write(value.encode('utf-8'))
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):
174
180
f.write(entry.encode('utf-8'))
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')
230
236
self._write_delta(rev_tree, base_tree, rev.revision_id, force_binary)
243
249
trailing_space_when_empty=True)
245
251
# Add an extra blank space at the end
246
self.to_file.write('\n')
252
self.to_file.write(b'\n')
248
254
def _write_action(self, name, parameters, properties=None):
249
255
if properties is None:
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')
257
263
def _write_delta(self, new_tree, old_tree, default_revision_id,
264
270
def do_diff(file_id, old_path, new_path, action, force_binary):
265
def tree_lines(tree, require_text=False):
267
tree_file = tree.get_file(file_id)
271
def tree_lines(tree, path, require_text=False):
272
if tree.has_id(file_id):
273
tree_file = tree.get_file(path)
268
274
if require_text is True:
269
275
tree_file = text_file(tree_file)
270
276
return tree_file.readlines()
276
282
raise errors.BinaryFile()
277
old_lines = tree_lines(old_tree, require_text=True)
278
new_lines = tree_lines(new_tree, require_text=True)
283
old_lines = tree_lines(old_tree, old_path, require_text=True)
284
new_lines = tree_lines(new_tree, new_path, require_text=True)
279
285
action.write(self.to_file)
280
286
internal_diff(old_path, old_lines, new_path, new_lines,
282
288
except errors.BinaryFile:
283
old_lines = tree_lines(old_tree, require_text=False)
284
new_lines = tree_lines(new_tree, require_text=False)
289
old_lines = tree_lines(old_tree, old_path, require_text=False)
290
new_lines = tree_lines(new_tree, new_path, require_text=False)
285
291
action.add_property('encoding', 'base64')
286
292
action.write(self.to_file)
287
293
binary_diff(old_path, old_lines, new_path, new_lines,
290
296
def finish_action(action, file_id, kind, meta_modified, text_modified,
291
297
old_path, new_path):
292
entry = new_tree.inventory[file_id]
298
entry = new_tree.root_inventory.get_entry(file_id)
293
299
if entry.revision != default_revision_id:
294
300
action.add_utf8_property('last-changed', entry.revision)
295
301
if meta_modified:
307
313
action = Action('removed', [kind, path]).write(self.to_file)
309
315
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))
317
'added', [kind, path], [('file-id', file_id.decode('utf-8'))])
318
meta_modified = (kind == 'file' and
319
new_tree.is_executable(path))
313
320
finish_action(action, file_id, kind, meta_modified, True,
328
335
for path, file_id, kind in delta.unchanged:
329
ie = new_tree.inventory[file_id]
330
new_rev = getattr(ie, 'revision', None)
336
new_rev = new_tree.get_file_revision(path)
331
337
if new_rev is None:
333
old_rev = getattr(old_tree.inventory[ie.file_id], 'revision', None)
339
old_rev = old_tree.get_file_revision(old_tree.id2path(file_id))
334
340
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)
341
action = Action('modified', [new_tree.kind(path), path])
342
action.add_utf8_property('last-changed', new_rev)
338
343
action.write(self.to_file)
342
347
"""This class reads in a bundle from a file, and returns
343
348
a Bundle object, which can then be applied against a tree.
345
351
def __init__(self, from_file):
346
352
"""Read in the bundle from the file.
401
407
for line in self._next():
402
408
# The bzr header is terminated with a blank line
403
409
# which does not start with '#'
404
if line is None or line == '\n':
410
if line is None or line == b'\n':
406
if not line.startswith('#'):
412
if not line.startswith(b'#'):
408
414
found_something = True
409
415
self._handle_next(line)
415
421
def _read_next_entry(self, line, indent=1):
416
422
"""Read in a key-value pair
418
if not line.startswith('#'):
424
if not line.startswith(b'#'):
419
425
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:
426
line = line[1:-1].decode('utf-8') # Remove the '#' and '\n'
427
if line[:indent] == ' ' * indent:
422
428
line = line[indent:]
424
return None, None# Ignore blank lines
430
return None, None # Ignore blank lines
426
432
loc = line.find(': ')
435
value = line[loc + 2:]
431
value = self._read_many(indent=indent+2)
437
value = self._read_many(indent=indent + 2)
432
438
elif line[-1:] == ':':
434
value = self._read_many(indent=indent+2)
440
value = self._read_many(indent=indent + 2)
436
442
raise errors.MalformedHeader('While looking for key: value pairs,'
437
' did not find the colon %r' % (line))
443
' did not find the colon %r' % (line))
439
445
key = key.replace(' ', '_')
440
446
#mutter('found %s: %s' % (key, value))
455
461
value = value.encode('utf8')
456
462
elif key in ('parent_ids'):
457
463
value = [v.encode('utf8') for v in value]
464
elif key in ('testament_sha1', 'inventory_sha1', 'sha1'):
465
value = value.encode('ascii')
458
466
setattr(revision_info, key, value)
460
468
raise errors.MalformedHeader('Duplicated Key: %s' % key)
470
478
does not start properly indented.
473
start = '#' + (' '*indent)
481
start = b'#' + (b' ' * indent)
475
if self._next_line is None or self._next_line[:len(start)] != start:
483
if self._next_line is None or not self._next_line.startswith(start):
478
486
for line in self._next():
479
487
values.append(line[len(start):-1].decode('utf-8'))
480
if self._next_line is None or self._next_line[:len(start)] != start:
488
if self._next_line is None or not self._next_line.startswith(start):
490
498
#mutter('_read_one_patch: %r' % self._next_line)
491
499
# Peek and see if there are no patches
492
if self._next_line is None or self._next_line.startswith('#'):
500
if self._next_line is None or self._next_line.startswith(b'#'):
493
501
return None, [], False
497
505
for line in self._next():
499
if not line.startswith('==='):
507
if not line.startswith(b'==='):
500
508
raise errors.MalformedPatches('The first line of all patches'
501
' should be a bzr meta line "==="'
509
' should be a bzr meta line "==="'
503
511
action = line[4:-1].decode('utf-8')
504
elif line.startswith('... '):
505
action += line[len('... '):-1].decode('utf-8')
512
elif line.startswith(b'... '):
513
action += line[len(b'... '):-1].decode('utf-8')
507
515
if (self._next_line is not None and
508
self._next_line.startswith('===')):
516
self._next_line.startswith(b'===')):
509
517
return action, lines, True
510
elif self._next_line is None or self._next_line.startswith('#'):
518
elif self._next_line is None or self._next_line.startswith(b'#'):
511
519
return action, lines, False
515
elif not line.startswith('... '):
523
elif not line.startswith(b'... '):
516
524
lines.append(line)
518
526
return action, lines, False
538
546
self._handle_next(line)
539
547
if self._next_line is None:
541
if not self._next_line.startswith('#'):
549
if not self._next_line.startswith(b'#'):
542
550
# Consume the trailing \n and stop processing
546
555
class BundleInfo08(BundleInfo):
548
557
def _update_tree(self, bundle_tree, revision_id):
553
562
testament = StrictTestament.from_revision(repository, revision_id)
554
563
return testament.as_sha1()
556
def _testament_sha1(self, revision, inventory):
557
return StrictTestament(revision, inventory).as_sha1()
565
def _testament_sha1(self, revision, tree):
566
return StrictTestament(revision, tree).as_sha1()