/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5128.1.1 by Vincent Ladeuil
Uncontroversial cleanups, mostly comments
1
# Copyright (C) 2006-2010 Canonical Ltd
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
2
# Authors:  Robert Collins <robert.collins@canonical.com>
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
17
18
import os
19
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
20
from breezy import (
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
21
    branch,
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
22
    conflicts,
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
23
    controldir,
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
24
    errors,
3335.1.3 by Jelmer Vernooij
Add tests for start_commit hook.
25
    mutabletree,
2922.2.1 by John Arbash Meinel
Add failing tests exposing part of bug #114615
26
    osutils,
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
27
    revision as _mod_revision,
5777.2.1 by Jelmer Vernooij
Skip commit exclude tests for repositories that don't support excludes.
28
    tests,
6437.70.1 by John Arbash Meinel
Set up the infrastructure to start testing a lightweight checkout of a remote repository.
29
    transport as _mod_transport,
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
30
    ui,
31
    )
6734.1.20 by Jelmer Vernooij
Move errors.
32
from breezy.commit import (
33
    CannotCommitSelectedFileMerge,
34
    PointlessCommit,
35
    )
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
36
from breezy.tests.matchers import HasPathRelations
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
37
from breezy.tests.per_workingtree import TestCaseWithWorkingTree
38
from breezy.tests.testui import ProgressRecordingUIFactory
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
39
40
41
class TestCommit(TestCaseWithWorkingTree):
42
2922.2.1 by John Arbash Meinel
Add failing tests exposing part of bug #114615
43
    def test_autodelete_renamed(self):
44
        tree_a = self.make_branch_and_tree('a')
45
        self.build_tree(['a/dir/', 'a/dir/f1', 'a/dir/f2'])
6745.1.1 by Jelmer Vernooij
Avoid explicitly setting file ids or guard it by checking
46
        tree_a.add(['dir', 'dir/f1', 'dir/f2'])
2922.2.1 by John Arbash Meinel
Add failing tests exposing part of bug #114615
47
        rev_id1 = tree_a.commit('init')
48
        # Start off by renaming entries,
49
        # but then actually auto delete the whole tree
50
        # https://bugs.launchpad.net/bzr/+bug/114615
51
        tree_a.rename_one('dir/f1', 'dir/a')
52
        tree_a.rename_one('dir/f2', 'dir/z')
53
        osutils.rmtree('a/dir')
54
        tree_a.commit('autoremoved')
55
7358.14.1 by Jelmer Vernooij
Remove Tree.get_root_id() in favour of Tree.path2id('').
56
        with tree_a.lock_read():
57
            root_id = tree_a.path2id('')
2922.2.1 by John Arbash Meinel
Add failing tests exposing part of bug #114615
58
            paths = [(path, ie.file_id)
59
                     for path, ie in tree_a.iter_entries_by_dir()]
60
        # The only paths left should be the root
61
        self.assertEqual([('', root_id)], paths)
62
2922.2.3 by John Arbash Meinel
Add a test which shows why the previous fix is broken.
63
    def test_no_autodelete_renamed_away(self):
64
        tree_a = self.make_branch_and_tree('a')
65
        self.build_tree(['a/dir/', 'a/dir/f1', 'a/dir/f2', 'a/dir2/'])
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
66
        tree_a.add(['dir', 'dir/f1', 'dir/f2', 'dir2'])
2922.2.3 by John Arbash Meinel
Add a test which shows why the previous fix is broken.
67
        rev_id1 = tree_a.commit('init')
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
68
        revtree = tree_a.branch.repository.revision_tree(rev_id1)
2922.2.3 by John Arbash Meinel
Add a test which shows why the previous fix is broken.
69
        # Rename one entry out of this directory
70
        tree_a.rename_one('dir/f1', 'dir2/a')
71
        osutils.rmtree('a/dir')
72
        tree_a.commit('autoremoved')
73
74
        # The only paths left should be the root
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
75
        self.assertThat(
76
            tree_a, HasPathRelations(
77
                revtree,
78
                [('', ''), ('dir2/', 'dir2/'), ('dir2/a', 'dir/f1')]))
2922.2.3 by John Arbash Meinel
Add a test which shows why the previous fix is broken.
79
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
80
    def test_no_autodelete_alternate_renamed(self):
81
        # Test for bug #114615
82
        tree_a = self.make_branch_and_tree('A')
83
        self.build_tree(['A/a/', 'A/a/m', 'A/a/n'])
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
84
        tree_a.add(['a', 'a/m', 'a/n'])
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
85
        tree_a.commit('init')
86
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
87
        tree_b = tree_a.controldir.sprout('B').open_workingtree()
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
88
        self.build_tree(['B/xyz/'])
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
89
        tree_b.add(['xyz'])
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
90
        tree_b.rename_one('a/m', 'xyz/m')
91
        osutils.rmtree('B/a')
92
        tree_b.commit('delete in B')
93
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
94
        self.assertThat(
7143.15.2 by Jelmer Vernooij
Run autopep8.
95
            tree_b,
96
            HasPathRelations(
97
                tree_a, [('', ''), ('xyz/', None), ('xyz/m', 'a/m')]))
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
98
6855.4.1 by Jelmer Vernooij
Yet more bees.
99
        self.build_tree_contents([('A/a/n', b'new contents for n\n')])
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
100
        tree_a.commit('change n in A')
101
102
        # Merging from A should introduce conflicts because 'n' was modified
5128.1.1 by Vincent Ladeuil
Uncontroversial cleanups, mostly comments
103
        # (in A) and removed (in B), so 'a' needs to be restored.
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
104
        num_conflicts = tree_b.merge_from_branch(tree_a.branch)
105
        self.assertEqual(3, num_conflicts)
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
106
107
        self.assertThat(
7143.15.2 by Jelmer Vernooij
Run autopep8.
108
            tree_b, HasPathRelations(
109
                tree_a,
110
                [('', ''), ('a/', 'a/'), ('xyz/', None),
111
                 ('a/n.OTHER', 'a/n'), ('xyz/m', 'a/m')]))
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
112
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
113
        osutils.rmtree('B/a')
114
        try:
115
            # bzr resolve --all
116
            tree_b.set_conflicts(conflicts.ConflictList())
117
        except errors.UnsupportedOperation:
118
            # On WT2, set_conflicts is unsupported, but the rmtree has the same
119
            # effect.
120
            pass
121
        tree_b.commit('autoremove a, without touching xyz/m')
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
122
123
        self.assertThat(
7143.15.2 by Jelmer Vernooij
Run autopep8.
124
            tree_b, HasPathRelations(
125
                tree_a,
126
                [('', ''), ('xyz/', None), ('xyz/m', 'a/m')]))
2922.2.4 by John Arbash Meinel
Fix bug #114615 by teaching unversion() to not touch renamed entries.
127
3602.1.1 by Robert Collins
Add support for -x or --exclude to bzr commit, fixing bug 3117. (Robert Collins)
128
    def test_commit_exclude_pending_merge_fails(self):
129
        """Excludes are a form of partial commit."""
130
        wt = self.make_branch_and_tree('.')
131
        self.build_tree(['foo'])
132
        wt.add('foo')
133
        wt.commit('commit one')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
134
        wt2 = wt.controldir.sprout('to').open_workingtree()
3602.1.1 by Robert Collins
Add support for -x or --exclude to bzr commit, fixing bug 3117. (Robert Collins)
135
        wt2.commit('change_right')
136
        wt.merge_from_branch(wt2.branch)
6747.1.1 by Jelmer Vernooij
Remove unnecessary ExcludesUnsupported exception.
137
        self.assertRaises(CannotCommitSelectedFileMerge,
7143.15.2 by Jelmer Vernooij
Run autopep8.
138
                          wt.commit, 'test', exclude=['foo'])
3602.1.1 by Robert Collins
Add support for -x or --exclude to bzr commit, fixing bug 3117. (Robert Collins)
139
3602.1.2 by Robert Collins
Review feedback : test for PointlessCommit and that the example given in the help (excluding a subtree of a specified tree) does in fact work.
140
    def test_commit_exclude_exclude_changed_is_pointless(self):
141
        tree = self.make_branch_and_tree('.')
142
        self.build_tree(['a'])
143
        tree.smart_add(['.'])
144
        tree.commit('setup test')
6855.4.1 by Jelmer Vernooij
Yet more bees.
145
        self.build_tree_contents([('a', b'new contents for "a"\n')])
6747.1.1 by Jelmer Vernooij
Remove unnecessary ExcludesUnsupported exception.
146
        self.assertRaises(PointlessCommit, tree.commit, 'test',
7143.15.2 by Jelmer Vernooij
Run autopep8.
147
                          exclude=['a'], allow_pointless=False)
3602.1.2 by Robert Collins
Review feedback : test for PointlessCommit and that the example given in the help (excluding a subtree of a specified tree) does in fact work.
148
3602.1.1 by Robert Collins
Add support for -x or --exclude to bzr commit, fixing bug 3117. (Robert Collins)
149
    def test_commit_exclude_excludes_modified_files(self):
150
        tree = self.make_branch_and_tree('.')
151
        self.build_tree(['a', 'b', 'c'])
152
        tree.smart_add(['.'])
6747.1.1 by Jelmer Vernooij
Remove unnecessary ExcludesUnsupported exception.
153
        tree.commit('test', exclude=['b', 'c'])
3602.1.4 by Robert Collins
Andrew's review feedback.
154
        # If b was excluded it will still be 'added' in status.
3602.1.1 by Robert Collins
Add support for -x or --exclude to bzr commit, fixing bug 3117. (Robert Collins)
155
        tree.lock_read()
156
        self.addCleanup(tree.unlock)
