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"""
68
return get_text(repo.inventories, (revision_id,))
73
class MockTree(object):
76
from ..bzr.inventory import InventoryDirectory, ROOT_ID
78
self.paths = {ROOT_ID: ""}
79
self.ids = {"": ROOT_ID}
81
self.root = InventoryDirectory(ROOT_ID, '', None)
83
inventory = property(lambda x: x)
84
root_inventory = property(lambda x: x)
86
def get_root_id(self):
87
return self.root.file_id
89
def all_file_ids(self):
90
return set(self.paths.keys())
92
def all_versioned_paths(self):
93
return set(self.paths.values())
95
def is_executable(self, path, file_id):
96
# Not all the files are executable.
99
def __getitem__(self, file_id):
100
if file_id == self.root.file_id:
103
return self.make_entry(file_id, self.paths[file_id])
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, file_id=None):
117
file_id = self.path2id(path)
118
if file_id in self.contents:
124
def make_entry(self, file_id, path):
125
from ..bzr.inventory import (InventoryFile, InventoryDirectory,
127
if not isinstance(file_id, bytes):
128
raise TypeError(file_id)
129
name = os.path.basename(path)
130
kind = self.kind(path, file_id)
131
parent_id = self.parent_id(file_id)
132
text_sha_1, text_size = self.contents_stats(path, file_id)
133
if kind == 'directory':
134
ie = InventoryDirectory(file_id, name, parent_id)
136
ie = InventoryFile(file_id, name, parent_id)
137
ie.text_sha1 = text_sha_1
138
ie.text_size = text_size
139
elif kind == 'symlink':
140
ie = InventoryLink(file_id, name, parent_id)
142
raise errors.BzrError('unknown kind %r' % kind)
145
def add_dir(self, file_id, path):
146
if not isinstance(file_id, bytes):
147
raise TypeError(file_id)
148
self.paths[file_id] = path
149
self.ids[path] = file_id
151
def add_file(self, file_id, path, contents):
152
if not isinstance(file_id, bytes):
153
raise TypeError(file_id)
154
self.add_dir(file_id, path)
155
self.contents[file_id] = contents
157
def path2id(self, path):
158
return self.ids.get(path)
160
def id2path(self, file_id):
161
return self.paths.get(file_id)
163
def has_id(self, file_id):
164
return self.id2path(file_id) is not None
166
def get_file(self, path, file_id=None):
168
file_id = self.path2id(path)
171
result.write(self.contents[file_id])
173
raise errors.NoSuchFile(path)
177
def get_file_revision(self, path, file_id=None):
179
file_id = self.path2id(path)
180
return self.inventory[file_id].revision
182
def get_file_size(self, path, file_id=None):
184
file_id = self.path2id(path)
185
return self.inventory[file_id].text_size
187
def get_file_sha1(self, path, file_id=None):
189
file_id = self.path2id(path)
190
return self.inventory[file_id].text_sha1
192
def contents_stats(self, path, file_id):
193
if file_id not in self.contents:
195
text_sha1 = osutils.sha_file(self.get_file(path, file_id))
196
return text_sha1, len(self.contents[file_id])
199
class BTreeTester(tests.TestCase):
200
"""A simple unittest tester for the BundleTree class."""
202
def make_tree_1(self):
204
mtree.add_dir(b"a", "grandparent")
205
mtree.add_dir(b"b", "grandparent/parent")
206
mtree.add_file(b"c", "grandparent/parent/file", b"Hello\n")
207
mtree.add_dir(b"d", "grandparent/alt_parent")
208
return BundleTree(mtree, ''), mtree
210
def test_renames(self):
211
"""Ensure that file renames have the proper effect on children"""
212
btree = self.make_tree_1()[0]
213
self.assertEqual(btree.old_path("grandparent"), "grandparent")
214
self.assertEqual(btree.old_path("grandparent/parent"),
215
"grandparent/parent")
216
self.assertEqual(btree.old_path("grandparent/parent/file"),
217
"grandparent/parent/file")
219
self.assertEqual(btree.id2path(b"a"), "grandparent")
220
self.assertEqual(btree.id2path(b"b"), "grandparent/parent")
221
self.assertEqual(btree.id2path(b"c"), "grandparent/parent/file")
223
self.assertEqual(btree.path2id("grandparent"), b"a")
224
self.assertEqual(btree.path2id("grandparent/parent"), b"b")
225
self.assertEqual(btree.path2id("grandparent/parent/file"), b"c")
227
self.assertIs(btree.path2id("grandparent2"), None)
228
self.assertIs(btree.path2id("grandparent2/parent"), None)
229
self.assertIs(btree.path2id("grandparent2/parent/file"), None)
231
btree.note_rename("grandparent", "grandparent2")
232
self.assertIs(btree.old_path("grandparent"), None)
233
self.assertIs(btree.old_path("grandparent/parent"), None)
234
self.assertIs(btree.old_path("grandparent/parent/file"), None)
236
self.assertEqual(btree.id2path(b"a"), "grandparent2")
237
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent")
238
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent/file")
240
self.assertEqual(btree.path2id("grandparent2"), b"a")
241
self.assertEqual(btree.path2id("grandparent2/parent"), b"b")
242
self.assertEqual(btree.path2id("grandparent2/parent/file"), b"c")
244
self.assertTrue(btree.path2id("grandparent") is None)
245
self.assertTrue(btree.path2id("grandparent/parent") is None)
246
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
248
btree.note_rename("grandparent/parent", "grandparent2/parent2")
249
self.assertEqual(btree.id2path(b"a"), "grandparent2")
250
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent2")
251
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent2/file")
253
self.assertEqual(btree.path2id("grandparent2"), b"a")
254
self.assertEqual(btree.path2id("grandparent2/parent2"), b"b")
255
self.assertEqual(btree.path2id("grandparent2/parent2/file"), b"c")
257
self.assertTrue(btree.path2id("grandparent2/parent") is None)
258
self.assertTrue(btree.path2id("grandparent2/parent/file") is None)
260
btree.note_rename("grandparent/parent/file",
261
"grandparent2/parent2/file2")
262
self.assertEqual(btree.id2path(b"a"), "grandparent2")
263
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent2")
264
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent2/file2")
266
self.assertEqual(btree.path2id("grandparent2"), b"a")
267
self.assertEqual(btree.path2id("grandparent2/parent2"), b"b")
268
self.assertEqual(btree.path2id("grandparent2/parent2/file2"), b"c")
270
self.assertTrue(btree.path2id("grandparent2/parent2/file") is None)
272
def test_moves(self):
273
"""Ensure that file moves have the proper effect on children"""
274
btree = self.make_tree_1()[0]
275
btree.note_rename("grandparent/parent/file",
276
"grandparent/alt_parent/file")
277
self.assertEqual(btree.id2path(b"c"), "grandparent/alt_parent/file")
278
self.assertEqual(btree.path2id("grandparent/alt_parent/file"), b"c")
279
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
281
def unified_diff(self, old, new):
283
diff.internal_diff("old", old, "new", new, out)
287
def make_tree_2(self):
288
btree = self.make_tree_1()[0]
289
btree.note_rename("grandparent/parent/file",
290
"grandparent/alt_parent/file")
291
self.assertTrue(btree.id2path(b"e") is None)
292
self.assertFalse(btree.is_versioned("grandparent/parent/file"))
293
btree.note_id(b"e", "grandparent/parent/file")
297
"""File/inventory adds"""
298
btree = self.make_tree_2()
299
add_patch = self.unified_diff([], [b"Extra cheese\n"])
300
btree.note_patch("grandparent/parent/file", add_patch)
301
btree.note_id(b'f', 'grandparent/parent/symlink', kind='symlink')
302
btree.note_target('grandparent/parent/symlink', 'venus')
303
self.adds_test(btree)
305
def adds_test(self, btree):
306
self.assertEqual(btree.id2path(b"e"), "grandparent/parent/file")
307
self.assertEqual(btree.path2id("grandparent/parent/file"), b"e")
308
with btree.get_file("grandparent/parent/file") as f:
309
self.assertEqual(f.read(), b"Extra cheese\n")
311
btree.get_symlink_target('grandparent/parent/symlink'), 'venus')
313
def test_adds2(self):
314
"""File/inventory adds, with patch-compatibile renames"""
315
btree = self.make_tree_2()
316
btree.contents_by_id = False
317
add_patch = self.unified_diff([b"Hello\n"], [b"Extra cheese\n"])
318
btree.note_patch("grandparent/parent/file", add_patch)
319
btree.note_id(b'f', 'grandparent/parent/symlink', kind='symlink')
320
btree.note_target('grandparent/parent/symlink', 'venus')
321
self.adds_test(btree)
323
def make_tree_3(self):
324
btree, mtree = self.make_tree_1()
325
mtree.add_file(b"e", "grandparent/parent/topping", b"Anchovies\n")
326
btree.note_rename("grandparent/parent/file",
327
"grandparent/alt_parent/file")
328
btree.note_rename("grandparent/parent/topping",
329
"grandparent/alt_parent/stopping")
332
def get_file_test(self, btree):
333
with btree.get_file(btree.id2path(b"e")) as f:
334
self.assertEqual(f.read(), b"Lemon\n")
335
with btree.get_file(btree.id2path(b"c")) as f:
336
self.assertEqual(f.read(), b"Hello\n")
338
def test_get_file(self):
339
"""Get file contents"""
340
btree = self.make_tree_3()
341
mod_patch = self.unified_diff([b"Anchovies\n"], [b"Lemon\n"])
342
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
343
self.get_file_test(btree)
345
def test_get_file2(self):
346
"""Get file contents, with patch-compatible renames"""
347
btree = self.make_tree_3()
348
btree.contents_by_id = False
349
mod_patch = self.unified_diff([], [b"Lemon\n"])
350
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
351
mod_patch = self.unified_diff([], [b"Hello\n"])
352
btree.note_patch("grandparent/alt_parent/file", mod_patch)
353
self.get_file_test(btree)
355
def test_delete(self):
357
btree = self.make_tree_1()[0]
358
with btree.get_file(btree.id2path(b"c")) as f:
359
self.assertEqual(f.read(), b"Hello\n")
360
btree.note_deletion("grandparent/parent/file")
361
self.assertTrue(btree.id2path(b"c") is None)
362
self.assertFalse(btree.is_versioned("grandparent/parent/file"))
364
def sorted_ids(self, tree):
365
ids = sorted(tree.all_file_ids())
368
def test_iteration(self):
369
"""Ensure that iteration through ids works properly"""
370
btree = self.make_tree_1()[0]
371
self.assertEqual(self.sorted_ids(btree),
372
[inventory.ROOT_ID, b'a', b'b', b'c', b'd'])
373
btree.note_deletion("grandparent/parent/file")
374
btree.note_id(b"e", "grandparent/alt_parent/fool", kind="directory")
375
btree.note_last_changed("grandparent/alt_parent/fool",
377
self.assertEqual(self.sorted_ids(btree),
378
[inventory.ROOT_ID, b'a', b'b', b'd', b'e'])
381
class BundleTester1(tests.TestCaseWithTransport):
383
def test_mismatched_bundle(self):
384
format = bzrdir.BzrDirMetaFormat1()
385
format.repository_format = knitrepo.RepositoryFormatKnit3()
386
serializer = BundleSerializerV08('0.8')
387
b = self.make_branch('.', format=format)
388
self.assertRaises(errors.IncompatibleBundleFormat, serializer.write,
389
b.repository, [], {}, BytesIO())
391
def test_matched_bundle(self):
392
"""Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
393
format = bzrdir.BzrDirMetaFormat1()
394
format.repository_format = knitrepo.RepositoryFormatKnit3()
395
serializer = BundleSerializerV09('0.9')
396
b = self.make_branch('.', format=format)
397
serializer.write(b.repository, [], {}, BytesIO())
399
def test_mismatched_model(self):
400
"""Try copying a bundle from knit2 to knit1"""
401
format = bzrdir.BzrDirMetaFormat1()
402
format.repository_format = knitrepo.RepositoryFormatKnit3()
403
source = self.make_branch_and_tree('source', format=format)
404
source.commit('one', rev_id=b'one-id')
405
source.commit('two', rev_id=b'two-id')
407
write_bundle(source.branch.repository, b'two-id', b'null:', text,
411
format = bzrdir.BzrDirMetaFormat1()
412
format.repository_format = knitrepo.RepositoryFormatKnit1()
413
target = self.make_branch('target', format=format)
414
self.assertRaises(errors.IncompatibleRevision, install_bundle,
415
target.repository, read_bundle(text))
418
class BundleTester(object):
420
def bzrdir_format(self):
421
format = bzrdir.BzrDirMetaFormat1()
422
format.repository_format = knitrepo.RepositoryFormatKnit1()
425
def make_branch_and_tree(self, path, format=None):
427
format = self.bzrdir_format()
428
return tests.TestCaseWithTransport.make_branch_and_tree(
431
def make_branch(self, path, format=None):
433
format = self.bzrdir_format()
434
return tests.TestCaseWithTransport.make_branch(self, path, format)
436
def create_bundle_text(self, base_rev_id, rev_id):
437
bundle_txt = BytesIO()
438
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
439
bundle_txt, format=self.format)
441
self.assertEqual(bundle_txt.readline(),
442
b'# Bazaar revision bundle v%s\n' % self.format.encode('ascii'))
443
self.assertEqual(bundle_txt.readline(), b'#\n')
445
rev = self.b1.repository.get_revision(rev_id)
446
self.assertEqual(bundle_txt.readline().decode('utf-8'),
449
return bundle_txt, rev_ids
451
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
452
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
453
Make sure that the text generated is valid, and that it
454
can be applied against the base, and generate the same information.
456
:return: The in-memory bundle
458
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
460
# This should also validate the generated bundle
461
bundle = read_bundle(bundle_txt)
462
repository = self.b1.repository
463
for bundle_rev in bundle.real_revisions:
464
# These really should have already been checked when we read the
465
# bundle, since it computes the sha1 hash for the revision, which
466
# only will match if everything is okay, but lets be explicit about
468
branch_rev = repository.get_revision(bundle_rev.revision_id)
469
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
470
'timestamp', 'timezone', 'message', 'committer',
471
'parent_ids', 'properties'):
472
self.assertEqual(getattr(branch_rev, a),
473
getattr(bundle_rev, a))
474
self.assertEqual(len(branch_rev.parent_ids),
475
len(bundle_rev.parent_ids))
476
self.assertEqual(rev_ids,
477
[r.revision_id for r in bundle.real_revisions])
478
self.valid_apply_bundle(base_rev_id, bundle, checkout_dir=checkout_dir)
482
def get_invalid_bundle(self, base_rev_id, rev_id):
483
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
484
Munge the text so that it's invalid.
486
:return: The in-memory bundle
488
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
489
new_text = bundle_txt.getvalue().replace(b'executable:no',
491
bundle_txt = BytesIO(new_text)
492
bundle = read_bundle(bundle_txt)
493
self.valid_apply_bundle(base_rev_id, bundle)
496
def test_non_bundle(self):
497
self.assertRaises(errors.NotABundle,
498
read_bundle, BytesIO(b'#!/bin/sh\n'))
500
def test_malformed(self):
501
self.assertRaises(errors.BadBundle, read_bundle,
502
BytesIO(b'# Bazaar revision bundle v'))
504
def test_crlf_bundle(self):
506
read_bundle(BytesIO(b'# Bazaar revision bundle v0.8\r\n'))
507
except errors.BadBundle:
508
# It is currently permitted for bundles with crlf line endings to
509
# make read_bundle raise a BadBundle, but this should be fixed.
510
# Anything else, especially NotABundle, is an error.
513
def get_checkout(self, rev_id, checkout_dir=None):
514
"""Get a new tree, with the specified revision in it.
517
if checkout_dir is None:
518
checkout_dir = osutils.mkdtemp(prefix='test-branch-', dir='.')
520
if not os.path.exists(checkout_dir):
521
os.mkdir(checkout_dir)
522
tree = self.make_branch_and_tree(checkout_dir)
524
ancestors = write_bundle(self.b1.repository, rev_id, b'null:', s,
527
self.assertIsInstance(s.getvalue(), bytes)
528
install_bundle(tree.branch.repository, read_bundle(s))
529
for ancestor in ancestors:
530
old = self.b1.repository.revision_tree(ancestor)
531
new = tree.branch.repository.revision_tree(ancestor)
535
# Check that there aren't any inventory level changes
536
delta = new.changes_from(old)
537
self.assertFalse(delta.has_changed(),
538
'Revision %s not copied correctly.'
541
# Now check that the file contents are all correct
542
for path in old.all_versioned_paths():
544
old_file = old.get_file(path)
545
except errors.NoSuchFile:
548
old_file.read(), new.get_file(path).read())
552
if not _mod_revision.is_null(rev_id):
553
tree.branch.generate_revision_history(rev_id)
555
delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
556
self.assertFalse(delta.has_changed(),
557
'Working tree has modifications: %s' % delta)
560
def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
561
"""Get the base revision, apply the changes, and make
562
sure everything matches the builtin branch.
564
to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
567
self._valid_apply_bundle(base_rev_id, info, to_tree)
571
def _valid_apply_bundle(self, base_rev_id, info, to_tree):
572
original_parents = to_tree.get_parent_ids()
573
repository = to_tree.branch.repository
574
original_parents = to_tree.get_parent_ids()
575
self.assertIs(repository.has_revision(base_rev_id), True)
576
for rev in info.real_revisions:
577
self.assertTrue(not repository.has_revision(rev.revision_id),
578
'Revision {%s} present before applying bundle'
580
merge_bundle(info, to_tree, True, merge.Merge3Merger, False, False)
582
for rev in info.real_revisions:
583
self.assertTrue(repository.has_revision(rev.revision_id),
584
'Missing revision {%s} after applying bundle'
587
self.assertTrue(to_tree.branch.repository.has_revision(info.target))
588
# Do we also want to verify that all the texts have been added?
590
self.assertEqual(original_parents + [info.target],
591
to_tree.get_parent_ids())
593
rev = info.real_revisions[-1]
594
base_tree = self.b1.repository.revision_tree(rev.revision_id)
595
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
597
# TODO: make sure the target tree is identical to base tree
598
# we might also check the working tree.
600
base_files = list(base_tree.list_files())
601
to_files = list(to_tree.list_files())
602
self.assertEqual(len(base_files), len(to_files))
603
for base_file, to_file in zip(base_files, to_files):
604
self.assertEqual(base_file, to_file)
606
for path, status, kind, fileid, entry in base_files:
607
# Check that the meta information is the same
608
self.assertEqual(base_tree.get_file_size(path, fileid),
609
to_tree.get_file_size(to_tree.id2path(fileid)))
610
self.assertEqual(base_tree.get_file_sha1(path, fileid),
611
to_tree.get_file_sha1(to_tree.id2path(fileid)))
612
# Check that the contents are the same
613
# This is pretty expensive
614
# self.assertEqual(base_tree.get_file(fileid).read(),
615
# to_tree.get_file(fileid).read())
617
def test_bundle(self):
618
self.tree1 = self.make_branch_and_tree('b1')
619
self.b1 = self.tree1.branch
621
self.build_tree_contents([('b1/one', b'one\n')])
622
self.tree1.add('one', b'one-id')
623
self.tree1.set_root_id(b'root-id')
624
self.tree1.commit('add one', rev_id=b'a@cset-0-1')
626
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-1')
628
# Make sure we can handle files with spaces, tabs, other
631
'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'
633
self.build_tree_contents([('b1/sub/sub/emptyfile.txt', b''),
634
('b1/dir/nolastnewline.txt', b'bloop')])
635
tt = TreeTransform(self.tree1)
636
tt.new_file('executable', tt.root, [b'#!/bin/sh\n'], b'exe-1', True)
638
# have to fix length of file-id so that we can predictably rewrite
639
# a (length-prefixed) record containing it later.
640
self.tree1.add('with space.txt', b'withspace-id')
642
'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'
644
self.tree1.commit('add whitespace', rev_id=b'a@cset-0-2')
646
bundle = self.get_valid_bundle(b'a@cset-0-1', b'a@cset-0-2')
648
# Check a rollup bundle
649
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-2')
653
['sub/sub/nonempty.txt', 'sub/sub/emptyfile.txt', 'sub/sub'
655
tt = TreeTransform(self.tree1)
656
trans_id = tt.trans_id_tree_path('executable')
657
tt.set_executability(False, trans_id)
659
self.tree1.commit('removed', rev_id=b'a@cset-0-3')
661
bundle = self.get_valid_bundle(b'a@cset-0-2', b'a@cset-0-3')
662
self.assertRaises((errors.TestamentMismatch,
663
errors.VersionedFileInvalidChecksum,
664
errors.BadBundle), self.get_invalid_bundle,
665
b'a@cset-0-2', b'a@cset-0-3')
666
# Check a rollup bundle
667
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-3')
669
# Now move the directory
670
self.tree1.rename_one('dir', 'sub/dir')
671
self.tree1.commit('rename dir', rev_id=b'a@cset-0-4')
673
bundle = self.get_valid_bundle(b'a@cset-0-3', b'a@cset-0-4')
674
# Check a rollup bundle
675
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-4')
678
with open('b1/sub/dir/WithCaps.txt', 'ab') as f:
679
f.write(b'\nAdding some text\n')
680
with open('b1/sub/dir/ pre space', 'ab') as f:
682
b'\r\nAdding some\r\nDOS format lines\r\n')
683
with open('b1/sub/dir/nolastnewline.txt', 'ab') as f:
685
self.tree1.rename_one('sub/dir/ pre space',
687
self.tree1.commit('Modified files', rev_id=b'a@cset-0-5')
688
bundle = self.get_valid_bundle(b'a@cset-0-4', b'a@cset-0-5')
690
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
691
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
692
self.tree1.rename_one('temp', 'with space.txt')
693
self.tree1.commit(u'swap filenames', rev_id=b'a@cset-0-6',
695
bundle = self.get_valid_bundle(b'a@cset-0-5', b'a@cset-0-6')
696
other = self.get_checkout(b'a@cset-0-5')
697
tree1_inv = get_inventory_text(self.tree1.branch.repository,
699
tree2_inv = get_inventory_text(other.branch.repository,
701
self.assertEqualDiff(tree1_inv, tree2_inv)
702
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
703
other.commit('rename file', rev_id=b'a@cset-0-6b')
704
self.tree1.merge_from_branch(other.branch)
705
self.tree1.commit(u'Merge', rev_id=b'a@cset-0-7',
707
bundle = self.get_valid_bundle(b'a@cset-0-6', b'a@cset-0-7')
709
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
712
self.requireFeature(features.SymlinkFeature)
713
self.tree1 = self.make_branch_and_tree('b1')
714
self.b1 = self.tree1.branch
716
tt = TreeTransform(self.tree1)
717
tt.new_symlink(link_name, tt.root, link_target, link_id)
719
self.tree1.commit('add symlink', rev_id=b'l@cset-0-1')
720
bundle = self.get_valid_bundle(b'null:', b'l@cset-0-1')
721
if getattr(bundle, 'revision_tree', None) is not None:
722
# Not all bundle formats supports revision_tree
723
bund_tree = bundle.revision_tree(self.b1.repository, b'l@cset-0-1')
725
link_target, bund_tree.get_symlink_target(link_name))
727
tt = TreeTransform(self.tree1)
728
trans_id = tt.trans_id_tree_path(link_name)
729
tt.adjust_path('link2', tt.root, trans_id)
730
tt.delete_contents(trans_id)
731
tt.create_symlink(new_link_target, trans_id)
733
self.tree1.commit('rename and change symlink', rev_id=b'l@cset-0-2')
734
bundle = self.get_valid_bundle(b'l@cset-0-1', b'l@cset-0-2')
735
if getattr(bundle, 'revision_tree', None) is not None:
736
# Not all bundle formats supports revision_tree
737
bund_tree = bundle.revision_tree(self.b1.repository, b'l@cset-0-2')
738
self.assertEqual(new_link_target,
739
bund_tree.get_symlink_target('link2'))
741
tt = TreeTransform(self.tree1)
742
trans_id = tt.trans_id_tree_path('link2')
743
tt.delete_contents(trans_id)
744
tt.create_symlink('jupiter', trans_id)
746
self.tree1.commit('just change symlink target', rev_id=b'l@cset-0-3')
747
bundle = self.get_valid_bundle(b'l@cset-0-2', b'l@cset-0-3')
749
tt = TreeTransform(self.tree1)
750
trans_id = tt.trans_id_tree_path('link2')
751
tt.delete_contents(trans_id)
753
self.tree1.commit('Delete symlink', rev_id=b'l@cset-0-4')
754
bundle = self.get_valid_bundle(b'l@cset-0-3', b'l@cset-0-4')
756
def test_symlink_bundle(self):
757
self._test_symlink_bundle('link', 'bar/foo', 'mars')
759
def test_unicode_symlink_bundle(self):
760
self.requireFeature(features.UnicodeFilenameFeature)
761
self._test_symlink_bundle(u'\N{Euro Sign}link',
762
u'bar/\N{Euro Sign}foo',
763
u'mars\N{Euro Sign}')
765
def test_binary_bundle(self):
766
self.tree1 = self.make_branch_and_tree('b1')
767
self.b1 = self.tree1.branch
768
tt = TreeTransform(self.tree1)
771
tt.new_file('file', tt.root, [
772
b'\x00\n\x00\r\x01\n\x02\r\xff'], b'binary-1')
773
tt.new_file('file2', tt.root, [b'\x01\n\x02\r\x03\n\x04\r\xff'],
776
self.tree1.commit('add binary', rev_id=b'b@cset-0-1')
777
self.get_valid_bundle(b'null:', b'b@cset-0-1')
780
tt = TreeTransform(self.tree1)
781
trans_id = tt.trans_id_tree_path('file')
782
tt.delete_contents(trans_id)
784
self.tree1.commit('delete binary', rev_id=b'b@cset-0-2')
785
self.get_valid_bundle(b'b@cset-0-1', b'b@cset-0-2')
788
tt = TreeTransform(self.tree1)
789
trans_id = tt.trans_id_tree_path('file2')
790
tt.adjust_path('file3', tt.root, trans_id)
791
tt.delete_contents(trans_id)
792
tt.create_file([b'file\rcontents\x00\n\x00'], trans_id)
794
self.tree1.commit('rename and modify binary', rev_id=b'b@cset-0-3')
795
self.get_valid_bundle(b'b@cset-0-2', b'b@cset-0-3')
798
tt = TreeTransform(self.tree1)
799
trans_id = tt.trans_id_tree_path('file3')
800
tt.delete_contents(trans_id)
801
tt.create_file([b'\x00file\rcontents'], trans_id)
803
self.tree1.commit('just modify binary', rev_id=b'b@cset-0-4')
804
self.get_valid_bundle(b'b@cset-0-3', b'b@cset-0-4')
807
self.get_valid_bundle(b'null:', b'b@cset-0-4')
809
def test_last_modified(self):
810
self.tree1 = self.make_branch_and_tree('b1')
811
self.b1 = self.tree1.branch
812
tt = TreeTransform(self.tree1)
813
tt.new_file('file', tt.root, [b'file'], b'file')
815
self.tree1.commit('create file', rev_id=b'a@lmod-0-1')
817
tt = TreeTransform(self.tree1)
818
trans_id = tt.trans_id_tree_path('file')
819
tt.delete_contents(trans_id)
820
tt.create_file([b'file2'], trans_id)
822
self.tree1.commit('modify text', rev_id=b'a@lmod-0-2a')
824
other = self.get_checkout(b'a@lmod-0-1')
825
tt = TreeTransform(other)
826
trans_id = tt.trans_id_tree_path('file2')
827
tt.delete_contents(trans_id)
828
tt.create_file([b'file2'], trans_id)
830
other.commit('modify text in another tree', rev_id=b'a@lmod-0-2b')
831
self.tree1.merge_from_branch(other.branch)
832
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-3',
834
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-4')
835
bundle = self.get_valid_bundle(b'a@lmod-0-2a', b'a@lmod-0-4')
837
def test_hide_history(self):
838
self.tree1 = self.make_branch_and_tree('b1')
839
self.b1 = self.tree1.branch
841
with open('b1/one', 'wb') as f:
843
self.tree1.add('one')
844
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
845
with open('b1/one', 'wb') as f:
847
self.tree1.commit('modify', rev_id=b'a@cset-0-2')
848
with open('b1/one', 'wb') as f:
850
self.tree1.commit('modify', rev_id=b'a@cset-0-3')
851
bundle_file = BytesIO()
852
rev_ids = write_bundle(self.tree1.branch.repository, b'a@cset-0-3',
853
b'a@cset-0-1', bundle_file, format=self.format)
854
self.assertNotContainsRe(bundle_file.getvalue(), b'\btwo\b')
855
self.assertContainsRe(self.get_raw(bundle_file), b'one')
856
self.assertContainsRe(self.get_raw(bundle_file), b'three')
858
def test_bundle_same_basis(self):
859
"""Ensure using the basis as the target doesn't cause an error"""
860
self.tree1 = self.make_branch_and_tree('b1')
861
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
862
bundle_file = BytesIO()
863
rev_ids = write_bundle(self.tree1.branch.repository, b'a@cset-0-1',
864
b'a@cset-0-1', bundle_file)
867
def get_raw(bundle_file):
868
return bundle_file.getvalue()
870
def test_unicode_bundle(self):
871
self.requireFeature(features.UnicodeFilenameFeature)
872
# Handle international characters
874
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
876
self.tree1 = self.make_branch_and_tree('b1')
877
self.b1 = self.tree1.branch
880
u'With international man of mystery\n'
881
u'William Dod\xe9\n').encode('utf-8'))
884
self.tree1.add([u'with Dod\N{Euro Sign}'], [b'withdod-id'])
885
self.tree1.commit(u'i18n commit from William Dod\xe9',
886
rev_id=b'i18n-1', committer=u'William Dod\xe9')
889
bundle = self.get_valid_bundle(b'null:', b'i18n-1')
892
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
893
f.write(u'Modified \xb5\n'.encode('utf8'))
895
self.tree1.commit(u'modified', rev_id=b'i18n-2')
897
bundle = self.get_valid_bundle(b'i18n-1', b'i18n-2')
900
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
901
self.tree1.commit(u'renamed, the new i18n man', rev_id=b'i18n-3',
902
committer=u'Erik B\xe5gfors')
904
bundle = self.get_valid_bundle(b'i18n-2', b'i18n-3')
907
self.tree1.remove([u'B\N{Euro Sign}gfors'])
908
self.tree1.commit(u'removed', rev_id=b'i18n-4')
910
bundle = self.get_valid_bundle(b'i18n-3', b'i18n-4')
913
bundle = self.get_valid_bundle(b'null:', b'i18n-4')
915
def test_whitespace_bundle(self):
916
if sys.platform in ('win32', 'cygwin'):
917
raise tests.TestSkipped('Windows doesn\'t support filenames'
918
' with tabs or trailing spaces')
919
self.tree1 = self.make_branch_and_tree('b1')
920
self.b1 = self.tree1.branch
922
self.build_tree(['b1/trailing space '])
923
self.tree1.add(['trailing space '])
924
# TODO: jam 20060701 Check for handling files with '\t' characters
925
# once we actually support them
928
self.tree1.commit('funky whitespace', rev_id=b'white-1')
930
bundle = self.get_valid_bundle(b'null:', b'white-1')
933
with open('b1/trailing space ', 'ab') as f:
934
f.write(b'add some text\n')
935
self.tree1.commit('add text', rev_id=b'white-2')
937
bundle = self.get_valid_bundle(b'white-1', b'white-2')
940
self.tree1.rename_one('trailing space ', ' start and end space ')
941
self.tree1.commit('rename', rev_id=b'white-3')
943
bundle = self.get_valid_bundle(b'white-2', b'white-3')
946
self.tree1.remove([' start and end space '])
947
self.tree1.commit('removed', rev_id=b'white-4')
949
bundle = self.get_valid_bundle(b'white-3', b'white-4')
951
# Now test a complet roll-up
952
bundle = self.get_valid_bundle(b'null:', b'white-4')
954
def test_alt_timezone_bundle(self):
955
self.tree1 = self.make_branch_and_memory_tree('b1')
956
self.b1 = self.tree1.branch
957
builder = treebuilder.TreeBuilder()
959
self.tree1.lock_write()
960
builder.start_tree(self.tree1)
961
builder.build(['newfile'])
962
builder.finish_tree()
964
# Asia/Colombo offset = 5 hours 30 minutes
965
self.tree1.commit('non-hour offset timezone', rev_id=b'tz-1',
966
timezone=19800, timestamp=1152544886.0)
968
bundle = self.get_valid_bundle(b'null:', b'tz-1')
970
rev = bundle.revisions[0]
971
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
972
self.assertEqual(19800, rev.timezone)
973
self.assertEqual(1152544886.0, rev.timestamp)
976
def test_bundle_root_id(self):
977
self.tree1 = self.make_branch_and_tree('b1')
978
self.b1 = self.tree1.branch
979
self.tree1.commit('message', rev_id=b'revid1')
980
bundle = self.get_valid_bundle(b'null:', b'revid1')
981
tree = self.get_bundle_tree(bundle, b'revid1')
982
root_revision = tree.get_file_revision(u'', tree.get_root_id())
983
self.assertEqual(b'revid1', root_revision)
985
def test_install_revisions(self):
986
self.tree1 = self.make_branch_and_tree('b1')
987
self.b1 = self.tree1.branch
988
self.tree1.commit('message', rev_id=b'rev2a')
989
bundle = self.get_valid_bundle(b'null:', b'rev2a')
990
branch2 = self.make_branch('b2')
991
self.assertFalse(branch2.repository.has_revision(b'rev2a'))
992
target_revision = bundle.install_revisions(branch2.repository)
993
self.assertTrue(branch2.repository.has_revision(b'rev2a'))
994
self.assertEqual(b'rev2a', target_revision)
996
def test_bundle_empty_property(self):
997
"""Test serializing revision properties with an empty value."""
998
tree = self.make_branch_and_memory_tree('tree')
1000
self.addCleanup(tree.unlock)
1001
tree.add([''], [b'TREE_ROOT'])
1002
tree.commit('One', revprops={u'one': 'two',
1003
u'empty': ''}, rev_id=b'rev1')
1004
self.b1 = tree.branch
1005
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1006
bundle = read_bundle(bundle_sio)
1007
revision_info = bundle.revisions[0]
1008
self.assertEqual(b'rev1', revision_info.revision_id)
1009
rev = revision_info.as_revision()
1010
self.assertEqual({'branch-nick': 'tree', 'empty': '', 'one': 'two'},
1013
def test_bundle_sorted_properties(self):
1014
"""For stability the writer should write properties in sorted order."""
1015
tree = self.make_branch_and_memory_tree('tree')
1017
self.addCleanup(tree.unlock)
1019
tree.add([''], [b'TREE_ROOT'])
1020
tree.commit('One', rev_id=b'rev1',
1021
revprops={u'a': '4', u'b': '3', u'c': '2', u'd': '1'})
1022
self.b1 = tree.branch
1023
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1024
bundle = read_bundle(bundle_sio)
1025
revision_info = bundle.revisions[0]
1026
self.assertEqual(b'rev1', revision_info.revision_id)
1027
rev = revision_info.as_revision()
1028
self.assertEqual({'branch-nick': 'tree', 'a': '4', 'b': '3', 'c': '2',
1029
'd': '1'}, rev.properties)
1031
def test_bundle_unicode_properties(self):
1032
"""We should be able to round trip a non-ascii property."""
1033
tree = self.make_branch_and_memory_tree('tree')
1035
self.addCleanup(tree.unlock)
1037
tree.add([''], [b'TREE_ROOT'])
1038
# Revisions themselves do not require anything about revision property
1039
# keys, other than that they are a basestring, and do not contain
1041
# However, Testaments assert than they are str(), and thus should not
1043
tree.commit('One', rev_id=b'rev1',
1044
revprops={u'omega': u'\u03a9', u'alpha': u'\u03b1'})
1045
self.b1 = tree.branch
1046
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1047
bundle = read_bundle(bundle_sio)
1048
revision_info = bundle.revisions[0]
1049
self.assertEqual(b'rev1', revision_info.revision_id)
1050
rev = revision_info.as_revision()
1051
self.assertEqual({'branch-nick': 'tree', 'omega': u'\u03a9',
1052
'alpha': u'\u03b1'}, rev.properties)
1054
def test_bundle_with_ghosts(self):
1055
tree = self.make_branch_and_tree('tree')
1056
self.b1 = tree.branch
1057
self.build_tree_contents([('tree/file', b'content1')])
1060
self.build_tree_contents([('tree/file', b'content2')])
1061
tree.add_parent_tree_id(b'ghost')
1062
tree.commit('rev2', rev_id=b'rev2')
1063
bundle = self.get_valid_bundle(b'null:', b'rev2')
1065
def make_simple_tree(self, format=None):
1066
tree = self.make_branch_and_tree('b1', format=format)
1067
self.b1 = tree.branch
1068
self.build_tree(['b1/file'])
1072
def test_across_serializers(self):
1073
tree = self.make_simple_tree('knit')
1074
tree.commit('hello', rev_id=b'rev1')
1075
tree.commit('hello', rev_id=b'rev2')
1076
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1077
repo = self.make_repository('repo', format='dirstate-with-subtree')
1078
bundle.install_revisions(repo)
1079
inv_text = repo._get_inventory_xml(b'rev2')
1080
self.assertNotContainsRe(inv_text, b'format="5"')
1081
self.assertContainsRe(inv_text, b'format="7"')
1083
def make_repo_with_installed_revisions(self):
1084
tree = self.make_simple_tree('knit')
1085
tree.commit('hello', rev_id=b'rev1')
1086
tree.commit('hello', rev_id=b'rev2')
1087
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1088
repo = self.make_repository('repo', format='dirstate-with-subtree')
1089
bundle.install_revisions(repo)
1092
def test_across_models(self):
1093
repo = self.make_repo_with_installed_revisions()
1094
inv = repo.get_inventory(b'rev2')
1095
self.assertEqual(b'rev2', inv.root.revision)
1096
root_id = inv.root.file_id
1098
self.addCleanup(repo.unlock)
1099
self.assertEqual({(root_id, b'rev1'): (),
1100
(root_id, b'rev2'): ((root_id, b'rev1'),)},
1101
repo.texts.get_parent_map([(root_id, b'rev1'), (root_id, b'rev2')]))
1103
def test_inv_hash_across_serializers(self):
1104
repo = self.make_repo_with_installed_revisions()
1105
recorded_inv_sha1 = repo.get_revision(b'rev2').inventory_sha1
1106
xml = repo._get_inventory_xml(b'rev2')
1107
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1109
def test_across_models_incompatible(self):
1110
tree = self.make_simple_tree('dirstate-with-subtree')
1111
tree.commit('hello', rev_id=b'rev1')
1112
tree.commit('hello', rev_id=b'rev2')
1114
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1115
except errors.IncompatibleBundleFormat:
1116
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1117
repo = self.make_repository('repo', format='knit')
1118
bundle.install_revisions(repo)
1120
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1121
self.assertRaises(errors.IncompatibleRevision,
1122
bundle.install_revisions, repo)
1124
def test_get_merge_request(self):
1125
tree = self.make_simple_tree()
1126
tree.commit('hello', rev_id=b'rev1')
1127
tree.commit('hello', rev_id=b'rev2')
1128
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1129
result = bundle.get_merge_request(tree.branch.repository)
1130
self.assertEqual((None, b'rev1', 'inapplicable'), result)
1132
def test_with_subtree(self):
1133
tree = self.make_branch_and_tree('tree',
1134
format='dirstate-with-subtree')
1135
self.b1 = tree.branch
1136
subtree = self.make_branch_and_tree('tree/subtree',
1137
format='dirstate-with-subtree')
1139
tree.commit('hello', rev_id=b'rev1')
1141
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1142
except errors.IncompatibleBundleFormat:
1143
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1144
if isinstance(bundle, v09.BundleInfo09):
1145
raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
1146
repo = self.make_repository('repo', format='knit')
1147
self.assertRaises(errors.IncompatibleRevision,
1148
bundle.install_revisions, repo)
1149
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1150
bundle.install_revisions(repo2)
1152
def test_revision_id_with_slash(self):
1153
self.tree1 = self.make_branch_and_tree('tree')
1154
self.b1 = self.tree1.branch
1156
self.tree1.commit('Revision/id/with/slashes', rev_id=b'rev/id')
1158
raise tests.TestSkipped(
1159
"Repository doesn't support revision ids with slashes")
1160
bundle = self.get_valid_bundle(b'null:', b'rev/id')
1162
def test_skip_file(self):
1163
"""Make sure we don't accidentally write to the wrong versionedfile"""
1164
self.tree1 = self.make_branch_and_tree('tree')
1165
self.b1 = self.tree1.branch
1166
# rev1 is not present in bundle, done by fetch
1167
self.build_tree_contents([('tree/file2', b'contents1')])
1168
self.tree1.add('file2', b'file2-id')
1169
self.tree1.commit('rev1', rev_id=b'reva')
1170
self.build_tree_contents([('tree/file3', b'contents2')])
1171
# rev2 is present in bundle, and done by fetch
1172
# having file1 in the bunle causes file1's versionedfile to be opened.
1173
self.tree1.add('file3', b'file3-id')
1174
rev2 = self.tree1.commit('rev2')
1175
# Updating file2 should not cause an attempt to add to file1's vf
1176
target = self.tree1.controldir.sprout('target').open_workingtree()
1177
self.build_tree_contents([('tree/file2', b'contents3')])
1178
self.tree1.commit('rev3', rev_id=b'rev3')
1179
bundle = self.get_valid_bundle(b'reva', b'rev3')
1180
if getattr(bundle, 'get_bundle_reader', None) is None:
1181
raise tests.TestSkipped('Bundle format cannot provide reader')
1183
(f, r) for b, m, k, r, f in bundle.get_bundle_reader().iter_records()
1186
{(b'file2-id', b'rev3'), (b'file3-id', rev2)}, file_ids)
1187
bundle.install_revisions(target.branch.repository)
1190
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1194
def test_bundle_empty_property(self):
1195
"""Test serializing revision properties with an empty value."""
1196
tree = self.make_branch_and_memory_tree('tree')
1198
self.addCleanup(tree.unlock)
1199
tree.add([''], [b'TREE_ROOT'])
1200
tree.commit('One', revprops={u'one': 'two',
1201
u'empty': ''}, rev_id=b'rev1')
1202
self.b1 = tree.branch
1203
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1204
self.assertContainsRe(bundle_sio.getvalue(),
1206
b'# branch-nick: tree\n'
1210
bundle = read_bundle(bundle_sio)
1211
revision_info = bundle.revisions[0]
1212
self.assertEqual(b'rev1', revision_info.revision_id)
1213
rev = revision_info.as_revision()
1214
self.assertEqual({'branch-nick': 'tree', 'empty': '', 'one': 'two'},
1217
def get_bundle_tree(self, bundle, revision_id):
1218
repository = self.make_repository('repo')
1219
return bundle.revision_tree(repository, b'revid1')
1221
def test_bundle_empty_property_alt(self):
1222
"""Test serializing revision properties with an empty value.
1224
Older readers had a bug when reading an empty property.
1225
They assumed that all keys ended in ': \n'. However they would write an
1226
empty value as ':\n'. This tests make sure that all newer bzr versions
1227
can handle th second form.
1229
tree = self.make_branch_and_memory_tree('tree')
1231
self.addCleanup(tree.unlock)
1232
tree.add([''], [b'TREE_ROOT'])
1233
tree.commit('One', revprops={u'one': 'two',
1234
u'empty': ''}, rev_id=b'rev1')
1235
self.b1 = tree.branch
1236
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1237
txt = bundle_sio.getvalue()
1238
loc = txt.find(b'# empty: ') + len(b'# empty:')
1239
# Create a new bundle, which strips the trailing space after empty
1240
bundle_sio = BytesIO(txt[:loc] + txt[loc + 1:])
1242
self.assertContainsRe(bundle_sio.getvalue(),
1244
b'# branch-nick: tree\n'
1248
bundle = read_bundle(bundle_sio)
1249
revision_info = bundle.revisions[0]
1250
self.assertEqual(b'rev1', revision_info.revision_id)
1251
rev = revision_info.as_revision()
1252
self.assertEqual({'branch-nick': 'tree', 'empty': '', 'one': 'two'},
1255
def test_bundle_sorted_properties(self):
1256
"""For stability the writer should write properties in sorted order."""
1257
tree = self.make_branch_and_memory_tree('tree')
1259
self.addCleanup(tree.unlock)
1261
tree.add([''], [b'TREE_ROOT'])
1262
tree.commit('One', rev_id=b'rev1',
1263
revprops={u'a': '4', u'b': '3', u'c': '2', u'd': '1'})
1264
self.b1 = tree.branch
1265
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1266
self.assertContainsRe(bundle_sio.getvalue(),
1270
b'# branch-nick: tree\n'
1274
bundle = read_bundle(bundle_sio)
1275
revision_info = bundle.revisions[0]
1276
self.assertEqual(b'rev1', revision_info.revision_id)
1277
rev = revision_info.as_revision()
1278
self.assertEqual({'branch-nick': 'tree', 'a': '4', 'b': '3', 'c': '2',
1279
'd': '1'}, rev.properties)
1281
def test_bundle_unicode_properties(self):
1282
"""We should be able to round trip a non-ascii property."""
1283
tree = self.make_branch_and_memory_tree('tree')
1285
self.addCleanup(tree.unlock)
1287
tree.add([''], [b'TREE_ROOT'])
1288
# Revisions themselves do not require anything about revision property
1289
# keys, other than that they are a basestring, and do not contain
1291
# However, Testaments assert than they are str(), and thus should not
1293
tree.commit('One', rev_id=b'rev1',
1294
revprops={u'omega': u'\u03a9', u'alpha': u'\u03b1'})
1295
self.b1 = tree.branch
1296
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1297
self.assertContainsRe(bundle_sio.getvalue(),
1299
b'# alpha: \xce\xb1\n'
1300
b'# branch-nick: tree\n'
1301
b'# omega: \xce\xa9\n'
1303
bundle = read_bundle(bundle_sio)
1304
revision_info = bundle.revisions[0]
1305
self.assertEqual(b'rev1', revision_info.revision_id)
1306
rev = revision_info.as_revision()
1307
self.assertEqual({'branch-nick': 'tree', 'omega': u'\u03a9',
1308
'alpha': u'\u03b1'}, rev.properties)
1311
class V09BundleKnit2Tester(V08BundleTester):
1315
def bzrdir_format(self):
1316
format = bzrdir.BzrDirMetaFormat1()
1317
format.repository_format = knitrepo.RepositoryFormatKnit3()
1321
class V09BundleKnit1Tester(V08BundleTester):
1325
def bzrdir_format(self):
1326
format = bzrdir.BzrDirMetaFormat1()
1327
format.repository_format = knitrepo.RepositoryFormatKnit1()
1331
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
1335
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1336
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1337
Make sure that the text generated is valid, and that it
1338
can be applied against the base, and generate the same information.
1340
:return: The in-memory bundle
1342
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1344
# This should also validate the generated bundle
1345
bundle = read_bundle(bundle_txt)
1346
repository = self.b1.repository
1347
for bundle_rev in bundle.real_revisions:
1348
# These really should have already been checked when we read the
1349
# bundle, since it computes the sha1 hash for the revision, which
1350
# only will match if everything is okay, but lets be explicit about
1352
branch_rev = repository.get_revision(bundle_rev.revision_id)
1353
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1354
'timestamp', 'timezone', 'message', 'committer',
1355
'parent_ids', 'properties'):
1356
self.assertEqual(getattr(branch_rev, a),
1357
getattr(bundle_rev, a))
1358
self.assertEqual(len(branch_rev.parent_ids),
1359
len(bundle_rev.parent_ids))
1360
self.assertEqual(set(rev_ids),
1361
{r.revision_id for r in bundle.real_revisions})
1362
self.valid_apply_bundle(base_rev_id, bundle,
1363
checkout_dir=checkout_dir)
1367
def get_invalid_bundle(self, base_rev_id, rev_id):
1368
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1369
Munge the text so that it's invalid.
1371
:return: The in-memory bundle
1373
from ..bundle import serializer
1374
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1375
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1376
new_text = new_text.replace(b'<file file_id="exe-1"',
1377
b'<file executable="y" file_id="exe-1"')
1378
new_text = new_text.replace(b'B260', b'B275')
1379
bundle_txt = BytesIO()
1380
bundle_txt.write(serializer._get_bundle_header('4'))
1381
bundle_txt.write(b'\n')
1382
bundle_txt.write(bz2.compress(new_text))
1384
bundle = read_bundle(bundle_txt)
1385
self.valid_apply_bundle(base_rev_id, bundle)
1388
def create_bundle_text(self, base_rev_id, rev_id):
1389
bundle_txt = BytesIO()
1390
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1391
bundle_txt, format=self.format)
1393
self.assertEqual(bundle_txt.readline(),
1394
b'# Bazaar revision bundle v%s\n' % self.format.encode('ascii'))
1395
self.assertEqual(bundle_txt.readline(), b'#\n')
1396
rev = self.b1.repository.get_revision(rev_id)
1398
return bundle_txt, rev_ids
1400
def get_bundle_tree(self, bundle, revision_id):
1401
repository = self.make_repository('repo')
1402
bundle.install_revisions(repository)
1403
return repository.revision_tree(revision_id)
1405
def test_creation(self):
1406
tree = self.make_branch_and_tree('tree')
1407
self.build_tree_contents([('tree/file', b'contents1\nstatic\n')])
1408
tree.add('file', b'fileid-2')
1409
tree.commit('added file', rev_id=b'rev1')
1410
self.build_tree_contents([('tree/file', b'contents2\nstatic\n')])
1411
tree.commit('changed file', rev_id=b'rev2')
1413
serializer = BundleSerializerV4('1.0')
1414
with tree.lock_read():
1415
serializer.write_bundle(
1416
tree.branch.repository, b'rev2', b'null:', s)
1418
tree2 = self.make_branch_and_tree('target')
1419
target_repo = tree2.branch.repository
1420
install_bundle(target_repo, serializer.read(s))
1421
target_repo.lock_read()
1422
self.addCleanup(target_repo.unlock)
1423
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1424
repo_texts = dict((i, b''.join(content)) for i, content
1425
in target_repo.iter_files_bytes(
1426
[(b'fileid-2', b'rev1', '1'),
1427
(b'fileid-2', b'rev2', '2')]))
1428
self.assertEqual({'1': b'contents1\nstatic\n',
1429
'2': b'contents2\nstatic\n'},
1431
rtree = target_repo.revision_tree(b'rev2')
1432
inventory_vf = target_repo.inventories
1433
# If the inventory store has a graph, it must match the revision graph.
1435
[inventory_vf.get_parent_map([(b'rev2',)])[(b'rev2',)]],
1436
[None, ((b'rev1',),)])
1437
self.assertEqual('changed file',
1438
target_repo.get_revision(b'rev2').message)
1441
def get_raw(bundle_file):
1443
line = bundle_file.readline()
1444
line = bundle_file.readline()
1445
lines = bundle_file.readlines()
1446
return bz2.decompress(b''.join(lines))
1448
def test_copy_signatures(self):
1449
tree_a = self.make_branch_and_tree('tree_a')
1451
import breezy.commit as commit
1452
oldstrategy = breezy.gpg.GPGStrategy
1453
branch = tree_a.branch
1454
repo_a = branch.repository
1455
tree_a.commit("base", allow_pointless=True, rev_id=b'A')
1456
self.assertFalse(branch.repository.has_signature_for_revision_id(b'A'))
1458
from ..testament import Testament
1459
# monkey patch gpg signing mechanism
1460
breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
1461
new_config = test_commit.MustSignConfig()
1462
commit.Commit(config_stack=new_config).commit(message="base",
1463
allow_pointless=True,
1465
working_tree=tree_a)
1468
return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
1469
self.assertTrue(repo_a.has_signature_for_revision_id(b'B'))
1471
breezy.gpg.GPGStrategy = oldstrategy
1472
tree_b = self.make_branch_and_tree('tree_b')
1473
repo_b = tree_b.branch.repository
1475
serializer = BundleSerializerV4('4')
1476
with tree_a.lock_read():
1477
serializer.write_bundle(
1478
tree_a.branch.repository, b'B', b'null:', s)
1480
install_bundle(repo_b, serializer.read(s))
1481
self.assertTrue(repo_b.has_signature_for_revision_id(b'B'))
1482
self.assertEqual(repo_b.get_signature_text(b'B'),
1483
repo_a.get_signature_text(b'B'))
1485
# ensure repeat installs are harmless
1486
install_bundle(repo_b, serializer.read(s))
1489
class V4_2aBundleTester(V4BundleTester):
1491
def bzrdir_format(self):
1494
def get_invalid_bundle(self, base_rev_id, rev_id):
1495
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1496
Munge the text so that it's invalid.
1498
:return: The in-memory bundle
1500
from ..bundle import serializer
1501
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1502
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1503
# We are going to be replacing some text to set the executable bit on a
1504
# file. Make sure the text replacement actually works correctly.
1505
self.assertContainsRe(new_text, b'(?m)B244\n\ni 1\n<inventory')
1506
new_text = new_text.replace(b'<file file_id="exe-1"',
1507
b'<file executable="y" file_id="exe-1"')
1508
new_text = new_text.replace(b'B244', b'B259')
1509
bundle_txt = BytesIO()
1510
bundle_txt.write(serializer._get_bundle_header('4'))
1511
bundle_txt.write(b'\n')
1512
bundle_txt.write(bz2.compress(new_text))
1514
bundle = read_bundle(bundle_txt)
1515
self.valid_apply_bundle(base_rev_id, bundle)
1518
def make_merged_branch(self):
1519
builder = self.make_branch_builder('source')
1520
builder.start_series()
1521
builder.build_snapshot(None, [
1522
('add', ('', b'root-id', 'directory', None)),
1523
('add', ('file', b'file-id', 'file', b'original content\n')),
1524
], revision_id=b'a@cset-0-1')
1525
builder.build_snapshot([b'a@cset-0-1'], [
1526
('modify', ('file', b'new-content\n')),
1527
], revision_id=b'a@cset-0-2a')
1528
builder.build_snapshot([b'a@cset-0-1'], [
1529
('add', ('other-file', b'file2-id', 'file', b'file2-content\n')),
1530
], revision_id=b'a@cset-0-2b')
1531
builder.build_snapshot([b'a@cset-0-2a', b'a@cset-0-2b'], [
1532
('add', ('other-file', b'file2-id', 'file', b'file2-content\n')),
1533
], revision_id=b'a@cset-0-3')
1534
builder.finish_series()
1535
self.b1 = builder.get_branch()
1537
self.addCleanup(self.b1.unlock)
1539
def make_bundle_just_inventories(self, base_revision_id,
1543
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1544
self.b1.repository, sio)
1545
writer.bundle.begin()
1546
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1551
def test_single_inventory_multiple_parents_as_xml(self):
1552
self.make_merged_branch()
1553
sio = self.make_bundle_just_inventories(b'a@cset-0-1', b'a@cset-0-3',
1555
reader = v4.BundleReader(sio, stream_input=False)
1556
records = list(reader.iter_records())
1557
self.assertEqual(1, len(records))
1558
(bytes, metadata, repo_kind, revision_id,
1559
file_id) = records[0]
1560
self.assertIs(None, file_id)
1561
self.assertEqual(b'a@cset-0-3', revision_id)
1562
self.assertEqual('inventory', repo_kind)
1563
self.assertEqual({b'parents': [b'a@cset-0-2a', b'a@cset-0-2b'],
1564
b'sha1': b'09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1565
b'storage_kind': b'mpdiff',
1567
# We should have an mpdiff that takes some lines from both parents.
1568
self.assertEqualDiff(
1570
b'<inventory format="10" revision_id="a@cset-0-3">\n'
1573
b'c 1 3 3 2\n', bytes)
1575
def test_single_inv_no_parents_as_xml(self):
1576
self.make_merged_branch()
1577
sio = self.make_bundle_just_inventories(b'null:', b'a@cset-0-1',
1579
reader = v4.BundleReader(sio, stream_input=False)
1580
records = list(reader.iter_records())
1581
self.assertEqual(1, len(records))
1582
(bytes, metadata, repo_kind, revision_id,
1583
file_id) = records[0]
1584
self.assertIs(None, file_id)
1585
self.assertEqual(b'a@cset-0-1', revision_id)
1586
self.assertEqual('inventory', repo_kind)
1587
self.assertEqual({b'parents': [],
1588
b'sha1': b'a13f42b142d544aac9b085c42595d304150e31a2',
1589
b'storage_kind': b'mpdiff',
1591
# We should have an mpdiff that takes some lines from both parents.
1592
self.assertEqualDiff(
1594
b'<inventory format="10" revision_id="a@cset-0-1">\n'
1595
b'<directory file_id="root-id" name=""'
1596
b' revision="a@cset-0-1" />\n'
1597
b'<file file_id="file-id" name="file" parent_id="root-id"'
1598
b' revision="a@cset-0-1"'
1599
b' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1600
b' text_size="17" />\n'
1604
def test_multiple_inventories_as_xml(self):
1605
self.make_merged_branch()
1606
sio = self.make_bundle_just_inventories(b'a@cset-0-1', b'a@cset-0-3',
1607
[b'a@cset-0-2a', b'a@cset-0-2b', b'a@cset-0-3'])
1608
reader = v4.BundleReader(sio, stream_input=False)
1609
records = list(reader.iter_records())
1610
self.assertEqual(3, len(records))
1611
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1612
self.assertEqual([b'a@cset-0-2a', b'a@cset-0-2b', b'a@cset-0-3'],
1614
metadata_2a = records[0][1]
1615
self.assertEqual({b'parents': [b'a@cset-0-1'],
1616
b'sha1': b'1e105886d62d510763e22885eec733b66f5f09bf',
1617
b'storage_kind': b'mpdiff',
1619
metadata_2b = records[1][1]
1620
self.assertEqual({b'parents': [b'a@cset-0-1'],
1621
b'sha1': b'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1622
b'storage_kind': b'mpdiff',
1624
metadata_3 = records[2][1]
1625
self.assertEqual({b'parents': [b'a@cset-0-2a', b'a@cset-0-2b'],
1626
b'sha1': b'09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1627
b'storage_kind': b'mpdiff',
1629
bytes_2a = records[0][0]
1630
self.assertEqualDiff(
1632
b'<inventory format="10" revision_id="a@cset-0-2a">\n'
1636
b'<file file_id="file-id" name="file" parent_id="root-id"'
1637
b' revision="a@cset-0-2a"'
1638
b' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1639
b' text_size="12" />\n'
1641
b'c 0 3 3 1\n', bytes_2a)
1642
bytes_2b = records[1][0]
1643
self.assertEqualDiff(
1645
b'<inventory format="10" revision_id="a@cset-0-2b">\n'
1649
b'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1650
b' revision="a@cset-0-2b"'
1651
b' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1652
b' text_size="14" />\n'
1654
b'c 0 3 4 1\n', bytes_2b)
1655
bytes_3 = records[2][0]
1656
self.assertEqualDiff(
1658
b'<inventory format="10" revision_id="a@cset-0-3">\n'
1661
b'c 1 3 3 2\n', bytes_3)
1663
def test_creating_bundle_preserves_chk_pages(self):
1664
self.make_merged_branch()
1665
target = self.b1.controldir.sprout('target',
1666
revision_id=b'a@cset-0-2a').open_branch()
1667
bundle_txt, rev_ids = self.create_bundle_text(b'a@cset-0-2a',
1669
self.assertEqual(set([b'a@cset-0-2b', b'a@cset-0-3']), set(rev_ids))
1670
bundle = read_bundle(bundle_txt)
1672
self.addCleanup(target.unlock)
1673
install_bundle(target.repository, bundle)
1674
inv1 = next(self.b1.repository.inventories.get_record_stream([
1675
(b'a@cset-0-3',)], 'unordered',
1676
True)).get_bytes_as('fulltext')
1677
inv2 = next(target.repository.inventories.get_record_stream([
1678
(b'a@cset-0-3',)], 'unordered',
1679
True)).get_bytes_as('fulltext')
1680
self.assertEqualDiff(inv1, inv2)
1683
class MungedBundleTester(object):
1685
def build_test_bundle(self):
1686
wt = self.make_branch_and_tree('b1')
1688
self.build_tree(['b1/one'])
1690
wt.commit('add one', rev_id=b'a@cset-0-1')
1691
self.build_tree(['b1/two'])
1693
wt.commit('add two', rev_id=b'a@cset-0-2',
1694
revprops={u'branch-nick': 'test'})
1696
bundle_txt = BytesIO()
1697
rev_ids = write_bundle(wt.branch.repository, b'a@cset-0-2',
1698
b'a@cset-0-1', bundle_txt, self.format)
1699
self.assertEqual({b'a@cset-0-2'}, set(rev_ids))
1700
bundle_txt.seek(0, 0)
1703
def check_valid(self, bundle):
1704
"""Check that after whatever munging, the final object is valid."""
1705
self.assertEqual([b'a@cset-0-2'],
1706
[r.revision_id for r in bundle.real_revisions])
1708
def test_extra_whitespace(self):
1709
bundle_txt = self.build_test_bundle()
1711
# Seek to the end of the file
1712
# Adding one extra newline used to give us
1713
# TypeError: float() argument must be a string or a number
1714
bundle_txt.seek(0, 2)
1715
bundle_txt.write(b'\n')
1718
bundle = read_bundle(bundle_txt)
1719
self.check_valid(bundle)
1721
def test_extra_whitespace_2(self):
1722
bundle_txt = self.build_test_bundle()
1724
# Seek to the end of the file
1725
# Adding two extra newlines used to give us
1726
# MalformedPatches: The first line of all patches should be ...
1727
bundle_txt.seek(0, 2)
1728
bundle_txt.write(b'\n\n')
1731
bundle = read_bundle(bundle_txt)
1732
self.check_valid(bundle)
1735
class MungedBundleTesterV09(tests.TestCaseWithTransport, MungedBundleTester):
1739
def test_missing_trailing_whitespace(self):
1740
bundle_txt = self.build_test_bundle()
1742
# Remove a trailing newline, it shouldn't kill the parser
1743
raw = bundle_txt.getvalue()
1744
# The contents of the bundle don't have to be this, but this
1745
# test is concerned with the exact case where the serializer
1746
# creates a blank line at the end, and fails if that
1748
self.assertEqual(b'\n\n', raw[-2:])
1749
bundle_txt = BytesIO(raw[:-1])
1751
bundle = read_bundle(bundle_txt)
1752
self.check_valid(bundle)
1754
def test_opening_text(self):
1755
bundle_txt = self.build_test_bundle()
1757
bundle_txt = BytesIO(
1758
b"Some random\nemail comments\n" + bundle_txt.getvalue())
1760
bundle = read_bundle(bundle_txt)
1761
self.check_valid(bundle)
1763
def test_trailing_text(self):
1764
bundle_txt = self.build_test_bundle()
1766
bundle_txt = BytesIO(
1767
bundle_txt.getvalue() + b"Some trailing\nrandom\ntext\n")
1769
bundle = read_bundle(bundle_txt)
1770
self.check_valid(bundle)
1773
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
1778
class TestBundleWriterReader(tests.TestCase):
1780
def test_roundtrip_record(self):
1782
writer = v4.BundleWriter(fileobj)
1784
writer.add_info_record({b'foo': b'bar'})
1785
writer._add_record(b"Record body", {b'parents': [b'1', b'3'],
1786
b'storage_kind': b'fulltext'}, 'file', b'revid', b'fileid')
1789
reader = v4.BundleReader(fileobj, stream_input=True)
1790
record_iter = reader.iter_records()
1791
record = next(record_iter)
1792
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1793
'info', None, None), record)
1794
record = next(record_iter)
1795
self.assertEqual((b"Record body", {b'storage_kind': b'fulltext',
1796
b'parents': [b'1', b'3']}, 'file', b'revid', b'fileid'),
1799
def test_roundtrip_record_memory_hungry(self):
1801
writer = v4.BundleWriter(fileobj)
1803
writer.add_info_record({b'foo': b'bar'})
1804
writer._add_record(b"Record body", {b'parents': [b'1', b'3'],
1805
b'storage_kind': b'fulltext'}, 'file', b'revid', b'fileid')
1808
reader = v4.BundleReader(fileobj, stream_input=False)
1809
record_iter = reader.iter_records()
1810
record = next(record_iter)
1811
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1812
'info', None, None), record)
1813
record = next(record_iter)
1814
self.assertEqual((b"Record body", {b'storage_kind': b'fulltext',
1815
b'parents': [b'1', b'3']}, 'file', b'revid', b'fileid'),
1818
def test_encode_name(self):
1819
self.assertEqual(b'revision/rev1',
1820
v4.BundleWriter.encode_name('revision', b'rev1'))
1821
self.assertEqual(b'file/rev//1/file-id-1',
1822
v4.BundleWriter.encode_name('file', b'rev/1', b'file-id-1'))
1823
self.assertEqual(b'info',
1824
v4.BundleWriter.encode_name('info', None, None))
1826
def test_decode_name(self):
1827
self.assertEqual(('revision', b'rev1', None),
1828
v4.BundleReader.decode_name(b'revision/rev1'))
1829
self.assertEqual(('file', b'rev/1', b'file-id-1'),
1830
v4.BundleReader.decode_name(b'file/rev//1/file-id-1'))
1831
self.assertEqual(('info', None, None),
1832
v4.BundleReader.decode_name(b'info'))
1834
def test_too_many_names(self):
1836
writer = v4.BundleWriter(fileobj)
1838
writer.add_info_record({b'foo': b'bar'})
1839
writer._container.add_bytes_record(b'blah', [(b'two', ), (b'names', )])
1842
record_iter = v4.BundleReader(fileobj).iter_records()
1843
record = next(record_iter)
1844
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1845
'info', None, None), record)
1846
self.assertRaises(errors.BadBundle, next, record_iter)
1849
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1851
def test_read_mergeable_skips_local(self):
1852
"""A local bundle named like the URL should not be read.
1854
out, wt = test_read_bundle.create_bundle_file(self)
1856
class FooService(object):
1857
"""A directory service that always returns source"""
1859
def look_up(self, name, url):
1861
directories.register('foo:', FooService, 'Testing directory service')
1862
self.addCleanup(directories.remove, 'foo:')
1863
self.build_tree_contents([('./foo:bar', out.getvalue())])
1864
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1867
def test_infinite_redirects_are_not_a_bundle(self):
1868
"""If a URL causes TooManyRedirections then NotABundle is raised.
1870
from .blackbox.test_push import RedirectingMemoryServer
1871
server = RedirectingMemoryServer()
1872
self.start_server(server)
1873
url = server.get_url() + 'infinite-loop'
1874
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1876
def test_smart_server_connection_reset(self):
1877
"""If a smart server connection fails during the attempt to read a
1878
bundle, then the ConnectionReset error should be propagated.
1880
# Instantiate a server that will provoke a ConnectionReset
1881
sock_server = DisconnectingServer()
1882
self.start_server(sock_server)
1883
# We don't really care what the url is since the server will close the
1884
# connection without interpreting it
1885
url = sock_server.get_url()
1886
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1889
class DisconnectingHandler(socketserver.BaseRequestHandler):
1890
"""A request handler that immediately closes any connection made to it."""
1893
self.request.close()
1896
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1899
super(DisconnectingServer, self).__init__(
1901
test_server.TestingTCPServer,
1902
DisconnectingHandler)
1905
"""Return the url of the server"""
1906
return "bzr://%s:%d/" % self.server.server_address