1976
1719
self.assertEqual([], list(target.iter_changes(revision_tree)))
1977
1720
self.assertTrue(source.is_executable('file1-id'))
1979
def install_rot13_content_filter(self, pattern):
1981
# self.addCleanup(filters._reset_registry, filters._reset_registry())
1982
# below, but that looks a bit... hard to read even if it's exactly
1984
original_registry = filters._reset_registry()
1985
def restore_registry():
1986
filters._reset_registry(original_registry)
1987
self.addCleanup(restore_registry)
1988
def rot13(chunks, context=None):
1989
return [''.join(chunks).encode('rot13')]
1990
rot13filter = filters.ContentFilter(rot13, rot13)
1991
filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
1992
os.mkdir(self.test_home_dir + '/.bazaar')
1993
rules_filename = self.test_home_dir + '/.bazaar/rules'
1994
f = open(rules_filename, 'wb')
1995
f.write('[name %s]\nrot13=yes\n' % (pattern,))
1997
def uninstall_rules():
1998
os.remove(rules_filename)
2000
self.addCleanup(uninstall_rules)
2003
def test_build_tree_content_filtered_files_are_not_hardlinked(self):
2004
"""build_tree will not hardlink files that have content filtering rules
2005
applied to them (but will still hardlink other files from the same tree
2008
self.requireFeature(HardlinkFeature)
2009
self.install_rot13_content_filter('file1')
2010
source = self.create_ab_tree()
2011
target = self.make_branch_and_tree('target')
2012
revision_tree = source.basis_tree()
2013
revision_tree.lock_read()
2014
self.addCleanup(revision_tree.unlock)
2015
build_tree(revision_tree, target, source, hardlink=True)
2017
self.addCleanup(target.unlock)
2018
self.assertEqual([], list(target.iter_changes(revision_tree)))
2019
source_stat = os.stat('source/file1')
2020
target_stat = os.stat('target/file1')
2021
self.assertNotEqual(source_stat, target_stat)
2022
source_stat = os.stat('source/file2')
2023
target_stat = os.stat('target/file2')
2024
self.assertEqualStat(source_stat, target_stat)
2026
def test_case_insensitive_build_tree_inventory(self):
2027
if (tests.CaseInsensitiveFilesystemFeature.available()
2028
or tests.CaseInsCasePresFilenameFeature.available()):
2029
raise tests.UnavailableFeature('Fully case sensitive filesystem')
2030
source = self.make_branch_and_tree('source')
2031
self.build_tree(['source/file', 'source/FILE'])
2032
source.add(['file', 'FILE'], ['lower-id', 'upper-id'])
2033
source.commit('added files')
2034
# Don't try this at home, kids!
2035
# Force the tree to report that it is case insensitive
2036
target = self.make_branch_and_tree('target')
2037
target.case_sensitive = False
2038
build_tree(source.basis_tree(), target, source, delta_from_tree=True)
2039
self.assertEqual('file.moved', target.id2path('lower-id'))
2040
self.assertEqual('FILE', target.id2path('upper-id'))
2043
class TestCommitTransform(tests.TestCaseWithTransport):
2045
def get_branch(self):
2046
tree = self.make_branch_and_tree('tree')
2048
self.addCleanup(tree.unlock)
2049
tree.commit('empty commit')
2052
def get_branch_and_transform(self):
2053
branch = self.get_branch()
2054
tt = TransformPreview(branch.basis_tree())
2055
self.addCleanup(tt.finalize)
2058
def test_commit_wrong_basis(self):
2059
branch = self.get_branch()
2060
basis = branch.repository.revision_tree(
2061
_mod_revision.NULL_REVISION)
2062
tt = TransformPreview(basis)
2063
self.addCleanup(tt.finalize)
2064
e = self.assertRaises(ValueError, tt.commit, branch, '')
2065
self.assertEqual('TreeTransform not based on branch basis: null:',
2068
def test_empy_commit(self):
2069
branch, tt = self.get_branch_and_transform()
2070
rev = tt.commit(branch, 'my message')
2071
self.assertEqual(2, branch.revno())
2072
repo = branch.repository
2073
self.assertEqual('my message', repo.get_revision(rev).message)
2075
def test_merge_parents(self):
2076
branch, tt = self.get_branch_and_transform()
2077
rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
2078
self.assertEqual(['rev1b', 'rev1c'],
2079
branch.basis_tree().get_parent_ids()[1:])
2081
def test_first_commit(self):
2082
branch = self.make_branch('branch')
2084
self.addCleanup(branch.unlock)
2085
tt = TransformPreview(branch.basis_tree())
2086
self.addCleanup(tt.finalize)
2087
tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
2088
rev = tt.commit(branch, 'my message')
2089
self.assertEqual([], branch.basis_tree().get_parent_ids())
2090
self.assertNotEqual(_mod_revision.NULL_REVISION,
2091
branch.last_revision())
2093
def test_first_commit_with_merge_parents(self):
2094
branch = self.make_branch('branch')
2096
self.addCleanup(branch.unlock)
2097
tt = TransformPreview(branch.basis_tree())
2098
self.addCleanup(tt.finalize)
2099
e = self.assertRaises(ValueError, tt.commit, branch,
2100
'my message', ['rev1b-id'])
2101
self.assertEqual('Cannot supply merge parents for first commit.',
2103
self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
2105
def test_add_files(self):
2106
branch, tt = self.get_branch_and_transform()
2107
tt.new_file('file', tt.root, 'contents', 'file-id')
2108
trans_id = tt.new_directory('dir', tt.root, 'dir-id')
2109
if SymlinkFeature.available():
2110
tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
2111
rev = tt.commit(branch, 'message')
2112
tree = branch.basis_tree()
2113
self.assertEqual('file', tree.id2path('file-id'))
2114
self.assertEqual('contents', tree.get_file_text('file-id'))
2115
self.assertEqual('dir', tree.id2path('dir-id'))
2116
if SymlinkFeature.available():
2117
self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2118
self.assertEqual('target', tree.get_symlink_target('symlink-id'))
2120
def test_add_unversioned(self):
2121
branch, tt = self.get_branch_and_transform()
2122
tt.new_file('file', tt.root, 'contents')
2123
self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
2124
'message', strict=True)
2126
def test_modify_strict(self):
2127
branch, tt = self.get_branch_and_transform()
2128
tt.new_file('file', tt.root, 'contents', 'file-id')
2129
tt.commit(branch, 'message', strict=True)
2130
tt = TransformPreview(branch.basis_tree())
2131
self.addCleanup(tt.finalize)
2132
trans_id = tt.trans_id_file_id('file-id')
2133
tt.delete_contents(trans_id)
2134
tt.create_file('contents', trans_id)
2135
tt.commit(branch, 'message', strict=True)
2137
def test_commit_malformed(self):
2138
"""Committing a malformed transform should raise an exception.
2140
In this case, we are adding a file without adding its parent.
2142
branch, tt = self.get_branch_and_transform()
2143
parent_id = tt.trans_id_file_id('parent-id')
2144
tt.new_file('file', parent_id, 'contents', 'file-id')
2145
self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2148
def test_commit_rich_revision_data(self):
2149
branch, tt = self.get_branch_and_transform()
2150
rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2151
committer='me <me@example.com>',
2152
revprops={'foo': 'bar'}, revision_id='revid-1',
2153
authors=['Author1 <author1@example.com>',
2154
'Author2 <author2@example.com>',
2156
self.assertEqual('revid-1', rev_id)
2157
revision = branch.repository.get_revision(rev_id)
2158
self.assertEqual(1, revision.timestamp)
2159
self.assertEqual(43201, revision.timezone)
2160
self.assertEqual('me <me@example.com>', revision.committer)
2161
self.assertEqual(['Author1 <author1@example.com>',
2162
'Author2 <author2@example.com>'],
2163
revision.get_apparent_authors())
2164
del revision.properties['authors']
2165
self.assertEqual({'foo': 'bar',
2166
'branch-nick': 'tree'},
2167
revision.properties)
2169
def test_no_explicit_revprops(self):
2170
branch, tt = self.get_branch_and_transform()
2171
rev_id = tt.commit(branch, 'message', authors=[
2172
'Author1 <author1@example.com>',
2173
'Author2 <author2@example.com>', ])
2174
revision = branch.repository.get_revision(rev_id)
2175
self.assertEqual(['Author1 <author1@example.com>',
2176
'Author2 <author2@example.com>'],
2177
revision.get_apparent_authors())
2178
self.assertEqual('tree', revision.properties['branch-nick'])
2181
1723
class MockTransform(object):
2507
2033
preview_tree = preview.get_preview_tree()
2508
2034
self.assertEqual('target',
2509
2035
preview_tree.get_symlink_target('symlink-id'))
2511
def test_all_file_ids(self):
2512
tree = self.make_branch_and_tree('tree')
2513
self.build_tree(['tree/a', 'tree/b', 'tree/c'])
2514
tree.add(['a', 'b', 'c'], ['a-id', 'b-id', 'c-id'])
2515
preview = TransformPreview(tree)
2516
self.addCleanup(preview.finalize)
2517
preview.unversion_file(preview.trans_id_file_id('b-id'))
2518
c_trans_id = preview.trans_id_file_id('c-id')
2519
preview.unversion_file(c_trans_id)
2520
preview.version_file('c-id', c_trans_id)
2521
preview_tree = preview.get_preview_tree()
2522
self.assertEqual(set(['a-id', 'c-id', tree.get_root_id()]),
2523
preview_tree.all_file_ids())
2525
def test_path2id_deleted_unchanged(self):
2526
tree = self.make_branch_and_tree('tree')
2527
self.build_tree(['tree/unchanged', 'tree/deleted'])
2528
tree.add(['unchanged', 'deleted'], ['unchanged-id', 'deleted-id'])
2529
preview = TransformPreview(tree)
2530
self.addCleanup(preview.finalize)
2531
preview.unversion_file(preview.trans_id_file_id('deleted-id'))
2532
preview_tree = preview.get_preview_tree()
2533
self.assertEqual('unchanged-id', preview_tree.path2id('unchanged'))
2534
self.assertIs(None, preview_tree.path2id('deleted'))
2536
def test_path2id_created(self):
2537
tree = self.make_branch_and_tree('tree')
2538
self.build_tree(['tree/unchanged'])
2539
tree.add(['unchanged'], ['unchanged-id'])
2540
preview = TransformPreview(tree)
2541
self.addCleanup(preview.finalize)
2542
preview.new_file('new', preview.trans_id_file_id('unchanged-id'),
2543
'contents', 'new-id')
2544
preview_tree = preview.get_preview_tree()
2545
self.assertEqual('new-id', preview_tree.path2id('unchanged/new'))
2547
def test_path2id_moved(self):
2548
tree = self.make_branch_and_tree('tree')
2549
self.build_tree(['tree/old_parent/', 'tree/old_parent/child'])
2550
tree.add(['old_parent', 'old_parent/child'],
2551
['old_parent-id', 'child-id'])
2552
preview = TransformPreview(tree)
2553
self.addCleanup(preview.finalize)
2554
new_parent = preview.new_directory('new_parent', preview.root,
2556
preview.adjust_path('child', new_parent,
2557
preview.trans_id_file_id('child-id'))
2558
preview_tree = preview.get_preview_tree()
2559
self.assertIs(None, preview_tree.path2id('old_parent/child'))
2560
self.assertEqual('child-id', preview_tree.path2id('new_parent/child'))
2562
def test_path2id_renamed_parent(self):
2563
tree = self.make_branch_and_tree('tree')
2564
self.build_tree(['tree/old_name/', 'tree/old_name/child'])
2565
tree.add(['old_name', 'old_name/child'],
2566
['parent-id', 'child-id'])
2567
preview = TransformPreview(tree)
2568
self.addCleanup(preview.finalize)
2569
preview.adjust_path('new_name', preview.root,
2570
preview.trans_id_file_id('parent-id'))
2571
preview_tree = preview.get_preview_tree()
2572
self.assertIs(None, preview_tree.path2id('old_name/child'))
2573
self.assertEqual('child-id', preview_tree.path2id('new_name/child'))
2575
def assertMatchingIterEntries(self, tt, specific_file_ids=None):
2576
preview_tree = tt.get_preview_tree()
2577
preview_result = list(preview_tree.iter_entries_by_dir(
2581
actual_result = list(tree.iter_entries_by_dir(specific_file_ids))
2582
self.assertEqual(actual_result, preview_result)
2584
def test_iter_entries_by_dir_new(self):
2585
tree = self.make_branch_and_tree('tree')
2586
tt = TreeTransform(tree)
2587
tt.new_file('new', tt.root, 'contents', 'new-id')
2588
self.assertMatchingIterEntries(tt)
2590
def test_iter_entries_by_dir_deleted(self):
2591
tree = self.make_branch_and_tree('tree')
2592
self.build_tree(['tree/deleted'])
2593
tree.add('deleted', 'deleted-id')
2594
tt = TreeTransform(tree)
2595
tt.delete_contents(tt.trans_id_file_id('deleted-id'))
2596
self.assertMatchingIterEntries(tt)
2598
def test_iter_entries_by_dir_unversioned(self):
2599
tree = self.make_branch_and_tree('tree')
2600
self.build_tree(['tree/removed'])
2601
tree.add('removed', 'removed-id')
2602
tt = TreeTransform(tree)
2603
tt.unversion_file(tt.trans_id_file_id('removed-id'))
2604
self.assertMatchingIterEntries(tt)
2606
def test_iter_entries_by_dir_moved(self):
2607
tree = self.make_branch_and_tree('tree')
2608
self.build_tree(['tree/moved', 'tree/new_parent/'])
2609
tree.add(['moved', 'new_parent'], ['moved-id', 'new_parent-id'])
2610
tt = TreeTransform(tree)
2611
tt.adjust_path('moved', tt.trans_id_file_id('new_parent-id'),
2612
tt.trans_id_file_id('moved-id'))
2613
self.assertMatchingIterEntries(tt)
2615
def test_iter_entries_by_dir_specific_file_ids(self):
2616
tree = self.make_branch_and_tree('tree')
2617
tree.set_root_id('tree-root-id')
2618
self.build_tree(['tree/parent/', 'tree/parent/child'])
2619
tree.add(['parent', 'parent/child'], ['parent-id', 'child-id'])
2620
tt = TreeTransform(tree)
2621
self.assertMatchingIterEntries(tt, ['tree-root-id', 'child-id'])
2623
def test_symlink_content_summary(self):
2624
self.requireFeature(SymlinkFeature)
2625
preview = self.get_empty_preview()
2626
preview.new_symlink('path', preview.root, 'target', 'path-id')
2627
summary = preview.get_preview_tree().path_content_summary('path')
2628
self.assertEqual(('symlink', None, None, 'target'), summary)
2630
def test_missing_content_summary(self):
2631
preview = self.get_empty_preview()
2632
summary = preview.get_preview_tree().path_content_summary('path')
2633
self.assertEqual(('missing', None, None, None), summary)
2635
def test_deleted_content_summary(self):
2636
tree = self.make_branch_and_tree('tree')
2637
self.build_tree(['tree/path/'])
2639
preview = TransformPreview(tree)
2640
self.addCleanup(preview.finalize)
2641
preview.delete_contents(preview.trans_id_tree_path('path'))
2642
summary = preview.get_preview_tree().path_content_summary('path')
2643
self.assertEqual(('missing', None, None, None), summary)
2645
def test_file_content_summary_executable(self):
2646
preview = self.get_empty_preview()
2647
path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
2648
preview.set_executability(True, path_id)
2649
summary = preview.get_preview_tree().path_content_summary('path')
2650
self.assertEqual(4, len(summary))
2651
self.assertEqual('file', summary[0])
2652
# size must be known
2653
self.assertEqual(len('contents'), summary[1])
2655
self.assertEqual(True, summary[2])
2656
# will not have hash (not cheap to determine)
2657
self.assertIs(None, summary[3])
2659
def test_change_executability(self):
2660
tree = self.make_branch_and_tree('tree')
2661
self.build_tree(['tree/path'])
2663
preview = TransformPreview(tree)
2664
self.addCleanup(preview.finalize)
2665
path_id = preview.trans_id_tree_path('path')
2666
preview.set_executability(True, path_id)
2667
summary = preview.get_preview_tree().path_content_summary('path')
2668
self.assertEqual(True, summary[2])
2670
def test_file_content_summary_non_exec(self):
2671
preview = self.get_empty_preview()
2672
preview.new_file('path', preview.root, 'contents', 'path-id')
2673
summary = preview.get_preview_tree().path_content_summary('path')
2674
self.assertEqual(4, len(summary))
2675
self.assertEqual('file', summary[0])
2676
# size must be known
2677
self.assertEqual(len('contents'), summary[1])
2679
self.assertEqual(False, summary[2])
2680
# will not have hash (not cheap to determine)
2681
self.assertIs(None, summary[3])
2683
def test_dir_content_summary(self):
2684
preview = self.get_empty_preview()
2685
preview.new_directory('path', preview.root, 'path-id')
2686
summary = preview.get_preview_tree().path_content_summary('path')
2687
self.assertEqual(('directory', None, None, None), summary)
2689
def test_tree_content_summary(self):
2690
preview = self.get_empty_preview()
2691
path = preview.new_directory('path', preview.root, 'path-id')
2692
preview.set_tree_reference('rev-1', path)
2693
summary = preview.get_preview_tree().path_content_summary('path')
2694
self.assertEqual(4, len(summary))
2695
self.assertEqual('tree-reference', summary[0])
2697
def test_annotate(self):
2698
tree = self.make_branch_and_tree('tree')
2699
self.build_tree_contents([('tree/file', 'a\n')])
2700
tree.add('file', 'file-id')
2701
tree.commit('a', rev_id='one')
2702
self.build_tree_contents([('tree/file', 'a\nb\n')])
2703
preview = TransformPreview(tree)
2704
self.addCleanup(preview.finalize)
2705
file_trans_id = preview.trans_id_file_id('file-id')
2706
preview.delete_contents(file_trans_id)
2707
preview.create_file('a\nb\nc\n', file_trans_id)
2708
preview_tree = preview.get_preview_tree()
2714
annotation = preview_tree.annotate_iter('file-id', 'me:')
2715
self.assertEqual(expected, annotation)
2717
def test_annotate_missing(self):
2718
preview = self.get_empty_preview()
2719
preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2720
preview_tree = preview.get_preview_tree()
2726
annotation = preview_tree.annotate_iter('file-id', 'me:')
2727
self.assertEqual(expected, annotation)
2729
def test_annotate_rename(self):
2730
tree = self.make_branch_and_tree('tree')
2731
self.build_tree_contents([('tree/file', 'a\n')])
2732
tree.add('file', 'file-id')
2733
tree.commit('a', rev_id='one')
2734
preview = TransformPreview(tree)
2735
self.addCleanup(preview.finalize)
2736
file_trans_id = preview.trans_id_file_id('file-id')
2737
preview.adjust_path('newname', preview.root, file_trans_id)
2738
preview_tree = preview.get_preview_tree()
2742
annotation = preview_tree.annotate_iter('file-id', 'me:')
2743
self.assertEqual(expected, annotation)
2745
def test_annotate_deleted(self):
2746
tree = self.make_branch_and_tree('tree')
2747
self.build_tree_contents([('tree/file', 'a\n')])
2748
tree.add('file', 'file-id')
2749
tree.commit('a', rev_id='one')
2750
self.build_tree_contents([('tree/file', 'a\nb\n')])
2751
preview = TransformPreview(tree)
2752
self.addCleanup(preview.finalize)
2753
file_trans_id = preview.trans_id_file_id('file-id')
2754
preview.delete_contents(file_trans_id)
2755
preview_tree = preview.get_preview_tree()
2756
annotation = preview_tree.annotate_iter('file-id', 'me:')
2757
self.assertIs(None, annotation)
2759
def test_stored_kind(self):
2760
preview = self.get_empty_preview()
2761
preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2762
preview_tree = preview.get_preview_tree()
2763
self.assertEqual('file', preview_tree.stored_kind('file-id'))
2765
def test_is_executable(self):
2766
preview = self.get_empty_preview()
2767
preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2768
preview.set_executability(True, preview.trans_id_file_id('file-id'))
2769
preview_tree = preview.get_preview_tree()
2770
self.assertEqual(True, preview_tree.is_executable('file-id'))
2772
def test_get_set_parent_ids(self):
2773
revision_tree, preview_tree = self.get_tree_and_preview_tree()
2774
self.assertEqual([], preview_tree.get_parent_ids())
2775
preview_tree.set_parent_ids(['rev-1'])
2776
self.assertEqual(['rev-1'], preview_tree.get_parent_ids())
2778
def test_plan_file_merge(self):
2779
work_a = self.make_branch_and_tree('wta')
2780
self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
2781
work_a.add('file', 'file-id')
2782
base_id = work_a.commit('base version')
2783
tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
2784
preview = TransformPreview(work_a)
2785
self.addCleanup(preview.finalize)
2786
trans_id = preview.trans_id_file_id('file-id')
2787
preview.delete_contents(trans_id)
2788
preview.create_file('b\nc\nd\ne\n', trans_id)
2789
self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
2790
tree_a = preview.get_preview_tree()
2791
tree_a.set_parent_ids([base_id])
2793
('killed-a', 'a\n'),
2794
('killed-b', 'b\n'),
2795
('unchanged', 'c\n'),
2796
('unchanged', 'd\n'),
2799
], list(tree_a.plan_file_merge('file-id', tree_b)))
2801
def test_plan_file_merge_revision_tree(self):
2802
work_a = self.make_branch_and_tree('wta')
2803
self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
2804
work_a.add('file', 'file-id')
2805
base_id = work_a.commit('base version')
2806
tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
2807
preview = TransformPreview(work_a.basis_tree())
2808
self.addCleanup(preview.finalize)
2809
trans_id = preview.trans_id_file_id('file-id')
2810
preview.delete_contents(trans_id)
2811
preview.create_file('b\nc\nd\ne\n', trans_id)
2812
self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
2813
tree_a = preview.get_preview_tree()
2814
tree_a.set_parent_ids([base_id])
2816
('killed-a', 'a\n'),
2817
('killed-b', 'b\n'),
2818
('unchanged', 'c\n'),
2819
('unchanged', 'd\n'),
2822
], list(tree_a.plan_file_merge('file-id', tree_b)))
2824
def test_walkdirs(self):
2825
preview = self.get_empty_preview()
2826
root = preview.new_directory('', ROOT_PARENT, 'tree-root')
2827
# FIXME: new_directory should mark root.
2828
preview.fixup_new_roots()
2829
preview_tree = preview.get_preview_tree()
2830
file_trans_id = preview.new_file('a', preview.root, 'contents',
2832
expected = [(('', 'tree-root'),
2833
[('a', 'a', 'file', None, 'a-id', 'file')])]
2834
self.assertEqual(expected, list(preview_tree.walkdirs()))
2836
def test_extras(self):
2837
work_tree = self.make_branch_and_tree('tree')
2838
self.build_tree(['tree/removed-file', 'tree/existing-file',
2839
'tree/not-removed-file'])
2840
work_tree.add(['removed-file', 'not-removed-file'])
2841
preview = TransformPreview(work_tree)
2842
self.addCleanup(preview.finalize)
2843
preview.new_file('new-file', preview.root, 'contents')
2844
preview.new_file('new-versioned-file', preview.root, 'contents',
2846
tree = preview.get_preview_tree()
2847
preview.unversion_file(preview.trans_id_tree_path('removed-file'))
2848
self.assertEqual(set(['new-file', 'removed-file', 'existing-file']),
2851
def test_merge_into_preview(self):
2852
work_tree = self.make_branch_and_tree('tree')
2853
self.build_tree_contents([('tree/file','b\n')])
2854
work_tree.add('file', 'file-id')
2855
work_tree.commit('first commit')
2856
child_tree = work_tree.bzrdir.sprout('child').open_workingtree()
2857
self.build_tree_contents([('child/file','b\nc\n')])
2858
child_tree.commit('child commit')
2859
child_tree.lock_write()
2860
self.addCleanup(child_tree.unlock)
2861
work_tree.lock_write()
2862
self.addCleanup(work_tree.unlock)
2863
preview = TransformPreview(work_tree)
2864
self.addCleanup(preview.finalize)
2865
file_trans_id = preview.trans_id_file_id('file-id')
2866
preview.delete_contents(file_trans_id)
2867
preview.create_file('a\nb\n', file_trans_id)
2868
preview_tree = preview.get_preview_tree()
2869
merger = Merger.from_revision_ids(None, preview_tree,
2870
child_tree.branch.last_revision(),
2871
other_branch=child_tree.branch,
2872
tree_branch=work_tree.branch)
2873
merger.merge_type = Merge3Merger
2874
tt = merger.make_merger().make_preview_transform()
2875
self.addCleanup(tt.finalize)
2876
final_tree = tt.get_preview_tree()
2877
self.assertEqual('a\nb\nc\n', final_tree.get_file_text('file-id'))
2879
def test_merge_preview_into_workingtree(self):
2880
tree = self.make_branch_and_tree('tree')
2881
tree.set_root_id('TREE_ROOT')
2882
tt = TransformPreview(tree)
2883
self.addCleanup(tt.finalize)
2884
tt.new_file('name', tt.root, 'content', 'file-id')
2885
tree2 = self.make_branch_and_tree('tree2')
2886
tree2.set_root_id('TREE_ROOT')
2887
merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2888
None, tree.basis_tree())
2889
merger.merge_type = Merge3Merger
2892
def test_merge_preview_into_workingtree_handles_conflicts(self):
2893
tree = self.make_branch_and_tree('tree')
2894
self.build_tree_contents([('tree/foo', 'bar')])
2895
tree.add('foo', 'foo-id')
2897
tt = TransformPreview(tree)
2898
self.addCleanup(tt.finalize)
2899
trans_id = tt.trans_id_file_id('foo-id')
2900
tt.delete_contents(trans_id)
2901
tt.create_file('baz', trans_id)
2902
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
2903
self.build_tree_contents([('tree2/foo', 'qux')])
2905
merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2906
pb, tree.basis_tree())
2907
merger.merge_type = Merge3Merger
2910
def test_is_executable(self):
2911
tree = self.make_branch_and_tree('tree')
2912
preview = TransformPreview(tree)
2913
self.addCleanup(preview.finalize)
2914
preview.new_file('foo', preview.root, 'bar', 'baz-id')
2915
preview_tree = preview.get_preview_tree()
2916
self.assertEqual(False, preview_tree.is_executable('baz-id',
2918
self.assertEqual(False, preview_tree.is_executable('baz-id'))
2920
def test_commit_preview_tree(self):
2921
tree = self.make_branch_and_tree('tree')
2922
rev_id = tree.commit('rev1')
2923
tree.branch.lock_write()
2924
self.addCleanup(tree.branch.unlock)
2925
tt = TransformPreview(tree)
2926
tt.new_file('file', tt.root, 'contents', 'file_id')
2927
self.addCleanup(tt.finalize)
2928
preview = tt.get_preview_tree()
2929
preview.set_parent_ids([rev_id])
2930
builder = tree.branch.get_commit_builder([rev_id])
2931
list(builder.record_iter_changes(preview, rev_id, tt.iter_changes()))
2932
builder.finish_inventory()
2933
rev2_id = builder.commit('rev2')
2934
rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2935
self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2937
def test_ascii_limbo_paths(self):
2938
self.requireFeature(tests.UnicodeFilenameFeature)
2939
branch = self.make_branch('any')
2940
tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2941
tt = TransformPreview(tree)
2942
self.addCleanup(tt.finalize)
2943
foo_id = tt.new_directory('', ROOT_PARENT)
2944
bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2945
limbo_path = tt._limbo_name(bar_id)
2946
self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
2949
class FakeSerializer(object):
2950
"""Serializer implementation that simply returns the input.
2952
The input is returned in the order used by pack.ContainerPushParser.
2955
def bytes_record(bytes, names):
2959
class TestSerializeTransform(tests.TestCaseWithTransport):
2961
_test_needs_features = [tests.UnicodeFilenameFeature]
2963
def get_preview(self, tree=None):
2965
tree = self.make_branch_and_tree('tree')
2966
tt = TransformPreview(tree)
2967
self.addCleanup(tt.finalize)
2970
def assertSerializesTo(self, expected, tt):
2971
records = list(tt.serialize(FakeSerializer()))
2972
self.assertEqual(expected, records)
2975
def default_attribs():
2980
'_new_executability': {},
2982
'_tree_path_ids': {'': 'new-0'},
2984
'_removed_contents': [],
2985
'_non_present_ids': {},
2988
def make_records(self, attribs, contents):
2990
(((('attribs'),),), bencode.bencode(attribs))]
2991
records.extend([(((n, k),), c) for n, k, c in contents])
2994
def creation_records(self):
2995
attribs = self.default_attribs()
2996
attribs['_id_number'] = 3
2997
attribs['_new_name'] = {
2998
'new-1': u'foo\u1234'.encode('utf-8'), 'new-2': 'qux'}
2999
attribs['_new_id'] = {'new-1': 'baz', 'new-2': 'quxx'}
3000
attribs['_new_parent'] = {'new-1': 'new-0', 'new-2': 'new-0'}
3001
attribs['_new_executability'] = {'new-1': 1}
3003
('new-1', 'file', 'i 1\nbar\n'),
3004
('new-2', 'directory', ''),
3006
return self.make_records(attribs, contents)
3008
def test_serialize_creation(self):
3009
tt = self.get_preview()
3010
tt.new_file(u'foo\u1234', tt.root, 'bar', 'baz', True)
3011
tt.new_directory('qux', tt.root, 'quxx')
3012
self.assertSerializesTo(self.creation_records(), tt)
3014
def test_deserialize_creation(self):
3015
tt = self.get_preview()
3016
tt.deserialize(iter(self.creation_records()))
3017
self.assertEqual(3, tt._id_number)
3018
self.assertEqual({'new-1': u'foo\u1234',
3019
'new-2': 'qux'}, tt._new_name)
3020
self.assertEqual({'new-1': 'baz', 'new-2': 'quxx'}, tt._new_id)
3021
self.assertEqual({'new-1': tt.root, 'new-2': tt.root}, tt._new_parent)
3022
self.assertEqual({'baz': 'new-1', 'quxx': 'new-2'}, tt._r_new_id)
3023
self.assertEqual({'new-1': True}, tt._new_executability)
3024
self.assertEqual({'new-1': 'file',
3025
'new-2': 'directory'}, tt._new_contents)
3026
foo_limbo = open(tt._limbo_name('new-1'), 'rb')
3028
foo_content = foo_limbo.read()
3031
self.assertEqual('bar', foo_content)
3033
def symlink_creation_records(self):
3034
attribs = self.default_attribs()
3035
attribs['_id_number'] = 2
3036
attribs['_new_name'] = {'new-1': u'foo\u1234'.encode('utf-8')}
3037
attribs['_new_parent'] = {'new-1': 'new-0'}
3038
contents = [('new-1', 'symlink', u'bar\u1234'.encode('utf-8'))]
3039
return self.make_records(attribs, contents)
3041
def test_serialize_symlink_creation(self):
3042
self.requireFeature(tests.SymlinkFeature)
3043
tt = self.get_preview()
3044
tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
3045
self.assertSerializesTo(self.symlink_creation_records(), tt)
3047
def test_deserialize_symlink_creation(self):
3048
self.requireFeature(tests.SymlinkFeature)
3049
tt = self.get_preview()
3050
tt.deserialize(iter(self.symlink_creation_records()))
3051
abspath = tt._limbo_name('new-1')
3052
foo_content = osutils.readlink(abspath)
3053
self.assertEqual(u'bar\u1234', foo_content)
3055
def make_destruction_preview(self):
3056
tree = self.make_branch_and_tree('.')
3057
self.build_tree([u'foo\u1234', 'bar'])
3058
tree.add([u'foo\u1234', 'bar'], ['foo-id', 'bar-id'])
3059
return self.get_preview(tree)
3061
def destruction_records(self):
3062
attribs = self.default_attribs()
3063
attribs['_id_number'] = 3
3064
attribs['_removed_id'] = ['new-1']
3065
attribs['_removed_contents'] = ['new-2']
3066
attribs['_tree_path_ids'] = {
3068
u'foo\u1234'.encode('utf-8'): 'new-1',
3071
return self.make_records(attribs, [])
3073
def test_serialize_destruction(self):
3074
tt = self.make_destruction_preview()
3075
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
3076
tt.unversion_file(foo_trans_id)
3077
bar_trans_id = tt.trans_id_tree_file_id('bar-id')
3078
tt.delete_contents(bar_trans_id)
3079
self.assertSerializesTo(self.destruction_records(), tt)
3081
def test_deserialize_destruction(self):
3082
tt = self.make_destruction_preview()
3083
tt.deserialize(iter(self.destruction_records()))
3084
self.assertEqual({u'foo\u1234': 'new-1',
3086
'': tt.root}, tt._tree_path_ids)
3087
self.assertEqual({'new-1': u'foo\u1234',
3089
tt.root: ''}, tt._tree_id_paths)
3090
self.assertEqual(set(['new-1']), tt._removed_id)
3091
self.assertEqual(set(['new-2']), tt._removed_contents)
3093
def missing_records(self):
3094
attribs = self.default_attribs()
3095
attribs['_id_number'] = 2
3096
attribs['_non_present_ids'] = {
3098
return self.make_records(attribs, [])
3100
def test_serialize_missing(self):
3101
tt = self.get_preview()
3102
boo_trans_id = tt.trans_id_file_id('boo')
3103
self.assertSerializesTo(self.missing_records(), tt)
3105
def test_deserialize_missing(self):
3106
tt = self.get_preview()
3107
tt.deserialize(iter(self.missing_records()))
3108
self.assertEqual({'boo': 'new-1'}, tt._non_present_ids)
3110
def make_modification_preview(self):
3111
LINES_ONE = 'aa\nbb\ncc\ndd\n'
3112
LINES_TWO = 'z\nbb\nx\ndd\n'
3113
tree = self.make_branch_and_tree('tree')
3114
self.build_tree_contents([('tree/file', LINES_ONE)])
3115
tree.add('file', 'file-id')
3116
return self.get_preview(tree), LINES_TWO
3118
def modification_records(self):
3119
attribs = self.default_attribs()
3120
attribs['_id_number'] = 2
3121
attribs['_tree_path_ids'] = {
3124
attribs['_removed_contents'] = ['new-1']
3125
contents = [('new-1', 'file',
3126
'i 1\nz\n\nc 0 1 1 1\ni 1\nx\n\nc 0 3 3 1\n')]
3127
return self.make_records(attribs, contents)
3129
def test_serialize_modification(self):
3130
tt, LINES = self.make_modification_preview()
3131
trans_id = tt.trans_id_file_id('file-id')
3132
tt.delete_contents(trans_id)
3133
tt.create_file(LINES, trans_id)
3134
self.assertSerializesTo(self.modification_records(), tt)
3136
def test_deserialize_modification(self):
3137
tt, LINES = self.make_modification_preview()
3138
tt.deserialize(iter(self.modification_records()))
3139
self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3141
def make_kind_change_preview(self):
3142
LINES = 'a\nb\nc\nd\n'
3143
tree = self.make_branch_and_tree('tree')
3144
self.build_tree(['tree/foo/'])
3145
tree.add('foo', 'foo-id')
3146
return self.get_preview(tree), LINES
3148
def kind_change_records(self):
3149
attribs = self.default_attribs()
3150
attribs['_id_number'] = 2
3151
attribs['_tree_path_ids'] = {
3154
attribs['_removed_contents'] = ['new-1']
3155
contents = [('new-1', 'file',
3156
'i 4\na\nb\nc\nd\n\n')]
3157
return self.make_records(attribs, contents)
3159
def test_serialize_kind_change(self):
3160
tt, LINES = self.make_kind_change_preview()
3161
trans_id = tt.trans_id_file_id('foo-id')
3162
tt.delete_contents(trans_id)
3163
tt.create_file(LINES, trans_id)
3164
self.assertSerializesTo(self.kind_change_records(), tt)
3166
def test_deserialize_kind_change(self):
3167
tt, LINES = self.make_kind_change_preview()
3168
tt.deserialize(iter(self.kind_change_records()))
3169
self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3171
def make_add_contents_preview(self):
3172
LINES = 'a\nb\nc\nd\n'
3173
tree = self.make_branch_and_tree('tree')
3174
self.build_tree(['tree/foo'])
3176
os.unlink('tree/foo')
3177
return self.get_preview(tree), LINES
3179
def add_contents_records(self):
3180
attribs = self.default_attribs()
3181
attribs['_id_number'] = 2
3182
attribs['_tree_path_ids'] = {
3185
contents = [('new-1', 'file',
3186
'i 4\na\nb\nc\nd\n\n')]
3187
return self.make_records(attribs, contents)
3189
def test_serialize_add_contents(self):
3190
tt, LINES = self.make_add_contents_preview()
3191
trans_id = tt.trans_id_tree_path('foo')
3192
tt.create_file(LINES, trans_id)
3193
self.assertSerializesTo(self.add_contents_records(), tt)
3195
def test_deserialize_add_contents(self):
3196
tt, LINES = self.make_add_contents_preview()
3197
tt.deserialize(iter(self.add_contents_records()))
3198
self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3200
def test_get_parents_lines(self):
3201
LINES_ONE = 'aa\nbb\ncc\ndd\n'
3202
LINES_TWO = 'z\nbb\nx\ndd\n'
3203
tree = self.make_branch_and_tree('tree')
3204
self.build_tree_contents([('tree/file', LINES_ONE)])
3205
tree.add('file', 'file-id')
3206
tt = self.get_preview(tree)
3207
trans_id = tt.trans_id_tree_path('file')
3208
self.assertEqual((['aa\n', 'bb\n', 'cc\n', 'dd\n'],),
3209
tt._get_parents_lines(trans_id))
3211
def test_get_parents_texts(self):
3212
LINES_ONE = 'aa\nbb\ncc\ndd\n'
3213
LINES_TWO = 'z\nbb\nx\ndd\n'
3214
tree = self.make_branch_and_tree('tree')
3215
self.build_tree_contents([('tree/file', LINES_ONE)])
3216
tree.add('file', 'file-id')
3217
tt = self.get_preview(tree)
3218
trans_id = tt.trans_id_tree_path('file')
3219
self.assertEqual((LINES_ONE,),
3220
tt._get_parents_texts(trans_id))