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 (
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)
70
from bzrlib.transform import (
60
84
class TestTreeTransform(tests.TestCaseWithTransport):
137
171
transform.finalize()
138
172
transform.finalize()
174
def test_apply_informs_tree_of_observed_sha1(self):
175
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
176
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
179
orig = self.wt._observed_sha1
180
def _observed_sha1(*args):
183
self.wt._observed_sha1 = _observed_sha1
185
self.assertEqual([(None, 'file1', trans._observed_sha1s[trans_id])],
188
def test_create_file_caches_sha1(self):
189
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
190
trans_id = trans.create_path('file1', root)
191
trans.create_file(contents, trans_id, sha1=sha1)
192
st_val = osutils.lstat(trans._limbo_name(trans_id))
193
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
194
self.assertEqual(o_sha1, sha1)
195
self.assertEqualStat(o_st_val, st_val)
197
def test__apply_insertions_updates_sha1(self):
198
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
199
trans_id = trans.create_path('file1', root)
200
trans.create_file(contents, trans_id, sha1=sha1)
201
st_val = osutils.lstat(trans._limbo_name(trans_id))
202
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
203
self.assertEqual(o_sha1, sha1)
204
self.assertEqualStat(o_st_val, st_val)
205
creation_mtime = trans._creation_mtime + 10.0
206
# We fake a time difference from when the file was created until now it
207
# is being renamed by using os.utime. Note that the change we actually
208
# want to see is the real ctime change from 'os.rename()', but as long
209
# as we observe a new stat value, we should be fine.
210
os.utime(trans._limbo_name(trans_id), (creation_mtime, creation_mtime))
212
new_st_val = osutils.lstat(self.wt.abspath('file1'))
213
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
214
self.assertEqual(o_sha1, sha1)
215
self.assertEqualStat(o_st_val, new_st_val)
216
self.assertNotEqual(st_val.st_mtime, new_st_val.st_mtime)
218
def test_new_file_caches_sha1(self):
219
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
220
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
222
st_val = osutils.lstat(trans._limbo_name(trans_id))
223
o_sha1, o_st_val = trans._observed_sha1s[trans_id]
224
self.assertEqual(o_sha1, sha1)
225
self.assertEqualStat(o_st_val, st_val)
227
def test_cancel_creation_removes_observed_sha1(self):
228
trans, root, contents, sha1 = self.get_transform_for_sha1_test()
229
trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
231
self.assertTrue(trans_id in trans._observed_sha1s)
232
trans.cancel_creation(trans_id)
233
self.assertFalse(trans_id in trans._observed_sha1s)
140
235
def test_create_files_same_timestamp(self):
141
236
transform, root = self.get_transform()
142
237
self.wt.lock_tree_write()
830
957
rename.set_executability(True, myfile)
960
def test_rename_fails(self):
961
self.requireFeature(features.not_running_as_root)
962
# see https://bugs.launchpad.net/bzr/+bug/491763
963
create, root_id = self.get_transform()
964
first_dir = create.new_directory('first-dir', root_id, 'first-id')
965
myfile = create.new_file('myfile', root_id, 'myfile-text',
968
if os.name == "posix" and sys.platform != "cygwin":
969
# posix filesystems fail on renaming if the readonly bit is set
970
osutils.make_readonly(self.wt.abspath('first-dir'))
971
elif os.name == "nt":
972
# windows filesystems fail on renaming open files
973
self.addCleanup(file(self.wt.abspath('myfile')).close)
975
self.skip("Don't know how to force a permissions error on rename")
976
# now transform to rename
977
rename_transform, root_id = self.get_transform()
978
file_trans_id = rename_transform.trans_id_file_id('myfile-id')
979
dir_id = rename_transform.trans_id_file_id('first-id')
980
rename_transform.adjust_path('newname', dir_id, file_trans_id)
981
e = self.assertRaises(errors.TransformRenameFailed,
982
rename_transform.apply)
984
# "Failed to rename .../work/.bzr/checkout/limbo/new-1
985
# to .../first-dir/newname: [Errno 13] Permission denied"
986
# On windows looks like:
987
# "Failed to rename .../work/myfile to
988
# .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
989
# This test isn't concerned with exactly what the error looks like,
990
# and the strerror will vary across OS and locales, but the assert
991
# that the exeception attributes are what we expect
992
self.assertEqual(e.errno, errno.EACCES)
993
if os.name == "posix":
994
self.assertEndsWith(e.to_path, "/first-dir/newname")
996
self.assertEqual(os.path.basename(e.from_path), "myfile")
833
998
def test_set_executability_order(self):
834
999
"""Ensure that executability behaves the same, no matter what order.
1451
1616
return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1619
class TestInventoryAltered(tests.TestCaseWithTransport):
1621
def test_inventory_altered_unchanged(self):
1622
tree = self.make_branch_and_tree('tree')
1623
self.build_tree(['tree/foo'])
1624
tree.add('foo', 'foo-id')
1625
with TransformPreview(tree) as tt:
1626
self.assertEqual([], tt._inventory_altered())
1628
def test_inventory_altered_changed_parent_id(self):
1629
tree = self.make_branch_and_tree('tree')
1630
self.build_tree(['tree/foo'])
1631
tree.add('foo', 'foo-id')
1632
with TransformPreview(tree) as tt:
1633
tt.unversion_file(tt.root)
1634
tt.version_file('new-id', tt.root)
1635
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1636
foo_tuple = ('foo', foo_trans_id)
1637
root_tuple = ('', tt.root)
1638
self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1640
def test_inventory_altered_noop_changed_parent_id(self):
1641
tree = self.make_branch_and_tree('tree')
1642
self.build_tree(['tree/foo'])
1643
tree.add('foo', 'foo-id')
1644
with TransformPreview(tree) as tt:
1645
tt.unversion_file(tt.root)
1646
tt.version_file(tree.get_root_id(), tt.root)
1647
foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1648
self.assertEqual([], tt._inventory_altered())
1454
1651
class TestTransformMerge(TestCaseInTempDir):
1456
1653
def test_text_merge(self):
1985
2194
self.assertEqual('file.moved', target.id2path('lower-id'))
1986
2195
self.assertEqual('FILE', target.id2path('upper-id'))
2197
def test_build_tree_observes_sha(self):
2198
source = self.make_branch_and_tree('source')
2199
self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
2200
source.add(['file1', 'dir', 'dir/file2'],
2201
['file1-id', 'dir-id', 'file2-id'])
2202
source.commit('new files')
2203
target = self.make_branch_and_tree('target')
2205
self.addCleanup(target.unlock)
2206
# We make use of the fact that DirState caches its cutoff time. So we
2207
# set the 'safe' time to one minute in the future.
2208
state = target.current_dirstate()
2209
state._cutoff_time = time.time() + 60
2210
build_tree(source.basis_tree(), target)
2211
entry1_sha = osutils.sha_file_by_name('source/file1')
2212
entry2_sha = osutils.sha_file_by_name('source/dir/file2')
2213
# entry[1] is the state information, entry[1][0] is the state of the
2214
# working tree, entry[1][0][1] is the sha value for the current working
2216
entry1 = state._get_entry(0, path_utf8='file1')
2217
self.assertEqual(entry1_sha, entry1[1][0][1])
2218
# The 'size' field must also be set.
2219
self.assertEqual(25, entry1[1][0][2])
2220
entry1_state = entry1[1][0]
2221
entry2 = state._get_entry(0, path_utf8='dir/file2')
2222
self.assertEqual(entry2_sha, entry2[1][0][1])
2223
self.assertEqual(29, entry2[1][0][2])
2224
entry2_state = entry2[1][0]
2225
# Now, make sure that we don't have to re-read the content. The
2226
# packed_stat should match exactly.
2227
self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
2228
self.assertEqual(entry2_sha,
2229
target.get_file_sha1('file2-id', 'dir/file2'))
2230
self.assertEqual(entry1_state, entry1[1][0])
2231
self.assertEqual(entry2_state, entry2[1][0])
1989
2234
class TestCommitTransform(tests.TestCaseWithTransport):
2091
2336
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):
2339
def test_commit_rich_revision_data(self):
2340
branch, tt = self.get_branch_and_transform()
2341
rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2342
committer='me <me@example.com>',
2343
revprops={'foo': 'bar'}, revision_id='revid-1',
2344
authors=['Author1 <author1@example.com>',
2345
'Author2 <author2@example.com>',
2347
self.assertEqual('revid-1', rev_id)
2348
revision = branch.repository.get_revision(rev_id)
2349
self.assertEqual(1, revision.timestamp)
2350
self.assertEqual(43201, revision.timezone)
2351
self.assertEqual('me <me@example.com>', revision.committer)
2352
self.assertEqual(['Author1 <author1@example.com>',
2353
'Author2 <author2@example.com>'],
2354
revision.get_apparent_authors())
2355
del revision.properties['authors']
2356
self.assertEqual({'foo': 'bar',
2357
'branch-nick': 'tree'},
2358
revision.properties)
2360
def test_no_explicit_revprops(self):
2361
branch, tt = self.get_branch_and_transform()
2362
rev_id = tt.commit(branch, 'message', authors=[
2363
'Author1 <author1@example.com>',
2364
'Author2 <author2@example.com>', ])
2365
revision = branch.repository.get_revision(rev_id)
2366
self.assertEqual(['Author1 <author1@example.com>',
2367
'Author2 <author2@example.com>'],
2368
revision.get_apparent_authors())
2369
self.assertEqual('tree', revision.properties['branch-nick'])
2372
class TestBackupName(tests.TestCase):
2374
def test_deprecations(self):
2375
class MockTransform(object):
2377
def has_named_child(self, by_parent, parent_id, name):
2378
return name in by_parent.get(parent_id, [])
2380
class MockEntry(object):
2383
object.__init__(self)
2115
2386
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~')
2387
name1 = self.applyDeprecated(
2388
symbol_versioning.deprecated_in((2, 3, 0)),
2389
transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2390
self.assertEqual('name.~1~', name1)
2391
name2 = self.applyDeprecated(
2392
symbol_versioning.deprecated_in((2, 3, 0)),
2393
transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2394
self.assertEqual('name.~2~', name2)
2128
2397
class TestFileMover(tests.TestCaseWithTransport):
2131
2400
self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2132
2401
mover = _FileMover()
2133
2402
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')
2403
self.assertPathExists('q')
2404
self.assertPathDoesNotExist('a')
2405
self.assertPathExists('q/b')
2406
self.assertPathExists('c')
2407
self.assertPathExists('c/d')
2140
2409
def test_pre_delete_rollback(self):
2141
2410
self.build_tree(['a/'])
2142
2411
mover = _FileMover()
2143
2412
mover.pre_delete('a', 'q')
2144
self.failUnlessExists('q')
2145
self.failIfExists('a')
2413
self.assertPathExists('q')
2414
self.assertPathDoesNotExist('a')
2146
2415
mover.rollback()
2147
self.failIfExists('q')
2148
self.failUnlessExists('a')
2416
self.assertPathDoesNotExist('q')
2417
self.assertPathExists('a')
2150
2419
def test_apply_deletions(self):
2151
2420
self.build_tree(['a/', 'b/'])
2152
2421
mover = _FileMover()
2153
2422
mover.pre_delete('a', 'q')
2154
2423
mover.pre_delete('b', 'r')
2155
self.failUnlessExists('q')
2156
self.failUnlessExists('r')
2157
self.failIfExists('a')
2158
self.failIfExists('b')
2424
self.assertPathExists('q')
2425
self.assertPathExists('r')
2426
self.assertPathDoesNotExist('a')
2427
self.assertPathDoesNotExist('b')
2159
2428
mover.apply_deletions()
2160
self.failIfExists('q')
2161
self.failIfExists('r')
2162
self.failIfExists('a')
2163
self.failIfExists('b')
2429
self.assertPathDoesNotExist('q')
2430
self.assertPathDoesNotExist('r')
2431
self.assertPathDoesNotExist('a')
2432
self.assertPathDoesNotExist('b')
2165
2434
def test_file_mover_rollback(self):
2166
2435
self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2240
2509
tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2241
2510
self.assertRaises(Bogus, tt.apply,
2242
2511
_mover=self.ExceptionFileMover(bad_target='d'))
2243
self.failUnlessExists('a')
2244
self.failUnlessExists('a/b')
2246
def test_resolve_no_parent(self):
2512
self.assertPathExists('a')
2513
self.assertPathExists('a/b')
2516
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2517
"""Ensure treetransform creation errors can be safely cleaned up after"""
2519
def _override_globals_in_method(self, instance, method_name, globals):
2520
"""Replace method on instance with one with updated globals"""
2522
func = getattr(instance, method_name).im_func
2523
new_globals = dict(func.func_globals)
2524
new_globals.update(globals)
2525
new_func = types.FunctionType(func.func_code, new_globals,
2526
func.func_name, func.func_defaults)
2527
setattr(instance, method_name,
2528
types.MethodType(new_func, instance, instance.__class__))
2529
self.addCleanup(delattr, instance, method_name)
2532
def _fake_open_raises_before(name, mode):
2533
"""Like open() but raises before doing anything"""
2537
def _fake_open_raises_after(name, mode):
2538
"""Like open() but raises after creating file without returning"""
2539
open(name, mode).close()
2542
def create_transform_and_root_trans_id(self):
2543
"""Setup a transform creating a file in limbo"""
2544
tree = self.make_branch_and_tree('.')
2545
tt = TreeTransform(tree)
2546
return tt, tt.create_path("a", tt.root)
2548
def create_transform_and_subdir_trans_id(self):
2549
"""Setup a transform creating a directory containing a file in limbo"""
2550
tree = self.make_branch_and_tree('.')
2551
tt = TreeTransform(tree)
2552
d_trans_id = tt.create_path("d", tt.root)
2553
tt.create_directory(d_trans_id)
2554
f_trans_id = tt.create_path("a", d_trans_id)
2555
tt.adjust_path("a", d_trans_id, f_trans_id)
2556
return tt, f_trans_id
2558
def test_root_create_file_open_raises_before_creation(self):
2559
tt, trans_id = self.create_transform_and_root_trans_id()
2560
self._override_globals_in_method(tt, "create_file",
2561
{"open": self._fake_open_raises_before})
2562
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2563
path = tt._limbo_name(trans_id)
2564
self.assertPathDoesNotExist(path)
2566
self.assertPathDoesNotExist(tt._limbodir)
2568
def test_root_create_file_open_raises_after_creation(self):
2569
tt, trans_id = self.create_transform_and_root_trans_id()
2570
self._override_globals_in_method(tt, "create_file",
2571
{"open": self._fake_open_raises_after})
2572
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2573
path = tt._limbo_name(trans_id)
2574
self.assertPathExists(path)
2576
self.assertPathDoesNotExist(path)
2577
self.assertPathDoesNotExist(tt._limbodir)
2579
def test_subdir_create_file_open_raises_before_creation(self):
2580
tt, trans_id = self.create_transform_and_subdir_trans_id()
2581
self._override_globals_in_method(tt, "create_file",
2582
{"open": self._fake_open_raises_before})
2583
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2584
path = tt._limbo_name(trans_id)
2585
self.assertPathDoesNotExist(path)
2587
self.assertPathDoesNotExist(tt._limbodir)
2589
def test_subdir_create_file_open_raises_after_creation(self):
2590
tt, trans_id = self.create_transform_and_subdir_trans_id()
2591
self._override_globals_in_method(tt, "create_file",
2592
{"open": self._fake_open_raises_after})
2593
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2594
path = tt._limbo_name(trans_id)
2595
self.assertPathExists(path)
2597
self.assertPathDoesNotExist(path)
2598
self.assertPathDoesNotExist(tt._limbodir)
2600
def test_rename_in_limbo_rename_raises_after_rename(self):
2601
tt, trans_id = self.create_transform_and_root_trans_id()
2602
parent1 = tt.new_directory('parent1', tt.root)
2603
child1 = tt.new_file('child1', parent1, 'contents')
2604
parent2 = tt.new_directory('parent2', tt.root)
2606
class FakeOSModule(object):
2607
def rename(self, old, new):
2610
self._override_globals_in_method(tt, "_rename_in_limbo",
2611
{"os": FakeOSModule()})
2613
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2614
path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2615
self.assertPathExists(path)
2617
self.assertPathDoesNotExist(path)
2618
self.assertPathDoesNotExist(tt._limbodir)
2620
def test_rename_in_limbo_rename_raises_before_rename(self):
2621
tt, trans_id = self.create_transform_and_root_trans_id()
2622
parent1 = tt.new_directory('parent1', tt.root)
2623
child1 = tt.new_file('child1', parent1, 'contents')
2624
parent2 = tt.new_directory('parent2', tt.root)
2626
class FakeOSModule(object):
2627
def rename(self, old, new):
2629
self._override_globals_in_method(tt, "_rename_in_limbo",
2630
{"os": FakeOSModule()})
2632
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2633
path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2634
self.assertPathExists(path)
2636
self.assertPathDoesNotExist(path)
2637
self.assertPathDoesNotExist(tt._limbodir)
2640
class TestTransformMissingParent(tests.TestCaseWithTransport):
2642
def make_tt_with_versioned_dir(self):
2247
2643
wt = self.make_branch_and_tree('.')
2644
self.build_tree(['dir/',])
2645
wt.add(['dir'], ['dir-id'])
2646
wt.commit('Create dir')
2248
2647
tt = TreeTransform(wt)
2249
2648
self.addCleanup(tt.finalize)
2250
parent = tt.trans_id_file_id('parent-id')
2251
tt.new_file('file', parent, 'Contents')
2252
resolve_conflicts(tt)
2651
def test_resolve_create_parent_for_versioned_file(self):
2652
wt, tt = self.make_tt_with_versioned_dir()
2653
dir_tid = tt.trans_id_tree_file_id('dir-id')
2654
file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
2655
tt.delete_contents(dir_tid)
2656
tt.unversion_file(dir_tid)
2657
conflicts = resolve_conflicts(tt)
2658
# one conflict for the missing directory, one for the unversioned
2660
self.assertLength(2, conflicts)
2662
def test_non_versioned_file_create_conflict(self):
2663
wt, tt = self.make_tt_with_versioned_dir()
2664
dir_tid = tt.trans_id_tree_file_id('dir-id')
2665
tt.new_file('file', dir_tid, 'Contents')
2666
tt.delete_contents(dir_tid)
2667
tt.unversion_file(dir_tid)
2668
conflicts = resolve_conflicts(tt)
2669
# no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
2670
self.assertLength(1, conflicts)
2671
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
2255
2675
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2256
2676
('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2257
2677
(False, False))
2258
2678
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2259
('', ''), ('directory', 'directory'), (False, None))
2679
('', ''), ('directory', 'directory'), (False, False))
2262
2682
class TestTransformPreview(tests.TestCaseWithTransport):
3132
3582
trans_id = tt.trans_id_tree_path('file')
3133
3583
self.assertEqual((LINES_ONE,),
3134
3584
tt._get_parents_texts(trans_id))
3587
class TestOrphan(tests.TestCaseWithTransport):
3589
def test_no_orphan_for_transform_preview(self):
3590
tree = self.make_branch_and_tree('tree')
3591
tt = transform.TransformPreview(tree)
3592
self.addCleanup(tt.finalize)
3593
self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3595
def _set_orphan_policy(self, wt, policy):
3596
wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
3599
def _prepare_orphan(self, wt):
3600
self.build_tree(['dir/', 'dir/file', 'dir/foo'])
3601
wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
3602
wt.commit('add dir and file ignoring foo')
3603
tt = transform.TreeTransform(wt)
3604
self.addCleanup(tt.finalize)
3605
# dir and bar are deleted
3606
dir_tid = tt.trans_id_tree_path('dir')
3607
file_tid = tt.trans_id_tree_path('dir/file')
3608
orphan_tid = tt.trans_id_tree_path('dir/foo')
3609
tt.delete_contents(file_tid)
3610
tt.unversion_file(file_tid)
3611
tt.delete_contents(dir_tid)
3612
tt.unversion_file(dir_tid)
3613
# There should be a conflict because dir still contain foo
3614
raw_conflicts = tt.find_conflicts()
3615
self.assertLength(1, raw_conflicts)
3616
self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
3617
return tt, orphan_tid
3619
def test_new_orphan_created(self):
3620
wt = self.make_branch_and_tree('.')
3621
self._set_orphan_policy(wt, 'move')
3622
tt, orphan_tid = self._prepare_orphan(wt)
3625
warnings.append(args[0] % args[1:])
3626
self.overrideAttr(trace, 'warning', warning)
3627
remaining_conflicts = resolve_conflicts(tt)
3628
self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3630
# Yeah for resolved conflicts !
3631
self.assertLength(0, remaining_conflicts)
3632
# We have a new orphan
3633
self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3634
self.assertEquals('bzr-orphans',
3635
tt.final_name(tt.final_parent(orphan_tid)))
3637
def test_never_orphan(self):
3638
wt = self.make_branch_and_tree('.')
3639
self._set_orphan_policy(wt, 'conflict')
3640
tt, orphan_tid = self._prepare_orphan(wt)
3641
remaining_conflicts = resolve_conflicts(tt)
3642
self.assertLength(1, remaining_conflicts)
3643
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3644
remaining_conflicts.pop())
3646
def test_orphan_error(self):
3647
def bogus_orphan(tt, orphan_id, parent_id):
3648
raise transform.OrphaningError(tt.final_name(orphan_id),
3649
tt.final_name(parent_id))
3650
transform.orphaning_registry.register('bogus', bogus_orphan,
3651
'Raise an error when orphaning')
3652
wt = self.make_branch_and_tree('.')
3653
self._set_orphan_policy(wt, 'bogus')
3654
tt, orphan_tid = self._prepare_orphan(wt)
3655
remaining_conflicts = resolve_conflicts(tt)
3656
self.assertLength(1, remaining_conflicts)
3657
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3658
remaining_conflicts.pop())
3660
def test_unknown_orphan_policy(self):
3661
wt = self.make_branch_and_tree('.')
3662
# Set a fictional policy nobody ever implemented
3663
self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3664
tt, orphan_tid = self._prepare_orphan(wt)
3667
warnings.append(args[0] % args[1:])
3668
self.overrideAttr(trace, 'warning', warning)
3669
remaining_conflicts = resolve_conflicts(tt)
3670
# We fallback to the default policy which create a conflict
3671
self.assertLength(1, remaining_conflicts)
3672
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3673
remaining_conflicts.pop())
3674
self.assertLength(1, warnings)
3675
self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')