~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Patch Queue Manager
  • Date: 2011-09-22 14:12:18 UTC
  • mfrom: (6155.3.1 jam)
  • Revision ID: pqm@pqm.ubuntu.com-20110922141218-86s4uu6nqvourw4f
(jameinel) Cleanup comments bzrlib/smart/__init__.py (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
import errno
17
18
import os
18
 
import stat
 
19
from StringIO import StringIO
19
20
import sys
 
21
import time
20
22
 
21
23
from bzrlib import (
 
24
    bencode,
22
25
    errors,
 
26
    filters,
23
27
    generate_ids,
 
28
    osutils,
 
29
    revision as _mod_revision,
 
30
    rules,
24
31
    symbol_versioning,
25
32
    tests,
 
33
    trace,
 
34
    transform,
26
35
    urlutils,
27
36
    )
28
37
from bzrlib.bzrdir import BzrDir
29
 
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
30
 
                              UnversionedParent, ParentLoop, DeletingParent,)
31
 
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
32
 
                           ReusingTransform, CantMoveRoot, 
33
 
                           PathsNotVersionedError, ExistingLimbo,
34
 
                           ImmortalLimbo, LockError)
35
 
from bzrlib.osutils import file_kind, has_symlinks, pathjoin
36
 
from bzrlib.merge import Merge3Merger
37
 
from bzrlib.tests import TestCaseInTempDir, TestSkipped, TestCase
38
 
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths, 
39
 
                              resolve_conflicts, cook_conflicts, 
40
 
                              find_interesting, build_tree, get_backup_name)
 
38
from bzrlib.conflicts import (
 
39
    DeletingParent,
 
40
    DuplicateEntry,
 
41
    DuplicateID,
 
42
    MissingParent,
 
43
    NonDirectoryParent,
 
44
    ParentLoop,
 
45
    UnversionedParent,
 
46
)
 
47
from bzrlib.diff import show_diff_trees
 
48
from bzrlib.errors import (
 
49
    DuplicateKey,
 
50
    ExistingLimbo,
 
51
    ExistingPendingDeletion,
 
52
    ImmortalLimbo,
 
53
    ImmortalPendingDeletion,
 
54
    LockError,
 
55
    MalformedTransform,
 
56
    ReusingTransform,
 
57
)
 
58
from bzrlib.osutils import (
 
59
    file_kind,
 
60
    pathjoin,
 
61
)
 
62
from bzrlib.merge import Merge3Merger, Merger
 
63
from bzrlib.tests import (
 
64
    features,
 
65
    TestCaseInTempDir,
 
66
    TestSkipped,
 
67
    )
 
68
from bzrlib.tests.features import (
 
69
    HardlinkFeature,
 
70
    SymlinkFeature,
 
71
    )
 
72
from bzrlib.transform import (
 
73
    build_tree,
 
74
    create_from_tree,
 
75
    cook_conflicts,
 
76
    _FileMover,
 
77
    FinalPaths,
 
78
    resolve_conflicts,
 
79
    resolve_checkout,
 
80
    ROOT_PARENT,
 
81
    TransformPreview,
 
82
    TreeTransform,
 
83
)
41
84
 
42
85
 
43
86
class TestTreeTransform(tests.TestCaseWithTransport):
49
92
 
50
93
    def get_transform(self):
51
94
        transform = TreeTransform(self.wt)
52
 
        #self.addCleanup(transform.finalize)
 
95
        self.addCleanup(transform.finalize)
53
96
        return transform, transform.root
54
97
 
 
98
    def get_transform_for_sha1_test(self):
 
99
        trans, root = self.get_transform()
 
100
        self.wt.lock_tree_write()
 
101
        self.addCleanup(self.wt.unlock)
 
102
        contents = ['just some content\n']
 
103
        sha1 = osutils.sha_strings(contents)
 
104
        # Roll back the clock
 
105
        trans._creation_mtime = time.time() - 20.0
 
106
        return trans, root, contents, sha1
 
107
 
55
108
    def test_existing_limbo(self):
56
 
        limbo_name = urlutils.local_path_from_url(
57
 
            self.wt._control_files.controlfilename('limbo'))
58
109
        transform, root = self.get_transform()
 
110
        limbo_name = transform._limbodir
 
111
        deletion_path = transform._deletiondir
59
112
        os.mkdir(pathjoin(limbo_name, 'hehe'))
60
113
        self.assertRaises(ImmortalLimbo, transform.apply)
61
114
        self.assertRaises(LockError, self.wt.unlock)
63
116
        self.assertRaises(LockError, self.wt.unlock)
64
117
        os.rmdir(pathjoin(limbo_name, 'hehe'))
65
118
        os.rmdir(limbo_name)
 
119
        os.rmdir(deletion_path)
66
120
        transform, root = self.get_transform()
67
121
        transform.apply()
68
122
 
 
123
    def test_existing_pending_deletion(self):
 
124
        transform, root = self.get_transform()
 
125
        deletion_path = self._limbodir = urlutils.local_path_from_url(
 
126
            transform._tree._transport.abspath('pending-deletion'))
 
127
        os.mkdir(pathjoin(deletion_path, 'blocking-directory'))
 
128
        self.assertRaises(ImmortalPendingDeletion, transform.apply)
 
129
        self.assertRaises(LockError, self.wt.unlock)
 
130
        self.assertRaises(ExistingPendingDeletion, self.get_transform)
 
131
 
69
132
    def test_build(self):
70
 
        transform, root = self.get_transform() 
 
133
        transform, root = self.get_transform()
 
134
        self.wt.lock_tree_write()
 
135
        self.addCleanup(self.wt.unlock)
71
136
        self.assertIs(transform.get_tree_parent(root), ROOT_PARENT)
72
137
        imaginary_id = transform.trans_id_tree_path('imaginary')
73
138
        imaginary_id2 = transform.trans_id_tree_path('imaginary/')
74
139
        self.assertEqual(imaginary_id, imaginary_id2)
75
 
        self.assertEqual(transform.get_tree_parent(imaginary_id), root)
76
 
        self.assertEqual(transform.final_kind(root), 'directory')
77
 
        self.assertEqual(transform.final_file_id(root), self.wt.get_root_id())
 
140
        self.assertEqual(root, transform.get_tree_parent(imaginary_id))
 
141
        self.assertEqual('directory', transform.final_kind(root))
 
142
        self.assertEqual(self.wt.get_root_id(), transform.final_file_id(root))
78
143
        trans_id = transform.create_path('name', root)
79
144
        self.assertIs(transform.final_file_id(trans_id), None)
80
 
        self.assertRaises(NoSuchFile, transform.final_kind, trans_id)
 
145
        self.assertIs(None, transform.final_kind(trans_id))
81
146
        transform.create_file('contents', trans_id)
82
147
        transform.set_executability(True, trans_id)
83
148
        transform.version_file('my_pretties', trans_id)
101
166
        self.assertIs(self.wt.is_executable('my_pretties2'), False)
102
167
        self.assertEqual('directory', file_kind(self.wt.abspath('oz')))
103
168
        self.assertEqual(len(modified_paths), 3)
104
 
        tree_mod_paths = [self.wt.id2abspath(f) for f in 
 
169
        tree_mod_paths = [self.wt.id2abspath(f) for f in
105
170
                          ('ozzie', 'my_pretties', 'my_pretties2')]
106
171
        self.assertSubset(tree_mod_paths, modified_paths)
107
172
        # is it safe to finalize repeatedly?
108
173
        transform.finalize()
109
174
        transform.finalize()
110
175
 
 
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',
 
179
                                  sha1=sha1)
 
180
        calls = []
 
181
        orig = self.wt._observed_sha1
 
182
        def _observed_sha1(*args):
 
183
            calls.append(args)
 
184
            orig(*args)
 
185
        self.wt._observed_sha1 = _observed_sha1
 
186
        trans.apply()
 
187
        self.assertEqual([(None, 'file1', trans._observed_sha1s[trans_id])],
 
188
                         calls)
 
189
 
 
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)
 
198
 
 
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))
 
213
        trans.apply()
 
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)
 
219
 
 
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',
 
223
                                  sha1=sha1)
 
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)
 
228
 
 
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',
 
232
                                  sha1=sha1)
 
233
        self.assertTrue(trans_id in trans._observed_sha1s)
 
234
        trans.cancel_creation(trans_id)
 
235
        self.assertFalse(trans_id in trans._observed_sha1s)
 
236
 
 
237
    def test_create_files_same_timestamp(self):
 
238
        transform, root = self.get_transform()
 
239
        self.wt.lock_tree_write()
 
240
        self.addCleanup(self.wt.unlock)
 
241
        # Roll back the clock, so that we know everything is being set to the
 
242
        # exact time
 
243
        transform._creation_mtime = creation_mtime = time.time() - 20.0
 
244
        transform.create_file('content-one',
 
245
                              transform.create_path('one', root))
 
246
        time.sleep(1) # *ugly*
 
247
        transform.create_file('content-two',
 
248
                              transform.create_path('two', root))
 
249
        transform.apply()
 
250
        fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
 
251
        fo.close()
 
252
        fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
 
253
        fo.close()
 
254
        # We only guarantee 2s resolution
 
255
        self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
 
256
            "%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
 
257
        # But if we have more than that, all files should get the same result
 
258
        self.assertEqual(st1.st_mtime, st2.st_mtime)
 
259
 
 
260
    def test_change_root_id(self):
 
261
        transform, root = self.get_transform()
 
262
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
 
263
        transform.new_directory('', ROOT_PARENT, 'new-root-id')
 
264
        transform.delete_contents(root)
 
265
        transform.unversion_file(root)
 
266
        transform.fixup_new_roots()
 
267
        transform.apply()
 
268
        self.assertEqual('new-root-id', self.wt.get_root_id())
 
269
 
 
270
    def test_change_root_id_add_files(self):
 
271
        transform, root = self.get_transform()
 
272
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
 
273
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
 
274
        transform.new_file('file', new_trans_id, ['new-contents\n'],
 
275
                           'new-file-id')
 
276
        transform.delete_contents(root)
 
277
        transform.unversion_file(root)
 
278
        transform.fixup_new_roots()
 
279
        transform.apply()
 
280
        self.assertEqual('new-root-id', self.wt.get_root_id())
 
281
        self.assertEqual('new-file-id', self.wt.path2id('file'))
 
282
        self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
 
283
 
 
284
    def test_add_two_roots(self):
 
285
        transform, root = self.get_transform()
 
286
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
 
287
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
 
288
        self.assertRaises(ValueError, transform.fixup_new_roots)
 
289
 
 
290
    def test_retain_existing_root(self):
 
291
        tt, root = self.get_transform()
 
292
        with tt:
 
293
            tt.new_directory('', ROOT_PARENT, 'new-root-id')
 
294
            tt.fixup_new_roots()
 
295
            self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
 
296
 
 
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')
 
301
        tt.fixup_new_roots()
 
302
        self.assertEqual(tt.root, tt.final_parent(child))
 
303
 
 
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)
 
310
 
 
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()
 
318
        transform.apply()
 
319
        self.assertEqual(old_root_id, self.wt.get_root_id())
 
320
 
 
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)
 
325
 
 
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))
 
333
 
 
334
    def test_apply_retains_root_directory(self):
 
335
        # Do not attempt to delete the physical root directory, because that
 
336
        # is impossible.
 
337
        transform, root = self.get_transform()
 
338
        with transform:
 
339
            transform.delete_contents(root)
 
340
            e = self.assertRaises(AssertionError, self.assertRaises,
 
341
                                  errors.TransformRenameFailed,
 
342
                                  transform.apply)
 
343
        self.assertContainsRe('TransformRenameFailed not raised', str(e))
 
344
 
 
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)
 
349
        transform.apply()
 
350
        self.assertEqual(old_root_id, self.wt.get_root_id())
 
351
 
 
352
    def test_hardlink(self):
 
353
        self.requireFeature(HardlinkFeature)
 
354
        transform, root = self.get_transform()
 
355
        transform.new_file('file1', root, 'contents')
 
356
        transform.apply()
 
357
        target = self.make_branch_and_tree('target')
 
358
        target_transform = TreeTransform(target)
 
359
        trans_id = target_transform.create_path('file1', target_transform.root)
 
360
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
 
361
        target_transform.apply()
 
362
        self.assertPathExists('target/file1')
 
363
        source_stat = os.stat(self.wt.abspath('file1'))
 
364
        target_stat = os.stat('target/file1')
 
365
        self.assertEqual(source_stat, target_stat)
 
366
 
111
367
    def test_convenience(self):
112
368
        transform, root = self.get_transform()
