387
430
bound = master.sprout('bound')
388
431
wt = bound.open_workingtree()
389
432
wt.branch.set_bound_location(os.path.realpath('master'))
434
orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
390
435
master_branch.lock_write()
437
lockdir._DEFAULT_TIMEOUT_SECONDS = 1
392
438
self.assertRaises(LockContention, wt.commit, 'silly')
440
lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
394
441
master_branch.unlock()
443
def test_commit_bound_merge(self):
444
# see bug #43959; commit of a merge in a bound branch fails to push
445
# the new commit into the master
446
master_branch = self.make_branch('master')
447
bound_tree = self.make_branch_and_tree('bound')
448
bound_tree.branch.bind(master_branch)
450
self.build_tree_contents([('bound/content_file', 'initial contents\n')])
451
bound_tree.add(['content_file'])
452
bound_tree.commit(message='woo!')
454
other_bzrdir = master_branch.bzrdir.sprout('other')
455
other_tree = other_bzrdir.open_workingtree()
457
# do a commit to the the other branch changing the content file so
458
# that our commit after merging will have a merged revision in the
459
# content file history.
460
self.build_tree_contents([('other/content_file', 'change in other\n')])
461
other_tree.commit('change in other')
463
# do a merge into the bound branch from other, and then change the
464
# content file locally to force a new revision (rather than using the
465
# revision from other). This forces extra processing in commit.
466
bound_tree.merge_from_branch(other_tree.branch)
467
self.build_tree_contents([('bound/content_file', 'change in bound\n')])
469
# before #34959 was fixed, this failed with 'revision not present in
470
# weave' when trying to implicitly push from the bound branch to the master
471
bound_tree.commit(message='commit of merge in bound tree')
473
def test_commit_reporting_after_merge(self):
474
# when doing a commit of a merge, the reporter needs to still
475
# be called for each item that is added/removed/deleted.
476
this_tree = self.make_branch_and_tree('this')
477
# we need a bunch of files and dirs, to perform one action on each.
480
'this/dirtoreparent/',
483
'this/filetoreparent',
500
this_tree.commit('create_files')
501
other_dir = this_tree.bzrdir.sprout('other')
502
other_tree = other_dir.open_workingtree()
503
other_tree.lock_write()
504
# perform the needed actions on the files and dirs.
506
other_tree.rename_one('dirtorename', 'renameddir')
507
other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
508
other_tree.rename_one('filetorename', 'renamedfile')
509
other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
510
other_tree.remove(['dirtoremove', 'filetoremove'])
511
self.build_tree_contents([
513
('other/filetomodify', 'new content'),
514
('other/newfile', 'new file content')])
515
other_tree.add('newfile')
516
other_tree.add('newdir/')
517
other_tree.commit('modify all sample files and dirs.')
520
this_tree.merge_from_branch(other_tree.branch)
521
reporter = CapturingReporter()
522
this_tree.commit('do the commit', reporter=reporter)
524
('change', 'unchanged', ''),
525
('change', 'unchanged', 'dirtoleave'),
526
('change', 'unchanged', 'filetoleave'),
527
('change', 'modified', 'filetomodify'),
528
('change', 'added', 'newdir'),
529
('change', 'added', 'newfile'),
530
('renamed', 'renamed', 'dirtorename', 'renameddir'),
531
('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
532
('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
533
('renamed', 'renamed', 'filetorename', 'renamedfile'),
534
('deleted', 'dirtoremove'),
535
('deleted', 'filetoremove'),
539
def test_commit_removals_respects_filespec(self):
540
"""Commit respects the specified_files for removals."""
541
tree = self.make_branch_and_tree('.')
542
self.build_tree(['a', 'b'])
544
tree.commit('added a, b')
545
tree.remove(['a', 'b'])
546
tree.commit('removed a', specific_files='a')
547
basis = tree.basis_tree()
550
self.assertIs(None, basis.path2id('a'))
551
self.assertFalse(basis.path2id('b') is None)
555
def test_commit_saves_1ms_timestamp(self):
556
"""Passing in a timestamp is saved with 1ms resolution"""
557
tree = self.make_branch_and_tree('.')
558
self.build_tree(['a'])
560
tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
563
rev = tree.branch.repository.get_revision('a1')
564
self.assertEqual(1153248633.419, rev.timestamp)
566
def test_commit_has_1ms_resolution(self):
567
"""Allowing commit to generate the timestamp also has 1ms resolution"""
568
tree = self.make_branch_and_tree('.')
569
self.build_tree(['a'])
571
tree.commit('added a', rev_id='a1')
573
rev = tree.branch.repository.get_revision('a1')
574
timestamp = rev.timestamp
575
timestamp_1ms = round(timestamp, 3)
576
self.assertEqual(timestamp_1ms, timestamp)
578
def assertBasisTreeKind(self, kind, tree, file_id):
579
basis = tree.basis_tree()
582
self.assertEqual(kind, basis.kind(file_id))
586
def test_commit_kind_changes(self):
587
if not osutils.has_symlinks():
588
raise tests.TestSkipped('Test requires symlink support')
589
tree = self.make_branch_and_tree('.')
590
os.symlink('target', 'name')
591
tree.add('name', 'a-file-id')
592
tree.commit('Added a symlink')
593
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
596
self.build_tree(['name'])
597
tree.commit('Changed symlink to file')
598
self.assertBasisTreeKind('file', tree, 'a-file-id')
601
os.symlink('target', 'name')
602
tree.commit('file to symlink')
603
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
607
tree.commit('symlink to directory')
608
self.assertBasisTreeKind('directory', tree, 'a-file-id')
611
os.symlink('target', 'name')
612
tree.commit('directory to symlink')
613
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
615
# prepare for directory <-> file tests
618
tree.commit('symlink to directory')
619
self.assertBasisTreeKind('directory', tree, 'a-file-id')
622
self.build_tree(['name'])
623
tree.commit('Changed directory to file')
624
self.assertBasisTreeKind('file', tree, 'a-file-id')
628
tree.commit('file to directory')
629
self.assertBasisTreeKind('directory', tree, 'a-file-id')
631
def test_commit_unversioned_specified(self):
632
"""Commit should raise if specified files isn't in basis or worktree"""
633
tree = self.make_branch_and_tree('.')
634
self.assertRaises(errors.PathsNotVersionedError, tree.commit,
635
'message', specific_files=['bogus'])
637
class Callback(object):
639
def __init__(self, message, testcase):
641
self.message = message
642
self.testcase = testcase
644
def __call__(self, commit_obj):
646
self.testcase.assertTrue(isinstance(commit_obj, Commit))
649
def test_commit_callback(self):
650
"""Commit should invoke a callback to get the message"""
652
tree = self.make_branch_and_tree('.')
656
self.assertTrue(isinstance(e, BzrError))
657
self.assertEqual('The message or message_callback keyword'
658
' parameter is required for commit().', str(e))
660
self.fail('exception not raised')
661
cb = self.Callback(u'commit 1', self)
662
tree.commit(message_callback=cb)
663
self.assertTrue(cb.called)
664
repository = tree.branch.repository
665
message = repository.get_revision(tree.last_revision()).message
666
self.assertEqual('commit 1', message)
668
def test_no_callback_pointless(self):
669
"""Callback should not be invoked for pointless commit"""
670
tree = self.make_branch_and_tree('.')
671
cb = self.Callback(u'commit 2', self)
672
self.assertRaises(PointlessCommit, tree.commit, message_callback=cb,
673
allow_pointless=False)
674
self.assertFalse(cb.called)
676
def test_no_callback_netfailure(self):
677
"""Callback should not be invoked if connectivity fails"""
678
tree = self.make_branch_and_tree('.')
679
cb = self.Callback(u'commit 2', self)
680
repository = tree.branch.repository
681
# simulate network failure
682
def raise_(self, arg, arg2):
683
raise errors.NoSuchFile('foo')
684
repository.add_inventory = raise_
685
self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
686
self.assertFalse(cb.called)
688
def test_selected_file_merge_commit(self):
689
"""Ensure the correct error is raised"""
690
tree = self.make_branch_and_tree('foo')
691
# pending merge would turn into a left parent
692
tree.commit('commit 1')
693
tree.add_parent_tree_id('example')
694
self.build_tree(['foo/bar', 'foo/baz'])
695
tree.add(['bar', 'baz'])
696
err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
697
tree.commit, 'commit 2', specific_files=['bar', 'baz'])
698
self.assertEqual(['bar', 'baz'], err.files)
699
self.assertEqual('Selected-file commit of merges is not supported'
700
' yet: files bar, baz', str(err))
702
def test_commit_no_author(self):
703
"""The default kwarg author in MutableTree.commit should not add
704
the 'author' revision property.
706
tree = self.make_branch_and_tree('foo')
707
rev_id = tree.commit('commit 1')
708
rev = tree.branch.repository.get_revision(rev_id)
709
self.assertFalse('author' in rev.properties)
711
def test_commit_author(self):
712
"""Passing a non-empty author kwarg to MutableTree.commit should add
713
the 'author' revision property.
715
tree = self.make_branch_and_tree('foo')
716
rev_id = tree.commit('commit 1', author='John Doe <jdoe@example.com>')
717
rev = tree.branch.repository.get_revision(rev_id)
718
self.assertEqual('John Doe <jdoe@example.com>',
719
rev.properties['author'])