/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

(John Arbash Meinel)  Fix bug #158333,
        make sure that Repository.fetch(self) is properly a no-op for all
        Repository implementations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
 
 
7
#
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
 
 
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
import os
19
19
 
20
20
import bzrlib
21
 
from bzrlib.tests import TestCaseWithTransport
 
21
from bzrlib import (
 
22
    errors,
 
23
    lockdir,
 
24
    osutils,
 
25
    tests,
 
26
    )
22
27
from bzrlib.branch import Branch
23
28
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
24
 
from bzrlib.workingtree import WorkingTree
25
 
from bzrlib.commit import Commit
 
29
from bzrlib.commit import Commit, NullCommitReporter
26
30
from bzrlib.config import BranchConfig
27
31
from bzrlib.errors import (PointlessCommit, BzrError, SigningFailed, 
28
32
                           LockContention)
 
33
from bzrlib.tests import SymlinkFeature, TestCaseWithTransport
 
34
from bzrlib.workingtree import WorkingTree
29
35
 
30
36
 
31
37
# TODO: Test commit with some added, and added-but-missing files
45
51
        return "bzrlib.ahook bzrlib.ahook"
46
52
 
47
53
 
 
54
class CapturingReporter(NullCommitReporter):
 
55
    """This reporter captures the calls made to it for evaluation later."""
 
56
 
 
57
    def __init__(self):
 
58
        # a list of the calls this received
 
59
        self.calls = []
 
60
 
 
61
    def snapshot_change(self, change, path):
 
62
        self.calls.append(('change', change, path))
 
63
 
 
64
    def deleted(self, file_id):
 
65
        self.calls.append(('deleted', file_id))
 
66
 
 
67
    def missing(self, path):
 
68
        self.calls.append(('missing', path))
 
69
 
 
70
    def renamed(self, change, old_path, new_path):
 
71
        self.calls.append(('renamed', change, old_path, new_path))
 
72
 
 
73
    def is_verbose(self):
 
74
        return True
 
75
 
 
76
 
48
77
class TestCommit(TestCaseWithTransport):
49
78
 
50
79
    def test_simple_commit(self):
197
226
        wt.move(['hello'], 'a')
198
227
        r2 = 'test@rev-2'
199
228
        wt.commit('two', rev_id=r2, allow_pointless=False)
200
 
        self.check_inventory_shape(wt.read_working_inventory(),
201
 
                                   ['a', 'a/hello', 'b'])
 
229
        wt.lock_read()
 
230
        try:
 
231
            self.check_inventory_shape(wt.read_working_inventory(),
 
232
                                       ['a/', 'a/hello', 'b/'])
 
233
        finally:
 
234
            wt.unlock()
202
235
 
203
236
        wt.move(['b'], 'a')
204
237
        r3 = 'test@rev-3'
205
238
        wt.commit('three', rev_id=r3, allow_pointless=False)
206
 
        self.check_inventory_shape(wt.read_working_inventory(),
207
 
                                   ['a', 'a/hello', 'a/b'])
208
 
        self.check_inventory_shape(b.repository.get_revision_inventory(r3),
209
 
                                   ['a', 'a/hello', 'a/b'])
 
239
        wt.lock_read()
 
240
        try:
 
241
            self.check_inventory_shape(wt.read_working_inventory(),
 
242
                                       ['a/', 'a/hello', 'a/b/'])
 
243
            self.check_inventory_shape(b.repository.get_revision_inventory(r3),
 
244
                                       ['a/', 'a/hello', 'a/b/'])
 
245
        finally:
 
246
            wt.unlock()
210
247
 
211
248
        wt.move(['a/hello'], 'a/b')
212
249
        r4 = 'test@rev-4'
213
250
        wt.commit('four', rev_id=r4, allow_pointless=False)
214
 
        self.check_inventory_shape(wt.read_working_inventory(),
215
 
                                   ['a', 'a/b/hello', 'a/b'])
 
251
        wt.lock_read()
 
252
        try:
 
253
            self.check_inventory_shape(wt.read_working_inventory(),
 
254
                                       ['a/', 'a/b/hello', 'a/b/'])
 
255
        finally:
 
256
            wt.unlock()
216
257
 
217
258
        inv = b.repository.get_revision_inventory(r4)
218
259
        eq(inv['hello-id'].revision, r4)
219
260
        eq(inv['a-id'].revision, r1)
220
261
        eq(inv['b-id'].revision, r3)
221
 
        
 
262
 
222
263
    def test_removed_commit(self):
223
264
        """Commit with a removed file"""
224
265
        wt = self.make_branch_and_tree('.')
319
360
                                                      allow_pointless=True,
320
361
                                                      rev_id='B',
