~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

  • Committer: Martin Pool
  • Date: 2007-04-04 06:17:31 UTC
  • mto: This revision was merged to the branch mainline in revision 2397.
  • Revision ID: mbp@sourcefrog.net-20070404061731-tt2xrzllqhbodn83
Contents of TODO file moved into bug tracker

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests of the dirstate functionality being built for WorkingTreeFormat4."""
18
18
 
19
19
import bisect
20
20
import os
 
21
import time
21
22
 
22
23
from bzrlib import (
23
24
    dirstate,
24
25
    errors,
25
 
    inventory,
26
 
    memorytree,
27
26
    osutils,
28
 
    revision as _mod_revision,
29
 
    tests,
30
27
    )
31
 
from bzrlib.tests import test_osutils
 
28
from bzrlib.memorytree import MemoryTree
 
29
from bzrlib.osutils import has_symlinks
 
30
from bzrlib.tests import (
 
31
        TestCase,
 
32
        TestCaseWithTransport,
 
33
        TestSkipped,
 
34
        )
32
35
 
33
36
 
34
37
# TODO:
44
47
# set_path_id  setting id when state is in memory modified
45
48
 
46
49
 
47
 
def load_tests(basic_tests, module, loader):
48
 
    suite = loader.suiteClass()
49
 
    dir_reader_tests, remaining_tests = tests.split_suite_by_condition(
50
 
        basic_tests, tests.condition_isinstance(TestCaseWithDirState))
51
 
    tests.multiply_tests(dir_reader_tests,
52
 
                         test_osutils.dir_reader_scenarios(), suite)
53
 
    suite.addTest(remaining_tests)
54
 
    return suite
55
 
 
56
 
 
57
 
class TestCaseWithDirState(tests.TestCaseWithTransport):
 
50
class TestCaseWithDirState(TestCaseWithTransport):
58
51
    """Helper functions for creating DirState objects with various content."""
59
52
 
60
 
    # Set by load_tests
61
 
    _dir_reader_class = None
62
 
    _native_to_unicode = None # Not used yet
63
 
 
64
 
    def setUp(self):
65
 
        tests.TestCaseWithTransport.setUp(self)
66
 
 
67
 
        # Save platform specific info and reset it
68
 
        cur_dir_reader = osutils._selected_dir_reader
69
 
 
70
 
        def restore():
71
 
            osutils._selected_dir_reader = cur_dir_reader
72
 
        self.addCleanup(restore)
73
 
 
74
 
        osutils._selected_dir_reader = self._dir_reader_class()
75
 
 
76
53
    def create_empty_dirstate(self):
77
54
        """Return a locked but empty dirstate"""
78
55
        state = dirstate.DirState.initialize('dirstate')
186
163
        """
187
164
        # The state should already be write locked, since we just had to do
188
165
        # some operation to get here.
189
 
        self.assertTrue(state._lock_token is not None)
 
166
        assert state._lock_token is not None
190
167
        try:
191
168
            self.assertEqual(expected_result[0],  state.get_parent_ids())
192
169
            # there should be no ghosts in this tree.
196
173
            state.save()
197
174
        finally:
198
175
            state.unlock()
199
 
        del state
 
176
        del state # Callers should unlock
200
177
        state = dirstate.DirState.on_file('dirstate')
201
178
        state.lock_read()
202
179
        try:
212
189
              c
213
190
              d/
214
191
                e
215
 
            b-c
216
192
            f
