~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2012, 2016 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
19
19
import os
20
20
 
21
21
from bzrlib import (
 
22
    config,
22
23
    errors,
23
 
    graph,
24
24
    inventory,
25
25
    osutils,
26
26
    repository,
28
28
    tests,
29
29
    )
30
30
from bzrlib.tests import per_repository
 
31
from bzrlib.tests import (
 
32
    features,
 
33
    )
31
34
 
32
35
 
33
36
class TestCommitBuilder(per_repository.TestCaseWithRepository):
36
39
        branch = self.make_branch('.')
37
40
        branch.repository.lock_write()
38
41
        builder = branch.repository.get_commit_builder(
39
 
            branch, [], branch.get_config())
 
42
            branch, [], branch.get_config_stack())
40
43
        self.assertIsInstance(builder, repository.CommitBuilder)
41
44
        self.assertTrue(builder.random_revid)
42
45
        branch.repository.commit_write_group()
46
49
        if builder.record_root_entry is True:
47
50
            tree.lock_read()
48
51
            try:
49
 
                ie = tree.inventory.root
 
52
                ie = tree.root_inventory.root
50
53
            finally:
51
54
                tree.unlock()
52
55
            parent_tree = tree.branch.repository.revision_tree(
60
63
        tree.lock_write()
61
64
        try:
62
65
            builder = tree.branch.get_commit_builder([])
 
66
            if not builder.supports_record_entry_contents:
 
67
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
68
                    "record_entry_contents")
63
69
            repo = tree.branch.repository
64
70
            self.record_root(builder, tree)
65
71
            builder.finish_inventory()
84
90
        finally:
85
91
            tree.unlock()
86
92
 
87
 
    def test_abort(self):
 
93
    def test_abort_record_entry_contents(self):
88
94
        tree = self.make_branch_and_tree(".")
89
95
        tree.lock_write()
90
96
        try:
91
97
            builder = tree.branch.get_commit_builder([])
 
98
            if not builder.supports_record_entry_contents:
 
99
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
100
                    "record_entry_contents")
92
101
            self.record_root(builder, tree)
93
102
            builder.finish_inventory()
94
103
            builder.abort()
111
120
        finally:
112
121
            tree.unlock()
113
122
 
 
123
    def test_commit_lossy(self):
 
124
        tree = self.make_branch_and_tree(".")
 
125
        tree.lock_write()
 
126
        try:
 
127
            builder = tree.branch.get_commit_builder([], lossy=True)
 
128
            list(builder.record_iter_changes(tree, tree.last_revision(),
 
129
                tree.iter_changes(tree.basis_tree())))
 
130
            builder.finish_inventory()
 
131
            rev_id = builder.commit('foo bar blah')
 
132
        finally:
 
133
            tree.unlock()
 
134
        rev = tree.branch.repository.get_revision(rev_id)
 
135
        self.assertEqual('foo bar blah', rev.message)
 
136
 
114
137
    def test_commit_message(self):
115
138
        tree = self.make_branch_and_tree(".")
116
139
        tree.lock_write()
117
140
        try:
118
141
            builder = tree.branch.get_commit_builder([])
119
 
            self.record_root(builder, tree)
 
142
            list(builder.record_iter_changes(tree, tree.last_revision(),
 
143
                tree.iter_changes(tree.basis_tree())))
120
144
            builder.finish_inventory()
121
145
            rev_id = builder.commit('foo bar blah')
122
146
        finally:
124
148
        rev = tree.branch.repository.get_revision(rev_id)
125
149
        self.assertEqual('foo bar blah', rev.message)
126
150
 
127
 
    def test_commit_with_revision_id(self):
 
151
    def test_updates_branch(self):
 
152
        tree = self.make_branch_and_tree(".")
 
153
        tree.lock_write()
 
154
        try:
 
155
            builder = tree.branch.get_commit_builder([])
 
156
            list(builder.record_iter_changes(tree, tree.last_revision(),
 
157
                tree.iter_changes(tree.basis_tree())))
 
158
            builder.finish_inventory()
 
159
            will_update_branch = builder.updates_branch
 
160
            rev_id = builder.commit('might update the branch')
 
161
        finally:
 
162
            tree.unlock()
 
163
        actually_updated_branch = (tree.branch.last_revision() == rev_id)
 
164
        self.assertEqual(actually_updated_branch, will_update_branch)
 
165
 
 
166
    def test_commit_with_revision_id_record_entry_contents(self):
128
167
        tree = self.make_branch_and_tree(".")
129
168
        tree.lock_write()
130
169
        try:
142
181
            except errors.CannotSetRevisionId:
143
182
                # This format doesn't support supplied revision ids
144
183
                return
 
184
            if not builder.supports_record_entry_contents:
 
185
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
186
                    "record_entry_contents")
145
187
            self.assertFalse(builder.random_revid)
146
188
            self.record_root(builder, tree)
147
189
            builder.finish_inventory()
191
233
        # on deserialisation, but thats all the current contract guarantees
192
234
        # anyway.
193
235
        self.assertEqual(revision_id,
194
 
            tree.branch.repository.get_inventory(revision_id).revision_id)
 
