~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_dirstate.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
17
"""Tests of the dirstate functionality being built for WorkingTreeFormat4."""
18
18
 
 
19
import bisect
19
20
import os
20
 
import tempfile
 
21
import time
21
22
 
22
23
from bzrlib import (
23
 
    bzrdir,
24
24
    dirstate,
25
25
    errors,
26
 
    inventory,
27
 
    memorytree,
28
26
    osutils,
29
 
    revision as _mod_revision,
30
 
    revisiontree,
31
 
    tests,
32
 
    workingtree_4,
33
27
    )
34
 
from bzrlib.transport import memory
 
28
from bzrlib.memorytree import MemoryTree
 
29
from bzrlib.osutils import has_symlinks
35
30
from bzrlib.tests import (
36
 
    features,
37
 
    test_osutils,
38
 
    )
39
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
31
        TestCase,
 
32
        TestCaseWithTransport,
 
33
        TestSkipped,
 
34
        )
40
35
 
41
36
 
42
37
# TODO:
52
47
# set_path_id  setting id when state is in memory modified
53
48
 
54
49
 
55
 
load_tests = load_tests_apply_scenarios
56
 
 
57
 
 
58
 
class TestCaseWithDirState(tests.TestCaseWithTransport):
 
50
class TestCaseWithDirState(TestCaseWithTransport):
59
51
    """Helper functions for creating DirState objects with various content."""
60
52
 
61
 
    scenarios = test_osutils.dir_reader_scenarios()
62
 
 
63
 
    # Set by load_tests
64
 
    _dir_reader_class = None
65
 
    _native_to_unicode = None # Not used yet
66
 
 
67
 
    def setUp(self):
68
 
        tests.TestCaseWithTransport.setUp(self)
69
 
 
70
 
        self.overrideAttr(osutils,
71
 
                          '_selected_dir_reader', self._dir_reader_class())
72
 
 
73
53
    def create_empty_dirstate(self):
74
54
        """Return a locked but empty dirstate"""
75
55
        state = dirstate.DirState.initialize('dirstate')
183
163
        """
184
164
        # The state should already be write locked, since we just had to do
185
165
        # some operation to get here.
186
 
        self.assertTrue(state._lock_token is not None)
 
166
        assert state._lock_token is not None
187
167
        try:
188
168
            self.assertEqual(expected_result[0],  state.get_parent_ids())
189
169
            # there should be no ghosts in this tree.
209
189
              c
210
190
              d/
211
191
                e
212
 
            b-c
213
192
            f
214
193
        """
215
194
        tree = self.make_branch_and_tree('tree')
216
 
        paths = ['a', 'b/', 'b/c', 'b/d/', 'b/d/e', 'b-c', 'f']
217
 
        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']
218
197
        self.build_tree(['tree/' + p for p in paths])
219
198
        tree.set_root_id('TREE_ROOT')
220
199
        tree.add([p.rstrip('/') for p in paths], file_ids)
221
200
        tree.commit('initial', rev_id='rev-1')
222
201
        revision_id = 'rev-1'
223
202
        # a_packed_stat = dirstate.pack_stat(os.stat('tree/a'))
224
 
        t = self.get_transport('tree')
 
203
        t = self.get_transport().clone('tree')
225
204
        a_text = t.get_bytes('a')
226
205
        a_sha = osutils.sha_string(a_text)
227
206
        a_len = len(a_text)
235
214
        e_text = t.get_bytes('b/d/e')
236
215
        e_sha = osutils.sha_string(e_text)
237
216
        e_len = len(e_text)
238
 
        b_c_text = t.get_bytes('b-c')
239
 
        b_c_sha = osutils.sha_string(b_c_text)
240
 
        b_c_len = len(b_c_text)
241
217
        # f_packed_stat = dirstate.pack_stat(os.stat('tree/f'))
242
218
        f_text = t.get_bytes('f')
243
219
        f_sha = osutils.sha_string(f_text)
268
244
                      ('f', '', 0, False, null_stat),
269
245
                      ('f', e_sha, e_len, False, revision_id),
270
246
                     ]),
271
 
            'b-c':(('', 'b-c', 'b-c-id'), [
272
 
                      ('f', '', 0, False, null_stat),
273
 
                      ('f', b_c_sha, b_c_len, False, revision_id),
274
 
                     ]),