113
 
        trans_id = transform.new_file('name', root, 'contents', 
 
369
        self.wt.lock_tree_write()
 
370
        self.addCleanup(self.wt.unlock)
 
371
        trans_id = transform.new_file('name', root, 'contents',
114
372
                                      'my_pretties', True)
115
373
        oz = transform.new_directory('oz', root, 'oz-id')
116
374
        dorothy = transform.new_directory('dorothy', oz, 'dorothy-id')
117
 
        toto = transform.new_file('toto', dorothy, 'toto-contents', 
 
375
        toto = transform.new_file('toto', dorothy, 'toto-contents',
118
376
                                  'toto-id', False)
119
377
 
120
378
        self.assertEqual(len(transform.find_conflicts()), 0)
144
402
 
145
403
    def test_conflicts(self):
146
404
        transform, root = self.get_transform()
147
 
        trans_id = transform.new_file('name', root, 'contents', 
 
405
        trans_id = transform.new_file('name', root, 'contents',
148
406
                                      'my_pretties')
149
407
        self.assertEqual(len(transform.find_conflicts()), 0)
150
408
        trans_id2 = transform.new_file('name', root, 'Crontents', 'toto')
151
 
        self.assertEqual(transform.find_conflicts(), 
 
409
        self.assertEqual(transform.find_conflicts(),
152
410
                         [('duplicate', trans_id, trans_id2, 'name')])
153
411
        self.assertRaises(MalformedTransform, transform.apply)
154
412
        transform.adjust_path('name', trans_id, trans_id2)
155
 
        self.assertEqual(transform.find_conflicts(), 
 
413
        self.assertEqual(transform.find_conflicts(),
156
414
                         [('non-directory parent', trans_id)])
157
415
        tinman_id = transform.trans_id_tree_path('tinman')
158
416
        transform.adjust_path('name', tinman_id, trans_id2)
159
 
        self.assertEqual(transform.find_conflicts(), 
160
 
                         [('unversioned parent', tinman_id), 
 
417
        self.assertEqual(transform.find_conflicts(),
 
418
                         [('unversioned parent', tinman_id),
161
419
                          ('missing parent', tinman_id)])
162
420
        lion_id = transform.create_path('lion', root)
163
 
        self.assertEqual(transform.find_conflicts(), 
164
 
                         [('unversioned parent', tinman_id), 
 
421
        self.assertEqual(transform.find_conflicts(),
 
422
                         [('unversioned parent', tinman_id),
165
423
                          ('missing parent', tinman_id)])
166
424
        transform.adjust_path('name', lion_id, trans_id2)
167
 
        self.assertEqual(transform.find_conflicts(), 
 
425
        self.assertEqual(transform.find_conflicts(),
168
426
                         [('unversioned parent', lion_id),
169
427
                          ('missing parent', lion_id)])
170
428
        transform.version_file("Courage", lion_id)
171
 
        self.assertEqual(transform.find_conflicts(), 
172
 
                         [('missing parent', lion_id), 
 
429
        self.assertEqual(transform.find_conflicts(),
 
430
                         [('missing parent', lion_id),
173
431
                          ('versioning no contents', lion_id)])
174
432
        transform.adjust_path('name2', root, trans_id2)
175
 
        self.assertEqual(transform.find_conflicts(), 
 
433
        self.assertEqual(transform.find_conflicts(),
176
434
                         [('versioning no contents', lion_id)])
177
435
        transform.create_file('Contents, okay?', lion_id)
178
436
        transform.adjust_path('name2', trans_id2, trans_id2)
179
 
        self.assertEqual(transform.find_conflicts(), 
180
 
                         [('parent loop', trans_id2), 
 
437
        self.assertEqual(transform.find_conflicts(),
 
438
                         [('parent loop', trans_id2),
181
439
                          ('non-directory parent', trans_id2)])
182
440
        transform.adjust_path('name2', root, trans_id2)
183
441
        oz_id = transform.new_directory('oz', root)
184
442
        transform.set_executability(True, oz_id)
185
 
        self.assertEqual(transform.find_conflicts(), 
 
443
        self.assertEqual(transform.find_conflicts(),
186
444
                         [('unversioned executability', oz_id)])
187
445
        transform.version_file('oz-id', oz_id)
188
 
        self.assertEqual(transform.find_conflicts(), 
 
446
        self.assertEqual(transform.find_conflicts(),
189
447
                         [('non-file executability', oz_id)])
190
448
        transform.set_executability(None, oz_id)
191
449
        tip_id = transform.new_file('tip', oz_id, 'ozma', 'tip-id')
200
458
        self.assert_('oz/tip' in transform2._tree_path_ids)
201
459
        self.assertEqual(fp.get_path(newtip), pathjoin('oz', 'tip'))
202
460
        self.assertEqual(len(result), 2)
203
 
        self.assertEqual((result[0][0], result[0][1]), 
 
461
        self.assertEqual((result[0][0], result[0][1]),
204
462
                         ('duplicate', newtip))
205
 
        self.assertEqual((result[1][0], result[1][2]), 
 
463
        self.assertEqual((result[1][0], result[1][2]),
206
464
                         ('duplicate id', newtip))
207
465
        transform2.finalize()
208
466
        transform3 = TreeTransform(self.wt)
209
467
        self.addCleanup(transform3.finalize)
210
468
        oz_id = transform3.trans_id_tree_file_id('oz-id')
211
469
        transform3.delete_contents(oz_id)
212
 
        self.assertEqual(transform3.find_conflicts(), 
 
470
        self.assertEqual(transform3.find_conflicts(),
213
471
                         [('missing parent', oz_id)])
214
472
        root_id = transform3.root
215
473
        tip_id = transform3.trans_id_tree_file_id('tip-id')
216
474
        transform3.adjust_path('tip', root_id, tip_id)
217
475
        transform3.apply()
218
476
 
 
477
    def test_conflict_on_case_insensitive(self):
 
478
        tree = self.make_branch_and_tree('tree')
 
479
        # Don't try this at home, kids!
 
480
        # Force the tree to report that it is case sensitive, for conflict
 
481
        # resolution tests
 
482
        tree.case_sensitive = True
 
483
        transform = TreeTransform(tree)
 
484
        self.addCleanup(transform.finalize)
 
485
        transform.new_file('file', transform.root, 'content')
 
486
        transform.new_file('FiLe', transform.root, 'content')
 
487
        result = transform.find_conflicts()
 
488
        self.assertEqual([], result)
 
489
        transform.finalize()
 
490
        # Force the tree to report that it is case insensitive, for conflict
 
491
        # generation tests
 
492
        tree.case_sensitive = False
 
493
        transform = TreeTransform(tree)
 
494
        self.addCleanup(transform.finalize)
 
495
        transform.new_file('file', transform.root, 'content')
 
496
        transform.new_file('FiLe', transform.root, 'content')
 
497
        result = transform.find_conflicts()
 
498
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
 
499
 
 
500
    def test_conflict_on_case_insensitive_existing(self):
 
501
        tree = self.make_branch_and_tree('tree')
 
502
        self.build_tree(['tree/FiLe'])
 
503
        # Don't try this at home, kids!
 
504
        # Force the tree to report that it is case sensitive, for conflict
 
505
        # resolution tests
 
506
        tree.case_sensitive = True
 
507
        transform = TreeTransform(tree)
 
508
        self.addCleanup(transform.finalize)
 
509
        transform.new_file('file', transform.root, 'content')
 
510
        result = transform.find_conflicts()
 
511
        self.assertEqual([], result)
 
512
        transform.finalize()
 
513
        # Force the tree to report that it is case insensitive, for conflict
 
514
        # generation tests
 
515
        tree.case_sensitive = False
 
516
        transform = TreeTransform(tree)
 
517
        self.addCleanup(transform.finalize)
 
518
        transform.new_file('file', transform.root, 'content')
 
519
        result = transform.find_conflicts()
 
520
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
 
521
 
 
522
    def test_resolve_case_insensitive_conflict(self):
 
523
        tree = self.make_branch_and_tree('tree')
 
524
        # Don't try this at home, kids!
 
525
        # Force the tree to report that it is case insensitive, for conflict
 
526
        # resolution tests
 
527
        tree.case_sensitive = False
 
528
        transform = TreeTransform(tree)
 
529
        self.addCleanup(transform.finalize)
 
530
        transform.new_file('file', transform.root, 'content')
 
531
        transform.new_file('FiLe', transform.root, 'content')
 
532
        resolve_conflicts(transform)
 
533
        transform.apply()
 
534
        self.assertPathExists('tree/file')
 
535
        self.assertPathExists('tree/FiLe.moved')
 
536
 
 
537
    def test_resolve_checkout_case_conflict(self):
 
538
        tree = self.make_branch_and_tree('tree')
 
539
        # Don't try this at home, kids!
 
540
        # Force the tree to report that it is case insensitive, for conflict
 
541
        # resolution tests
 
542
        tree.case_sensitive = False
 
543
        transform = TreeTransform(tree)
 
544
        self.addCleanup(transform.finalize)
 
545
        transform.new_file('file', transform.root, 'content')
 
546
        transform.new_file('FiLe', transform.root, 'content')
 
547
        resolve_conflicts(transform,
 
548
                          pass_func=lambda t, c: resolve_checkout(t, c, []))
 
549
        transform.apply()
 
550
        self.assertPathExists('tree/file')
 
551
        self.assertPathExists('tree/FiLe.moved')
 
552
 
 
553
    def test_apply_case_conflict(self):
 
554
        """Ensure that a transform with case conflicts can always be applied"""
 
555
        tree = self.make_branch_and_tree('tree')
 
556
        transform = TreeTransform(tree)
 
557
        self.addCleanup(transform.finalize)
 
558
        transform.new_file('file', transform.root, 'content')
 
559
        transform.new_file('FiLe', transform.root, 'content')
 
560
        dir = transform.new_directory('dir', transform.root)
 
561
        transform.new_file('dirfile', dir, 'content')
 
562
        transform.new_file('dirFiLe', dir, 'content')
 
563
        resolve_conflicts(transform)
 
564
        transform.apply()
 
565
        self.assertPathExists('tree/file')
 
566
        if not os.path.exists('tree/FiLe.moved'):
 
567
            self.assertPathExists('tree/FiLe')
 
568
        self.assertPathExists('tree/dir/dirfile')
 
569
        if not os.path.exists('tree/dir/dirFiLe.moved'):
 
570
            self.assertPathExists('tree/dir/dirFiLe')
 
571
 
 
572
    def test_case_insensitive_limbo(self):
 
573
        tree = self.make_branch_and_tree('tree')
 
574
        # Don't try this at home, kids!
 
575
        # Force the tree to report that it is case insensitive
 
576
        tree.case_sensitive = False
 
577
        transform = TreeTransform(tree)
 
578
        self.addCleanup(transform.finalize)
 
579
        dir = transform.new_directory('dir', transform.root)
 
580
        first = transform.new_file('file', dir, 'content')
 
581
        second = transform.new_file('FiLe', dir, 'content')
 
582
        self.assertContainsRe(transform._limbo_name(first), 'new-1/file')
 
583
        self.assertNotContainsRe(transform._limbo_name(second), 'new-1/FiLe')
 
584
 
 
585
    def test_adjust_path_updates_child_limbo_names(self):
 
586
        tree = self.make_branch_and_tree('tree')
 
587
        transform = TreeTransform(tree)
 
588
        self.addCleanup(transform.finalize)
 
589
        foo_id = transform.new_directory('foo', transform.root)
 
590
        bar_id = transform.new_directory('bar', foo_id)
 
591
        baz_id = transform.new_directory('baz', bar_id)
 
592
        qux_id = transform.new_directory('qux', baz_id)
 
593
        transform.adjust_path('quxx', foo_id, bar_id)
 
594
        self.assertStartsWith(transform._limbo_name(qux_id),
 
595
                              transform._limbo_name(bar_id))
 
596
 
219
597
    def test_add_del(self):
220
598
        start, root = self.get_transform()
221
599
        start.new_directory('a', root, 'a')
234
612
        self.addCleanup(unversion.finalize)
235
613
        parent = unversion.trans_id_tree_path('parent')
236
614
        unversion.unversion_file(parent)
237
 
        self.assertEqual(unversion.find_conflicts(), 
 
615
        self.assertEqual(unversion.find_conflicts(),
238
616
                         [('unversioned parent', parent_id)])
239
617
        file_id = unversion.trans_id_tree_file_id('child-id')
240
618
        unversion.unversion_file(file_id)
260
638
        mangle_tree.adjust_path('name2', root, name1)
261
639
        mangle_tree.adjust_path('name1', root, name2)
262
640
 
263
 
        #tests for deleting parent directories 
 
641
        #tests for deleting parent directories
264
642
        ddir = mangle_tree.trans_id_tree_file_id('ddir')
265
643
        mangle_tree.delete_contents(ddir)
266
644
        dfile = mangle_tree.trans_id_tree_file_id('dfile')
295
673
        create_tree,root = self.get_transform()
296
674
        newdir = create_tree.new_directory('selftest', root, 'selftest-id')
297
675
        create_tree.new_file('blackbox.py', newdir, 'hello1', 'blackbox-id')
298
 
        create_tree.apply()        
 
676
        create_tree.apply()
299
677
        mangle_tree,root = self.get_transform()
300
678
        selftest = mangle_tree.trans_id_tree_file_id('selftest-id')
301
679
        blackbox = mangle_tree.trans_id_tree_file_id('blackbox-id')
309
687
        bzrlib = create_tree.new_directory('bzrlib', root, 'bzrlib-id')
310
688
        tests = create_tree.new_directory('tests', bzrlib, 'tests-id')
311
689
        blackbox = create_tree.new_directory('blackbox', tests, 'blackbox-id')
312
 
        create_tree.new_file('test_too_much.py', blackbox, 'hello1', 
 
690
        create_tree.new_file('test_too_much.py', blackbox, 'hello1',
313
691
                             'test_too_much-id')
314
 
        create_tree.apply()        
 
692
        create_tree.apply()
315
693
        mangle_tree,root = self.get_transform()
316
694
        bzrlib = mangle_tree.trans_id_tree_file_id('bzrlib-id')
317
695
        tests = mangle_tree.trans_id_tree_file_id('tests-id')
318
696
        test_too_much = mangle_tree.trans_id_tree_file_id('test_too_much-id')
319
697
        mangle_tree.adjust_path('selftest', bzrlib, tests)
320
 
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much) 
 
698
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much)
321
699
        mangle_tree.set_executability(True, test_too_much)
322
700
        mangle_tree.apply()
323
701
 
324
702
    def test_both_rename3(self):
325
703
        create_tree,root = self.get_transform()
326
704
        tests = create_tree.new_directory('tests', root, 'tests-id')
327
 
        create_tree.new_file('test_too_much.py', tests, 'hello1', 
 
705
        create_tree.new_file('test_too_much.py', tests, 'hello1',
328
706
                             'test_too_much-id')
329
 
        create_tree.apply()        
 
707
        create_tree.apply()
330
708
        mangle_tree,root = self.get_transform()
331
709
        tests = mangle_tree.trans_id_tree_file_id('tests-id')
332
710
        test_too_much = mangle_tree.trans_id_tree_file_id('test_too_much-id')
333
711
        mangle_tree.adjust_path('selftest', root, tests)
334
 
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much) 
 
712
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much)
335
713
        mangle_tree.set_executability(True, test_too_much)
336
714
        mangle_tree.apply()
337
715
 
350
728
        newdir = move_id.new_directory('dir', root, 'newdir')
351
729
        move_id.adjust_path('name2', newdir, name1)
352
730
        move_id.apply()
353
 
        
 
731
 
354
732
    def test_replace_dangling_ie(self):
355
733
        create_tree, root = self.get_transform()
356
734
        # prepare tree
372
750
        resolve_conflicts(replace)
373
751
        replace.apply()
374
752
 
375
 
    def test_symlinks(self):
376
 
        if not has_symlinks():
377
 
            raise TestSkipped('Symlinks are not supported on this platform')
378
 
        transform,root = self.get_transform()
 
753
    def _test_symlinks(self, link_name1,link_target1,
 
754
                       link_name2, link_target2):
 
755
 
 
756
        def ozpath(p): return 'oz/' + p
 
757
 
 
758
        self.requireFeature(SymlinkFeature)
 
759
        transform, root = self.get_transform()
379
760
        oz_id = transform.new_directory('oz', root, 'oz-id')
380
 
        wizard = transform.new_symlink('wizard', oz_id, 'wizard-target', 
 
761
        wizard = transform.new_symlink(link_name1, oz_id, link_target1,
381
762
                                       'wizard-id')
382
 
        wiz_id = transform.create_path('wizard2', oz_id)
383
 
        transform.create_symlink('behind_curtain', wiz_id)
384
 
        transform.version_file('wiz-id2', wiz_id)            
 
763
        wiz_id = transform.create_path(link_name2, oz_id)
 
764
        transform.create_symlink(link_target2, wiz_id)
 
765
        transform.version_file('wiz-id2', wiz_id)
385
766
        transform.set_executability(True, wiz_id)
386
 
        self.assertEqual(transform.find_conflicts(), 
 
767
        self.assertEqual(transform.find_conflicts(),
387
768
                         [('non-file executability', wiz_id)])
388
769
        transform.set_executability(None, wiz_id)
389
770
        transform.apply()
390
 
        self.assertEqual(self.wt.path2id('oz/wizard'), 'wizard-id')
391
 
        self.assertEqual(file_kind(self.wt.abspath('oz/wizard')), 'symlink')
392
 
        self.assertEqual(os.readlink(self.wt.abspath('oz/wizard2')), 
393
 
                         'behind_curtain')
394
 
        self.assertEqual(os.readlink(self.wt.abspath('oz/wizard')),
395
 
                         'wizard-target')
396
 
 
 
771
        self.assertEqual(self.wt.path2id(ozpath(link_name1)), 'wizard-id')
 
772
        self.assertEqual('symlink',
 
773
                         file_kind(self.wt.abspath(ozpath(link_name1))))
 
774
        self.assertEqual(link_target2,
 
775
                         osutils.readlink(self.wt.abspath(ozpath(link_name2))))
 
776
        self.assertEqual(link_target1,
 
777
                         osutils.readlink(self.wt.abspath(ozpath(link_name1))))
 
778
 
 
779
    def test_symlinks(self):
 
780
        self._test_symlinks('wizard', 'wizard-target',
 
781
                            'wizard2', 'behind_curtain')
 
782
 
 
783
    def test_symlinks_unicode(self):
 
784
        self.requireFeature(features.UnicodeFilenameFeature)
 
785
        self._test_symlinks(u'\N{Euro Sign}wizard',
 
786
                            u'wizard-targ\N{Euro Sign}t',
 
787
                            u'\N{Euro Sign}wizard2',
 
788
                            u'b\N{Euro Sign}hind_curtain')
 
789
 
 
790
    def test_unable_create_symlink(self):
 
791
        def tt_helper():
 
792
            wt = self.make_branch_and_tree('.')
 
793
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
794
            try:
 
795
                tt.new_symlink('foo', tt.root, 'bar')
 
796
                tt.apply()
 
797
            finally:
 
798
                wt.unlock()
 
799
        os_symlink = getattr(os, 'symlink', None)
 
800
        os.symlink = None
 
801
        try:
 
802
            err = self.assertRaises(errors.UnableCreateSymlink, tt_helper)
 
803
            self.assertEquals(
 
804
                "Unable to create symlink 'foo' on this platform",
 
805
                str(err))
 
806
        finally:
 
807
            if os_symlink:
 
808
                os.symlink = os_symlink
397
809
 
398
810
    def get_conflicted(self):
399
811
        create,root = self.get_transform()
403
815
        create.apply()
404
816
        conflicts,root = self.get_transform()
405
817
        # set up duplicate entry, duplicate id
406
 
        new_dorothy = conflicts.new_file('dorothy', root, 'dorothy', 
 
818
        new_dorothy = conflicts.new_file('dorothy', root, 'dorothy',
407
819
                                         'dorothy-id')
408
820
        old_dorothy = conflicts.trans_id_tree_file_id('dorothy-id')
409
821
        oz = conflicts.trans_id_tree_file_id('oz-id')
433
845
        tt, emerald, oz, old_dorothy, new_dorothy = self.get_conflicted()
434
846
        raw_conflicts = resolve_conflicts(tt)
435
847
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
436
 
        duplicate = DuplicateEntry('Moved existing file to', 'dorothy.moved', 
 
848
        duplicate = DuplicateEntry('Moved existing file to', 'dorothy.moved',
437
849
                                   'dorothy', None, 'dorothy-id')
438
850
        self.assertEqual(cooked_conflicts[0], duplicate)
439
 
        duplicate_id = DuplicateID('Unversioned existing file', 
 
851
        duplicate_id = DuplicateID('Unversioned existing file',
440
852
                                   'dorothy.moved', 'dorothy', None,
441
853
                                   'dorothy-id')
442
854
        self.assertEqual(cooked_conflicts[1], duplicate_id)
450
862
        unversioned_parent2 = UnversionedParent('Versioned directory', 'oz',
451
863
                                               'oz-id')
452
864
        self.assertEqual(cooked_conflicts[3], unversioned_parent)
453
 
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity', 
 
865
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity',
454
866
                                 'oz/emeraldcity', 'emerald-id', 'emerald-id')
455
867
        self.assertEqual(cooked_conflicts[4], deleted_parent)
456
868
        self.assertEqual(cooked_conflicts[5], unversioned_parent2)
463
875
        raw_conflicts = resolve_conflicts(tt)
464
876
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
465
877
        tt.finalize()
466
 
        conflicts_s = [str(c) for c in cooked_conflicts]
 
878
        conflicts_s = [unicode(c) for c in cooked_conflicts]
467
879
        self.assertEqual(len(cooked_conflicts), len(conflicts_s))
468
880
        self.assertEqual(conflicts_s[0], 'Conflict adding file dorothy.  '
469
881
                                         'Moved existing file to '
482
894
                                         ' versioned, but has versioned'
483
895
                                         ' children.  Versioned directory.')
484
896
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
485
 
                                         ' oz/emeraldcity.  Cancelled move.')
 
897
                                         ' oz/emeraldcity. Cancelled move.')
 
898
 
 
899
    def prepare_wrong_parent_kind(self):
 
900
        tt, root = self.get_transform()
 
901
        tt.new_file('parent', root, 'contents', 'parent-id')
 
902
        tt.apply()
 
903
        tt, root = self.get_transform()
 
904
        parent_id = tt.trans_id_file_id('parent-id')
 
905
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
 
906
        return tt
 
907
 
 
908
    def test_find_conflicts_wrong_parent_kind(self):
 
909
        tt = self.prepare_wrong_parent_kind()
 
910
        tt.find_conflicts()
 
911
 
 
912
    def test_resolve_conflicts_wrong_existing_parent_kind(self):
 
913
        tt = self.prepare_wrong_parent_kind()
 
914
        raw_conflicts = resolve_conflicts(tt)
 
915
        self.assertEqual(set([('non-directory parent', 'Created directory',
 
916
                         'new-3')]), raw_conflicts)
 
917
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
 
918
        self.assertEqual([NonDirectoryParent('Created directory', 'parent.new',
 
919
        'parent-id')], cooked_conflicts)
 
920
        tt.apply()
 
921
        self.assertEqual(None, self.wt.path2id('parent'))
 
922
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
 
923
 
 
924
    def test_resolve_conflicts_wrong_new_parent_kind(self):
 
925
        tt, root = self.get_transform()
 
926
        parent_id = tt.new_directory('parent', root, 'parent-id')
 
927
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
 
928
        tt.apply()
 
929
        tt, root = self.get_transform()
 
930
        parent_id = tt.trans_id_file_id('parent-id')
 
931
        tt.delete_contents(parent_id)
 
932
        tt.create_file('contents', parent_id)
 
933
        raw_conflicts = resolve_conflicts(tt)
 
934
        self.assertEqual(set([('non-directory parent', 'Created directory',
 
935
                         'new-3')]), raw_conflicts)
 
936
        tt.apply()
 
937
        self.assertEqual(None, self.wt.path2id('parent'))
 
938
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
 
939
 
 
940
    def test_resolve_conflicts_wrong_parent_kind_unversioned(self):
 
941
        tt, root = self.get_transform()
 
942
        parent_id = tt.new_directory('parent', root)
 
943
        tt.new_file('child,', parent_id, 'contents2')
 
944
        tt.apply()
 
945
        tt, root = self.get_transform()
 
946
        parent_id = tt.trans_id_tree_path('parent')
 
947
        tt.delete_contents(parent_id)
 
948
        tt.create_file('contents', parent_id)
 
949
        resolve_conflicts(tt)
 
950
        tt.apply()
 
951
        self.assertIs(None, self.wt.path2id('parent'))
 
952
        self.assertIs(None, self.wt.path2id('parent.new'))
 
953
 
 
954
    def test_resolve_conflicts_missing_parent(self):
 
955
        wt = self.make_branch_and_tree('.')
 
956
        tt = TreeTransform(wt)
 
957
        self.addCleanup(tt.finalize)
 
958
        parent = tt.trans_id_file_id('parent-id')
 
959
        tt.new_file('file', parent, 'Contents')
 
960
        raw_conflicts = resolve_conflicts(tt)
 
961
        # Since the directory doesn't exist it's seen as 'missing'.  So
 
962
        # 'resolve_conflicts' create a conflict asking for it to be created.
 
963
        self.assertLength(1, raw_conflicts)
 
964
        self.assertEqual(('missing parent', 'Created directory', 'new-1'),
 
965
                         raw_conflicts.pop())
 
966
        # apply fail since the missing directory doesn't exist
 
967
        self.assertRaises(errors.NoFinalPath, tt.apply)
486
968
 
487
969
    def test_moving_versioned_directories(self):
488
970
        create, root = self.get_transform()
504
986
        create.apply()
505
987
        transform, root = self.get_transform()
506
988
        transform.adjust_root_path('oldroot', fun)
507
 
        new_root=transform.trans_id_tree_path('')
 
989
        new_root = transform.trans_id_tree_path('')
508
990
        transform.version_file('new-root', new_root)
509
991
        transform.apply()
510
992
 
522
1004
        rename.set_executability(True, myfile)
523
1005
        rename.apply()
524
1006
 
525
 
    def test_find_interesting(self):
526
 
        create, root = self.get_transform()
527
 
        wt = create._tree
528
 
        create.new_file('vfile', root, 'myfile-text', 'myfile-id')
529
 
        create.new_file('uvfile', root, 'othertext')
 
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',
 
1013
                                 'myfile-id')
530
1014
        create.apply()
531
 
        result = self.applyDeprecated(symbol_versioning.zero_fifteen,
532
 
            find_interesting, wt, wt, ['vfile'])
533
 
        self.assertEqual(result, set(['myfile-id']))
 
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)
 
1021
        else:
 
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")
 
1042
        else:
 
1043
            self.assertEqual(os.path.basename(e.from_path), "myfile")
534
1044
 
535
1045
    def test_set_executability_order(self):
536
1046
        """Ensure that executability behaves the same, no matter what order.
537
 
        
 
1047
 
538
1048
        - create file and set executability simultaneously
539
1049
        - create file and set executability afterward
540
1050
        - unsetting the executability of a file whose executability has not been
543
1053
        """
544
1054
        transform, root = self.get_transform()
545
1055
        wt = transform._tree
 
1056
        wt.lock_read()
 
1057
        self.addCleanup(wt.unlock)
546
1058
        transform.new_file('set_on_creation', root, 'Set on creation', 'soc',
547
1059
                           True)
548
 
        sac = transform.new_file('set_after_creation', root, 'Set after creation', 'sac')
 
1060
        sac = transform.new_file('set_after_creation', root,
 
1061
                                 'Set after creation', 'sac')
549
1062
        transform.set_executability(True, sac)
550
 
        uws = transform.new_file('unset_without_set', root, 'Unset badly', 'uws')
 
1063
        uws = transform.new_file('unset_without_set', root, 'Unset badly',
 
1064
                                 'uws')
551
1065
        self.assertRaises(KeyError, transform.set_executability, None, uws)
552
1066
        transform.apply()
553
1067
        self.assertTrue(wt.is_executable('soc'))
560
1074
        transform, root = self.get_transform()
561
1075
        transform.new_file('file1', root, 'contents', 'file1-id', True)
562
1076
        transform.apply()
 
1077
        self.wt.lock_write()
 
1078
        self.addCleanup(self.wt.unlock)
563
1079
        self.assertTrue(self.wt.is_executable('file1-id'))
564
1080
        transform, root = self.get_transform()
565
1081
        file1_id = transform.trans_id_tree_file_id('file1-id')
605
1121
        transform.apply()
606
1122
        transform, root = self.get_transform()
607
1123
        try:
608
 
            self.assertEqual([], list(transform._iter_changes()))
 
1124
            self.assertEqual([], list(transform.iter_changes()))
609
1125
            old = transform.trans_id_tree_file_id('id-1')
610
1126
            transform.unversion_file(old)
611
1127
            self.assertEqual([('id-1', ('old', None), False, (True, False),
612
1128
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
613
 
                (True, True))], list(transform._iter_changes()))
 
1129
                (True, True))], list(transform.iter_changes()))
614
1130
            transform.new_directory('new', root, 'id-1')
615
1131
            self.assertEqual([('id-1', ('old', 'new'), True, (True, True),
616
1132
                ('eert_toor', 'eert_toor'), ('old', 'new'),
617
1133
                ('file', 'directory'),
618
 
                (True, False))], list(transform._iter_changes()))
 
1134
                (True, False))], list(transform.iter_changes()))
619
1135
        finally:
620
1136
            transform.finalize()
621
1137
 
630
1146
            transform.version_file('id-1', old)
631
1147
            self.assertEqual([('id-1', (None, 'old'), False, (False, True),
632
1148
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
633
 
                (False, False))], list(transform._iter_changes()))
 
1149
                (False, False))], list(transform.iter_changes()))
634
1150
        finally:
635
1151
            transform.finalize()
636
1152
 
646
1162
            old = transform.trans_id_tree_path('old')
647
1163
            subdir = transform.trans_id_tree_file_id('subdir-id')
648
1164
            new = transform.trans_id_tree_path('new')
649
 
            self.assertEqual([], list(transform._iter_changes()))
 
1165
            self.assertEqual([], list(transform.iter_changes()))
650
1166
 
651
1167
            #content deletion
652
1168
            transform.delete_contents(old)
653
1169
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
654
1170
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', None),
655
 
                (False, False))], list(transform._iter_changes()))
 
1171
                (False, False))], list(transform.iter_changes()))
656
1172
 
657
1173
            #content change
658
1174
            transform.create_file('blah', old)
659
1175
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
660
1176
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
661
 
                (False, False))], list(transform._iter_changes()))
 
1177
                (False, False))], list(transform.iter_changes()))