321
362
                                                      working_tree=wt)
322
 
            self.assertEqual(Testament.from_revision(branch.repository,
323
 
                             'B').as_short_text(),
 
363
            def sign(text):
 
364
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
 
365
            self.assertEqual(sign(Testament.from_revision(branch.repository,
 
366
                             'B').as_short_text()),
324
367
                             branch.repository.get_signature_text('B'))
325
368
        finally:
326
369
            bzrlib.gpg.GPGStrategy = oldstrategy
387
430
        bound = master.sprout('bound')
388
431
        wt = bound.open_workingtree()
389
432
        wt.branch.set_bound_location(os.path.realpath('master'))
 
433
 
 
434
        orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
390
435
        master_branch.lock_write()
391
436
        try:
 
437
            lockdir._DEFAULT_TIMEOUT_SECONDS = 1
392
438
            self.assertRaises(LockContention, wt.commit, 'silly')
393
439
        finally:
 
440
            lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
394
441
            master_branch.unlock()
 
442
 
 
443
    def test_commit_bound_merge(self):
 
444
        # see bug #43959; commit of a merge in a bound branch fails to push
 
445
        # the new commit into the master
 
446
        master_branch = self.make_branch('master')
 
447
        bound_tree = self.make_branch_and_tree('bound')
 
448
        bound_tree.branch.bind(master_branch)
 
449
 
 
450
        self.build_tree_contents([('bound/content_file', 'initial contents\n')])
 
451
        bound_tree.add(['content_file'])
 
452
        bound_tree.commit(message='woo!')
 
453
 
 
454
        other_bzrdir = master_branch.bzrdir.sprout('other')
 
455
        other_tree = other_bzrdir.open_workingtree()
 
456
 
 
457
        # do a commit to the the other branch changing the content file so
 
458
        # that our commit after merging will have a merged revision in the
 
459
        # content file history.
 
460
        self.build_tree_contents([('other/content_file', 'change in other\n')])
 
461
        other_tree.commit('change in other')
 
462
 
 
463
        # do a merge into the bound branch from other, and then change the
 
464
        # content file locally to force a new revision (rather than using the
 
465
        # revision from other). This forces extra processing in commit.
 
466
        bound_tree.merge_from_branch(other_tree.branch)
 
467
        self.build_tree_contents([('bound/content_file', 'change in bound\n')])
 
468
 
 
469
        # before #34959 was fixed, this failed with 'revision not present in
 
470
        # weave' when trying to implicitly push from the bound branch to the master
 
471
        bound_tree.commit(message='commit of merge in bound tree')
 
472
 
 
473
    def test_commit_reporting_after_merge(self):
 
474
        # when doing a commit of a merge, the reporter needs to still 
 
475
        # be called for each item that is added/removed/deleted.
 
476
        this_tree = self.make_branch_and_tree('this')
 
477
        # we need a bunch of files and dirs, to perform one action on each.
 
478
        self.build_tree([
 
479
            'this/dirtorename/',
 
480
            'this/dirtoreparent/',
 
481
            'this/dirtoleave/',
 
482
            'this/dirtoremove/',
 
483
            'this/filetoreparent',
 
484
            'this/filetorename',
 
485
            'this/filetomodify',
 
486
            'this/filetoremove',
 
487
            'this/filetoleave']
 
488
            )
 
489
        this_tree.add([
 
490
            'dirtorename',
 
491
            'dirtoreparent',
 
492
            'dirtoleave',
 
493
            'dirtoremove',
 
494
            'filetoreparent',
 
495
            'filetorename',
 
496
            'filetomodify',
 
497
            'filetoremove',
 
498
            'filetoleave']
 
499
            )
 
500
        this_tree.commit('create_files')
 
501
        other_dir = this_tree.bzrdir.sprout('other')
 
502
        other_tree = other_dir.open_workingtree()
 
503
        other_tree.lock_write()
 
504
        # perform the needed actions on the files and dirs.
 
505
        try:
 
506
            other_tree.rename_one('dirtorename', 'renameddir')
 
507
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
 
508
            other_tree.rename_one('filetorename', 'renamedfile')
 
509
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
 
510
            other_tree.remove(['dirtoremove', 'filetoremove'])
 
511
            self.build_tree_contents([
 
512
                ('other/newdir/', ),
 
513
                ('other/filetomodify', 'new content'),
 
514
                ('other/newfile', 'new file content')])
 
515
            other_tree.add('newfile')
 
516
            other_tree.add('newdir/')
 
517
            other_tree.commit('modify all sample files and dirs.')
 
518
        finally:
 
519
            other_tree.unlock()
 
520
        this_tree.merge_from_branch(other_tree.branch)
 
521
        reporter = CapturingReporter()
 
522
        this_tree.commit('do the commit', reporter=reporter)
 
523
        self.assertEqual([
 
524
            ('change', 'unchanged', ''),
 
525
            ('change', 'unchanged', 'dirtoleave'),
 
526
            ('change', 'unchanged', 'filetoleave'),
 
527
            ('change', 'modified', 'filetomodify'),
 
528
            ('change', 'added', 'newdir'),
 
529
            ('change', 'added', 'newfile'),
 
530
            ('renamed', 'renamed', 'dirtorename', 'renameddir'),
 
531
            ('renamed', 'renamed', 'filetorename', 'renamedfile'),
 
532
            ('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
 
533
            ('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
 
534
            ('deleted', 'dirtoremove'),
 
535
            ('deleted', 'filetoremove'),
 
536
            ],
 
537
            reporter.calls)
 
538
 
 
539
    def test_commit_removals_respects_filespec(self):
 
540
        """Commit respects the specified_files for removals."""
 
541
        tree = self.make_branch_and_tree('.')
 
542
        self.build_tree(['a', 'b'])
 
543
        tree.add(['a', 'b'])
 
544
        tree.commit('added a, b')
 
545
        tree.remove(['a', 'b'])
 
546
        tree.commit('removed a', specific_files='a')
 
547
        basis = tree.basis_tree()
 
548
        tree.lock_read()
 
549
        try:
 
550
            self.assertIs(None, basis.path2id('a'))
 
551
            self.assertFalse(basis.path2id('b') is None)
 
552
        finally:
 
553
            tree.unlock()
 
554
 
 
555
    def test_commit_saves_1ms_timestamp(self):
 
556
        """Passing in a timestamp is saved with 1ms resolution"""
 
557
        tree = self.make_branch_and_tree('.')
 
558
        self.build_tree(['a'])
 
559
        tree.add('a')
 
560
        tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
 
561
                    rev_id='a1')
 
562
 
 
563
        rev = tree.branch.repository.get_revision('a1')
 
564
        self.assertEqual(1153248633.419, rev.timestamp)
 
565
 
 
566
    def test_commit_has_1ms_resolution(self):
 
567
        """Allowing commit to generate the timestamp also has 1ms resolution"""
 
568
        tree = self.make_branch_and_tree('.')
 
569
        self.build_tree(['a'])
 
570
        tree.add('a')
 
571
        tree.commit('added a', rev_id='a1')
 
572
 
 
573
        rev = tree.branch.repository.get_revision('a1')
 
574
        timestamp = rev.timestamp
 
575
        timestamp_1ms = round(timestamp, 3)
 
576
        self.assertEqual(timestamp_1ms, timestamp)
 
577
 
 
578
    def assertBasisTreeKind(self, kind, tree, file_id):
 
579
        basis = tree.basis_tree()
 
580
        basis.lock_read()
 
581
        try:
 
582
            self.assertEqual(kind, basis.kind(file_id))
 
583
        finally:
 
584
            basis.unlock()
 
585
 
 
586
    def test_commit_kind_changes(self):
 
587
        self.requireFeature(SymlinkFeature)
 
588
        tree = self.make_branch_and_tree('.')
 
589
        os.symlink('target', 'name')
 
590
        tree.add('name', 'a-file-id')
 
591
        tree.commit('Added a symlink')
 
592
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
 
593
 
 
594
        os.unlink('name')
 
595
        self.build_tree(['name'])
 
596
        tree.commit('Changed symlink to file')
 
597
        self.assertBasisTreeKind('file', tree, 'a-file-id')
 
598
 
 
599
        os.unlink('name')
 
600
        os.symlink('target', 'name')
 
601
        tree.commit('file to symlink')
 
602
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
 
603
 
 
604
        os.unlink('name')
 
605
        os.mkdir('name')
 
606
        tree.commit('symlink to directory')
 
607
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
 
608
 
 
609
        os.rmdir('name')
 
610
        os.symlink('target', 'name')
 
611
        tree.commit('directory to symlink')
 
612
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
 
613
 
 
614
        # prepare for directory <-> file tests
 
615
        os.unlink('name')
 
616
        os.mkdir('name')
 
617
        tree.commit('symlink to directory')
 
618
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
 
619
 
 
620
        os.rmdir('name')
 
621
        self.build_tree(['name'])
 
622
        tree.commit('Changed directory to file')
 
623
        self.assertBasisTreeKind('file', tree, 'a-file-id')
 
624
 
 
625
        os.unlink('name')
 
626
        os.mkdir('name')
 
627
        tree.commit('file to directory')
 
628
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
 
629
 
 
630
    def test_commit_unversioned_specified(self):
 
631
        """Commit should raise if specified files isn't in basis or worktree"""
 
632
        tree = self.make_branch_and_tree('.')
 
633
        self.assertRaises(errors.PathsNotVersionedError, tree.commit, 
 
634
                          'message', specific_files=['bogus'])
 
635
 
 
636
    class Callback(object):
 
637
        
 
638
        def __init__(self, message, testcase):
 
639
            self.called = False
 
640
            self.message = message
 
641
            self.testcase = testcase
 
642
 
 
643
        def __call__(self, commit_obj):
 
644
            self.called = True
 
645
            self.testcase.assertTrue(isinstance(commit_obj, Commit))
 
646
            return self.message
 
647
 
 
648
    def test_commit_callback(self):
 
649
        """Commit should invoke a callback to get the message"""
 
650
 
 
651
        tree = self.make_branch_and_tree('.')
 
652
        try:
 
653
            tree.commit()
 
654
        except Exception, e:
 
655
            self.assertTrue(isinstance(e, BzrError))
 
656
            self.assertEqual('The message or message_callback keyword'
 
657
                             ' parameter is required for commit().', str(e))
 
658
        else:
 
659
            self.fail('exception not raised')
 
660
        cb = self.Callback(u'commit 1', self)
 
661
        tree.commit(message_callback=cb)
 
662
        self.assertTrue(cb.called)
 
663
        repository = tree.branch.repository
 
664
        message = repository.get_revision(tree.last_revision()).message
 
665
        self.assertEqual('commit 1', message)
 
666
 
 
667
    def test_no_callback_pointless(self):
 
668
        """Callback should not be invoked for pointless commit"""
 
669
        tree = self.make_branch_and_tree('.')
 
670
        cb = self.Callback(u'commit 2', self)
 
671
        self.assertRaises(PointlessCommit, tree.commit, message_callback=cb, 
 
672
                          allow_pointless=False)
 
673
        self.assertFalse(cb.called)
 
674
 
 
675
    def test_no_callback_netfailure(self):
 
676
        """Callback should not be invoked if connectivity fails"""
 
677
        tree = self.make_branch_and_tree('.')
 
678
        cb = self.Callback(u'commit 2', self)
 
679
        repository = tree.branch.repository
 
680
        # simulate network failure
 
681
        def raise_(self, arg, arg2):
 
682
            raise errors.NoSuchFile('foo')
 
683
        repository.add_inventory = raise_
 
684
        self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
 
685
        self.assertFalse(cb.called)
 
686
 
 
687
    def test_selected_file_merge_commit(self):
 
688
        """Ensure the correct error is raised"""
 
689
        tree = self.make_branch_and_tree('foo')
 
690
        # pending merge would turn into a left parent
 
691
        tree.commit('commit 1')
 
692
        tree.add_parent_tree_id('example')
 
693
        self.build_tree(['foo/bar', 'foo/baz'])
 
694
        tree.add(['bar', 'baz'])
 
695
        err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
 
696
            tree.commit, 'commit 2', specific_files=['bar', 'baz'])
 
697
        self.assertEqual(['bar', 'baz'], err.files)
 
698
        self.assertEqual('Selected-file commit of merges is not supported'
 
699
                         ' yet: files bar, baz', str(err))
 
700
 
 
701
    def test_commit_ordering(self):
 
702
        """Test of corner-case commit ordering error"""
 
703
        tree = self.make_branch_and_tree('.')
 
704
        self.build_tree(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
 
705
        tree.add(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
 
706
        tree.commit('setup')
 
707
        self.build_tree(['a/c/d/'])
 
708
        tree.add('a/c/d')
 
709
        tree.rename_one('a/z/x', 'a/c/d/x')
 
710
        tree.commit('test', specific_files=['a/z/y'])
 
711
 
 
712
    def test_commit_no_author(self):
 
713
        """The default kwarg author in MutableTree.commit should not add
 
714
        the 'author' revision property.
 
715
        """
 
716
        tree = self.make_branch_and_tree('foo')
 
717
        rev_id = tree.commit('commit 1')
 
718
        rev = tree.branch.repository.get_revision(rev_id)
 
719
        self.assertFalse('author' in rev.properties)
 
720
 
 
721
    def test_commit_author(self):
 
722
        """Passing a non-empty author kwarg to MutableTree.commit should add
 
723
        the 'author' revision property.
 
724
        """
 
725
        tree = self.make_branch_and_tree('foo')
 
726
        rev_id = tree.commit('commit 1', author='John Doe <jdoe@example.com>')
 
727
        rev = tree.branch.repository.get_revision(rev_id)
 
728
        self.assertEqual('John Doe <jdoe@example.com>',
 
729
                         rev.properties['author'])