23
23
from bzrlib import (
29
31
revision as _mod_revision,
32
from bzrlib.bzrdir import BzrDir
33
35
from bzrlib.bundle import read_mergeable_from_url
34
36
from bzrlib.bundle.apply_bundle import install_bundle, merge_bundle
35
37
from bzrlib.bundle.bundle_data import BundleTree
38
from bzrlib.bzrdir import BzrDir
36
39
from bzrlib.directory_service import directories
37
40
from bzrlib.bundle.serializer import write_bundle, read_bundle, v09, v4
38
41
from bzrlib.bundle.serializer.v08 import BundleSerializerV08
39
42
from bzrlib.bundle.serializer.v09 import BundleSerializerV09
40
43
from bzrlib.bundle.serializer.v4 import BundleSerializerV4
41
44
from bzrlib.branch import Branch
42
from bzrlib.diff import internal_diff
43
from bzrlib.merge import Merge3Merger
44
45
from bzrlib.repofmt import knitrepo
45
from bzrlib.osutils import sha_file, sha_string
46
46
from bzrlib.tests import (
50
TestCaseWithTransport,
55
50
from bzrlib.transform import TreeTransform
53
def get_text(vf, key):
54
"""Get the fulltext for a given revision id that is present in the vf"""
55
stream = vf.get_record_stream([key], 'unordered', True)
56
record = stream.next()
57
return record.get_bytes_as('fulltext')
60
def get_inventory_text(repo, revision_id):
61
"""Get the fulltext for the inventory at revision id"""
64
return get_text(repo.inventories, (revision_id,))
58
69
class MockTree(object):
59
70
def __init__(self):
60
71
from bzrlib.inventory import InventoryDirectory, ROOT_ID
662
677
bundle = self.get_valid_bundle('a@cset-0-6', 'a@cset-0-7')
664
def test_symlink_bundle(self):
665
self.requireFeature(SymlinkFeature)
679
def _test_symlink_bundle(self, link_name, link_target, new_link_target):
682
self.requireFeature(tests.SymlinkFeature)
666
683
self.tree1 = self.make_branch_and_tree('b1')
667
684
self.b1 = self.tree1.branch
668
686
tt = TreeTransform(self.tree1)
669
tt.new_symlink('link', tt.root, 'bar/foo', 'link-1')
687
tt.new_symlink(link_name, tt.root, link_target, link_id)
671
689
self.tree1.commit('add symlink', rev_id='l@cset-0-1')
672
self.get_valid_bundle('null:', 'l@cset-0-1')
690
bundle = self.get_valid_bundle('null:', 'l@cset-0-1')
691
if getattr(bundle ,'revision_tree', None) is not None:
692
# Not all bundle formats supports revision_tree
693
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-1')
694
self.assertEqual(link_target, bund_tree.get_symlink_target(link_id))
673
696
tt = TreeTransform(self.tree1)
674
trans_id = tt.trans_id_tree_file_id('link-1')
697
trans_id = tt.trans_id_tree_file_id(link_id)
675
698
tt.adjust_path('link2', tt.root, trans_id)
676
699
tt.delete_contents(trans_id)
677
tt.create_symlink('mars', trans_id)
700
tt.create_symlink(new_link_target, trans_id)
679
702
self.tree1.commit('rename and change symlink', rev_id='l@cset-0-2')
680
self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
703
bundle = self.get_valid_bundle('l@cset-0-1', 'l@cset-0-2')
704
if getattr(bundle ,'revision_tree', None) is not None:
705
# Not all bundle formats supports revision_tree
706
bund_tree = bundle.revision_tree(self.b1.repository, 'l@cset-0-2')
707
self.assertEqual(new_link_target,
708
bund_tree.get_symlink_target(link_id))
681
710
tt = TreeTransform(self.tree1)
682
trans_id = tt.trans_id_tree_file_id('link-1')
711
trans_id = tt.trans_id_tree_file_id(link_id)
683
712
tt.delete_contents(trans_id)
684
713
tt.create_symlink('jupiter', trans_id)
686
715
self.tree1.commit('just change symlink target', rev_id='l@cset-0-3')
687
self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
716
bundle = self.get_valid_bundle('l@cset-0-2', 'l@cset-0-3')
688
718
tt = TreeTransform(self.tree1)
689
trans_id = tt.trans_id_tree_file_id('link-1')
719
trans_id = tt.trans_id_tree_file_id(link_id)
690
720
tt.delete_contents(trans_id)
692
722
self.tree1.commit('Delete symlink', rev_id='l@cset-0-4')
693
self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
723
bundle = self.get_valid_bundle('l@cset-0-3', 'l@cset-0-4')
725
def test_symlink_bundle(self):
726
self._test_symlink_bundle('link', 'bar/foo', 'mars')
728
def test_unicode_symlink_bundle(self):
729
self.requireFeature(tests.UnicodeFilenameFeature)
730
self._test_symlink_bundle(u'\N{Euro Sign}link',
731
u'bar/\N{Euro Sign}foo',
732
u'mars\N{Euro Sign}')
695
734
def test_binary_bundle(self):
696
735
self.tree1 = self.make_branch_and_tree('b1')
697
736
self.b1 = self.tree1.branch
698
737
tt = TreeTransform(self.tree1)
701
740
tt.new_file('file', tt.root, '\x00\n\x00\r\x01\n\x02\r\xff', 'binary-1')
702
741
tt.new_file('file2', tt.root, '\x01\n\x02\r\x03\n\x04\r\xff',
1407
1448
return 'metaweave'
1451
class V4_2aBundleTester(V4BundleTester):
1453
def bzrdir_format(self):
1456
def get_invalid_bundle(self, base_rev_id, rev_id):
1457
"""Create a bundle from base_rev_id -> rev_id in built-in branch.
1458
Munge the text so that it's invalid.
1460
:return: The in-memory bundle
1462
from bzrlib.bundle import serializer
1463
bundle_txt, rev_ids = self.create_bundle_text(base_rev_id, rev_id)
1464
new_text = self.get_raw(StringIO(''.join(bundle_txt)))
1465
# We are going to be replacing some text to set the executable bit on a
1466
# file. Make sure the text replacement actually works correctly.
1467
self.assertContainsRe(new_text, '(?m)B244\n\ni 1\n<inventory')
1468
new_text = new_text.replace('<file file_id="exe-1"',
1469
'<file executable="y" file_id="exe-1"')
1470
new_text = new_text.replace('B244', 'B259')
1471
bundle_txt = StringIO()
1472
bundle_txt.write(serializer._get_bundle_header('4'))
1473
bundle_txt.write('\n')
1474
bundle_txt.write(new_text.encode('bz2'))
1476
bundle = read_bundle(bundle_txt)
1477
self.valid_apply_bundle(base_rev_id, bundle)
1480
def make_merged_branch(self):
1481
builder = self.make_branch_builder('source')
1482
builder.start_series()
1483
builder.build_snapshot('a@cset-0-1', None, [
1484
('add', ('', 'root-id', 'directory', None)),
1485
('add', ('file', 'file-id', 'file', 'original content\n')),
1487
builder.build_snapshot('a@cset-0-2a', ['a@cset-0-1'], [
1488
('modify', ('file-id', 'new-content\n')),
1490
builder.build_snapshot('a@cset-0-2b', ['a@cset-0-1'], [
1491
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1493
builder.build_snapshot('a@cset-0-3', ['a@cset-0-2a', 'a@cset-0-2b'], [
1494
('add', ('other-file', 'file2-id', 'file', 'file2-content\n')),
1496
builder.finish_series()
1497
self.b1 = builder.get_branch()
1499
self.addCleanup(self.b1.unlock)
1501
def make_bundle_just_inventories(self, base_revision_id,
1505
writer = v4.BundleWriteOperation(base_revision_id, target_revision_id,
1506
self.b1.repository, sio)
1507
writer.bundle.begin()
1508
writer._add_inventory_mpdiffs_from_serializer(revision_ids)
1513
def test_single_inventory_multiple_parents_as_xml(self):
1514
self.make_merged_branch()
1515
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1517
reader = v4.BundleReader(sio, stream_input=False)
1518
records = list(reader.iter_records())
1519
self.assertEqual(1, len(records))
1520
(bytes, metadata, repo_kind, revision_id,
1521
file_id) = records[0]
1522
self.assertIs(None, file_id)
1523
self.assertEqual('a@cset-0-3', revision_id)
1524
self.assertEqual('inventory', repo_kind)
1525
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1526
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1527
'storage_kind': 'mpdiff',
1529
# We should have an mpdiff that takes some lines from both parents.
1530
self.assertEqualDiff(
1532
'<inventory format="10" revision_id="a@cset-0-3">\n'
1535
'c 1 3 3 2\n', bytes)
1537
def test_single_inv_no_parents_as_xml(self):
1538
self.make_merged_branch()
1539
sio = self.make_bundle_just_inventories('null:', 'a@cset-0-1',
1541
reader = v4.BundleReader(sio, stream_input=False)
1542
records = list(reader.iter_records())
1543
self.assertEqual(1, len(records))
1544
(bytes, metadata, repo_kind, revision_id,
1545
file_id) = records[0]
1546
self.assertIs(None, file_id)
1547
self.assertEqual('a@cset-0-1', revision_id)
1548
self.assertEqual('inventory', repo_kind)
1549
self.assertEqual({'parents': [],
1550
'sha1': 'a13f42b142d544aac9b085c42595d304150e31a2',
1551
'storage_kind': 'mpdiff',
1553
# We should have an mpdiff that takes some lines from both parents.
1554
self.assertEqualDiff(
1556
'<inventory format="10" revision_id="a@cset-0-1">\n'
1557
'<directory file_id="root-id" name=""'
1558
' revision="a@cset-0-1" />\n'
1559
'<file file_id="file-id" name="file" parent_id="root-id"'
1560
' revision="a@cset-0-1"'
1561
' text_sha1="09c2f8647e14e49e922b955c194102070597c2d1"'
1562
' text_size="17" />\n'
1566
def test_multiple_inventories_as_xml(self):
1567
self.make_merged_branch()
1568
sio = self.make_bundle_just_inventories('a@cset-0-1', 'a@cset-0-3',
1569
['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'])
1570
reader = v4.BundleReader(sio, stream_input=False)
1571
records = list(reader.iter_records())
1572
self.assertEqual(3, len(records))
1573
revision_ids = [rev_id for b, m, k, rev_id, f in records]
1574
self.assertEqual(['a@cset-0-2a', 'a@cset-0-2b', 'a@cset-0-3'],
1576
metadata_2a = records[0][1]
1577
self.assertEqual({'parents': ['a@cset-0-1'],
1578
'sha1': '1e105886d62d510763e22885eec733b66f5f09bf',
1579
'storage_kind': 'mpdiff',
1581
metadata_2b = records[1][1]
1582
self.assertEqual({'parents': ['a@cset-0-1'],
1583
'sha1': 'f03f12574bdb5ed2204c28636c98a8547544ccd8',
1584
'storage_kind': 'mpdiff',
1586
metadata_3 = records[2][1]
1587
self.assertEqual({'parents': ['a@cset-0-2a', 'a@cset-0-2b'],
1588
'sha1': '09c53b0c4de0895e11a2aacc34fef60a6e70865c',
1589
'storage_kind': 'mpdiff',
1591
bytes_2a = records[0][0]
1592
self.assertEqualDiff(
1594
'<inventory format="10" revision_id="a@cset-0-2a">\n'
1598
'<file file_id="file-id" name="file" parent_id="root-id"'
1599
' revision="a@cset-0-2a"'
1600
' text_sha1="50f545ff40e57b6924b1f3174b267ffc4576e9a9"'
1601
' text_size="12" />\n'
1603
'c 0 3 3 1\n', bytes_2a)
1604
bytes_2b = records[1][0]
1605
self.assertEqualDiff(
1607
'<inventory format="10" revision_id="a@cset-0-2b">\n'
1611
'<file file_id="file2-id" name="other-file" parent_id="root-id"'
1612
' revision="a@cset-0-2b"'
1613
' text_sha1="b46c0c8ea1e5ef8e46fc8894bfd4752a88ec939e"'
1614
' text_size="14" />\n'
1616
'c 0 3 4 1\n', bytes_2b)
1617
bytes_3 = records[2][0]
1618
self.assertEqualDiff(
1620
'<inventory format="10" revision_id="a@cset-0-3">\n'
1623
'c 1 3 3 2\n', bytes_3)
1625
def test_creating_bundle_preserves_chk_pages(self):
1626
self.make_merged_branch()
1627
target = self.b1.bzrdir.sprout('target',
1628
revision_id='a@cset-0-2a').open_branch()
1629
bundle_txt, rev_ids = self.create_bundle_text('a@cset-0-2a',
1631
self.assertEqual(['a@cset-0-2b', 'a@cset-0-3'], rev_ids)
1632
bundle = read_bundle(bundle_txt)
1634
self.addCleanup(target.unlock)
1635
install_bundle(target.repository, bundle)
1636
inv1 = self.b1.repository.inventories.get_record_stream([
1637
('a@cset-0-3',)], 'unordered',
1638
True).next().get_bytes_as('fulltext')
1639
inv2 = target.repository.inventories.get_record_stream([
1640
('a@cset-0-3',)], 'unordered',
1641
True).next().get_bytes_as('fulltext')
1642
self.assertEqualDiff(inv1, inv2)
1410
1645
class MungedBundleTester(object):
1412
1647
def build_test_bundle(self):
1585
1820
def look_up(self, name, url):
1586
1821
return 'source'
1587
1822
directories.register('foo:', FooService, 'Testing directory service')
1588
self.addCleanup(lambda: directories.remove('foo:'))
1823
self.addCleanup(directories.remove, 'foo:')
1589
1824
self.build_tree_contents([('./foo:bar', out.getvalue())])
1590
1825
self.assertRaises(errors.NotABundle, read_mergeable_from_url,
1828
def test_infinite_redirects_are_not_a_bundle(self):
1829
"""If a URL causes TooManyRedirections then NotABundle is raised.
1831
from bzrlib.tests.blackbox.test_push import RedirectingMemoryServer
1832
server = RedirectingMemoryServer()
1833
self.start_server(server)
1834
url = server.get_url() + 'infinite-loop'
1835
self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
1593
1837
def test_smart_server_connection_reset(self):
1594
1838
"""If a smart server connection fails during the attempt to read a
1595
1839
bundle, then the ConnectionReset error should be propagated.
1597
1841
# Instantiate a server that will provoke a ConnectionReset
1598
1842
sock_server = _DisconnectingTCPServer()
1600
self.addCleanup(sock_server.tearDown)
1843
self.start_server(sock_server)
1844
# We don't really care what the url is since the server will close the
1845
# connection without interpreting it
1601
1846
url = sock_server.get_url()
1602
1847
self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)