275
247
            'f':(('', 'f', 'f-id'), [
276
248
                  ('f', '', 0, False, null_stat),
277
249
                  ('f', f_sha, f_len, False, revision_id),
304
276
        tree, state, expected = self.create_basic_dirstate()
305
277
        # Now we will just remove and add every file so we get an extra entry
306
278
        # per entry. Unversion in reverse order so we handle subdirs
307
 
        tree.unversion(['f-id', 'b-c-id', 'e-id', 'd-id', 'c-id', 'b-id', 'a-id'])
308
 
        tree.add(['a', 'b', 'b/c', 'b/d', 'b/d/e', 'b-c', 'f'],
309
 
                 ['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'])
310
282
 
311
283
        # Update the expected dictionary.
312
 
        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']:
313
285
            orig = expected[path]
314
286
            path2 = path + '2'
315
287
            # This record was deleted in the current tree
372
344
            state.lock_read()
373
345
        return tree, state, expected
374
346
 
375
 
 
376
347
class TestTreeToDirState(TestCaseWithDirState):
377
348
 
378
349
    def test_empty_to_dirstate(self):
380
351
        # There are no files on disk and no parents
381
352
        tree = self.make_branch_and_tree('tree')
382
353
        expected_result = ([], [
383
 
            (('', '', tree.get_root_id()), # common details
 
354
            (('', '', tree.path2id('')), # common details
384
355
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
385
356
             ])])
386
357
        state = dirstate.DirState.from_tree(tree, 'dirstate')
393
364
        rev_id = tree.commit('first post').encode('utf8')
394
365
        root_stat_pack = dirstate.pack_stat(os.stat(tree.basedir))
395
366
        expected_result = ([rev_id], [
396
 
            (('', '', tree.get_root_id()), # common details
 
367
            (('', '', tree.path2id('')), # common details
397
368
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
398
369
              ('d', '', 0, False, rev_id), # first parent details
399
370
             ])])
413
384
        rev_id2 = tree2.commit('second post', allow_pointless=True)
414
385
        tree.merge_from_branch(tree2.branch)
415
386
        expected_result = ([rev_id, rev_id2], [
416
 
            (('', '', tree.get_root_id()), # common details
 
387
            (('', '', tree.path2id('')), # common details
417
388
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
418
389
              ('d', '', 0, False, rev_id), # first parent details
419
 
              ('d', '', 0, False, rev_id), # second parent details
 
390
              ('d', '', 0, False, rev_id2), # second parent details
420
391
             ])])
421
392
        state = dirstate.DirState.from_tree(tree, 'dirstate')
422
393
        self.check_state_with_reopen(expected_result, state)
432
403
        tree = self.make_branch_and_tree('tree')
433
404
        self.build_tree(['tree/unknown'])
434
405
        expected_result = ([], [
435
 
            (('', '', tree.get_root_id()), # common details
 
406
            (('', '', tree.path2id('')), # common details
436
407
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
437
408
             ])])
438
409
        state = dirstate.DirState.from_tree(tree, 'dirstate')
441
412
    def get_tree_with_a_file(self):
442
413
        tree = self.make_branch_and_tree('tree')
443
414
        self.build_tree(['tree/a file'])
444
 
        tree.add('a file', 'a-file-id')
 
415
        tree.add('a file', 'a file id')
445
416
        return tree
446
417
 
447
418
    def test_non_empty_no_parents_to_dirstate(self):
449
420
        # There are files on disk and no parents
450
421
        tree = self.get_tree_with_a_file()
451
422
        expected_result = ([], [
452
 
            (('', '', tree.get_root_id()), # common details
 
423
            (('', '', tree.path2id('')), # common details
453
424
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
454
425
             ]),
455
 
            (('', 'a file', 'a-file-id'), # common
 
426
            (('', 'a file', 'a file id'), # common
456
427
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
457
428
             ]),
458
429
            ])
467
438
        # and length:
468
439
        self.build_tree_contents([('tree/a file', 'new content\n')])
469
440
        expected_result = ([rev_id], [
470
 
            (('', '', tree.get_root_id()), # common details
 
441
            (('', '', tree.path2id('')), # common details
471
442
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
472
443
              ('d', '', 0, False, rev_id), # first parent details
473
444
             ]),
474
 
            (('', 'a file', 'a-file-id'), # common
 
445
            (('', 'a file', 'a file id'), # common
475
446
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
476
447
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
477
448
               rev_id), # first parent
494
465
        # and length again, giving us three distinct values:
495
466
        self.build_tree_contents([('tree/a file', 'new content\n')])
496
467
        expected_result = ([rev_id, rev_id2], [
497
 
            (('', '', tree.get_root_id()), # common details
 
468
            (('', '', tree.path2id('')), # common details
498
469
             [('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
499
470
              ('d', '', 0, False, rev_id), # first parent details
500
 
              ('d', '', 0, False, rev_id), # second parent details
 
471
              ('d', '', 0, False, rev_id2), # second parent details
501
472
             ]),
502
 
            (('', 'a file', 'a-file-id'), # common
 
473
            (('', 'a file', 'a file id'), # common
503
474
             [('f', '', 0, False, dirstate.DirState.NULLSTAT), # current
504
475
              ('f', 'c3ed76e4bfd45ff1763ca206055bca8e9fc28aa8', 24, False,
505
476
               rev_id), # first parent
535
506
 
536
507
class TestDirStateOnFile(TestCaseWithDirState):
537
508
 
538
 
    def create_updated_dirstate(self):
539
 
        self.build_tree(['a-file'])
540
 
        tree = self.make_branch_and_tree('.')
541
 
        tree.add(['a-file'], ['a-id'])
542
 
        tree.commit('add a-file')
543
 
        # Save and unlock the state, re-open it in readonly mode
544
 
        state = dirstate.DirState.from_tree(tree, 'dirstate')
545
 
        state.save()
546
 
        state.unlock()
547
 
        state = dirstate.DirState.on_file('dirstate')
548
 
        state.lock_read()
549
 
        return state
550
 
 
551
509
    def test_construct_with_path(self):
552
510
        tree = self.make_branch_and_tree('tree')
553
511
        state = dirstate.DirState.from_tree(tree, 'dirstate.from_tree')
559
517
        # get a state object
560
518
        # no parents, default tree content
561
519
        expected_result = ([], [
562
 
            (('', '', tree.get_root_id()), # common details
 
520
            (('', '', tree.path2id('')), # common details
563
521
             # current tree details, but new from_tree skips statting, it
564
522
             # uses set_state_from_inventory, and thus depends on the
565
523
             # inventory state.
582
540
            state.unlock()
583
541
 
584
542
    def test_can_save_in_read_lock(self):
585
 
        state = self.create_updated_dirstate()
 
543
        self.build_tree(['a-file'])
 
544
        state = dirstate.DirState.initialize('dirstate')
 
545
        try:
 
546
            # No stat and no sha1 sum.
 
547
            state.add('a-file', 'a-file-id', 'file', None, '')
 
548
            state.save()
 
549
        finally:
 
550
            state.unlock()
 
551
 
 
552
        # Now open in readonly mode
 
553
        state = dirstate.DirState.on_file('dirstate')
 
554
        state.lock_read()
586
555
        try:
587
556
            entry = state._get_entry(0, path_utf8='a-file')
588
 
            # The current size should be 0 (default)
589
 
            self.assertEqual(0, entry[1][0][2])
 
557
            # The current sha1 sum should be empty
 
558
            self.assertEqual('', entry[1][0][1])
590
559
            # We should have a real entry.
591
560
            self.assertNotEqual((None, None), entry)
592
 
            # Set the cutoff-time into the future, so things look cacheable
593
 
            state._sha_cutoff_time()
594
 
            state._cutoff_time += 10.0
595
 
            st = os.lstat('a-file')
596
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
597
 
            # We updated the current sha1sum because the file is cacheable
 
561
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
562
            # We should have gotten a real sha1
598
563
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
599
564
                             sha1sum)
600
565
 
601
566
            # The dirblock has been updated
602
 
            self.assertEqual(st.st_size, entry[1][0][2])
603
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
567
            self.assertEqual(sha1sum, entry[1][0][1])
 
568
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
604
569
                             state._dirblock_state)
605
570
 
606
571
            del entry
615
580
        state.lock_read()
616
581
        try:
617
582
            entry = state._get_entry(0, path_utf8='a-file')
618
 
            self.assertEqual(st.st_size, entry[1][0][2])
 
583
            self.assertEqual(sha1sum, entry[1][0][1])
619
584
        finally:
620
585
            state.unlock()
621
586
 
622
587
    def test_save_fails_quietly_if_locked(self):
623
588
        """If dirstate is locked, save will fail without complaining."""
624
 
        state = self.create_updated_dirstate()
 
589
        self.build_tree(['a-file'])
 
590
        state = dirstate.DirState.initialize('dirstate')
 
591
        try:
 
592
            # No stat and no sha1 sum.
 
593
            state.add('a-file', 'a-file-id', 'file', None, '')
 
594
            state.save()
 
595
        finally:
 
596
            state.unlock()
 
597
 
 
598
        state = dirstate.DirState.on_file('dirstate')
 
599
        state.lock_read()
625
600
        try:
626
601
            entry = state._get_entry(0, path_utf8='a-file')
627
 
            # No cached sha1 yet.
628
 
            self.assertEqual('', entry[1][0][1])
629
 
            # Set the cutoff-time into the future, so things look cacheable
630
 
            state._sha_cutoff_time()
631
 
            state._cutoff_time += 10.0
632
 
            st = os.lstat('a-file')
633
 
            sha1sum = dirstate.update_entry(state, entry, 'a-file', st)
 
602
            sha1sum = state.update_entry(entry, 'a-file', os.lstat('a-file'))
 
603
            # We should have gotten a real sha1
634
604
            self.assertEqual('ecc5374e9ed82ad3ea3b4d452ea995a5fd3e70e3',
635
605
                             sha1sum)
636
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
 
606
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
637
607
                             state._dirblock_state)
638
608
 
639
609
            # Now, before we try to save, grab another dirstate, and take out a
655
625
                state2.unlock()
656
626
        finally:
657
627
            state.unlock()
658
 
 
 
628
        
659
629
        # The file on disk should not be modified.
660
630
        state = dirstate.DirState.on_file('dirstate')
661
631
        state.lock_read()
665
635
        finally:
666
636
            state.unlock()
667
637
 
668
 
    def test_save_refuses_if_changes_aborted(self):
669
 
        self.build_tree(['a-file', 'a-dir/'])
670
 
        state = dirstate.DirState.initialize('dirstate')
671
 
        try:
672
 
            # No stat and no sha1 sum.
673
 
            state.add('a-file', 'a-file-id', 'file', None, '')
674
 
            state.save()
675
 
        finally:
676
 
            state.unlock()
677
 
 
678
 
        # The dirstate should include TREE_ROOT and 'a-file' and nothing else
679
 
        expected_blocks = [
680
 
            ('', [(('', '', 'TREE_ROOT'),
681
 
                   [('d', '', 0, False, dirstate.DirState.NULLSTAT)])]),
682
 
            ('', [(('', 'a-file', 'a-file-id'),
683
 
                   [('f', '', 0, False, dirstate.DirState.NULLSTAT)])]),
684
 
        ]
685
 
 
686
 
        state = dirstate.DirState.on_file('dirstate')
687
 
        state.lock_write()
688
 
        try:
689
 
            state._read_dirblocks_if_needed()
690
 
            self.assertEqual(expected_blocks, state._dirblocks)
691
 
 
692
 
            # Now modify the state, but mark it as inconsistent
693
 
            state.add('a-dir', 'a-dir-id', 'directory', None, '')
694
 
            state._changes_aborted = True
695
 
            state.save()
696
 
        finally:
697
 
            state.unlock()
698
 
 
699
 
        state = dirstate.DirState.on_file('dirstate')
700
 
        state.lock_read()
701
 
        try:
702
 
            state._read_dirblocks_if_needed()
703
 
            self.assertEqual(expected_blocks, state._dirblocks)
704
 
        finally:
705
 
            state.unlock()
706
 
 
707
638
 
708
639
class TestDirStateInitialize(TestCaseWithDirState):
709
640
 
729
660
 
730
661
class TestDirStateManipulations(TestCaseWithDirState):
731
662
 
732
 
    def make_minimal_tree(self):
733
 
        tree1 = self.make_branch_and_memory_tree('tree1')
734
 
        tree1.lock_write()
735
 
        self.addCleanup(tree1.unlock)
736
 
        tree1.add('')
737
 
        revid1 = tree1.commit('foo')
738
 
        return tree1, revid1
739
 
 
740
 
    def test_update_minimal_updates_id_index(self):
741
 
        state = self.create_dirstate_with_root_and_subdir()
742
 
        self.addCleanup(state.unlock)
743
 
        id_index = state._get_id_index()
744
 
        self.assertEqual(['a-root-value', 'subdir-id'], sorted(id_index))
745
 
        state.add('file-name', 'file-id', 'file', None, '')
746
 
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
747
 
                         sorted(id_index))
748
 
        state.update_minimal(('', 'new-name', 'file-id'), 'f',
749
 
                             path_utf8='new-name')
750
 
        self.assertEqual(['a-root-value', 'file-id', 'subdir-id'],
751
 
                         sorted(id_index))
752
 
        self.assertEqual([('', 'new-name', 'file-id')],
753
 
                         sorted(id_index['file-id']))
754
 
        state._validate()
755
 
 
756
663
    def test_set_state_from_inventory_no_content_no_parents(self):
757
664
        # setting the current inventory is a slow but important api to support.
758
 
        tree1, revid1 = self.make_minimal_tree()
759
 
        inv = tree1.inventory
760
 
        root_id = inv.path2id('')
 
665
        tree1 = self.make_branch_and_memory_tree('tree1')
 
666
        tree1.lock_write()
 
667
        try:
 
668
            tree1.add('')
 
669
            revid1 = tree1.commit('foo').encode('utf8')
 
670
            root_id = tree1.inventory.root.file_id
 
671
            inv = tree1.inventory
 
672
        finally:
 
673
            tree1.unlock()
761
674
        expected_result = [], [
762
675
            (('', '', root_id), [
763
676
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
775
688
            # This will unlock it
776
689
            self.check_state_with_reopen(expected_result, state)
777
690
 
778
 
    def test_set_state_from_scratch_no_parents(self):
779
 
        tree1, revid1 = self.make_minimal_tree()
780
 
        inv = tree1.inventory
781
 
        root_id = inv.path2id('')
782
 
        expected_result = [], [
783
 
            (('', '', root_id), [
784
 
             ('d', '', 0, False, dirstate.DirState.NULLSTAT)])]
785
 
        state = dirstate.DirState.initialize('dirstate')
786
 
        try:
787
 
            state.set_state_from_scratch(inv, [], [])
788
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
789
 
                             state._header_state)
790
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
791
 
                             state._dirblock_state)
792
 
        except:
793
 
            state.unlock()
794
 
            raise
795
 
        else:
796
 
            # This will unlock it
797
 
            self.check_state_with_reopen(expected_result, state)
798
 
 
799
 
    def test_set_state_from_scratch_identical_parent(self):
800
 
        tree1, revid1 = self.make_minimal_tree()
801
 
        inv = tree1.inventory
802
 
        root_id = inv.path2id('')
803
 
        rev_tree1 = tree1.branch.repository.revision_tree(revid1)
804
 
        d_entry = ('d', '', 0, False, dirstate.DirState.NULLSTAT)
805
 
        parent_entry = ('d', '', 0, False, revid1)
806
 
        expected_result = [revid1], [
807
 
            (('', '', root_id), [d_entry, parent_entry])]
808
 
        state = dirstate.DirState.initialize('dirstate')
809
 
        try:
810
 
            state.set_state_from_scratch(inv, [(revid1, rev_tree1)], [])
811
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
812
 
                             state._header_state)
813
 
            self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
814
 
                             state._dirblock_state)
815
 
        except:
816
 
            state.unlock()
817
 
            raise
818
 
        else:
819
 
            # This will unlock it
820
 
            self.check_state_with_reopen(expected_result, state)
821
 
 
822
 
    def test_set_state_from_inventory_preserves_hashcache(self):
823
 
        # https://bugs.launchpad.net/bzr/+bug/146176
824
 
        # set_state_from_inventory should preserve the stat and hash value for
825
 
        # workingtree files that are not changed by the inventory.
826
 
 
827
 
        tree = self.make_branch_and_tree('.')
828
 
        # depends on the default format using dirstate...
829
 
        tree.lock_write()
830
 
        try:
831
 
            # make a dirstate with some valid hashcache data
832
 
            # file on disk, but that's not needed for this test
833
 
            foo_contents = 'contents of foo'
834
 
            self.build_tree_contents([('foo', foo_contents)])
835
 
            tree.add('foo', 'foo-id')
836
 
 
837
 
            foo_stat = os.stat('foo')
838
 
            foo_packed = dirstate.pack_stat(foo_stat)
839
 
            foo_sha = osutils.sha_string(foo_contents)
840
 
            foo_size = len(foo_contents)
841
 
 
842
 
            # should not be cached yet, because the file's too fresh
843
 
            self.assertEqual(
844
 
                (('', 'foo', 'foo-id',),
845
 
                 [('f', '', 0, False, dirstate.DirState.NULLSTAT)]),
846
 
                tree._dirstate._get_entry(0, 'foo-id'))
847
 
            # poke in some hashcache information - it wouldn't normally be
848
 
            # stored because it's too fresh
849
 
            tree._dirstate.update_minimal(
850
 
                ('', 'foo', 'foo-id'),
851
 
                'f', False, foo_sha, foo_packed, foo_size, 'foo')
852
 
            # now should be cached
853
 
            self.assertEqual(
854
 
                (('', 'foo', 'foo-id',),
855
 
                 [('f', foo_sha, foo_size, False, foo_packed)]),
856
 
                tree._dirstate._get_entry(0, 'foo-id'))
857
 
 
858
 
            # extract the inventory, and add something to it
859
 
            inv = tree._get_inventory()
860
 
            # should see the file we poked in...
861
 
            self.assertTrue(inv.has_id('foo-id'))
862
 
            self.assertTrue(inv.has_filename('foo'))
863
 
            inv.add_path('bar', 'file', 'bar-id')
864
 
            tree._dirstate._validate()
865
 
            # this used to cause it to lose its hashcache
866
 
            tree._dirstate.set_state_from_inventory(inv)
867
 
            tree._dirstate._validate()
868
 
        finally:
869
 
            tree.unlock()
870
 
 
871
 
        tree.lock_read()
872
 
        try:
873
 
            # now check that the state still has the original hashcache value
874
 
            state = tree._dirstate
875
 
            state._validate()
876
 
            foo_tuple = state._get_entry(0, path_utf8='foo')
877
 
            self.assertEqual(
878
 
                (('', 'foo', 'foo-id',),
879
 
                 [('f', foo_sha, len(foo_contents), False,
880
 
                   dirstate.pack_stat(foo_stat))]),
881
 
                foo_tuple)
882
 
        finally:
883
 
            tree.unlock()
884
 
 
885
 
    def test_set_state_from_inventory_mixed_paths(self):
886
 
        tree1 = self.make_branch_and_tree('tree1')
887
 
        self.build_tree(['tree1/a/', 'tree1/a/b/', 'tree1/a-b/',
888
 
                         'tree1/a/b/foo', 'tree1/a-b/bar'])
889
 
        tree1.lock_write()
890
 
        try:
891
 
            tree1.add(['a', 'a/b', 'a-b', 'a/b/foo', 'a-b/bar'],
892
 
                      ['a-id', 'b-id', 'a-b-id', 'foo-id', 'bar-id'])
893
 
            tree1.commit('rev1', rev_id='rev1')
894
 
            root_id = tree1.get_root_id()
895
 
            inv = tree1.inventory
896
 
        finally:
897
 
            tree1.unlock()
898
 
        expected_result1 = [('', '', root_id, 'd'),
899
 
                            ('', 'a', 'a-id', 'd'),
900
 
                            ('', 'a-b', 'a-b-id', 'd'),
901
 
                            ('a', 'b', 'b-id', 'd'),
902
 
                            ('a/b', 'foo', 'foo-id', 'f'),
903
 
                            ('a-b', 'bar', 'bar-id', 'f'),
904
 
                           ]
905
 
        expected_result2 = [('', '', root_id, 'd'),
906
 
                            ('', 'a', 'a-id', 'd'),
907
 
                            ('', 'a-b', 'a-b-id', 'd'),
908
 
                            ('a-b', 'bar', 'bar-id', 'f'),
909
 
                           ]
910
 
        state = dirstate.DirState.initialize('dirstate')
911
 
        try:
912
 
            state.set_state_from_inventory(inv)
913
 
            values = []
914
 
            for entry in state._iter_entries():
915
 
                values.append(entry[0] + entry[1][0][:1])
916
 
            self.assertEqual(expected_result1, values)
917
 
            del inv['b-id']
918
 
            state.set_state_from_inventory(inv)
919
 
            values = []
920
 
            for entry in state._iter_entries():
921
 
                values.append(entry[0] + entry[1][0][:1])
922
 
            self.assertEqual(expected_result2, values)
923
 
        finally:
924
 
            state.unlock()
925
 
 
926
691
    def test_set_path_id_no_parents(self):
927
692
        """The id of a path can be changed trivally with no parents."""
928
693
        state = dirstate.DirState.initialize('dirstate')
929
694
        try:
930
695
            # check precondition to be sure the state does change appropriately.
931
 
            root_entry = (('', '', 'TREE_ROOT'), [('d', '', 0, False, 'x'*32)])
932
 
            self.assertEqual([root_entry], list(state._iter_entries()))
933
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
934
 
            self.assertEqual(root_entry,
935
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
936
 
            self.assertEqual((None, None),
937
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
938
 
            state.set_path_id('', 'second-root-id')
939
 
            new_root_entry = (('', '', 'second-root-id'),
940
 
                              [('d', '', 0, False, 'x'*32)])
941
 
            expected_rows = [new_root_entry]
 
696
            self.assertEqual(
 
697
                [(('', '', 'TREE_ROOT'), [('d', '', 0, False,
 
698
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])],
 
699
                list(state._iter_entries()))
 
700
            state.set_path_id('', 'foobarbaz')
 
701
            expected_rows = [
 
702
                (('', '', 'foobarbaz'), [('d', '', 0, False,
 
703
                   'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])]
942
704
            self.assertEqual(expected_rows, list(state._iter_entries()))
943
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
944
 
            self.assertEqual(new_root_entry, 
945
 
                             state._get_entry(0, fileid_utf8='second-root-id'))
946
 
            self.assertEqual((None, None),
947
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
948
705
            # should work across save too
949
706
            state.save()
950
707
        finally:
968
725
        state._validate()
969
726
        try:
970
727
            state.set_parent_trees([('parent-revid', rt)], ghosts=[])
971
 
            root_entry = (('', '', 'TREE_ROOT'),
972
 
                          [('d', '', 0, False, 'x'*32),
973
 
                           ('d', '', 0, False, 'parent-revid')])
974
 
            self.assertEqual(root_entry, state._get_entry(0, path_utf8=''))
975
 
            self.assertEqual(root_entry,
976
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
977
 
            self.assertEqual((None, None),
978
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
979
 
            state.set_path_id('', 'Asecond-root-id')
 
728
            state.set_path_id('', 'foobarbaz')
980
729
            state._validate()
981
730
            # now see that it is what we expected
982
 
            old_root_entry = (('', '', 'TREE_ROOT'),
983
 
                              [('a', '', 0, False, ''),
984
 
                               ('d', '', 0, False, 'parent-revid')])
985
 
            new_root_entry = (('', '', 'Asecond-root-id'),
986
 
                              [('d', '', 0, False, ''),
987
 
                               ('a', '', 0, False, '')])
988
 
            expected_rows = [new_root_entry, old_root_entry]
 
731
            expected_rows = [
 
732
                (('', '', 'TREE_ROOT'),
 
733
                    [('a', '', 0, False, ''),
 
734
                     ('d', '', 0, False, 'parent-revid'),
 
735
                     ]),
 
736
                (('', '', 'foobarbaz'),
 
737
                    [('d', '', 0, False, ''),
 
738
                     ('a', '', 0, False, ''),
 
739
                     ]),
 
740
                ]
989
741
            state._validate()
990
742
            self.assertEqual(expected_rows, list(state._iter_entries()))
991
 
            self.assertEqual(new_root_entry, state._get_entry(0, path_utf8=''))
992
 
            self.assertEqual(old_root_entry, state._get_entry(1, path_utf8=''))
993
 
            self.assertEqual((None, None),
994
 
                             state._get_entry(0, fileid_utf8='TREE_ROOT'))
995
 
            self.assertEqual(old_root_entry,
996
 
                             state._get_entry(1, fileid_utf8='TREE_ROOT'))
997
 
            self.assertEqual(new_root_entry,
998
 
                             state._get_entry(0, fileid_utf8='Asecond-root-id'))
999
 
            self.assertEqual((None, None),
1000
 
                             state._get_entry(1, fileid_utf8='Asecond-root-id'))
1001
743
            # should work across save too
1002
744
            state.save()
1003
745
        finally:
1019
761
        finally:
1020
762
            state.unlock()
1021
763
 
 
764
 
1022
765
    def test_set_parent_trees_no_content(self):
1023
766
        # set_parent_trees is a slow but important api to support.
1024
767
        tree1 = self.make_branch_and_memory_tree('tree1')
1029
772
        finally:
1030
773
            tree1.unlock()
1031
774
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1032
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
775
        tree2 = MemoryTree.create_on_branch(branch2)
1033
776
        tree2.lock_write()
1034
777
        try:
1035
778
            revid2 = tree2.commit('foo')
1036
 
            root_id = tree2.get_root_id()
 
779
            root_id = tree2.inventory.root.file_id
1037
780
        finally:
1038
781
            tree2.unlock()
1039
782
        state = dirstate.DirState.initialize('dirstate')
1067
810
            state.set_parent_trees(
1068
811
                ((revid1, tree1.branch.repository.revision_tree(revid1)),
1069
812
                 (revid2, tree2.branch.repository.revision_tree(revid2)),
1070
 
                 ('ghost-rev', tree2.branch.repository.revision_tree(
1071
 
                                   _mod_revision.NULL_REVISION))),
 
813
                 ('ghost-rev', tree2.branch.repository.revision_tree(None))),
1072
814
                ['ghost-rev'])
1073
815
            self.assertEqual([revid1, revid2, 'ghost-rev'],
1074
816
                             state.get_parent_ids())
1078
820
                [(('', '', root_id), [
1079
821
                  ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1080
822
                  ('d', '', 0, False, revid1),
1081
 
                  ('d', '', 0, False, revid1)
 
823
                  ('d', '', 0, False, revid2)
1082
824
                  ])],
1083
825
                list(state._iter_entries()))
1084
826
        finally:
1099
841
        finally:
1100
842
            tree1.unlock()
1101
843
        branch2 = tree1.branch.bzrdir.clone('tree2').open_branch()
1102
 
        tree2 = memorytree.MemoryTree.create_on_branch(branch2)
 
844
        tree2 = MemoryTree.create_on_branch(branch2)
1103
845
        tree2.lock_write()
1104
846
        try:
1105
847
            tree2.put_file_bytes_non_atomic('file-id', 'new file-content')
1106
848
            revid2 = tree2.commit('foo')
1107
 
            root_id = tree2.get_root_id()
 
849
            root_id = tree2.inventory.root.file_id
1108
850
        finally:
1109
851
            tree2.unlock()
1110
852
        # check the layout in memory
1112
854
            (('', '', root_id), [
1113
855
             ('d', '', 0, False, dirstate.DirState.NULLSTAT),
1114
856
             ('d', '', 0, False, revid1.encode('utf8')),
1115
 
             ('d', '', 0, False, revid1.encode('utf8'))
 
857
             ('d', '', 0, False, revid2.encode('utf8'))
1116
858
             ]),
1117
859
            (('', 'a file', 'file-id'), [
1118
860
             ('a', '', 0, False, ''),
1150
892
            (('', '', 'TREE_ROOT'), [
1151
893
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1152
894
             ]),
1153
 
            (('', 'a file', 'a-file-id'), [
 
895
            (('', 'a file', 'a file id'), [
1154
896
             ('f', '1'*20, 19, False, dirstate.pack_stat(stat)), # current tree
1155
897
             ]),
1156
898
            ]
1157
899
        try:
1158
 
            state.add('a file', 'a-file-id', 'file', stat, '1'*20)
 
900
            state.add('a file', 'a file id', 'file', stat, '1'*20)
1159
901
            # having added it, it should be in the output of iter_entries.
1160
902
            self.assertEqual(expected_entries, list(state._iter_entries()))
1161
903
            # saving and reloading should not affect this.
1164
906
            state.unlock()
1165
907
        state = dirstate.DirState.on_file('dirstate')
1166
908
        state.lock_read()
1167
 
        self.addCleanup(state.unlock)
1168
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
909
        try:
 
910
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
911
        finally:
 
912
            state.unlock()
1169
913
 
1170
914
    def test_add_path_to_unversioned_directory(self):
1171
915
        """Adding a path to an unversioned directory should error.
1176
920
        """
1177
921
        self.build_tree(['unversioned/', 'unversioned/a file'])
1178
922
        state = dirstate.DirState.initialize('dirstate')
1179
 
        self.addCleanup(state.unlock)
1180
 
        self.assertRaises(errors.NotVersionedError, state.add,
1181
 
                          'unversioned/a file', 'a-file-id', 'file', None, None)
 
923
        try:
 
924
            self.assertRaises(errors.NotVersionedError, state.add,
 
925
                'unversioned/a file', 'a file id', 'file', None, None)
 
926
        finally:
 
927
            state.unlock()
1182
928
 
1183
929
    def test_add_directory_to_root_no_parents_all_data(self):
1184
930
        # The most trivial addition of a dir is when there are no parents and
1204
950
            state.unlock()
1205
951
        state = dirstate.DirState.on_file('dirstate')
1206
952
        state.lock_read()
1207
 
        self.addCleanup(state.unlock)
1208
953
        state._validate()
1209
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
954
        try:
 
955
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
956
        finally:
 
957
            state.unlock()
1210
958
 
1211
 
    def _test_add_symlink_to_root_no_parents_all_data(self, link_name, target):
 
959
    def test_add_symlink_to_root_no_parents_all_data(self):
1212
960
        # The most trivial addition of a symlink when there are no parents and
1213
961
        # its in the root and all data about the file is supplied
1214
962
        # bzr doesn't support fake symlinks on windows, yet.
1215
 
        self.requireFeature(features.SymlinkFeature)
1216
 
        os.symlink(target, link_name)
1217
 
        stat = os.lstat(link_name)
 
963
        if not has_symlinks():
 
964
            raise TestSkipped("No symlink support")
 
965
        os.symlink('target', 'a link')
 
966
        stat = os.lstat('a link')
1218
967
        expected_entries = [
1219
968
            (('', '', 'TREE_ROOT'), [
1220
969
             ('d', '', 0, False, dirstate.DirState.NULLSTAT), # current tree
1221
970
             ]),
1222
 
            (('', link_name.encode('UTF-8'), 'a link id'), [
1223
 
             ('l', target.encode('UTF-8'), stat[6],
1224
 
              False, dirstate.pack_stat(stat)), # current tree
 
971
            (('', 'a link', 'a link id'), [
 
972
             ('l', 'target', 6, False, dirstate.pack_stat(stat)), # current tree
1225
973
             ]),
1226
974
            ]
1227
975
        state = dirstate.DirState.initialize('dirstate')
1228
976
        try:
1229
 
            state.add(link_name, 'a link id', 'symlink', stat,
1230
 
                      target.encode('UTF-8'))
 
977
            state.add('a link', 'a link id', 'symlink', stat, 'target')
1231
978
            # having added it, it should be in the output of iter_entries.
1232
979
            self.assertEqual(expected_entries, list(state._iter_entries()))
1233
980
            # saving and reloading should not affect this.
1236
983
            state.unlock()
1237
984
        state = dirstate.DirState.on_file('dirstate')
1238
985
        state.lock_read()
1239
 
        self.addCleanup(state.unlock)
1240
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
1241
 
 
1242
 
    def test_add_symlink_to_root_no_parents_all_data(self):
1243
 
        self._test_add_symlink_to_root_no_parents_all_data('a link', 'target')
1244
 
 
1245
 
    def test_add_symlink_unicode_to_root_no_parents_all_data(self):
1246
 
        self.requireFeature(features.UnicodeFilenameFeature)
1247
 
        self._test_add_symlink_to_root_no_parents_all_data(
1248
 
            u'\N{Euro Sign}link', u'targ\N{Euro Sign}et')
 
986
        try:
 
987
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
988
        finally:
 
989
            state.unlock()
1249
990
 
1250
991
    def test_add_directory_and_child_no_parents_all_data(self):
1251
992
        # after adding a directory, we should be able to add children to it.
1259
1000
            (('', 'a dir', 'a dir id'), [
1260
1001
             ('d', '', 0, False, dirstate.pack_stat(dirstat)), # current tree
1261
1002
             ]),
1262
 
            (('a dir', 'a file', 'a-file-id'), [
 
1003
            (('a dir', 'a file', 'a file id'), [
1263
1004
             ('f', '1'*20, 25, False,
1264
1005
              dirstate.pack_stat(filestat)), # current tree details
1265
1006
             ]),
1267
1008
        state = dirstate.DirState.initialize('dirstate')
1268
1009
        try:
1269
1010
            state.add('a dir', 'a dir id', 'directory', dirstat, None)
1270
 
            state.add('a dir/a file', 'a-file-id', 'file', filestat, '1'*20)
 
1011
            state.add('a dir/a file', 'a file id', 'file', filestat, '1'*20)
1271
1012
            # added it, it should be in the output of iter_entries.
1272
1013
            self.assertEqual(expected_entries, list(state._iter_entries()))
1273
1014
            # saving and reloading should not affect this.
1276
1017
            state.unlock()
1277
1018
        state = dirstate.DirState.on_file('dirstate')
1278
1019
        state.lock_read()
1279
 
        self.addCleanup(state.unlock)
1280
 
        self.assertEqual(expected_entries, list(state._iter_entries()))
 
1020
        try:
 
1021
            self.assertEqual(expected_entries, list(state._iter_entries()))
 
1022
        finally:
 
1023
            state.unlock()
1281
1024
 
1282
1025
    def test_add_tree_reference(self):
1283
1026
        # make a dirstate and add a tree reference
1297
1040
            state.unlock()
1298
1041
        # now check we can read it back
1299
1042
        state.lock_read()
1300
 
        self.addCleanup(state.unlock)
1301
1043
        state._validate()
1302
 
        entry2 = state._get_entry(0, 'subdir-id', 'subdir')
1303
 
        self.assertEqual(entry, entry2)
1304
 
        self.assertEqual(entry, expected_entry)
1305
 
        # and lookup by id should work too
1306
 
        entry2 = state._get_entry(0, fileid_utf8='subdir-id')
1307
 
        self.assertEqual(entry, expected_entry)
 
1044
        try:
 
1045
            entry2 = state._get_entry(0, 'subdir-id', 'subdir')
 
1046
            self.assertEqual(entry, entry2)
 
1047
            self.assertEqual(entry, expected_entry)
 
1048
            # and lookup by id should work too
 
1049
            entry2 = state._get_entry(0, fileid_utf8='subdir-id')
 
1050
            self.assertEqual(entry, expected_entry)
 
1051
        finally:
 
1052
            state.unlock()
1308
1053
 
1309
1054
    def test_add_forbidden_names(self):
1310
1055
        state = dirstate.DirState.initialize('dirstate')
1314
1059
        self.assertRaises(errors.BzrError,
1315
1060
            state.add, '..', 'ass-id', 'directory', None, None)
1316
1061
 
1317
 
    def test_set_state_with_rename_b_a_bug_395556(self):
1318
 
        # bug 395556 uncovered a bug where the dirstate ends up with a false
1319
 
        # relocation record - in a tree with no parents there should be no
1320
 
        # absent or relocated records. This then leads to further corruption
1321
 
        # when a commit occurs, as the incorrect relocation gathers an
1322
 
        # incorrect absent in tree 1, and future changes go to pot.
1323
 
        tree1 = self.make_branch_and_tree('tree1')
1324
 
        self.build_tree(['tree1/b'])
1325
 
        tree1.lock_write()
1326
 
        try:
1327
 
            tree1.add(['b'], ['b-id'])
1328
 
            root_id = tree1.get_root_id()
1329
 
            inv = tree1.inventory
1330
 
            state = dirstate.DirState.initialize('dirstate')
1331
 
            try:
1332
 
                # Set the initial state with 'b'
1333
 
                state.set_state_from_inventory(inv)
1334
 
                inv.rename('b-id', root_id, 'a')
1335
 
                # Set the new state with 'a', which currently corrupts.
1336
 
                state.set_state_from_inventory(inv)
1337
 
                expected_result1 = [('', '', root_id, 'd'),
1338
 
                                    ('', 'a', 'b-id', 'f'),
1339
 
                                   ]
1340
 
                values = []
1341
 
                for entry in state._iter_entries():
1342
 
                    values.append(entry[0] + entry[1][0][:1])
1343
 
                self.assertEqual(expected_result1, values)
1344
 
            finally:
1345
 
                state.unlock()
1346
 
        finally:
1347
 
            tree1.unlock()
1348
 
 
1349
 
 
1350
 
class TestDirStateHashUpdates(TestCaseWithDirState):
1351
 
 
1352
 
    def do_update_entry(self, state, path):
1353
 
        entry = state._get_entry(0, path_utf8=path)
1354
 
        stat = os.lstat(path)
1355
 
        return dirstate.update_entry(state, entry, os.path.abspath(path), stat)
1356
 
 
1357
 
    def _read_state_content(self, state):
1358
 
        """Read the content of the dirstate file.
1359
 
 
1360
 
        On Windows when one process locks a file, you can't even open() the
1361
 
        file in another process (to read it). So we go directly to
1362
 
        state._state_file. This should always be the exact disk representation,
1363
 
        so it is reasonable to do so.
1364
 
        DirState also always seeks before reading, so it doesn't matter if we
1365
 
        bump the file pointer.
1366
 
        """
1367
 
        state._state_file.seek(0)
1368
 
        return state._state_file.read()
1369
 
 
1370
 
    def test_worth_saving_limit_avoids_writing(self):
1371
 
        tree = self.make_branch_and_tree('.')
1372
 
        self.build_tree(['c', 'd'])
1373
 
        tree.lock_write()
1374
 
        tree.add(['c', 'd'], ['c-id', 'd-id'])
1375
 
        tree.commit('add c and d')
1376
 
        state = InstrumentedDirState.on_file(tree.current_dirstate()._filename,
1377
 
                                             worth_saving_limit=2)
1378
 
        tree.unlock()
1379
 
        state.lock_write()
1380
 
        self.addCleanup(state.unlock)
1381
 
        state._read_dirblocks_if_needed()
1382
 
        state.adjust_time(+20) # Allow things to be cached
1383
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1384
 
                         state._dirblock_state)
1385
 
        content = self._read_state_content(state)
1386
 
        self.do_update_entry(state, 'c')
1387
 
        self.assertEqual(1, len(state._known_hash_changes))
1388
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
1389
 
                         state._dirblock_state)
1390
 
        state.save()
1391
 
        # It should not have set the state to IN_MEMORY_UNMODIFIED because the
1392
 
        # hash values haven't been written out.
1393
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
1394
 
                         state._dirblock_state)
1395
 
        self.assertEqual(content, self._read_state_content(state))
1396
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_HASH_MODIFIED,
1397
 
                         state._dirblock_state)
1398
 
        self.do_update_entry(state, 'd')
1399
 
        self.assertEqual(2, len(state._known_hash_changes))
1400
 
        state.save()
1401
 
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
1402
 
                         state._dirblock_state)
1403
 
        self.assertEqual(0, len(state._known_hash_changes))
1404
 
 
1405
1062
 
1406
1063
class TestGetLines(TestCaseWithDirState):
1407
1064
 
1620
1277
            state.unlock()
1621
1278
 
1622
1279
 
1623
 
class TestIterChildEntries(TestCaseWithDirState):
1624
 
 
1625
 
    def create_dirstate_with_two_trees(self):
1626
 
        """This dirstate contains multiple files and directories.
1627
 
 
1628
 
         /        a-root-value
1629
 
         a/       a-dir
1630
 
         b/       b-dir
1631
 
         c        c-file
1632
 
         d        d-file
1633
 
         a/e/     e-dir
1634
 
         a/f      f-file
1635
 
         b/g      g-file
1636
 
         b/h\xc3\xa5  h-\xc3\xa5-file  #This is u'\xe5' encoded into utf-8
1637
 
 
1638
 
        Notice that a/e is an empty directory.
1639
 
 
1640
 
        There is one parent tree, which has the same shape with the following variations:
1641
 
        b/g in the parent is gone.
1642
 
        b/h in the parent has a different id
1643
 
        b/i is new in the parent
1644
 
        c is renamed to b/j in the parent
1645
 
 
1646
 
        :return: The dirstate, still write-locked.
1647
 
        """
1648
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
1649
 
        null_sha = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
1650
 
        NULL_PARENT_DETAILS = dirstate.DirState.NULL_PARENT_DETAILS
1651
 
        root_entry = ('', '', 'a-root-value'), [
1652
 
            ('d', '', 0, False, packed_stat),
1653
 
            ('d', '', 0, False, 'parent-revid'),
1654
 
            ]
1655
 
        a_entry = ('', 'a', 'a-dir'), [
1656
 
            ('d', '', 0, False, packed_stat),
1657
 
            ('d', '', 0, False, 'parent-revid'),
1658
 
            ]
1659
 
        b_entry = ('', 'b', 'b-dir'), [
1660
 
            ('d', '', 0, False, packed_stat),
1661
 
            ('d', '', 0, False, 'parent-revid'),
1662
 
            ]
1663
 
        c_entry = ('', 'c', 'c-file'), [
1664
 
            ('f', null_sha, 10, False, packed_stat),
1665
 
            ('r', 'b/j', 0, False, ''),
1666
 
            ]
1667
 
        d_entry = ('', 'd', 'd-file'), [
1668
 
            ('f', null_sha, 20, False, packed_stat),
1669
 
            ('f', 'd', 20, False, 'parent-revid'),
1670
 
            ]
1671
 
        e_entry = ('a', 'e', 'e-dir'), [
1672
 
            ('d', '', 0, False, packed_stat),
1673
 
            ('d', '', 0, False, 'parent-revid'),
1674
 
            ]
1675
 
        f_entry = ('a', 'f', 'f-file'), [
1676
 
            ('f', null_sha, 30, False, packed_stat),
1677
 
            ('f', 'f', 20, False, 'parent-revid'),
1678
 
            ]
1679
 
        g_entry = ('b', 'g', 'g-file'), [
1680
 
            ('f', null_sha, 30, False, packed_stat),
1681
 
            NULL_PARENT_DETAILS,
1682
 
            ]
1683
 
        h_entry1 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file1'), [
1684
 
            ('f', null_sha, 40, False, packed_stat),
1685
 
            NULL_PARENT_DETAILS,
1686
 
            ]
1687
 
        h_entry2 = ('b', 'h\xc3\xa5', 'h-\xc3\xa5-file2'), [
1688
 
            NULL_PARENT_DETAILS,
1689
 
            ('f', 'h', 20, False, 'parent-revid'),
1690
 
            ]
1691
 
        i_entry = ('b', 'i', 'i-file'), [
1692
 
            NULL_PARENT_DETAILS,
1693
 
            ('f', 'h', 20, False, 'parent-revid'),
1694
 
            ]
1695
 
        j_entry = ('b', 'j', 'c-file'), [
1696
 
            ('r', 'c', 0, False, ''),
1697
 
            ('f', 'j', 20, False, 'parent-revid'),
1698
 
            ]
1699
 
        dirblocks = []
1700
 
        dirblocks.append(('', [root_entry]))
1701
 
        dirblocks.append(('', [a_entry, b_entry, c_entry, d_entry]))
1702
 
        dirblocks.append(('a', [e_entry, f_entry]))
1703
 
        dirblocks.append(('b', [g_entry, h_entry1, h_entry2, i_entry, j_entry]))
1704
 
        state = dirstate.DirState.initialize('dirstate')
1705
 
        state._validate()
1706
 
        try:
1707
 
            state._set_data(['parent'], dirblocks)
1708
 
        except:
1709
 
            state.unlock()
1710
 
            raise
1711
 
        return state, dirblocks
1712
 
 
1713
 
    def test_iter_children_b(self):
1714
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1715
 
        self.addCleanup(state.unlock)
1716
 
        expected_result = []
1717
 
        expected_result.append(dirblocks[3][1][2]) # h2
1718
 
        expected_result.append(dirblocks[3][1][3]) # i
1719
 
        expected_result.append(dirblocks[3][1][4]) # j
1720
 
        self.assertEqual(expected_result,
1721
 
            list(state._iter_child_entries(1, 'b')))
1722
 
 
1723
 
    def test_iter_child_root(self):
1724
 
        state, dirblocks = self.create_dirstate_with_two_trees()
1725
 
        self.addCleanup(state.unlock)
1726
 
        expected_result = []
1727
 
        expected_result.append(dirblocks[1][1][0]) # a
1728
 
        expected_result.append(dirblocks[1][1][1]) # b
1729
 
        expected_result.append(dirblocks[1][1][3]) # d
1730
 
        expected_result.append(dirblocks[2][1][0]) # e
1731
 
        expected_result.append(dirblocks[2][1][1]) # f
1732
 
        expected_result.append(dirblocks[3][1][2]) # h2
1733
 
        expected_result.append(dirblocks[3][1][3]) # i
1734
 
        expected_result.append(dirblocks[3][1][4]) # j
1735
 
        self.assertEqual(expected_result,
1736
 
            list(state._iter_child_entries(1, '')))
1737
 
 
1738
 
 
1739
 
class TestDirstateSortOrder(tests.TestCaseWithTransport):
 
1280
class TestDirstateSortOrder(TestCaseWithTransport):
1740
1281
    """Test that DirState adds entries in the right order."""
1741
1282
 
1742
1283
    def test_add_sorting(self):
1791
1332
 
1792
1333
        # *really* cheesy way to just get an empty tree
1793
1334
        repo = self.make_repository('repo')
1794
 
        empty_tree = repo.revision_tree(_mod_revision.NULL_REVISION)
 
1335
        empty_tree = repo.revision_tree(None)
1795
1336
        state.set_parent_trees([('null:', empty_tree)], [])
1796
1337
 
1797
1338
        dirblock_names = [d[0] for d in state._dirblocks]
1801
1342
class InstrumentedDirState(dirstate.DirState):
1802
1343
    """An DirState with instrumented sha1 functionality."""
1803
1344
 
1804
 
    def __init__(self, path, sha1_provider, worth_saving_limit=0):
1805
 
        super(InstrumentedDirState, self).__init__(path, sha1_provider,
1806
 
            worth_saving_limit=worth_saving_limit)
 
1345
    def __init__(self, path):
 
1346
        super(InstrumentedDirState, self).__init__(path)
1807
1347
        self._time_offset = 0
1808
1348
        self._log = []
1809
 
        # member is dynamically set in DirState.__init__ to turn on trace
1810
 
        self._sha1_provider = sha1_provider
1811
 
        self._sha1_file = self._sha1_file_and_log
1812
1349
 
1813
1350
    def _sha_cutoff_time(self):
1814
1351
        timestamp = super(InstrumentedDirState, self)._sha_cutoff_time()
1815
1352
        self._cutoff_time = timestamp + self._time_offset
1816
1353
 
1817
 
    def _sha1_file_and_log(self, abspath):
 
1354
    def _sha1_file(self, abspath, entry):
1818
1355
        self._log.append(('sha1', abspath))
1819
 
        return self._sha1_provider.sha1(abspath)
 
1356
        return super(InstrumentedDirState, self)._sha1_file(abspath, entry)
1820
1357
 
1821
1358
    def _read_link(self, abspath, old_link):
1822
1359
        self._log.append(('read_link', abspath, old_link))
1853
1390
        self.st_ino = ino
1854
1391
        self.st_mode = mode
1855
1392
 
1856
 
    @staticmethod
1857
 
    def from_stat(st):
1858
 
        return _FakeStat(st.st_size, st.st_mtime, st.st_ctime, st.st_dev,
1859
 
            st.st_ino, st.st_mode)
1860
 
 
1861
 
 
1862
 
class TestPackStat(tests.TestCaseWithTransport):
 
1393
 
 
1394
class TestUpdateEntry(TestCaseWithDirState):
 
1395
    """Test the DirState.update_entry functions"""
 
1396
 
 
1397
    def get_state_with_a(self):
 
1398
        """Create a DirState tracking a single object named 'a'"""
 
1399
        state = InstrumentedDirState.initialize('dirstate')
 
1400
        self.addCleanup(state.unlock)
 
1401
        state.add('a', 'a-id', 'file', None, '')
 
1402
        entry = state._get_entry(0, path_utf8='a')
 
1403
        return state, entry
 
1404
 
 
1405
    def test_update_entry(self):
 
1406
        state, entry = self.get_state_with_a()
 
1407
        self.build_tree(['a'])
 
1408
        # Add one where we don't provide the stat or sha already
 
1409
        self.assertEqual(('', 'a', 'a-id'), entry[0])
 
1410
        self.assertEqual([('f', '', 0, False, dirstate.DirState.NULLSTAT)],
 
1411
                         entry[1])
 
1412
        # Flush the buffers to disk
 
1413
        state.save()
 
1414
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1415
                         state._dirblock_state)
 
1416
 
 
1417
        stat_value = os.lstat('a')
 
1418
        packed_stat = dirstate.pack_stat(stat_value)
 
1419
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1420
                                          stat_value=stat_value)
 
1421
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1422
                         link_or_sha1)
 
1423
 
 
1424
        # The dirblock entry should be updated with the new info
 
1425
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1426
                         entry[1])
 
1427
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1428
                         state._dirblock_state)
 
1429
        mode = stat_value.st_mode
 
1430
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False)], state._log)
 
1431
 
 
1432
        state.save()
 
1433
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1434
                         state._dirblock_state)
 
1435
 
 
1436
        # If we do it again right away, we don't know if the file has changed
 
1437
        # so we will re-read the file. Roll the clock back so the file is
 
1438
        # guaranteed to look too new.
 
1439
        state.adjust_time(-10)
 
1440
 
 
1441
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1442
                                          stat_value=stat_value)
 
1443
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1444
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1445
                         ], state._log)
 
1446
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1447
                         link_or_sha1)
 
1448
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1449
                         state._dirblock_state)
 
1450
        state.save()
 
1451
 
 
1452
        # However, if we move the clock forward so the file is considered
 
1453
        # "stable", it should just returned the cached value.
 
1454
        state.adjust_time(20)
 
1455
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1456
                                          stat_value=stat_value)
 
1457
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1458
                         link_or_sha1)
 
1459
        self.assertEqual([('sha1', 'a'), ('is_exec', mode, False),
 
1460
                          ('sha1', 'a'), ('is_exec', mode, False),
 
1461
                         ], state._log)
 
1462
 
 
1463
    def test_update_entry_no_stat_value(self):
 
1464
        """Passing the stat_value is optional."""
 
1465
        state, entry = self.get_state_with_a()
 
1466
        state.adjust_time(-10) # Make sure the file looks new
 
1467
        self.build_tree(['a'])
 
1468
        # Add one where we don't provide the stat or sha already
 
1469
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1470
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1471
                         link_or_sha1)
 
1472
        stat_value = os.lstat('a')
 
1473
        self.assertEqual([('lstat', 'a'), ('sha1', 'a'),
 
1474
                          ('is_exec', stat_value.st_mode, False),
 
1475
                         ], state._log)
 
1476
 
 
1477
    def test_update_entry_symlink(self):
 
1478
        """Update entry should read symlinks."""
 
1479
        if not osutils.has_symlinks():
 
1480
            # PlatformDeficiency / TestSkipped
 
1481
            raise TestSkipped("No symlink support")
 
1482
        state, entry = self.get_state_with_a()
 
1483
        state.save()
 
1484
        self.assertEqual(dirstate.DirState.IN_MEMORY_UNMODIFIED,
 
1485
                         state._dirblock_state)
 
1486
        os.symlink('target', 'a')
 
1487
 
 
1488
        state.adjust_time(-10) # Make the symlink look new
 
1489
        stat_value = os.lstat('a')
 
1490
        packed_stat = dirstate.pack_stat(stat_value)
 
1491
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1492
                                          stat_value=stat_value)
 
1493
        self.assertEqual('target', link_or_sha1)
 
1494
        self.assertEqual([('read_link', 'a', '')], state._log)
 
1495
        # Dirblock is updated
 
1496
        self.assertEqual([('l', link_or_sha1, 6, False, packed_stat)],
 
1497
                         entry[1])
 
1498
        self.assertEqual(dirstate.DirState.IN_MEMORY_MODIFIED,
 
1499
                         state._dirblock_state)
 
1500
 
 
1501
        # Because the stat_value looks new, we should re-read the target
 
1502
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1503
                                          stat_value=stat_value)
 
1504
        self.assertEqual('target', link_or_sha1)
 
1505
        self.assertEqual([('read_link', 'a', ''),
 
1506
                          ('read_link', 'a', 'target'),
 
1507
                         ], state._log)
 
1508
        state.adjust_time(+20) # Skip into the future, all files look old
 
1509
        link_or_sha1 = state.update_entry(entry, abspath='a',
 
1510
                                          stat_value=stat_value)
 
1511
        self.assertEqual('target', link_or_sha1)
 
1512
        # There should not be a new read_link call.
 
1513
        # (this is a weak assertion, because read_link is fairly inexpensive,
 
1514
        # versus the number of symlinks that we would have)
 
1515
        self.assertEqual([('read_link', 'a', ''),
 
1516
                          ('read_link', 'a', 'target'),
 
1517
                         ], state._log)
 
1518
 
 
1519
    def test_update_entry_dir(self):
 
1520
        state, entry = self.get_state_with_a()
 
1521
        self.build_tree(['a/'])
 
1522
        self.assertIs(None, state.update_entry(entry, 'a'))
 
1523
 
 
1524
    def create_and_test_file(self, state, entry):
 
1525
        """Create a file at 'a' and verify the state finds it.
 
1526
 
 
1527
        The state should already be versioning *something* at 'a'. This makes
 
1528
        sure that state.update_entry recognizes it as a file.
 
1529
        """
 
1530
        self.build_tree(['a'])
 
1531
        stat_value = os.lstat('a')
 
1532
        packed_stat = dirstate.pack_stat(stat_value)
 
1533
 
 
1534
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1535
        self.assertEqual('b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6',
 
1536
                         link_or_sha1)
 
1537
        self.assertEqual([('f', link_or_sha1, 14, False, packed_stat)],
 
1538
                         entry[1])
 
1539
        return packed_stat
 
1540
 
 
1541
    def create_and_test_dir(self, state, entry):
 
1542
        """Create a directory at 'a' and verify the state finds it.
 
1543
 
 
1544
        The state should already be versioning *something* at 'a'. This makes
 
1545
        sure that state.update_entry recognizes it as a directory.
 
1546
        """
 
1547
        self.build_tree(['a/'])
 
1548
        stat_value = os.lstat('a')
 
1549
        packed_stat = dirstate.pack_stat(stat_value)
 
1550
 
 
1551
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1552
        self.assertIs(None, link_or_sha1)
 
1553
        self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
 
1554
 
 
1555
        return packed_stat
 
1556
 
 
1557
    def create_and_test_symlink(self, state, entry):
 
1558
        """Create a symlink at 'a' and verify the state finds it.
 
1559
 
 
1560
        The state should already be versioning *something* at 'a'. This makes
 
1561
        sure that state.update_entry recognizes it as a symlink.
 
1562
 
 
1563
        This should not be called if this platform does not have symlink
 
1564
        support.
 
1565
        """
 
1566
        # caller should care about skipping test on platforms without symlinks
 
1567
        os.symlink('path/to/foo', 'a')
 
1568
 
 
1569
        stat_value = os.lstat('a')
 
1570
        packed_stat = dirstate.pack_stat(stat_value)
 
1571
 
 
1572
        link_or_sha1 = state.update_entry(entry, abspath='a')
 
1573
        self.assertEqual('path/to/foo', link_or_sha1)
 
1574
        self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
 
1575
                         entry[1])
 
