~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005, 2006 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
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.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
"""Tests for the commit CLI of bzr."""
19
20
import os
21
import re
22
import sys
23
1551.9.5 by Aaron Bentley
Revert broken save-commit-message code
24
from bzrlib import (
25
    ignores,
26
    )
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
27
from bzrlib.branch import Branch
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
28
from bzrlib.bzrdir import BzrDir
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
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):
35
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
36
    def test_05_empty_commit(self):
37
        """Commit of tree with no versioned files should fail"""
38
        # If forced, it should succeed, but this is not tested here.
1711.2.60 by John Arbash Meinel
Fix an empty commit to raise the right exception.
39
        self.run_bzr("init")
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
40
        self.build_tree(['hello.txt'])
2089.1.1 by wang
If a commit fails, the commit message is stored in a file at the root of
41
        out,err = self.run_bzr("commit", "-m", "empty", retcode=3)
42
        self.assertEqual('', out)
43
        self.assertStartsWith(err, 'bzr: ERROR: no changes to commit.'
44
                                  ' use --unchanged to commit anyhow\n')
45
46
    def test_commit_success(self):
47
        """Successful commit should not leave behind a bzr-commit-* file"""
48
        self.run_bzr("init")
49
        self.run_bzr("commit", "--unchanged", "-m", "message")
50
        self.assertEqual('', self.capture('unknowns'))
51
52
        # same for unicode messages
53
        self.run_bzr("commit", "--unchanged", "-m", u'foo\xb5')
54
        self.assertEqual('', self.capture('unknowns'))
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
55
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
56
    def test_commit_with_path(self):
57
        """Commit tree with path of root specified"""
58
        self.run_bzr('init', 'a')
59
        self.build_tree(['a/a_file'])
60
        self.run_bzr('add', 'a/a_file')
61
        self.run_bzr('commit', '-m', 'first commit', 'a')
62
63
        self.run_bzr('branch', 'a', 'b')
64
        self.build_tree_contents([('b/a_file', 'changes in b')])
65
        self.run_bzr('commit', '-m', 'first commit in b', 'b')
66
67
        self.build_tree_contents([('a/a_file', 'new contents')])
68
        self.run_bzr('commit', '-m', 'change in a', 'a')
69
70
        os.chdir('b')
71
        self.run_bzr('merge', '../a', retcode=1) # will conflict
72
        os.chdir('..')
73
        self.run_bzr('resolved', 'b/a_file')
74
        self.run_bzr('commit', '-m', 'merge into b', 'b')
75
76
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
77
    def test_10_verbose_commit(self):
78
        """Add one file and examine verbose commit output"""
79
        self.runbzr("init")
80
        self.build_tree(['hello.txt'])
81
        self.runbzr("add hello.txt")
82
        out,err = self.run_bzr("commit", "-m", "added")
83
        self.assertEqual('', out)
84
        self.assertEqual('added hello.txt\n'
85
                         'Committed revision 1.\n',
86
                         err)
87
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
88
    def prepare_simple_history(self):
89
        """Prepare and return a working tree with one commit of one file"""
90
        # Commit with modified file should say so
91
        wt = BzrDir.create_standalone_workingtree('.')
92
        self.build_tree(['hello.txt', 'extra.txt'])
93
        wt.add(['hello.txt'])
94
        wt.commit(message='added')
95
        return wt
96
97
    def test_verbose_commit_modified(self):
98
        # Verbose commit of modified file should say so
99
        wt = self.prepare_simple_history()
100
        self.build_tree_contents([('hello.txt', 'new contents')])
101
        out, err = self.run_bzr("commit", "-m", "modified")
102
        self.assertEqual('', out)
103
        self.assertEqual('modified hello.txt\n'
104
                         'Committed revision 2.\n',
105
                         err)
106
107
    def test_verbose_commit_renamed(self):
108
        # Verbose commit of renamed file should say so
109
        wt = self.prepare_simple_history()
110
        wt.rename_one('hello.txt', 'gutentag.txt')
111
        out, err = self.run_bzr("commit", "-m", "renamed")
112
        self.assertEqual('', out)
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
113
        self.assertEqual('renamed hello.txt => gutentag.txt\n'
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
114
                         'Committed revision 2.\n',
115
                         err)
116
117
    def test_verbose_commit_moved(self):
118
        # Verbose commit of file moved to new directory should say so
119
        wt = self.prepare_simple_history()
120
        os.mkdir('subdir')
121
        wt.add(['subdir'])
122
        wt.rename_one('hello.txt', 'subdir/hello.txt')
123
        out, err = self.run_bzr("commit", "-m", "renamed")
124
        self.assertEqual('', out)
125
        self.assertEqualDiff('added subdir\n'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
126
                             'renamed hello.txt => subdir/hello.txt\n'
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
127
                             'Committed revision 2.\n',
128
                             err)
129
130
    def test_verbose_commit_with_unknown(self):
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
131
        """Unknown files should not be listed by default in verbose output"""
