14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
19
from StringIO import StringIO
22
23
from bzrlib import (
28
29
revision as _mod_revision,
33
from bzrlib.bzrdir import BzrDir
34
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
35
UnversionedParent, ParentLoop, DeletingParent,
37
from bzrlib.conflicts import (
46
from bzrlib.controldir import ControlDir
37
47
from bzrlib.diff import show_diff_trees
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
48
from bzrlib.errors import (
51
ExistingPendingDeletion,
53
ImmortalPendingDeletion,
58
from bzrlib.osutils import (
44
62
from bzrlib.merge import Merge3Merger, Merger
63
from bzrlib.mutabletree import MutableTree
45
64
from bzrlib.tests import (
69
from bzrlib.tests.features import (
52
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths,
53
resolve_conflicts, cook_conflicts,
54
build_tree, get_backup_name,
55
_FileMover, resolve_checkout,
56
TransformPreview, create_from_tree)
73
from bzrlib.transform import (
59
87
class TestTreeTransform(tests.TestCaseWithTransport):
62
90
super(TestTreeTransform, self).setUp()
63
self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
91
self.wt = self.make_branch_and_tree('.', format='development-subtree')
66
94
def get_transform(self):
136
174
transform.finalize()
137
175
transform.finalize()
177
def test_apply_informs_tree_of_observed_sha1(self):
178
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
179
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
182
orig = self.wt._observed_sha1
183
def _observed_sha1(*args):
186
self.wt._observed_sha1 = _observed_sha1
188
self.assertEqual([(None, 'file1', trans._observed_sha1s[trans_id])],
191
def test_create_file_caches_sha1(self):
192
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
193
trans_id = trans.create_path('file1', root)
194
trans.create_file(contents, trans_id, sha1=sha1)
195
st_val = osutils.lstat(trans._limbo_name(trans_id))
196
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
197
self.assertEqual(o_sha1, sha1)
198
self.assertEqualStat(o_st_val, st_val)
200
def test__apply_insertions_updates_sha1(self):
201
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
202
trans_id = trans.create_path('file1', root)
203
trans.create_file(contents, trans_id, sha1=sha1)
204
st_val = osutils.lstat(trans._limbo_name(trans_id))
205
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
206
self.assertEqual(o_sha1, sha1)
207
self.assertEqualStat(o_st_val, st_val)
208
creation_mtime = trans._creation_mtime + 10.0
209
# We fake a time difference from when the file was created until now it
210
# is being renamed by using os.utime. Note that the change we actually
211
# want to see is the real ctime change from 'os.rename()', but as long
212
# as we observe a new stat value, we should be fine.
213
os.utime(trans._limbo_name(trans_id), (creation_mtime, creation_mtime))
215
new_st_val = osutils.lstat(self.wt.abspath('file1'))
216
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
217
self.assertEqual(o_sha1, sha1)
218
self.assertEqualStat(o_st_val, new_st_val)
219
self.assertNotEqual(st_val.st_mtime, new_st_val.st_mtime)
221
def test_new_file_caches_sha1(self):
222
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
223
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
225
st_val = osutils.lstat(trans._limbo_name(trans_id))
226
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
227
self.assertEqual(o_sha1, sha1)
228
self.assertEqualStat(o_st_val, st_val)
230
def test_cancel_creation_removes_observed_sha1(self):
231
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
232
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
234
self.assertTrue(trans_id in trans._observed_sha1s)
235
trans.cancel_creation(trans_id)
236
self.assertFalse(trans_id in trans._observed_sha1s)
238
def test_create_files_same_timestamp(self):
239
transform, root = self.get_transform()
240
self.wt.lock_tree_write()
241
self.addCleanup(self.wt.unlock)
242
# Roll back the clock, so that we know everything is being set to the
244
transform._creation_mtime = creation_mtime = time.time() - 20.0
245
transform.create_file('content-one',
246
transform.create_path('one', root))
247
time.sleep(1) # *ugly*
248
transform.create_file('content-two',
249
transform.create_path('two', root))
251
fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
253
fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
255
# We only guarantee 2s resolution
256
self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
257
"%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
258
# But if we have more than that, all files should get the same result
259
self.assertEqual(st1.st_mtime, st2.st_mtime)
261
def test_change_root_id(self):
262
transform, root = self.get_transform()
263
self.assertNotEqual('new-root-id', self.wt.get_root_id())
264
transform.new_directory('', ROOT_PARENT, 'new-root-id')
265
transform.delete_contents(root)
266
transform.unversion_file(root)
267
transform.fixup_new_roots()
269
self.assertEqual('new-root-id', self.wt.get_root_id())
271
def test_change_root_id_add_files(self):
272
transform, root = self.get_transform()
273
self.assertNotEqual('new-root-id', self.wt.get_root_id())
274
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
275
transform.new_file('file', new_trans_id, ['new-contents\n'],
277
transform.delete_contents(root)
278
transform.unversion_file(root)
279
transform.fixup_new_roots()
281
self.assertEqual('new-root-id', self.wt.get_root_id())
282
self.assertEqual('new-file-id', self.wt.path2id('file'))
283
self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
285
def test_add_two_roots(self):
286
transform, root = self.get_transform()
287
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
288
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
289
self.assertRaises(ValueError, transform.fixup_new_roots)
291
def test_retain_existing_root(self):
292
tt, root = self.get_transform()
294
tt.new_directory('', ROOT_PARENT, 'new-root-id')
296
self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
298
def test_retain_existing_root_added_file(self):
299
tt, root = self.get_transform()
300
new_trans_id = tt.new_directory('', ROOT_PARENT, 'new-root-id')
301
child = tt.new_directory('child', new_trans_id, 'child-id')
303
self.assertEqual(tt.root, tt.final_parent(child))
305
def test_add_unversioned_root(self):
306
transform, root = self.get_transform()
307
new_trans_id = transform.new_directory('', ROOT_PARENT, None)
308
transform.delete_contents(transform.root)
309
transform.fixup_new_roots()
310
self.assertNotIn(transform.root, transform._new_id)
312
def test_remove_root_fixup(self):
313
transform, root = self.get_transform()
314
old_root_id = self.wt.get_root_id()
315
self.assertNotEqual('new-root-id', old_root_id)
316
transform.delete_contents(root)
317
transform.unversion_file(root)
318
transform.fixup_new_roots()
320
self.assertEqual(old_root_id, self.wt.get_root_id())
322
transform, root = self.get_transform()
323
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
324
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
325
self.assertRaises(ValueError, transform.fixup_new_roots)
327
def test_fixup_new_roots_permits_empty_tree(self):
328
transform, root = self.get_transform()
329
transform.delete_contents(root)
330
transform.unversion_file(root)
331
transform.fixup_new_roots()
332
self.assertIs(None, transform.final_kind(root))
333
self.assertIs(None, transform.final_file_id(root))
335
def test_apply_retains_root_directory(self):
336
# Do not attempt to delete the physical root directory, because that
338
transform, root = self.get_transform()
340
transform.delete_contents(root)
341
e = self.assertRaises(AssertionError, self.assertRaises,
342
errors.TransformRenameFailed,
344
self.assertContainsRe('TransformRenameFailed not raised', str(e))
346
def test_apply_retains_file_id(self):
347
transform, root = self.get_transform()
348
old_root_id = transform.tree_file_id(root)
349
transform.unversion_file(root)
351
self.assertEqual(old_root_id, self.wt.get_root_id())
139
353
def test_hardlink(self):
140
354
self.requireFeature(HardlinkFeature)
141
355
transform, root = self.get_transform()
764
1005
rename.set_executability(True, myfile)
1008
def test_rename_fails(self):
1009
self.requireFeature(features.not_running_as_root)
1010
# see https://bugs.launchpad.net/bzr/+bug/491763
1011
create, root_id = self.get_transform()
1012
first_dir = create.new_directory('first-dir', root_id, 'first-id')
1013
myfile = create.new_file('myfile', root_id, 'myfile-text',
1016
if os.name == "posix" and sys.platform != "cygwin":
1017
# posix filesystems fail on renaming if the readonly bit is set
1018
osutils.make_readonly(self.wt.abspath('first-dir'))
1019
elif os.name == "nt":
1020
# windows filesystems fail on renaming open files
1021
self.addCleanup(file(self.wt.abspath('myfile')).close)
1023
self.skip("Don't know how to force a permissions error on rename")
1024
# now transform to rename
1025
rename_transform, root_id = self.get_transform()
1026
file_trans_id = rename_transform.trans_id_file_id('myfile-id')
1027
dir_id = rename_transform.trans_id_file_id('first-id')
1028
rename_transform.adjust_path('newname', dir_id, file_trans_id)
1029
e = self.assertRaises(errors.TransformRenameFailed,
1030
rename_transform.apply)
1031
# On nix looks like:
1032
# "Failed to rename .../work/.bzr/checkout/limbo/new-1
1033
# to .../first-dir/newname: [Errno 13] Permission denied"
1034
# On windows looks like:
1035
# "Failed to rename .../work/myfile to
1036
# .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
1037
# This test isn't concerned with exactly what the error looks like,
1038
# and the strerror will vary across OS and locales, but the assert
1039
# that the exeception attributes are what we expect
1040
self.assertEqual(e.errno, errno.EACCES)
1041
if os.name == "posix":
1042
self.assertEndsWith(e.to_path, "/first-dir/newname")
1044
self.assertEqual(os.path.basename(e.from_path), "myfile")
767
1046
def test_set_executability_order(self):
768
1047
"""Ensure that executability behaves the same, no matter what order.
1385
1657
return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1660
class TestInventoryAltered(tests.TestCaseWithTransport):
1662
def test_inventory_altered_unchanged(self):
1663
tree = self.make_branch_and_tree('tree')
1664
self.build_tree(['tree/foo'])
1665
tree.add('foo', 'foo-id')
1666
with TransformPreview(tree) as tt:
1667
self.assertEqual([], tt._inventory_altered())
1669
def test_inventory_altered_changed_parent_id(self):
1670
tree = self.make_branch_and_tree('tree')
1671
self.build_tree(['tree/foo'])
1672
tree.add('foo', 'foo-id')
1673
with TransformPreview(tree) as tt:
1674
tt.unversion_file(tt.root)
1675
tt.version_file('new-id', tt.root)
1676
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1677
foo_tuple = ('foo', foo_trans_id)
1678
root_tuple = ('', tt.root)
1679
self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1681
def test_inventory_altered_noop_changed_parent_id(self):
1682
tree = self.make_branch_and_tree('tree')
1683
self.build_tree(['tree/foo'])
1684
tree.add('foo', 'foo-id')
1685
with TransformPreview(tree) as tt:
1686
tt.unversion_file(tt.root)
1687
tt.version_file(tree.get_root_id(), tt.root)
1688
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1689
self.assertEqual([], tt._inventory_altered())
1388
1692
class TestTransformMerge(TestCaseInTempDir):
1390
1694
def test_text_merge(self):
1856
2172
self.assertEqual([], list(target.iter_changes(revision_tree)))
1857
2173
self.assertTrue(source.is_executable('file1-id'))
2175
def install_rot13_content_filter(self, pattern):
2177
# self.addCleanup(filters._reset_registry, filters._reset_registry())
2178
# below, but that looks a bit... hard to read even if it's exactly
2180
original_registry = filters._reset_registry()
2181
def restore_registry():
2182
filters._reset_registry(original_registry)
2183
self.addCleanup(restore_registry)
2184
def rot13(chunks, context=None):
2185
return [''.join(chunks).encode('rot13')]
2186
rot13filter = filters.ContentFilter(rot13, rot13)
2187
filters.filter_stacks_registry.register(
2188
'rot13', {'yes': [rot13filter]}.get)
2189
os.mkdir(self.test_home_dir + '/.bazaar')
2190
rules_filename = self.test_home_dir + '/.bazaar/rules'
2191
f = open(rules_filename, 'wb')
2192
f.write('[name %s]\nrot13=yes\n' % (pattern,))
2194
def uninstall_rules():
2195
os.remove(rules_filename)
2197
self.addCleanup(uninstall_rules)
2200
def test_build_tree_content_filtered_files_are_not_hardlinked(self):
2201
"""build_tree will not hardlink files that have content filtering rules
2202
applied to them (but will still hardlink other files from the same tree
2205
self.requireFeature(HardlinkFeature)
2206
self.install_rot13_content_filter('file1')
2207
source = self.create_ab_tree()
2208
target = self.make_branch_and_tree('target')
2209
revision_tree = source.basis_tree()
2210
revision_tree.lock_read()
2211
self.addCleanup(revision_tree.unlock)
2212
build_tree(revision_tree, target, source, hardlink=True)
2214
self.addCleanup(target.unlock)
2215
self.assertEqual([], list(target.iter_changes(revision_tree)))
2216
source_stat = os.stat('source/file1')
2217
target_stat = os.stat('target/file1')
2218
self.assertNotEqual(source_stat, target_stat)
2219
source_stat = os.stat('source/file2')
2220
target_stat = os.stat('target/file2')
2221
self.assertEqualStat(source_stat, target_stat)
1859
2223
def test_case_insensitive_build_tree_inventory(self):
1860
if (tests.CaseInsensitiveFilesystemFeature.available()
1861
or tests.CaseInsCasePresFilenameFeature.available()):
2224
if (features.CaseInsensitiveFilesystemFeature.available()
2225
or features.CaseInsCasePresFilenameFeature.available()):
1862
2226
raise tests.UnavailableFeature('Fully case sensitive filesystem')
1863
2227
source = self.make_branch_and_tree('source')
1864
2228
self.build_tree(['source/file', 'source/FILE'])
1872
2236
self.assertEqual('file.moved', target.id2path('lower-id'))
1873
2237
self.assertEqual('FILE', target.id2path('upper-id'))
2239
def test_build_tree_observes_sha(self):
2240
source = self.make_branch_and_tree('source')
2241
self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
2242
source.add(['file1', 'dir', 'dir/file2'],
2243
['file1-id', 'dir-id', 'file2-id'])
2244
source.commit('new files')
2245
target = self.make_branch_and_tree('target')
2247
self.addCleanup(target.unlock)
2248
# We make use of the fact that DirState caches its cutoff time. So we
2249
# set the 'safe' time to one minute in the future.
2250
state = target.current_dirstate()
2251
state._cutoff_time = time.time() + 60
2252
build_tree(source.basis_tree(), target)
2253
entry1_sha = osutils.sha_file_by_name('source/file1')
2254
entry2_sha = osutils.sha_file_by_name('source/dir/file2')
2255
# entry[1] is the state information, entry[1][0] is the state of the
2256
# working tree, entry[1][0][1] is the sha value for the current working
2258
entry1 = state._get_entry(0, path_utf8='file1')
2259
self.assertEqual(entry1_sha, entry1[1][0][1])
2260
# The 'size' field must also be set.
2261
self.assertEqual(25, entry1[1][0][2])
2262
entry1_state = entry1[1][0]
2263
entry2 = state._get_entry(0, path_utf8='dir/file2')
2264
self.assertEqual(entry2_sha, entry2[1][0][1])
2265
self.assertEqual(29, entry2[1][0][2])
2266
entry2_state = entry2[1][0]
2267
# Now, make sure that we don't have to re-read the content. The
2268
# packed_stat should match exactly.
2269
self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
2270
self.assertEqual(entry2_sha,
2271
target.get_file_sha1('file2-id', 'dir/file2'))
2272
self.assertEqual(entry1_state, entry1[1][0])
2273
self.assertEqual(entry2_state, entry2[1][0])
1876
2276
class TestCommitTransform(tests.TestCaseWithTransport):
1973
2378
self.assertRaises(errors.MalformedTransform, tt.commit, branch,
1977
class MockTransform(object):
1979
def has_named_child(self, by_parent, parent_id, name):
1980
for child_id in by_parent[parent_id]:
1984
elif name == "name.~%s~" % child_id:
1989
class MockEntry(object):
1991
object.__init__(self)
1995
class TestGetBackupName(TestCase):
1996
def test_get_backup_name(self):
1997
tt = MockTransform()
1998
name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
1999
self.assertEqual(name, 'name.~1~')
2000
name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
2001
self.assertEqual(name, 'name.~2~')
2002
name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
2003
self.assertEqual(name, 'name.~1~')
2004
name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
2005
self.assertEqual(name, 'name.~1~')
2006
name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
2007
self.assertEqual(name, 'name.~4~')
2381
def test_commit_rich_revision_data(self):
2382
branch, tt = self.get_branch_and_transform()
2383
rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2384
committer='me <me@example.com>',
2385
revprops={'foo': 'bar'}, revision_id='revid-1',
2386
authors=['Author1 <author1@example.com>',
2387
'Author2 <author2@example.com>',
2389
self.assertEqual('revid-1', rev_id)
2390
revision = branch.repository.get_revision(rev_id)
2391
self.assertEqual(1, revision.timestamp)
2392
self.assertEqual(43201, revision.timezone)
2393
self.assertEqual('me <me@example.com>', revision.committer)
2394
self.assertEqual(['Author1 <author1@example.com>',
2395
'Author2 <author2@example.com>'],
2396
revision.get_apparent_authors())
2397
del revision.properties['authors']
2398
self.assertEqual({'foo': 'bar',
2399
'branch-nick': 'tree'},
2400
revision.properties)
2402
def test_no_explicit_revprops(self):
2403
branch, tt = self.get_branch_and_transform()
2404
rev_id = tt.commit(branch, 'message', authors=[
2405
'Author1 <author1@example.com>',
2406
'Author2 <author2@example.com>', ])
2407
revision = branch.repository.get_revision(rev_id)
2408
self.assertEqual(['Author1 <author1@example.com>',
2409
'Author2 <author2@example.com>'],
2410
revision.get_apparent_authors())
2411
self.assertEqual('tree', revision.properties['branch-nick'])
2010
2414
class TestFileMover(tests.TestCaseWithTransport):
2013
2417
self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2014
2418
mover = _FileMover()
2015
2419
mover.rename('a', 'q')
2016
self.failUnlessExists('q')
2017
self.failIfExists('a')
2018
self.failUnlessExists('q/b')
2019
self.failUnlessExists('c')
2020
self.failUnlessExists('c/d')
2420
self.assertPathExists('q')
2421
self.assertPathDoesNotExist('a')
2422
self.assertPathExists('q/b')
2423
self.assertPathExists('c')
2424
self.assertPathExists('c/d')
2022
2426
def test_pre_delete_rollback(self):
2023
2427
self.build_tree(['a/'])
2024
2428
mover = _FileMover()
2025
2429
mover.pre_delete('a', 'q')
2026
self.failUnlessExists('q')
2027
self.failIfExists('a')
2430
self.assertPathExists('q')
2431
self.assertPathDoesNotExist('a')
2028
2432
mover.rollback()
2029
self.failIfExists('q')
2030
self.failUnlessExists('a')
2433
self.assertPathDoesNotExist('q')
2434
self.assertPathExists('a')
2032
2436
def test_apply_deletions(self):
2033
2437
self.build_tree(['a/', 'b/'])
2034
2438
mover = _FileMover()
2035
2439
mover.pre_delete('a', 'q')
2036
2440
mover.pre_delete('b', 'r')
2037
self.failUnlessExists('q')
2038
self.failUnlessExists('r')
2039
self.failIfExists('a')
2040
self.failIfExists('b')
2441
self.assertPathExists('q')
2442
self.assertPathExists('r')
2443
self.assertPathDoesNotExist('a')
2444
self.assertPathDoesNotExist('b')
2041
2445
mover.apply_deletions()
2042
self.failIfExists('q')
2043
self.failIfExists('r')
2044
self.failIfExists('a')
2045
self.failIfExists('b')
2446
self.assertPathDoesNotExist('q')
2447
self.assertPathDoesNotExist('r')
2448
self.assertPathDoesNotExist('a')
2449
self.assertPathDoesNotExist('b')
2047
2451
def test_file_mover_rollback(self):
2048
2452
self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2122
2526
tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2123
2527
self.assertRaises(Bogus, tt.apply,
2124
2528
_mover=self.ExceptionFileMover(bad_target='d'))
2125
self.failUnlessExists('a')
2126
self.failUnlessExists('a/b')
2128
def test_resolve_no_parent(self):
2529
self.assertPathExists('a')
2530
self.assertPathExists('a/b')
2533
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2534
"""Ensure treetransform creation errors can be safely cleaned up after"""
2536
def _override_globals_in_method(self, instance, method_name, globals):
2537
"""Replace method on instance with one with updated globals"""
2539
func = getattr(instance, method_name).im_func
2540
new_globals = dict(func.func_globals)
2541
new_globals.update(globals)
2542
new_func = types.FunctionType(func.func_code, new_globals,
2543
func.func_name, func.func_defaults)
2544
setattr(instance, method_name,
2545
types.MethodType(new_func, instance, instance.__class__))
2546
self.addCleanup(delattr, instance, method_name)
2549
def _fake_open_raises_before(name, mode):
2550
"""Like open() but raises before doing anything"""
2554
def _fake_open_raises_after(name, mode):
2555
"""Like open() but raises after creating file without returning"""
2556
open(name, mode).close()
2559
def create_transform_and_root_trans_id(self):
2560
"""Setup a transform creating a file in limbo"""
2561
tree = self.make_branch_and_tree('.')
2562
tt = TreeTransform(tree)
2563
return tt, tt.create_path("a", tt.root)
2565
def create_transform_and_subdir_trans_id(self):
2566
"""Setup a transform creating a directory containing a file in limbo"""
2567
tree = self.make_branch_and_tree('.')
2568
tt = TreeTransform(tree)
2569
d_trans_id = tt.create_path("d", tt.root)
2570
tt.create_directory(d_trans_id)
2571
f_trans_id = tt.create_path("a", d_trans_id)
2572
tt.adjust_path("a", d_trans_id, f_trans_id)
2573
return tt, f_trans_id
2575
def test_root_create_file_open_raises_before_creation(self):
2576
tt, trans_id = self.create_transform_and_root_trans_id()
2577
self._override_globals_in_method(tt, "create_file",
2578
{"open": self._fake_open_raises_before})
2579
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2580
path = tt._limbo_name(trans_id)
2581
self.assertPathDoesNotExist(path)
2583
self.assertPathDoesNotExist(tt._limbodir)
2585
def test_root_create_file_open_raises_after_creation(self):
2586
tt, trans_id = self.create_transform_and_root_trans_id()
2587
self._override_globals_in_method(tt, "create_file",
2588
{"open": self._fake_open_raises_after})
2589
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2590
path = tt._limbo_name(trans_id)
2591
self.assertPathExists(path)
2593
self.assertPathDoesNotExist(path)
2594
self.assertPathDoesNotExist(tt._limbodir)
2596
def test_subdir_create_file_open_raises_before_creation(self):
2597
tt, trans_id = self.create_transform_and_subdir_trans_id()
2598
self._override_globals_in_method(tt, "create_file",
2599
{"open": self._fake_open_raises_before})
2600
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2601
path = tt._limbo_name(trans_id)
2602
self.assertPathDoesNotExist(path)
2604
self.assertPathDoesNotExist(tt._limbodir)
2606
def test_subdir_create_file_open_raises_after_creation(self):
2607
tt, trans_id = self.create_transform_and_subdir_trans_id()
2608
self._override_globals_in_method(tt, "create_file",
2609
{"open": self._fake_open_raises_after})
2610
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2611
path = tt._limbo_name(trans_id)
2612
self.assertPathExists(path)
2614
self.assertPathDoesNotExist(path)
2615
self.assertPathDoesNotExist(tt._limbodir)
2617
def test_rename_in_limbo_rename_raises_after_rename(self):
2618
tt, trans_id = self.create_transform_and_root_trans_id()
2619
parent1 = tt.new_directory('parent1', tt.root)
2620
child1 = tt.new_file('child1', parent1, 'contents')
2621
parent2 = tt.new_directory('parent2', tt.root)
2623
class FakeOSModule(object):
2624
def rename(self, old, new):
2627
self._override_globals_in_method(tt, "_rename_in_limbo",
2628
{"os": FakeOSModule()})
2630
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2631
path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2632
self.assertPathExists(path)
2634
self.assertPathDoesNotExist(path)
2635
self.assertPathDoesNotExist(tt._limbodir)
2637
def test_rename_in_limbo_rename_raises_before_rename(self):
2638
tt, trans_id = self.create_transform_and_root_trans_id()
2639
parent1 = tt.new_directory('parent1', tt.root)
2640
child1 = tt.new_file('child1', parent1, 'contents')
2641
parent2 = tt.new_directory('parent2', tt.root)
2643
class FakeOSModule(object):
2644
def rename(self, old, new):
2646
self._override_globals_in_method(tt, "_rename_in_limbo",
2647
{"os": FakeOSModule()})
2649
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2650
path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2651
self.assertPathExists(path)
2653
self.assertPathDoesNotExist(path)
2654
self.assertPathDoesNotExist(tt._limbodir)
2657
class TestTransformMissingParent(tests.TestCaseWithTransport):
2659
def make_tt_with_versioned_dir(self):
2129
2660
wt = self.make_branch_and_tree('.')
2661
self.build_tree(['dir/',])
2662
wt.add(['dir'], ['dir-id'])
2663
wt.commit('Create dir')
2130
2664
tt = TreeTransform(wt)
2131
2665
self.addCleanup(tt.finalize)
2132
parent = tt.trans_id_file_id('parent-id')
2133
tt.new_file('file', parent, 'Contents')
2134
resolve_conflicts(tt)
2668
def test_resolve_create_parent_for_versioned_file(self):
2669
wt, tt = self.make_tt_with_versioned_dir()
2670
dir_tid = tt.trans_id_tree_file_id('dir-id')
2671
file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
2672
tt.delete_contents(dir_tid)
2673
tt.unversion_file(dir_tid)
2674
conflicts = resolve_conflicts(tt)
2675
# one conflict for the missing directory, one for the unversioned
2677
self.assertLength(2, conflicts)
2679
def test_non_versioned_file_create_conflict(self):
2680
wt, tt = self.make_tt_with_versioned_dir()
2681
dir_tid = tt.trans_id_tree_file_id('dir-id')
2682
tt.new_file('file', dir_tid, 'Contents')
2683
tt.delete_contents(dir_tid)
2684
tt.unversion_file(dir_tid)
2685
conflicts = resolve_conflicts(tt)
2686
# no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
2687
self.assertLength(1, conflicts)
2688
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
2137
2692
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2138
2693
('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2139
2694
(False, False))
2140
2695
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2141
('', ''), ('directory', 'directory'), (False, None))
2696
('', ''), ('directory', 'directory'), (False, False))
2144
2699
class TestTransformPreview(tests.TestCaseWithTransport):
2998
3599
trans_id = tt.trans_id_tree_path('file')
2999
3600
self.assertEqual((LINES_ONE,),
3000
3601
tt._get_parents_texts(trans_id))
3604
class TestOrphan(tests.TestCaseWithTransport):
3606
def test_no_orphan_for_transform_preview(self):
3607
tree = self.make_branch_and_tree('tree')
3608
tt = transform.TransformPreview(tree)
3609
self.addCleanup(tt.finalize)
3610
self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3612
def _set_orphan_policy(self, wt, policy):
3613
wt.branch.get_config_stack().set('bzr.transform.orphan_policy',
3616
def _prepare_orphan(self, wt):
3617
self.build_tree(['dir/', 'dir/file', 'dir/foo'])
3618
wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
3619
wt.commit('add dir and file ignoring foo')
3620
tt = transform.TreeTransform(wt)
3621
self.addCleanup(tt.finalize)
3622
# dir and bar are deleted
3623
dir_tid = tt.trans_id_tree_path('dir')
3624
file_tid = tt.trans_id_tree_path('dir/file')
3625
orphan_tid = tt.trans_id_tree_path('dir/foo')
3626
tt.delete_contents(file_tid)
3627
tt.unversion_file(file_tid)
3628
tt.delete_contents(dir_tid)
3629
tt.unversion_file(dir_tid)
3630
# There should be a conflict because dir still contain foo
3631
raw_conflicts = tt.find_conflicts()
3632
self.assertLength(1, raw_conflicts)
3633
self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
3634
return tt, orphan_tid
3636
def test_new_orphan_created(self):
3637
wt = self.make_branch_and_tree('.')
3638
self._set_orphan_policy(wt, 'move')
3639
tt, orphan_tid = self._prepare_orphan(wt)
3642
warnings.append(args[0] % args[1:])
3643
self.overrideAttr(trace, 'warning', warning)
3644
remaining_conflicts = resolve_conflicts(tt)
3645
self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3647
# Yeah for resolved conflicts !
3648
self.assertLength(0, remaining_conflicts)
3649
# We have a new orphan
3650
self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3651
self.assertEquals('bzr-orphans',
3652
tt.final_name(tt.final_parent(orphan_tid)))
3654
def test_never_orphan(self):
3655
wt = self.make_branch_and_tree('.')
3656
self._set_orphan_policy(wt, 'conflict')
3657
tt, orphan_tid = self._prepare_orphan(wt)
3658
remaining_conflicts = resolve_conflicts(tt)
3659
self.assertLength(1, remaining_conflicts)
3660
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3661
remaining_conflicts.pop())
3663
def test_orphan_error(self):
3664
def bogus_orphan(tt, orphan_id, parent_id):
3665
raise transform.OrphaningError(tt.final_name(orphan_id),
3666
tt.final_name(parent_id))
3667
transform.orphaning_registry.register('bogus', bogus_orphan,
3668
'Raise an error when orphaning')
3669
wt = self.make_branch_and_tree('.')
3670
self._set_orphan_policy(wt, 'bogus')
3671
tt, orphan_tid = self._prepare_orphan(wt)
3672
remaining_conflicts = resolve_conflicts(tt)
3673
self.assertLength(1, remaining_conflicts)
3674
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3675
remaining_conflicts.pop())
3677
def test_unknown_orphan_policy(self):
3678
wt = self.make_branch_and_tree('.')
3679
# Set a fictional policy nobody ever implemented
3680
self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3681
tt, orphan_tid = self._prepare_orphan(wt)
3684
warnings.append(args[0] % args[1:])
3685
self.overrideAttr(trace, 'warning', warning)
3686
remaining_conflicts = resolve_conflicts(tt)
3687
# We fallback to the default policy which create a conflict
3688
self.assertLength(1, remaining_conflicts)
3689
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3690
remaining_conflicts.pop())
3691
self.assertLength(1, warnings)
3692
self.assertStartsWith(warnings[0], 'Value "donttouchmypreciouuus" ')
3695
class TestTransformHooks(tests.TestCaseWithTransport):
3698
super(TestTransformHooks, self).setUp()
3699
self.wt = self.make_branch_and_tree('.')
3702
def get_transform(self):
3703
transform = TreeTransform(self.wt)
3704
self.addCleanup(transform.finalize)
3705
return transform, transform.root
3707
def test_pre_commit_hooks(self):
3709
def record_pre_transform(tree, tt):
3710
calls.append((tree, tt))
3711
MutableTree.hooks.install_named_hook('pre_transform',
3712
record_pre_transform, "Pre transform")
3713
transform, root = self.get_transform()
3714
old_root_id = transform.tree_file_id(root)
3716
self.assertEqual(old_root_id, self.wt.get_root_id())
3717
self.assertEquals([(self.wt, transform)], calls)
3719
def test_post_commit_hooks(self):
3721
def record_post_transform(tree, tt):
3722
calls.append((tree, tt))
3723
MutableTree.hooks.install_named_hook('post_transform',
3724
record_post_transform, "Post transform")
3725
transform, root = self.get_transform()
3726
old_root_id = transform.tree_file_id(root)
3728
self.assertEqual(old_root_id, self.wt.get_root_id())
3729
self.assertEquals([(self.wt, transform)], calls)