~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_repository/test_commit_builder.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
from bzrlib import (
22
22
    config,
23
23
    errors,
24
 
    graph,
25
24
    inventory,
26
25
    osutils,
27
26
    repository,
61
60
        tree.lock_write()
62
61
        try:
63
62
            builder = tree.branch.get_commit_builder([])
 
63
            if not builder.supports_record_entry_contents:
 
64
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
65
                    "record_entry_contents")
64
66
            repo = tree.branch.repository
65
67
            self.record_root(builder, tree)
66
68
            builder.finish_inventory()
85
87
        finally:
86
88
            tree.unlock()
87
89
 
88
 
    def test_abort(self):
 
90
    def test_abort_record_entry_contents(self):
89
91
        tree = self.make_branch_and_tree(".")
90
92
        tree.lock_write()
91
93
        try:
92
94
            builder = tree.branch.get_commit_builder([])
 
95
            if not builder.supports_record_entry_contents:
 
96
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
97
                    "record_entry_contents")
93
98
            self.record_root(builder, tree)
94
99
            builder.finish_inventory()
95
100
            builder.abort()
112
117
        finally:
113
118
            tree.unlock()
114
119
 
 
120
    def test_commit_lossy(self):
 
121
        tree = self.make_branch_and_tree(".")
 
122
        tree.lock_write()
 
123
        try:
 
124
            builder = tree.branch.get_commit_builder([], lossy=True)
 
125
            list(builder.record_iter_changes(tree, tree.last_revision(),
 
126
                tree.iter_changes(tree.basis_tree())))
 
127
            builder.finish_inventory()
 
128
            rev_id = builder.commit('foo bar blah')
 
129
        finally:
 
130
            tree.unlock()
 
131
        rev = tree.branch.repository.get_revision(rev_id)
 
132
        self.assertEqual('foo bar blah', rev.message)
 
133
 
115
134
    def test_commit_message(self):
116
135
        tree = self.make_branch_and_tree(".")
117
136
        tree.lock_write()
118
137
        try:
119
138
            builder = tree.branch.get_commit_builder([])
120
 
            self.record_root(builder, tree)
 
139
            list(builder.record_iter_changes(tree, tree.last_revision(),
 
140
                tree.iter_changes(tree.basis_tree())))
121
141
            builder.finish_inventory()
122
142
            rev_id = builder.commit('foo bar blah')
123
143
        finally:
125
145
        rev = tree.branch.repository.get_revision(rev_id)
126
146
        self.assertEqual('foo bar blah', rev.message)
127
147
 
128
 
    def test_commit_with_revision_id(self):
 
148
    def test_commit_with_revision_id_record_entry_contents(self):
129
149
        tree = self.make_branch_and_tree(".")
130
150
        tree.lock_write()
131
151
        try:
143
163
            except errors.CannotSetRevisionId:
144
164
                # This format doesn't support supplied revision ids
145
165
                return
 
166
            if not builder.supports_record_entry_contents:
 
167
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
168
                    "record_entry_contents")
146
169
            self.assertFalse(builder.random_revid)
147
170
            self.record_root(builder, tree)
148
171
            builder.finish_inventory()
217
240
        try:
218
241
            self.build_tree(['foo'])
219
242
            tree.add('foo', 'foo-id')
 
243
            builder = tree.branch.get_commit_builder([])
 
244
            if not builder.supports_record_entry_contents:
 
245
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
246
                    "record_entry_contents")
220
247
            entry = tree.inventory['foo-id']
221
 
            builder = tree.branch.get_commit_builder([])
222
248
            self.assertRaises(errors.RootMissing,
223
249
                builder.record_entry_contents, entry, [], 'foo', tree,
224
250
                    tree.path_content_summary('foo'))
226
252
        finally:
227
253
            tree.unlock()
228
254
 
229
 
    def test_commit_unchanged_root(self):
 
255
    def test_commit_unchanged_root_record_entry_contents(self):
230
256
        tree = self.make_branch_and_tree(".")
231
257
        old_revision_id = tree.commit('')
232
258
        tree.lock_write()
235
261
        self.addCleanup(parent_tree.unlock)
236
262
        builder = tree.branch.get_commit_builder([old_revision_id])
237
263
        try:
 
