~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: John Arbash Meinel
  • Date: 2008-08-18 22:34:21 UTC
  • mto: (3606.5.6 1.6)
  • mto: This revision was merged to the branch mainline in revision 3641.
  • Revision ID: john@arbash-meinel.com-20080818223421-todjny24vj4faj4t
Add tests for the fetching behavior.

The proper parameter passed is 'unordered' add an assert for it, and
fix callers that were passing 'unsorted' instead.
Add tests that we make the right get_record_stream call based
on the value of _fetch_uses_deltas.
Fix the fetch request for signatures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
2
 
 
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
import os
 
18
import stat
 
19
from StringIO import StringIO
 
20
import sys
18
21
 
 
22
from bzrlib import (
 
23
    errors,
 
24
    generate_ids,
 
25
    osutils,
 
26
    progress,
 
27
    revision as _mod_revision,
 
28
    symbol_versioning,
 
29
    tests,
 
30
    urlutils,
 
31
    )
19
32
from bzrlib.bzrdir import BzrDir
20
33
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
21
 
                              UnversionedParent, ParentLoop)
 
34
                              UnversionedParent, ParentLoop, DeletingParent,
 
35
                              NonDirectoryParent)
 
36
from bzrlib.diff import show_diff_trees
22
37
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
23
 
                           ReusingTransform, CantMoveRoot, NotVersionedError,
24
 
                           ExistingLimbo, ImmortalLimbo, LockError)
25
 
from bzrlib.osutils import file_kind, has_symlinks, pathjoin
 
38
                           ReusingTransform, CantMoveRoot, 
 
39
                           PathsNotVersionedError, ExistingLimbo,
 
40
                           ExistingPendingDeletion, ImmortalLimbo,
 
41
                           ImmortalPendingDeletion, LockError)
 
42
from bzrlib.osutils import file_kind, pathjoin
26
43
from bzrlib.merge import Merge3Merger
27
 
from bzrlib.tests import TestCaseInTempDir, TestSkipped, TestCase
 
44
from bzrlib.tests import (
 
45
    CaseInsensitiveFilesystemFeature,
 
46
    HardlinkFeature,
 
47
    SymlinkFeature,
 
48
    TestCase,
 
49
    TestCaseInTempDir,
 
50
    TestSkipped,
 
51
    )
28
52
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths, 
29
53
                              resolve_conflicts, cook_conflicts, 
30
 
                              find_interesting, build_tree, get_backup_name)
31
 
import bzrlib.urlutils as urlutils
 
54
                              build_tree, get_backup_name,
 
55
                              _FileMover, resolve_checkout,
 
56
                              TransformPreview)
32
57
 
33
 
class TestTreeTransform(TestCaseInTempDir):
 
58
class TestTreeTransform(tests.TestCaseWithTransport):
34
59
 
35
60
    def setUp(self):
36
61
        super(TestTreeTransform, self).setUp()
37
 
        self.wt = BzrDir.create_standalone_workingtree('.')
 
62
        self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
38
63
        os.chdir('..')
39
64
 
40
65
    def get_transform(self):
41
66
        transform = TreeTransform(self.wt)
42
 
        #self.addCleanup(transform.finalize)
43
 
        return transform, transform.trans_id_tree_file_id(self.wt.get_root_id())
 
67
        self.addCleanup(transform.finalize)
 
68
        return transform, transform.root
44
69
 
45
70
    def test_existing_limbo(self):
46
 
        limbo_name = urlutils.local_path_from_url(
47
 
            self.wt._control_files.controlfilename('limbo'))
48
71
        transform, root = self.get_transform()
 
72
        limbo_name = transform._limbodir
 
73
        deletion_path = transform._deletiondir
49
74
        os.mkdir(pathjoin(limbo_name, 'hehe'))
50
75
        self.assertRaises(ImmortalLimbo, transform.apply)
51
76
        self.assertRaises(LockError, self.wt.unlock)
53
78
        self.assertRaises(LockError, self.wt.unlock)
54
79
        os.rmdir(pathjoin(limbo_name, 'hehe'))
55
80
        os.rmdir(limbo_name)
 
81
        os.rmdir(deletion_path)
56
82
        transform, root = self.get_transform()
57
83
        transform.apply()
58
84
 
 
85
    def test_existing_pending_deletion(self):
 
86
        transform, root = self.get_transform()
 
87
        deletion_path = self._limbodir = urlutils.local_path_from_url(
 
88
            transform._tree._transport.abspath('pending-deletion'))
 
89
        os.mkdir(pathjoin(deletion_path, 'blocking-directory'))
 
90
        self.assertRaises(ImmortalPendingDeletion, transform.apply)
 
91
        self.assertRaises(LockError, self.wt.unlock)
 
92
        self.assertRaises(ExistingPendingDeletion, self.get_transform)
 
93
 
59
94
    def test_build(self):
60
 
        transform, root = self.get_transform() 
 
95
        transform, root = self.get_transform()
 
96
        self.wt.lock_tree_write()
 
97
        self.addCleanup(self.wt.unlock)
61
98
        self.assertIs(transform.get_tree_parent(root), ROOT_PARENT)
62
99
        imaginary_id = transform.trans_id_tree_path('imaginary')
63
100
        imaginary_id2 = transform.trans_id_tree_path('imaginary/')
98
135
        transform.finalize()
99
136
        transform.finalize()
100
137
 
 
138
    def test_hardlink(self):
 
139
        self.requireFeature(HardlinkFeature)
 
140
        transform, root = self.get_transform()
 
141
        transform.new_file('file1', root, 'contents')
 
142
        transform.apply()
 
143
        target = self.make_branch_and_tree('target')
 
144
        target_transform = TreeTransform(target)
 
145
        trans_id = target_transform.create_path('file1', target_transform.root)
 
146
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
 
147
        target_transform.apply()
 
148
        self.failUnlessExists('target/file1')
 
149
        source_stat = os.stat(self.wt.abspath('file1'))
 
150
        target_stat = os.stat('target/file1')
 
151
        self.assertEqual(source_stat, target_stat)
 
152
 
101
153
    def test_convenience(self):
102
154
        transform, root = self.get_transform()
 
155
        self.wt.lock_tree_write()
 
156
        self.addCleanup(self.wt.unlock)
103
157
        trans_id = transform.new_file('name', root, 'contents', 
104
158
                                      'my_pretties', True)
105
159
        oz = transform.new_directory('oz', root, 'oz-id')
117
171
        self.assertEqual(self.wt.path2id('oz/dorothy'), 'dorothy-id')
118
172
        self.assertEqual(self.wt.path2id('oz/dorothy/toto'), 'toto-id')
119
173
 
