~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Jelmer Vernooij
  • Date: 2008-06-11 18:58:19 UTC
  • mto: (3649.3.2 bzr.dev)
  • mto: This revision was merged to the branch mainline in revision 3658.
  • Revision ID: jelmer@samba.org-20080611185819-o4shi1ranh9zh01e
Move ftp transport into separate directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import os
 
18
import stat
18
19
from StringIO import StringIO
19
20
import sys
20
 
import time
21
21
 
22
22
from bzrlib import (
23
 
    bencode,
24
23
    errors,
25
 
    filters,
26
24
    generate_ids,
27
25
    osutils,
 
26
    progress,
28
27
    revision as _mod_revision,
29
 
    rules,
 
28
    symbol_versioning,
30
29
    tests,
31
30
    urlutils,
32
31
    )
33
32
from bzrlib.bzrdir import BzrDir
34
 
from bzrlib.conflicts import (
35
 
    DeletingParent,
36
 
    DuplicateEntry,
37
 
    DuplicateID,
38
 
    MissingParent,
39
 
    NonDirectoryParent,
40
 
    ParentLoop,
41
 
    UnversionedParent,
42
 
)
 
33
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
 
34
                              UnversionedParent, ParentLoop, DeletingParent,
 
35
                              NonDirectoryParent)
43
36
from bzrlib.diff import show_diff_trees
44
 
from bzrlib.errors import (
45
 
    DuplicateKey,
46
 
    ExistingLimbo,
47
 
    ExistingPendingDeletion,
48
 
    ImmortalLimbo,
49
 
    ImmortalPendingDeletion,
50
 
    LockError,
51
 
    MalformedTransform,
52
 
    NoSuchFile,
53
 
    ReusingTransform,
54
 
)
55
 
from bzrlib.osutils import (
56
 
    file_kind,
57
 
    pathjoin,
58
 
)
59
 
from bzrlib.merge import Merge3Merger, Merger
 
37
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
 
38
                           ReusingTransform, CantMoveRoot, 
 
39
                           PathsNotVersionedError, ExistingLimbo,
 
40
                           ExistingPendingDeletion, ImmortalLimbo,
 
41
                           ImmortalPendingDeletion, LockError)
 
42
from bzrlib.osutils import file_kind, pathjoin
 
43
from bzrlib.merge import Merge3Merger
60
44
from bzrlib.tests import (
 
45
    CaseInsensitiveFilesystemFeature,
61
46
    HardlinkFeature,
62
47
    SymlinkFeature,
63
48
    TestCase,
64
49
    TestCaseInTempDir,
65
50
    TestSkipped,
66
 
)
67
 
from bzrlib.transform import (
68
 
    build_tree,
69
 
    create_from_tree,
70
 
    cook_conflicts,
71
 
    _FileMover,
72
 
    FinalPaths,
73
 
    get_backup_name,
74
 
    resolve_conflicts,
75
 
    resolve_checkout,
76
 
    ROOT_PARENT,
77
 
    TransformPreview,
78
 
    TreeTransform,
79
 
)
80
 
 
 
51
    )
 
52
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths, 
 
53
                              resolve_conflicts, cook_conflicts, 
 
54
                              find_interesting, build_tree, get_backup_name,
 
55
                              change_entry, _FileMover, resolve_checkout,
 
56
                              TransformPreview)
81
57
 
82
58
class TestTreeTransform(tests.TestCaseWithTransport):
83
59
 
88
64
 
89
65
    def get_transform(self):
90
66
        transform = TreeTransform(self.wt)
91
 
        self.addCleanup(transform.finalize)
 
67
        #self.addCleanup(transform.finalize)
92
68
        return transform, transform.root
93
69
 
94
70
    def test_existing_limbo(self):
109
85
    def test_existing_pending_deletion(self):
110
86
        transform, root = self.get_transform()
111
87
        deletion_path = self._limbodir = urlutils.local_path_from_url(
112
 
            transform._tree._transport.abspath('pending-deletion'))
 
88
            transform._tree._control_files.controlfilename('pending-deletion'))
113
89
        os.mkdir(pathjoin(deletion_path, 'blocking-directory'))
114
90
        self.assertRaises(ImmortalPendingDeletion, transform.apply)
115
91
        self.assertRaises(LockError, self.wt.unlock)
152
128
        self.assertIs(self.wt.is_executable('my_pretties2'), False)
153
129
        self.assertEqual('directory', file_kind(self.wt.abspath('oz')))
154
130
        self.assertEqual(len(modified_paths), 3)