1576
        return packed_stat
 
1577
 
 
1578
    def test_update_missing_file(self):
 
1579
        state, entry = self.get_state_with_a()
 
1580
        packed_stat = self.create_and_test_file(state, entry)
 
1581
        # Now if we delete the file, update_entry should recover and
 
1582
        # return None.
 
1583
        os.remove('a')
 
1584
        self.assertIs(None, state.update_entry(entry, abspath='a'))
 
1585
        # And the record shouldn't be changed.
 
1586
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1587
        self.assertEqual([('f', digest, 14, False, packed_stat)],
 
1588
                         entry[1])
 
1589
 
 
1590
    def test_update_missing_dir(self):
 
1591
        state, entry = self.get_state_with_a()
 
1592
        packed_stat = self.create_and_test_dir(state, entry)
 
1593
        # Now if we delete the directory, update_entry should recover and
 
1594
        # return None.
 
1595
        os.rmdir('a')
 
1596
        self.assertIs(None, state.update_entry(entry, abspath='a'))
 
1597
        self.assertEqual([('d', '', 0, False, packed_stat)], entry[1])
 
1598
 
 
1599
    def test_update_missing_symlink(self):
 
1600
        if not osutils.has_symlinks():
 
1601
            # PlatformDeficiency / TestSkipped
 
