~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_commit.py

merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
2
2
#
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
12
12
#
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
16
16
 
17
17
 
18
18
"""Tests for the commit CLI of bzr."""
19
19
 
20
 
import doctest
21
20
import os
22
21
import re
23
22
import sys
24
23
 
25
 
from testtools.matchers import DocTestMatches
26
 
 
27
24
from bzrlib import (
28
 
    config,
29
 
    osutils,
30
25
    ignores,
31
 
    msgeditor,
32
 
    tests,
33
26
    )
 
27
from bzrlib.branch import Branch
34
28
from bzrlib.bzrdir import BzrDir
35
 
from bzrlib.tests import (
36
 
    probe_bad_non_ascii,
37
 
    test_foreign,
38
 
    TestSkipped,
39
 
    UnicodeFilenameFeature,
40
 
    )
41
 
from bzrlib.tests import TestCaseWithTransport
42
 
 
43
 
 
44
 
class TestCommit(TestCaseWithTransport):
 
29
from bzrlib.errors import BzrCommandError
 
30
from bzrlib.tests.blackbox import ExternalBase
 
31
from bzrlib.workingtree import WorkingTree
 
32
 
 
33
 
 
34
class TestCommit(ExternalBase):
45
35
 
46
36
    def test_05_empty_commit(self):
47
37
        """Commit of tree with no versioned files should fail"""
48
38
        # If forced, it should succeed, but this is not tested here.
49
 
        self.make_branch_and_tree('.')
 
39
        self.run_bzr("init")
50
40
        self.build_tree(['hello.txt'])
51
 
        out,err = self.run_bzr('commit -m empty', retcode=3)
 
41
        out,err = self.run_bzr("commit", "-m", "empty", retcode=3)
52
42
        self.assertEqual('', out)