217
193
        """
218
194
        tree = self.make_branch_and_tree('tree')
219
 
        paths = ['a', 'b/', 'b/c', 'b/d/', 'b/d/e', 'b-c', 'f']
220
 
        file_ids = ['a-id', 'b-id', 'c-id', 'd-id', 'e-id', 'b-c-id', 'f-id']
 
195
        paths = ['a', 'b/', 'b/c', 'b/d/', 'b/d/e', 'f']
 
196
        file_ids = ['a-id', 'b-id', 'c-id', 'd-id', 'e-id', 'f-id']
221
197
        self.build_tree(['tree/' + p for p in paths])
222
198
        tree.set_root_id('TREE_ROOT')
223
199
        tree.add([p.rstrip('/') for p in paths], file_ids)
224
200
        tree.commit('initial', rev_id='rev-1')
225
201
        revision_id = 'rev-1'
226
202
        # a_packed_stat = dirstate.pack_stat(os.stat('tree/a'))
227
 
        t = self.get_transport('tree')
 
203
        t = self.get_transport().clone('tree')
228
204
        a_text = t.get_bytes('a')
229
205
        a_sha = osutils.sha_string(a_text)
230
206
        a_len = len(a_text)
238
214
        e_text = t.get_bytes('b/d/e')
239
215
        e_sha = osutils.sha_string(e_text)
240
216
        e_len = len(e_text)
241
 
        b_c_text = t.get_bytes('b-c')
242
 
        b_c_sha = osutils.sha_string(b_c_text)
243
 
        b_c_len = len(b_c_text)
244
217
        # f_packed_stat = dirstate.pack_stat(os.stat('tree/f'))
245
218
        f_text = t.get_bytes('f')
246
219
        f_sha = osutils.sha_string(f_text)
271
244
                      ('f', '', 0, False, null_stat),
272
245
                      ('f', e_sha, e_len, False, revision_id),
273
246
                     ]),
274
 
            'b-c':(('', 'b-c', 'b-c-id'), [
275
 
                      ('f', '', 0, False, null_stat),
276
 
                      ('f', b_c_sha, b_c_len, False, revision_id),
277
 
                     ]),
278
247
            'f':(('', 'f', 'f-id'), [
279
248
                  ('f', '', 0, False, null_stat),
280
249
                  ('f', f_sha, f_len, False, revision_id),
307
276
        tree, state, expected = self.create_basic_dirstate()
308
277
        # Now we will just remove and add every file so we get an extra entry
309
278
        # per entry. Unversion in reverse order so we handle subdirs
310
 
        tree.unversion(['f-id', 'b-c-id', 'e-id', 'd-id', 'c-id', 'b-id', 'a-id'])
311
 
        tree.add(['a', 'b', 'b/c', 'b/d', 'b/d/e', 'b-c', 'f'],
312
 
                 ['a-id2', 'b-id2', 'c-id2', 'd-id2', 'e-id2', 'b-c-id2', 'f-id2'])
 
279
        tree.unversion(['f-id', 'e-id', 'd-id', 'c-id', 'b-id', 'a-id'])
 
280
        tree.add(['a', 'b', 'b/c', 'b/d', 'b/d/e', 'f'],
 
281
                 ['a-id2', 'b-id2', 'c-id2', 'd-id2', 'e-id2', 'f-id2'])
313
282
 
314
283
        # Update the expected dictionary.
315
 
        for path in ['a', 'b', 'b/c', 'b/d', 'b/d/e', 'b-c', 'f']:
 
284
        for path in ['a', 'b', 'b/c', 'b/d', 'b/d/e', 'f']:
316
285
            orig = expected[path]
317
286
            path2 = path + '2'
318
287
            # This record was deleted in the current tree
375
344
            state.lock_read()
376
345
        return tree, state, expected
377
346
 
378
 
 
379
347
class TestTreeToDirState(TestCaseWithDirState):
380
348
 
381
349
    def test_empty_to_dirstate(self):
383
351
        # There are no files on disk and no parents
384
352
        tree = self.make_branch_and_tree('tree')
385
353
        expected_result = ([], [
386
 
            (('', '', tree.get_root_id()), # common details
 
354
            (('', '', tree.path2id('')), # common details
387
355
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
388
356
             ])])
389
357
        state = dirstate.DirState.from_tree(tree, 'dirstate')
396
364
        rev_id = tree.commit('first post').encode('utf8')
397
365
        root_stat_pack = dirstate.pack_stat(os.stat(tree.basedir))
398
366
        expected_result = ([rev_id], [
399
 
            (('', '', tree.get_root_id()), # common details
 
367
            (('', '', tree.path2id('')), # common details
400
368
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
401
369
              ('d', '', 0, False, rev_id), # first parent details
402
370
             ])])
416
384
        rev_id2 = tree2.commit('second post', allow_pointless=True)
417
385
        tree.merge_from_branch(tree2.branch)
418
386
        expected_result = ([rev_id, rev_id2], [
419
 
            (('', '', tree.get_root_id()), # common details
 
387
            (('', '', tree.path2id('')), # common details
420
388
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
421
389
              ('d', '', 0, False, rev_id), # first parent details
422
390
              ('d', '', 0, False, rev_id2), # second parent details
435
403
        tree = self.make_branch_and_tree('tree')
436
404
        self.build_tree(['tree/unknown'])
437
405
        expected_result = ([], [
438
 
            (('', '', tree.get_root_id()), # common details
 
406
            (('', '', tree.path2id('')), # common details
439
407
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
440
408
             ])])
441
409
        state = dirstate.DirState.from_tree(tree, 'dirstate')
444
412
    def get_tree_with_a_file(self):
445
413
        tree = self.make_branch_and_tree('tree')
446
414
        self.build_tree(['tree/a file'])
447
 
        tree.add('a file', 'a-file-id')
 
415
        tree.add('a file', 'a file id')
448
416
        return tree
449
417
 
450
418
    def test_non_empty_no_parents_to_dirstate(self):
452
420
        # There are files on disk and no parents
453
421
        tree = self.get_tree_with_a_file()
454
422
        expected_result = ([], [
455
 
            (('', '', tree.get_root_id()), # common details
 
423
            (('', '', tree.path2id('')), # common details
456
424
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
457
425
             ]),
458
 
            (('', 'a file', 'a-file-id'), # common
 
426
            (('', 'a file', 'a file id'), # common
459
427
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
460
428
             ]),
461
429
            ])
470
438
        # and length:
471
439
        self.build_tree_contents([('tree/a file', 'new content\n')])
472
440
        expected_result = ([rev_id], [
473
 
            (('', '', tree.get_root_id()), # common details
 
441
            (('', '', tree.path2id('')), # common details
474
442
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
475
443
              ('d', '', 0, False, rev_id), # first parent details
476
444
             ]),
477
 
            (('', 'a file', 'a-file-id'), # common
 
445
            (('', 'a file', 'a file id'), # common
478
446
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
479
447
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
480
448
               rev_id), # first parent
497
465
        # and length again, giving us three distinct values:
498
466
        self.build_tree_contents([('tree/a file', 'new content\n')])
499
467
        expected_result = ([rev_id, rev_id2], [
500
 
            (('', '', tree.get_root_id()), # common details
 
468
            (('', '', tree.path2id('')), # common details
501
469
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
502
470
              ('d', '', 0, False, rev_id), # first parent details
503
471
              ('d', '', 0, False, rev_id2), # second parent details
504
472
             ]),
505
 
            (('', 'a file', 'a-file-id'), # common
 
473
            (('', 'a file', 'a file id'), # common
506
474
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
507
475
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
508
476
               rev_id), # first parent
549
517
        # get a state object
550
518
        # no parents, default tree content
551
519
        expected_result = ([], [
552
 
            (('', '', tree.get_root_id()), # common details
 
520
            (('', '', tree.path2id('')), # common details
553
521
             # current tree details, but new from_tree skips statting, it
554
522
             # uses set_state_from_inventory, and thus depends on the
555
523
             # inventory state.
586
554
        state.lock_read()
587
555
        try:
588
556
            entry = state._get_entry(0, path_utf8='a-file')
589
 
            # The current size should be 0 (default)
590
 
            self.assertEqual(0, entry[1][0][2])
 
557
            # The current sha1 sum should be empty
 
558
            self.assertEqual('', entry[1][0][1])
591
559
            # We should have a real entry.
592
560
            self.assertNotEqual((None, None), entry)
593
 
            # Make sure everything is old enough
594
 
            state._sha_cutoff_time()
595
 
            state._cutoff_time += 10
596
 
            # Change the file length
597
 
            self.build_tree_contents([('a-file', 'shorter')])
598
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
599
 
                os.lstat('a-file'))
600
 
            # new file, no cached sha:
601
 
            self.assertEqual(None, sha1sum)
 
561
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
562
            # We should have gotten a real sha1
 
563
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
564
                             sha1sum)
602
565
 
603
566
            # The dirblock has been updated
604
 
            self.assertEqual(7, entry[1][0][2])
 
567
            self.assertEqual(sha1sum, entry[1][0][1])
605
568
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
606
569
                             state._dirblock_state)
607
570
 
617
580
        state.lock_read()
618
581
        try:
619
582
            entry = state._get_entry(0, path_utf8='a-file')
620
 
            self.assertEqual(7, entry[1][0][2])
 
583
            self.assertEqual(sha1sum, entry[1][0][1])
621
584
        finally:
622
585
            state.unlock()
623
586
 
636
599
        state.lock_read()
637
600
        try:
638
601
            entry = state._get_entry(0, path_utf8='a-file')
639
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file',
640
 
                os.lstat('a-file'))
641
 
            # No sha - too new
642
 
            self.assertEqual(None, sha1sum)
 
602
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
603
            # We should have gotten a real sha1
 
604
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
 
605
                             sha1sum)
643
606
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
644
607
                             state._dirblock_state)
645
608
 
662
625
                state2.unlock()
663
626
        finally:
664
627
            state.unlock()
665
 
 
 
628
        
666
629
        # The file on disk should not be modified.
667
630
        state = dirstate.DirState.on_file('dirstate')
668
631
        state.lock_read()
672
635
        finally:
673
636
            state.unlock()
674
637
 
675
 
    def test_save_refuses_if_changes_aborted(self):
676
 
        self.build_tree(['a-file', 'a-dir/'])
677
 
        state = dirstate.DirState.initialize('dirstate')
678
 
        try:
679
 
            # No stat and no sha1 sum.
680
 
            state.add('a-file', 'a-file-id', 'file', None, '')
681
 
            state.save()
682
 
        finally:
683
 
            state.unlock()
684
 
 
685
 
        # The dirstate should include TREE_ROOT and 'a-file' and nothing else
686
 
        expected_blocks = [
687
 
            ('', [(('', '', 'TREE_ROOT'),
688
 
                   [('d', '', 0, False, dirstate.DirState.NULLSTAT)])]),
689
 
            ('', [(('', 'a-file', 'a-file-id'),
690
 
                   [('f', '', 0, False, dirstate.DirState.NULLSTAT)])]),
691
 
        ]
692
 
 
693
 
        state = dirstate.DirState.on_file('dirstate')
694
 
        state.lock_write()
695
 
        try:
696
 
            state._read_dirblocks_if_needed()
697
 
            self.assertEqual(expected_blocks, state._dirblocks)
698
 
 
699
 
            # Now modify the state, but mark it as inconsistent
700
 
            state.add('a-dir', 'a-dir-id', 'directory', None, '')
701
 
            state._changes_aborted = True
702
 
            state.save()
703
 
        finally:
704
 
            state.unlock()
705
 
 
706
 
        state = dirstate.DirState.on_file('dirstate')
707
 
        state.lock_read()
708
 
        try:
709
 
            state._read_dirblocks_if_needed()
710
 
            self.assertEqual(expected_blocks, state._dirblocks)
711
 
        finally:
712
 
            state.unlock()
713
 
 
714
638
 
715
639
class TestDirStateInitialize(TestCaseWithDirState):
716
640
 
724
648
        try:
725
649
            self.assertIsInstance(state, dirstate.DirState)
726
650
            lines = state.get_lines()
727
 
        finally:
 
651
            self.assertFileEqual(''.join(state.get_lines()),
 
652
                'dirstate')
 
653
            self.check_state_with_reopen(expected_result, state)
 
654
        except:
728
655
            state.unlock()
729
 
        # On win32 you can't read from a locked file, even within the same
730
 
        # process. So we have to unlock and release before we check the file
731
 
        # contents.
732
 
        self.assertFileEqual(''.join(lines), 'dirstate')
733
 
        state.lock_read() # check_state_with_reopen will unlock
734
 
        self.check_state_with_reopen(expected_result, state)
 
656
            raise
735
657
 
736
658
 
737
659
class TestDirStateManipulations(TestCaseWithDirState):
743
665
        try:
744
666
            tree1.add('')
745
667
            revid1 = tree1.commit('foo').encode('utf8')
746
 
            root_id = tree1.get_root_id()
 
668
            root_id = tree1.inventory.root.file_id
747
669
            inv = tree1.inventory
748
670
        finally:
749
671
            tree1.unlock()
764
686
            # This will unlock it
765
687
            self.check_state_with_reopen(expected_result, state)
766
688
 
767
 
    def test_set_state_from_inventory_preserves_hashcache(self):
768
 
        # https://bugs.launchpad.net/bzr/+bug/146176
769
 
        # set_state_from_inventory should preserve the stat and hash value for
770
 
        # workingtree files that are not changed by the inventory.
771
 
 
772
 
        tree = self.make_branch_and_tree('.')
773
 
        # depends on the default format using dirstate...
774
 
        tree.lock_write()
775
 
        try:
776
 
            # make a dirstate with some valid hashcache data
777
 
            # file on disk, but that's not needed for this test
778
 
            foo_contents = 'contents of foo'
779
 
            self.build_tree_contents([('foo', foo_contents)])
780
 
            tree.add('foo', 'foo-id')
781
 
 
782
 
            foo_stat = os.stat('foo')
783
 
            foo_packed = dirstate.pack_stat(foo_stat)
784
 
            foo_sha = osutils.sha_string(foo_contents)
785
 
            foo_size = len(foo_contents)
786
 
 
787
 
            # should not be cached yet, because the file's too fresh
788
 
            self.assertEqual(
789
 
                (('', 'foo', 'foo-id',),
790
 
                 [('f', '', 0, False, dirstate.DirState.NULLSTAT)]),
791
 
                tree._dirstate._get_entry(0, 'foo-id'))
792
 
            # poke in some hashcache information - it wouldn't normally be
793
 
            # stored because it's too fresh
794
 
            tree._dirstate.update_minimal(
795
 
                ('', 'foo', 'foo-id'),
796
 
                'f', False, foo_sha, foo_packed, foo_size, 'foo')
797
 
            # now should be cached
798
 
            self.assertEqual(
799
 
                (('', 'foo', 'foo-id',),
800
 
                 [('f', foo_sha, foo_size, False, foo_packed)]),
801
 
                tree._dirstate._get_entry(0, 'foo-id'))
802
 
 
803
 
            # extract the inventory, and add something to it
804
 
            inv = tree._get_inventory()
805
 
            # should see the file we poked in...
806
 
            self.assertTrue(inv.has_id('foo-id'))
807
 
            self.assertTrue(inv.has_filename('foo'))
808
 
            inv.add_path('bar', 'file', 'bar-id')
809
 
            tree._dirstate._validate()
810
 
            # this used to cause it to lose its hashcache
811
 
            tree._dirstate.set_state_from_inventory(inv)
812
 
            tree._dirstate._validate()
813
 
        finally:
814
 
            tree.unlock()
815
 
 
816
 
        tree.lock_read()
817
 
        try:
818
 
            # now check that the state still has the original hashcache value
819
 
            state = tree._dirstate
820
 
            state._validate()
821
 
            foo_tuple = state._get_entry(0, path_utf8='foo')
822
 
            self.assertEqual(
823
 
                (('', 'foo', 'foo-id',),
824
 
                 [('f', foo_sha, len(foo_contents), False,
825
 
                   dirstate.pack_stat(foo_stat))]),
826
 
                foo_tuple)
827
 
        finally:
828
 
            tree.unlock()
829
 
 
830
 
 
831
 
    def test_set_state_from_inventory_mixed_paths(self):
832
 
        tree1 = self.make_branch_and_tree('tree1')
833
 
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
834
 
                         'tree1/a/b/foo', 'tree1/a-b/bar'])
835
 
        tree1.lock_write()
836
 
        try:
837
 
            tree1.add(['a', 'a/b', 'a-b', 'a/b/foo', 'a-b/bar'],
838
 
                      ['a-id', 'b-id', 'a-b-id', 'foo-id', 'bar-id'])
839
 
            tree1.commit('rev1', rev_id='rev1')
840
 
            root_id = tree1.get_root_id()
841
 
            inv = tree1.inventory
842
 
        finally:
843
 
            tree1.unlock()
844
 
        expected_result1 = [('', '', root_id, 'd'),
845
 
                            ('', 'a', 'a-id', 'd'),
846
 
                            ('', 'a-b', 'a-b-id', 'd'),
847
 
                            ('a', 'b', 'b-id', 'd'),
848
 
                            ('a/b', 'foo', 'foo-id', 'f'),
849
 
                            ('a-b', 'bar', 'bar-id', 'f'),
850
 
                           ]
851
 
        expected_result2 = [('', '', root_id, 'd'),
852
 
                            ('', 'a', 'a-id', 'd'),
853
 
                            ('', 'a-b', 'a-b-id', 'd'),
854
 
                            ('a-b', 'bar', 'bar-id', 'f'),
855
 
                           ]
856
 
        state = dirstate.DirState.initialize('dirstate')
857
 
        try:
858
 
            state.set_state_from_inventory(inv)
859
 
            values = []
860
 
            for entry in state._iter_entries():
861
 
                values.append(entry[0] + entry[1][0][:1])
862
 
            self.assertEqual(expected_result1, values)
863
 
            del inv['b-id']
864
 
            state.set_state_from_inventory(inv)
865
 
            values = []
866
 
            for entry in state._iter_entries():
867
 
                values.append(entry[0] + entry[1][0][:1])
868
 
            self.assertEqual(expected_result2, values)
869
 
        finally:
870
 
            state.unlock()
871
 
 
872
689
    def test_set_path_id_no_parents(self):
873
690
        """The id of a path can be changed trivally with no parents."""
874
691
        state = dirstate.DirState.initialize('dirstate')
953
770
        finally:
954
771
            tree1.unlock()
955
772
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
956
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
773
        tree2 = MemoryTree.create_on_branch(branch2)
957
774
        tree2.lock_write()
958
775
        try:
959
776
            revid2 = tree2.commit('foo')
960
 
            root_id = tree2.get_root_id()
 
777
            root_id = tree2.inventory.root.file_id
961
778
        finally:
962
779
            tree2.unlock()
963
780
        state = dirstate.DirState.initialize('dirstate')
991
808
            state.set_parent_trees(
992
809
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
993
810
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
994
 
                 ('ghost-rev', tree2.branch.repository.revision_tree(
995
 
                                   _mod_revision.NULL_REVISION))),
 
811
                 ('ghost-rev', tree2.branch.repository.revision_tree(None))),
996
812
                ['ghost-rev'])
997
813
            self.assertEqual([revid1, revid2, 'ghost-rev'],
998
814
                             state.get_parent_ids())
1023
839
        finally:
1024
840
            tree1.unlock()
1025
841
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1026
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
842
        tree2 = MemoryTree.create_on_branch(branch2)
1027
843
        tree2.lock_write()
1028
844
        try:
1029
845
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
1030
846
            revid2 = tree2.commit('foo')
1031
 
            root_id = tree2.get_root_id()
 
847
            root_id = tree2.inventory.root.file_id
1032
848
        finally:
1033
849
            tree2.unlock()
1034
850
        # check the layout in memory
1074
890
            (('', '', 'TREE_ROOT'), [
1075
891
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1076
892
             ]),
1077
 
            (('', 'a file', 'a-file-id'), [
 
893
            (('', 'a file', 'a file id'), [
1078
894
             ('f', '1'*20, 19, False, dirstate.pack_stat(stat)), # current tree
1079
895
             ]),
1080
896
            ]
1081
897
        try:
1082
 
            state.add('a file', 'a-file-id', 'file', stat, '1'*20)
 
898
            state.add('a file', 'a file id', 'file', stat, '1'*20)
1083
899
            # having added it, it should be in the output of iter_entries.
1084
900
            self.assertEqual(expected_entries, list(state._iter_entries()))
1085
901
            # saving and reloading should not affect this.
1088
904
            state.unlock()
1089
905
        state = dirstate.DirState.on_file('dirstate')
1090
906
        state.lock_read()
1091
 
        self.addCleanup(state.unlock)
1092
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
907
        try:
 
908
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
909
        finally:
 
910
            state.unlock()
1093
911
 
1094
912
    def test_add_path_to_unversioned_directory(self):
1095
913
        """Adding a path to an unversioned directory should error.