1602
            raise TestSkipped("No symlink support")
 
1603
        state, entry = self.get_state_with_a()
 
1604
        packed_stat = self.create_and_test_symlink(state, entry)
 
1605
        os.remove('a')
 
1606
        self.assertIs(None, state.update_entry(entry, abspath='a'))
 
1607
        # And the record shouldn't be changed.
 
1608
        self.assertEqual([('l', 'path/to/foo', 11, False, packed_stat)],
 
1609
                         entry[1])
 
1610
 
 
1611
    def test_update_file_to_dir(self):
 
1612
        """If a file changes to a directory we return None for the sha.
 
1613
        We also update the inventory record.
 
1614
        """
 
1615
        state, entry = self.get_state_with_a()
 
1616
        self.create_and_test_file(state, entry)
 
1617
        os.remove('a')
 
1618
        self.create_and_test_dir(state, entry)
 
1619
 
 
1620
    def test_update_file_to_symlink(self):
 
1621
        """File becomes a symlink"""
 
1622
        if not osutils.has_symlinks():
 
1623
            # PlatformDeficiency / TestSkipped
 
1624
            raise TestSkipped("No symlink support")
 
1625
        state, entry = self.get_state_with_a()
 
1626
        self.create_and_test_file(state, entry)
 
1627
        os.remove('a')
 