53
 
        # Two ugly bits here.
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 '\'
56
 
        self.assertThat(
57
 
            err,
58
 
            DocTestMatches("""\
59
 
Committing to: ...
60
 
bzr: ERROR: No changes to commit.\
61
 
 Please 'bzr add' the files you want to commit,\
62
 
 or use --unchanged to force an empty commit.
63
 
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))
 
43
        self.assertStartsWith(err, 'bzr: ERROR: no changes to commit.'
 
44
                                  ' use --unchanged to commit anyhow\n')
64
45
 
65
46
    def test_commit_success(self):
66
47
        """Successful commit should not leave behind a bzr-commit-* file"""
67
 
        self.make_branch_and_tree('.')
68
 
        self.run_bzr('commit --unchanged -m message')
69
 
        self.assertEqual('', self.run_bzr('unknowns')[0])
 
48
        self.run_bzr("init")
 
49
        self.run_bzr("commit", "--unchanged", "-m", "message")
 
50
        self.assertEqual('', self.capture('unknowns'))
70
51
 
71
52
        # same for unicode messages
72
 
        self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
73
 
        self.assertEqual('', self.run_bzr('unknowns')[0])
74
 
 
75
 
    def test_commit_lossy_native(self):
76
 
        """A --lossy option to commit is supported."""
77
 
        self.make_branch_and_tree('.')
78
 
        self.run_bzr('commit --lossy --unchanged -m message')
79
 
        self.assertEqual('', self.run_bzr('unknowns')[0])
80
 
 
81
 
    def test_commit_lossy_foreign(self):
82
 
        test_foreign.register_dummy_foreign_for_test(self)
83
 
        self.make_branch_and_tree('.',
84
 
            format=test_foreign.DummyForeignVcsDirFormat())
85
 
        self.run_bzr('commit --lossy --unchanged -m message')
86
 
        output = self.run_bzr('revision-info')[0]
87
 
        self.assertTrue(output.startswith('1 dummy-'))
 
53
        self.run_bzr("commit", "--unchanged", "-m", u'foo\xb5')
 
54
        self.assertEqual('', self.capture('unknowns'))
88
55
 
89
56
    def test_commit_with_path(self):
90
57
        """Commit tree with path of root specified"""
91
 
        a_tree = self.make_branch_and_tree('a')
 
58
        self.run_bzr('init', 'a')
92
59
        self.build_tree(['a/a_file'])
93
 
        a_tree.add('a_file')
94
 
        self.run_bzr(['commit', '-m', 'first commit', 'a'])
 
60
        self.run_bzr('add', 'a/a_file')
 
61
        self.run_bzr('commit', '-m', 'first commit', 'a')
95
62
 
96
 
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
63
        self.run_bzr('branch', 'a', 'b')
97
64
        self.build_tree_contents([('b/a_file', 'changes in b')])
98
 
        self.run_bzr(['commit', '-m', 'first commit in b', 'b'])
 
65
        self.run_bzr('commit', '-m', 'first commit in b', 'b')
99
66
 
100
67
        self.build_tree_contents([('a/a_file', 'new contents')])
101
 
        self.run_bzr(['commit', '-m', 'change in a', 'a'])
102
 
 
103
 
        b_tree.merge_from_branch(a_tree.branch)
104
 
        self.assertEqual(len(b_tree.conflicts()), 1)
105
 
        self.run_bzr('resolved b/a_file')
106
 
        self.run_bzr(['commit', '-m', 'merge into b', 'b'])
 
68
        self.run_bzr('commit', '-m', 'change in a', 'a')
 
69
 
 
70
        os.chdir('b')
 
71
        self.run_bzr('merge', '../a', retcode=1) # will conflict
 
72
        os.chdir('..')
 
73
        self.run_bzr('resolved', 'b/a_file')
 
74
        self.run_bzr('commit', '-m', 'merge into b', 'b')
 
75
 
107
76
 
108
77
    def test_10_verbose_commit(self):
109
78
        """Add one file and examine verbose commit output"""
110
 
        tree = self.make_branch_and_tree('.')
 
79
        self.runbzr("init")
111
80
        self.build_tree(['hello.txt'])
112
 
        tree.add("hello.txt")
113
 
        out,err = self.run_bzr('commit -m added')
 
81
        self.runbzr("add hello.txt")
 
82
        out,err = self.run_bzr("commit", "-m", "added")
114
83
        self.assertEqual('', out)
115
 
        self.assertContainsRe(err, '^Committing to: .*\n'
116
 
                              'added hello.txt\n'
117
 
                              'Committed revision 1.\n$',)
 
84
        self.assertEqual('added hello.txt\n'
 
85
                         'Committed revision 1.\n',
 
86
                         err)
118
87
 
119
88
    def prepare_simple_history(self):
120
89
        """Prepare and return a working tree with one commit of one file"""
129
98
        # Verbose commit of modified file should say so
130
99
        wt = self.prepare_simple_history()
131
100
        self.build_tree_contents([('hello.txt', 'new contents')])
132
 
        out, err = self.run_bzr('commit -m modified')
 
101
        out, err = self.run_bzr("commit", "-m", "modified")
133
102
        self.assertEqual('', out)
134
 
        self.assertContainsRe(err, '^Committing to: .*\n'
135
 
                              'modified hello\.txt\n'
136
 
                              'Committed revision 2\.\n$')
137
 
 
138
 
    def test_unicode_commit_message_is_filename(self):
139
 
        """Unicode commit message same as a filename (Bug #563646).
140
 
        """
141
 
        self.requireFeature(UnicodeFilenameFeature)
142
 
        file_name = u'\N{euro sign}'
143
 
        self.run_bzr(['init'])
144
 
        open(file_name, 'w').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.decode(te),
150
 
            u'The commit message is a file name:',
151
 
            flags=reflags)
152
 
 
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
159
 
        try:
160
 
            osutils.get_terminal_encoding = lambda trace=None: 'ascii'
161
 
            file_name = u'foo\u1234'
162
 
            open(file_name, 'w').write('hello world')
163
 
            self.run_bzr(['add'])
164
 
            out, err = self.run_bzr(['commit', '-m', file_name])
165
 
            reflags = re.MULTILINE|re.DOTALL|re.UNICODE
166
 
            te = osutils.get_terminal_encoding()
167
 
            self.assertContainsRe(err.decode(te, 'replace'),
168
 
                u'The commit message is a file name:',
169
 
                flags=reflags)
170
 
        finally:
171
 
            osutils.get_terminal_encoding = default_get_terminal_enc
172
 
 
173
 
    def test_warn_about_forgotten_commit_message(self):
174
 
        """Test that the lack of -m parameter is caught"""
175
 
        wt = self.make_branch_and_tree('.')
176
 
        self.build_tree(['one', 'two'])
177
 
        wt.add(['two'])
178
 
        out, err = self.run_bzr('commit -m one two')
179
 
        self.assertContainsRe(err, "The commit message is a file name")
 
103
        self.assertEqual('modified hello.txt\n'
 
104
                         'Committed revision 2.\n',
 
105
                         err)
180
106
 
181
107
    def test_verbose_commit_renamed(self):
182
108
        # Verbose commit of renamed file should say so
183
109
        wt = self.prepare_simple_history()
184
110
        wt.rename_one('hello.txt', 'gutentag.txt')
185
 
        out, err = self.run_bzr('commit -m renamed')
 
111
        out, err = self.run_bzr("commit", "-m", "renamed")
186
112
        self.assertEqual('', out)
187
 
        self.assertContainsRe(err, '^Committing to: .*\n'
188
 
                              'renamed hello\.txt => gutentag\.txt\n'
189
 
                              'Committed revision 2\.$\n')
 
113
        self.assertEqual('renamed hello.txt => gutentag.txt\n'
 
114
                         'Committed revision 2.\n',
 
115
                         err)
190
116
 
191
117
    def test_verbose_commit_moved(self):
192
118
        # Verbose commit of file moved to new directory should say so
194
120
        os.mkdir('subdir')
195
121
        wt.add(['subdir'])
196
122
        wt.rename_one('hello.txt', 'subdir/hello.txt')
197
 
        out, err = self.run_bzr('commit -m renamed')
 
123
        out, err = self.run_bzr("commit", "-m", "renamed")
198
124
        self.assertEqual('', out)
199
 
        self.assertEqual(set([
200
 
            'Committing to: %s/' % osutils.getcwd(),
201
 
            'added subdir',
202
 
            'renamed hello.txt => subdir/hello.txt',
203
 
            'Committed revision 2.',
204
 
            '',
205
 
            ]), set(err.split('\n')))
 
125
        self.assertEqualDiff('added subdir\n'
 
126
                             'renamed hello.txt => subdir/hello.txt\n'
 
127
                             'Committed revision 2.\n',
 
128
                             err)
206
129
 
207
130
    def test_verbose_commit_with_unknown(self):
208
131
        """Unknown files should not be listed by default in verbose output"""
210
133
        wt = BzrDir.create_standalone_workingtree('.')
211
134
        self.build_tree(['hello.txt', 'extra.txt'])
212
135
        wt.add(['hello.txt'])
213
 
        out,err = self.run_bzr('commit -m added')
 
136
        out,err = self.run_bzr("commit", "-m", "added")
214
137
        self.assertEqual('', out)
215
 
        self.assertContainsRe(err, '^Committing to: .*\n'
216
 
                              'added hello\.txt\n'
217
 
                              'Committed revision 1\.\n$')
 
138
        self.assertEqual('added hello.txt\n'
 
139
                         'Committed revision 1.\n',
 
140
                         err)
218
141
 
219
142
    def test_verbose_commit_with_unchanged(self):
220
143
        """Unchanged files should not be listed by default in verbose output"""
221
 
        tree = self.make_branch_and_tree('.')
 
144
        self.runbzr("init")
222
145
        self.build_tree(['hello.txt', 'unchanged.txt'])
223
 
        tree.add('unchanged.txt')
224
 
        self.run_bzr('commit -m unchanged unchanged.txt')
225
 
        tree.add("hello.txt")
226
 
        out,err = self.run_bzr('commit -m added')
 
146
        self.runbzr('add unchanged.txt')
 
147
        self.runbzr('commit -m unchanged unchanged.txt')
 
148
        self.runbzr("add hello.txt")
 
149
        out,err = self.run_bzr("commit", "-m", "added")
227
150
        self.assertEqual('', out)
228
 
        self.assertContainsRe(err, '^Committing to: .*\n'
229
 
                              'added hello\.txt\n'
230
 
                              'Committed revision 2\.$\n')
231
 
 
232
 
    def test_verbose_commit_includes_master_location(self):
233
 
        """Location of master is displayed when committing to bound branch"""
234
 
        a_tree = self.make_branch_and_tree('a')
235
 
        self.build_tree(['a/b'])
236
 
        a_tree.add('b')
237
 
        a_tree.commit(message='Initial message')
238
 
 
239
 
        b_tree = a_tree.branch.create_checkout('b')
240
 
        expected = "%s/" % (osutils.abspath('a'), )
241
 
        out, err = self.run_bzr('commit -m blah --unchanged', working_dir='b')
242
 
        self.assertEqual(err, 'Committing to: %s\n'
243
 
                         'Committed revision 2.\n' % expected)
244
 
 
245
 
    def test_commit_sanitizes_CR_in_message(self):
246
 
        # See bug #433779, basically Emacs likes to pass '\r\n' style line
247
 
        # endings to 'bzr commit -m ""' which breaks because we don't allow
248
 
        # '\r' in commit messages. (Mostly because of issues where XML style
249
 
        # formats arbitrarily strip it out of the data while parsing.)
250
 
        # To make life easier for users, we just always translate '\r\n' =>
251
 
        # '\n'. And '\r' => '\n'.
252
 
        a_tree = self.make_branch_and_tree('a')
253
 
        self.build_tree(['a/b'])
254
 
        a_tree.add('b')
255
 
        self.run_bzr(['commit',
256
 
                      '-m', 'a string\r\n\r\nwith mixed\r\rendings\n'],
257
 
                     working_dir='a')
258
 
        rev_id = a_tree.branch.last_revision()
259
 
        rev = a_tree.branch.repository.get_revision(rev_id)
260
 
        self.assertEqualDiff('a string\n\nwith mixed\n\nendings\n',
261
 
                             rev.message)
 
151
        self.assertEqual('added hello.txt\n'
 
152
                         'Committed revision 2.\n',
 
153
                         err)
262
154
 
263
155
    def test_commit_merge_reports_all_modified_files(self):
264
156
        # the commit command should show all the files that are shown by
297
189
            other_tree.rename_one('dirtorename', 'renameddir')
298
190
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
299
191
            other_tree.rename_one('filetorename', 'renamedfile')
300
 
            other_tree.rename_one('filetoreparent',
301
 
                                  'renameddir/reparentedfile')
 
192
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
302
193
            other_tree.remove(['dirtoremove', 'filetoremove'])
303
194
            self.build_tree_contents([
304
 
                ('other/newdir/',),
 
195
                ('other/newdir/', ),
305
196
                ('other/filetomodify', 'new content'),
306
197
                ('other/newfile', 'new file content')])
307
198
            other_tree.add('newfile')
311
202
            other_tree.unlock()
312
203
        this_tree.merge_from_branch(other_tree.branch)
313
204
        os.chdir('this')
314
 
        out,err = self.run_bzr('commit -m added')
 
205
        out,err = self.run_bzr("commit", "-m", "added")
 
206
        os.chdir('..')
315
207
        self.assertEqual('', out)
316
 
        self.assertEqual(set([
317
 
            'Committing to: %s/' % osutils.getcwd(),
318
 
            'modified filetomodify',
319
 
            'added newdir',
320
 
            'added newfile',
321
 
            'renamed dirtorename => renameddir',
322
 
            'renamed filetorename => renamedfile',
323
 
            'renamed dirtoreparent => renameddir/reparenteddir',
324
 
            'renamed filetoreparent => renameddir/reparentedfile',
325
 
            'deleted dirtoremove',
326
 
            'deleted filetoremove',
327
 
            'Committed revision 2.',
328
 
            ''
329
 
            ]), set(err.split('\n')))
 
208
        self.assertEqualDiff(
 
209
            'modified filetomodify\n'
 
210
            'added newdir\n'
 
211
            'added newfile\n'
 
212
            'renamed dirtorename => renameddir\n'
 
213
            'renamed dirtoreparent => renameddir/reparenteddir\n'
 
214
            'renamed filetoreparent => renameddir/reparentedfile\n'
 
215
            'renamed filetorename => renamedfile\n'
 
216
            'deleted dirtoremove\n'
 
217
            'deleted filetoremove\n'
 
218
            'Committed revision 2.\n',
 
219
            err)
330
220
 
331
221
    def test_empty_commit_message(self):
332
 
        tree = self.make_branch_and_tree('.')
333
 
        self.build_tree_contents([('foo.c', 'int main() {}')])
334
 
        tree.add('foo.c')
335
 
        self.run_bzr('commit -m ""', retcode=3)
 
222
        self.runbzr("init")
 
223
        file('foo.c', 'wt').write('int main() {}')
 
224
        self.runbzr(['add', 'foo.c'])
 
225
        self.runbzr(["commit", "-m", ""] , retcode=3)
336
226
 
337
227
    def test_other_branch_commit(self):
338
228
        # this branch is to ensure consistent behaviour, whether we're run
339
229
        # inside a branch, or not.
340
 
        outer_tree = self.make_branch_and_tree('.')
341
 
        inner_tree = self.make_branch_and_tree('branch')
342
 
        self.build_tree_contents([
343
 
            ('branch/foo.c', 'int main() {}'),
344
 
            ('branch/bar.c', 'int main() {}')])
345
 
        inner_tree.add(['foo.c', 'bar.c'])
 
230
        os.mkdir('empty_branch')
 
231
        os.chdir('empty_branch')
 
232
        self.runbzr('init')
 
233
        os.mkdir('branch')
 
234
        os.chdir('branch')
 
235
        self.runbzr('init')
 
236
        file('foo.c', 'wt').write('int main() {}')
 
237
        file('bar.c', 'wt').write('int main() {}')
 
238
        os.chdir('..')
 
239
        self.runbzr(['add', 'branch/foo.c'])
 
240
        self.runbzr(['add', 'branch'])
346
241
        # can't commit files in different trees; sane error
347
 
        self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
348
 
        # can commit to branch - records foo.c only
349
 
        self.run_bzr('commit -m newstuff branch/foo.c')
350
 
        # can commit to branch - records bar.c
351
 
        self.run_bzr('commit -m newstuff branch')
352
 
        # No changes left
353
 
        self.run_bzr_error(["No changes to commit"], 'commit -m newstuff branch')
 
242
        self.runbzr('commit -m newstuff branch/foo.c .', retcode=3)
 
243
        self.runbzr('commit -m newstuff branch/foo.c')
 
244
        self.runbzr('commit -m newstuff branch')
 
245
        self.runbzr('commit -m newstuff branch', retcode=3)
354
246
 
355
247
    def test_out_of_date_tree_commit(self):
356
248
        # check we get an error code and a clear message committing with an out
357
249
        # of date checkout
358
 
        tree = self.make_branch_and_tree('branch')
 
250
        self.make_branch_and_tree('branch')
359
251
        # make a checkout
360
 
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
 
252
        self.runbzr('checkout --lightweight branch checkout')
361
253
        # commit to the original branch to make the checkout out of date
362
 
        tree.commit('message branch', allow_pointless=True)
 
254
        self.runbzr('commit --unchanged -m message branch')
363
255
        # now commit to the checkout should emit
364
256
        # ERROR: Out of date with the branch, 'bzr update' is suggested
365
 
        output = self.run_bzr('commit --unchanged -m checkout_message '
 
257
        output = self.runbzr('commit --unchanged -m checkout_message '
366
258
                             'checkout', retcode=3)
367
259
        self.assertEqual(output,
368
260
                         ('',
369
 
                          "bzr: ERROR: Working tree is out of date, please "
370
 
                          "run 'bzr update'.\n"))
 
261
                          "bzr: ERROR: Working tree is out of date, please run "
 
262
                          "'bzr update'.\n"))
371
263
 
372
264
    def test_local_commit_unbound(self):
373
265
        # a --local commit on an unbound branch is an error
374
266
        self.make_branch_and_tree('.')
375
 
        out, err = self.run_bzr('commit --local', retcode=3)
 
267
        out, err = self.run_bzr('commit', '--local', retcode=3)
376
268
        self.assertEqualDiff('', out)
377
269
        self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
378
270
                             'on unbound branches.\n', err)
380
272
    def test_commit_a_text_merge_in_a_checkout(self):
381
273
        # checkouts perform multiple actions in a transaction across bond
382
274
        # branches and their master, and have been observed to fail in the
383
 
        # past. This is a user story reported to fail in bug #43959 where
 
275
        # past. This is a user story reported to fail in bug #43959 where 
384
276
        # a merge done in a checkout (using the update command) failed to
385
277
        # commit correctly.
386
 
        trunk = self.make_branch_and_tree('trunk')
387
 
 
388
 
        u1 = trunk.branch.create_checkout('u1')
389
 
        self.build_tree_contents([('u1/hosts', 'initial contents\n')])
390
 
        u1.add('hosts')
391
 
        self.run_bzr('commit -m add-hosts u1')
392
 
 
393
 
        u2 = trunk.branch.create_checkout('u2')
394
 
        self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
395
 
        self.run_bzr('commit -m checkin-from-u2 u2')
 
278
        self.run_bzr('init', 'trunk')
 
279
 
 
280
        self.run_bzr('checkout', 'trunk', 'u1')
 
281
        self.build_tree_contents([('u1/hosts', 'initial contents')])
 
282
        self.run_bzr('add', 'u1/hosts')
 
283
        self.run_bzr('commit', '-m', 'add hosts', 'u1')
 
284
 
 
285
        self.run_bzr('checkout', 'trunk', 'u2')
 
286
        self.build_tree_contents([('u2/hosts', 'altered in u2')])
 
287
        self.run_bzr('commit', '-m', 'checkin from u2', 'u2')
396
288
 
397
289
        # make an offline commits
398
 
        self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
399
 
        self.run_bzr('commit -m checkin-offline --local u1')
 
290
        self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
 
291
        self.run_bzr('commit', '-m', 'checkin offline', '--local', 'u1')
400
292
 
401
293
        # now try to pull in online work from u2, and then commit our offline
402
294
        # work as a merge
403
295
        # retcode 1 as we expect a text conflict
404
 
        self.run_bzr('update u1', retcode=1)
405
 
        self.assertFileEqual('''\
406
 
<<<<<<< TREE
407
 
first offline change in u1
408
 
=======
409
 
altered in u2
410
 
>>>>>>> MERGE-SOURCE
411
 
''',
412
 
                             'u1/hosts')
413
 
 
414
 
        self.run_bzr('resolved u1/hosts')
 
296
        self.run_bzr('update', 'u1', retcode=1)
 
297
        self.run_bzr('resolved', 'u1/hosts')
415
298
        # add a text change here to represent resolving the merge conflicts in
416
299
        # favour of a new version of the file not identical to either the u1
417
300
        # version or the u2 version.
418
301
        self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
419
 
        self.run_bzr('commit -m checkin-merge-of-the-offline-work-from-u1 u1')
420
 
 
421
 
    def test_commit_exclude_excludes_modified_files(self):
422
 
        """Commit -x foo should ignore changes to foo."""
423
 
        tree = self.make_branch_and_tree('.')
424
 
        self.build_tree(['a', 'b', 'c'])
425
 
        tree.smart_add(['.'])
426
 
        out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b'])
427
 
        self.assertFalse('added b' in out)
428
 
        self.assertFalse('added b' in err)
429
 
        # If b was excluded it will still be 'added' in status.
430
 
        out, err = self.run_bzr(['added'])
431
 
        self.assertEqual('b\n', out)
432
 
        self.assertEqual('', err)
433
 
 
434
 
    def test_commit_exclude_twice_uses_both_rules(self):
435
 
        """Commit -x foo -x bar should ignore changes to foo and bar."""
436
 
        tree = self.make_branch_and_tree('.')
437
 
        self.build_tree(['a', 'b', 'c'])
438
 
        tree.smart_add(['.'])
439
 
        out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b', '-x', 'c'])
440
 
        self.assertFalse('added b' in out)
441
 
        self.assertFalse('added c' in out)
442
 
        self.assertFalse('added b' in err)
443
 
        self.assertFalse('added c' in err)
444
 
        # If b was excluded it will still be 'added' in status.
445
 
        out, err = self.run_bzr(['added'])
446
 
        self.assertTrue('b\n' in out)
447
 
        self.assertTrue('c\n' in out)
448
 
        self.assertEqual('', err)
 
302
        self.run_bzr('commit', '-m', 'checkin merge of the offline work from u1', 'u1')
449
303
 
450
304
    def test_commit_respects_spec_for_removals(self):
451
305
        """Commit with a file spec should only commit removals that match"""
455
309
        t.commit('Create')
456
310
        t.remove(['file-a', 'dir-a/file-b'])
457
311
        os.chdir('dir-a')
458
 
        result = self.run_bzr('commit . -m removed-file-b')[1]
 
312
        result = self.run_bzr('commit', '.', '-m' 'removed file-b')[1]
459
313
        self.assertNotContainsRe(result, 'file-a')
460
314
        result = self.run_bzr('status')[0]
461
315
        self.assertContainsRe(result, 'removed:\n  file-a')
467
321
        self.build_tree(['tree/a'])
468
322
        tree.add('a')
469
323
        # A simple change should just work
470
 
        self.run_bzr('commit --strict -m adding-a',
 
324
        self.run_bzr('commit', '--strict', '-m', 'adding a',
471
325
                     working_dir='tree')
472
326
 
473
327
    def test_strict_commit_no_changes(self):
479
333
 
480
334
        # With no changes, it should just be 'no changes'
481
335
        # Make sure that commit is failing because there is nothing to do
482
 
        self.run_bzr_error(['No changes to commit'],
483
 
                           'commit --strict -m no-changes',
 
336
        self.run_bzr_error(['no changes to commit'],
 
337
                           'commit', '--strict', '-m', 'no changes',
484
338
                           working_dir='tree')
485
339
 
486
340
        # But --strict doesn't care if you supply --unchanged
487
 
        self.run_bzr('commit --strict --unchanged -m no-changes',
 
341
        self.run_bzr('commit', '--strict', '--unchanged', '-m', 'no changes',
488
342
                     working_dir='tree')
489
343
 
490
344
    def test_strict_commit_unknown(self):
498
352
        self.build_tree(['tree/b', 'tree/c'])
499
353
        tree.add('b')
500
354
        self.run_bzr_error(['Commit refused because there are unknown files'],
501
 
                           'commit --strict -m add-b',
 
355
                           'commit', '--strict', '-m', 'add b',
502
356
                           working_dir='tree')
503
357
 
504
358
        # --no-strict overrides --strict
505
 
        self.run_bzr('commit --strict -m add-b --no-strict',
 
359
        self.run_bzr('commit', '--strict', '-m', 'add b', '--no-strict',
506
360
                     working_dir='tree')
507
 
 
508
 
    def test_fixes_bug_output(self):
509
 
        """commit --fixes=lp:23452 succeeds without output."""
510
 
        tree = self.make_branch_and_tree('tree')
511
 
        self.build_tree(['tree/hello.txt'])
512
 
        tree.add('hello.txt')
513
 
        output, err = self.run_bzr(
514
 
            'commit -m hello --fixes=lp:23452 tree/hello.txt')
515
 
        self.assertEqual('', output)
516
 
        self.assertContainsRe(err, 'Committing to: .*\n'
517
 
                              'added hello\.txt\n'
518
 
                              'Committed revision 1\.\n')
519
 
 
520
 
    def test_no_bugs_no_properties(self):
521
 
        """If no bugs are fixed, the bugs property is not set.
522
 
 
523
 
        see https://beta.launchpad.net/bzr/+bug/109613
524
 
        """
525
 
        tree = self.make_branch_and_tree('tree')
526
 
        self.build_tree(['tree/hello.txt'])
527
 
        tree.add('hello.txt')
528
 
        self.run_bzr( 'commit -m hello tree/hello.txt')
529
 
        # Get the revision properties, ignoring the branch-nick property, which
530
 
        # we don't care about for this test.
531
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
532
 
        properties = dict(last_rev.properties)
533
 
        del properties['branch-nick']
534
 
        self.assertFalse('bugs' in properties)
535
 
 
536
 
    def test_fixes_bug_sets_property(self):
537
 
        """commit --fixes=lp:234 sets the lp:234 revprop to 'fixed'."""
538
 
        tree = self.make_branch_and_tree('tree')
539
 
        self.build_tree(['tree/hello.txt'])
540
 
        tree.add('hello.txt')
541
 
        self.run_bzr('commit -m hello --fixes=lp:234 tree/hello.txt')
542
 
 
543
 
        # Get the revision properties, ignoring the branch-nick property, which
544
 
        # we don't care about for this test.
545
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
546
 
        properties = dict(last_rev.properties)
547
 
        del properties['branch-nick']
548
 
 
549
 
        self.assertEqual({'bugs': 'https://launchpad.net/bugs/234 fixed'},
550
 
                         properties)
551
 
 
552
 
    def test_fixes_multiple_bugs_sets_properties(self):
553
 
        """--fixes can be used more than once to show that bugs are fixed."""
554
 
        tree = self.make_branch_and_tree('tree')
555
 
        self.build_tree(['tree/hello.txt'])
556
 
        tree.add('hello.txt')
557
 
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=lp:235'
558
 
                     ' tree/hello.txt')
559
 
 
560
 
        # Get the revision properties, ignoring the branch-nick property, which
561
 
        # we don't care about for this test.
562
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
563
 
        properties = dict(last_rev.properties)
564
 
        del properties['branch-nick']
565
 
 
566
 
        self.assertEqual(
567
 
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
568
 
                     'https://launchpad.net/bugs/235 fixed'},
569
 
            properties)
570
 
 
571
 
    def test_fixes_bug_with_alternate_trackers(self):
572
 
        """--fixes can be used on a properly configured branch to mark bug
573
 
        fixes on multiple trackers.
574
 
        """
575
 
        tree = self.make_branch_and_tree('tree')
576
 
        tree.branch.get_config().set_user_option(
577
 
            'trac_twisted_url', 'http://twistedmatrix.com/trac')
578
 
        self.build_tree(['tree/hello.txt'])
579
 
        tree.add('hello.txt')
580
 
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=twisted:235 tree/')
581
 
 
582
 
        # Get the revision properties, ignoring the branch-nick property, which
583
 
        # we don't care about for this test.
584
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
585
 
        properties = dict(last_rev.properties)
586
 
        del properties['branch-nick']
587
 
 
588
 
        self.assertEqual(
589
 
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
590
 
                     'http://twistedmatrix.com/trac/ticket/235 fixed'},
591
 
            properties)
592
 
 
593
 
    def test_fixes_unknown_bug_prefix(self):
594
 
        tree = self.make_branch_and_tree('tree')
595
 
        self.build_tree(['tree/hello.txt'])
596
 
        tree.add('hello.txt')
597
 
        self.run_bzr_error(
598
 
            ["Unrecognized bug %s. Commit refused." % 'xxx:123'],
599
 
            'commit -m add-b --fixes=xxx:123',
600
 
            working_dir='tree')
601
 
 
602
 
    def test_fixes_invalid_bug_number(self):
603
 
        tree = self.make_branch_and_tree('tree')
604
 
        self.build_tree(['tree/hello.txt'])
605
 
        tree.add('hello.txt')
606
 
        self.run_bzr_error(
607
 
            ["Did not understand bug identifier orange: Must be an integer. "
608
 
             "See \"bzr help bugs\" for more information on this feature.\n"
609
 
             "Commit refused."],
610
 
            'commit -m add-b --fixes=lp:orange',
611
 
            working_dir='tree')
612
 
 
613
 
    def test_fixes_invalid_argument(self):
614
 
        """Raise an appropriate error when the fixes argument isn't tag:id."""
615
 
        tree = self.make_branch_and_tree('tree')
616
 
        self.build_tree(['tree/hello.txt'])
617
 
        tree.add('hello.txt')
618
 
        self.run_bzr_error(
619
 
            [r"Invalid bug orange. Must be in the form of 'tracker:id'\. "
620
 
             r"See \"bzr help bugs\" for more information on this feature.\n"
621
 
             r"Commit refused\."],
622
 
            'commit -m add-b --fixes=orange',
623
 
            working_dir='tree')
624
 
 
625
 
    def test_no_author(self):
626
 
        """If the author is not specified, the author property is not set."""
627
 
        tree = self.make_branch_and_tree('tree')
628
 
        self.build_tree(['tree/hello.txt'])
629
 
        tree.add('hello.txt')
630
 
        self.run_bzr( 'commit -m hello tree/hello.txt')
631
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
632
 
        properties = last_rev.properties
633
 
        self.assertFalse('author' in properties)
634
 
 
635
 
    def test_author_sets_property(self):
636
 
        """commit --author='John Doe <jdoe@example.com>' sets the author
637
 
           revprop.
638
 
        """
639
 
        tree = self.make_branch_and_tree('tree')
640
 
        self.build_tree(['tree/hello.txt'])
641
 
        tree.add('hello.txt')
642
 
        self.run_bzr(["commit", '-m', 'hello',
643
 
                      '--author', u'John D\xf6 <jdoe@example.com>',
644
 
                     "tree/hello.txt"])
645
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
646
 
        properties = last_rev.properties
647
 
        self.assertEqual(u'John D\xf6 <jdoe@example.com>', properties['authors'])
648
 
 
649
 
    def test_author_no_email(self):
650
 
        """Author's name without an email address is allowed, too."""
651
 
        tree = self.make_branch_and_tree('tree')
652
 
        self.build_tree(['tree/hello.txt'])
653
 
        tree.add('hello.txt')
654
 
        out, err = self.run_bzr("commit -m hello --author='John Doe' "
655
 
                                "tree/hello.txt")
656
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
657
 
        properties = last_rev.properties
658
 
        self.assertEqual('John Doe', properties['authors'])
659
 
 
660
 
    def test_multiple_authors(self):
661
 
        """Multiple authors can be specyfied, and all are stored."""
662
 
        tree = self.make_branch_and_tree('tree')
663
 
        self.build_tree(['tree/hello.txt'])
664
 
        tree.add('hello.txt')
665
 
        out, err = self.run_bzr("commit -m hello --author='John Doe' "
666
 
                                "--author='Jane Rey' tree/hello.txt")
667
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
668
 
        properties = last_rev.properties
669
 
        self.assertEqual('John Doe\nJane Rey', properties['authors'])
670
 
 
671
 
    def test_commit_time(self):
672
 
        tree = self.make_branch_and_tree('tree')
673
 
        self.build_tree(['tree/hello.txt'])
674
 
        tree.add('hello.txt')
675
 
        out, err = self.run_bzr("commit -m hello "
676
 
            "--commit-time='2009-10-10 08:00:00 +0100' tree/hello.txt")
677
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
678
 
        self.assertEqual(
679
 
            'Sat 2009-10-10 08:00:00 +0100',
680
 
            osutils.format_date(last_rev.timestamp, last_rev.timezone))
681
 
        
682
 
    def test_commit_time_bad_time(self):
683
 
        tree = self.make_branch_and_tree('tree')
684
 
        self.build_tree(['tree/hello.txt'])
685
 
        tree.add('hello.txt')
686
 
        out, err = self.run_bzr("commit -m hello "
687
 
            "--commit-time='NOT A TIME' tree/hello.txt", retcode=3)
688
 
        self.assertStartsWith(
689
 
            err, "bzr: ERROR: Could not parse --commit-time:")
690
 
 
691
 
    def test_partial_commit_with_renames_in_tree(self):
692
 
        # this test illustrates bug #140419
693
 
        t = self.make_branch_and_tree('.')
694
 
        self.build_tree(['dir/', 'dir/a', 'test'])
695
 
        t.add(['dir/', 'dir/a', 'test'])
696
 
        t.commit('initial commit')
697
 
        # important part: file dir/a should change parent
698
 
        # and should appear before old parent
699
 
        # then during partial commit we have error
700
 
        # parent_id {dir-XXX} not in inventory
701
 
        t.rename_one('dir/a', 'a')
702
 
        self.build_tree_contents([('test', 'changes in test')])
703
 
        # partial commit
704
 
        out, err = self.run_bzr('commit test -m "partial commit"')
705
 
        self.assertEquals('', out)
706
 
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
707
 
 
708
 
    def test_commit_readonly_checkout(self):
709
 
        # https://bugs.launchpad.net/bzr/+bug/129701
710
 
        # "UnlockableTransport error trying to commit in checkout of readonly
711
 
        # branch"
712
 
        self.make_branch('master')
713
 
        master = BzrDir.open_from_transport(
714
 
            self.get_readonly_transport('master')).open_branch()
715
 
        master.create_checkout('checkout')
716
 
        out, err = self.run_bzr(['commit', '--unchanged', '-mfoo', 'checkout'],
717
 
            retcode=3)
718
 
        self.assertContainsRe(err,
719
 
            r'^bzr: ERROR: Cannot lock.*readonly transport')
720
 
 
721
 
    def setup_editor(self):
722
 
        # Test that commit template hooks work
723
 
        if sys.platform == "win32":
724
 
            f = file('fed.bat', 'w')
725
 
            f.write('@rem dummy fed')
726
 
            f.close()
727
 
            self.overrideEnv('BZR_EDITOR', "fed.bat")
728
 
        else:
729
 
            f = file('fed.sh', 'wb')
730
 
            f.write('#!/bin/sh\n')
731
 
            f.close()
732
 
            os.chmod('fed.sh', 0755)
733
 
            self.overrideEnv('BZR_EDITOR', "./fed.sh")
734
 
 
735
 
    def setup_commit_with_template(self):
736
 
        self.setup_editor()
737
 
        msgeditor.hooks.install_named_hook("commit_message_template",
738
 
                lambda commit_obj, msg: "save me some typing\n", None)
739
 
        tree = self.make_branch_and_tree('tree')
740
 
        self.build_tree(['tree/hello.txt'])
741
 
        tree.add('hello.txt')
742
 
        return tree
743
 
 
744
 
    def test_commit_hook_template_accepted(self):
745
 
        tree = self.setup_commit_with_template()
746
 
        out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
747
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
748
 
        self.assertEqual('save me some typing\n', last_rev.message)
749
 
 
750
 
    def test_commit_hook_template_rejected(self):
751
 
        tree = self.setup_commit_with_template()
752
 
        expected = tree.last_revision()
753
 
        out, err = self.run_bzr_error(["empty commit message"],
754
 
            "commit tree/hello.txt", stdin="n\n")
755
 
        self.assertEqual(expected, tree.last_revision())
756
 
 
757
 
    def test_set_commit_message(self):
758
 
        msgeditor.hooks.install_named_hook("set_commit_message",
759
 
                lambda commit_obj, msg: "save me some typing\n", None)
760
 
        tree = self.make_branch_and_tree('tree')
761
 
        self.build_tree(['tree/hello.txt'])
762
 
        tree.add('hello.txt')
763
 
        out, err = self.run_bzr("commit tree/hello.txt")
764
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
765
 
        self.assertEqual('save me some typing\n', last_rev.message)
766
 
 
767
 
    def test_commit_without_username(self):
768
 
        """Ensure commit error if username is not set.
769
 
        """
770
 
        self.run_bzr(['init', 'foo'])
771
 
        os.chdir('foo')
772
 
        open('foo.txt', 'w').write('hello')
773
 
        self.run_bzr(['add'])
774
 
        self.overrideEnv('EMAIL', None)
775
 
        self.overrideEnv('BZR_EMAIL', None)
776
 
        # Also, make sure that it's not inferred from mailname.
777
 
        self.overrideAttr(config, '_auto_user_id',
778
 
            lambda: (None, None))
779
 
        out, err = self.run_bzr(['commit', '-m', 'initial'], 3)
780
 
        self.assertContainsRe(err, 'Unable to determine your name')
781
 
 
782
 
    def test_commit_recursive_checkout(self):
783
 
        """Ensure that a commit to a recursive checkout fails cleanly.
784
 
        """
785
 
        self.run_bzr(['init', 'test_branch'])
786
 
        self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
787
 
        os.chdir('test_checkout')
788
 
        self.run_bzr(['bind', '.']) # bind to self
789
 
        open('foo.txt', 'w').write('hello')
790
 
        self.run_bzr(['add'])
791
 
        out, err = self.run_bzr(['commit', '-m', 'addedfoo'], 3)
792
 
        self.assertEqual(out, '')
793
 
        self.assertContainsRe(err,
794
 
            'Branch.*test_checkout.*appears to be bound to itself')