392
449
self.assertRaises(LockContention, wt.commit, 'silly')
394
451
master_branch.unlock()
453
def test_commit_bound_merge(self):
454
# see bug #43959; commit of a merge in a bound branch fails to push
455
# the new commit into the master
456
master_branch = self.make_branch('master')
457
bound_tree = self.make_branch_and_tree('bound')
458
bound_tree.branch.bind(master_branch)
460
self.build_tree_contents([('bound/content_file', 'initial contents\n')])
461
bound_tree.add(['content_file'])
462
bound_tree.commit(message='woo!')
464
other_bzrdir = master_branch.bzrdir.sprout('other')
465
other_tree = other_bzrdir.open_workingtree()
467
# do a commit to the the other branch changing the content file so
468
# that our commit after merging will have a merged revision in the
469
# content file history.
470
self.build_tree_contents([('other/content_file', 'change in other\n')])
471
other_tree.commit('change in other')
473
# do a merge into the bound branch from other, and then change the
474
# content file locally to force a new revision (rather than using the
475
# revision from other). This forces extra processing in commit.
476
bound_tree.merge_from_branch(other_tree.branch)
477
self.build_tree_contents([('bound/content_file', 'change in bound\n')])
479
# before #34959 was fixed, this failed with 'revision not present in
480
# weave' when trying to implicitly push from the bound branch to the master
481
bound_tree.commit(message='commit of merge in bound tree')
483
def test_commit_reporting_after_merge(self):
484
# when doing a commit of a merge, the reporter needs to still
485
# be called for each item that is added/removed/deleted.
486
this_tree = self.make_branch_and_tree('this')
487
# we need a bunch of files and dirs, to perform one action on each.
490
'this/dirtoreparent/',
493
'this/filetoreparent',
510
this_tree.commit('create_files')
511
other_dir = this_tree.bzrdir.sprout('other')
512
other_tree = other_dir.open_workingtree()
513
other_tree.lock_write()
514
# perform the needed actions on the files and dirs.
516
other_tree.rename_one('dirtorename', 'renameddir')
517
other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
518
other_tree.rename_one('filetorename', 'renamedfile')
519
other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
520
other_tree.remove(['dirtoremove', 'filetoremove'])
521
self.build_tree_contents([
523
('other/filetomodify', 'new content'),
524
('other/newfile', 'new file content')])
525
other_tree.add('newfile')
526
other_tree.add('newdir/')
527
other_tree.commit('modify all sample files and dirs.')
530
this_tree.merge_from_branch(other_tree.branch)
531
reporter = CapturingReporter()
532
this_tree.commit('do the commit', reporter=reporter)
534
('change', 'unchanged', ''),
535
('change', 'unchanged', 'dirtoleave'),
536
('change', 'unchanged', 'filetoleave'),
537
('change', 'modified', 'filetomodify'),
538
('change', 'added', 'newdir'),
539
('change', 'added', 'newfile'),
540
('renamed', 'renamed', 'dirtorename', 'renameddir'),
541
('renamed', 'renamed', 'filetorename', 'renamedfile'),
542
('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
543
('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
544
('deleted', 'dirtoremove'),
545
('deleted', 'filetoremove'),
549
def test_commit_removals_respects_filespec(self):
550
"""Commit respects the specified_files for removals."""
551
tree = self.make_branch_and_tree('.')
552
self.build_tree(['a', 'b'])
554
tree.commit('added a, b')
555
tree.remove(['a', 'b'])
556
tree.commit('removed a', specific_files='a')
557
basis = tree.basis_tree()
560
self.assertIs(None, basis.path2id('a'))
561
self.assertFalse(basis.path2id('b') is None)
565
def test_commit_saves_1ms_timestamp(self):
566
"""Passing in a timestamp is saved with 1ms resolution"""
567
tree = self.make_branch_and_tree('.')
568
self.build_tree(['a'])
570
tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
573
rev = tree.branch.repository.get_revision('a1')
574
self.assertEqual(1153248633.419, rev.timestamp)
576
def test_commit_has_1ms_resolution(self):
577
"""Allowing commit to generate the timestamp also has 1ms resolution"""
578
tree = self.make_branch_and_tree('.')
579
self.build_tree(['a'])
581
tree.commit('added a', rev_id='a1')
583
rev = tree.branch.repository.get_revision('a1')
584
timestamp = rev.timestamp
585
timestamp_1ms = round(timestamp, 3)
586
self.assertEqual(timestamp_1ms, timestamp)
588
def assertBasisTreeKind(self, kind, tree, file_id):
589
basis = tree.basis_tree()
592
self.assertEqual(kind, basis.kind(file_id))
596
def test_commit_kind_changes(self):
597
self.requireFeature(SymlinkFeature)
598
tree = self.make_branch_and_tree('.')
599
os.symlink('target', 'name')
600
tree.add('name', 'a-file-id')
601
tree.commit('Added a symlink')
602
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
605
self.build_tree(['name'])
606
tree.commit('Changed symlink to file')
607
self.assertBasisTreeKind('file', tree, 'a-file-id')
610
os.symlink('target', 'name')
611
tree.commit('file to symlink')
612
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
616
tree.commit('symlink to directory')
617
self.assertBasisTreeKind('directory', tree, 'a-file-id')
620
os.symlink('target', 'name')
621
tree.commit('directory to symlink')
622
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
624
# prepare for directory <-> file tests
627
tree.commit('symlink to directory')
628
self.assertBasisTreeKind('directory', tree, 'a-file-id')
631
self.build_tree(['name'])
632
tree.commit('Changed directory to file')
633
self.assertBasisTreeKind('file', tree, 'a-file-id')
637
tree.commit('file to directory')
638
self.assertBasisTreeKind('directory', tree, 'a-file-id')
640
def test_commit_unversioned_specified(self):
641
"""Commit should raise if specified files isn't in basis or worktree"""
642
tree = self.make_branch_and_tree('.')
643
self.assertRaises(errors.PathsNotVersionedError, tree.commit,
644
'message', specific_files=['bogus'])
646
class Callback(object):
648
def __init__(self, message, testcase):
650
self.message = message
651
self.testcase = testcase
653
def __call__(self, commit_obj):
655
self.testcase.assertTrue(isinstance(commit_obj, Commit))
658
def test_commit_callback(self):
659
"""Commit should invoke a callback to get the message"""
661
tree = self.make_branch_and_tree('.')
665
self.assertTrue(isinstance(e, BzrError))
666
self.assertEqual('The message or message_callback keyword'
667
' parameter is required for commit().', str(e))
669
self.fail('exception not raised')
670
cb = self.Callback(u'commit 1', self)
671
tree.commit(message_callback=cb)
672
self.assertTrue(cb.called)
673
repository = tree.branch.repository
674
message = repository.get_revision(tree.last_revision()).message
675
self.assertEqual('commit 1', message)
677
def test_no_callback_pointless(self):
678
"""Callback should not be invoked for pointless commit"""
679
tree = self.make_branch_and_tree('.')
680
cb = self.Callback(u'commit 2', self)
681
self.assertRaises(PointlessCommit, tree.commit, message_callback=cb,
682
allow_pointless=False)
683
self.assertFalse(cb.called)
685
def test_no_callback_netfailure(self):
686
"""Callback should not be invoked if connectivity fails"""
687
tree = self.make_branch_and_tree('.')
688
cb = self.Callback(u'commit 2', self)
689
repository = tree.branch.repository
690
# simulate network failure
691
def raise_(self, arg, arg2):
692
raise errors.NoSuchFile('foo')
693
repository.add_inventory = raise_
694
self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
695
self.assertFalse(cb.called)
697
def test_selected_file_merge_commit(self):
698
"""Ensure the correct error is raised"""
699
tree = self.make_branch_and_tree('foo')
700
# pending merge would turn into a left parent
701
tree.commit('commit 1')
702
tree.add_parent_tree_id('example')
703
self.build_tree(['foo/bar', 'foo/baz'])
704
tree.add(['bar', 'baz'])
705
err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
706
tree.commit, 'commit 2', specific_files=['bar', 'baz'])
707
self.assertEqual(['bar', 'baz'], err.files)
708
self.assertEqual('Selected-file commit of merges is not supported'
709
' yet: files bar, baz', str(err))
711
def test_commit_ordering(self):
712
"""Test of corner-case commit ordering error"""
713
tree = self.make_branch_and_tree('.')
714
self.build_tree(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
715
tree.add(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
717
self.build_tree(['a/c/d/'])
719
tree.rename_one('a/z/x', 'a/c/d/x')
720
tree.commit('test', specific_files=['a/z/y'])
722
def test_commit_no_author(self):
723
"""The default kwarg author in MutableTree.commit should not add
724
the 'author' revision property.
726
tree = self.make_branch_and_tree('foo')
727
rev_id = tree.commit('commit 1')
728
rev = tree.branch.repository.get_revision(rev_id)
729
self.assertFalse('author' in rev.properties)
731
def test_commit_author(self):
732
"""Passing a non-empty author kwarg to MutableTree.commit should add
733
the 'author' revision property.
735
tree = self.make_branch_and_tree('foo')
736
rev_id = tree.commit('commit 1', author='John Doe <jdoe@example.com>')
737
rev = tree.branch.repository.get_revision(rev_id)
738
self.assertEqual('John Doe <jdoe@example.com>',
739
rev.properties['author'])
741
def test_commit_with_checkout_and_branch_sharing_repo(self):
742
repo = self.make_repository('repo', shared=True)
743
# make_branch_and_tree ignores shared repos
744
branch = bzrdir.BzrDir.create_branch_convenience('repo/branch')
745
tree2 = branch.create_checkout('repo/tree2')
746
tree2.commit('message', rev_id='rev1')
747
self.assertTrue(tree2.branch.repository.has_revision('rev1'))