15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19
from StringIO import StringIO
22
22
from bzrlib import (
28
27
revision as _mod_revision,
33
32
from bzrlib.bzrdir import BzrDir
34
from bzrlib.conflicts import (
33
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
34
UnversionedParent, ParentLoop, DeletingParent,
43
36
from bzrlib.diff import show_diff_trees
44
from bzrlib.errors import (
47
ExistingPendingDeletion,
49
ImmortalPendingDeletion,
55
from bzrlib.osutils import (
37
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
38
ReusingTransform, CantMoveRoot,
39
PathsNotVersionedError, ExistingLimbo,
40
ExistingPendingDeletion, ImmortalLimbo,
41
ImmortalPendingDeletion, LockError)
42
from bzrlib.osutils import file_kind, pathjoin
59
43
from bzrlib.merge import Merge3Merger, Merger
60
44
from bzrlib.tests import (
159
136
transform.finalize()
160
137
transform.finalize()
162
def test_create_files_same_timestamp(self):
163
transform, root = self.get_transform()
164
self.wt.lock_tree_write()
165
self.addCleanup(self.wt.unlock)
166
# Roll back the clock, so that we know everything is being set to the
168
transform._creation_mtime = creation_mtime = time.time() - 20.0
169
transform.create_file('content-one',
170
transform.create_path('one', root))
171
time.sleep(1) # *ugly*
172
transform.create_file('content-two',
173
transform.create_path('two', root))
175
fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
177
fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
179
# We only guarantee 2s resolution
180
self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
181
"%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
182
# But if we have more than that, all files should get the same result
183
self.assertEqual(st1.st_mtime, st2.st_mtime)
185
def test_change_root_id(self):
186
transform, root = self.get_transform()
187
self.assertNotEqual('new-root-id', self.wt.get_root_id())
188
transform.new_directory('', ROOT_PARENT, 'new-root-id')
189
transform.delete_contents(root)
190
transform.unversion_file(root)
191
transform.fixup_new_roots()
193
self.assertEqual('new-root-id', self.wt.get_root_id())
195
def test_change_root_id_add_files(self):
196
transform, root = self.get_transform()
197
self.assertNotEqual('new-root-id', self.wt.get_root_id())
198
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
199
transform.new_file('file', new_trans_id, ['new-contents\n'],
201
transform.delete_contents(root)
202
transform.unversion_file(root)
203
transform.fixup_new_roots()
205
self.assertEqual('new-root-id', self.wt.get_root_id())
206
self.assertEqual('new-file-id', self.wt.path2id('file'))
207
self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
209
def test_add_two_roots(self):
210
transform, root = self.get_transform()
211
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
212
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
213
self.assertRaises(ValueError, transform.fixup_new_roots)
215
139
def test_hardlink(self):
216
140
self.requireFeature(HardlinkFeature)
217
141
transform, root = self.get_transform()
613
525
resolve_conflicts(replace)
616
def _test_symlinks(self, link_name1,link_target1,
617
link_name2, link_target2):
619
def ozpath(p): return 'oz/' + p
528
def test_symlinks(self):
621
529
self.requireFeature(SymlinkFeature)
622
transform, root = self.get_transform()
530
transform,root = self.get_transform()
623
531
oz_id = transform.new_directory('oz', root, 'oz-id')
624
wizard = transform.new_symlink(link_name1, oz_id, link_target1,
532
wizard = transform.new_symlink('wizard', oz_id, 'wizard-target',
626
wiz_id = transform.create_path(link_name2, oz_id)
627
transform.create_symlink(link_target2, wiz_id)
534
wiz_id = transform.create_path('wizard2', oz_id)
535
transform.create_symlink('behind_curtain', wiz_id)
628
536
transform.version_file('wiz-id2', wiz_id)
629
537
transform.set_executability(True, wiz_id)
630
538
self.assertEqual(transform.find_conflicts(),
631
539
[('non-file executability', wiz_id)])
632
540
transform.set_executability(None, wiz_id)
633
541
transform.apply()
634
self.assertEqual(self.wt.path2id(ozpath(link_name1)), 'wizard-id')
635
self.assertEqual('symlink',
636
file_kind(self.wt.abspath(ozpath(link_name1))))
637
self.assertEqual(link_target2,
638
osutils.readlink(self.wt.abspath(ozpath(link_name2))))
639
self.assertEqual(link_target1,
640
osutils.readlink(self.wt.abspath(ozpath(link_name1))))
642
def test_symlinks(self):
643
self._test_symlinks('wizard', 'wizard-target',
644
'wizard2', 'behind_curtain')
646
def test_symlinks_unicode(self):
647
self.requireFeature(tests.UnicodeFilenameFeature)
648
self._test_symlinks(u'\N{Euro Sign}wizard',
649
u'wizard-targ\N{Euro Sign}t',
650
u'\N{Euro Sign}wizard2',
651
u'b\N{Euro Sign}hind_curtain')
542
self.assertEqual(self.wt.path2id('oz/wizard'), 'wizard-id')
543
self.assertEqual(file_kind(self.wt.abspath('oz/wizard')), 'symlink')
544
self.assertEqual(os.readlink(self.wt.abspath('oz/wizard2')),
546
self.assertEqual(os.readlink(self.wt.abspath('oz/wizard')),
653
549
def test_unable_create_symlink(self):
1944
1840
self.assertEqual([], list(target.iter_changes(revision_tree)))
1945
1841
self.assertTrue(source.is_executable('file1-id'))
1947
def install_rot13_content_filter(self, pattern):
1949
# self.addCleanup(filters._reset_registry, filters._reset_registry())
1950
# below, but that looks a bit... hard to read even if it's exactly
1952
original_registry = filters._reset_registry()
1953
def restore_registry():
1954
filters._reset_registry(original_registry)
1955
self.addCleanup(restore_registry)
1956
def rot13(chunks, context=None):
1957
return [''.join(chunks).encode('rot13')]
1958
rot13filter = filters.ContentFilter(rot13, rot13)
1959
filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
1960
os.mkdir(self.test_home_dir + '/.bazaar')
1961
rules_filename = self.test_home_dir + '/.bazaar/rules'
1962
f = open(rules_filename, 'wb')
1963
f.write('[name %s]\nrot13=yes\n' % (pattern,))
1965
def uninstall_rules():
1966
os.remove(rules_filename)
1968
self.addCleanup(uninstall_rules)
1971
def test_build_tree_content_filtered_files_are_not_hardlinked(self):
1972
"""build_tree will not hardlink files that have content filtering rules
1973
applied to them (but will still hardlink other files from the same tree
1976
self.requireFeature(HardlinkFeature)
1977
self.install_rot13_content_filter('file1')
1978
source = self.create_ab_tree()
1979
target = self.make_branch_and_tree('target')
1980
revision_tree = source.basis_tree()
1981
revision_tree.lock_read()
1982
self.addCleanup(revision_tree.unlock)
1983
build_tree(revision_tree, target, source, hardlink=True)
1985
self.addCleanup(target.unlock)
1986
self.assertEqual([], list(target.iter_changes(revision_tree)))
1987
source_stat = os.stat('source/file1')
1988
target_stat = os.stat('target/file1')
1989
self.assertNotEqual(source_stat, target_stat)
1990
source_stat = os.stat('source/file2')
1991
target_stat = os.stat('target/file2')
1992
self.assertEqualStat(source_stat, target_stat)
1994
1843
def test_case_insensitive_build_tree_inventory(self):
1995
if (tests.CaseInsensitiveFilesystemFeature.available()
1996
or tests.CaseInsCasePresFilenameFeature.available()):
1844
if (not tests.CaseInsensitiveFilesystemFeature.available()
1845
or not tests.CaseInsCasePresFilenameFeature.available()):
1997
1846
raise tests.UnavailableFeature('Fully case sensitive filesystem')
1998
1847
source = self.make_branch_and_tree('source')
1999
1848
self.build_tree(['source/file', 'source/FILE'])
2008
1857
self.assertEqual('FILE', target.id2path('upper-id'))
2011
class TestCommitTransform(tests.TestCaseWithTransport):
2013
def get_branch(self):
2014
tree = self.make_branch_and_tree('tree')
2016
self.addCleanup(tree.unlock)
2017
tree.commit('empty commit')
2020
def get_branch_and_transform(self):
2021
branch = self.get_branch()
2022
tt = TransformPreview(branch.basis_tree())
2023
self.addCleanup(tt.finalize)
2026
def test_commit_wrong_basis(self):
2027
branch = self.get_branch()
2028
basis = branch.repository.revision_tree(
2029
_mod_revision.NULL_REVISION)
2030
tt = TransformPreview(basis)
2031
self.addCleanup(tt.finalize)
2032
e = self.assertRaises(ValueError, tt.commit, branch, '')
2033
self.assertEqual('TreeTransform not based on branch basis: null:',
2036
def test_empy_commit(self):
2037
branch, tt = self.get_branch_and_transform()
2038
rev = tt.commit(branch, 'my message')
2039
self.assertEqual(2, branch.revno())
2040
repo = branch.repository
2041
self.assertEqual('my message', repo.get_revision(rev).message)
2043
def test_merge_parents(self):
2044
branch, tt = self.get_branch_and_transform()
2045
rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
2046
self.assertEqual(['rev1b', 'rev1c'],
2047
branch.basis_tree().get_parent_ids()[1:])
2049
def test_first_commit(self):
2050
branch = self.make_branch('branch')
2052
self.addCleanup(branch.unlock)
2053
tt = TransformPreview(branch.basis_tree())
2054
self.addCleanup(tt.finalize)
2055
tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
2056
rev = tt.commit(branch, 'my message')
2057
self.assertEqual([], branch.basis_tree().get_parent_ids())
2058
self.assertNotEqual(_mod_revision.NULL_REVISION,
2059
branch.last_revision())
2061
def test_first_commit_with_merge_parents(self):
2062
branch = self.make_branch('branch')
2064
self.addCleanup(branch.unlock)
2065
tt = TransformPreview(branch.basis_tree())
2066
self.addCleanup(tt.finalize)
2067
e = self.assertRaises(ValueError, tt.commit, branch,
2068
'my message', ['rev1b-id'])
2069
self.assertEqual('Cannot supply merge parents for first commit.',
2071
self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
2073
def test_add_files(self):
2074
branch, tt = self.get_branch_and_transform()
2075
tt.new_file('file', tt.root, 'contents', 'file-id')
2076
trans_id = tt.new_directory('dir', tt.root, 'dir-id')
2077
if SymlinkFeature.available():
2078
tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
2079
rev = tt.commit(branch, 'message')
2080
tree = branch.basis_tree()
2081
self.assertEqual('file', tree.id2path('file-id'))
2082
self.assertEqual('contents', tree.get_file_text('file-id'))
2083
self.assertEqual('dir', tree.id2path('dir-id'))
2084
if SymlinkFeature.available():
2085
self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2086
self.assertEqual('target', tree.get_symlink_target('symlink-id'))
2088
def test_add_unversioned(self):
2089
branch, tt = self.get_branch_and_transform()
2090
tt.new_file('file', tt.root, 'contents')
2091
self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
2092
'message', strict=True)
2094
def test_modify_strict(self):
2095
branch, tt = self.get_branch_and_transform()
2096
tt.new_file('file', tt.root, 'contents', 'file-id')
2097
tt.commit(branch, 'message', strict=True)
2098
tt = TransformPreview(branch.basis_tree())
2099
self.addCleanup(tt.finalize)
2100
trans_id = tt.trans_id_file_id('file-id')
2101
tt.delete_contents(trans_id)
2102
tt.create_file('contents', trans_id)
2103
tt.commit(branch, 'message', strict=True)
2105
def test_commit_malformed(self):
2106
"""Committing a malformed transform should raise an exception.
2108
In this case, we are adding a file without adding its parent.
2110
branch, tt = self.get_branch_and_transform()
2111
parent_id = tt.trans_id_file_id('parent-id')
2112
tt.new_file('file', parent_id, 'contents', 'file-id')
2113
self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2116
def test_commit_rich_revision_data(self):
2117
branch, tt = self.get_branch_and_transform()
2118
rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2119
committer='me <me@example.com>',
2120
revprops={'foo': 'bar'}, revision_id='revid-1',
2121
authors=['Author1 <author1@example.com>',
2122
'Author2 <author2@example.com>',
2124
self.assertEqual('revid-1', rev_id)
2125
revision = branch.repository.get_revision(rev_id)
2126
self.assertEqual(1, revision.timestamp)
2127
self.assertEqual(43201, revision.timezone)
2128
self.assertEqual('me <me@example.com>', revision.committer)
2129
self.assertEqual(['Author1 <author1@example.com>',
2130
'Author2 <author2@example.com>'],
2131
revision.get_apparent_authors())
2132
del revision.properties['authors']
2133
self.assertEqual({'foo': 'bar',
2134
'branch-nick': 'tree'},
2135
revision.properties)
2137
def test_no_explicit_revprops(self):
2138
branch, tt = self.get_branch_and_transform()
2139
rev_id = tt.commit(branch, 'message', authors=[
2140
'Author1 <author1@example.com>',
2141
'Author2 <author2@example.com>', ])
2142
revision = branch.repository.get_revision(rev_id)
2143
self.assertEqual(['Author1 <author1@example.com>',
2144
'Author2 <author2@example.com>'],
2145
revision.get_apparent_authors())
2146
self.assertEqual('tree', revision.properties['branch-nick'])
2149
1860
class MockTransform(object):
2151
1862
def has_named_child(self, by_parent, parent_id, name):
2886
2589
self.assertEqual(False, preview_tree.is_executable('baz-id'))
2888
def test_commit_preview_tree(self):
2889
tree = self.make_branch_and_tree('tree')
2890
rev_id = tree.commit('rev1')
2891
tree.branch.lock_write()
2892
self.addCleanup(tree.branch.unlock)
2893
tt = TransformPreview(tree)
2894
tt.new_file('file', tt.root, 'contents', 'file_id')
2895
self.addCleanup(tt.finalize)
2896
preview = tt.get_preview_tree()
2897
preview.set_parent_ids([rev_id])
2898
builder = tree.branch.get_commit_builder([rev_id])
2899
list(builder.record_iter_changes(preview, rev_id, tt.iter_changes()))
2900
builder.finish_inventory()
2901
rev2_id = builder.commit('rev2')
2902
rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2903
self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2905
def test_ascii_limbo_paths(self):
2906
self.requireFeature(tests.UnicodeFilenameFeature)
2907
branch = self.make_branch('any')
2908
tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2909
tt = TransformPreview(tree)
2910
self.addCleanup(tt.finalize)
2911
foo_id = tt.new_directory('', ROOT_PARENT)
2912
bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2913
limbo_path = tt._limbo_name(bar_id)
2914
self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
2917
2592
class FakeSerializer(object):
2918
2593
"""Serializer implementation that simply returns the input.