~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Martin Pool
  • Date: 2007-04-04 06:17:31 UTC
  • mto: This revision was merged to the branch mainline in revision 2397.
  • Revision ID: mbp@sourcefrog.net-20070404061731-tt2xrzllqhbodn83
Contents of TODO file moved into bug tracker

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
26
    )
 
27
from bzrlib.branch import Branch
33
28
from bzrlib.bzrdir import BzrDir
34
 
from bzrlib.tests import (
35
 
    test_foreign,
36
 
    features,
37
 
    )
38
 
from bzrlib.tests import TestCaseWithTransport
39
 
 
40
 
 
41
 
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):
42
35
 
43
36
    def test_05_empty_commit(self):
44
37
        """Commit of tree with no versioned files should fail"""
45
38
        # If forced, it should succeed, but this is not tested here.
46
 
        self.make_branch_and_tree('.')
 
39
        self.run_bzr("init")
47
40
        self.build_tree(['hello.txt'])
48
 
        out,err = self.run_bzr('commit -m empty', retcode=3)
 
41
        out,err = self.run_bzr("commit", "-m", "empty", retcode=3)
49
42
        self.assertEqual('', out)
50
 
        # Two ugly bits here.
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 '\'
53
 
        self.assertThat(
54
 
            err,
55
 
            DocTestMatches("""\
56
 
Committing to: ...
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))
 
43
        self.assertStartsWith(err, 'bzr: ERROR: no changes to commit.'
 
44
                                  ' use --unchanged to commit anyhow\n')
61
45
 
62
46
    def test_commit_success(self):
63
47
        """Successful commit should not leave behind a bzr-commit-* file"""
64
 
        self.make_branch_and_tree('.')
65
 
        self.run_bzr('commit --unchanged -m message')
66
 
        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'))
67
51
 
68
52
        # same for unicode messages
69
 
        self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
70
 
        self.assertEqual('', self.run_bzr('unknowns')[0])
71
 
 
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])
77
 
 
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-'))
 
53
        self.run_bzr("commit", "--unchanged", "-m", u'foo\xb5')
 
54
        self.assertEqual('', self.capture('unknowns'))
85
55
 
86
56
    def test_commit_with_path(self):
87
57
        """Commit tree with path of root specified"""
88
 
        a_tree = self.make_branch_and_tree('a')
 
58
        self.run_bzr('init', 'a')
89
59
        self.build_tree(['a/a_file'])
90
 
        a_tree.add('a_file')
91
 
        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')
92
62
 
93
 
        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
 
63
        self.run_bzr('branch', 'a', 'b')
94
64
        self.build_tree_contents([('b/a_file', 'changes in b')])
95
 
        self.run_bzr(['commit', '-m', 'first commit in b', 'b'])
 
65
        self.run_bzr('commit', '-m', 'first commit in b', 'b')
96
66
 
97
67
        self.build_tree_contents([('a/a_file', 'new contents')])
98
 
        self.run_bzr(['commit', '-m', 'change in a', 'a'])
99
 
 
100
 
        b_tree.merge_from_branch(a_tree.branch)
101
 
        self.assertEqual(len(b_tree.conflicts()), 1)
102
 
        self.run_bzr('resolved b/a_file')
103
 
        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
 
104
76
 
105
77
    def test_10_verbose_commit(self):
106
78
        """Add one file and examine verbose commit output"""
107
 
        tree = self.make_branch_and_tree('.')
 
79
        self.runbzr("init")
108
80
        self.build_tree(['hello.txt'])
109
 
        tree.add("hello.txt")
110
 
        out,err = self.run_bzr('commit -m added')
 
81
        self.runbzr("add hello.txt")
 
82
        out,err = self.run_bzr("commit", "-m", "added")
111
83
        self.assertEqual('', out)
112
 
        self.assertContainsRe(err, '^Committing to: .*\n'
113
 
                              'added hello.txt\n'
114
 
                              'Committed revision 1.\n$',)
 
84
        self.assertEqual('added hello.txt\n'
 
85
                         'Committed revision 1.\n',
 
86
                         err)
115
87
 
116
88
    def prepare_simple_history(self):
117
89
        """Prepare and return a working tree with one commit of one file"""
126
98
        # Verbose commit of modified file should say so
127
99
        wt = self.prepare_simple_history()
128
100
        self.build_tree_contents([('hello.txt', 'new contents')])
129
 
        out, err = self.run_bzr('commit -m modified')
 
101
        out, err = self.run_bzr("commit", "-m", "modified")
130
102
        self.assertEqual('', out)
131
 
        self.assertContainsRe(err, '^Committing to: .*\n'
132
 
                              'modified hello\.txt\n'
133
 
                              'Committed revision 2\.\n$')
134
 
 
135
 
    def test_unicode_commit_message_is_filename(self):
136
 
        """Unicode commit message same as a filename (Bug #563646).
137
 
        """
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:',
148
 
            flags=reflags)
149
 
 
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
156
 
        try:
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:',
166
 
                flags=reflags)
167
 
        finally:
168
 
            osutils.get_terminal_encoding = default_get_terminal_enc
169
 
 
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'])
174
 
        wt.add(['two'])
175
 
        out, err = self.run_bzr('commit -m one two')
176
 
        self.assertContainsRe(err, "The commit message is a file name")
 
103
        self.assertEqual('modified hello.txt\n'
 
104
                         'Committed revision 2.\n',
 
105
                         err)
177
106
 
178
107
    def test_verbose_commit_renamed(self):
179
108
        # Verbose commit of renamed file should say so
180
109
        wt = self.prepare_simple_history()
181
110
        wt.rename_one('hello.txt', 'gutentag.txt')
182
 
        out, err = self.run_bzr('commit -m renamed')
 
111
        out, err = self.run_bzr("commit", "-m", "renamed")
183
112
        self.assertEqual('', out)
184
 
        self.assertContainsRe(err, '^Committing to: .*\n'
185
 
                              'renamed hello\.txt => gutentag\.txt\n'
186
 
                              'Committed revision 2\.$\n')
 
113
        self.assertEqual('renamed hello.txt => gutentag.txt\n'
 
114
                         'Committed revision 2.\n',
 
115
                         err)
187
116
 
188
117
    def test_verbose_commit_moved(self):
189
118
        # Verbose commit of file moved to new directory should say so
191
120
        os.mkdir('subdir')
192
121
        wt.add(['subdir'])
193
122
        wt.rename_one('hello.txt', 'subdir/hello.txt')
194
 
        out, err = self.run_bzr('commit -m renamed')
 
123
        out, err = self.run_bzr("commit", "-m", "renamed")
195
124
        self.assertEqual('', out)
196
 
        self.assertEqual(set([
197
 
            'Committing to: %s/' % osutils.getcwd(),
198
 
            'added subdir',
199
 
            'renamed hello.txt => subdir/hello.txt',
200
 
            'Committed revision 2.',
201
 
            '',
202
 
            ]), 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)
203
129
 
204
130
    def test_verbose_commit_with_unknown(self):
205
131
        """Unknown files should not be listed by default in verbose output"""
207
133
        wt = BzrDir.create_standalone_workingtree('.')
208
134
        self.build_tree(['hello.txt', 'extra.txt'])
209
135
        wt.add(['hello.txt'])
210
 
        out,err = self.run_bzr('commit -m added')
 
136
        out,err = self.run_bzr("commit", "-m", "added")
211
137
        self.assertEqual('', out)
212
 
        self.assertContainsRe(err, '^Committing to: .*\n'
213
 
                              'added hello\.txt\n'
214
 
                              'Committed revision 1\.\n$')
 
138
        self.assertEqual('added hello.txt\n'
 
139
                         'Committed revision 1.\n',
 
140
                         err)
215
141
 
216
142
    def test_verbose_commit_with_unchanged(self):
217
143
        """Unchanged files should not be listed by default in verbose output"""
218
 
        tree = self.make_branch_and_tree('.')
 
144
        self.runbzr("init")
219
145
        self.build_tree(['hello.txt', 'unchanged.txt'])
220
 
        tree.add('unchanged.txt')
221
 
        self.run_bzr('commit -m unchanged unchanged.txt')
222
 
        tree.add("hello.txt")
223
 
        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")
224
150
        self.assertEqual('', out)
225
 
        self.assertContainsRe(err, '^Committing to: .*\n'
226
 
                              'added hello\.txt\n'
227
 
                              'Committed revision 2\.$\n')
228
 
 
229
 
    def test_verbose_commit_includes_master_location(self):
230
 
        """Location of master is displayed when committing to bound branch"""
231
 
        a_tree = self.make_branch_and_tree('a')
232
 
        self.build_tree(['a/b'])
233
 
        a_tree.add('b')
234
 
        a_tree.commit(message='Initial message')
235
 
 
236
 
        b_tree = a_tree.branch.create_checkout('b')
237
 
        expected = "%s/" % (osutils.abspath('a'), )
238
 
        out, err = self.run_bzr('commit -m blah --unchanged', working_dir='b')
239
 
        self.assertEqual(err, 'Committing to: %s\n'
240
 
                         'Committed revision 2.\n' % expected)
241
 
 
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'])
251
 
        a_tree.add('b')
252
 
        self.run_bzr(['commit',
253
 
                      '-m', 'a string\r\n\r\nwith mixed\r\rendings\n'],
254
 
                     working_dir='a')
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',
258
 
                             rev.message)
 
151
        self.assertEqual('added hello.txt\n'
 
152
                         'Committed revision 2.\n',
 
153
                         err)
259
154
 
260
155
    def test_commit_merge_reports_all_modified_files(self):
261
156
        # the commit command should show all the files that are shown by
294
189
            other_tree.rename_one('dirtorename', 'renameddir')
295
190
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
296
191
            other_tree.rename_one('filetorename', 'renamedfile')
297
 
            other_tree.rename_one('filetoreparent',
298
 
                                  'renameddir/reparentedfile')
 
192
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
299
193
            other_tree.remove(['dirtoremove', 'filetoremove'])
300
194
            self.build_tree_contents([
301
 
                ('other/newdir/',),
 
195
                ('other/newdir/', ),
302
196
                ('other/filetomodify', 'new content'),
303
197
                ('other/newfile', 'new file content')])
304
198
            other_tree.add('newfile')
308
202
            other_tree.unlock()
309
203
        this_tree.merge_from_branch(other_tree.branch)
310
204
        os.chdir('this')
311
 
        out,err = self.run_bzr('commit -m added')
 
205
        out,err = self.run_bzr("commit", "-m", "added")
 
206
        os.chdir('..')
312
207
        self.assertEqual('', out)
313
 
        self.assertEqual(set([
314
 
            'Committing to: %s/' % osutils.getcwd(),
315
 
            'modified filetomodify',
316
 
            'added newdir',
317
 
            'added newfile',
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.',
325
 
            ''
326
 
            ]), 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)
327
220
 
328
221
    def test_empty_commit_message(self):
329
 
        tree = self.make_branch_and_tree('.')
330
 
        self.build_tree_contents([('foo.c', 'int main() {}')])
331
 
        tree.add('foo.c')
332
 
        self.run_bzr('commit -m ""')
 
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)
333
226
 
334
227
    def test_other_branch_commit(self):
335
228
        # this branch is to ensure consistent behaviour, whether we're run
336
229
        # inside a branch, or not.
337
 
        outer_tree = self.make_branch_and_tree('.')
338
 
        inner_tree = self.make_branch_and_tree('branch')
339
 
        self.build_tree_contents([
340
 
            ('branch/foo.c', 'int main() {}'),
341
 
            ('branch/bar.c', 'int main() {}')])
342
 
        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'])
343
241
        # can't commit files in different trees; sane error
344
 
        self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
345
 
        # can commit to branch - records foo.c only
346
 
        self.run_bzr('commit -m newstuff branch/foo.c')
347
 
        # can commit to branch - records bar.c
348
 
        self.run_bzr('commit -m newstuff branch')
349
 
        # No changes left
350
 
        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)
351
246
 
352
247
    def test_out_of_date_tree_commit(self):
353
248
        # check we get an error code and a clear message committing with an out
354
249
        # of date checkout
355
 
        tree = self.make_branch_and_tree('branch')
 
250
        self.make_branch_and_tree('branch')
356
251
        # make a checkout
357
 
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
 
252
        self.runbzr('checkout --lightweight branch checkout')
358
253
        # commit to the original branch to make the checkout out of date
359
 
        tree.commit('message branch', allow_pointless=True)
 
254
        self.runbzr('commit --unchanged -m message branch')
360
255
        # now commit to the checkout should emit
361
256
        # ERROR: Out of date with the branch, 'bzr update' is suggested
362
 
        output = self.run_bzr('commit --unchanged -m checkout_message '
 
257
        output = self.runbzr('commit --unchanged -m checkout_message '
363
258
                             'checkout', retcode=3)
364
259
        self.assertEqual(output,
365
260
                         ('',
366
 
                          "bzr: ERROR: Working tree is out of date, please "
367
 
                          "run 'bzr update'.\n"))
 
261
                          "bzr: ERROR: Working tree is out of date, please run "
 
262
                          "'bzr update'.\n"))
368
263
 
369
264
    def test_local_commit_unbound(self):
370
265
        # a --local commit on an unbound branch is an error
371
266
        self.make_branch_and_tree('.')
372
 
        out, err = self.run_bzr('commit --local', retcode=3)
 
267
        out, err = self.run_bzr('commit', '--local', retcode=3)
373
268
        self.assertEqualDiff('', out)
374
269
        self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
375
270
                             'on unbound branches.\n', err)
377
272
    def test_commit_a_text_merge_in_a_checkout(self):
378
273
        # checkouts perform multiple actions in a transaction across bond
379
274
        # 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
 
275
        # past. This is a user story reported to fail in bug #43959 where 
381
276
        # a merge done in a checkout (using the update command) failed to
382
277
        # commit correctly.
383
 
        trunk = self.make_branch_and_tree('trunk')
384
 
 
385
 
        u1 = trunk.branch.create_checkout('u1')
386
 
        self.build_tree_contents([('u1/hosts', 'initial contents\n')])
387
 
        u1.add('hosts')
388
 
        self.run_bzr('commit -m add-hosts u1')
389
 
 
390
 
        u2 = trunk.branch.create_checkout('u2')
391
 
        self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
392
 
        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')
393
288
 
394
289
        # make an offline commits
395
 
        self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
396
 
        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')
397
292
 
398
293
        # now try to pull in online work from u2, and then commit our offline
399
294
        # work as a merge
400
295
        # retcode 1 as we expect a text conflict
401
 
        self.run_bzr('update u1', retcode=1)
402
 
        self.assertFileEqual('''\
403
 
<<<<<<< TREE
404
 
first offline change in u1
405
 
=======
406
 
altered in u2
407
 
>>>>>>> MERGE-SOURCE
408
 
''',
409
 
                             'u1/hosts')
410
 
 
411
 
        self.run_bzr('resolved u1/hosts')
 
296
        self.run_bzr('update', 'u1', retcode=1)
 
297
        self.run_bzr('resolved', 'u1/hosts')
412
298
        # add a text change here to represent resolving the merge conflicts in
413
299
        # favour of a new version of the file not identical to either the u1
414
300
        # version or the u2 version.
415
301
        self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
416
 
        self.run_bzr('commit -m checkin-merge-of-the-offline-work-from-u1 u1')
417
 
 
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)
430
 
 
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)
 
302
        self.run_bzr('commit', '-m', 'checkin merge of the offline work from u1', 'u1')
446
303
 
447
304
    def test_commit_respects_spec_for_removals(self):
448
305
        """Commit with a file spec should only commit removals that match"""
452
309
        t.commit('Create')
453
310
        t.remove(['file-a', 'dir-a/file-b'])
454
311
        os.chdir('dir-a')
455
 
        result = self.run_bzr('commit . -m removed-file-b')[1]
 
312
        result = self.run_bzr('commit', '.', '-m' 'removed file-b')[1]
456
313
        self.assertNotContainsRe(result, 'file-a')
457
314
        result = self.run_bzr('status')[0]
458
315
        self.assertContainsRe(result, 'removed:\n  file-a')
464
321
        self.build_tree(['tree/a'])
465
322
        tree.add('a')
466
323
        # A simple change should just work
467
 
        self.run_bzr('commit --strict -m adding-a',
 
324
        self.run_bzr('commit', '--strict', '-m', 'adding a',
468
325
                     working_dir='tree')
469
326
 
470
327
    def test_strict_commit_no_changes(self):
476
333
 
477
334
        # With no changes, it should just be 'no changes'
478
335
        # Make sure that commit is failing because there is nothing to do
479
 
        self.run_bzr_error(['No changes to commit'],
480
 
                           'commit --strict -m no-changes',
 
336
        self.run_bzr_error(['no changes to commit'],
 
337
                           'commit', '--strict', '-m', 'no changes',
481
338
                           working_dir='tree')
482
339
 
483
340
        # But --strict doesn't care if you supply --unchanged
484
 
        self.run_bzr('commit --strict --unchanged -m no-changes',
 
341
        self.run_bzr('commit', '--strict', '--unchanged', '-m', 'no changes',
485
342
                     working_dir='tree')
486
343
 
487
344
    def test_strict_commit_unknown(self):
495
352
        self.build_tree(['tree/b', 'tree/c'])
496
353
        tree.add('b')
497
354
        self.run_bzr_error(['Commit refused because there are unknown files'],
498
 
                           'commit --strict -m add-b',
 
355
                           'commit', '--strict', '-m', 'add b',
499
356
                           working_dir='tree')
500
357
 
501
358
        # --no-strict overrides --strict
502
 
        self.run_bzr('commit --strict -m add-b --no-strict',
 
359
        self.run_bzr('commit', '--strict', '-m', 'add b', '--no-strict',
503
360
                     working_dir='tree')
504
 
 
505
 
    def test_fixes_bug_output(self):
506
 
        """commit --fixes=lp:23452 succeeds without output."""
507
 
        tree = self.make_branch_and_tree('tree')
508
 
        self.build_tree(['tree/hello.txt'])
509
 
        tree.add('hello.txt')
510
 
        output, err = self.run_bzr(
511
 
            'commit -m hello --fixes=lp:23452 tree/hello.txt')
512
 
        self.assertEqual('', output)
513
 
        self.assertContainsRe(err, 'Committing to: .*\n'
514
 
                              'added hello\.txt\n'
515
 
                              'Committed revision 1\.\n')
516
 
 
517
 
    def test_no_bugs_no_properties(self):
518
 
        """If no bugs are fixed, the bugs property is not set.
519
 
 
520
 
        see https://beta.launchpad.net/bzr/+bug/109613
521
 
        """
522
 
        tree = self.make_branch_and_tree('tree')
523
 
        self.build_tree(['tree/hello.txt'])
524
 
        tree.add('hello.txt')
525
 
        self.run_bzr( 'commit -m hello tree/hello.txt')
526
 
        # Get the revision properties, ignoring the branch-nick property, which
527
 
        # we don't care about for this test.
528
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
529
 
        properties = dict(last_rev.properties)
530
 
        del properties['branch-nick']
531
 
        self.assertFalse('bugs' in properties)
532
 
 
533
 
    def test_fixes_bug_sets_property(self):
534
 
        """commit --fixes=lp:234 sets the lp:234 revprop to 'fixed'."""
535
 
        tree = self.make_branch_and_tree('tree')
536
 
        self.build_tree(['tree/hello.txt'])
537
 
        tree.add('hello.txt')
538
 
        self.run_bzr('commit -m hello --fixes=lp:234 tree/hello.txt')
539
 
 
540
 
        # Get the revision properties, ignoring the branch-nick property, which
541
 
        # we don't care about for this test.
542
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
543
 
        properties = dict(last_rev.properties)
544
 
        del properties['branch-nick']
545
 
 
546
 
        self.assertEqual({'bugs': 'https://launchpad.net/bugs/234 fixed'},
547
 
                         properties)
548
 
 
549
 
    def test_fixes_multiple_bugs_sets_properties(self):
550
 
        """--fixes can be used more than once to show that bugs are fixed."""
551
 
        tree = self.make_branch_and_tree('tree')
552
 
        self.build_tree(['tree/hello.txt'])
553
 
        tree.add('hello.txt')
554
 
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=lp:235'
555
 
                     ' tree/hello.txt')
556
 
 
557
 
        # Get the revision properties, ignoring the branch-nick property, which
558
 
        # we don't care about for this test.
559
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
560
 
        properties = dict(last_rev.properties)
561
 
        del properties['branch-nick']
562
 
 
563
 
        self.assertEqual(
564
 
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
565
 
                     'https://launchpad.net/bugs/235 fixed'},
566
 
            properties)
567
 
 
568
 
    def test_fixes_bug_with_alternate_trackers(self):
569
 
        """--fixes can be used on a properly configured branch to mark bug
570
 
        fixes on multiple trackers.
571
 
        """
572
 
        tree = self.make_branch_and_tree('tree')
573
 
        tree.branch.get_config().set_user_option(
574
 
            'trac_twisted_url', 'http://twistedmatrix.com/trac')
575
 
        self.build_tree(['tree/hello.txt'])
576
 
        tree.add('hello.txt')
577
 
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=twisted:235 tree/')
578
 
 
579
 
        # Get the revision properties, ignoring the branch-nick property, which
580
 
        # we don't care about for this test.
581
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
582
 
        properties = dict(last_rev.properties)
583
 
        del properties['branch-nick']
584
 
 
585
 
        self.assertEqual(
586
 
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
587
 
                     'http://twistedmatrix.com/trac/ticket/235 fixed'},
588
 
            properties)
589
 
 
590
 
    def test_fixes_unknown_bug_prefix(self):
591
 
        tree = self.make_branch_and_tree('tree')
592
 
        self.build_tree(['tree/hello.txt'])
593
 
        tree.add('hello.txt')
594
 
        self.run_bzr_error(
595
 
            ["Unrecognized bug %s. Commit refused." % 'xxx:123'],
596
 
            'commit -m add-b --fixes=xxx:123',
597
 
            working_dir='tree')
598
 
 
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')
604
 
        self.run_bzr_error(
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. "
609
 
            "Commit refused."],
610
 
            'commit -m add-b --fixes=123',
611
 
            working_dir='tree')
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'])
617
 
 
618
 
    def test_fixes_invalid_bug_number(self):
619
 
        tree = self.make_branch_and_tree('tree')
620
 
        self.build_tree(['tree/hello.txt'])
621
 
        tree.add('hello.txt')
622
 
        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"
625
 
             "Commit refused."],
626
 
            'commit -m add-b --fixes=lp:orange',
627
 
            working_dir='tree')
628
 
 
629
 
    def test_fixes_invalid_argument(self):
630
 
        """Raise an appropriate error when the fixes argument isn't tag:id."""
631
 
        tree = self.make_branch_and_tree('tree')
632
 
        self.build_tree(['tree/hello.txt'])
633
 
        tree.add('hello.txt')
634
 
        self.run_bzr_error(
635
 
            [r"Invalid bug orange:apples:bananas. Must be in the form of "
636
 
             r"'tracker:id'\. See \"bzr help bugs\" for more information on "
637
 
             r"this feature.\nCommit refused\."],
638
 
            'commit -m add-b --fixes=orange:apples:bananas',
639
 
            working_dir='tree')
640
 
 
641
 
    def test_no_author(self):
642
 
        """If the author is not specified, the author property is not set."""
643
 
        tree = self.make_branch_and_tree('tree')
644
 
        self.build_tree(['tree/hello.txt'])
645
 
        tree.add('hello.txt')
646
 
        self.run_bzr( 'commit -m hello tree/hello.txt')
647
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
648
 
        properties = last_rev.properties
649
 
        self.assertFalse('author' in properties)
650
 
 
651
 
    def test_author_sets_property(self):
652
 
        """commit --author='John Doe <jdoe@example.com>' sets the author
653
 
           revprop.
654
 
        """
655
 
        tree = self.make_branch_and_tree('tree')
656
 
        self.build_tree(['tree/hello.txt'])
657
 
        tree.add('hello.txt')
658
 
        self.run_bzr(["commit", '-m', 'hello',
659
 
                      '--author', u'John D\xf6 <jdoe@example.com>',
660
 
                     "tree/hello.txt"])
661
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
662
 
        properties = last_rev.properties
663
 
        self.assertEqual(u'John D\xf6 <jdoe@example.com>', properties['authors'])
664
 
 
665
 
    def test_author_no_email(self):
666
 
        """Author's name without an email address is allowed, too."""
667
 
        tree = self.make_branch_and_tree('tree')
668
 
        self.build_tree(['tree/hello.txt'])
669
 
        tree.add('hello.txt')
670
 
        out, err = self.run_bzr("commit -m hello --author='John Doe' "
671
 
                                "tree/hello.txt")
672
 
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
673
 
        properties = last_rev.properties
674
 
        self.assertEqual('John Doe', properties['authors'])
675
 
 
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'])
686
 
 
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())
694
 
        self.assertEqual(
695
 
            'Sat 2009-10-10 08:00:00 +0100',
696
 
            osutils.format_date(last_rev.timestamp, last_rev.timezone))
697
 
        
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:")
706
 
 
707
 
    def test_partial_commit_with_renames_in_tree(self):
708
 
        # this test illustrates bug #140419
709
 
        t = self.make_branch_and_tree('.')
710
 
        self.build_tree(['dir/', 'dir/a', 'test'])
711
 
        t.add(['dir/', 'dir/a', 'test'])
712
 
        t.commit('initial commit')
713
 
        # important part: file dir/a should change parent
714
 
        # and should appear before old parent
715
 
        # then during partial commit we have error
716
 
        # parent_id {dir-XXX} not in inventory
717
 
        t.rename_one('dir/a', 'a')
718
 
        self.build_tree_contents([('test', 'changes in test')])
719
 
        # partial commit
720
 
        out, err = self.run_bzr('commit test -m "partial commit"')
721
 
        self.assertEquals('', out)
722
 
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')
723
 
 
724
 
    def test_commit_readonly_checkout(self):
725
 
        # https://bugs.launchpad.net/bzr/+bug/129701
726
 
        # "UnlockableTransport error trying to commit in checkout of readonly
727
 
        # branch"
728
 
        self.make_branch('master')
729
 
        master = BzrDir.open_from_transport(
730
 
            self.get_readonly_transport('master')).open_branch()
731
 
        master.create_checkout('checkout')
732
 
        out, err = self.run_bzr(['commit', '--unchanged', '-mfoo', 'checkout'],
733
 
            retcode=3)
734
 
        self.assertContainsRe(err,
735
 
            r'^bzr: ERROR: Cannot lock.*readonly transport')
736
 
 
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')
742
 
            f.close()
743
 
            self.overrideEnv('BZR_EDITOR', "fed.bat")
744
 
        else:
745
 
            f = file('fed.sh', 'wb')
746
 
            f.write('#!/bin/sh\n')
747
 
            f.close()
748
 
            os.chmod('fed.sh', 0755)
749
 
            self.overrideEnv('BZR_EDITOR', "./fed.sh")
750
 
 
751
 
    def setup_commit_with_template(self):
752
 
        self.setup_editor()
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')
758
 
        return tree
759
 
 
760
 
    def test_edit_empty_message(self):
761
 
        tree = self.make_branch_and_tree('tree')
762
 
        self.setup_editor()
763
 
        self.build_tree(['tree/hello.txt'])
764
 
        tree.add('hello.txt')
765
 
        out, err = self.run_bzr("commit tree/hello.txt", retcode=3,
766
 
            stdin="y\n")
767
 
        self.assertContainsRe(err,
768
 
            "bzr: ERROR: Empty commit message specified")
769
 
 
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)
775
 
 
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())
785
 
 
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)
795
 
 
796
 
    def test_commit_without_username(self):
797
 
        """Ensure commit error if username is not set.
798
 
        """
799
 
        self.run_bzr(['init', 'foo'])
800
 
        os.chdir('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')
810
 
 
811
 
    def test_commit_recursive_checkout(self):
812
 
        """Ensure that a commit to a recursive checkout fails cleanly.
813
 
        """
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')