1100
918
        """
1101
919
        self.build_tree(['unversioned/', 'unversioned/a file'])
1102
920
        state = dirstate.DirState.initialize('dirstate')
1103
 
        self.addCleanup(state.unlock)
1104
 
        self.assertRaises(errors.NotVersionedError, state.add,
1105
 
                          'unversioned/a file', 'a-file-id', 'file', None, None)
 
921
        try:
 
922
            self.assertRaises(errors.NotVersionedError, state.add,
 
923
                'unversioned/a file', 'a file id', 'file', None, None)
 
924
        finally:
 
925
            state.unlock()
1106
926
 
1107
927
    def test_add_directory_to_root_no_parents_all_data(self):
1108
928
        # The most trivial addition of a dir is when there are no parents and
1128
948
            state.unlock()
1129
949
        state = dirstate.DirState.on_file('dirstate')
1130
950
        state.lock_read()
1131
 
        self.addCleanup(state.unlock)
1132
951
        state._validate()
1133
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
952
        try:
 
953
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
954
        finally:
 
955
            state.unlock()
1134
956
 
1135
 
    def _test_add_symlink_to_root_no_parents_all_data(self, link_name, target):
 
957
    def test_add_symlink_to_root_no_parents_all_data(self):
1136
958
        # The most trivial addition of a symlink when there are no parents and
1137
959
        # its in the root and all data about the file is supplied
1138
960
        # bzr doesn't support fake symlinks on windows, yet.
1139
 
        self.requireFeature(tests.SymlinkFeature)
1140
 
        os.symlink(target, link_name)
1141
 
        stat = os.lstat(link_name)
 
961
        if not has_symlinks():
 
962
            raise TestSkipped("No symlink support")
 
963
        os.symlink('target', 'a link')
 
964
        stat = os.lstat('a link')
1142
965
        expected_entries = [
1143
966
            (('', '', 'TREE_ROOT'), [
1144
967
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1145
968
             ]),
1146
 
            (('', link_name.encode('UTF-8'), 'a link id'), [
1147
 
             ('l', target.encode('UTF-8'), stat[6],
1148
 
              False, dirstate.pack_stat(stat)), # current tree
 
969
            (('', 'a link', 'a link id'), [
 
970
             ('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
1149
971
             ]),
1150
972
            ]
1151
973
        state = dirstate.DirState.initialize('dirstate')
1152
974
        try:
1153
 
            state.add(link_name, 'a link id', 'symlink', stat,
1154
 
                      target.encode('UTF-8'))
 
975
            state.add('a link', 'a link id', 'symlink', stat, 'target')
1155
976
            # having added it, it should be in the output of iter_entries.
1156
977
            self.assertEqual(expected_entries, list(state._iter_entries()))
1157
978
            # saving and reloading should not affect this.
1160
981
            state.unlock()
1161
982
        state = dirstate.DirState.on_file('dirstate')
1162
983
        state.lock_read()
1163
 
        self.addCleanup(state.unlock)
1164
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
1165
 
 
1166
 
    def test_add_symlink_to_root_no_parents_all_data(self):
1167
 
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1168
 
 
1169
 
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1170
 
        self.requireFeature(tests.UnicodeFilenameFeature)
1171
 
        self._test_add_symlink_to_root_no_parents_all_data(
1172
 
            u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
 
984
        try:
 
985
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
986
        finally:
 
987
            state.unlock()
1173
988
 
1174
989
    def test_add_directory_and_child_no_parents_all_data(self):
1175
990
        # after adding a directory, we should be able to add children to it.
1183
998
            (('', 'a dir', 'a dir id'), [
1184
999
             ('d', '', 0, False, dirstate.pack_stat(dirstat)), # current tree
1185
1000
             ]),
1186
 
            (('a dir', 'a file', 'a-file-id'), [
 
1001
            (('a dir', 'a file', 'a file id'), [
1187
1002
             ('f', '1'*20, 25, False,
1188
1003
              dirstate.pack_stat(filestat)), # current tree details
1189
1004
             ]),
1191
1006
        state = dirstate.DirState.initialize('dirstate')
1192
1007
        try:
1193
1008
            state.add('a dir', 'a dir id', 'directory', dirstat, None)
1194
 
            state.add('a dir/a file', 'a-file-id', 'file', filestat, '1'*20)
 
1009
            state.add('a dir/a file', 'a file id', 'file', filestat, '1'*20)
1195
1010
            # added it, it should be in the output of iter_entries.
1196
1011
            self.assertEqual(expected_entries, list(state._iter_entries()))
1197
1012
            # saving and reloading should not affect this.
1200
1015
            state.unlock()
1201
1016
        state = dirstate.DirState.on_file('dirstate')
1202
1017
        state.lock_read()
1203
 
        self.addCleanup(state.unlock)
1204
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1018
        try:
 
1019
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1020
        finally:
 
1021
            state.unlock()
1205
1022
 
1206
1023
    def test_add_tree_reference(self):
1207
1024
        # make a dirstate and add a tree reference
1221
1038
            state.unlock()
1222
1039
        # now check we can read it back
1223
1040
        state.lock_read()
1224
 
        self.addCleanup(state.unlock)
1225
1041
        state._validate()
1226
 
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
1227
 
        self.assertEqual(entry, entry2)
1228
 
        self.assertEqual(entry, expected_entry)
1229
 
        # and lookup by id should work too
1230
 
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
1231
 
        self.assertEqual(entry, expected_entry)
 
1042
        try:
 
1043
            entry2 = state._get_entry(0, 'subdir-id', 'subdir')
 
1044
            self.assertEqual(entry, entry2)
 
1045
            self.assertEqual(entry, expected_entry)
 
1046
            # and lookup by id should work too
 
1047
            entry2 = state._get_entry(0, fileid_utf8='subdir-id')
 
1048
            self.assertEqual(entry, expected_entry)
 
1049
        finally:
 
1050
            state.unlock()
1232
1051
 
1233
1052
    def test_add_forbidden_names(self):
1234
1053
        state = dirstate.DirState.initialize('dirstate')
1456
1275
            state.unlock()
1457
1276
 
1458
1277
 
1459
 
class TestIterChildEntries(TestCaseWithDirState):
1460
 
 
1461
 
    def create_dirstate_with_two_trees(self):
1462
 
        """This dirstate contains multiple files and directories.
