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
28
revision as _mod_revision,
33
33
from bzrlib.bzrdir import BzrDir
34
from bzrlib.conflicts import (
34
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
35
UnversionedParent, ParentLoop, DeletingParent,
43
37
from bzrlib.diff import show_diff_trees
44
from bzrlib.errors import (
47
ExistingPendingDeletion,
49
ImmortalPendingDeletion,
55
from bzrlib.osutils import (
38
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
39
ReusingTransform, CantMoveRoot,
40
PathsNotVersionedError, ExistingLimbo,
41
ExistingPendingDeletion, ImmortalLimbo,
42
ImmortalPendingDeletion, LockError)
43
from bzrlib.osutils import file_kind, pathjoin
59
44
from bzrlib.merge import Merge3Merger, Merger
60
45
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()
852
764
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
if os.name == "posix" and sys.platform != "cygwin":
863
# posix filesystems fail on renaming if the readonly bit is set
864
osutils.make_readonly(self.wt.abspath('first-dir'))
865
elif os.name == "nt":
866
# windows filesystems fail on renaming open files
867
self.addCleanup(file(self.wt.abspath('myfile')).close)
869
self.skip("Don't know how to force a permissions error on rename")
870
# now transform to rename
871
rename_transform, root_id = self.get_transform()
872
file_trans_id = rename_transform.trans_id_file_id('myfile-id')
873
dir_id = rename_transform.trans_id_file_id('first-id')
874
rename_transform.adjust_path('newname', dir_id, file_trans_id)
875
e = self.assertRaises(errors.TransformRenameFailed,
876
rename_transform.apply)
878
# "Failed to rename .../work/.bzr/checkout/limbo/new-1
879
# to .../first-dir/newname: [Errno 13] Permission denied"
880
# On windows looks like:
881
# "Failed to rename .../work/myfile to
882
# .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
883
# The strerror will vary per OS and language so it's not checked here
884
self.assertContainsRe(str(e),
885
"Failed to rename .*(first-dir.newname:|myfile)")
887
767
def test_set_executability_order(self):
888
768
"""Ensure that executability behaves the same, no matter what order.
1976
1856
self.assertEqual([], list(target.iter_changes(revision_tree)))
1977
1857
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
1859
def test_case_insensitive_build_tree_inventory(self):
2027
1860
if (tests.CaseInsensitiveFilesystemFeature.available()
2028
1861
or tests.CaseInsCasePresFilenameFeature.available()):
2040
1873
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
1876
class MockTransform(object):
2183
1878
def has_named_child(self, by_parent, parent_id, name):