264
            if not builder.supports_record_entry_contents:
 
265
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
266
                    "record_entry_contents")
238
267
            ie = inventory.make_entry('directory', '', None,
239
268
                    tree.get_root_id())
240
269
            delta, version_recorded, fs_hash = builder.record_entry_contents(
274
303
            # pointless commit.
275
304
            self.assertFalse(builder.any_changes())
276
305
            builder.finish_inventory()
277
 
            new_root = tree.branch.repository.get_inventory(
278
 
                builder._new_revision_id).root
 
306
            builder_tree = builder.revision_tree()
 
307
            new_root_id = builder_tree.get_root_id()
 
308
            new_root_revision = builder_tree.get_file_revision(new_root_id)
279
309
            if tree.branch.repository.supports_rich_root():
280
310
                # We should not have seen a new root revision
281
 
                self.assertEqual(old_revision_id, new_root.revision)
 
311
                self.assertEqual(old_revision_id, new_root_revision)
282
312
            else:
283
313
                # We should see a new root revision
284
 
                self.assertNotEqual(old_revision_id, new_root.revision)
 
314
                self.assertNotEqual(old_revision_id, new_root_revision)
285
315
        finally:
286
316
            builder.abort()
287
317
            tree.unlock()
288
318
 
289
 
    def test_commit(self):
 
319
    def test_commit_record_entry_contents(self):
290
320
        tree = self.make_branch_and_tree(".")
291
321
        tree.lock_write()
292
322
        try:
293
323
            builder = tree.branch.get_commit_builder([])
 
324
            if not builder.supports_record_entry_contents:
 
325
                raise tests.TestNotApplicable("CommitBuilder doesn't "
 
326
                    "support record_entry_contents")
294
327
            self.record_root(builder, tree)
295
328
            builder.finish_inventory()
296
329
            rev_id = builder.commit('foo bar')
318
351
            builder = tree.branch.get_commit_builder([old_revision_id])
319
352
            total_delta = []
320
353
            try:
 
354
                if not builder.supports_record_entry_contents:
 
355
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
356
                        "support record_entry_contents")
321
357
                parent_invs = [basis.inventory]
322
358
                builder.will_record_deletes()
323
359
                if builder.record_root_entry:
373
409
            basis = tree.branch.repository.revision_tree(rev_id)
374
410
            builder = tree.branch.get_commit_builder([rev_id])
375
411
            try:
 
412
                if not builder.supports_record_entry_contents:
 
413
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
414
                        "support record_entry_contents")
376
415
                builder.will_record_deletes()
377
416
                if builder.record_root_entry is True:
378
417
                    parent_invs = [basis.inventory]
433
472
        try:
434
473
            builder = tree.branch.get_commit_builder([rev_id])
435
474
            try:
 
475
                if not builder.supports_record_entry_contents:
 
476
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
477
                        "support record_entry_contents")
436
478
                self.record_root(builder, tree)
437
479
                self.assertRaises(AssertionError,
438
480
                    builder.record_delete, "foo", "foo-id")
441
483
        finally:
442
484
            tree.unlock()
443
485
 
444
 
    def test_revision_tree(self):
 
486
    def test_revision_tree_record_entry_contents(self):
445
487
        tree = self.make_branch_and_tree(".")
446
488
        tree.lock_write()
447
489
        try:
448
490
            builder = tree.branch.get_commit_builder([])
 
491
            if not builder.supports_record_entry_contents:
 
492
                raise tests.TestNotApplicable("CommitBuilder doesn't "
 
493
                    "support record_entry_contents")
449
494
            self.record_root(builder, tree)
450
495
            builder.finish_inventory()
451
496
            rev_id = builder.commit('foo bar')
490
535
        basis_tree = tree.basis_tree()
491
536
        basis_tree.lock_read()
492
537
        self.addCleanup(basis_tree.unlock)
493
 
        self.assertEqual(rev_id, basis_tree.inventory.root.revision)
 
538
        self.assertEqual(rev_id,
 
539
            basis_tree.get_file_revision(basis_tree.get_root_id()))
494
540
 
495
541
    def _get_revtrees(self, tree, revision_ids):
496
542
        tree.lock_read()
510
556
        rev1 = tree.commit('')
