/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

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

  • Committer: Parth Malwankar
  • Date: 2010-04-20 09:21:07 UTC
  • mto: This revision was merged to the branch mainline in revision 5214.
  • Revision ID: parth.malwankar@gmail.com-20100420092107-1s02h71fh53l68fv
updated NEWS

Show diffs side-by-side

added added

removed removed

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