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