1628
        self.create_and_test_symlink(state, entry)
 
1629
 
 
1630
    def test_update_dir_to_file(self):
 
1631
        """Directory becoming a file updates the entry."""
 
1632
        state, entry = self.get_state_with_a()
 
1633
        self.create_and_test_dir(state, entry)
 
1634
        os.rmdir('a')
 
1635
        self.create_and_test_file(state, entry)
 
1636
 
 
1637
    def test_update_dir_to_symlink(self):
 
1638
        """Directory becomes a symlink"""
 
1639
        if not osutils.has_symlinks():
 
1640
            # PlatformDeficiency / TestSkipped
 
1641
            raise TestSkipped("No symlink support")
 
1642
        state, entry = self.get_state_with_a()
 
1643
        self.create_and_test_dir(state, entry)
 
1644
        os.rmdir('a')
 
1645
        self.create_and_test_symlink(state, entry)
 
1646
 
 
1647
    def test_update_symlink_to_file(self):
 
1648
        """Symlink becomes a file"""
 
1649
        if not has_symlinks():
 
1650
            raise TestSkipped("No symlink support")
 
1651
        state, entry = self.get_state_with_a()
 
1652
        self.create_and_test_symlink(state, entry)
 
1653
        os.remove('a')
 
1654
        self.create_and_test_file(state, entry)
 
1655
 
 
1656
    def test_update_symlink_to_dir(self):
 
1657
        """Symlink becomes a directory"""
 
1658
        if not has_symlinks():
 
1659
            raise TestSkipped("No symlink support")
 
1660
        state, entry = self.get_state_with_a()
 
1661
        self.create_and_test_symlink(state, entry)
 
1662
        os.remove('a')
 
1663
        self.create_and_test_dir(state, entry)
 
1664
 
 
1665
    def test__is_executable_win32(self):
 
1666
        state, entry = self.get_state_with_a()
 
1667
        self.build_tree(['a'])
 
1668
 
 
1669
        # Make sure we are using the win32 implementation of _is_executable
 
1670
        state._is_executable = state._is_executable_win32
 
1671
 
 
1672
        # The file on disk is not executable, but we are marking it as though
 
1673
        # it is. With _is_executable_win32 we ignore what is on disk.
 
1674
        entry[1][0] = ('f', '', 0, True, dirstate.DirState.NULLSTAT)
 
1675
 
 
1676
        stat_value = os.lstat('a')
 
1677
        packed_stat = dirstate.pack_stat(stat_value)
 
1678
 
 
1679
        state.adjust_time(-10) # Make sure everything is new
 
1680
        # Make sure it wants to kkkkkkkk
 
1681
        state.update_entry(entry, abspath='a', stat_value=stat_value)
 
1682
 
 
1683
        # The row is updated, but the executable bit stays set.
 
1684
        digest = 'b50e5406bb5e153ebbeb20268fcf37c87e1ecfb6'
 
1685
        self.assertEqual([('f', digest, 14, True, packed_stat)], entry[1])
 
1686
 
 
1687
 
 
1688
class TestPackStat(TestCaseWithTransport):
1863
1689
 
1864
1690
    def assertPackStat(self, expected, stat_value):
1865
1691
        """Check the packed and serialized form of a stat value."""
1914
1740
class TestBisect(TestCaseWithDirState):
1915
1741
    """Test the ability to bisect into the disk format."""
1916
1742
 
 
1743
 
1917
1744
    def assertBisect(self, expected_map, map_keys, state, paths):
1918
1745
        """Assert that bisecting for paths returns the right result.
1919
1746
 
1924
1751
                      (dir, name) tuples, and sorted according to how _bisect
1925
1752
                      requires.
1926
1753
        """
1927
 
        result = state._bisect(paths)
 
1754
        dir_names = sorted(osutils.split(p) for p in paths)
 
1755
        result = state._bisect(dir_names)
1928
1756
        # For now, results are just returned in whatever order we read them.
1929
1757
        # We could sort by (dir, name, file_id) or something like that, but in
1930
1758
        # the end it would still be fairly arbitrary, and we don't want the
1931
1759
        # extra overhead if we can avoid it. So sort everything to make sure
1932
1760
        # equality is true
1933
 
        self.assertEqual(len(map_keys), len(paths))
 
1761
        assert len(map_keys) == len(dir_names)
1934
1762
        expected = {}
1935
 
        for path, keys in zip(paths, map_keys):
 
1763
        for dir_name, keys in zip(dir_names, map_keys):
1936
1764
            if keys is None:
1937
1765
                # This should not be present in the output
1938
1766
                continue
1939
 
            expected[path] = sorted(expected_map[k] for k in keys)
 
1767
            expected[dir_name] = sorted(expected_map[k] for k in keys)
1940
1768
 
1941
 
        # The returned values are just arranged randomly based on when they
1942
 
        # were read, for testing, make sure it is properly sorted.
1943
 
        for path in result:
1944
 
            result[path].sort()
 
1769
        for dir_name in result:
 
1770
            result[dir_name].sort()
1945
1771
 
1946
1772
        self.assertEqual(expected, result)
1947
1773
 
1955
1781
        :param paths: A list of directories