236
            tree.branch.repository.revision_tree(revision_id).get_revision_id())
195
237
 
196
238
    def test_commit_without_root_errors(self):
197
239
        tree = self.make_branch_and_tree(".")
206
248
                except:
207
249
                    builder.abort()
208
250
                    raise
 
251
                else:
 
252
                    builder.commit("msg")
209
253
            self.assertRaises(errors.RootMissing, do_commit)
210
254
        finally:
211
255
            tree.unlock()
216
260
        try:
217
261
            self.build_tree(['foo'])
218
262
            tree.add('foo', 'foo-id')
219
 
            entry = tree.inventory['foo-id']
220
263
            builder = tree.branch.get_commit_builder([])
 
264
            if not builder.supports_record_entry_contents:
 
265
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
266
                    "record_entry_contents")
 
267
            entry = tree.root_inventory['foo-id']
221
268
            self.assertRaises(errors.RootMissing,
222
269
                builder.record_entry_contents, entry, [], 'foo', tree,
223
270
                    tree.path_content_summary('foo'))
225
272
        finally:
226
273
            tree.unlock()
227
274
 
228
 
    def test_commit_unchanged_root(self):
 
275
    def test_commit_unchanged_root_record_entry_contents(self):
229
276
        tree = self.make_branch_and_tree(".")
230
277
        old_revision_id = tree.commit('')
231
278
        tree.lock_write()
234
281
        self.addCleanup(parent_tree.unlock)
235
282
        builder = tree.branch.get_commit_builder([old_revision_id])
236
283
        try:
 
284
            if not builder.supports_record_entry_contents:
 
285
                raise tests.TestNotApplicable("CommitBuilder doesn't support "
 
286
                    "record_entry_contents")
 
287
            builder.will_record_deletes()
237
288
            ie = inventory.make_entry('directory', '', None,
238
289
                    tree.get_root_id())
239
290
            delta, version_recorded, fs_hash = builder.record_entry_contents(
240
 
                ie, [parent_tree.inventory], '', tree,
 
291
                ie, [parent_tree.root_inventory], '', tree,
241
292
                tree.path_content_summary(''))
242
293
            # Regardless of repository root behaviour we should consider this a
243
294
            # pointless commit.
249
300
            if got_new_revision:
250
301
                self.assertEqual(('', '', ie.file_id, ie), delta)
251
302
                # The delta should be tracked
252
 
                self.assertEqual(delta, builder._basis_delta[-1])
 
303
                self.assertEqual(delta, builder.get_basis_delta()[-1])
253
304
            else:
254
305
                self.assertEqual(None, delta)
255
306
            # Directories do not get hashed.
273
324
            # pointless commit.
274
325
            self.assertFalse(builder.any_changes())
275
326
            builder.finish_inventory()
276
 
            new_root = tree.branch.repository.get_inventory(
277
 
                builder._new_revision_id).root
 
327
            builder.commit('')
 
328
            builder_tree = builder.revision_tree()
 
329
            new_root_id = builder_tree.get_root_id()
 
330
            new_root_revision = builder_tree.get_file_revision(new_root_id)
278
331
            if tree.branch.repository.supports_rich_root():
279
332
                # We should not have seen a new root revision
280
 
                self.assertEqual(old_revision_id, new_root.revision)
 
333
                self.assertEqual(old_revision_id, new_root_revision)
281
334
            else:
282
335
                # We should see a new root revision
283
 
                self.assertNotEqual(old_revision_id, new_root.revision)
 
336
                self.assertNotEqual(old_revision_id, new_root_revision)
284
337
        finally:
285
 
            builder.abort()
286
338
            tree.unlock()
287
339
 
288
 
    def test_commit(self):
 
340
    def test_commit_record_entry_contents(self):
289
341
        tree = self.make_branch_and_tree(".")
290
342
        tree.lock_write()
291
343
        try:
292
344
            builder = tree.branch.get_commit_builder([])
 
345
            if not builder.supports_record_entry_contents:
 
346
                raise tests.TestNotApplicable("CommitBuilder doesn't "
 
347
                    "support record_entry_contents")
293
348
            self.record_root(builder, tree)
294
349
            builder.finish_inventory()
295
350
            rev_id = builder.commit('foo bar')
317
372
            builder = tree.branch.get_commit_builder([old_revision_id])
318
373
            total_delta = []
319
374
            try:
320
 
                parent_invs = [basis.inventory]
 
375
                if not builder.supports_record_entry_contents:
 
376
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
377
                        "support record_entry_contents")
 
378
                parent_invs = [basis.root_inventory]
321
379
                builder.will_record_deletes()
322
380
                if builder.record_root_entry:
323
 
                    ie = basis.inventory.root.copy()
 
381
                    ie = basis.root_inventory.root.copy()
324
382
                    delta, _, _ = builder.record_entry_contents(ie, parent_invs,
325
383
                        '', tree, tree.path_content_summary(''))
326
384
                    if delta is not None:
372
430
            basis = tree.branch.repository.revision_tree(rev_id)