1463
 
 
1464
 
         /        a-root-value
1465
 
         a/       a-dir
1466
 
         b/       b-dir
1467
 
         c        c-file
1468
 
         d        d-file
1469
 
         a/e/     e-dir
1470
 
         a/f      f-file
1471
 
         b/g      g-file
1472
 
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
1473
 
 
1474
 
        Notice that a/e is an empty directory.
1475
 
 
1476
 
        There is one parent tree, which has the same shape with the following variations:
1477
 
        b/g in the parent is gone.
1478
 
        b/h in the parent has a different id
1479
 
        b/i is new in the parent
1480
 
        c is renamed to b/j in the parent
1481
 
 
1482
 
        :return: The dirstate, still write-locked.
1483
 
        """
1484
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
1485
 
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
1486
 
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
1487
 
        root_entry = ('', '', 'a-root-value'), [
1488
 
            ('d', '', 0, False, packed_stat),
1489
 
            ('d', '', 0, False, 'parent-revid'),
1490
 
            ]
1491
 
        a_entry = ('', 'a', 'a-dir'), [
1492
 
            ('d', '', 0, False, packed_stat),
1493
 
            ('d', '', 0, False, 'parent-revid'),
1494
 
            ]
1495
 
        b_entry = ('', 'b', 'b-dir'), [
1496
 
            ('d', '', 0, False, packed_stat),
1497
 
            ('d', '', 0, False, 'parent-revid'),
1498
 
            ]
1499
 
        c_entry = ('', 'c', 'c-file'), [
1500
 
            ('f', null_sha, 10, False, packed_stat),
1501
 
            ('r', 'b/j', 0, False, ''),
1502
 
            ]
1503
 
        d_entry = ('', 'd', 'd-file'), [
1504
 
            ('f', null_sha, 20, False, packed_stat),
1505
 
            ('f', 'd', 20, False, 'parent-revid'),
1506
 
            ]
1507
 
        e_entry = ('a', 'e', 'e-dir'), [
1508
 
            ('d', '', 0, False, packed_stat),
1509
 
            ('d', '', 0, False, 'parent-revid'),
1510
 
            ]
1511
 
        f_entry = ('a', 'f', 'f-file'), [
1512
 
            ('f', null_sha, 30, False, packed_stat),
1513
 
            ('f', 'f', 20, False, 'parent-revid'),
1514
 
            ]
1515
 
        g_entry = ('b', 'g', 'g-file'), [
1516
 
            ('f', null_sha, 30, False, packed_stat),
1517
 
            NULL_PARENT_DETAILS,
1518
 
            ]
1519
 
        h_entry1 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file1'), [
1520
 
            ('f', null_sha, 40, False, packed_stat),
1521
 
            NULL_PARENT_DETAILS,
1522
 
            ]
1523
 
        h_entry2 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file2'), [
1524
 
            NULL_PARENT_DETAILS,
1525
 
            ('f', 'h', 20, False, 'parent-revid'),
1526
 
            ]
1527
 
        i_entry = ('b', 'i', 'i-file'), [
1528
 
            NULL_PARENT_DETAILS,
1529
 
            ('f', 'h', 20, False, 'parent-revid'),
1530
 
            ]
1531
 
        j_entry = ('b', 'j', 'c-file'), [
1532
 
            ('r', 'c', 0, False, ''),
1533
 
            ('f', 'j', 20, False, 'parent-revid'),
1534
 
            ]
1535
 
        dirblocks = []
1536
 
        dirblocks.append(('', [root_entry]))
1537
 
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
1538
 
        dirblocks.append(('a', [e_entry, f_entry]))
1539
 
        dirblocks.append(('b', [g_entry, h_entry1, h_entry2, i_entry, j_entry]))
1540
 
        state = dirstate.DirState.initialize('dirstate')
1541
 
        state._validate()
1542
 
        try:
1543
 
            state._set_data(['parent'], dirblocks)
1544
 
        except:
1545
 
            state.unlock()
1546
 
            raise
1547
 
        return state, dirblocks
1548
 
 
1549
 
    def test_iter_children_b(self):
1550
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1551
 
        self.addCleanup(state.unlock)
1552
 
        expected_result = []
1553
 
        expected_result.append(dirblocks[3][1][2]) # h2
1554
 
        expected_result.append(dirblocks[3][1][3]) # i
1555
 
        expected_result.append(dirblocks[3][1][4]) # j
1556
 
        self.assertEqual(expected_result,
1557
 
            list(state._iter_child_entries(1, 'b')))
1558
 
 
1559
 
    def test_iter_child_root(self):
1560
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1561
 
        self.addCleanup(state.unlock)
1562
 
        expected_result = []
1563
 
        expected_result.append(dirblocks[1][1][0]) # a
1564
 
        expected_result.append(dirblocks[1][1][1]) # b
1565
 
        expected_result.append(dirblocks[1][1][3]) # d
1566
 
        expected_result.append(dirblocks[2][1][0]) # e
1567
 
        expected_result.append(dirblocks[2][1][1]) # f
1568
 
        expected_result.append(dirblocks[3][1][2]) # h2
1569
 
        expected_result.append(dirblocks[3][1][3]) # i
1570
 
        expected_result.append(dirblocks[3][1][4]) # j
1571
 
        self.assertEqual(expected_result,
1572
 
            list(state._iter_child_entries(1, '')))
1573
 
 
1574
 
 
1575
 
class TestDirstateSortOrder(tests.TestCaseWithTransport):
 
1278
class TestDirstateSortOrder(TestCaseWithTransport):
1576
1279
    """Test that DirState adds entries in the right order."""
1577
1280
 
1578
1281
    def test_add_sorting(self):
1627
1330
 
1628
1331
        # *really* cheesy way to just get an empty tree
1629
1332
        repo = self.make_repository('repo')
1630
 
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
 
1333
        empty_tree = repo.revision_tree(None)
1631
1334
        state.set_parent_trees([('null:', empty_tree)], [])
1632
1335
 
1633
1336
        dirblock_names = [d[0] for d in state._dirblocks]
1637
1340
class InstrumentedDirState(dirstate.DirState):
1638
1341
    """An DirState with instrumented sha1 functionality."""
1639
1342
 
1640
 
    def __init__(self, path, sha1_provider):
1641
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider)
 
1343
    def __init__(self, path):
 
1344
        super(InstrumentedDirState, self).__init__(path)
1642
1345
        self._time_offset = 0
1643
1346
        self._log = []
1644
 
        # member is dynamically set in DirState.__init__ to turn on trace
1645
 
        self._sha1_provider = sha1_provider
1646
 
        self._sha1_file = self._sha1_file_and_log
1647
1347
 
1648
1348
    def _sha_cutoff_time(self):
1649
1349
        timestamp = super(InstrumentedDirState, self)._sha_cutoff_time()
1650
1350
        self._cutoff_time = timestamp + self._time_offset
1651
1351
 
1652
 
    def _sha1_file_and_log(self, abspath):
 
1352
    def _sha1_file(self, abspath, entry):
1653
1353
        self._log.append(('sha1', abspath))
1654
 
        return self._sha1_provider.sha1(abspath)
 
1354
        return super(InstrumentedDirState, self)._sha1_file(abspath, entry)
1655
1355
 
1656
1356
    def _read_link(self, abspath, old_link):
1657
1357
        self._log.append(('read_link', abspath, old_link))
1688
1388
        self.st_ino = ino
1689
1389
        self.st_mode = mode
1690
1390
 
1691
 
    @staticmethod
1692
 
    def from_stat(st):
1693
 
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
1694
 
            st.st_ino, st.st_mode)
1695
 
 
1696
 
 
1697
 
class TestPackStat(tests.TestCaseWithTransport):
 
1391
 
 
1392
class TestUpdateEntry(TestCaseWithDirState):
 
1393
    """Test the DirState.update_entry functions"""
 
1394
 
 
1395
    def get_state_with_a(self):
 
1396
        """Create a DirState tracking a single object named 'a'"""
 
1397
        state = InstrumentedDirState.initialize('dirstate')
 
1398
        self.addCleanup(state.unlock)
 
1399
        state.add('a', 'a-id', 'file', None, '')
 
1400
        entry = state._get_entry(0, path_utf8='a')
 
1401
        return state, entry
 
1402
 
 
1403
    def test_update_entry(self):
 
1404
        state, entry = self.get_state_with_a()
 
1405
        self.build_tree(['a'])
 
1406
        # Add one where we don't provide the stat or sha already
 
1407
        self.assertEqual(('', 'a', 'a-id'), entry[0])
 
1408
        self.assertEqual([('f', '', 0, False, dirstate.DirState.NULLSTAT)],
 
1409
                         entry[1])
 
1410
        # Flush the buffers to disk
 
1411
        state.save()
 
1412
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1413
                         state._dirblock_state)
 
1414
 
 
1415
        stat_value = os.lstat('a')
 
1416
        packed_stat = dirstate.pack_stat(stat_value)
 
1417
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1418
                                          stat_value=stat_value)
 
1419
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1420
                         link_or_sha1)
 
1421
 
 
1422
        # The dirblock entry should be updated with the new info
 
1423
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1424
                         entry[1])
 
1425
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1426
                         state._dirblock_state)
 
1427
        mode = stat_value.st_mode
 
1428
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False)], state._log)
 
1429
 
 
1430
        state.save()
 
1431
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1432
                         state._dirblock_state)
 
1433
 
 
1434
        # If we do it again right away, we don't know if the file has changed
 
1435
        # so we will re-read the file. Roll the clock back so the file is
 
1436
        # guaranteed to look too new.
 
1437
        state.adjust_time(-10)
 
1438
 
 
1439
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1440
                                          stat_value=stat_value)
 
1441
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1442
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1443
                         ], state._log)
 
1444
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1445
                         link_or_sha1)
 
1446
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1447
                         state._dirblock_state)
 
1448
        state.save()
 
1449
 
 
1450
        # However, if we move the clock forward so the file is considered
 
1451
        # "stable", it should just returned the cached value.
 
1452
        state.adjust_time(20)
 
1453
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1454
                                          stat_value=stat_value)
 
1455
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1456
                         link_or_sha1)
 
1457
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1458
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1459
                         ], state._log)
 
1460
 
 
1461
    def test_update_entry_no_stat_value(self):
 
1462
        """Passing the stat_value is optional."""
 
1463
        state, entry = self.get_state_with_a()
 
1464
        state.adjust_time(-10) # Make sure the file looks new
 
1465
        self.build_tree(['a'])
 
1466
        # Add one where we don't provide the stat or sha already
 
1467
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1468
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1469
                         link_or_sha1)
 
1470
        stat_value = os.lstat('a')
 
1471
        self.assertEqual([('lstat', 'a'), ('sha1', 'a'),
 
1472
                          ('is_exec', stat_value.st_mode, False),
 
1473
                         ], state._log)
 
1474
 
 
1475
    def test_update_entry_symlink(self):
 
1476
        """Update entry should read symlinks."""
 
1477
        if not osutils.has_symlinks():
 
1478
            # PlatformDeficiency / TestSkipped
 
1479
            raise TestSkipped("No symlink support")
 
1480
        state, entry = self.get_state_with_a()
 
1481
        state.save()
 
1482
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1483
                         state._dirblock_state)
 
1484
        os.symlink('target', 'a')
 
1485
 
 
1486
        state.adjust_time(-10) # Make the symlink look new
 
1487
        stat_value = os.lstat('a')
 
1488
        packed_stat = dirstate.pack_stat(stat_value)
 
1489
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1490
                                          stat_value=stat_value)
 
1491
        self.assertEqual('target', link_or_sha1)
 
1492
        self.assertEqual([('read_link', 'a', '')], state._log)
 
1493
        # Dirblock is updated
 
1494
        self.assertEqual([('l', link_or_sha1, 6, False, packed_stat)],
 
1495
                         entry[1])
 
1496
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1497
                         state._dirblock_state)
 
1498
 
 
1499
        # Because the stat_value looks new, we should re-read the target
 
1500
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1501
                                          stat_value=stat_value)
 
1502
        self.assertEqual('target', link_or_sha1)
 
1503
        self.assertEqual([('read_link', 'a', ''),
 
1504
                          ('read_link', 'a', 'target'),
 
1505
                         ], state._log)
 
1506
        state.adjust_time(+20) # Skip into the future, all files look old
 
1507
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1508
                                          stat_value=stat_value)
 
1509
        self.assertEqual('target', link_or_sha1)
 
1510
        # There should not be a new read_link call.
 
1511
        # (this is a weak assertion, because read_link is fairly inexpensive,
 
1512
        # versus the number of symlinks that we would have)
 
1513
        self.assertEqual([('read_link', 'a', ''),
 
1514
                          ('read_link', 'a', 'target'),
 
1515
                         ], state._log)
 
1516
 
 
1517
    def test_update_entry_dir(self):
 
1518
        state, entry = self.get_state_with_a()
 
1519
        self.build_tree(['a/'])
 
1520
        self.assertIs(None, state.update_entry(entry, 'a'))
 
1521
 
 
1522
    def create_and_test_file(self, state, entry):
 
1523
        """Create a file at 'a' and verify the state finds it.
 
