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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
18
"""Tests for the commit CLI of bzr."""
25
from testtools.matchers import DocTestMatches
27
24
from bzrlib import (
33
29
from bzrlib.bzrdir import BzrDir
34
30
from bzrlib.tests import (
38
from bzrlib.tests import TestCaseWithTransport
41
class TestCommit(TestCaseWithTransport):
34
from bzrlib.tests.blackbox import ExternalBase
37
class TestCommit(ExternalBase):
43
39
def test_05_empty_commit(self):
44
40
"""Commit of tree with no versioned files should fail"""
47
43
self.build_tree(['hello.txt'])
48
44
out,err = self.run_bzr('commit -m empty', retcode=3)
49
45
self.assertEqual('', out)
51
# 1) We really don't want 'aborting commit write group' anymore.
52
# 2) bzr: ERROR: is a really long line, so we wrap it with '\'
57
bzr: ERROR: No changes to commit.\
58
Please 'bzr add' the files you want to commit,\
59
or use --unchanged to force an empty commit.
60
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
46
self.assertContainsRe(err, 'bzr: ERROR: no changes to commit\.'
47
' use --unchanged to commit anyhow\n')
62
49
def test_commit_success(self):
63
50
"""Successful commit should not leave behind a bzr-commit-* file"""
69
56
self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
70
57
self.assertEqual('', self.run_bzr('unknowns')[0])
72
def test_commit_lossy_native(self):
73
"""A --lossy option to commit is supported."""
74
self.make_branch_and_tree('.')
75
self.run_bzr('commit --lossy --unchanged -m message')
76
self.assertEqual('', self.run_bzr('unknowns')[0])
78
def test_commit_lossy_foreign(self):
79
test_foreign.register_dummy_foreign_for_test(self)
80
self.make_branch_and_tree('.',
81
format=test_foreign.DummyForeignVcsDirFormat())
82
self.run_bzr('commit --lossy --unchanged -m message')
83
output = self.run_bzr('revision-info')[0]
84
self.assertTrue(output.startswith('1 dummy-'))
86
59
def test_commit_with_path(self):
87
60
"""Commit tree with path of root specified"""
88
61
a_tree = self.make_branch_and_tree('a')
132
106
'modified hello\.txt\n'
133
107
'Committed revision 2\.\n$')
135
def test_unicode_commit_message_is_filename(self):
136
"""Unicode commit message same as a filename (Bug #563646).
138
self.requireFeature(features.UnicodeFilenameFeature)
139
file_name = u'\N{euro sign}'
140
self.run_bzr(['init'])
141
open(file_name, 'w').write('hello world')
142
self.run_bzr(['add'])
143
out, err = self.run_bzr(['commit', '-m', file_name])
144
reflags = re.MULTILINE|re.DOTALL|re.UNICODE
145
te = osutils.get_terminal_encoding()
146
self.assertContainsRe(err.decode(te),
147
u'The commit message is a file name:',
150
# Run same test with a filename that causes encode
151
# error for the terminal encoding. We do this
152
# by forcing terminal encoding of ascii for
153
# osutils.get_terminal_encoding which is used
154
# by ui.text.show_warning
155
default_get_terminal_enc = osutils.get_terminal_encoding
157
osutils.get_terminal_encoding = lambda trace=None: 'ascii'
158
file_name = u'foo\u1234'
159
open(file_name, 'w').write('hello world')
160
self.run_bzr(['add'])
161
out, err = self.run_bzr(['commit', '-m', file_name])
162
reflags = re.MULTILINE|re.DOTALL|re.UNICODE
163
te = osutils.get_terminal_encoding()
164
self.assertContainsRe(err.decode(te, 'replace'),
165
u'The commit message is a file name:',
168
osutils.get_terminal_encoding = default_get_terminal_enc
170
def test_warn_about_forgotten_commit_message(self):
171
"""Test that the lack of -m parameter is caught"""
172
wt = self.make_branch_and_tree('.')
173
self.build_tree(['one', 'two'])
175
out, err = self.run_bzr('commit -m one two')
176
self.assertContainsRe(err, "The commit message is a file name")
178
109
def test_verbose_commit_renamed(self):
179
110
# Verbose commit of renamed file should say so
180
111
wt = self.prepare_simple_history()
193
124
wt.rename_one('hello.txt', 'subdir/hello.txt')
194
125
out, err = self.run_bzr('commit -m renamed')
195
126
self.assertEqual('', out)
196
self.assertEqual(set([
197
'Committing to: %s/' % osutils.getcwd(),
199
'renamed hello.txt => subdir/hello.txt',
200
'Committed revision 2.',
202
]), set(err.split('\n')))
127
self.assertContainsRe(err, '^Committing to: .*\n'
129
'renamed hello\.txt => subdir/hello\.txt\n'
130
'Committed revision 2\.\n$')
204
132
def test_verbose_commit_with_unknown(self):
205
133
"""Unknown files should not be listed by default in verbose output"""
239
167
self.assertEqual(err, 'Committing to: %s\n'
240
168
'Committed revision 2.\n' % expected)
242
def test_commit_sanitizes_CR_in_message(self):
243
# See bug #433779, basically Emacs likes to pass '\r\n' style line
244
# endings to 'bzr commit -m ""' which breaks because we don't allow
245
# '\r' in commit messages. (Mostly because of issues where XML style
246
# formats arbitrarily strip it out of the data while parsing.)
247
# To make life easier for users, we just always translate '\r\n' =>
248
# '\n'. And '\r' => '\n'.
249
a_tree = self.make_branch_and_tree('a')
250
self.build_tree(['a/b'])
252
self.run_bzr(['commit',
253
'-m', 'a string\r\n\r\nwith mixed\r\rendings\n'],
255
rev_id = a_tree.branch.last_revision()
256
rev = a_tree.branch.repository.get_revision(rev_id)
257
self.assertEqualDiff('a string\n\nwith mixed\n\nendings\n',
260
170
def test_commit_merge_reports_all_modified_files(self):
261
171
# the commit command should show all the files that are shown by
262
172
# bzr diff or bzr status when committing, even when they were not
311
221
out,err = self.run_bzr('commit -m added')
312
222
self.assertEqual('', out)
313
self.assertEqual(set([
314
'Committing to: %s/' % osutils.getcwd(),
315
'modified filetomodify',
318
'renamed dirtorename => renameddir',
319
'renamed filetorename => renamedfile',
320
'renamed dirtoreparent => renameddir/reparenteddir',
321
'renamed filetoreparent => renameddir/reparentedfile',
322
'deleted dirtoremove',
323
'deleted filetoremove',
324
'Committed revision 2.',
326
]), set(err.split('\n')))
223
expected = '%s/' % (osutils.getcwd(), )
224
self.assertEqualDiff(
225
'Committing 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, ),
328
238
def test_empty_commit_message(self):
329
239
tree = self.make_branch_and_tree('.')
330
240
self.build_tree_contents([('foo.c', 'int main() {}')])
331
241
tree.add('foo.c')
332
self.run_bzr('commit -m ""')
242
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.')
334
261
def test_other_branch_commit(self):
335
262
# this branch is to ensure consistent behaviour, whether we're run
339
266
self.build_tree_contents([
340
267
('branch/foo.c', 'int main() {}'),
341
268
('branch/bar.c', 'int main() {}')])
342
inner_tree.add(['foo.c', 'bar.c'])
269
inner_tree.add('foo.c')
270
inner_tree.add('bar.c')
343
271
# can't commit files in different trees; sane error
344
272
self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
345
# can commit to branch - records foo.c only
346
273
self.run_bzr('commit -m newstuff branch/foo.c')
347
# can commit to branch - records bar.c
348
274
self.run_bzr('commit -m newstuff branch')
350
self.run_bzr_error(["No changes to commit"], 'commit -m newstuff branch')
275
self.run_bzr('commit -m newstuff branch', retcode=3)
352
277
def test_out_of_date_tree_commit(self):
353
278
# check we get an error code and a clear message committing with an out
377
302
def test_commit_a_text_merge_in_a_checkout(self):
378
303
# checkouts perform multiple actions in a transaction across bond
379
304
# branches and their master, and have been observed to fail in the
380
# past. This is a user story reported to fail in bug #43959 where
305
# past. This is a user story reported to fail in bug #43959 where
381
306
# a merge done in a checkout (using the update command) failed to
382
307
# commit correctly.
383
308
trunk = self.make_branch_and_tree('trunk')
385
310
u1 = trunk.branch.create_checkout('u1')
386
self.build_tree_contents([('u1/hosts', 'initial contents\n')])
311
self.build_tree_contents([('u1/hosts', 'initial contents')])
388
313
self.run_bzr('commit -m add-hosts u1')
390
315
u2 = trunk.branch.create_checkout('u2')
391
self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
316
self.build_tree_contents([('u2/hosts', 'altered in u2')])
392
317
self.run_bzr('commit -m checkin-from-u2 u2')
394
319
# make an offline commits
395
self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
320
self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
396
321
self.run_bzr('commit -m checkin-offline --local u1')
398
323
# now try to pull in online work from u2, and then commit our offline
399
324
# work as a merge
400
325
# retcode 1 as we expect a text conflict
401
326
self.run_bzr('update u1', retcode=1)
402
self.assertFileEqual('''\
404
first offline change in u1
411
327
self.run_bzr('resolved u1/hosts')
412
328
# add a text change here to represent resolving the merge conflicts in
413
329
# favour of a new version of the file not identical to either the u1
415
331
self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
416
332
self.run_bzr('commit -m checkin-merge-of-the-offline-work-from-u1 u1')
418
def test_commit_exclude_excludes_modified_files(self):
419
"""Commit -x foo should ignore changes to foo."""
420
tree = self.make_branch_and_tree('.')
421
self.build_tree(['a', 'b', 'c'])
422
tree.smart_add(['.'])
423
out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b'])
424
self.assertFalse('added b' in out)
425
self.assertFalse('added b' in err)
426
# If b was excluded it will still be 'added' in status.
427
out, err = self.run_bzr(['added'])
428
self.assertEqual('b\n', out)
429
self.assertEqual('', err)
431
def test_commit_exclude_twice_uses_both_rules(self):
432
"""Commit -x foo -x bar should ignore changes to foo and bar."""
433
tree = self.make_branch_and_tree('.')
434
self.build_tree(['a', 'b', 'c'])
435
tree.smart_add(['.'])
436
out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b', '-x', 'c'])
437
self.assertFalse('added b' in out)
438
self.assertFalse('added c' in out)
439
self.assertFalse('added b' in err)
440
self.assertFalse('added c' in err)
441
# If b was excluded it will still be 'added' in status.
442
out, err = self.run_bzr(['added'])
443
self.assertTrue('b\n' in out)
444
self.assertTrue('c\n' in out)
445
self.assertEqual('', err)
447
334
def test_commit_respects_spec_for_removals(self):
448
335
"""Commit with a file spec should only commit removals that match"""
449
336
t = self.make_branch_and_tree('.')
596
483
'commit -m add-b --fixes=xxx:123',
597
484
working_dir='tree')
599
def test_fixes_bug_with_default_tracker(self):
600
"""commit --fixes=234 uses the default bug tracker."""
601
tree = self.make_branch_and_tree('tree')
602
self.build_tree(['tree/hello.txt'])
603
tree.add('hello.txt')
605
["bzr: ERROR: No tracker specified for bug 123. Use the form "
606
"'tracker:id' or specify a default bug tracker using the "
607
"`bugtracker` option.\n"
608
"See \"bzr help bugs\" for more information on this feature. "
610
'commit -m add-b --fixes=123',
612
tree.branch.get_config().set_user_option("bugtracker", "lp")
613
self.run_bzr('commit -m hello --fixes=234 tree/hello.txt')
614
last_rev = tree.branch.repository.get_revision(tree.last_revision())
615
self.assertEqual('https://launchpad.net/bugs/234 fixed',
616
last_rev.properties['bugs'])
618
486
def test_fixes_invalid_bug_number(self):
619
487
tree = self.make_branch_and_tree('tree')
620
488
self.build_tree(['tree/hello.txt'])
621
489
tree.add('hello.txt')
622
490
self.run_bzr_error(
623
["Did not understand bug identifier orange: Must be an integer. "
624
"See \"bzr help bugs\" for more information on this feature.\n"
491
["Invalid bug identifier for %s. Commit refused." % 'lp:orange'],
626
492
'commit -m add-b --fixes=lp:orange',
627
493
working_dir='tree')
671
536
"tree/hello.txt")
672
537
last_rev = tree.branch.repository.get_revision(tree.last_revision())
673
538
properties = last_rev.properties
674
self.assertEqual('John Doe', properties['authors'])
676
def test_multiple_authors(self):
677
"""Multiple authors can be specyfied, and all are stored."""
678
tree = self.make_branch_and_tree('tree')
679
self.build_tree(['tree/hello.txt'])
680
tree.add('hello.txt')
681
out, err = self.run_bzr("commit -m hello --author='John Doe' "
682
"--author='Jane Rey' tree/hello.txt")
683
last_rev = tree.branch.repository.get_revision(tree.last_revision())
684
properties = last_rev.properties
685
self.assertEqual('John Doe\nJane Rey', properties['authors'])
687
def test_commit_time(self):
688
tree = self.make_branch_and_tree('tree')
689
self.build_tree(['tree/hello.txt'])
690
tree.add('hello.txt')
691
out, err = self.run_bzr("commit -m hello "
692
"--commit-time='2009-10-10 08:00:00 +0100' tree/hello.txt")
693
last_rev = tree.branch.repository.get_revision(tree.last_revision())
695
'Sat 2009-10-10 08:00:00 +0100',
696
osutils.format_date(last_rev.timestamp, last_rev.timezone))
698
def test_commit_time_bad_time(self):
699
tree = self.make_branch_and_tree('tree')
700
self.build_tree(['tree/hello.txt'])
701
tree.add('hello.txt')
702
out, err = self.run_bzr("commit -m hello "
703
"--commit-time='NOT A TIME' tree/hello.txt", retcode=3)
704
self.assertStartsWith(
705
err, "bzr: ERROR: Could not parse --commit-time:")
539
self.assertEqual('John Doe', properties['author'])
707
541
def test_partial_commit_with_renames_in_tree(self):
708
542
# this test illustrates bug #140419
734
568
self.assertContainsRe(err,
735
569
r'^bzr: ERROR: Cannot lock.*readonly transport')
737
def setup_editor(self):
738
# Test that commit template hooks work
739
if sys.platform == "win32":
740
f = file('fed.bat', 'w')
741
f.write('@rem dummy fed')
743
self.overrideEnv('BZR_EDITOR', "fed.bat")
745
f = file('fed.sh', 'wb')
746
f.write('#!/bin/sh\n')
748
os.chmod('fed.sh', 0755)
749
self.overrideEnv('BZR_EDITOR', "./fed.sh")
751
def setup_commit_with_template(self):
753
msgeditor.hooks.install_named_hook("commit_message_template",
754
lambda commit_obj, msg: "save me some typing\n", None)
755
tree = self.make_branch_and_tree('tree')
756
self.build_tree(['tree/hello.txt'])
757
tree.add('hello.txt')
760
def test_edit_empty_message(self):
761
tree = self.make_branch_and_tree('tree')
763
self.build_tree(['tree/hello.txt'])
764
tree.add('hello.txt')
765
out, err = self.run_bzr("commit tree/hello.txt", retcode=3,
767
self.assertContainsRe(err,
768
"bzr: ERROR: Empty commit message specified")
770
def test_commit_hook_template_accepted(self):
771
tree = self.setup_commit_with_template()
772
out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
773
last_rev = tree.branch.repository.get_revision(tree.last_revision())
774
self.assertEqual('save me some typing\n', last_rev.message)
776
def test_commit_hook_template_rejected(self):
777
tree = self.setup_commit_with_template()
778
expected = tree.last_revision()
779
out, err = self.run_bzr_error(["Empty commit message specified."
780
" Please specify a commit message with either"
781
" --message or --file or leave a blank message"
782
" with --message \"\"."],
783
"commit tree/hello.txt", stdin="n\n")
784
self.assertEqual(expected, tree.last_revision())
786
def test_set_commit_message(self):
787
msgeditor.hooks.install_named_hook("set_commit_message",
788
lambda commit_obj, msg: "save me some typing\n", None)
789
tree = self.make_branch_and_tree('tree')
790
self.build_tree(['tree/hello.txt'])
791
tree.add('hello.txt')
792
out, err = self.run_bzr("commit tree/hello.txt")
793
last_rev = tree.branch.repository.get_revision(tree.last_revision())
794
self.assertEqual('save me some typing\n', last_rev.message)
796
def test_commit_without_username(self):
797
"""Ensure commit error if username is not set.
799
self.run_bzr(['init', 'foo'])
801
open('foo.txt', 'w').write('hello')
802
self.run_bzr(['add'])
803
self.overrideEnv('EMAIL', None)
804
self.overrideEnv('BZR_EMAIL', None)
805
# Also, make sure that it's not inferred from mailname.
806
self.overrideAttr(config, '_auto_user_id',
807
lambda: (None, None))
808
out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
809
self.assertContainsRe(err, 'Unable to determine your name')
811
def test_commit_recursive_checkout(self):
812
"""Ensure that a commit to a recursive checkout fails cleanly.
814
self.run_bzr(['init', 'test_branch'])
815
self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
816
os.chdir('test_checkout')
817
self.run_bzr(['bind', '.']) # bind to self
818
open('foo.txt', 'w').write('hello')
819
self.run_bzr(['add'])
820
out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
821
self.assertEqual(out, '')
822
self.assertContainsRe(err,
823
'Branch.*test_checkout.*appears to be bound to itself')