120
 
        self.assertEqual('toto-contents', 
 
174
        self.assertEqual('toto-contents',
121
175
                         self.wt.get_file_byname('oz/dorothy/toto').read())
122
176
        self.assertIs(self.wt.is_executable('toto-id'), False)
123
177
 
 
178
    def test_tree_reference(self):
 
179
        transform, root = self.get_transform()
 
180
        tree = transform._tree
 
181
        trans_id = transform.new_directory('reference', root, 'subtree-id')
 
182
        transform.set_tree_reference('subtree-revision', trans_id)
 
183
        transform.apply()
 
184
        tree.lock_read()
 
185
        self.addCleanup(tree.unlock)
 
186
        self.assertEqual('subtree-revision',
 
187
                         tree.inventory['subtree-id'].reference_revision)
 
188
 
124
189
    def test_conflicts(self):
125
190
        transform, root = self.get_transform()
126
191
        trans_id = transform.new_file('name', root, 'contents', 
190
255
        transform3.delete_contents(oz_id)
191
256
        self.assertEqual(transform3.find_conflicts(), 
192
257
                         [('missing parent', oz_id)])
193
 
        root_id = transform3.trans_id_tree_file_id('TREE_ROOT')
 
258
        root_id = transform3.root
194
259
        tip_id = transform3.trans_id_tree_file_id('tip-id')
195
260
        transform3.adjust_path('tip', root_id, tip_id)
196
261
        transform3.apply()
197
262
 
 
263
    def test_conflict_on_case_insensitive(self):
 
264
        tree = self.make_branch_and_tree('tree')
 
265
        # Don't try this at home, kids!
 
266
        # Force the tree to report that it is case sensitive, for conflict
 
267
        # resolution tests
 
268
        tree.case_sensitive = True
 
269
        transform = TreeTransform(tree)
 
270
        self.addCleanup(transform.finalize)
 
271
        transform.new_file('file', transform.root, 'content')
 
272
        transform.new_file('FiLe', transform.root, 'content')
 
273
        result = transform.find_conflicts()
 
274
        self.assertEqual([], result)
 
275
        transform.finalize()
 
276
        # Force the tree to report that it is case insensitive, for conflict
 
277
        # generation tests
 
278
        tree.case_sensitive = False
 
279
        transform = TreeTransform(tree)
 
280
        self.addCleanup(transform.finalize)
 
281
        transform.new_file('file', transform.root, 'content')
 
282
        transform.new_file('FiLe', transform.root, 'content')
 
283
        result = transform.find_conflicts()
 
284
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
 
285
 
 
286
    def test_conflict_on_case_insensitive_existing(self):
 
287
        tree = self.make_branch_and_tree('tree')
 
288
        self.build_tree(['tree/FiLe'])
 
289
        # Don't try this at home, kids!
 
290
        # Force the tree to report that it is case sensitive, for conflict
 
291
        # resolution tests
 
292
        tree.case_sensitive = True
 
293
        transform = TreeTransform(tree)
 
294
        self.addCleanup(transform.finalize)
 
295
        transform.new_file('file', transform.root, 'content')
 
296
        result = transform.find_conflicts()
 
297
        self.assertEqual([], result)
 
298
        transform.finalize()
 
299
        # Force the tree to report that it is case insensitive, for conflict
 
300
        # generation tests
 
301
        tree.case_sensitive = False
 
302
        transform = TreeTransform(tree)
 
303
        self.addCleanup(transform.finalize)
 
304
        transform.new_file('file', transform.root, 'content')
 
305
        result = transform.find_conflicts()
 
306
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
 
307
 
 
308
    def test_resolve_case_insensitive_conflict(self):
 
309
        tree = self.make_branch_and_tree('tree')
 
310
        # Don't try this at home, kids!
 
311
        # Force the tree to report that it is case insensitive, for conflict
 
312
        # resolution tests
 
313
        tree.case_sensitive = False
 
314
        transform = TreeTransform(tree)
 
315
        self.addCleanup(transform.finalize)
 
316
        transform.new_file('file', transform.root, 'content')
 
317
        transform.new_file('FiLe', transform.root, 'content')
 
318
        resolve_conflicts(transform)
 
319
        transform.apply()
 
320
        self.failUnlessExists('tree/file')
 
321
        self.failUnlessExists('tree/FiLe.moved')
 
322
 
 
323
    def test_resolve_checkout_case_conflict(self):
 
324
        tree = self.make_branch_and_tree('tree')
 
325
        # Don't try this at home, kids!
 
326
        # Force the tree to report that it is case insensitive, for conflict
 
327
        # resolution tests
 
328
        tree.case_sensitive = False
 
329
        transform = TreeTransform(tree)
 
330
        self.addCleanup(transform.finalize)
 
331
        transform.new_file('file', transform.root, 'content')
 
332
        transform.new_file('FiLe', transform.root, 'content')
 
333
        resolve_conflicts(transform,
 
334
                          pass_func=lambda t, c: resolve_checkout(t, c, []))
 
335
        transform.apply()
 
336
        self.failUnlessExists('tree/file')
 
337
        self.failUnlessExists('tree/FiLe.moved')
 
338
 
 
339
    def test_apply_case_conflict(self):
 
340
        """Ensure that a transform with case conflicts can always be applied"""
 
341
        tree = self.make_branch_and_tree('tree')
 
342
        transform = TreeTransform(tree)
 
343
        self.addCleanup(transform.finalize)
 
344
        transform.new_file('file', transform.root, 'content')
 
345
        transform.new_file('FiLe', transform.root, 'content')
 
346
        dir = transform.new_directory('dir', transform.root)
 
347
        transform.new_file('dirfile', dir, 'content')
 
348
        transform.new_file('dirFiLe', dir, 'content')
 
349
        resolve_conflicts(transform)
 
350
        transform.apply()
 
351
        self.failUnlessExists('tree/file')
 
352
        if not os.path.exists('tree/FiLe.moved'):
 
353
            self.failUnlessExists('tree/FiLe')
 
354
        self.failUnlessExists('tree/dir/dirfile')
 
355
        if not os.path.exists('tree/dir/dirFiLe.moved'):
 
356
            self.failUnlessExists('tree/dir/dirFiLe')
 
357
 
 
358
    def test_case_insensitive_limbo(self):
 
359
        tree = self.make_branch_and_tree('tree')
 
360
        # Don't try this at home, kids!
 
361
        # Force the tree to report that it is case insensitive
 
362
        tree.case_sensitive = False
 
363
        transform = TreeTransform(tree)
 
364
        self.addCleanup(transform.finalize)
 
365
        dir = transform.new_directory('dir', transform.root)
 
366
        first = transform.new_file('file', dir, 'content')
 
367
        second = transform.new_file('FiLe', dir, 'content')
 
368
        self.assertContainsRe(transform._limbo_name(first), 'new-1/file')
 
369
        self.assertNotContainsRe(transform._limbo_name(second), 'new-1/FiLe')
 
370
 
198
371
    def test_add_del(self):
199
372
        start, root = self.get_transform()
200
373
        start.new_directory('a', root, 'a')
222
395
    def test_name_invariants(self):
223
396
        create_tree, root = self.get_transform()
224
397
        # prepare tree
225
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
398
        root = create_tree.root
226
399
        create_tree.new_file('name1', root, 'hello1', 'name1')
227
400
        create_tree.new_file('name2', root, 'hello2', 'name2')
228
401
        ddir = create_tree.new_directory('dying_directory', root, 'ddir')
232
405
        create_tree.apply()
233
406
 
234
407
        mangle_tree,root = self.get_transform()
235
 
        root = mangle_tree.trans_id_tree_file_id('TREE_ROOT')
 
408
        root = mangle_tree.root
236
409
        #swap names
237
410
        name1 = mangle_tree.trans_id_tree_file_id('name1')
238
411
        name2 = mangle_tree.trans_id_tree_file_id('name2')
317
490
    def test_move_dangling_ie(self):
318
491
        create_tree, root = self.get_transform()
319
492
        # prepare tree
320
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
493
        root = create_tree.root
321
494
        create_tree.new_file('name1', root, 'hello1', 'name1')
322
495
        create_tree.apply()
323
496
        delete_contents, root = self.get_transform()
333
506
    def test_replace_dangling_ie(self):
334
507
        create_tree, root = self.get_transform()
335
508
        # prepare tree
336
 
        root = create_tree.trans_id_tree_file_id('TREE_ROOT')
 
509
        root = create_tree.root
337
510
        create_tree.new_file('name1', root, 'hello1', 'name1')
338
511
        create_tree.apply()
339
512
        delete_contents = TreeTransform(self.wt)
352
525
        replace.apply()
353
526
 
354
527
    def test_symlinks(self):
355
 
        if not has_symlinks():
356
 
            raise TestSkipped('Symlinks are not supported on this platform')
 
528
        self.requireFeature(SymlinkFeature)
357
529
        transform,root = self.get_transform()
358
530
        oz_id = transform.new_directory('oz', root, 'oz-id')
359
531
        wizard = transform.new_symlink('wizard', oz_id, 'wizard-target', 
373
545
        self.assertEqual(os.readlink(self.wt.abspath('oz/wizard')),
374
546
                         'wizard-target')
375
547
 
 
548
    def test_unable_create_symlink(self):
 
549
        def tt_helper():
 
550
            wt = self.make_branch_and_tree('.')
 
551
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
552
            try:
 
553
                tt.new_symlink('foo', tt.root, 'bar')
 
554
                tt.apply()
 
555
            finally:
 
556
                wt.unlock()
 
557
        os_symlink = getattr(os, 'symlink', None)
 
558
        os.symlink = None
 
559
        try:
 
560
            err = self.assertRaises(errors.UnableCreateSymlink, tt_helper)
 
561
            self.assertEquals(
 
562
                "Unable to create symlink 'foo' on this platform",
 
563
                str(err))
 
564
        finally:
 
565
            if os_symlink:
 
566
                os.symlink = os_symlink
376
567
 
377
568
    def get_conflicted(self):
378
569
        create,root = self.get_transform()
386
577
                                         'dorothy-id')
387
578
        old_dorothy = conflicts.trans_id_tree_file_id('dorothy-id')
388
579
        oz = conflicts.trans_id_tree_file_id('oz-id')
389
 
        # set up missing, unversioned parent
 
580
        # set up DeletedParent parent conflict
390
581
        conflicts.delete_versioned(oz)
391
582
        emerald = conflicts.trans_id_tree_file_id('emerald-id')
 
583
        # set up MissingParent conflict
 
584
        munchkincity = conflicts.trans_id_file_id('munchkincity-id')
 
585
        conflicts.adjust_path('munchkincity', root, munchkincity)
 
586
        conflicts.new_directory('auntem', munchkincity, 'auntem-id')
392
587
        # set up parent loop
393
588
        conflicts.adjust_path('emeraldcity', emerald, emerald)
394
589
        return conflicts, emerald, oz, old_dorothy, new_dorothy
415
610
                                   'dorothy.moved', 'dorothy', None,
416
611
                                   'dorothy-id')
417
612
        self.assertEqual(cooked_conflicts[1], duplicate_id)
418
 
        missing_parent = MissingParent('Not deleting', 'oz', 'oz-id')
 
613
        missing_parent = MissingParent('Created directory', 'munchkincity',
 
614
                                       'munchkincity-id')
 
615
        deleted_parent = DeletingParent('Not deleting', 'oz', 'oz-id')
419
616
        self.assertEqual(cooked_conflicts[2], missing_parent)
420
 
        unversioned_parent = UnversionedParent('Versioned directory', 'oz',
 
617
        unversioned_parent = UnversionedParent('Versioned directory',
 
618
                                               'munchkincity',
 
619
                                               'munchkincity-id')
 
620
        unversioned_parent2 = UnversionedParent('Versioned directory', 'oz',
421
621
                                               'oz-id')
422
622
        self.assertEqual(cooked_conflicts[3], unversioned_parent)
423
623
        parent_loop = ParentLoop('Cancelled move', 'oz/emeraldcity', 
424
624
                                 'oz/emeraldcity', 'emerald-id', 'emerald-id')
425
 
        self.assertEqual(cooked_conflicts[4], parent_loop)
426
 
        self.assertEqual(len(cooked_conflicts), 5)
 
625
        self.assertEqual(cooked_conflicts[4], deleted_parent)
 
626
        self.assertEqual(cooked_conflicts[5], unversioned_parent2)
 
627
        self.assertEqual(cooked_conflicts[6], parent_loop)
 
628
        self.assertEqual(len(cooked_conflicts), 7)
427
629
        tt.finalize()
428
630
 
429
631
    def test_string_conflicts(self):
439
641
        self.assertEqual(conflicts_s[1], 'Conflict adding id to dorothy.  '
440
642
                                         'Unversioned existing file '
441
643
                                         'dorothy.moved.')
442
 
        self.assertEqual(conflicts_s[2], 'Conflict adding files to oz.  '
443
 
                                         'Not deleting.')
444
 
        self.assertEqual(conflicts_s[3], 'Conflict adding versioned files to '
445
 
                                         'oz.  Versioned directory.')
446
 
        self.assertEqual(conflicts_s[4], 'Conflict moving oz/emeraldcity into'
 
644
        self.assertEqual(conflicts_s[2], 'Conflict adding files to'
 
645
                                         ' munchkincity.  Created directory.')
 
646
        self.assertEqual(conflicts_s[3], 'Conflict because munchkincity is not'
 
647
                                         ' versioned, but has versioned'
 
648
                                         ' children.  Versioned directory.')
 
649
        self.assertEqualDiff(conflicts_s[4], "Conflict: can't delete oz because it"
 
650
                                         " is not empty.  Not deleting.")
 
651
        self.assertEqual(conflicts_s[5], 'Conflict because oz is not'
 
652
                                         ' versioned, but has versioned'
 
653
                                         ' children.  Versioned directory.')
 
654
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
447
655
                                         ' oz/emeraldcity.  Cancelled move.')
448
656
 
 
657
    def prepare_wrong_parent_kind(self):
 
658
        tt, root = self.get_transform()
 
659
        tt.new_file('parent', root, 'contents', 'parent-id')
 
660
        tt.apply()
 
661
        tt, root = self.get_transform()
 
662
        parent_id = tt.trans_id_file_id('parent-id')
 
663
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
 
664
        return tt
 
665
 
 
666
    def test_find_conflicts_wrong_parent_kind(self):
 
667
        tt = self.prepare_wrong_parent_kind()
 
668
        tt.find_conflicts()
 
669
 
 
670
    def test_resolve_conflicts_wrong_existing_parent_kind(self):
 
671
        tt = self.prepare_wrong_parent_kind()
 
672
        raw_conflicts = resolve_conflicts(tt)
 
673
        self.assertEqual(set([('non-directory parent', 'Created directory',
 
674
                         'new-3')]), raw_conflicts)
 
675
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
 
676
        self.assertEqual([NonDirectoryParent('Created directory', 'parent.new',
 
677
        'parent-id')], cooked_conflicts)
 
678
        tt.apply()
 
679
        self.assertEqual(None, self.wt.path2id('parent'))
 
680
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
 
681
 
 
682
    def test_resolve_conflicts_wrong_new_parent_kind(self):
 
683
        tt, root = self.get_transform()
 
684
        parent_id = tt.new_directory('parent', root, 'parent-id')
 
685
        tt.new_file('child,', parent_id, 'contents2', 'file-id')
 
686
        tt.apply()
 
687
        tt, root = self.get_transform()
 
688
        parent_id = tt.trans_id_file_id('parent-id')
 
689
        tt.delete_contents(parent_id)
 
690
        tt.create_file('contents', parent_id)
 
691
        raw_conflicts = resolve_conflicts(tt)
 
692
        self.assertEqual(set([('non-directory parent', 'Created directory',
 
693
                         'new-3')]), raw_conflicts)
 
694
        tt.apply()
 
695
        self.assertEqual(None, self.wt.path2id('parent'))
 
696
        self.assertEqual('parent-id', self.wt.path2id('parent.new'))
 
697
 
 
698
    def test_resolve_conflicts_wrong_parent_kind_unversioned(self):
 
699
        tt, root = self.get_transform()
 
700
        parent_id = tt.new_directory('parent', root)
 
701
        tt.new_file('child,', parent_id, 'contents2')
 
702
        tt.apply()
 
703
        tt, root = self.get_transform()
 
704
        parent_id = tt.trans_id_tree_path('parent')
 
705
        tt.delete_contents(parent_id)
 
706
        tt.create_file('contents', parent_id)
 
707
        resolve_conflicts(tt)
 
708
        tt.apply()
 
709
        self.assertIs(None, self.wt.path2id('parent'))
 
710
        self.assertIs(None, self.wt.path2id('parent.new'))
 
711
 
449
712
    def test_moving_versioned_directories(self):
450
713
        create, root = self.get_transform()
451
714
        kansas = create.new_directory('kansas', root, 'kansas-id')
484
747
        rename.set_executability(True, myfile)
485
748
        rename.apply()
486
749
 
487
 
    def test_find_interesting(self):
488
 
        create, root = self.get_transform()
489
 
        wt = create._tree
490
 
        create.new_file('vfile', root, 'myfile-text', 'myfile-id')
491
 
        create.new_file('uvfile', root, 'othertext')
492
 
        create.apply()
493
 
        self.assertEqual(find_interesting(wt, wt, ['vfile']),
494
 
                         set(['myfile-id']))
495
 
        self.assertRaises(NotVersionedError, find_interesting, wt, wt,
496
 
                          ['uvfile'])
497
 
 
498
750
    def test_set_executability_order(self):
499
751
        """Ensure that executability behaves the same, no matter what order.
500
752
        
506
758
        """
507
759
        transform, root = self.get_transform()
508
760
        wt = transform._tree
 
761
        wt.lock_read()
 
762
        self.addCleanup(wt.unlock)
509
763
        transform.new_file('set_on_creation', root, 'Set on creation', 'soc',
510
764
                           True)
511
 
        sac = transform.new_file('set_after_creation', root, 'Set after creation', 'sac')
 
765
        sac = transform.new_file('set_after_creation', root,
 
766
                                 'Set after creation', 'sac')
512
767
        transform.set_executability(True, sac)
513
 
        uws = transform.new_file('unset_without_set', root, 'Unset badly', 'uws')
 
768
        uws = transform.new_file('unset_without_set', root, 'Unset badly',
 
769
                                 'uws')
514
770
        self.assertRaises(KeyError, transform.set_executability, None, uws)
515
771
        transform.apply()
516
772
        self.assertTrue(wt.is_executable('soc'))
517
773
        self.assertTrue(wt.is_executable('sac'))
518
774
 
 
775
    def test_preserve_mode(self):
 
776
        """File mode is preserved when replacing content"""
 
777
        if sys.platform == 'win32':
 
778
            raise TestSkipped('chmod has no effect on win32')
 
779
        transform, root = self.get_transform()
 
780
        transform.new_file('file1', root, 'contents', 'file1-id', True)
 
781
        transform.apply()
 
782
        self.wt.lock_write()
 
783
        self.addCleanup(self.wt.unlock)
 
784
        self.assertTrue(self.wt.is_executable('file1-id'))
 
785
        transform, root = self.get_transform()
 
786
        file1_id = transform.trans_id_tree_file_id('file1-id')
 
787
        transform.delete_contents(file1_id)
 
788
        transform.create_file('contents2', file1_id)
 
789
        transform.apply()
 
790
        self.assertTrue(self.wt.is_executable('file1-id'))
 
791
 
 
792
    def test__set_mode_stats_correctly(self):
 
793
        """_set_mode stats to determine file mode."""
 
794
        if sys.platform == 'win32':
 
795
            raise TestSkipped('chmod has no effect on win32')
 
796
 
 
797
        stat_paths = []
 
798
        real_stat = os.stat
 
799
        def instrumented_stat(path):
 
800
            stat_paths.append(path)
 
801
            return real_stat(path)
 
802
 
 
803
        transform, root = self.get_transform()
 
804
 
 
805
        bar1_id = transform.new_file('bar', root, 'bar contents 1\n',
 
806
                                     file_id='bar-id-1', executable=False)
 
807
        transform.apply()
 
808
 
 
809
        transform, root = self.get_transform()
 
810
        bar1_id = transform.trans_id_tree_path('bar')
 
811
        bar2_id = transform.trans_id_tree_path('bar2')
 
812
        try:
 
813
            os.stat = instrumented_stat
 
814
            transform.create_file('bar2 contents\n', bar2_id, mode_id=bar1_id)
 
815
        finally:
 
816
            os.stat = real_stat
 
817
            transform.finalize()
 
818
 
 
819
        bar1_abspath = self.wt.abspath('bar')
 
820
        self.assertEqual([bar1_abspath], stat_paths)
 
821
 
 
822
    def test_iter_changes(self):
 
823
        self.wt.set_root_id('eert_toor')
 
824
        transform, root = self.get_transform()
 
825
        transform.new_file('old', root, 'blah', 'id-1', True)
 
826
        transform.apply()
 
827
        transform, root = self.get_transform()
 
828
        try:
 
829
            self.assertEqual([], list(transform.iter_changes()))
 
830
            old = transform.trans_id_tree_file_id('id-1')
 
831
            transform.unversion_file(old)
 
832
            self.assertEqual([('id-1', ('old', None), False, (True, False),
 
833
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
834
                (True, True))], list(transform.iter_changes()))
 
835
            transform.new_directory('new', root, 'id-1')
 
836
            self.assertEqual([('id-1', ('old', 'new'), True, (True, True),
 
837
                ('eert_toor', 'eert_toor'), ('old', 'new'),
 
838
                ('file', 'directory'),
 
839
                (True, False))], list(transform.iter_changes()))
 
840
        finally:
 
841
            transform.finalize()
 
842
 
 
843
    def test_iter_changes_new(self):
 
844
        self.wt.set_root_id('eert_toor')
 
845
        transform, root = self.get_transform()
 
846
        transform.new_file('old', root, 'blah')
 
847
        transform.apply()
 
848
        transform, root = self.get_transform()
 
849
        try:
 
850
            old = transform.trans_id_tree_path('old')
 
851
            transform.version_file('id-1', old)
 
852
            self.assertEqual([('id-1', (None, 'old'), False, (False, True),
 
853
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
854
                (False, False))], list(transform.iter_changes()))
 
855
        finally:
 
856
            transform.finalize()
 
857
 
 
858
    def test_iter_changes_modifications(self):
 
859
        self.wt.set_root_id('eert_toor')
 
860
        transform, root = self.get_transform()
 
861
        transform.new_file('old', root, 'blah', 'id-1')
 
862
        transform.new_file('new', root, 'blah')
 
863
        transform.new_directory('subdir', root, 'subdir-id')
 
864
        transform.apply()
 
865
        transform, root = self.get_transform()
 
866
        try:
 
867
            old = transform.trans_id_tree_path('old')
 
868
            subdir = transform.trans_id_tree_file_id('subdir-id')
 
869
            new = transform.trans_id_tree_path('new')
 
870
            self.assertEqual([], list(transform.iter_changes()))
 
871
 
 
872
            #content deletion
 
873
            transform.delete_contents(old)
 
874
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
875
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', None),
 
876
                (False, False))], list(transform.iter_changes()))
 