373
431
            builder = tree.branch.get_commit_builder([rev_id])
374
432
            try:
 
433
                if not builder.supports_record_entry_contents:
 
434
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
435
                        "support record_entry_contents")
375
436
                builder.will_record_deletes()
376
437
                if builder.record_root_entry is True:
377
 
                    parent_invs = [basis.inventory]
378
 
                    del basis.inventory.root.children['foo']
379
 
                    builder.record_entry_contents(basis.inventory.root,
 
438
                    parent_invs = [basis.root_inventory]
 
439
                    del basis.root_inventory.root.children['foo']
 
440
                    builder.record_entry_contents(basis.root_inventory.root,
380
441
                        parent_invs, '', tree, tree.path_content_summary(''))
381
442
                # the delta should be returned, and recorded in _basis_delta
382
443
                delta = builder.record_delete("foo", "foo-id")
383
444
                self.assertEqual(("foo", None, "foo-id", None), delta)
384
 
                self.assertEqual(delta, builder._basis_delta[-1])
 
445
                self.assertEqual(delta, builder.get_basis_delta()[-1])
385
446
                builder.finish_inventory()
386
447
                rev_id2 = builder.commit('delete foo')
387
448
            except:
403
464
        try:
404
465
            builder = tree.branch.get_commit_builder([rev_id])
405
466
            try:
 
467
                builder.will_record_deletes()
406
468
                delete_change = ('foo-id', ('foo', None), True, (True, False),
407
469
                    (tree.path2id(''), None), ('foo', None), ('file', None),
408
470
                    (False, None))
409
471
                list(builder.record_iter_changes(tree, rev_id,
410
472
                    [delete_change]))
411
473
                self.assertEqual(("foo", None, "foo-id", None),
412
 
                    builder._basis_delta[0])
 
474
                    builder.get_basis_delta()[0])
413
475
                self.assertTrue(builder.any_changes())
414
476
                builder.finish_inventory()
415
477
                rev_id2 = builder.commit('delete foo')
432
494
        try:
433
495
            builder = tree.branch.get_commit_builder([rev_id])
434
496
            try:
 
497
                if not builder.supports_record_entry_contents:
 
498
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
499
                        "support record_entry_contents")
435
500
                self.record_root(builder, tree)
436
501
                self.assertRaises(AssertionError,
437
502
                    builder.record_delete, "foo", "foo-id")
440
505
        finally:
441
506
            tree.unlock()
442
507
 
443
 
    def test_revision_tree(self):
 
508
    def test_revision_tree_record_entry_contents(self):
444
509
        tree = self.make_branch_and_tree(".")
445
510
        tree.lock_write()
446
511
        try:
447
512
            builder = tree.branch.get_commit_builder([])
 
513
            if not builder.supports_record_entry_contents:
 
514
                raise tests.TestNotApplicable("CommitBuilder doesn't "
 
515
                    "support record_entry_contents")
448
516
            self.record_root(builder, tree)
449
517
            builder.finish_inventory()
450
518
            rev_id = builder.commit('foo bar')
474
542
            # Just a couple simple tests to ensure that it actually follows
475
543
            # the RevisionTree api.
476
544
            self.assertEqual(rev_id, rev_tree.get_revision_id())
477
 
            self.assertEqual([], rev_tree.get_parent_ids())
 
545
            self.assertEqual((), tuple(rev_tree.get_parent_ids()))
478
546
        finally:
479
547
            tree.unlock()
480
548
 
489
557
        basis_tree = tree.basis_tree()
490
558
        basis_tree.lock_read()
491
559
        self.addCleanup(basis_tree.unlock)
492
 
        self.assertEqual(rev_id, basis_tree.inventory.root.revision)
 
560
        self.assertEqual(rev_id,
 
561
            basis_tree.get_file_revision(basis_tree.get_root_id()))
493
562
 
494
563
    def _get_revtrees(self, tree, revision_ids):
495
564
        tree.lock_read()
509
578
        rev1 = tree.commit('')
510
579
        rev2 = tree.commit('')
511
580
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
512
 
        self.assertEqual(rev1, tree1.inventory.root.revision)
 
581
        self.assertEqual(rev1, tree1.get_file_revision(tree1.get_root_id()))
513
582
        if tree.branch.repository.supports_rich_root():
514
 
            self.assertEqual(rev1, tree2.inventory.root.revision)
 
583
            self.assertEqual(rev1,
 
584
                tree2.get_file_revision(tree2.get_root_id()))
515
585
        else:
516
 
            self.assertEqual(rev2, tree2.inventory.root.revision)
 
586
            self.assertEqual(rev2,
 
587
                tree2.get_file_revision(tree2.get_root_id()))
517
588
 
518
589
    def _add_commit_check_unchanged(self, tree, name, mini_commit=None):
519
590
        tree.add([name], [name + 'id'])
526
597
            mini_commit = self.mini_commit
527
598
        rev2 = mini_commit(tree, name, name, False, False)
528
599
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
529
 
        self.assertEqual(rev1, tree1.inventory[file_id].revision)
530
 
        self.assertEqual(rev1, tree2.inventory[file_id].revision)
 
