/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
1
# Copyright (C) 2005, 2006 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
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18
from cStringIO import StringIO
19
import os
20
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
21
from bzrlib import (
22
    branch,
23
    bzrdir,
24
    errors,
25
    revision as _mod_revision,
26
    ui,
27
    uncommit,
28
    workingtree,
29
    )
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
30
from bzrlib.errors import (NotBranchError, NotVersionedError, 
31
                           UnsupportedOperation)
32
from bzrlib.osutils import pathjoin, getcwd, has_symlinks
33
from bzrlib.tests import TestSkipped, TestCase
34
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
35
from bzrlib.trace import mutter
36
from bzrlib.workingtree import (TreeEntry, TreeDirectory, TreeFile, TreeLink,
37
                                WorkingTree)
38
39
40
class CapturingUIFactory(ui.UIFactory):
41
    """A UI Factory for testing - capture the updates made through it."""
42
43
    def __init__(self):
44
        super(CapturingUIFactory, self).__init__()
45
        self._calls = []
46
        self.depth = 0
47
48
    def clear(self):
49
        """See progress.ProgressBar.clear()."""
50
51
    def clear_term(self):
52
        """See progress.ProgressBar.clear_term()."""
53
54
    def finished(self):
55
        """See progress.ProgressBar.finished()."""
56
        self.depth -= 1
57
58
    def note(self, fmt_string, *args, **kwargs):
59
        """See progress.ProgressBar.note()."""
60
61
    def progress_bar(self):
62
        return self
63
    
64
    def nested_progress_bar(self):
65
        self.depth += 1
66
        return self
67
68
    def update(self, message, count=None, total=None):
69
        """See progress.ProgressBar.update()."""
70
        if self.depth == 1:
2531.1.3 by Ian Clatworthy
Fix whitespace and improve tests to cover actual progress messages
71
            self._calls.append(("update", count, total, message))
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
72
73
74
class TestCapturingUI(TestCase):
75
76
    def test_nested_ignore_depth_beyond_one(self):
77
        # we only want to capture the first level out progress, not
78
        # want sub-components might do. So we have nested bars ignored.
79
        factory = CapturingUIFactory()
80
        pb1 = factory.nested_progress_bar()
81
        pb1.update('foo', 0, 1)
82
        pb2 = factory.nested_progress_bar()
83
        pb2.update('foo', 0, 1)
84
        pb2.finished()
85
        pb1.finished()
2531.1.3 by Ian Clatworthy
Fix whitespace and improve tests to cover actual progress messages
86
        self.assertEqual([("update", 0, 1, 'foo')], factory._calls)
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
87
88
89
class TestCommit(TestCaseWithWorkingTree):
90
91
    def test_commit_sets_last_revision(self):
92
        tree = self.make_branch_and_tree('tree')
1773.1.1 by Robert Collins
Teach WorkingTree.commit to return the committed revision id.
93
        committed_id = tree.commit('foo', rev_id='foo', allow_pointless=True)
1908.7.6 by Robert Collins
Deprecate WorkingTree.last_revision.
94
        self.assertEqual(['foo'], tree.get_parent_ids())
1773.1.1 by Robert Collins
Teach WorkingTree.commit to return the committed revision id.
95
        # the commit should have returned the same id we asked for.
96
        self.assertEqual('foo', committed_id)
97
98
    def test_commit_returns_revision_id(self):
99
        tree = self.make_branch_and_tree('.')
100
        committed_id = tree.commit('message', allow_pointless=True)
101
        self.assertTrue(tree.branch.repository.has_revision(committed_id))
102
        self.assertNotEqual(None, committed_id)
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
103
104
    def test_commit_local_unbound(self):
105
        # using the library api to do a local commit on unbound branches is 
106
        # also an error
107
        tree = self.make_branch_and_tree('tree')
108
        self.assertRaises(errors.LocalRequiresBoundBranch,
109
                          tree.commit,
110
                          'foo',
111
                          local=True)