1524
 
 
1525
        The state should already be versioning *something* at 'a'. This makes
 
1526
        sure that state.update_entry recognizes it as a file.
 
1527
        """
 
1528
        self.build_tree(['a'])
 
1529
        stat_value = os.lstat('a')
 
1530
        packed_stat = dirstate.pack_stat(stat_value)
 
1531
 
 
1532
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1533
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1534
                         link_or_sha1)
 
1535
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1536
                         entry[1])
 
1537
        return packed_stat
 
1538
 
 
1539
    def create_and_test_dir(self, state, entry):
 
1540
        """Create a directory at 'a' and verify the state finds it.
 
1541
 
 
1542
        The state should already be versioning *something* at 'a'. This makes
 
1543
        sure that state.update_entry recognizes it as a directory.
 
1544
        """
 
1545
        self.build_tree(['a/'])
 
1546
        stat_value = os.lstat('a')
 
1547
        packed_stat = dirstate.pack_stat(stat_value)
 
1548
 
 
1549
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1550
        self.assertIs(None, link_or_sha1)
 
1551
        self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
 
1552
 
 
1553
        return packed_stat
 
1554
 
 
1555
    def create_and_test_symlink(self, state, entry):
 
1556
        """Create a symlink at 'a' and verify the state finds it.
 
1557
 
 
1558
        The state should already be versioning *something* at 'a'. This makes
 
1559
        sure that state.update_entry recognizes it as a symlink.
 
1560
 
 
1561
        This should not be called if this platform does not have symlink
 
1562
        support.
 