600
        self.assertEqual(rev1, tree1.get_file_revision(file_id))
 
601
        self.assertEqual(rev1, tree2.get_file_revision(file_id))
531
602
        expected_graph = {}
532
603
        expected_graph[(file_id, rev1)] = ()
533
604
        self.assertFileGraph(expected_graph, tree, (file_id, rev1))
556
627
        tree.add(['dir/content'], ['contentid'])
557
628
        rev2 = tree.commit('')
558
629
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
559
 
        self.assertEqual(rev1, tree1.inventory['dirid'].revision)
560
 
        self.assertEqual(rev1, tree2.inventory['dirid'].revision)
 
630
        self.assertEqual(rev1, tree1.get_file_revision('dirid'))
 
631
        self.assertEqual(rev1, tree2.get_file_revision('dirid'))
561
632
        file_id = 'dirid'
562
633
        expected_graph = {}
563
634
        expected_graph[(file_id, rev1)] = ()
578
649
 
579
650
    def test_last_modified_revision_after_commit_link_unchanged(self):
580
651
        # committing without changing a link does not change the last modified.
581
 
        self.requireFeature(tests.SymlinkFeature)
 
652
        self.requireFeature(features.SymlinkFeature)
582
653
        tree = self.make_branch_and_tree('.')
583
654
        os.symlink('target', 'link')
584
655
        self._add_commit_check_unchanged(tree, 'link')
585
656
 
586
657
    def test_last_modified_revision_after_commit_link_unchanged_ric(self):
587
658
        # committing without changing a link does not change the last modified.
588
 
        self.requireFeature(tests.SymlinkFeature)
 
659
        self.requireFeature(features.SymlinkFeature)
589
660
        tree = self.make_branch_and_tree('.')
590
661
        os.symlink('target', 'link')