132
        # Is that really the best policy?
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
133
        wt = BzrDir.create_standalone_workingtree('.')
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
134
        self.build_tree(['hello.txt', 'extra.txt'])
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
135
        wt.add(['hello.txt'])
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
136
        out,err = self.run_bzr("commit", "-m", "added")
137
        self.assertEqual('', out)
138
        self.assertEqual('added hello.txt\n'
139
                         'Committed revision 1.\n',
140
                         err)
141
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
142
    def test_verbose_commit_with_unchanged(self):
1616.1.4 by Martin Pool
Verbose commit shouldn't talk about every unchanged file.
143
        """Unchanged files should not be listed by default in verbose output"""
144
        self.runbzr("init")
145
        self.build_tree(['hello.txt', 'unchanged.txt'])
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")
150
        self.assertEqual('', out)
151
        self.assertEqual('added hello.txt\n'
152
                         'Committed revision 2.\n',
153
                         err)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
154
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
155
    def test_commit_merge_reports_all_modified_files(self):
156
        # the commit command should show all the files that are shown by
157
        # bzr diff or bzr status when committing, even when they were not
158
        # changed by the user but rather through doing a merge.
159
        this_tree = self.make_branch_and_tree('this')
160
        # we need a bunch of files and dirs, to perform one action on each.
161
        self.build_tree([
162
            'this/dirtorename/',
163
            'this/dirtoreparent/',
164
            'this/dirtoleave/',
165
            'this/dirtoremove/',
166
            'this/filetoreparent',
167
            'this/filetorename',
168
            'this/filetomodify',
169
            'this/filetoremove',
170
            'this/filetoleave']
171
            )
172
        this_tree.add([
173
            'dirtorename',
174
            'dirtoreparent',
175
            'dirtoleave',
176
            'dirtoremove',
177
            'filetoreparent',
178
            'filetorename',
179
            'filetomodify',
180
            'filetoremove',
181
            'filetoleave']
182
            )
183
        this_tree.commit('create_files')
184
        other_dir = this_tree.bzrdir.sprout('other')
185
        other_tree = other_dir.open_workingtree()
186
        other_tree.lock_write()
187
        # perform the needed actions on the files and dirs.
188
        try:
189
            other_tree.rename_one('dirtorename', 'renameddir')
190
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
191
            other_tree.rename_one('filetorename', 'renamedfile')
192
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
193
            other_tree.remove(['dirtoremove', 'filetoremove'])
194
            self.build_tree_contents([
195
                ('other/newdir/', ),
196
                ('other/filetomodify', 'new content'),
197
                ('other/newfile', 'new file content')])
198
            other_tree.add('newfile')
199
            other_tree.add('newdir/')
200
            other_tree.commit('modify all sample files and dirs.')
201
        finally:
202
            other_tree.unlock()
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
203
        this_tree.merge_from_branch(other_tree.branch)
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
204
        os.chdir('this')
205
        out,err = self.run_bzr("commit", "-m", "added")
206
        os.chdir('..')
207
        self.assertEqual('', out)
208
        self.assertEqualDiff(
209
            'modified filetomodify\n'
210
            'added newdir\n'
211
            'added newfile\n'
212
            'renamed dirtorename => renameddir\n'
213
            'renamed dirtoreparent => renameddir/reparenteddir\n'
214
            'renamed filetoreparent => renameddir/reparentedfile\n'
215
            'renamed filetorename => renamedfile\n'
216
            'deleted dirtoremove\n'
217
            'deleted filetoremove\n'
218
            'Committed revision 2.\n',
219
            err)
220
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
221
    def test_empty_commit_message(self):
222
        self.runbzr("init")
223
        file('foo.c', 'wt').write('int main() {}')
224
        self.runbzr(['add', 'foo.c'])
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
225
        self.runbzr(["commit", "-m", ""] , retcode=3)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
226
227
    def test_other_branch_commit(self):
228
        # this branch is to ensure consistent behaviour, whether we're run
229
        # inside a branch, or not.
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'])
241
        # can't commit files in different trees; sane error
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)
246
247
    def test_out_of_date_tree_commit(self):
248
        # check we get an error code and a clear message committing with an out
249
        # of date checkout
250
        self.make_branch_and_tree('branch')
251
        # make a checkout
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
252
        self.runbzr('checkout --lightweight branch checkout')
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
253
        # commit to the original branch to make the checkout out of date
254
        self.runbzr('commit --unchanged -m message branch')
255
        # now commit to the checkout should emit
256
        # ERROR: Out of date with the branch, 'bzr update' is suggested
257
        output = self.runbzr('commit --unchanged -m checkout_message '
258
                             'checkout', retcode=3)
259
        self.assertEqual(output,
260
                         ('',
261
                          "bzr: ERROR: Working tree is out of date, please run "
262
                          "'bzr update'.\n"))
