1
# Copyright (C) 2005-2013, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
from io import BytesIO
23
import SocketServer as socketserver
31
revision as _mod_revision,
39
from ..bundle import read_mergeable_from_url
40
from ..bundle.apply_bundle import install_bundle, merge_bundle
41
from ..bundle.bundle_data import BundleTree
42
from ..directory_service import directories
43
from ..bundle.serializer import write_bundle, read_bundle, v09, v4
44
from ..bundle.serializer.v08 import BundleSerializerV08
45
from ..bundle.serializer.v09 import BundleSerializerV09
46
from ..bundle.serializer.v4 import BundleSerializerV4
47
from ..bzr import knitrepo
54
from ..transform import TreeTransform
57
def get_text(vf, key):
58
"""Get the fulltext for a given revision id that is present in the vf"""
59
stream = vf.get_record_stream([key], 'unordered', True)
61
return record.get_bytes_as('fulltext')
64
def get_inventory_text(repo, revision_id):
65
"""Get the fulltext for the inventory at revision id"""
66
with repo.lock_read():
67
return get_text(repo.inventories, (revision_id,))
70
class MockTree(object):
73
from ..bzr.inventory import InventoryDirectory, ROOT_ID
75
self.paths = {ROOT_ID: ""}
76
self.ids = {"": ROOT_ID}
78
self.root = InventoryDirectory(ROOT_ID, '', None)
80
inventory = property(lambda x: x)
81
root_inventory = property(lambda x: x)
83
def get_root_id(self):
84
return self.root.file_id
86
def all_file_ids(self):
87
return set(self.paths.keys())
89
def all_versioned_paths(self):
90
return set(self.paths.values())
92
def is_executable(self, path):
93
# Not all the files are executable.
96
def __getitem__(self, file_id):
97
if file_id == self.root.file_id:
100
return self.make_entry(file_id, self.paths[file_id])
102
def get_entry_by_path(self, path):
103
return self[self.path2id(path)]
105
def parent_id(self, file_id):
106
parent_dir = os.path.dirname(self.paths[file_id])
109
return self.ids[parent_dir]
111
def iter_entries(self):
112
for path, file_id in self.ids.items():
113
yield path, self[file_id]
115
def kind(self, path):
116
if path in self.contents:
122
def make_entry(self, file_id, path):
123
from ..bzr.inventory import (InventoryFile, InventoryDirectory,
125
if not isinstance(file_id, bytes):
126
raise TypeError(file_id)
127
name = os.path.basename(path)
128
kind = self.kind(path)
129
parent_id = self.parent_id(file_id)
130
text_sha_1, text_size = self.contents_stats(path)
131
if kind == 'directory':
132
ie = InventoryDirectory(file_id, name, parent_id)
134
ie = InventoryFile(file_id, name, parent_id)
135
ie.text_sha1 = text_sha_1
136
ie.text_size = text_size
137
elif kind == 'symlink':
138
ie = InventoryLink(file_id, name, parent_id)
140
raise errors.BzrError('unknown kind %r' % kind)
143
def add_dir(self, file_id, path):
144
if not isinstance(file_id, bytes):
145
raise TypeError(file_id)
146
self.paths[file_id] = path
147
self.ids[path] = file_id
149
def add_file(self, file_id, path, contents):
150
if not isinstance(file_id, bytes):
151
raise TypeError(file_id)
152
self.add_dir(file_id, path)
153
self.contents[path] = contents
155
def path2id(self, path):
156
return self.ids.get(path)
158
def id2path(self, file_id):
159
return self.paths.get(file_id)
161
def has_id(self, file_id):
162
return self.id2path(file_id) is not None
164
def get_file(self, path):
167
result.write(self.contents[path])
169
raise errors.NoSuchFile(path)
173
def get_file_revision(self, path):
174
return self.inventory.get_entry_by_path(path).revision
176
def get_file_size(self, path):
177
return self.inventory.get_entry_by_path(path).text_size
179
def get_file_sha1(self, path, file_id=None):
180
return self.inventory.get_entry_by_path(path).text_sha1
182
def contents_stats(self, path):
183
if path not in self.contents:
185
text_sha1 = osutils.sha_file(self.get_file(path))
186
return text_sha1, len(self.contents[path])
189
class BTreeTester(tests.TestCase):
190
"""A simple unittest tester for the BundleTree class."""
192
def make_tree_1(self):
194
mtree.add_dir(b"a", "grandparent")
195
mtree.add_dir(b"b", "grandparent/parent")
196
mtree.add_file(b"c", "grandparent/parent/file", b"Hello\n")
197
mtree.add_dir(b"d", "grandparent/alt_parent")
198
return BundleTree(mtree, b''), mtree
200
def test_renames(self):
201
"""Ensure that file renames have the proper effect on children"""
202
btree = self.make_tree_1()[0]
203
self.assertEqual(btree.old_path("grandparent"), "grandparent")
204
self.assertEqual(btree.old_path("grandparent/parent"),
205
"grandparent/parent")
206
self.assertEqual(btree.old_path("grandparent/parent/file"),
207
"grandparent/parent/file")
209
self.assertEqual(btree.id2path(b"a"), "grandparent")
210
self.assertEqual(btree.id2path(b"b"), "grandparent/parent")
211
self.assertEqual(btree.id2path(b"c"), "grandparent/parent/file")
213
self.assertEqual(btree.path2id("grandparent"), b"a")
214
self.assertEqual(btree.path2id("grandparent/parent"), b"b")
215
self.assertEqual(btree.path2id("grandparent/parent/file"), b"c")
217
self.assertIs(btree.path2id("grandparent2"), None)
218
self.assertIs(btree.path2id("grandparent2/parent"), None)
219
self.assertIs(btree.path2id("grandparent2/parent/file"), None)
221
btree.note_rename("grandparent", "grandparent2")
222
self.assertIs(btree.old_path("grandparent"), None)
223
self.assertIs(btree.old_path("grandparent/parent"), None)
224
self.assertIs(btree.old_path("grandparent/parent/file"), None)
226
self.assertEqual(btree.id2path(b"a"), "grandparent2")
227
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent")
228
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent/file")
230
self.assertEqual(btree.path2id("grandparent2"), b"a")
231
self.assertEqual(btree.path2id("grandparent2/parent"), b"b")
232
self.assertEqual(btree.path2id("grandparent2/parent/file"), b"c")
234
self.assertTrue(btree.path2id("grandparent") is None)
235
self.assertTrue(btree.path2id("grandparent/parent") is None)
236
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
238
btree.note_rename("grandparent/parent", "grandparent2/parent2")
239
self.assertEqual(btree.id2path(b"a"), "grandparent2")
240
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent2")
241
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent2/file")
243
self.assertEqual(btree.path2id("grandparent2"), b"a")
244
self.assertEqual(btree.path2id("grandparent2/parent2"), b"b")
245
self.assertEqual(btree.path2id("grandparent2/parent2/file"), b"c")
247
self.assertTrue(btree.path2id("grandparent2/parent") is None)
248
self.assertTrue(btree.path2id("grandparent2/parent/file") is None)
250
btree.note_rename("grandparent/parent/file",
251
"grandparent2/parent2/file2")
252
self.assertEqual(btree.id2path(b"a"), "grandparent2")
253
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent2")
254
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent2/file2")
256
self.assertEqual(btree.path2id("grandparent2"), b"a")
257
self.assertEqual(btree.path2id("grandparent2/parent2"), b"b")
258
self.assertEqual(btree.path2id("grandparent2/parent2/file2"), b"c")
260
self.assertTrue(btree.path2id("grandparent2/parent2/file") is None)
262
def test_moves(self):
263
"""Ensure that file moves have the proper effect on children"""
264
btree = self.make_tree_1()[0]
265
btree.note_rename("grandparent/parent/file",
266
"grandparent/alt_parent/file")
267
self.assertEqual(btree.id2path(b"c"), "grandparent/alt_parent/file")
268
self.assertEqual(btree.path2id("grandparent/alt_parent/file"), b"c")
269
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
271
def unified_diff(self, old, new):
273
diff.internal_diff("old", old, "new", new, out)
277
def make_tree_2(self):
278
btree = self.make_tree_1()[0]
279
btree.note_rename("grandparent/parent/file",
280
"grandparent/alt_parent/file")
281
self.assertTrue(btree.id2path(b"e") is None)
282
self.assertFalse(btree.is_versioned("grandparent/parent/file"))
283
btree.note_id(b"e", "grandparent/parent/file")
287
"""File/inventory adds"""
288
btree = self.make_tree_2()
289
add_patch = self.unified_diff([], [b"Extra cheese\n"])
290
btree.note_patch("grandparent/parent/file", add_patch)
291
btree.note_id(b'f', 'grandparent/parent/symlink', kind='symlink')
292
btree.note_target('grandparent/parent/symlink', 'venus')
293
self.adds_test(btree)
295
def adds_test(self, btree):
296
self.assertEqual(btree.id2path(b"e"), "grandparent/parent/file")
297
self.assertEqual(btree.path2id("grandparent/parent/file"), b"e")
298
with btree.get_file("grandparent/parent/file") as f:
299
self.assertEqual(f.read(), b"Extra cheese\n")
301
btree.get_symlink_target('grandparent/parent/symlink'), 'venus')
303
def test_adds2(self):
304
"""File/inventory adds, with patch-compatibile renames"""
305
btree = self.make_tree_2()
306
btree.contents_by_id = False
307
add_patch = self.unified_diff([b"Hello\n"], [b"Extra cheese\n"])
308
btree.note_patch("grandparent/parent/file", add_patch)
309
btree.note_id(b'f', 'grandparent/parent/symlink', kind='symlink')
310
btree.note_target('grandparent/parent/symlink', 'venus')
311
self.adds_test(btree)
313
def make_tree_3(self):
314
btree, mtree = self.make_tree_1()
315
mtree.add_file(b"e", "grandparent/parent/topping", b"Anchovies\n")
316
btree.note_rename("grandparent/parent/file",
317
"grandparent/alt_parent/file")
318
btree.note_rename("grandparent/parent/topping",
319
"grandparent/alt_parent/stopping")
322
def get_file_test(self, btree):
323
with btree.get_file(btree.id2path(b"e")) as f:
324
self.assertEqual(f.read(), b"Lemon\n")
325
with btree.get_file(btree.id2path(b"c")) as f:
326
self.assertEqual(f.read(), b"Hello\n")
328
def test_get_file(self):
329
"""Get file contents"""
330
btree = self.make_tree_3()
331
mod_patch = self.unified_diff([b"Anchovies\n"], [b"Lemon\n"])
332
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
333
self.get_file_test(btree)
335
def test_get_file2(self):
336
"""Get file contents, with patch-compatible renames"""
337
btree = self.make_tree_3()
338
btree.contents_by_id = False
339
mod_patch = self.unified_diff([], [b"Lemon\n"])
340
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
341
mod_patch = self.unified_diff([], [b"Hello\n"])
342
btree.note_patch("grandparent/alt_parent/file", mod_patch)
343
self.get_file_test(btree)
345
def test_delete(self):
347
btree = self.make_tree_1()[0]
348
with btree.get_file(btree.id2path(b"c")) as f:
349
self.assertEqual(f.read(), b"Hello\n")
350
btree.note_deletion("grandparent/parent/file")
351
self.assertTrue(btree.id2path(b"c") is None)
352
self.assertFalse(btree.is_versioned("grandparent/parent/file"))
354
def sorted_ids(self, tree):
355
ids = sorted(tree.all_file_ids())
358
def test_iteration(self):
359
"""Ensure that iteration through ids works properly"""
360
btree = self.make_tree_1()[0]
361
self.assertEqual(self.sorted_ids(btree),
362
[inventory.ROOT_ID, b'a', b'b', b'c', b'd'])
363
btree.note_deletion("grandparent/parent/file")
364
btree.note_id(b"e", "grandparent/alt_parent/fool", kind="directory")
365
btree.note_last_changed("grandparent/alt_parent/fool",
367
self.assertEqual(self.sorted_ids(btree),
368
[inventory.ROOT_ID, b'a', b'b', b'd', b'e'])
371
class BundleTester1(tests.TestCaseWithTransport):
373
def test_mismatched_bundle(self):
374
format = bzrdir.BzrDirMetaFormat1()
375
format.repository_format = knitrepo.RepositoryFormatKnit3()
376
serializer = BundleSerializerV08('0.8')
377
b = self.make_branch('.', format=format)
378
self.assertRaises(errors.IncompatibleBundleFormat, serializer.write,
379
b.repository, [], {}, BytesIO())
381
def test_matched_bundle(self):
382
"""Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
383
format = bzrdir.BzrDirMetaFormat1()
384
format.repository_format = knitrepo.RepositoryFormatKnit3()
385
serializer = BundleSerializerV09('0.9')
386
b = self.make_branch('.', format=format)
387
serializer.write(b.repository, [], {}, BytesIO())
389
def test_mismatched_model(self):
390
"""Try copying a bundle from knit2 to knit1"""
391
format = bzrdir.BzrDirMetaFormat1()
392
format.repository_format = knitrepo.RepositoryFormatKnit3()
393
source = self.make_branch_and_tree('source', format=format)
394
source.commit('one', rev_id=b'one-id')
395
source.commit('two', rev_id=b'two-id')
397
write_bundle(source.branch.repository, b'two-id', b'null:', text,
401
format = bzrdir.BzrDirMetaFormat1()
402
format.repository_format = knitrepo.RepositoryFormatKnit1()
403
target = self.make_branch('target', format=format)
404
self.assertRaises(errors.IncompatibleRevision, install_bundle,
405
target.repository, read_bundle(text))
408
class BundleTester(object):
410
def bzrdir_format(self):
411
format = bzrdir.BzrDirMetaFormat1()
412
format.repository_format = knitrepo.RepositoryFormatKnit1()
415
def make_branch_and_tree(self, path, format=None):
417
format = self.bzrdir_format()
418
return tests.TestCaseWithTransport.make_branch_and_tree(
421
def make_branch(self, path, format=None):
423
format = self.bzrdir_format()
424
return tests.TestCaseWithTransport.make_branch(self, path, format)
426
def create_bundle_text(self, base_rev_id, rev_id):
427
bundle_txt = BytesIO()
428
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
429
bundle_txt, format=self.format)
431
self.assertEqual(bundle_txt.readline(),
432
b'# Bazaar revision bundle v%s\n' % self.format.encode('ascii'))
433
self.assertEqual(bundle_txt.readline(), b'#\n')
435
rev = self.b1.repository.get_revision(rev_id)
436
self.assertEqual(bundle_txt.readline().decode('utf-8'),
439
return bundle_txt, rev_ids
441
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
442
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
443
Make sure that the text generated is valid, and that it
444
can be applied against the base, and generate the same information.
446
:return: The in-memory bundle
448
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
450
# This should also validate the generated bundle
451
bundle = read_bundle(bundle_txt)
452
repository = self.b1.repository
453
for bundle_rev in bundle.real_revisions:
454
# These really should have already been checked when we read the
455
# bundle, since it computes the sha1 hash for the revision, which
456
# only will match if everything is okay, but lets be explicit about
458
branch_rev = repository.get_revision(bundle_rev.revision_id)
459
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
460
'timestamp', 'timezone', 'message', 'committer',
461
'parent_ids', 'properties'):
462
self.assertEqual(getattr(branch_rev, a),
463
getattr(bundle_rev, a))
464
self.assertEqual(len(branch_rev.parent_ids),
465
len(bundle_rev.parent_ids))
466
self.assertEqual(rev_ids,
467
[r.revision_id for r in bundle.real_revisions])
468
self.valid_apply_bundle(base_rev_id, bundle, checkout_dir=checkout_dir)
472
def get_invalid_bundle(self, base_rev_id, rev_id):
473
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
474
Munge the text so that it's invalid.
476
:return: The in-memory bundle
478
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
479
new_text = bundle_txt.getvalue().replace(b'executable:no',
481
bundle_txt = BytesIO(new_text)
482
bundle = read_bundle(bundle_txt)
483
self.valid_apply_bundle(base_rev_id, bundle)
486
def test_non_bundle(self):
487
self.assertRaises(errors.NotABundle,
488
read_bundle, BytesIO(b'#!/bin/sh\n'))
490
def test_malformed(self):
491
self.assertRaises(errors.BadBundle, read_bundle,
492
BytesIO(b'# Bazaar revision bundle v'))
494
def test_crlf_bundle(self):
496
read_bundle(BytesIO(b'# Bazaar revision bundle v0.8\r\n'))
497
except errors.BadBundle:
498
# It is currently permitted for bundles with crlf line endings to
499
# make read_bundle raise a BadBundle, but this should be fixed.
500
# Anything else, especially NotABundle, is an error.
503
def get_checkout(self, rev_id, checkout_dir=None):
504
"""Get a new tree, with the specified revision in it.
507
if checkout_dir is None:
508
checkout_dir = osutils.mkdtemp(prefix='test-branch-', dir='.')
510
if not os.path.exists(checkout_dir):
511
os.mkdir(checkout_dir)
512
tree = self.make_branch_and_tree(checkout_dir)
514
ancestors = write_bundle(self.b1.repository, rev_id, b'null:', s,
517
self.assertIsInstance(s.getvalue(), bytes)
518
install_bundle(tree.branch.repository, read_bundle(s))
519
for ancestor in ancestors:
520
old = self.b1.repository.revision_tree(ancestor)
521
new = tree.branch.repository.revision_tree(ancestor)
525
# Check that there aren't any inventory level changes
526
delta = new.changes_from(old)
527
self.assertFalse(delta.has_changed(),
528
'Revision %s not copied correctly.'
531
# Now check that the file contents are all correct
532
for path in old.all_versioned_paths():
534
old_file = old.get_file(path)
535
except errors.NoSuchFile:
538
old_file.read(), new.get_file(path).read())
542
if not _mod_revision.is_null(rev_id):
543
tree.branch.generate_revision_history(rev_id)
545
delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
546
self.assertFalse(delta.has_changed(),
547
'Working tree has modifications: %s' % delta)
550
def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
551
"""Get the base revision, apply the changes, and make
552
sure everything matches the builtin branch.
554
to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
557
self._valid_apply_bundle(base_rev_id, info, to_tree)
561
def _valid_apply_bundle(self, base_rev_id, info, to_tree):
562
original_parents = to_tree.get_parent_ids()
563
repository = to_tree.branch.repository
564
original_parents = to_tree.get_parent_ids()
565
self.assertIs(repository.has_revision(base_rev_id), True)
566
for rev in info.real_revisions:
567
self.assertTrue(not repository.has_revision(rev.revision_id),
568
'Revision {%s} present before applying bundle'
570
merge_bundle(info, to_tree, True, merge.Merge3Merger, False, False)
572
for rev in info.real_revisions:
573
self.assertTrue(repository.has_revision(rev.revision_id),
574
'Missing revision {%s} after applying bundle'
577
self.assertTrue(to_tree.branch.repository.has_revision(info.target))
578
# Do we also want to verify that all the texts have been added?
580
self.assertEqual(original_parents + [info.target],
581
to_tree.get_parent_ids())
583
rev = info.real_revisions[-1]
584
base_tree = self.b1.repository.revision_tree(rev.revision_id)
585
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
587
# TODO: make sure the target tree is identical to base tree
588
# we might also check the working tree.
590
base_files = list(base_tree.list_files())
591
to_files = list(to_tree.list_files())
592
self.assertEqual(len(base_files), len(to_files))
593
for base_file, to_file in zip(base_files, to_files):
594
self.assertEqual(base_file, to_file)
596
for path, status, kind, entry in base_files:
597
# Check that the meta information is the same
599
base_tree.get_file_size(path),
600
to_tree.get_file_size(to_tree.id2path(entry.file_id)))
602
base_tree.get_file_sha1(path, entry.file_id),
603
to_tree.get_file_sha1(to_tree.id2path(entry.file_id)))
604
# Check that the contents are the same
605
# This is pretty expensive
606
# self.assertEqual(base_tree.get_file(fileid).read(),
607
# to_tree.get_file(fileid).read())
609
def test_bundle(self):
610
self.tree1 = self.make_branch_and_tree('b1')
611
self.b1 = self.tree1.branch
613
self.build_tree_contents([('b1/one', b'one\n')])
614
self.tree1.add('one', b'one-id')
615
self.tree1.set_root_id(b'root-id')
616
self.tree1.commit('add one', rev_id=b'a@cset-0-1')
618
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-1')
620
# Make sure we can handle files with spaces, tabs, other
623
'b1/with space.txt', 'b1/dir/', 'b1/dir/filein subdir.c', 'b1/dir/WithCaps.txt', 'b1/dir/ pre space', 'b1/sub/', 'b1/sub/sub/', 'b1/sub/sub/nonempty.txt'
625
self.build_tree_contents([('b1/sub/sub/emptyfile.txt', b''),
626
('b1/dir/nolastnewline.txt', b'bloop')])
627
tt = TreeTransform(self.tree1)
628
tt.new_file('executable', tt.root, [b'#!/bin/sh\n'], b'exe-1', True)
630
# have to fix length of file-id so that we can predictably rewrite
631
# a (length-prefixed) record containing it later.
632
self.tree1.add('with space.txt', b'withspace-id')
634
'dir', 'dir/filein subdir.c', 'dir/WithCaps.txt', 'dir/ pre space', 'dir/nolastnewline.txt', 'sub', 'sub/sub', 'sub/sub/nonempty.txt', 'sub/sub/emptyfile.txt'
636
self.tree1.commit('add whitespace', rev_id=b'a@cset-0-2')
638
bundle = self.get_valid_bundle(b'a@cset-0-1', b'a@cset-0-2')
640
# Check a rollup bundle
641
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-2')
645
['sub/sub/nonempty.txt', 'sub/sub/emptyfile.txt', 'sub/sub'
647
tt = TreeTransform(self.tree1)
648
trans_id = tt.trans_id_tree_path('executable')
649
tt.set_executability(False, trans_id)
651
self.tree1.commit('removed', rev_id=b'a@cset-0-3')
653
bundle = self.get_valid_bundle(b'a@cset-0-2', b'a@cset-0-3')
654
self.assertRaises((errors.TestamentMismatch,
655
errors.VersionedFileInvalidChecksum,
656
errors.BadBundle), self.get_invalid_bundle,
657
b'a@cset-0-2', b'a@cset-0-3')
658
# Check a rollup bundle
659
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-3')
661
# Now move the directory
662
self.tree1.rename_one('dir', 'sub/dir')
663
self.tree1.commit('rename dir', rev_id=b'a@cset-0-4')
665
bundle = self.get_valid_bundle(b'a@cset-0-3', b'a@cset-0-4')
666
# Check a rollup bundle
667
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-4')
670
with open('b1/sub/dir/WithCaps.txt', 'ab') as f:
671
f.write(b'\nAdding some text\n')
672
with open('b1/sub/dir/ pre space', 'ab') as f:
674
b'\r\nAdding some\r\nDOS format lines\r\n')
675
with open('b1/sub/dir/nolastnewline.txt', 'ab') as f:
677
self.tree1.rename_one('sub/dir/ pre space',
679
self.tree1.commit('Modified files', rev_id=b'a@cset-0-5')
680
bundle = self.get_valid_bundle(b'a@cset-0-4', b'a@cset-0-5')
682
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
683
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
684
self.tree1.rename_one('temp', 'with space.txt')
685
self.tree1.commit(u'swap filenames', rev_id=b'a@cset-0-6',
687
bundle = self.get_valid_bundle(b'a@cset-0-5', b'a@cset-0-6')
688
other = self.get_checkout(b'a@cset-0-5')
689
tree1_inv = get_inventory_text(self.tree1.branch.repository,
691
tree2_inv = get_inventory_text(other.branch.repository,
693
self.assertEqualDiff(tree1_inv, tree2_inv)
694
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
695
other.commit('rename file', rev_id=b'a@cset-0-6b')
696
self.tree1.merge_from_branch(other.branch)
697
self.tree1.commit(u'Merge', rev_id=b'a@cset-0-7',
699
bundle = self.get_valid_bundle(b'a@cset-0-6', b'a@cset-0-7')
701
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
704
self.requireFeature(features.SymlinkFeature)
705
self.tree1 = self.make_branch_and_tree('b1')
706
self.b1 = self.tree1.branch
708
tt = TreeTransform(self.tree1)
709
tt.new_symlink(link_name, tt.root, link_target, link_id)
711
self.tree1.commit('add symlink', rev_id=b'l@cset-0-1')
712
bundle = self.get_valid_bundle(b'null:', b'l@cset-0-1')
713
if getattr(bundle, 'revision_tree', None) is not None:
714
# Not all bundle formats supports revision_tree
715
bund_tree = bundle.revision_tree(self.b1.repository, b'l@cset-0-1')
717
link_target, bund_tree.get_symlink_target(link_name))
719
tt = TreeTransform(self.tree1)
720
trans_id = tt.trans_id_tree_path(link_name)
721
tt.adjust_path('link2', tt.root, trans_id)
722
tt.delete_contents(trans_id)
723
tt.create_symlink(new_link_target, trans_id)
725
self.tree1.commit('rename and change symlink', rev_id=b'l@cset-0-2')
726
bundle = self.get_valid_bundle(b'l@cset-0-1', b'l@cset-0-2')
727
if getattr(bundle, 'revision_tree', None) is not None:
728
# Not all bundle formats supports revision_tree
729
bund_tree = bundle.revision_tree(self.b1.repository, b'l@cset-0-2')
730
self.assertEqual(new_link_target,
731
bund_tree.get_symlink_target('link2'))
733
tt = TreeTransform(self.tree1)
734
trans_id = tt.trans_id_tree_path('link2')
735
tt.delete_contents(trans_id)
736
tt.create_symlink('jupiter', trans_id)
738
self.tree1.commit('just change symlink target', rev_id=b'l@cset-0-3')
739
bundle = self.get_valid_bundle(b'l@cset-0-2', b'l@cset-0-3')
741
tt = TreeTransform(self.tree1)
742
trans_id = tt.trans_id_tree_path('link2')
743
tt.delete_contents(trans_id)
745
self.tree1.commit('Delete symlink', rev_id=b'l@cset-0-4')
746
bundle = self.get_valid_bundle(b'l@cset-0-3', b'l@cset-0-4')
748
def test_symlink_bundle(self):
749
self._test_symlink_bundle('link', 'bar/foo', 'mars')
751
def test_unicode_symlink_bundle(self):
752
self.requireFeature(features.UnicodeFilenameFeature)
753
self._test_symlink_bundle(u'\N{Euro Sign}link',
754
u'bar/\N{Euro Sign}foo',
755
u'mars\N{Euro Sign}')
757
def test_binary_bundle(self):
758
self.tree1 = self.make_branch_and_tree('b1')
759
self.b1 = self.tree1.branch
760
tt = TreeTransform(self.tree1)
763
tt.new_file('file', tt.root, [
764
b'\x00\n\x00\r\x01\n\x02\r\xff'], b'binary-1')
765
tt.new_file('file2', tt.root, [b'\x01\n\x02\r\x03\n\x04\r\xff'],
768
self.tree1.commit('add binary', rev_id=b'b@cset-0-1')
769
self.get_valid_bundle(b'null:', b'b@cset-0-1')
772
tt = TreeTransform(self.tree1)
773
trans_id = tt.trans_id_tree_path('file')
774
tt.delete_contents(trans_id)
776
self.tree1.commit('delete binary', rev_id=b'b@cset-0-2')
777
self.get_valid_bundle(b'b@cset-0-1', b'b@cset-0-2')
780
tt = TreeTransform(self.tree1)
781
trans_id = tt.trans_id_tree_path('file2')
782
tt.adjust_path('file3', tt.root, trans_id)
783
tt.delete_contents(trans_id)
784
tt.create_file([b'file\rcontents\x00\n\x00'], trans_id)
786
self.tree1.commit('rename and modify binary', rev_id=b'b@cset-0-3')
787
self.get_valid_bundle(b'b@cset-0-2', b'b@cset-0-3')
790
tt = TreeTransform(self.tree1)
791
trans_id = tt.trans_id_tree_path('file3')
792
tt.delete_contents(trans_id)
793
tt.create_file([b'\x00file\rcontents'], trans_id)
795
self.tree1.commit('just modify binary', rev_id=b'b@cset-0-4')
796
self.get_valid_bundle(b'b@cset-0-3', b'b@cset-0-4')
799
self.get_valid_bundle(b'null:', b'b@cset-0-4')
801
def test_last_modified(self):
802
self.tree1 = self.make_branch_and_tree('b1')
803
self.b1 = self.tree1.branch
804
tt = TreeTransform(self.tree1)
805
tt.new_file('file', tt.root, [b'file'], b'file')
807
self.tree1.commit('create file', rev_id=b'a@lmod-0-1')
809
tt = TreeTransform(self.tree1)
810
trans_id = tt.trans_id_tree_path('file')
811
tt.delete_contents(trans_id)
812
tt.create_file([b'file2'], trans_id)
814
self.tree1.commit('modify text', rev_id=b'a@lmod-0-2a')
816
other = self.get_checkout(b'a@lmod-0-1')
817
tt = TreeTransform(other)
818
trans_id = tt.trans_id_tree_path('file2')
819
tt.delete_contents(trans_id)
820
tt.create_file([b'file2'], trans_id)
822
other.commit('modify text in another tree', rev_id=b'a@lmod-0-2b')
823
self.tree1.merge_from_branch(other.branch)
824
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-3',
826
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-4')
827
bundle = self.get_valid_bundle(b'a@lmod-0-2a', b'a@lmod-0-4')
829
def test_hide_history(self):
830
self.tree1 = self.make_branch_and_tree('b1')
831
self.b1 = self.tree1.branch
833
with open('b1/one', 'wb') as f:
835
self.tree1.add('one')
836
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
837
with open('b1/one', 'wb') as f:
839
self.tree1.commit('modify', rev_id=b'a@cset-0-2')
840
with open('b1/one', 'wb') as f:
842
self.tree1.commit('modify', rev_id=b'a@cset-0-3')
843
bundle_file = BytesIO()
844
rev_ids = write_bundle(self.tree1.branch.repository, b'a@cset-0-3',
845
b'a@cset-0-1', bundle_file, format=self.format)
846
self.assertNotContainsRe(bundle_file.getvalue(), b'\btwo\b')
847
self.assertContainsRe(self.get_raw(bundle_file), b'one')
848
self.assertContainsRe(self.get_raw(bundle_file), b'three')
850
def test_bundle_same_basis(self):
851
"""Ensure using the basis as the target doesn't cause an error"""
852
self.tree1 = self.make_branch_and_tree('b1')
853
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
854
bundle_file = BytesIO()
855
rev_ids = write_bundle(self.tree1.branch.repository, b'a@cset-0-1',
856
b'a@cset-0-1', bundle_file)
859
def get_raw(bundle_file):
860
return bundle_file.getvalue()
862
def test_unicode_bundle(self):
863
self.requireFeature(features.UnicodeFilenameFeature)
864
# Handle international characters
866
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
868
self.tree1 = self.make_branch_and_tree('b1')
869
self.b1 = self.tree1.branch
872
u'With international man of mystery\n'
873
u'William Dod\xe9\n').encode('utf-8'))
876
self.tree1.add([u'with Dod\N{Euro Sign}'], [b'withdod-id'])
877
self.tree1.commit(u'i18n commit from William Dod\xe9',
878
rev_id=b'i18n-1', committer=u'William Dod\xe9')
881
bundle = self.get_valid_bundle(b'null:', b'i18n-1')
884
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
885
f.write(u'Modified \xb5\n'.encode('utf8'))
887
self.tree1.commit(u'modified', rev_id=b'i18n-2')
889
bundle = self.get_valid_bundle(b'i18n-1', b'i18n-2')
892
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
893
self.tree1.commit(u'renamed, the new i18n man', rev_id=b'i18n-3',
894
committer=u'Erik B\xe5gfors')
896
bundle = self.get_valid_bundle(b'i18n-2', b'i18n-3')
899
self.tree1.remove([u'B\N{Euro Sign}gfors'])
900
self.tree1.commit(u'removed', rev_id=b'i18n-4')
902
bundle = self.get_valid_bundle(b'i18n-3', b'i18n-4')
905
bundle = self.get_valid_bundle(b'null:', b'i18n-4')
907
def test_whitespace_bundle(self):
908
if sys.platform in ('win32', 'cygwin'):
909
raise tests.TestSkipped('Windows doesn\'t support filenames'
910
' with tabs or trailing spaces')
911
self.tree1 = self.make_branch_and_tree('b1')
912
self.b1 = self.tree1.branch
914
self.build_tree(['b1/trailing space '])
915
self.tree1.add(['trailing space '])
916
# TODO: jam 20060701 Check for handling files with '\t' characters
917
# once we actually support them
920
self.tree1.commit('funky whitespace', rev_id=b'white-1')
922
bundle = self.get_valid_bundle(b'null:', b'white-1')
925
with open('b1/trailing space ', 'ab') as f:
926
f.write(b'add some text\n')
927
self.tree1.commit('add text', rev_id=b'white-2')
929
bundle = self.get_valid_bundle(b'white-1', b'white-2')
932
self.tree1.rename_one('trailing space ', ' start and end space ')
933
self.tree1.commit('rename', rev_id=b'white-3')
935
bundle = self.get_valid_bundle(b'white-2', b'white-3')
938
self.tree1.remove([' start and end space '])
939
self.tree1.commit('removed', rev_id=b'white-4')
941
bundle = self.get_valid_bundle(b'white-3', b'white-4')
943
# Now test a complet roll-up
944
bundle = self.get_valid_bundle(b'null:', b'white-4')
946
def test_alt_timezone_bundle(self):
947
self.tree1 = self.make_branch_and_memory_tree('b1')
948
self.b1 = self.tree1.branch
949
builder = treebuilder.TreeBuilder()
951
self.tree1.lock_write()
952
builder.start_tree(self.tree1)
953
builder.build(['newfile'])
954
builder.finish_tree()
956
# Asia/Colombo offset = 5 hours 30 minutes
957
self.tree1.commit('non-hour offset timezone', rev_id=b'tz-1',
958
timezone=19800, timestamp=1152544886.0)
960
bundle = self.get_valid_bundle(b'null:', b'tz-1')
962
rev = bundle.revisions[0]
963
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
964
self.assertEqual(19800, rev.timezone)
965
self.assertEqual(1152544886.0, rev.timestamp)
968
def test_bundle_root_id(self):
969
self.tree1 = self.make_branch_and_tree('b1')
970
self.b1 = self.tree1.branch
971
self.tree1.commit('message', rev_id=b'revid1')
972
bundle = self.get_valid_bundle(b'null:', b'revid1')
973
tree = self.get_bundle_tree(bundle, b'revid1')
974
root_revision = tree.get_file_revision(u'')
975
self.assertEqual(b'revid1', root_revision)
977
def test_install_revisions(self):
978
self.tree1 = self.make_branch_and_tree('b1')
979
self.b1 = self.tree1.branch
980
self.tree1.commit('message', rev_id=b'rev2a')
981
bundle = self.get_valid_bundle(b'null:', b'rev2a')
982
branch2 = self.make_branch('b2')
983
self.assertFalse(branch2.repository.has_revision(b'rev2a'))
984
target_revision = bundle.install_revisions(branch2.repository)
985
self.assertTrue(branch2.repository.has_revision(b'rev2a'))
986
self.assertEqual(b'rev2a', target_revision)
988
def test_bundle_empty_property(self):
989
"""Test serializing revision properties with an empty value."""
990
tree = self.make_branch_and_memory_tree('tree')
992
self.addCleanup(tree.unlock)
993
tree.add([''], [b'TREE_ROOT'])
994
tree.commit('One', revprops={u'one': 'two',
995
u'empty': ''}, rev_id=b'rev1')
996
self.b1 = tree.branch
997
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
998
bundle = read_bundle(bundle_sio)
999
revision_info = bundle.revisions[0]
1000
self.assertEqual(b'rev1', revision_info.revision_id)
1001
rev = revision_info.as_revision()
1002
self.assertEqual({'branch-nick': 'tree', 'empty': '', 'one': 'two'},
1005
def test_bundle_sorted_properties(self):
1006
"""For stability the writer should write properties in sorted order."""
1007
tree = self.make_branch_and_memory_tree('tree')
1009
self.addCleanup(tree.unlock)
1011
tree.add([''], [b'TREE_ROOT'])
1012
tree.commit('One', rev_id=b'rev1',
1013
revprops={u'a': '4', u'b': '3', u'c': '2', u'd': '1'})
1014
self.b1 = tree.branch
1015
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1016
bundle = read_bundle(bundle_sio)
1017
revision_info = bundle.revisions[0]
1018
self.assertEqual(b'rev1', revision_info.revision_id)
1019
rev = revision_info.as_revision()
1020
self.assertEqual({'branch-nick': 'tree', 'a': '4', 'b': '3', 'c': '2',
1021
'd': '1'}, rev.properties)
1023
def test_bundle_unicode_properties(self):
1024
"""We should be able to round trip a non-ascii property."""
1025
tree = self.make_branch_and_memory_tree('tree')
1027
self.addCleanup(tree.unlock)
1029
tree.add([''], [b'TREE_ROOT'])
1030
# Revisions themselves do not require anything about revision property
1031
# keys, other than that they are a basestring, and do not contain
1033
# However, Testaments assert than they are str(), and thus should not
1035
tree.commit('One', rev_id=b'rev1',
1036
revprops={u'omega': u'\u03a9', u'alpha': u'\u03b1'})
1037
self.b1 = tree.branch
1038
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1039
bundle = read_bundle(bundle_sio)
1040
revision_info = bundle.revisions[0]
1041
self.assertEqual(b'rev1', revision_info.revision_id)
1042
rev = revision_info.as_revision()
1043
self.assertEqual({'branch-nick': 'tree', 'omega': u'\u03a9',
1044
'alpha': u'\u03b1'}, rev.properties)
1046
def test_bundle_with_ghosts(self):
1047
tree = self.make_branch_and_tree('tree')
1048
self.b1 = tree.branch
1049
self.build_tree_contents([('tree/file', b'content1')])
1052
self.build_tree_contents([('tree/file', b'content2')])
1053
tree.add_parent_tree_id(b'ghost')
1054
tree.commit('rev2', rev_id=b'rev2')
1055
bundle = self.get_valid_bundle(b'null:', b'rev2')
1057
def make_simple_tree(self, format=None):
1058
tree = self.make_branch_and_tree('b1', format=format)
1059
self.b1 = tree.branch
1060
self.build_tree(['b1/file'])
1064
def test_across_serializers(self):
1065
tree = self.make_simple_tree('knit')
1066
tree.commit('hello', rev_id=b'rev1')
1067
tree.commit('hello', rev_id=b'rev2')
1068
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1069
repo = self.make_repository('repo', format='dirstate-with-subtree')
1070
bundle.install_revisions(repo)
1071
inv_text = repo._get_inventory_xml(b'rev2')
1072
self.assertNotContainsRe(inv_text, b'format="5"')
1073
self.assertContainsRe(inv_text, b'format="7"')
1075
def make_repo_with_installed_revisions(self):
1076
tree = self.make_simple_tree('knit')
1077
tree.commit('hello', rev_id=b'rev1')
1078
tree.commit('hello', rev_id=b'rev2')
1079
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1080
repo = self.make_repository('repo', format='dirstate-with-subtree')
1081
bundle.install_revisions(repo)
1084
def test_across_models(self):
1085
repo = self.make_repo_with_installed_revisions()
1086
inv = repo.get_inventory(b'rev2')
1087
self.assertEqual(b'rev2', inv.root.revision)
1088
root_id = inv.root.file_id
1090
self.addCleanup(repo.unlock)
1091
self.assertEqual({(root_id, b'rev1'): (),
1092
(root_id, b'rev2'): ((root_id, b'rev1'),)},
1093
repo.texts.get_parent_map([(root_id, b'rev1'), (root_id, b'rev2')]))
1095
def test_inv_hash_across_serializers(self):
1096
repo = self.make_repo_with_installed_revisions()
1097
recorded_inv_sha1 = repo.get_revision(b'rev2').inventory_sha1
1098
xml = repo._get_inventory_xml(b'rev2')
1099
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1101
def test_across_models_incompatible(self):
1102
tree = self.make_simple_tree('dirstate-with-subtree')
1103
tree.commit('hello', rev_id=b'rev1')
1104
tree.commit('hello', rev_id=b'rev2')
1106
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1107
except errors.IncompatibleBundleFormat:
1108
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1109
repo = self.make_repository('repo', format='knit')
1110
bundle.install_revisions(repo)
1112
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1113
self.assertRaises(errors.IncompatibleRevision,
1114
bundle.install_revisions, repo)
1116
def test_get_merge_request(self):
1117
tree = self.make_simple_tree()
1118
tree.commit('hello', rev_id=b'rev1')
1119
tree.commit('hello', rev_id=b'rev2')
1120
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1121
result = bundle.get_merge_request(tree.branch.repository)
1122
self.assertEqual((None, b'rev1', 'inapplicable'), result)
1124
def test_with_subtree(self):
1125
tree = self.make_branch_and_tree('tree',
1126
format='dirstate-with-subtree')
1127
self.b1 = tree.branch
1128
subtree = self.make_branch_and_tree('tree/subtree',
1129
format='dirstate-with-subtree')
1131
tree.commit('hello', rev_id=b'rev1')
1133
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1134
except errors.IncompatibleBundleFormat:
1135
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1136
if isinstance(bundle, v09.BundleInfo09):
1137
raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
1138
repo = self.make_repository('repo', format='knit')
1139
self.assertRaises(errors.IncompatibleRevision,
1140
bundle.install_revisions, repo)
1141
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1142
bundle.install_revisions(repo2)
1144
def test_revision_id_with_slash(self):
1145
self.tree1 = self.make_branch_and_tree('tree')
1146
self.b1 = self.tree1.branch
1148
self.tree1.commit('Revision/id/with/slashes', rev_id=b'rev/id')
1150
raise tests.TestSkipped(
1151
"Repository doesn't support revision ids with slashes")
1152
bundle = self.get_valid_bundle(b'null:', b'rev/id')
1154
def test_skip_file(self):
1155
"""Make sure we don't accidentally write to the wrong versionedfile"""
1156
self.tree1 = self.make_branch_and_tree('tree')
1157
self.b1 = self.tree1.branch
1158
# rev1 is not present in bundle, done by fetch
1159
self.build_tree_contents([('tree/file2', b'contents1')])
1160
self.tree1.add('file2', b'file2-id')
1161
self.tree1.commit('rev1', rev_id=b'reva')
1162
self.build_tree_contents([('tree/file3', b'contents2')])
1163
# rev2 is present in bundle, and done by fetch
1164
# having file1 in the bunle causes file1's versionedfile to be opened.
1165
self.tree1.add('file3', b'file3-id')
1166
rev2 = self.tree1.commit('rev2')
1167
# Updating file2 should not cause an attempt to add to file1's vf
1168
target = self.tree1.controldir.sprout('target').open_workingtree()
1169
self.build_tree_contents([('tree/file2', b'contents3')])
1170
self.tree1.commit('rev3', rev_id=b'rev3')
1171
bundle = self.get_valid_bundle(b'reva', b'rev3')
1172
if getattr(bundle, 'get_bundle_reader', None) is None:
1173
raise tests.TestSkipped('Bundle format cannot provide reader')
1175
(f, r) for b, m, k, r, f in bundle.get_bundle_reader().iter_records()
1178
{(b'file2-id', b'rev3'), (b'file3-id', rev2)}, file_ids)
1179
bundle.install_revisions(target.branch.repository)
1182
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1186
def test_bundle_empty_property(self):
1187
"""Test serializing revision properties with an empty value."""
1188
tree = self.make_branch_and_memory_tree('tree')
1190
self.addCleanup(tree.unlock)
1191
tree.add([''], [b'TREE_ROOT'])
1192
tree.commit('One', revprops={u'one': 'two',
1193
u'empty': ''}, rev_id=b'rev1')
1194
self.b1 = tree.branch
1195
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1196
self.assertContainsRe(bundle_sio.getvalue(),
1198
b'# branch-nick: tree\n'
1202
bundle = read_bundle(bundle_sio)
1203
revision_info = bundle.revisions[0]
1204
self.assertEqual(b'rev1', revision_info.revision_id)
1205
rev = revision_info.as_revision()
1206
self.assertEqual({'branch-nick': 'tree', 'empty': '', 'one': 'two'},
1209
def get_bundle_tree(self, bundle, revision_id):
1210
repository = self.make_repository('repo')
1211
return bundle.revision_tree(repository, b'revid1')
1213
def test_bundle_empty_property_alt(self):
1214
"""Test serializing revision properties with an empty value.
1216
Older readers had a bug when reading an empty property.
1217
They assumed that all keys ended in ': \n'. However they would write an
1218
empty value as ':\n'. This tests make sure that all newer bzr versions
1219
can handle th second form.
1221
tree = self.make_branch_and_memory_tree('tree')
1223
self.addCleanup(tree.unlock)
1224
tree.add([''], [b'TREE_ROOT'])
1225
tree.commit('One', revprops={u'one': 'two',
1226
u'empty': ''}, rev_id=b'rev1')
1227
self.b1 = tree.branch
1228
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1229
txt = bundle_sio.getvalue()
1230
loc = txt.find(b'# empty: ') + len(b'# empty:')
1231
# Create a new bundle, which strips the trailing space after empty
1232
bundle_sio = BytesIO(txt[:loc] + txt[loc + 1:])
1234
self.assertContainsRe(bundle_sio.getvalue(),
1236
b'# branch-nick: tree\n'
1240
bundle = read_bundle(bundle_sio)
1241
revision_info = bundle.revisions[0]
1242
self.assertEqual(b'rev1', revision_info.revision_id)
1243
rev = revision_info.as_revision()
1244
self.assertEqual({'branch-nick': 'tree', 'empty': '', 'one': 'two'},
1247
def test_bundle_sorted_properties(self):
1248
"""For stability the writer should write properties in sorted order."""
1249
tree = self.make_branch_and_memory_tree('tree')
1251
self.addCleanup(tree.unlock)
1253
tree.add([''], [b'TREE_ROOT'])
1254
tree.commit('One', rev_id=b'rev1',
1255
revprops={u'a': '4', u'b': '3', u'c': '2', u'd': '1'})
1256
self.b1 = tree.branch
1257
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1258
self.assertContainsRe(bundle_sio.getvalue(),
1262
b'# branch-nick: tree\n'
1266
bundle = read_bundle(bundle_sio)
1267
revision_info = bundle.revisions[0]
1268
self.assertEqual(b'rev1', revision_info.revision_id)
1269
rev = revision_info.as_revision()
1270
self.assertEqual({'branch-nick': 'tree', 'a': '4', 'b': '3', 'c': '2',
1271
'd': '1'}, rev.properties)
1273
def test_bundle_unicode_properties(self):
1274
"""We should be able to round trip a non-ascii property."""
1275
tree = self.make_branch_and_memory_tree('tree')
1277
self.addCleanup(tree.unlock)
1279
tree.add([''], [b'TREE_ROOT'])
1280
# Revisions themselves do not require anything about revision property
1281
# keys, other than that they are a basestring, and do not contain
1283
# However, Testaments assert than they are str(), and thus should not
1285
tree.commit('One', rev_id=b'rev1',
1286
revprops={u'omega': u'\u03a9', u'alpha': u'\u03b1'})
1287
self.b1 = tree.branch
1288
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1289
self.assertContainsRe(bundle_sio.getvalue(),
1291
b'# alpha: \xce\xb1\n'
1292
b'# branch-nick: tree\n'
1293
b'# omega: \xce\xa9\n'
1295
bundle = read_bundle(bundle_sio)
1296
revision_info = bundle.revisions[0]
1297
self.assertEqual(b'rev1', revision_info.revision_id)
1298
rev = revision_info.as_revision()
1299
self.assertEqual({'branch-nick': 'tree', 'omega': u'\u03a9',
1300
'alpha': u'\u03b1'}, rev.properties)
1303
class V09BundleKnit2Tester(V08BundleTester):
1307
def bzrdir_format(self):
1308
format = bzrdir.BzrDirMetaFormat1()
1309
format.repository_format = knitrepo.RepositoryFormatKnit3()
1313
class V09BundleKnit1Tester(V08BundleTester):
1317
def bzrdir_format(self):
1318
format = bzrdir.BzrDirMetaFormat1()
1319
format.repository_format = knitrepo.RepositoryFormatKnit1()
1323
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
1327
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1328
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1329
Make sure that the text generated is valid, and that it
1330
can be applied against the base, and generate the same information.
1332
:return: The in-memory bundle
1334
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1336
# This should also validate the generated bundle
1337
bundle = read_bundle(bundle_txt)
1338
repository = self.b1.repository
1339
for bundle_rev in bundle.real_revisions:
1340
# These really should have already been checked when we read the
1341
# bundle, since it computes the sha1 hash for the revision, which
1342
# only will match if everything is okay, but lets be explicit about
1344
branch_rev = repository.get_revision(bundle_rev.revision_id)
1345
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1346
'timestamp', 'timezone', 'message', 'committer',
1347
'parent_ids', 'properties'):
1348
self.assertEqual(getattr(branch_rev, a),
1349
getattr(bundle_rev, a))
1350
self.assertEqual(len(branch_rev.parent_ids),
1351
len(bundle_rev.parent_ids))
1352
self.assertEqual(set(rev_ids),
1353
{r.revision_id for r in bundle.real_revisions})
1354
self.valid_apply_bundle(base_rev_id, bundle,
1355
checkout_dir=checkout_dir)
1359
def get_invalid_bundle(self, base_rev_id, rev_id):
1360
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1361
Munge the text so that it's invalid.
1363
:return: The in-memory bundle
1365
from ..bundle import serializer
1366
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1367
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1368
new_text = new_text.replace(b'<file file_id="exe-1"',
1369
b'<file executable="y" file_id="exe-1"')
1370
new_text = new_text.replace(b'B260', b'B275')
1371
bundle_txt = BytesIO()
1372
bundle_txt.write(serializer._get_bundle_header('4'))
1373
bundle_txt.write(b'\n')
1374
bundle_txt.write(bz2.compress(new_text))
1376
bundle = read_bundle(bundle_txt)
1377
self.valid_apply_bundle(base_rev_id, bundle)
1380
def create_bundle_text(self, base_rev_id, rev_id):
1381
bundle_txt = BytesIO()
1382
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1383
bundle_txt, format=self.format)
1385
self.assertEqual(bundle_txt.readline(),
1386
b'# Bazaar revision bundle v%s\n' % self.format.encode('ascii'))
1387
self.assertEqual(bundle_txt.readline(), b'#\n')
1388
rev = self.b1.repository.get_revision(rev_id)
1390
return bundle_txt, rev_ids
1392
def get_bundle_tree(self, bundle, revision_id):
1393
repository = self.make_repository('repo')
1394
bundle.install_revisions(repository)
1395
return repository.revision_tree(revision_id)
1397
def test_creation(self):
1398
tree = self.make_branch_and_tree('tree')
1399
self.build_tree_contents([('tree/file', b'contents1\nstatic\n')])
1400
tree.add('file', b'fileid-2')
1401
tree.commit('added file', rev_id=b'rev1')
1402
self.build_tree_contents([('tree/file', b'contents2\nstatic\n')])
1403
tree.commit('changed file', rev_id=b'rev2')
1405
serializer = BundleSerializerV4('1.0')
1406
with tree.lock_read():
1407
serializer.write_bundle(
1408
tree.branch.repository, b'rev2', b'null:', s)
1410
tree2 = self.make_branch_and_tree('target')
1411
target_repo = tree2.branch.repository
1412
install_bundle(target_repo, serializer.read(s))
1413
target_repo.lock_read()
1414
self.addCleanup(target_repo.unlock)
1415
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1416
repo_texts = dict((i, b''.join(content)) for i, content
1417
in target_repo.iter_files_bytes(
1418
[(b'fileid-2', b'rev1', '1'),
1419
(b'fileid-2', b'rev2', '2')]))
1420
self.assertEqual({'1': b'contents1\nstatic\n',
1421
'2': b'contents2\nstatic\n'},
1423
rtree = target_repo.revision_tree(b'rev2')
1424
inventory_vf = target_repo.inventories
1425
# If the inventory store has a graph, it must match the revision graph.
1427
[inventory_vf.get_parent_map([(b'rev2',)])[(b'rev2',)]],
1428
[None, ((b'rev1',),)])
1429
self.assertEqual('changed file',
1430
target_repo.get_revision(b'rev2').message)
1433
def get_raw(bundle_file):
1435
line = bundle_file.readline()
1436
line = bundle_file.readline()
1437
lines = bundle_file.readlines()
1438
return bz2.decompress(b''.join(lines))
1440
def test_copy_signatures(self):
1441
tree_a = self.make_branch_and_tree('tree_a')
1443
import breezy.commit as commit
1444
oldstrategy = breezy.gpg.GPGStrategy
1445
branch = tree_a.branch
1446
repo_a = branch.repository
1447
tree_a.commit("base", allow_pointless=True, rev_id=b'A')
1448
self.assertFalse(branch.repository.has_signature_for_revision_id(b'A'))
1450
from ..bzr.testament import Testament
1451
# monkey patch gpg signing mechanism
1452
breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
1453
new_config = test_commit.MustSignConfig()
1454
commit.Commit(config_stack=new_config).commit(message="base",
1455
allow_pointless=True,
1457
working_tree=tree_a)
1460
return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
1461
self.assertTrue(repo_a.has_signature_for_revision_id(b'B'))
1463
breezy.gpg.GPGStrategy = oldstrategy
1464
tree_b = self.make_branch_and_tree('tree_b')
1465
repo_b = tree_b.branch.repository
1467
serializer = BundleSerializerV4('4')
1468
with tree_a.lock_read():
1469
serializer.write_bundle(
1470
tree_a.branch.repository, b'B', b'null:', s)
1472
install_bundle(repo_b, serializer.read(s))
1473
self.assertTrue(repo_b.has_signature_for_revision_id(b'B'))
1474
self.assertEqual(repo_b.get_signature_text(b'B'),
1475
repo_a.get_signature_text(b'B'))
1477
# ensure repeat installs are harmless
1478
install_bundle(repo_b, serializer.read(s))
1481
class V4_2aBundleTester(V4BundleTester):
1483
def bzrdir_format(self):
1486
def get_invalid_bundle(self, base_rev_id, rev_id):
1487
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1488
Munge the text so that it's invalid.
1490
:return: The in-memory bundle
1492
from ..bundle import serializer
1493
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1494
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1495
# We are going to be replacing some text to set the executable bit on a
1496
# file. Make sure the text replacement actually works correctly.
1497
self.assertContainsRe(new_text, b'(?m)B244\n\ni 1\n<inventory')
1498
new_text = new_text.replace(b'<file file_id="exe-1"',
1499
b'<file executable="y" file_id="exe-1"')
1500
new_text = new_text.replace(b'B244', b'B259')
1501
bundle_txt = BytesIO()
1502
bundle_txt.write(serializer._get_bundle_header('4'))
1503
bundle_txt.write(b'\n')
1504
bundle_txt.write(bz2.compress(new_text))
1506
bundle = read_bundle(bundle_txt)
1507
self.valid_apply_bundle(base_rev_id, bundle)
1510
def make_merged_branch(self):
1511
builder = self.make_branch_builder('source')
1512
builder.start_series()
1513
builder.build_snapshot(None, [
1514
('add', ('', b'root-id', 'directory', None)),
1515
('add', ('file', b'file-id', 'file', b'original content\n')),
1516
], revision_id=b'a@cset-0-1')
1517
builder.build_snapshot([b'a@cset-0-1'], [
1518
('modify', ('file', b'new-content\n')),
1519
], revision_id=b'a@cset-0-2a')
1520
builder.build_snapshot([b'a@cset-0-1'], [
1521
('add', ('other-file', b'file2-id', 'file', b'file2-content\n')),
1522
], revision_id=b'a@cset-0-2b')
1523
builder.build_snapshot([b'a@cset-0-2a', b'a@cset-0-2b'], [
1524
('add', ('other-file', b'file2-id', 'file', b'file2-content\n')),
1525
], revision_id=b'a@cset-0-3')
1526
builder.finish_series()
1527
self.b1 = builder.get_branch()
1529
self.addCleanup(self.b1.unlock)
1531
def make_bundle_just_inventories(self, base_revision_id,
1535
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1536
self.b1.repository, sio)
1537
writer.bundle.begin()
1538
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1543
def test_single_inventory_multiple_parents_as_xml(self):
1544
self.make_merged_branch()
1545
sio = self.make_bundle_just_inventories(b'a@cset-0-1', b'a@cset-0-3',
1547
reader = v4.BundleReader(sio, stream_input=False)
1548
records = list(reader.iter_records())
1549
self.assertEqual(1, len(records))
1550
(bytes, metadata, repo_kind, revision_id,
1551
file_id) = records[0]
1552
self.assertIs(None, file_id)
1553
self.assertEqual(b'a@cset-0-3', revision_id)
1554
self.assertEqual('inventory', repo_kind)
1555
self.assertEqual({b'parents': [b'a@cset-0-2a', b'a@cset-0-2b'],
1556
b'sha1': b'09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1557
b'storage_kind': b'mpdiff',
1559
# We should have an mpdiff that takes some lines from both parents.
1560
self.assertEqualDiff(
1562
b'<inventory format="10" revision_id="a@cset-0-3">\n'
1565
b'c 1 3 3 2\n', bytes)
1567
def test_single_inv_no_parents_as_xml(self):
1568
self.make_merged_branch()
1569
sio = self.make_bundle_just_inventories(b'null:', b'a@cset-0-1',
1571
reader = v4.BundleReader(sio, stream_input=False)
1572
records = list(reader.iter_records())
1573
self.assertEqual(1, len(records))
1574
(bytes, metadata, repo_kind, revision_id,
1575
file_id) = records[0]
1576
self.assertIs(None, file_id)
1577
self.assertEqual(b'a@cset-0-1', revision_id)
1578
self.assertEqual('inventory', repo_kind)
1579
self.assertEqual({b'parents': [],
1580
b'sha1': b'a13f42b142d544aac9b085c42595d304150e31a2',
1581
b'storage_kind': b'mpdiff',
1583
# We should have an mpdiff that takes some lines from both parents.
1584
self.assertEqualDiff(
1586
b'<inventory format="10" revision_id="a@cset-0-1">\n'
1587
b'<directory file_id="root-id" name=""'
1588
b' revision="a@cset-0-1" />\n'
1589
b'<file file_id="file-id" name="file" parent_id="root-id"'
1590
b' revision="a@cset-0-1"'
1591
b' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1592
b' text_size="17" />\n'
1596
def test_multiple_inventories_as_xml(self):
1597
self.make_merged_branch()
1598
sio = self.make_bundle_just_inventories(b'a@cset-0-1', b'a@cset-0-3',
1599
[b'a@cset-0-2a', b'a@cset-0-2b', b'a@cset-0-3'])
1600
reader = v4.BundleReader(sio, stream_input=False)
1601
records = list(reader.iter_records())
1602
self.assertEqual(3, len(records))
1603
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1604
self.assertEqual([b'a@cset-0-2a', b'a@cset-0-2b', b'a@cset-0-3'],
1606
metadata_2a = records[0][1]
1607
self.assertEqual({b'parents': [b'a@cset-0-1'],
1608
b'sha1': b'1e105886d62d510763e22885eec733b66f5f09bf',
1609
b'storage_kind': b'mpdiff',
1611
metadata_2b = records[1][1]
1612
self.assertEqual({b'parents': [b'a@cset-0-1'],
1613
b'sha1': b'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1614
b'storage_kind': b'mpdiff',
1616
metadata_3 = records[2][1]
1617
self.assertEqual({b'parents': [b'a@cset-0-2a', b'a@cset-0-2b'],
1618
b'sha1': b'09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1619
b'storage_kind': b'mpdiff',
1621
bytes_2a = records[0][0]
1622
self.assertEqualDiff(
1624
b'<inventory format="10" revision_id="a@cset-0-2a">\n'
1628
b'<file file_id="file-id" name="file" parent_id="root-id"'
1629
b' revision="a@cset-0-2a"'
1630
b' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1631
b' text_size="12" />\n'
1633
b'c 0 3 3 1\n', bytes_2a)
1634
bytes_2b = records[1][0]
1635
self.assertEqualDiff(
1637
b'<inventory format="10" revision_id="a@cset-0-2b">\n'
1641
b'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1642
b' revision="a@cset-0-2b"'
1643
b' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1644
b' text_size="14" />\n'
1646
b'c 0 3 4 1\n', bytes_2b)
1647
bytes_3 = records[2][0]
1648
self.assertEqualDiff(
1650
b'<inventory format="10" revision_id="a@cset-0-3">\n'
1653
b'c 1 3 3 2\n', bytes_3)
1655
def test_creating_bundle_preserves_chk_pages(self):
1656
self.make_merged_branch()
1657
target = self.b1.controldir.sprout('target',
1658
revision_id=b'a@cset-0-2a').open_branch()
1659
bundle_txt, rev_ids = self.create_bundle_text(b'a@cset-0-2a',
1661
self.assertEqual(set([b'a@cset-0-2b', b'a@cset-0-3']), set(rev_ids))
1662
bundle = read_bundle(bundle_txt)
1664
self.addCleanup(target.unlock)
1665
install_bundle(target.repository, bundle)
1666
inv1 = next(self.b1.repository.inventories.get_record_stream([
1667
(b'a@cset-0-3',)], 'unordered',
1668
True)).get_bytes_as('fulltext')
1669
inv2 = next(target.repository.inventories.get_record_stream([
1670
(b'a@cset-0-3',)], 'unordered',
1671
True)).get_bytes_as('fulltext')
1672
self.assertEqualDiff(inv1, inv2)
1675
class MungedBundleTester(object):
1677
def build_test_bundle(self):
1678
wt = self.make_branch_and_tree('b1')
1680
self.build_tree(['b1/one'])
1682
wt.commit('add one', rev_id=b'a@cset-0-1')
1683
self.build_tree(['b1/two'])
1685
wt.commit('add two', rev_id=b'a@cset-0-2',
1686
revprops={u'branch-nick': 'test'})
1688
bundle_txt = BytesIO()
1689
rev_ids = write_bundle(wt.branch.repository, b'a@cset-0-2',
1690
b'a@cset-0-1', bundle_txt, self.format)
1691
self.assertEqual({b'a@cset-0-2'}, set(rev_ids))
1692
bundle_txt.seek(0, 0)
1695
def check_valid(self, bundle):
1696
"""Check that after whatever munging, the final object is valid."""
1697
self.assertEqual([b'a@cset-0-2'],
1698
[r.revision_id for r in bundle.real_revisions])
1700
def test_extra_whitespace(self):
1701
bundle_txt = self.build_test_bundle()
1703
# Seek to the end of the file
1704
# Adding one extra newline used to give us
1705
# TypeError: float() argument must be a string or a number
1706
bundle_txt.seek(0, 2)
1707
bundle_txt.write(b'\n')
1710
bundle = read_bundle(bundle_txt)
1711
self.check_valid(bundle)
1713
def test_extra_whitespace_2(self):
1714
bundle_txt = self.build_test_bundle()
1716
# Seek to the end of the file
1717
# Adding two extra newlines used to give us
1718
# MalformedPatches: The first line of all patches should be ...
1719
bundle_txt.seek(0, 2)
1720
bundle_txt.write(b'\n\n')
1723
bundle = read_bundle(bundle_txt)
1724
self.check_valid(bundle)
1727
class MungedBundleTesterV09(tests.TestCaseWithTransport, MungedBundleTester):
1731
def test_missing_trailing_whitespace(self):
1732
bundle_txt = self.build_test_bundle()
1734
# Remove a trailing newline, it shouldn't kill the parser
1735
raw = bundle_txt.getvalue()
1736
# The contents of the bundle don't have to be this, but this
1737
# test is concerned with the exact case where the serializer
1738
# creates a blank line at the end, and fails if that
1740
self.assertEqual(b'\n\n', raw[-2:])
1741
bundle_txt = BytesIO(raw[:-1])
1743
bundle = read_bundle(bundle_txt)
1744
self.check_valid(bundle)
1746
def test_opening_text(self):
1747
bundle_txt = self.build_test_bundle()
1749
bundle_txt = BytesIO(
1750
b"Some random\nemail comments\n" + bundle_txt.getvalue())
1752
bundle = read_bundle(bundle_txt)
1753
self.check_valid(bundle)
1755
def test_trailing_text(self):
1756
bundle_txt = self.build_test_bundle()
1758
bundle_txt = BytesIO(
1759
bundle_txt.getvalue() + b"Some trailing\nrandom\ntext\n")
1761
bundle = read_bundle(bundle_txt)
1762
self.check_valid(bundle)
1765
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
1770
class TestBundleWriterReader(tests.TestCase):
1772
def test_roundtrip_record(self):
1774
writer = v4.BundleWriter(fileobj)
1776
writer.add_info_record({b'foo': b'bar'})
1777
writer._add_record(b"Record body", {b'parents': [b'1', b'3'],
1778
b'storage_kind': b'fulltext'}, 'file', b'revid', b'fileid')
1781
reader = v4.BundleReader(fileobj, stream_input=True)
1782
record_iter = reader.iter_records()
1783
record = next(record_iter)
1784
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1785
'info', None, None), record)
1786
record = next(record_iter)
1787
self.assertEqual((b"Record body", {b'storage_kind': b'fulltext',
1788
b'parents': [b'1', b'3']}, 'file', b'revid', b'fileid'),
1791
def test_roundtrip_record_memory_hungry(self):
1793
writer = v4.BundleWriter(fileobj)
1795
writer.add_info_record({b'foo': b'bar'})
1796
writer._add_record(b"Record body", {b'parents': [b'1', b'3'],
1797
b'storage_kind': b'fulltext'}, 'file', b'revid', b'fileid')
1800
reader = v4.BundleReader(fileobj, stream_input=False)
1801
record_iter = reader.iter_records()
1802
record = next(record_iter)
1803
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1804
'info', None, None), record)
1805
record = next(record_iter)
1806
self.assertEqual((b"Record body", {b'storage_kind': b'fulltext',
1807
b'parents': [b'1', b'3']}, 'file', b'revid', b'fileid'),
1810
def test_encode_name(self):
1811
self.assertEqual(b'revision/rev1',
1812
v4.BundleWriter.encode_name('revision', b'rev1'))
1813
self.assertEqual(b'file/rev//1/file-id-1',
1814
v4.BundleWriter.encode_name('file', b'rev/1', b'file-id-1'))
1815
self.assertEqual(b'info',
1816
v4.BundleWriter.encode_name('info', None, None))
1818
def test_decode_name(self):
1819
self.assertEqual(('revision', b'rev1', None),
1820
v4.BundleReader.decode_name(b'revision/rev1'))
1821
self.assertEqual(('file', b'rev/1', b'file-id-1'),
1822
v4.BundleReader.decode_name(b'file/rev//1/file-id-1'))
1823
self.assertEqual(('info', None, None),
1824
v4.BundleReader.decode_name(b'info'))
1826
def test_too_many_names(self):
1828
writer = v4.BundleWriter(fileobj)
1830
writer.add_info_record({b'foo': b'bar'})
1831
writer._container.add_bytes_record(b'blah', [(b'two', ), (b'names', )])
1834
record_iter = v4.BundleReader(fileobj).iter_records()
1835
record = next(record_iter)
1836
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1837
'info', None, None), record)
1838
self.assertRaises(errors.BadBundle, next, record_iter)
1841
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1843
def test_read_mergeable_skips_local(self):
1844
"""A local bundle named like the URL should not be read.
1846
out, wt = test_read_bundle.create_bundle_file(self)
1848
class FooService(object):
1849
"""A directory service that always returns source"""
1851
def look_up(self, name, url):
1853
directories.register('foo:', FooService, 'Testing directory service')
1854
self.addCleanup(directories.remove, 'foo:')
1855
self.build_tree_contents([('./foo:bar', out.getvalue())])
1856
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1859
def test_infinite_redirects_are_not_a_bundle(self):
1860
"""If a URL causes TooManyRedirections then NotABundle is raised.
1862
from .blackbox.test_push import RedirectingMemoryServer
1863
server = RedirectingMemoryServer()
1864
self.start_server(server)
1865
url = server.get_url() + 'infinite-loop'
1866
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1868
def test_smart_server_connection_reset(self):
1869
"""If a smart server connection fails during the attempt to read a
1870
bundle, then the ConnectionReset error should be propagated.
1872
# Instantiate a server that will provoke a ConnectionReset
1873
sock_server = DisconnectingServer()
1874
self.start_server(sock_server)
1875
# We don't really care what the url is since the server will close the
1876
# connection without interpreting it
1877
url = sock_server.get_url()
1878
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1881
class DisconnectingHandler(socketserver.BaseRequestHandler):
1882
"""A request handler that immediately closes any connection made to it."""
1885
self.request.close()
1888
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1891
super(DisconnectingServer, self).__init__(
1893
test_server.TestingTCPServer,
1894
DisconnectingHandler)
1897
"""Return the url of the server"""
1898
return "bzr://%s:%d/" % self.server.server_address