591
662
        self._add_commit_check_unchanged(tree, 'link',
660
731
 
661
732
    def test_last_modified_revision_after_rename_link_changes(self):
662
733
        # renaming a link changes the last modified.
663
 
        self.requireFeature(tests.SymlinkFeature)
 
734
        self.requireFeature(features.SymlinkFeature)
664
735
        tree = self.make_branch_and_tree('.')
665
736
        os.symlink('target', 'link')
666
737
        self._add_commit_renamed_check_changed(tree, 'link')
667
738
 
668
739
    def test_last_modified_revision_after_rename_link_changes_ric(self):
669
740
        # renaming a link changes the last modified.
670
 
        self.requireFeature(tests.SymlinkFeature)
 
741
        self.requireFeature(features.SymlinkFeature)
671
742
        tree = self.make_branch_and_tree('.')
672
743
        os.symlink('target', 'link')
673
744
        self._add_commit_renamed_check_changed(tree, 'link',
735
806
 
736
807
    def test_last_modified_revision_after_reparent_link_changes(self):
737
808
        # reparenting a link changes the last modified.
738
 
        self.requireFeature(tests.SymlinkFeature)
 
809
        self.requireFeature(features.SymlinkFeature)
739
810
        tree = self.make_branch_and_tree('.')
740
811
        os.symlink('target', 'link')
741
812
        self._add_commit_reparent_check_changed(tree, 'link')
742
813
 
743
814
    def test_last_modified_revision_after_reparent_link_changes_ric(self):
744
815
        # reparenting a link changes the last modified.
745
 
        self.requireFeature(tests.SymlinkFeature)
 
816
        self.requireFeature(features.SymlinkFeature)
746
817
        tree = self.make_branch_and_tree('.')
747
818
        os.symlink('target', 'link')
748
819
        self._add_commit_reparent_check_changed(tree, 'link',
766
837
        rev2 = mini_commit(tree, name, tree.id2path(file_id),
767
838
            expect_fs_hash=expect_fs_hash)
768
839
        tree1, tree2 = self._get_revtrees(tree, [rev1, rev2])
769
 
        self.assertEqual(rev1, tree1.inventory[file_id].revision)
770
 
        self.assertEqual(rev2, tree2.inventory[file_id].revision)
 
840
        self.assertEqual(rev1, tree1.get_file_revision(file_id))
 
841
        self.assertEqual(rev2, tree2.get_file_revision(file_id))
771
842
        expected_graph = {}
772
843
        expected_graph[(file_id, rev1)] = ()
773
844
        expected_graph[(file_id, rev2)] = ((file_id, rev1),)
793
864
            # record_entry_contents.
794
865
            parent_ids = tree.get_parent_ids()
795
866
            builder = tree.branch.get_commit_builder(parent_ids)
796
 
            parent_tree = tree.basis_tree()
797
 
            parent_tree.lock_read()
798
 
            self.addCleanup(parent_tree.unlock)
799
 
            parent_invs = [parent_tree.inventory]
800
 
            for parent_id in parent_ids[1:]:
801
 
                parent_invs.append(tree.branch.repository.revision_tree(
802
 
                    parent_id).inventory)
803
 
            # root
804
 
            builder.record_entry_contents(
805
 
                inventory.make_entry('directory', '', None,
806
 
                    tree.get_root_id()), parent_invs, '', tree,
807
 
                    tree.path_content_summary(''))
808
 
            def commit_id(file_id):
809
 
                old_ie = tree.inventory[file_id]
810
 
                path = tree.id2path(file_id)
811
 
                ie = inventory.make_entry(tree.kind(file_id), old_ie.name,
812
 
                    old_ie.parent_id, file_id)
813
 
                content_summary = tree.path_content_summary(path)
814
 
                if content_summary[0] == 'tree-reference':
815
 
                    content_summary = content_summary[:3] + (
816
 
                        tree.get_reference_revision(file_id),)
817
 
                return builder.record_entry_contents(ie, parent_invs, path,
818
 
                    tree, content_summary)
 
867
            try:
 
868
                if not builder.supports_record_entry_contents:
 
869
                    raise tests.TestNotApplicable("CommitBuilder doesn't "
 
870
                        "support record_entry_contents")
 
871
                builder.will_record_deletes()
 
872
                parent_tree = tree.basis_tree()
 
873
                parent_tree.lock_read()
 
874
                self.addCleanup(parent_tree.unlock)
 
875
                parent_invs = [parent_tree.root_inventory]
 
876
                for parent_id in parent_ids[1:]:
 
877
                    parent_invs.append(tree.branch.repository.revision_tree(
 
878
                        parent_id).root_inventory)
 
879
                # root
 
880
                builder.record_entry_contents(
 
881
                    inventory.make_entry('directory', '', None,
 
882
                        tree.get_root_id()), parent_invs, '', tree,
 
883
                        tree.path_content_summary(''))
 
884
                def commit_id(file_id):
 
885
                    old_ie = tree.root_inventory[file_id]
 
886
                    path = tree.id2path(file_id)
 
887
                    ie = inventory.make_entry(tree.kind(file_id), old_ie.name,
 
888
                        old_ie.parent_id, file_id)
 
889
                    content_summary = tree.path_content_summary(path)
 
890
                    if content_summary[0] == 'tree-reference':
 
891
                        content_summary = content_summary[:3] + (
 
892
                            tree.get_reference_revision(file_id),)
 
893
                    return builder.record_entry_contents(ie, parent_invs, path,
 
894
                        tree, content_summary)
819
895
 
820
 
            file_id = tree.path2id(new_name)
821
 
            parent_id = tree.inventory[file_id].parent_id
822
 
            if parent_id != tree.get_root_id():
823
 
                commit_id(parent_id)
824
 
            # because a change of some sort is meant to have occurred,
825
 
            # recording the entry must return True.
826
 
            delta, version_recorded, fs_hash = commit_id(file_id)
827
 
            if records_version:
828
 
                self.assertTrue(version_recorded)
829
 
            else:
830
 
                self.assertFalse(version_recorded)
831
 
            if expect_fs_hash:
832
 
                tree_file_stat = tree.get_file_with_stat(file_id)
833
 
                tree_file_stat[0].close()
834
 
                self.assertEqual(2, len(fs_hash))
835
 
                self.assertEqual(tree.get_file_sha1(file_id), fs_hash[0])
836
 
                self.assertEqualStat(tree_file_stat[1], fs_hash[1])
837
 
            else:
838
 
                self.assertEqual(None, fs_hash)
839
 
            new_entry = builder.new_inventory[file_id]
840
 
            if delta_against_basis:
841
 
                expected_delta = (name, new_name, file_id, new_entry)
842
 
                # The delta should be recorded
843
 
                self.assertEqual(expected_delta, builder._basis_delta[-1])
844
 
            else:
845
 
                expected_delta = None
846
 
            self.assertEqual(expected_delta, delta)
847
 
            builder.finish_inventory()
848
 
            rev2 = builder.commit('')
 
896
                file_id = tree.path2id(new_name)
 
897
                parent_id = tree.root_inventory[file_id].parent_id
 
898
                if parent_id != tree.get_root_id():
 
899
                    commit_id(parent_id)
 
900
                # because a change of some sort is meant to have occurred,
 
901
                # recording the entry must return True.
 
902
                delta, version_recorded, fs_hash = commit_id(file_id)
 
903
                if records_version:
 
904
                    self.assertTrue(version_recorded)
 
905
                else:
 
906
                    self.assertFalse(version_recorded)
 
907
                if expect_fs_hash:
 
908
                    tree_file_stat = tree.get_file_with_stat(file_id)
 
909
                    tree_file_stat[0].close()
 
910
                    self.assertEqual(2, len(fs_hash))
 
911
                    self.assertEqual(tree.get_file_sha1(file_id), fs_hash[0])
 
912
                    self.assertEqualStat(tree_file_stat[1], fs_hash[1])
 
913
                else:
 
914
                    self.assertEqual(None, fs_hash)
 
915
                new_entry = builder.new_inventory[file_id]
 
916
                if delta_against_basis:
 
917
                    expected_delta = (name, new_name, file_id, new_entry)
 
918
                    # The delta should be recorded
 
919
                    self.assertEqual(expected_delta,
 
920
                        builder.get_basis_delta()[-1])
 
921
                else:
 
922
                    expected_delta = None
 
923
                self.assertEqual(expected_delta, delta)
 
924
                builder.finish_inventory()
 
925
            except:
 
926
                builder.abort()
 
927
                raise
 
928
            else:
 
929
                rev2 = builder.commit('')
849
930
        except:
850
 
            builder.abort()
851
931
            tree.unlock()
852
932
            raise
853
933
        try:
878
958
            # record_entry_contents.
879
959
            parent_ids = tree.get_parent_ids()
880
960
            builder = tree.branch.get_commit_builder(parent_ids)
 
961
            builder.will_record_deletes()
881
962
            parent_tree = tree.basis_tree()
882
963
            parent_tree.lock_read()
883
964
            self.addCleanup(parent_tree.unlock)
884
 
            parent_invs = [parent_tree.inventory]
 
965
            parent_trees = [parent_tree]
885
966
            for parent_id in parent_ids[1:]:
886
 
                parent_invs.append(tree.branch.repository.revision_tree(
887
 
                    parent_id).inventory)
 
967
                parent_trees.append(tree.branch.repository.revision_tree(
 
968
                    parent_id))
888
969
            changes = list(tree.iter_changes(parent_tree))
889
970
            result = list(builder.record_iter_changes(tree, parent_ids[0],
890
971
                changes))
899
980
                self.assertEqualStat(result[2][1], tree_file_stat[1])
900
981
            else:
901
982
                self.assertEqual([], result)
902
 
            delta = builder._basis_delta
 
983
            self.assertIs(None, builder.new_inventory)
 
984
            builder.finish_inventory()
 
985
            if tree.branch.repository._format.supports_full_versioned_files:
 
986
                inv_key = (builder._new_revision_id,)
 
987
                inv_sha1 = tree.branch.repository.inventories.get_sha1s(
 
988
                                [inv_key])[inv_key]
 
989
                self.assertEqual(inv_sha1, builder.inv_sha1)
 
990
            self.assertIs(None, builder.new_inventory)
 
991
            rev2 = builder.commit('')
 
992
            delta = builder.get_basis_delta()
903
993
            delta_dict = dict((change[2], change) for change in delta)
904
994
            version_recorded = (file_id in delta_dict and
905
995
                delta_dict[file_id][3] is not None and
906
 
                delta_dict[file_id][3].revision == builder._new_revision_id)
 
996
                delta_dict[file_id][3].revision == rev2)
907
997
            if records_version:
908
998
                self.assertTrue(version_recorded)
909
999
            else:
910
1000
                self.assertFalse(version_recorded)
911
 
            self.assertIs(None, builder.new_inventory)
912
 
            builder.finish_inventory()
913
 
            inv_key = (builder._new_revision_id,)
914
 
            inv_sha1 = tree.branch.repository.inventories.get_sha1s(
915
 
                            [inv_key])[inv_key]
916
 
            self.assertEqual(inv_sha1, builder.inv_sha1)
917
 
            self.assertIs(None, builder.new_inventory)
918
 
            new_inventory = builder.revision_tree().inventory
 
1001
 
 
1002
            new_inventory = builder.revision_tree().root_inventory
919
1003
            new_entry = new_inventory[file_id]
920
1004
            if delta_against_basis:
921
1005
                expected_delta = (name, new_name, file_id, new_entry)
923
1007
            else:
924
1008
                expected_delta = None
925
1009
                self.assertFalse(version_recorded)
926
 
            rev2 = builder.commit('')
927
1010
            tree.set_parent_ids([rev2])
928
1011
        except:
929
1012
            builder.abort()
938
1021
        # (closest to a public per-file graph API we have today)
939
1022
        tree.lock_read()
940
1023
        self.addCleanup(tree.unlock)
941
 
        g = dict(graph.Graph(tree.branch.repository.texts).iter_ancestry([tip]))
 
1024
        g = dict(tree.branch.repository.get_file_graph().iter_ancestry([tip]))
942
1025
        self.assertEqual(expected_graph, g)
943
1026
 
944
1027
    def test_last_modified_revision_after_content_file_changes(self):
962
1045
 
963
1046
    def test_last_modified_revision_after_content_link_changes(self):
964
1047
        # changing a link changes the last modified.
965
 
        self.requireFeature(tests.SymlinkFeature)
 
1048
        self.requireFeature(features.SymlinkFeature)
966
1049
        tree = self.make_branch_and_tree('.')
967
1050
        os.symlink('target', 'link')
968
1051
        def change_link():
975
1058
        if file_id is None:
976
1059
            file_id = link
977
1060
        # changing a link changes the last modified.
978
 
        self.requireFeature(tests.SymlinkFeature)
 
1061
        self.requireFeature(features.SymlinkFeature)
979
1062
        tree = self.make_branch_and_tree('.')
980
1063
        os.symlink(target, link)
981
1064
        def change_link():
991
1074
            'link', 'target', 'newtarget')
992
1075
 
993
1076
    def test_last_modified_rev_after_content_unicode_link_changes_ric(self):
994
 
        self.requireFeature(tests.UnicodeFilenameFeature)
 
1077
        self.requireFeature(features.UnicodeFilenameFeature)
995
1078
        self._test_last_mod_rev_after_content_link_changes_ric(
996
1079
            u'li\u1234nk', u'targ\N{Euro Sign}t', u'n\N{Euro Sign}wtarget',
997
1080
 
1019
1102
        rev4 = mini_commit(tree1, 'new_' + name, 'new_' + name,
1020
1103
            expect_fs_hash=expect_fs_hash)
1021
1104
        tree3, = self._get_revtrees(tree1, [rev4])
1022
 
        self.assertEqual(rev4, tree3.inventory[name + 'id'].revision)
 
1105
        self.assertEqual(rev4, tree3.get_file_revision(name + 'id'))
1023
1106
        file_id = name + 'id'
1024
1107
        expected_graph = {}
1025
1108
        expected_graph[(file_id, rev1)] = ()
1056
1139
 
1057
1140
    def test_last_modified_revision_after_merge_link_changes(self):
1058
1141
        # merge a link changes the last modified.
1059
 
        self.requireFeature(tests.SymlinkFeature)
 
1142
        self.requireFeature(features.SymlinkFeature)
1060
1143
        tree1 = self.make_branch_and_tree('t1')
1061
1144
        os.symlink('target', 't1/link')
1062
1145
        self._commit_sprout_rename_merge(tree1, 'link')
1063
1146
 
1064
1147
    def test_last_modified_revision_after_merge_link_changes_ric(self):
1065
1148
        # merge a link changes the last modified.
1066
 
        self.requireFeature(tests.SymlinkFeature)
 
1149
        self.requireFeature(features.SymlinkFeature)
1067
1150
        tree1 = self.make_branch_and_tree('t1')
1068
1151
        os.symlink('target', 't1/link')
1069
1152
        self._commit_sprout_rename_merge(tree1, 'link',
1085
1168
            rev3 = mini_commit(in_tree, name, 'new_' + name, False,
1086
1169
                delta_against_basis=changed_in_tree)
1087
1170
            tree3, = self._get_revtrees(in_tree, [rev2])
1088
 
            self.assertEqual(rev2, tree3.inventory[name + 'id'].revision)
 
1171
            self.assertEqual(rev2, tree3.get_file_revision(name + 'id'))
1089
1172
            file_id = name + 'id'
1090
1173
            expected_graph = {}
1091
1174
            expected_graph[(file_id, rev1)] = ()
1117
1200
        rev3 = mini_commit(tree1, None, 'name', False)
1118
1201
        tree3, = self._get_revtrees(tree1, [rev2])
1119
1202
        # in rev2, name should be only changed in rev2
1120
 
        self.assertEqual(rev2, tree3.inventory[file_id].revision)
 
1203
        self.assertEqual(rev2, tree3.get_file_revision(file_id))
1121
1204
        expected_graph = {}
1122
1205
        expected_graph[(file_id, rev2)] = ()
1123
1206
        self.assertFileGraph(expected_graph, tree1, (file_id, rev2))
1150
1233
 
1151
1234
    def test_last_modified_revision_after_converged_merge_link_unchanged(self):
1152
1235
        # merge a link that changed preserves the last modified.
1153
 
        self.requireFeature(tests.SymlinkFeature)
 
1236
        self.requireFeature(features.SymlinkFeature)
1154
1237
        tree1 = self.make_branch_and_tree('t1')
1155
1238
        os.symlink('target', 't1/link')
1156
1239
        self._commit_sprout_rename_merge_converged(tree1, 'link')
1157
1240
 
1158
1241
    def test_last_modified_revision_after_converged_merge_link_unchanged_ric(self):
1159
1242
        # merge a link that changed preserves the last modified.
1160
 
        self.requireFeature(tests.SymlinkFeature)
 
1243
        self.requireFeature(features.SymlinkFeature)
1161
1244
        tree1 = self.make_branch_and_tree('t1')
1162
1245
        os.symlink('target', 't1/link')
1163
1246
        self._commit_sprout_rename_merge_converged(tree1, 'link',
1203
1286
        self.build_tree([name])
1204
1287
 
1205
1288
    def make_link(self, name):
1206
 
        self.requireFeature(tests.SymlinkFeature)
 
1289
        self.requireFeature(features.SymlinkFeature)
1207
1290
        os.symlink('target', name)
1208
1291
 
1209
1292
    def make_reference(self, name):
1218
1301
        make_before(path)
1219
1302
 
1220
1303
        def change_kind():
1221
 
            osutils.delete_any(path)
 
1304
            if osutils.file_kind(path) == "directory":
 
1305
                osutils.rmtree(path)
 
1306
            else:
 
1307
                osutils.delete_any(path)
1222
1308
            make_after(path)
1223
1309
 
1224
1310
        self._add_commit_change_check_changed(tree, path, change_kind,
1229
1315
            expect_fs_hash=True)
1230
1316
 
1231
1317
    def test_last_modified_dir_file_ric(self):
1232
 
        self._check_kind_change(self.make_dir, self.make_file,
1233
 
            expect_fs_hash=True,
1234
 
            mini_commit=self.mini_commit_record_iter_changes)
 
1318
        try:
 
1319
            self._check_kind_change(self.make_dir, self.make_file,
 
1320
                expect_fs_hash=True,
 
1321
                mini_commit=self.mini_commit_record_iter_changes)
 
1322
        except errors.UnsupportedKindChange:
 
1323
            raise tests.TestSkipped(
 
1324
                "tree does not support changing entry kind from "
 
1325
                "directory to file")
1235
1326
 
1236
1327
    def test_last_modified_dir_link(self):
1237
1328
        self._check_kind_change(self.make_dir, self.make_link)
1238
1329
 
1239
1330
    def test_last_modified_dir_link_ric(self):
1240
 
        self._check_kind_change(self.make_dir, self.make_link,
1241
 
            mini_commit=self.mini_commit_record_iter_changes)
 
1331
        try:
 
1332
            self._check_kind_change(self.make_dir, self.make_link,
 
1333
                mini_commit=self.mini_commit_record_iter_changes)
 
1334
        except errors.UnsupportedKindChange:
 
1335
            raise tests.TestSkipped(
 
1336
                "tree does not support changing entry kind from "
 
1337
                "directory to link")
1242
1338
 
1243
1339
    def test_last_modified_link_file(self):
1244
1340
        self._check_kind_change(self.make_link, self.make_file,
1275
1371
        branch.repository.lock_write()
1276
1372
        self.addCleanup(branch.repository.unlock)
1277
1373
        self.assertRaises(ValueError, branch.repository.get_commit_builder,
1278
 
            branch, [], branch.get_config(),
 
1374
            branch, [], branch.get_config_stack(),
1279
1375
            revprops={'invalid': u'property\rwith\r\ninvalid chars'})
1280
1376
 
1281
1377
    def test_commit_builder_commit_with_invalid_message(self):
1283
1379
        branch.repository.lock_write()
1284
1380
        self.addCleanup(branch.repository.unlock)
1285
1381
        builder = branch.repository.get_commit_builder(branch, [],
1286
 
            branch.get_config())
 
1382
            branch.get_config_stack())
1287
1383
        self.addCleanup(branch.repository.abort_write_group)
1288
1384
        self.assertRaises(ValueError, builder.commit,
1289
1385
            u'Invalid\r\ncommit message\r\n')
1290
1386
 
 
1387
    def test_non_ascii_str_committer_rejected(self):
 
1388
        """Ensure an error is raised on a non-ascii byte string committer"""
 
1389
        branch = self.make_branch('.')
 
1390
        branch.repository.lock_write()
 
1391
        self.addCleanup(branch.repository.unlock)
 
1392
        self.assertRaises(UnicodeDecodeError,
 
1393
            branch.repository.get_commit_builder,
 
1394
            branch, [], branch.get_config_stack(),
 
1395
            committer="Erik B\xe5gfors <erik@example.com>")
 
1396
 
1291
1397
    def test_stacked_repositories_reject_commit_builder(self):
1292
1398
        # As per bug 375013, committing to stacked repositories is currently
1293
 
        # broken, so repositories with fallbacks refuse to hand out a commit
1294
 
        # builder.
 
1399
        # broken if we aren't in a chk repository. So old repositories with
 
1400
        # fallbacks refuse to hand out a commit builder.
1295
1401
        repo_basis = self.make_repository('basis')
1296
1402
        branch = self.make_branch('local')
1297
1403
        repo_local = branch.repository
1299
1405
            repo_local.add_fallback_repository(repo_basis)
1300
1406
        except errors.UnstackableRepositoryFormat:
1301
1407
            raise tests.TestNotApplicable("not a stackable format.")
1302
 
        repo_local.lock_write()
1303
 
        self.addCleanup(repo_local.unlock)
1304
 
        self.assertRaises(errors.BzrError, repo_local.get_commit_builder,
1305
 
            branch, [], branch.get_config())
 
1408
        self.addCleanup(repo_local.lock_write().unlock)
 
1409
        if not repo_local._format.supports_chks:
 
1410
            self.assertRaises(errors.BzrError, repo_local.get_commit_builder,
 
1411
                branch, [], branch.get_config_stack())
 
1412
        else:
 
1413
            builder = repo_local.get_commit_builder(branch, [],
 
1414
                                                    branch.get_config_stack())
 
1415
            builder.abort()
1306
1416
 
1307
1417
    def test_committer_no_username(self):
1308
1418
        # Ensure that when no username is available but a committer is
1309
1419
        # supplied, commit works.
1310
 
        del os.environ['EMAIL']
 
1420
        self.overrideEnv('EMAIL', None)
 
1421
        self.overrideEnv('BZR_EMAIL', None)
 
1422
        # Also, make sure that it's not inferred from mailname.
 
1423
        self.overrideAttr(config, '_auto_user_id',
 
1424
            lambda: (None, None))
1311
1425
        tree = self.make_branch_and_tree(".")
1312
1426
        tree.lock_write()
1313
1427
        try: