159
171
transform.finalize()
160
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)
162
235
def test_create_files_same_timestamp(self):
163
236
transform, root = self.get_transform()
164
237
self.wt.lock_tree_write()
814
887
self.assertIs(None, self.wt.path2id('parent'))
815
888
self.assertIs(None, self.wt.path2id('parent.new'))
890
def test_resolve_conflicts_missing_parent(self):
891
wt = self.make_branch_and_tree('.')
892
tt = TreeTransform(wt)
893
self.addCleanup(tt.finalize)
894
parent = tt.trans_id_file_id('parent-id')
895
tt.new_file('file', parent, 'Contents')
896
raw_conflicts = resolve_conflicts(tt)
897
# Since the directory doesn't exist it's seen as 'missing'. So
898
# 'resolve_conflicts' create a conflict asking for it to be created.
899
self.assertLength(1, raw_conflicts)
900
self.assertEqual(('missing parent', 'Created directory', 'new-1'),
902
# apply fail since the missing directory doesn't exist
903
self.assertRaises(errors.NoFinalPath, tt.apply)
817
905
def test_moving_versioned_directories(self):
818
906
create, root = self.get_transform()
819
907
kansas = create.new_directory('kansas', root, 'kansas-id')
880
969
# On windows looks like:
881
970
# "Failed to rename .../work/myfile to
882
971
# .../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)")
972
# This test isn't concerned with exactly what the error looks like,
973
# and the strerror will vary across OS and locales, but the assert
974
# that the exeception attributes are what we expect
975
self.assertEqual(e.errno, errno.EACCES)
976
if os.name == "posix":
977
self.assertEndsWith(e.to_path, "/first-dir/newname")
979
self.assertEqual(os.path.basename(e.from_path), "myfile")
887
981
def test_set_executability_order(self):
888
982
"""Ensure that executability behaves the same, no matter what order.
1774
1868
source.commit('added file')
1775
1869
build_tree(source.basis_tree(), target)
1776
1870
self.assertEqual([], target.conflicts())
1777
self.failUnlessExists('target/dir1/file')
1871
self.assertPathExists('target/dir1/file')
1779
1873
# Ensure contents are merged
1780
1874
target = self.make_branch_and_tree('target2')
1781
1875
self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
1782
1876
build_tree(source.basis_tree(), target)
1783
1877
self.assertEqual([], target.conflicts())
1784
self.failUnlessExists('target2/dir1/file2')
1785
self.failUnlessExists('target2/dir1/file')
1878
self.assertPathExists('target2/dir1/file2')
1879
self.assertPathExists('target2/dir1/file')
1787
1881
# Ensure new contents are suppressed for existing branches
1788
1882
target = self.make_branch_and_tree('target3')
1789
1883
self.make_branch('target3/dir1')
1790
1884
self.build_tree(['target3/dir1/file2'])
1791
1885
build_tree(source.basis_tree(), target)
1792
self.failIfExists('target3/dir1/file')
1793
self.failUnlessExists('target3/dir1/file2')
1794
self.failUnlessExists('target3/dir1.diverted/file')
1886
self.assertPathDoesNotExist('target3/dir1/file')
1887
self.assertPathExists('target3/dir1/file2')
1888
self.assertPathExists('target3/dir1.diverted/file')
1795
1889
self.assertEqual([DuplicateEntry('Diverted to',
1796
1890
'dir1.diverted', 'dir1', 'new-dir1', None)],
1797
1891
target.conflicts())
1876
1970
self.addCleanup(target.unlock)
1877
1971
self.assertEqual([], list(target.iter_changes(revision_tree)))
1973
def test_build_tree_accelerator_tree_observes_sha1(self):
1974
source = self.create_ab_tree()
1975
sha1 = osutils.sha_string('A')
1976
target = self.make_branch_and_tree('target')
1978
self.addCleanup(target.unlock)
1979
state = target.current_dirstate()
1980
state._cutoff_time = time.time() + 60
1981
build_tree(source.basis_tree(), target, source)
1982
entry = state._get_entry(0, path_utf8='file1')
1983
self.assertEqual(sha1, entry[1][0][1])
1879
1985
def test_build_tree_accelerator_tree_missing_file(self):
1880
1986
source = self.create_ab_tree()
1881
1987
os.unlink('source/file1')
2039
2145
self.assertEqual('file.moved', target.id2path('lower-id'))
2040
2146
self.assertEqual('FILE', target.id2path('upper-id'))
2148
def test_build_tree_observes_sha(self):
2149
source = self.make_branch_and_tree('source')
2150
self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
2151
source.add(['file1', 'dir', 'dir/file2'],
2152
['file1-id', 'dir-id', 'file2-id'])
2153
source.commit('new files')
2154
target = self.make_branch_and_tree('target')
2156
self.addCleanup(target.unlock)
2157
# We make use of the fact that DirState caches its cutoff time. So we
2158
# set the 'safe' time to one minute in the future.
2159
state = target.current_dirstate()
2160
state._cutoff_time = time.time() + 60
2161
build_tree(source.basis_tree(), target)
2162
entry1_sha = osutils.sha_file_by_name('source/file1')
2163
entry2_sha = osutils.sha_file_by_name('source/dir/file2')
2164
# entry[1] is the state information, entry[1][0] is the state of the
2165
# working tree, entry[1][0][1] is the sha value for the current working
2167
entry1 = state._get_entry(0, path_utf8='file1')
2168
self.assertEqual(entry1_sha, entry1[1][0][1])
2169
# The 'size' field must also be set.
2170
self.assertEqual(25, entry1[1][0][2])
2171
entry1_state = entry1[1][0]
2172
entry2 = state._get_entry(0, path_utf8='dir/file2')
2173
self.assertEqual(entry2_sha, entry2[1][0][1])
2174
self.assertEqual(29, entry2[1][0][2])
2175
entry2_state = entry2[1][0]
2176
# Now, make sure that we don't have to re-read the content. The
2177
# packed_stat should match exactly.
2178
self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
2179
self.assertEqual(entry2_sha,
2180
target.get_file_sha1('file2-id', 'dir/file2'))
2181
self.assertEqual(entry1_state, entry1[1][0])
2182
self.assertEqual(entry2_state, entry2[1][0])
2043
2185
class TestCommitTransform(tests.TestCaseWithTransport):
2178
2320
self.assertEqual('tree', revision.properties['branch-nick'])
2181
class MockTransform(object):
2183
def has_named_child(self, by_parent, parent_id, name):
2184
for child_id in by_parent[parent_id]:
2188
elif name == "name.~%s~" % child_id:
2193
class MockEntry(object):
2195
object.__init__(self)
2199
class TestGetBackupName(TestCase):
2200
def test_get_backup_name(self):
2323
class TestBackupName(tests.TestCase):
2325
def test_deprecations(self):
2326
class MockTransform(object):
2328
def has_named_child(self, by_parent, parent_id, name):
2329
return name in by_parent.get(parent_id, [])
2331
class MockEntry(object):
2334
object.__init__(self)
2201
2337
tt = MockTransform()
2202
name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
2203
self.assertEqual(name, 'name.~1~')
2204
name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
2205
self.assertEqual(name, 'name.~2~')
2206
name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
2207
self.assertEqual(name, 'name.~1~')
2208
name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
2209
self.assertEqual(name, 'name.~1~')
2210
name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
2211
self.assertEqual(name, 'name.~4~')
2338
name1 = self.applyDeprecated(
2339
symbol_versioning.deprecated_in((2, 3, 0)),
2340
transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2341
self.assertEqual('name.~1~', name1)
2342
name2 = self.applyDeprecated(
2343
symbol_versioning.deprecated_in((2, 3, 0)),
2344
transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2345
self.assertEqual('name.~2~', name2)
2214
2348
class TestFileMover(tests.TestCaseWithTransport):
2217
2351
self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2218
2352
mover = _FileMover()
2219
2353
mover.rename('a', 'q')
2220
self.failUnlessExists('q')
2221
self.failIfExists('a')
2222
self.failUnlessExists('q/b')
2223
self.failUnlessExists('c')
2224
self.failUnlessExists('c/d')
2354
self.assertPathExists('q')
2355
self.assertPathDoesNotExist('a')
2356
self.assertPathExists('q/b')
2357
self.assertPathExists('c')
2358
self.assertPathExists('c/d')
2226
2360
def test_pre_delete_rollback(self):
2227
2361
self.build_tree(['a/'])
2228
2362
mover = _FileMover()
2229
2363
mover.pre_delete('a', 'q')
2230
self.failUnlessExists('q')
2231
self.failIfExists('a')
2364
self.assertPathExists('q')
2365
self.assertPathDoesNotExist('a')
2232
2366
mover.rollback()
2233
self.failIfExists('q')
2234
self.failUnlessExists('a')
2367
self.assertPathDoesNotExist('q')
2368
self.assertPathExists('a')
2236
2370
def test_apply_deletions(self):
2237
2371
self.build_tree(['a/', 'b/'])
2238
2372
mover = _FileMover()
2239
2373
mover.pre_delete('a', 'q')
2240
2374
mover.pre_delete('b', 'r')
2241
self.failUnlessExists('q')
2242
self.failUnlessExists('r')
2243
self.failIfExists('a')
2244
self.failIfExists('b')
2375
self.assertPathExists('q')
2376
self.assertPathExists('r')
2377
self.assertPathDoesNotExist('a')
2378
self.assertPathDoesNotExist('b')
2245
2379
mover.apply_deletions()
2246
self.failIfExists('q')
2247
self.failIfExists('r')
2248
self.failIfExists('a')
2249
self.failIfExists('b')
2380
self.assertPathDoesNotExist('q')
2381
self.assertPathDoesNotExist('r')
2382
self.assertPathDoesNotExist('a')
2383
self.assertPathDoesNotExist('b')
2251
2385
def test_file_mover_rollback(self):
2252
2386
self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2326
2460
tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2327
2461
self.assertRaises(Bogus, tt.apply,
2328
2462
_mover=self.ExceptionFileMover(bad_target='d'))
2329
self.failUnlessExists('a')
2330
self.failUnlessExists('a/b')
2332
def test_resolve_no_parent(self):
2463
self.assertPathExists('a')
2464
self.assertPathExists('a/b')
2467
class TestTransformMissingParent(tests.TestCaseWithTransport):
2469
def make_tt_with_versioned_dir(self):
2333
2470
wt = self.make_branch_and_tree('.')
2471
self.build_tree(['dir/',])
2472
wt.add(['dir'], ['dir-id'])
2473
wt.commit('Create dir')
2334
2474
tt = TreeTransform(wt)
2335
2475
self.addCleanup(tt.finalize)
2336
parent = tt.trans_id_file_id('parent-id')
2337
tt.new_file('file', parent, 'Contents')
2338
resolve_conflicts(tt)
2478
def test_resolve_create_parent_for_versioned_file(self):
2479
wt, tt = self.make_tt_with_versioned_dir()
2480
dir_tid = tt.trans_id_tree_file_id('dir-id')
2481
file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
2482
tt.delete_contents(dir_tid)
2483
tt.unversion_file(dir_tid)
2484
conflicts = resolve_conflicts(tt)
2485
# one conflict for the missing directory, one for the unversioned
2487
self.assertLength(2, conflicts)
2489
def test_non_versioned_file_create_conflict(self):
2490
wt, tt = self.make_tt_with_versioned_dir()
2491
dir_tid = tt.trans_id_tree_file_id('dir-id')
2492
tt.new_file('file', dir_tid, 'Contents')
2493
tt.delete_contents(dir_tid)
2494
tt.unversion_file(dir_tid)
2495
conflicts = resolve_conflicts(tt)
2496
# no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
2497
self.assertLength(1, conflicts)
2498
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
2341
2502
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2342
2503
('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2343
2504
(False, False))
2344
2505
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2345
('', ''), ('directory', 'directory'), (False, None))
2506
('', ''), ('directory', 'directory'), (False, False))
2348
2509
class TestTransformPreview(tests.TestCaseWithTransport):
2490
2651
preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
2491
2652
work_mtime = work_tree.get_file_mtime('file-id', 'file')
2654
def test_get_file_size(self):
2655
work_tree = self.make_branch_and_tree('tree')
2656
self.build_tree_contents([('tree/old', 'old')])
2657
work_tree.add('old', 'old-id')
2658
preview = TransformPreview(work_tree)
2659
self.addCleanup(preview.finalize)
2660
new_id = preview.new_file('name', preview.root, 'contents', 'new-id',
2662
tree = preview.get_preview_tree()
2663
self.assertEqual(len('old'), tree.get_file_size('old-id'))
2664
self.assertEqual(len('contents'), tree.get_file_size('new-id'))
2493
2666
def test_get_file(self):
2494
2667
preview = self.get_empty_preview()
2495
2668
preview.new_file('file', preview.root, 'contents', 'file-id')
2907
3080
merger.merge_type = Merge3Merger
2908
3081
merger.do_merge()
3083
def test_has_filename(self):
3084
wt = self.make_branch_and_tree('tree')
3085
self.build_tree(['tree/unmodified', 'tree/removed', 'tree/modified'])
3086
tt = TransformPreview(wt)
3087
removed_id = tt.trans_id_tree_path('removed')
3088
tt.delete_contents(removed_id)
3089
tt.new_file('new', tt.root, 'contents')
3090
modified_id = tt.trans_id_tree_path('modified')
3091
tt.delete_contents(modified_id)
3092
tt.create_file('modified-contents', modified_id)
3093
self.addCleanup(tt.finalize)
3094
tree = tt.get_preview_tree()
3095
self.assertTrue(tree.has_filename('unmodified'))
3096
self.assertFalse(tree.has_filename('not-present'))
3097
self.assertFalse(tree.has_filename('removed'))
3098
self.assertTrue(tree.has_filename('new'))
3099
self.assertTrue(tree.has_filename('modified'))
2910
3101
def test_is_executable(self):
2911
3102
tree = self.make_branch_and_tree('tree')
2912
3103
preview = TransformPreview(tree)
3218
3409
trans_id = tt.trans_id_tree_path('file')
3219
3410
self.assertEqual((LINES_ONE,),
3220
3411
tt._get_parents_texts(trans_id))
3414
class TestOrphan(tests.TestCaseWithTransport):
3416
def test_no_orphan_for_transform_preview(self):
3417
tree = self.make_branch_and_tree('tree')
3418
tt = transform.TransformPreview(tree)
3419
self.addCleanup(tt.finalize)
3420
self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3422
def _set_orphan_policy(self, wt, policy):
3423
wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
3426
def _prepare_orphan(self, wt):
3427
self.build_tree(['dir/', 'dir/file', 'dir/foo'])
3428
wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
3429
wt.commit('add dir and file ignoring foo')
3430
tt = transform.TreeTransform(wt)
3431
self.addCleanup(tt.finalize)
3432
# dir and bar are deleted
3433
dir_tid = tt.trans_id_tree_path('dir')
3434
file_tid = tt.trans_id_tree_path('dir/file')
3435
orphan_tid = tt.trans_id_tree_path('dir/foo')
3436
tt.delete_contents(file_tid)
3437
tt.unversion_file(file_tid)
3438
tt.delete_contents(dir_tid)
3439
tt.unversion_file(dir_tid)
3440
# There should be a conflict because dir still contain foo
3441
raw_conflicts = tt.find_conflicts()
3442
self.assertLength(1, raw_conflicts)
3443
self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
3444
return tt, orphan_tid
3446
def test_new_orphan_created(self):
3447
wt = self.make_branch_and_tree('.')
3448
self._set_orphan_policy(wt, 'move')
3449
tt, orphan_tid = self._prepare_orphan(wt)
3452
warnings.append(args[0] % args[1:])
3453
self.overrideAttr(trace, 'warning', warning)
3454
remaining_conflicts = resolve_conflicts(tt)
3455
self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3457
# Yeah for resolved conflicts !
3458
self.assertLength(0, remaining_conflicts)
3459
# We have a new orphan
3460
self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3461
self.assertEquals('bzr-orphans',
3462
tt.final_name(tt.final_parent(orphan_tid)))
3464
def test_never_orphan(self):
3465
wt = self.make_branch_and_tree('.')
3466
self._set_orphan_policy(wt, 'conflict')
3467
tt, orphan_tid = self._prepare_orphan(wt)
3468
remaining_conflicts = resolve_conflicts(tt)
3469
self.assertLength(1, remaining_conflicts)
3470
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3471
remaining_conflicts.pop())
3473
def test_orphan_error(self):
3474
def bogus_orphan(tt, orphan_id, parent_id):
3475
raise transform.OrphaningError(tt.final_name(orphan_id),
3476
tt.final_name(parent_id))
3477
transform.orphaning_registry.register('bogus', bogus_orphan,
3478
'Raise an error when orphaning')
3479
wt = self.make_branch_and_tree('.')
3480
self._set_orphan_policy(wt, 'bogus')
3481
tt, orphan_tid = self._prepare_orphan(wt)
3482
remaining_conflicts = resolve_conflicts(tt)
3483
self.assertLength(1, remaining_conflicts)
3484
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3485
remaining_conflicts.pop())
3487
def test_unknown_orphan_policy(self):
3488
wt = self.make_branch_and_tree('.')
3489
# Set a fictional policy nobody ever implemented
3490
self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3491
tt, orphan_tid = self._prepare_orphan(wt)
3494
warnings.append(args[0] % args[1:])
3495
self.overrideAttr(trace, 'warning', warning)
3496
remaining_conflicts = resolve_conflicts(tt)
3497
# We fallback to the default policy which create a conflict
3498
self.assertLength(1, remaining_conflicts)
3499
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3500
remaining_conflicts.pop())
3501
self.assertLength(1, warnings)
3502
self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')