387
443
bound = master.sprout('bound')
388
444
wt = bound.open_workingtree()
389
445
wt.branch.set_bound_location(os.path.realpath('master'))
447
orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
390
448
master_branch.lock_write()
450
lockdir._DEFAULT_TIMEOUT_SECONDS = 1
392
451
self.assertRaises(LockContention, wt.commit, 'silly')
453
lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
394
454
master_branch.unlock()
456
def test_commit_bound_merge(self):
457
# see bug #43959; commit of a merge in a bound branch fails to push
458
# the new commit into the master
459
master_branch = self.make_branch('master')
460
bound_tree = self.make_branch_and_tree('bound')
461
bound_tree.branch.bind(master_branch)
463
self.build_tree_contents([('bound/content_file', 'initial contents\n')])
464
bound_tree.add(['content_file'])
465
bound_tree.commit(message='woo!')
467
other_bzrdir = master_branch.bzrdir.sprout('other')
468
other_tree = other_bzrdir.open_workingtree()
470
# do a commit to the the other branch changing the content file so
471
# that our commit after merging will have a merged revision in the
472
# content file history.
473
self.build_tree_contents([('other/content_file', 'change in other\n')])
474
other_tree.commit('change in other')
476
# do a merge into the bound branch from other, and then change the
477
# content file locally to force a new revision (rather than using the
478
# revision from other). This forces extra processing in commit.
479
bound_tree.merge_from_branch(other_tree.branch)
480
self.build_tree_contents([('bound/content_file', 'change in bound\n')])
482
# before #34959 was fixed, this failed with 'revision not present in
483
# weave' when trying to implicitly push from the bound branch to the master
484
bound_tree.commit(message='commit of merge in bound tree')
486
def test_commit_reporting_after_merge(self):
487
# when doing a commit of a merge, the reporter needs to still
488
# be called for each item that is added/removed/deleted.
489
this_tree = self.make_branch_and_tree('this')
490
# we need a bunch of files and dirs, to perform one action on each.
493
'this/dirtoreparent/',
496
'this/filetoreparent',
513
this_tree.commit('create_files')
514
other_dir = this_tree.bzrdir.sprout('other')
515
other_tree = other_dir.open_workingtree()
516
other_tree.lock_write()
517
# perform the needed actions on the files and dirs.
519
other_tree.rename_one('dirtorename', 'renameddir')
520
other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
521
other_tree.rename_one('filetorename', 'renamedfile')
522
other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
523
other_tree.remove(['dirtoremove', 'filetoremove'])
524
self.build_tree_contents([
526
('other/filetomodify', 'new content'),
527
('other/newfile', 'new file content')])
528
other_tree.add('newfile')
529
other_tree.add('newdir/')
530
other_tree.commit('modify all sample files and dirs.')
533
this_tree.merge_from_branch(other_tree.branch)
534
reporter = CapturingReporter()
535
this_tree.commit('do the commit', reporter=reporter)
537
('change', 'unchanged', ''),
538
('change', 'unchanged', 'dirtoleave'),
539
('change', 'unchanged', 'filetoleave'),
540
('change', 'modified', 'filetomodify'),
541
('change', 'added', 'newdir'),
542
('change', 'added', 'newfile'),
543
('renamed', 'renamed', 'dirtorename', 'renameddir'),
544
('renamed', 'renamed', 'filetorename', 'renamedfile'),
545
('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
546
('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
547
('deleted', 'dirtoremove'),
548
('deleted', 'filetoremove'),
552
def test_commit_removals_respects_filespec(self):
553
"""Commit respects the specified_files for removals."""
554
tree = self.make_branch_and_tree('.')
555
self.build_tree(['a', 'b'])
557
tree.commit('added a, b')
558
tree.remove(['a', 'b'])
559
tree.commit('removed a', specific_files='a')
560
basis = tree.basis_tree()
563
self.assertIs(None, basis.path2id('a'))
564
self.assertFalse(basis.path2id('b') is None)
568
def test_commit_saves_1ms_timestamp(self):
569
"""Passing in a timestamp is saved with 1ms resolution"""
570
tree = self.make_branch_and_tree('.')
571
self.build_tree(['a'])
573
tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
576
rev = tree.branch.repository.get_revision('a1')
577
self.assertEqual(1153248633.419, rev.timestamp)
579
def test_commit_has_1ms_resolution(self):
580
"""Allowing commit to generate the timestamp also has 1ms resolution"""
581
tree = self.make_branch_and_tree('.')
582
self.build_tree(['a'])
584
tree.commit('added a', rev_id='a1')
586
rev = tree.branch.repository.get_revision('a1')
587
timestamp = rev.timestamp
588
timestamp_1ms = round(timestamp, 3)
589
self.assertEqual(timestamp_1ms, timestamp)
591
def assertBasisTreeKind(self, kind, tree, file_id):
592
basis = tree.basis_tree()
595
self.assertEqual(kind, basis.kind(file_id))
599
def test_commit_kind_changes(self):
600
self.requireFeature(SymlinkFeature)
601
tree = self.make_branch_and_tree('.')
602
os.symlink('target', 'name')
603
tree.add('name', 'a-file-id')
604
tree.commit('Added a symlink')
605
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
608
self.build_tree(['name'])
609
tree.commit('Changed symlink to file')
610
self.assertBasisTreeKind('file', tree, 'a-file-id')
613
os.symlink('target', 'name')
614
tree.commit('file to symlink')
615
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
619
tree.commit('symlink to directory')
620
self.assertBasisTreeKind('directory', tree, 'a-file-id')
623
os.symlink('target', 'name')
624
tree.commit('directory to symlink')
625
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
627
# prepare for directory <-> file tests
630
tree.commit('symlink to directory')
631
self.assertBasisTreeKind('directory', tree, 'a-file-id')
634
self.build_tree(['name'])
635
tree.commit('Changed directory to file')
636
self.assertBasisTreeKind('file', tree, 'a-file-id')
640
tree.commit('file to directory')
641
self.assertBasisTreeKind('directory', tree, 'a-file-id')
643
def test_commit_unversioned_specified(self):
644
"""Commit should raise if specified files isn't in basis or worktree"""
645
tree = self.make_branch_and_tree('.')
646
self.assertRaises(errors.PathsNotVersionedError, tree.commit,
647
'message', specific_files=['bogus'])
649
class Callback(object):
651
def __init__(self, message, testcase):
653
self.message = message
654
self.testcase = testcase
656
def __call__(self, commit_obj):
658
self.testcase.assertTrue(isinstance(commit_obj, Commit))
661
def test_commit_callback(self):
662
"""Commit should invoke a callback to get the message"""
664
tree = self.make_branch_and_tree('.')
668
self.assertTrue(isinstance(e, BzrError))
669
self.assertEqual('The message or message_callback keyword'
670
' parameter is required for commit().', str(e))
672
self.fail('exception not raised')
673
cb = self.Callback(u'commit 1', self)
674
tree.commit(message_callback=cb)
675
self.assertTrue(cb.called)
676
repository = tree.branch.repository
677
message = repository.get_revision(tree.last_revision()).message
678
self.assertEqual('commit 1', message)
680
def test_no_callback_pointless(self):
681
"""Callback should not be invoked for pointless commit"""
682
tree = self.make_branch_and_tree('.')
683
cb = self.Callback(u'commit 2', self)
684
self.assertRaises(PointlessCommit, tree.commit, message_callback=cb,
685
allow_pointless=False)
686
self.assertFalse(cb.called)
688
def test_no_callback_netfailure(self):
689
"""Callback should not be invoked if connectivity fails"""
690
tree = self.make_branch_and_tree('.')
691
cb = self.Callback(u'commit 2', self)
692
repository = tree.branch.repository
693
# simulate network failure
694
def raise_(self, arg, arg2):
695
raise errors.NoSuchFile('foo')
696
repository.add_inventory = raise_
697
self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
698
self.assertFalse(cb.called)
700
def test_selected_file_merge_commit(self):
701
"""Ensure the correct error is raised"""
702
tree = self.make_branch_and_tree('foo')
703
# pending merge would turn into a left parent
704
tree.commit('commit 1')
705
tree.add_parent_tree_id('example')
706
self.build_tree(['foo/bar', 'foo/baz'])
707
tree.add(['bar', 'baz'])
708
err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
709
tree.commit, 'commit 2', specific_files=['bar', 'baz'])
710
self.assertEqual(['bar', 'baz'], err.files)
711
self.assertEqual('Selected-file commit of merges is not supported'
712
' yet: files bar, baz', str(err))
714
def test_commit_ordering(self):
715
"""Test of corner-case commit ordering error"""
716
tree = self.make_branch_and_tree('.')
717
self.build_tree(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
718
tree.add(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
720
self.build_tree(['a/c/d/'])
722
tree.rename_one('a/z/x', 'a/c/d/x')
723
tree.commit('test', specific_files=['a/z/y'])
725
def test_commit_no_author(self):
726
"""The default kwarg author in MutableTree.commit should not add
727
the 'author' revision property.
729
tree = self.make_branch_and_tree('foo')
730
rev_id = tree.commit('commit 1')
731
rev = tree.branch.repository.get_revision(rev_id)
732
self.assertFalse('author' in rev.properties)
734
def test_commit_author(self):
735
"""Passing a non-empty author kwarg to MutableTree.commit should add
736
the 'author' revision property.
738
tree = self.make_branch_and_tree('foo')
739
rev_id = tree.commit('commit 1', author='John Doe <jdoe@example.com>')
740
rev = tree.branch.repository.get_revision(rev_id)
741
self.assertEqual('John Doe <jdoe@example.com>',
742
rev.properties['author'])