662
1178
            transform.cancel_deletion(old)
663
1179
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
664
1180
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
665
 
                (False, False))], list(transform._iter_changes()))
 
1181
                (False, False))], list(transform.iter_changes()))
666
1182
            transform.cancel_creation(old)
667
1183
 
668
1184
            # move file_id to a different file
669
 
            self.assertEqual([], list(transform._iter_changes()))
 
1185
            self.assertEqual([], list(transform.iter_changes()))
670
1186
            transform.unversion_file(old)
671
1187
            transform.version_file('id-1', new)
672
1188
            transform.adjust_path('old', root, new)
673
1189
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
674
1190
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
675
 
                (False, False))], list(transform._iter_changes()))
 
1191
                (False, False))], list(transform.iter_changes()))
676
1192
            transform.cancel_versioning(new)
677
1193
            transform._removed_id = set()
678
1194
 
679
1195
            #execute bit
680
 
            self.assertEqual([], list(transform._iter_changes()))
 
1196
            self.assertEqual([], list(transform.iter_changes()))
681
1197
            transform.set_executability(True, old)
682
1198
            self.assertEqual([('id-1', ('old', 'old'), False, (True, True),
683
1199
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
684
 
                (False, True))], list(transform._iter_changes()))
 
1200
                (False, True))], list(transform.iter_changes()))
685
1201
            transform.set_executability(None, old)
686
1202
 
687
1203
            # filename
688
 
            self.assertEqual([], list(transform._iter_changes()))
 
1204
            self.assertEqual([], list(transform.iter_changes()))
689
1205
            transform.adjust_path('new', root, old)
690
1206
            transform._new_parent = {}
691
1207
            self.assertEqual([('id-1', ('old', 'new'), False, (True, True),
692
1208
                ('eert_toor', 'eert_toor'), ('old', 'new'), ('file', 'file'),
693
 
                (False, False))], list(transform._iter_changes()))
 
1209
                (False, False))], list(transform.iter_changes()))
694
1210
            transform._new_name = {}
695
1211
 
696
1212
            # parent directory
697
 
            self.assertEqual([], list(transform._iter_changes()))
 
1213
            self.assertEqual([], list(transform.iter_changes()))
698
1214
            transform.adjust_path('new', subdir, old)
699
1215
            transform._new_name = {}
700
1216
            self.assertEqual([('id-1', ('old', 'subdir/old'), False,
701
1217
                (True, True), ('eert_toor', 'subdir-id'), ('old', 'old'),
702
1218
                ('file', 'file'), (False, False))],
703
 
                list(transform._iter_changes()))
 
1219
                list(transform.iter_changes()))
704
1220
            transform._new_path = {}
705
1221
 
706
1222
        finally:
728
1244
                ('id-2', (u'file2', u'file2'), False, (True, True),
729
1245
                ('eert_toor', 'eert_toor'), ('file2', u'file2'),
730
1246
                ('file', 'file'), (False, True))],
731
 
                list(transform._iter_changes()))
 
1247
                list(transform.iter_changes()))
732
1248
        finally:
733
1249
            transform.finalize()
734
1250
 
748
1264
            transform.adjust_path('flitter', root, floater)
749
1265
            self.assertEqual([('floater-id', ('floater', 'flitter'), False,
750
1266
            (True, True), ('toor_eert', 'toor_eert'), ('floater', 'flitter'),
751
 
            (None, None), (False, False))], list(transform._iter_changes()))
 
1267
            (None, None), (False, False))], list(transform.iter_changes()))
752
1268
        finally:
753
1269
            transform.finalize()
754
1270
 
763
1279
        try:
764
1280
            old = transform.trans_id_tree_path('old')
765
1281
            subdir = transform.trans_id_tree_file_id('subdir-id')
766
 
            self.assertEqual([], list(transform._iter_changes()))
 
1282
            self.assertEqual([], list(transform.iter_changes()))
767
1283
            transform.delete_contents(subdir)
768
1284
            transform.create_directory(subdir)
769
1285
            transform.set_executability(False, old)
770
1286
            transform.unversion_file(old)
771
1287
            transform.version_file('id-1', old)
772
1288
            transform.adjust_path('old', root, old)
773
 
            self.assertEqual([], list(transform._iter_changes()))
774
 
        finally:
775
 
            transform.finalize()
 
1289
            self.assertEqual([], list(transform.iter_changes()))
 
1290
        finally:
 
1291
            transform.finalize()
 
1292
 
 
1293
    def test_rename_count(self):
 
1294
        transform, root = self.get_transform()
 
1295
        transform.new_file('name1', root, 'contents')
 
1296
        self.assertEqual(transform.rename_count, 0)
 
1297
        transform.apply()
 
1298
        self.assertEqual(transform.rename_count, 1)
 
1299
        transform2, root = self.get_transform()
 
1300
        transform2.adjust_path('name2', root,
 
1301
                               transform2.trans_id_tree_path('name1'))
 
1302
        self.assertEqual(transform2.rename_count, 0)
 
1303
        transform2.apply()
 
1304
        self.assertEqual(transform2.rename_count, 2)
 
1305
 
 
1306
    def test_change_parent(self):
 
1307
        """Ensure that after we change a parent, the results are still right.
 
1308
 
 
1309
        Renames and parent changes on pending transforms can happen as part
 
1310
        of conflict resolution, and are explicitly permitted by the
 
1311
        TreeTransform API.
 
1312
 
 
1313
        This test ensures they work correctly with the rename-avoidance
 
1314
        optimization.
 
1315
        """
 
1316
        transform, root = self.get_transform()
 
1317
        parent1 = transform.new_directory('parent1', root)
 
1318
        child1 = transform.new_file('child1', parent1, 'contents')
 
1319
        parent2 = transform.new_directory('parent2', root)
 
1320
        transform.adjust_path('child1', parent2, child1)
 
1321
        transform.apply()
 
1322
        self.assertPathDoesNotExist(self.wt.abspath('parent1/child1'))
 
1323
        self.assertPathExists(self.wt.abspath('parent2/child1'))
 
1324
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
 
1325
        # no rename for child1 (counting only renames during apply)
 
1326
        self.assertEqual(2, transform.rename_count)
 
1327
 
 
1328
    def test_cancel_parent(self):
 
1329
        """Cancelling a parent doesn't cause deletion of a non-empty directory
 
1330
 
 
1331
        This is like the test_change_parent, except that we cancel the parent
 
1332
        before adjusting the path.  The transform must detect that the
 
1333
        directory is non-empty, and move children to safe locations.
 
1334
        """
 
1335
        transform, root = self.get_transform()
 
1336
        parent1 = transform.new_directory('parent1', root)
 
1337
        child1 = transform.new_file('child1', parent1, 'contents')
 
1338
        child2 = transform.new_file('child2', parent1, 'contents')
 
1339
        try:
 
1340
            transform.cancel_creation(parent1)
 
1341
        except OSError:
 
1342
            self.fail('Failed to move child1 before deleting parent1')
 
1343
        transform.cancel_creation(child2)
 
1344
        transform.create_directory(parent1)
 
1345
        try:
 
1346
            transform.cancel_creation(parent1)
 
1347
        # If the transform incorrectly believes that child2 is still in
 
1348
        # parent1's limbo directory, it will try to rename it and fail
 
1349
        # because was already moved by the first cancel_creation.
 
1350
        except OSError:
 
1351
            self.fail('Transform still thinks child2 is a child of parent1')
 
1352
        parent2 = transform.new_directory('parent2', root)
 
1353
        transform.adjust_path('child1', parent2, child1)
 
1354
        transform.apply()
 
1355
        self.assertPathDoesNotExist(self.wt.abspath('parent1'))
 
1356
        self.assertPathExists(self.wt.abspath('parent2/child1'))
 
1357
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
 
1358
        self.assertEqual(2, transform.rename_count)
 
1359
 
 
1360
    def test_adjust_and_cancel(self):
 
1361
        """Make sure adjust_path keeps track of limbo children properly"""
 
1362
        transform, root = self.get_transform()
 
1363
        parent1 = transform.new_directory('parent1', root)
 
1364
        child1 = transform.new_file('child1', parent1, 'contents')
 
1365
        parent2 = transform.new_directory('parent2', root)
 
1366
        transform.adjust_path('child1', parent2, child1)
 
1367
        transform.cancel_creation(child1)
 
1368
        try:
 
1369
            transform.cancel_creation(parent1)
 
1370
        # if the transform thinks child1 is still in parent1's limbo
 
1371
        # directory, it will attempt to move it and fail.
 
1372
        except OSError:
 
1373
            self.fail('Transform still thinks child1 is a child of parent1')
 
1374
        transform.finalize()
 
1375
 
 
1376
    def test_noname_contents(self):
 
1377
        """TreeTransform should permit deferring naming files."""
 
1378
        transform, root = self.get_transform()
 
1379
        parent = transform.trans_id_file_id('parent-id')
 
1380
        try:
 
1381
            transform.create_directory(parent)
 
1382
        except KeyError:
 
1383
            self.fail("Can't handle contents with no name")
 
1384
        transform.finalize()
 
1385
 
 
1386
    def test_noname_contents_nested(self):
 
1387
        """TreeTransform should permit deferring naming files."""
 
1388
        transform, root = self.get_transform()
 
1389
        parent = transform.trans_id_file_id('parent-id')
 
1390
        try:
 
1391
            transform.create_directory(parent)
 
1392
        except KeyError:
 
1393
            self.fail("Can't handle contents with no name")
 
1394
        child = transform.new_directory('child', parent)
 
1395
        transform.adjust_path('parent', root, parent)
 
1396
        transform.apply()
 
1397
        self.assertPathExists(self.wt.abspath('parent/child'))
 
1398
        self.assertEqual(1, transform.rename_count)
 
1399
 
 
1400
    def test_reuse_name(self):
 
1401
        """Avoid reusing the same limbo name for different files"""
 
1402
        transform, root = self.get_transform()
 
1403
        parent = transform.new_directory('parent', root)
 
1404
        child1 = transform.new_directory('child', parent)
 
1405
        try:
 
1406
            child2 = transform.new_directory('child', parent)
 
1407
        except OSError:
 
1408
            self.fail('Tranform tried to use the same limbo name twice')
 
1409
        transform.adjust_path('child2', parent, child2)
 
1410
        transform.apply()
 
1411
        # limbo/new-1 => parent, limbo/new-3 => parent/child2
 
1412
        # child2 is put into top-level limbo because child1 has already
 
1413
        # claimed the direct limbo path when child2 is created.  There is no
 
1414
        # advantage in renaming files once they're in top-level limbo, except
 
1415
        # as part of apply.
 
1416
        self.assertEqual(2, transform.rename_count)
 
1417
 
 
1418
    def test_reuse_when_first_moved(self):
 
1419
        """Don't avoid direct paths when it is safe to use them"""
 
1420
        transform, root = self.get_transform()
 
1421
        parent = transform.new_directory('parent', root)
 
1422
        child1 = transform.new_directory('child', parent)
 
1423
        transform.adjust_path('child1', parent, child1)
 
1424
        child2 = transform.new_directory('child', parent)
 
1425
        transform.apply()
 
1426
        # limbo/new-1 => parent
 
1427
        self.assertEqual(1, transform.rename_count)
 
1428
 
 
1429
    def test_reuse_after_cancel(self):
 
1430
        """Don't avoid direct paths when it is safe to use them"""
 
1431
        transform, root = self.get_transform()
 
1432
        parent2 = transform.new_directory('parent2', root)
 
1433
        child1 = transform.new_directory('child1', parent2)
 
1434
        transform.cancel_creation(parent2)
 
1435
        transform.create_directory(parent2)
 
1436
        child2 = transform.new_directory('child1', parent2)
 
1437
        transform.adjust_path('child2', parent2, child1)
 
1438
        transform.apply()
 
1439
        # limbo/new-1 => parent2, limbo/new-2 => parent2/child1
 
1440
        self.assertEqual(2, transform.rename_count)
 
1441
 
 
1442
    def test_finalize_order(self):
 
1443
        """Finalize must be done in child-to-parent order"""
 
1444
        transform, root = self.get_transform()
 
1445
        parent = transform.new_directory('parent', root)
 
1446
        child = transform.new_directory('child', parent)
 
1447
        try:
 
1448
            transform.finalize()
 
1449
        except OSError:
 
1450
            self.fail('Tried to remove parent before child1')
 
1451
 
 
1452
    def test_cancel_with_cancelled_child_should_succeed(self):
 
1453
        transform, root = self.get_transform()
 
1454
        parent = transform.new_directory('parent', root)
 
1455
        child = transform.new_directory('child', parent)
 
1456
        transform.cancel_creation(child)
 
1457
        transform.cancel_creation(parent)
 
1458
        transform.finalize()
 
1459
 
 
1460
    def test_rollback_on_directory_clash(self):
 
1461
        def tt_helper():
 
1462
            wt = self.make_branch_and_tree('.')
 
1463
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1464
            try:
 
1465
                foo = tt.new_directory('foo', tt.root)
 
1466
                tt.new_file('bar', foo, 'foobar')
 
1467
                baz = tt.new_directory('baz', tt.root)
 
1468
                tt.new_file('qux', baz, 'quux')
 
1469
                # Ask for a rename 'foo' -> 'baz'
 
1470
                tt.adjust_path('baz', tt.root, foo)
 
1471
                # Lie to tt that we've already resolved all conflicts.
 
1472
                tt.apply(no_conflicts=True)
 
1473
            except:
 
1474
                wt.unlock()
 
1475
                raise
 
1476
        # The rename will fail because the target directory is not empty (but
 
1477
        # raises FileExists anyway).
 
1478
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1479
        self.assertContainsRe(str(err),
 
1480
            "^File exists: .+/baz")
 
1481
 
 
1482
    def test_two_directories_clash(self):
 
1483
        def tt_helper():
 
1484
            wt = self.make_branch_and_tree('.')
 
1485
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1486
            try:
 
1487
                foo_1 = tt.new_directory('foo', tt.root)
 
1488
                tt.new_directory('bar', foo_1)
 
1489
                # Adding the same directory with a different content
 
1490
                foo_2 = tt.new_directory('foo', tt.root)
 
1491
                tt.new_directory('baz', foo_2)
 
1492
                # Lie to tt that we've already resolved all conflicts.
 
1493
                tt.apply(no_conflicts=True)
 
1494
            except:
 
1495
                wt.unlock()
 
1496
                raise
 
1497
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1498
        self.assertContainsRe(str(err),
 
1499
            "^File exists: .+/foo")
 
1500
 
 
1501
    def test_two_directories_clash_finalize(self):
 
1502
        def tt_helper():
 
1503
            wt = self.make_branch_and_tree('.')
 
1504
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1505
            try:
 
1506
                foo_1 = tt.new_directory('foo', tt.root)
 
1507
                tt.new_directory('bar', foo_1)
 
1508
                # Adding the same directory with a different content
 
1509
                foo_2 = tt.new_directory('foo', tt.root)
 
1510
                tt.new_directory('baz', foo_2)
 
1511
                # Lie to tt that we've already resolved all conflicts.
 
1512
                tt.apply(no_conflicts=True)
 
1513
            except:
 
1514
                tt.finalize()
 
1515
                raise
 
1516
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1517
        self.assertContainsRe(str(err),
 
1518
            "^File exists: .+/foo")
 
1519
 
 
1520
    def test_file_to_directory(self):
 
1521
        wt = self.make_branch_and_tree('.')
 
1522
        self.build_tree(['foo'])
 
1523
        wt.add(['foo'])
 
1524
        wt.commit("one")
 
1525
        tt = TreeTransform(wt)
 
1526
        self.addCleanup(tt.finalize)
 
1527
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1528
        tt.delete_contents(foo_trans_id)
 
1529
        tt.create_directory(foo_trans_id)
 
1530
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
 
1531
        tt.create_file(["aa\n"], bar_trans_id)
 
1532
        tt.version_file("bar-1", bar_trans_id)
 
1533
        tt.apply()
 
1534
        self.assertPathExists("foo/bar")
 
1535
        wt.lock_read()
 
1536
        try:
 
1537
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1538
                    "directory")
 
1539
        finally:
 
1540
            wt.unlock()
 
1541
        wt.commit("two")
 
1542
        changes = wt.changes_from(wt.basis_tree())
 
1543
        self.assertFalse(changes.has_changed(), changes)
 
1544
 
 
1545
    def test_file_to_symlink(self):
 
1546
        self.requireFeature(SymlinkFeature)
 
1547
        wt = self.make_branch_and_tree('.')
 
1548
        self.build_tree(['foo'])
 
1549
        wt.add(['foo'])
 
1550
        wt.commit("one")
 
1551
        tt = TreeTransform(wt)
 
1552
        self.addCleanup(tt.finalize)
 
1553
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1554
        tt.delete_contents(foo_trans_id)
 
1555
        tt.create_symlink("bar", foo_trans_id)
 
1556
        tt.apply()
 
1557
        self.assertPathExists("foo")
 
1558
        wt.lock_read()
 
1559
        self.addCleanup(wt.unlock)
 
1560
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1561
                "symlink")
 
1562
 
 
1563
    def test_dir_to_file(self):
 
1564
        wt = self.make_branch_and_tree('.')
 
1565
        self.build_tree(['foo/', 'foo/bar'])
 
1566
        wt.add(['foo', 'foo/bar'])
 
1567
        wt.commit("one")
 
1568
        tt = TreeTransform(wt)
 
1569
        self.addCleanup(tt.finalize)
 
1570
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1571
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
 
1572
        tt.delete_contents(foo_trans_id)
 
1573
        tt.delete_versioned(bar_trans_id)
 
1574
        tt.create_file(["aa\n"], foo_trans_id)
 