511
557
        rev2 = tree.commit('')
512
558
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
513
 
        self.assertEqual(rev1, tree1.inventory.root.revision)
 
559
        self.assertEqual(rev1, tree1.get_file_revision(tree1.get_root_id()))
514
560
        if tree.branch.repository.supports_rich_root():
515
 
            self.assertEqual(rev1, tree2.inventory.root.revision)
 
561
            self.assertEqual(rev1,
 
562
                tree2.get_file_revision(tree2.get_root_id()))
516
563
        else:
517
 
            self.assertEqual(rev2, tree2.inventory.root.revision)
 
564
            self.assertEqual(rev2,
 
565
                tree2.get_file_revision(tree2.get_root_id()))
518
566
 
519
567
    def _add_commit_check_unchanged(self, tree, name, mini_commit=None):
520
568
        tree.add([name], [name + 'id'])
527
575
            mini_commit = self.mini_commit
528
576
        rev2 = mini_commit(tree, name, name, False, False)
529
577
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
530
 
        self.assertEqual(rev1, tree1.inventory[file_id].revision)
531
 
        self.assertEqual(rev1, tree2.inventory[file_id].revision)
 
578
        self.assertEqual(rev1, tree1.get_file_revision(file_id))
 
579
        self.assertEqual(rev1, tree2.get_file_revision(file_id))
532
580
        expected_graph = {}
533
581
        expected_graph[(file_id, rev1)] = ()
534
582
        self.assertFileGraph(expected_graph, tree, (file_id, rev1))
557
605
        tree.add(['dir/content'], ['contentid'])
558
606
        rev2 = tree.commit('')
559
607
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
560
 
        self.assertEqual(rev1, tree1.inventory['dirid'].revision)
561
 
        self.assertEqual(rev1, tree2.inventory['dirid'].revision)
 
608
        self.assertEqual(rev1, tree1.get_file_revision('dirid'))
 
609
        self.assertEqual(rev1, tree2.get_file_revision('dirid'))
562
610
        file_id = 'dirid'
563
611
        expected_graph = {}
564
612
        expected_graph[(file_id, rev1)] = ()
767
815
        rev2 = mini_commit(tree, name, tree.id2path(file_id),
768
816
            expect_fs_hash=expect_fs_hash)
769
817
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
770
 
        self.assertEqual(rev1, tree1.inventory[file_id].revision)
771
 
        self.assertEqual(rev2, tree2.inventory[file_id].revision)
 
818
        self.assertEqual(rev1, tree1.get_file_revision(file_id))
 
819
        self.assertEqual(rev2, tree2.get_file_revision(file_id))
772
820
        expected_graph = {}
773
821
        expected_graph[(file_id, rev1)] = ()
774
822
        expected_graph[(file_id, rev2)] = ((file_id, rev1),)
794
842
            # record_entry_contents.
795
843
            parent_ids = tree.get_parent_ids()
796
844
            builder = tree.branch.get_commit_builder(parent_ids)
797
 
            parent_tree = tree.basis_tree()
798
 
            parent_tree.lock_read()
799
 
            self.addCleanup(parent_tree.unlock)
800
 
            parent_invs = [parent_tree.inventory]
801
 
            for parent_id in parent_ids[1:]:
802
 
                parent_invs.append(tree.branch.repository.revision_tree(
803
 
                    parent_id).inventory)
804
 
            # root
805
 
            builder.record_entry_contents(
806
 
                inventory.make_entry('directory', '', None,
807
 
                    tree.get_root_id()), parent_invs, '', tree,
808
 
                    tree.path_content_summary(''))
809
 
            def commit_id(file_id):
810
 
                old_ie = tree.inventory[file_id]
811
 
                path = tree.id2path(file_id)
812
 
                ie = inventory.make_entry(tree.kind(file_id), old_ie.name,
813
 
                    old_ie.parent_id, file_id)
814
 
                content_summary = tree.path_content_summary(path)
815
 
                if content_summary[0] == 'tree-reference':
816
 
                    content_summary = content_summary[:3] + (
817
 
                        tree.get_reference_revision(file_id),)
818
 
                return builder.record_entry_contents(ie, parent_invs, path,
819
 
                    tree, content_summary)
 
845
            try:
 