877
 
 
878
            #content change
 
879
            transform.create_file('blah', old)
 
880
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
881
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
882
                (False, False))], list(transform.iter_changes()))
 
883
            transform.cancel_deletion(old)
 
884
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
885
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
886
                (False, False))], list(transform.iter_changes()))
 
887
            transform.cancel_creation(old)
 
888
 
 
889
            # move file_id to a different file
 
890
            self.assertEqual([], list(transform.iter_changes()))
 
891
            transform.unversion_file(old)
 
892
            transform.version_file('id-1', new)
 
893
            transform.adjust_path('old', root, new)
 
894
            self.assertEqual([('id-1', ('old', 'old'), True, (True, True),
 
895
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
896
                (False, False))], list(transform.iter_changes()))
 
897
            transform.cancel_versioning(new)
 
898
            transform._removed_id = set()
 
899
 
 
900
            #execute bit
 
901
            self.assertEqual([], list(transform.iter_changes()))
 
902
            transform.set_executability(True, old)
 
903
            self.assertEqual([('id-1', ('old', 'old'), False, (True, True),
 
904
                ('eert_toor', 'eert_toor'), ('old', 'old'), ('file', 'file'),
 
905
                (False, True))], list(transform.iter_changes()))
 
906
            transform.set_executability(None, old)
 
907
 
 
908
            # filename
 
909
            self.assertEqual([], list(transform.iter_changes()))
 
910
            transform.adjust_path('new', root, old)
 
911
            transform._new_parent = {}
 
912
            self.assertEqual([('id-1', ('old', 'new'), False, (True, True),
 
913
                ('eert_toor', 'eert_toor'), ('old', 'new'), ('file', 'file'),
 
914
                (False, False))], list(transform.iter_changes()))
 
915
            transform._new_name = {}
 
916
 
 
917
            # parent directory
 
918
            self.assertEqual([], list(transform.iter_changes()))
 
919
            transform.adjust_path('new', subdir, old)
 
920
            transform._new_name = {}
 
921
            self.assertEqual([('id-1', ('old', 'subdir/old'), False,
 
922
                (True, True), ('eert_toor', 'subdir-id'), ('old', 'old'),
 
923
                ('file', 'file'), (False, False))],
 
924
                list(transform.iter_changes()))
 
925
            transform._new_path = {}
 
926
 
 
927
        finally:
 
928
            transform.finalize()
 
929
 
 
930
    def test_iter_changes_modified_bleed(self):
 
931
        self.wt.set_root_id('eert_toor')
 
932
        """Modified flag should not bleed from one change to another"""
 
933
        # unfortunately, we have no guarantee that file1 (which is modified)
 
934
        # will be applied before file2.  And if it's applied after file2, it
 
935
        # obviously can't bleed into file2's change output.  But for now, it
 
936
        # works.
 
937
        transform, root = self.get_transform()
 
938
        transform.new_file('file1', root, 'blah', 'id-1')
 
939
        transform.new_file('file2', root, 'blah', 'id-2')
 
940
        transform.apply()
 
941
        transform, root = self.get_transform()
 
942
        try:
 
943
            transform.delete_contents(transform.trans_id_file_id('id-1'))
 
944
            transform.set_executability(True,
 
945
            transform.trans_id_file_id('id-2'))
 
946
            self.assertEqual([('id-1', (u'file1', u'file1'), True, (True, True),
 
947
                ('eert_toor', 'eert_toor'), ('file1', u'file1'),
 
948
                ('file', None), (False, False)),
 
949
                ('id-2', (u'file2', u'file2'), False, (True, True),
 
950
                ('eert_toor', 'eert_toor'), ('file2', u'file2'),
 
951
                ('file', 'file'), (False, True))],
 
952
                list(transform.iter_changes()))
 
953
        finally:
 
954
            transform.finalize()
 
955
 
 
956
    def test_iter_changes_move_missing(self):
 
957
        """Test moving ids with no files around"""
 
958
        self.wt.set_root_id('toor_eert')
 
959
        # Need two steps because versioning a non-existant file is a conflict.
 
960
        transform, root = self.get_transform()
 
961
        transform.new_directory('floater', root, 'floater-id')
 
962
        transform.apply()
 
963
        transform, root = self.get_transform()
 
964
        transform.delete_contents(transform.trans_id_tree_path('floater'))
 
965
        transform.apply()
 
966
        transform, root = self.get_transform()
 
967
        floater = transform.trans_id_tree_path('floater')
 
968
        try:
 
969
            transform.adjust_path('flitter', root, floater)
 
970
            self.assertEqual([('floater-id', ('floater', 'flitter'), False,
 
971
            (True, True), ('toor_eert', 'toor_eert'), ('floater', 'flitter'),
 
972
            (None, None), (False, False))], list(transform.iter_changes()))
 
973
        finally:
 
974
            transform.finalize()
 
975
 
 
976
    def test_iter_changes_pointless(self):
 
977
        """Ensure that no-ops are not treated as modifications"""
 
978
        self.wt.set_root_id('eert_toor')
 
979
        transform, root = self.get_transform()
 
980
        transform.new_file('old', root, 'blah', 'id-1')
 
981
        transform.new_directory('subdir', root, 'subdir-id')
 
982
        transform.apply()
 
983
        transform, root = self.get_transform()
 
984
        try:
 
985
            old = transform.trans_id_tree_path('old')
 
986
            subdir = transform.trans_id_tree_file_id('subdir-id')
 
987
            self.assertEqual([], list(transform.iter_changes()))
 
988
            transform.delete_contents(subdir)
 
989
            transform.create_directory(subdir)
 
990
            transform.set_executability(False, old)
 
991
            transform.unversion_file(old)
 
992
            transform.version_file('id-1', old)
 
993
            transform.adjust_path('old', root, old)
 
994
            self.assertEqual([], list(transform.iter_changes()))
 
995
        finally:
 
996
            transform.finalize()
 
997
 
 
998
    def test_rename_count(self):
 
999
        transform, root = self.get_transform()
 
1000
        transform.new_file('name1', root, 'contents')
 
1001
        self.assertEqual(transform.rename_count, 0)
 
1002
        transform.apply()
 
1003
        self.assertEqual(transform.rename_count, 1)
 
1004
        transform2, root = self.get_transform()
 
1005
        transform2.adjust_path('name2', root,
 
1006
                               transform2.trans_id_tree_path('name1'))
 
1007
        self.assertEqual(transform2.rename_count, 0)
 
1008
        transform2.apply()
 
1009
        self.assertEqual(transform2.rename_count, 2)
 
1010
 
 
1011
    def test_change_parent(self):
 
1012
        """Ensure that after we change a parent, the results are still right.
 
1013
 
 
1014
        Renames and parent changes on pending transforms can happen as part
 
1015
        of conflict resolution, and are explicitly permitted by the
 
1016
        TreeTransform API.
 
1017
 
 
1018
        This test ensures they work correctly with the rename-avoidance
 
1019
        optimization.
 
1020
        """
 
1021
        transform, root = self.get_transform()
 
1022
        parent1 = transform.new_directory('parent1', root)
 
1023
        child1 = transform.new_file('child1', parent1, 'contents')
 
1024
        parent2 = transform.new_directory('parent2', root)
 
1025
        transform.adjust_path('child1', parent2, child1)
 
1026
        transform.apply()
 
1027
        self.failIfExists(self.wt.abspath('parent1/child1'))
 
1028
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
1029
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
 
1030
        # no rename for child1 (counting only renames during apply)
 
1031
        self.failUnlessEqual(2, transform.rename_count)
 
1032
 
 
1033
    def test_cancel_parent(self):
 
1034
        """Cancelling a parent doesn't cause deletion of a non-empty directory
 
1035
 
 
1036
        This is like the test_change_parent, except that we cancel the parent
 
1037
        before adjusting the path.  The transform must detect that the
 
1038
        directory is non-empty, and move children to safe locations.
 
1039
        """
 
1040
        transform, root = self.get_transform()
 
1041
        parent1 = transform.new_directory('parent1', root)
 
1042
        child1 = transform.new_file('child1', parent1, 'contents')
 
1043
        child2 = transform.new_file('child2', parent1, 'contents')
 
1044
        try:
 
1045
            transform.cancel_creation(parent1)
 
1046
        except OSError:
 
1047
            self.fail('Failed to move child1 before deleting parent1')
 
1048
        transform.cancel_creation(child2)
 
1049
        transform.create_directory(parent1)
 
