15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
18
from StringIO import StringIO
22
22
from bzrlib import (
27
28
revision as _mod_revision,
32
33
from bzrlib.bzrdir import BzrDir
33
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
34
UnversionedParent, ParentLoop, DeletingParent,
34
from bzrlib.conflicts import (
36
43
from bzrlib.diff import show_diff_trees
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
44
from bzrlib.errors import (
47
ExistingPendingDeletion,
49
ImmortalPendingDeletion,
55
from bzrlib.osutils import (
43
59
from bzrlib.merge import Merge3Merger, Merger
44
60
from bzrlib.tests import (
136
159
transform.finalize()
137
160
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)
139
215
def test_hardlink(self):
140
216
self.requireFeature(HardlinkFeature)
141
217
transform, root = self.get_transform()
525
613
resolve_conflicts(replace)
528
def test_symlinks(self):
616
def _test_symlinks(self, link_name1,link_target1,
617
link_name2, link_target2):
619
def ozpath(p): return 'oz/' + p
529
621
self.requireFeature(SymlinkFeature)
530
transform,root = self.get_transform()
622
transform, root = self.get_transform()
531
623
oz_id = transform.new_directory('oz', root, 'oz-id')
532
wizard = transform.new_symlink('wizard', oz_id, 'wizard-target',
624
wizard = transform.new_symlink(link_name1, oz_id, link_target1,
534
wiz_id = transform.create_path('wizard2', oz_id)
535
transform.create_symlink('behind_curtain', wiz_id)
626
wiz_id = transform.create_path(link_name2, oz_id)
627
transform.create_symlink(link_target2, wiz_id)
536
628
transform.version_file('wiz-id2', wiz_id)
537
629
transform.set_executability(True, wiz_id)
538
630
self.assertEqual(transform.find_conflicts(),
539
631
[('non-file executability', wiz_id)])
540
632
transform.set_executability(None, wiz_id)
541
633
transform.apply()
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')),
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')
549
653
def test_unable_create_symlink(self):
748
852
rename.set_executability(True, myfile)
855
def test_rename_fails(self):
856
# see https://bugs.launchpad.net/bzr/+bug/491763
857
create, root_id = self.get_transform()
858
first_dir = create.new_directory('first-dir', root_id, 'first-id')
859
myfile = create.new_file('myfile', root_id, 'myfile-text',
862
# make the file and directory readonly in the hope this will prevent
864
osutils.make_readonly(self.wt.abspath('first-dir'))
865
osutils.make_readonly(self.wt.abspath('myfile'))
866
# now transform to rename
867
rename_transform, root_id = self.get_transform()
868
file_trans_id = rename_transform.trans_id_file_id('myfile-id')
869
dir_id = rename_transform.trans_id_file_id('first-id')
870
rename_transform.adjust_path('newname', dir_id, file_trans_id)
871
e = self.assertRaises(errors.TransformRenameFailed,
872
rename_transform.apply)
874
# "Failed to rename .../work/.bzr/checkout/limbo/new-1
875
# to .../first-dir/newname: [Errno 13] Permission denied"
876
# so the first filename is not visible in it; we expect a strerror but
877
# it may vary per OS and language so it's not checked here
878
self.assertContainsRe(str(e),
879
"Failed to rename .*first-dir.newname:")
751
881
def test_set_executability_order(self):
752
882
"""Ensure that executability behaves the same, no matter what order.
1840
1970
self.assertEqual([], list(target.iter_changes(revision_tree)))
1841
1971
self.assertTrue(source.is_executable('file1-id'))
1973
def install_rot13_content_filter(self, pattern):
1975
# self.addCleanup(filters._reset_registry, filters._reset_registry())
1976
# below, but that looks a bit... hard to read even if it's exactly
1978
original_registry = filters._reset_registry()
1979
def restore_registry():
1980
filters._reset_registry(original_registry)
1981
self.addCleanup(restore_registry)
1982
def rot13(chunks, context=None):
1983
return [''.join(chunks).encode('rot13')]
1984
rot13filter = filters.ContentFilter(rot13, rot13)
1985
filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
1986
os.mkdir(self.test_home_dir + '/.bazaar')
1987
rules_filename = self.test_home_dir + '/.bazaar/rules'
1988
f = open(rules_filename, 'wb')
1989
f.write('[name %s]\nrot13=yes\n' % (pattern,))
1991
def uninstall_rules():
1992
os.remove(rules_filename)
1994
self.addCleanup(uninstall_rules)
1997
def test_build_tree_content_filtered_files_are_not_hardlinked(self):
1998
"""build_tree will not hardlink files that have content filtering rules
1999
applied to them (but will still hardlink other files from the same tree
2002
self.requireFeature(HardlinkFeature)
2003
self.install_rot13_content_filter('file1')
2004
source = self.create_ab_tree()
2005
target = self.make_branch_and_tree('target')
2006
revision_tree = source.basis_tree()
2007
revision_tree.lock_read()
2008
self.addCleanup(revision_tree.unlock)
2009
build_tree(revision_tree, target, source, hardlink=True)
2011
self.addCleanup(target.unlock)
2012
self.assertEqual([], list(target.iter_changes(revision_tree)))
2013
source_stat = os.stat('source/file1')
2014
target_stat = os.stat('target/file1')
2015
self.assertNotEqual(source_stat, target_stat)
2016
source_stat = os.stat('source/file2')
2017
target_stat = os.stat('target/file2')
2018
self.assertEqualStat(source_stat, target_stat)
1843
2020
def test_case_insensitive_build_tree_inventory(self):
2021
if (tests.CaseInsensitiveFilesystemFeature.available()
2022
or tests.CaseInsCasePresFilenameFeature.available()):
2023
raise tests.UnavailableFeature('Fully case sensitive filesystem')
1844
2024
source = self.make_branch_and_tree('source')
1845
2025
self.build_tree(['source/file', 'source/FILE'])
1846
2026
source.add(['file', 'FILE'], ['lower-id', 'upper-id'])
1854
2034
self.assertEqual('FILE', target.id2path('upper-id'))
2037
class TestCommitTransform(tests.TestCaseWithTransport):
2039
def get_branch(self):
2040
tree = self.make_branch_and_tree('tree')
2042
self.addCleanup(tree.unlock)
2043
tree.commit('empty commit')
2046
def get_branch_and_transform(self):
2047
branch = self.get_branch()
2048
tt = TransformPreview(branch.basis_tree())
2049
self.addCleanup(tt.finalize)
2052
def test_commit_wrong_basis(self):
2053
branch = self.get_branch()
2054
basis = branch.repository.revision_tree(
2055
_mod_revision.NULL_REVISION)
2056
tt = TransformPreview(basis)
2057
self.addCleanup(tt.finalize)
2058
e = self.assertRaises(ValueError, tt.commit, branch, '')
2059
self.assertEqual('TreeTransform not based on branch basis: null:',
2062
def test_empy_commit(self):
2063
branch, tt = self.get_branch_and_transform()
2064
rev = tt.commit(branch, 'my message')
2065
self.assertEqual(2, branch.revno())
2066
repo = branch.repository
2067
self.assertEqual('my message', repo.get_revision(rev).message)
2069
def test_merge_parents(self):
2070
branch, tt = self.get_branch_and_transform()
2071
rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
2072
self.assertEqual(['rev1b', 'rev1c'],
2073
branch.basis_tree().get_parent_ids()[1:])
2075
def test_first_commit(self):
2076
branch = self.make_branch('branch')
2078
self.addCleanup(branch.unlock)
2079
tt = TransformPreview(branch.basis_tree())
2080
self.addCleanup(tt.finalize)
2081
tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
2082
rev = tt.commit(branch, 'my message')
2083
self.assertEqual([], branch.basis_tree().get_parent_ids())
2084
self.assertNotEqual(_mod_revision.NULL_REVISION,
2085
branch.last_revision())
2087
def test_first_commit_with_merge_parents(self):
2088
branch = self.make_branch('branch')
2090
self.addCleanup(branch.unlock)
2091
tt = TransformPreview(branch.basis_tree())
2092
self.addCleanup(tt.finalize)
2093
e = self.assertRaises(ValueError, tt.commit, branch,
2094
'my message', ['rev1b-id'])
2095
self.assertEqual('Cannot supply merge parents for first commit.',
2097
self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
2099
def test_add_files(self):
2100
branch, tt = self.get_branch_and_transform()
2101
tt.new_file('file', tt.root, 'contents', 'file-id')
2102
trans_id = tt.new_directory('dir', tt.root, 'dir-id')
2103
if SymlinkFeature.available():
2104
tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
2105
rev = tt.commit(branch, 'message')
2106
tree = branch.basis_tree()
2107
self.assertEqual('file', tree.id2path('file-id'))
2108
self.assertEqual('contents', tree.get_file_text('file-id'))
2109
self.assertEqual('dir', tree.id2path('dir-id'))
2110
if SymlinkFeature.available():
2111
self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2112
self.assertEqual('target', tree.get_symlink_target('symlink-id'))
2114
def test_add_unversioned(self):
2115
branch, tt = self.get_branch_and_transform()
2116
tt.new_file('file', tt.root, 'contents')
2117
self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
2118
'message', strict=True)
2120
def test_modify_strict(self):
2121
branch, tt = self.get_branch_and_transform()
2122
tt.new_file('file', tt.root, 'contents', 'file-id')
2123
tt.commit(branch, 'message', strict=True)
2124
tt = TransformPreview(branch.basis_tree())
2125
self.addCleanup(tt.finalize)
2126
trans_id = tt.trans_id_file_id('file-id')
2127
tt.delete_contents(trans_id)
2128
tt.create_file('contents', trans_id)
2129
tt.commit(branch, 'message', strict=True)
2131
def test_commit_malformed(self):
2132
"""Committing a malformed transform should raise an exception.
2134
In this case, we are adding a file without adding its parent.
2136
branch, tt = self.get_branch_and_transform()
2137
parent_id = tt.trans_id_file_id('parent-id')
2138
tt.new_file('file', parent_id, 'contents', 'file-id')
2139
self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2142
def test_commit_rich_revision_data(self):
2143
branch, tt = self.get_branch_and_transform()
2144
rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2145
committer='me <me@example.com>',
2146
revprops={'foo': 'bar'}, revision_id='revid-1',
2147
authors=['Author1 <author1@example.com>',
2148
'Author2 <author2@example.com>',
2150
self.assertEqual('revid-1', rev_id)
2151
revision = branch.repository.get_revision(rev_id)
2152
self.assertEqual(1, revision.timestamp)
2153
self.assertEqual(43201, revision.timezone)
2154
self.assertEqual('me <me@example.com>', revision.committer)
2155
self.assertEqual(['Author1 <author1@example.com>',
2156
'Author2 <author2@example.com>'],
2157
revision.get_apparent_authors())
2158
del revision.properties['authors']
2159
self.assertEqual({'foo': 'bar',
2160
'branch-nick': 'tree'},
2161
revision.properties)
2163
def test_no_explicit_revprops(self):
2164
branch, tt = self.get_branch_and_transform()
2165
rev_id = tt.commit(branch, 'message', authors=[
2166
'Author1 <author1@example.com>',
2167
'Author2 <author2@example.com>', ])
2168
revision = branch.repository.get_revision(rev_id)
2169
self.assertEqual(['Author1 <author1@example.com>',
2170
'Author2 <author2@example.com>'],
2171
revision.get_apparent_authors())
2172
self.assertEqual('tree', revision.properties['branch-nick'])
1857
2175
class MockTransform(object):
1859
2177
def has_named_child(self, by_parent, parent_id, name):
2586
2912
self.assertEqual(False, preview_tree.is_executable('baz-id'))
2914
def test_commit_preview_tree(self):
2915
tree = self.make_branch_and_tree('tree')
2916
rev_id = tree.commit('rev1')
2917
tree.branch.lock_write()
2918
self.addCleanup(tree.branch.unlock)
2919
tt = TransformPreview(tree)
2920
tt.new_file('file', tt.root, 'contents', 'file_id')
2921
self.addCleanup(tt.finalize)
2922
preview = tt.get_preview_tree()
2923
preview.set_parent_ids([rev_id])
2924
builder = tree.branch.get_commit_builder([rev_id])
2925
list(builder.record_iter_changes(preview, rev_id, tt.iter_changes()))
2926
builder.finish_inventory()
2927
rev2_id = builder.commit('rev2')
2928
rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2929
self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2931
def test_ascii_limbo_paths(self):
2932
self.requireFeature(tests.UnicodeFilenameFeature)
2933
branch = self.make_branch('any')
2934
tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2935
tt = TransformPreview(tree)
2936
self.addCleanup(tt.finalize)
2937
foo_id = tt.new_directory('', ROOT_PARENT)
2938
bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2939
limbo_path = tt._limbo_name(bar_id)
2940
self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
2589
2943
class FakeSerializer(object):
2590
2944
"""Serializer implementation that simply returns the input.