2374.2.1 by John Arbash Meinel
(broken) merge a test case showing that commiting a merge of a kind change fails.
112
113
    def test_commit_merged_kind_change(self):
114
        """Test merging a kind change.
115
116
        Test making a kind change in a working tree, and then merging that
117
        from another. When committed it should commit the new kind.
118
        """
119
        wt = self.make_branch_and_tree('.')
120
        self.build_tree(['a'])
121
        wt.add(['a'])
122
        wt.commit('commit one')
123
        wt2 = wt.bzrdir.sprout('to').open_workingtree()
124
        os.remove('a')
125
        os.mkdir('a')
126
        wt.commit('changed kind')
127
        wt2.merge_from_branch(wt.branch)
128
        wt2.commit('merged kind change')
129
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
130
    def test_local_commit_ignores_master(self):
131
        # a --local commit does not require access to the master branch
132
        # at all, or even for it to exist.
133
        # we test this by setting up a bound branch and then corrupting
134
        # the master.
135
        master = self.make_branch('master')
136
        tree = self.make_branch_and_tree('tree')
137
        try:
138
            tree.branch.bind(master)
139
        except errors.UpgradeRequired:
140
            # older format.
141
            return
1955.3.14 by John Arbash Meinel
Correctly fix the workingtree put() test fixes
142
        master.bzrdir.transport.put_bytes('branch-format', 'garbage')
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
143
        del master
144
        # check its corrupted.
145
        self.assertRaises(errors.UnknownFormatError,
146
                          bzrdir.BzrDir.open,
147
                          'master')
148
        tree.commit('foo', rev_id='foo', local=True)
149
 
150
    def test_local_commit_does_not_push_to_master(self):
151
        # a --local commit does not require access to the master branch
152
        # at all, or even for it to exist.
153
        # we test that even when its available it does not push to it.
154
        master = self.make_branch('master')
155
        tree = self.make_branch_and_tree('tree')
156
        try:
157
            tree.branch.bind(master)
158
        except errors.UpgradeRequired:
159
            # older format.
160
            return
161
        tree.commit('foo', rev_id='foo', local=True)