1956
1782
        """
1957
1783
        result = state._bisect_dirblocks(paths)
1958
 
        self.assertEqual(len(map_keys), len(paths))
 
1784
        assert len(map_keys) == len(paths)
 
1785
 
1959
1786
        expected = {}
1960
1787
        for path, keys in zip(paths, map_keys):
1961
1788
            if keys is None:
1983
1810
            dir_name_id, trees_info = entry
1984
1811
            expected[dir_name_id] = trees_info
1985
1812
 
1986
 
        result = state._bisect_recursive(paths)
 
1813
        dir_names = sorted(osutils.split(p) for p in paths)
 
1814
        result = state._bisect_recursive(dir_names)
1987
1815
 
1988
1816
        self.assertEqual(expected, result)
1989
1817
 
1998
1826
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
1999
1827
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
2000
1828
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
2001
 
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
2002
1829
        self.assertBisect(expected, [['f']], state, ['f'])
2003
1830
 
2004
1831
    def test_bisect_multi(self):
2007
1834
        # Bisect should be capable of finding multiple entries at the same time
2008
1835
        self.assertBisect(expected, [['a'], ['b'], ['f']],
2009
1836
                          state, ['a', 'b', 'f'])
 
1837
        # ('', 'f') sorts before the others
2010
1838
        self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
2011
 
                          state, ['f', 'b/d', 'b/d/e'])
2012
 
        self.assertBisect(expected, [['b'], ['b-c'], ['b/c']],
2013
 
                          state, ['b', 'b-c', 'b/c'])
 
1839
                          state, ['b/d', 'b/d/e', 'f'])
2014
1840
 
2015
1841
    def test_bisect_one_page(self):
2016
1842
        """Test bisect when there is only 1 page to read"""
2022
1848
        self.assertBisect(expected,[['b/c']], state, ['b/c'])
2023
1849
        self.assertBisect(expected,[['b/d']], state, ['b/d'])
2024
1850
        self.assertBisect(expected,[['b/d/e']], state, ['b/d/e'])
2025
 
        self.assertBisect(expected,[['b-c']], state, ['b-c'])
2026
1851
        self.assertBisect(expected,[['f']], state, ['f'])
2027
1852
        self.assertBisect(expected,[['a'], ['b'], ['f']],
2028
1853
                          state, ['a', 'b', 'f'])
2029
 
        self.assertBisect(expected, [['b/d'], ['b/d/e'], ['f']],
 
1854
        # ('', 'f') sorts before the others
 
1855
        self.assertBisect(expected, [['f'], ['b/d'], ['b/d/e']],
2030
1856
                          state, ['b/d', 'b/d/e', 'f'])
2031
 
        self.assertBisect(expected, [['b'], ['b/c'], ['b-c']],
2032
 
                          state, ['b', 'b/c', 'b-c'])
2033
1857
 
2034
1858
    def test_bisect_duplicate_paths(self):
2035
1859
        """When bisecting for a path, handle multiple entries."""
2043
1867
        self.assertBisect(expected, [['b/d', 'b/d2']], state, ['b/d'])
2044
1868
        self.assertBisect(expected, [['b/d/e', 'b/d/e2']],
2045
1869
                          state, ['b/d/e'])
2046
 
        self.assertBisect(expected, [['b-c', 'b-c2']], state, ['b-c'])
2047
1870
        self.assertBisect(expected, [['f', 'f2']], state, ['f'])
2048
1871
 
2049
1872
    def test_bisect_page_size_too_small(self):
2056
1879
        self.assertBisect(expected, [['b/c']], state, ['b/c'])
2057
1880
        self.assertBisect(expected, [['b/d']], state, ['b/d'])
2058
1881
        self.assertBisect(expected, [['b/d/e']], state, ['b/d/e'])
2059
 
        self.assertBisect(expected, [['b-c']], state, ['b-c'])
2060
1882
        self.assertBisect(expected, [['f']], state, ['f'])
2061
1883
 
2062
1884
    def test_bisect_missing(self):
2065
1887
        self.assertBisect(expected, [None], state, ['foo'])
2066
1888
        self.assertBisect(expected, [None], state, ['b/foo'])
2067
1889
        self.assertBisect(expected, [None], state, ['bar/foo'])
2068
 
        self.assertBisect(expected, [None], state, ['b-c/foo'])
2069
1890
 
2070
1891
        self.assertBisect(expected, [['a'], None, ['b/d']],
2071
1892
                          state, ['a', 'foo', 'b/d'])
2087
1908
    def test_bisect_dirblocks(self):
2088
1909
        tree, state, expected = self.create_duplicated_dirstate()
2089
1910
        self.assertBisectDirBlocks(expected,
2090
 
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2']],
2091
 
            state, [''])
 
1911
            [['', 'a', 'a2', 'b', 'b2', 'f', 'f2']], state, [''])
2092
1912
        self.assertBisectDirBlocks(expected,
2093
1913
            [['b/c', 'b/c2', 'b/d', 'b/d2']], state, ['b'])
2094
1914
        self.assertBisectDirBlocks(expected,
2095
1915
            [['b/d/e', 'b/d/e2']], state, ['b/d'])
2096
1916
        self.assertBisectDirBlocks(expected,
2097
 
            [['', 'a', 'a2', 'b', 'b2', 'b-c', 'b-c2', 'f', 'f2'],
 
1917
            [['', 'a', 'a2', 'b', 'b2', 'f', 'f2'],
2098
1918
             ['b/c', 'b/c2', 'b/d', 'b/d2'],
2099
1919
             ['b/d/e', 'b/d/e2'],
2100
1920
            ], state, ['', 'b', 'b/d'])
2115
1935
        self.assertBisectRecursive(expected, ['a'], state, ['a'])
2116
1936
        self.assertBisectRecursive(expected, ['b/c'], state, ['b/c'])
2117
1937
        self.assertBisectRecursive(expected, ['b/d/e'], state, ['b/d/e'])
2118
 
        self.assertBisectRecursive(expected, ['b-c'], state, ['b-c'])
2119
1938
        self.assertBisectRecursive(expected, ['b/d', 'b/d/e'],
2120
1939
                                   state, ['b/d'])
2121
1940
        self.assertBisectRecursive(expected, ['b', 'b/c', 'b/d', 'b/d/e'],
2122
1941
                                   state, ['b'])
2123
 
        self.assertBisectRecursive(expected, ['', 'a', 'b', 'b-c', 'f', 'b/c',
 
1942
        self.assertBisectRecursive(expected, ['', 'a', 'b', 'f', 'b/c',
2124
1943
                                              'b/d', 'b/d/e'],
2125
1944
                                   state, [''])
2126
1945
 
2150
1969
                                   state, ['b'])
2151
1970
 
2152
1971
 
 
1972
class TestBisectDirblock(TestCase):
 
1973
    """Test that bisect_dirblock() returns the expected values.
 
1974
 
 
1975
    bisect_dirblock is intended to work like bisect.bisect_left() except it
 
1976
    knows it is working on dirblocks and that dirblocks are sorted by ('path',
 
1977
    'to', 'foo') chunks rather than by raw 'path/to/foo'.
 
1978
    """
 
1979
 
 
1980
    def assertBisect(self, dirblocks, split_dirblocks, path, *args, **kwargs):
 
1981
        """Assert that bisect_split works like bisect_left on the split paths.
 
1982
 
 
1983
        :param dirblocks: A list of (path, [info]) pairs.
 
1984
        :param split_dirblocks: A list of ((split, path), [info]) pairs.
 
1985
        :param path: The path we are indexing.
 
1986
 
 
1987
        All other arguments will be passed along.
 
1988
        """
 
1989
        bisect_split_idx = dirstate.bisect_dirblock(dirblocks, path,
 
1990
                                                 *args, **kwargs)
 
1991
        split_dirblock = (path.split('/'), [])
 
1992
        bisect_left_idx = bisect.bisect_left(split_dirblocks, split_dirblock,
 
1993
                                             *args)
 
1994
        self.assertEqual(bisect_left_idx, bisect_split_idx,
 
1995
                         'bisect_split disagreed. %s != %s'
 
1996
                         ' for key %s'
 
1997
                         % (bisect_left_idx, bisect_split_idx, path)
 
1998
                         )
 
1999
 
 
2000
    def paths_to_dirblocks(self, paths):
 
2001
        """Convert a list of paths into dirblock form.
 
2002
 
 
2003
        Also, ensure that the paths are in proper sorted order.
 
