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