~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Packman
  • Date: 2011-12-23 19:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 6405.
  • Revision ID: martin.packman@canonical.com-20111223193822-hesheea4o8aqwexv
Accept and document passing the medium rather than transport for smart connections

Show diffs side-by-side

added added

removed removed

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