1050
        try:
 
1051
            transform.cancel_creation(parent1)
 
1052
        # If the transform incorrectly believes that child2 is still in
 
1053
        # parent1's limbo directory, it will try to rename it and fail
 
1054
        # because was already moved by the first cancel_creation.
 
1055
        except OSError:
 
1056
            self.fail('Transform still thinks child2 is a child of parent1')
 
1057
        parent2 = transform.new_directory('parent2', root)
 
1058
        transform.adjust_path('child1', parent2, child1)
 
1059
        transform.apply()
 
1060
        self.failIfExists(self.wt.abspath('parent1'))
 
1061
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
1062
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
 
1063
        self.failUnlessEqual(2, transform.rename_count)
 
1064
 
 
1065
    def test_adjust_and_cancel(self):
 
1066
        """Make sure adjust_path keeps track of limbo children properly"""
 
1067
        transform, root = self.get_transform()
 
1068
        parent1 = transform.new_directory('parent1', root)
 
1069
        child1 = transform.new_file('child1', parent1, 'contents')
 
1070
        parent2 = transform.new_directory('parent2', root)
 
1071
        transform.adjust_path('child1', parent2, child1)
 
1072
        transform.cancel_creation(child1)
 
1073
        try:
 
1074
            transform.cancel_creation(parent1)
 
1075
        # if the transform thinks child1 is still in parent1's limbo
 
1076
        # directory, it will attempt to move it and fail.
 
1077
        except OSError:
 
1078
            self.fail('Transform still thinks child1 is a child of parent1')
 
1079
        transform.finalize()
 
1080
 
 
1081
    def test_noname_contents(self):
 
1082
        """TreeTransform should permit deferring naming files."""
 
1083
        transform, root = self.get_transform()
 
1084
        parent = transform.trans_id_file_id('parent-id')
 
1085
        try:
 
1086
            transform.create_directory(parent)
 
1087
        except KeyError:
 
1088
            self.fail("Can't handle contents with no name")
 
1089
        transform.finalize()
 
1090
 
 
1091
    def test_noname_contents_nested(self):
 
1092
        """TreeTransform should permit deferring naming files."""
 
1093
        transform, root = self.get_transform()
 
1094
        parent = transform.trans_id_file_id('parent-id')
 
1095
        try:
 
1096
            transform.create_directory(parent)
 
1097
        except KeyError:
 
1098
            self.fail("Can't handle contents with no name")
 
1099
        child = transform.new_directory('child', parent)
 
1100
        transform.adjust_path('parent', root, parent)
 
1101
        transform.apply()
 
1102
        self.failUnlessExists(self.wt.abspath('parent/child'))
 
1103
        self.assertEqual(1, transform.rename_count)
 
1104
 
 
1105
    def test_reuse_name(self):
 
1106
        """Avoid reusing the same limbo name for different files"""
 
1107
        transform, root = self.get_transform()
 
1108
        parent = transform.new_directory('parent', root)
 
1109
        child1 = transform.new_directory('child', parent)
 
1110
        try:
 
1111
            child2 = transform.new_directory('child', parent)
 
1112
        except OSError:
 
1113
            self.fail('Tranform tried to use the same limbo name twice')
 
1114
        transform.adjust_path('child2', parent, child2)
 
1115
        transform.apply()
 
1116
        # limbo/new-1 => parent, limbo/new-3 => parent/child2
 
1117
        # child2 is put into top-level limbo because child1 has already
 
1118
        # claimed the direct limbo path when child2 is created.  There is no
 
1119
        # advantage in renaming files once they're in top-level limbo, except
 
1120
        # as part of apply.
 
1121
        self.assertEqual(2, transform.rename_count)
 
1122
 
 
1123
    def test_reuse_when_first_moved(self):
 
1124
        """Don't avoid direct paths when it is safe to use them"""
 
1125
        transform, root = self.get_transform()
 
1126
        parent = transform.new_directory('parent', root)
 
1127
        child1 = transform.new_directory('child', parent)
 
1128
        transform.adjust_path('child1', parent, child1)
 
1129
        child2 = transform.new_directory('child', parent)
 
1130
        transform.apply()
 
1131
        # limbo/new-1 => parent
 
1132
        self.assertEqual(1, transform.rename_count)
 
1133
 
 
1134
    def test_reuse_after_cancel(self):
 
1135
        """Don't avoid direct paths when it is safe to use them"""
 
1136
        transform, root = self.get_transform()
 
1137
        parent2 = transform.new_directory('parent2', root)
 
1138
        child1 = transform.new_directory('child1', parent2)
 
1139
        transform.cancel_creation(parent2)
 
1140
        transform.create_directory(parent2)
 
1141
        child2 = transform.new_directory('child1', parent2)
 
1142
        transform.adjust_path('child2', parent2, child1)
 
1143
        transform.apply()
 
1144
        # limbo/new-1 => parent2, limbo/new-2 => parent2/child1
 
1145
        self.assertEqual(2, transform.rename_count)
 
1146
 
 
1147
    def test_finalize_order(self):
 
1148
        """Finalize must be done in child-to-parent order"""
 
1149
        transform, root = self.get_transform()
 
1150
        parent = transform.new_directory('parent', root)
 
1151
        child = transform.new_directory('child', parent)
 
1152
        try:
 
1153
            transform.finalize()
 
1154
        except OSError:
 
1155
            self.fail('Tried to remove parent before child1')
 
1156
 
 
1157
    def test_cancel_with_cancelled_child_should_succeed(self):
 
1158
        transform, root = self.get_transform()
 
1159
        parent = transform.new_directory('parent', root)
 
1160
        child = transform.new_directory('child', parent)
 
1161
        transform.cancel_creation(child)
 
1162
        transform.cancel_creation(parent)
 
1163
        transform.finalize()
 
1164
 
 
1165
    def test_case_insensitive_clash(self):
 
1166
        self.requireFeature(CaseInsensitiveFilesystemFeature)
 
1167
        def tt_helper():
 
1168
            wt = self.make_branch_and_tree('.')
 
1169
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1170
            try:
 
1171
                tt.new_file('foo', tt.root, 'bar')
 
1172
                tt.new_file('Foo', tt.root, 'spam')
 
1173
                # Lie to tt that we've already resolved all conflicts.
 
1174
                tt.apply(no_conflicts=True)
 
1175
            except:
 
1176
                wt.unlock()
 
1177
                raise
 
1178
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1179
        self.assertContainsRe(str(err),
 
1180
            "^File exists: .+/foo")
 
1181
 
 
1182
    def test_two_directories_clash(self):
 
1183
        def tt_helper():
 
1184
            wt = self.make_branch_and_tree('.')
 
1185
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1186
            try:
 
1187
                foo_1 = tt.new_directory('foo', tt.root)
 
1188
                tt.new_directory('bar', foo_1)
 
1189
                foo_2 = tt.new_directory('foo', tt.root)
 
1190
                tt.new_directory('baz', foo_2)
 
1191
                # Lie to tt that we've already resolved all conflicts.
 
1192
                tt.apply(no_conflicts=True)
 
1193
            except:
 
1194
                wt.unlock()
 
1195
                raise
 
1196
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1197
        self.assertContainsRe(str(err),
 
1198
            "^File exists: .+/foo")
 
1199
 
 
1200
    def test_two_directories_clash_finalize(self):
 
1201
        def tt_helper():
 
1202
            wt = self.make_branch_and_tree('.')
 
1203
            tt = TreeTransform(wt)  # TreeTransform obtains write lock
 
1204
            try:
 
1205
                foo_1 = tt.new_directory('foo', tt.root)
 
1206
                tt.new_directory('bar', foo_1)
 
1207
                foo_2 = tt.new_directory('foo', tt.root)
 
1208
                tt.new_directory('baz', foo_2)
 
1209
                # Lie to tt that we've already resolved all conflicts.
 
1210
                tt.apply(no_conflicts=True)
 
1211
            except:
 
1212
                tt.finalize()
 
1213
                raise
 
1214
        err = self.assertRaises(errors.FileExists, tt_helper)
 
1215
        self.assertContainsRe(str(err),
 
1216
            "^File exists: .+/foo")
 
1217
 
 
1218
    def test_file_to_directory(self):
 
1219
        wt = self.make_branch_and_tree('.')
 
1220
        self.build_tree(['foo'])
 
1221
        wt.add(['foo'])
 
1222
        wt.commit("one")
 
1223
        tt = TreeTransform(wt)
 
1224
        self.addCleanup(tt.finalize)
 
1225
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1226
        tt.delete_contents(foo_trans_id)
 
1227
        tt.create_directory(foo_trans_id)
 
1228
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
 
1229
        tt.create_file(["aa\n"], bar_trans_id)
 
1230
        tt.version_file("bar-1", bar_trans_id)
 
1231
        tt.apply()
 
1232
        self.failUnlessExists("foo/bar")
 
1233
        wt.lock_read()
 
1234
        try:
 
1235
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1236
                    "directory")
 
1237
        finally:
 
1238
            wt.unlock()
 
1239
        wt.commit("two")
 
1240
        changes = wt.changes_from(wt.basis_tree())
 
1241
        self.assertFalse(changes.has_changed(), changes)
 
1242
 
 
1243
    def test_file_to_symlink(self):
 
1244
        self.requireFeature(SymlinkFeature)
 
1245
        wt = self.make_branch_and_tree('.')
 
1246
        self.build_tree(['foo'])
 
1247
        wt.add(['foo'])
 
1248
        wt.commit("one")
 
1249
        tt = TreeTransform(wt)
 
1250
        self.addCleanup(tt.finalize)
 
1251
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1252
        tt.delete_contents(foo_trans_id)
 
1253
        tt.create_symlink("bar", foo_trans_id)
 
1254
        tt.apply()
 
1255
        self.failUnlessExists("foo")
 
1256
        wt.lock_read()
 
1257
        self.addCleanup(wt.unlock)
 
1258
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1259
                "symlink")
 
1260
 
 
1261
    def test_dir_to_file(self):
 
1262
        wt = self.make_branch_and_tree('.')
 
1263
        self.build_tree(['foo/', 'foo/bar'])
 
1264
        wt.add(['foo', 'foo/bar'])
 
1265
        wt.commit("one")
 
1266
        tt = TreeTransform(wt)
 
1267
        self.addCleanup(tt.finalize)
 
1268
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1269
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
 
1270
        tt.delete_contents(foo_trans_id)
 
1271
        tt.delete_versioned(bar_trans_id)
 
1272
        tt.create_file(["aa\n"], foo_trans_id)
 
1273
        tt.apply()
 
1274
        self.failUnlessExists("foo")
 
1275
        wt.lock_read()
 
1276
        self.addCleanup(wt.unlock)
 
1277
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1278
                "file")
 
1279
 
 
1280
    def test_dir_to_hardlink(self):
 
1281
        self.requireFeature(HardlinkFeature)
 
1282
        wt = self.make_branch_and_tree('.')
 
1283
        self.build_tree(['foo/', 'foo/bar'])
 
1284
        wt.add(['foo', 'foo/bar'])
 
1285
        wt.commit("one")
 
1286
        tt = TreeTransform(wt)
 
1287
        self.addCleanup(tt.finalize)
 
1288
        foo_trans_id = tt.trans_id_tree_path("foo")
 
1289
        bar_trans_id = tt.trans_id_tree_path("foo/bar")
 
1290
        tt.delete_contents(foo_trans_id)
 
1291
        tt.delete_versioned(bar_trans_id)
 
1292
        self.build_tree(['baz'])
 
1293
        tt.create_hardlink("baz", foo_trans_id)
 
1294
        tt.apply()
 
1295
        self.failUnlessExists("foo")
 
1296
        self.failUnlessExists("baz")
 
1297
        wt.lock_read()
 
1298
        self.addCleanup(wt.unlock)
 
1299
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
 
1300
                "file")
 
1301
 
519
1302
 
520
1303
class TransformGroup(object):
521
 
    def __init__(self, dirname):
 
1304
 
 
1305
    def __init__(self, dirname, root_id):
522
1306
        self.name = dirname
523
1307
        os.mkdir(dirname)
524
1308
        self.wt = BzrDir.create_standalone_workingtree(dirname)
 
1309
        self.wt.set_root_id(root_id)
525
1310
        self.b = self.wt.branch
526
1311
        self.tt = TreeTransform(self.wt)
527
1312
        self.root = self.tt.trans_id_tree_file_id(self.wt.get_root_id())
528
1313
 
 
1314
 
529
1315
def conflict_text(tree, merge):
530
1316
    template = '%s TREE\n%s%s\n%s%s MERGE-SOURCE\n'
531
1317
    return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
532
1318
 
533
1319
 
534
1320
class TestTransformMerge(TestCaseInTempDir):
 
1321
 
535
1322
    def test_text_merge(self):