162
        self.failIf(master.repository.has_revision('foo'))
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
163
        self.assertTrue(_mod_revision.is_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.
164
165
    def test_record_initial_ghost(self):
166
        """The working tree needs to record ghosts during commit."""
167
        wt = self.make_branch_and_tree('.')
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
168
        wt.set_parent_ids(['non:existent@rev--ision--0--2'],
169
            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.
170
        rev_id = wt.commit('commit against a ghost first parent.')
171
        rev = wt.branch.repository.get_revision(rev_id)
172
        self.assertEqual(rev.parent_ids, ['non:existent@rev--ision--0--2'])
173
        # parent_sha1s is not populated now, WTF. rbc 20051003
174
        self.assertEqual(len(rev.parent_sha1s), 0)
175
176
    def test_record_two_ghosts(self):
177
        """The working tree should preserve all the parents during commit."""
178
        wt = self.make_branch_and_tree('.')
1908.6.7 by Robert Collins
Remove all users of set_pending_merges and add_pending_merge except tests that they work correctly.
179
        wt.set_parent_ids([
180
                'foo@azkhazan-123123-abcabc',
181
                'wibble@fofof--20050401--1928390812',
182
            ],
183
            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.
184
        rev_id = wt.commit("commit from ghost base with one merge")
185
        # the revision should have been committed with two parents
186
        rev = wt.branch.repository.get_revision(rev_id)
187
        self.assertEqual(['foo@azkhazan-123123-abcabc',
188
            'wibble@fofof--20050401--1928390812'],
189
            rev.parent_ids)
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
190
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.
191
    def test_commit_deleted_subtree_and_files_updates_workingtree(self):
192
        """The working trees inventory may be adjusted by commit."""
193
        wt = self.make_branch_and_tree('.')
194
        wt.lock_write()
195
        self.build_tree(['a', 'b/', 'b/c', 'd'])
196
        wt.add(['a', 'b', 'b/c', 'd'], ['a-id', 'b-id', 'c-id', 'd-id'])
197
        this_dir = self.get_transport()
198
        this_dir.delete_tree('b')
199
        this_dir.delete('d')
200
        # now we have a tree with a through d in the inventory, but only
201
        # a present on disk. After commit b-id, c-id and d-id should be
202
        # missing from the inventory, within the same tree transaction.
203
        wt.commit('commit stuff')
204
        self.assertTrue(wt.has_id('a-id'))
205
        self.assertFalse(wt.has_or_had_id('b-id'))
206
        self.assertFalse(wt.has_or_had_id('c-id'))
207
        self.assertFalse(wt.has_or_had_id('d-id'))
208
        self.assertTrue(wt.has_filename('a'))
209
        self.assertFalse(wt.has_filename('b'))
210
        self.assertFalse(wt.has_filename('b/c'))
211
        self.assertFalse(wt.has_filename('d'))
212
        wt.unlock()
213
        # the changes should have persisted to disk - reopen the workingtree
214
        # to be sure.
215
        wt = wt.bzrdir.open_workingtree()
216
        wt.lock_read()
217
        self.assertTrue(wt.has_id('a-id'))
218
        self.assertFalse(wt.has_or_had_id('b-id'))
219
        self.assertFalse(wt.has_or_had_id('c-id'))
220
        self.assertFalse(wt.has_or_had_id('d-id'))
221
        self.assertTrue(wt.has_filename('a'))
222
        self.assertFalse(wt.has_filename('b'))
223
        self.assertFalse(wt.has_filename('b/c'))
224
        self.assertFalse(wt.has_filename('d'))
225
        wt.unlock()
1731.2.4 by Aaron Bentley
Ensure subsume works with Knit2 repos
226
2363.2.2 by John Arbash Meinel
Simplify the test even further....
227
    def test_commit_deleted_subtree_with_removed(self):
2363.2.1 by John Arbash Meinel
(broken) Add a simplified test which exposes the bug.
228
        wt = self.make_branch_and_tree('.')
229
        self.build_tree(['a', 'b/', 'b/c', 'd'])
230
        wt.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
231
        wt.commit('first')
2363.2.2 by John Arbash Meinel
Simplify the test even further....
232
        wt.remove('b/c')
2363.2.1 by John Arbash Meinel
(broken) Add a simplified test which exposes the bug.
233
        this_dir = self.get_transport()
234
        this_dir.delete_tree('b')
235
        wt.lock_write()
236
        wt.commit('commit deleted rename')
237
        self.assertTrue(wt.has_id('a-id'))
238
        self.assertFalse(wt.has_or_had_id('b-id'))
239
        self.assertFalse(wt.has_or_had_id('c-id'))
240
        self.assertTrue(wt.has_filename('a'))
241
        self.assertFalse(wt.has_filename('b'))
242
        self.assertFalse(wt.has_filename('b/c'))
243
        wt.unlock()
244
1731.2.4 by Aaron Bentley
Ensure subsume works with Knit2 repos
245
    def test_commit_move_new(self):
246
        wt = self.make_branch_and_tree('first')
247
        wt.commit('first')
248
        wt2 = wt.bzrdir.sprout('second').open_workingtree()
249
        self.build_tree(['second/name1'])
250
        wt2.add('name1', 'name1-id')
251
        wt2.commit('second')
252
        wt.merge_from_branch(wt2.branch)
253
        wt.rename_one('name1', 'name2')
254
        wt.commit('third')
255
        wt.path2id('name1-id')
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
256
257
    def test_nested_commit(self):
258
        """Commit in multiply-nested trees"""
259
        tree = self.make_branch_and_tree('.')
260
        if not tree.supports_tree_reference():
261
            # inapplicable test.
262
            return
263
        subtree = self.make_branch_and_tree('subtree')
264
        subsubtree = self.make_branch_and_tree('subtree/subtree')
265
        subtree.add(['subtree'])
266
        tree.add(['subtree'])
267
        # use allow_pointless=False to ensure that the deepest tree, which
268
        # has no commits made to it, does not get a pointless commit.
269
        rev_id = tree.commit('added reference', allow_pointless=False)
270
        tree.lock_read()
271
        self.addCleanup(tree.unlock)
272
        # the deepest subtree has not changed, so no commit should take place.
273
        self.assertEqual(None, subsubtree.last_revision())
274
        # the intermediate tree should have committed a pointer to the current
275
        # subtree revision.
276
        sub_basis = subtree.basis_tree()
277
        sub_basis.lock_read()
278
        self.addCleanup(sub_basis.unlock)
279
        self.assertEqual(subsubtree.last_revision(),
2255.2.227 by Robert Collins
Make all test_commit tests pass.
280
            sub_basis.get_reference_revision(sub_basis.path2id('subtree')))
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
281
        # the intermediate tree has changed, so should have had a commit
282
        # take place.
283
        self.assertNotEqual(None, subtree.last_revision())
284
        # the outer tree should have committed a pointer to the current
285
        # subtree revision.
286
        basis = tree.basis_tree()
287
        basis.lock_read()
288
        self.addCleanup(basis.unlock)
289
        self.assertEqual(subtree.last_revision(),
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
290
            basis.get_reference_revision(basis.path2id('subtree')))
2255.2.218 by Robert Collins
Make the nested tree commit smoke test be more rigourous.
291
        # the outer tree must have have changed too.
292
        self.assertNotEqual(None, rev_id)
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.
293
        
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
294
    def test_nested_commit_second_commit_detects_changes(self):
295
        """Commit with a nested tree picks up the correct child revid."""
296
        tree = self.make_branch_and_tree('.')
297
        if not tree.supports_tree_reference():
298
            # inapplicable test.
299
            return
300
        subtree = self.make_branch_and_tree('subtree')
301
        tree.add(['subtree'])
302
        self.build_tree(['subtree/file'])
303
        subtree.add(['file'], ['file-id'])
304
        rev_id = tree.commit('added reference', allow_pointless=False)
305
        child_revid = subtree.last_revision()
306
        # now change the child tree
307
        self.build_tree_contents([('subtree/file', 'new-content')])
308
        # and commit in the parent should commit the child and grab its revid,
309
        # we test with allow_pointless=False here so that we are simulating
310
        # what users will see.
311
        rev_id2 = tree.commit('changed subtree only', allow_pointless=False)
312
        # the child tree has changed, so should have had a commit
313
        # take place.
314
        self.assertNotEqual(None, subtree.last_revision())
315
        self.assertNotEqual(child_revid, subtree.last_revision())
316
        # the outer tree should have committed a pointer to the current
317
        # subtree revision.
318
        basis = tree.basis_tree()
319
        basis.lock_read()
320
        self.addCleanup(basis.unlock)
321
        self.assertEqual(subtree.last_revision(),
2255.2.226 by Robert Collins
Get merge_nested finally working: change nested tree iterators to take file_ids, and ensure the right branch is connected to in the merge logic. May not be suitable for shared repositories yet.
322
            basis.get_reference_revision(basis.path2id('subtree')))
2255.2.220 by Robert Collins
Fix failing detection of changes restricted to subtrees causing spurious pointless commit errors.
323
        self.assertNotEqual(rev_id, rev_id2)
324
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.
325
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
326
class TestCommitProgress(TestCaseWithWorkingTree):
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
327
    
328
    def restoreDefaults(self):
329
        ui.ui_factory = self.old_ui_factory
330
331
    def test_commit_progress_steps(self):
332
        # during commit we one progress update for every entry in the 
333
        # inventory, and then one for the inventory, and one for the
334
        # inventory, and one for the revision insertions.
335
        # first we need a test commit to do. Lets setup a branch with 
336
        # 3 files, and alter one in a selected-file commit. This exercises
337
        # a number of cases quickly. We should also test things like 
338
        # selective commits which excludes newly added files.
339
        tree = self.make_branch_and_tree('.')
340
        self.build_tree(['a', 'b', 'c'])
341
        tree.add(['a', 'b', 'c'])
342
        tree.commit('first post')
343
        f = file('b', 'wt')
344
        f.write('new content')
345
        f.close()
346
        # set a progress bar that captures the calls so we can see what is 
347
        # emitted
348
        self.old_ui_factory = ui.ui_factory
349
        self.addCleanup(self.restoreDefaults)
350
        factory = CapturingUIFactory()
351
        ui.ui_factory = factory
352
        # TODO RBC 20060421 it would be nice to merge the reporter output
353
        # into the factory for this test - just make the test ui factory
354
        # pun as a reporter. Then we can check the ordering is right.
355
        tree.commit('second post', specific_files=['b'])
2531.1.2 by Ian Clatworthy
Improved progress reporting for commit
356
        # 4 steps, the first of which is reported 5 times, once per file
357
        # 2 files don't trigger an update, as 'a' and 'c' are not 
1740.3.10 by Jelmer Vernooij
Fix some minor issues pointed out by j-a-m.
358
        # committed.
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
359
        self.assertEqual(
2531.1.3 by Ian Clatworthy
Fix whitespace and improve tests to cover actual progress messages
360
            [('update', 1, 4, 'Collecting changes [Entry 0/?] - Stage'),
361
             ('update', 1, 4, 'Collecting changes [Entry 1/4] - Stage'),
362
             ('update', 1, 4, 'Collecting changes [Entry 2/4] - Stage'),
363
             ('update', 1, 4, 'Collecting changes [Entry 3/4] - Stage'),
364
             ('update', 1, 4, 'Collecting changes [Entry 4/4] - Stage'),
365
             ('update', 2, 4, 'Saving data locally - Stage'),
366
             ('update', 3, 4, 'Updating the working tree - Stage'),
367
             ('update', 4, 4, 'Running post commit hooks - Stage')],
1666.1.19 by Robert Collins
Introduce a progress bar during commit.
368
            factory._calls
369
           )
2553.1.2 by Robert Collins
Show hook names during commit.
370
371
    def test_commit_progress_shows_hook_names(self):
372
        tree = self.make_branch_and_tree('.')
373
        # set a progress bar that captures the calls so we can see what is 
374
        # emitted
375
        self.old_ui_factory = ui.ui_factory
376
        self.addCleanup(self.restoreDefaults)
377
        factory = CapturingUIFactory()
378
        ui.ui_factory = factory
379
        def a_hook(_, _2, _3, _4, _5, _6):
380
            pass
381
        branch.Branch.hooks.install_hook('post_commit', a_hook)
382
        branch.Branch.hooks.name_hook(a_hook, 'hook name')
383
        tree.commit('first post')
384
        self.assertEqual(
385
            [('update', 1, 4, 'Collecting changes [Entry 0/?] - Stage'),
386
             ('update', 1, 4, 'Collecting changes [Entry 1/1] - Stage'),
387
             ('update', 2, 4, 'Saving data locally - Stage'),
388
             ('update', 3, 4, 'Updating the working tree - Stage'),
389
             ('update', 4, 4, 'Running post commit hooks - Stage'),
390
             ('update', 4, 4, 'Running post commit hooks [hook name] - Stage'),
391
             ],
392
            factory._calls
393
           )
2598.5.2 by Aaron Bentley
Got all tests passing with Branch returning 'null:' for null revision
394
395
396
class TestUncommit(TestCaseWithWorkingTree):
397
398
    def test_uncommit_to_null(self):
399
        tree = self.make_branch_and_tree('branch')
400
        tree.lock_write()
401
        revid = tree.commit('a revision')
402
        tree.unlock()
403
        uncommit.uncommit(tree.branch, tree=tree)
404
        self.assertEqual([], tree.get_parent_ids())