1587.1.8 by Robert Collins
Local commits on unbound branches fail.
263
264
    def test_local_commit_unbound(self):
265
        # a --local commit on an unbound branch is an error
266
        self.make_branch_and_tree('.')
267
        out, err = self.run_bzr('commit', '--local', retcode=3)
268
        self.assertEqualDiff('', out)
269
        self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
270
                             'on unbound branches.\n', err)
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
271
272
    def test_commit_a_text_merge_in_a_checkout(self):
273
        # checkouts perform multiple actions in a transaction across bond
274
        # branches and their master, and have been observed to fail in the
275
        # past. This is a user story reported to fail in bug #43959 where 
276
        # a merge done in a checkout (using the update command) failed to
277
        # commit correctly.
278
        self.run_bzr('init', 'trunk')
279
280
        self.run_bzr('checkout', 'trunk', 'u1')
281
        self.build_tree_contents([('u1/hosts', 'initial contents')])
282
        self.run_bzr('add', 'u1/hosts')
283
        self.run_bzr('commit', '-m', 'add hosts', 'u1')
284
285
        self.run_bzr('checkout', 'trunk', 'u2')
286
        self.build_tree_contents([('u2/hosts', 'altered in u2')])
287
        self.run_bzr('commit', '-m', 'checkin from u2', 'u2')
288
289
        # make an offline commits
290
        self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
291
        self.run_bzr('commit', '-m', 'checkin offline', '--local', 'u1')
292
293
        # now try to pull in online work from u2, and then commit our offline
294
        # work as a merge
295
        # retcode 1 as we expect a text conflict
296
        self.run_bzr('update', 'u1', retcode=1)
297
        self.run_bzr('resolved', 'u1/hosts')
298
        # add a text change here to represent resolving the merge conflicts in
299
        # favour of a new version of the file not identical to either the u1
300
        # version or the u2 version.
301
        self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
1704.2.12 by Martin Pool
use the correct transaction when committing snapshot (Malone: #43959)
302
        self.run_bzr('commit', '-m', 'checkin merge of the offline work from u1', 'u1')
1551.7.24 by Aaron Bentley
Ensure commit respects file spec when committing removals
303
304
    def test_commit_respects_spec_for_removals(self):
305
        """Commit with a file spec should only commit removals that match"""
306
        t = self.make_branch_and_tree('.')
307
        self.build_tree(['file-a', 'dir-a/', 'dir-a/file-b'])
308
        t.add(['file-a', 'dir-a', 'dir-a/file-b'])
309
        t.commit('Create')
310
        t.remove(['file-a', 'dir-a/file-b'])
311
        os.chdir('dir-a')
312
        result = self.run_bzr('commit', '.', '-m' 'removed file-b')[1]
313
        self.assertNotContainsRe(result, 'file-a')
314
        result = self.run_bzr('status')[0]
315
        self.assertContainsRe(result, 'removed:\n  file-a')
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
316
317
    def test_strict_commit(self):
318
        """Commit with --strict works if everything is known"""
1551.9.5 by Aaron Bentley
Revert broken save-commit-message code
319
        ignores._set_user_ignores([])
2116.2.1 by John Arbash Meinel
Add commit --strict tests, and add a default ignore so that commit --strict works again
320
        tree = self.make_branch_and_tree('tree')
321
        self.build_tree(['tree/a'])
322
        tree.add('a')
323
        # A simple change should just work
324
        self.run_bzr('commit', '--strict', '-m', 'adding a',
325
                     working_dir='tree')
326
327
    def test_strict_commit_no_changes(self):
328
        """commit --strict gives "no changes" if there is nothing to commit"""
329
        tree = self.make_branch_and_tree('tree')
330
        self.build_tree(['tree/a'])
331
        tree.add('a')
332
        tree.commit('adding a')
333
334
        # With no changes, it should just be 'no changes'
335
        # Make sure that commit is failing because there is nothing to do
336
        self.run_bzr_error(['no changes to commit'],
337
                           'commit', '--strict', '-m', 'no changes',
338
                           working_dir='tree')
339
340
        # But --strict doesn't care if you supply --unchanged
341
        self.run_bzr('commit', '--strict', '--unchanged', '-m', 'no changes',
342
                     working_dir='tree')
343
344
    def test_strict_commit_unknown(self):
345
        """commit --strict fails if a file is unknown"""
346
        tree = self.make_branch_and_tree('tree')
347
        self.build_tree(['tree/a'])
348
        tree.add('a')
349
        tree.commit('adding a')
350
351
        # Add one file so there is a change, but forget the other
352
        self.build_tree(['tree/b', 'tree/c'])
353
        tree.add('b')
354
        self.run_bzr_error(['Commit refused because there are unknown files'],
355
                           'commit', '--strict', '-m', 'add b',
356
                           working_dir='tree')
357
358
        # --no-strict overrides --strict
359
        self.run_bzr('commit', '--strict', '-m', 'add b', '--no-strict',
360
                     working_dir='tree')