536
 
        base = TransformGroup("base")
 
1323
        root_id = generate_ids.gen_root_id()
 
1324
        base = TransformGroup("base", root_id)
537
1325
        base.tt.new_file('a', base.root, 'a\nb\nc\nd\be\n', 'a')
538
1326
        base.tt.new_file('b', base.root, 'b1', 'b')
539
1327
        base.tt.new_file('c', base.root, 'c', 'c')
543
1331
        base.tt.new_directory('g', base.root, 'g')
544
1332
        base.tt.new_directory('h', base.root, 'h')
545
1333
        base.tt.apply()
546
 
        other = TransformGroup("other")
 
1334
        other = TransformGroup("other", root_id)
547
1335
        other.tt.new_file('a', other.root, 'y\nb\nc\nd\be\n', 'a')
548
1336
        other.tt.new_file('b', other.root, 'b2', 'b')
549
1337
        other.tt.new_file('c', other.root, 'c2', 'c')
554
1342
        other.tt.new_file('h', other.root, 'h\ni\nj\nk\n', 'h')
555
1343
        other.tt.new_file('i', other.root, 'h\ni\nj\nk\n', 'i')
556
1344
        other.tt.apply()
557
 
        this = TransformGroup("this")
 
1345
        this = TransformGroup("this", root_id)
558
1346
        this.tt.new_file('a', this.root, 'a\nb\nc\nd\bz\n', 'a')
559
1347
        this.tt.new_file('b', this.root, 'b', 'b')
560
1348
        this.tt.new_file('c', this.root, 'c', 'c')
566
1354
        this.tt.new_file('i', this.root, '1\n2\n3\n4\n', 'i')
567
1355
        this.tt.apply()
568
1356
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
 
1357
 
569
1358
        # textual merge
570
1359
        self.assertEqual(this.wt.get_file('a').read(), 'y\nb\nc\nd\bz\n')
571
1360
        # three-way text conflict
606
1395
        self.assertSubset(merge_modified, modified)
607
1396
        self.assertEqual(len(merge_modified), len(modified))
608
1397
        this.wt.remove('b')
609
 
        this.wt.revert([])
 
1398
        this.wt.revert()
610
1399
 
611
1400
    def test_file_merge(self):
612
 
        if not has_symlinks():
613
 
            raise TestSkipped('Symlinks are not supported on this platform')
614
 
        base = TransformGroup("BASE")
615
 
        this = TransformGroup("THIS")
616
 
        other = TransformGroup("OTHER")
 
1401
        self.requireFeature(SymlinkFeature)
 
1402
        root_id = generate_ids.gen_root_id()
 
1403
        base = TransformGroup("BASE", root_id)
 
1404
        this = TransformGroup("THIS", root_id)
 
1405
        other = TransformGroup("OTHER", root_id)
617
1406
        for tg in this, base, other:
618
1407
            tg.tt.new_directory('a', tg.root, 'a')
619
1408
            tg.tt.new_symlink('b', tg.root, 'b', 'b')
651
1440
        self.assertIs(os.path.lexists(this.wt.abspath('h.OTHER')), True)
652
1441
 
653
1442
    def test_filename_merge(self):
654
 
        base = TransformGroup("BASE")
655
 
        this = TransformGroup("THIS")
656
 
        other = TransformGroup("OTHER")
 
1443
        root_id = generate_ids.gen_root_id()
 
1444
        base = TransformGroup("BASE", root_id)
 
1445
        this = TransformGroup("THIS", root_id)
 
1446
        other = TransformGroup("OTHER", root_id)
657
1447
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
658
1448
                                   for t in [base, this, other]]
659
1449
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
683
1473
        self.assertEqual(this.wt.id2path('f'), pathjoin('b/f1'))
684
1474
 
685
1475
    def test_filename_merge_conflicts(self):
686
 
        base = TransformGroup("BASE")
687
 
        this = TransformGroup("THIS")
688
 
        other = TransformGroup("OTHER")
 
1476
        root_id = generate_ids.gen_root_id()
 
1477
        base = TransformGroup("BASE", root_id)
 
1478
        this = TransformGroup("THIS", root_id)
 
1479
        other = TransformGroup("OTHER", root_id)
689
1480
        base_a, this_a, other_a = [t.tt.new_directory('a', t.root, 'a') 
690
1481
                                   for t in [base, this, other]]
691
1482
        base_b, this_b, other_b = [t.tt.new_directory('b', t.root, 'b') 
712
1503
        self.assertIs(os.path.lexists(this.wt.abspath('b/h1.OTHER')), False)
713
1504
        self.assertEqual(this.wt.id2path('i'), pathjoin('b/i1.OTHER'))
714
1505
 
715
 
class TestBuildTree(TestCaseInTempDir):
716
 
    def test_build_tree(self):
717
 
        if not has_symlinks():
718
 
            raise TestSkipped('Test requires symlink support')
 
1506
 
 
1507
class TestBuildTree(tests.TestCaseWithTransport):
 
1508
 
 
1509
    def test_build_tree_with_symlinks(self):
 
1510
        self.requireFeature(SymlinkFeature)
719
1511
        os.mkdir('a')
720
1512
        a = BzrDir.create_standalone_workingtree('a')
721
1513
        os.mkdir('a/foo')
724
1516
        a.add(['foo', 'foo/bar', 'foo/baz'])
725
1517
        a.commit('initial commit')
726
1518
        b = BzrDir.create_standalone_workingtree('b')
727
 
        build_tree(a.basis_tree(), b)
 
1519
        basis = a.basis_tree()
 
1520
        basis.lock_read()
 
1521
        self.addCleanup(basis.unlock)
 
1522
        build_tree(basis, b)
728
1523
        self.assertIs(os.path.isdir('b/foo'), True)
729
1524
        self.assertEqual(file('b/foo/bar', 'rb').read(), "contents")
730
1525
        self.assertEqual(os.readlink('b/foo/baz'), 'a/foo/bar')
 
1526
 
 
1527
    def test_build_with_references(self):
 
1528
        tree = self.make_branch_and_tree('source',
 
1529
            format='dirstate-with-subtree')
 
1530
        subtree = self.make_branch_and_tree('source/subtree',
 
1531
            format='dirstate-with-subtree')
 
1532
        tree.add_reference(subtree)
 
1533
        tree.commit('a revision')
 
1534
        tree.branch.create_checkout('target')
 
1535
        self.failUnlessExists('target')
 
1536
        self.failUnlessExists('target/subtree')
 
1537
 
 
1538
    def test_file_conflict_handling(self):
 
1539
        """Ensure that when building trees, conflict handling is done"""
 
1540
        source = self.make_branch_and_tree('source')
 
1541
        target = self.make_branch_and_tree('target')
 
1542
        self.build_tree(['source/file', 'target/file'])
 
1543
        source.add('file', 'new-file')
 
1544
        source.commit('added file')
 
1545
        build_tree(source.basis_tree(), target)
 
1546
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
1547
                          'file.moved', 'file', None, 'new-file')],
 
1548
                         target.conflicts())
 
1549
        target2 = self.make_branch_and_tree('target2')
 
1550
        target_file = file('target2/file', 'wb')
 
1551
        try:
 
1552
            source_file = file('source/file', 'rb')
 
1553
            try:
 
1554
                target_file.write(source_file.read())
 
1555
            finally:
 
1556
                source_file.close()
 
1557
        finally:
 
1558
            target_file.close()
 
1559
        build_tree(source.basis_tree(), target2)
 
1560
        self.assertEqual([], target2.conflicts())
 
1561
 
 
1562
    def test_symlink_conflict_handling(self):
 
1563
        """Ensure that when building trees, conflict handling is done"""
 
1564
        self.requireFeature(SymlinkFeature)
 
1565
        source = self.make_branch_and_tree('source')
 
1566
        os.symlink('foo', 'source/symlink')
 
1567
        source.add('symlink', 'new-symlink')
 
1568
        source.commit('added file')
 
1569
        target = self.make_branch_and_tree('target')
 
1570
        os.symlink('bar', 'target/symlink')
 
1571
        build_tree(source.basis_tree(), target)
 
1572
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
1573
            'symlink.moved', 'symlink', None, 'new-symlink')],
 
1574
            target.conflicts())
 
1575
        target = self.make_branch_and_tree('target2')
 
1576
        os.symlink('foo', 'target2/symlink')
 
1577
        build_tree(source.basis_tree(), target)
 
1578
        self.assertEqual([], target.conflicts())
731
1579
        
 
1580
    def test_directory_conflict_handling(self):
 
1581
        """Ensure that when building trees, conflict handling is done"""
 
1582
        source = self.make_branch_and_tree('source')
 
1583
        target = self.make_branch_and_tree('target')
 
1584
        self.build_tree(['source/dir1/', 'source/dir1/file', 'target/dir1/'])
 
1585
        source.add(['dir1', 'dir1/file'], ['new-dir1', 'new-file'])
 
1586
        source.commit('added file')
 
1587
        build_tree(source.basis_tree(), target)
 
1588
        self.assertEqual([], target.conflicts())
 
1589
        self.failUnlessExists('target/dir1/file')
 
1590
 
 
1591
        # Ensure contents are merged
 
1592
        target = self.make_branch_and_tree('target2')
 
1593
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
 
1594
        build_tree(source.basis_tree(), target)
 
1595
        self.assertEqual([], target.conflicts())
 
1596
        self.failUnlessExists('target2/dir1/file2')
 
1597
        self.failUnlessExists('target2/dir1/file')
 
1598
 
 
1599
        # Ensure new contents are suppressed for existing branches
 
1600
        target = self.make_branch_and_tree('target3')
 
1601
        self.make_branch('target3/dir1')
 
1602
        self.build_tree(['target3/dir1/file2'])
 
1603
        build_tree(source.basis_tree(), target)
 
1604
        self.failIfExists('target3/dir1/file')
 
1605
        self.failUnlessExists('target3/dir1/file2')
 
1606
        self.failUnlessExists('target3/dir1.diverted/file')
 
1607
        self.assertEqual([DuplicateEntry('Diverted to',
 
1608
            'dir1.diverted', 'dir1', 'new-dir1', None)],
 
1609
            target.conflicts())
 
1610
 
 
1611
        target = self.make_branch_and_tree('target4')
 
1612
        self.build_tree(['target4/dir1/'])
 
1613
        self.make_branch('target4/dir1/file')
 
1614
        build_tree(source.basis_tree(), target)
 
1615
        self.failUnlessExists('target4/dir1/file')
 
1616
        self.assertEqual('directory', file_kind('target4/dir1/file'))
 
1617
        self.failUnlessExists('target4/dir1/file.diverted')
 
