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
21
import SocketServer as socketserver
29
revision as _mod_revision,
37
from ..bundle import read_mergeable_from_url
38
from ..bundle.apply_bundle import install_bundle, merge_bundle
39
from ..bundle.bundle_data import BundleTree
40
from ..directory_service import directories
41
from ..bundle.serializer import write_bundle, read_bundle, v09, v4
42
from ..bundle.serializer.v08 import BundleSerializerV08
43
from ..bundle.serializer.v09 import BundleSerializerV09
44
from ..bundle.serializer.v4 import BundleSerializerV4
45
from ..bzr import knitrepo
46
from ..sixish import (
55
from ..transform import TreeTransform
58
def get_text(vf, key):
59
"""Get the fulltext for a given revision id that is present in the vf"""
60
stream = vf.get_record_stream([key], 'unordered', True)
62
return record.get_bytes_as('fulltext')
65
def get_inventory_text(repo, revision_id):
66
"""Get the fulltext for the inventory at revision id"""
69
return get_text(repo.inventories, (revision_id,))
74
class MockTree(object):
77
from ..bzr.inventory import InventoryDirectory, ROOT_ID
79
self.paths = {ROOT_ID: ""}
80
self.ids = {"": ROOT_ID}
82
self.root = InventoryDirectory(ROOT_ID, '', None)
84
inventory = property(lambda x:x)
85
root_inventory = property(lambda x:x)
87
def get_root_id(self):
88
return self.root.file_id
90
def all_file_ids(self):
91
return set(self.paths.keys())
93
def all_versioned_paths(self):
94
return set(self.paths.values())
96
def is_executable(self, path, file_id):
97
# Not all the files are executable.
100
def __getitem__(self, file_id):
101
if file_id == self.root.file_id:
104
return self.make_entry(file_id, self.paths[file_id])
106
def parent_id(self, file_id):
107
parent_dir = os.path.dirname(self.paths[file_id])
110
return self.ids[parent_dir]
112
def iter_entries(self):
113
for path, file_id in self.ids.items():
114
yield path, self[file_id]
116
def kind(self, path, file_id=None):
118
file_id = self.path2id(path)
119
if file_id in self.contents:
125
def make_entry(self, file_id, path):
126
from ..bzr.inventory import (InventoryFile, InventoryDirectory,
128
name = os.path.basename(path)
129
kind = self.kind(path, file_id)
130
parent_id = self.parent_id(file_id)
131
text_sha_1, text_size = self.contents_stats(path, file_id)
132
if kind == 'directory':
133
ie = InventoryDirectory(file_id, name, parent_id)
135
ie = InventoryFile(file_id, name, parent_id)
136
ie.text_sha1 = text_sha_1
137
ie.text_size = text_size
138
elif kind == 'symlink':
139
ie = InventoryLink(file_id, name, parent_id)
141
raise errors.BzrError('unknown kind %r' % kind)
144
def add_dir(self, file_id, path):
145
self.paths[file_id] = path
146
self.ids[path] = file_id
148
def add_file(self, file_id, path, contents):
149
self.add_dir(file_id, path)
150
self.contents[file_id] = contents
152
def path2id(self, path):
153
return self.ids.get(path)
155
def id2path(self, file_id):
156
return self.paths.get(file_id)
158
def has_id(self, file_id):
159
return self.id2path(file_id) is not None
161
def get_file(self, path, file_id=None):
163
file_id = self.path2id(path)
166
result.write(self.contents[file_id])
168
raise errors.NoSuchFile(path)
172
def get_file_revision(self, path, file_id=None):
174
file_id = self.path2id(path)
175
return self.inventory[file_id].revision
177
def get_file_size(self, path, file_id=None):
179
file_id = self.path2id(path)
180
return self.inventory[file_id].text_size
182
def get_file_sha1(self, path, file_id=None):
184
file_id = self.path2id(path)
185
return self.inventory[file_id].text_sha1
187
def contents_stats(self, path, file_id):
188
if file_id not in self.contents:
190
text_sha1 = osutils.sha_file(self.get_file(path, file_id))
191
return text_sha1, len(self.contents[file_id])
194
class BTreeTester(tests.TestCase):
195
"""A simple unittest tester for the BundleTree class."""
197
def make_tree_1(self):
199
mtree.add_dir(b"a", "grandparent")
200
mtree.add_dir(b"b", "grandparent/parent")
201
mtree.add_file(b"c", "grandparent/parent/file", "Hello\n")
202
mtree.add_dir(b"d", "grandparent/alt_parent")
203
return BundleTree(mtree, ''), mtree
205
def test_renames(self):
206
"""Ensure that file renames have the proper effect on children"""
207
btree = self.make_tree_1()[0]
208
self.assertEqual(btree.old_path("grandparent"), "grandparent")
209
self.assertEqual(btree.old_path("grandparent/parent"),
210
"grandparent/parent")
211
self.assertEqual(btree.old_path("grandparent/parent/file"),
212
"grandparent/parent/file")
214
self.assertEqual(btree.id2path(b"a"), "grandparent")
215
self.assertEqual(btree.id2path(b"b"), "grandparent/parent")
216
self.assertEqual(btree.id2path(b"c"), "grandparent/parent/file")
218
self.assertEqual(btree.path2id("grandparent"), b"a")
219
self.assertEqual(btree.path2id("grandparent/parent"), b"b")
220
self.assertEqual(btree.path2id("grandparent/parent/file"), b"c")
222
self.assertIs(btree.path2id("grandparent2"), None)
223
self.assertIs(btree.path2id("grandparent2/parent"), None)
224
self.assertIs(btree.path2id("grandparent2/parent/file"), None)
226
btree.note_rename("grandparent", "grandparent2")
227
self.assertIs(btree.old_path("grandparent"), None)
228
self.assertIs(btree.old_path("grandparent/parent"), None)
229
self.assertIs(btree.old_path("grandparent/parent/file"), None)
231
self.assertEqual(btree.id2path(b"a"), "grandparent2")
232
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent")
233
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent/file")
235
self.assertEqual(btree.path2id("grandparent2"), b"a")
236
self.assertEqual(btree.path2id("grandparent2/parent"), b"b")
237
self.assertEqual(btree.path2id("grandparent2/parent/file"), b"c")
239
self.assertTrue(btree.path2id("grandparent") is None)
240
self.assertTrue(btree.path2id("grandparent/parent") is None)
241
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
243
btree.note_rename("grandparent/parent", "grandparent2/parent2")
244
self.assertEqual(btree.id2path(b"a"), "grandparent2")
245
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent2")
246
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent2/file")
248
self.assertEqual(btree.path2id("grandparent2"), b"a")
249
self.assertEqual(btree.path2id("grandparent2/parent2"), b"b")
250
self.assertEqual(btree.path2id("grandparent2/parent2/file"), b"c")
252
self.assertTrue(btree.path2id("grandparent2/parent") is None)
253
self.assertTrue(btree.path2id("grandparent2/parent/file") is None)
255
btree.note_rename("grandparent/parent/file",
256
"grandparent2/parent2/file2")
257
self.assertEqual(btree.id2path(b"a"), "grandparent2")
258
self.assertEqual(btree.id2path(b"b"), "grandparent2/parent2")
259
self.assertEqual(btree.id2path(b"c"), "grandparent2/parent2/file2")
261
self.assertEqual(btree.path2id("grandparent2"), b"a")
262
self.assertEqual(btree.path2id("grandparent2/parent2"), b"b")
263
self.assertEqual(btree.path2id("grandparent2/parent2/file2"), b"c")
265
self.assertTrue(btree.path2id("grandparent2/parent2/file") is None)
267
def test_moves(self):
268
"""Ensure that file moves have the proper effect on children"""
269
btree = self.make_tree_1()[0]
270
btree.note_rename("grandparent/parent/file",
271
"grandparent/alt_parent/file")
272
self.assertEqual(btree.id2path(b"c"), "grandparent/alt_parent/file")
273
self.assertEqual(btree.path2id("grandparent/alt_parent/file"), b"c")
274
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
276
def unified_diff(self, old, new):
278
diff.internal_diff("old", old, "new", new, out)
282
def make_tree_2(self):
283
btree = self.make_tree_1()[0]
284
btree.note_rename("grandparent/parent/file",
285
"grandparent/alt_parent/file")
286
self.assertTrue(btree.id2path(b"e") is None)
287
self.assertTrue(btree.path2id("grandparent/parent/file") is None)
288
btree.note_id("e", "grandparent/parent/file")
292
"""File/inventory adds"""
293
btree = self.make_tree_2()
294
add_patch = self.unified_diff([], ["Extra cheese\n"])
295
btree.note_patch("grandparent/parent/file", add_patch)
296
btree.note_id(b'f', 'grandparent/parent/symlink', kind='symlink')
297
btree.note_target('grandparent/parent/symlink', 'venus')
298
self.adds_test(btree)
300
def adds_test(self, btree):
301
self.assertEqual(btree.id2path(b"e"), "grandparent/parent/file")
302
self.assertEqual(btree.path2id("grandparent/parent/file"), b"e")
303
self.assertEqual(btree.get_file("grandparent/parent/file").read(),
306
btree.get_symlink_target('grandparent/parent/symlink'), 'venus')
308
def test_adds2(self):
309
"""File/inventory adds, with patch-compatibile renames"""
310
btree = self.make_tree_2()
311
btree.contents_by_id = False
312
add_patch = self.unified_diff(["Hello\n"], ["Extra cheese\n"])
313
btree.note_patch("grandparent/parent/file", add_patch)
314
btree.note_id('f', 'grandparent/parent/symlink', kind='symlink')
315
btree.note_target('grandparent/parent/symlink', 'venus')
316
self.adds_test(btree)
318
def make_tree_3(self):
319
btree, mtree = self.make_tree_1()
320
mtree.add_file("e", "grandparent/parent/topping", "Anchovies\n")
321
btree.note_rename("grandparent/parent/file",
322
"grandparent/alt_parent/file")
323
btree.note_rename("grandparent/parent/topping",
324
"grandparent/alt_parent/stopping")
327
def get_file_test(self, btree):
328
self.assertEqual(btree.get_file(btree.id2path(b"e")).read(), "Lemon\n")
329
self.assertEqual(btree.get_file(btree.id2path(b"c")).read(), "Hello\n")
331
def test_get_file(self):
332
"""Get file contents"""
333
btree = self.make_tree_3()
334
mod_patch = self.unified_diff(["Anchovies\n"], ["Lemon\n"])
335
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
336
self.get_file_test(btree)
338
def test_get_file2(self):
339
"""Get file contents, with patch-compatible renames"""
340
btree = self.make_tree_3()
341
btree.contents_by_id = False
342
mod_patch = self.unified_diff([], ["Lemon\n"])
343
btree.note_patch("grandparent/alt_parent/stopping", mod_patch)
344
mod_patch = self.unified_diff([], ["Hello\n"])
345
btree.note_patch("grandparent/alt_parent/file", mod_patch)
346
self.get_file_test(btree)
348
def test_delete(self):
350
btree = self.make_tree_1()[0]
351
self.assertEqual(btree.get_file(btree.id2path(b"c")).read(), "Hello\n")
352
btree.note_deletion("grandparent/parent/file")
353
self.assertTrue(btree.id2path(b"c") is None)
354
self.assertFalse(btree.is_versioned("grandparent/parent/file"))
356
def sorted_ids(self, tree):
357
ids = sorted(tree.all_file_ids())
360
def test_iteration(self):
361
"""Ensure that iteration through ids works properly"""
362
btree = self.make_tree_1()[0]
363
self.assertEqual(self.sorted_ids(btree),
364
[inventory.ROOT_ID, 'a', 'b', 'c', 'd'])
365
btree.note_deletion("grandparent/parent/file")
366
btree.note_id("e", "grandparent/alt_parent/fool", kind="directory")
367
btree.note_last_changed("grandparent/alt_parent/fool",
369
self.assertEqual(self.sorted_ids(btree),
370
[inventory.ROOT_ID, 'a', 'b', 'd', 'e'])
373
class BundleTester1(tests.TestCaseWithTransport):
375
def test_mismatched_bundle(self):
376
format = bzrdir.BzrDirMetaFormat1()
377
format.repository_format = knitrepo.RepositoryFormatKnit3()
378
serializer = BundleSerializerV08('0.8')
379
b = self.make_branch('.', format=format)
380
self.assertRaises(errors.IncompatibleBundleFormat, serializer.write,
381
b.repository, [], {}, BytesIO())
383
def test_matched_bundle(self):
384
"""Don't raise IncompatibleBundleFormat for knit2 and bundle0.9"""
385
format = bzrdir.BzrDirMetaFormat1()
386
format.repository_format = knitrepo.RepositoryFormatKnit3()
387
serializer = BundleSerializerV09('0.9')
388
b = self.make_branch('.', format=format)
389
serializer.write(b.repository, [], {}, BytesIO())
391
def test_mismatched_model(self):
392
"""Try copying a bundle from knit2 to knit1"""
393
format = bzrdir.BzrDirMetaFormat1()
394
format.repository_format = knitrepo.RepositoryFormatKnit3()
395
source = self.make_branch_and_tree('source', format=format)
396
source.commit('one', rev_id=b'one-id')
397
source.commit('two', rev_id=b'two-id')
399
write_bundle(source.branch.repository, 'two-id', 'null:', text,
403
format = bzrdir.BzrDirMetaFormat1()
404
format.repository_format = knitrepo.RepositoryFormatKnit1()
405
target = self.make_branch('target', format=format)
406
self.assertRaises(errors.IncompatibleRevision, install_bundle,
407
target.repository, read_bundle(text))
410
class BundleTester(object):
412
def bzrdir_format(self):
413
format = bzrdir.BzrDirMetaFormat1()
414
format.repository_format = knitrepo.RepositoryFormatKnit1()
417
def make_branch_and_tree(self, path, format=None):
419
format = self.bzrdir_format()
420
return tests.TestCaseWithTransport.make_branch_and_tree(
423
def make_branch(self, path, format=None):
425
format = self.bzrdir_format()
426
return tests.TestCaseWithTransport.make_branch(self, path, format)
428
def create_bundle_text(self, base_rev_id, rev_id):
429
bundle_txt = BytesIO()
430
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
431
bundle_txt, format=self.format)
433
self.assertEqual(bundle_txt.readline(),
434
'# Bazaar revision bundle v%s\n' % self.format)
435
self.assertEqual(bundle_txt.readline(), '#\n')
437
rev = self.b1.repository.get_revision(rev_id)
438
self.assertEqual(bundle_txt.readline().decode('utf-8'),
441
return bundle_txt, rev_ids
443
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
444
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
445
Make sure that the text generated is valid, and that it
446
can be applied against the base, and generate the same information.
448
:return: The in-memory bundle
450
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
452
# This should also validate the generated bundle
453
bundle = read_bundle(bundle_txt)
454
repository = self.b1.repository
455
for bundle_rev in bundle.real_revisions:
456
# These really should have already been checked when we read the
457
# bundle, since it computes the sha1 hash for the revision, which
458
# only will match if everything is okay, but lets be explicit about
460
branch_rev = repository.get_revision(bundle_rev.revision_id)
461
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
462
'timestamp', 'timezone', 'message', 'committer',
463
'parent_ids', 'properties'):
464
self.assertEqual(getattr(branch_rev, a),
465
getattr(bundle_rev, a))
466
self.assertEqual(len(branch_rev.parent_ids),
467
len(bundle_rev.parent_ids))
468
self.assertEqual(rev_ids,
469
[r.revision_id for r in bundle.real_revisions])
470
self.valid_apply_bundle(base_rev_id, bundle,
471
checkout_dir=checkout_dir)
475
def get_invalid_bundle(self, base_rev_id, rev_id):
476
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
477
Munge the text so that it's invalid.
479
:return: The in-memory bundle
481
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
482
new_text = bundle_txt.getvalue().replace('executable:no',
484
bundle_txt = BytesIO(new_text)
485
bundle = read_bundle(bundle_txt)
486
self.valid_apply_bundle(base_rev_id, bundle)
489
def test_non_bundle(self):
490
self.assertRaises(errors.NotABundle,
491
read_bundle, BytesIO(b'#!/bin/sh\n'))
493
def test_malformed(self):
494
self.assertRaises(errors.BadBundle, read_bundle,
495
BytesIO(b'# Bazaar revision bundle v'))
497
def test_crlf_bundle(self):
499
read_bundle(BytesIO(b'# Bazaar revision bundle v0.8\r\n'))
500
except errors.BadBundle:
501
# It is currently permitted for bundles with crlf line endings to
502
# make read_bundle raise a BadBundle, but this should be fixed.
503
# Anything else, especially NotABundle, is an error.
506
def get_checkout(self, rev_id, checkout_dir=None):
507
"""Get a new tree, with the specified revision in it.
510
if checkout_dir is None:
511
checkout_dir = osutils.mkdtemp(prefix='test-branch-', dir='.')
513
if not os.path.exists(checkout_dir):
514
os.mkdir(checkout_dir)
515
tree = self.make_branch_and_tree(checkout_dir)
517
ancestors = write_bundle(self.b1.repository, rev_id, 'null:', s,
520
self.assertIsInstance(s.getvalue(), str)
521
install_bundle(tree.branch.repository, read_bundle(s))
522
for ancestor in ancestors:
523
old = self.b1.repository.revision_tree(ancestor)
524
new = tree.branch.repository.revision_tree(ancestor)
528
# Check that there aren't any inventory level changes
529
delta = new.changes_from(old)
530
self.assertFalse(delta.has_changed(),
531
'Revision %s not copied correctly.'
534
# Now check that the file contents are all correct
535
for path in old.all_versioned_paths():
537
old_file = old.get_file(path)
538
except errors.NoSuchFile:
541
old_file.read(), new.get_file(path).read())
545
if not _mod_revision.is_null(rev_id):
546
tree.branch.generate_revision_history(rev_id)
548
delta = tree.changes_from(self.b1.repository.revision_tree(rev_id))
549
self.assertFalse(delta.has_changed(),
550
'Working tree has modifications: %s' % delta)
553
def valid_apply_bundle(self, base_rev_id, info, checkout_dir=None):
554
"""Get the base revision, apply the changes, and make
555
sure everything matches the builtin branch.
557
to_tree = self.get_checkout(base_rev_id, checkout_dir=checkout_dir)
560
self._valid_apply_bundle(base_rev_id, info, to_tree)
564
def _valid_apply_bundle(self, base_rev_id, info, to_tree):
565
original_parents = to_tree.get_parent_ids()
566
repository = to_tree.branch.repository
567
original_parents = to_tree.get_parent_ids()
568
self.assertIs(repository.has_revision(base_rev_id), True)
569
for rev in info.real_revisions:
570
self.assertTrue(not repository.has_revision(rev.revision_id),
571
'Revision {%s} present before applying bundle'
573
merge_bundle(info, to_tree, True, merge.Merge3Merger, False, False)
575
for rev in info.real_revisions:
576
self.assertTrue(repository.has_revision(rev.revision_id),
577
'Missing revision {%s} after applying bundle'
580
self.assertTrue(to_tree.branch.repository.has_revision(info.target))
581
# Do we also want to verify that all the texts have been added?
583
self.assertEqual(original_parents + [info.target],
584
to_tree.get_parent_ids())
586
rev = info.real_revisions[-1]
587
base_tree = self.b1.repository.revision_tree(rev.revision_id)
588
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
590
# TODO: make sure the target tree is identical to base tree
591
# we might also check the working tree.
593
base_files = list(base_tree.list_files())
594
to_files = list(to_tree.list_files())
595
self.assertEqual(len(base_files), len(to_files))
596
for base_file, to_file in zip(base_files, to_files):
597
self.assertEqual(base_file, to_file)
599
for path, status, kind, fileid, entry in base_files:
600
# Check that the meta information is the same
601
self.assertEqual(base_tree.get_file_size(path, fileid),
602
to_tree.get_file_size(to_tree.id2path(fileid)))
603
self.assertEqual(base_tree.get_file_sha1(path, fileid),
604
to_tree.get_file_sha1(to_tree.id2path(fileid)))
605
# Check that the contents are the same
606
# This is pretty expensive
607
# self.assertEqual(base_tree.get_file(fileid).read(),
608
# to_tree.get_file(fileid).read())
610
def test_bundle(self):
611
self.tree1 = self.make_branch_and_tree('b1')
612
self.b1 = self.tree1.branch
614
self.build_tree_contents([('b1/one', b'one\n')])
615
self.tree1.add('one', b'one-id')
616
self.tree1.set_root_id(b'root-id')
617
self.tree1.commit('add one', rev_id=b'a@cset-0-1')
619
bundle = self.get_valid_bundle('null:', b'a@cset-0-1')
621
# Make sure we can handle files with spaces, tabs, other
626
, 'b1/dir/filein subdir.c'
627
, 'b1/dir/WithCaps.txt'
628
, 'b1/dir/ pre space'
631
, '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, '#!/bin/sh\n', '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')
643
, 'dir/filein subdir.c'
646
, 'dir/nolastnewline.txt'
649
, 'sub/sub/nonempty.txt'
650
, 'sub/sub/emptyfile.txt'
652
self.tree1.commit('add whitespace', rev_id=b'a@cset-0-2')
654
bundle = self.get_valid_bundle('a@cset-0-1', b'a@cset-0-2')
656
# Check a rollup bundle
657
bundle = self.get_valid_bundle('null:', 'a@cset-0-2')
661
['sub/sub/nonempty.txt'
662
, 'sub/sub/emptyfile.txt'
665
tt = TreeTransform(self.tree1)
666
trans_id = tt.trans_id_tree_path('executable')
667
tt.set_executability(False, trans_id)
669
self.tree1.commit('removed', rev_id=b'a@cset-0-3')
671
bundle = self.get_valid_bundle('a@cset-0-2', 'a@cset-0-3')
672
self.assertRaises((errors.TestamentMismatch,
673
errors.VersionedFileInvalidChecksum,
674
errors.BadBundle), self.get_invalid_bundle,
675
'a@cset-0-2', 'a@cset-0-3')
676
# Check a rollup bundle
677
bundle = self.get_valid_bundle('null:', 'a@cset-0-3')
679
# Now move the directory
680
self.tree1.rename_one('dir', 'sub/dir')
681
self.tree1.commit('rename dir', rev_id=b'a@cset-0-4')
683
bundle = self.get_valid_bundle('a@cset-0-3', 'a@cset-0-4')
684
# Check a rollup bundle
685
bundle = self.get_valid_bundle('null:', 'a@cset-0-4')
688
with open('b1/sub/dir/WithCaps.txt', 'ab') as f: f.write('\nAdding some text\n')
689
with open('b1/sub/dir/ pre space', 'ab') as f: f.write(
690
'\r\nAdding some\r\nDOS format lines\r\n')
691
with open('b1/sub/dir/nolastnewline.txt', 'ab') as f: f.write('\n')
692
self.tree1.rename_one('sub/dir/ pre space',
694
self.tree1.commit('Modified files', rev_id=b'a@cset-0-5')
695
bundle = self.get_valid_bundle('a@cset-0-4', 'a@cset-0-5')
697
self.tree1.rename_one('sub/dir/WithCaps.txt', 'temp')
698
self.tree1.rename_one('with space.txt', 'WithCaps.txt')
699
self.tree1.rename_one('temp', 'with space.txt')
700
self.tree1.commit(u'swap filenames', rev_id=b'a@cset-0-6',
702
bundle = self.get_valid_bundle('a@cset-0-5', 'a@cset-0-6')
703
other = self.get_checkout('a@cset-0-5')
704
tree1_inv = get_inventory_text(self.tree1.branch.repository,
706
tree2_inv = get_inventory_text(other.branch.repository,
708
self.assertEqualDiff(tree1_inv, tree2_inv)
709
other.rename_one('sub/dir/nolastnewline.txt', 'sub/nolastnewline.txt')
710
other.commit('rename file', rev_id=b'a@cset-0-6b')
711
self.tree1.merge_from_branch(other.branch)
712
self.tree1.commit(u'Merge', rev_id=b'a@cset-0-7',
714
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
716
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
719
self.requireFeature(features.SymlinkFeature)
720
self.tree1 = self.make_branch_and_tree('b1')
721
self.b1 = self.tree1.branch
723
tt = TreeTransform(self.tree1)
724
tt.new_symlink(link_name, tt.root, link_target, link_id)
726
self.tree1.commit('add symlink', rev_id=b'l@cset-0-1')
727
bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
728
if getattr(bundle, 'revision_tree', None) is not None:
729
# Not all bundle formats supports revision_tree
730
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-1')
731
self.assertEqual(link_target, bund_tree.get_symlink_target(link_name))
733
tt = TreeTransform(self.tree1)
734
trans_id = tt.trans_id_tree_path(link_name)
735
tt.adjust_path('link2', tt.root, trans_id)
736
tt.delete_contents(trans_id)
737
tt.create_symlink(new_link_target, trans_id)
739
self.tree1.commit('rename and change symlink', rev_id=b'l@cset-0-2')
740
bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
741
if getattr(bundle, 'revision_tree', None) is not None:
742
# Not all bundle formats supports revision_tree
743
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
744
self.assertEqual(new_link_target,
745
bund_tree.get_symlink_target('link2'))
747
tt = TreeTransform(self.tree1)
748
trans_id = tt.trans_id_tree_path('link2')
749
tt.delete_contents(trans_id)
750
tt.create_symlink('jupiter', trans_id)
752
self.tree1.commit('just change symlink target', rev_id=b'l@cset-0-3')
753
bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
755
tt = TreeTransform(self.tree1)
756
trans_id = tt.trans_id_tree_path('link2')
757
tt.delete_contents(trans_id)
759
self.tree1.commit('Delete symlink', rev_id=b'l@cset-0-4')
760
bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
762
def test_symlink_bundle(self):
763
self._test_symlink_bundle('link', 'bar/foo', 'mars')
765
def test_unicode_symlink_bundle(self):
766
self.requireFeature(features.UnicodeFilenameFeature)
767
self._test_symlink_bundle(u'\N{Euro Sign}link',
768
u'bar/\N{Euro Sign}foo',
769
u'mars\N{Euro Sign}')
771
def test_binary_bundle(self):
772
self.tree1 = self.make_branch_and_tree('b1')
773
self.b1 = self.tree1.branch
774
tt = TreeTransform(self.tree1)
777
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
778
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
781
self.tree1.commit('add binary', rev_id=b'b@cset-0-1')
782
self.get_valid_bundle('null:', 'b@cset-0-1')
785
tt = TreeTransform(self.tree1)
786
trans_id = tt.trans_id_tree_path('file')
787
tt.delete_contents(trans_id)
789
self.tree1.commit('delete binary', rev_id=b'b@cset-0-2')
790
self.get_valid_bundle('b@cset-0-1', 'b@cset-0-2')
793
tt = TreeTransform(self.tree1)
794
trans_id = tt.trans_id_tree_path('file2')
795
tt.adjust_path('file3', tt.root, trans_id)
796
tt.delete_contents(trans_id)
797
tt.create_file('file\rcontents\x00\n\x00', trans_id)
799
self.tree1.commit('rename and modify binary', rev_id=b'b@cset-0-3')
800
self.get_valid_bundle('b@cset-0-2', 'b@cset-0-3')
803
tt = TreeTransform(self.tree1)
804
trans_id = tt.trans_id_tree_path('file3')
805
tt.delete_contents(trans_id)
806
tt.create_file('\x00file\rcontents', trans_id)
808
self.tree1.commit('just modify binary', rev_id=b'b@cset-0-4')
809
self.get_valid_bundle('b@cset-0-3', 'b@cset-0-4')
812
self.get_valid_bundle('null:', 'b@cset-0-4')
814
def test_last_modified(self):
815
self.tree1 = self.make_branch_and_tree('b1')
816
self.b1 = self.tree1.branch
817
tt = TreeTransform(self.tree1)
818
tt.new_file('file', tt.root, 'file', 'file')
820
self.tree1.commit('create file', rev_id=b'a@lmod-0-1')
822
tt = TreeTransform(self.tree1)
823
trans_id = tt.trans_id_tree_path('file')
824
tt.delete_contents(trans_id)
825
tt.create_file('file2', trans_id)
827
self.tree1.commit('modify text', rev_id=b'a@lmod-0-2a')
829
other = self.get_checkout('a@lmod-0-1')
830
tt = TreeTransform(other)
831
trans_id = tt.trans_id_tree_path('file2')
832
tt.delete_contents(trans_id)
833
tt.create_file('file2', trans_id)
835
other.commit('modify text in another tree', rev_id=b'a@lmod-0-2b')
836
self.tree1.merge_from_branch(other.branch)
837
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-3',
839
self.tree1.commit(u'Merge', rev_id=b'a@lmod-0-4')
840
bundle = self.get_valid_bundle('a@lmod-0-2a', 'a@lmod-0-4')
842
def test_hide_history(self):
843
self.tree1 = self.make_branch_and_tree('b1')
844
self.b1 = self.tree1.branch
846
with open('b1/one', 'wb') as f: f.write(b'one\n')
847
self.tree1.add('one')
848
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
849
with open('b1/one', 'wb') as f: f.write(b'two\n')
850
self.tree1.commit('modify', rev_id=b'a@cset-0-2')
851
with open('b1/one', 'wb') as f: f.write(b'three\n')
852
self.tree1.commit('modify', rev_id=b'a@cset-0-3')
853
bundle_file = BytesIO()
854
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-3',
855
'a@cset-0-1', bundle_file, format=self.format)
856
self.assertNotContainsRe(bundle_file.getvalue(), '\btwo\b')
857
self.assertContainsRe(self.get_raw(bundle_file), 'one')
858
self.assertContainsRe(self.get_raw(bundle_file), 'three')
860
def test_bundle_same_basis(self):
861
"""Ensure using the basis as the target doesn't cause an error"""
862
self.tree1 = self.make_branch_and_tree('b1')
863
self.tree1.commit('add file', rev_id=b'a@cset-0-1')
864
bundle_file = BytesIO()
865
rev_ids = write_bundle(self.tree1.branch.repository, 'a@cset-0-1',
866
'a@cset-0-1', bundle_file)
869
def get_raw(bundle_file):
870
return bundle_file.getvalue()
872
def test_unicode_bundle(self):
873
self.requireFeature(features.UnicodeFilenameFeature)
874
# Handle international characters
876
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
878
self.tree1 = self.make_branch_and_tree('b1')
879
self.b1 = self.tree1.branch
882
u'With international man of mystery\n'
883
u'William Dod\xe9\n').encode('utf-8'))
886
self.tree1.add([u'with Dod\N{Euro Sign}'], [b'withdod-id'])
887
self.tree1.commit(u'i18n commit from William Dod\xe9',
888
rev_id=b'i18n-1', committer=u'William Dod\xe9')
891
bundle = self.get_valid_bundle('null:', 'i18n-1')
894
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
895
f.write(u'Modified \xb5\n'.encode('utf8'))
897
self.tree1.commit(u'modified', rev_id=b'i18n-2')
899
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
902
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
903
self.tree1.commit(u'renamed, the new i18n man', rev_id=b'i18n-3',
904
committer=u'Erik B\xe5gfors')
906
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
909
self.tree1.remove([u'B\N{Euro Sign}gfors'])
910
self.tree1.commit(u'removed', rev_id=b'i18n-4')
912
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
915
bundle = self.get_valid_bundle('null:', 'i18n-4')
918
def test_whitespace_bundle(self):
919
if sys.platform in ('win32', 'cygwin'):
920
raise tests.TestSkipped('Windows doesn\'t support filenames'
921
' with tabs or trailing spaces')
922
self.tree1 = self.make_branch_and_tree('b1')
923
self.b1 = self.tree1.branch
925
self.build_tree(['b1/trailing space '])
926
self.tree1.add(['trailing space '])
927
# TODO: jam 20060701 Check for handling files with '\t' characters
928
# once we actually support them
931
self.tree1.commit('funky whitespace', rev_id=b'white-1')
933
bundle = self.get_valid_bundle('null:', 'white-1')
936
with open('b1/trailing space ', 'ab') as f: f.write('add some text\n')
937
self.tree1.commit('add text', rev_id=b'white-2')
939
bundle = self.get_valid_bundle('white-1', 'white-2')
942
self.tree1.rename_one('trailing space ', ' start and end space ')
943
self.tree1.commit('rename', rev_id=b'white-3')
945
bundle = self.get_valid_bundle('white-2', 'white-3')
948
self.tree1.remove([' start and end space '])
949
self.tree1.commit('removed', rev_id=b'white-4')
951
bundle = self.get_valid_bundle('white-3', 'white-4')
953
# Now test a complet roll-up
954
bundle = self.get_valid_bundle('null:', 'white-4')
956
def test_alt_timezone_bundle(self):
957
self.tree1 = self.make_branch_and_memory_tree('b1')
958
self.b1 = self.tree1.branch
959
builder = treebuilder.TreeBuilder()
961
self.tree1.lock_write()
962
builder.start_tree(self.tree1)
963
builder.build(['newfile'])
964
builder.finish_tree()
966
# Asia/Colombo offset = 5 hours 30 minutes
967
self.tree1.commit('non-hour offset timezone', rev_id=b'tz-1',
968
timezone=19800, timestamp=1152544886.0)
970
bundle = self.get_valid_bundle('null:', 'tz-1')
972
rev = bundle.revisions[0]
973
self.assertEqual('Mon 2006-07-10 20:51:26.000000000 +0530', rev.date)
974
self.assertEqual(19800, rev.timezone)
975
self.assertEqual(1152544886.0, rev.timestamp)
978
def test_bundle_root_id(self):
979
self.tree1 = self.make_branch_and_tree('b1')
980
self.b1 = self.tree1.branch
981
self.tree1.commit('message', rev_id=b'revid1')
982
bundle = self.get_valid_bundle('null:', 'revid1')
983
tree = self.get_bundle_tree(bundle, 'revid1')
984
root_revision = tree.get_file_revision(u'', tree.get_root_id())
985
self.assertEqual('revid1', root_revision)
987
def test_install_revisions(self):
988
self.tree1 = self.make_branch_and_tree('b1')
989
self.b1 = self.tree1.branch
990
self.tree1.commit('message', rev_id=b'rev2a')
991
bundle = self.get_valid_bundle('null:', 'rev2a')
992
branch2 = self.make_branch('b2')
993
self.assertFalse(branch2.repository.has_revision('rev2a'))
994
target_revision = bundle.install_revisions(branch2.repository)
995
self.assertTrue(branch2.repository.has_revision('rev2a'))
996
self.assertEqual('rev2a', target_revision)
998
def test_bundle_empty_property(self):
999
"""Test serializing revision properties with an empty value."""
1000
tree = self.make_branch_and_memory_tree('tree')
1002
self.addCleanup(tree.unlock)
1003
tree.add([''], ['TREE_ROOT'])
1004
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id=b'rev1')
1005
self.b1 = tree.branch
1006
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1007
bundle = read_bundle(bundle_sio)
1008
revision_info = bundle.revisions[0]
1009
self.assertEqual('rev1', revision_info.revision_id)
1010
rev = revision_info.as_revision()
1011
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1014
def test_bundle_sorted_properties(self):
1015
"""For stability the writer should write properties in sorted order."""
1016
tree = self.make_branch_and_memory_tree('tree')
1018
self.addCleanup(tree.unlock)
1020
tree.add([''], ['TREE_ROOT'])
1021
tree.commit('One', rev_id=b'rev1',
1022
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1023
self.b1 = tree.branch
1024
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1025
bundle = read_bundle(bundle_sio)
1026
revision_info = bundle.revisions[0]
1027
self.assertEqual('rev1', revision_info.revision_id)
1028
rev = revision_info.as_revision()
1029
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1030
'd':'1'}, rev.properties)
1032
def test_bundle_unicode_properties(self):
1033
"""We should be able to round trip a non-ascii property."""
1034
tree = self.make_branch_and_memory_tree('tree')
1036
self.addCleanup(tree.unlock)
1038
tree.add([''], ['TREE_ROOT'])
1039
# Revisions themselves do not require anything about revision property
1040
# keys, other than that they are a basestring, and do not contain
1042
# However, Testaments assert than they are str(), and thus should not
1044
tree.commit('One', rev_id=b'rev1',
1045
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1046
self.b1 = tree.branch
1047
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1048
bundle = read_bundle(bundle_sio)
1049
revision_info = bundle.revisions[0]
1050
self.assertEqual('rev1', revision_info.revision_id)
1051
rev = revision_info.as_revision()
1052
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1053
'alpha':u'\u03b1'}, rev.properties)
1055
def test_bundle_with_ghosts(self):
1056
tree = self.make_branch_and_tree('tree')
1057
self.b1 = tree.branch
1058
self.build_tree_contents([('tree/file', b'content1')])
1061
self.build_tree_contents([('tree/file', b'content2')])
1062
tree.add_parent_tree_id(b'ghost')
1063
tree.commit('rev2', rev_id=b'rev2')
1064
bundle = self.get_valid_bundle(b'null:', b'rev2')
1066
def make_simple_tree(self, format=None):
1067
tree = self.make_branch_and_tree('b1', format=format)
1068
self.b1 = tree.branch
1069
self.build_tree(['b1/file'])
1073
def test_across_serializers(self):
1074
tree = self.make_simple_tree('knit')
1075
tree.commit('hello', rev_id=b'rev1')
1076
tree.commit('hello', rev_id=b'rev2')
1077
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1078
repo = self.make_repository('repo', format='dirstate-with-subtree')
1079
bundle.install_revisions(repo)
1080
inv_text = repo._get_inventory_xml('rev2')
1081
self.assertNotContainsRe(inv_text, 'format="5"')
1082
self.assertContainsRe(inv_text, 'format="7"')
1084
def make_repo_with_installed_revisions(self):
1085
tree = self.make_simple_tree('knit')
1086
tree.commit('hello', rev_id=b'rev1')
1087
tree.commit('hello', rev_id=b'rev2')
1088
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1089
repo = self.make_repository('repo', format='dirstate-with-subtree')
1090
bundle.install_revisions(repo)
1093
def test_across_models(self):
1094
repo = self.make_repo_with_installed_revisions()
1095
inv = repo.get_inventory('rev2')
1096
self.assertEqual('rev2', inv.root.revision)
1097
root_id = inv.root.file_id
1099
self.addCleanup(repo.unlock)
1100
self.assertEqual({(root_id, 'rev1'):(),
1101
(root_id, 'rev2'):((root_id, 'rev1'),)},
1102
repo.texts.get_parent_map([(root_id, 'rev1'), (root_id, 'rev2')]))
1104
def test_inv_hash_across_serializers(self):
1105
repo = self.make_repo_with_installed_revisions()
1106
recorded_inv_sha1 = repo.get_revision('rev2').inventory_sha1
1107
xml = repo._get_inventory_xml('rev2')
1108
self.assertEqual(osutils.sha_string(xml), recorded_inv_sha1)
1110
def test_across_models_incompatible(self):
1111
tree = self.make_simple_tree('dirstate-with-subtree')
1112
tree.commit('hello', rev_id=b'rev1')
1113
tree.commit('hello', rev_id=b'rev2')
1115
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1116
except errors.IncompatibleBundleFormat:
1117
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1118
repo = self.make_repository('repo', format='knit')
1119
bundle.install_revisions(repo)
1121
bundle = read_bundle(self.create_bundle_text('null:', 'rev2')[0])
1122
self.assertRaises(errors.IncompatibleRevision,
1123
bundle.install_revisions, repo)
1125
def test_get_merge_request(self):
1126
tree = self.make_simple_tree()
1127
tree.commit('hello', rev_id=b'rev1')
1128
tree.commit('hello', rev_id=b'rev2')
1129
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1130
result = bundle.get_merge_request(tree.branch.repository)
1131
self.assertEqual((None, 'rev1', 'inapplicable'), result)
1133
def test_with_subtree(self):
1134
tree = self.make_branch_and_tree('tree',
1135
format='dirstate-with-subtree')
1136
self.b1 = tree.branch
1137
subtree = self.make_branch_and_tree('tree/subtree',
1138
format='dirstate-with-subtree')
1140
tree.commit('hello', rev_id=b'rev1')
1142
bundle = read_bundle(self.create_bundle_text('null:', 'rev1')[0])
1143
except errors.IncompatibleBundleFormat:
1144
raise tests.TestSkipped("Format 0.8 doesn't work with knit3")
1145
if isinstance(bundle, v09.BundleInfo09):
1146
raise tests.TestSkipped("Format 0.9 doesn't work with subtrees")
1147
repo = self.make_repository('repo', format='knit')
1148
self.assertRaises(errors.IncompatibleRevision,
1149
bundle.install_revisions, repo)
1150
repo2 = self.make_repository('repo2', format='dirstate-with-subtree')
1151
bundle.install_revisions(repo2)
1153
def test_revision_id_with_slash(self):
1154
self.tree1 = self.make_branch_and_tree('tree')
1155
self.b1 = self.tree1.branch
1157
self.tree1.commit('Revision/id/with/slashes', rev_id=b'rev/id')
1159
raise tests.TestSkipped(
1160
"Repository doesn't support revision ids with slashes")
1161
bundle = self.get_valid_bundle('null:', 'rev/id')
1163
def test_skip_file(self):
1164
"""Make sure we don't accidentally write to the wrong versionedfile"""
1165
self.tree1 = self.make_branch_and_tree('tree')
1166
self.b1 = self.tree1.branch
1167
# rev1 is not present in bundle, done by fetch
1168
self.build_tree_contents([('tree/file2', b'contents1')])
1169
self.tree1.add('file2', b'file2-id')
1170
self.tree1.commit('rev1', rev_id=b'reva')
1171
self.build_tree_contents([('tree/file3', b'contents2')])
1172
# rev2 is present in bundle, and done by fetch
1173
# having file1 in the bunle causes file1's versionedfile to be opened.
1174
self.tree1.add('file3', b'file3-id')
1175
self.tree1.commit('rev2')
1176
# Updating file2 should not cause an attempt to add to file1's vf
1177
target = self.tree1.controldir.sprout('target').open_workingtree()
1178
self.build_tree_contents([('tree/file2', b'contents3')])
1179
self.tree1.commit('rev3', rev_id=b'rev3')
1180
bundle = self.get_valid_bundle('reva', 'rev3')
1181
if getattr(bundle, 'get_bundle_reader', None) is None:
1182
raise tests.TestSkipped('Bundle format cannot provide reader')
1183
# be sure that file1 comes before file2
1184
for b, m, k, r, f in bundle.get_bundle_reader().iter_records():
1185
if f == b'file3-id':
1187
self.assertNotEqual(f, b'file2-id')
1188
bundle.install_revisions(target.branch.repository)
1191
class V08BundleTester(BundleTester, tests.TestCaseWithTransport):
1195
def test_bundle_empty_property(self):
1196
"""Test serializing revision properties with an empty value."""
1197
tree = self.make_branch_and_memory_tree('tree')
1199
self.addCleanup(tree.unlock)
1200
tree.add([''], ['TREE_ROOT'])
1201
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id=b'rev1')
1202
self.b1 = tree.branch
1203
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1204
self.assertContainsRe(bundle_sio.getvalue(),
1206
'# branch-nick: tree\n'
1210
bundle = read_bundle(bundle_sio)
1211
revision_info = bundle.revisions[0]
1212
self.assertEqual('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, '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([''], ['TREE_ROOT'])
1233
tree.commit('One', revprops={'one':'two', 'empty':''}, rev_id=b'rev1')
1234
self.b1 = tree.branch
1235
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1236
txt = bundle_sio.getvalue()
1237
loc = txt.find('# empty: ') + len('# empty:')
1238
# Create a new bundle, which strips the trailing space after empty
1239
bundle_sio = BytesIO(txt[:loc] + txt[loc+1:])
1241
self.assertContainsRe(bundle_sio.getvalue(),
1243
'# branch-nick: tree\n'
1247
bundle = read_bundle(bundle_sio)
1248
revision_info = bundle.revisions[0]
1249
self.assertEqual('rev1', revision_info.revision_id)
1250
rev = revision_info.as_revision()
1251
self.assertEqual({'branch-nick':'tree', 'empty':'', 'one':'two'},
1254
def test_bundle_sorted_properties(self):
1255
"""For stability the writer should write properties in sorted order."""
1256
tree = self.make_branch_and_memory_tree('tree')
1258
self.addCleanup(tree.unlock)
1260
tree.add([''], ['TREE_ROOT'])
1261
tree.commit('One', rev_id=b'rev1',
1262
revprops={'a':'4', 'b':'3', 'c':'2', 'd':'1'})
1263
self.b1 = tree.branch
1264
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1265
self.assertContainsRe(bundle_sio.getvalue(),
1269
'# branch-nick: tree\n'
1273
bundle = read_bundle(bundle_sio)
1274
revision_info = bundle.revisions[0]
1275
self.assertEqual('rev1', revision_info.revision_id)
1276
rev = revision_info.as_revision()
1277
self.assertEqual({'branch-nick':'tree', 'a':'4', 'b':'3', 'c':'2',
1278
'd':'1'}, rev.properties)
1280
def test_bundle_unicode_properties(self):
1281
"""We should be able to round trip a non-ascii property."""
1282
tree = self.make_branch_and_memory_tree('tree')
1284
self.addCleanup(tree.unlock)
1286
tree.add([''], ['TREE_ROOT'])
1287
# Revisions themselves do not require anything about revision property
1288
# keys, other than that they are a basestring, and do not contain
1290
# However, Testaments assert than they are str(), and thus should not
1292
tree.commit('One', rev_id=b'rev1',
1293
revprops={'omega':u'\u03a9', 'alpha':u'\u03b1'})
1294
self.b1 = tree.branch
1295
bundle_sio, revision_ids = self.create_bundle_text('null:', 'rev1')
1296
self.assertContainsRe(bundle_sio.getvalue(),
1298
'# alpha: \xce\xb1\n'
1299
'# branch-nick: tree\n'
1300
'# omega: \xce\xa9\n'
1302
bundle = read_bundle(bundle_sio)
1303
revision_info = bundle.revisions[0]
1304
self.assertEqual('rev1', revision_info.revision_id)
1305
rev = revision_info.as_revision()
1306
self.assertEqual({'branch-nick':'tree', 'omega':u'\u03a9',
1307
'alpha':u'\u03b1'}, rev.properties)
1310
class V09BundleKnit2Tester(V08BundleTester):
1314
def bzrdir_format(self):
1315
format = bzrdir.BzrDirMetaFormat1()
1316
format.repository_format = knitrepo.RepositoryFormatKnit3()
1320
class V09BundleKnit1Tester(V08BundleTester):
1324
def bzrdir_format(self):
1325
format = bzrdir.BzrDirMetaFormat1()
1326
format.repository_format = knitrepo.RepositoryFormatKnit1()
1330
class V4BundleTester(BundleTester, tests.TestCaseWithTransport):
1334
def get_valid_bundle(self, base_rev_id, rev_id, checkout_dir=None):
1335
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1336
Make sure that the text generated is valid, and that it
1337
can be applied against the base, and generate the same information.
1339
:return: The in-memory bundle
1341
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1343
# This should also validate the generated bundle
1344
bundle = read_bundle(bundle_txt)
1345
repository = self.b1.repository
1346
for bundle_rev in bundle.real_revisions:
1347
# These really should have already been checked when we read the
1348
# bundle, since it computes the sha1 hash for the revision, which
1349
# only will match if everything is okay, but lets be explicit about
1351
branch_rev = repository.get_revision(bundle_rev.revision_id)
1352
for a in ('inventory_sha1', 'revision_id', 'parent_ids',
1353
'timestamp', 'timezone', 'message', 'committer',
1354
'parent_ids', 'properties'):
1355
self.assertEqual(getattr(branch_rev, a),
1356
getattr(bundle_rev, a))
1357
self.assertEqual(len(branch_rev.parent_ids),
1358
len(bundle_rev.parent_ids))
1359
self.assertEqual(set(rev_ids),
1360
{r.revision_id for r in bundle.real_revisions})
1361
self.valid_apply_bundle(base_rev_id, bundle,
1362
checkout_dir=checkout_dir)
1366
def get_invalid_bundle(self, base_rev_id, rev_id):
1367
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1368
Munge the text so that it's invalid.
1370
:return: The in-memory bundle
1372
from ..bundle import serializer
1373
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1374
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1375
new_text = new_text.replace('<file file_id="exe-1"',
1376
'<file executable="y" file_id="exe-1"')
1377
new_text = new_text.replace('B260', 'B275')
1378
bundle_txt = BytesIO()
1379
bundle_txt.write(serializer._get_bundle_header('4'))
1380
bundle_txt.write('\n')
1381
bundle_txt.write(new_text.encode('bz2'))
1383
bundle = read_bundle(bundle_txt)
1384
self.valid_apply_bundle(base_rev_id, bundle)
1387
def create_bundle_text(self, base_rev_id, rev_id):
1388
bundle_txt = BytesIO()
1389
rev_ids = write_bundle(self.b1.repository, rev_id, base_rev_id,
1390
bundle_txt, format=self.format)
1392
self.assertEqual(bundle_txt.readline(),
1393
'# Bazaar revision bundle v%s\n' % self.format)
1394
self.assertEqual(bundle_txt.readline(), '#\n')
1395
rev = self.b1.repository.get_revision(rev_id)
1397
return bundle_txt, rev_ids
1399
def get_bundle_tree(self, bundle, revision_id):
1400
repository = self.make_repository('repo')
1401
bundle.install_revisions(repository)
1402
return repository.revision_tree(revision_id)
1404
def test_creation(self):
1405
tree = self.make_branch_and_tree('tree')
1406
self.build_tree_contents([('tree/file', b'contents1\nstatic\n')])
1407
tree.add('file', 'fileid-2')
1408
tree.commit('added file', rev_id=b'rev1')
1409
self.build_tree_contents([('tree/file', b'contents2\nstatic\n')])
1410
tree.commit('changed file', rev_id=b'rev2')
1412
serializer = BundleSerializerV4('1.0')
1413
serializer.write(tree.branch.repository, ['rev1', 'rev2'], {}, s)
1415
tree2 = self.make_branch_and_tree('target')
1416
target_repo = tree2.branch.repository
1417
install_bundle(target_repo, serializer.read(s))
1418
target_repo.lock_read()
1419
self.addCleanup(target_repo.unlock)
1420
# Turn the 'iterators_of_bytes' back into simple strings for comparison
1421
repo_texts = dict((i, ''.join(content)) for i, content
1422
in target_repo.iter_files_bytes(
1423
[('fileid-2', 'rev1', '1'),
1424
('fileid-2', 'rev2', '2')]))
1425
self.assertEqual({'1':'contents1\nstatic\n',
1426
'2':'contents2\nstatic\n'},
1428
rtree = target_repo.revision_tree('rev2')
1429
inventory_vf = target_repo.inventories
1430
# If the inventory store has a graph, it must match the revision graph.
1432
[inventory_vf.get_parent_map([('rev2',)])[('rev2',)]],
1433
[None, (('rev1',),)])
1434
self.assertEqual('changed file',
1435
target_repo.get_revision('rev2').message)
1438
def get_raw(bundle_file):
1440
line = bundle_file.readline()
1441
line = bundle_file.readline()
1442
lines = bundle_file.readlines()
1443
return ''.join(lines).decode('bz2')
1445
def test_copy_signatures(self):
1446
tree_a = self.make_branch_and_tree('tree_a')
1448
import breezy.commit as commit
1449
oldstrategy = breezy.gpg.GPGStrategy
1450
branch = tree_a.branch
1451
repo_a = branch.repository
1452
tree_a.commit("base", allow_pointless=True, rev_id=b'A')
1453
self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
1455
from ..testament import Testament
1456
# monkey patch gpg signing mechanism
1457
breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
1458
new_config = test_commit.MustSignConfig()
1459
commit.Commit(config_stack=new_config).commit(message="base",
1460
allow_pointless=True,
1462
working_tree=tree_a)
1464
return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
1465
self.assertTrue(repo_a.has_signature_for_revision_id('B'))
1467
breezy.gpg.GPGStrategy = oldstrategy
1468
tree_b = self.make_branch_and_tree('tree_b')
1469
repo_b = tree_b.branch.repository
1471
serializer = BundleSerializerV4('4')
1472
serializer.write(tree_a.branch.repository, ['A', 'B'], {}, s)
1474
install_bundle(repo_b, serializer.read(s))
1475
self.assertTrue(repo_b.has_signature_for_revision_id('B'))
1476
self.assertEqual(repo_b.get_signature_text('B'),
1477
repo_a.get_signature_text('B'))
1479
# ensure repeat installs are harmless
1480
install_bundle(repo_b, serializer.read(s))
1483
class V4_2aBundleTester(V4BundleTester):
1485
def bzrdir_format(self):
1488
def get_invalid_bundle(self, base_rev_id, rev_id):
1489
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1490
Munge the text so that it's invalid.
1492
:return: The in-memory bundle
1494
from ..bundle import serializer
1495
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1496
new_text = self.get_raw(BytesIO(b''.join(bundle_txt)))
1497
# We are going to be replacing some text to set the executable bit on a
1498
# file. Make sure the text replacement actually works correctly.
1499
self.assertContainsRe(new_text, '(?m)B244\n\ni 1\n<inventory')
1500
new_text = new_text.replace('<file file_id="exe-1"',
1501
'<file executable="y" file_id="exe-1"')
1502
new_text = new_text.replace('B244', 'B259')
1503
bundle_txt = BytesIO()
1504
bundle_txt.write(serializer._get_bundle_header('4'))
1505
bundle_txt.write('\n')
1506
bundle_txt.write(new_text.encode('bz2'))
1508
bundle = read_bundle(bundle_txt)
1509
self.valid_apply_bundle(base_rev_id, bundle)
1512
def make_merged_branch(self):
1513
builder = self.make_branch_builder('source')
1514
builder.start_series()
1515
builder.build_snapshot(None, [
1516
('add', ('', 'root-id', 'directory', None)),
1517
('add', ('file', 'file-id', 'file', 'original content\n')),
1518
], revision_id='a@cset-0-1')
1519
builder.build_snapshot(['a@cset-0-1'], [
1520
('modify', ('file', 'new-content\n')),
1521
], revision_id='a@cset-0-2a')
1522
builder.build_snapshot(['a@cset-0-1'], [
1523
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1524
], revision_id='a@cset-0-2b')
1525
builder.build_snapshot(['a@cset-0-2a', 'a@cset-0-2b'], [
1526
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1527
], revision_id='a@cset-0-3')
1528
builder.finish_series()
1529
self.b1 = builder.get_branch()
1531
self.addCleanup(self.b1.unlock)
1533
def make_bundle_just_inventories(self, base_revision_id,
1537
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1538
self.b1.repository, sio)
1539
writer.bundle.begin()
1540
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1545
def test_single_inventory_multiple_parents_as_xml(self):
1546
self.make_merged_branch()
1547
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1549
reader = v4.BundleReader(sio, stream_input=False)
1550
records = list(reader.iter_records())
1551
self.assertEqual(1, len(records))
1552
(bytes, metadata, repo_kind, revision_id,
1553
file_id) = records[0]
1554
self.assertIs(None, file_id)
1555
self.assertEqual('a@cset-0-3', revision_id)
1556
self.assertEqual('inventory', repo_kind)
1557
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1558
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1559
'storage_kind': 'mpdiff',
1561
# We should have an mpdiff that takes some lines from both parents.
1562
self.assertEqualDiff(
1564
'<inventory format="10" revision_id="a@cset-0-3">\n'
1567
'c 1 3 3 2\n', bytes)
1569
def test_single_inv_no_parents_as_xml(self):
1570
self.make_merged_branch()
1571
sio = self.make_bundle_just_inventories('null:', 'a@cset-0-1',
1573
reader = v4.BundleReader(sio, stream_input=False)
1574
records = list(reader.iter_records())
1575
self.assertEqual(1, len(records))
1576
(bytes, metadata, repo_kind, revision_id,
1577
file_id) = records[0]
1578
self.assertIs(None, file_id)
1579
self.assertEqual('a@cset-0-1', revision_id)
1580
self.assertEqual('inventory', repo_kind)
1581
self.assertEqual({'parents': [],
1582
'sha1': 'a13f42b142d544aac9b085c42595d304150e31a2',
1583
'storage_kind': 'mpdiff',
1585
# We should have an mpdiff that takes some lines from both parents.
1586
self.assertEqualDiff(
1588
'<inventory format="10" revision_id="a@cset-0-1">\n'
1589
'<directory file_id="root-id" name=""'
1590
' revision="a@cset-0-1" />\n'
1591
'<file file_id="file-id" name="file" parent_id="root-id"'
1592
' revision="a@cset-0-1"'
1593
' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1594
' text_size="17" />\n'
1598
def test_multiple_inventories_as_xml(self):
1599
self.make_merged_branch()
1600
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1601
['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'])
1602
reader = v4.BundleReader(sio, stream_input=False)
1603
records = list(reader.iter_records())
1604
self.assertEqual(3, len(records))
1605
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1606
self.assertEqual(['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'],
1608
metadata_2a = records[0][1]
1609
self.assertEqual({'parents': ['a@cset-0-1'],
1610
'sha1': '1e105886d62d510763e22885eec733b66f5f09bf',
1611
'storage_kind': 'mpdiff',
1613
metadata_2b = records[1][1]
1614
self.assertEqual({'parents': ['a@cset-0-1'],
1615
'sha1': 'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1616
'storage_kind': 'mpdiff',
1618
metadata_3 = records[2][1]
1619
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1620
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1621
'storage_kind': 'mpdiff',
1623
bytes_2a = records[0][0]
1624
self.assertEqualDiff(
1626
'<inventory format="10" revision_id="a@cset-0-2a">\n'
1630
'<file file_id="file-id" name="file" parent_id="root-id"'
1631
' revision="a@cset-0-2a"'
1632
' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1633
' text_size="12" />\n'
1635
'c 0 3 3 1\n', bytes_2a)
1636
bytes_2b = records[1][0]
1637
self.assertEqualDiff(
1639
'<inventory format="10" revision_id="a@cset-0-2b">\n'
1643
'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1644
' revision="a@cset-0-2b"'
1645
' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1646
' text_size="14" />\n'
1648
'c 0 3 4 1\n', bytes_2b)
1649
bytes_3 = records[2][0]
1650
self.assertEqualDiff(
1652
'<inventory format="10" revision_id="a@cset-0-3">\n'
1655
'c 1 3 3 2\n', bytes_3)
1657
def test_creating_bundle_preserves_chk_pages(self):
1658
self.make_merged_branch()
1659
target = self.b1.controldir.sprout('target',
1660
revision_id='a@cset-0-2a').open_branch()
1661
bundle_txt, rev_ids = self.create_bundle_text('a@cset-0-2a',
1663
self.assertEqual(['a@cset-0-2b', 'a@cset-0-3'], rev_ids)
1664
bundle = read_bundle(bundle_txt)
1666
self.addCleanup(target.unlock)
1667
install_bundle(target.repository, bundle)
1668
inv1 = self.b1.repository.inventories.get_record_stream([
1669
('a@cset-0-3',)], 'unordered',
1670
True).next().get_bytes_as('fulltext')
1671
inv2 = target.repository.inventories.get_record_stream([
1672
('a@cset-0-3',)], 'unordered',
1673
True).next().get_bytes_as('fulltext')
1674
self.assertEqualDiff(inv1, inv2)
1677
class MungedBundleTester(object):
1679
def build_test_bundle(self):
1680
wt = self.make_branch_and_tree('b1')
1682
self.build_tree(['b1/one'])
1684
wt.commit('add one', rev_id=b'a@cset-0-1')
1685
self.build_tree(['b1/two'])
1687
wt.commit('add two', rev_id=b'a@cset-0-2',
1688
revprops={'branch-nick':'test'})
1690
bundle_txt = BytesIO()
1691
rev_ids = write_bundle(wt.branch.repository, 'a@cset-0-2',
1692
'a@cset-0-1', bundle_txt, self.format)
1693
self.assertEqual({'a@cset-0-2'}, set(rev_ids))
1694
bundle_txt.seek(0, 0)
1697
def check_valid(self, bundle):
1698
"""Check that after whatever munging, the final object is valid."""
1699
self.assertEqual(['a@cset-0-2'],
1700
[r.revision_id for r in bundle.real_revisions])
1702
def test_extra_whitespace(self):
1703
bundle_txt = self.build_test_bundle()
1705
# Seek to the end of the file
1706
# Adding one extra newline used to give us
1707
# TypeError: float() argument must be a string or a number
1708
bundle_txt.seek(0, 2)
1709
bundle_txt.write('\n')
1712
bundle = read_bundle(bundle_txt)
1713
self.check_valid(bundle)
1715
def test_extra_whitespace_2(self):
1716
bundle_txt = self.build_test_bundle()
1718
# Seek to the end of the file
1719
# Adding two extra newlines used to give us
1720
# MalformedPatches: The first line of all patches should be ...
1721
bundle_txt.seek(0, 2)
1722
bundle_txt.write('\n\n')
1725
bundle = read_bundle(bundle_txt)
1726
self.check_valid(bundle)
1729
class MungedBundleTesterV09(tests.TestCaseWithTransport, MungedBundleTester):
1733
def test_missing_trailing_whitespace(self):
1734
bundle_txt = self.build_test_bundle()
1736
# Remove a trailing newline, it shouldn't kill the parser
1737
raw = bundle_txt.getvalue()
1738
# The contents of the bundle don't have to be this, but this
1739
# test is concerned with the exact case where the serializer
1740
# creates a blank line at the end, and fails if that
1742
self.assertEqual('\n\n', raw[-2:])
1743
bundle_txt = BytesIO(raw[:-1])
1745
bundle = read_bundle(bundle_txt)
1746
self.check_valid(bundle)
1748
def test_opening_text(self):
1749
bundle_txt = self.build_test_bundle()
1751
bundle_txt = BytesIO(
1752
b"Some random\nemail comments\n" + bundle_txt.getvalue())
1754
bundle = read_bundle(bundle_txt)
1755
self.check_valid(bundle)
1757
def test_trailing_text(self):
1758
bundle_txt = self.build_test_bundle()
1760
bundle_txt = BytesIO(
1761
bundle_txt.getvalue() + b"Some trailing\nrandom\ntext\n")
1763
bundle = read_bundle(bundle_txt)
1764
self.check_valid(bundle)
1767
class MungedBundleTesterV4(tests.TestCaseWithTransport, MungedBundleTester):
1772
class TestBundleWriterReader(tests.TestCase):
1774
def test_roundtrip_record(self):
1776
writer = v4.BundleWriter(fileobj)
1778
writer.add_info_record(foo='bar')
1779
writer._add_record("Record body", {'parents': ['1', '3'],
1780
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1783
reader = v4.BundleReader(fileobj, stream_input=True)
1784
record_iter = reader.iter_records()
1785
record = next(record_iter)
1786
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1787
'info', None, None), record)
1788
record = next(record_iter)
1789
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1790
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1793
def test_roundtrip_record_memory_hungry(self):
1795
writer = v4.BundleWriter(fileobj)
1797
writer.add_info_record(foo='bar')
1798
writer._add_record("Record body", {'parents': ['1', '3'],
1799
'storage_kind':'fulltext'}, 'file', 'revid', 'fileid')
1802
reader = v4.BundleReader(fileobj, stream_input=False)
1803
record_iter = reader.iter_records()
1804
record = next(record_iter)
1805
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1806
'info', None, None), record)
1807
record = next(record_iter)
1808
self.assertEqual(("Record body", {'storage_kind': 'fulltext',
1809
'parents': ['1', '3']}, 'file', 'revid', 'fileid'),
1812
def test_encode_name(self):
1813
self.assertEqual('revision/rev1',
1814
v4.BundleWriter.encode_name('revision', 'rev1'))
1815
self.assertEqual('file/rev//1/file-id-1',
1816
v4.BundleWriter.encode_name('file', 'rev/1', 'file-id-1'))
1817
self.assertEqual('info',
1818
v4.BundleWriter.encode_name('info', None, None))
1820
def test_decode_name(self):
1821
self.assertEqual(('revision', 'rev1', None),
1822
v4.BundleReader.decode_name('revision/rev1'))
1823
self.assertEqual(('file', 'rev/1', 'file-id-1'),
1824
v4.BundleReader.decode_name('file/rev//1/file-id-1'))
1825
self.assertEqual(('info', None, None),
1826
v4.BundleReader.decode_name('info'))
1828
def test_too_many_names(self):
1830
writer = v4.BundleWriter(fileobj)
1832
writer.add_info_record(foo='bar')
1833
writer._container.add_bytes_record('blah', ['two', 'names'])
1836
record_iter = v4.BundleReader(fileobj).iter_records()
1837
record = next(record_iter)
1838
self.assertEqual((None, {'foo': 'bar', 'storage_kind': 'header'},
1839
'info', None, None), record)
1840
self.assertRaises(errors.BadBundle, next, record_iter)
1843
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
1845
def test_read_mergeable_skips_local(self):
1846
"""A local bundle named like the URL should not be read.
1848
out, wt = test_read_bundle.create_bundle_file(self)
1849
class FooService(object):
1850
"""A directory service that always returns source"""
1852
def look_up(self, name, url):
1854
directories.register('foo:', FooService, 'Testing directory service')
1855
self.addCleanup(directories.remove, 'foo:')
1856
self.build_tree_contents([('./foo:bar', out.getvalue())])
1857
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1860
def test_infinite_redirects_are_not_a_bundle(self):
1861
"""If a URL causes TooManyRedirections then NotABundle is raised.
1863
from .blackbox.test_push import RedirectingMemoryServer
1864
server = RedirectingMemoryServer()
1865
self.start_server(server)
1866
url = server.get_url() + 'infinite-loop'
1867
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1869
def test_smart_server_connection_reset(self):
1870
"""If a smart server connection fails during the attempt to read a
1871
bundle, then the ConnectionReset error should be propagated.
1873
# Instantiate a server that will provoke a ConnectionReset
1874
sock_server = DisconnectingServer()
1875
self.start_server(sock_server)
1876
# We don't really care what the url is since the server will close the
1877
# connection without interpreting it
1878
url = sock_server.get_url()
1879
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1882
class DisconnectingHandler(socketserver.BaseRequestHandler):
1883
"""A request handler that immediately closes any connection made to it."""
1886
self.request.close()
1889
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1892
super(DisconnectingServer, self).__init__(
1894
test_server.TestingTCPServer,
1895
DisconnectingHandler)
1898
"""Return the url of the server"""
1899
return "bzr://%s:%d/" % self.server.server_address