1575
        tt.apply()
 
1576
        self.assertPathExists("foo")
 
1577
        wt.lock_read()
 
1578
        self.addCleanup(wt.unlock)
 
1579
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1580
                "file")
 
1581
 
 
1582
    def test_dir_to_hardlink(self):
 
1583
        self.requireFeature(HardlinkFeature)
 
1584
        wt = self.make_branch_and_tree('.')
 
1585
        self.build_tree(['foo/', 'foo/bar'])
 
1586
        wt.add(['foo', 'foo/bar'])
 
1587
        wt.commit("one")
 
1588
        tt = TreeTransform(wt)
 
1589
        self.addCleanup(tt.finalize)
 
1590
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1591
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
 
1592
        tt.delete_contents(foo_trans_id)
 
1593
        tt.delete_versioned(bar_trans_id)
 
1594
        self.build_tree(['baz'])
 
1595
        tt.create_hardlink("baz", foo_trans_id)
 
1596
        tt.apply()
 
1597
        self.assertPathExists("foo")
 
1598
        self.assertPathExists("baz")
 
1599
        wt.lock_read()
 
1600
        self.addCleanup(wt.unlock)
 
1601
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1602
                "file")
 
1603
 
 
1604
    def test_no_final_path(self):
 
1605
        transform, root = self.get_transform()
 
1606
        trans_id = transform.trans_id_file_id('foo')
 
1607
        transform.create_file('bar', trans_id)
 
1608
        transform.cancel_creation(trans_id)
 
1609
        transform.apply()
 
1610
 
 
1611
    def test_create_from_tree(self):
 
1612
        tree1 = self.make_branch_and_tree('tree1')
 
1613
        self.build_tree_contents([('tree1/foo/',), ('tree1/bar', 'baz')])
 
1614
        tree1.add(['foo', 'bar'], ['foo-id', 'bar-id'])
 
1615
        tree2 = self.make_branch_and_tree('tree2')
 
1616
        tt = TreeTransform(tree2)
 
1617
        foo_trans_id = tt.create_path('foo', tt.root)
 
1618
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id')
 
1619
        bar_trans_id = tt.create_path('bar', tt.root)
 
1620
        create_from_tree(tt, bar_trans_id, tree1, 'bar-id')
 
1621
        tt.apply()
 
1622
        self.assertEqual('directory', osutils.file_kind('tree2/foo'))
 
1623
        self.assertFileEqual('baz', 'tree2/bar')
 
1624
 
 
1625
    def test_create_from_tree_bytes(self):
 
1626
        """Provided lines are used instead of tree content."""
 
1627
        tree1 = self.make_branch_and_tree('tree1')
 
1628
        self.build_tree_contents([('tree1/foo', 'bar'),])
 
1629
        tree1.add('foo', 'foo-id')
 
1630
        tree2 = self.make_branch_and_tree('tree2')
 
1631
        tt = TreeTransform(tree2)
 
1632
        foo_trans_id = tt.create_path('foo', tt.root)
 
1633
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id', bytes='qux')
 
1634
        tt.apply()
 
1635
        self.assertFileEqual('qux', 'tree2/foo')
 
1636
 
 
1637
    def test_create_from_tree_symlink(self):
 
1638
        self.requireFeature(SymlinkFeature)
 
1639
        tree1 = self.make_branch_and_tree('tree1')
 
1640
        os.symlink('bar', 'tree1/foo')
 
1641
        tree1.add('foo', 'foo-id')
 
1642
        tt = TreeTransform(self.make_branch_and_tree('tree2'))
 
1643
        foo_trans_id = tt.create_path('foo', tt.root)
 
1644
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id')
 
1645
        tt.apply()
 
1646
        self.assertEqual('bar', os.readlink('tree2/foo'))
 
1647
 
776
1648
 
777
1649
class TransformGroup(object):
 
1650
 
778
1651
    def __init__(self, dirname, root_id):
779
1652
        self.name = dirname
780
1653
        os.mkdir(dirname)
790
1663
    return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
791
1664
 
792
1665
 
 
1666
class TestInventoryAltered(tests.TestCaseWithTransport):
 
1667
 
 
1668
    def test_inventory_altered_unchanged(self):
 
1669
        tree = self.make_branch_and_tree('tree')
 
1670
        self.build_tree(['tree/foo'])
 
1671
        tree.add('foo', 'foo-id')
 
1672
        with TransformPreview(tree) as tt:
 
1673
            self.assertEqual([], tt._inventory_altered())
 
1674
 
 
1675
    def test_inventory_altered_changed_parent_id(self):
 
1676
        tree = self.make_branch_and_tree('tree')
 
1677
        self.build_tree(['tree/foo'])
 
1678
        tree.add('foo', 'foo-id')
 
1679
        with TransformPreview(tree) as tt:
 
1680
            tt.unversion_file(tt.root)
 
1681
            tt.version_file('new-id', tt.root)
 
1682
            foo_trans_id = tt.trans_id_tree_file_id('foo-id')
 
1683
            foo_tuple = ('foo', foo_trans_id)
 
1684
            root_tuple = ('', tt.root)
 
1685
            self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
 
1686
 
 
1687
    def test_inventory_altered_noop_changed_parent_id(self):
 
1688
        tree = self.make_branch_and_tree('tree')
 
1689
        self.build_tree(['tree/foo'])
 
1690
        tree.add('foo', 'foo-id')
 
1691
        with TransformPreview(tree) as tt:
 
1692
            tt.unversion_file(tt.root)
 
1693
            tt.version_file(tree.get_root_id(), tt.root)
 
1694
            foo_trans_id = tt.trans_id_tree_file_id('foo-id')
 
1695
            self.assertEqual([], tt._inventory_altered())
 
1696
 
 
1697
 
793
1698
class TestTransformMerge(TestCaseInTempDir):
 
1699
 
794
1700
    def test_text_merge(self):
795
1701
        root_id = generate_ids.gen_root_id()
796
1702
        base = TransformGroup("base", root_id)
826
1732
        this.tt.new_file('i', this.root, '1\n2\n3\n4\n', 'i')
827
1733
        this.tt.apply()
828
1734
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
 
1735
 
829
1736
        # textual merge
830
1737
        self.assertEqual(this.wt.get_file('a').read(), 'y\nb\nc\nd\bz\n')
831
1738
        # three-way text conflict
832
 
        self.assertEqual(this.wt.get_file('b').read(), 
 
1739
        self.assertEqual(this.wt.get_file('b').read(),
833
1740
                         conflict_text('b', 'b2'))
834
1741
        # OTHER wins
835
1742
        self.assertEqual(this.wt.get_file('c').read(), 'c2')
839
1746
        self.assertEqual(this.wt.get_file('e').read(), 'e2')
840
1747
        # No change
841
1748
        self.assertEqual(this.wt.get_file('f').read(), 'f')
842
 
        # Correct correct results when THIS == OTHER 
 
1749
        # Correct correct results when THIS == OTHER
843
1750
        self.assertEqual(this.wt.get_file('g').read(), 'g')
844
1751
        # Text conflict when THIS & OTHER are text and BASE is dir
845
 
        self.assertEqual(this.wt.get_file('h').read(), 
 
1752
        self.assertEqual(this.wt.get_file('h').read(),
846
1753
                         conflict_text('1\n2\n3\n4\n', 'h\ni\nj\nk\n'))
847
1754
        self.assertEqual(this.wt.get_file_byname('h.THIS').read(),
848
1755
                         '1\n2\n3\n4\n')
849
1756
        self.assertEqual(this.wt.get_file_byname('h.OTHER').read(),
850
1757
                         'h\ni\nj\nk\n')
851
1758
        self.assertEqual(file_kind(this.wt.abspath('h.BASE')), 'directory')
852
 
        self.assertEqual(this.wt.get_file('i').read(), 
 
1759
        self.assertEqual(this.wt.get_file('i').read(),
853
1760
                         conflict_text('1\n2\n3\n4\n', 'h\ni\nj\nk\n'))
854
1761
        self.assertEqual(this.wt.get_file_byname('i.THIS').read(),
855
1762
                         '1\n2\n3\n4\n')
866
1773
        self.assertSubset(merge_modified, modified)
867
1774
        self.assertEqual(len(merge_modified), len(modified))
868
1775
        this.wt.remove('b')
869
 
        this.wt.revert([])
 
1776
        this.wt.revert()
870
1777
 
871
1778
    def test_file_merge(self):
872
 
        if not has_symlinks():
873
 
            raise TestSkipped('Symlinks are not supported on this platform')
 
1779
        self.requireFeature(SymlinkFeature)
874
1780
        root_id = generate_ids.gen_root_id()
875
1781
        base = TransformGroup("BASE", root_id)
876
1782
        this = TransformGroup("THIS", root_id)
880
1786
            tg.tt.new_symlink('b', tg.root, 'b', 'b')
881
1787
            tg.tt.new_file('c', tg.root, 'c', 'c')
882
1788
            tg.tt.new_symlink('d', tg.root, tg.name, 'd')
883
 
        targets = ((base, 'base-e', 'base-f', None, None), 
884
 
                   (this, 'other-e', 'this-f', 'other-g', 'this-h'), 
 
1789
        targets = ((base, 'base-e', 'base-f', None, None),
 
1790
                   (this, 'other-e', 'this-f', 'other-g', 'this-h'),
885
1791
                   (other, 'other-e', None, 'other-g', 'other-h'))
886
1792
        for tg, e_target, f_target, g_target, h_target in targets:
887
 
            for link, target in (('e', e_target), ('f', f_target), 
 
1793
            for link, target in (('e', e_target), ('f', f_target),
888
1794
                                 ('g', g_target), ('h', h_target)):
889
1795
                if target is not None:
890
1796
                    tg.tt.new_symlink(link, tg.root, target, link)
916
1822
        base = TransformGroup("BASE", root_id)
917
1823
        this = TransformGroup("THIS", root_id)
918
1824
        other = TransformGroup("OTHER", root_id)
919
 
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
 
1825
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a')
920
1826
                                   for t in [base, this, other]]
921
 
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
 
1827
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b')
922
1828
                                   for t in [base, this, other]]
923
1829
        base.tt.new_directory('c', base_a, 'c')
924
1830
        this.tt.new_directory('c1', this_a, 'c')
949
1855
        base = TransformGroup("BASE", root_id)
950
1856
        this = TransformGroup("THIS", root_id)
951
1857
        other = TransformGroup("OTHER", root_id)
952
 
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
 
1858
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a')
953
1859
                                   for t in [base, this, other]]
954
 
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
 
1860
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b')
955
1861
                                   for t in [base, this, other]]
956
1862
 
957
1863
        base.tt.new_file('g', base_a, 'g', 'g')
978
1884
 
979
1885
class TestBuildTree(tests.TestCaseWithTransport):
980
1886
 
981
 
    def test_build_tree(self):
982
 
        if not has_symlinks():
983
 
            raise TestSkipped('Test requires symlink support')
 
1887
    def test_build_tree_with_symlinks(self):
 
1888
        self.requireFeature(SymlinkFeature)
984
1889
        os.mkdir('a')
985
1890
        a = BzrDir.create_standalone_workingtree('a')
986
1891
        os.mkdir('a/foo')
1005
1910
        tree.add_reference(subtree)
1006
1911
        tree.commit('a revision')
1007
1912
        tree.branch.create_checkout('target')
1008
 
        self.failUnlessExists('target')
1009
 
        self.failUnlessExists('target/subtree')
 
1913
        self.assertPathExists('target')
 
1914
        self.assertPathExists('target/subtree')
1010
1915
 
1011
1916
    def test_file_conflict_handling(self):
1012
1917
        """Ensure that when building trees, conflict handling is done"""
1034
1939
 
1035
1940
    def test_symlink_conflict_handling(self):
1036
1941
        """Ensure that when building trees, conflict handling is done"""
1037
 
        if not has_symlinks():
1038
 
            raise TestSkipped('Test requires symlink support')
 
1942
        self.requireFeature(SymlinkFeature)
1039
1943
        source = self.make_branch_and_tree('source')
1040
1944
        os.symlink('foo', 'source/symlink')
1041
1945
        source.add('symlink', 'new-symlink')
1050
1954
        os.symlink('foo', 'target2/symlink')
1051
1955
        build_tree(source.basis_tree(), target)
1052
1956
        self.assertEqual([], target.conflicts())
1053
 
        
 
1957
 
1054
1958
    def test_directory_conflict_handling(self):
1055
1959
        """Ensure that when building trees, conflict handling is done"""
1056
1960
        source = self.make_branch_and_tree('source')
1060
1964
        source.commit('added file')
1061
1965
        build_tree(source.basis_tree(), target)
1062
1966
        self.assertEqual([], target.conflicts())
1063
 
        self.failUnlessExists('target/dir1/file')
 
1967
        self.assertPathExists('target/dir1/file')
1064
1968
 
1065
1969
        # Ensure contents are merged
1066
1970
        target = self.make_branch_and_tree('target2')
1067
1971
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
1068
1972
        build_tree(source.basis_tree(), target)
1069
1973
        self.assertEqual([], target.conflicts())
1070
 
        self.failUnlessExists('target2/dir1/file2')
1071
 
        self.failUnlessExists('target2/dir1/file')
 
1974
        self.assertPathExists('target2/dir1/file2')
 
1975
        self.assertPathExists('target2/dir1/file')
1072
1976
 
1073
1977
        # Ensure new contents are suppressed for existing branches
1074
1978
        target = self.make_branch_and_tree('target3')
1075
1979
        self.make_branch('target3/dir1')
1076
1980
        self.build_tree(['target3/dir1/file2'])
1077
1981
        build_tree(source.basis_tree(), target)
1078
 
        self.failIfExists('target3/dir1/file')
1079
 
        self.failUnlessExists('target3/dir1/file2')
1080
 
        self.failUnlessExists('target3/dir1.diverted/file')
 
1982
        self.assertPathDoesNotExist('target3/dir1/file')
 
1983
        self.assertPathExists('target3/dir1/file2')
 
1984
        self.assertPathExists('target3/dir1.diverted/file')
1081
1985
        self.assertEqual([DuplicateEntry('Diverted to',
1082
1986
            'dir1.diverted', 'dir1', 'new-dir1', None)],
1083
1987
            target.conflicts())
1086
1990
        self.build_tree(['target4/dir1/'])
1087
1991
        self.make_branch('target4/dir1/file')
1088
1992
        build_tree(source.basis_tree(), target)
1089
 
        self.failUnlessExists('target4/dir1/file')
 
1993
        self.assertPathExists('target4/dir1/file')
1090
1994
        self.assertEqual('directory', file_kind('target4/dir1/file'))
1091
 
        self.failUnlessExists('target4/dir1/file.diverted')
 
1995
        self.assertPathExists('target4/dir1/file.diverted')
1092
1996
        self.assertEqual([DuplicateEntry('Diverted to',
1093
1997
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
1094
1998
            target.conflicts())
1112
2016
        target = self.make_branch_and_tree('target')
1113
2017
        self.build_tree(['target/name'])
1114
2018
        target.add('name')
1115
 
        self.assertRaises(errors.WorkingTreeAlreadyPopulated, 
 
2019
        self.assertRaises(errors.WorkingTreeAlreadyPopulated,
1116
2020
            build_tree, source.basis_tree(), target)
1117
2021
 
1118
 
 
1119
 
class MockTransform(object):
1120
 
 
1121
 
    def has_named_child(self, by_parent, parent_id, name):
1122
 
        for child_id in by_parent[parent_id]:
1123
 
            if child_id == '0':
1124
 
                if name == "name~":
1125
 
                    return True
1126
 
            elif name == "name.~%s~" % child_id:
1127
 
                return True
1128
 
        return False
1129
 
 
1130
 
class MockEntry(object):
1131
 
    def __init__(self):
1132
 
        object.__init__(self)
1133
 
        self.name = "name"
1134
 
 
1135
 
class TestGetBackupName(TestCase):
1136
 
    def test_get_backup_name(self):
 
2022
    def test_build_tree_rename_count(self):
 
2023
        source = self.make_branch_and_tree('source')
 
2024
        self.build_tree(['source/file1', 'source/dir1/'])
 
2025
        source.add(['file1', 'dir1'])
 
2026
        source.commit('add1')
 
2027
        target1 = self.make_branch_and_tree('target1')
 
2028
        transform_result = build_tree(source.basis_tree(), target1)
 
2029
        self.assertEqual(2, transform_result.rename_count)
 
2030
 
 
2031
        self.build_tree(['source/dir1/file2'])
 
2032
        source.add(['dir1/file2'])
 
2033
        source.commit('add3')
 
2034
        target2 = self.make_branch_and_tree('target2')
 
2035
        transform_result = build_tree(source.basis_tree(), target2)
 
2036
        # children of non-root directories should not be renamed
 
2037
        self.assertEqual(2, transform_result.rename_count)
 
2038
 
 
2039
    def create_ab_tree(self):
 
2040
        """Create a committed test tree with two files"""
 
2041
        source = self.make_branch_and_tree('source')
 
2042
        self.build_tree_contents([('source/file1', 'A')])
 
2043
        self.build_tree_contents([('source/file2', 'B')])
 
2044
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
 
2045
        source.commit('commit files')
 
2046
        source.lock_write()
 
2047
        self.addCleanup(source.unlock)
 
2048
        return source
 
2049
 
 
2050
    def test_build_tree_accelerator_tree(self):
 
2051
        source = self.create_ab_tree()
 
2052
        self.build_tree_contents([('source/file2', 'C')])
 
2053
        calls = []
 
2054
        real_source_get_file = source.get_file
 
2055
        def get_file(file_id, path=None):
 
2056
            calls.append(file_id)
 
2057
            return real_source_get_file(file_id, path)
 
2058
        source.get_file = get_file
 
2059
        target = self.make_branch_and_tree('target')
 
2060
        revision_tree = source.basis_tree()
 
2061
        revision_tree.lock_read()
 
2062
        self.addCleanup(revision_tree.unlock)
 
2063
        build_tree(revision_tree, target, source)
 
2064
        self.assertEqual(['file1-id'], calls)
 
2065
        target.lock_read()
 
2066
        self.addCleanup(target.unlock)
 
2067
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
2068
 
 
2069
    def test_build_tree_accelerator_tree_observes_sha1(self):
 
2070
        source = self.create_ab_tree()
 
2071
        sha1 = osutils.sha_string('A')
 
2072
        target = self.make_branch_and_tree('target')
 
2073
        target.lock_write()
 
2074
        self.addCleanup(target.unlock)
 
2075
        state = target.current_dirstate()
 
2076
        state._cutoff_time = time.time() + 60
 
2077
        build_tree(source.basis_tree(), target, source)
 
2078
        entry = state._get_entry(0, path_utf8='file1')
 
2079
        self.assertEqual(sha1, entry[1][0][1])
 
2080
 
 
2081
    def test_build_tree_accelerator_tree_missing_file(self):
 
2082
        source = self.create_ab_tree()
 
2083
        os.unlink('source/file1')
 
2084
        source.remove(['file2'])
 
2085
        target = self.make_branch_and_tree('target')
 
2086
        revision_tree = source.basis_tree()
 
2087
        revision_tree.lock_read()
 
2088
        self.addCleanup(revision_tree.unlock)
 
2089
        build_tree(revision_tree, target, source)
 
2090
        target.lock_read()
 
2091
        self.addCleanup(target.unlock)
 
2092
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
2093
 
 
2094
    def test_build_tree_accelerator_wrong_kind(self):
 
2095
        self.requireFeature(SymlinkFeature)
 
2096
        source = self.make_branch_and_tree('source')
 
2097
        self.build_tree_contents([('source/file1', '')])
 
2098
        self.build_tree_contents([('source/file2', '')])
 
2099
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
 
2100
        source.commit('commit files')
 
2101
        os.unlink('source/file2')
 
2102
        self.build_tree_contents([('source/file2/', 'C')])
 
2103
        os.unlink('source/file1')
 
2104
        os.symlink('file2', 'source/file1')
 
2105
        calls = []
 
2106
        real_source_get_file = source.get_file
 
2107
        def get_file(file_id, path=None):
 
2108
            calls.append(file_id)
 
2109
            return real_source_get_file(file_id, path)
 
2110
        source.get_file = get_file
 
2111
        target = self.make_branch_and_tree('target')
 
2112
        revision_tree = source.basis_tree()
 
2113
        revision_tree.lock_read()
 
2114
        self.addCleanup(revision_tree.unlock)
 
2115
        build_tree(revision_tree, target, source)
 
2116
        self.assertEqual([], calls)
 
2117
        target.lock_read()
 
2118
        self.addCleanup(target.unlock)
 
2119
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
2120
 
 
2121
    def test_build_tree_hardlink(self):
 
2122
        self.requireFeature(HardlinkFeature)
 
2123
        source = self.create_ab_tree()
 
2124
        target = self.make_branch_and_tree('target')
 
2125
        revision_tree = source.basis_tree()
 
2126
        revision_tree.lock_read()
 
2127
        self.addCleanup(revision_tree.unlock)
 
2128
        build_tree(revision_tree, target, source, hardlink=True)
 
2129
        target.lock_read()
 
2130
        self.addCleanup(target.unlock)
 
2131
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
2132
        source_stat = os.stat('source/file1')
 
2133
        target_stat = os.stat('target/file1')
 
2134
        self.assertEqual(source_stat, target_stat)
 
2135
 
 
2136
        # Explicitly disallowing hardlinks should prevent them.
 
2137
        target2 = self.make_branch_and_tree('target2')
 
2138
        build_tree(revision_tree, target2, source, hardlink=False)
 
2139
        target2.lock_read()
 
2140
        self.addCleanup(target2.unlock)
 
2141
        self.assertEqual([], list(target2.iter_changes(revision_tree)))
 
2142
        source_stat = os.stat('source/file1')
 
2143
        target2_stat = os.stat('target2/file1')
 
2144
        self.assertNotEqual(source_stat, target2_stat)
 
2145
 
 
2146
    def test_build_tree_accelerator_tree_moved(self):
 
2147
        source = self.make_branch_and_tree('source')
 
2148
        self.build_tree_contents([('source/file1', 'A')])
 
2149
        source.add(['file1'], ['file1-id'])
 
2150
        source.commit('commit files')
 
2151
        source.rename_one('file1', 'file2')
 
2152
        source.lock_read()
 
2153
        self.addCleanup(source.unlock)
 
2154
        target = self.make_branch_and_tree('target')
 
2155
        revision_tree = source.basis_tree()
 
2156
        revision_tree.lock_read()
 
2157
        self.addCleanup(revision_tree.unlock)
 
2158
        build_tree(revision_tree, target, source)
 
2159
        target.lock_read()
 
2160
        self.addCleanup(target.unlock)
 
2161
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
2162
 
 
2163
    def test_build_tree_hardlinks_preserve_execute(self):
 
2164
        self.requireFeature(HardlinkFeature)
 
2165
        source = self.create_ab_tree()
 
2166
        tt = TreeTransform(source)
 
2167
        trans_id = tt.trans_id_tree_file_id('file1-id')
 
2168
        tt.set_executability(True, trans_id)
 
2169
        tt.apply()
 
2170
        self.assertTrue(source.is_executable('file1-id'))
 
2171
        target = self.make_branch_and_tree('target')
 
2172
        revision_tree = source.basis_tree()
 
2173
        revision_tree.lock_read()
 
2174
        self.addCleanup(revision_tree.unlock)
 
2175
        build_tree(revision_tree, target, source, hardlink=True)
 
2176
        target.lock_read()
 
2177
        self.addCleanup(target.unlock)
 
2178
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
2179
        self.assertTrue(source.is_executable('file1-id'))
 
2180
 
 
2181
    def install_rot13_content_filter(self, pattern):
 
2182
        # We could use
 
2183
        # self.addCleanup(filters._reset_registry, filters._reset_registry())
 
2184
        # below, but that looks a bit... hard to read even if it's exactly
 
2185
        # the same thing.
 
2186
        original_registry = filters._reset_registry()
 
2187
        def restore_registry():
 
2188
            filters._reset_registry(original_registry)
 
2189
        self.addCleanup(restore_registry)
 
2190
        def rot13(chunks, context=None):
 
2191
            return [''.join(chunks).encode('rot13')]
 
2192
        rot13filter = filters.ContentFilter(rot13, rot13)
 
2193
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
 
2194
        os.mkdir(self.test_home_dir + '/.bazaar')
 
2195
        rules_filename = self.test_home_dir + '/.bazaar/rules'
 
2196
        f = open(rules_filename, 'wb')
 
2197
        f.write('[name %s]\nrot13=yes\n' % (pattern,))
 
2198
        f.close()
 
2199
        def uninstall_rules():
 
2200
            os.remove(rules_filename)
 
2201
            rules.reset_rules()
 
2202
        self.addCleanup(uninstall_rules)
 
2203
        rules.reset_rules()
 
2204
 
 
2205
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
 
2206
        """build_tree will not hardlink files that have content filtering rules
 
2207
        applied to them (but will still hardlink other files from the same tree
 
2208
        if it can).
 
2209
        """
 
2210
        self.requireFeature(HardlinkFeature)
 
2211
        self.install_rot13_content_filter('file1')
 
2212
        source = self.create_ab_tree()
 
2213
        target = self.make_branch_and_tree('target')
 
2214
        revision_tree = source.basis_tree()
 
2215
        revision_tree.lock_read()
 
2216
        self.addCleanup(revision_tree.unlock)
 
2217
        build_tree(revision_tree, target, source, hardlink=True)
 
2218
        target.lock_read()
 
2219
        self.addCleanup(target.unlock)
 
2220
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
2221
        source_stat = os.stat('source/file1')
 
2222
        target_stat = os.stat('target/file1')
 
2223
        self.assertNotEqual(source_stat, target_stat)
 
2224
        source_stat = os.stat('source/file2')
 
2225
        target_stat = os.stat('target/file2')
 
2226
        self.assertEqualStat(source_stat, target_stat)
 
2227
 
 
2228
    def test_case_insensitive_build_tree_inventory(self):
 
2229
        if (features.CaseInsensitiveFilesystemFeature.available()
 
2230
            or features.CaseInsCasePresFilenameFeature.available()):
 
2231
            raise tests.UnavailableFeature('Fully case sensitive filesystem')
 
2232
        source = self.make_branch_and_tree('source')
 
2233
        self.build_tree(['source/file', 'source/FILE'])
 
2234
        source.add(['file', 'FILE'], ['lower-id', 'upper-id'])
 
2235
        source.commit('added files')
 
2236
        # Don't try this at home, kids!
 
2237
        # Force the tree to report that it is case insensitive
 
2238
        target = self.make_branch_and_tree('target')
 
2239
        target.case_sensitive = False
 
2240
        build_tree(source.basis_tree(), target, source, delta_from_tree=True)
 
2241
        self.assertEqual('file.moved', target.id2path('lower-id'))
 
2242
        self.assertEqual('FILE', target.id2path('upper-id'))
 
2243
 
 
2244
    def test_build_tree_observes_sha(self):
 
2245
        source = self.make_branch_and_tree('source')
 
2246
        self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
 
2247
        source.add(['file1', 'dir', 'dir/file2'],
 
2248
                   ['file1-id', 'dir-id', 'file2-id'])
 
2249
        source.commit('new files')
 
2250
        target = self.make_branch_and_tree('target')
 
2251
        target.lock_write()
 
2252
        self.addCleanup(target.unlock)
 
2253
        # We make use of the fact that DirState caches its cutoff time. So we
 
2254
        # set the 'safe' time to one minute in the future.
 
2255
        state = target.current_dirstate()
 
2256
        state._cutoff_time = time.time() + 60
 
2257
        build_tree(source.basis_tree(), target)
 
2258
        entry1_sha = osutils.sha_file_by_name('source/file1')
 
2259
        entry2_sha = osutils.sha_file_by_name('source/dir/file2')
 
2260
        # entry[1] is the state information, entry[1][0] is the state of the
 
2261
        # working tree, entry[1][0][1] is the sha value for the current working
 
2262
        # tree
 
2263
        entry1 = state._get_entry(0, path_utf8='file1')
 
2264
        self.assertEqual(entry1_sha, entry1[1][0][1])
 
2265
        # The 'size' field must also be set.
 
2266
        self.assertEqual(25, entry1[1][0][2])
 
2267
        entry1_state = entry1[1][0]
 
2268
        entry2 = state._get_entry(0, path_utf8='dir/file2')
 
2269
        self.assertEqual(entry2_sha, entry2[1][0][1])
 
2270
        self.assertEqual(29, entry2[1][0][2])
 
2271
        entry2_state = entry2[1][0]
 
2272
        # Now, make sure that we don't have to re-read the content. The
 
2273
        # packed_stat should match exactly.
 
2274
        self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
 
2275
        self.assertEqual(entry2_sha,
 
2276
                         target.get_file_sha1('file2-id', 'dir/file2'))
 
2277
        self.assertEqual(entry1_state, entry1[1][0])
 
2278
        self.assertEqual(entry2_state, entry2[1][0])
 
2279
 
 
2280
 
 
2281
class TestCommitTransform(tests.TestCaseWithTransport):
 
2282
 
 
2283
    def get_branch(self):
 
2284
        tree = self.make_branch_and_tree('tree')
 
2285
        tree.lock_write()
 
2286
        self.addCleanup(tree.unlock)
 
2287
        tree.commit('empty commit')
 
2288
        return tree.branch
 
2289
 
 
2290
    def get_branch_and_transform(self):
 
2291
        branch = self.get_branch()
 
2292
        tt = TransformPreview(branch.basis_tree())
 
2293
        self.addCleanup(tt.finalize)
 
2294
        return branch, tt
 
2295
 
 
2296
    def test_commit_wrong_basis(self):
 
2297
        branch = self.get_branch()
 
2298
        basis = branch.repository.revision_tree(
 
2299
            _mod_revision.NULL_REVISION)
 
2300
        tt = TransformPreview(basis)
 
2301
        self.addCleanup(tt.finalize)
 
2302
        e = self.assertRaises(ValueError, tt.commit, branch, '')
 
2303
        self.assertEqual('TreeTransform not based on branch basis: null:',
 
2304
                         str(e))
 
2305
 
 
2306
    def test_empy_commit(self):
 
2307
        branch, tt = self.get_branch_and_transform()
 
2308
        rev = tt.commit(branch, 'my message')
 
2309
        self.assertEqual(2, branch.revno())
 
2310
        repo = branch.repository
 
2311
        self.assertEqual('my message', repo.get_revision(rev).message)
 
2312
 
 
2313
    def test_merge_parents(self):
 
2314
        branch, tt = self.get_branch_and_transform()
 
2315
        rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
 
2316
        self.assertEqual(['rev1b', 'rev1c'],
 
2317
                         branch.basis_tree().get_parent_ids()[1:])
 
2318
 
 
2319
    def test_first_commit(self):
 
2320
        branch = self.make_branch('branch')
 
2321
        branch.lock_write()
 
2322
        self.addCleanup(branch.unlock)
 
2323
        tt = TransformPreview(branch.basis_tree())
 
2324
        self.addCleanup(tt.finalize)
 
2325
        tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
 
2326
        rev = tt.commit(branch, 'my message')
 
2327
        self.assertEqual([], branch.basis_tree().get_parent_ids())
 
2328
        self.assertNotEqual(_mod_revision.NULL_REVISION,
 
2329
                            branch.last_revision())
 
2330
 
 
2331
    def test_first_commit_with_merge_parents(self):
 
2332
        branch = self.make_branch('branch')
 
2333
        branch.lock_write()
 
2334
        self.addCleanup(branch.unlock)
 
2335
        tt = TransformPreview(branch.basis_tree())
 
2336
        self.addCleanup(tt.finalize)
 
2337
        e = self.assertRaises(ValueError, tt.commit, branch,
 
2338
                          'my message', ['rev1b-id'])
 
2339
        self.assertEqual('Cannot supply merge parents for first commit.',
 
2340
                         str(e))
 
2341
        self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
 
2342
 
 
2343
    def test_add_files(self):
 
2344
        branch, tt = self.get_branch_and_transform()
 
2345
        tt.new_file('file', tt.root, 'contents', 'file-id')
 
2346
        trans_id = tt.new_directory('dir', tt.root, 'dir-id')
 
2347
        if SymlinkFeature.available():
 
2348
            tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
 
2349
        rev = tt.commit(branch, 'message')
 
2350
        tree = branch.basis_tree()
 
2351
        self.assertEqual('file', tree.id2path('file-id'))
 
2352
        self.assertEqual('contents', tree.get_file_text('file-id'))
 
2353
        self.assertEqual('dir', tree.id2path('dir-id'))
 
2354
        if SymlinkFeature.available():
 
2355
            self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
 
2356
            self.assertEqual('target', tree.get_symlink_target('symlink-id'))
 
2357
 
 
2358
    def test_add_unversioned(self):
 
2359
        branch, tt = self.get_branch_and_transform()
 
2360
        tt.new_file('file', tt.root, 'contents')
 
2361
        self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
 
2362
                          'message', strict=True)
 
2363
 
 
2364
    def test_modify_strict(self):
 
2365
        branch, tt = self.get_branch_and_transform()
 
2366
        tt.new_file('file', tt.root, 'contents', 'file-id')
 
2367
        tt.commit(branch, 'message', strict=True)
 
2368
        tt = TransformPreview(branch.basis_tree())
 
2369
        self.addCleanup(tt.finalize)
 
2370
        trans_id = tt.trans_id_file_id('file-id')
 
2371
        tt.delete_contents(trans_id)
 
2372
        tt.create_file('contents', trans_id)
 
2373
        tt.commit(branch, 'message', strict=True)
 
2374
 
 
2375
    def test_commit_malformed(self):
 
2376
        """Committing a malformed transform should raise an exception.
 
2377
 
 
2378
        In this case, we are adding a file without adding its parent.
 
2379
        """
 
2380
        branch, tt = self.get_branch_and_transform()
 
2381
        parent_id = tt.trans_id_file_id('parent-id')
 
2382
        tt.new_file('file', parent_id, 'contents', 'file-id')
 
2383
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
 
2384
                          'message')
 
2385
 
 
2386
    def test_commit_rich_revision_data(self):
 
2387
        branch, tt = self.get_branch_and_transform()
 
2388
        rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
 
2389
                           committer='me <me@example.com>',
 
2390
                           revprops={'foo': 'bar'}, revision_id='revid-1',
 
2391
                           authors=['Author1 <author1@example.com>',
 
2392
                              'Author2 <author2@example.com>',
 
2393
                               ])
 
2394
        self.assertEqual('revid-1', rev_id)
 
2395
        revision = branch.repository.get_revision(rev_id)
 
2396
        self.assertEqual(1, revision.timestamp)
 
2397
        self.assertEqual(43201, revision.timezone)
 
2398
        self.assertEqual('me <me@example.com>', revision.committer)
 
2399
        self.assertEqual(['Author1 <author1@example.com>',
 
2400
                          'Author2 <author2@example.com>'],
 
2401
                         revision.get_apparent_authors())
 
2402
        del revision.properties['authors']
 
2403
        self.assertEqual({'foo': 'bar',
 
2404
                          'branch-nick': 'tree'},
 
2405
                         revision.properties)
 
2406
 
 
2407
    def test_no_explicit_revprops(self):
 
2408
        branch, tt = self.get_branch_and_transform()
 
2409
        rev_id = tt.commit(branch, 'message', authors=[
 
2410
            'Author1 <author1@example.com>',
 
2411
            'Author2 <author2@example.com>', ])
 
2412
        revision = branch.repository.get_revision(rev_id)
 
2413
        self.assertEqual(['Author1 <author1@example.com>',
 
2414
                          'Author2 <author2@example.com>'],
 
2415
                         revision.get_apparent_authors())
 
2416
        self.assertEqual('tree', revision.properties['branch-nick'])
 
2417
 
 
2418
 
 
2419
class TestBackupName(tests.TestCase):
 
2420
 
 
2421
    def test_deprecations(self):
 
2422
        class MockTransform(object):
 
2423
 
 
2424
            def has_named_child(self, by_parent, parent_id, name):
 
2425
                return name in by_parent.get(parent_id, [])
 
2426
 
 
2427
        class MockEntry(object):
 
2428
 
 
2429
            def __init__(self):
 
2430
                object.__init__(self)
 
2431
                self.name = "name"
 
2432
 
1137
2433
        tt = MockTransform()
1138
 
        name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
1139
 
        self.assertEqual(name, 'name.~1~')
1140
 
        name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
1141
 
        self.assertEqual(name, 'name.~2~')
1142
 
        name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
1143
 
        self.assertEqual(name, 'name.~1~')
1144
 
        name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
1145
 
        self.assertEqual(name, 'name.~1~')
1146
 
        name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
1147
 
        self.assertEqual(name, 'name.~4~')
 
2434
        name1 = self.applyDeprecated(
 
2435
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2436
            transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
 
2437
        self.assertEqual('name.~1~', name1)
 
2438
        name2 = self.applyDeprecated(
 
2439
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2440
            transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
 
2441
        self.assertEqual('name.~2~', name2)
 
2442
 
 
2443
 
 
2444
class TestFileMover(tests.TestCaseWithTransport):
 
2445
 
 
2446
    def test_file_mover(self):
 
2447
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
 
2448
        mover = _FileMover()
 
2449
        mover.rename('a', 'q')
 
2450
        self.assertPathExists('q')
 
2451
        self.assertPathDoesNotExist('a')
 
2452
        self.assertPathExists('q/b')
 
2453
        self.assertPathExists('c')
 
2454
        self.assertPathExists('c/d')
 
2455
 
 
2456
    def test_pre_delete_rollback(self):
 
2457
        self.build_tree(['a/'])
 
2458
        mover = _FileMover()
 
2459
        mover.pre_delete('a', 'q')
 
2460
        self.assertPathExists('q')
 
2461
        self.assertPathDoesNotExist('a')
 
2462
        mover.rollback()
 
2463
        self.assertPathDoesNotExist('q')
 
2464
        self.assertPathExists('a')
 
2465
 
 
2466
    def test_apply_deletions(self):
 
2467
        self.build_tree(['a/', 'b/'])
 
2468
        mover = _FileMover()
 
2469
        mover.pre_delete('a', 'q')
 
2470
        mover.pre_delete('b', 'r')
 
2471
        self.assertPathExists('q')
 
2472
        self.assertPathExists('r')
 
2473
        self.assertPathDoesNotExist('a')
 
2474
        self.assertPathDoesNotExist('b')
 
2475
        mover.apply_deletions()
 
2476
        self.assertPathDoesNotExist('q')
 
2477
        self.assertPathDoesNotExist('r')
 
2478
        self.assertPathDoesNotExist('a')
 
2479
        self.assertPathDoesNotExist('b')
 
2480
 
 
2481
    def test_file_mover_rollback(self):
 
2482
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
 
2483
        mover = _FileMover()
 
2484
        mover.rename('c/d', 'c/f')
 
2485
        mover.rename('c/e', 'c/d')
 
2486
        try:
 
2487
            mover.rename('a', 'c')
 
2488
        except errors.FileExists, e:
 
2489
            mover.rollback()
 
2490
        self.assertPathExists('a')
 
2491
        self.assertPathExists('c/d')
 
2492
 
 
2493
 
 
2494
class Bogus(Exception):
 
2495
    pass
 
2496
 
 
2497
 
 
2498
class TestTransformRollback(tests.TestCaseWithTransport):
 
2499
 
 
2500
    class ExceptionFileMover(_FileMover):
 
2501
 
 
2502
        def __init__(self, bad_source=None, bad_target=None):
 
2503
            _FileMover.__init__(self)
 
2504
            self.bad_source = bad_source
 
2505
            self.bad_target = bad_target
 
2506
 
 
2507
        def rename(self, source, target):
 
2508
            if (self.bad_source is not None and
 
2509
                source.endswith(self.bad_source)):
 
2510
                raise Bogus
 
2511
            elif (self.bad_target is not None and
 
2512
                target.endswith(self.bad_target)):
 
2513
                raise Bogus
 
2514
            else:
 
2515
                _FileMover.rename(self, source, target)
 
2516
 
 
2517
    def test_rollback_rename(self):
 
2518
        tree = self.make_branch_and_tree('.')
 
2519
        self.build_tree(['a/', 'a/b'])
 
2520
        tt = TreeTransform(tree)
 
2521
        self.addCleanup(tt.finalize)
 
2522
        a_id = tt.trans_id_tree_path('a')
 
2523
        tt.adjust_path('c', tt.root, a_id)
 
2524
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
 
2525
        self.assertRaises(Bogus, tt.apply,
 
2526
                          _mover=self.ExceptionFileMover(bad_source='a'))
 
2527
        self.assertPathExists('a')
 
2528
        self.assertPathExists('a/b')
 
2529
        tt.apply()
 
2530
        self.assertPathExists('c')
 
2531
        self.assertPathExists('c/d')
 
2532
 
 
2533
    def test_rollback_rename_into_place(self):
 
2534
        tree = self.make_branch_and_tree('.')
 
2535
        self.build_tree(['a/', 'a/b'])
 
2536
        tt = TreeTransform(tree)
 
2537
        self.addCleanup(tt.finalize)
 
2538
        a_id = tt.trans_id_tree_path('a')
 
2539
        tt.adjust_path('c', tt.root, a_id)
 
2540
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
 
2541
        self.assertRaises(Bogus, tt.apply,
 
2542
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
 
2543
        self.assertPathExists('a')
 
2544
        self.assertPathExists('a/b')
 
2545
        tt.apply()
 
2546
        self.assertPathExists('c')
 
2547
        self.assertPathExists('c/d')
 
2548
 
 
2549
    def test_rollback_deletion(self):
 
2550
        tree = self.make_branch_and_tree('.')
 
2551
        self.build_tree(['a/', 'a/b'])
 
2552
        tt = TreeTransform(tree)
 
2553
        self.addCleanup(tt.finalize)
 
2554
        a_id = tt.trans_id_tree_path('a')
 
2555
        tt.delete_contents(a_id)
 
2556
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
 
2557
        self.assertRaises(Bogus, tt.apply,
 
2558
                          _mover=self.ExceptionFileMover(bad_target='d'))
 
2559
        self.assertPathExists('a')
 
2560
        self.assertPathExists('a/b')
 
2561
 
 
2562
 
 
2563
class TestFinalizeRobustness(tests.TestCaseWithTransport):
 
2564
    """Ensure treetransform creation errors can be safely cleaned up after"""
 
2565
 
 
2566
    def _override_globals_in_method(self, instance, method_name, globals):
 
2567
        """Replace method on instance with one with updated globals"""
 
2568
        import types
 
2569
        func = getattr(instance, method_name).im_func
 
2570
        new_globals = dict(func.func_globals)
 
2571
        new_globals.update(globals)
 
2572
        new_func = types.FunctionType(func.func_code, new_globals,
 
2573
            func.func_name, func.func_defaults)
 
2574
        setattr(instance, method_name,
 
2575
            types.MethodType(new_func, instance, instance.__class__))
 
2576
        self.addCleanup(delattr, instance, method_name)
 
2577
 
 
2578
    @staticmethod
 
2579
    def _fake_open_raises_before(name, mode):
 
2580
        """Like open() but raises before doing anything"""
 
2581
        raise RuntimeError
 
2582
 
 
2583
    @staticmethod
 
2584
    def _fake_open_raises_after(name, mode):
 
2585
        """Like open() but raises after creating file without returning"""
 
2586
        open(name, mode).close()
 
2587
        raise RuntimeError
 
2588
 
 
2589
    def create_transform_and_root_trans_id(self):
 
2590
        """Setup a transform creating a file in limbo"""
 
2591
        tree = self.make_branch_and_tree('.')
 
2592
        tt = TreeTransform(tree)
 
2593
        return tt, tt.create_path("a", tt.root)
 
2594
 
 
2595
    def create_transform_and_subdir_trans_id(self):
 
2596
        """Setup a transform creating a directory containing a file in limbo"""
 
2597
        tree = self.make_branch_and_tree('.')
 
2598
        tt = TreeTransform(tree)
 
2599
        d_trans_id = tt.create_path("d", tt.root)
 
2600
        tt.create_directory(d_trans_id)
 
2601
        f_trans_id = tt.create_path("a", d_trans_id)
 
2602
        tt.adjust_path("a", d_trans_id, f_trans_id)
 
2603
        return tt, f_trans_id
 
2604
 
 
2605
    def test_root_create_file_open_raises_before_creation(self):
 
2606
        tt, trans_id = self.create_transform_and_root_trans_id()
 
2607
        self._override_globals_in_method(tt, "create_file",
 
2608
            {"open": self._fake_open_raises_before})
 
2609
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
 
2610
        path = tt._limbo_name(trans_id)
 
2611
        self.assertPathDoesNotExist(path)
 
2612
        tt.finalize()
 
2613
        self.assertPathDoesNotExist(tt._limbodir)
 
2614
 
 
2615
    def test_root_create_file_open_raises_after_creation(self):
 
2616
        tt, trans_id = self.create_transform_and_root_trans_id()
 
2617
        self._override_globals_in_method(tt, "create_file",
 
2618
            {"open": self._fake_open_raises_after})
 
2619
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
 
2620
        path = tt._limbo_name(trans_id)
 
2621
        self.assertPathExists(path)
 
2622
        tt.finalize()
 
2623
        self.assertPathDoesNotExist(path)
 
2624
        self.assertPathDoesNotExist(tt._limbodir)
 
2625
 
 
2626
    def test_subdir_create_file_open_raises_before_creation(self):
 
2627
        tt, trans_id = self.create_transform_and_subdir_trans_id()
 
2628
        self._override_globals_in_method(tt, "create_file",
 
2629
            {"open": self._fake_open_raises_before})
 
2630
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
 
2631
        path = tt._limbo_name(trans_id)
 
2632
        self.assertPathDoesNotExist(path)
 
2633
        tt.finalize()
 
2634
        self.assertPathDoesNotExist(tt._limbodir)
 
2635
 
 
2636
    def test_subdir_create_file_open_raises_after_creation(self):
 
2637
        tt, trans_id = self.create_transform_and_subdir_trans_id()
 
2638
        self._override_globals_in_method(tt, "create_file",
 
2639
            {"open": self._fake_open_raises_after})
 
2640
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
 
2641
        path = tt._limbo_name(trans_id)
 
2642
        self.assertPathExists(path)
 
2643
        tt.finalize()
 
2644
        self.assertPathDoesNotExist(path)
 
2645
        self.assertPathDoesNotExist(tt._limbodir)
 
2646
 
 
2647
    def test_rename_in_limbo_rename_raises_after_rename(self):
 
2648
        tt, trans_id = self.create_transform_and_root_trans_id()
 
2649
        parent1 = tt.new_directory('parent1', tt.root)
 
2650
        child1 = tt.new_file('child1', parent1, 'contents')
 
2651
        parent2 = tt.new_directory('parent2', tt.root)
 
2652
 
 
2653
        class FakeOSModule(object):
 
2654
            def rename(self, old, new):
 
2655
                os.rename(old, new)
 
2656
                raise RuntimeError
 
2657
        self._override_globals_in_method(tt, "_rename_in_limbo",
 
2658
            {"os": FakeOSModule()})
 
2659
        self.assertRaises(
 
2660
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
 
2661
        path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
 
2662
        self.assertPathExists(path)
 
2663
        tt.finalize()
 
2664
        self.assertPathDoesNotExist(path)
 
2665
        self.assertPathDoesNotExist(tt._limbodir)
 
2666
 
 
2667
    def test_rename_in_limbo_rename_raises_before_rename(self):
 
2668
        tt, trans_id = self.create_transform_and_root_trans_id()
 
2669
        parent1 = tt.new_directory('parent1', tt.root)
 
2670
        child1 = tt.new_file('child1', parent1, 'contents')
 
2671
        parent2 = tt.new_directory('parent2', tt.root)
 
2672
 
 
2673
        class FakeOSModule(object):
 
2674
            def rename(self, old, new):
 
2675
                raise RuntimeError
 
2676
        self._override_globals_in_method(tt, "_rename_in_limbo",
 
2677
            {"os": FakeOSModule()})
 
2678
        self.assertRaises(
 
2679
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
 
2680
        path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
 
2681
        self.assertPathExists(path)
 
2682
        tt.finalize()
 
2683
        self.assertPathDoesNotExist(path)
 
2684
        self.assertPathDoesNotExist(tt._limbodir)
 
2685
 
 
2686
 
 
2687
class TestTransformMissingParent(tests.TestCaseWithTransport):
 
2688
 
 
2689
    def make_tt_with_versioned_dir(self):
 
2690
        wt = self.make_branch_and_tree('.')
 
2691
        self.build_tree(['dir/',])
 
2692
        wt.add(['dir'], ['dir-id'])
 
2693
        wt.commit('Create dir')
 
2694
        tt = TreeTransform(wt)
 
2695
        self.addCleanup(tt.finalize)
 
2696
        return wt, tt
 
2697
 
 
2698
    def test_resolve_create_parent_for_versioned_file(self):
 
2699
        wt, tt = self.make_tt_with_versioned_dir()
 
2700
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2701
        file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
 
2702
        tt.delete_contents(dir_tid)
 
2703
        tt.unversion_file(dir_tid)
 
2704
        conflicts = resolve_conflicts(tt)
 
2705
        # one conflict for the missing directory, one for the unversioned
 
2706
        # parent
 
2707
        self.assertLength(2, conflicts)
 
2708
 
 
2709
    def test_non_versioned_file_create_conflict(self):
 
2710
        wt, tt = self.make_tt_with_versioned_dir()
 
2711
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2712
        tt.new_file('file', dir_tid, 'Contents')
 
2713
        tt.delete_contents(dir_tid)
 
2714
        tt.unversion_file(dir_tid)
 
2715
        conflicts = resolve_conflicts(tt)
 
2716
        # no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
 
2717
        self.assertLength(1, conflicts)
 
2718
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
2719
                         conflicts.pop())
 
2720
 
 
2721
 
 
2722
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
 
2723
                  ('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
 
2724
                  (False, False))
 
2725
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
 
2726
              ('', ''), ('directory', 'directory'), (False, False))
 
2727
 
 
2728
 
 
2729
class TestTransformPreview(tests.TestCaseWithTransport):
 
2730
 
 
2731
    def create_tree(self):
 
2732
        tree = self.make_branch_and_tree('.')
 
2733
        self.build_tree_contents([('a', 'content 1')])
 
2734
        tree.set_root_id('TREE_ROOT')
 
2735
        tree.add('a', 'a-id')
 
2736
        tree.commit('rev1', rev_id='rev1')
 
2737
        return tree.branch.repository.revision_tree('rev1')
 
2738
 
 
2739
    def get_empty_preview(self):
 
2740
        repository = self.make_repository('repo')
 
2741
        tree = repository.revision_tree(_mod_revision.NULL_REVISION)
 
2742
        preview = TransformPreview(tree)
 
2743
        self.addCleanup(preview.finalize)
 
2744
        return preview
 
2745
 
 
2746
    def test_transform_preview(self):
 
2747
        revision_tree = self.create_tree()
 
2748
        preview = TransformPreview(revision_tree)
 
2749
        self.addCleanup(preview.finalize)
 
2750
 
 
2751
    def test_transform_preview_tree(self):
 
2752
        revision_tree = self.create_tree()
 
2753
        preview = TransformPreview(revision_tree)
 
2754
        self.addCleanup(preview.finalize)
 
2755
        preview.get_preview_tree()
 
2756
 
 
2757
    def test_transform_new_file(self):
 
2758
        revision_tree = self.create_tree()
 
2759
        preview = TransformPreview(revision_tree)
 
2760
        self.addCleanup(preview.finalize)
 
2761
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
2762
        preview_tree = preview.get_preview_tree()
 
2763
        self.assertEqual(preview_tree.kind('file2-id'), 'file')
 
2764
        self.assertEqual(
 
2765
            preview_tree.get_file('file2-id').read(), 'content B\n')
 
2766
 
 
2767
    def test_diff_preview_tree(self):
 
2768
        revision_tree = self.create_tree()
 
2769
        preview = TransformPreview(revision_tree)
 
2770
        self.addCleanup(preview.finalize)
 
2771
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
2772
        preview_tree = preview.get_preview_tree()
 
2773
        out = StringIO()
 
2774
        show_diff_trees(revision_tree, preview_tree, out)
 
2775
        lines = out.getvalue().splitlines()
 
2776
        self.assertEqual(lines[0], "=== added file 'file2'")
 
2777
        # 3 lines of diff administrivia
 
2778
        self.assertEqual(lines[4], "+content B")
 
2779
 
 
2780
    def test_transform_conflicts(self):
 
2781
        revision_tree = self.create_tree()
 
2782
        preview = TransformPreview(revision_tree)
 
2783
        self.addCleanup(preview.finalize)
 
2784
        preview.new_file('a', preview.root, 'content 2')
 
2785
        resolve_conflicts(preview)
 
2786
        trans_id = preview.trans_id_file_id('a-id')
 
2787
        self.assertEqual('a.moved', preview.final_name(trans_id))
 
2788
 
 
2789
    def get_tree_and_preview_tree(self):
 
2790
        revision_tree = self.create_tree()
 
2791
        preview = TransformPreview(revision_tree)
 
2792
        self.addCleanup(preview.finalize)
 
2793
        a_trans_id = preview.trans_id_file_id('a-id')
 
2794
        preview.delete_contents(a_trans_id)
 
2795
        preview.create_file('b content', a_trans_id)
 
2796
        preview_tree = preview.get_preview_tree()
 
2797
        return revision_tree, preview_tree
 
2798
 
 
2799
    def test_iter_changes(self):
 
2800
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2801
        root = revision_tree.inventory.root.file_id
 
2802
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
 
2803
                          (root, root), ('a', 'a'), ('file', 'file'),
 
2804
                          (False, False))],
 
2805
                          list(preview_tree.iter_changes(revision_tree)))
 
2806
 
 
2807
    def test_include_unchanged_succeeds(self):
 
2808
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2809
        changes = preview_tree.iter_changes(revision_tree,
 
2810
                                            include_unchanged=True)
 
2811
        root = revision_tree.inventory.root.file_id
 
2812
 
 
2813
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
2814
 
 
2815
    def test_specific_files(self):
 
2816
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2817
        changes = preview_tree.iter_changes(revision_tree,
 
2818
                                            specific_files=[''])
 
2819
        self.assertEqual([A_ENTRY], list(changes))
 
2820
 
 
2821
    def test_want_unversioned(self):
 
2822
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2823
        changes = preview_tree.iter_changes(revision_tree,
 
2824
                                            want_unversioned=True)
 
2825
        self.assertEqual([A_ENTRY], list(changes))
 
2826
 
 
2827
    def test_ignore_extra_trees_no_specific_files(self):
 
2828
        # extra_trees is harmless without specific_files, so we'll silently
 
2829
        # accept it, even though we won't use it.
 
2830
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2831
        preview_tree.iter_changes(revision_tree, extra_trees=[preview_tree])
 
2832
 
 
2833
    def test_ignore_require_versioned_no_specific_files(self):
 
2834
        # require_versioned is meaningless without specific_files.
 
2835
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2836
        preview_tree.iter_changes(revision_tree, require_versioned=False)
 
2837
 
 
2838
    def test_ignore_pb(self):
 
2839
        # pb could be supported, but TT.iter_changes doesn't support it.
 
2840
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2841
        preview_tree.iter_changes(revision_tree)
 
2842
 
 
2843
    def test_kind(self):
 
2844
        revision_tree = self.create_tree()
 
2845
        preview = TransformPreview(revision_tree)
 
2846
        self.addCleanup(preview.finalize)
 
2847
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
2848
        preview.new_directory('directory', preview.root, 'dir-id')
 
2849
        preview_tree = preview.get_preview_tree()
 
2850
        self.assertEqual('file', preview_tree.kind('file-id'))
 
2851
        self.assertEqual('directory', preview_tree.kind('dir-id'))
 
2852
 
 
2853
    def test_get_file_mtime(self):
 
2854
        preview = self.get_empty_preview()
 
2855
        file_trans_id = preview.new_file('file', preview.root, 'contents',
 
2856
                                         'file-id')
 
2857
        limbo_path = preview._limbo_name(file_trans_id)
 
2858
        preview_tree = preview.get_preview_tree()
 
2859
        self.assertEqual(os.stat(limbo_path).st_mtime,
 
2860
                         preview_tree.get_file_mtime('file-id'))
 
2861
 
 
2862
    def test_get_file_mtime_renamed(self):
 
2863
        work_tree = self.make_branch_and_tree('tree')
 
2864
        self.build_tree(['tree/file'])
 
2865
        work_tree.add('file', 'file-id')
 
2866
        preview = TransformPreview(work_tree)
 
2867
        self.addCleanup(preview.finalize)
 
2868
        file_trans_id = preview.trans_id_tree_file_id('file-id')
 
2869
        preview.adjust_path('renamed', preview.root, file_trans_id)
 
2870
        preview_tree = preview.get_preview_tree()
 
2871
        preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
 
2872
        work_mtime = work_tree.get_file_mtime('file-id', 'file')
 
2873
 
 
2874
    def test_get_file_size(self):
 
2875
        work_tree = self.make_branch_and_tree('tree')
 
2876
        self.build_tree_contents([('tree/old', 'old')])
 
2877
        work_tree.add('old', 'old-id')
 
2878
        preview = TransformPreview(work_tree)
 
2879
        self.addCleanup(preview.finalize)
 
2880
        new_id = preview.new_file('name', preview.root, 'contents', 'new-id',
 
2881
                                  'executable')
 
2882
        tree = preview.get_preview_tree()
 
2883
        self.assertEqual(len('old'), tree.get_file_size('old-id'))
 
2884
        self.assertEqual(len('contents'), tree.get_file_size('new-id'))
 
2885
 
 
2886
    def test_get_file(self):
 
2887
        preview = self.get_empty_preview()
 
2888
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
2889
        preview_tree = preview.get_preview_tree()
 
2890
        tree_file = preview_tree.get_file('file-id')
 
2891
        try:
 
2892
            self.assertEqual('contents', tree_file.read())
 
2893
        finally:
 
2894
            tree_file.close()
 
2895
 
 
2896
    def test_get_symlink_target(self):
 
2897
        self.requireFeature(SymlinkFeature)
 
2898
        preview = self.get_empty_preview()
 
2899
        preview.new_symlink('symlink', preview.root, 'target', 'symlink-id')
 
2900
        preview_tree = preview.get_preview_tree()
 
2901
        self.assertEqual('target',
 
2902
                         preview_tree.get_symlink_target('symlink-id'))
 
2903
 
 
2904
    def test_all_file_ids(self):
 
2905
        tree = self.make_branch_and_tree('tree')
 
2906
        self.build_tree(['tree/a', 'tree/b', 'tree/c'])
 
2907
        tree.add(['a', 'b', 'c'], ['a-id', 'b-id', 'c-id'])
 
2908
        preview = TransformPreview(tree)
 
2909
        self.addCleanup(preview.finalize)
 
2910
        preview.unversion_file(preview.trans_id_file_id('b-id'))
 
2911
        c_trans_id = preview.trans_id_file_id('c-id')
 
2912
        preview.unversion_file(c_trans_id)
 
2913
        preview.version_file('c-id', c_trans_id)
 
2914
        preview_tree = preview.get_preview_tree()
 
2915
        self.assertEqual(set(['a-id', 'c-id', tree.get_root_id()]),
 
2916
                         preview_tree.all_file_ids())
 
2917
 
 
2918
    def test_path2id_deleted_unchanged(self):
 
2919
        tree = self.make_branch_and_tree('tree')
 
2920
        self.build_tree(['tree/unchanged', 'tree/deleted'])
 
2921
        tree.add(['unchanged', 'deleted'], ['unchanged-id', 'deleted-id'])
 
2922
        preview = TransformPreview(tree)
 
2923
        self.addCleanup(preview.finalize)
 
2924
        preview.unversion_file(preview.trans_id_file_id('deleted-id'))
 
2925
        preview_tree = preview.get_preview_tree()
 
2926
        self.assertEqual('unchanged-id', preview_tree.path2id('unchanged'))
 
2927
        self.assertIs(None, preview_tree.path2id('deleted'))
 
2928
 
 
2929
    def test_path2id_created(self):
 
2930
        tree = self.make_branch_and_tree('tree')
 
2931
        self.build_tree(['tree/unchanged'])
 
2932
        tree.add(['unchanged'], ['unchanged-id'])
 
2933
        preview = TransformPreview(tree)
 
2934
        self.addCleanup(preview.finalize)
 
2935
        preview.new_file('new', preview.trans_id_file_id('unchanged-id'),
 
2936
            'contents', 'new-id')
 
2937
        preview_tree = preview.get_preview_tree()
 
2938
        self.assertEqual('new-id', preview_tree.path2id('unchanged/new'))
 
2939
 
 
2940
    def test_path2id_moved(self):
 
2941
        tree = self.make_branch_and_tree('tree')
 
2942
        self.build_tree(['tree/old_parent/', 'tree/old_parent/child'])
 
2943
        tree.add(['old_parent', 'old_parent/child'],
 
2944
                 ['old_parent-id', 'child-id'])
 
2945
        preview = TransformPreview(tree)
 
2946
        self.addCleanup(preview.finalize)
 
2947
        new_parent = preview.new_directory('new_parent', preview.root,
 
2948
                                           'new_parent-id')
 
2949
        preview.adjust_path('child', new_parent,
 
2950
                            preview.trans_id_file_id('child-id'))
 
2951
        preview_tree = preview.get_preview_tree()
 
2952
        self.assertIs(None, preview_tree.path2id('old_parent/child'))
 
2953
        self.assertEqual('child-id', preview_tree.path2id('new_parent/child'))
 
2954
 
 
2955
    def test_path2id_renamed_parent(self):
 
2956
        tree = self.make_branch_and_tree('tree')
 
2957
        self.build_tree(['tree/old_name/', 'tree/old_name/child'])
 
2958
        tree.add(['old_name', 'old_name/child'],
 
2959
                 ['parent-id', 'child-id'])
 
2960
        preview = TransformPreview(tree)
 
2961
        self.addCleanup(preview.finalize)
 
2962
        preview.adjust_path('new_name', preview.root,
 
2963
                            preview.trans_id_file_id('parent-id'))
 
2964
        preview_tree = preview.get_preview_tree()
 
2965
        self.assertIs(None, preview_tree.path2id('old_name/child'))
 
2966
        self.assertEqual('child-id', preview_tree.path2id('new_name/child'))
 
2967
 
 
2968
    def assertMatchingIterEntries(self, tt, specific_file_ids=None):
 
2969
        preview_tree = tt.get_preview_tree()
 
2970
        preview_result = list(preview_tree.iter_entries_by_dir(
 
2971
                              specific_file_ids))
 
2972
        tree = tt._tree
 
2973
        tt.apply()
 
2974
        actual_result = list(tree.iter_entries_by_dir(specific_file_ids))
 
2975
        self.assertEqual(actual_result, preview_result)
 
2976
 
 
2977
    def test_iter_entries_by_dir_new(self):
 
2978
        tree = self.make_branch_and_tree('tree')
 
2979
        tt = TreeTransform(tree)
 
2980
        tt.new_file('new', tt.root, 'contents', 'new-id')
 
2981
        self.assertMatchingIterEntries(tt)
 
2982
 
 
2983
    def test_iter_entries_by_dir_deleted(self):
 
2984
        tree = self.make_branch_and_tree('tree')
 
2985
        self.build_tree(['tree/deleted'])
 
2986
        tree.add('deleted', 'deleted-id')
 
2987
        tt = TreeTransform(tree)
 
2988
        tt.delete_contents(tt.trans_id_file_id('deleted-id'))
 
2989
        self.assertMatchingIterEntries(tt)
 
2990
 
 
2991
    def test_iter_entries_by_dir_unversioned(self):
 
2992
        tree = self.make_branch_and_tree('tree')
 
2993
        self.build_tree(['tree/removed'])
 
2994
        tree.add('removed', 'removed-id')
 
2995
        tt = TreeTransform(tree)
 
2996
        tt.unversion_file(tt.trans_id_file_id('removed-id'))
 
2997
        self.assertMatchingIterEntries(tt)
 
2998
 
 
2999
    def test_iter_entries_by_dir_moved(self):
 
3000
        tree = self.make_branch_and_tree('tree')
 
3001
        self.build_tree(['tree/moved', 'tree/new_parent/'])
 
3002
        tree.add(['moved', 'new_parent'], ['moved-id', 'new_parent-id'])
 
3003
        tt = TreeTransform(tree)
 
3004
        tt.adjust_path('moved', tt.trans_id_file_id('new_parent-id'),
 
3005
                       tt.trans_id_file_id('moved-id'))
 
3006
        self.assertMatchingIterEntries(tt)
 
3007
 
 
3008
    def test_iter_entries_by_dir_specific_file_ids(self):
 
3009
        tree = self.make_branch_and_tree('tree')
 
3010
        tree.set_root_id('tree-root-id')
 
3011
        self.build_tree(['tree/parent/', 'tree/parent/child'])
 
3012
        tree.add(['parent', 'parent/child'], ['parent-id', 'child-id'])
 
3013
        tt = TreeTransform(tree)
 
3014
        self.assertMatchingIterEntries(tt, ['tree-root-id', 'child-id'])
 
3015
 
 
3016
    def test_symlink_content_summary(self):
 
3017
        self.requireFeature(SymlinkFeature)
 
3018
        preview = self.get_empty_preview()
 
3019
        preview.new_symlink('path', preview.root, 'target', 'path-id')
 
3020
        summary = preview.get_preview_tree().path_content_summary('path')
 
3021
        self.assertEqual(('symlink', None, None, 'target'), summary)
 
3022
 
 
3023
    def test_missing_content_summary(self):
 
3024
        preview = self.get_empty_preview()
 
3025
        summary = preview.get_preview_tree().path_content_summary('path')
 
3026
        self.assertEqual(('missing', None, None, None), summary)
 
3027
 
 
3028
    def test_deleted_content_summary(self):
 
3029
        tree = self.make_branch_and_tree('tree')
 
3030
        self.build_tree(['tree/path/'])
 
3031
        tree.add('path')
 
3032
        preview = TransformPreview(tree)
 
3033
        self.addCleanup(preview.finalize)
 
3034
        preview.delete_contents(preview.trans_id_tree_path('path'))
 
3035
        summary = preview.get_preview_tree().path_content_summary('path')
 
3036
        self.assertEqual(('missing', None, None, None), summary)
 
3037
 
 
3038
    def test_file_content_summary_executable(self):
 
3039
        preview = self.get_empty_preview()
 
3040
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
 
3041
        preview.set_executability(True, path_id)
 
3042
        summary = preview.get_preview_tree().path_content_summary('path')
 
3043
        self.assertEqual(4, len(summary))
 
3044
        self.assertEqual('file', summary[0])
 
3045
        # size must be known
 
3046
        self.assertEqual(len('contents'), summary[1])
 
3047
        # executable
 
3048
        self.assertEqual(True, summary[2])
 
3049
        # will not have hash (not cheap to determine)
 
3050
        self.assertIs(None, summary[3])
 
3051
 
 
3052
    def test_change_executability(self):
 
3053
        tree = self.make_branch_and_tree('tree')
 
3054
        self.build_tree(['tree/path'])
 
3055
        tree.add('path')
 
3056
        preview = TransformPreview(tree)
 
3057
        self.addCleanup(preview.finalize)
 
3058
        path_id = preview.trans_id_tree_path('path')
 
3059
        preview.set_executability(True, path_id)
 
3060
        summary = preview.get_preview_tree().path_content_summary('path')
 
3061
        self.assertEqual(True, summary[2])
 
3062
 
 
3063
    def test_file_content_summary_non_exec(self):
 
3064
        preview = self.get_empty_preview()
 
3065
        preview.new_file('path', preview.root, 'contents', 'path-id')
 
3066
        summary = preview.get_preview_tree().path_content_summary('path')
 
3067
        self.assertEqual(4, len(summary))
 
3068
        self.assertEqual('file', summary[0])
 
3069
        # size must be known
 
3070
        self.assertEqual(len('contents'), summary[1])
 
3071
        # not executable
 
3072
        self.assertEqual(False, summary[2])
 
3073
        # will not have hash (not cheap to determine)
 
3074
        self.assertIs(None, summary[3])
 
3075
 
 
3076
    def test_dir_content_summary(self):
 
3077
        preview = self.get_empty_preview()
 
3078
        preview.new_directory('path', preview.root, 'path-id')
 
3079
        summary = preview.get_preview_tree().path_content_summary('path')
 
3080
        self.assertEqual(('directory', None, None, None), summary)
 
3081
 
 
3082
    def test_tree_content_summary(self):
 
3083
        preview = self.get_empty_preview()
 
3084
        path = preview.new_directory('path', preview.root, 'path-id')
 
3085
        preview.set_tree_reference('rev-1', path)
 
3086
        summary = preview.get_preview_tree().path_content_summary('path')
 
3087
        self.assertEqual(4, len(summary))
 
3088
        self.assertEqual('tree-reference', summary[0])
 
3089
 
 
3090
    def test_annotate(self):
 
3091
        tree = self.make_branch_and_tree('tree')
 
3092
        self.build_tree_contents([('tree/file', 'a\n')])
 
3093
        tree.add('file', 'file-id')
 
3094
        tree.commit('a', rev_id='one')
 
3095
        self.build_tree_contents([('tree/file', 'a\nb\n')])
 
3096
        preview = TransformPreview(tree)
 
3097
        self.addCleanup(preview.finalize)
 
3098
        file_trans_id = preview.trans_id_file_id('file-id')
 
3099
        preview.delete_contents(file_trans_id)
 
3100
        preview.create_file('a\nb\nc\n', file_trans_id)
 
3101
        preview_tree = preview.get_preview_tree()
 
3102
        expected = [
 
3103
            ('one', 'a\n'),
 
3104
            ('me:', 'b\n'),
 
3105
            ('me:', 'c\n'),
 
3106
        ]
 
3107
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
3108
        self.assertEqual(expected, annotation)
 
3109
 
 
3110
    def test_annotate_missing(self):
 
3111
        preview = self.get_empty_preview()
 
3112
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
 
3113
        preview_tree = preview.get_preview_tree()
 
3114
        expected = [
 
3115
            ('me:', 'a\n'),
 
3116
            ('me:', 'b\n'),
 
3117
            ('me:', 'c\n'),
 
3118
         ]
 
3119
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
3120
        self.assertEqual(expected, annotation)
 
3121
 
 
3122
    def test_annotate_rename(self):
 
3123
        tree = self.make_branch_and_tree('tree')
 
3124
        self.build_tree_contents([('tree/file', 'a\n')])
 
3125
        tree.add('file', 'file-id')
 
3126
        tree.commit('a', rev_id='one')
 
3127
        preview = TransformPreview(tree)
 
3128
        self.addCleanup(preview.finalize)
 
3129
        file_trans_id = preview.trans_id_file_id('file-id')
 
3130
        preview.adjust_path('newname', preview.root, file_trans_id)
 
3131
        preview_tree = preview.get_preview_tree()
 
3132
        expected = [
 
3133
            ('one', 'a\n'),
 
3134
        ]
 
3135
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
3136
        self.assertEqual(expected, annotation)
 
3137
 
 
3138
    def test_annotate_deleted(self):
 
3139
        tree = self.make_branch_and_tree('tree')
 
3140
        self.build_tree_contents([('tree/file', 'a\n')])
 
3141
        tree.add('file', 'file-id')
 
3142
        tree.commit('a', rev_id='one')
 
3143
        self.build_tree_contents([('tree/file', 'a\nb\n')])
 
3144
        preview = TransformPreview(tree)
 
3145
        self.addCleanup(preview.finalize)
 
3146
        file_trans_id = preview.trans_id_file_id('file-id')
 
3147
        preview.delete_contents(file_trans_id)
 
3148
        preview_tree = preview.get_preview_tree()
 
3149
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
3150
        self.assertIs(None, annotation)
 
3151
 
 
3152
    def test_stored_kind(self):
 
3153
        preview = self.get_empty_preview()
 
3154
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
 
3155
        preview_tree = preview.get_preview_tree()
 
3156
        self.assertEqual('file', preview_tree.stored_kind('file-id'))
 
3157
 
 
3158
    def test_is_executable(self):
 
3159
        preview = self.get_empty_preview()
 
3160
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
 
3161
        preview.set_executability(True, preview.trans_id_file_id('file-id'))
 
3162
        preview_tree = preview.get_preview_tree()
 
3163
        self.assertEqual(True, preview_tree.is_executable('file-id'))
 
3164
 
 
3165
    def test_get_set_parent_ids(self):
 
3166
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
3167
        self.assertEqual([], preview_tree.get_parent_ids())
 
3168
        preview_tree.set_parent_ids(['rev-1'])
 
3169
        self.assertEqual(['rev-1'], preview_tree.get_parent_ids())
 
3170
 
 
3171
    def test_plan_file_merge(self):
 
3172
        work_a = self.make_branch_and_tree('wta')
 
3173
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
 
3174
        work_a.add('file', 'file-id')
 
3175
        base_id = work_a.commit('base version')
 
3176
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
 
3177
        preview = TransformPreview(work_a)
 
3178
        self.addCleanup(preview.finalize)
 
3179
        trans_id = preview.trans_id_file_id('file-id')
 
3180
        preview.delete_contents(trans_id)
 
3181
        preview.create_file('b\nc\nd\ne\n', trans_id)
 
3182
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
 
3183
        tree_a = preview.get_preview_tree()
 
3184
        tree_a.set_parent_ids([base_id])
 
3185
        self.assertEqual([
 
3186
            ('killed-a', 'a\n'),
 
3187
            ('killed-b', 'b\n'),
 
3188
            ('unchanged', 'c\n'),
 
3189
            ('unchanged', 'd\n'),
 
3190
            ('new-a', 'e\n'),
 
3191
            ('new-b', 'f\n'),
 
3192
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
 
3193
 
 
3194
    def test_plan_file_merge_revision_tree(self):
 
3195
        work_a = self.make_branch_and_tree('wta')
 
3196
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
 
3197
        work_a.add('file', 'file-id')
 
3198
        base_id = work_a.commit('base version')
 
3199
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
 
3200
        preview = TransformPreview(work_a.basis_tree())
 
3201
        self.addCleanup(preview.finalize)
 
3202
        trans_id = preview.trans_id_file_id('file-id')
 
3203
        preview.delete_contents(trans_id)
 
3204
        preview.create_file('b\nc\nd\ne\n', trans_id)
 
3205
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
 
3206
        tree_a = preview.get_preview_tree()
 
3207
        tree_a.set_parent_ids([base_id])
 
3208
        self.assertEqual([
 
3209
            ('killed-a', 'a\n'),
 
3210
            ('killed-b', 'b\n'),
 
3211
            ('unchanged', 'c\n'),
 
3212
            ('unchanged', 'd\n'),
 
3213
            ('new-a', 'e\n'),
 
3214
            ('new-b', 'f\n'),
 
3215
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
 
3216
 
 
3217
    def test_walkdirs(self):
 
3218
        preview = self.get_empty_preview()
 
3219
        root = preview.new_directory('', ROOT_PARENT, 'tree-root')
 
3220
        # FIXME: new_directory should mark root.
 
3221
        preview.fixup_new_roots()
 
3222
        preview_tree = preview.get_preview_tree()
 
3223
        file_trans_id = preview.new_file('a', preview.root, 'contents',
 
3224
                                         'a-id')
 
3225
        expected = [(('', 'tree-root'),
 
3226
                    [('a', 'a', 'file', None, 'a-id', 'file')])]
 
3227
        self.assertEqual(expected, list(preview_tree.walkdirs()))
 
3228
 
 
3229
    def test_extras(self):
 
3230
        work_tree = self.make_branch_and_tree('tree')
 
3231
        self.build_tree(['tree/removed-file', 'tree/existing-file',
 
3232
                         'tree/not-removed-file'])
 
3233
        work_tree.add(['removed-file', 'not-removed-file'])
 
3234
        preview = TransformPreview(work_tree)
 
3235
        self.addCleanup(preview.finalize)
 
3236
        preview.new_file('new-file', preview.root, 'contents')
 
3237
        preview.new_file('new-versioned-file', preview.root, 'contents',
 
3238
                         'new-versioned-id')
 
3239
        tree = preview.get_preview_tree()
 
3240
        preview.unversion_file(preview.trans_id_tree_path('removed-file'))
 
3241
        self.assertEqual(set(['new-file', 'removed-file', 'existing-file']),
 
3242
                         set(tree.extras()))
 
3243
 
 
3244
    def test_merge_into_preview(self):
 
3245
        work_tree = self.make_branch_and_tree('tree')
 
3246
        self.build_tree_contents([('tree/file','b\n')])
 
3247
        work_tree.add('file', 'file-id')
 
3248
        work_tree.commit('first commit')
 
3249
        child_tree = work_tree.bzrdir.sprout('child').open_workingtree()
 
3250
        self.build_tree_contents([('child/file','b\nc\n')])
 
3251
        child_tree.commit('child commit')
 
3252
        child_tree.lock_write()
 
3253
        self.addCleanup(child_tree.unlock)
 
3254
        work_tree.lock_write()
 
3255
        self.addCleanup(work_tree.unlock)
 
3256
        preview = TransformPreview(work_tree)
 
3257
        self.addCleanup(preview.finalize)
 
3258
        file_trans_id = preview.trans_id_file_id('file-id')
 
3259
        preview.delete_contents(file_trans_id)
 
3260
        preview.create_file('a\nb\n', file_trans_id)
 
3261
        preview_tree = preview.get_preview_tree()
 
3262
        merger = Merger.from_revision_ids(None, preview_tree,
 
3263
                                          child_tree.branch.last_revision(),
 
3264
                                          other_branch=child_tree.branch,
 
3265
                                          tree_branch=work_tree.branch)
 
3266
        merger.merge_type = Merge3Merger
 
3267
        tt = merger.make_merger().make_preview_transform()
 
3268
        self.addCleanup(tt.finalize)
 
3269
        final_tree = tt.get_preview_tree()
 
3270
        self.assertEqual('a\nb\nc\n', final_tree.get_file_text('file-id'))
 
3271
 
 
3272
    def test_merge_preview_into_workingtree(self):
 
3273
        tree = self.make_branch_and_tree('tree')
 
3274
        tree.set_root_id('TREE_ROOT')
 
3275
        tt = TransformPreview(tree)
 
3276
        self.addCleanup(tt.finalize)
 
3277
        tt.new_file('name', tt.root, 'content', 'file-id')
 
3278
        tree2 = self.make_branch_and_tree('tree2')
 
3279
        tree2.set_root_id('TREE_ROOT')
 
3280
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
 
3281
                                         None, tree.basis_tree())
 
3282
        merger.merge_type = Merge3Merger
 
3283
        merger.do_merge()
 
3284
 
 
3285
    def test_merge_preview_into_workingtree_handles_conflicts(self):
 
3286
        tree = self.make_branch_and_tree('tree')
 
3287
        self.build_tree_contents([('tree/foo', 'bar')])
 
3288
        tree.add('foo', 'foo-id')
 
3289
        tree.commit('foo')
 
3290
        tt = TransformPreview(tree)
 
3291
        self.addCleanup(tt.finalize)
 
3292
        trans_id = tt.trans_id_file_id('foo-id')
 
3293
        tt.delete_contents(trans_id)
 
3294
        tt.create_file('baz', trans_id)
 
3295
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
 
3296
        self.build_tree_contents([('tree2/foo', 'qux')])
 
3297
        pb = None
 
3298
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
 
3299
                                         pb, tree.basis_tree())
 
3300
        merger.merge_type = Merge3Merger
 
3301
        merger.do_merge()
 
3302
 
 
3303
    def test_has_filename(self):
 
3304
        wt = self.make_branch_and_tree('tree')
 
3305
        self.build_tree(['tree/unmodified', 'tree/removed', 'tree/modified'])
 
3306
        tt = TransformPreview(wt)
 
3307
        removed_id = tt.trans_id_tree_path('removed')
 
3308
        tt.delete_contents(removed_id)
 
3309
        tt.new_file('new', tt.root, 'contents')
 
3310
        modified_id = tt.trans_id_tree_path('modified')
 
3311
        tt.delete_contents(modified_id)
 
3312
        tt.create_file('modified-contents', modified_id)
 
3313
        self.addCleanup(tt.finalize)
 
3314
        tree = tt.get_preview_tree()
 
3315
        self.assertTrue(tree.has_filename('unmodified'))
 
3316
        self.assertFalse(tree.has_filename('not-present'))
 
3317
        self.assertFalse(tree.has_filename('removed'))
 
3318
        self.assertTrue(tree.has_filename('new'))
 
3319
        self.assertTrue(tree.has_filename('modified'))
 
3320
 
 
3321
    def test_is_executable(self):
 
3322
        tree = self.make_branch_and_tree('tree')
 
3323
        preview = TransformPreview(tree)
 
3324
        self.addCleanup(preview.finalize)
 
3325
        preview.new_file('foo', preview.root, 'bar', 'baz-id')
 
3326
        preview_tree = preview.get_preview_tree()
 
3327
        self.assertEqual(False, preview_tree.is_executable('baz-id',
 
3328
                                                           'tree/foo'))
 
3329
        self.assertEqual(False, preview_tree.is_executable('baz-id'))
 
3330
 
 
3331
    def test_commit_preview_tree(self):
 
3332
        tree = self.make_branch_and_tree('tree')
 
3333
        rev_id = tree.commit('rev1')
 
3334
        tree.branch.lock_write()
 
3335
        self.addCleanup(tree.branch.unlock)
 
3336
        tt = TransformPreview(tree)
 
3337
        tt.new_file('file', tt.root, 'contents', 'file_id')
 
3338
        self.addCleanup(tt.finalize)
 
3339
        preview = tt.get_preview_tree()
 
3340
        preview.set_parent_ids([rev_id])
 
3341
        builder = tree.branch.get_commit_builder([rev_id])
 
3342
        list(builder.record_iter_changes(preview, rev_id, tt.iter_changes()))
 
3343
        builder.finish_inventory()
 
3344
        rev2_id = builder.commit('rev2')
 
3345
        rev2_tree = tree.branch.repository.revision_tree(rev2_id)
 
3346
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
 
3347
 
 
3348
    def test_ascii_limbo_paths(self):
 
3349
        self.requireFeature(features.UnicodeFilenameFeature)
 
3350
        branch = self.make_branch('any')
 
3351
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
 
3352
        tt = TransformPreview(tree)
 
3353
        self.addCleanup(tt.finalize)
 
3354
        foo_id = tt.new_directory('', ROOT_PARENT)
 
3355
        bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
 
3356
        limbo_path = tt._limbo_name(bar_id)
 
3357
        self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
 
3358
 
 
3359
 
 
3360
class FakeSerializer(object):
 
3361
    """Serializer implementation that simply returns the input.
 
3362
 
 
3363
    The input is returned in the order used by pack.ContainerPushParser.
 
3364
    """
 
3365
    @staticmethod
 
3366
    def bytes_record(bytes, names):
 
3367
        return names, bytes
 
3368
 
 
3369
 
 
3370
class TestSerializeTransform(tests.TestCaseWithTransport):
 
3371
 
 
3372
    _test_needs_features = [features.UnicodeFilenameFeature]
 
3373
 
 
3374
    def get_preview(self, tree=None):
 
3375
        if tree is None:
 
3376
            tree = self.make_branch_and_tree('tree')
 
3377
        tt = TransformPreview(tree)
 
3378
        self.addCleanup(tt.finalize)
 
3379
        return tt
 
3380
 
 
3381
    def assertSerializesTo(self, expected, tt):
 
3382
        records = list(tt.serialize(FakeSerializer()))
 
3383
        self.assertEqual(expected, records)
 
3384
 
 
3385
    @staticmethod
 
3386
    def default_attribs():
 
3387
        return {
 
3388
            '_id_number': 1,
 
3389
            '_new_name': {},
 
3390
            '_new_parent': {},
 
3391
            '_new_executability': {},
 
3392
            '_new_id': {},
 
3393
            '_tree_path_ids': {'': 'new-0'},
 
3394
            '_removed_id': [],
 
3395
            '_removed_contents': [],
 
3396
            '_non_present_ids': {},
 
3397
            }
 
3398
 
 
3399
    def make_records(self, attribs, contents):
 
3400
        records = [
 
3401
            (((('attribs'),),), bencode.bencode(attribs))]
 
3402
        records.extend([(((n, k),), c) for n, k, c in contents])
 
3403
        return records
 
3404
 
 
3405
    def creation_records(self):
 
3406
        attribs = self.default_attribs()
 
3407
        attribs['_id_number'] = 3
 
3408
        attribs['_new_name'] = {
 
3409
            'new-1': u'foo\u1234'.encode('utf-8'), 'new-2': 'qux'}
 
3410
        attribs['_new_id'] = {'new-1': 'baz', 'new-2': 'quxx'}
 
3411
        attribs['_new_parent'] = {'new-1': 'new-0', 'new-2': 'new-0'}
 
3412
        attribs['_new_executability'] = {'new-1': 1}
 
3413
        contents = [
 
3414
            ('new-1', 'file', 'i 1\nbar\n'),
 
3415
            ('new-2', 'directory', ''),
 
3416
            ]
 
3417
        return self.make_records(attribs, contents)
 
3418
 
 
3419
    def test_serialize_creation(self):
 
3420
        tt = self.get_preview()
 
3421
        tt.new_file(u'foo\u1234', tt.root, 'bar', 'baz', True)
 
3422
        tt.new_directory('qux', tt.root, 'quxx')
 
3423
        self.assertSerializesTo(self.creation_records(), tt)
 
3424
 
 
3425
    def test_deserialize_creation(self):
 
3426
        tt = self.get_preview()
 
3427
        tt.deserialize(iter(self.creation_records()))
 
3428
        self.assertEqual(3, tt._id_number)
 
3429
        self.assertEqual({'new-1': u'foo\u1234',
 
3430
                          'new-2': 'qux'}, tt._new_name)
 
3431
        self.assertEqual({'new-1': 'baz', 'new-2': 'quxx'}, tt._new_id)
 
3432
        self.assertEqual({'new-1': tt.root, 'new-2': tt.root}, tt._new_parent)
 
3433
        self.assertEqual({'baz': 'new-1', 'quxx': 'new-2'}, tt._r_new_id)
 
3434
        self.assertEqual({'new-1': True}, tt._new_executability)
 
3435
        self.assertEqual({'new-1': 'file',
 
3436
                          'new-2': 'directory'}, tt._new_contents)
 
3437
        foo_limbo = open(tt._limbo_name('new-1'), 'rb')
 
3438
        try:
 
3439
            foo_content = foo_limbo.read()
 
3440
        finally:
 
3441
            foo_limbo.close()
 
3442
        self.assertEqual('bar', foo_content)
 
3443
 
 
3444
    def symlink_creation_records(self):
 
3445
        attribs = self.default_attribs()
 
3446
        attribs['_id_number'] = 2
 
3447
        attribs['_new_name'] = {'new-1': u'foo\u1234'.encode('utf-8')}
 
3448
        attribs['_new_parent'] = {'new-1': 'new-0'}
 
3449
        contents = [('new-1', 'symlink', u'bar\u1234'.encode('utf-8'))]
 
3450
        return self.make_records(attribs, contents)
 
3451
 
 
3452
    def test_serialize_symlink_creation(self):
 
3453
        self.requireFeature(features.SymlinkFeature)
 
3454
        tt = self.get_preview()
 
3455
        tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
 
3456
        self.assertSerializesTo(self.symlink_creation_records(), tt)
 
3457
 
 
3458
    def test_deserialize_symlink_creation(self):
 
3459
        self.requireFeature(features.SymlinkFeature)
 
3460
        tt = self.get_preview()
 
3461
        tt.deserialize(iter(self.symlink_creation_records()))
 
3462
        abspath = tt._limbo_name('new-1')
 
3463
        foo_content = osutils.readlink(abspath)
 
3464
        self.assertEqual(u'bar\u1234', foo_content)
 
3465
 
 
3466
    def make_destruction_preview(self):
 
3467
        tree = self.make_branch_and_tree('.')
 
3468
        self.build_tree([u'foo\u1234', 'bar'])
 
3469
        tree.add([u'foo\u1234', 'bar'], ['foo-id', 'bar-id'])
 
3470
        return self.get_preview(tree)
 
3471
 
 
3472
    def destruction_records(self):
 
3473
        attribs = self.default_attribs()
 
3474
        attribs['_id_number'] = 3
 
3475
        attribs['_removed_id'] = ['new-1']
 
3476
        attribs['_removed_contents'] = ['new-2']
 
3477
        attribs['_tree_path_ids'] = {
 
3478
            '': 'new-0',
 
3479
            u'foo\u1234'.encode('utf-8'): 'new-1',
 
3480
            'bar': 'new-2',
 
3481
            }
 
3482
        return self.make_records(attribs, [])
 
3483
 
 
3484
    def test_serialize_destruction(self):
 
3485
        tt = self.make_destruction_preview()
 
3486
        foo_trans_id = tt.trans_id_tree_file_id('foo-id')
 
3487
        tt.unversion_file(foo_trans_id)
 
3488
        bar_trans_id = tt.trans_id_tree_file_id('bar-id')
 
3489
        tt.delete_contents(bar_trans_id)
 
3490
        self.assertSerializesTo(self.destruction_records(), tt)
 
3491
 
 
3492
    def test_deserialize_destruction(self):
 
3493
        tt = self.make_destruction_preview()
 
3494
        tt.deserialize(iter(self.destruction_records()))
 
3495
        self.assertEqual({u'foo\u1234': 'new-1',
 
3496
                          'bar': 'new-2',
 
3497
                          '': tt.root}, tt._tree_path_ids)
 
3498
        self.assertEqual({'new-1': u'foo\u1234',
 
3499
                          'new-2': 'bar',
 
3500
                          tt.root: ''}, tt._tree_id_paths)
 
3501
        self.assertEqual(set(['new-1']), tt._removed_id)
 
3502
        self.assertEqual(set(['new-2']), tt._removed_contents)
 
3503
 
 
3504
    def missing_records(self):
 
3505
        attribs = self.default_attribs()
 
3506
        attribs['_id_number'] = 2
 
3507
        attribs['_non_present_ids'] = {
 
3508
            'boo': 'new-1',}
 
3509
        return self.make_records(attribs, [])
 
3510
 
 
3511
    def test_serialize_missing(self):
 
3512
        tt = self.get_preview()
 
3513
        boo_trans_id = tt.trans_id_file_id('boo')
 
3514
        self.assertSerializesTo(self.missing_records(), tt)
 
3515
 
 
3516
    def test_deserialize_missing(self):
 
3517
        tt = self.get_preview()
 
3518
        tt.deserialize(iter(self.missing_records()))
 
3519
        self.assertEqual({'boo': 'new-1'}, tt._non_present_ids)
 
3520
 
 
3521
    def make_modification_preview(self):
 
3522
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
 
3523
        LINES_TWO = 'z\nbb\nx\ndd\n'
 
3524
        tree = self.make_branch_and_tree('tree')
 
3525
        self.build_tree_contents([('tree/file', LINES_ONE)])
 
3526
        tree.add('file', 'file-id')
 
3527
        return self.get_preview(tree), LINES_TWO
 
3528
 
 
3529
    def modification_records(self):
 
3530
        attribs = self.default_attribs()
 
3531
        attribs['_id_number'] = 2
 
3532
        attribs['_tree_path_ids'] = {
 
3533
            'file': 'new-1',
 
3534
            '': 'new-0',}
 
3535
        attribs['_removed_contents'] = ['new-1']
 
3536
        contents = [('new-1', 'file',
 
3537
                     'i 1\nz\n\nc 0 1 1 1\ni 1\nx\n\nc 0 3 3 1\n')]
 
3538
        return self.make_records(attribs, contents)
 
3539
 
 
3540
    def test_serialize_modification(self):
 
3541
        tt, LINES = self.make_modification_preview()
 
3542
        trans_id = tt.trans_id_file_id('file-id')
 
3543
        tt.delete_contents(trans_id)
 
3544
        tt.create_file(LINES, trans_id)
 
3545
        self.assertSerializesTo(self.modification_records(), tt)
 
3546
 
 
3547
    def test_deserialize_modification(self):
 
3548
        tt, LINES = self.make_modification_preview()
 
3549
        tt.deserialize(iter(self.modification_records()))
 
3550
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
 
3551
 
 
3552
    def make_kind_change_preview(self):
 
3553
        LINES = 'a\nb\nc\nd\n'
 
3554
        tree = self.make_branch_and_tree('tree')
 
3555
        self.build_tree(['tree/foo/'])
 
3556
        tree.add('foo', 'foo-id')
 
3557
        return self.get_preview(tree), LINES
 
3558
 
 
3559
    def kind_change_records(self):
 
3560
        attribs = self.default_attribs()
 
3561
        attribs['_id_number'] = 2
 
3562
        attribs['_tree_path_ids'] = {
 
3563
            'foo': 'new-1',
 
3564
            '': 'new-0',}
 
3565
        attribs['_removed_contents'] = ['new-1']
 
3566
        contents = [('new-1', 'file',
 
3567
                     'i 4\na\nb\nc\nd\n\n')]
 
3568
        return self.make_records(attribs, contents)
 
3569
 
 
3570
    def test_serialize_kind_change(self):
 
3571
        tt, LINES = self.make_kind_change_preview()
 
3572
        trans_id = tt.trans_id_file_id('foo-id')
 
3573
        tt.delete_contents(trans_id)
 
3574
        tt.create_file(LINES, trans_id)
 
3575
        self.assertSerializesTo(self.kind_change_records(), tt)
 
3576
 
 
3577
    def test_deserialize_kind_change(self):
 
3578
        tt, LINES = self.make_kind_change_preview()
 
3579
        tt.deserialize(iter(self.kind_change_records()))
 
3580
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
 
3581
 
 
3582
    def make_add_contents_preview(self):
 
3583
        LINES = 'a\nb\nc\nd\n'
 
3584
        tree = self.make_branch_and_tree('tree')
 
3585
        self.build_tree(['tree/foo'])
 
3586
        tree.add('foo')
 
3587
        os.unlink('tree/foo')
 
3588
        return self.get_preview(tree), LINES
 
3589
 
 
3590
    def add_contents_records(self):
 
3591
        attribs = self.default_attribs()
 
3592
        attribs['_id_number'] = 2
 
3593
        attribs['_tree_path_ids'] = {
 
3594
            'foo': 'new-1',
 
3595
            '': 'new-0',}
 
3596
        contents = [('new-1', 'file',
 
3597
                     'i 4\na\nb\nc\nd\n\n')]
 
3598
        return self.make_records(attribs, contents)
 
3599
 
 
3600
    def test_serialize_add_contents(self):
 
3601
        tt, LINES = self.make_add_contents_preview()
 
3602
        trans_id = tt.trans_id_tree_path('foo')
 
3603
        tt.create_file(LINES, trans_id)
 
3604
        self.assertSerializesTo(self.add_contents_records(), tt)
 
3605
 
 
3606
    def test_deserialize_add_contents(self):
 
3607
        tt, LINES = self.make_add_contents_preview()
 
3608
        tt.deserialize(iter(self.add_contents_records()))
 
3609
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
 
3610
 
 
3611
    def test_get_parents_lines(self):
 
3612
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
 
3613
        LINES_TWO = 'z\nbb\nx\ndd\n'
 
3614
        tree = self.make_branch_and_tree('tree')
 
3615
        self.build_tree_contents([('tree/file', LINES_ONE)])
 
3616
        tree.add('file', 'file-id')
 
3617
        tt = self.get_preview(tree)
 
3618
        trans_id = tt.trans_id_tree_path('file')
 
3619
        self.assertEqual((['aa\n', 'bb\n', 'cc\n', 'dd\n'],),
 
3620
            tt._get_parents_lines(trans_id))
 
3621
 
 
3622
    def test_get_parents_texts(self):
 
3623
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
 
3624
        LINES_TWO = 'z\nbb\nx\ndd\n'
 
3625
        tree = self.make_branch_and_tree('tree')
 
3626
        self.build_tree_contents([('tree/file', LINES_ONE)])
 
3627
        tree.add('file', 'file-id')
 
3628
        tt = self.get_preview(tree)
 
3629
        trans_id = tt.trans_id_tree_path('file')
 
3630
        self.assertEqual((LINES_ONE,),
 
3631
            tt._get_parents_texts(trans_id))
 
3632
 
 
3633
 
 
3634
class TestOrphan(tests.TestCaseWithTransport):
 
3635
 
 
3636
    def test_no_orphan_for_transform_preview(self):
 
3637
        tree = self.make_branch_and_tree('tree')
 
3638
        tt = transform.TransformPreview(tree)
 
3639
        self.addCleanup(tt.finalize)
 
3640
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
 
3641
 
 
3642
    def _set_orphan_policy(self, wt, policy):
 
3643
        wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
 
3644
                                               policy)
 
3645
 
 
3646
    def _prepare_orphan(self, wt):
 
3647
        self.build_tree(['dir/', 'dir/file', 'dir/foo'])
 
3648
        wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
3649
        wt.commit('add dir and file ignoring foo')
 
3650
        tt = transform.TreeTransform(wt)
 
3651
        self.addCleanup(tt.finalize)
 
3652
        # dir and bar are deleted
 
3653
        dir_tid = tt.trans_id_tree_path('dir')
 
3654
        file_tid = tt.trans_id_tree_path('dir/file')
 
3655
        orphan_tid = tt.trans_id_tree_path('dir/foo')
 
3656
        tt.delete_contents(file_tid)
 
3657
        tt.unversion_file(file_tid)
 
3658
        tt.delete_contents(dir_tid)
 
3659
        tt.unversion_file(dir_tid)
 
3660
        # There should be a conflict because dir still contain foo
 
3661
        raw_conflicts = tt.find_conflicts()
 
3662
        self.assertLength(1, raw_conflicts)
 
3663
        self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
 
3664
        return tt, orphan_tid
 
3665
 
 
3666
    def test_new_orphan_created(self):
 
3667
        wt = self.make_branch_and_tree('.')
 
3668
        self._set_orphan_policy(wt, 'move')
 
3669
        tt, orphan_tid = self._prepare_orphan(wt)
 
3670
        warnings = []
 
3671
        def warning(*args):
 
3672
            warnings.append(args[0] % args[1:])
 
3673
        self.overrideAttr(trace, 'warning', warning)
 
3674
        remaining_conflicts = resolve_conflicts(tt)
 
3675
        self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
 
3676
                          warnings)
 
3677
        # Yeah for resolved conflicts !
 
3678
        self.assertLength(0, remaining_conflicts)
 
3679
        # We have a new orphan
 
3680
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
 
3681
        self.assertEquals('bzr-orphans',
 
3682
                          tt.final_name(tt.final_parent(orphan_tid)))
 
3683
 
 
3684
    def test_never_orphan(self):
 
3685
        wt = self.make_branch_and_tree('.')
 
3686
        self._set_orphan_policy(wt, 'conflict')
 
3687
        tt, orphan_tid = self._prepare_orphan(wt)
 
3688
        remaining_conflicts = resolve_conflicts(tt)
 
3689
        self.assertLength(1, remaining_conflicts)
 
3690
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3691
                         remaining_conflicts.pop())
 