1618
        self.assertEqual([DuplicateEntry('Diverted to',
 
1619
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
 
1620
            target.conflicts())
 
1621
 
 
1622
    def test_mixed_conflict_handling(self):
 
1623
        """Ensure that when building trees, conflict handling is done"""
 
1624
        source = self.make_branch_and_tree('source')
 
1625
        target = self.make_branch_and_tree('target')
 
1626
        self.build_tree(['source/name', 'target/name/'])
 
1627
        source.add('name', 'new-name')
 
1628
        source.commit('added file')
 
1629
        build_tree(source.basis_tree(), target)
 
1630
        self.assertEqual([DuplicateEntry('Moved existing file to',
 
1631
            'name.moved', 'name', None, 'new-name')], target.conflicts())
 
1632
 
 
1633
    def test_raises_in_populated(self):
 
1634
        source = self.make_branch_and_tree('source')
 
1635
        self.build_tree(['source/name'])
 
1636
        source.add('name')
 
1637
        source.commit('added name')
 
1638
        target = self.make_branch_and_tree('target')
 
1639
        self.build_tree(['target/name'])
 
1640
        target.add('name')
 
1641
        self.assertRaises(errors.WorkingTreeAlreadyPopulated, 
 
1642
            build_tree, source.basis_tree(), target)
 
1643
 
 
1644
    def test_build_tree_rename_count(self):
 
1645
        source = self.make_branch_and_tree('source')
 
1646
        self.build_tree(['source/file1', 'source/dir1/'])
 
1647
        source.add(['file1', 'dir1'])
 
1648
        source.commit('add1')
 
1649
        target1 = self.make_branch_and_tree('target1')
 
1650
        transform_result = build_tree(source.basis_tree(), target1)
 
1651
        self.assertEqual(2, transform_result.rename_count)
 
1652
 
 
1653
        self.build_tree(['source/dir1/file2'])
 
1654
        source.add(['dir1/file2'])
 
1655
        source.commit('add3')
 
1656
        target2 = self.make_branch_and_tree('target2')
 
1657
        transform_result = build_tree(source.basis_tree(), target2)
 
1658
        # children of non-root directories should not be renamed
 
1659
        self.assertEqual(2, transform_result.rename_count)
 
1660
 
 
1661
    def create_ab_tree(self):
 
1662
        """Create a committed test tree with two files"""
 
1663
        source = self.make_branch_and_tree('source')
 
1664
        self.build_tree_contents([('source/file1', 'A')])
 
1665
        self.build_tree_contents([('source/file2', 'B')])
 
1666
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
 
1667
        source.commit('commit files')
 
1668
        source.lock_write()
 
1669
        self.addCleanup(source.unlock)
 
1670
        return source
 
1671
 
 
1672
    def test_build_tree_accelerator_tree(self):
 
1673
        source = self.create_ab_tree()
 
1674
        self.build_tree_contents([('source/file2', 'C')])
 
1675
        calls = []
 
1676
        real_source_get_file = source.get_file
 
1677
        def get_file(file_id, path=None):
 
1678
            calls.append(file_id)
 
1679
            return real_source_get_file(file_id, path)
 
1680
        source.get_file = get_file
 
1681
        target = self.make_branch_and_tree('target')
 
1682
        revision_tree = source.basis_tree()
 
1683
        revision_tree.lock_read()
 
1684
        self.addCleanup(revision_tree.unlock)
 
1685
        build_tree(revision_tree, target, source)
 
1686
        self.assertEqual(['file1-id'], calls)
 
1687
        target.lock_read()
 
1688
        self.addCleanup(target.unlock)
 
1689
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1690
 
 
1691
    def test_build_tree_accelerator_tree_missing_file(self):
 
1692
        source = self.create_ab_tree()
 
1693
        os.unlink('source/file1')
 
1694
        source.remove(['file2'])
 
1695
        target = self.make_branch_and_tree('target')
 
1696
        revision_tree = source.basis_tree()
 
1697
        revision_tree.lock_read()
 
1698
        self.addCleanup(revision_tree.unlock)
 
1699
        build_tree(revision_tree, target, source)
 
1700
        target.lock_read()
 
1701
        self.addCleanup(target.unlock)
 
1702
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1703
 
 
1704
    def test_build_tree_accelerator_wrong_kind(self):
 
1705
        self.requireFeature(SymlinkFeature)
 
1706
        source = self.make_branch_and_tree('source')
 
1707
        self.build_tree_contents([('source/file1', '')])
 
1708
        self.build_tree_contents([('source/file2', '')])
 
1709
        source.add(['file1', 'file2'], ['file1-id', 'file2-id'])
 
1710
        source.commit('commit files')
 
1711
        os.unlink('source/file2')
 
1712
        self.build_tree_contents([('source/file2/', 'C')])
 
1713
        os.unlink('source/file1')
 
1714
        os.symlink('file2', 'source/file1')
 
1715
        calls = []
 
1716
        real_source_get_file = source.get_file
 
1717
        def get_file(file_id, path=None):
 
1718
            calls.append(file_id)
 
1719
            return real_source_get_file(file_id, path)
 
1720
        source.get_file = get_file
 
1721
        target = self.make_branch_and_tree('target')
 
1722
        revision_tree = source.basis_tree()
 
1723
        revision_tree.lock_read()
 
1724
        self.addCleanup(revision_tree.unlock)
 
1725
        build_tree(revision_tree, target, source)
 
1726
        self.assertEqual([], calls)
 
1727
        target.lock_read()
 
1728
        self.addCleanup(target.unlock)
 
1729
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1730
 
 
1731
    def test_build_tree_hardlink(self):
 
1732
        self.requireFeature(HardlinkFeature)
 
1733
        source = self.create_ab_tree()
 
1734
        target = self.make_branch_and_tree('target')
 
1735
        revision_tree = source.basis_tree()
 
1736
        revision_tree.lock_read()
 
1737
        self.addCleanup(revision_tree.unlock)
 
1738
        build_tree(revision_tree, target, source, hardlink=True)
 
1739
        target.lock_read()
 
1740
        self.addCleanup(target.unlock)
 
1741
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1742
        source_stat = os.stat('source/file1')
 
1743
        target_stat = os.stat('target/file1')
 
1744
        self.assertEqual(source_stat, target_stat)
 
1745
 
 
1746
        # Explicitly disallowing hardlinks should prevent them.
 
1747
        target2 = self.make_branch_and_tree('target2')
 
1748
        build_tree(revision_tree, target2, source, hardlink=False)
 
1749
        target2.lock_read()
 
1750
        self.addCleanup(target2.unlock)
 
1751
        self.assertEqual([], list(target2.iter_changes(revision_tree)))
 
1752
        source_stat = os.stat('source/file1')
 
1753
        target2_stat = os.stat('target2/file1')
 
1754
        self.assertNotEqual(source_stat, target2_stat)
 
1755
 
 
1756
    def test_build_tree_accelerator_tree_moved(self):
 
1757
        source = self.make_branch_and_tree('source')
 
1758
        self.build_tree_contents([('source/file1', 'A')])
 
1759
        source.add(['file1'], ['file1-id'])
 
1760
        source.commit('commit files')
 
1761
        source.rename_one('file1', 'file2')
 
1762
        source.lock_read()
 
1763
        self.addCleanup(source.unlock)
 
1764
        target = self.make_branch_and_tree('target')
 
1765
        revision_tree = source.basis_tree()
 
1766
        revision_tree.lock_read()
 
1767
        self.addCleanup(revision_tree.unlock)
 
1768
        build_tree(revision_tree, target, source)
 
1769
        target.lock_read()
 
1770
        self.addCleanup(target.unlock)
 
1771
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1772
 
 
1773
    def test_build_tree_hardlinks_preserve_execute(self):
 
1774
        self.requireFeature(HardlinkFeature)
 
1775
        source = self.create_ab_tree()
 
1776
        tt = TreeTransform(source)
 
1777
        trans_id = tt.trans_id_tree_file_id('file1-id')
 
1778
        tt.set_executability(True, trans_id)
 
1779
        tt.apply()
 
1780
        self.assertTrue(source.is_executable('file1-id'))
 
1781
        target = self.make_branch_and_tree('target')
 
1782
        revision_tree = source.basis_tree()
 
1783
        revision_tree.lock_read()
 
1784
        self.addCleanup(revision_tree.unlock)
 
1785
        build_tree(revision_tree, target, source, hardlink=True)
 
1786
        target.lock_read()
 
1787
        self.addCleanup(target.unlock)
 
1788
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1789
        self.assertTrue(source.is_executable('file1-id'))
 
1790
 
 
1791
    def test_case_insensitive_build_tree_inventory(self):
 
1792
        source = self.make_branch_and_tree('source')
 
1793
        self.build_tree(['source/file', 'source/FILE'])
 
1794
        source.add(['file', 'FILE'], ['lower-id', 'upper-id'])
 
1795
        source.commit('added files')
 
1796
        # Don't try this at home, kids!
 
1797
        # Force the tree to report that it is case insensitive
 
1798
        target = self.make_branch_and_tree('target')
 
1799
        target.case_sensitive = False
 
1800
        build_tree(source.basis_tree(), target, source, delta_from_tree=True)
 
1801
        self.assertEqual('file.moved', target.id2path('lower-id'))
 
1802
        self.assertEqual('FILE', target.id2path('upper-id'))
 
1803
 
 
1804
 
732
1805
class MockTransform(object):
733
1806
 
734
1807
    def has_named_child(self, by_parent, parent_id, name):
740
1813
                return True
741
1814
        return False
742
1815
 
 
1816
 
743
1817
class MockEntry(object):
744
1818
    def __init__(self):
745
1819
        object.__init__(self)
746
1820
        self.name = "name"
747
1821
 
 
1822
 
748
1823
class TestGetBackupName(TestCase):
749
1824
    def test_get_backup_name(self):
750
1825
        tt = MockTransform()
758
1833
        self.assertEqual(name, 'name.~1~')
759
1834
        name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
760
1835
        self.assertEqual(name, 'name.~4~')
 
1836
 
 
1837
 
 
1838
class TestFileMover(tests.TestCaseWithTransport):
 
1839
 
 
1840
    def test_file_mover(self):
 
1841
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
 
1842
        mover = _FileMover()
 
1843
        mover.rename('a', 'q')
 
1844
        self.failUnlessExists('q')
 
1845
        self.failIfExists('a')
 
1846
        self.failUnlessExists('q/b')
 
1847
        self.failUnlessExists('c')
 
1848
        self.failUnlessExists('c/d')
 
1849
 
 
1850
    def test_pre_delete_rollback(self):
 
1851
        self.build_tree(['a/'])
 
1852
        mover = _FileMover()
 
1853
        mover.pre_delete('a', 'q')
 
1854
        self.failUnlessExists('q')
 
1855
        self.failIfExists('a')
 
1856
        mover.rollback()
 
1857
        self.failIfExists('q')
 
1858
        self.failUnlessExists('a')
 
1859
 
 
1860
    def test_apply_deletions(self):
 
1861
        self.build_tree(['a/', 'b/'])
 
1862
        mover = _FileMover()
 
1863
        mover.pre_delete('a', 'q')
 
1864
        mover.pre_delete('b', 'r')
 
1865
        self.failUnlessExists('q')
 
1866
        self.failUnlessExists('r')
 
1867
        self.failIfExists('a')
 
1868
        self.failIfExists('b')
 
1869
        mover.apply_deletions()
 
1870
        self.failIfExists('q')
 
1871
        self.failIfExists('r')
 
1872
        self.failIfExists('a')
 
1873
        self.failIfExists('b')
 
1874
 
 
1875
    def test_file_mover_rollback(self):
 
1876
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
 
1877
        mover = _FileMover()
 
1878
        mover.rename('c/d', 'c/f')
 
1879
        mover.rename('c/e', 'c/d')
 
1880
        try:
 
1881
            mover.rename('a', 'c')
 
1882
        except errors.FileExists, e:
 
1883
            mover.rollback()
 
1884
        self.failUnlessExists('a')
 
1885
        self.failUnlessExists('c/d')
 
1886
 
 
1887
 
 
1888
class Bogus(Exception):
 
1889
    pass
 
1890
 
 
1891
 
 
1892
class TestTransformRollback(tests.TestCaseWithTransport):
 
1893
 
 
1894
    class ExceptionFileMover(_FileMover):
 
1895
 
 
1896
        def __init__(self, bad_source=None, bad_target=None):
 
1897
            _FileMover.__init__(self)
 
1898
            self.bad_source = bad_source
 
1899
            self.bad_target = bad_target
 
1900
 
 
1901
        def rename(self, source, target):
 
1902
            if (self.bad_source is not None and
 
1903
                source.endswith(self.bad_source)):
 
1904
                raise Bogus
 
1905
            elif (self.bad_target is not None and
 
1906
                target.endswith(self.bad_target)):
 
1907
                raise Bogus
 
1908
            else:
 
1909
                _FileMover.rename(self, source, target)
 
1910
 
 
1911
    def test_rollback_rename(self):
 
1912
        tree = self.make_branch_and_tree('.')
 
1913
        self.build_tree(['a/', 'a/b'])
 
1914
        tt = TreeTransform(tree)
 
1915
        self.addCleanup(tt.finalize)
 
1916
        a_id = tt.trans_id_tree_path('a')
 
1917
        tt.adjust_path('c', tt.root, a_id)
 
1918
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
 
1919
        self.assertRaises(Bogus, tt.apply,
 
1920
                          _mover=self.ExceptionFileMover(bad_source='a'))
 
1921
        self.failUnlessExists('a')
 
1922
        self.failUnlessExists('a/b')
 
1923
        tt.apply()
 
1924
        self.failUnlessExists('c')
 
1925
        self.failUnlessExists('c/d')
 
1926
 
 
1927
    def test_rollback_rename_into_place(self):
 
1928
        tree = self.make_branch_and_tree('.')
 
1929
        self.build_tree(['a/', 'a/b'])
 
1930
        tt = TreeTransform(tree)
 
1931
        self.addCleanup(tt.finalize)
 
1932
        a_id = tt.trans_id_tree_path('a')
 
1933
        tt.adjust_path('c', tt.root, a_id)
 
1934
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
 
1935
        self.assertRaises(Bogus, tt.apply,
 
1936
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
 
1937
        self.failUnlessExists('a')
 
1938
        self.failUnlessExists('a/b')
 
1939
        tt.apply()
 
1940
        self.failUnlessExists('c')
 
1941
        self.failUnlessExists('c/d')
 
1942
 
 
1943
    def test_rollback_deletion(self):
 
1944
        tree = self.make_branch_and_tree('.')
 
1945
        self.build_tree(['a/', 'a/b'])
 
1946
        tt = TreeTransform(tree)
 
1947
        self.addCleanup(tt.finalize)
 
1948
        a_id = tt.trans_id_tree_path('a')
 
1949
        tt.delete_contents(a_id)
 
1950
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
 
1951
        self.assertRaises(Bogus, tt.apply,
 
1952
                          _mover=self.ExceptionFileMover(bad_target='d'))
 
1953
        self.failUnlessExists('a')
 
1954
        self.failUnlessExists('a/b')
 
1955
 
 
1956
    def test_resolve_no_parent(self):
 
1957
        wt = self.make_branch_and_tree('.')
 
1958
        tt = TreeTransform(wt)
 
1959
        self.addCleanup(tt.finalize)
 
1960
        parent = tt.trans_id_file_id('parent-id')
 
1961
        tt.new_file('file', parent, 'Contents')
 
1962
        resolve_conflicts(tt)
 
1963
 
 
1964
 
 
1965
class TestTransformPreview(tests.TestCaseWithTransport):
 
1966
 
 
1967
    def create_tree(self):
 
1968
        tree = self.make_branch_and_tree('.')
 
1969
        self.build_tree_contents([('a', 'content 1')])
 
1970
        tree.add('a', 'a-id')
 
1971
        tree.commit('rev1', rev_id='rev1')
 
1972
        return tree.branch.repository.revision_tree('rev1')
 
1973
 
 
1974
    def get_empty_preview(self):
 
1975
        repository = self.make_repository('repo')
 
1976
        tree = repository.revision_tree(_mod_revision.NULL_REVISION)
 
1977
        preview = TransformPreview(tree)
 
1978
        self.addCleanup(preview.finalize)
 
1979
        return preview
 
1980
 
 
1981
    def test_transform_preview(self):
 
1982
        revision_tree = self.create_tree()
 
1983
        preview = TransformPreview(revision_tree)
 
1984
        self.addCleanup(preview.finalize)
 
1985
 
 
1986
    def test_transform_preview_tree(self):
 
1987
        revision_tree = self.create_tree()
 
1988
        preview = TransformPreview(revision_tree)
 
1989
        self.addCleanup(preview.finalize)
 
1990
        preview.get_preview_tree()
 
1991
 
 
1992
    def test_transform_new_file(self):
 
1993
        revision_tree = self.create_tree()
 
1994
        preview = TransformPreview(revision_tree)
 
1995
        self.addCleanup(preview.finalize)
 
1996
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
1997
        preview_tree = preview.get_preview_tree()
 
1998
        self.assertEqual(preview_tree.kind('file2-id'), 'file')
 
1999
        self.assertEqual(
 
2000
            preview_tree.get_file('file2-id').read(), 'content B\n')
 
2001
 
 
2002
    def test_diff_preview_tree(self):
 
2003
        revision_tree = self.create_tree()
 
2004
        preview = TransformPreview(revision_tree)
 
2005
        self.addCleanup(preview.finalize)
 
2006
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
2007
        preview_tree = preview.get_preview_tree()
 
2008
        out = StringIO()
 
2009
        show_diff_trees(revision_tree, preview_tree, out)
 
2010
        lines = out.getvalue().splitlines()
 
2011
        self.assertEqual(lines[0], "=== added file 'file2'")
 
2012
        # 3 lines of diff administrivia
 
2013
        self.assertEqual(lines[4], "+content B")
 
2014
 
 
2015
    def test_transform_conflicts(self):
 
2016
        revision_tree = self.create_tree()
 
2017
        preview = TransformPreview(revision_tree)
 
2018
        self.addCleanup(preview.finalize)
 
2019
        preview.new_file('a', preview.root, 'content 2')
 
2020
        resolve_conflicts(preview)
 
2021
        trans_id = preview.trans_id_file_id('a-id')
 
2022
        self.assertEqual('a.moved', preview.final_name(trans_id))
 
2023
 
 
2024
    def get_tree_and_preview_tree(self):
 
2025
        revision_tree = self.create_tree()
 
2026
        preview = TransformPreview(revision_tree)
 
2027
        self.addCleanup(preview.finalize)
 
2028
        a_trans_id = preview.trans_id_file_id('a-id')
 
2029
        preview.delete_contents(a_trans_id)
 
2030
        preview.create_file('b content', a_trans_id)
 
2031
        preview_tree = preview.get_preview_tree()
 
2032
        return revision_tree, preview_tree
 
2033
 
 
2034
    def test_iter_changes(self):
 
2035
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2036
        root = revision_tree.inventory.root.file_id
 
2037
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
 
2038
                          (root, root), ('a', 'a'), ('file', 'file'),
 
2039
                          (False, False))],
 
2040
                          list(preview_tree.iter_changes(revision_tree)))
 
