~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005 by 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
20
20
import bzrlib
21
21
from bzrlib.tests import TestCaseWithTransport
22
22
from bzrlib.branch import Branch
23
 
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
24
23
from bzrlib.workingtree import WorkingTree
25
 
from bzrlib.commit import Commit, NullCommitReporter
 
24
from bzrlib.commit import Commit
26
25
from bzrlib.config import BranchConfig
27
 
from bzrlib.errors import (PointlessCommit, BzrError, SigningFailed, 
28
 
                           LockContention)
 
26
from bzrlib.errors import PointlessCommit, BzrError, SigningFailed
29
27
 
30
28
 
31
29
# TODO: Test commit with some added, and added-but-missing files
45
43
        return "bzrlib.ahook bzrlib.ahook"
46
44
 
47
45
 
48
 
class CapturingReporter(NullCommitReporter):
49
 
    """This reporter captures the calls made to it for evaluation later."""
50
 
 
51
 
    def __init__(self):
52
 
        # a list of the calls this received
53
 
        self.calls = []
54
 
 
55
 
    def snapshot_change(self, change, path):
56
 
        self.calls.append(('change', change, path))
57
 
 
58
 
    def deleted(self, file_id):
59
 
        self.calls.append(('deleted', file_id))
60
 
 
61
 
    def missing(self, path):
62
 
        self.calls.append(('missing', path))
63
 
 
64
 
    def renamed(self, change, old_path, new_path):
65
 
        self.calls.append(('renamed', change, old_path, new_path))
66
 
 
67
 
 
68
46
class TestCommit(TestCaseWithTransport):
69
47
 
70
48
    def test_simple_commit(self):
200
178
        b = wt.branch
201
179
        wt.commit('initial', rev_id='test@rev-1', allow_pointless=True)
202
180
        self.assertRaises(Exception,
203
 
                          wt.commit,
 
181
                          b.working_tree().commit,
204
182
                          message='reused id',
205
183
                          rev_id='test@rev-1',
206
184
                          allow_pointless=True)
217
195
        wt.move(['hello'], 'a')
218
196
        r2 = 'test@rev-2'
219
197
        wt.commit('two', rev_id=r2, allow_pointless=False)
220
 
        self.check_inventory_shape(wt.read_working_inventory(),
 
198
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
221
199
                                   ['a', 'a/hello', 'b'])
222
200
 
223
201
        wt.move(['b'], 'a')
292
270
        file('hello', 'w').write('hello world')
293
271
        wt.add('hello')
294
272
        file('goodbye', 'w').write('goodbye cruel world!')
295
 
        self.assertRaises(StrictCommitFailed, wt.commit,
 
273
        self.assertRaises(StrictCommitFailed, b.working_tree().commit,
296
274
            message='add hello but not goodbye', strict=True)
297
275
 
298
276
    def test_strict_commit_without_unknowns(self):
330
308
        wt = self.make_branch_and_tree('.')
331
309
        branch = wt.branch
332
310
        wt.commit("base", allow_pointless=True, rev_id='A')
333
 
        self.failIf(branch.repository.has_signature_for_revision_id('A'))
 
311
        self.failIf(branch.repository.revision_store.has_id('A', 'sig'))
334
312
        try:
335
313
            from bzrlib.testament import Testament
336
314
            # monkey patch gpg signing mechanism
341
319
                                                      working_tree=wt)
342
320
            self.assertEqual(Testament.from_revision(branch.repository,
343
321
                             'B').as_short_text(),
344
 
                             branch.repository.get_signature_text('B'))
 
322
                             branch.repository.revision_store.get('B', 
 
323
                                                               'sig').read())
345
324
        finally:
346
325
            bzrlib.gpg.GPGStrategy = oldstrategy
347
326
 
352
331
        wt = self.make_branch_and_tree('.')
353
332
        branch = wt.branch
354
333
        wt.commit("base", allow_pointless=True, rev_id='A')
355
 
        self.failIf(branch.repository.has_signature_for_revision_id('A'))
 
334
        self.failIf(branch.repository.revision_store.has_id('A', 'sig'))
356
335
        try:
357
336
            from bzrlib.testament import Testament
358
337
            # monkey patch gpg signing mechanism
360
339
            config = MustSignConfig(branch)
361
340
            self.assertRaises(SigningFailed,
362
341
                              commit.Commit(config=config).commit,
363
 
                              message="base",
 
342
                              branch, "base",
364
343
                              allow_pointless=True,
365
 
                              rev_id='B',
366
 
                              working_tree=wt)
 
344
                              rev_id='B')
367
345
            branch = Branch.open(self.get_url('.'))
368
346
            self.assertEqual(branch.revision_history(), ['A'])
369
 
            self.failIf(branch.repository.has_revision('B'))
 
347
            self.failIf(branch.repository.revision_store.has_id('B'))
370
348
        finally:
371
349
            bzrlib.gpg.GPGStrategy = oldstrategy
372
350
 
387
365
            self.assertEqual(['called', 'called'], calls)
388
366
        finally:
389
367
            del bzrlib.ahook
390
 
 
391
 
    def test_commit_object_doesnt_set_nick(self):
392
 
        # using the Commit object directly does not set the branch nick.
393
 
        wt = self.make_branch_and_tree('.')
394
 
        c = Commit()
395
 
        c.commit(working_tree=wt, message='empty tree', allow_pointless=True)
