451
423
def get_invalid_bundle(self, base_rev_id, rev_id):
452
424
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
453
425
Munge the text so that it's invalid.
455
427
:return: The in-memory bundle
457
429
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
458
new_text = bundle_txt.getvalue().replace('executable:no',
430
new_text = bundle_txt.getvalue().replace('executable:no',
459
431
'executable:yes')
460
432
bundle_txt = StringIO(new_text)
461
433
bundle = read_bundle(bundle_txt)
462
434
self.valid_apply_bundle(base_rev_id, bundle)
465
437
def test_non_bundle(self):
466
self.assertRaises(errors.NotABundle,
467
read_bundle, StringIO('#!/bin/sh\n'))
438
self.assertRaises(NotABundle, read_bundle, StringIO('#!/bin/sh\n'))
469
440
def test_malformed(self):
470
self.assertRaises(errors.BadBundle, read_bundle,
441
self.assertRaises(BadBundle, read_bundle,
471
442
StringIO('# Bazaar revision bundle v'))
473
444
def test_crlf_bundle(self):
475
446
read_bundle(StringIO('# Bazaar revision bundle v0.8\r\n'))
476
except errors.BadBundle:
477
448
# It is currently permitted for bundles with crlf line endings to
478
449
# make read_bundle raise a BadBundle, but this should be fixed.
479
450
# Anything else, especially NotABundle, is an error.
545
517
original_parents = to_tree.get_parent_ids()
546
518
self.assertIs(repository.has_revision(base_rev_id), True)
547
519
for rev in info.real_revisions:
548
self.assertTrue(not repository.has_revision(rev.revision_id),
549
'Revision {%s} present before applying bundle'
551
merge_bundle(info, to_tree, True, merge.Merge3Merger, False, False)
520
self.assert_(not repository.has_revision(rev.revision_id),
521
'Revision {%s} present before applying bundle'
523
merge_bundle(info, to_tree, True, Merge3Merger, False, False)
553
525
for rev in info.real_revisions:
554
self.assertTrue(repository.has_revision(rev.revision_id),
555
'Missing revision {%s} after applying bundle'
526
self.assert_(repository.has_revision(rev.revision_id),
527
'Missing revision {%s} after applying bundle'
558
self.assertTrue(to_tree.branch.repository.has_revision(info.target))
530
self.assert_(to_tree.branch.repository.has_revision(info.target))
559
531
# Do we also want to verify that all the texts have been added?
561
533
self.assertEqual(original_parents + [info.target],
562
to_tree.get_parent_ids())
534
to_tree.get_parent_ids())
564
536
rev = info.real_revisions[-1]
565
537
base_tree = self.b1.repository.revision_tree(rev.revision_id)
566
538
to_tree = to_tree.branch.repository.revision_tree(rev.revision_id)
568
540
# TODO: make sure the target tree is identical to base tree
569
541
# we might also check the working tree.
692
661
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
694
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
697
self.requireFeature(features.SymlinkFeature)
663
def test_symlink_bundle(self):
664
self.requireFeature(SymlinkFeature)
698
665
self.tree1 = self.make_branch_and_tree('b1')
699
666
self.b1 = self.tree1.branch
701
667
tt = TreeTransform(self.tree1)
702
tt.new_symlink(link_name, tt.root, link_target, link_id)
668
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
704
670
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
705
bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
706
if getattr(bundle ,'revision_tree', None) is not None:
707
# Not all bundle formats supports revision_tree
708
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-1')
709
self.assertEqual(link_target, bund_tree.get_symlink_target(link_id))
671
self.get_valid_bundle('null:', 'l@cset-0-1')
711
672
tt = TreeTransform(self.tree1)
712
trans_id = tt.trans_id_tree_file_id(link_id)
673
trans_id = tt.trans_id_tree_file_id('link-1')
713
674
tt.adjust_path('link2', tt.root, trans_id)
714
675
tt.delete_contents(trans_id)
715
tt.create_symlink(new_link_target, trans_id)
676
tt.create_symlink('mars', trans_id)
717
678
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
718
bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
719
if getattr(bundle ,'revision_tree', None) is not None:
720
# Not all bundle formats supports revision_tree
721
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
722
self.assertEqual(new_link_target,
723
bund_tree.get_symlink_target(link_id))
679
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
725
680
tt = TreeTransform(self.tree1)
726
trans_id = tt.trans_id_tree_file_id(link_id)
681
trans_id = tt.trans_id_tree_file_id('link-1')
727
682
tt.delete_contents(trans_id)
728
683
tt.create_symlink('jupiter', trans_id)
730
685
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
731
bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
686
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
733
687
tt = TreeTransform(self.tree1)
734
trans_id = tt.trans_id_tree_file_id(link_id)
688
trans_id = tt.trans_id_tree_file_id('link-1')
735
689
tt.delete_contents(trans_id)
737
691
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
738
bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
740
def test_symlink_bundle(self):
741
self._test_symlink_bundle('link', 'bar/foo', 'mars')
743
def test_unicode_symlink_bundle(self):
744
self.requireFeature(features.UnicodeFilenameFeature)
745
self._test_symlink_bundle(u'\N{Euro Sign}link',
746
u'bar/\N{Euro Sign}foo',
747
u'mars\N{Euro Sign}')
692
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
749
694
def test_binary_bundle(self):
750
695
self.tree1 = self.make_branch_and_tree('b1')
751
696
self.b1 = self.tree1.branch
752
697
tt = TreeTransform(self.tree1)
755
700
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
756
701
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
861
808
u'William Dod\xe9\n').encode('utf-8'))
864
self.tree1.add([u'with Dod\N{Euro Sign}'], ['withdod-id'])
811
self.tree1.add([u'with Dod\xe9'], ['withdod-id'])
865
812
self.tree1.commit(u'i18n commit from William Dod\xe9',
866
813
rev_id='i18n-1', committer=u'William Dod\xe9')
815
if sys.platform == 'darwin':
816
from bzrlib.workingtree import WorkingTree3
817
if type(self.tree1) is WorkingTree3:
818
self.knownFailure("Bug #141438: fails for WorkingTree3 on OSX")
820
# On Mac the '\xe9' gets changed to 'e\u0301'
821
self.assertEqual([u'.bzr', u'with Dode\u0301'],
822
sorted(os.listdir(u'b1')))
823
delta = self.tree1.changes_from(self.tree1.basis_tree())
824
self.assertEqual([(u'with Dod\xe9', 'withdod-id', 'file')],
826
self.knownFailure("Mac OSX doesn't preserve unicode"
827
" combining characters.")
869
830
bundle = self.get_valid_bundle('null:', 'i18n-1')
872
f = open(u'b1/with Dod\N{Euro Sign}', 'wb')
833
f = open(u'b1/with Dod\xe9', 'wb')
873
834
f.write(u'Modified \xb5\n'.encode('utf8'))
875
836
self.tree1.commit(u'modified', rev_id='i18n-2')
877
838
bundle = self.get_valid_bundle('i18n-1', 'i18n-2')
880
self.tree1.rename_one(u'with Dod\N{Euro Sign}', u'B\N{Euro Sign}gfors')
841
self.tree1.rename_one(u'with Dod\xe9', u'B\xe5gfors')
881
842
self.tree1.commit(u'renamed, the new i18n man', rev_id='i18n-3',
882
843
committer=u'Erik B\xe5gfors')
884
845
bundle = self.get_valid_bundle('i18n-2', 'i18n-3')
887
self.tree1.remove([u'B\N{Euro Sign}gfors'])
848
self.tree1.remove([u'B\xe5gfors'])
888
849
self.tree1.commit(u'removed', rev_id='i18n-4')
890
851
bundle = self.get_valid_bundle('i18n-3', 'i18n-4')
1458
1414
install_bundle(repo_b, serializer.read(s))
1461
class V4_2aBundleTester(V4BundleTester):
1417
class V4WeaveBundleTester(V4BundleTester):
1463
1419
def bzrdir_format(self):
1466
def get_invalid_bundle(self, base_rev_id, rev_id):
1467
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1468
Munge the text so that it's invalid.
1470
:return: The in-memory bundle
1472
from bzrlib.bundle import serializer
1473
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1474
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1475
# We are going to be replacing some text to set the executable bit on a
1476
# file. Make sure the text replacement actually works correctly.
1477
self.assertContainsRe(new_text, '(?m)B244\n\ni 1\n<inventory')
1478
new_text = new_text.replace('<file file_id="exe-1"',
1479
'<file executable="y" file_id="exe-1"')
1480
new_text = new_text.replace('B244', 'B259')
1481
bundle_txt = StringIO()
1482
bundle_txt.write(serializer._get_bundle_header('4'))
1483
bundle_txt.write('\n')
1484
bundle_txt.write(new_text.encode('bz2'))
1486
bundle = read_bundle(bundle_txt)
1487
self.valid_apply_bundle(base_rev_id, bundle)
1490
def make_merged_branch(self):
1491
builder = self.make_branch_builder('source')
1492
builder.start_series()
1493
builder.build_snapshot('a@cset-0-1', None, [
1494
('add', ('', 'root-id', 'directory', None)),
1495
('add', ('file', 'file-id', 'file', 'original content\n')),
1497
builder.build_snapshot('a@cset-0-2a', ['a@cset-0-1'], [
1498
('modify', ('file-id', 'new-content\n')),
1500
builder.build_snapshot('a@cset-0-2b', ['a@cset-0-1'], [
1501
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1503
builder.build_snapshot('a@cset-0-3', ['a@cset-0-2a', 'a@cset-0-2b'], [
1504
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1506
builder.finish_series()
1507
self.b1 = builder.get_branch()
1509
self.addCleanup(self.b1.unlock)
1511
def make_bundle_just_inventories(self, base_revision_id,
1515
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1516
self.b1.repository, sio)
1517
writer.bundle.begin()
1518
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1523
def test_single_inventory_multiple_parents_as_xml(self):
1524
self.make_merged_branch()
1525
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1527
reader = v4.BundleReader(sio, stream_input=False)
1528
records = list(reader.iter_records())
1529
self.assertEqual(1, len(records))
1530
(bytes, metadata, repo_kind, revision_id,
1531
file_id) = records[0]
1532
self.assertIs(None, file_id)
1533
self.assertEqual('a@cset-0-3', revision_id)
1534
self.assertEqual('inventory', repo_kind)
1535
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1536
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1537
'storage_kind': 'mpdiff',
1539
# We should have an mpdiff that takes some lines from both parents.
1540
self.assertEqualDiff(
1542
'<inventory format="10" revision_id="a@cset-0-3">\n'
1545
'c 1 3 3 2\n', bytes)
1547
def test_single_inv_no_parents_as_xml(self):
1548
self.make_merged_branch()
1549
sio = self.make_bundle_just_inventories('null:', 'a@cset-0-1',
1551
reader = v4.BundleReader(sio, stream_input=False)
1552
records = list(reader.iter_records())
1553
self.assertEqual(1, len(records))
1554
(bytes, metadata, repo_kind, revision_id,
1555
file_id) = records[0]
1556
self.assertIs(None, file_id)
1557
self.assertEqual('a@cset-0-1', revision_id)
1558
self.assertEqual('inventory', repo_kind)
1559
self.assertEqual({'parents': [],
1560
'sha1': 'a13f42b142d544aac9b085c42595d304150e31a2',
1561
'storage_kind': 'mpdiff',
1563
# We should have an mpdiff that takes some lines from both parents.
1564
self.assertEqualDiff(
1566
'<inventory format="10" revision_id="a@cset-0-1">\n'
1567
'<directory file_id="root-id" name=""'
1568
' revision="a@cset-0-1" />\n'
1569
'<file file_id="file-id" name="file" parent_id="root-id"'
1570
' revision="a@cset-0-1"'
1571
' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1572
' text_size="17" />\n'
1576
def test_multiple_inventories_as_xml(self):
1577
self.make_merged_branch()
1578
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1579
['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'])
1580
reader = v4.BundleReader(sio, stream_input=False)
1581
records = list(reader.iter_records())
1582
self.assertEqual(3, len(records))
1583
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1584
self.assertEqual(['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'],
1586
metadata_2a = records[0][1]
1587
self.assertEqual({'parents': ['a@cset-0-1'],
1588
'sha1': '1e105886d62d510763e22885eec733b66f5f09bf',
1589
'storage_kind': 'mpdiff',
1591
metadata_2b = records[1][1]
1592
self.assertEqual({'parents': ['a@cset-0-1'],
1593
'sha1': 'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1594
'storage_kind': 'mpdiff',
1596
metadata_3 = records[2][1]
1597
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1598
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1599
'storage_kind': 'mpdiff',
1601
bytes_2a = records[0][0]
1602
self.assertEqualDiff(
1604
'<inventory format="10" revision_id="a@cset-0-2a">\n'
1608
'<file file_id="file-id" name="file" parent_id="root-id"'
1609
' revision="a@cset-0-2a"'
1610
' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1611
' text_size="12" />\n'
1613
'c 0 3 3 1\n', bytes_2a)
1614
bytes_2b = records[1][0]
1615
self.assertEqualDiff(
1617
'<inventory format="10" revision_id="a@cset-0-2b">\n'
1621
'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1622
' revision="a@cset-0-2b"'
1623
' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1624
' text_size="14" />\n'
1626
'c 0 3 4 1\n', bytes_2b)
1627
bytes_3 = records[2][0]
1628
self.assertEqualDiff(
1630
'<inventory format="10" revision_id="a@cset-0-3">\n'
1633
'c 1 3 3 2\n', bytes_3)
1635
def test_creating_bundle_preserves_chk_pages(self):
1636
self.make_merged_branch()
1637
target = self.b1.bzrdir.sprout('target',
1638
revision_id='a@cset-0-2a').open_branch()
1639
bundle_txt, rev_ids = self.create_bundle_text('a@cset-0-2a',
1641
self.assertEqual(['a@cset-0-2b', 'a@cset-0-3'], rev_ids)
1642
bundle = read_bundle(bundle_txt)
1644
self.addCleanup(target.unlock)
1645
install_bundle(target.repository, bundle)
1646
inv1 = self.b1.repository.inventories.get_record_stream([
1647
('a@cset-0-3',)], 'unordered',
1648
True).next().get_bytes_as('fulltext')
1649
inv2 = target.repository.inventories.get_record_stream([
1650
('a@cset-0-3',)], 'unordered',
1651
True).next().get_bytes_as('fulltext')
1652
self.assertEqualDiff(inv1, inv2)
1655
1423
class MungedBundleTester(object):
1830
1598
def look_up(self, name, url):
1831
1599
return 'source'
1832
1600
directories.register('foo:', FooService, 'Testing directory service')
1833
self.addCleanup(directories.remove, 'foo:')
1601
self.addCleanup(lambda: directories.remove('foo:'))
1834
1602
self.build_tree_contents([('./foo:bar', out.getvalue())])
1835
1603
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1838
def test_infinite_redirects_are_not_a_bundle(self):
1839
"""If a URL causes TooManyRedirections then NotABundle is raised.
1841
from bzrlib.tests.blackbox.test_push import RedirectingMemoryServer
1842
server = RedirectingMemoryServer()
1843
self.start_server(server)
1844
url = server.get_url() + 'infinite-loop'
1845
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1847
def test_smart_server_connection_reset(self):
1848
"""If a smart server connection fails during the attempt to read a
1849
bundle, then the ConnectionReset error should be propagated.
1851
# Instantiate a server that will provoke a ConnectionReset
1852
sock_server = DisconnectingServer()
1853
self.start_server(sock_server)
1854
# We don't really care what the url is since the server will close the
1855
# connection without interpreting it
1856
url = sock_server.get_url()
1857
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
1860
class DisconnectingHandler(SocketServer.BaseRequestHandler):
1861
"""A request handler that immediately closes any connection made to it."""
1864
self.request.close()
1867
class DisconnectingServer(test_server.TestingTCPServerInAThread):
1870
super(DisconnectingServer, self).__init__(
1872
test_server.TestingTCPServer,
1873
DisconnectingHandler)
1876
"""Return the url of the server"""
1877
return "bzr://%s:%d/" % self.server.server_address