1563
        """
 
1564
        # caller should care about skipping test on platforms without symlinks
 
1565
        os.symlink('path/to/foo', 'a')
 
1566
 
 
1567
        stat_value = os.lstat('a')
 
1568
        packed_stat = dirstate.pack_stat(stat_value)
 
1569
 
 
1570
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1571
        self.assertEqual('path/to/foo', link_or_sha1)
 
1572
        self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
 
1573
                         entry[1])
 
1574
        return packed_stat
 
1575
 
 
1576
    def test_update_missing_file(self):
 
1577
        state, entry = self.get_state_with_a()
 
1578
        packed_stat = self.create_and_test_file(state, entry)
 
1579
        # Now if we delete the file, update_entry should recover and
 
1580
        # return None.
 
1581
        os.remove('a')
 
1582
        self.assertIs(None, state.update_entry(entry, abspath='a'))
 
1583
        # And the record shouldn't be changed.
 
1584
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1585
        self.assertEqual([('f', digest, 14, False, packed_stat)],
 
1586
                         entry[1])
 
1587
 
 
1588
    def test_update_missing_dir(self):
 
1589
        state, entry = self.get_state_with_a()
 
1590
        packed_stat = self.create_and_test_dir(state, entry)
 
1591
        # Now if we delete the directory, update_entry should recover and
 
1592
        # return None.
 
1593
        os.rmdir('a')
 
1594
        self.assertIs(None, state.update_entry(entry, abspath='a'))
 
1595
        self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
 
1596
 
 
1597
    def test_update_missing_symlink(self):
 
1598
        if not osutils.has_symlinks():
 
1599
            # PlatformDeficiency / TestSkipped
 
1600
            raise TestSkipped("No symlink support")
 
1601
        state, entry = self.get_state_with_a()
 
1602
        packed_stat = self.create_and_test_symlink(state, entry)
 
1603
        os.remove('a')
 
1604
        self.assertIs(None, state.update_entry(entry, abspath='a'))
 
1605
        # And the record shouldn't be changed.
 
1606
        self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
 
1607
                         entry[1])
 
1608
 
 
1609
    def test_update_file_to_dir(self):
 
1610
        """If a file changes to a directory we return None for the sha.
 
1611
        We also update the inventory record.
 
1612
        """
 
1613
        state, entry = self.get_state_with_a()
 
1614
        self.create_and_test_file(state, entry)
 
1615
        os.remove('a')
 
1616
        self.create_and_test_dir(state, entry)
 
1617
 
 
1618
    def test_update_file_to_symlink(self):
 
1619
        """File becomes a symlink"""
 
1620
        if not osutils.has_symlinks():
 
1621
            # PlatformDeficiency / TestSkipped
 
1622
            raise TestSkipped("No symlink support")
 
1623
        state, entry = self.get_state_with_a()
 
1624
        self.create_and_test_file(state, entry)
 
1625
        os.remove('a')
 
1626
        self.create_and_test_symlink(state, entry)
 
1627
 
 
1628
    def test_update_dir_to_file(self):
 
1629
        """Directory becoming a file updates the entry."""
 
1630
        state, entry = self.get_state_with_a()
 
1631
        self.create_and_test_dir(state, entry)
 
1632
        os.rmdir('a')
 
1633
        self.create_and_test_file(state, entry)
 
1634
 
 
1635
    def test_update_dir_to_symlink(self):
 
1636
        """Directory becomes a symlink"""
 
1637
        if not osutils.has_symlinks():
 
1638
            # PlatformDeficiency / TestSkipped
 
1639
            raise TestSkipped("No symlink support")
 
1640
        state, entry = self.get_state_with_a()
 
1641
        self.create_and_test_dir(state, entry)
 
1642
        os.rmdir('a')
 
1643
        self.create_and_test_symlink(state, entry)
 
1644
 
 
1645
    def test_update_symlink_to_file(self):
 
1646
        """Symlink becomes a file"""
 
1647
        if not has_symlinks():
 
1648
            raise TestSkipped("No symlink support")
 
1649
        state, entry = self.get_state_with_a()
 
1650
        self.create_and_test_symlink(state, entry)
 
1651
        os.remove('a')
 
1652
        self.create_and_test_file(state, entry)
 
1653
 
 
1654
    def test_update_symlink_to_dir(self):
 
1655
        """Symlink becomes a directory"""
 
1656
        if not has_symlinks():
 
1657
            raise TestSkipped("No symlink support")
 
1658
        state, entry = self.get_state_with_a()
 
1659
        self.create_and_test_symlink(state, entry)
 
1660
        os.remove('a')
 
1661
        self.create_and_test_dir(state, entry)
 
1662
 
 
1663
    def test__is_executable_win32(self):
 
1664
        state, entry = self.get_state_with_a()
 
1665
        self.build_tree(['a'])
 
1666
 
 
1667
        # Make sure we are using the win32 implementation of _is_executable
 
1668
        state._is_executable = state._is_executable_win32
 
1669
 
 
1670
        # The file on disk is not executable, but we are marking it as though
 
1671
        # it is. With _is_executable_win32 we ignore what is on disk.
 
1672
        entry[1][0] = ('f', '', 0, True, dirstate.DirState.NULLSTAT)
 
1673
 
 
1674
        stat_value = os.lstat('a')
 
1675
        packed_stat = dirstate.pack_stat(stat_value)
 
1676
 
 
1677
        state.adjust_time(-10) # Make sure everything is new
 
1678
        # Make sure it wants to kkkkkkkk
 
1679
        state.update_entry(entry, abspath='a', stat_value=stat_value)
 
1680
 
 
1681
        # The row is updated, but the executable bit stays set.
 
1682
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1683
        self.assertEqual([('f', digest, 14, True, packed_stat)], entry[1])
 
1684
 
 
1685
 
 
1686
class TestPackStat(TestCaseWithTransport):
1698
1687
 
1699
1688
    def assertPackStat(self, expected, stat_value):
1700
1689
        """Check the packed and serialized form of a stat value."""
1749
1738
class TestBisect(TestCaseWithDirState):
1750
1739
    """Test the ability to bisect into the disk format."""
1751
1740
 
 
1741
 
1752
1742
    def assertBisect(self, expected_map, map_keys, state, paths):
1753
1743
        """Assert that bisecting for paths returns the right result.
1754
1744
 
1759
1749
                      (dir, name) tuples, and sorted according to how _bisect
1760
1750
                      requires.
1761
1751
        """
1762
 
        result = state._bisect(paths)
 
1752
        dir_names = sorted(osutils.split(p) for p in paths)
 
1753
        result = state._bisect(dir_names)
1763
1754
        # For now, results are just returned in whatever order we read them.
1764
1755
        # We could sort by (dir, name, file_id) or something like that, but in
1765
1756
        # the end it would still be fairly arbitrary, and we don't want the
1766
1757
        # extra overhead if we can avoid it. So sort everything to make sure
1767
1758
        # equality is true
1768
 
        self.assertEqual(len(map_keys), len(paths))
 
1759
        assert len(map_keys) == len(dir_names)
1769
1760
        expected = {}
1770
 
        for path, keys in zip(paths, map_keys):
 
1761
        for dir_name, keys in zip(dir_names, map_keys):
1771
1762
            if keys is None:
1772
1763
                # This should not be present in the output
1773
1764
                continue
1774
 
            expected[path] = sorted(expected_map[k] for k in keys)
 
1765
            expected[dir_name] = sorted(expected_map[k] for k in keys)
1775
1766
 
1776
 
        # The returned values are just arranged randomly based on when they
1777
 
        # were read, for testing, make sure it is properly sorted.
1778
 
        for path in result:
1779
 
            result[path].sort()
 
1767
        for dir_name in result:
 
1768
            result[dir_name].sort()
1780
1769
 
1781
1770
        self.assertEqual(expected, result)
1782
1771
 
1790
1779
        :param paths: A list of directories
1791
1780
        """
1792
1781
        result = state._bisect_dirblocks(paths)
1793
 
        self.assertEqual(len(map_keys), len(paths))
 
1782
        assert len(map_keys) == len(paths)
 
1783
 
1794
1784
        expected = {}
1795
1785
        for path, keys in zip(paths, map_keys):
1796
1786
            if keys is None:
1818
1808
            dir_name_id, trees_info = entry
1819
1809
            expected[dir_name_id] = trees_info
1820
1810
 
1821
 
        result = state._bisect_recursive(paths)
 
1811
        dir_names = sorted(osutils.split(p) for p in paths)
 
1812
        result = state._bisect_recursive(dir_names)
1822
1813
 
1823
1814
        self.assertEqual(expected, result)
1824
1815
 
1833
1824
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
1834
1825
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
1835
1826
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
1836
 
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
1837
1827
        self.assertBisect(expected, [['f']], state, ['f'])
1838
1828
 
1839
1829
    def test_bisect_multi(self):
1842
1832
        # Bisect should be capable of finding multiple entries at the same time
1843
1833
        self.assertBisect(expected, [['a'], ['b'], ['f']],
1844
1834
                          state, ['a', 'b', 'f'])
 
1835
        # ('', 'f') sorts before the others
1845
1836
        self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
1846
 
                          state, ['f', 'b/d', 'b/d/e'])
1847
 
        self.assertBisect(expected, [['b'], ['b-c'], ['b/c']],
1848
 
                          state, ['b', 'b-c', 'b/c'])
 
1837
                          state, ['b/d', 'b/d/e', 'f'])
1849
1838
 
1850
1839
    def test_bisect_one_page(self):
1851
1840
        """Test bisect when there is only 1 page to read"""
1857
1846
        self.assertBisect(expected,[['b/c']], state, ['b/c'])
1858
1847
        self.assertBisect(expected,[['b/d']], state, ['b/d'])
1859
1848
        self.assertBisect(expected,[['b/d/e']], state, ['b/d/e'])
1860
 
        self.assertBisect(expected,[['b-c']], state, ['b-c'])
1861
1849
        self.assertBisect(expected,[['f']], state, ['f'])
1862
1850
        self.assertBisect(expected,[['a'], ['b'], ['f']],
1863
1851
                          state, ['a', 'b', 'f'])
1864
 
        self.assertBisect(expected, [['b/d'], ['b/d/e'], ['f']],
 
1852
        # ('', 'f') sorts before the others
 
1853
        self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
1865
1854
                          state, ['b/d', 'b/d/e', 'f'])
