~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2011-04-11 09:38:59 UTC
  • mfrom: (5609.33.1 2.3)
  • mto: This revision was merged to the branch mainline in revision 5783.
  • Revision ID: v.ladeuil+lp@free.fr-20110411093859-0gyunr0tkvnoskrm
Merge 2.3 to trunk

Show diffs side-by-side

added added

removed removed

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