29
29
revision as _mod_revision,
34
37
from bzrlib.bzrdir import BzrDir
35
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
36
UnversionedParent, ParentLoop, DeletingParent,
38
from bzrlib.conflicts import (
38
47
from bzrlib.diff import show_diff_trees
39
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
40
ReusingTransform, CantMoveRoot,
41
PathsNotVersionedError, ExistingLimbo,
42
ExistingPendingDeletion, ImmortalLimbo,
43
ImmortalPendingDeletion, LockError)
44
from bzrlib.osutils import file_kind, pathjoin
48
from bzrlib.errors import (
51
ExistingPendingDeletion,
53
ImmortalPendingDeletion,
58
from bzrlib.osutils import (
45
62
from bzrlib.merge import Merge3Merger, Merger
46
63
from bzrlib.tests import (
68
from bzrlib.tests.features import (
53
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths,
54
resolve_conflicts, cook_conflicts,
55
build_tree, get_backup_name,
56
_FileMover, resolve_checkout,
57
TransformPreview, create_from_tree)
72
from bzrlib.transform import (
60
86
class TestTreeTransform(tests.TestCaseWithTransport):
137
173
transform.finalize()
138
174
transform.finalize()
176
def test_apply_informs_tree_of_observed_sha1(self):
177
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
178
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
181
orig = self.wt._observed_sha1
182
def _observed_sha1(*args):
185
self.wt._observed_sha1 = _observed_sha1
187
self.assertEqual([(None, 'file1', trans._observed_sha1s[trans_id])],
190
def test_create_file_caches_sha1(self):
191
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
192
trans_id = trans.create_path('file1', root)
193
trans.create_file(contents, trans_id, sha1=sha1)
194
st_val = osutils.lstat(trans._limbo_name(trans_id))
195
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
196
self.assertEqual(o_sha1, sha1)
197
self.assertEqualStat(o_st_val, st_val)
199
def test__apply_insertions_updates_sha1(self):
200
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
201
trans_id = trans.create_path('file1', root)
202
trans.create_file(contents, trans_id, sha1=sha1)
203
st_val = osutils.lstat(trans._limbo_name(trans_id))
204
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
205
self.assertEqual(o_sha1, sha1)
206
self.assertEqualStat(o_st_val, st_val)
207
creation_mtime = trans._creation_mtime + 10.0
208
# We fake a time difference from when the file was created until now it
209
# is being renamed by using os.utime. Note that the change we actually
210
# want to see is the real ctime change from 'os.rename()', but as long
211
# as we observe a new stat value, we should be fine.
212
os.utime(trans._limbo_name(trans_id), (creation_mtime, creation_mtime))
214
new_st_val = osutils.lstat(self.wt.abspath('file1'))
215
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
216
self.assertEqual(o_sha1, sha1)
217
self.assertEqualStat(o_st_val, new_st_val)
218
self.assertNotEqual(st_val.st_mtime, new_st_val.st_mtime)
220
def test_new_file_caches_sha1(self):
221
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
222
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
224
st_val = osutils.lstat(trans._limbo_name(trans_id))
225
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
226
self.assertEqual(o_sha1, sha1)
227
self.assertEqualStat(o_st_val, st_val)
229
def test_cancel_creation_removes_observed_sha1(self):
230
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
231
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
233
self.assertTrue(trans_id in trans._observed_sha1s)
234
trans.cancel_creation(trans_id)
235
self.assertFalse(trans_id in trans._observed_sha1s)
140
237
def test_create_files_same_timestamp(self):
141
238
transform, root = self.get_transform()
142
239
self.wt.lock_tree_write()
190
287
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
191
288
self.assertRaises(ValueError, transform.fixup_new_roots)
290
def test_retain_existing_root(self):
291
tt, root = self.get_transform()
293
tt.new_directory('', ROOT_PARENT, 'new-root-id')
295
self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
297
def test_retain_existing_root_added_file(self):
298
tt, root = self.get_transform()
299
new_trans_id = tt.new_directory('', ROOT_PARENT, 'new-root-id')
300
child = tt.new_directory('child', new_trans_id, 'child-id')
302
self.assertEqual(tt.root, tt.final_parent(child))
304
def test_add_unversioned_root(self):
305
transform, root = self.get_transform()
306
new_trans_id = transform.new_directory('', ROOT_PARENT, None)
307
transform.delete_contents(transform.root)
308
transform.fixup_new_roots()
309
self.assertNotIn(transform.root, transform._new_id)
311
def test_remove_root_fixup(self):
312
transform, root = self.get_transform()
313
old_root_id = self.wt.get_root_id()
314
self.assertNotEqual('new-root-id', old_root_id)
315
transform.delete_contents(root)
316
transform.unversion_file(root)
317
transform.fixup_new_roots()
319
self.assertEqual(old_root_id, self.wt.get_root_id())
321
transform, root = self.get_transform()
322
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
323
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
324
self.assertRaises(ValueError, transform.fixup_new_roots)
326
def test_fixup_new_roots_permits_empty_tree(self):
327
transform, root = self.get_transform()
328
transform.delete_contents(root)
329
transform.unversion_file(root)
330
transform.fixup_new_roots()
331
self.assertIs(None, transform.final_kind(root))
332
self.assertIs(None, transform.final_file_id(root))
334
def test_apply_retains_root_directory(self):
335
# Do not attempt to delete the physical root directory, because that
337
transform, root = self.get_transform()
339
transform.delete_contents(root)
340
e = self.assertRaises(AssertionError, self.assertRaises,
341
errors.TransformRenameFailed,
343
self.assertContainsRe('TransformRenameFailed not raised', str(e))
345
def test_apply_retains_file_id(self):
346
transform, root = self.get_transform()
347
old_root_id = transform.tree_file_id(root)
348
transform.unversion_file(root)
350
self.assertEqual(old_root_id, self.wt.get_root_id())
193
352
def test_hardlink(self):
194
353
self.requireFeature(HardlinkFeature)
195
354
transform, root = self.get_transform()
830
1004
rename.set_executability(True, myfile)
1007
def test_rename_fails(self):
1008
self.requireFeature(features.not_running_as_root)
1009
# see https://bugs.launchpad.net/bzr/+bug/491763
1010
create, root_id = self.get_transform()
1011
first_dir = create.new_directory('first-dir', root_id, 'first-id')
1012
myfile = create.new_file('myfile', root_id, 'myfile-text',
1015
if os.name == "posix" and sys.platform != "cygwin":
1016
# posix filesystems fail on renaming if the readonly bit is set
1017
osutils.make_readonly(self.wt.abspath('first-dir'))
1018
elif os.name == "nt":
1019
# windows filesystems fail on renaming open files
1020
self.addCleanup(file(self.wt.abspath('myfile')).close)
1022
self.skip("Don't know how to force a permissions error on rename")
1023
# now transform to rename
1024
rename_transform, root_id = self.get_transform()
1025
file_trans_id = rename_transform.trans_id_file_id('myfile-id')
1026
dir_id = rename_transform.trans_id_file_id('first-id')
1027
rename_transform.adjust_path('newname', dir_id, file_trans_id)
1028
e = self.assertRaises(errors.TransformRenameFailed,
1029
rename_transform.apply)
1030
# On nix looks like:
1031
# "Failed to rename .../work/.bzr/checkout/limbo/new-1
1032
# to .../first-dir/newname: [Errno 13] Permission denied"
1033
# On windows looks like:
1034
# "Failed to rename .../work/myfile to
1035
# .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
1036
# This test isn't concerned with exactly what the error looks like,
1037
# and the strerror will vary across OS and locales, but the assert
1038
# that the exeception attributes are what we expect
1039
self.assertEqual(e.errno, errno.EACCES)
1040
if os.name == "posix":
1041
self.assertEndsWith(e.to_path, "/first-dir/newname")
1043
self.assertEqual(os.path.basename(e.from_path), "myfile")
833
1045
def test_set_executability_order(self):
834
1046
"""Ensure that executability behaves the same, no matter what order.
1451
1660
return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1663
class TestInventoryAltered(tests.TestCaseWithTransport):
1665
def test_inventory_altered_unchanged(self):
1666
tree = self.make_branch_and_tree('tree')
1667
self.build_tree(['tree/foo'])
1668
tree.add('foo', 'foo-id')
1669
with TransformPreview(tree) as tt:
1670
self.assertEqual([], tt._inventory_altered())
1672
def test_inventory_altered_changed_parent_id(self):
1673
tree = self.make_branch_and_tree('tree')
1674
self.build_tree(['tree/foo'])
1675
tree.add('foo', 'foo-id')
1676
with TransformPreview(tree) as tt:
1677
tt.unversion_file(tt.root)
1678
tt.version_file('new-id', tt.root)
1679
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1680
foo_tuple = ('foo', foo_trans_id)
1681
root_tuple = ('', tt.root)
1682
self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1684
def test_inventory_altered_noop_changed_parent_id(self):
1685
tree = self.make_branch_and_tree('tree')
1686
self.build_tree(['tree/foo'])
1687
tree.add('foo', 'foo-id')
1688
with TransformPreview(tree) as tt:
1689
tt.unversion_file(tt.root)
1690
tt.version_file(tree.get_root_id(), tt.root)
1691
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1692
self.assertEqual([], tt._inventory_altered())
1454
1695
class TestTransformMerge(TestCaseInTempDir):
1456
1697
def test_text_merge(self):
1985
2238
self.assertEqual('file.moved', target.id2path('lower-id'))
1986
2239
self.assertEqual('FILE', target.id2path('upper-id'))
2241
def test_build_tree_observes_sha(self):
2242
source = self.make_branch_and_tree('source')
2243
self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
2244
source.add(['file1', 'dir', 'dir/file2'],
2245
['file1-id', 'dir-id', 'file2-id'])
2246
source.commit('new files')
2247
target = self.make_branch_and_tree('target')
2249
self.addCleanup(target.unlock)
2250
# We make use of the fact that DirState caches its cutoff time. So we
2251
# set the 'safe' time to one minute in the future.
2252
state = target.current_dirstate()
2253
state._cutoff_time = time.time() + 60
2254
build_tree(source.basis_tree(), target)
2255
entry1_sha = osutils.sha_file_by_name('source/file1')
2256
entry2_sha = osutils.sha_file_by_name('source/dir/file2')
2257
# entry[1] is the state information, entry[1][0] is the state of the
2258
# working tree, entry[1][0][1] is the sha value for the current working
2260
entry1 = state._get_entry(0, path_utf8='file1')
2261
self.assertEqual(entry1_sha, entry1[1][0][1])
2262
# The 'size' field must also be set.
2263
self.assertEqual(25, entry1[1][0][2])
2264
entry1_state = entry1[1][0]
2265
entry2 = state._get_entry(0, path_utf8='dir/file2')
2266
self.assertEqual(entry2_sha, entry2[1][0][1])
2267
self.assertEqual(29, entry2[1][0][2])
2268
entry2_state = entry2[1][0]
2269
# Now, make sure that we don't have to re-read the content. The
2270
# packed_stat should match exactly.
2271
self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
2272
self.assertEqual(entry2_sha,
2273
target.get_file_sha1('file2-id', 'dir/file2'))
2274
self.assertEqual(entry1_state, entry1[1][0])
2275
self.assertEqual(entry2_state, entry2[1][0])
1989
2278
class TestCommitTransform(tests.TestCaseWithTransport):
2091
2380
self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2095
class MockTransform(object):
2097
def has_named_child(self, by_parent, parent_id, name):
2098
for child_id in by_parent[parent_id]:
2102
elif name == "name.~%s~" % child_id:
2107
class MockEntry(object):
2109
object.__init__(self)
2113
class TestGetBackupName(TestCase):
2114
def test_get_backup_name(self):
2383
def test_commit_rich_revision_data(self):
2384
branch, tt = self.get_branch_and_transform()
2385
rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2386
committer='me <me@example.com>',
2387
revprops={'foo': 'bar'}, revision_id='revid-1',
2388
authors=['Author1 <author1@example.com>',
2389
'Author2 <author2@example.com>',
2391
self.assertEqual('revid-1', rev_id)
2392
revision = branch.repository.get_revision(rev_id)
2393
self.assertEqual(1, revision.timestamp)
2394
self.assertEqual(43201, revision.timezone)
2395
self.assertEqual('me <me@example.com>', revision.committer)
2396
self.assertEqual(['Author1 <author1@example.com>',
2397
'Author2 <author2@example.com>'],
2398
revision.get_apparent_authors())
2399
del revision.properties['authors']
2400
self.assertEqual({'foo': 'bar',
2401
'branch-nick': 'tree'},
2402
revision.properties)
2404
def test_no_explicit_revprops(self):
2405
branch, tt = self.get_branch_and_transform()
2406
rev_id = tt.commit(branch, 'message', authors=[
2407
'Author1 <author1@example.com>',
2408
'Author2 <author2@example.com>', ])
2409
revision = branch.repository.get_revision(rev_id)
2410
self.assertEqual(['Author1 <author1@example.com>',
2411
'Author2 <author2@example.com>'],
2412
revision.get_apparent_authors())
2413
self.assertEqual('tree', revision.properties['branch-nick'])
2416
class TestBackupName(tests.TestCase):
2418
def test_deprecations(self):
2419
class MockTransform(object):
2421
def has_named_child(self, by_parent, parent_id, name):
2422
return name in by_parent.get(parent_id, [])
2424
class MockEntry(object):
2427
object.__init__(self)
2115
2430
tt = MockTransform()
2116
name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
2117
self.assertEqual(name, 'name.~1~')
2118
name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
2119
self.assertEqual(name, 'name.~2~')
2120
name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
2121
self.assertEqual(name, 'name.~1~')
2122
name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
2123
self.assertEqual(name, 'name.~1~')
2124
name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
2125
self.assertEqual(name, 'name.~4~')
2431
name1 = self.applyDeprecated(
2432
symbol_versioning.deprecated_in((2, 3, 0)),
2433
transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2434
self.assertEqual('name.~1~', name1)
2435
name2 = self.applyDeprecated(
2436
symbol_versioning.deprecated_in((2, 3, 0)),
2437
transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2438
self.assertEqual('name.~2~', name2)
2128
2441
class TestFileMover(tests.TestCaseWithTransport):
2131
2444
self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2132
2445
mover = _FileMover()
2133
2446
mover.rename('a', 'q')
2134
self.failUnlessExists('q')
2135
self.failIfExists('a')
2136
self.failUnlessExists('q/b')
2137
self.failUnlessExists('c')
2138
self.failUnlessExists('c/d')
2447
self.assertPathExists('q')
2448
self.assertPathDoesNotExist('a')
2449
self.assertPathExists('q/b')
2450
self.assertPathExists('c')
2451
self.assertPathExists('c/d')
2140
2453
def test_pre_delete_rollback(self):
2141
2454
self.build_tree(['a/'])
2142
2455
mover = _FileMover()
2143
2456
mover.pre_delete('a', 'q')
2144
self.failUnlessExists('q')
2145
self.failIfExists('a')
2457
self.assertPathExists('q')
2458
self.assertPathDoesNotExist('a')
2146
2459
mover.rollback()
2147
self.failIfExists('q')
2148
self.failUnlessExists('a')
2460
self.assertPathDoesNotExist('q')
2461
self.assertPathExists('a')
2150
2463
def test_apply_deletions(self):
2151
2464
self.build_tree(['a/', 'b/'])
2152
2465
mover = _FileMover()
2153
2466
mover.pre_delete('a', 'q')
2154
2467
mover.pre_delete('b', 'r')
2155
self.failUnlessExists('q')
2156
self.failUnlessExists('r')
2157
self.failIfExists('a')
2158
self.failIfExists('b')
2468
self.assertPathExists('q')
2469
self.assertPathExists('r')
2470
self.assertPathDoesNotExist('a')
2471
self.assertPathDoesNotExist('b')
2159
2472
mover.apply_deletions()
2160
self.failIfExists('q')
2161
self.failIfExists('r')
2162
self.failIfExists('a')
2163
self.failIfExists('b')
2473
self.assertPathDoesNotExist('q')
2474
self.assertPathDoesNotExist('r')
2475
self.assertPathDoesNotExist('a')
2476
self.assertPathDoesNotExist('b')
2165
2478
def test_file_mover_rollback(self):
2166
2479
self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2240
2553
tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2241
2554
self.assertRaises(Bogus, tt.apply,
2242
2555
_mover=self.ExceptionFileMover(bad_target='d'))
2243
self.failUnlessExists('a')
2244
self.failUnlessExists('a/b')
2246
def test_resolve_no_parent(self):
2556
self.assertPathExists('a')
2557
self.assertPathExists('a/b')
2560
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2561
"""Ensure treetransform creation errors can be safely cleaned up after"""
2563
def _override_globals_in_method(self, instance, method_name, globals):
2564
"""Replace method on instance with one with updated globals"""
2566
func = getattr(instance, method_name).im_func
2567
new_globals = dict(func.func_globals)
2568
new_globals.update(globals)
2569
new_func = types.FunctionType(func.func_code, new_globals,
2570
func.func_name, func.func_defaults)
2571
setattr(instance, method_name,
2572
types.MethodType(new_func, instance, instance.__class__))
2573
self.addCleanup(delattr, instance, method_name)
2576
def _fake_open_raises_before(name, mode):
2577
"""Like open() but raises before doing anything"""
2581
def _fake_open_raises_after(name, mode):
2582
"""Like open() but raises after creating file without returning"""
2583
open(name, mode).close()
2586
def create_transform_and_root_trans_id(self):
2587
"""Setup a transform creating a file in limbo"""
2588
tree = self.make_branch_and_tree('.')
2589
tt = TreeTransform(tree)
2590
return tt, tt.create_path("a", tt.root)
2592
def create_transform_and_subdir_trans_id(self):
2593
"""Setup a transform creating a directory containing a file in limbo"""
2594
tree = self.make_branch_and_tree('.')
2595
tt = TreeTransform(tree)
2596
d_trans_id = tt.create_path("d", tt.root)
2597
tt.create_directory(d_trans_id)
2598
f_trans_id = tt.create_path("a", d_trans_id)
2599
tt.adjust_path("a", d_trans_id, f_trans_id)
2600
return tt, f_trans_id
2602
def test_root_create_file_open_raises_before_creation(self):
2603
tt, trans_id = self.create_transform_and_root_trans_id()
2604
self._override_globals_in_method(tt, "create_file",
2605
{"open": self._fake_open_raises_before})
2606
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2607
path = tt._limbo_name(trans_id)
2608
self.assertPathDoesNotExist(path)
2610
self.assertPathDoesNotExist(tt._limbodir)
2612
def test_root_create_file_open_raises_after_creation(self):
2613
tt, trans_id = self.create_transform_and_root_trans_id()
2614
self._override_globals_in_method(tt, "create_file",
2615
{"open": self._fake_open_raises_after})
2616
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2617
path = tt._limbo_name(trans_id)
2618
self.assertPathExists(path)
2620
self.assertPathDoesNotExist(path)
2621
self.assertPathDoesNotExist(tt._limbodir)
2623
def test_subdir_create_file_open_raises_before_creation(self):
2624
tt, trans_id = self.create_transform_and_subdir_trans_id()
2625
self._override_globals_in_method(tt, "create_file",
2626
{"open": self._fake_open_raises_before})
2627
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2628
path = tt._limbo_name(trans_id)
2629
self.assertPathDoesNotExist(path)
2631
self.assertPathDoesNotExist(tt._limbodir)
2633
def test_subdir_create_file_open_raises_after_creation(self):
2634
tt, trans_id = self.create_transform_and_subdir_trans_id()
2635
self._override_globals_in_method(tt, "create_file",
2636
{"open": self._fake_open_raises_after})
2637
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2638
path = tt._limbo_name(trans_id)
2639
self.assertPathExists(path)
2641
self.assertPathDoesNotExist(path)
2642
self.assertPathDoesNotExist(tt._limbodir)
2644
def test_rename_in_limbo_rename_raises_after_rename(self):
2645
tt, trans_id = self.create_transform_and_root_trans_id()
2646
parent1 = tt.new_directory('parent1', tt.root)
2647
child1 = tt.new_file('child1', parent1, 'contents')
2648
parent2 = tt.new_directory('parent2', tt.root)
2650
class FakeOSModule(object):
2651
def rename(self, old, new):
2654
self._override_globals_in_method(tt, "_rename_in_limbo",
2655
{"os": FakeOSModule()})
2657
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2658
path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2659
self.assertPathExists(path)
2661
self.assertPathDoesNotExist(path)
2662
self.assertPathDoesNotExist(tt._limbodir)
2664
def test_rename_in_limbo_rename_raises_before_rename(self):
2665
tt, trans_id = self.create_transform_and_root_trans_id()
2666
parent1 = tt.new_directory('parent1', tt.root)
2667
child1 = tt.new_file('child1', parent1, 'contents')
2668
parent2 = tt.new_directory('parent2', tt.root)
2670
class FakeOSModule(object):
2671
def rename(self, old, new):
2673
self._override_globals_in_method(tt, "_rename_in_limbo",
2674
{"os": FakeOSModule()})
2676
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2677
path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2678
self.assertPathExists(path)
2680
self.assertPathDoesNotExist(path)
2681
self.assertPathDoesNotExist(tt._limbodir)
2684
class TestTransformMissingParent(tests.TestCaseWithTransport):
2686
def make_tt_with_versioned_dir(self):
2247
2687
wt = self.make_branch_and_tree('.')
2688
self.build_tree(['dir/',])
2689
wt.add(['dir'], ['dir-id'])
2690
wt.commit('Create dir')
2248
2691
tt = TreeTransform(wt)
2249
2692
self.addCleanup(tt.finalize)
2250
parent = tt.trans_id_file_id('parent-id')
2251
tt.new_file('file', parent, 'Contents')
2252
resolve_conflicts(tt)
2695
def test_resolve_create_parent_for_versioned_file(self):
2696
wt, tt = self.make_tt_with_versioned_dir()
2697
dir_tid = tt.trans_id_tree_file_id('dir-id')
2698
file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
2699
tt.delete_contents(dir_tid)
2700
tt.unversion_file(dir_tid)
2701
conflicts = resolve_conflicts(tt)
2702
# one conflict for the missing directory, one for the unversioned
2704
self.assertLength(2, conflicts)
2706
def test_non_versioned_file_create_conflict(self):
2707
wt, tt = self.make_tt_with_versioned_dir()
2708
dir_tid = tt.trans_id_tree_file_id('dir-id')
2709
tt.new_file('file', dir_tid, 'Contents')
2710
tt.delete_contents(dir_tid)
2711
tt.unversion_file(dir_tid)
2712
conflicts = resolve_conflicts(tt)
2713
# no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
2714
self.assertLength(1, conflicts)
2715
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
2255
2719
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2256
2720
('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2257
2721
(False, False))
2258
2722
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2259
('', ''), ('directory', 'directory'), (False, None))
2723
('', ''), ('directory', 'directory'), (False, False))
2262
2726
class TestTransformPreview(tests.TestCaseWithTransport):
3132
3626
trans_id = tt.trans_id_tree_path('file')
3133
3627
self.assertEqual((LINES_ONE,),
3134
3628
tt._get_parents_texts(trans_id))
3631
class TestOrphan(tests.TestCaseWithTransport):
3633
def test_no_orphan_for_transform_preview(self):
3634
tree = self.make_branch_and_tree('tree')
3635
tt = transform.TransformPreview(tree)
3636
self.addCleanup(tt.finalize)
3637
self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3639
def _set_orphan_policy(self, wt, policy):
3640
wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
3643
def _prepare_orphan(self, wt):
3644
self.build_tree(['dir/', 'dir/file', 'dir/foo'])
3645
wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
3646
wt.commit('add dir and file ignoring foo')
3647
tt = transform.TreeTransform(wt)
3648
self.addCleanup(tt.finalize)
3649
# dir and bar are deleted
3650
dir_tid = tt.trans_id_tree_path('dir')
3651
file_tid = tt.trans_id_tree_path('dir/file')
3652
orphan_tid = tt.trans_id_tree_path('dir/foo')
3653
tt.delete_contents(file_tid)
3654
tt.unversion_file(file_tid)
3655
tt.delete_contents(dir_tid)
3656
tt.unversion_file(dir_tid)
3657
# There should be a conflict because dir still contain foo
3658
raw_conflicts = tt.find_conflicts()
3659
self.assertLength(1, raw_conflicts)
3660
self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
3661
return tt, orphan_tid
3663
def test_new_orphan_created(self):
3664
wt = self.make_branch_and_tree('.')
3665
self._set_orphan_policy(wt, 'move')
3666
tt, orphan_tid = self._prepare_orphan(wt)
3669
warnings.append(args[0] % args[1:])
3670
self.overrideAttr(trace, 'warning', warning)
3671
remaining_conflicts = resolve_conflicts(tt)
3672
self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3674
# Yeah for resolved conflicts !
3675
self.assertLength(0, remaining_conflicts)
3676
# We have a new orphan
3677
self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3678
self.assertEquals('bzr-orphans',
3679
tt.final_name(tt.final_parent(orphan_tid)))
3681
def test_never_orphan(self):
3682
wt = self.make_branch_and_tree('.')
3683
self._set_orphan_policy(wt, 'conflict')
3684
tt, orphan_tid = self._prepare_orphan(wt)
3685
remaining_conflicts = resolve_conflicts(tt)
3686
self.assertLength(1, remaining_conflicts)
3687
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3688
remaining_conflicts.pop())
3690
def test_orphan_error(self):
3691
def bogus_orphan(tt, orphan_id, parent_id):
3692
raise transform.OrphaningError(tt.final_name(orphan_id),
3693
tt.final_name(parent_id))
3694
transform.orphaning_registry.register('bogus', bogus_orphan,
3695
'Raise an error when orphaning')
3696
wt = self.make_branch_and_tree('.')
3697
self._set_orphan_policy(wt, 'bogus')
3698
tt, orphan_tid = self._prepare_orphan(wt)
3699
remaining_conflicts = resolve_conflicts(tt)
3700
self.assertLength(1, remaining_conflicts)
3701
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3702
remaining_conflicts.pop())
3704
def test_unknown_orphan_policy(self):
3705
wt = self.make_branch_and_tree('.')
3706
# Set a fictional policy nobody ever implemented
3707
self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3708
tt, orphan_tid = self._prepare_orphan(wt)
3711
warnings.append(args[0] % args[1:])
3712
self.overrideAttr(trace, 'warning', warning)
3713
remaining_conflicts = resolve_conflicts(tt)
3714
# We fallback to the default policy which create a conflict
3715
self.assertLength(1, remaining_conflicts)
3716
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3717
remaining_conflicts.pop())
3718
self.assertLength(1, warnings)
3719
self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')