1
# Copyright (C) 2005, 2006 by Canonical Ltd
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
"""Tests for the commit CLI of bzr."""
20
from cStringIO import StringIO
26
from bzrlib.branch import Branch
27
import bzrlib.bzrdir as bzrdir
28
from bzrlib.errors import BzrCommandError
29
from bzrlib.tests.blackbox import ExternalBase
30
from bzrlib.workingtree import WorkingTree
33
class TestCommit(ExternalBase):
25
from testtools.matchers import DocTestMatches
33
from ...controldir import ControlDir
34
from ...sixish import PY3
39
from .. import TestCaseWithTransport
40
from ..matchers import ContainsNoVfsCalls
43
class TestCommit(TestCaseWithTransport):
35
45
def test_05_empty_commit(self):
36
46
"""Commit of tree with no versioned files should fail"""
37
47
# If forced, it should succeed, but this is not tested here.
48
self.make_branch_and_tree('.')
39
49
self.build_tree(['hello.txt'])
40
self.runbzr("commit -m empty", retcode=3)
50
out, err = self.run_bzr('commit -m empty', retcode=3)
51
self.assertEqual('', out)
53
# 1) We really don't want 'aborting commit write group' anymore.
54
# 2) brz: ERROR: is a really long line, so we wrap it with '\'
59
brz: ERROR: No changes to commit.\
60
Please 'brz add' the files you want to commit,\
61
or use --unchanged to force an empty commit.
62
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
64
def test_commit_success(self):
65
"""Successful commit should not leave behind a bzr-commit-* file"""
66
self.make_branch_and_tree('.')
67
self.run_bzr('commit --unchanged -m message')
68
self.assertEqual('', self.run_bzr('unknowns')[0])
70
# same for unicode messages
71
self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
72
self.assertEqual('', self.run_bzr('unknowns')[0])
74
def test_commit_lossy_native(self):
75
"""A --lossy option to commit is supported."""
76
self.make_branch_and_tree('.')
77
self.run_bzr('commit --lossy --unchanged -m message')
78
self.assertEqual('', self.run_bzr('unknowns')[0])
80
def test_commit_lossy_foreign(self):
81
test_foreign.register_dummy_foreign_for_test(self)
82
self.make_branch_and_tree('.',
83
format=test_foreign.DummyForeignVcsDirFormat())
84
self.run_bzr('commit --lossy --unchanged -m message')
85
output = self.run_bzr('revision-info')[0]
86
self.assertTrue(output.startswith('1 dummy-'))
88
def test_commit_with_path(self):
89
"""Commit tree with path of root specified"""
90
a_tree = self.make_branch_and_tree('a')
91
self.build_tree(['a/a_file'])
93
self.run_bzr(['commit', '-m', 'first commit', 'a'])
95
b_tree = a_tree.controldir.sprout('b').open_workingtree()
96
self.build_tree_contents([('b/a_file', b'changes in b')])
97
self.run_bzr(['commit', '-m', 'first commit in b', 'b'])
99
self.build_tree_contents([('a/a_file', b'new contents')])
100
self.run_bzr(['commit', '-m', 'change in a', 'a'])
102
b_tree.merge_from_branch(a_tree.branch)
103
self.assertEqual(len(b_tree.conflicts()), 1)
104
self.run_bzr('resolved b/a_file')
105
self.run_bzr(['commit', '-m', 'merge into b', 'b'])
42
107
def test_10_verbose_commit(self):
43
108
"""Add one file and examine verbose commit output"""
109
tree = self.make_branch_and_tree('.')
45
110
self.build_tree(['hello.txt'])
46
self.runbzr("add hello.txt")
47
out,err = self.run_bzr("commit", "-m", "added")
48
self.assertEqual('', out)
49
self.assertEqual('added hello.txt\n'
50
'Committed revision 1.\n',
53
def test_15_verbose_commit_with_unknown(self):
111
tree.add("hello.txt")
112
out, err = self.run_bzr('commit -m added')
113
self.assertEqual('', out)
114
self.assertContainsRe(err, '^Committing to: .*\n'
116
'Committed revision 1.\n$',)
118
def prepare_simple_history(self):
119
"""Prepare and return a working tree with one commit of one file"""
120
# Commit with modified file should say so
121
wt = ControlDir.create_standalone_workingtree('.')
122
self.build_tree(['hello.txt', 'extra.txt'])
123
wt.add(['hello.txt'])
124
wt.commit(message='added')
127
def test_verbose_commit_modified(self):
128
# Verbose commit of modified file should say so
129
wt = self.prepare_simple_history()
130
self.build_tree_contents([('hello.txt', b'new contents')])
131
out, err = self.run_bzr('commit -m modified')
132
self.assertEqual('', out)
133
self.assertContainsRe(err, '^Committing to: .*\n'
134
'modified hello\\.txt\n'
135
'Committed revision 2\\.\n$')
137
def test_unicode_commit_message_is_filename(self):
138
"""Unicode commit message same as a filename (Bug #563646).
140
self.requireFeature(features.UnicodeFilenameFeature)
141
file_name = u'\N{euro sign}'
142
self.run_bzr(['init'])
143
with open(file_name, 'w') as f:
144
f.write('hello world')
145
self.run_bzr(['add'])
146
out, err = self.run_bzr(['commit', '-m', file_name])
147
reflags = re.MULTILINE|re.DOTALL|re.UNICODE
148
te = osutils.get_terminal_encoding()
149
self.assertContainsRe(err if PY3 else err.decode(te),
150
u'The commit message is a file name:',
153
# Run same test with a filename that causes encode
154
# error for the terminal encoding. We do this
155
# by forcing terminal encoding of ascii for
156
# osutils.get_terminal_encoding which is used
157
# by ui.text.show_warning
158
default_get_terminal_enc = osutils.get_terminal_encoding
160
osutils.get_terminal_encoding = lambda trace=None: 'ascii'
161
file_name = u'foo\u1234'
162
with open(file_name, 'w') as f:
163
f.write('hello world')
164
self.run_bzr(['add'])
165
out, err = self.run_bzr(['commit', '-m', file_name])
166
reflags = re.MULTILINE|re.DOTALL|re.UNICODE
167
te = osutils.get_terminal_encoding()
168
self.assertContainsRe(err if PY3 else err.decode(te, 'replace'),
169
u'The commit message is a file name:',
172
osutils.get_terminal_encoding = default_get_terminal_enc
174
def test_non_ascii_file_unversioned_utf8(self):
175
self.requireFeature(features.UnicodeFilenameFeature)
176
tree = self.make_branch_and_tree(".")
177
self.build_tree(["f"])
179
out, err = self.run_bzr_raw(["commit", "-m", "Wrong filename", u"\xa7"],
180
encoding="utf-8", retcode=3)
181
self.assertContainsRe(err, b"(?m)not versioned: \"\xc2\xa7\"$")
183
def test_non_ascii_file_unversioned_iso_8859_5(self):
184
self.requireFeature(features.UnicodeFilenameFeature)
185
tree = self.make_branch_and_tree(".")
186
self.build_tree(["f"])
188
out, err = self.run_bzr_raw(["commit", "-m", "Wrong filename", u"\xa7"],
189
encoding="iso-8859-5", retcode=3)
191
self.expectFailure("Error messages are always written as UTF-8",
192
self.assertNotContainsString, err, b"\xc2\xa7")
194
self.assertNotContainsString(err, b"\xc2\xa7")
195
self.assertContainsRe(err, b"(?m)not versioned: \"\xfd\"$")
197
def test_warn_about_forgotten_commit_message(self):
198
"""Test that the lack of -m parameter is caught"""
199
wt = self.make_branch_and_tree('.')
200
self.build_tree(['one', 'two'])
202
out, err = self.run_bzr('commit -m one two')
203
self.assertContainsRe(err, "The commit message is a file name")
205
def test_verbose_commit_renamed(self):
206
# Verbose commit of renamed file should say so
207
wt = self.prepare_simple_history()
208
wt.rename_one('hello.txt', 'gutentag.txt')
209
out, err = self.run_bzr('commit -m renamed')
210
self.assertEqual('', out)
211
self.assertContainsRe(err, '^Committing to: .*\n'
212
'renamed hello\\.txt => gutentag\\.txt\n'
213
'Committed revision 2\\.$\n')
215
def test_verbose_commit_moved(self):
216
# Verbose commit of file moved to new directory should say so
217
wt = self.prepare_simple_history()
220
wt.rename_one('hello.txt', 'subdir/hello.txt')
221
out, err = self.run_bzr('commit -m renamed')
222
self.assertEqual('', out)
224
'Committing to: %s/' % osutils.getcwd(),
226
'renamed hello.txt => subdir/hello.txt',
227
'Committed revision 2.',
229
}, set(err.split('\n')))
231
def test_verbose_commit_with_unknown(self):
54
232
"""Unknown files should not be listed by default in verbose output"""
55
233
# Is that really the best policy?
234
wt = ControlDir.create_standalone_workingtree('.')
57
235
self.build_tree(['hello.txt', 'extra.txt'])
58
self.runbzr("add hello.txt")
59
out,err = self.run_bzr("commit", "-m", "added")
236
wt.add(['hello.txt'])
237
out, err = self.run_bzr('commit -m added')
60
238
self.assertEqual('', out)
61
self.assertEqual('added hello.txt\n'
62
'Committed revision 1.\n',
239
self.assertContainsRe(err, '^Committing to: .*\n'
240
'added hello\\.txt\n'
241
'Committed revision 1\\.\n$')
65
def test_16_verbose_commit_with_unchanged(self):
243
def test_verbose_commit_with_unchanged(self):
66
244
"""Unchanged files should not be listed by default in verbose output"""
245
tree = self.make_branch_and_tree('.')
68
246
self.build_tree(['hello.txt', 'unchanged.txt'])
69
self.runbzr('add unchanged.txt')
70
self.runbzr('commit -m unchanged unchanged.txt')
71
self.runbzr("add hello.txt")
72
out,err = self.run_bzr("commit", "-m", "added")
73
self.assertEqual('', out)
74
self.assertEqual('added hello.txt\n'
75
'Committed revision 2.\n',
247
tree.add('unchanged.txt')
248
self.run_bzr('commit -m unchanged unchanged.txt')
249
tree.add("hello.txt")
250
out, err = self.run_bzr('commit -m added')
251
self.assertEqual('', out)
252
self.assertContainsRe(err, '^Committing to: .*\n'
253
'added hello\\.txt\n'
254
'Committed revision 2\\.$\n')
256
def test_verbose_commit_includes_master_location(self):
257
"""Location of master is displayed when committing to bound branch"""
258
a_tree = self.make_branch_and_tree('a')
259
self.build_tree(['a/b'])
261
a_tree.commit(message='Initial message')
263
b_tree = a_tree.branch.create_checkout('b')
264
expected = "%s/" % (osutils.abspath('a'), )
265
out, err = self.run_bzr('commit -m blah --unchanged', working_dir='b')
266
self.assertEqual(err, 'Committing to: %s\n'
267
'Committed revision 2.\n' % expected)
269
def test_commit_sanitizes_CR_in_message(self):
270
# See bug #433779, basically Emacs likes to pass '\r\n' style line
271
# endings to 'brz commit -m ""' which breaks because we don't allow
272
# '\r' in commit messages. (Mostly because of issues where XML style
273
# formats arbitrarily strip it out of the data while parsing.)
274
# To make life easier for users, we just always translate '\r\n' =>
275
# '\n'. And '\r' => '\n'.
276
a_tree = self.make_branch_and_tree('a')
277
self.build_tree(['a/b'])
279
self.run_bzr(['commit',
280
'-m', 'a string\r\n\r\nwith mixed\r\rendings\n'],
282
rev_id = a_tree.branch.last_revision()
283
rev = a_tree.branch.repository.get_revision(rev_id)
284
self.assertEqualDiff('a string\n\nwith mixed\n\nendings\n',
287
def test_commit_merge_reports_all_modified_files(self):
288
# the commit command should show all the files that are shown by
289
# brz diff or brz status when committing, even when they were not
290
# changed by the user but rather through doing a merge.
291
this_tree = self.make_branch_and_tree('this')
292
# we need a bunch of files and dirs, to perform one action on each.
295
'this/dirtoreparent/',
298
'this/filetoreparent',
315
this_tree.commit('create_files')
316
other_dir = this_tree.controldir.sprout('other')
317
other_tree = other_dir.open_workingtree()
318
other_tree.lock_write()
319
# perform the needed actions on the files and dirs.
321
other_tree.rename_one('dirtorename', 'renameddir')
322
other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
323
other_tree.rename_one('filetorename', 'renamedfile')
324
other_tree.rename_one('filetoreparent',
325
'renameddir/reparentedfile')
326
other_tree.remove(['dirtoremove', 'filetoremove'])
327
self.build_tree_contents([
329
('other/filetomodify', b'new content'),
330
('other/newfile', b'new file content')])
331
other_tree.add('newfile')
332
other_tree.add('newdir/')
333
other_tree.commit('modify all sample files and dirs.')
336
this_tree.merge_from_branch(other_tree.branch)
337
out, err = self.run_bzr('commit -m added', working_dir='this')
338
self.assertEqual('', out)
340
'Committing to: %s/' % osutils.pathjoin(osutils.getcwd(), 'this'),
341
'modified filetomodify',
344
'renamed dirtorename => renameddir',
345
'renamed filetorename => renamedfile',
346
'renamed dirtoreparent => renameddir/reparenteddir',
347
'renamed filetoreparent => renameddir/reparentedfile',
348
'deleted dirtoremove',
349
'deleted filetoremove',
350
'Committed revision 2.',
352
}, set(err.split('\n')))
78
354
def test_empty_commit_message(self):
80
file('foo.c', 'wt').write('int main() {}')
81
self.runbzr(['add', 'foo.c'])
82
self.runbzr(["commit", "-m", ""] , retcode=3)
355
tree = self.make_branch_and_tree('.')
356
self.build_tree_contents([('foo.c', b'int main() {}')])
358
self.run_bzr('commit -m ""')
84
360
def test_other_branch_commit(self):
85
361
# this branch is to ensure consistent behaviour, whether we're run
86
362
# inside a branch, or not.
87
os.mkdir('empty_branch')
88
os.chdir('empty_branch')
93
file('foo.c', 'wt').write('int main() {}')
94
file('bar.c', 'wt').write('int main() {}')
96
self.runbzr(['add', 'branch/foo.c'])
97
self.runbzr(['add', 'branch'])
363
outer_tree = self.make_branch_and_tree('.')
364
inner_tree = self.make_branch_and_tree('branch')
365
self.build_tree_contents([
366
('branch/foo.c', b'int main() {}'),
367
('branch/bar.c', b'int main() {}')])
368
inner_tree.add(['foo.c', 'bar.c'])
98
369
# can't commit files in different trees; sane error
99
self.runbzr('commit -m newstuff branch/foo.c .', retcode=3)
100
self.runbzr('commit -m newstuff branch/foo.c')
101
self.runbzr('commit -m newstuff branch')
102
self.runbzr('commit -m newstuff branch', retcode=3)
370
self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
371
# can commit to branch - records foo.c only
372
self.run_bzr('commit -m newstuff branch/foo.c')
373
# can commit to branch - records bar.c
374
self.run_bzr('commit -m newstuff branch')
376
self.run_bzr_error(["No changes to commit"], 'commit -m newstuff branch')
104
378
def test_out_of_date_tree_commit(self):
105
379
# check we get an error code and a clear message committing with an out
106
380
# of date checkout
107
self.make_branch_and_tree('branch')
381
tree = self.make_branch_and_tree('branch')
108
382
# make a checkout
109
self.runbzr('checkout --lightweight branch checkout')
383
checkout = tree.branch.create_checkout('checkout', lightweight=True)
110
384
# commit to the original branch to make the checkout out of date
111
self.runbzr('commit --unchanged -m message branch')
385
tree.commit('message branch', allow_pointless=True)
112
386
# now commit to the checkout should emit
113
# ERROR: Out of date with the branch, 'bzr update' is suggested
114
output = self.runbzr('commit --unchanged -m checkout_message '
387
# ERROR: Out of date with the branch, 'brz update' is suggested
388
output = self.run_bzr('commit --unchanged -m checkout_message '
115
389
'checkout', retcode=3)
116
390
self.assertEqual(output,
118
"bzr: ERROR: Working tree is out of date, please run "
392
"brz: ERROR: Working tree is out of date, please "
393
"run 'brz update'.\n"))
121
395
def test_local_commit_unbound(self):
122
396
# a --local commit on an unbound branch is an error
123
397
self.make_branch_and_tree('.')
124
out, err = self.run_bzr('commit', '--local', retcode=3)
398
out, err = self.run_bzr('commit --local', retcode=3)
125
399
self.assertEqualDiff('', out)
126
self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
400
self.assertEqualDiff('brz: ERROR: Cannot perform local-only commits '
127
401
'on unbound branches.\n', err)
403
def test_commit_a_text_merge_in_a_checkout(self):
404
# checkouts perform multiple actions in a transaction across bond
405
# branches and their master, and have been observed to fail in the
406
# past. This is a user story reported to fail in bug #43959 where
407
# a merge done in a checkout (using the update command) failed to
409
trunk = self.make_branch_and_tree('trunk')
411
u1 = trunk.branch.create_checkout('u1')
412
self.build_tree_contents([('u1/hosts', b'initial contents\n')])
414
self.run_bzr('commit -m add-hosts u1')
416
u2 = trunk.branch.create_checkout('u2')
417
self.build_tree_contents([('u2/hosts', b'altered in u2\n')])
418
self.run_bzr('commit -m checkin-from-u2 u2')
420
# make an offline commits
421
self.build_tree_contents([('u1/hosts', b'first offline change in u1\n')])
422
self.run_bzr('commit -m checkin-offline --local u1')
424
# now try to pull in online work from u2, and then commit our offline
426
# retcode 1 as we expect a text conflict
427
self.run_bzr('update u1', retcode=1)
428
self.assertFileEqual(b'''\
430
first offline change in u1
437
self.run_bzr('resolved u1/hosts')
438
# add a text change here to represent resolving the merge conflicts in
439
# favour of a new version of the file not identical to either the u1
440
# version or the u2 version.
441
self.build_tree_contents([('u1/hosts', b'merge resolution\n')])
442
self.run_bzr('commit -m checkin-merge-of-the-offline-work-from-u1 u1')
444
def test_commit_exclude_excludes_modified_files(self):
445
"""Commit -x foo should ignore changes to foo."""
446
tree = self.make_branch_and_tree('.')
447
self.build_tree(['a', 'b', 'c'])
448
tree.smart_add(['.'])
449
out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b'])
450
self.assertFalse('added b' in out)
451
self.assertFalse('added b' in err)
452
# If b was excluded it will still be 'added' in status.
453
out, err = self.run_bzr(['added'])
454
self.assertEqual('b\n', out)
455
self.assertEqual('', err)
457
def test_commit_exclude_twice_uses_both_rules(self):
458
"""Commit -x foo -x bar should ignore changes to foo and bar."""
459
tree = self.make_branch_and_tree('.')
460
self.build_tree(['a', 'b', 'c'])
461
tree.smart_add(['.'])
462
out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b', '-x', 'c'])
463
self.assertFalse('added b' in out)
464
self.assertFalse('added c' in out)
465
self.assertFalse('added b' in err)
466
self.assertFalse('added c' in err)
467
# If b was excluded it will still be 'added' in status.
468
out, err = self.run_bzr(['added'])
469
self.assertTrue('b\n' in out)
470
self.assertTrue('c\n' in out)
471
self.assertEqual('', err)
473
def test_commit_respects_spec_for_removals(self):
474
"""Commit with a file spec should only commit removals that match"""
475
t = self.make_branch_and_tree('.')
476
self.build_tree(['file-a', 'dir-a/', 'dir-a/file-b'])
477
t.add(['file-a', 'dir-a', 'dir-a/file-b'])
479
t.remove(['file-a', 'dir-a/file-b'])
480
result = self.run_bzr('commit . -m removed-file-b',
481
working_dir='dir-a')[1]
482
self.assertNotContainsRe(result, 'file-a')
483
result = self.run_bzr('status', working_dir='dir-a')[0]
484
self.assertContainsRe(result, 'removed:\n file-a')
486
def test_strict_commit(self):
487
"""Commit with --strict works if everything is known"""
488
ignores._set_user_ignores([])
489
tree = self.make_branch_and_tree('tree')
490
self.build_tree(['tree/a'])
492
# A simple change should just work
493
self.run_bzr('commit --strict -m adding-a', working_dir='tree')
495
def test_strict_commit_no_changes(self):
496
"""commit --strict gives "no changes" if there is nothing to commit"""
497
tree = self.make_branch_and_tree('tree')
498
self.build_tree(['tree/a'])
500
tree.commit('adding a')
502
# With no changes, it should just be 'no changes'
503
# Make sure that commit is failing because there is nothing to do
504
self.run_bzr_error(['No changes to commit'],
505
'commit --strict -m no-changes',
508
# But --strict doesn't care if you supply --unchanged
509
self.run_bzr('commit --strict --unchanged -m no-changes',
512
def test_strict_commit_unknown(self):
513
"""commit --strict fails if a file is unknown"""
514
tree = self.make_branch_and_tree('tree')
515
self.build_tree(['tree/a'])
517
tree.commit('adding a')
519
# Add one file so there is a change, but forget the other
520
self.build_tree(['tree/b', 'tree/c'])
522
self.run_bzr_error(['Commit refused because there are unknown files'],
523
'commit --strict -m add-b',
526
# --no-strict overrides --strict
527
self.run_bzr('commit --strict -m add-b --no-strict',
530
def test_fixes_bug_output(self):
531
"""commit --fixes=lp:23452 succeeds without output."""
532
tree = self.make_branch_and_tree('tree')
533
self.build_tree(['tree/hello.txt'])
534
tree.add('hello.txt')
535
output, err = self.run_bzr(
536
'commit -m hello --fixes=lp:23452 tree/hello.txt')
537
self.assertEqual('', output)
538
self.assertContainsRe(err, 'Committing to: .*\n'
539
'added hello\\.txt\n'
540
'Committed revision 1\\.\n')
542
def test_fixes_bug_unicode(self):
543
"""commit --fixes=lp:unicode succeeds without output."""
544
tree = self.make_branch_and_tree('tree')
545
self.build_tree(['tree/hello.txt'])
546
tree.add('hello.txt')
547
output, err = self.run_bzr_raw(
548
['commit', '-m', 'hello',
549
u'--fixes=generic:\u20ac', 'tree/hello.txt'],
550
encoding='utf-8', retcode=3)
551
self.assertEqual(b'', output)
552
self.assertContainsRe(err,
553
b'brz: ERROR: Unrecognized bug generic:\xe2\x82\xac\\. Commit refused.\n')
555
def test_no_bugs_no_properties(self):
556
"""If no bugs are fixed, the bugs property is not set.
558
see https://beta.launchpad.net/bzr/+bug/109613
560
tree = self.make_branch_and_tree('tree')
561
self.build_tree(['tree/hello.txt'])
562
tree.add('hello.txt')
563
self.run_bzr('commit -m hello tree/hello.txt')
564
# Get the revision properties, ignoring the branch-nick property, which
565
# we don't care about for this test.
566
last_rev = tree.branch.repository.get_revision(tree.last_revision())
567
properties = dict(last_rev.properties)
568
del properties['branch-nick']
569
self.assertFalse('bugs' in properties)
571
def test_fixes_bug_sets_property(self):
572
"""commit --fixes=lp:234 sets the lp:234 revprop to 'fixed'."""
573
tree = self.make_branch_and_tree('tree')
574
self.build_tree(['tree/hello.txt'])
575
tree.add('hello.txt')
576
self.run_bzr('commit -m hello --fixes=lp:234 tree/hello.txt')
578
# Get the revision properties, ignoring the branch-nick property, which
579
# we don't care about for this test.
580
last_rev = tree.branch.repository.get_revision(tree.last_revision())
581
properties = dict(last_rev.properties)
582
del properties[u'branch-nick']
584
self.assertEqual({u'bugs': 'https://launchpad.net/bugs/234 fixed'},
587
def test_fixes_multiple_bugs_sets_properties(self):
588
"""--fixes can be used more than once to show that bugs are fixed."""
589
tree = self.make_branch_and_tree('tree')
590
self.build_tree(['tree/hello.txt'])
591
tree.add('hello.txt')
592
self.run_bzr('commit -m hello --fixes=lp:123 --fixes=lp:235'
595
# Get the revision properties, ignoring the branch-nick property, which
596
# we don't care about for this test.
597
last_rev = tree.branch.repository.get_revision(tree.last_revision())
598
properties = dict(last_rev.properties)
599
del properties['branch-nick']
602
{u'bugs': 'https://launchpad.net/bugs/123 fixed\n'
603
'https://launchpad.net/bugs/235 fixed'},
606
def test_fixes_bug_with_alternate_trackers(self):
607
"""--fixes can be used on a properly configured branch to mark bug
608
fixes on multiple trackers.
610
tree = self.make_branch_and_tree('tree')
611
tree.branch.get_config().set_user_option(
612
'trac_twisted_url', 'http://twistedmatrix.com/trac')
613
self.build_tree(['tree/hello.txt'])
614
tree.add('hello.txt')
615
self.run_bzr('commit -m hello --fixes=lp:123 --fixes=twisted:235 tree/')
617
# Get the revision properties, ignoring the branch-nick property, which
618
# we don't care about for this test.
619
last_rev = tree.branch.repository.get_revision(tree.last_revision())
620
properties = dict(last_rev.properties)
621
del properties['branch-nick']
624
{'bugs': 'https://launchpad.net/bugs/123 fixed\n'
625
'http://twistedmatrix.com/trac/ticket/235 fixed'},
628
def test_fixes_unknown_bug_prefix(self):
629
tree = self.make_branch_and_tree('tree')
630
self.build_tree(['tree/hello.txt'])
631
tree.add('hello.txt')
633
["Unrecognized bug %s. Commit refused." % 'xxx:123'],
634
'commit -m add-b --fixes=xxx:123',
637
def test_fixes_bug_with_default_tracker(self):
638
"""commit --fixes=234 uses the default bug tracker."""
639
tree = self.make_branch_and_tree('tree')
640
self.build_tree(['tree/hello.txt'])
641
tree.add('hello.txt')
643
["brz: ERROR: No tracker specified for bug 123. Use the form "
644
"'tracker:id' or specify a default bug tracker using the "
645
"`bugtracker` option.\n"
646
"See \"brz help bugs\" for more information on this feature. "
648
'commit -m add-b --fixes=123',
650
tree.branch.get_config_stack().set("bugtracker", "lp")
651
self.run_bzr('commit -m hello --fixes=234 tree/hello.txt')
652
last_rev = tree.branch.repository.get_revision(tree.last_revision())
653
self.assertEqual('https://launchpad.net/bugs/234 fixed',
654
last_rev.properties['bugs'])
656
def test_fixes_invalid_bug_number(self):
657
tree = self.make_branch_and_tree('tree')
658
self.build_tree(['tree/hello.txt'])
659
tree.add('hello.txt')
661
["Did not understand bug identifier orange: Must be an integer. "
662
"See \"brz help bugs\" for more information on this feature.\n"
664
'commit -m add-b --fixes=lp:orange',
667
def test_fixes_invalid_argument(self):
668
"""Raise an appropriate error when the fixes argument isn't tag:id."""
669
tree = self.make_branch_and_tree('tree')
670
self.build_tree(['tree/hello.txt'])
671
tree.add('hello.txt')
673
[r"Invalid bug orange:apples:bananas. Must be in the form of "
674
r"'tracker:id'\. See \"brz help bugs\" for more information on "
675
r"this feature.\nCommit refused\."],
676
'commit -m add-b --fixes=orange:apples:bananas',
679
def test_no_author(self):
680
"""If the author is not specified, the author property is not set."""
681
tree = self.make_branch_and_tree('tree')
682
self.build_tree(['tree/hello.txt'])
683
tree.add('hello.txt')
684
self.run_bzr( 'commit -m hello tree/hello.txt')
685
last_rev = tree.branch.repository.get_revision(tree.last_revision())
686
properties = last_rev.properties
687
self.assertFalse('author' in properties)
689
def test_author_sets_property(self):
690
"""commit --author='John Doe <jdoe@example.com>' sets the author
693
tree = self.make_branch_and_tree('tree')
694
self.build_tree(['tree/hello.txt'])
695
tree.add('hello.txt')
696
self.run_bzr(["commit", '-m', 'hello',
697
'--author', u'John D\xf6 <jdoe@example.com>',
699
last_rev = tree.branch.repository.get_revision(tree.last_revision())
700
properties = last_rev.properties
701
self.assertEqual(u'John D\xf6 <jdoe@example.com>', properties['authors'])
703
def test_author_no_email(self):
704
"""Author's name without an email address is allowed, too."""
705
tree = self.make_branch_and_tree('tree')
706
self.build_tree(['tree/hello.txt'])
707
tree.add('hello.txt')
708
out, err = self.run_bzr("commit -m hello --author='John Doe' "
710
last_rev = tree.branch.repository.get_revision(tree.last_revision())
711
properties = last_rev.properties
712
self.assertEqual('John Doe', properties['authors'])
714
def test_multiple_authors(self):
715
"""Multiple authors can be specyfied, and all are stored."""
716
tree = self.make_branch_and_tree('tree')
717
self.build_tree(['tree/hello.txt'])
718
tree.add('hello.txt')
719
out, err = self.run_bzr("commit -m hello --author='John Doe' "
720
"--author='Jane Rey' tree/hello.txt")
721
last_rev = tree.branch.repository.get_revision(tree.last_revision())
722
properties = last_rev.properties
723
self.assertEqual('John Doe\nJane Rey', properties['authors'])
725
def test_commit_time(self):
726
tree = self.make_branch_and_tree('tree')
727
self.build_tree(['tree/hello.txt'])
728
tree.add('hello.txt')
729
out, err = self.run_bzr("commit -m hello "
730
"--commit-time='2009-10-10 08:00:00 +0100' tree/hello.txt")
731
last_rev = tree.branch.repository.get_revision(tree.last_revision())
733
'Sat 2009-10-10 08:00:00 +0100',
734
osutils.format_date(last_rev.timestamp, last_rev.timezone))
736
def test_commit_time_bad_time(self):
737
tree = self.make_branch_and_tree('tree')
738
self.build_tree(['tree/hello.txt'])
739
tree.add('hello.txt')
740
out, err = self.run_bzr("commit -m hello "
741
"--commit-time='NOT A TIME' tree/hello.txt", retcode=3)
742
self.assertStartsWith(
743
err, "brz: ERROR: Could not parse --commit-time:")
745
def test_commit_time_missing_tz(self):
746
tree = self.make_branch_and_tree('tree')
747
self.build_tree(['tree/hello.txt'])
748
tree.add('hello.txt')
749
out, err = self.run_bzr("commit -m hello "
750
"--commit-time='2009-10-10 08:00:00' tree/hello.txt", retcode=3)
751
self.assertStartsWith(
752
err, "brz: ERROR: Could not parse --commit-time:")
753
# Test that it is actually checking and does not simply crash with
754
# some other exception
755
self.assertContainsString(err, "missing a timezone offset")
757
def test_partial_commit_with_renames_in_tree(self):
758
# this test illustrates bug #140419
759
t = self.make_branch_and_tree('.')
760
self.build_tree(['dir/', 'dir/a', 'test'])
761
t.add(['dir/', 'dir/a', 'test'])
762
t.commit('initial commit')
763
# important part: file dir/a should change parent
764
# and should appear before old parent
765
# then during partial commit we have error
766
# parent_id {dir-XXX} not in inventory
767
t.rename_one('dir/a', 'a')
768
self.build_tree_contents([('test', b'changes in test')])
770
out, err = self.run_bzr('commit test -m "partial commit"')
771
self.assertEqual('', out)
772
self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
774
def test_commit_readonly_checkout(self):
775
# https://bugs.launchpad.net/bzr/+bug/129701
776
# "UnlockableTransport error trying to commit in checkout of readonly
778
self.make_branch('master')
779
master = ControlDir.open_from_transport(
780
self.get_readonly_transport('master')).open_branch()
781
master.create_checkout('checkout')
782
out, err = self.run_bzr(['commit', '--unchanged', '-mfoo', 'checkout'],
784
self.assertContainsRe(err,
785
r'^brz: ERROR: Cannot lock.*readonly transport')
787
def setup_editor(self):
788
# Test that commit template hooks work
789
if sys.platform == "win32":
790
with open('fed.bat', 'w') as f:
791
f.write('@rem dummy fed')
792
self.overrideEnv('BRZ_EDITOR', "fed.bat")
794
with open('fed.sh', 'wb') as f:
795
f.write(b'#!/bin/sh\n')
796
os.chmod('fed.sh', 0o755)
797
self.overrideEnv('BRZ_EDITOR', "./fed.sh")
799
def setup_commit_with_template(self):
801
msgeditor.hooks.install_named_hook("commit_message_template",
802
lambda commit_obj, msg: "save me some typing\n", None)
803
tree = self.make_branch_and_tree('tree')
804
self.build_tree(['tree/hello.txt'])
805
tree.add('hello.txt')
808
def test_edit_empty_message(self):
809
tree = self.make_branch_and_tree('tree')
811
self.build_tree(['tree/hello.txt'])
812
tree.add('hello.txt')
813
out, err = self.run_bzr("commit tree/hello.txt", retcode=3,
815
self.assertContainsRe(err,
816
"brz: ERROR: Empty commit message specified")
818
def test_commit_hook_template_accepted(self):
819
tree = self.setup_commit_with_template()
820
out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
821
last_rev = tree.branch.repository.get_revision(tree.last_revision())
822
self.assertEqual('save me some typing\n', last_rev.message)
824
def test_commit_hook_template_rejected(self):
825
tree = self.setup_commit_with_template()
826
expected = tree.last_revision()
827
out, err = self.run_bzr_error(["Empty commit message specified."
828
" Please specify a commit message with either"
829
" --message or --file or leave a blank message"
830
" with --message \"\"."],
831
"commit tree/hello.txt", stdin="n\n")
832
self.assertEqual(expected, tree.last_revision())
834
def test_set_commit_message(self):
835
msgeditor.hooks.install_named_hook("set_commit_message",
836
lambda commit_obj, msg: "save me some typing\n", None)
837
tree = self.make_branch_and_tree('tree')
838
self.build_tree(['tree/hello.txt'])
839
tree.add('hello.txt')
840
out, err = self.run_bzr("commit tree/hello.txt")
841
last_rev = tree.branch.repository.get_revision(tree.last_revision())
842
self.assertEqual('save me some typing\n', last_rev.message)
844
def test_commit_without_username(self):
845
"""Ensure commit error if username is not set.
847
self.run_bzr(['init', 'foo'])
848
with open('foo/foo.txt', 'w') as f:
850
self.run_bzr(['add'], working_dir='foo')
851
self.overrideEnv('EMAIL', None)
852
self.overrideEnv('BRZ_EMAIL', None)
853
# Also, make sure that it's not inferred from mailname.
854
self.overrideAttr(config, '_auto_user_id',
855
lambda: (None, None))
857
['Unable to determine your name'],
858
['commit', '-m', 'initial'], working_dir='foo')
860
def test_commit_recursive_checkout(self):
861
"""Ensure that a commit to a recursive checkout fails cleanly.
863
self.run_bzr(['init', 'test_branch'])
864
self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
865
self.run_bzr(['bind', '.'], working_dir='test_checkout') # bind to self
866
with open('test_checkout/foo.txt', 'w') as f:
868
self.run_bzr(['add'], working_dir='test_checkout')
869
out, err = self.run_bzr_error(
870
['Branch.*test_checkout.*appears to be bound to itself'],
871
['commit', '-m', 'addedfoo'], working_dir='test_checkout')
873
def test_mv_dirs_non_ascii(self):
874
"""Move directory with non-ascii name and containing files.
876
Regression test for bug 185211.
878
tree = self.make_branch_and_tree('.')
879
self.build_tree([u'abc\xa7/', u'abc\xa7/foo'])
881
tree.add([u'abc\xa7/', u'abc\xa7/foo'])
882
tree.commit('checkin')
884
tree.rename_one(u'abc\xa7', 'abc')
886
self.run_bzr('ci -m "non-ascii mv"')
889
class TestSmartServerCommit(TestCaseWithTransport):
891
def test_commit_to_lightweight(self):
892
self.setup_smart_server_with_call_log()
893
t = self.make_branch_and_tree('from')
894
for count in range(9):
895
t.commit(message='commit %d' % count)
896
out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
898
self.reset_smart_call_log()
899
self.build_tree(['target/afile'])
900
self.run_bzr(['add', 'target/afile'])
901
out, err = self.run_bzr(['commit', '-m', 'do something', 'target'])
902
# This figure represent the amount of work to perform this use case. It
903
# is entirely ok to reduce this number if a test fails due to rpc_count
904
# being too low. If rpc_count increases, more network roundtrips have
905
# become necessary for this use case. Please do not adjust this number
906
# upwards without agreement from bzr's network support maintainers.
907
self.assertLength(211, self.hpss_calls)
908
self.assertLength(2, self.hpss_connections)
909
self.expectFailure("commit still uses VFS calls",
910
self.assertThat, self.hpss_calls, ContainsNoVfsCalls)