2004
        """
 
2005
        dirblocks = [(path, []) for path in paths]
 
2006
        split_dirblocks = [(path.split('/'), []) for path in paths]
 
2007
        self.assertEqual(sorted(split_dirblocks), split_dirblocks)
 
2008
        return dirblocks, split_dirblocks
 
2009
 
 
2010
    def test_simple(self):
 
2011
        """In the simple case it works just like bisect_left"""
 
2012
        paths = ['', 'a', 'b', 'c', 'd']
 
2013
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
 
2014
        for path in paths:
 
2015
            self.assertBisect(dirblocks, split_dirblocks, path)
 
2016
        self.assertBisect(dirblocks, split_dirblocks, '_')
 
2017
        self.assertBisect(dirblocks, split_dirblocks, 'aa')
 
2018
        self.assertBisect(dirblocks, split_dirblocks, 'bb')
 
2019
        self.assertBisect(dirblocks, split_dirblocks, 'cc')
 
2020
        self.assertBisect(dirblocks, split_dirblocks, 'dd')
 
2021
        self.assertBisect(dirblocks, split_dirblocks, 'a/a')
 
2022
        self.assertBisect(dirblocks, split_dirblocks, 'b/b')
 
2023
        self.assertBisect(dirblocks, split_dirblocks, 'c/c')
 
2024
        self.assertBisect(dirblocks, split_dirblocks, 'd/d')
 
2025
 
 
2026
    def test_involved(self):
 
2027
        """This is where bisect_left diverges slightly."""
 
2028
        paths = ['', 'a',
 
2029
                 'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
 
2030
                 'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
 
2031
                 'a-a', 'a-z',
 
2032
                 'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
 
2033
                 'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
 
2034
                 'z-a', 'z-z',
 
2035
                ]
 
2036
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
 
2037
        for path in paths:
 
2038
            self.assertBisect(dirblocks, split_dirblocks, path)
 
2039
 
 
2040
    def test_involved_cached(self):
 
2041
        """This is where bisect_left diverges slightly."""
 
2042
        paths = ['', 'a',
 
2043
                 'a/a', 'a/a/a', 'a/a/z', 'a/a-a', 'a/a-z',
 
2044
                 'a/z', 'a/z/a', 'a/z/z', 'a/z-a', 'a/z-z',
 
2045
                 'a-a', 'a-z',
 
2046
                 'z', 'z/a/a', 'z/a/z', 'z/a-a', 'z/a-z',
 
2047
                 'z/z', 'z/z/a', 'z/z/z', 'z/z-a', 'z/z-z',
 
2048
                 'z-a', 'z-z',
 
2049
                ]
 
2050
        cache = {}
 
2051
        dirblocks, split_dirblocks = self.paths_to_dirblocks(paths)
 
2052
        for path in paths:
 
2053
            self.assertBisect(dirblocks, split_dirblocks, path, cache=cache)
 
2054
 
 
2055
 
2153
2056
class TestDirstateValidation(TestCaseWithDirState):
2154
2057
 
2155
2058
    def test_validate_correct_dirstate(self):
2205
2108
            state._validate)
2206
2109
        self.assertContainsRe(str(e),
2207
2110
            'file a-id is absent in row')
2208
 
 
2209
 
 
2210
 
class TestDirstateTreeReference(TestCaseWithDirState):
2211
 
 
2212
 
    def test_reference_revision_is_none(self):
2213
 
        tree = self.make_branch_and_tree('tree', format='dirstate-with-subtree')
2214
 
        subtree = self.make_branch_and_tree('tree/subtree',
2215
 
                            format='dirstate-with-subtree')
2216
 
        subtree.set_root_id('subtree')
2217
 
        tree.add_reference(subtree)
2218
 
        tree.add('subtree')
2219
 
        state = dirstate.DirState.from_tree(tree, 'dirstate')
2220
 
        key = ('', 'subtree', 'subtree')
2221
 
        expected = ('', [(key,
2222
 
            [('t', '', 0, False, 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')])])
2223
 
 
2224
 
        try:
2225
 
            self.assertEqual(expected, state._find_block(key))
2226
 
        finally:
2227
 
            state.unlock()
2228
 
 
2229
 
 
2230
 
class TestDiscardMergeParents(TestCaseWithDirState):
2231
 
 
2232
 
    def test_discard_no_parents(self):
2233
 
        # This should be a no-op
2234
 
        state = self.create_empty_dirstate()
2235
 
        self.addCleanup(state.unlock)
2236
 
        state._discard_merge_parents()
2237
 
        state._validate()
2238
 
 
2239
 
    def test_discard_one_parent(self):
2240
 
        # No-op
2241
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2242
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2243
 
            ('d', '', 0, False, packed_stat),
2244
 
            ('d', '', 0, False, packed_stat),
2245
 
            ]
2246
 
        dirblocks = []
2247
 
        dirblocks.append(('', [root_entry_direntry]))
2248
 
        dirblocks.append(('', []))
2249
 
 
2250
 
        state = self.create_empty_dirstate()
2251
 
        self.addCleanup(state.unlock)
2252
 
        state._set_data(['parent-id'], dirblocks[:])
2253
 
        state._validate()
2254
 
 
2255
 
        state._discard_merge_parents()
2256
 
        state._validate()
2257
 
        self.assertEqual(dirblocks, state._dirblocks)
2258
 
 
2259
 
    def test_discard_simple(self):
2260
 
        # No-op
2261
 
        packed_stat = 'AAAAREUHaIpFB2iKAAADAQAtkqUAAIGk'
2262
 
        root_entry_direntry = ('', '', 'a-root-value'), [
2263
 
            ('d', '', 0, False, packed_stat),
2264
 
            ('d', '', 0, False, packed_stat),
2265
 
            ('d', '', 0, False, packed_stat),
2266
 
            ]
2267
 
        expected_root_entry_direntry = ('', '', 'a-root-value'), [
2268
 
            ('d', '', 0, False, packed_stat),
2269
 
            ('d', '', 0, False, packed_stat),
2270
 
            ]
2271
 
        dirblocks = []
2272
 
        dirblocks.append(('', [root_entry_direntry]))
2273
 
        dirblocks.append(('', []))
2274
 
 
2275
 
        state = self.create_empty_dirstate()
2276
 
        self.addCleanup(state.unlock)
2277
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2278
 
        state._validate()
2279
 
 
2280
 
        # This should strip of the extra column
2281
 
        state._discard_merge_parents()
2282
 
        state._validate()
2283
 
        expected_dirblocks = [('', [expected_root_entry_direntry]), ('', [])]
2284
 
        self.assertEqual(expected_dirblocks, state._dirblocks)
2285
 
 
2286
 
    def test_discard_absent(self):
2287
 
        """If entries are only in a merge, discard should remove the entries"""
2288
 
        null_stat = dirstate.DirState.NULLSTAT
2289
 
        present_dir = ('d', '', 0, False, null_stat)
2290
 
        present_file = ('f', '', 0, False, null_stat)
2291
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2292
 
        root_key = ('', '', 'a-root-value')
2293
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2294
 
        file_in_merged_key = ('', 'file-in-merged', 'b-file-id')
2295
 
        dirblocks = [('', [(root_key, [present_dir, present_dir, present_dir])]),
2296
 
                     ('', [(file_in_merged_key,
2297
 
                            [absent, absent, present_file]),
2298
 
                           (file_in_root_key,
2299
 
                            [present_file, present_file, present_file]),
2300
 
                          ]),
2301
 
                    ]
2302
 
 
2303
 
        state = self.create_empty_dirstate()
2304
 
        self.addCleanup(state.unlock)
2305
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2306
 
        state._validate()
2307
 
 
2308
 
        exp_dirblocks = [('', [(root_key, [present_dir, present_dir])]),
2309
 
                         ('', [(file_in_root_key,
2310
 
                                [present_file, present_file]),
2311
 
                              ]),
2312
 
                        ]
2313
 
        state._discard_merge_parents()
2314
 
        state._validate()
2315
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2316
 
 
2317
 
    def test_discard_renamed(self):
2318
 
        null_stat = dirstate.DirState.NULLSTAT
2319
 
        present_dir = ('d', '', 0, False, null_stat)
2320
 
        present_file = ('f', '', 0, False, null_stat)
2321
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2322
 
        root_key = ('', '', 'a-root-value')
2323
 
        file_in_root_key = ('', 'file-in-root', 'a-file-id')
2324
 
        # Renamed relative to parent
2325
 
        file_rename_s_key = ('', 'file-s', 'b-file-id')
2326
 
        file_rename_t_key = ('', 'file-t', 'b-file-id')
2327
 
        # And one that is renamed between the parents, but absent in this
2328
 
        key_in_1 = ('', 'file-in-1', 'c-file-id')
2329
 
        key_in_2 = ('', 'file-in-2', 'c-file-id')
2330
 
 
2331
 
        dirblocks = [
2332
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2333
 
            ('', [(key_in_1,
2334
 
                   [absent, present_file, ('r', 'file-in-2', 'c-file-id')]),
2335
 
                  (key_in_2,
2336
 
                   [absent, ('r', 'file-in-1', 'c-file-id'), present_file]),
2337
 
                  (file_in_root_key,
2338
 
                   [present_file, present_file, present_file]),
2339
 
                  (file_rename_s_key,
2340
 
                   [('r', 'file-t', 'b-file-id'), absent, present_file]),
2341
 
                  (file_rename_t_key,
2342
 
                   [present_file, absent, ('r', 'file-s', 'b-file-id')]),
2343
 
                 ]),
2344
 
        ]
2345
 
        exp_dirblocks = [
2346
 
            ('', [(root_key, [present_dir, present_dir])]),
2347
 
            ('', [(key_in_1, [absent, present_file]),
2348
 
                  (file_in_root_key, [present_file, present_file]),
2349
 
                  (file_rename_t_key, [present_file, absent]),
2350
 
                 ]),
2351
 
        ]
2352
 
        state = self.create_empty_dirstate()
2353
 
        self.addCleanup(state.unlock)
2354
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2355
 
        state._validate()
2356
 
 
2357
 
        state._discard_merge_parents()
2358
 
        state._validate()
2359
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2360
 
 
2361
 
    def test_discard_all_subdir(self):
2362
 
        null_stat = dirstate.DirState.NULLSTAT
2363
 
        present_dir = ('d', '', 0, False, null_stat)
2364
 
        present_file = ('f', '', 0, False, null_stat)
2365
 
        absent = dirstate.DirState.NULL_PARENT_DETAILS
2366
 
        root_key = ('', '', 'a-root-value')
2367
 
        subdir_key = ('', 'sub', 'dir-id')
2368
 
        child1_key = ('sub', 'child1', 'child1-id')
2369
 
        child2_key = ('sub', 'child2', 'child2-id')
2370
 
        child3_key = ('sub', 'child3', 'child3-id')
2371
 
 
2372
 
        dirblocks = [
2373
 
            ('', [(root_key, [present_dir, present_dir, present_dir])]),
2374
 
            ('', [(subdir_key, [present_dir, present_dir, present_dir])]),
2375
 
            ('sub', [(child1_key, [absent, absent, present_file]),
2376
 
                     (child2_key, [absent, absent, present_file]),
2377
 
                     (child3_key, [absent, absent, present_file]),
2378
 
                    ]),
2379
 
        ]
2380
 
        exp_dirblocks = [
2381
 
            ('', [(root_key, [present_dir, present_dir])]),
2382
 
            ('', [(subdir_key, [present_dir, present_dir])]),
2383
 
            ('sub', []),
2384
 
        ]
2385
 
        state = self.create_empty_dirstate()
2386
 
        self.addCleanup(state.unlock)
2387
 
        state._set_data(['parent-id', 'merged-id'], dirblocks[:])
2388
 
        state._validate()
2389
 
 
2390
 
        state._discard_merge_parents()
2391
 
        state._validate()
2392
 
        self.assertEqual(exp_dirblocks, state._dirblocks)
2393
 
 
2394
 
 
2395
 
class Test_InvEntryToDetails(tests.TestCase):
2396
 
 
2397
 
    def assertDetails(self, expected, inv_entry):
2398
 
        details = dirstate.DirState._inv_entry_to_details(inv_entry)
2399
 
        self.assertEqual(expected, details)
2400
 
        # details should always allow join() and always be a plain str when
2401
 
        # finished
2402
 
        (minikind, fingerprint, size, executable, tree_data) = details
2403
 
        self.assertIsInstance(minikind, str)
2404
 
        self.assertIsInstance(fingerprint, str)
2405
 
        self.assertIsInstance(tree_data, str)
2406
 
 
2407
 
    def test_unicode_symlink(self):
2408
 
        inv_entry = inventory.InventoryLink('link-file-id',
2409
 
                                            u'nam\N{Euro Sign}e',
2410
 
                                            'link-parent-id')
2411
 
        inv_entry.revision = 'link-revision-id'
2412
 
        target = u'link-targ\N{Euro Sign}t'
2413
 
        inv_entry.symlink_target = target
2414
 
        self.assertDetails(('l', target.encode('UTF-8'), 0, False,
2415
 
                            'link-revision-id'), inv_entry)
2416
 
 
2417
 
 
2418
 
class TestSHA1Provider(tests.TestCaseInTempDir):
2419
 
 
2420
 
    def test_sha1provider_is_an_interface(self):
2421
 
        p = dirstate.SHA1Provider()
2422
 
        self.assertRaises(NotImplementedError, p.sha1, "foo")
2423
 
        self.assertRaises(NotImplementedError, p.stat_and_sha1, "foo")
2424
 
 
2425
 
    def test_defaultsha1provider_sha1(self):
2426
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2427
 
        self.build_tree_contents([('foo', text)])
2428
 
        expected_sha = osutils.sha_string(text)
2429
 
        p = dirstate.DefaultSHA1Provider()
2430
 
        self.assertEqual(expected_sha, p.sha1('foo'))
2431
 
 
2432
 
    def test_defaultsha1provider_stat_and_sha1(self):
2433
 
        text = 'test\r\nwith\nall\rpossible line endings\r\n'
2434
 
        self.build_tree_contents([('foo', text)])
2435
 
        expected_sha = osutils.sha_string(text)
2436
 
        p = dirstate.DefaultSHA1Provider()
2437
 
        statvalue, sha1 = p.stat_and_sha1('foo')
2438
 
        self.assertTrue(len(statvalue) >= 10)
2439
 
        self.assertEqual(len(text), statvalue.st_size)
2440
 
        self.assertEqual(expected_sha, sha1)
2441
 
 
2442
 
 
2443
 
class _Repo(object):
2444
 
    """A minimal api to get InventoryRevisionTree to work."""
2445
 
 
2446
 
    def __init__(self):
2447
 
        default_format = bzrdir.format_registry.make_bzrdir('default')
2448
 
        self._format = default_format.repository_format
2449
 
 
2450
 
    def lock_read(self):
2451
 
        pass
2452
 
 
2453
 
    def unlock(self):
2454
 
        pass
2455
 
 
2456
 
 
2457
 
class TestUpdateBasisByDelta(tests.TestCase):
2458
 
 
2459
 
    def path_to_ie(self, path, file_id, rev_id, dir_ids):
2460
 
        if path.endswith('/'):
2461
 
            is_dir = True
2462
 
            path = path[:-1]
2463
 
        else:
2464
 
            is_dir = False
2465
 
        dirname, basename = osutils.split(path)
2466
 
        try:
2467
 
            dir_id = dir_ids[dirname]
2468
 
        except KeyError:
2469
 
            dir_id = osutils.basename(dirname) + '-id'
2470
 
        if is_dir:
2471
 
            ie = inventory.InventoryDirectory(file_id, basename, dir_id)
2472
 
            dir_ids[path] = file_id
2473
 
        else:
2474
 
            ie = inventory.InventoryFile(file_id, basename, dir_id)
2475
 
            ie.text_size = 0
2476
 
            ie.text_sha1 = ''
2477
 
        ie.revision = rev_id
2478
 
        return ie
2479
 
 
2480
 
    def create_tree_from_shape(self, rev_id, shape):
2481
 
        dir_ids = {'': 'root-id'}
2482
 
        inv = inventory.Inventory('root-id', rev_id)
2483
 
        for path, file_id in shape:
2484
 
            if path == '':
2485
 
                # Replace the root entry
2486
 
                del inv._byid[inv.root.file_id]
2487
 
                inv.root.file_id = file_id
2488
 
                inv._byid[file_id] = inv.root
2489
 
                dir_ids[''] = file_id
2490
 
                continue
2491
 
            inv.add(self.path_to_ie(path, file_id, rev_id, dir_ids))
2492
 
        return revisiontree.InventoryRevisionTree(_Repo(), inv, rev_id)
2493
 
 
2494
 
    def create_empty_dirstate(self):
2495
 
        fd, path = tempfile.mkstemp(prefix='bzr-dirstate')
2496
 
        self.addCleanup(os.remove, path)
2497
 
        os.close(fd)
2498
 
        state = dirstate.DirState.initialize(path)
2499
 
        self.addCleanup(state.unlock)
2500
 
        return state
2501
 
 
2502
 
    def create_inv_delta(self, delta, rev_id):
2503
 
        """Translate a 'delta shape' into an actual InventoryDelta"""
2504
 
        dir_ids = {'': 'root-id'}
2505
 
        inv_delta = []
2506
 
        for old_path, new_path, file_id in delta:
2507
 
            if old_path is not None and old_path.endswith('/'):
2508
 
                # Don't have to actually do anything for this, because only
2509
 
                # new_path creates InventoryEntries
2510
 
                old_path = old_path[:-1]
2511
 
            if new_path is None: # Delete
2512
 
                inv_delta.append((old_path, None, file_id, None))
2513
 
                continue
2514
 
            ie = self.path_to_ie(new_path, file_id, rev_id, dir_ids)
2515
 
            inv_delta.append((old_path, new_path, file_id, ie))
2516
 
        return inv_delta
2517
 
 
2518
 
    def assertUpdate(self, active, basis, target):
2519
 
        """Assert that update_basis_by_delta works how we want.
2520
 
 
2521
 
        Set up a DirState object with active_shape for tree 0, basis_shape for
2522
 
        tree 1. Then apply the delta from basis_shape to target_shape,
2523
 
        and assert that the DirState is still valid, and that its stored
2524
 
        content matches the target_shape.
2525
 
        """
2526
 
        active_tree = self.create_tree_from_shape('active', active)
2527
 
        basis_tree = self.create_tree_from_shape('basis', basis)
2528
 
        target_tree = self.create_tree_from_shape('target', target)
2529
 
        state = self.create_empty_dirstate()
2530
 
        state.set_state_from_scratch(active_tree.inventory,
2531
 
            [('basis', basis_tree)], [])
2532
 
        delta = target_tree.inventory._make_delta(basis_tree.inventory)
2533
 
        state.update_basis_by_delta(delta, 'target')
2534
 
        state._validate()
2535
 
        dirstate_tree = workingtree_4.DirStateRevisionTree(state,
2536
 
            'target', _Repo())
2537
 
        # The target now that delta has been applied should match the
2538
 
        # RevisionTree
2539
 
        self.assertEqual([], list(dirstate_tree.iter_changes(target_tree)))
2540
 
        # And the dirblock state should be identical to the state if we created
2541
 
        # it from scratch.
2542
 
        state2 = self.create_empty_dirstate()
2543
 
        state2.set_state_from_scratch(active_tree.inventory,
2544
 
            [('target', target_tree)], [])
2545
 
        self.assertEqual(state2._dirblocks, state._dirblocks)
2546
 
        return state
2547
 
 
2548
 
    def assertBadDelta(self, active, basis, delta):
2549
 
        """Test that we raise InconsistentDelta when appropriate.
2550
 
 
2551
 
        :param active: The active tree shape
2552
 
        :param basis: The basis tree shape
2553
 
        :param delta: A description of the delta to apply. Similar to the form
2554
 
            for regular inventory deltas, but omitting the InventoryEntry.
2555
 
            So adding a file is: (None, 'path', 'file-id')
2556
 
            Adding a directory is: (None, 'path/', 'dir-id')
2557
 
            Renaming a dir is: ('old/', 'new/', 'dir-id')
2558
 
            etc.