157
        changes = list(tree.iter_changes(tree.basis_tree()))
7358.16.1 by Jelmer Vernooij
Don't make TreeChange tuple-like objects anymore, so we can add and remove attributes as necessary.
158
        self.assertEqual([(None, 'b'), (None, 'c')], [c.path for c in changes])
3602.1.1 by Robert Collins
Add support for -x or --exclude to bzr commit, fixing bug 3117. (Robert Collins)
159
3602.1.2 by Robert Collins
Review feedback : test for PointlessCommit and that the example given in the help (excluding a subtree of a specified tree) does in fact work.
160
    def test_commit_exclude_subtree_of_selected(self):
161
        tree = self.make_branch_and_tree('.')
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
162
        self.build_tree(['a/', 'a/b', 'a/c'])
3602.1.2 by Robert Collins
Review feedback : test for PointlessCommit and that the example given in the help (excluding a subtree of a specified tree) does in fact work.
163
        tree.smart_add(['.'])
6913.5.2 by Jelmer Vernooij
Use HasPathRelations.
164
        tree.commit('test', specific_files=['a', 'a/c'], exclude=['a/b'])
3602.1.4 by Robert Collins
Andrew's review feedback.
165
        # If a/b was excluded it will still be 'added' in status.
3602.1.2 by Robert Collins
Review feedback : test for PointlessCommit and that the example given in the help (excluding a subtree of a specified tree) does in fact work.
166
        tree.lock_read()
167
        self.addCleanup(tree.unlock)
168
        changes = list(tree.iter_changes(tree.basis_tree()))
169
        self.assertEqual(1, len(changes))
7358.16.1 by Jelmer Vernooij
Don't make TreeChange tuple-like objects anymore, so we can add and remove attributes as necessary.
170
        self.assertEqual((None, 'a/b'), changes[0].path)
3602.1.2 by Robert Collins
Review feedback : test for PointlessCommit and that the example given in the help (excluding a subtree of a specified tree) does in fact work.
171
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
172
    def test_commit_sets_last_revision(self):
173
        tree = self.make_branch_and_tree('tree')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
174
        if tree.branch.repository._format.supports_setting_revision_ids:
6855.4.1 by Jelmer Vernooij
Yet more bees.
175
            committed_id = tree.commit('foo', rev_id=b'foo')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
176
            # the commit should have returned the same id we asked for.
7045.1.14 by Jelmer Vernooij
More fixes.
177
            self.assertEqual(b'foo', committed_id)
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
178
        else:
179
            committed_id = tree.commit('foo')
180
        self.assertEqual([committed_id], tree.get_parent_ids())
1773.1.1 by Robert Collins
Teach WorkingTree.commit to return the committed revision id.
181
182
    def test_commit_returns_revision_id(self):
183
        tree = self.make_branch_and_tree('.')
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
184
        committed_id = tree.commit('message')
1773.1.1 by Robert Collins
Teach WorkingTree.commit to return the committed revision id.
185
        self.assertTrue(tree.branch.repository.has_revision(committed_id))