155
 
        tree_mod_paths = [self.wt.id2abspath(f) for f in
 
131
        tree_mod_paths = [self.wt.id2abspath(f) for f in 
156
132
                          ('ozzie', 'my_pretties', 'my_pretties2')]
157
133
        self.assertSubset(tree_mod_paths, modified_paths)
158
134
        # is it safe to finalize repeatedly?
159
135
        transform.finalize()
160
136
        transform.finalize()
161
137
 
162
 
    def test_create_files_same_timestamp(self):
163
 
        transform, root = self.get_transform()
164
 
        self.wt.lock_tree_write()
165
 
        self.addCleanup(self.wt.unlock)
166
 
        # Roll back the clock, so that we know everything is being set to the
167
 
        # exact time
168
 
        transform._creation_mtime = creation_mtime = time.time() - 20.0
169
 
        transform.create_file('content-one',
170
 
                              transform.create_path('one', root))
171
 
        time.sleep(1) # *ugly*
172
 
        transform.create_file('content-two',
173
 
                              transform.create_path('two', root))
174
 
        transform.apply()
175
 
        fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
176
 
        fo.close()
177
 
        fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
178
 
        fo.close()
179
 
        # We only guarantee 2s resolution
180
 
        self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
181
 
            "%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
182
 
        # But if we have more than that, all files should get the same result
183
 
        self.assertEqual(st1.st_mtime, st2.st_mtime)
184
 
 
185
 
    def test_change_root_id(self):
186
 
        transform, root = self.get_transform()
187
 
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
188
 
        transform.new_directory('', ROOT_PARENT, 'new-root-id')
189
 
        transform.delete_contents(root)
190
 
        transform.unversion_file(root)
191
 
        transform.fixup_new_roots()
192
 
        transform.apply()
193
 
        self.assertEqual('new-root-id', self.wt.get_root_id())
194
 
 
195
 
    def test_change_root_id_add_files(self):
196
 
        transform, root = self.get_transform()
197
 
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
198
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
199
 
        transform.new_file('file', new_trans_id, ['new-contents\n'],
200
 
                           'new-file-id')
201
 
        transform.delete_contents(root)
202
 
        transform.unversion_file(root)
203
 
        transform.fixup_new_roots()
204
 
        transform.apply()
205
 
        self.assertEqual('new-root-id', self.wt.get_root_id())
206
 
        self.assertEqual('new-file-id', self.wt.path2id('file'))
207
 
        self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
208
 
 
209
 
    def test_add_two_roots(self):
210
 
        transform, root = self.get_transform()
211
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
212
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
213
 
        self.assertRaises(ValueError, transform.fixup_new_roots)
214
 
 
215
138
    def test_hardlink(self):
216
139
        self.requireFeature(HardlinkFeature)
217
140
        transform, root = self.get_transform()
231
154
        transform, root = self.get_transform()
232
155
        self.wt.lock_tree_write()
233
156
        self.addCleanup(self.wt.unlock)
234
 
        trans_id = transform.new_file('name', root, 'contents',
 
157
        trans_id = transform.new_file('name', root, 'contents', 
235
158
                                      'my_pretties', True)
236
159
        oz = transform.new_directory('oz', root, 'oz-id')
237
160
        dorothy = transform.new_directory('dorothy', oz, 'dorothy-id')
238
 
        toto = transform.new_file('toto', dorothy, 'toto-contents',
 
161
        toto = transform.new_file('toto', dorothy, 'toto-contents', 
239
162
                                  'toto-id', False)
240
163
 
241
164
        self.assertEqual(len(transform.find_conflicts()), 0)
265
188
 
266
189
    def test_conflicts(self):
267
190
        transform, root = self.get_transform()
268
 
        trans_id = transform.new_file('name', root, 'contents',
 
191
        trans_id = transform.new_file('name', root, 'contents', 
269
192
                                      'my_pretties')
270
193
        self.assertEqual(len(transform.find_conflicts()), 0)
271
194
        trans_id2 = transform.new_file('name', root, 'Crontents', 'toto')
272
 
        self.assertEqual(transform.find_conflicts(),
 
195
        self.assertEqual(transform.find_conflicts(), 
273
196
                         [('duplicate', trans_id, trans_id2, 'name')])
274
197
        self.assertRaises(MalformedTransform, transform.apply)
275
198
        transform.adjust_path('name', trans_id, trans_id2)
276
 
        self.assertEqual(transform.find_conflicts(),
 
199
        self.assertEqual(transform.find_conflicts(), 
277
200
                         [('non-directory parent', trans_id)])
278
201
        tinman_id = transform.trans_id_tree_path('tinman')
279
202
        transform.adjust_path('name', tinman_id, trans_id2)
280
 
        self.assertEqual(transform.find_conflicts(),
281
 
                         [('unversioned parent', tinman_id),
 
203
        self.assertEqual(transform.find_conflicts(), 
 
204
                         [('unversioned parent', tinman_id), 
282
205
                          ('missing parent', tinman_id)])
283
206
        lion_id = transform.create_path('lion', root)
284
 
        self.assertEqual(transform.find_conflicts(),
285
 
                         [('unversioned parent', tinman_id),
 
207
        self.assertEqual(transform.find_conflicts(), 
 
208
                         [('unversioned parent', tinman_id), 
286
209
                          ('missing parent', tinman_id)])
287
210
        transform.adjust_path('name', lion_id, trans_id2)
288
 
        self.assertEqual(transform.find_conflicts(),
 
211
        self.assertEqual(transform.find_conflicts(), 
289
212
                         [('unversioned parent', lion_id),
290
213
                          ('missing parent', lion_id)])
291
214
        transform.version_file("Courage", lion_id)
292
 
        self.assertEqual(transform.find_conflicts(),
293
 
                         [('missing parent', lion_id),
 
215
        self.assertEqual(transform.find_conflicts(), 
 
216
                         [('missing parent', lion_id), 
294
217
                          ('versioning no contents', lion_id)])
295
218
        transform.adjust_path('name2', root, trans_id2)
296
 
        self.assertEqual(transform.find_conflicts(),
 
219
        self.assertEqual(transform.find_conflicts(), 
297
220
                         [('versioning no contents', lion_id)])
298
221
        transform.create_file('Contents, okay?', lion_id)
299
222
        transform.adjust_path('name2', trans_id2, trans_id2)
300
 
        self.assertEqual(transform.find_conflicts(),
301
 
                         [('parent loop', trans_id2),
 
223
        self.assertEqual(transform.find_conflicts(), 
 
224
                         [('parent loop', trans_id2), 
302
225
                          ('non-directory parent', trans_id2)])
303
226
        transform.adjust_path('name2', root, trans_id2)
304
227
        oz_id = transform.new_directory('oz', root)
305
228
        transform.set_executability(True, oz_id)
306
 
        self.assertEqual(transform.find_conflicts(),
 
229
        self.assertEqual(transform.find_conflicts(), 
307
230
                         [('unversioned executability', oz_id)])
308
231
        transform.version_file('oz-id', oz_id)
309
 
        self.assertEqual(transform.find_conflicts(),
 
232
        self.assertEqual(transform.find_conflicts(), 
310
233
                         [('non-file executability', oz_id)])
311
234
        transform.set_executability(None, oz_id)
312
235
        tip_id = transform.new_file('tip', oz_id, 'ozma', 'tip-id')
321
244
        self.assert_('oz/tip' in transform2._tree_path_ids)
322
245
        self.assertEqual(fp.get_path(newtip), pathjoin('oz', 'tip'))
323
246
        self.assertEqual(len(result), 2)
324
 
        self.assertEqual((result[0][0], result[0][1]),
 
247
        self.assertEqual((result[0][0], result[0][1]), 
325
248
                         ('duplicate', newtip))
326
 
        self.assertEqual((result[1][0], result[1][2]),
 
249
        self.assertEqual((result[1][0], result[1][2]), 
327
250
                         ('duplicate id', newtip))
328
251
        transform2.finalize()
329
252
        transform3 = TreeTransform(self.wt)
330
253
        self.addCleanup(transform3.finalize)
331
254
        oz_id = transform3.trans_id_tree_file_id('oz-id')
332
255
        transform3.delete_contents(oz_id)
333
 
        self.assertEqual(transform3.find_conflicts(),
 
256
        self.assertEqual(transform3.find_conflicts(), 
334
257
                         [('missing parent', oz_id)])
335
258
        root_id = transform3.root
336
259
        tip_id = transform3.trans_id_tree_file_id('tip-id')
445
368
        self.assertContainsRe(transform._limbo_name(first), 'new-1/file')
446
369
        self.assertNotContainsRe(transform._limbo_name(second), 'new-1/FiLe')
447
370
 
448
 
    def test_adjust_path_updates_child_limbo_names(self):
449
 
        tree = self.make_branch_and_tree('tree')
450
 
        transform = TreeTransform(tree)
451
 
        self.addCleanup(transform.finalize)
452
 
        foo_id = transform.new_directory('foo', transform.root)
453
 
        bar_id = transform.new_directory('bar', foo_id)
454
 
        baz_id = transform.new_directory('baz', bar_id)
455
 
        qux_id = transform.new_directory('qux', baz_id)
456
 
        transform.adjust_path('quxx', foo_id, bar_id)
457
 
        self.assertStartsWith(transform._limbo_name(qux_id),
458
 
                              transform._limbo_name(bar_id))
459
 
 
460
371
    def test_add_del(self):
461
372
        start, root = self.get_transform()
462
373
        start.new_directory('a', root, 'a')
475
386
        self.addCleanup(unversion.finalize)
476
387
        parent = unversion.trans_id_tree_path('parent')
477
388
        unversion.unversion_file(parent)
478
 
        self.assertEqual(unversion.find_conflicts(),
 
389
        self.assertEqual(unversion.find_conflicts(), 
479
390
                         [('unversioned parent', parent_id)])
480
391
        file_id = unversion.trans_id_tree_file_id('child-id')
481
392
        unversion.unversion_file(file_id)
501
412
        mangle_tree.adjust_path('name2', root, name1)
502
413
        mangle_tree.adjust_path('name1', root, name2)
503
414
 
504
 
        #tests for deleting parent directories
 
415
        #tests for deleting parent directories 
505
416
        ddir = mangle_tree.trans_id_tree_file_id('ddir')
506
417
        mangle_tree.delete_contents(ddir)
507
418
        dfile = mangle_tree.trans_id_tree_file_id('dfile')
536
447
        create_tree,root = self.get_transform()
537
448
        newdir = create_tree.new_directory('selftest', root, 'selftest-id')
538
449
        create_tree.new_file('blackbox.py', newdir, 'hello1', 'blackbox-id')
539
 
        create_tree.apply()
 
450
        create_tree.apply()        
540
451
        mangle_tree,root = self.get_transform()
541
452
        selftest = mangle_tree.trans_id_tree_file_id('selftest-id')
542
453
        blackbox = mangle_tree.trans_id_tree_file_id('blackbox-id')
550
461
        bzrlib = create_tree.new_directory('bzrlib', root, 'bzrlib-id')
551
462
        tests = create_tree.new_directory('tests', bzrlib, 'tests-id')
552
463
        blackbox = create_tree.new_directory('blackbox', tests, 'blackbox-id')
553
 
        create_tree.new_file('test_too_much.py', blackbox, 'hello1',
 
464
        create_tree.new_file('test_too_much.py', blackbox, 'hello1', 
554
465
                             'test_too_much-id')
555
 
        create_tree.apply()
 
466
        create_tree.apply()        
556
467
        mangle_tree,root = self.get_transform()
557
468
        bzrlib = mangle_tree.trans_id_tree_file_id('bzrlib-id')
558
469
        tests = mangle_tree.trans_id_tree_file_id('tests-id')
559
470
        test_too_much = mangle_tree.trans_id_tree_file_id('test_too_much-id')
560
471
        mangle_tree.adjust_path('selftest', bzrlib, tests)
561
 
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much)
 
472
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much) 
562
473
        mangle_tree.set_executability(True, test_too_much)
563
474
        mangle_tree.apply()
564
475
 
565
476
    def test_both_rename3(self):
566
477
        create_tree,root = self.get_transform()
567
478
        tests = create_tree.new_directory('tests', root, 'tests-id')
568
 
        create_tree.new_file('test_too_much.py', tests, 'hello1',
 
479
        create_tree.new_file('test_too_much.py', tests, 'hello1', 
569
480
                             'test_too_much-id')
570
 
        create_tree.apply()
 
481
        create_tree.apply()        
571
482
        mangle_tree,root = self.get_transform()
572
483
        tests = mangle_tree.trans_id_tree_file_id('tests-id')
573
484
        test_too_much = mangle_tree.trans_id_tree_file_id('test_too_much-id')
574
485
        mangle_tree.adjust_path('selftest', root, tests)
575
 
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much)
 
486
        mangle_tree.adjust_path('blackbox.py', tests, test_too_much) 
576
487
        mangle_tree.set_executability(True, test_too_much)
577
488
        mangle_tree.apply()
578
489
 
591
502
        newdir = move_id.new_directory('dir', root, 'newdir')
592
503
        move_id.adjust_path('name2', newdir, name1)
593
504
        move_id.apply()
594
 
 
 
505
        
595
506
    def test_replace_dangling_ie(self):
596
507
        create_tree, root = self.get_transform()
597
508
        # prepare tree
613
524
        resolve_conflicts(replace)
614
525
        replace.apply()
615
526
 
616
 
    def _test_symlinks(self, link_name1,link_target1,
617
 
                       link_name2, link_target2):
618
 
 
619
 
        def ozpath(p): return 'oz/' + p
620
 
 
 
527
    def test_symlinks(self):
621
528
        self.requireFeature(SymlinkFeature)
622
 
        transform, root = self.get_transform()
 
529
        transform,root = self.get_transform()
623
530
        oz_id = transform.new_directory('oz', root, 'oz-id')
624
 
        wizard = transform.new_symlink(link_name1, oz_id, link_target1,
 
531
        wizard = transform.new_symlink('wizard', oz_id, 'wizard-target', 
625
532
                                       'wizard-id')
626
 
        wiz_id = transform.create_path(link_name2, oz_id)
627
 
        transform.create_symlink(link_target2, wiz_id)
628
 
        transform.version_file('wiz-id2', wiz_id)
 
533
        wiz_id = transform.create_path('wizard2', oz_id)
 
534
        transform.create_symlink('behind_curtain', wiz_id)
 
535
        transform.version_file('wiz-id2', wiz_id)            
629
536
        transform.set_executability(True, wiz_id)
630
 
        self.assertEqual(transform.find_conflicts(),
 
537
        self.assertEqual(transform.find_conflicts(), 
631
538
                         [('non-file executability', wiz_id)])
632
539
        transform.set_executability(None, wiz_id)
633
540
        transform.apply()
634
 
        self.assertEqual(self.wt.path2id(ozpath(link_name1)), 'wizard-id')
635
 
        self.assertEqual('symlink',
636
 
                         file_kind(self.wt.abspath(ozpath(link_name1))))
637
 
        self.assertEqual(link_target2,
638
 
                         osutils.readlink(self.wt.abspath(ozpath(link_name2))))
639
 
        self.assertEqual(link_target1,
640
 
                         osutils.readlink(self.wt.abspath(ozpath(link_name1))))
641
 
 
642
 
    def test_symlinks(self):
643
 
        self._test_symlinks('wizard', 'wizard-target',
644
 
                            'wizard2', 'behind_curtain')
645
 
 
646
 
    def test_symlinks_unicode(self):
647
 
        self.requireFeature(tests.UnicodeFilenameFeature)
648
 
        self._test_symlinks(u'\N{Euro Sign}wizard',
649
 
                            u'wizard-targ\N{Euro Sign}t',
650
 
                            u'\N{Euro Sign}wizard2',
651
 
                            u'b\N{Euro Sign}hind_curtain')
 
541
        self.assertEqual(self.wt.path2id('oz/wizard'), 'wizard-id')
 
542
        self.assertEqual(file_kind(self.wt.abspath('oz/wizard')), 'symlink')
 
543
        self.assertEqual(os.readlink(self.wt.abspath('oz/wizard2')), 
 
544
                         'behind_curtain')
 
545
        self.assertEqual(os.readlink(self.wt.abspath('oz/wizard')),
 
546
                         'wizard-target')
652
547
 
653
548
    def test_unable_create_symlink(self):
654
549
        def tt_helper():
678
573
        create.apply()
679
574
        conflicts,root = self.get_transform()
680
575
        # set up duplicate entry, duplicate id
681
 
        new_dorothy = conflicts.new_file('dorothy', root, 'dorothy',
 
576
        new_dorothy = conflicts.new_file('dorothy', root, 'dorothy', 
682
577
                                         'dorothy-id')
683
578
        old_dorothy = conflicts.trans_id_tree_file_id('dorothy-id')
684
579
        oz = conflicts.trans_id_tree_file_id('oz-id')
708
603
        tt, emerald, oz, old_dorothy, new_dorothy = self.get_conflicted()
709
604
        raw_conflicts = resolve_conflicts(tt)
710
605
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
711
 
        duplicate = DuplicateEntry('Moved existing file to', 'dorothy.moved',
 
606
        duplicate = DuplicateEntry('Moved existing file to', 'dorothy.moved', 
712
607
                                   'dorothy', None, 'dorothy-id')
713
608
        self.assertEqual(cooked_conflicts[0], duplicate)
714
 
        duplicate_id = DuplicateID('Unversioned existing file',
 
609
        duplicate_id = DuplicateID('Unversioned existing file', 
715
610
                                   'dorothy.moved', 'dorothy', None,
716
611
                                   'dorothy-id')
717
612
        self.assertEqual(cooked_conflicts[1], duplicate_id)
725
620
        unversioned_parent2 = UnversionedParent('Versioned directory', 'oz',
726
621
                                               'oz-id')
727
622
        self.assertEqual(cooked_conflicts[3], unversioned_parent)
728
 
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity',
 
623
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity', 
729
624
                                 'oz/emeraldcity', 'emerald-id', 'emerald-id')
730
625
        self.assertEqual(cooked_conflicts[4], deleted_parent)
731
626
        self.assertEqual(cooked_conflicts[5], unversioned_parent2)
757
652
                                         ' versioned, but has versioned'
758
653
                                         ' children.  Versioned directory.')
759
654
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
760
 
                                         ' oz/emeraldcity. Cancelled move.')
 
655
                                         ' oz/emeraldcity.  Cancelled move.')
761
656
 
762
657
    def prepare_wrong_parent_kind(self):
763
658
        tt, root = self.get_transform()
834
729
        create.apply()
835
730
        transform, root = self.get_transform()
836
731
        transform.adjust_root_path('oldroot', fun)
837
 
        new_root = transform.trans_id_tree_path('')
 
732
        new_root=transform.trans_id_tree_path('')
838
733
        transform.version_file('new-root', new_root)
839
734
        transform.apply()
840
735
 
852
747
        rename.set_executability(True, myfile)
853
748
        rename.apply()
854
749
 
855
 
    def test_rename_fails(self):
856
 
        # see https://bugs.launchpad.net/bzr/+bug/491763
857
 
        create, root_id = self.get_transform()
858
 
        first_dir = create.new_directory('first-dir', root_id, 'first-id')
859
 
        myfile = create.new_file('myfile', root_id, 'myfile-text',
860
 
                                 'myfile-id')
 
750
    def test_find_interesting(self):
 
751
        create, root = self.get_transform()
 
752
        wt = create._tree
 
753
        create.new_file('vfile', root, 'myfile-text', 'myfile-id')
 
754
        create.new_file('uvfile', root, 'othertext')
861
755
        create.apply()
862
 
        if os.name == "posix" and sys.platform != "cygwin":
863
 
            # posix filesystems fail on renaming if the readonly bit is set
864
 
            osutils.make_readonly(self.wt.abspath('first-dir'))
865
 
        elif os.name == "nt":
866
 
            # windows filesystems fail on renaming open files
867
 
            self.addCleanup(file(self.wt.abspath('myfile')).close)
868
 
        else:
869
 
            self.skip("Don't know how to force a permissions error on rename")
870
 
        # now transform to rename
871
 
        rename_transform, root_id = self.get_transform()
872
 
        file_trans_id = rename_transform.trans_id_file_id('myfile-id')
873
 
        dir_id = rename_transform.trans_id_file_id('first-id')
874
 
        rename_transform.adjust_path('newname', dir_id, file_trans_id)
875
 
        e = self.assertRaises(errors.TransformRenameFailed,
876
 
            rename_transform.apply)
877
 
        # On nix looks like: 
878
 
        # "Failed to rename .../work/.bzr/checkout/limbo/new-1
879
 
        # to .../first-dir/newname: [Errno 13] Permission denied"
880
 
        # On windows looks like:
881
 
        # "Failed to rename .../work/myfile to 
882
 
        # .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
883
 
        # The strerror will vary per OS and language so it's not checked here
884
 
        self.assertContainsRe(str(e),
885
 
            "Failed to rename .*(first-dir.newname:|myfile)")
 
756
        result = self.applyDeprecated(symbol_versioning.zero_fifteen,
 
757
            find_interesting, wt, wt, ['vfile'])
 
758
        self.assertEqual(result, set(['myfile-id']))
886
759
 
887
760
    def test_set_executability_order(self):
888
761
        """Ensure that executability behaves the same, no matter what order.
889
 
 
 
762
        
890
763
        - create file and set executability simultaneously
891
764
        - create file and set executability afterward
892
765
        - unsetting the executability of a file whose executability has not been
1299
1172
        transform.cancel_creation(parent)
1300
1173
        transform.finalize()
1301
1174
 
1302
 
    def test_rollback_on_directory_clash(self):
 
1175
    def test_change_entry(self):
 
1176
        txt = 'bzrlib.transform.change_entry was deprecated in version 0.90.'
 
1177
        self.callDeprecated([txt], change_entry, None, None, None, None, None,
 
1178
            None, None, None)
 
1179
 
 
1180
    def test_case_insensitive_clash(self):
 
1181
        self.requireFeature(CaseInsensitiveFilesystemFeature)
1303
1182
        def tt_helper():
1304
1183
            wt = self.make_branch_and_tree('.')
1305
1184
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
1306
1185
            try:
1307
 
                foo = tt.new_directory('foo', tt.root)
1308
 
                tt.new_file('bar', foo, 'foobar')
1309
 
                baz = tt.new_directory('baz', tt.root)
1310
 
                tt.new_file('qux', baz, 'quux')
1311
 
                # Ask for a rename 'foo' -> 'baz'
1312
 
                tt.adjust_path('baz', tt.root, foo)
 
1186
                tt.new_file('foo', tt.root, 'bar')
 
1187
                tt.new_file('Foo', tt.root, 'spam')
1313
1188
                # Lie to tt that we've already resolved all conflicts.
1314
1189
                tt.apply(no_conflicts=True)
1315
1190
            except:
1316
1191
                wt.unlock()
1317
1192
                raise
1318
 
        # The rename will fail because the target directory is not empty (but
1319
 
        # raises FileExists anyway).
1320
1193
        err = self.assertRaises(errors.FileExists, tt_helper)
1321
1194
        self.assertContainsRe(str(err),
1322
 
            "^File exists: .+/baz")
 
1195
            "^File exists: .+/foo")
1323
1196
 
1324
1197
    def test_two_directories_clash(self):
1325
1198
        def tt_helper():
1328
1201
            try:
1329
1202
                foo_1 = tt.new_directory('foo', tt.root)
1330
1203
                tt.new_directory('bar', foo_1)
1331
 
                # Adding the same directory with a different content
1332
1204
                foo_2 = tt.new_directory('foo', tt.root)
1333
1205
                tt.new_directory('baz', foo_2)
1334
1206
                # Lie to tt that we've already resolved all conflicts.
1347
1219
            try:
1348
1220
                foo_1 = tt.new_directory('foo', tt.root)
1349
1221
                tt.new_directory('bar', foo_1)
1350
 
                # Adding the same directory with a different content
1351
1222
                foo_2 = tt.new_directory('foo', tt.root)
1352
1223
                tt.new_directory('baz', foo_2)
1353
1224
                # Lie to tt that we've already resolved all conflicts.
1359
1230
        self.assertContainsRe(str(err),
1360
1231
            "^File exists: .+/foo")
1361
1232
 
1362
 
    def test_file_to_directory(self):
1363
 
        wt = self.make_branch_and_tree('.')
1364
 
        self.build_tree(['foo'])
1365
 
        wt.add(['foo'])
1366
 
        wt.commit("one")
1367
 
        tt = TreeTransform(wt)
1368
 
        self.addCleanup(tt.finalize)
1369
 
        foo_trans_id = tt.trans_id_tree_path("foo")
1370
 
        tt.delete_contents(foo_trans_id)
1371
 
        tt.create_directory(foo_trans_id)
1372
 
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1373
 
        tt.create_file(["aa\n"], bar_trans_id)
1374
 
        tt.version_file("bar-1", bar_trans_id)
1375
 
        tt.apply()
1376
 
        self.failUnlessExists("foo/bar")
1377
 
        wt.lock_read()
1378
 
        try:
1379
 
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1380
 
                    "directory")
1381
 
        finally:
1382
 
            wt.unlock()
1383
 
        wt.commit("two")
1384
 
        changes = wt.changes_from(wt.basis_tree())
1385
 
        self.assertFalse(changes.has_changed(), changes)
1386
 
 
1387
 
    def test_file_to_symlink(self):
1388
 
        self.requireFeature(SymlinkFeature)
1389
 
        wt = self.make_branch_and_tree('.')
1390
 
        self.build_tree(['foo'])
1391
 
        wt.add(['foo'])
1392
 
        wt.commit("one")
1393
 
        tt = TreeTransform(wt)
1394
 
        self.addCleanup(tt.finalize)
1395
 
        foo_trans_id = tt.trans_id_tree_path("foo")
1396
 
        tt.delete_contents(foo_trans_id)
1397
 
        tt.create_symlink("bar", foo_trans_id)
1398
 
        tt.apply()
1399
 
        self.failUnlessExists("foo")
1400
 
        wt.lock_read()
1401
 
        self.addCleanup(wt.unlock)
1402
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1403
 
                "symlink")
1404
 
 
1405
 
    def test_dir_to_file(self):
1406
 
        wt = self.make_branch_and_tree('.')
1407
 
        self.build_tree(['foo/', 'foo/bar'])
1408
 
        wt.add(['foo', 'foo/bar'])
1409
 
        wt.commit("one")
1410
 
        tt = TreeTransform(wt)
1411
 
        self.addCleanup(tt.finalize)
1412
 
        foo_trans_id = tt.trans_id_tree_path("foo")
1413
 
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1414
 
        tt.delete_contents(foo_trans_id)
1415
 
        tt.delete_versioned(bar_trans_id)
1416
 
        tt.create_file(["aa\n"], foo_trans_id)
1417
 
        tt.apply()
1418
 
        self.failUnlessExists("foo")
1419
 
        wt.lock_read()
1420
 
        self.addCleanup(wt.unlock)
1421
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1422
 
                "file")
1423
 
 
1424
 
    def test_dir_to_hardlink(self):
1425
 
        self.requireFeature(HardlinkFeature)
1426
 
        wt = self.make_branch_and_tree('.')
1427
 
        self.build_tree(['foo/', 'foo/bar'])
1428
 
        wt.add(['foo', 'foo/bar'])
1429
 
        wt.commit("one")
1430
 
        tt = TreeTransform(wt)
1431
 
        self.addCleanup(tt.finalize)
1432
 
        foo_trans_id = tt.trans_id_tree_path("foo")
1433
 
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
1434
 
        tt.delete_contents(foo_trans_id)
1435
 
        tt.delete_versioned(bar_trans_id)
1436
 
        self.build_tree(['baz'])
1437
 
        tt.create_hardlink("baz", foo_trans_id)
1438
 
        tt.apply()
1439
 
        self.failUnlessExists("foo")
1440
 
        self.failUnlessExists("baz")
1441
 
        wt.lock_read()
1442
 
        self.addCleanup(wt.unlock)
1443
 
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1444
 
                "file")
1445
 
 
1446
 
    def test_no_final_path(self):
1447
 
        transform, root = self.get_transform()
1448
 
        trans_id = transform.trans_id_file_id('foo')
1449
 
        transform.create_file('bar', trans_id)
1450
 
        transform.cancel_creation(trans_id)
1451
 
        transform.apply()
1452
 
 
1453
 
    def test_create_from_tree(self):
1454
 
        tree1 = self.make_branch_and_tree('tree1')
1455
 
        self.build_tree_contents([('tree1/foo/',), ('tree1/bar', 'baz')])
1456
 
        tree1.add(['foo', 'bar'], ['foo-id', 'bar-id'])
1457
 
        tree2 = self.make_branch_and_tree('tree2')
1458
 
        tt = TreeTransform(tree2)
1459
 
        foo_trans_id = tt.create_path('foo', tt.root)
1460
 
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id')
1461
 
        bar_trans_id = tt.create_path('bar', tt.root)
1462
 
        create_from_tree(tt, bar_trans_id, tree1, 'bar-id')
1463
 
        tt.apply()
1464
 
        self.assertEqual('directory', osutils.file_kind('tree2/foo'))
1465
 
        self.assertFileEqual('baz', 'tree2/bar')
1466
 
 
1467
 
    def test_create_from_tree_bytes(self):
1468
 
        """Provided lines are used instead of tree content."""
1469
 
        tree1 = self.make_branch_and_tree('tree1')
1470
 
        self.build_tree_contents([('tree1/foo', 'bar'),])
1471
 
        tree1.add('foo', 'foo-id')
1472
 
        tree2 = self.make_branch_and_tree('tree2')
1473
 
        tt = TreeTransform(tree2)
1474
 
        foo_trans_id = tt.create_path('foo', tt.root)
1475
 
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id', bytes='qux')
1476
 
        tt.apply()
1477
 
        self.assertFileEqual('qux', 'tree2/foo')
1478
 
 
1479
 
    def test_create_from_tree_symlink(self):
1480
 
        self.requireFeature(SymlinkFeature)
1481
 
        tree1 = self.make_branch_and_tree('tree1')
1482
 
        os.symlink('bar', 'tree1/foo')
1483
 
        tree1.add('foo', 'foo-id')
1484
 
        tt = TreeTransform(self.make_branch_and_tree('tree2'))
1485
 
        foo_trans_id = tt.create_path('foo', tt.root)
1486
 
        create_from_tree(tt, foo_trans_id, tree1, 'foo-id')
1487
 
        tt.apply()
1488
 
        self.assertEqual('bar', os.readlink('tree2/foo'))
1489
 
 
1490
1233
 
1491
1234
class TransformGroup(object):
1492
1235
 
1546
1289
        # textual merge
1547
1290
        self.assertEqual(this.wt.get_file('a').read(), 'y\nb\nc\nd\bz\n')
1548
1291
        # three-way text conflict
1549
 
        self.assertEqual(this.wt.get_file('b').read(),
 
1292
        self.assertEqual(this.wt.get_file('b').read(), 
1550
1293
                         conflict_text('b', 'b2'))
1551
1294
        # OTHER wins
1552
1295
        self.assertEqual(this.wt.get_file('c').read(), 'c2')
1556
1299
        self.assertEqual(this.wt.get_file('e').read(), 'e2')
1557
1300
        # No change
1558
1301
        self.assertEqual(this.wt.get_file('f').read(), 'f')
1559
 
        # Correct correct results when THIS == OTHER
 
1302
        # Correct correct results when THIS == OTHER 
1560
1303
        self.assertEqual(this.wt.get_file('g').read(), 'g')
1561
1304
        # Text conflict when THIS & OTHER are text and BASE is dir
1562
 
        self.assertEqual(this.wt.get_file('h').read(),
 
1305
        self.assertEqual(this.wt.get_file('h').read(), 
1563
1306
                         conflict_text('1\n2\n3\n4\n', 'h\ni\nj\nk\n'))
1564
1307
        self.assertEqual(this.wt.get_file_byname('h.THIS').read(),
1565
1308
                         '1\n2\n3\n4\n')
1566
1309
        self.assertEqual(this.wt.get_file_byname('h.OTHER').read(),
1567
1310
                         'h\ni\nj\nk\n')
1568
1311
        self.assertEqual(file_kind(this.wt.abspath('h.BASE')), 'directory')
1569
 
        self.assertEqual(this.wt.get_file('i').read(),
 
1312
        self.assertEqual(this.wt.get_file('i').read(), 
1570
1313
                         conflict_text('1\n2\n3\n4\n', 'h\ni\nj\nk\n'))
1571
1314
        self.assertEqual(this.wt.get_file_byname('i.THIS').read(),
1572
1315
                         '1\n2\n3\n4\n')
1596
1339
            tg.tt.new_symlink('b', tg.root, 'b', 'b')
1597
1340
            tg.tt.new_file('c', tg.root, 'c', 'c')
1598
1341
            tg.tt.new_symlink('d', tg.root, tg.name, 'd')
1599
 
        targets = ((base, 'base-e', 'base-f', None, None),
1600
 
                   (this, 'other-e', 'this-f', 'other-g', 'this-h'),
 
1342
        targets = ((base, 'base-e', 'base-f', None, None), 
 
1343
                   (this, 'other-e', 'this-f', 'other-g', 'this-h'), 
1601
1344
                   (other, 'other-e', None, 'other-g', 'other-h'))
1602
1345
        for tg, e_target, f_target, g_target, h_target in targets:
1603
 
            for link, target in (('e', e_target), ('f', f_target),
 
1346
            for link, target in (('e', e_target), ('f', f_target), 
1604
1347
                                 ('g', g_target), ('h', h_target)):
1605
1348
                if target is not None:
1606
1349
                    tg.tt.new_symlink(link, tg.root, target, link)
1632
1375
        base = TransformGroup("BASE", root_id)
1633
1376
        this = TransformGroup("THIS", root_id)
1634
1377
        other = TransformGroup("OTHER", root_id)
1635
 
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a')
 
1378
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
1636
1379
                                   for t in [base, this, other]]
1637
 
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b')
 
1380
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
1638
1381
                                   for t in [base, this, other]]
1639
1382
        base.tt.new_directory('c', base_a, 'c')
1640
1383
        this.tt.new_directory('c1', this_a, 'c')
1665
1408
        base = TransformGroup("BASE", root_id)
1666
1409
        this = TransformGroup("THIS", root_id)
1667
1410
        other = TransformGroup("OTHER", root_id)
1668
 
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a')
 
1411
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
1669
1412
                                   for t in [base, this, other]]
1670
 
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b')
 
1413
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
1671
1414
                                   for t in [base, this, other]]
1672
1415
 
1673
1416
        base.tt.new_file('g', base_a, 'g', 'g')
1764
1507
        os.symlink('foo', 'target2/symlink')
1765
1508
        build_tree(source.basis_tree(), target)
1766
1509
        self.assertEqual([], target.conflicts())
1767
 
 
 
1510
        
1768
1511
    def test_directory_conflict_handling(self):
1769
1512
        """Ensure that when building trees, conflict handling is done"""
1770
1513
        source = self.make_branch_and_tree('source')
1826
1569
        target = self.make_branch_and_tree('target')
1827
1570
        self.build_tree(['target/name'])
1828
1571
        target.add('name')
1829
 
        self.assertRaises(errors.WorkingTreeAlreadyPopulated,
 
1572
        self.assertRaises(errors.WorkingTreeAlreadyPopulated, 
1830
1573
            build_tree, source.basis_tree(), target)
1831
1574
 
1832
1575
    def test_build_tree_rename_count(self):
1976
1719
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1977
1720
        self.assertTrue(source.is_executable('file1-id'))
1978
1721
 
1979
 
    def install_rot13_content_filter(self, pattern):
1980
 
        # We could use
1981
 
        # self.addCleanup(filters._reset_registry, filters._reset_registry())
1982
 
        # below, but that looks a bit... hard to read even if it's exactly
1983
 
        # the same thing.
1984
 
        original_registry = filters._reset_registry()
1985
 
        def restore_registry():
1986
 
            filters._reset_registry(original_registry)
1987
 
        self.addCleanup(restore_registry)
1988
 
        def rot13(chunks, context=None):
1989
 
            return [''.join(chunks).encode('rot13')]
1990
 
        rot13filter = filters.ContentFilter(rot13, rot13)
1991
 
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
1992
 
        os.mkdir(self.test_home_dir + '/.bazaar')
1993
 
        rules_filename = self.test_home_dir + '/.bazaar/rules'
1994
 
        f = open(rules_filename, 'wb')
1995
 
        f.write('[name %s]\nrot13=yes\n' % (pattern,))
1996
 
        f.close()
1997
 
        def uninstall_rules():
1998
 
            os.remove(rules_filename)
1999
 
            rules.reset_rules()
2000
 
        self.addCleanup(uninstall_rules)
2001
 
        rules.reset_rules()
2002
 
 
2003
 
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
2004
 
        """build_tree will not hardlink files that have content filtering rules
2005
 
        applied to them (but will still hardlink other files from the same tree
2006
 
        if it can).
2007
 
        """
2008
 
        self.requireFeature(HardlinkFeature)
2009
 
        self.install_rot13_content_filter('file1')
2010
 
        source = self.create_ab_tree()
2011
 
        target = self.make_branch_and_tree('target')
2012
 
        revision_tree = source.basis_tree()
2013
 
        revision_tree.lock_read()
2014
 
        self.addCleanup(revision_tree.unlock)
2015
 
        build_tree(revision_tree, target, source, hardlink=True)
2016
 
        target.lock_read()
2017
 
        self.addCleanup(target.unlock)
2018
 
        self.assertEqual([], list(target.iter_changes(revision_tree)))
2019
 
        source_stat = os.stat('source/file1')
2020
 
        target_stat = os.stat('target/file1')
2021
 
        self.assertNotEqual(source_stat, target_stat)
2022
 
        source_stat = os.stat('source/file2')
2023
 
        target_stat = os.stat('target/file2')
2024
 
        self.assertEqualStat(source_stat, target_stat)
2025
 
 
2026
 
    def test_case_insensitive_build_tree_inventory(self):
2027
 
        if (tests.CaseInsensitiveFilesystemFeature.available()
2028
 
            or tests.CaseInsCasePresFilenameFeature.available()):
2029
 
            raise tests.UnavailableFeature('Fully case sensitive filesystem')
2030
 
        source = self.make_branch_and_tree('source')
2031
 
        self.build_tree(['source/file', 'source/FILE'])
2032
 
        source.add(['file', 'FILE'], ['lower-id', 'upper-id'])
2033
 
        source.commit('added files')
2034
 
        # Don't try this at home, kids!
2035
 
        # Force the tree to report that it is case insensitive
2036
 
        target = self.make_branch_and_tree('target')
2037
 
        target.case_sensitive = False
2038
 
        build_tree(source.basis_tree(), target, source, delta_from_tree=True)
2039
 
        self.assertEqual('file.moved', target.id2path('lower-id'))
2040
 
        self.assertEqual('FILE', target.id2path('upper-id'))
2041
 
 
2042
 
 
2043
 
class TestCommitTransform(tests.TestCaseWithTransport):
2044
 
 
2045
 
    def get_branch(self):
2046
 
        tree = self.make_branch_and_tree('tree')
2047
 
        tree.lock_write()
2048
 
        self.addCleanup(tree.unlock)
2049
 
        tree.commit('empty commit')
2050
 
        return tree.branch
2051
 
 
2052
 
    def get_branch_and_transform(self):
2053
 
        branch = self.get_branch()
2054
 
        tt = TransformPreview(branch.basis_tree())
2055
 
        self.addCleanup(tt.finalize)
2056
 
        return branch, tt
2057
 
 
2058
 
    def test_commit_wrong_basis(self):
2059
 
        branch = self.get_branch()
2060
 
        basis = branch.repository.revision_tree(
2061
 
            _mod_revision.NULL_REVISION)
2062
 
        tt = TransformPreview(basis)
2063
 
        self.addCleanup(tt.finalize)
2064
 
        e = self.assertRaises(ValueError, tt.commit, branch, '')
2065
 
        self.assertEqual('TreeTransform not based on branch basis: null:',
2066
 
                         str(e))
2067
 
 
2068
 
    def test_empy_commit(self):
2069
 
        branch, tt = self.get_branch_and_transform()
2070
 
        rev = tt.commit(branch, 'my message')
2071
 
        self.assertEqual(2, branch.revno())
2072
 
        repo = branch.repository
2073
 
        self.assertEqual('my message', repo.get_revision(rev).message)
2074
 
 
2075
 
    def test_merge_parents(self):
2076
 
        branch, tt = self.get_branch_and_transform()
2077
 
        rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
2078
 
        self.assertEqual(['rev1b', 'rev1c'],
2079
 
                         branch.basis_tree().get_parent_ids()[1:])
2080
 
 
2081
 
    def test_first_commit(self):
2082
 
        branch = self.make_branch('branch')
2083
 
        branch.lock_write()
2084
 
        self.addCleanup(branch.unlock)
2085
 
        tt = TransformPreview(branch.basis_tree())
2086
 
        self.addCleanup(tt.finalize)
2087
 
        tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
2088
 
        rev = tt.commit(branch, 'my message')
2089
 
        self.assertEqual([], branch.basis_tree().get_parent_ids())
2090
 
        self.assertNotEqual(_mod_revision.NULL_REVISION,
2091
 
                            branch.last_revision())
2092
 
 
2093
 
    def test_first_commit_with_merge_parents(self):
2094
 
        branch = self.make_branch('branch')
2095
 
        branch.lock_write()
2096
 
        self.addCleanup(branch.unlock)
2097
 
        tt = TransformPreview(branch.basis_tree())
2098
 
        self.addCleanup(tt.finalize)
2099
 
        e = self.assertRaises(ValueError, tt.commit, branch,
2100
 
                          'my message', ['rev1b-id'])
2101
 
        self.assertEqual('Cannot supply merge parents for first commit.',
2102
 
                         str(e))
2103
 
        self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
2104
 
 
2105
 
    def test_add_files(self):
2106
 
        branch, tt = self.get_branch_and_transform()
2107
 
        tt.new_file('file', tt.root, 'contents', 'file-id')
2108
 
        trans_id = tt.new_directory('dir', tt.root, 'dir-id')
2109
 
        if SymlinkFeature.available():
2110
 
            tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
2111
 
        rev = tt.commit(branch, 'message')
2112
 
        tree = branch.basis_tree()
2113
 
        self.assertEqual('file', tree.id2path('file-id'))
2114
 
        self.assertEqual('contents', tree.get_file_text('file-id'))
2115
 
        self.assertEqual('dir', tree.id2path('dir-id'))
2116
 
        if SymlinkFeature.available():
2117
 
            self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2118
 
            self.assertEqual('target', tree.get_symlink_target('symlink-id'))
2119
 
 
2120
 
    def test_add_unversioned(self):
2121
 
        branch, tt = self.get_branch_and_transform()
2122
 
        tt.new_file('file', tt.root, 'contents')
2123
 
        self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
2124
 
                          'message', strict=True)
2125
 
 
2126
 
    def test_modify_strict(self):
2127
 
        branch, tt = self.get_branch_and_transform()
2128
 
        tt.new_file('file', tt.root, 'contents', 'file-id')
2129
 
        tt.commit(branch, 'message', strict=True)
2130
 
        tt = TransformPreview(branch.basis_tree())
2131
 
        self.addCleanup(tt.finalize)
2132
 
        trans_id = tt.trans_id_file_id('file-id')
2133
 
        tt.delete_contents(trans_id)
2134
 
        tt.create_file('contents', trans_id)
2135
 
        tt.commit(branch, 'message', strict=True)
2136
 
 
2137
 
    def test_commit_malformed(self):
2138
 
        """Committing a malformed transform should raise an exception.
2139
 
 
2140
 
        In this case, we are adding a file without adding its parent.
2141
 
        """
2142
 
        branch, tt = self.get_branch_and_transform()
2143
 
        parent_id = tt.trans_id_file_id('parent-id')
2144
 
        tt.new_file('file', parent_id, 'contents', 'file-id')
2145
 
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2146
 
                          'message')
2147
 
 
2148
 
    def test_commit_rich_revision_data(self):
2149
 
        branch, tt = self.get_branch_and_transform()
2150
 
        rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2151
 
                           committer='me <me@example.com>',
2152
 
                           revprops={'foo': 'bar'}, revision_id='revid-1',
2153
 
                           authors=['Author1 <author1@example.com>',
2154
 
                              'Author2 <author2@example.com>',
2155
 
                               ])
2156
 
        self.assertEqual('revid-1', rev_id)
2157
 
        revision = branch.repository.get_revision(rev_id)
2158
 
        self.assertEqual(1, revision.timestamp)
2159
 
        self.assertEqual(43201, revision.timezone)
2160
 
        self.assertEqual('me <me@example.com>', revision.committer)
2161
 
        self.assertEqual(['Author1 <author1@example.com>',
2162
 
                          'Author2 <author2@example.com>'],
2163
 
                         revision.get_apparent_authors())
2164
 
        del revision.properties['authors']
2165
 
        self.assertEqual({'foo': 'bar',
2166
 
                          'branch-nick': 'tree'},
2167
 
                         revision.properties)
2168
 
 
2169
 
    def test_no_explicit_revprops(self):
2170
 
        branch, tt = self.get_branch_and_transform()
2171
 
        rev_id = tt.commit(branch, 'message', authors=[
2172
 
            'Author1 <author1@example.com>',
2173
 
            'Author2 <author2@example.com>', ])
2174
 
        revision = branch.repository.get_revision(rev_id)
2175
 
        self.assertEqual(['Author1 <author1@example.com>',
2176
 
                          'Author2 <author2@example.com>'],
2177
 
                         revision.get_apparent_authors())
2178
 
        self.assertEqual('tree', revision.properties['branch-nick'])
2179
 
 
2180
1722
 
2181
1723
class MockTransform(object):
2182
1724
 
2338
1880
        resolve_conflicts(tt)
2339
1881
 
2340
1882
 
2341
 
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2342
 
                  ('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2343
 
                  (False, False))
2344
 
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2345
 
              ('', ''), ('directory', 'directory'), (False, None))
2346
 
 
2347
 
 
2348
1883
class TestTransformPreview(tests.TestCaseWithTransport):
2349
1884
 
2350
1885
    def create_tree(self):
2351
1886
        tree = self.make_branch_and_tree('.')
2352
1887
        self.build_tree_contents([('a', 'content 1')])
2353
 
        tree.set_root_id('TREE_ROOT')
2354
1888
        tree.add('a', 'a-id')
2355
1889
        tree.commit('rev1', rev_id='rev1')
2356
1890
        return tree.branch.repository.revision_tree('rev1')
2423
1957
                          (False, False))],
2424
1958
                          list(preview_tree.iter_changes(revision_tree)))
2425
1959
 
2426
 
    def test_include_unchanged_succeeds(self):
 
1960
    def test_wrong_tree_value_error(self):
2427
1961
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2428
 
        changes = preview_tree.iter_changes(revision_tree,
2429
 
                                            include_unchanged=True)
2430
 
        root = revision_tree.inventory.root.file_id
 
1962
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1963
                              preview_tree)
 
1964
        self.assertEqual('from_tree must be transform source tree.', str(e))
2431
1965
 
2432
 
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
1966
    def test_include_unchanged_value_error(self):
 
1967
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1968
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1969
                              revision_tree, include_unchanged=True)
 
1970
        self.assertEqual('include_unchanged is not supported', str(e))
2433
1971
 
2434
1972
    def test_specific_files(self):
2435
1973
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2436
 
        changes = preview_tree.iter_changes(revision_tree,
2437
 
                                            specific_files=[''])
2438
 
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
1974
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1975
                              revision_tree, specific_files=['pete'])
 
1976
        self.assertEqual('specific_files is not supported', str(e))
2439
1977
 
2440
 
    def test_want_unversioned(self):
 
1978
    def test_want_unversioned_value_error(self):
2441
1979
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2442
 
        changes = preview_tree.iter_changes(revision_tree,
2443
 
                                            want_unversioned=True)
2444
 
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
1980
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
1981
                              revision_tree, want_unversioned=True)
 
1982
        self.assertEqual('want_unversioned is not supported', str(e))
2445
1983
 
2446
1984
    def test_ignore_extra_trees_no_specific_files(self):
2447
1985
        # extra_trees is harmless without specific_files, so we'll silently
2457
1995
    def test_ignore_pb(self):
2458
1996
        # pb could be supported, but TT.iter_changes doesn't support it.
2459
1997
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2460
 
        preview_tree.iter_changes(revision_tree)
 
1998
        preview_tree.iter_changes(revision_tree, pb=progress.DummyProgress())
2461
1999
 
2462
2000
    def test_kind(self):
2463
2001
        revision_tree = self.create_tree()
2478
2016
        self.assertEqual(os.stat(limbo_path).st_mtime,
2479
2017
                         preview_tree.get_file_mtime('file-id'))
2480
2018
 
2481
 
    def test_get_file_mtime_renamed(self):
2482
 
        work_tree = self.make_branch_and_tree('tree')
2483
 
        self.build_tree(['tree/file'])
2484
 
        work_tree.add('file', 'file-id')
2485
 
        preview = TransformPreview(work_tree)
2486
 
        self.addCleanup(preview.finalize)
2487
 
        file_trans_id = preview.trans_id_tree_file_id('file-id')
2488
 
        preview.adjust_path('renamed', preview.root, file_trans_id)
2489
 
        preview_tree = preview.get_preview_tree()
2490
 
        preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
2491
 
        work_mtime = work_tree.get_file_mtime('file-id', 'file')
2492
 
 
2493
2019
    def test_get_file(self):
2494
2020
        preview = self.get_empty_preview()
2495
2021
        preview.new_file('file', preview.root, 'contents', 'file-id')
2507
2033
        preview_tree = preview.get_preview_tree()
2508
2034
        self.assertEqual('target',
2509
2035
                         preview_tree.get_symlink_target('symlink-id'))
2510
 
 
2511
 
    def test_all_file_ids(self):
2512
 
        tree = self.make_branch_and_tree('tree')
2513
 
        self.build_tree(['tree/a', 'tree/b', 'tree/c'])
2514
 
        tree.add(['a', 'b', 'c'], ['a-id', 'b-id', 'c-id'])
2515
 
        preview = TransformPreview(tree)
2516
 
        self.addCleanup(preview.finalize)
2517
 
        preview.unversion_file(preview.trans_id_file_id('b-id'))
2518
 
        c_trans_id = preview.trans_id_file_id('c-id')
2519
 
        preview.unversion_file(c_trans_id)
2520
 
        preview.version_file('c-id', c_trans_id)
2521
 
        preview_tree = preview.get_preview_tree()
2522
 
        self.assertEqual(set(['a-id', 'c-id', tree.get_root_id()]),
2523
 
                         preview_tree.all_file_ids())
2524
 
 
2525
 
    def test_path2id_deleted_unchanged(self):
2526
 
        tree = self.make_branch_and_tree('tree')
2527
 
        self.build_tree(['tree/unchanged', 'tree/deleted'])
2528
 
        tree.add(['unchanged', 'deleted'], ['unchanged-id', 'deleted-id'])
2529
 
        preview = TransformPreview(tree)
2530
 
        self.addCleanup(preview.finalize)
2531
 
        preview.unversion_file(preview.trans_id_file_id('deleted-id'))
2532
 
        preview_tree = preview.get_preview_tree()
2533
 
        self.assertEqual('unchanged-id', preview_tree.path2id('unchanged'))
2534
 
        self.assertIs(None, preview_tree.path2id('deleted'))
2535
 
 
2536
 
    def test_path2id_created(self):
2537
 
        tree = self.make_branch_and_tree('tree')
2538
 
        self.build_tree(['tree/unchanged'])
2539
 
        tree.add(['unchanged'], ['unchanged-id'])
2540
 
        preview = TransformPreview(tree)
2541
 
        self.addCleanup(preview.finalize)
2542
 
        preview.new_file('new', preview.trans_id_file_id('unchanged-id'),
2543
 
            'contents', 'new-id')
2544
 
        preview_tree = preview.get_preview_tree()
2545
 
        self.assertEqual('new-id', preview_tree.path2id('unchanged/new'))
2546
 
 
2547
 
    def test_path2id_moved(self):
2548
 
        tree = self.make_branch_and_tree('tree')
2549
 
        self.build_tree(['tree/old_parent/', 'tree/old_parent/child'])
2550
 
        tree.add(['old_parent', 'old_parent/child'],
2551
 
                 ['old_parent-id', 'child-id'])
2552
 
        preview = TransformPreview(tree)
2553
 
        self.addCleanup(preview.finalize)
2554
 
        new_parent = preview.new_directory('new_parent', preview.root,
2555
 
                                           'new_parent-id')
2556
 
        preview.adjust_path('child', new_parent,
2557
 
                            preview.trans_id_file_id('child-id'))
2558
 
        preview_tree = preview.get_preview_tree()
2559
 
        self.assertIs(None, preview_tree.path2id('old_parent/child'))
2560
 
        self.assertEqual('child-id', preview_tree.path2id('new_parent/child'))
2561
 
 
2562
 
    def test_path2id_renamed_parent(self):
2563
 
        tree = self.make_branch_and_tree('tree')
2564
 
        self.build_tree(['tree/old_name/', 'tree/old_name/child'])
2565
 
        tree.add(['old_name', 'old_name/child'],
2566
 
                 ['parent-id', 'child-id'])
2567
 
        preview = TransformPreview(tree)
2568
 
        self.addCleanup(preview.finalize)
2569
 
        preview.adjust_path('new_name', preview.root,
2570
 
                            preview.trans_id_file_id('parent-id'))
2571
 
        preview_tree = preview.get_preview_tree()
2572
 
        self.assertIs(None, preview_tree.path2id('old_name/child'))
2573
 
        self.assertEqual('child-id', preview_tree.path2id('new_name/child'))
2574
 
 
2575
 
    def assertMatchingIterEntries(self, tt, specific_file_ids=None):
2576
 
        preview_tree = tt.get_preview_tree()
2577
 
        preview_result = list(preview_tree.iter_entries_by_dir(
2578
 
                              specific_file_ids))
2579
 
        tree = tt._tree
2580
 
        tt.apply()
2581
 
        actual_result = list(tree.iter_entries_by_dir(specific_file_ids))
2582
 
        self.assertEqual(actual_result, preview_result)
2583
 
 
2584
 
    def test_iter_entries_by_dir_new(self):
2585
 
        tree = self.make_branch_and_tree('tree')
2586
 
        tt = TreeTransform(tree)
2587
 
        tt.new_file('new', tt.root, 'contents', 'new-id')
2588
 
        self.assertMatchingIterEntries(tt)
2589
 
 
2590
 
    def test_iter_entries_by_dir_deleted(self):
2591
 
        tree = self.make_branch_and_tree('tree')
2592
 
        self.build_tree(['tree/deleted'])
2593
 
        tree.add('deleted', 'deleted-id')
2594
 
        tt = TreeTransform(tree)
2595
 
        tt.delete_contents(tt.trans_id_file_id('deleted-id'))
2596
 
        self.assertMatchingIterEntries(tt)
2597
 
 
2598
 
    def test_iter_entries_by_dir_unversioned(self):
2599
 
        tree = self.make_branch_and_tree('tree')
2600
 
        self.build_tree(['tree/removed'])
2601
 
        tree.add('removed', 'removed-id')
2602
 
        tt = TreeTransform(tree)
2603
 
        tt.unversion_file(tt.trans_id_file_id('removed-id'))
2604
 
        self.assertMatchingIterEntries(tt)
2605
 
 
2606
 
    def test_iter_entries_by_dir_moved(self):
2607
 
        tree = self.make_branch_and_tree('tree')
2608
 
        self.build_tree(['tree/moved', 'tree/new_parent/'])
2609
 
        tree.add(['moved', 'new_parent'], ['moved-id', 'new_parent-id'])
2610
 
        tt = TreeTransform(tree)
2611
 
        tt.adjust_path('moved', tt.trans_id_file_id('new_parent-id'),
2612
 
                       tt.trans_id_file_id('moved-id'))
2613
 
        self.assertMatchingIterEntries(tt)
2614
 
 
2615
 
    def test_iter_entries_by_dir_specific_file_ids(self):
2616
 
        tree = self.make_branch_and_tree('tree')
2617
 
        tree.set_root_id('tree-root-id')
2618
 
        self.build_tree(['tree/parent/', 'tree/parent/child'])
2619
 
        tree.add(['parent', 'parent/child'], ['parent-id', 'child-id'])
2620
 
        tt = TreeTransform(tree)
2621
 
        self.assertMatchingIterEntries(tt, ['tree-root-id', 'child-id'])
2622
 
 
2623
 
    def test_symlink_content_summary(self):
2624
 
        self.requireFeature(SymlinkFeature)
2625
 
        preview = self.get_empty_preview()
2626
 
        preview.new_symlink('path', preview.root, 'target', 'path-id')
2627
 
        summary = preview.get_preview_tree().path_content_summary('path')
2628
 
        self.assertEqual(('symlink', None, None, 'target'), summary)
2629
 
 
2630
 
    def test_missing_content_summary(self):
2631
 
        preview = self.get_empty_preview()
2632
 
        summary = preview.get_preview_tree().path_content_summary('path')
2633
 
        self.assertEqual(('missing', None, None, None), summary)
2634
 
 
2635
 
    def test_deleted_content_summary(self):
2636
 
        tree = self.make_branch_and_tree('tree')
2637
 
        self.build_tree(['tree/path/'])
2638
 
        tree.add('path')
2639
 
        preview = TransformPreview(tree)
2640
 
        self.addCleanup(preview.finalize)
2641
 
        preview.delete_contents(preview.trans_id_tree_path('path'))
2642
 
        summary = preview.get_preview_tree().path_content_summary('path')
2643
 
        self.assertEqual(('missing', None, None, None), summary)
2644
 
 
2645
 
    def test_file_content_summary_executable(self):
2646
 
        preview = self.get_empty_preview()
2647
 
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
2648
 
        preview.set_executability(True, path_id)
2649
 
        summary = preview.get_preview_tree().path_content_summary('path')
2650
 
        self.assertEqual(4, len(summary))
2651
 
        self.assertEqual('file', summary[0])
2652
 
        # size must be known
2653
 
        self.assertEqual(len('contents'), summary[1])
2654
 
        # executable
2655
 
        self.assertEqual(True, summary[2])
2656
 
        # will not have hash (not cheap to determine)
2657
 
        self.assertIs(None, summary[3])
2658
 
 
2659
 
    def test_change_executability(self):
2660
 
        tree = self.make_branch_and_tree('tree')
2661
 
        self.build_tree(['tree/path'])
2662
 
        tree.add('path')
2663
 
        preview = TransformPreview(tree)
2664
 
        self.addCleanup(preview.finalize)
2665
 
        path_id = preview.trans_id_tree_path('path')
2666
 
        preview.set_executability(True, path_id)
2667
 
        summary = preview.get_preview_tree().path_content_summary('path')
2668
 
        self.assertEqual(True, summary[2])
2669
 
 
2670
 
    def test_file_content_summary_non_exec(self):
2671
 
        preview = self.get_empty_preview()
2672
 
        preview.new_file('path', preview.root, 'contents', 'path-id')
2673
 
        summary = preview.get_preview_tree().path_content_summary('path')
2674
 
        self.assertEqual(4, len(summary))
2675
 
        self.assertEqual('file', summary[0])
2676
 
        # size must be known
2677
 
        self.assertEqual(len('contents'), summary[1])
2678
 
        # not executable
2679
 
        self.assertEqual(False, summary[2])
2680
 
        # will not have hash (not cheap to determine)
2681
 
        self.assertIs(None, summary[3])
2682
 
 
2683
 
    def test_dir_content_summary(self):
2684
 
        preview = self.get_empty_preview()
2685
 
        preview.new_directory('path', preview.root, 'path-id')
2686
 
        summary = preview.get_preview_tree().path_content_summary('path')
2687
 
        self.assertEqual(('directory', None, None, None), summary)
2688
 
 
2689
 
    def test_tree_content_summary(self):
2690
 
        preview = self.get_empty_preview()
2691
 
        path = preview.new_directory('path', preview.root, 'path-id')
2692
 
        preview.set_tree_reference('rev-1', path)
2693
 
        summary = preview.get_preview_tree().path_content_summary('path')
2694
 
        self.assertEqual(4, len(summary))
2695
 
        self.assertEqual('tree-reference', summary[0])
2696
 
 
2697
 
    def test_annotate(self):
2698
 
        tree = self.make_branch_and_tree('tree')
2699
 
        self.build_tree_contents([('tree/file', 'a\n')])
2700
 
        tree.add('file', 'file-id')
2701
 
        tree.commit('a', rev_id='one')
2702
 
        self.build_tree_contents([('tree/file', 'a\nb\n')])
2703
 
        preview = TransformPreview(tree)
2704
 
        self.addCleanup(preview.finalize)
2705
 
        file_trans_id = preview.trans_id_file_id('file-id')
2706
 
        preview.delete_contents(file_trans_id)
2707
 
        preview.create_file('a\nb\nc\n', file_trans_id)
2708
 
        preview_tree = preview.get_preview_tree()
2709
 
        expected = [
2710
 
            ('one', 'a\n'),
2711
 
            ('me:', 'b\n'),
2712
 
            ('me:', 'c\n'),
2713
 
        ]
2714
 
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2715
 
        self.assertEqual(expected, annotation)
2716
 
 
2717
 
    def test_annotate_missing(self):
2718
 
        preview = self.get_empty_preview()
2719
 
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2720
 
        preview_tree = preview.get_preview_tree()
2721
 
        expected = [
2722
 
            ('me:', 'a\n'),
2723
 
            ('me:', 'b\n'),
2724
 
            ('me:', 'c\n'),
2725
 
         ]
2726
 
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2727
 
        self.assertEqual(expected, annotation)
2728
 
 
2729
 
    def test_annotate_rename(self):
2730
 
        tree = self.make_branch_and_tree('tree')
2731
 
        self.build_tree_contents([('tree/file', 'a\n')])
2732
 
        tree.add('file', 'file-id')
2733
 
        tree.commit('a', rev_id='one')
2734
 
        preview = TransformPreview(tree)
2735
 
        self.addCleanup(preview.finalize)
2736
 
        file_trans_id = preview.trans_id_file_id('file-id')
2737
 
        preview.adjust_path('newname', preview.root, file_trans_id)
2738
 
        preview_tree = preview.get_preview_tree()
2739
 
        expected = [
2740
 
            ('one', 'a\n'),
2741
 
        ]
2742
 
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2743
 
        self.assertEqual(expected, annotation)
2744
 
 
2745
 
    def test_annotate_deleted(self):
2746
 
        tree = self.make_branch_and_tree('tree')
2747
 
        self.build_tree_contents([('tree/file', 'a\n')])
2748
 
        tree.add('file', 'file-id')
2749
 
        tree.commit('a', rev_id='one')
2750
 
        self.build_tree_contents([('tree/file', 'a\nb\n')])
2751
 
        preview = TransformPreview(tree)
2752
 
        self.addCleanup(preview.finalize)
2753
 
        file_trans_id = preview.trans_id_file_id('file-id')
2754
 
        preview.delete_contents(file_trans_id)
2755
 
        preview_tree = preview.get_preview_tree()
2756
 
        annotation = preview_tree.annotate_iter('file-id', 'me:')
2757
 
        self.assertIs(None, annotation)
2758
 
 
2759
 
    def test_stored_kind(self):
2760
 
        preview = self.get_empty_preview()
2761
 
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2762
 
        preview_tree = preview.get_preview_tree()
2763
 
        self.assertEqual('file', preview_tree.stored_kind('file-id'))
2764
 
 
2765
 
    def test_is_executable(self):
2766
 
        preview = self.get_empty_preview()
2767
 
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
2768
 
        preview.set_executability(True, preview.trans_id_file_id('file-id'))
2769
 
        preview_tree = preview.get_preview_tree()
2770
 
        self.assertEqual(True, preview_tree.is_executable('file-id'))
2771
 
 
2772
 
    def test_get_set_parent_ids(self):
2773
 
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2774
 
        self.assertEqual([], preview_tree.get_parent_ids())
2775
 
        preview_tree.set_parent_ids(['rev-1'])
2776
 
        self.assertEqual(['rev-1'], preview_tree.get_parent_ids())
2777
 
 
2778
 
    def test_plan_file_merge(self):
2779
 
        work_a = self.make_branch_and_tree('wta')
2780
 
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
2781
 
        work_a.add('file', 'file-id')
2782
 
        base_id = work_a.commit('base version')
2783
 
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
2784
 
        preview = TransformPreview(work_a)
2785
 
        self.addCleanup(preview.finalize)
2786
 
        trans_id = preview.trans_id_file_id('file-id')
2787
 
        preview.delete_contents(trans_id)
2788
 
        preview.create_file('b\nc\nd\ne\n', trans_id)
2789
 
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
2790
 
        tree_a = preview.get_preview_tree()
2791
 
        tree_a.set_parent_ids([base_id])
2792
 
        self.assertEqual([
2793
 
            ('killed-a', 'a\n'),
2794
 
            ('killed-b', 'b\n'),
2795
 
            ('unchanged', 'c\n'),
2796
 
            ('unchanged', 'd\n'),
2797
 
            ('new-a', 'e\n'),
2798
 
            ('new-b', 'f\n'),
2799
 
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
2800
 
 
2801
 
    def test_plan_file_merge_revision_tree(self):
2802
 
        work_a = self.make_branch_and_tree('wta')
2803
 
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
2804
 
        work_a.add('file', 'file-id')
2805
 
        base_id = work_a.commit('base version')
2806
 
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
2807
 
        preview = TransformPreview(work_a.basis_tree())
2808
 
        self.addCleanup(preview.finalize)
2809
 
        trans_id = preview.trans_id_file_id('file-id')
2810
 
        preview.delete_contents(trans_id)
2811
 
        preview.create_file('b\nc\nd\ne\n', trans_id)
2812
 
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
2813
 
        tree_a = preview.get_preview_tree()
2814
 
        tree_a.set_parent_ids([base_id])
2815
 
        self.assertEqual([
2816
 
            ('killed-a', 'a\n'),
2817
 
            ('killed-b', 'b\n'),
2818
 
            ('unchanged', 'c\n'),
2819
 
            ('unchanged', 'd\n'),
2820
 
            ('new-a', 'e\n'),
2821
 
            ('new-b', 'f\n'),
2822
 
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
2823
 
 
2824
 
    def test_walkdirs(self):
2825
 
        preview = self.get_empty_preview()
2826
 
        root = preview.new_directory('', ROOT_PARENT, 'tree-root')
2827
 
        # FIXME: new_directory should mark root.
2828
 
        preview.fixup_new_roots()
2829
 
        preview_tree = preview.get_preview_tree()
2830
 
        file_trans_id = preview.new_file('a', preview.root, 'contents',
2831
 
                                         'a-id')
2832
 
        expected = [(('', 'tree-root'),
2833
 
                    [('a', 'a', 'file', None, 'a-id', 'file')])]
2834
 
        self.assertEqual(expected, list(preview_tree.walkdirs()))
2835
 
 
2836
 
    def test_extras(self):
2837
 
        work_tree = self.make_branch_and_tree('tree')
2838
 
        self.build_tree(['tree/removed-file', 'tree/existing-file',
2839
 
                         'tree/not-removed-file'])
2840
 
        work_tree.add(['removed-file', 'not-removed-file'])
2841
 
        preview = TransformPreview(work_tree)
2842
 
        self.addCleanup(preview.finalize)
2843
 
        preview.new_file('new-file', preview.root, 'contents')
2844
 
        preview.new_file('new-versioned-file', preview.root, 'contents',
2845
 
                         'new-versioned-id')
2846
 
        tree = preview.get_preview_tree()
2847
 
        preview.unversion_file(preview.trans_id_tree_path('removed-file'))
2848
 
        self.assertEqual(set(['new-file', 'removed-file', 'existing-file']),
2849
 
                         set(tree.extras()))
2850
 
 
2851
 
    def test_merge_into_preview(self):
2852
 
        work_tree = self.make_branch_and_tree('tree')
2853
 
        self.build_tree_contents([('tree/file','b\n')])
2854
 
        work_tree.add('file', 'file-id')
2855
 
        work_tree.commit('first commit')
2856
 
        child_tree = work_tree.bzrdir.sprout('child').open_workingtree()
2857
 
        self.build_tree_contents([('child/file','b\nc\n')])
2858
 
        child_tree.commit('child commit')
2859
 
        child_tree.lock_write()
2860
 
        self.addCleanup(child_tree.unlock)
2861
 
        work_tree.lock_write()
2862
 
        self.addCleanup(work_tree.unlock)
2863
 
        preview = TransformPreview(work_tree)
2864
 
        self.addCleanup(preview.finalize)
2865
 
        file_trans_id = preview.trans_id_file_id('file-id')
2866
 
        preview.delete_contents(file_trans_id)
2867
 
        preview.create_file('a\nb\n', file_trans_id)
2868
 
        preview_tree = preview.get_preview_tree()
2869
 
        merger = Merger.from_revision_ids(None, preview_tree,
2870
 
                                          child_tree.branch.last_revision(),
2871
 
                                          other_branch=child_tree.branch,
2872
 
                                          tree_branch=work_tree.branch)
2873
 
        merger.merge_type = Merge3Merger
2874
 
        tt = merger.make_merger().make_preview_transform()
2875
 
        self.addCleanup(tt.finalize)
2876
 
        final_tree = tt.get_preview_tree()
2877
 
        self.assertEqual('a\nb\nc\n', final_tree.get_file_text('file-id'))
2878
 
 
2879
 
    def test_merge_preview_into_workingtree(self):
2880
 
        tree = self.make_branch_and_tree('tree')
2881
 
        tree.set_root_id('TREE_ROOT')
2882
 
        tt = TransformPreview(tree)
2883
 
        self.addCleanup(tt.finalize)
2884
 
        tt.new_file('name', tt.root, 'content', 'file-id')
2885
 
        tree2 = self.make_branch_and_tree('tree2')
2886
 
        tree2.set_root_id('TREE_ROOT')
2887
 
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2888
 
                                         None, tree.basis_tree())
2889
 
        merger.merge_type = Merge3Merger
2890
 
        merger.do_merge()
2891
 
 
2892
 
    def test_merge_preview_into_workingtree_handles_conflicts(self):
2893
 
        tree = self.make_branch_and_tree('tree')
2894
 
        self.build_tree_contents([('tree/foo', 'bar')])
2895
 
        tree.add('foo', 'foo-id')
2896
 
        tree.commit('foo')
2897
 
        tt = TransformPreview(tree)
2898
 
        self.addCleanup(tt.finalize)
2899
 
        trans_id = tt.trans_id_file_id('foo-id')
2900
 
        tt.delete_contents(trans_id)
2901
 
        tt.create_file('baz', trans_id)
2902
 
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
2903
 
        self.build_tree_contents([('tree2/foo', 'qux')])
2904
 
        pb = None
2905
 
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2906
 
                                         pb, tree.basis_tree())
2907
 
        merger.merge_type = Merge3Merger
2908
 
        merger.do_merge()
2909
 
 
2910
 
    def test_is_executable(self):
2911
 
        tree = self.make_branch_and_tree('tree')
2912
 
        preview = TransformPreview(tree)
2913
 
        self.addCleanup(preview.finalize)
2914
 
        preview.new_file('foo', preview.root, 'bar', 'baz-id')
2915
 
        preview_tree = preview.get_preview_tree()
2916
 
        self.assertEqual(False, preview_tree.is_executable('baz-id',
2917
 
                                                           'tree/foo'))
2918
 
        self.assertEqual(False, preview_tree.is_executable('baz-id'))
2919
 
 
2920
 
    def test_commit_preview_tree(self):
2921
 
        tree = self.make_branch_and_tree('tree')
2922
 
        rev_id = tree.commit('rev1')
2923
 
        tree.branch.lock_write()
2924
 
        self.addCleanup(tree.branch.unlock)
2925
 
        tt = TransformPreview(tree)
2926
 
        tt.new_file('file', tt.root, 'contents', 'file_id')
2927
 
        self.addCleanup(tt.finalize)
2928
 
        preview = tt.get_preview_tree()
2929
 
        preview.set_parent_ids([rev_id])
2930
 
        builder = tree.branch.get_commit_builder([rev_id])
2931
 
        list(builder.record_iter_changes(preview, rev_id, tt.iter_changes()))
2932
 
        builder.finish_inventory()
2933
 
        rev2_id = builder.commit('rev2')
2934
 
        rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2935
 
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2936
 
 
2937
 
    def test_ascii_limbo_paths(self):
2938
 
        self.requireFeature(tests.UnicodeFilenameFeature)
2939
 
        branch = self.make_branch('any')
2940
 
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2941
 
        tt = TransformPreview(tree)
2942
 
        self.addCleanup(tt.finalize)
2943
 
        foo_id = tt.new_directory('', ROOT_PARENT)
2944
 
        bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2945
 
        limbo_path = tt._limbo_name(bar_id)
2946
 
        self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
2947
 
 
2948
 
 
2949
 
class FakeSerializer(object):
2950
 
    """Serializer implementation that simply returns the input.
2951
 
 
2952
 
    The input is returned in the order used by pack.ContainerPushParser.
2953
 
    """
2954
 
    @staticmethod
2955
 
    def bytes_record(bytes, names):
2956
 
        return names, bytes
2957
 
 
2958
 
 
2959
 
class TestSerializeTransform(tests.TestCaseWithTransport):
2960
 
 
2961
 
    _test_needs_features = [tests.UnicodeFilenameFeature]
2962
 
 
2963
 
    def get_preview(self, tree=None):
2964
 
        if tree is None:
2965
 
            tree = self.make_branch_and_tree('tree')
2966
 
        tt = TransformPreview(tree)
2967
 
        self.addCleanup(tt.finalize)
2968
 
        return tt
2969
 
 
2970
 
    def assertSerializesTo(self, expected, tt):
2971
 
        records = list(tt.serialize(FakeSerializer()))
2972
 
        self.assertEqual(expected, records)
2973
 
 
2974
 
    @staticmethod
2975
 
    def default_attribs():
2976
 
        return {
2977
 
            '_id_number': 1,
2978
 
            '_new_name': {},
2979
 
            '_new_parent': {},
2980
 
            '_new_executability': {},
2981
 
            '_new_id': {},
2982
 
            '_tree_path_ids': {'': 'new-0'},
2983
 
            '_removed_id': [],
2984
 
            '_removed_contents': [],
2985
 
            '_non_present_ids': {},
2986
 
            }
2987
 
 
2988
 
    def make_records(self, attribs, contents):
2989
 
        records = [
2990
 
            (((('attribs'),),), bencode.bencode(attribs))]
2991
 
        records.extend([(((n, k),), c) for n, k, c in contents])
2992
 
        return records
2993
 
 
2994
 
    def creation_records(self):
2995
 
        attribs = self.default_attribs()
2996
 
        attribs['_id_number'] = 3
2997
 
        attribs['_new_name'] = {
2998
 
            'new-1': u'foo\u1234'.encode('utf-8'), 'new-2': 'qux'}
2999
 
        attribs['_new_id'] = {'new-1': 'baz', 'new-2': 'quxx'}
3000
 
        attribs['_new_parent'] = {'new-1': 'new-0', 'new-2': 'new-0'}
3001
 
        attribs['_new_executability'] = {'new-1': 1}
3002
 
        contents = [
3003
 
            ('new-1', 'file', 'i 1\nbar\n'),
3004
 
            ('new-2', 'directory', ''),
3005
 
            ]
3006
 
        return self.make_records(attribs, contents)
3007
 
 
3008
 
    def test_serialize_creation(self):
3009
 
        tt = self.get_preview()
3010
 
        tt.new_file(u'foo\u1234', tt.root, 'bar', 'baz', True)
3011
 
        tt.new_directory('qux', tt.root, 'quxx')
3012
 
        self.assertSerializesTo(self.creation_records(), tt)
3013
 
 
3014
 
    def test_deserialize_creation(self):
3015
 
        tt = self.get_preview()
3016
 
        tt.deserialize(iter(self.creation_records()))
3017
 
        self.assertEqual(3, tt._id_number)
3018
 
        self.assertEqual({'new-1': u'foo\u1234',
3019
 
                          'new-2': 'qux'}, tt._new_name)
3020
 
        self.assertEqual({'new-1': 'baz', 'new-2': 'quxx'}, tt._new_id)
3021
 
        self.assertEqual({'new-1': tt.root, 'new-2': tt.root}, tt._new_parent)
3022
 
        self.assertEqual({'baz': 'new-1', 'quxx': 'new-2'}, tt._r_new_id)
3023
 
        self.assertEqual({'new-1': True}, tt._new_executability)
3024
 
        self.assertEqual({'new-1': 'file',
3025
 
                          'new-2': 'directory'}, tt._new_contents)
3026
 
        foo_limbo = open(tt._limbo_name('new-1'), 'rb')
3027
 
        try:
3028
 
            foo_content = foo_limbo.read()
3029
 
        finally:
3030
 
            foo_limbo.close()
3031
 
        self.assertEqual('bar', foo_content)
3032
 
 
3033
 
    def symlink_creation_records(self):
3034
 
        attribs = self.default_attribs()
3035
 
        attribs['_id_number'] = 2
3036
 
        attribs['_new_name'] = {'new-1': u'foo\u1234'.encode('utf-8')}
3037
 
        attribs['_new_parent'] = {'new-1': 'new-0'}
3038
 
        contents = [('new-1', 'symlink', u'bar\u1234'.encode('utf-8'))]
3039
 
        return self.make_records(attribs, contents)
3040
 
 
3041
 
    def test_serialize_symlink_creation(self):
3042
 
        self.requireFeature(tests.SymlinkFeature)
3043
 
        tt = self.get_preview()
3044
 
        tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
3045
 
        self.assertSerializesTo(self.symlink_creation_records(), tt)
3046
 
 
3047
 
    def test_deserialize_symlink_creation(self):
3048
 
        self.requireFeature(tests.SymlinkFeature)
3049
 
        tt = self.get_preview()
3050
 
        tt.deserialize(iter(self.symlink_creation_records()))
3051
 
        abspath = tt._limbo_name('new-1')
3052
 
        foo_content = osutils.readlink(abspath)
3053
 
        self.assertEqual(u'bar\u1234', foo_content)
3054
 
 
3055
 
    def make_destruction_preview(self):
3056
 
        tree = self.make_branch_and_tree('.')
3057
 
        self.build_tree([u'foo\u1234', 'bar'])
3058
 
        tree.add([u'foo\u1234', 'bar'], ['foo-id', 'bar-id'])
3059
 
        return self.get_preview(tree)
3060
 
 
3061
 
    def destruction_records(self):
3062
 
        attribs = self.default_attribs()
3063
 
        attribs['_id_number'] = 3
3064
 
        attribs['_removed_id'] = ['new-1']
3065
 
        attribs['_removed_contents'] = ['new-2']
3066
 
        attribs['_tree_path_ids'] = {
3067
 
            '': 'new-0',
3068
 
            u'foo\u1234'.encode('utf-8'): 'new-1',
3069
 
            'bar': 'new-2',
3070
 
            }
3071
 
        return self.make_records(attribs, [])
3072
 
 
3073
 
    def test_serialize_destruction(self):
3074
 
        tt = self.make_destruction_preview()
3075
 
        foo_trans_id = tt.trans_id_tree_file_id('foo-id')
3076
 
        tt.unversion_file(foo_trans_id)
3077
 
        bar_trans_id = tt.trans_id_tree_file_id('bar-id')
3078
 
        tt.delete_contents(bar_trans_id)
3079
 
        self.assertSerializesTo(self.destruction_records(), tt)
3080
 
 
3081
 
    def test_deserialize_destruction(self):
3082
 
        tt = self.make_destruction_preview()
3083
 
        tt.deserialize(iter(self.destruction_records()))
3084
 
        self.assertEqual({u'foo\u1234': 'new-1',
3085
 
                          'bar': 'new-2',
3086
 
                          '': tt.root}, tt._tree_path_ids)
3087
 
        self.assertEqual({'new-1': u'foo\u1234',
3088
 
                          'new-2': 'bar',
3089
 
                          tt.root: ''}, tt._tree_id_paths)
3090
 
        self.assertEqual(set(['new-1']), tt._removed_id)
3091
 
        self.assertEqual(set(['new-2']), tt._removed_contents)
3092
 
 
3093
 
    def missing_records(self):
3094
 
        attribs = self.default_attribs()
3095
 
        attribs['_id_number'] = 2
3096
 
        attribs['_non_present_ids'] = {
3097
 
            'boo': 'new-1',}
3098
 
        return self.make_records(attribs, [])
3099
 
 
3100
 
    def test_serialize_missing(self):
3101
 
        tt = self.get_preview()
3102
 
        boo_trans_id = tt.trans_id_file_id('boo')
3103
 
        self.assertSerializesTo(self.missing_records(), tt)
3104
 
 
3105
 
    def test_deserialize_missing(self):
3106
 
        tt = self.get_preview()
3107
 
        tt.deserialize(iter(self.missing_records()))
3108
 
        self.assertEqual({'boo': 'new-1'}, tt._non_present_ids)
3109
 
 
3110
 
    def make_modification_preview(self):
3111
 
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
3112
 
        LINES_TWO = 'z\nbb\nx\ndd\n'
3113
 
        tree = self.make_branch_and_tree('tree')
3114
 
        self.build_tree_contents([('tree/file', LINES_ONE)])
3115
 
        tree.add('file', 'file-id')
3116
 
        return self.get_preview(tree), LINES_TWO
3117
 
 
3118
 
    def modification_records(self):
3119
 
        attribs = self.default_attribs()
3120
 
        attribs['_id_number'] = 2
3121
 
        attribs['_tree_path_ids'] = {
3122
 
            'file': 'new-1',
3123
 
            '': 'new-0',}
3124
 
        attribs['_removed_contents'] = ['new-1']
3125
 
        contents = [('new-1', 'file',
3126
 
                     'i 1\nz\n\nc 0 1 1 1\ni 1\nx\n\nc 0 3 3 1\n')]
3127
 
        return self.make_records(attribs, contents)
3128
 
 
3129
 
    def test_serialize_modification(self):
3130
 
        tt, LINES = self.make_modification_preview()
3131
 
        trans_id = tt.trans_id_file_id('file-id')
3132
 
        tt.delete_contents(trans_id)
3133
 
        tt.create_file(LINES, trans_id)
3134
 
        self.assertSerializesTo(self.modification_records(), tt)
3135
 
 
3136
 
    def test_deserialize_modification(self):
3137
 
        tt, LINES = self.make_modification_preview()
3138
 
        tt.deserialize(iter(self.modification_records()))
3139
 
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3140
 
 
3141
 
    def make_kind_change_preview(self):
3142
 
        LINES = 'a\nb\nc\nd\n'
3143
 
        tree = self.make_branch_and_tree('tree')
3144
 
        self.build_tree(['tree/foo/'])
3145
 
        tree.add('foo', 'foo-id')
3146
 
        return self.get_preview(tree), LINES
3147
 
 
3148
 
    def kind_change_records(self):
3149
 
        attribs = self.default_attribs()
3150
 
        attribs['_id_number'] = 2
3151
 
        attribs['_tree_path_ids'] = {
3152
 
            'foo': 'new-1',
3153
 
            '': 'new-0',}
3154
 
        attribs['_removed_contents'] = ['new-1']
3155
 
        contents = [('new-1', 'file',
3156
 
                     'i 4\na\nb\nc\nd\n\n')]
3157
 
        return self.make_records(attribs, contents)
3158
 
 
3159
 
    def test_serialize_kind_change(self):
3160
 
        tt, LINES = self.make_kind_change_preview()
3161
 
        trans_id = tt.trans_id_file_id('foo-id')
3162
 
        tt.delete_contents(trans_id)
3163
 
        tt.create_file(LINES, trans_id)
3164
 
        self.assertSerializesTo(self.kind_change_records(), tt)
3165
 
 
3166
 
    def test_deserialize_kind_change(self):
3167
 
        tt, LINES = self.make_kind_change_preview()
3168
 
        tt.deserialize(iter(self.kind_change_records()))
3169
 
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3170
 
 
3171
 
    def make_add_contents_preview(self):
3172
 
        LINES = 'a\nb\nc\nd\n'
3173
 
        tree = self.make_branch_and_tree('tree')
3174
 
        self.build_tree(['tree/foo'])
3175
 
        tree.add('foo')
3176
 
        os.unlink('tree/foo')
3177
 
        return self.get_preview(tree), LINES
3178
 
 
3179
 
    def add_contents_records(self):
3180
 
        attribs = self.default_attribs()
3181
 
        attribs['_id_number'] = 2
3182
 
        attribs['_tree_path_ids'] = {
3183
 
            'foo': 'new-1',
3184
 
            '': 'new-0',}
3185
 
        contents = [('new-1', 'file',
3186
 
                     'i 4\na\nb\nc\nd\n\n')]
3187
 
        return self.make_records(attribs, contents)
3188
 
 
3189
 
    def test_serialize_add_contents(self):
3190
 
        tt, LINES = self.make_add_contents_preview()
3191
 
        trans_id = tt.trans_id_tree_path('foo')
3192
 
        tt.create_file(LINES, trans_id)
3193
 
        self.assertSerializesTo(self.add_contents_records(), tt)
3194
 
 
3195
 
    def test_deserialize_add_contents(self):
3196
 
        tt, LINES = self.make_add_contents_preview()
3197
 
        tt.deserialize(iter(self.add_contents_records()))
3198
 
        self.assertFileEqual(LINES, tt._limbo_name('new-1'))
3199
 
 
3200
 
    def test_get_parents_lines(self):
3201
 
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
3202
 
        LINES_TWO = 'z\nbb\nx\ndd\n'
3203
 
        tree = self.make_branch_and_tree('tree')
3204
 
        self.build_tree_contents([('tree/file', LINES_ONE)])
3205
 
        tree.add('file', 'file-id')
3206
 
        tt = self.get_preview(tree)
3207
 
        trans_id = tt.trans_id_tree_path('file')
3208
 
        self.assertEqual((['aa\n', 'bb\n', 'cc\n', 'dd\n'],),
3209
 
            tt._get_parents_lines(trans_id))
3210
 
 
3211
 
    def test_get_parents_texts(self):
3212
 
        LINES_ONE = 'aa\nbb\ncc\ndd\n'
3213
 
        LINES_TWO = 'z\nbb\nx\ndd\n'
3214
 
        tree = self.make_branch_and_tree('tree')
3215
 
        self.build_tree_contents([('tree/file', LINES_ONE)])
3216
 
        tree.add('file', 'file-id')
3217
 
        tt = self.get_preview(tree)
3218
 
        trans_id = tt.trans_id_tree_path('file')
3219
 
        self.assertEqual((LINES_ONE,),
3220
 
            tt._get_parents_texts(trans_id))