2559
 
        """
2560
 
        active_tree = self.create_tree_from_shape('active', active)
2561
 
        basis_tree = self.create_tree_from_shape('basis', basis)
2562
 
        inv_delta = self.create_inv_delta(delta, 'target')
2563
 
        state = self.create_empty_dirstate()
2564
 
        state.set_state_from_scratch(active_tree.inventory,
2565
 
            [('basis', basis_tree)], [])
2566
 
        self.assertRaises(errors.InconsistentDelta,
2567
 
            state.update_basis_by_delta, inv_delta, 'target')
2568
 
        ## try:
2569
 
        ##     state.update_basis_by_delta(inv_delta, 'target')
2570
 
        ## except errors.InconsistentDelta, e:
2571
 
        ##     import pdb; pdb.set_trace()
2572
 
        ## else:
2573
 
        ##     import pdb; pdb.set_trace()
2574
 
        self.assertTrue(state._changes_aborted)
2575
 
 
2576
 
    def test_remove_file_matching_active_state(self):
2577
 
        state = self.assertUpdate(
2578
 
            active=[],
2579
 
            basis =[('file', 'file-id')],
2580
 
            target=[],
2581
 
            )
2582
 
 
2583
 
    def test_remove_file_present_in_active_state(self):
2584
 
        state = self.assertUpdate(
2585
 
            active=[('file', 'file-id')],
2586
 
            basis =[('file', 'file-id')],
2587
 
            target=[],
2588
 
            )
2589
 
 
2590
 
    def test_remove_file_present_elsewhere_in_active_state(self):
2591
 
        state = self.assertUpdate(
2592
 
            active=[('other-file', 'file-id')],
2593
 
            basis =[('file', 'file-id')],
2594
 
            target=[],
2595
 
            )
2596
 
 
2597
 
    def test_remove_file_active_state_has_diff_file(self):
2598
 
        state = self.assertUpdate(
2599
 
            active=[('file', 'file-id-2')],
2600
 
            basis =[('file', 'file-id')],
2601
 
            target=[],
2602
 
            )
2603
 
 
2604
 
    def test_remove_file_active_state_has_diff_file_and_file_elsewhere(self):
2605
 
        state = self.assertUpdate(
2606
 
            active=[('file', 'file-id-2'),
2607
 
                    ('other-file', 'file-id')],
2608
 
            basis =[('file', 'file-id')],
2609
 
            target=[],
2610
 
            )
2611
 
 
2612
 
    def test_add_file_matching_active_state(self):
2613
 
        state = self.assertUpdate(
2614
 
            active=[('file', 'file-id')],
2615
 
            basis =[],
2616
 
            target=[('file', 'file-id')],
2617
 
            )
2618
 
 
2619
 
    def test_add_file_missing_in_active_state(self):
2620
 
        state = self.assertUpdate(
2621
 
            active=[],
2622
 
            basis =[],
2623
 
            target=[('file', 'file-id')],
2624
 
            )
2625
 
 
2626
 
    def test_add_file_elsewhere_in_active_state(self):
2627
 
        state = self.assertUpdate(
2628
 
            active=[('other-file', 'file-id')],
2629
 
            basis =[],
2630
 
            target=[('file', 'file-id')],
2631
 
            )
2632
 
 
2633
 
    def test_add_file_active_state_has_diff_file_and_file_elsewhere(self):
2634
 
        state = self.assertUpdate(
2635
 
            active=[('other-file', 'file-id'),
2636
 
                    ('file', 'file-id-2')],
2637
 
            basis =[],
2638
 
            target=[('file', 'file-id')],
2639
 
            )
2640
 
 
2641
 
    def test_rename_file_matching_active_state(self):
2642
 
        state = self.assertUpdate(
2643
 
            active=[('other-file', 'file-id')],
2644
 
            basis =[('file', 'file-id')],
2645
 
            target=[('other-file', 'file-id')],
2646
 
            )
2647
 
 
2648
 
    def test_rename_file_missing_in_active_state(self):
2649
 
        state = self.assertUpdate(
2650
 
            active=[],
2651
 
            basis =[('file', 'file-id')],
2652
 
            target=[('other-file', 'file-id')],
2653
 
            )
2654
 
 
2655
 
    def test_rename_file_present_elsewhere_in_active_state(self):
2656
 
        state = self.assertUpdate(
2657
 
            active=[('third', 'file-id')],
2658
 
            basis =[('file', 'file-id')],
2659
 
            target=[('other-file', 'file-id')],
2660
 
            )
2661
 
 
2662
 
    def test_rename_file_active_state_has_diff_source_file(self):
2663
 
        state = self.assertUpdate(
2664
 
            active=[('file', 'file-id-2')],
2665
 
            basis =[('file', 'file-id')],
2666
 
            target=[('other-file', 'file-id')],
2667
 
            )
2668
 
 
2669
 
    def test_rename_file_active_state_has_diff_target_file(self):
2670
 
        state = self.assertUpdate(
2671
 
            active=[('other-file', 'file-id-2')],
2672
 
            basis =[('file', 'file-id')],
2673
 
            target=[('other-file', 'file-id')],
2674
 
            )
2675
 
 
2676
 
    def test_rename_file_active_has_swapped_files(self):
2677
 
        state = self.assertUpdate(
2678
 
            active=[('file', 'file-id'),
2679
 
                    ('other-file', 'file-id-2')],
2680
 
            basis= [('file', 'file-id'),
2681
 
                    ('other-file', 'file-id-2')],
2682
 
            target=[('file', 'file-id-2'),
2683
 
                    ('other-file', 'file-id')])
2684
 
 
2685
 
    def test_rename_file_basis_has_swapped_files(self):
2686
 
        state = self.assertUpdate(
2687
 
            active=[('file', 'file-id'),
2688
 
                    ('other-file', 'file-id-2')],
2689
 
            basis= [('file', 'file-id-2'),
2690
 
                    ('other-file', 'file-id')],
2691
 
            target=[('file', 'file-id'),
2692
 
                    ('other-file', 'file-id-2')])
2693
 
 
2694
 
    def test_rename_directory_with_contents(self):
2695
 
        state = self.assertUpdate( # active matches basis
2696
 
            active=[('dir1/', 'dir-id'),
2697
 
                    ('dir1/file', 'file-id')],
2698
 
            basis= [('dir1/', 'dir-id'),
2699
 
                    ('dir1/file', 'file-id')],
2700
 
            target=[('dir2/', 'dir-id'),
2701
 
                    ('dir2/file', 'file-id')])
2702
 
        state = self.assertUpdate( # active matches target
2703
 
            active=[('dir2/', 'dir-id'),
2704
 
                    ('dir2/file', 'file-id')],
2705
 
            basis= [('dir1/', 'dir-id'),
2706
 
                    ('dir1/file', 'file-id')],
2707
 
            target=[('dir2/', 'dir-id'),
2708
 
                    ('dir2/file', 'file-id')])
2709
 
        state = self.assertUpdate( # active empty
2710
 
            active=[],
2711
 
            basis= [('dir1/', 'dir-id'),
2712
 
                    ('dir1/file', 'file-id')],
2713
 
            target=[('dir2/', 'dir-id'),
2714
 
                    ('dir2/file', 'file-id')])
2715
 
        state = self.assertUpdate( # active present at other location
2716
 
            active=[('dir3/', 'dir-id'),
2717
 
                    ('dir3/file', 'file-id')],
2718
 
            basis= [('dir1/', 'dir-id'),
2719
 
                    ('dir1/file', 'file-id')],
2720
 
            target=[('dir2/', 'dir-id'),
2721
 
                    ('dir2/file', 'file-id')])
2722
 
        state = self.assertUpdate( # active has different ids
2723
 
            active=[('dir1/', 'dir1-id'),
2724
 
                    ('dir1/file', 'file1-id'),
2725
 
                    ('dir2/', 'dir2-id'),
2726
 
                    ('dir2/file', 'file2-id')],
2727
 
            basis= [('dir1/', 'dir-id'),
2728
 
                    ('dir1/file', 'file-id')],
2729
 
            target=[('dir2/', 'dir-id'),
2730
 
                    ('dir2/file', 'file-id')])
2731
 
 
2732
 
    def test_invalid_file_not_present(self):
2733
 
        state = self.assertBadDelta(
2734
 
            active=[('file', 'file-id')],
2735
 
            basis= [('file', 'file-id')],
2736
 
            delta=[('other-file', 'file', 'file-id')])
2737
 
 
2738
 
    def test_invalid_new_id_same_path(self):
2739
 
        # The bad entry comes after
2740
 
        state = self.assertBadDelta(
2741
 
            active=[('file', 'file-id')],
2742
 
            basis= [('file', 'file-id')],
2743
 
            delta=[(None, 'file', 'file-id-2')])
2744
 
        # The bad entry comes first
2745
 
        state = self.assertBadDelta(
2746
 
            active=[('file', 'file-id-2')],
2747
 
            basis=[('file', 'file-id-2')],
2748
 
            delta=[(None, 'file', 'file-id')])
2749
 
 
2750
 
    def test_invalid_existing_id(self):
2751
 
        state = self.assertBadDelta(
2752
 
            active=[('file', 'file-id')],
2753
 
            basis= [('file', 'file-id')],
2754
 
            delta=[(None, 'file', 'file-id')])
2755
 
 
2756
 
    def test_invalid_parent_missing(self):
2757
 
        state = self.assertBadDelta(
2758
 
            active=[],
2759
 
            basis= [],
2760
 
            delta=[(None, 'path/path2', 'file-id')])
2761
 
        # Note: we force the active tree to have the directory, by knowing how
2762
 
        #       path_to_ie handles entries with missing parents
2763
 
        state = self.assertBadDelta(
2764
 
            active=[('path/', 'path-id')],
2765
 
            basis= [],
2766
 
            delta=[(None, 'path/path2', 'file-id')])
2767
 
        state = self.assertBadDelta(
2768
 
            active=[('path/', 'path-id'),
2769
 
                    ('path/path2', 'file-id')],
2770
 
            basis= [],
2771
 
            delta=[(None, 'path/path2', 'file-id')])
2772
 
 
2773
 
    def test_renamed_dir_same_path(self):
2774
 
        # We replace the parent directory, with another parent dir. But the C
2775
 
        # file doesn't look like it has been moved.
2776
 
        state = self.assertUpdate(# Same as basis
2777
 
            active=[('dir/', 'A-id'),
2778
 
                    ('dir/B', 'B-id')],
2779
 
            basis= [('dir/', 'A-id'),
2780
 
                    ('dir/B', 'B-id')],
2781
 
            target=[('dir/', 'C-id'),
2782
 
                    ('dir/B', 'B-id')])
2783
 
        state = self.assertUpdate(# Same as target
2784
 
            active=[('dir/', 'C-id'),
2785
 
                    ('dir/B', 'B-id')],
2786
 
            basis= [('dir/', 'A-id'),
2787
 
                    ('dir/B', 'B-id')],
2788
 
            target=[('dir/', 'C-id'),
2789
 
                    ('dir/B', 'B-id')])
2790
 
        state = self.assertUpdate(# empty active
2791
 
            active=[],
2792
 
            basis= [('dir/', 'A-id'),
2793
 
                    ('dir/B', 'B-id')],
2794
 
            target=[('dir/', 'C-id'),
2795
 
                    ('dir/B', 'B-id')])
2796
 
        state = self.assertUpdate(# different active
2797
 
            active=[('dir/', 'D-id'),
2798
 
                    ('dir/B', 'B-id')],
2799
 
            basis= [('dir/', 'A-id'),
2800
 
                    ('dir/B', 'B-id')],
2801
 
            target=[('dir/', 'C-id'),
2802
 
                    ('dir/B', 'B-id')])
2803
 
 
2804
 
    def test_parent_child_swap(self):
2805
 
        state = self.assertUpdate(# Same as basis
2806
 
            active=[('A/', 'A-id'),
2807
 
                    ('A/B/', 'B-id'),
2808
 
                    ('A/B/C', 'C-id')],
2809
 
            basis= [('A/', 'A-id'),
2810
 
                    ('A/B/', 'B-id'),
2811
 
                    ('A/B/C', 'C-id')],
2812
 
            target=[('A/', 'B-id'),
2813
 
                    ('A/B/', 'A-id'),
2814
 
                    ('A/B/C', 'C-id')])
2815
 
        state = self.assertUpdate(# Same as target
2816
 
            active=[('A/', 'B-id'),
2817
 
                    ('A/B/', 'A-id'),
2818
 
                    ('A/B/C', 'C-id')],
2819
 
            basis= [('A/', 'A-id'),
2820
 
                    ('A/B/', 'B-id'),
2821
 
                    ('A/B/C', 'C-id')],
2822
 
            target=[('A/', 'B-id'),
2823
 
                    ('A/B/', 'A-id'),
2824
 
                    ('A/B/C', 'C-id')])
2825
 
        state = self.assertUpdate(# empty active
2826
 
            active=[],
2827
 
            basis= [('A/', 'A-id'),
2828
 
                    ('A/B/', 'B-id'),
2829
 
                    ('A/B/C', 'C-id')],
2830
 
            target=[('A/', 'B-id'),
2831
 
                    ('A/B/', 'A-id'),
2832
 
                    ('A/B/C', 'C-id')])
2833
 
        state = self.assertUpdate(# different active
2834
 
            active=[('D/', 'A-id'),
2835
 
                    ('D/E/', 'B-id'),
2836
 
                    ('F', 'C-id')],
2837
 
            basis= [('A/', 'A-id'),
2838
 
                    ('A/B/', 'B-id'),
2839
 
                    ('A/B/C', 'C-id')],
2840
 
            target=[('A/', 'B-id'),
2841
 
                    ('A/B/', 'A-id'),
2842
 
                    ('A/B/C', 'C-id')])
2843
 
 
2844
 
    def test_change_root_id(self):
2845
 
        state = self.assertUpdate( # same as basis
2846
 
            active=[('', 'root-id'),
2847
 
                    ('file', 'file-id')],
2848
 
            basis= [('', 'root-id'),
2849
 
                    ('file', 'file-id')],
2850
 
            target=[('', 'target-root-id'),
2851
 
                    ('file', 'file-id')])
2852
 
        state = self.assertUpdate( # same as target
2853
 
            active=[('', 'target-root-id'),
2854
 
                    ('file', 'file-id')],
2855
 
            basis= [('', 'root-id'),
2856
 
                    ('file', 'file-id')],
2857
 
            target=[('', 'target-root-id'),
2858
 
                    ('file', 'root-id')])
2859
 
        state = self.assertUpdate( # all different
2860
 
            active=[('', 'active-root-id'),
2861
 
                    ('file', 'file-id')],
2862
 
            basis= [('', 'root-id'),
2863
 
                    ('file', 'file-id')],
2864
 
            target=[('', 'target-root-id'),
2865
 
                    ('file', 'root-id')])
2866
 
 
2867
 
    def test_change_file_absent_in_active(self):
2868
 
        state = self.assertUpdate(
2869
 
            active=[],
2870
 
            basis= [('file', 'file-id')],
2871
 
            target=[('file', 'file-id')])
2872
 
 
2873
 
    def test_invalid_changed_file(self):
2874
 
        state = self.assertBadDelta( # Not present in basis
2875
 
            active=[('file', 'file-id')],
2876
 
            basis= [],
2877
 
            delta=[('file', 'file', 'file-id')])
2878
 
        state = self.assertBadDelta( # present at another location in basis
2879
 
            active=[('file', 'file-id')],
2880
 
            basis= [('other-file', 'file-id')],
2881
 
            delta=[('file', 'file', 'file-id')])