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
633
, 'b1/dir/filein subdir.c'
634
, 'b1/dir/WithCaps.txt'
635
, 'b1/dir/ pre space'
638
, 'b1/sub/sub/nonempty.txt'
640
self.build_tree_contents([('b1/sub/sub/emptyfile.txt', b''),
641
('b1/dir/nolastnewline.txt', b'bloop')])
642
tt = TreeTransform(self.tree1)
643
tt.new_file('executable', tt.root, [b'#!/bin/sh\n'], b'exe-1', True)
645
# have to fix length of file-id so that we can predictably rewrite
646
# a (length-prefixed) record containing it later.
647
self.tree1.add('with space.txt', b'withspace-id')
650
, 'dir/filein subdir.c'
653
, 'dir/nolastnewline.txt'
656
, 'sub/sub/nonempty.txt'
657
, 'sub/sub/emptyfile.txt'
659
self.tree1.commit('add whitespace', rev_id=b'a@cset-0-2')
661
bundle = self.get_valid_bundle(b'a@cset-0-1', b'a@cset-0-2')
663
# Check a rollup bundle
664
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-2')
668
['sub/sub/nonempty.txt'
669
, 'sub/sub/emptyfile.txt'
672
tt = TreeTransform(self.tree1)
673
trans_id = tt.trans_id_tree_path('executable')
674
tt.set_executability(False, trans_id)
676
self.tree1.commit('removed', rev_id=b'a@cset-0-3')
678
bundle = self.get_valid_bundle(b'a@cset-0-2', b'a@cset-0-3')
679
self.assertRaises((errors.TestamentMismatch,
680
errors.VersionedFileInvalidChecksum,
681
errors.BadBundle), self.get_invalid_bundle,
682
b'a@cset-0-2', b'a@cset-0-3')
683
# Check a rollup bundle
684
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-3')
686
# Now move the directory
687
self.tree1.rename_one('dir', 'sub/dir')
688
self.tree1.commit('rename dir', rev_id=b'a@cset-0-4')
690
bundle = self.get_valid_bundle(b'a@cset-0-3', b'a@cset-0-4')
691
# Check a rollup bundle
692
bundle = self.get_valid_bundle(b'null:', b'a@cset-0-4')
695
with open('b1/sub/dir/WithCaps.txt', 'ab') as f: f.write(b'\nAdding some text\n')
696
with open('b1/sub/dir/ pre space', 'ab') as f: f.write(
697
b'\r\nAdding some\r\nDOS format lines\r\n')
698
with open('b1/sub/dir/nolastnewline.txt', 'ab') as f: f.write(b'\n')
699
self.tree1.rename_one('sub/dir/ pre space',
701
self.tree1.commit('Modified files', rev_id=b'a@cset-0-5')
702
bundle = self.get_valid_bundle(b'a@cset-0-4', b'a@cset-0-5')
704
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
705
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
706
self.tree1.rename_one('temp', 'with space.txt')
707
self.tree1.commit(u'swap filenames', rev_id=b'a@cset-0-6',
709
bundle = self.get_valid_bundle(b'a@cset-0-5', b'a@cset-0-6')
710
other = self.get_checkout(b'a@cset-0-5')
711
tree1_inv = get_inventory_text(self.tree1.branch.repository,
713
tree2_inv = get_inventory_text(other.branch.repository,
715
self.assertEqualDiff(tree1_inv, tree2_inv)
716
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
717
other.commit('rename file', rev_id=b'a@cset-0-6b')
718
self.tree1.merge_from_branch(other.branch)
719
self.tree1.commit(u'Merge', rev_id=b'a@cset-0-7',
721
bundle = self.get_valid_bundle(b'a@cset-0-6', b'a@cset-0-7')
723
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
726
self.requireFeature(features.SymlinkFeature)
727
self.tree1 = self.make_branch_and_tree('b1')
728
self.b1 = self.tree1.branch
730
tt = TreeTransform(self.tree1)
731
tt.new_symlink(link_name, tt.root, link_target, link_id)
733
self.tree1.commit('add symlink', rev_id=b'l@cset-0-1')
734
bundle = self.get_valid_bundle(b'null:', b'l@cset-0-1')
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-1')
738
self.assertEqual(link_target, bund_tree.get_symlink_target(link_name))
740
tt = TreeTransform(self.tree1)
741
trans_id = tt.trans_id_tree_path(link_name)
742
tt.adjust_path('link2', tt.root, trans_id)
743
tt.delete_contents(trans_id)
744
tt.create_symlink(new_link_target, trans_id)
746
self.tree1.commit('rename and change symlink', rev_id=b'l@cset-0-2')
747
bundle = self.get_valid_bundle(b'l@cset-0-1', b'l@cset-0-2')
748
if getattr(bundle, 'revision_tree', None) is not None:
749
# Not all bundle formats supports revision_tree
750
bund_tree = bundle.revision_tree(self.b1.repository, b'l@cset-0-2')
751
self.assertEqual(new_link_target,
752
bund_tree.get_symlink_target('link2'))
754
tt = TreeTransform(self.tree1)
755
trans_id = tt.trans_id_tree_path('link2')
756
tt.delete_contents(trans_id)
757
tt.create_symlink('jupiter', trans_id)
759
self.tree1.commit('just change symlink target', rev_id=b'l@cset-0-3')
760
bundle = self.get_valid_bundle(b'l@cset-0-2', b'l@cset-0-3')
762
tt = TreeTransform(self.tree1)
763
trans_id = tt.trans_id_tree_path('link2')
764
tt.delete_contents(trans_id)
766
self.tree1.commit('Delete symlink', rev_id=b'l@cset-0-4')
767
bundle = self.get_valid_bundle(b'l@cset-0-3', b'l@cset-0-4')
769
def test_symlink_bundle(self):
770
self._test_symlink_bundle('link', 'bar/foo', 'mars')
772
def test_unicode_symlink_bundle(self):
773
self.requireFeature(features.UnicodeFilenameFeature)
774
self._test_symlink_bundle(u'\N{Euro Sign}link',
775
u'bar/\N{Euro Sign}foo',
776
u'mars\N{Euro Sign}')
778
def test_binary_bundle(self):
779
self.tree1 = self.make_branch_and_tree('b1')
780
self.b1 = self.tree1.branch
781
tt = TreeTransform(self.tree1)
784
tt.new_file('file', tt.root, [b'\x00\n\x00\r\x01\n\x02\r\xff'], b'binary-1')
785
tt.new_file('file2', tt.root, [b'\x01\n\x02\r\x03\n\x04\r\xff'],
788
self.tree1.commit('add binary', rev_id=b'b@cset-0-1')
789
self.get_valid_bundle(b'null:', b'b@cset-0-1')
792
tt = TreeTransform(self.tree1)
793
trans_id = tt.trans_id_tree_path('file')
794
tt.delete_contents(trans_id)
796
self.tree1.commit('delete binary', rev_id=b'b@cset-0-2')
797
self.get_valid_bundle(b'b@cset-0-1', b'b@cset-0-2')
800
tt = TreeTransform(self.tree1)
801
trans_id = tt.trans_id_tree_path('file2')
802
tt.adjust_path('file3', tt.root, trans_id)
803
tt.delete_contents(trans_id)
804
tt.create_file([b'file\rcontents\x00\n\x00'], trans_id)
806
self.tree1.commit('rename and modify binary', rev_id=b'b@cset-0-3')
807
self.get_valid_bundle(b'b@cset-0-2', b'b@cset-0-3')
810
tt = TreeTransform(self.tree1)
811
trans_id = tt.trans_id_tree_path('file3')
812
tt.delete_contents(trans_id)
813
tt.create_file([b'\x00file\rcontents'], trans_id)
815
self.tree1.commit('just modify binary', rev_id=b'b@cset-0-4')
816
self.get_valid_bundle(b'b@cset-0-3', b'b@cset-0-4')
819
self.get_valid_bundle(b'null:', b'b@cset-0-4')
821
def test_last_modified(self):
822
self.tree1 = self.make_branch_and_tree('b1')
823
self.b1 = self.tree1.branch
824
tt = TreeTransform(self.tree1)
825
tt.new_file('file', tt.root, [b'file'], b'file')
827
self.tree1.commit('create file', rev_id=b'a@lmod-0-1')
829
tt = TreeTransform(self.tree1)
830
trans_id = tt.trans_id_tree_path('file')
831
tt.delete_contents(trans_id)
832
tt.create_file([b'file2'], trans_id)
834
self.tree1.commit('modify text', rev_id=b'a@lmod-0-2a')
836
other = self.get_checkout(b'a@lmod-0-1')
837
tt = TreeTransform(other)
838
trans_id = tt.trans_id_tree_path('file2')
839
tt.delete_contents(trans_id)
840
tt.create_file([b'file2'], trans_id)
842
other.commit('modify text in another tree', rev_id=b'a@lmod-0-2b')
843
self.tree1.merge_from_branch(other.branch)
844
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-3',
846
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-4')
847
bundle = self.get_valid_bundle(b'a@lmod-0-2a', b'a@lmod-0-4')
849
def test_hide_history(self):
850
self.tree1 = self.make_branch_and_tree('b1')
851
self.b1 = self.tree1.branch
853
with open('b1/one', 'wb') as f: f.write(b'one\n')
854
self.tree1.add('one')
855
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
856
with open('b1/one', 'wb') as f: f.write(b'two\n')
857
self.tree1.commit('modify', rev_id=b'a@cset-0-2')
858
with open('b1/one', 'wb') as f: f.write(b'three\n')
859
self.tree1.commit('modify', rev_id=b'a@cset-0-3')
860
bundle_file = BytesIO()
861
rev_ids = write_bundle(self.tree1.branch.repository, b'a@cset-0-3',
862
b'a@cset-0-1', bundle_file, format=self.format)
863
self.assertNotContainsRe(bundle_file.getvalue(), b'\btwo\b')
864
self.assertContainsRe(self.get_raw(bundle_file), b'one')
865
self.assertContainsRe(self.get_raw(bundle_file), b'three')
867
def test_bundle_same_basis(self):
868
"""Ensure using the basis as the target doesn't cause an error"""
869
self.tree1 = self.make_branch_and_tree('b1')
870
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
871
bundle_file = BytesIO()
872
rev_ids = write_bundle(self.tree1.branch.repository, b'a@cset-0-1',
873
b'a@cset-0-1', bundle_file)
876
def get_raw(bundle_file):
877
return bundle_file.getvalue()
879
def test_unicode_bundle(self):
880
self.requireFeature(features.UnicodeFilenameFeature)
881
# Handle international characters
883
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
885
self.tree1 = self.make_branch_and_tree('b1')
886
self.b1 = self.tree1.branch
889
u'With international man of mystery\n'
890
u'William Dod\xe9\n').encode('utf-8'))
893
self.tree1.add([u'with Dod\N{Euro Sign}'], [b'withdod-id'])
894
self.tree1.commit(u'i18n commit from William Dod\xe9',
895
rev_id=b'i18n-1', committer=u'William Dod\xe9')
898
bundle = self.get_valid_bundle(b'null:', b'i18n-1')
901
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
902
f.write(u'Modified \xb5\n'.encode('utf8'))
904
self.tree1.commit(u'modified', rev_id=b'i18n-2')
906
bundle = self.get_valid_bundle(b'i18n-1', b'i18n-2')
909
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
910
self.tree1.commit(u'renamed, the new i18n man', rev_id=b'i18n-3',
911
committer=u'Erik B\xe5gfors')
913
bundle = self.get_valid_bundle(b'i18n-2', b'i18n-3')
916
self.tree1.remove([u'B\N{Euro Sign}gfors'])
917
self.tree1.commit(u'removed', rev_id=b'i18n-4')
919
bundle = self.get_valid_bundle(b'i18n-3', b'i18n-4')
922
bundle = self.get_valid_bundle(b'null:', b'i18n-4')
925
def test_whitespace_bundle(self):
926
if sys.platform in ('win32', 'cygwin'):
927
raise tests.TestSkipped('Windows doesn\'t support filenames'
928
' with tabs or trailing spaces')
929
self.tree1 = self.make_branch_and_tree('b1')
930
self.b1 = self.tree1.branch
932
self.build_tree(['b1/trailing space '])
933
self.tree1.add(['trailing space '])
934
# TODO: jam 20060701 Check for handling files with '\t' characters
935
# once we actually support them
938
self.tree1.commit('funky whitespace', rev_id=b'white-1')
940
bundle = self.get_valid_bundle(b'null:', b'white-1')
943
with open('b1/trailing space ', 'ab') as f: f.write(b'add some text\n')
944
self.tree1.commit('add text', rev_id=b'white-2')
946
bundle = self.get_valid_bundle(b'white-1', b'white-2')
949
self.tree1.rename_one('trailing space ', ' start and end space ')
950
self.tree1.commit('rename', rev_id=b'white-3')
952
bundle = self.get_valid_bundle(b'white-2', b'white-3')
955
self.tree1.remove([' start and end space '])
956
self.tree1.commit('removed', rev_id=b'white-4')
958
bundle = self.get_valid_bundle(b'white-3', b'white-4')
960
# Now test a complet roll-up
961
bundle = self.get_valid_bundle(b'null:', b'white-4')
963
def test_alt_timezone_bundle(self):
964
self.tree1 = self.make_branch_and_memory_tree('b1')
965
self.b1 = self.tree1.branch
966
builder = treebuilder.TreeBuilder()
968
self.tree1.lock_write()
969
builder.start_tree(self.tree1)
970
builder.build(['newfile'])
971
builder.finish_tree()
973
# Asia/Colombo offset = 5 hours 30 minutes
974
self.tree1.commit('non-hour offset timezone', rev_id=b'tz-1',
975
timezone=19800, timestamp=1152544886.0)
977
bundle = self.get_valid_bundle(b'null:', b'tz-1')
979
rev = bundle.revisions[0]
980
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
981
self.assertEqual(19800, rev.timezone)
982
self.assertEqual(1152544886.0, rev.timestamp)
985
def test_bundle_root_id(self):
986
self.tree1 = self.make_branch_and_tree('b1')
987
self.b1 = self.tree1.branch
988
self.tree1.commit('message', rev_id=b'revid1')
989
bundle = self.get_valid_bundle(b'null:', b'revid1')
990
tree = self.get_bundle_tree(bundle, b'revid1')
991
root_revision = tree.get_file_revision(u'', tree.get_root_id())
992
self.assertEqual(b'revid1', root_revision)
994
def test_install_revisions(self):
995
self.tree1 = self.make_branch_and_tree('b1')
996
self.b1 = self.tree1.branch
997
self.tree1.commit('message', rev_id=b'rev2a')
998
bundle = self.get_valid_bundle(b'null:', b'rev2a')
999
branch2 = self.make_branch('b2')
1000
self.assertFalse(branch2.repository.has_revision(b'rev2a'))
1001
target_revision = bundle.install_revisions(branch2.repository)
1002
self.assertTrue(branch2.repository.has_revision(b'rev2a'))
1003
self.assertEqual(b'rev2a', target_revision)
1005
def test_bundle_empty_property(self):
1006
"""Test serializing revision properties with an empty value."""
1007
tree = self.make_branch_and_memory_tree('tree')
1009
self.addCleanup(tree.unlock)
1010
tree.add([''], [b'TREE_ROOT'])
1011
tree.commit('One', revprops={u'one': 'two', u'empty': ''}, rev_id=b'rev1')
1012
self.b1 = tree.branch
1013
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1014
bundle = read_bundle(bundle_sio)
1015
revision_info = bundle.revisions[0]
1016
self.assertEqual(b'rev1', revision_info.revision_id)
1017
rev = revision_info.as_revision()
1018
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1021
def test_bundle_sorted_properties(self):
1022
"""For stability the writer should write properties in sorted order."""
1023
tree = self.make_branch_and_memory_tree('tree')
1025
self.addCleanup(tree.unlock)
1027
tree.add([''], [b'TREE_ROOT'])
1028
tree.commit('One', rev_id=b'rev1',
1029
revprops={u'a':'4', u'b':'3', u'c':'2', u'd':'1'})
1030
self.b1 = tree.branch
1031
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1032
bundle = read_bundle(bundle_sio)
1033
revision_info = bundle.revisions[0]
1034
self.assertEqual(b'rev1', revision_info.revision_id)
1035
rev = revision_info.as_revision()
1036
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1037
'd':'1'}, rev.properties)
1039
def test_bundle_unicode_properties(self):
1040
"""We should be able to round trip a non-ascii property."""
1041
tree = self.make_branch_and_memory_tree('tree')
1043
self.addCleanup(tree.unlock)
1045
tree.add([''], [b'TREE_ROOT'])
1046
# Revisions themselves do not require anything about revision property
1047
# keys, other than that they are a basestring, and do not contain
1049
# However, Testaments assert than they are str(), and thus should not
1051
tree.commit('One', rev_id=b'rev1',
1052
revprops={u'omega':u'\u03a9', u'alpha':u'\u03b1'})
1053
self.b1 = tree.branch
1054
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1055
bundle = read_bundle(bundle_sio)
1056
revision_info = bundle.revisions[0]
1057
self.assertEqual(b'rev1', revision_info.revision_id)
1058
rev = revision_info.as_revision()
1059
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1060
'alpha':u'\u03b1'}, rev.properties)
1062
def test_bundle_with_ghosts(self):
1063
tree = self.make_branch_and_tree('tree')
1064
self.b1 = tree.branch
1065
self.build_tree_contents([('tree/file', b'content1')])
1068
self.build_tree_contents([('tree/file', b'content2')])
1069
tree.add_parent_tree_id(b'ghost')
1070
tree.commit('rev2', rev_id=b'rev2')
1071
bundle = self.get_valid_bundle(b'null:', b'rev2')
1073
def make_simple_tree(self, format=None):
1074
tree = self.make_branch_and_tree('b1', format=format)
1075
self.b1 = tree.branch
1076
self.build_tree(['b1/file'])
1080
def test_across_serializers(self):
1081
tree = self.make_simple_tree('knit')
1082
tree.commit('hello', rev_id=b'rev1')
1083
tree.commit('hello', rev_id=b'rev2')
1084
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1085
repo = self.make_repository('repo', format='dirstate-with-subtree')
1086
bundle.install_revisions(repo)
1087
inv_text = repo._get_inventory_xml(b'rev2')
1088
self.assertNotContainsRe(inv_text, b'format="5"')
1089
self.assertContainsRe(inv_text, b'format="7"')
1091
def make_repo_with_installed_revisions(self):
1092
tree = self.make_simple_tree('knit')
1093
tree.commit('hello', rev_id=b'rev1')
1094
tree.commit('hello', rev_id=b'rev2')
1095
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1096
repo = self.make_repository('repo', format='dirstate-with-subtree')
1097
bundle.install_revisions(repo)
1100
def test_across_models(self):
1101
repo = self.make_repo_with_installed_revisions()
1102
inv = repo.get_inventory(b'rev2')
1103
self.assertEqual(b'rev2', inv.root.revision)
1104
root_id = inv.root.file_id
1106
self.addCleanup(repo.unlock)
1107
self.assertEqual({(root_id, b'rev1'):(),
1108
(root_id, b'rev2'):((root_id, b'rev1'),)},
1109
repo.texts.get_parent_map([(root_id, b'rev1'), (root_id, b'rev2')]))
1111
def test_inv_hash_across_serializers(self):
1112
repo = self.make_repo_with_installed_revisions()
1113
recorded_inv_sha1 = repo.get_revision(b'rev2').inventory_sha1
1114
xml = repo._get_inventory_xml(b'rev2')
1115
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1117
def test_across_models_incompatible(self):
1118
tree = self.make_simple_tree('dirstate-with-subtree')
1119
tree.commit('hello', rev_id=b'rev1')
1120
tree.commit('hello', rev_id=b'rev2')
1122
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1123
except errors.IncompatibleBundleFormat:
1124
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1125
repo = self.make_repository('repo', format='knit')
1126
bundle.install_revisions(repo)
1128
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev2')[0])
1129
self.assertRaises(errors.IncompatibleRevision,
1130
bundle.install_revisions, repo)
1132
def test_get_merge_request(self):
1133
tree = self.make_simple_tree()
1134
tree.commit('hello', rev_id=b'rev1')
1135
tree.commit('hello', rev_id=b'rev2')
1136
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1137
result = bundle.get_merge_request(tree.branch.repository)
1138
self.assertEqual((None, b'rev1', 'inapplicable'), result)
1140
def test_with_subtree(self):
1141
tree = self.make_branch_and_tree('tree',
1142
format='dirstate-with-subtree')
1143
self.b1 = tree.branch
1144
subtree = self.make_branch_and_tree('tree/subtree',
1145
format='dirstate-with-subtree')
1147
tree.commit('hello', rev_id=b'rev1')
1149
bundle = read_bundle(self.create_bundle_text(b'null:', b'rev1')[0])
1150
except errors.IncompatibleBundleFormat:
1151
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1152
if isinstance(bundle, v09.BundleInfo09):
1153
raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
1154
repo = self.make_repository('repo', format='knit')
1155
self.assertRaises(errors.IncompatibleRevision,
1156
bundle.install_revisions, repo)
1157
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1158
bundle.install_revisions(repo2)
1160
def test_revision_id_with_slash(self):
1161
self.tree1 = self.make_branch_and_tree('tree')
1162
self.b1 = self.tree1.branch
1164
self.tree1.commit('Revision/id/with/slashes', rev_id=b'rev/id')
1166
raise tests.TestSkipped(
1167
"Repository doesn't support revision ids with slashes")
1168
bundle = self.get_valid_bundle(b'null:', b'rev/id')
1170
def test_skip_file(self):
1171
"""Make sure we don't accidentally write to the wrong versionedfile"""
1172
self.tree1 = self.make_branch_and_tree('tree')
1173
self.b1 = self.tree1.branch
1174
# rev1 is not present in bundle, done by fetch
1175
self.build_tree_contents([('tree/file2', b'contents1')])
1176
self.tree1.add('file2', b'file2-id')
1177
self.tree1.commit('rev1', rev_id=b'reva')
1178
self.build_tree_contents([('tree/file3', b'contents2')])
1179
# rev2 is present in bundle, and done by fetch
1180
# having file1 in the bunle causes file1's versionedfile to be opened.
1181
self.tree1.add('file3', b'file3-id')
1182
rev2 = self.tree1.commit('rev2')
1183
# Updating file2 should not cause an attempt to add to file1's vf
1184
target = self.tree1.controldir.sprout('target').open_workingtree()
1185
self.build_tree_contents([('tree/file2', b'contents3')])
1186
self.tree1.commit('rev3', rev_id=b'rev3')
1187
bundle = self.get_valid_bundle(b'reva', b'rev3')
1188
if getattr(bundle, 'get_bundle_reader', None) is None:
1189
raise tests.TestSkipped('Bundle format cannot provide reader')
1191
(f, r) for b, m, k, r, f in bundle.get_bundle_reader().iter_records()
1193
self.assertEqual({(b'file2-id', b'rev3'), (b'file3-id', rev2)}, file_ids)
1194
bundle.install_revisions(target.branch.repository)
1197
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1201
def test_bundle_empty_property(self):
1202
"""Test serializing revision properties with an empty value."""
1203
tree = self.make_branch_and_memory_tree('tree')
1205
self.addCleanup(tree.unlock)
1206
tree.add([''], [b'TREE_ROOT'])
1207
tree.commit('One', revprops={u'one':'two', u'empty':''}, rev_id=b'rev1')
1208
self.b1 = tree.branch
1209
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1210
self.assertContainsRe(bundle_sio.getvalue(),
1212
b'# branch-nick: tree\n'
1216
bundle = read_bundle(bundle_sio)
1217
revision_info = bundle.revisions[0]
1218
self.assertEqual(b'rev1', revision_info.revision_id)
1219
rev = revision_info.as_revision()
1220
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1223
def get_bundle_tree(self, bundle, revision_id):
1224
repository = self.make_repository('repo')
1225
return bundle.revision_tree(repository, b'revid1')
1227
def test_bundle_empty_property_alt(self):
1228
"""Test serializing revision properties with an empty value.
1230
Older readers had a bug when reading an empty property.
1231
They assumed that all keys ended in ': \n'. However they would write an
1232
empty value as ':\n'. This tests make sure that all newer bzr versions
1233
can handle th second form.
1235
tree = self.make_branch_and_memory_tree('tree')
1237
self.addCleanup(tree.unlock)
1238
tree.add([''], [b'TREE_ROOT'])
1239
tree.commit('One', revprops={u'one':'two', u'empty':''}, rev_id=b'rev1')
1240
self.b1 = tree.branch
1241
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1242
txt = bundle_sio.getvalue()
1243
loc = txt.find(b'# empty: ') + len(b'# empty:')
1244
# Create a new bundle, which strips the trailing space after empty
1245
bundle_sio = BytesIO(txt[:loc] + txt[loc+1:])
1247
self.assertContainsRe(bundle_sio.getvalue(),
1249
b'# branch-nick: tree\n'
1253
bundle = read_bundle(bundle_sio)
1254
revision_info = bundle.revisions[0]
1255
self.assertEqual(b'rev1', revision_info.revision_id)
1256
rev = revision_info.as_revision()
1257
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1260
def test_bundle_sorted_properties(self):
1261
"""For stability the writer should write properties in sorted order."""
1262
tree = self.make_branch_and_memory_tree('tree')
1264
self.addCleanup(tree.unlock)
1266
tree.add([''], [b'TREE_ROOT'])
1267
tree.commit('One', rev_id=b'rev1',
1268
revprops={u'a':'4', u'b':'3', u'c':'2', u'd':'1'})
1269
self.b1 = tree.branch
1270
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1271
self.assertContainsRe(bundle_sio.getvalue(),
1275
b'# branch-nick: tree\n'
1279
bundle = read_bundle(bundle_sio)
1280
revision_info = bundle.revisions[0]
1281
self.assertEqual(b'rev1', revision_info.revision_id)
1282
rev = revision_info.as_revision()
1283
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1284
'd':'1'}, rev.properties)
1286
def test_bundle_unicode_properties(self):
1287
"""We should be able to round trip a non-ascii property."""
1288
tree = self.make_branch_and_memory_tree('tree')
1290
self.addCleanup(tree.unlock)
1292
tree.add([''], [b'TREE_ROOT'])
1293
# Revisions themselves do not require anything about revision property
1294
# keys, other than that they are a basestring, and do not contain
1296
# However, Testaments assert than they are str(), and thus should not
1298
tree.commit('One', rev_id=b'rev1',
1299
revprops={u'omega':u'\u03a9', u'alpha':u'\u03b1'})
1300
self.b1 = tree.branch
1301
bundle_sio, revision_ids = self.create_bundle_text(b'null:', b'rev1')
1302
self.assertContainsRe(bundle_sio.getvalue(),
1304
b'# alpha: \xce\xb1\n'
1305
b'# branch-nick: tree\n'
1306
b'# omega: \xce\xa9\n'
1308
bundle = read_bundle(bundle_sio)
1309
revision_info = bundle.revisions[0]
1310
self.assertEqual(b'rev1', revision_info.revision_id)
1311
rev = revision_info.as_revision()
1312
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1313
'alpha':u'\u03b1'}, rev.properties)
1316
class V09BundleKnit2Tester(V08BundleTester):
1320
def bzrdir_format(self):
1321
format = bzrdir.BzrDirMetaFormat1()
1322
format.repository_format = knitrepo.RepositoryFormatKnit3()
1326
class V09BundleKnit1Tester(V08BundleTester):
1330
def bzrdir_format(self):
1331
format = bzrdir.BzrDirMetaFormat1()
1332
format.repository_format = knitrepo.RepositoryFormatKnit1()
1336
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
1340
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1341
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1342
Make sure that the text generated is valid, and that it
1343
can be applied against the base, and generate the same information.
1345
:return: The in-memory bundle
1347
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1349
# This should also validate the generated bundle
1350
bundle = read_bundle(bundle_txt)
1351
repository = self.b1.repository
1352
for bundle_rev in bundle.real_revisions:
1353
# These really should have already been checked when we read the
1354
# bundle, since it computes the sha1 hash for the revision, which
1355
# only will match if everything is okay, but lets be explicit about
1357
branch_rev = repository.get_revision(bundle_rev.revision_id)
1358
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1359
'timestamp', 'timezone', 'message', 'committer',
1360
'parent_ids', 'properties'):
1361
self.assertEqual(getattr(branch_rev, a),
1362
getattr(bundle_rev, a))
1363
self.assertEqual(len(branch_rev.parent_ids),
1364
len(bundle_rev.parent_ids))
1365
self.assertEqual(set(rev_ids),
1366
{r.revision_id for r in bundle.real_revisions})
1367
self.valid_apply_bundle(base_rev_id, bundle,
1368
checkout_dir=checkout_dir)
1372
def get_invalid_bundle(self, base_rev_id, rev_id):
1373
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1374
Munge the text so that it's invalid.
1376
:return: The in-memory bundle
1378
from ..bundle import serializer
1379
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1380
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1381
new_text = new_text.replace(b'<file file_id="exe-1"',
1382
b'<file executable="y" file_id="exe-1"')
1383
new_text = new_text.replace(b'B260', b'B275')
1384
bundle_txt = BytesIO()
1385
bundle_txt.write(serializer._get_bundle_header('4'))
1386
bundle_txt.write(b'\n')
1387
bundle_txt.write(bz2.compress(new_text))
1389
bundle = read_bundle(bundle_txt)
1390
self.valid_apply_bundle(base_rev_id, bundle)
1393
def create_bundle_text(self, base_rev_id, rev_id):
1394
bundle_txt = BytesIO()
1395
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1396
bundle_txt, format=self.format)
1398
self.assertEqual(bundle_txt.readline(),
1399
b'# Bazaar revision bundle v%s\n' % self.format.encode('ascii'))
1400
self.assertEqual(bundle_txt.readline(), b'#\n')
1401
rev = self.b1.repository.get_revision(rev_id)
1403
return bundle_txt, rev_ids
1405
def get_bundle_tree(self, bundle, revision_id):
1406
repository = self.make_repository('repo')
1407
bundle.install_revisions(repository)
1408
return repository.revision_tree(revision_id)
1410
def test_creation(self):
1411
tree = self.make_branch_and_tree('tree')
1412
self.build_tree_contents([('tree/file', b'contents1\nstatic\n')])
1413
tree.add('file', b'fileid-2')
1414
tree.commit('added file', rev_id=b'rev1')
1415
self.build_tree_contents([('tree/file', b'contents2\nstatic\n')])
1416
tree.commit('changed file', rev_id=b'rev2')
1418
serializer = BundleSerializerV4('1.0')
1419
with tree.lock_read():
1420
serializer.write_bundle(tree.branch.repository, b'rev2', b'null:', s)
1422
tree2 = self.make_branch_and_tree('target')
1423
target_repo = tree2.branch.repository
1424
install_bundle(target_repo, serializer.read(s))
1425
target_repo.lock_read()
1426
self.addCleanup(target_repo.unlock)
1427
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1428
repo_texts = dict((i, b''.join(content)) for i, content
1429
in target_repo.iter_files_bytes(
1430
[(b'fileid-2', b'rev1', '1'),
1431
(b'fileid-2', b'rev2', '2')]))
1432
self.assertEqual({'1':b'contents1\nstatic\n',
1433
'2':b'contents2\nstatic\n'},
1435
rtree = target_repo.revision_tree(b'rev2')
1436
inventory_vf = target_repo.inventories
1437
# If the inventory store has a graph, it must match the revision graph.
1439
[inventory_vf.get_parent_map([(b'rev2',)])[(b'rev2',)]],
1440
[None, ((b'rev1',),)])
1441
self.assertEqual('changed file',
1442
target_repo.get_revision(b'rev2').message)
1445
def get_raw(bundle_file):
1447
line = bundle_file.readline()
1448
line = bundle_file.readline()
1449
lines = bundle_file.readlines()
1450
return bz2.decompress(b''.join(lines))
1452
def test_copy_signatures(self):
1453
tree_a = self.make_branch_and_tree('tree_a')
1455
import breezy.commit as commit
1456
oldstrategy = breezy.gpg.GPGStrategy
1457
branch = tree_a.branch
1458
repo_a = branch.repository
1459
tree_a.commit("base", allow_pointless=True, rev_id=b'A')
1460
self.assertFalse(branch.repository.has_signature_for_revision_id(b'A'))
1462
from ..testament import Testament
1463
# monkey patch gpg signing mechanism
1464
breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
1465
new_config = test_commit.MustSignConfig()
1466
commit.Commit(config_stack=new_config).commit(message="base",
1467
allow_pointless=True,
1469
working_tree=tree_a)
1471
return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
1472
self.assertTrue(repo_a.has_signature_for_revision_id(b'B'))
1474
breezy.gpg.GPGStrategy = oldstrategy
1475
tree_b = self.make_branch_and_tree('tree_b')
1476
repo_b = tree_b.branch.repository
1478
serializer = BundleSerializerV4('4')
1479
with tree_a.lock_read():
1480
serializer.write_bundle(tree_a.branch.repository, b'B', b'null:', s)
1482
install_bundle(repo_b, serializer.read(s))
1483
self.assertTrue(repo_b.has_signature_for_revision_id(b'B'))
1484
self.assertEqual(repo_b.get_signature_text(b'B'),
1485
repo_a.get_signature_text(b'B'))
1487
# ensure repeat installs are harmless
1488
install_bundle(repo_b, serializer.read(s))
1491
class V4_2aBundleTester(V4BundleTester):
1493
def bzrdir_format(self):
1496
def get_invalid_bundle(self, base_rev_id, rev_id):
1497
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1498
Munge the text so that it's invalid.
1500
:return: The in-memory bundle
1502
from ..bundle import serializer
1503
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1504
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1505
# We are going to be replacing some text to set the executable bit on a
1506
# file. Make sure the text replacement actually works correctly.
1507
self.assertContainsRe(new_text, b'(?m)B244\n\ni 1\n<inventory')
1508
new_text = new_text.replace(b'<file file_id="exe-1"',
1509
b'<file executable="y" file_id="exe-1"')
1510
new_text = new_text.replace(b'B244', b'B259')
1511
bundle_txt = BytesIO()
1512
bundle_txt.write(serializer._get_bundle_header('4'))
1513
bundle_txt.write(b'\n')
1514
bundle_txt.write(bz2.compress(new_text))
1516
bundle = read_bundle(bundle_txt)
1517
self.valid_apply_bundle(base_rev_id, bundle)
1520
def make_merged_branch(self):
1521
builder = self.make_branch_builder('source')
1522
builder.start_series()
1523
builder.build_snapshot(None, [
1524
('add', ('', b'root-id', 'directory', None)),
1525
('add', ('file', b'file-id', 'file', b'original content\n')),
1526
], revision_id=b'a@cset-0-1')
1527
builder.build_snapshot([b'a@cset-0-1'], [
1528
('modify', ('file', b'new-content\n')),
1529
], revision_id=b'a@cset-0-2a')
1530
builder.build_snapshot([b'a@cset-0-1'], [
1531
('add', ('other-file', b'file2-id', 'file', b'file2-content\n')),
1532
], revision_id=b'a@cset-0-2b')
1533
builder.build_snapshot([b'a@cset-0-2a', b'a@cset-0-2b'], [
1534
('add', ('other-file', b'file2-id', 'file', b'file2-content\n')),
1535
], revision_id=b'a@cset-0-3')
1536
builder.finish_series()
1537
self.b1 = builder.get_branch()
1539
self.addCleanup(self.b1.unlock)
1541
def make_bundle_just_inventories(self, base_revision_id,
1545
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1546
self.b1.repository, sio)
1547
writer.bundle.begin()
1548
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1553
def test_single_inventory_multiple_parents_as_xml(self):
1554
self.make_merged_branch()
1555
sio = self.make_bundle_just_inventories(b'a@cset-0-1', b'a@cset-0-3',
1557
reader = v4.BundleReader(sio, stream_input=False)
1558
records = list(reader.iter_records())
1559
self.assertEqual(1, len(records))
1560
(bytes, metadata, repo_kind, revision_id,
1561
file_id) = records[0]
1562
self.assertIs(None, file_id)
1563
self.assertEqual(b'a@cset-0-3', revision_id)
1564
self.assertEqual('inventory', repo_kind)
1565
self.assertEqual({b'parents': [b'a@cset-0-2a', b'a@cset-0-2b'],
1566
b'sha1': b'09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1567
b'storage_kind': b'mpdiff',
1569
# We should have an mpdiff that takes some lines from both parents.
1570
self.assertEqualDiff(
1572
b'<inventory format="10" revision_id="a@cset-0-3">\n'
1575
b'c 1 3 3 2\n', bytes)
1577
def test_single_inv_no_parents_as_xml(self):
1578
self.make_merged_branch()
1579
sio = self.make_bundle_just_inventories(b'null:', b'a@cset-0-1',
1581
reader = v4.BundleReader(sio, stream_input=False)
1582
records = list(reader.iter_records())
1583
self.assertEqual(1, len(records))
1584
(bytes, metadata, repo_kind, revision_id,
1585
file_id) = records[0]
1586
self.assertIs(None, file_id)
1587
self.assertEqual(b'a@cset-0-1', revision_id)
1588
self.assertEqual('inventory', repo_kind)
1589
self.assertEqual({b'parents': [],
1590
b'sha1': b'a13f42b142d544aac9b085c42595d304150e31a2',
1591
b'storage_kind': b'mpdiff',
1593
# We should have an mpdiff that takes some lines from both parents.
1594
self.assertEqualDiff(
1596
b'<inventory format="10" revision_id="a@cset-0-1">\n'
1597
b'<directory file_id="root-id" name=""'
1598
b' revision="a@cset-0-1" />\n'
1599
b'<file file_id="file-id" name="file" parent_id="root-id"'
1600
b' revision="a@cset-0-1"'
1601
b' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1602
b' text_size="17" />\n'
1606
def test_multiple_inventories_as_xml(self):
1607
self.make_merged_branch()
1608
sio = self.make_bundle_just_inventories(b'a@cset-0-1', b'a@cset-0-3',
1609
[b'a@cset-0-2a', b'a@cset-0-2b', b'a@cset-0-3'])
1610
reader = v4.BundleReader(sio, stream_input=False)
1611
records = list(reader.iter_records())
1612
self.assertEqual(3, len(records))
1613
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1614
self.assertEqual([b'a@cset-0-2a', b'a@cset-0-2b', b'a@cset-0-3'],
1616
metadata_2a = records[0][1]
1617
self.assertEqual({b'parents': [b'a@cset-0-1'],
1618
b'sha1': b'1e105886d62d510763e22885eec733b66f5f09bf',
1619
b'storage_kind': b'mpdiff',
1621
metadata_2b = records[1][1]
1622
self.assertEqual({b'parents': [b'a@cset-0-1'],
1623
b'sha1': b'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1624
b'storage_kind': b'mpdiff',
1626
metadata_3 = records[2][1]
1627
self.assertEqual({b'parents': [b'a@cset-0-2a', b'a@cset-0-2b'],
1628
b'sha1': b'09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1629
b'storage_kind': b'mpdiff',
1631
bytes_2a = records[0][0]
1632
self.assertEqualDiff(
1634
b'<inventory format="10" revision_id="a@cset-0-2a">\n'
1638
b'<file file_id="file-id" name="file" parent_id="root-id"'
1639
b' revision="a@cset-0-2a"'
1640
b' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1641
b' text_size="12" />\n'
1643
b'c 0 3 3 1\n', bytes_2a)
1644
bytes_2b = records[1][0]
1645
self.assertEqualDiff(
1647
b'<inventory format="10" revision_id="a@cset-0-2b">\n'
1651
b'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1652
b' revision="a@cset-0-2b"'
1653
b' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1654
b' text_size="14" />\n'
1656
b'c 0 3 4 1\n', bytes_2b)
1657
bytes_3 = records[2][0]
1658
self.assertEqualDiff(
1660
b'<inventory format="10" revision_id="a@cset-0-3">\n'
1663
b'c 1 3 3 2\n', bytes_3)
1665
def test_creating_bundle_preserves_chk_pages(self):
1666
self.make_merged_branch()
1667
target = self.b1.controldir.sprout('target',
1668
revision_id=b'a@cset-0-2a').open_branch()
1669
bundle_txt, rev_ids = self.create_bundle_text(b'a@cset-0-2a',
1671
self.assertEqual(set([b'a@cset-0-2b', b'a@cset-0-3']), set(rev_ids))
1672
bundle = read_bundle(bundle_txt)
1674
self.addCleanup(target.unlock)
1675
install_bundle(target.repository, bundle)
1676
inv1 = next(self.b1.repository.inventories.get_record_stream([
1677
(b'a@cset-0-3',)], 'unordered',
1678
True)).get_bytes_as('fulltext')
1679
inv2 = next(target.repository.inventories.get_record_stream([
1680
(b'a@cset-0-3',)], 'unordered',
1681
True)).get_bytes_as('fulltext')
1682
self.assertEqualDiff(inv1, inv2)
1685
class MungedBundleTester(object):
1687
def build_test_bundle(self):
1688
wt = self.make_branch_and_tree('b1')
1690
self.build_tree(['b1/one'])
1692
wt.commit('add one', rev_id=b'a@cset-0-1')
1693
self.build_tree(['b1/two'])
1695
wt.commit('add two', rev_id=b'a@cset-0-2',
1696
revprops={u'branch-nick':'test'})
1698
bundle_txt = BytesIO()
1699
rev_ids = write_bundle(wt.branch.repository, b'a@cset-0-2',
1700
b'a@cset-0-1', bundle_txt, self.format)
1701
self.assertEqual({b'a@cset-0-2'}, set(rev_ids))
1702
bundle_txt.seek(0, 0)
1705
def check_valid(self, bundle):
1706
"""Check that after whatever munging, the final object is valid."""
1707
self.assertEqual([b'a@cset-0-2'],
1708
[r.revision_id for r in bundle.real_revisions])
1710
def test_extra_whitespace(self):
1711
bundle_txt = self.build_test_bundle()
1713
# Seek to the end of the file
1714
# Adding one extra newline used to give us
1715
# TypeError: float() argument must be a string or a number
1716
bundle_txt.seek(0, 2)
1717
bundle_txt.write(b'\n')
1720
bundle = read_bundle(bundle_txt)
1721
self.check_valid(bundle)
1723
def test_extra_whitespace_2(self):
1724
bundle_txt = self.build_test_bundle()
1726
# Seek to the end of the file
1727
# Adding two extra newlines used to give us
1728
# MalformedPatches: The first line of all patches should be ...
1729
bundle_txt.seek(0, 2)
1730
bundle_txt.write(b'\n\n')
1733
bundle = read_bundle(bundle_txt)
1734
self.check_valid(bundle)
1737
class MungedBundleTesterV09(tests.TestCaseWithTransport, MungedBundleTester):
1741
def test_missing_trailing_whitespace(self):
1742
bundle_txt = self.build_test_bundle()
1744
# Remove a trailing newline, it shouldn't kill the parser
1745
raw = bundle_txt.getvalue()
1746
# The contents of the bundle don't have to be this, but this
1747
# test is concerned with the exact case where the serializer
1748
# creates a blank line at the end, and fails if that
1750
self.assertEqual(b'\n\n', raw[-2:])
1751
bundle_txt = BytesIO(raw[:-1])
1753
bundle = read_bundle(bundle_txt)
1754
self.check_valid(bundle)
1756
def test_opening_text(self):
1757
bundle_txt = self.build_test_bundle()
1759
bundle_txt = BytesIO(
1760
b"Some random\nemail comments\n" + bundle_txt.getvalue())
1762
bundle = read_bundle(bundle_txt)
1763
self.check_valid(bundle)
1765
def test_trailing_text(self):
1766
bundle_txt = self.build_test_bundle()
1768
bundle_txt = BytesIO(
1769
bundle_txt.getvalue() + b"Some trailing\nrandom\ntext\n")
1771
bundle = read_bundle(bundle_txt)
1772
self.check_valid(bundle)
1775
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
1780
class TestBundleWriterReader(tests.TestCase):
1782
def test_roundtrip_record(self):
1784
writer = v4.BundleWriter(fileobj)
1786
writer.add_info_record({b'foo': b'bar'})
1787
writer._add_record(b"Record body", {b'parents': [b'1', b'3'],
1788
b'storage_kind': b'fulltext'}, 'file', b'revid', b'fileid')
1791
reader = v4.BundleReader(fileobj, stream_input=True)
1792
record_iter = reader.iter_records()
1793
record = next(record_iter)
1794
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1795
'info', None, None), record)
1796
record = next(record_iter)
1797
self.assertEqual((b"Record body", {b'storage_kind': b'fulltext',
1798
b'parents': [b'1', b'3']}, 'file', b'revid', b'fileid'),
1801
def test_roundtrip_record_memory_hungry(self):
1803
writer = v4.BundleWriter(fileobj)
1805
writer.add_info_record({b'foo': b'bar'})
1806
writer._add_record(b"Record body", {b'parents': [b'1', b'3'],
1807
b'storage_kind': b'fulltext'}, 'file', b'revid', b'fileid')
1810
reader = v4.BundleReader(fileobj, stream_input=False)
1811
record_iter = reader.iter_records()
1812
record = next(record_iter)
1813
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1814
'info', None, None), record)
1815
record = next(record_iter)
1816
self.assertEqual((b"Record body", {b'storage_kind': b'fulltext',
1817
b'parents': [b'1', b'3']}, 'file', b'revid', b'fileid'),
1820
def test_encode_name(self):
1821
self.assertEqual(b'revision/rev1',
1822
v4.BundleWriter.encode_name('revision', b'rev1'))
1823
self.assertEqual(b'file/rev//1/file-id-1',
1824
v4.BundleWriter.encode_name('file', b'rev/1', b'file-id-1'))
1825
self.assertEqual(b'info',
1826
v4.BundleWriter.encode_name('info', None, None))
1828
def test_decode_name(self):
1829
self.assertEqual(('revision', b'rev1', None),
1830
v4.BundleReader.decode_name(b'revision/rev1'))
1831
self.assertEqual(('file', b'rev/1', b'file-id-1'),
1832
v4.BundleReader.decode_name(b'file/rev//1/file-id-1'))
1833
self.assertEqual(('info', None, None),
1834
v4.BundleReader.decode_name(b'info'))
1836
def test_too_many_names(self):
1838
writer = v4.BundleWriter(fileobj)
1840
writer.add_info_record({b'foo': b'bar'})
1841
writer._container.add_bytes_record(b'blah', [(b'two', ), (b'names', )])
1844
record_iter = v4.BundleReader(fileobj).iter_records()
1845
record = next(record_iter)
1846
self.assertEqual((None, {b'foo': b'bar', b'storage_kind': b'header'},
1847
'info', None, None), record)
1848
self.assertRaises(errors.BadBundle, next, record_iter)
1851
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1853
def test_read_mergeable_skips_local(self):
1854
"""A local bundle named like the URL should not be read.
1856
out, wt = test_read_bundle.create_bundle_file(self)
1857
class FooService(object):
1858
"""A directory service that always returns source"""
1860
def look_up(self, name, url):
1862
directories.register('foo:', FooService, 'Testing directory service')
1863
self.addCleanup(directories.remove, 'foo:')
1864
self.build_tree_contents([('./foo:bar', out.getvalue())])
1865
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1868
def test_infinite_redirects_are_not_a_bundle(self):
1869
"""If a URL causes TooManyRedirections then NotABundle is raised.
1871
from .blackbox.test_push import RedirectingMemoryServer
1872
server = RedirectingMemoryServer()
1873
self.start_server(server)
1874
url = server.get_url() + 'infinite-loop'
1875
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1877
def test_smart_server_connection_reset(self):
1878
"""If a smart server connection fails during the attempt to read a
1879
bundle, then the ConnectionReset error should be propagated.
1881
# Instantiate a server that will provoke a ConnectionReset
1882
sock_server = DisconnectingServer()
1883
self.start_server(sock_server)
1884
# We don't really care what the url is since the server will close the
1885
# connection without interpreting it
1886
url = sock_server.get_url()
1887
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1890
class DisconnectingHandler(socketserver.BaseRequestHandler):
1891
"""A request handler that immediately closes any connection made to it."""
1894
self.request.close()
1897
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1900
super(DisconnectingServer, self).__init__(
1902
test_server.TestingTCPServer,
1903
DisconnectingHandler)
1906
"""Return the url of the server"""
1907
return "bzr://%s:%d/" % self.server.server_address