186
        self.assertNotEqual(None, committed_id)
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
187
188
    def test_commit_local_unbound(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
189
        # using the library api to do a local commit on unbound branches is
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
190
        # also an error
191
        tree = self.make_branch_and_tree('tree')
192
        self.assertRaises(errors.LocalRequiresBoundBranch,
193
                          tree.commit,
194
                          'foo',
195
                          local=True)
2374.2.1 by John Arbash Meinel
(broken) merge a test case showing that commiting a merge of a kind change fails.
196
197
    def test_commit_merged_kind_change(self):
198
        """Test merging a kind change.
199
200
        Test making a kind change in a working tree, and then merging that
201
        from another. When committed it should commit the new kind.
202
        """
203
        wt = self.make_branch_and_tree('.')
204
        self.build_tree(['a'])
205
        wt.add(['a'])
206
        wt.commit('commit one')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
207
        wt2 = wt.controldir.sprout('to').open_workingtree()
2374.2.1 by John Arbash Meinel
(broken) merge a test case showing that commiting a merge of a kind change fails.
208
        os.remove('a')
209
        os.mkdir('a')
210
        wt.commit('changed kind')
211
        wt2.merge_from_branch(wt.branch)
212
        wt2.commit('merged kind change')
213
4536.3.1 by Robert Collins
Defer doing unversioning of file ids during commit to after completing branch operations. (Robert Collins, bug 282402)
214
    def test_commit_aborted_does_not_apply_automatic_changes_bug_282402(self):
215
        wt = self.make_branch_and_tree('.')
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
216
        wt.add(['a'], None, ['file'])
7391.3.1 by Jelmer Vernooij
Use id2path in fewer places.
217
        self.assertTrue(wt.is_versioned('a'))
218
        if wt.supports_setting_file_ids():
219
            a_id = wt.path2id('a')
220
            self.assertEqual('a', wt.id2path(a_id))
7143.15.2 by Jelmer Vernooij
Run autopep8.
221
4536.3.1 by Robert Collins
Defer doing unversioning of file ids during commit to after completing branch operations. (Robert Collins, bug 282402)
222
        def fail_message(obj):
7490.61.1 by Jelmer Vernooij
Rename BzrCommandError to CommandError.
223
            raise errors.CommandError("empty commit message")
224
        self.assertRaises(errors.CommandError, wt.commit,
7143.15.2 by Jelmer Vernooij
Run autopep8.
225
                          message_callback=fail_message)
7391.3.1 by Jelmer Vernooij
Use id2path in fewer places.
226
        self.assertTrue(wt.is_versioned('a'))
227
        if wt.supports_setting_file_ids():
228
            self.assertEqual('a', wt.id2path(a_id))
4536.3.1 by Robert Collins
Defer doing unversioning of file ids during commit to after completing branch operations. (Robert Collins, bug 282402)
229
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
230
    def test_local_commit_ignores_master(self):
231
        # a --local commit does not require access to the master branch
232
        # at all, or even for it to exist.
233
        # we test this by setting up a bound branch and then corrupting
234
        # the master.
235
        master = self.make_branch('master')
236
        tree = self.make_branch_and_tree('tree')
237
        try:
238
            tree.branch.bind(master)
239
        except errors.UpgradeRequired:
240
            # older format.
241
            return
7027.3.3 by Jelmer Vernooij
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.
242
        master.controldir.transport.put_bytes('branch-format', b'garbage')
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
243
        del master
244
        # check its corrupted.
245
        self.assertRaises(errors.UnknownFormatError,
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
246
                          controldir.ControlDir.open,
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
247
                          'master')
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
248
        tree.commit('foo', local=True)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
249
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
250
    def test_local_commit_does_not_push_to_master(self):
251
        # a --local commit does not require access to the master branch
252
        # at all, or even for it to exist.
253
        # we test that even when its available it does not push to it.
254
        master = self.make_branch('master')
255
        tree = self.make_branch_and_tree('tree')
256
        try:
257
            tree.branch.bind(master)
258
        except errors.UpgradeRequired:
259
            # older format.
260
            return
6747.3.1 by Jelmer Vernooij
Avoid more uses of revision_id.
261
        committed_id = tree.commit('foo', local=True)
262
        self.assertFalse(master.repository.has_revision(committed_id))
2598.5.7 by Aaron Bentley
Updates from review
263
        self.assertEqual(_mod_revision.NULL_REVISION,
264
                         (_mod_revision.ensure_null(master.last_revision())))
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
265
266
    def test_record_initial_ghost(self):
267
        """The working tree needs to record ghosts during commit."""
268
        wt = self.make_branch_and_tree('.')
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
269
        if not wt.branch.repository._format.supports_ghosts:
270
            raise tests.TestNotApplicable(
271
                'format does not support ghosts')
6973.10.6 by Jelmer Vernooij
Fix tests.
272
        wt.set_parent_ids([b'non:existent@rev--ision--0--2'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
273
                          allow_leftmost_as_ghost=True)
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
274
        rev_id = wt.commit('commit against a ghost first parent.')
275
        rev = wt.branch.repository.get_revision(rev_id)
6973.10.6 by Jelmer Vernooij
Fix tests.
276
        self.assertEqual(rev.parent_ids, [b'non:existent@rev--ision--0--2'])
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
277
        # parent_sha1s is not populated now, WTF. rbc 20051003
278
        self.assertEqual(len(rev.parent_sha1s), 0)
279
280
    def test_record_two_ghosts(self):
281
        """The working tree should preserve all the parents during commit."""
282
        wt = self.make_branch_and_tree('.')
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
283
        if not wt.branch.repository._format.supports_ghosts:
284
            raise tests.TestNotApplicable(
285
                'format does not support ghosts')
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
286
        wt.set_parent_ids([
7143.15.2 by Jelmer Vernooij
Run autopep8.
287
            b'foo@azkhazan-123123-abcabc',
288
            b'wibble@fofof--20050401--1928390812',
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
289
            ],
290
            allow_leftmost_as_ghost=True)
1927.2.1 by Robert Collins
Alter set_pending_merges to shove the left most merge into the trees last-revision if that is not set. Related bugfixes include basis_tree handling ghosts, de-duping the merges with the last-revision and update changing where and how it adds its pending merge.
291
        rev_id = wt.commit("commit from ghost base with one merge")
292
        # the revision should have been committed with two parents
293
        rev = wt.branch.repository.get_revision(rev_id)
6973.10.6 by Jelmer Vernooij
Fix tests.
294
        self.assertEqual([b'foo@azkhazan-123123-abcabc',
7143.15.2 by Jelmer Vernooij
Run autopep8.
295
                          b'wibble@fofof--20050401--1928390812'],
296
                         rev.parent_ids)
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
297
1988.3.1 by Robert Collins
Add test case to ensure that the working tree inventory and disk state is correctly update when commit is removing directory entries.
298
    def test_commit_deleted_subtree_and_files_updates_workingtree(self):
299
        """The working trees inventory may be adjusted by commit."""
300
        wt = self.make_branch_and_tree('.')
301
        wt.lock_write()
302
        self.build_tree(['a', 'b/', 'b/c', 'd'])
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
303
        wt.add(['a', 'b', 'b/c', 'd'])
304
        a_id = wt.path2id('a')
305
        b_id = wt.path2id('b')
306
        c_id = wt.path2id('b/c')
307
        d_id = wt.path2id('d')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
308
        this_dir = wt.controldir.root_transport
1988.3.1 by Robert Collins
Add test case to ensure that the working tree inventory and disk state is correctly update when commit is removing directory entries.
309
        this_dir.delete_tree('b')
310
        this_dir.delete('d')
311
        # now we have a tree with a through d in the inventory, but only
312
        # a present on disk. After commit b-id, c-id and d-id should be
313
        # missing from the inventory, within the same tree transaction.
314
        wt.commit('commit stuff')
315
        self.assertTrue(wt.has_filename('a'))
316
        self.assertFalse(wt.has_filename('b'))
317
        self.assertFalse(wt.has_filename('b/c'))
318
        self.assertFalse(wt.has_filename('d'))
319
        wt.unlock()
320
        # the changes should have persisted to disk - reopen the workingtree
321
        # to be sure.
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
322
        wt = wt.controldir.open_workingtree()
7397.4.4 by Jelmer Vernooij
Allow has_id to be unsupported.
323
        with wt.lock_read():
324
            self.assertTrue(wt.has_filename('a'))
325
            self.assertFalse(wt.has_filename('b'))
326
            self.assertFalse(wt.has_filename('b/c'))
327
            self.assertFalse(wt.has_filename('d'))
1731.2.4 by Aaron Bentley
Ensure subsume works with Knit2 repos
328
2363.2.2 by John Arbash Meinel
Simplify the test even further....
329
    def test_commit_deleted_subtree_with_removed(self):
2363.2.1 by John Arbash Meinel
(broken) Add a simplified test which exposes the bug.
330
        wt = self.make_branch_and_tree('.')
331
        self.build_tree(['a', 'b/', 'b/c', 'd'])
6844.1.1 by Jelmer Vernooij
Many more foreign branch fixes.
332
        wt.add(['a', 'b', 'b/c'])
333
        a_id = wt.path2id('a')
334
        b_id = wt.path2id('b')
335
        c_id = wt.path2id('b/c')
2363.2.1 by John Arbash Meinel
(broken) Add a simplified test which exposes the bug.
336
        wt.commit('first')
2363.2.2 by John Arbash Meinel
Simplify the test even further....
337
        wt.remove('b/c')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
338
        this_dir = wt.controldir.root_transport
2363.2.1 by John Arbash Meinel
(broken) Add a simplified test which exposes the bug.
339
        this_dir.delete_tree('b')
7397.4.4 by Jelmer Vernooij
Allow has_id to be unsupported.
340
        with wt.lock_write():
341
            wt.commit('commit deleted rename')
342
            self.assertTrue(wt.is_versioned('a'))
343
            self.assertTrue(wt.has_filename('a'))
344
            self.assertFalse(wt.has_filename('b'))
345
            self.assertFalse(wt.has_filename('b/c'))
2363.2.1 by John Arbash Meinel
(broken) Add a simplified test which exposes the bug.
346
1731.2.4 by Aaron Bentley
Ensure subsume works with Knit2 repos
347
    def test_commit_move_new(self):
348
        wt = self.make_branch_and_tree('first')
349
        wt.commit('first')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
350
        wt2 = wt.controldir.sprout('second').open_workingtree()
1731.2.4 by Aaron Bentley
Ensure subsume works with Knit2 repos
351
        self.build_tree(['second/name1'])
6745.1.1 by Jelmer Vernooij
Avoid explicitly setting file ids or guard it by checking
352
        wt2.add('name1')
1731.2.4 by Aaron Bentley
Ensure subsume works with Knit2 repos
353
        wt2.commit('second')
354
        wt.merge_from_branch(wt2.branch)
355
        wt.rename_one('name1', 'name2')
356
        wt.commit('third')
6745.1.1 by Jelmer Vernooij
Avoid explicitly setting file ids or guard it by checking
357
        wt.path2id('name1')
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
358
359
    def test_nested_commit(self):
360
        """Commit in multiply-nested trees"""
361
        tree = self.make_branch_and_tree('.')
362
        if not tree.supports_tree_reference():
363
            # inapplicable test.
364
            return
365
        subtree = self.make_branch_and_tree('subtree')
366
        subsubtree = self.make_branch_and_tree('subtree/subtree')
6926.2.8 by Jelmer Vernooij
Fix some more tests.
367
        subsub_revid = subsubtree.commit('subsubtree')
368
        subtree.commit('subtree')
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
369
        subtree.add(['subtree'])
370
        tree.add(['subtree'])
371
        # use allow_pointless=False to ensure that the deepest tree, which
372
        # has no commits made to it, does not get a pointless commit.
373
        rev_id = tree.commit('added reference', allow_pointless=False)
374
        tree.lock_read()
375
        self.addCleanup(tree.unlock)
376
        # the deepest subtree has not changed, so no commit should take place.
6926.2.8 by Jelmer Vernooij
Fix some more tests.
377
        self.assertEqual(subsub_revid, subsubtree.last_revision())
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
378
        # the intermediate tree should have committed a pointer to the current
379
        # subtree revision.
380
        sub_basis = subtree.basis_tree()
381
        sub_basis.lock_read()
382
        self.addCleanup(sub_basis.unlock)
6809.4.1 by Jelmer Vernooij
Swap file_id and path arguments for get_reference_revision and get_nested_tree.
383
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
384
            subsubtree.last_revision(),
385
            sub_basis.get_reference_revision('subtree'))
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
386
        # the intermediate tree has changed, so should have had a commit
387
        # take place.
388
        self.assertNotEqual(None, subtree.last_revision())
389
        # the outer tree should have committed a pointer to the current
390
        # subtree revision.
391
        basis = tree.basis_tree()
392
        basis.lock_read()
393
        self.addCleanup(basis.unlock)
394
        self.assertEqual(subtree.last_revision(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
395
                         basis.get_reference_revision('subtree'))
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
396
        # the outer tree must have have changed too.
397
        self.assertNotEqual(None, rev_id)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
398
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
399
    def test_nested_commit_second_commit_detects_changes(self):
400
        """Commit with a nested tree picks up the correct child revid."""
401
        tree = self.make_branch_and_tree('.')
402
        if not tree.supports_tree_reference():
403
            # inapplicable test.
404
            return
6700.1.3 by Jelmer Vernooij
Drop support for committing using record_entr_contents.
405
        self.knownFailure('nested trees don\'t work well with iter_changes')
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
406
        subtree = self.make_branch_and_tree('subtree')
407
        tree.add(['subtree'])
408
        self.build_tree(['subtree/file'])
6745.1.1 by Jelmer Vernooij
Avoid explicitly setting file ids or guard it by checking
409
        subtree.add(['file'])
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
410
        rev_id = tree.commit('added reference', allow_pointless=False)
6809.4.1 by Jelmer Vernooij
Swap file_id and path arguments for get_reference_revision and get_nested_tree.
411
        tree.get_reference_revision('subtree')
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
412
        child_revid = subtree.last_revision()
413
        # now change the child tree
6855.4.1 by Jelmer Vernooij
Yet more bees.
414
        self.build_tree_contents([('subtree/file', b'new-content')])
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
415
        # and commit in the parent should commit the child and grab its revid,
416
        # we test with allow_pointless=False here so that we are simulating
417
        # what users will see.
418
        rev_id2 = tree.commit('changed subtree only', allow_pointless=False)
419
        # the child tree has changed, so should have had a commit
420
        # take place.
421
        self.assertNotEqual(None, subtree.last_revision())
422
        self.assertNotEqual(child_revid, subtree.last_revision())
423
        # the outer tree should have committed a pointer to the current
424
        # subtree revision.
425
        basis = tree.basis_tree()
426
        basis.lock_read()
427
        self.addCleanup(basis.unlock)
428
        self.assertEqual(subtree.last_revision(),
7143.15.2 by Jelmer Vernooij
Run autopep8.
429
                         basis.get_reference_revision('subtree'))
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
430
        self.assertNotEqual(rev_id, rev_id2)
431
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
432
    def test_nested_pointless_commits_are_pointless(self):
433
        tree = self.make_branch_and_tree('.')
434
        if not tree.supports_tree_reference():
435
            # inapplicable test.
436
            return
437
        subtree = self.make_branch_and_tree('subtree')
6926.2.8 by Jelmer Vernooij
Fix some more tests.
438
        subtree.commit('')
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
439
        tree.add(['subtree'])
440
        # record the reference.
441
        rev_id = tree.commit('added reference')
442
        child_revid = subtree.last_revision()
443
        # now do a no-op commit with allow_pointless=False
6734.1.20 by Jelmer Vernooij
Move errors.
444
        self.assertRaises(PointlessCommit, tree.commit, '',
7143.15.2 by Jelmer Vernooij
Run autopep8.
445
                          allow_pointless=False)
2825.5.2 by Robert Collins
Review feedback, and fix pointless commits with nested trees to raise PointlessCommit appropriately.
446
        self.assertEqual(child_revid, subtree.last_revision())
447
        self.assertEqual(rev_id, tree.last_revision())
448
1988.3.1 by Robert Collins
Add test case to ensure that the working tree inventory and disk state is correctly update when commit is removing directory entries.
449
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
450
class TestCommitProgress(TestCaseWithWorkingTree):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
451
4985.2.1 by Vincent Ladeuil
Deploy addAttrCleanup on the whole test suite.
452
    def setUp(self):
453
        super(TestCommitProgress, self).setUp()
5422.1.4 by Martin Pool
Rename CapturingUIFactory to ProgressRecordingUIFactory
454
        ui.ui_factory = ProgressRecordingUIFactory()
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
455
456
    def test_commit_progress_steps(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
457
        # during commit we one progress update for every entry in the
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
458
        # inventory, and then one for the inventory, and one for the
459
        # inventory, and one for the revision insertions.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
460
        # first we need a test commit to do. Lets setup a branch with
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
461
        # 3 files, and alter one in a selected-file commit. This exercises
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
462
        # a number of cases quickly. We should also test things like
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
463
        # selective commits which excludes newly added files.
464
        tree = self.make_branch_and_tree('.')
465
        self.build_tree(['a', 'b', 'c'])
466
        tree.add(['a', 'b', 'c'])
467
        tree.commit('first post')
6973.7.5 by Jelmer Vernooij
s/file/open.
468
        with open('b', 'wt') as f:
469
            f.write('new content')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
470
        # set a progress bar that captures the calls so we can see what is
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
471
        # emitted
5422.1.4 by Martin Pool
Rename CapturingUIFactory to ProgressRecordingUIFactory
472
        factory = ProgressRecordingUIFactory()
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
473
        ui.ui_factory = factory
474
        # TODO RBC 20060421 it would be nice to merge the reporter output
475
        # into the factory for this test - just make the test ui factory
476
        # pun as a reporter. Then we can check the ordering is right.
477
        tree.commit('second post', specific_files=['b'])
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
478
        # 5 steps, the first of which is reported 2 times, once per dir
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
479
        self.assertEqual(
4183.5.5 by Robert Collins
Enable record_iter_changes for cases where it can work.
480
            [('update', 1, 5, 'Collecting changes [0] - Stage'),
481
             ('update', 1, 5, 'Collecting changes [1] - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
482
             ('update', 2, 5, 'Saving data locally - Stage'),
2659.3.9 by NamNguyen
branch.py:
483
             ('update', 3, 5, 'Running pre_commit hooks - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
484
             ('update', 4, 5, 'Updating the working tree - Stage'),
2659.3.9 by NamNguyen
branch.py:
485
             ('update', 5, 5, 'Running post_commit hooks - Stage')],
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
486
            factory._calls
7143.15.2 by Jelmer Vernooij
Run autopep8.
487
            )
2553.1.2 by Robert Collins
Show hook names during commit.
488
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
489
    def test_commit_progress_shows_post_hook_names(self):
2553.1.2 by Robert Collins
Show hook names during commit.
490
        tree = self.make_branch_and_tree('.')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
491
        # set a progress bar that captures the calls so we can see what is
2553.1.2 by Robert Collins
Show hook names during commit.
492
        # emitted
5422.1.4 by Martin Pool
Rename CapturingUIFactory to ProgressRecordingUIFactory
493
        factory = ProgressRecordingUIFactory()
2553.1.2 by Robert Collins
Show hook names during commit.
494
        ui.ui_factory = factory
7143.15.2 by Jelmer Vernooij
Run autopep8.
495
2553.1.2 by Robert Collins
Show hook names during commit.
496
        def a_hook(_, _2, _3, _4, _5, _6):
497
            pass
3256.2.19 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.workingtree_implementations.test_commit.
498
        branch.Branch.hooks.install_named_hook('post_commit', a_hook,
499
                                               'hook name')
2553.1.2 by Robert Collins
Show hook names during commit.
500
        tree.commit('first post')
501
        self.assertEqual(
4183.5.5 by Robert Collins
Enable record_iter_changes for cases where it can work.
502
            [('update', 1, 5, 'Collecting changes [0] - Stage'),
503
             ('update', 1, 5, 'Collecting changes [1] - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
504
             ('update', 2, 5, 'Saving data locally - Stage'),
2659.3.9 by NamNguyen
branch.py:
505
             ('update', 3, 5, 'Running pre_commit hooks - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
506
             ('update', 4, 5, 'Updating the working tree - Stage'),
2659.3.9 by NamNguyen
branch.py:
507
             ('update', 5, 5, 'Running post_commit hooks - Stage'),
508
             ('update', 5, 5, 'Running post_commit hooks [hook name] - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
509
             ],
510
            factory._calls
7143.15.2 by Jelmer Vernooij
Run autopep8.
511
            )
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
512
513
    def test_commit_progress_shows_pre_hook_names(self):
514
        tree = self.make_branch_and_tree('.')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
515
        # set a progress bar that captures the calls so we can see what is
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
516
        # emitted
5422.1.4 by Martin Pool
Rename CapturingUIFactory to ProgressRecordingUIFactory
517
        factory = ProgressRecordingUIFactory()
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
518
        ui.ui_factory = factory
7143.15.2 by Jelmer Vernooij
Run autopep8.
519
2659.3.3 by NamNguyen
Changed ``pre_commit`` hook signature.
520
        def a_hook(_, _2, _3, _4, _5, _6, _7, _8):
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
521
            pass
3256.2.19 by Daniel Watkins
Updated uses of Hooks.install_hook to Hooks.install_named_hook in tests.workingtree_implementations.test_commit.
522
        branch.Branch.hooks.install_named_hook('pre_commit', a_hook,
523
                                               'hook name')
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
524
        tree.commit('first post')
525
        self.assertEqual(
4183.5.5 by Robert Collins
Enable record_iter_changes for cases where it can work.
526
            [('update', 1, 5, 'Collecting changes [0] - Stage'),
527
             ('update', 1, 5, 'Collecting changes [1] - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
528
             ('update', 2, 5, 'Saving data locally - Stage'),
2659.3.9 by NamNguyen
branch.py:
529
             ('update', 3, 5, 'Running pre_commit hooks - Stage'),
530
             ('update', 3, 5, 'Running pre_commit hooks [hook name] - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
531
             ('update', 4, 5, 'Updating the working tree - Stage'),
2659.3.9 by NamNguyen
branch.py:
532
             ('update', 5, 5, 'Running post_commit hooks - Stage'),
2659.3.1 by NamNguyen
``Branch.hooks`` now supports ``pre_commit`` hook.
533
             ],
534
            factory._calls
7143.15.2 by Jelmer Vernooij
Run autopep8.
535
            )
3335.1.3 by Jelmer Vernooij
Add tests for start_commit hook.
536
537
    def test_start_commit_hook(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
538
        """Make sure a start commit hook can modify the tree that is
3335.1.3 by Jelmer Vernooij
Add tests for start_commit hook.
539
        committed."""
540
        def start_commit_hook_adds_file(tree):
7143.15.2 by Jelmer Vernooij
Run autopep8.
541
            with open(tree.abspath("newfile"), 'w') as f:
542
                f.write("data")
3335.1.3 by Jelmer Vernooij
Add tests for start_commit hook.
543
            tree.add(["newfile"])
7143.15.2 by Jelmer Vernooij
Run autopep8.
544
3335.1.3 by Jelmer Vernooij
Add tests for start_commit hook.
545
        def restoreDefaults():
546
            mutabletree.MutableTree.hooks['start_commit'] = []
547
        self.addCleanup(restoreDefaults)
548
        tree = self.make_branch_and_tree('.')
3256.2.26 by Daniel Watkins
Updated tests to use install_named_hook.
549
        mutabletree.MutableTree.hooks.install_named_hook(
550
            'start_commit',
551
            start_commit_hook_adds_file,
552
            None)
3335.1.3 by Jelmer Vernooij
Add tests for start_commit hook.
553
        revid = tree.commit('first post')
554
        committed_tree = tree.basis_tree()
555
        self.assertTrue(committed_tree.has_filename("newfile"))
4634.33.1 by Ian Clatworthy
original finish_commit hook patch
556
4634.33.3 by Ian Clatworthy
review feedback from Robert: rename finish_commit to post_commit
557
    def test_post_commit_hook(self):
558
        """Make sure a post_commit hook is called after a commit."""
559
        def post_commit_hook_test_params(params):
4634.33.2 by Ian Clatworthy
review feedback from jam
560
            self.assertTrue(isinstance(params,
7143.15.2 by Jelmer Vernooij
Run autopep8.
561
                                       mutabletree.PostCommitHookParams))
4634.33.2 by Ian Clatworthy
review feedback from jam
562
            self.assertTrue(isinstance(params.mutable_tree,
7143.15.2 by Jelmer Vernooij
Run autopep8.
563
                                       mutabletree.MutableTree))
564
            with open(tree.abspath("newfile"), 'w') as f:
565
                f.write("data")
4634.33.2 by Ian Clatworthy
review feedback from jam
566
            params.mutable_tree.add(["newfile"])
4634.33.1 by Ian Clatworthy
original finish_commit hook patch
567
        tree = self.make_branch_and_tree('.')
568
        mutabletree.MutableTree.hooks.install_named_hook(
4634.33.3 by Ian Clatworthy
review feedback from Robert: rename finish_commit to post_commit
569
            'post_commit',
570
            post_commit_hook_test_params,
4634.33.1 by Ian Clatworthy
original finish_commit hook patch
571
            None)
572
        self.assertFalse(tree.has_filename("newfile"))
573
        revid = tree.commit('first post')
574
        self.assertTrue(tree.has_filename("newfile"))
575
        committed_tree = tree.basis_tree()
576
        self.assertFalse(committed_tree.has_filename("newfile"))