1866
 
        self.assertBisect(expected, [['b'], ['b/c'], ['b-c']],
1867
 
                          state, ['b', 'b/c', 'b-c'])
1868
1855
 
1869
1856
    def test_bisect_duplicate_paths(self):
1870
1857
        """When bisecting for a path, handle multiple entries."""
1878
1865
        self.assertBisect(expected, [['b/d', 'b/d2']], state, ['b/d'])
1879
1866
        self.assertBisect(expected, [['b/d/e', 'b/d/e2']],
1880
1867
                          state, ['b/d/e'])
1881
 
        self.assertBisect(expected, [['b-c', 'b-c2']], state, ['b-c'])
1882
1868
        self.assertBisect(expected, [['f', 'f2']], state, ['f'])
1883
1869
 
1884
1870
    def test_bisect_page_size_too_small(self):
1891
1877
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
1892
1878
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
1893
1879
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
1894
 
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
1895
1880
        self.assertBisect(expected, [['f']], state, ['f'])
1896
1881
 
1897
1882
    def test_bisect_missing(self):
1900
1885
        self.assertBisect(expected, [None], state, ['foo'])
1901
1886
        self.assertBisect(expected, [None], state, ['b/foo'])
1902
1887
        self.assertBisect(expected, [None], state, ['bar/foo'])
1903
 
        self.assertBisect(expected, [None], state, ['b-c/foo'])
1904
1888
 
1905
1889
        self.assertBisect(expected, [['a'], None, ['b/d']],
1906
1890
                          state, ['a', 'foo', 'b/d'])
1922
1906
    def test_bisect_dirblocks(self):
1923
1907
        tree, state, expected = self.create_duplicated_dirstate()
1924
1908
        self.assertBisectDirBlocks(expected,
1925
 
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2']],
1926
 
            state, [''])
 
1909
            [['', 'a', 'a2', 'b', 'b2', 'f', 'f2']], state, [''])
1927
1910
        self.assertBisectDirBlocks(expected,
1928
1911
            [['b/c', 'b/c2', 'b/d', 'b/d2']], state, ['b'])
1929
1912
        self.assertBisectDirBlocks(expected,
1930
1913
            [['b/d/e', 'b/d/e2']], state, ['b/d'])
1931
1914
        self.assertBisectDirBlocks(expected,
1932
 
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2'],
 
1915
            [['', 'a', 'a2', 'b', 'b2', 'f', 'f2'],
1933
1916
             ['b/c', 'b/c2', 'b/d', 'b/d2'],
1934
1917
             ['b/d/e', 'b/d/e2'],
1935
1918
            ], state, ['', 'b', 'b/d'])
1950
1933
        self.assertBisectRecursive(expected, ['a'], state, ['a'])
1951
1934
        self.assertBisectRecursive(expected, ['b/c'], state, ['b/c'])
1952
1935
        self.assertBisectRecursive(expected, ['b/d/e'], state, ['b/d/e'])
1953
 
        self.assertBisectRecursive(expected, ['b-c'], state, ['b-c'])
1954
1936
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
1955
1937
                                   state, ['b/d'])
1956
1938
        self.assertBisectRecursive(expected, ['b', 'b/c', 'b/d', 'b/d/e'],
1957
1939
                                   state, ['b'])
1958
 
        self.assertBisectRecursive(expected, ['', 'a', 'b', 'b-c', 'f', 'b/c',
 
1940
        self.assertBisectRecursive(expected, ['', 'a', 'b', 'f', 'b/c',
1959
1941
                                              'b/d', 'b/d/e'],
1960
1942
                                   state, [''])
1961
1943
 
1985
1967
                                   state, ['b'])
1986
1968
 
1987
1969
 
 
1970
class TestBisectDirblock(TestCase):
 
1971
    """Test that bisect_dirblock() returns the expected values.
 
1972
 
 
1973
    bisect_dirblock is intended to work like bisect.bisect_left() except it
 
1974
    knows it is working on dirblocks and that dirblocks are sorted by ('path',
 
1975
    'to', 'foo') chunks rather than by raw 'path/to/foo'.
 
1976
    """
 
1977
 
 
1978
    def assertBisect(self, dirblocks, split_dirblocks, path, *args, **kwargs):
 
1979
        """Assert that bisect_split works like bisect_left on the split paths.
 
1980
 
 
1981
        :param dirblocks: A list of (path, [info]) pairs.
 
1982
        :param split_dirblocks: A list of ((split, path), [info]) pairs.
 
1983
        :param path: The path we are indexing.
 
1984
 
 
1985
        All other arguments will be passed along.
 
1986
        """
 
1987
        bisect_split_idx = dirstate.bisect_dirblock(dirblocks, path,
 
1988
                                                 *args, **kwargs)
 
1989
        split_dirblock = (path.split('/'), [])
 
1990
        bisect_left_idx = bisect.bisect_left(split_dirblocks, split_dirblock,
 
1991
                                             *args)
 
1992
        self.assertEqual(bisect_left_idx, bisect_split_idx,
 
1993
                         'bisect_split disagreed. %s != %s'
 
1994
                         ' for key %s'
 
1995
                         % (bisect_left_idx, bisect_split_idx, path)
 
1996
                         )
 
1997
 
 
1998
    def paths_to_dirblocks(self, paths):
 
1999
        """Convert a list of paths into dirblock form.
 
2000
 
 
2001
        Also, ensure that the paths are in proper sorted order.
 
2002
        """
 
2003
        dirblocks = [(path, []) for path in paths]
 
2004
        split_dirblocks = [(path.split('/'), []) for path in paths]
 
2005
        self.assertEqual(sorted(split_dirblocks), split_dirblocks)
 
2006
        return dirblocks, split_dirblocks
 
2007
 
 
2008
    def test_simple(self):
 
2009
        """In the simple case it works just like bisect_left"""
 
2010
        paths = ['', 'a', 'b', 'c', 'd']
 
2011
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
 
2012
        for path in paths:
 
2013
            self.assertBisect(dirblocks, split_dirblocks, path)
 
2014
        self.assertBisect(dirblocks, split_dirblocks, '_')
 
2015
        self.assertBisect(dirblocks, split_dirblocks, 'aa')
 
2016
        self.assertBisect(dirblocks, split_dirblocks, 'bb')
 
2017
        self.assertBisect(dirblocks, split_dirblocks, 'cc')
 
2018
        self.assertBisect(dirblocks, split_dirblocks, 'dd')
 
2019
        self.assertBisect(dirblocks, split_dirblocks, 'a/a')
 
2020
        self.assertBisect(dirblocks, split_dirblocks, 'b/b')
 
2021
        self.assertBisect(dirblocks, split_dirblocks, 'c/c')
 
2022
        self.assertBisect(dirblocks, split_dirblocks, 'd/d')
 
2023
 
 
2024
    def test_involved(self):
 
2025
        """This is where bisect_left diverges slightly."""
 
2026
        paths = ['', 'a',
 
2027
                 'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
 
2028
                 'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
 
2029
                 'a-a', 'a-z',
 
2030
                 'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
 
2031
                 'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
 
2032
                 'z-a', 'z-z',
 
2033
                ]
 
2034
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
 
2035
        for path in paths:
 
2036
            self.assertBisect(dirblocks, split_dirblocks, path)
 
2037
 
 
2038
    def test_involved_cached(self):
 
2039
        """This is where bisect_left diverges slightly."""
 
2040
        paths = ['', 'a',
 
2041
                 'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
 
2042
                 'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
 
2043
                 'a-a', 'a-z',
 
2044
                 'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
 
2045
                 'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
 
2046
                 'z-a', 'z-z',
 
2047
                ]
 
2048
        cache = {}
 
2049
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
 
2050
        for path in paths:
 
2051
            self.assertBisect(dirblocks, split_dirblocks, path, cache=cache)
 
2052
 
 
2053
 
1988
2054
class TestDirstateValidation(TestCaseWithDirState):
1989
2055
 
1990
2056
    def test_validate_correct_dirstate(self):
2040
2106
            state._validate)
2041
2107
        self.assertContainsRe(str(e),
2042
2108
            'file a-id is absent in row')
2043
 
 
2044
 
 
2045
 
class TestDirstateTreeReference(TestCaseWithDirState):
2046
 
 
2047
 
    def test_reference_revision_is_none(self):
2048
 
        tree = self.make_branch_and_tree('tree', format='dirstate-with-subtree')
2049
 
        subtree = self.make_branch_and_tree('tree/subtree',
2050
 
                            format='dirstate-with-subtree')
2051
 
        subtree.set_root_id('subtree')
2052
 
        tree.add_reference(subtree)
2053
 
        tree.add('subtree')
2054
 
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2055
 
        key = ('', 'subtree', 'subtree')
2056
 
        expected = ('', [(key,
2057
 
            [('t', '', 0, False, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])])
2058
 
 
2059
 
        try:
2060
 
            self.assertEqual(expected, state._find_block(key))
2061
 
        finally:
2062
 
            state.unlock()
2063
 
 
2064
 
 
2065
 
class TestDiscardMergeParents(TestCaseWithDirState):
2066
 
 
2067
 
    def test_discard_no_parents(self):
2068
 
        # This should be a no-op
2069
 
        state = self.create_empty_dirstate()
2070
 
        self.addCleanup(state.unlock)
2071
 
        state._discard_merge_parents()
2072
 
        state._validate()
2073
 
 
2074
 
    def test_discard_one_parent(self):
2075
 
        # No-op
2076
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2077
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2078
 
            ('d', '', 0, False, packed_stat),
2079
 
            ('d', '', 0, False, packed_stat),
2080
 
            ]