396
 
        self.assertEquals(wt.branch.revno(), 1)
397
 
        self.assertEqual({},
398
 
                         wt.branch.repository.get_revision(
399
 
                            wt.branch.last_revision()).properties)
400
 
 
401
 
    def test_safe_master_lock(self):
402
 
        os.mkdir('master')
403
 
        master = BzrDirMetaFormat1().initialize('master')
404
 
        master.create_repository()
405
 
        master_branch = master.create_branch()
406
 
        master.create_workingtree()
407
 
        bound = master.sprout('bound')
408
 
        wt = bound.open_workingtree()
409
 
        wt.branch.set_bound_location(os.path.realpath('master'))
410
 
        master_branch.lock_write()
411
 
        try:
412
 
            self.assertRaises(LockContention, wt.commit, 'silly')
413
 
        finally:
414
 
            master_branch.unlock()
415
 
 
416
 
    def test_commit_bound_merge(self):
417
 
        # see bug #43959; commit of a merge in a bound branch fails to push
418
 
        # the new commit into the master
419
 
        master_branch = self.make_branch('master')
420
 
        bound_tree = self.make_branch_and_tree('bound')
421
 
        bound_tree.branch.bind(master_branch)
422
 
 
423
 
        self.build_tree_contents([('bound/content_file', 'initial contents\n')])
424
 
        bound_tree.add(['content_file'])
425
 
        bound_tree.commit(message='woo!')
426
 
 
427
 
        other_bzrdir = master_branch.bzrdir.sprout('other')
428
 
        other_tree = other_bzrdir.open_workingtree()
429
 
 
430
 
        # do a commit to the the other branch changing the content file so
431
 
        # that our commit after merging will have a merged revision in the
432
 
        # content file history.
433
 
        self.build_tree_contents([('other/content_file', 'change in other\n')])
434
 
        other_tree.commit('change in other')
435
 
 
436
 
        # do a merge into the bound branch from other, and then change the
437
 
        # content file locally to force a new revision (rather than using the
438
 
        # revision from other). This forces extra processing in commit.
439
 
        self.merge(other_tree.branch, bound_tree)
440
 
        self.build_tree_contents([('bound/content_file', 'change in bound\n')])
441
 
 
442
 
        # before #34959 was fixed, this failed with 'revision not present in
443
 
        # weave' when trying to implicitly push from the bound branch to the master
444
 
        bound_tree.commit(message='commit of merge in bound tree')
445
 
 
446
 
    def test_commit_reporting_after_merge(self):
447
 
        # when doing a commit of a merge, the reporter needs to still 
448
 
        # be called for each item that is added/removed/deleted.
449
 
        this_tree = self.make_branch_and_tree('this')
450
 
        # we need a bunch of files and dirs, to perform one action on each.
451
 
        self.build_tree([
452
 
            'this/dirtorename/',
453
 
            'this/dirtoreparent/',
454
 
            'this/dirtoleave/',
455
 
            'this/dirtoremove/',
456
 
            'this/filetoreparent',
457
 
            'this/filetorename',
458
 
            'this/filetomodify',
459
 
            'this/filetoremove',
460
 
            'this/filetoleave']
461
 
            )
462
 
        this_tree.add([
463
 
            'dirtorename',
464
 
            'dirtoreparent',
465
 
            'dirtoleave',
466
 
            'dirtoremove',
467
 
            'filetoreparent',
468
 
            'filetorename',
469
 
            'filetomodify',
470
 
            'filetoremove',
471
 
            'filetoleave']
472
 
            )
473
 
        this_tree.commit('create_files')
474
 
        other_dir = this_tree.bzrdir.sprout('other')
475
 
        other_tree = other_dir.open_workingtree()
476
 
        other_tree.lock_write()
477
 
        # perform the needed actions on the files and dirs.
478
 
        try:
479
 
            other_tree.rename_one('dirtorename', 'renameddir')
480
 
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
481
 
            other_tree.rename_one('filetorename', 'renamedfile')
482
 
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
483
 
            other_tree.remove(['dirtoremove', 'filetoremove'])
484
 
            self.build_tree_contents([
485
 
                ('other/newdir/', ),
486
 
                ('other/filetomodify', 'new content'),
487
 
                ('other/newfile', 'new file content')])
488
 
            other_tree.add('newfile')
489
 
            other_tree.add('newdir/')
490
 
            other_tree.commit('modify all sample files and dirs.')
491
 
        finally:
492
 
            other_tree.unlock()
493
 
        self.merge(other_tree.branch, this_tree)
494
 
        reporter = CapturingReporter()
495
 
        this_tree.commit('do the commit', reporter=reporter)
496
 
        self.assertEqual([
497
 
            ('change', 'unchanged', 'dirtoleave'),
498
 
            ('change', 'unchanged', 'filetoleave'),
499
 
            ('change', 'modified', 'filetomodify'),
500
 
            ('change', 'added', 'newdir'),
501
 
            ('change', 'added', 'newfile'),
502
 
            ('renamed', 'renamed', 'dirtorename', 'renameddir'),
503
 
            ('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
504
 
            ('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
505
 
            ('renamed', 'renamed', 'filetorename', 'renamedfile'),
506
 
            ('deleted', 'dirtoremove'),
507
 
            ('deleted', 'filetoremove'),
508
 
            ],
509
 
            reporter.calls)