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."""
25
from testtools.matchers import DocTestMatches
24
27
from bzrlib import (
29
34
from bzrlib.bzrdir import BzrDir
30
35
from bzrlib.tests import (
31
36
probe_bad_non_ascii,
39
UnicodeFilenameFeature,
34
from bzrlib.tests.blackbox import ExternalBase
37
class TestCommit(ExternalBase):
41
from bzrlib.tests import TestCaseWithTransport
44
class TestCommit(TestCaseWithTransport):
39
46
def test_05_empty_commit(self):
40
47
"""Commit of tree with no versioned files should fail"""
43
50
self.build_tree(['hello.txt'])
44
51
out,err = self.run_bzr('commit -m empty', retcode=3)
45
52
self.assertEqual('', out)
46
self.assertContainsRe(err, 'bzr: ERROR: no changes to commit\.'
47
' use --unchanged to commit anyhow\n')
54
# 1) We really don't want 'aborting commit write group' anymore.
55
# 2) bzr: ERROR: is a really long line, so we wrap it with '\'
60
aborting commit write group: PointlessCommit(No changes to commit)
61
bzr: ERROR: No changes to commit.\
62
Please 'bzr add' the files you want to commit,\
63
or use --unchanged to force an empty commit.
64
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
49
66
def test_commit_success(self):
50
67
"""Successful commit should not leave behind a bzr-commit-* file"""
56
73
self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
57
74
self.assertEqual('', self.run_bzr('unknowns')[0])
76
def test_commit_lossy_native(self):
77
"""A --lossy option to commit is supported."""
78
self.make_branch_and_tree('.')
79
self.run_bzr('commit --lossy --unchanged -m message')
80
self.assertEqual('', self.run_bzr('unknowns')[0])
82
def test_commit_lossy_foreign(self):
83
test_foreign.register_dummy_foreign_for_test(self)
84
self.make_branch_and_tree('.',
85
format=test_foreign.DummyForeignVcsDirFormat())
86
self.run_bzr('commit --lossy --unchanged -m message')
87
output = self.run_bzr('revision-info')[0]
88
self.assertTrue(output.startswith('1 dummy-'))
59
90
def test_commit_with_path(self):
60
91
"""Commit tree with path of root specified"""
61
92
a_tree = self.make_branch_and_tree('a')
102
132
self.build_tree_contents([('hello.txt', 'new contents')])
103
133
out, err = self.run_bzr('commit -m modified')
104
134
self.assertEqual('', out)
105
self.assertContainsRe(err, '^Committing revision 2 to ".*"\.\n'
135
self.assertContainsRe(err, '^Committing to: .*\n'
106
136
'modified hello\.txt\n'
107
137
'Committed revision 2\.\n$')
139
def test_unicode_commit_message_is_filename(self):
140
"""Unicode commit message same as a filename (Bug #563646).
142
self.requireFeature(UnicodeFilenameFeature)
143
file_name = u'\N{euro sign}'
144
self.run_bzr(['init'])
145
open(file_name, 'w').write('hello world')
146
self.run_bzr(['add'])
147
out, err = self.run_bzr(['commit', '-m', file_name])
148
reflags = re.MULTILINE|re.DOTALL|re.UNICODE
149
te = osutils.get_terminal_encoding()
150
self.assertContainsRe(err.decode(te),
151
u'The commit message is a file name:',
154
# Run same test with a filename that causes encode
155
# error for the terminal encoding. We do this
156
# by forcing terminal encoding of ascii for
157
# osutils.get_terminal_encoding which is used
158
# by ui.text.show_warning
159
default_get_terminal_enc = osutils.get_terminal_encoding
161
osutils.get_terminal_encoding = lambda trace=None: 'ascii'
162
file_name = u'foo\u1234'
163
open(file_name, 'w').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.decode(te, 'replace'),
169
u'The commit message is a file name:',
172
osutils.get_terminal_encoding = default_get_terminal_enc
174
def test_warn_about_forgotten_commit_message(self):
175
"""Test that the lack of -m parameter is caught"""
176
wt = self.make_branch_and_tree('.')
177
self.build_tree(['one', 'two'])
179
out, err = self.run_bzr('commit -m one two')
180
self.assertContainsRe(err, "The commit message is a file name")
109
182
def test_verbose_commit_renamed(self):
110
183
# Verbose commit of renamed file should say so
111
184
wt = self.prepare_simple_history()
112
185
wt.rename_one('hello.txt', 'gutentag.txt')
113
186
out, err = self.run_bzr('commit -m renamed')
114
187
self.assertEqual('', out)
115
self.assertContainsRe(err, '^Committing revision 2 to ".*"\.\n'
188
self.assertContainsRe(err, '^Committing to: .*\n'
116
189
'renamed hello\.txt => gutentag\.txt\n'
117
190
'Committed revision 2\.$\n')
124
197
wt.rename_one('hello.txt', 'subdir/hello.txt')
125
198
out, err = self.run_bzr('commit -m renamed')
126
199
self.assertEqual('', out)
127
self.assertContainsRe(err, '^Committing revision 2 to ".*"\.\n'
129
'renamed hello\.txt => subdir/hello\.txt\n'
130
'Committed revision 2\.\n$')
200
self.assertEqual(set([
201
'Committing to: %s/' % osutils.getcwd(),
203
'renamed hello.txt => subdir/hello.txt',
204
'Committed revision 2.',
206
]), set(err.split('\n')))
132
208
def test_verbose_commit_with_unknown(self):
133
209
"""Unknown files should not be listed by default in verbose output"""
164
240
b_tree = a_tree.branch.create_checkout('b')
165
241
expected = "%s/" % (osutils.abspath('a'), )
166
242
out, err = self.run_bzr('commit -m blah --unchanged', working_dir='b')
167
self.assertEqual(err, 'Committing revision 2 to "%s".\n'
243
self.assertEqual(err, 'Committing to: %s\n'
168
244
'Committed revision 2.\n' % expected)
246
def test_commit_sanitizes_CR_in_message(self):
247
# See bug #433779, basically Emacs likes to pass '\r\n' style line
248
# endings to 'bzr commit -m ""' which breaks because we don't allow
249
# '\r' in commit messages. (Mostly because of issues where XML style
250
# formats arbitrarily strip it out of the data while parsing.)
251
# To make life easier for users, we just always translate '\r\n' =>
252
# '\n'. And '\r' => '\n'.
253
a_tree = self.make_branch_and_tree('a')
254
self.build_tree(['a/b'])
256
self.run_bzr(['commit',
257
'-m', 'a string\r\n\r\nwith mixed\r\rendings\n'],
259
rev_id = a_tree.branch.last_revision()
260
rev = a_tree.branch.repository.get_revision(rev_id)
261
self.assertEqualDiff('a string\n\nwith mixed\n\nendings\n',
170
264
def test_commit_merge_reports_all_modified_files(self):
171
265
# the commit command should show all the files that are shown by
172
266
# bzr diff or bzr status when committing, even when they were not
221
315
out,err = self.run_bzr('commit -m added')
222
316
self.assertEqual('', out)
223
expected = '%s/' % (osutils.getcwd(), )
224
self.assertEqualDiff(
225
'Committing revision 2 to "%s".\n'
226
'modified filetomodify\n'
229
'renamed dirtorename => renameddir\n'
230
'renamed filetorename => renamedfile\n'
231
'renamed dirtoreparent => renameddir/reparenteddir\n'
232
'renamed filetoreparent => renameddir/reparentedfile\n'
233
'deleted dirtoremove\n'
234
'deleted filetoremove\n'
235
'Committed revision 2.\n' % (expected, ),
317
self.assertEqual(set([
318
'Committing to: %s/' % osutils.getcwd(),
319
'modified filetomodify',
322
'renamed dirtorename => renameddir',
323
'renamed filetorename => renamedfile',
324
'renamed dirtoreparent => renameddir/reparenteddir',
325
'renamed filetoreparent => renameddir/reparentedfile',
326
'deleted dirtoremove',
327
'deleted filetoremove',
328
'Committed revision 2.',
330
]), set(err.split('\n')))
238
332
def test_empty_commit_message(self):
239
333
tree = self.make_branch_and_tree('.')
241
335
tree.add('foo.c')
242
336
self.run_bzr('commit -m ""', retcode=3)
244
def test_unsupported_encoding_commit_message(self):
245
tree = self.make_branch_and_tree('.')
246
self.build_tree_contents([('foo.c', 'int main() {}')])
248
# LANG env variable has no effect on Windows
249
# but some characters anyway cannot be represented
250
# in default user encoding
251
char = probe_bad_non_ascii(bzrlib.user_encoding)
253
raise TestSkipped('Cannot find suitable non-ascii character'
254
'for user_encoding (%s)' % bzrlib.user_encoding)
255
out,err = self.run_bzr_subprocess('commit -m "%s"' % char,
257
env_changes={'LANG': 'C'})
258
self.assertContainsRe(err, r'bzrlib.errors.BzrError: Parameter.*is '
259
'unsupported by the current encoding.')
261
338
def test_other_branch_commit(self):
262
339
# this branch is to ensure consistent behaviour, whether we're run
263
340
# inside a branch, or not.
266
343
self.build_tree_contents([
267
344
('branch/foo.c', 'int main() {}'),
268
345
('branch/bar.c', 'int main() {}')])
269
inner_tree.add('foo.c')
270
inner_tree.add('bar.c')
346
inner_tree.add(['foo.c', 'bar.c'])
271
347
# can't commit files in different trees; sane error
272
348
self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
349
# can commit to branch - records foo.c only
273
350
self.run_bzr('commit -m newstuff branch/foo.c')
351
# can commit to branch - records bar.c
274
352
self.run_bzr('commit -m newstuff branch')
275
self.run_bzr('commit -m newstuff branch', retcode=3)
354
self.run_bzr_error(["No changes to commit"], 'commit -m newstuff branch')
277
356
def test_out_of_date_tree_commit(self):
278
357
# check we get an error code and a clear message committing with an out
302
381
def test_commit_a_text_merge_in_a_checkout(self):
303
382
# checkouts perform multiple actions in a transaction across bond
304
383
# branches and their master, and have been observed to fail in the
305
# past. This is a user story reported to fail in bug #43959 where
384
# past. This is a user story reported to fail in bug #43959 where
306
385
# a merge done in a checkout (using the update command) failed to
307
386
# commit correctly.
308
387
trunk = self.make_branch_and_tree('trunk')
310
389
u1 = trunk.branch.create_checkout('u1')
311
self.build_tree_contents([('u1/hosts', 'initial contents')])
390
self.build_tree_contents([('u1/hosts', 'initial contents\n')])
313
392
self.run_bzr('commit -m add-hosts u1')
315
394
u2 = trunk.branch.create_checkout('u2')
316
self.build_tree_contents([('u2/hosts', 'altered in u2')])
395
self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
317
396
self.run_bzr('commit -m checkin-from-u2 u2')
319
398
# make an offline commits
320
self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
399
self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
321
400
self.run_bzr('commit -m checkin-offline --local u1')
323
402
# now try to pull in online work from u2, and then commit our offline
324
403
# work as a merge
325
404
# retcode 1 as we expect a text conflict
326
405
self.run_bzr('update u1', retcode=1)
406
self.assertFileEqual('''\
408
first offline change in u1
327
415
self.run_bzr('resolved u1/hosts')
328
416
# add a text change here to represent resolving the merge conflicts in
329
417
# favour of a new version of the file not identical to either the u1
331
419
self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
332
420
self.run_bzr('commit -m checkin-merge-of-the-offline-work-from-u1 u1')
422
def test_commit_exclude_excludes_modified_files(self):
423
"""Commit -x foo should ignore changes to foo."""
424
tree = self.make_branch_and_tree('.')
425
self.build_tree(['a', 'b', 'c'])
426
tree.smart_add(['.'])
427
out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b'])
428
self.assertFalse('added b' in out)
429
self.assertFalse('added b' in err)
430
# If b was excluded it will still be 'added' in status.
431
out, err = self.run_bzr(['added'])
432
self.assertEqual('b\n', out)
433
self.assertEqual('', err)
435
def test_commit_exclude_twice_uses_both_rules(self):
436
"""Commit -x foo -x bar should ignore changes to foo and bar."""
437
tree = self.make_branch_and_tree('.')
438
self.build_tree(['a', 'b', 'c'])
439
tree.smart_add(['.'])
440
out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b', '-x', 'c'])
441
self.assertFalse('added b' in out)
442
self.assertFalse('added c' in out)
443
self.assertFalse('added b' in err)
444
self.assertFalse('added c' in err)
445
# If b was excluded it will still be 'added' in status.
446
out, err = self.run_bzr(['added'])
447
self.assertTrue('b\n' in out)
448
self.assertTrue('c\n' in out)
449
self.assertEqual('', err)
334
451
def test_commit_respects_spec_for_removals(self):
335
452
"""Commit with a file spec should only commit removals that match"""
336
453
t = self.make_branch_and_tree('.')
520
640
tree = self.make_branch_and_tree('tree')
521
641
self.build_tree(['tree/hello.txt'])
522
642
tree.add('hello.txt')
523
self.run_bzr("commit -m hello --author='John Doe <jdoe@example.com>' "
643
self.run_bzr(["commit", '-m', 'hello',
644
'--author', u'John D\xf6 <jdoe@example.com>',
525
646
last_rev = tree.branch.repository.get_revision(tree.last_revision())
526
647
properties = last_rev.properties
527
self.assertEqual('John Doe <jdoe@example.com>', properties['author'])
648
self.assertEqual(u'John D\xf6 <jdoe@example.com>', properties['authors'])
529
650
def test_author_no_email(self):
530
651
"""Author's name without an email address is allowed, too."""
535
656
"tree/hello.txt")
536
657
last_rev = tree.branch.repository.get_revision(tree.last_revision())
537
658
properties = last_rev.properties
538
self.assertEqual('John Doe', properties['author'])
659
self.assertEqual('John Doe', properties['authors'])
661
def test_multiple_authors(self):
662
"""Multiple authors can be specyfied, and all are stored."""
663
tree = self.make_branch_and_tree('tree')
664
self.build_tree(['tree/hello.txt'])
665
tree.add('hello.txt')
666
out, err = self.run_bzr("commit -m hello --author='John Doe' "
667
"--author='Jane Rey' tree/hello.txt")
668
last_rev = tree.branch.repository.get_revision(tree.last_revision())
669
properties = last_rev.properties
670
self.assertEqual('John Doe\nJane Rey', properties['authors'])
672
def test_commit_time(self):
673
tree = self.make_branch_and_tree('tree')
674
self.build_tree(['tree/hello.txt'])
675
tree.add('hello.txt')
676
out, err = self.run_bzr("commit -m hello "
677
"--commit-time='2009-10-10 08:00:00 +0100' tree/hello.txt")
678
last_rev = tree.branch.repository.get_revision(tree.last_revision())
680
'Sat 2009-10-10 08:00:00 +0100',
681
osutils.format_date(last_rev.timestamp, last_rev.timezone))
683
def test_commit_time_bad_time(self):
684
tree = self.make_branch_and_tree('tree')
685
self.build_tree(['tree/hello.txt'])
686
tree.add('hello.txt')
687
out, err = self.run_bzr("commit -m hello "
688
"--commit-time='NOT A TIME' tree/hello.txt", retcode=3)
689
self.assertStartsWith(
690
err, "bzr: ERROR: Could not parse --commit-time:")
540
692
def test_partial_commit_with_renames_in_tree(self):
541
693
# this test illustrates bug #140419
567
719
self.assertContainsRe(err,
568
720
r'^bzr: ERROR: Cannot lock.*readonly transport')
722
def setup_editor(self):
723
# Test that commit template hooks work
724
if sys.platform == "win32":
725
f = file('fed.bat', 'w')
726
f.write('@rem dummy fed')
728
self.overrideEnv('BZR_EDITOR', "fed.bat")
730
f = file('fed.sh', 'wb')
731
f.write('#!/bin/sh\n')
733
os.chmod('fed.sh', 0755)
734
self.overrideEnv('BZR_EDITOR', "./fed.sh")
736
def setup_commit_with_template(self):
738
msgeditor.hooks.install_named_hook("commit_message_template",
739
lambda commit_obj, msg: "save me some typing\n", None)
740
tree = self.make_branch_and_tree('tree')
741
self.build_tree(['tree/hello.txt'])
742
tree.add('hello.txt')
745
def test_commit_hook_template_accepted(self):
746
tree = self.setup_commit_with_template()
747
out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
748
last_rev = tree.branch.repository.get_revision(tree.last_revision())
749
self.assertEqual('save me some typing\n', last_rev.message)
751
def test_commit_hook_template_rejected(self):
752
tree = self.setup_commit_with_template()
753
expected = tree.last_revision()
754
out, err = self.run_bzr_error(["empty commit message"],
755
"commit tree/hello.txt", stdin="n\n")
756
self.assertEqual(expected, tree.last_revision())
758
def test_commit_without_username(self):
759
"""Ensure commit error if username is not set.
761
self.run_bzr(['init', 'foo'])
763
open('foo.txt', 'w').write('hello')
764
self.run_bzr(['add'])
765
self.overrideEnv('EMAIL', None)
766
self.overrideEnv('BZR_EMAIL', None)
767
# Also, make sure that it's not inferred from mailname.
768
self.overrideAttr(config, '_auto_user_id',
769
lambda: (None, None))
770
out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
771
self.assertContainsRe(err, 'Unable to determine your name')
773
def test_commit_recursive_checkout(self):
774
"""Ensure that a commit to a recursive checkout fails cleanly.
776
self.run_bzr(['init', 'test_branch'])
777
self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
778
os.chdir('test_checkout')
779
self.run_bzr(['bind', '.']) # bind to self
780
open('foo.txt', 'w').write('hello')
781
self.run_bzr(['add'])
782
out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
783
self.assertEqual(out, '')
784
self.assertContainsRe(err,
785
'Branch.*test_checkout.*appears to be bound to itself')