392
505
self.assertRaises(LockContention, wt.commit, 'silly')
394
507
master_branch.unlock()
509
def test_commit_bound_merge(self):
510
# see bug #43959; commit of a merge in a bound branch fails to push
511
# the new commit into the master
512
master_branch = self.make_branch('master')
513
bound_tree = self.make_branch_and_tree('bound')
514
bound_tree.branch.bind(master_branch)
516
self.build_tree_contents([('bound/content_file', 'initial contents\n')])
517
bound_tree.add(['content_file'])
518
bound_tree.commit(message='woo!')
520
other_bzrdir = master_branch.bzrdir.sprout('other')
521
other_tree = other_bzrdir.open_workingtree()
523
# do a commit to the other branch changing the content file so
524
# that our commit after merging will have a merged revision in the
525
# content file history.
526
self.build_tree_contents([('other/content_file', 'change in other\n')])
527
other_tree.commit('change in other')
529
# do a merge into the bound branch from other, and then change the
530
# content file locally to force a new revision (rather than using the
531
# revision from other). This forces extra processing in commit.
532
bound_tree.merge_from_branch(other_tree.branch)
533
self.build_tree_contents([('bound/content_file', 'change in bound\n')])
535
# before #34959 was fixed, this failed with 'revision not present in
536
# weave' when trying to implicitly push from the bound branch to the master
537
bound_tree.commit(message='commit of merge in bound tree')
539
def test_commit_reporting_after_merge(self):
540
# when doing a commit of a merge, the reporter needs to still
541
# be called for each item that is added/removed/deleted.
542
this_tree = self.make_branch_and_tree('this')
543
# we need a bunch of files and dirs, to perform one action on each.
546
'this/dirtoreparent/',
549
'this/filetoreparent',
566
this_tree.commit('create_files')
567
other_dir = this_tree.bzrdir.sprout('other')
568
other_tree = other_dir.open_workingtree()
569
other_tree.lock_write()
570
# perform the needed actions on the files and dirs.
572
other_tree.rename_one('dirtorename', 'renameddir')
573
other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
574
other_tree.rename_one('filetorename', 'renamedfile')
575
other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
576
other_tree.remove(['dirtoremove', 'filetoremove'])
577
self.build_tree_contents([
579
('other/filetomodify', 'new content'),
580
('other/newfile', 'new file content')])
581
other_tree.add('newfile')
582
other_tree.add('newdir/')
583
other_tree.commit('modify all sample files and dirs.')
586
this_tree.merge_from_branch(other_tree.branch)
587
reporter = CapturingReporter()
588
this_tree.commit('do the commit', reporter=reporter)
590
('change', 'modified', 'filetomodify'),
591
('change', 'added', 'newdir'),
592
('change', 'added', 'newfile'),
593
('renamed', 'renamed', 'dirtorename', 'renameddir'),
594
('renamed', 'renamed', 'filetorename', 'renamedfile'),
595
('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
596
('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
597
('deleted', 'dirtoremove'),
598
('deleted', 'filetoremove'),
600
result = set(reporter.calls)
601
missing = expected - result
602
new = result - expected
603
self.assertEqual((set(), set()), (missing, new))
605
def test_commit_removals_respects_filespec(self):
606
"""Commit respects the specified_files for removals."""
607
tree = self.make_branch_and_tree('.')
608
self.build_tree(['a', 'b'])
610
tree.commit('added a, b')
611
tree.remove(['a', 'b'])
612
tree.commit('removed a', specific_files='a')
613
basis = tree.basis_tree()
616
self.assertIs(None, basis.path2id('a'))
617
self.assertFalse(basis.path2id('b') is None)
621
def test_commit_saves_1ms_timestamp(self):
622
"""Passing in a timestamp is saved with 1ms resolution"""
623
tree = self.make_branch_and_tree('.')
624
self.build_tree(['a'])
626
tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
629
rev = tree.branch.repository.get_revision('a1')
630
self.assertEqual(1153248633.419, rev.timestamp)
632
def test_commit_has_1ms_resolution(self):
633
"""Allowing commit to generate the timestamp also has 1ms resolution"""
634
tree = self.make_branch_and_tree('.')
635
self.build_tree(['a'])
637
tree.commit('added a', rev_id='a1')
639
rev = tree.branch.repository.get_revision('a1')
640
timestamp = rev.timestamp
641
timestamp_1ms = round(timestamp, 3)
642
self.assertEqual(timestamp_1ms, timestamp)
644
def assertBasisTreeKind(self, kind, tree, file_id):
645
basis = tree.basis_tree()
648
self.assertEqual(kind, basis.kind(file_id))
652
def test_commit_kind_changes(self):
653
self.requireFeature(SymlinkFeature)
654
tree = self.make_branch_and_tree('.')
655
os.symlink('target', 'name')
656
tree.add('name', 'a-file-id')
657
tree.commit('Added a symlink')
658
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
661
self.build_tree(['name'])
662
tree.commit('Changed symlink to file')
663
self.assertBasisTreeKind('file', tree, 'a-file-id')
666
os.symlink('target', 'name')
667
tree.commit('file to symlink')
668
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
672
tree.commit('symlink to directory')
673
self.assertBasisTreeKind('directory', tree, 'a-file-id')
676
os.symlink('target', 'name')
677
tree.commit('directory to symlink')
678
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
680
# prepare for directory <-> file tests
683
tree.commit('symlink to directory')
684
self.assertBasisTreeKind('directory', tree, 'a-file-id')
687
self.build_tree(['name'])
688
tree.commit('Changed directory to file')
689
self.assertBasisTreeKind('file', tree, 'a-file-id')
693
tree.commit('file to directory')
694
self.assertBasisTreeKind('directory', tree, 'a-file-id')
696
def test_commit_unversioned_specified(self):
697
"""Commit should raise if specified files isn't in basis or worktree"""
698
tree = self.make_branch_and_tree('.')
699
self.assertRaises(errors.PathsNotVersionedError, tree.commit,
700
'message', specific_files=['bogus'])
702
class Callback(object):
704
def __init__(self, message, testcase):
706
self.message = message
707
self.testcase = testcase
709
def __call__(self, commit_obj):
711
self.testcase.assertTrue(isinstance(commit_obj, Commit))
714
def test_commit_callback(self):
715
"""Commit should invoke a callback to get the message"""
717
tree = self.make_branch_and_tree('.')
721
self.assertTrue(isinstance(e, BzrError))
722
self.assertEqual('The message or message_callback keyword'
723
' parameter is required for commit().', str(e))
725
self.fail('exception not raised')
726
cb = self.Callback(u'commit 1', self)
727
tree.commit(message_callback=cb)
728
self.assertTrue(cb.called)
729
repository = tree.branch.repository
730
message = repository.get_revision(tree.last_revision()).message
731
self.assertEqual('commit 1', message)
733
def test_no_callback_pointless(self):
734
"""Callback should not be invoked for pointless commit"""
735
tree = self.make_branch_and_tree('.')
736
cb = self.Callback(u'commit 2', self)
737
self.assertRaises(PointlessCommit, tree.commit, message_callback=cb,
738
allow_pointless=False)
739
self.assertFalse(cb.called)
741
def test_no_callback_netfailure(self):
742
"""Callback should not be invoked if connectivity fails"""
743
tree = self.make_branch_and_tree('.')
744
cb = self.Callback(u'commit 2', self)
745
repository = tree.branch.repository
746
# simulate network failure
747
def raise_(self, arg, arg2, arg3=None, arg4=None):
748
raise errors.NoSuchFile('foo')
749
repository.add_inventory = raise_
750
repository.add_inventory_by_delta = raise_
751
self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
752
self.assertFalse(cb.called)
754
def test_selected_file_merge_commit(self):
755
"""Ensure the correct error is raised"""
756
tree = self.make_branch_and_tree('foo')
757
# pending merge would turn into a left parent
758
tree.commit('commit 1')
759
tree.add_parent_tree_id('example')
760
self.build_tree(['foo/bar', 'foo/baz'])
761
tree.add(['bar', 'baz'])
762
err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
763
tree.commit, 'commit 2', specific_files=['bar', 'baz'])
764
self.assertEqual(['bar', 'baz'], err.files)
765
self.assertEqual('Selected-file commit of merges is not supported'
766
' yet: files bar, baz', str(err))
768
def test_commit_ordering(self):
769
"""Test of corner-case commit ordering error"""
770
tree = self.make_branch_and_tree('.')
771
self.build_tree(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
772
tree.add(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
774
self.build_tree(['a/c/d/'])
776
tree.rename_one('a/z/x', 'a/c/d/x')
777
tree.commit('test', specific_files=['a/z/y'])
779
def test_commit_no_author(self):
780
"""The default kwarg author in MutableTree.commit should not add
781
the 'author' revision property.
783
tree = self.make_branch_and_tree('foo')
784
rev_id = tree.commit('commit 1')
785
rev = tree.branch.repository.get_revision(rev_id)
786
self.assertFalse('author' in rev.properties)
787
self.assertFalse('authors' in rev.properties)
789
def test_commit_author(self):
790
"""Passing a non-empty author kwarg to MutableTree.commit should add
791
the 'author' revision property.
793
tree = self.make_branch_and_tree('foo')
794
rev_id = self.callDeprecated(['The parameter author was '
795
'deprecated in version 1.13. Use authors instead'],
796
tree.commit, 'commit 1', author='John Doe <jdoe@example.com>')
797
rev = tree.branch.repository.get_revision(rev_id)
798
self.assertEqual('John Doe <jdoe@example.com>',
799
rev.properties['authors'])
800
self.assertFalse('author' in rev.properties)
802
def test_commit_empty_authors_list(self):
803
"""Passing an empty list to authors shouldn't add the property."""
804
tree = self.make_branch_and_tree('foo')
805
rev_id = tree.commit('commit 1', authors=[])
806
rev = tree.branch.repository.get_revision(rev_id)
807
self.assertFalse('author' in rev.properties)
808
self.assertFalse('authors' in rev.properties)
810
def test_multiple_authors(self):
811
tree = self.make_branch_and_tree('foo')
812
rev_id = tree.commit('commit 1',
813
authors=['John Doe <jdoe@example.com>',
814
'Jane Rey <jrey@example.com>'])
815
rev = tree.branch.repository.get_revision(rev_id)
816
self.assertEqual('John Doe <jdoe@example.com>\n'
817
'Jane Rey <jrey@example.com>', rev.properties['authors'])
818
self.assertFalse('author' in rev.properties)
820
def test_author_and_authors_incompatible(self):
821
tree = self.make_branch_and_tree('foo')
822
self.assertRaises(AssertionError, tree.commit, 'commit 1',
823
authors=['John Doe <jdoe@example.com>',
824
'Jane Rey <jrey@example.com>'],
825
author="Jack Me <jme@example.com>")
827
def test_author_with_newline_rejected(self):
828
tree = self.make_branch_and_tree('foo')
829
self.assertRaises(AssertionError, tree.commit, 'commit 1',
830
authors=['John\nDoe <jdoe@example.com>'])
832
def test_commit_with_checkout_and_branch_sharing_repo(self):
833
repo = self.make_repository('repo', shared=True)
834
# make_branch_and_tree ignores shared repos
835
branch = bzrdir.BzrDir.create_branch_convenience('repo/branch')
836
tree2 = branch.create_checkout('repo/tree2')
837
tree2.commit('message', rev_id='rev1')
838
self.assertTrue(tree2.branch.repository.has_revision('rev1'))