21
from bzrlib.tests import TestCaseWithTransport
25
22
from bzrlib.branch import Branch
26
23
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
24
from bzrlib.workingtree import WorkingTree
27
25
from bzrlib.commit import Commit, NullCommitReporter
28
26
from bzrlib.config import BranchConfig
29
27
from bzrlib.errors import (PointlessCommit, BzrError, SigningFailed,
31
from bzrlib.tests import TestCaseWithTransport
32
from bzrlib.workingtree import WorkingTree
35
31
# TODO: Test commit with some added, and added-but-missing files
411
407
bound = master.sprout('bound')
412
408
wt = bound.open_workingtree()
413
409
wt.branch.set_bound_location(os.path.realpath('master'))
415
orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
416
410
master_branch.lock_write()
418
lockdir._DEFAULT_TIMEOUT_SECONDS = 1
419
412
self.assertRaises(LockContention, wt.commit, 'silly')
421
lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
422
414
master_branch.unlock()
424
416
def test_commit_bound_merge(self):
444
436
# do a merge into the bound branch from other, and then change the
445
437
# content file locally to force a new revision (rather than using the
446
438
# revision from other). This forces extra processing in commit.
447
bound_tree.merge_from_branch(other_tree.branch)
439
self.merge(other_tree.branch, bound_tree)
448
440
self.build_tree_contents([('bound/content_file', 'change in bound\n')])
450
442
# before #34959 was fixed, this failed with 'revision not present in
498
490
other_tree.commit('modify all sample files and dirs.')
500
492
other_tree.unlock()
501
this_tree.merge_from_branch(other_tree.branch)
493
self.merge(other_tree.branch, this_tree)
502
494
reporter = CapturingReporter()
503
495
this_tree.commit('do the commit', reporter=reporter)
504
496
self.assertEqual([
505
('change', 'unchanged', ''),
506
497
('change', 'unchanged', 'dirtoleave'),
507
498
('change', 'unchanged', 'filetoleave'),
508
499
('change', 'modified', 'filetomodify'),
524
515
tree.add(['a', 'b'])
525
516
tree.commit('added a, b')
526
517
tree.remove(['a', 'b'])
527
tree.commit('removed a', specific_files='a')
518
Commit().commit(message='removed a', working_tree=tree,
528
520
basis = tree.basis_tree().inventory
529
521
self.assertIs(None, basis.path2id('a'))
530
522
self.assertFalse(basis.path2id('b') is None)
551
543
timestamp = rev.timestamp
552
544
timestamp_1ms = round(timestamp, 3)
553
545
self.assertEqual(timestamp_1ms, timestamp)
555
def test_commit_unversioned_specified(self):
556
"""Commit should raise if specified files isn't in basis or worktree"""
557
tree = self.make_branch_and_tree('.')
558
self.assertRaises(errors.PathsNotVersionedError, tree.commit,
559
'message', specific_files=['bogus'])
561
class Callback(object):
563
def __init__(self, message, testcase):
565
self.message = message
566
self.testcase = testcase
568
def __call__(self, commit_obj):
570
self.testcase.assertTrue(isinstance(commit_obj, Commit))
573
def test_commit_callback(self):
574
"""Commit should invoke a callback to get the message"""
576
tree = self.make_branch_and_tree('.')
580
self.assertTrue(isinstance(e, BzrError))
581
self.assertEqual('The message or message_callback keyword'
582
' parameter is required for commit().', str(e))
584
self.fail('exception not raised')
585
cb = self.Callback(u'commit 1', self)
586
tree.commit(message_callback=cb)
587
self.assertTrue(cb.called)
588
repository = tree.branch.repository
589
message = repository.get_revision(tree.last_revision()).message
590
self.assertEqual('commit 1', message)
592
def test_no_callback_pointless(self):
593
"""Callback should not be invoked for pointless commit"""
594
tree = self.make_branch_and_tree('.')
595
cb = self.Callback(u'commit 2', self)
596
self.assertRaises(PointlessCommit, tree.commit, message_callback=cb,
597
allow_pointless=False)
598
self.assertFalse(cb.called)
600
def test_no_callback_netfailure(self):
601
"""Callback should not be invoked if connectivity fails"""
602
tree = self.make_branch_and_tree('.')
603
cb = self.Callback(u'commit 2', self)
604
repository = tree.branch.repository
605
# simulate network failure
606
def raise_(self, arg, arg2):
607
raise errors.NoSuchFile('foo')
608
repository.add_inventory = raise_
609
self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
610
self.assertFalse(cb.called)