846
                if not builder.supports_record_entry_contents:
 
847
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
848
                        "support record_entry_contents")
 
849
                parent_tree = tree.basis_tree()
 
850
                parent_tree.lock_read()
 
851
                self.addCleanup(parent_tree.unlock)
 
852
                parent_invs = [parent_tree.inventory]
 
853
                for parent_id in parent_ids[1:]:
 
854
                    parent_invs.append(tree.branch.repository.revision_tree(
 
855
                        parent_id).inventory)
 
856
                # root
 
857
                builder.record_entry_contents(
 
858
                    inventory.make_entry('directory', '', None,
 
859
                        tree.get_root_id()), parent_invs, '', tree,
 
860
                        tree.path_content_summary(''))
 
861
                def commit_id(file_id):
 
862
                    old_ie = tree.inventory[file_id]
 
863
                    path = tree.id2path(file_id)
 
864
                    ie = inventory.make_entry(tree.kind(file_id), old_ie.name,
 
865
                        old_ie.parent_id, file_id)
 
866
                    content_summary = tree.path_content_summary(path)
 
867
                    if content_summary[0] == 'tree-reference':
 
868
                        content_summary = content_summary[:3] + (
 
869
                            tree.get_reference_revision(file_id),)
 
870
                    return builder.record_entry_contents(ie, parent_invs, path,
 
871
                        tree, content_summary)
820
872
 
821
 
            file_id = tree.path2id(new_name)
822
 
            parent_id = tree.inventory[file_id].parent_id
823
 
            if parent_id != tree.get_root_id():
824
 
                commit_id(parent_id)
825
 
            # because a change of some sort is meant to have occurred,
826
 
            # recording the entry must return True.
827
 
            delta, version_recorded, fs_hash = commit_id(file_id)
828
 
            if records_version:
829
 
                self.assertTrue(version_recorded)
830
 
            else:
831
 
                self.assertFalse(version_recorded)
832
 
            if expect_fs_hash:
833
 
                tree_file_stat = tree.get_file_with_stat(file_id)
834
 
                tree_file_stat[0].close()
835
 
                self.assertEqual(2, len(fs_hash))
836
 
                self.assertEqual(tree.get_file_sha1(file_id), fs_hash[0])
837
 
                self.assertEqualStat(tree_file_stat[1], fs_hash[1])
838
 
            else:
839
 
                self.assertEqual(None, fs_hash)
840
 
            new_entry = builder.new_inventory[file_id]
841
 
            if delta_against_basis:
842
 
                expected_delta = (name, new_name, file_id, new_entry)
843
 
                # The delta should be recorded
844
 
                self.assertEqual(expected_delta, builder._basis_delta[-1])
845
 
            else:
846
 
                expected_delta = None
847
 
            self.assertEqual(expected_delta, delta)
848
 
            builder.finish_inventory()
849
 
            rev2 = builder.commit('')
 
873
                file_id = tree.path2id(new_name)
 
874
                parent_id = tree.inventory[file_id].parent_id
 
875
                if parent_id != tree.get_root_id():
 
876
                    commit_id(parent_id)
 
877
                # because a change of some sort is meant to have occurred,
 
878
                # recording the entry must return True.
 
879
                delta, version_recorded, fs_hash = commit_id(file_id)
 
880
                if records_version:
 
881
                    self.assertTrue(version_recorded)
 
882
                else:
 
883
                    self.assertFalse(version_recorded)
 
884
                if expect_fs_hash:
 
885
                    tree_file_stat = tree.get_file_with_stat(file_id)
 
886
                    tree_file_stat[0].close()
 
887
                    self.assertEqual(2, len(fs_hash))
 
888
                    self.assertEqual(tree.get_file_sha1(file_id), fs_hash[0])
 
889
                    self.assertEqualStat(tree_file_stat[1], fs_hash[1])
 
890
                else:
 
891
                    self.assertEqual(None, fs_hash)
 
892
                new_entry = builder.new_inventory[file_id]
 
893
                if delta_against_basis:
 
894
                    expected_delta = (name, new_name, file_id, new_entry)
 
895
                    # The delta should be recorded
 
896
                    self.assertEqual(expected_delta, builder._basis_delta[-1])
 
897
                else:
 
898
                    expected_delta = None
 