2041
 
 
2042
    def test_wrong_tree_value_error(self):
 
2043
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2044
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
2045
                              preview_tree)
 
2046
        self.assertEqual('from_tree must be transform source tree.', str(e))
 
2047
 
 
2048
    def test_include_unchanged_value_error(self):
 
2049
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2050
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
2051
                              revision_tree, include_unchanged=True)
 
2052
        self.assertEqual('include_unchanged is not supported', str(e))
 
2053
 
 
2054
    def test_specific_files(self):
 
2055
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2056
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
2057
                              revision_tree, specific_files=['pete'])
 
2058
        self.assertEqual('specific_files is not supported', str(e))
 
2059
 
 
2060
    def test_want_unversioned_value_error(self):
 
2061
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2062
        e = self.assertRaises(ValueError, preview_tree.iter_changes,
 
2063
                              revision_tree, want_unversioned=True)
 
2064
        self.assertEqual('want_unversioned is not supported', str(e))
 
2065
 
 
2066
    def test_ignore_extra_trees_no_specific_files(self):
 
2067
        # extra_trees is harmless without specific_files, so we'll silently
 
2068
        # accept it, even though we won't use it.
 
2069
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2070
        preview_tree.iter_changes(revision_tree, extra_trees=[preview_tree])
 
2071
 
 
2072
    def test_ignore_require_versioned_no_specific_files(self):
 
2073
        # require_versioned is meaningless without specific_files.
 
2074
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2075
        preview_tree.iter_changes(revision_tree, require_versioned=False)
 
2076
 
 
2077
    def test_ignore_pb(self):
 
2078
        # pb could be supported, but TT.iter_changes doesn't support it.
 
2079
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2080
        preview_tree.iter_changes(revision_tree, pb=progress.DummyProgress())
 
2081
 
 
2082
    def test_kind(self):
 
2083
        revision_tree = self.create_tree()
 
2084
        preview = TransformPreview(revision_tree)
 
2085
        self.addCleanup(preview.finalize)
 
2086
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
2087
        preview.new_directory('directory', preview.root, 'dir-id')
 
2088
        preview_tree = preview.get_preview_tree()
 
2089
        self.assertEqual('file', preview_tree.kind('file-id'))
 
2090
        self.assertEqual('directory', preview_tree.kind('dir-id'))
 
2091
 
 
2092
    def test_get_file_mtime(self):
 
2093
        preview = self.get_empty_preview()
 
2094
        file_trans_id = preview.new_file('file', preview.root, 'contents',
 
2095
                                         'file-id')
 
2096
        limbo_path = preview._limbo_name(file_trans_id)
 
2097
        preview_tree = preview.get_preview_tree()
 
2098
        self.assertEqual(os.stat(limbo_path).st_mtime,
 
2099
                         preview_tree.get_file_mtime('file-id'))
 
2100
 
 
2101
    def test_get_file(self):
 
2102
        preview = self.get_empty_preview()
 
2103
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
2104
        preview_tree = preview.get_preview_tree()
 
2105
        tree_file = preview_tree.get_file('file-id')
 
2106
        try:
 
2107
            self.assertEqual('contents', tree_file.read())
 
2108
        finally:
 
2109
            tree_file.close()
 
2110
 
 
2111
    def test_get_symlink_target(self):
 
2112
        self.requireFeature(SymlinkFeature)
 
2113
        preview = self.get_empty_preview()
 
2114
        preview.new_symlink('symlink', preview.root, 'target', 'symlink-id')
 
2115
        preview_tree = preview.get_preview_tree()
 
2116
        self.assertEqual('target',
 
2117
                         preview_tree.get_symlink_target('symlink-id'))
 
2118
 
 
2119
    def test_all_file_ids(self):
 
2120
        tree = self.make_branch_and_tree('tree')
 
2121
        self.build_tree(['tree/a', 'tree/b', 'tree/c'])
 
2122
        tree.add(['a', 'b', 'c'], ['a-id', 'b-id', 'c-id'])
 
2123
        preview = TransformPreview(tree)
 
2124
        self.addCleanup(preview.finalize)
 
2125
        preview.unversion_file(preview.trans_id_file_id('b-id'))
 
2126
        c_trans_id = preview.trans_id_file_id('c-id')
 
2127
        preview.unversion_file(c_trans_id)
 
2128
        preview.version_file('c-id', c_trans_id)
 
2129
        preview_tree = preview.get_preview_tree()
 
2130
        self.assertEqual(set(['a-id', 'c-id', tree.get_root_id()]),
 
2131
                         preview_tree.all_file_ids())
 
2132
 
 
2133
    def test_path2id_deleted_unchanged(self):
 
2134
        tree = self.make_branch_and_tree('tree')
 
2135
        self.build_tree(['tree/unchanged', 'tree/deleted'])
 
2136
        tree.add(['unchanged', 'deleted'], ['unchanged-id', 'deleted-id'])
 
2137
        preview = TransformPreview(tree)
 
2138
        self.addCleanup(preview.finalize)
 
2139
        preview.unversion_file(preview.trans_id_file_id('deleted-id'))
 
2140
        preview_tree = preview.get_preview_tree()
 
2141
        self.assertEqual('unchanged-id', preview_tree.path2id('unchanged'))
 
2142
        self.assertIs(None, preview_tree.path2id('deleted'))
 
2143
 
 
2144
    def test_path2id_created(self):
 
2145
        tree = self.make_branch_and_tree('tree')
 
2146
        self.build_tree(['tree/unchanged'])
 
2147
        tree.add(['unchanged'], ['unchanged-id'])
 
2148
        preview = TransformPreview(tree)
 
2149
        self.addCleanup(preview.finalize)
 
2150
        preview.new_file('new', preview.trans_id_file_id('unchanged-id'),
 
2151
            'contents', 'new-id')
 
2152
        preview_tree = preview.get_preview_tree()
 
2153
        self.assertEqual('new-id', preview_tree.path2id('unchanged/new'))
 
2154
 
 
2155
    def test_path2id_moved(self):
 
2156
        tree = self.make_branch_and_tree('tree')
 
2157
        self.build_tree(['tree/old_parent/', 'tree/old_parent/child'])
 
2158
        tree.add(['old_parent', 'old_parent/child'],
 
2159
                 ['old_parent-id', 'child-id'])
 
2160
        preview = TransformPreview(tree)
 
2161
        self.addCleanup(preview.finalize)
 
2162
        new_parent = preview.new_directory('new_parent', preview.root,
 
2163
                                           'new_parent-id')
 
2164
        preview.adjust_path('child', new_parent,
 
2165
                            preview.trans_id_file_id('child-id'))
 
2166
        preview_tree = preview.get_preview_tree()
 
2167
        self.assertIs(None, preview_tree.path2id('old_parent/child'))
 
2168
        self.assertEqual('child-id', preview_tree.path2id('new_parent/child'))
 
2169
 
 
2170
    def test_path2id_renamed_parent(self):
 
2171
        tree = self.make_branch_and_tree('tree')
 
2172
        self.build_tree(['tree/old_name/', 'tree/old_name/child'])
 
2173
        tree.add(['old_name', 'old_name/child'],
 
2174
                 ['parent-id', 'child-id'])
 
2175
        preview = TransformPreview(tree)
 
2176
        self.addCleanup(preview.finalize)
 
2177
        preview.adjust_path('new_name', preview.root,
 
2178
                            preview.trans_id_file_id('parent-id'))
 
2179
        preview_tree = preview.get_preview_tree()
 
2180
        self.assertIs(None, preview_tree.path2id('old_name/child'))
 
2181
        self.assertEqual('child-id', preview_tree.path2id('new_name/child'))
 
2182
 
 
2183
    def assertMatchingIterEntries(self, tt, specific_file_ids=None):
 
2184
        preview_tree = tt.get_preview_tree()
 
2185
        preview_result = list(preview_tree.iter_entries_by_dir(
 
2186
                              specific_file_ids))
 
2187
        tree = tt._tree
 
2188
        tt.apply()
 
2189
        actual_result = list(tree.iter_entries_by_dir(specific_file_ids))
 
2190
        self.assertEqual(actual_result, preview_result)
 
2191
 
 
2192
    def test_iter_entries_by_dir_new(self):
 
2193
        tree = self.make_branch_and_tree('tree')
 
2194
        tt = TreeTransform(tree)
 
2195
        tt.new_file('new', tt.root, 'contents', 'new-id')
 