3692
 
 
3693
    def test_orphan_error(self):
 
3694
        def bogus_orphan(tt, orphan_id, parent_id):
 
3695
            raise transform.OrphaningError(tt.final_name(orphan_id),
 
3696
                                           tt.final_name(parent_id))
 
3697
        transform.orphaning_registry.register('bogus', bogus_orphan,
 
3698
                                              'Raise an error when orphaning')
 
3699
        wt = self.make_branch_and_tree('.')
 
3700
        self._set_orphan_policy(wt, 'bogus')
 
3701
        tt, orphan_tid = self._prepare_orphan(wt)
 
3702
        remaining_conflicts = resolve_conflicts(tt)
 
3703
        self.assertLength(1, remaining_conflicts)
 
3704
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3705
                         remaining_conflicts.pop())
 
3706
 
 
3707
    def test_unknown_orphan_policy(self):
 
3708
        wt = self.make_branch_and_tree('.')
 
3709
        # Set a fictional policy nobody ever implemented
 
3710
        self._set_orphan_policy(wt, 'donttouchmypreciouuus')
 
3711
        tt, orphan_tid = self._prepare_orphan(wt)
 
3712
        warnings = []
 
3713
        def warning(*args):
 
3714
            warnings.append(args[0] % args[1:])
 
3715
        self.overrideAttr(trace, 'warning', warning)
 
3716
        remaining_conflicts = resolve_conflicts(tt)
 
3717
        # We fallback to the default policy which create a conflict
 
3718
        self.assertLength(1, remaining_conflicts)
 
3719
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3720
                         remaining_conflicts.pop())
 
3721
        self.assertLength(1, warnings)
 
3722
        self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')