2081
 
        dirblocks = []
2082
 
        dirblocks.append(('', [root_entry_direntry]))
2083
 
        dirblocks.append(('', []))
2084
 
 
2085
 
        state = self.create_empty_dirstate()
2086
 
        self.addCleanup(state.unlock)
2087
 
        state._set_data(['parent-id'], dirblocks[:])
2088
 
        state._validate()
2089
 
 
2090
 
        state._discard_merge_parents()
2091
 
        state._validate()
2092
 
        self.assertEqual(dirblocks, state._dirblocks)
2093
 
 
2094
 
    def test_discard_simple(self):
2095
 
        # No-op
2096
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2097
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2098
 
            ('d', '', 0, False, packed_stat),
2099
 
            ('d', '', 0, False, packed_stat),
2100
 
            ('d', '', 0, False, packed_stat),
2101
 
            ]
2102
 
        expected_root_entry_direntry = ('', '', 'a-root-value'), [
2103
 
            ('d', '', 0, False, packed_stat),
2104
 
            ('d', '', 0, False, packed_stat),
2105
 
            ]
2106
 
        dirblocks = []
2107
 
        dirblocks.append(('', [root_entry_direntry]))
2108
 
        dirblocks.append(('', []))
2109
 
 
2110
 
        state = self.create_empty_dirstate()
2111
 
        self.addCleanup(state.unlock)
2112
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2113
 
        state._validate()
2114
 
 
2115
 
        # This should strip of the extra column
2116
 
        state._discard_merge_parents()
2117
 
        state._validate()
2118
 
        expected_dirblocks = [('', [expected_root_entry_direntry]), ('', [])]
2119
 
        self.assertEqual(expected_dirblocks, state._dirblocks)
2120
 
 
2121
 
    def test_discard_absent(self):
2122
 
        """If entries are only in a merge, discard should remove the entries"""
2123
 
        null_stat = dirstate.DirState.NULLSTAT
2124
 
        present_dir = ('d', '', 0, False, null_stat)
2125
 
        present_file = ('f', '', 0, False, null_stat)
2126
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2127
 
        root_key = ('', '', 'a-root-value')
2128
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2129
 
        file_in_merged_key = ('', 'file-in-merged', 'b-file-id')
2130
 
        dirblocks = [('', [(root_key, [present_dir, present_dir, present_dir])]),
2131
 
                     ('', [(file_in_merged_key,
2132
 
                            [absent, absent, present_file]),
2133
 
                           (file_in_root_key,
2134
 
                            [present_file, present_file, present_file]),
2135
 
                          ]),
2136
 
                    ]
2137
 
 
2138
 
        state = self.create_empty_dirstate()
2139
 
        self.addCleanup(state.unlock)
2140
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2141
 
        state._validate()
2142
 
 
2143
 
        exp_dirblocks = [('', [(root_key, [present_dir, present_dir])]),
2144
 
                         ('', [(file_in_root_key,
2145
 
                                [present_file, present_file]),
2146
 
                              ]),
2147
 
                        ]
2148
 
        state._discard_merge_parents()
2149
 
        state._validate()
2150
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2151
 
 
2152
 
    def test_discard_renamed(self):
2153
 
        null_stat = dirstate.DirState.NULLSTAT
2154
 
        present_dir = ('d', '', 0, False, null_stat)
2155
 
        present_file = ('f', '', 0, False, null_stat)
2156
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2157
 
        root_key = ('', '', 'a-root-value')
2158
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2159
 
        # Renamed relative to parent
2160
 
        file_rename_s_key = ('', 'file-s', 'b-file-id')
2161
 
        file_rename_t_key = ('', 'file-t', 'b-file-id')
2162
 
        # And one that is renamed between the parents, but absent in this
2163
 
        key_in_1 = ('', 'file-in-1', 'c-file-id')
2164
 
        key_in_2 = ('', 'file-in-2', 'c-file-id')
2165
 
 
2166
 
        dirblocks = [
2167
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2168
 
            ('', [(key_in_1,
2169
 
                   [absent, present_file, ('r', 'file-in-2', 'c-file-id')]),
2170
 
                  (key_in_2,
2171
 
                   [absent, ('r', 'file-in-1', 'c-file-id'), present_file]),
2172
 
                  (file_in_root_key,
2173
 
                   [present_file, present_file, present_file]),
2174
 
                  (file_rename_s_key,
2175
 
                   [('r', 'file-t', 'b-file-id'), absent, present_file]),
2176
 
                  (file_rename_t_key,
2177
 
                   [present_file, absent, ('r', 'file-s', 'b-file-id')]),
2178
 
                 ]),
2179
 
        ]
2180
 
        exp_dirblocks = [
2181
 
            ('', [(root_key, [present_dir, present_dir])]),
2182
 
            ('', [(key_in_1, [absent, present_file]),
2183
 
                  (file_in_root_key, [present_file, present_file]),
2184
 
                  (file_rename_t_key, [present_file, absent]),
2185
 
                 ]),
2186
 
        ]
2187
 
        state = self.create_empty_dirstate()
2188
 
        self.addCleanup(state.unlock)
2189
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2190
 
        state._validate()
2191
 
 
2192
 
        state._discard_merge_parents()
2193
 
        state._validate()
2194
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2195
 
 
2196
 
    def test_discard_all_subdir(self):
2197
 
        null_stat = dirstate.DirState.NULLSTAT
2198
 
        present_dir = ('d', '', 0, False, null_stat)
2199
 
        present_file = ('f', '', 0, False, null_stat)
2200
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2201
 
        root_key = ('', '', 'a-root-value')
2202
 
        subdir_key = ('', 'sub', 'dir-id')
2203
 
        child1_key = ('sub', 'child1', 'child1-id')
2204
 
        child2_key = ('sub', 'child2', 'child2-id')
2205
 
        child3_key = ('sub', 'child3', 'child3-id')
2206
 
 
2207
 
        dirblocks = [
2208
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2209
 
            ('', [(subdir_key, [present_dir, present_dir, present_dir])]),
2210
 
            ('sub', [(child1_key, [absent, absent, present_file]),
2211
 
                     (child2_key, [absent, absent, present_file]),
2212
 
                     (child3_key, [absent, absent, present_file]),
2213
 
                    ]),
2214
 
        ]
2215
 
        exp_dirblocks = [
2216
 
            ('', [(root_key, [present_dir, present_dir])]),
2217
 
            ('', [(subdir_key, [present_dir, present_dir])]),
2218
 
            ('sub', []),
2219
 
        ]
2220
 
        state = self.create_empty_dirstate()
2221
 
        self.addCleanup(state.unlock)
2222
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2223
 
        state._validate()
2224
 
 
2225
 
        state._discard_merge_parents()
2226
 
        state._validate()
2227
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2228
 
 
2229
 
 
2230
 
class Test_InvEntryToDetails(tests.TestCase):
2231
 
 
2232
 
    def assertDetails(self, expected, inv_entry):
2233
 
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
2234
 
        self.assertEqual(expected, details)
2235
 
        # details should always allow join() and always be a plain str when
2236
 
        # finished
2237
 
        (minikind, fingerprint, size, executable, tree_data) = details
2238
 
        self.assertIsInstance(minikind, str)
2239
 
        self.assertIsInstance(fingerprint, str)
2240
 
        self.assertIsInstance(tree_data, str)
2241
 
 
2242
 
    def test_unicode_symlink(self):
2243
 
        inv_entry = inventory.InventoryLink('link-file-id',
2244
 
                                            u'nam\N{Euro Sign}e',
2245
 
                                            'link-parent-id')
2246
 
        inv_entry.revision = 'link-revision-id'
2247
 
        target = u'link-targ\N{Euro Sign}t'
2248
 
        inv_entry.symlink_target = target
2249
 
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2250
 
                            'link-revision-id'), inv_entry)
2251
 
 
2252
 
 
2253
 
class TestSHA1Provider(tests.TestCaseInTempDir):
2254
 
 
2255
 
    def test_sha1provider_is_an_interface(self):
2256
 
        p = dirstate.SHA1Provider()
2257
 
        self.assertRaises(NotImplementedError, p.sha1, "foo")
2258
 
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
2259
 
 
2260
 
    def test_defaultsha1provider_sha1(self):
2261
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2262
 
        self.build_tree_contents([('foo', text)])
2263
 
        expected_sha = osutils.sha_string(text)
2264
 
        p = dirstate.DefaultSHA1Provider()
2265
 
        self.assertEqual(expected_sha, p.sha1('foo'))
2266
 
 
2267
 
    def test_defaultsha1provider_stat_and_sha1(self):
2268
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2269
 
        self.build_tree_contents([('foo', text)])
2270
 
        expected_sha = osutils.sha_string(text)
2271
 
        p = dirstate.DefaultSHA1Provider()
2272
 
        statvalue, sha1 = p.stat_and_sha1('foo')
2273
 
        self.assertTrue(len(statvalue) >= 10)
2274
 
        self.assertEqual(len(text), statvalue.st_size)
2275
 
        self.assertEqual(expected_sha, sha1)