899
                self.assertEqual(expected_delta, delta)
 
900
                builder.finish_inventory()
 
901
            except:
 
902
                builder.abort()
 
903
                raise
 
904
            else:
 
905
                rev2 = builder.commit('')
850
906
        except:
851
 
            builder.abort()
852
907
            tree.unlock()
853
908
            raise
854
909
        try:
882
937
            parent_tree = tree.basis_tree()
883
938
            parent_tree.lock_read()
884
939
            self.addCleanup(parent_tree.unlock)
885
 
            parent_invs = [parent_tree.inventory]
 
940
            parent_trees = [parent_tree]
886
941
            for parent_id in parent_ids[1:]:
887
 
                parent_invs.append(tree.branch.repository.revision_tree(
888
 
                    parent_id).inventory)
 
942
                parent_trees.append(tree.branch.repository.revision_tree(
 
943
                    parent_id))
889
944
            changes = list(tree.iter_changes(parent_tree))
890
945
            result = list(builder.record_iter_changes(tree, parent_ids[0],
891
946
                changes))
911
966
                self.assertFalse(version_recorded)
912
967
            self.assertIs(None, builder.new_inventory)
913
968
            builder.finish_inventory()
914
 
            inv_key = (builder._new_revision_id,)
915
 
            inv_sha1 = tree.branch.repository.inventories.get_sha1s(
916
 
                            [inv_key])[inv_key]
917
 
            self.assertEqual(inv_sha1, builder.inv_sha1)
 
969
            if tree.branch.repository._format.supports_full_versioned_files:
 
970
                inv_key = (builder._new_revision_id,)
 
971
                inv_sha1 = tree.branch.repository.inventories.get_sha1s(
 
972
                                [inv_key])[inv_key]
 
973
                self.assertEqual(inv_sha1, builder.inv_sha1)
918
974
            self.assertIs(None, builder.new_inventory)
919
975
            new_inventory = builder.revision_tree().inventory
920
976
            new_entry = new_inventory[file_id]
939
995
        # (closest to a public per-file graph API we have today)
940
996
        tree.lock_read()
941
997
        self.addCleanup(tree.unlock)
942
 
        g = dict(graph.Graph(tree.branch.repository.texts).iter_ancestry([tip]))
 
998
        g = dict(tree.branch.repository.get_file_graph().iter_ancestry([tip]))
943
999
        self.assertEqual(expected_graph, g)
944
1000
 
945
1001
    def test_last_modified_revision_after_content_file_changes(self):
1020
1076
        rev4 = mini_commit(tree1, 'new_' + name, 'new_' + name,
1021
1077
            expect_fs_hash=expect_fs_hash)
1022
1078
        tree3, = self._get_revtrees(tree1, [rev4])
1023
 
        self.assertEqual(rev4, tree3.inventory[name + 'id'].revision)
 
1079
        self.assertEqual(rev4, tree3.get_file_revision(name + 'id'))
1024
1080
        file_id = name + 'id'
1025
1081
        expected_graph = {}
1026
1082
        expected_graph[(file_id, rev1)] = ()
1086
1142
            rev3 = mini_commit(in_tree, name, 'new_' + name, False,
1087
1143
                delta_against_basis=changed_in_tree)
1088
1144
            tree3, = self._get_revtrees(in_tree, [rev2])
1089
 
            self.assertEqual(rev2, tree3.inventory[name + 'id'].revision)
 
1145
            self.assertEqual(rev2, tree3.get_file_revision(name + 'id'))
1090
1146
            file_id = name + 'id'
1091
1147
            expected_graph = {}
1092
1148
            expected_graph[(file_id, rev1)] = ()
1118
1174
        rev3 = mini_commit(tree1, None, 'name', False)
1119
1175
        tree3, = self._get_revtrees(tree1, [rev2])
1120
1176
        # in rev2, name should be only changed in rev2
1121
 
        self.assertEqual(rev2, tree3.inventory[file_id].revision)
 
1177
        self.assertEqual(rev2, tree3.get_file_revision(file_id))
1122
1178
        expected_graph = {}
1123
1179
        expected_graph[(file_id, rev2)] = ()
1124
1180
        self.assertFileGraph(expected_graph, tree1, (file_id, rev2))