2196
        self.assertMatchingIterEntries(tt)
 
2197
 
 
2198
    def test_iter_entries_by_dir_deleted(self):
 
2199
        tree = self.make_branch_and_tree('tree')
 
2200
        self.build_tree(['tree/deleted'])
 
2201
        tree.add('deleted', 'deleted-id')
 
2202
        tt = TreeTransform(tree)
 
2203
        tt.delete_contents(tt.trans_id_file_id('deleted-id'))
 
2204
        self.assertMatchingIterEntries(tt)
 
2205
 
 
2206
    def test_iter_entries_by_dir_unversioned(self):
 
2207
        tree = self.make_branch_and_tree('tree')
 
2208
        self.build_tree(['tree/removed'])
 
2209
        tree.add('removed', 'removed-id')
 
2210
        tt = TreeTransform(tree)
 
2211
        tt.unversion_file(tt.trans_id_file_id('removed-id'))
 
2212
        self.assertMatchingIterEntries(tt)
 
2213
 
 
2214
    def test_iter_entries_by_dir_moved(self):
 
2215
        tree = self.make_branch_and_tree('tree')
 
2216
        self.build_tree(['tree/moved', 'tree/new_parent/'])
 
2217
        tree.add(['moved', 'new_parent'], ['moved-id', 'new_parent-id'])
 
2218
        tt = TreeTransform(tree)
 
2219
        tt.adjust_path('moved', tt.trans_id_file_id('new_parent-id'),
 
2220
                       tt.trans_id_file_id('moved-id'))
 
2221
        self.assertMatchingIterEntries(tt)
 
2222
 
 
2223
    def test_iter_entries_by_dir_specific_file_ids(self):
 
2224
        tree = self.make_branch_and_tree('tree')
 
2225
        tree.set_root_id('tree-root-id')
 
2226
        self.build_tree(['tree/parent/', 'tree/parent/child'])
 
2227
        tree.add(['parent', 'parent/child'], ['parent-id', 'child-id'])
 
2228
        tt = TreeTransform(tree)
 
2229
        self.assertMatchingIterEntries(tt, ['tree-root-id', 'child-id'])
 
2230
 
 
2231
    def test_symlink_content_summary(self):
 
2232
        self.requireFeature(SymlinkFeature)
 
2233
        preview = self.get_empty_preview()
 
2234
        preview.new_symlink('path', preview.root, 'target', 'path-id')
 
2235
        summary = preview.get_preview_tree().path_content_summary('path')
 
2236
        self.assertEqual(('symlink', None, None, 'target'), summary)
 
2237
 
 
2238
    def test_missing_content_summary(self):
 
2239
        preview = self.get_empty_preview()
 
2240
        summary = preview.get_preview_tree().path_content_summary('path')
 
2241
        self.assertEqual(('missing', None, None, None), summary)
 
2242
 
 
2243
    def test_deleted_content_summary(self):
 
2244
        tree = self.make_branch_and_tree('tree')
 
2245
        self.build_tree(['tree/path/'])
 
2246
        tree.add('path')
 
2247
        preview = TransformPreview(tree)
 
2248
        self.addCleanup(preview.finalize)
 
2249
        preview.delete_contents(preview.trans_id_tree_path('path'))
 
2250
        summary = preview.get_preview_tree().path_content_summary('path')
 
2251
        self.assertEqual(('missing', None, None, None), summary)
 
2252
 
 
2253
    def test_file_content_summary_executable(self):
 
2254
        if not osutils.supports_executable():
 
2255
            raise TestNotApplicable()
 
2256
        preview = self.get_empty_preview()
 
2257
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
 
2258
        preview.set_executability(True, path_id)
 
2259
        summary = preview.get_preview_tree().path_content_summary('path')
 
2260
        self.assertEqual(4, len(summary))
 
2261
        self.assertEqual('file', summary[0])
 
2262
        # size must be known
 
2263
        self.assertEqual(len('contents'), summary[1])
 
2264
        # executable
 
2265
        self.assertEqual(True, summary[2])
 
2266
        # will not have hash (not cheap to determine)
 
2267
        self.assertIs(None, summary[3])
 
2268
 
 
2269
    def test_change_executability(self):
 
2270
        if not osutils.supports_executable():
 
2271
            raise TestNotApplicable()
 
2272
        tree = self.make_branch_and_tree('tree')
 
2273
        self.build_tree(['tree/path'])
 
2274
        tree.add('path')
 
2275
        preview = TransformPreview(tree)
 
2276
        self.addCleanup(preview.finalize)
 
2277
        path_id = preview.trans_id_tree_path('path')
 
2278
        preview.set_executability(True, path_id)
 
2279
        summary = preview.get_preview_tree().path_content_summary('path')
 
2280
        self.assertEqual(True, summary[2])
 
2281
 
 
2282
    def test_file_content_summary_non_exec(self):
 
2283
        preview = self.get_empty_preview()
 
2284
        preview.new_file('path', preview.root, 'contents', 'path-id')
 
2285
        summary = preview.get_preview_tree().path_content_summary('path')
 
2286
        self.assertEqual(4, len(summary))
 
2287
        self.assertEqual('file', summary[0])
 
2288
        # size must be known
 
2289
        self.assertEqual(len('contents'), summary[1])
 
2290
        # not executable
 
2291
        if osutils.supports_executable():
 
2292
            self.assertEqual(False, summary[2])
 
2293
        else:
 
2294
            self.assertEqual(None, summary[2])
 
2295
        # will not have hash (not cheap to determine)
 
2296
        self.assertIs(None, summary[3])
 
2297
 
 
2298
    def test_dir_content_summary(self):
 
2299
        preview = self.get_empty_preview()
 
2300
        preview.new_directory('path', preview.root, 'path-id')
 
2301
        summary = preview.get_preview_tree().path_content_summary('path')
 
2302
        self.assertEqual(('directory', None, None, None), summary)
 
2303
 
 
2304
    def test_tree_content_summary(self):
 
2305
        preview = self.get_empty_preview()
 
2306
        path = preview.new_directory('path', preview.root, 'path-id')
 
2307
        preview.set_tree_reference('rev-1', path)
 
2308
        summary = preview.get_preview_tree().path_content_summary('path')
 
2309
        self.assertEqual(4, len(summary))
 
2310
        self.assertEqual('tree-reference', summary[0])
 
2311
 
 
2312
    def test_annotate(self):
 
2313
        tree = self.make_branch_and_tree('tree')
 
2314
        self.build_tree_contents([('tree/file', 'a\n')])
 
2315
        tree.add('file', 'file-id')
 
2316
        tree.commit('a', rev_id='one')
 
2317
        self.build_tree_contents([('tree/file', 'a\nb\n')])
 
2318
        preview = TransformPreview(tree)
 
2319
        self.addCleanup(preview.finalize)
 
2320
        file_trans_id = preview.trans_id_file_id('file-id')
 
2321
        preview.delete_contents(file_trans_id)
 
2322
        preview.create_file('a\nb\nc\n', file_trans_id)
 
2323
        preview_tree = preview.get_preview_tree()
 
2324
        expected = [
 
2325
            ('one', 'a\n'),
 
2326
            ('me:', 'b\n'),
 
2327
            ('me:', 'c\n'),
 
2328
        ]
 
2329
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
2330
        self.assertEqual(expected, annotation)
 
2331
 
 
2332
    def test_annotate_missing(self):
 
2333
        preview = self.get_empty_preview()
 
2334
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
 
2335
        preview_tree = preview.get_preview_tree()
 
2336
        expected = [
 
2337
            ('me:', 'a\n'),
 
2338
            ('me:', 'b\n'),
 
2339
            ('me:', 'c\n'),
 
2340
         ]
 
2341
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
2342
        self.assertEqual(expected, annotation)
 
2343
 
 
2344
    def test_annotate_rename(self):
 
2345
        tree = self.make_branch_and_tree('tree')
 
2346
        self.build_tree_contents([('tree/file', 'a\n')])
 
2347
        tree.add('file', 'file-id')
 
2348
        tree.commit('a', rev_id='one')
 
2349
        preview = TransformPreview(tree)
 
2350
        self.addCleanup(preview.finalize)
 
2351
        file_trans_id = preview.trans_id_file_id('file-id')
 
2352
        preview.adjust_path('newname', preview.root, file_trans_id)
 
2353
        preview_tree = preview.get_preview_tree()
 
2354
        expected = [
 
2355
            ('one', 'a\n'),
 
2356
        ]
 
2357
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
2358
        self.assertEqual(expected, annotation)
 
2359
 
 
2360
    def test_annotate_deleted(self):
 
2361
        tree = self.make_branch_and_tree('tree')
 
2362
        self.build_tree_contents([('tree/file', 'a\n')])
 
2363
        tree.add('file', 'file-id')
 
2364
        tree.commit('a', rev_id='one')
 
2365
        self.build_tree_contents([('tree/file', 'a\nb\n')])
 
2366
        preview = TransformPreview(tree)
 
2367
        self.addCleanup(preview.finalize)
 
2368
        file_trans_id = preview.trans_id_file_id('file-id')
 
2369
        preview.delete_contents(file_trans_id)
 
2370
        preview_tree = preview.get_preview_tree()
 
2371
        annotation = preview_tree.annotate_iter('file-id', 'me:')
 
2372
        self.assertIs(None, annotation)
 
2373
 
 
2374
    def test_stored_kind(self):
 
2375
        preview = self.get_empty_preview()
 
2376
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
 
2377
        preview_tree = preview.get_preview_tree()
 
2378
        self.assertEqual('file', preview_tree.stored_kind('file-id'))
 
2379
 
 
2380
    def test_is_executable(self):
 
2381
        preview = self.get_empty_preview()
 
2382
        preview.new_file('file', preview.root, 'a\nb\nc\n', 'file-id')
 
2383
        preview.set_executability(True, preview.trans_id_file_id('file-id'))
 
2384
        preview_tree = preview.get_preview_tree()
 
2385
        self.assertEqual(True, preview_tree.is_executable('file-id'))
 
2386
 
 
2387
    def test_get_set_parent_ids(self):
 
2388
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
2389
        self.assertEqual([], preview_tree.get_parent_ids())
 
2390
        preview_tree.set_parent_ids(['rev-1'])
 
2391
        self.assertEqual(['rev-1'], preview_tree.get_parent_ids())
 
2392
 
 
2393
    def test_plan_file_merge(self):
 
2394
        work_a = self.make_branch_and_tree('wta')
 
2395
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
 
2396
        work_a.add('file', 'file-id')
 
2397
        base_id = work_a.commit('base version')
 
2398
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
 
2399
        preview = TransformPreview(work_a)
 
2400
        self.addCleanup(preview.finalize)
 
2401
        trans_id = preview.trans_id_file_id('file-id')
 
2402
        preview.delete_contents(trans_id)
 
2403
        preview.create_file('b\nc\nd\ne\n', trans_id)
 
2404
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
 
2405
        tree_a = preview.get_preview_tree()
 
2406
        tree_a.set_parent_ids([base_id])
 
2407
        self.assertEqual([
 
2408
            ('killed-a', 'a\n'),
 
2409
            ('killed-b', 'b\n'),
 
2410
            ('unchanged', 'c\n'),
 
2411
            ('unchanged', 'd\n'),
 
2412
            ('new-a', 'e\n'),
 
2413
            ('new-b', 'f\n'),
 
2414
        ], list(tree_a.plan_file_merge('file-id', tree_b)))
 
2415
 
 
2416
    def test_plan_file_merge_revision_tree(self):
 
2417
        work_a = self.make_branch_and_tree('wta')
 
2418
        self.build_tree_contents([('wta/file', 'a\nb\nc\nd\n')])
 
2419
        work_a.add('file', 'file-id')
 
2420
        base_id = work_a.commit('base version')
 
2421
        tree_b = work_a.bzrdir.sprout('wtb').open_workingtree()
 
2422
        preview = TransformPreview(work_a.basis_tree())
 
2423
        self.addCleanup(preview.finalize)
 
2424
        trans_id = preview.trans_id_file_id('file-id')
 
2425
        preview.delete_contents(trans_id)
 
2426
        preview.create_file('b\nc\nd\ne\n', trans_id)
 
2427
        self.build_tree_contents([('wtb/file', 'a\nc\nd\nf\n')])
 
2428
        tree_a = preview.get_preview_tree()
 
2429
        tree_a.set_parent_ids([base_id])
 
2430
        self.assertEqual([
 
2431
            ('killed-a', 'a\n'),
 
2432
            ('killed-b', 'b\n'),
 
2433
            ('unchanged', 'c\n'),
 
2434
            ('unchanged', 'd\n'),
 
2435
            ('new-a', 'e\n'),
 
2436
            ('new-b', 'f\n'),
 
2437
        ], list(tree_a.plan_file_merge('file-id', tree_b)))