72
101
def test_delete_commit(self):
73
102
"""Test a commit with a deleted file"""
74
b = Branch.initialize(u'.')
103
wt = self.make_branch_and_tree('.')
75
105
file('hello', 'w').write('hello world')
76
b.working_tree().add(['hello'], ['hello-id'])
77
b.working_tree().commit(message='add hello')
106
wt.add(['hello'], ['hello-id'])
107
wt.commit(message='add hello')
79
109
os.remove('hello')
80
b.working_tree().commit('removed hello', rev_id='rev2')
110
wt.commit('removed hello', rev_id='rev2')
82
112
tree = b.repository.revision_tree('rev2')
83
113
self.assertFalse(tree.has_id('hello-id'))
85
115
def test_pointless_commit(self):
86
116
"""Commit refuses unless there are changes or it's forced."""
87
b = Branch.initialize(u'.')
117
wt = self.make_branch_and_tree('.')
88
119
file('hello', 'w').write('hello')
89
b.working_tree().add(['hello'])
90
b.working_tree().commit(message='add hello')
121
wt.commit(message='add hello')
91
122
self.assertEquals(b.revno(), 1)
92
123
self.assertRaises(PointlessCommit,
93
b.working_tree().commit,
95
126
allow_pointless=False)
96
127
self.assertEquals(b.revno(), 1)
98
129
def test_commit_empty(self):
99
130
"""Commiting an empty tree works."""
100
b = Branch.initialize(u'.')
101
b.working_tree().commit(message='empty tree', allow_pointless=True)
131
wt = self.make_branch_and_tree('.')
133
wt.commit(message='empty tree', allow_pointless=True)
102
134
self.assertRaises(PointlessCommit,
103
b.working_tree().commit,
104
136
message='empty tree',
105
137
allow_pointless=False)
106
b.working_tree().commit(message='empty tree', allow_pointless=True)
138
wt.commit(message='empty tree', allow_pointless=True)
107
139
self.assertEquals(b.revno(), 2)
110
141
def test_selective_delete(self):
111
142
"""Selective commit in tree with deletions"""
112
b = Branch.initialize(u'.')
143
wt = self.make_branch_and_tree('.')
113
145
file('hello', 'w').write('hello')
114
146
file('buongia', 'w').write('buongia')
115
b.working_tree().add(['hello', 'buongia'],
147
wt.add(['hello', 'buongia'],
116
148
['hello-id', 'buongia-id'])
117
b.working_tree().commit(message='add files',
149
wt.commit(message='add files',
118
150
rev_id='test@rev-1')
120
152
os.remove('hello')
121
153
file('buongia', 'w').write('new text')
122
b.working_tree().commit(message='update text',
154
wt.commit(message='update text',
123
155
specific_files=['buongia'],
124
156
allow_pointless=False,
125
157
rev_id='test@rev-2')
127
b.working_tree().commit(message='remove hello',
159
wt.commit(message='remove hello',
128
160
specific_files=['hello'],
129
161
allow_pointless=False,
130
162
rev_id='test@rev-3')
182
214
def test_commit_move(self):
183
215
"""Test commit of revisions with moved files and directories"""
184
216
eq = self.assertEquals
185
b = Branch.initialize(u'.')
217
wt = self.make_branch_and_tree('.')
186
219
r1 = 'test@rev-1'
187
220
self.build_tree(['hello', 'a/', 'b/'])
188
b.working_tree().add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
189
b.working_tree().commit('initial', rev_id=r1, allow_pointless=False)
190
b.working_tree().move(['hello'], 'a')
221
wt.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
222
wt.commit('initial', rev_id=r1, allow_pointless=False)
223
wt.move(['hello'], 'a')
191
224
r2 = 'test@rev-2'
192
b.working_tree().commit('two', rev_id=r2, allow_pointless=False)
193
self.check_inventory_shape(b.working_tree().read_working_inventory(),
194
['a', 'a/hello', 'b'])
225
wt.commit('two', rev_id=r2, allow_pointless=False)
228
self.check_inventory_shape(wt.read_working_inventory(),
229
['a/', 'a/hello', 'b/'])
196
b.working_tree().move(['b'], 'a')
197
234
r3 = 'test@rev-3'
198
b.working_tree().commit('three', rev_id=r3, allow_pointless=False)
199
self.check_inventory_shape(b.working_tree().read_working_inventory(),
200
['a', 'a/hello', 'a/b'])
201
self.check_inventory_shape(b.repository.get_revision_inventory(r3),
202
['a', 'a/hello', 'a/b'])
235
wt.commit('three', rev_id=r3, allow_pointless=False)
238
self.check_inventory_shape(wt.read_working_inventory(),
239
['a/', 'a/hello', 'a/b/'])
240
self.check_inventory_shape(b.repository.get_revision_inventory(r3),
241
['a/', 'a/hello', 'a/b/'])
204
b.working_tree().move(['a/hello'], 'a/b')
245
wt.move(['a/hello'], 'a/b')
205
246
r4 = 'test@rev-4'
206
b.working_tree().commit('four', rev_id=r4, allow_pointless=False)
207
self.check_inventory_shape(b.working_tree().read_working_inventory(),
208
['a', 'a/b/hello', 'a/b'])
247
wt.commit('four', rev_id=r4, allow_pointless=False)
250
self.check_inventory_shape(wt.read_working_inventory(),
251
['a/', 'a/b/hello', 'a/b/'])
210
255
inv = b.repository.get_revision_inventory(r4)
211
256
eq(inv['hello-id'].revision, r4)
212
257
eq(inv['a-id'].revision, r1)
213
258
eq(inv['b-id'].revision, r3)
215
260
def test_removed_commit(self):
216
261
"""Commit with a removed file"""
217
b = Branch.initialize(u'.')
218
wt = b.working_tree()
262
wt = self.make_branch_and_tree('.')
219
264
file('hello', 'w').write('hello world')
220
b.working_tree().add(['hello'], ['hello-id'])
221
b.working_tree().commit(message='add hello')
223
wt = b.working_tree() # FIXME: kludge for aliasing of working inventory
265
wt.add(['hello'], ['hello-id'])
266
wt.commit(message='add hello')
224
267
wt.remove('hello')
225
b.working_tree().commit('removed hello', rev_id='rev2')
268
wt.commit('removed hello', rev_id='rev2')
227
270
tree = b.repository.revision_tree('rev2')
228
271
self.assertFalse(tree.has_id('hello-id'))
231
273
def test_committed_ancestry(self):
232
274
"""Test commit appends revisions to ancestry."""
233
b = Branch.initialize(u'.')
275
wt = self.make_branch_and_tree('.')
235
278
for i in range(4):
236
279
file('hello', 'w').write((str(i) * 4) + '\n')
238
b.working_tree().add(['hello'], ['hello-id'])
281
wt.add(['hello'], ['hello-id'])
239
282
rev_id = 'test@rev-%d' % (i+1)
240
283
rev_ids.append(rev_id)
241
b.working_tree().commit(message='rev %d' % (i+1),
284
wt.commit(message='rev %d' % (i+1),
243
286
eq = self.assertEquals
244
287
eq(b.revision_history(), rev_ids)
261
305
def test_strict_commit(self):
262
306
"""Try and commit with unknown files and strict = True, should fail."""
263
307
from bzrlib.errors import StrictCommitFailed
264
b = Branch.initialize(u'.')
308
wt = self.make_branch_and_tree('.')
265
310
file('hello', 'w').write('hello world')
266
b.working_tree().add('hello')
267
312
file('goodbye', 'w').write('goodbye cruel world!')
268
self.assertRaises(StrictCommitFailed, b.working_tree().commit,
313
self.assertRaises(StrictCommitFailed, wt.commit,
269
314
message='add hello but not goodbye', strict=True)
271
316
def test_strict_commit_without_unknowns(self):
272
317
"""Try and commit with no unknown files and strict = True,
274
319
from bzrlib.errors import StrictCommitFailed
275
b = Branch.initialize(u'.')
320
wt = self.make_branch_and_tree('.')
276
322
file('hello', 'w').write('hello world')
277
b.working_tree().add('hello')
278
b.working_tree().commit(message='add hello', strict=True)
324
wt.commit(message='add hello', strict=True)
280
326
def test_nonstrict_commit(self):
281
327
"""Try and commit with unknown files and strict = False, should work."""
282
b = Branch.initialize(u'.')
328
wt = self.make_branch_and_tree('.')
283
330
file('hello', 'w').write('hello world')
284
b.working_tree().add('hello')
285
332
file('goodbye', 'w').write('goodbye cruel world!')
286
b.working_tree().commit(message='add hello but not goodbye', strict=False)
333
wt.commit(message='add hello but not goodbye', strict=False)
288
335
def test_nonstrict_commit_without_unknowns(self):
289
336
"""Try and commit with no unknown files and strict = False,
291
b = Branch.initialize(u'.')
338
wt = self.make_branch_and_tree('.')
292
340
file('hello', 'w').write('hello world')
293
b.working_tree().add('hello')
294
b.working_tree().commit(message='add hello', strict=False)
342
wt.commit(message='add hello', strict=False)
296
344
def test_signed_commit(self):
297
345
import bzrlib.gpg
298
346
import bzrlib.commit as commit
299
347
oldstrategy = bzrlib.gpg.GPGStrategy
300
branch = Branch.initialize(u'.')
301
branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
302
self.failIf(branch.repository.revision_store.has_id('A', 'sig'))
348
wt = self.make_branch_and_tree('.')
350
wt.commit("base", allow_pointless=True, rev_id='A')
351
self.failIf(branch.repository.has_signature_for_revision_id('A'))
304
353
from bzrlib.testament import Testament
305
354
# monkey patch gpg signing mechanism
306
355
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
307
commit.Commit(config=MustSignConfig(branch)).commit(branch, "base",
356
commit.Commit(config=MustSignConfig(branch)).commit(message="base",
308
357
allow_pointless=True,
310
self.assertEqual(Testament.from_revision(branch.repository,
311
'B').as_short_text(),
312
branch.repository.revision_store.get('B',
361
return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
362
self.assertEqual(sign(Testament.from_revision(branch.repository,
363
'B').as_short_text()),
364
branch.repository.get_signature_text('B'))
315
366
bzrlib.gpg.GPGStrategy = oldstrategy
348
402
config = BranchWithHooks(branch)
349
403
commit.Commit(config=config).commit(
351
405
allow_pointless=True,
406
rev_id='A', working_tree = wt)
353
407
self.assertEqual(['called', 'called'], calls)
411
def test_commit_object_doesnt_set_nick(self):
412
# using the Commit object directly does not set the branch nick.
413
wt = self.make_branch_and_tree('.')
415
c.commit(working_tree=wt, message='empty tree', allow_pointless=True)
416
self.assertEquals(wt.branch.revno(), 1)
418
wt.branch.repository.get_revision(
419
wt.branch.last_revision()).properties)
421
def test_safe_master_lock(self):
423
master = BzrDirMetaFormat1().initialize('master')
424
master.create_repository()
425
master_branch = master.create_branch()
426
master.create_workingtree()
427
bound = master.sprout('bound')
428
wt = bound.open_workingtree()
429
wt.branch.set_bound_location(os.path.realpath('master'))
431
orig_default = lockdir._DEFAULT_TIMEOUT_SECONDS
432
master_branch.lock_write()
434
lockdir._DEFAULT_TIMEOUT_SECONDS = 1
435
self.assertRaises(LockContention, wt.commit, 'silly')
437
lockdir._DEFAULT_TIMEOUT_SECONDS = orig_default
438
master_branch.unlock()
440
def test_commit_bound_merge(self):
441
# see bug #43959; commit of a merge in a bound branch fails to push
442
# the new commit into the master
443
master_branch = self.make_branch('master')
444
bound_tree = self.make_branch_and_tree('bound')
445
bound_tree.branch.bind(master_branch)
447
self.build_tree_contents([('bound/content_file', 'initial contents\n')])
448
bound_tree.add(['content_file'])
449
bound_tree.commit(message='woo!')
451
other_bzrdir = master_branch.bzrdir.sprout('other')
452
other_tree = other_bzrdir.open_workingtree()
454
# do a commit to the the other branch changing the content file so
455
# that our commit after merging will have a merged revision in the
456
# content file history.
457
self.build_tree_contents([('other/content_file', 'change in other\n')])
458
other_tree.commit('change in other')
460
# do a merge into the bound branch from other, and then change the
461
# content file locally to force a new revision (rather than using the
462
# revision from other). This forces extra processing in commit.
463
bound_tree.merge_from_branch(other_tree.branch)
464
self.build_tree_contents([('bound/content_file', 'change in bound\n')])
466
# before #34959 was fixed, this failed with 'revision not present in
467
# weave' when trying to implicitly push from the bound branch to the master
468
bound_tree.commit(message='commit of merge in bound tree')
470
def test_commit_reporting_after_merge(self):
471
# when doing a commit of a merge, the reporter needs to still
472
# be called for each item that is added/removed/deleted.
473
this_tree = self.make_branch_and_tree('this')
474
# we need a bunch of files and dirs, to perform one action on each.
477
'this/dirtoreparent/',
480
'this/filetoreparent',
497
this_tree.commit('create_files')
498
other_dir = this_tree.bzrdir.sprout('other')
499
other_tree = other_dir.open_workingtree()
500
other_tree.lock_write()
501
# perform the needed actions on the files and dirs.
503
other_tree.rename_one('dirtorename', 'renameddir')
504
other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
505
other_tree.rename_one('filetorename', 'renamedfile')
506
other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
507
other_tree.remove(['dirtoremove', 'filetoremove'])
508
self.build_tree_contents([
510
('other/filetomodify', 'new content'),
511
('other/newfile', 'new file content')])
512
other_tree.add('newfile')
513
other_tree.add('newdir/')
514
other_tree.commit('modify all sample files and dirs.')
517
this_tree.merge_from_branch(other_tree.branch)
518
reporter = CapturingReporter()
519
this_tree.commit('do the commit', reporter=reporter)
521
('change', 'unchanged', ''),
522
('change', 'unchanged', 'dirtoleave'),
523
('change', 'unchanged', 'filetoleave'),
524
('change', 'modified', 'filetomodify'),
525
('change', 'added', 'newdir'),
526
('change', 'added', 'newfile'),
527
('renamed', 'renamed', 'dirtorename', 'renameddir'),
528
('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
529
('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
530
('renamed', 'renamed', 'filetorename', 'renamedfile'),
531
('deleted', 'dirtoremove'),
532
('deleted', 'filetoremove'),
536
def test_commit_removals_respects_filespec(self):
537
"""Commit respects the specified_files for removals."""
538
tree = self.make_branch_and_tree('.')
539
self.build_tree(['a', 'b'])
541
tree.commit('added a, b')
542
tree.remove(['a', 'b'])
543
tree.commit('removed a', specific_files='a')
544
basis = tree.basis_tree()
547
self.assertIs(None, basis.path2id('a'))
548
self.assertFalse(basis.path2id('b') is None)
552
def test_commit_saves_1ms_timestamp(self):
553
"""Passing in a timestamp is saved with 1ms resolution"""
554
tree = self.make_branch_and_tree('.')
555
self.build_tree(['a'])
557
tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
560
rev = tree.branch.repository.get_revision('a1')
561
self.assertEqual(1153248633.419, rev.timestamp)
563
def test_commit_has_1ms_resolution(self):
564
"""Allowing commit to generate the timestamp also has 1ms resolution"""
565
tree = self.make_branch_and_tree('.')
566
self.build_tree(['a'])
568
tree.commit('added a', rev_id='a1')
570
rev = tree.branch.repository.get_revision('a1')
571
timestamp = rev.timestamp
572
timestamp_1ms = round(timestamp, 3)
573
self.assertEqual(timestamp_1ms, timestamp)
575
def assertBasisTreeKind(self, kind, tree, file_id):
576
basis = tree.basis_tree()
579
self.assertEqual(kind, basis.kind(file_id))
583
def test_commit_kind_changes(self):
584
if not osutils.has_symlinks():
585
raise tests.TestSkipped('Test requires symlink support')
586
tree = self.make_branch_and_tree('.')
587
os.symlink('target', 'name')
588
tree.add('name', 'a-file-id')
589
tree.commit('Added a symlink')
590
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
593
self.build_tree(['name'])
594
tree.commit('Changed symlink to file')
595
self.assertBasisTreeKind('file', tree, 'a-file-id')
598
os.symlink('target', 'name')
599
tree.commit('file to symlink')
600
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
604
tree.commit('symlink to directory')
605
self.assertBasisTreeKind('directory', tree, 'a-file-id')
608
os.symlink('target', 'name')
609
tree.commit('directory to symlink')
610
self.assertBasisTreeKind('symlink', tree, 'a-file-id')
612
# prepare for directory <-> file tests
615
tree.commit('symlink to directory')
616
self.assertBasisTreeKind('directory', tree, 'a-file-id')
619
self.build_tree(['name'])
620
tree.commit('Changed directory to file')
621
self.assertBasisTreeKind('file', tree, 'a-file-id')
625
tree.commit('file to directory')
626
self.assertBasisTreeKind('directory', tree, 'a-file-id')
628
def test_commit_unversioned_specified(self):
629
"""Commit should raise if specified files isn't in basis or worktree"""
630
tree = self.make_branch_and_tree('.')
631
self.assertRaises(errors.PathsNotVersionedError, tree.commit,
632
'message', specific_files=['bogus'])
634
class Callback(object):
636
def __init__(self, message, testcase):
638
self.message = message
639
self.testcase = testcase
641
def __call__(self, commit_obj):
643
self.testcase.assertTrue(isinstance(commit_obj, Commit))
646
def test_commit_callback(self):
647
"""Commit should invoke a callback to get the message"""
649
tree = self.make_branch_and_tree('.')
653
self.assertTrue(isinstance(e, BzrError))
654
self.assertEqual('The message or message_callback keyword'
655
' parameter is required for commit().', str(e))
657
self.fail('exception not raised')
658
cb = self.Callback(u'commit 1', self)
659
tree.commit(message_callback=cb)
660
self.assertTrue(cb.called)
661
repository = tree.branch.repository
662
message = repository.get_revision(tree.last_revision()).message
663
self.assertEqual('commit 1', message)
665
def test_no_callback_pointless(self):
666
"""Callback should not be invoked for pointless commit"""
667
tree = self.make_branch_and_tree('.')
668
cb = self.Callback(u'commit 2', self)
669
self.assertRaises(PointlessCommit, tree.commit, message_callback=cb,
670
allow_pointless=False)
671
self.assertFalse(cb.called)
673
def test_no_callback_netfailure(self):
674
"""Callback should not be invoked if connectivity fails"""
675
tree = self.make_branch_and_tree('.')
676
cb = self.Callback(u'commit 2', self)
677
repository = tree.branch.repository
678
# simulate network failure
679
def raise_(self, arg, arg2):
680
raise errors.NoSuchFile('foo')
681
repository.add_inventory = raise_
682
self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
683
self.assertFalse(cb.called)
685
def test_selected_file_merge_commit(self):
686
"""Ensure the correct error is raised"""
687
tree = self.make_branch_and_tree('foo')
688
# pending merge would turn into a left parent
689
tree.commit('commit 1')
690
tree.add_parent_tree_id('example')
691
self.build_tree(['foo/bar', 'foo/baz'])
692
tree.add(['bar', 'baz'])
693
err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
694
tree.commit, 'commit 2', specific_files=['bar', 'baz'])
695
self.assertEqual(['bar', 'baz'], err.files)
696
self.assertEqual('Selected-file commit of merges is not supported'
697
' yet: files bar, baz', str(err))