~bzr-pqm/bzr/bzr.dev

1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
1
# Copyright (C) 2005, 2006 by 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., 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
24
from bzrlib.branch import Branch
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
25
from bzrlib.bzrdir import BzrDir
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
26
from bzrlib.errors import BzrCommandError
27
from bzrlib.tests.blackbox import ExternalBase
28
from bzrlib.workingtree import WorkingTree
29
30
31
class TestCommit(ExternalBase):
32
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
33
    def test_05_empty_commit(self):
34
        """Commit of tree with no versioned files should fail"""
35
        # 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.
36
        self.run_bzr("init")
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
37
        self.build_tree(['hello.txt'])
1711.2.60 by John Arbash Meinel
Fix an empty commit to raise the right exception.
38
        result = self.run_bzr("commit", "-m", "empty", retcode=3)
39
        self.assertEqual(('', 'bzr: ERROR: no changes to commit.'
40
                              ' use --unchanged to commit anyhow\n'),
41
                         result)
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
42
1704.2.11 by Martin Pool
Handle 'bzr commit DIR' when dir contains pending merges.
43
    def test_commit_with_path(self):
44
        """Commit tree with path of root specified"""
45
        self.run_bzr('init', 'a')
46
        self.build_tree(['a/a_file'])
47
        self.run_bzr('add', 'a/a_file')
48
        self.run_bzr('commit', '-m', 'first commit', 'a')
49
50
        self.run_bzr('branch', 'a', 'b')
51
        self.build_tree_contents([('b/a_file', 'changes in b')])
52
        self.run_bzr('commit', '-m', 'first commit in b', 'b')
53
54
        self.build_tree_contents([('a/a_file', 'new contents')])
55
        self.run_bzr('commit', '-m', 'change in a', 'a')
56
57
        os.chdir('b')
58
        self.run_bzr('merge', '../a', retcode=1) # will conflict
59
        os.chdir('..')
60
        self.run_bzr('resolved', 'b/a_file')
61
        self.run_bzr('commit', '-m', 'merge into b', 'b')
62
63
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
64
    def test_10_verbose_commit(self):
65
        """Add one file and examine verbose commit output"""
66
        self.runbzr("init")
67
        self.build_tree(['hello.txt'])
68
        self.runbzr("add hello.txt")
69
        out,err = self.run_bzr("commit", "-m", "added")
70
        self.assertEqual('', out)
71
        self.assertEqual('added hello.txt\n'
72
                         'Committed revision 1.\n',
73
                         err)
74
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
75
    def prepare_simple_history(self):
76
        """Prepare and return a working tree with one commit of one file"""
77
        # Commit with modified file should say so
78
        wt = BzrDir.create_standalone_workingtree('.')
79
        self.build_tree(['hello.txt', 'extra.txt'])
80
        wt.add(['hello.txt'])
81
        wt.commit(message='added')
82
        return wt
83
84
    def test_verbose_commit_modified(self):
85
        # Verbose commit of modified file should say so
86
        wt = self.prepare_simple_history()
87
        self.build_tree_contents([('hello.txt', 'new contents')])
88
        out, err = self.run_bzr("commit", "-m", "modified")
89
        self.assertEqual('', out)
90
        self.assertEqual('modified hello.txt\n'
91
                         'Committed revision 2.\n',
92
                         err)
93
94
    def test_verbose_commit_renamed(self):
95
        # Verbose commit of renamed file should say so
96
        wt = self.prepare_simple_history()
97
        wt.rename_one('hello.txt', 'gutentag.txt')
98
        out, err = self.run_bzr("commit", "-m", "renamed")
99
        self.assertEqual('', out)
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
100
        self.assertEqual('renamed hello.txt => gutentag.txt\n'
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
101
                         'Committed revision 2.\n',
102
                         err)
103
104
    def test_verbose_commit_moved(self):
105
        # Verbose commit of file moved to new directory should say so
106
        wt = self.prepare_simple_history()
107
        os.mkdir('subdir')
108
        wt.add(['subdir'])
109
        wt.rename_one('hello.txt', 'subdir/hello.txt')
110
        out, err = self.run_bzr("commit", "-m", "renamed")
111
        self.assertEqual('', out)
112
        self.assertEqualDiff('added subdir\n'
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
113
                             'renamed hello.txt => subdir/hello.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_with_unknown(self):
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
118
        """Unknown files should not be listed by default in verbose output"""
119
        # Is that really the best policy?
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
120
        wt = BzrDir.create_standalone_workingtree('.')
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
121
        self.build_tree(['hello.txt', 'extra.txt'])
1669.2.1 by Martin Pool
verbose commit now specifically identifies modified/renamed/reparented files
122
        wt.add(['hello.txt'])
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
123
        out,err = self.run_bzr("commit", "-m", "added")
124
        self.assertEqual('', out)
125
        self.assertEqual('added hello.txt\n'
126
                         'Committed revision 1.\n',
127
                         err)
128
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
129
    def test_verbose_commit_with_unchanged(self):
1616.1.4 by Martin Pool
Verbose commit shouldn't talk about every unchanged file.
130
        """Unchanged files should not be listed by default in verbose output"""
131
        self.runbzr("init")
132
        self.build_tree(['hello.txt', 'unchanged.txt'])
133
        self.runbzr('add unchanged.txt')
134
        self.runbzr('commit -m unchanged unchanged.txt')
135
        self.runbzr("add hello.txt")
136
        out,err = self.run_bzr("commit", "-m", "added")
137
        self.assertEqual('', out)
138
        self.assertEqual('added hello.txt\n'
139
                         'Committed revision 2.\n',
140
                         err)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
141
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
142
    def test_commit_merge_reports_all_modified_files(self):
143
        # the commit command should show all the files that are shown by
144
        # bzr diff or bzr status when committing, even when they were not
145
        # changed by the user but rather through doing a merge.
146
        this_tree = self.make_branch_and_tree('this')
147
        # we need a bunch of files and dirs, to perform one action on each.
148
        self.build_tree([
149
            'this/dirtorename/',
150
            'this/dirtoreparent/',
151
            'this/dirtoleave/',
152
            'this/dirtoremove/',
153
            'this/filetoreparent',
154
            'this/filetorename',
155
            'this/filetomodify',
156
            'this/filetoremove',
157
            'this/filetoleave']
158
            )
159
        this_tree.add([
160
            'dirtorename',
161
            'dirtoreparent',
162
            'dirtoleave',
163
            'dirtoremove',
164
            'filetoreparent',
165
            'filetorename',
166
            'filetomodify',
167
            'filetoremove',
168
            'filetoleave']
169
            )
170
        this_tree.commit('create_files')
171
        other_dir = this_tree.bzrdir.sprout('other')
172
        other_tree = other_dir.open_workingtree()
173
        other_tree.lock_write()
174
        # perform the needed actions on the files and dirs.
175
        try:
176
            other_tree.rename_one('dirtorename', 'renameddir')
177
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
178
            other_tree.rename_one('filetorename', 'renamedfile')
179
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
180
            other_tree.remove(['dirtoremove', 'filetoremove'])
181
            self.build_tree_contents([
182
                ('other/newdir/', ),
183
                ('other/filetomodify', 'new content'),
184
                ('other/newfile', 'new file content')])
185
            other_tree.add('newfile')
186
            other_tree.add('newdir/')
187
            other_tree.commit('modify all sample files and dirs.')
188
        finally:
189
            other_tree.unlock()
190
        self.merge(other_tree.branch, this_tree)
191
        os.chdir('this')
192
        out,err = self.run_bzr("commit", "-m", "added")
193
        os.chdir('..')
194
        self.assertEqual('', out)
195
        self.assertEqualDiff(
196
            'modified filetomodify\n'
197
            'added newdir\n'
198
            'added newfile\n'
199
            'renamed dirtorename => renameddir\n'
200
            'renamed dirtoreparent => renameddir/reparenteddir\n'
201
            'renamed filetoreparent => renameddir/reparentedfile\n'
202
            'renamed filetorename => renamedfile\n'
203
            'deleted dirtoremove\n'
204
            'deleted filetoremove\n'
205
            'Committed revision 2.\n',
206
            err)
207
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
208
    def test_empty_commit_message(self):
209
        self.runbzr("init")
210
        file('foo.c', 'wt').write('int main() {}')
211
        self.runbzr(['add', 'foo.c'])
1616.1.3 by Martin Pool
Clean up cut&pasted test for verbose commit
212
        self.runbzr(["commit", "-m", ""] , retcode=3)
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
213
214
    def test_other_branch_commit(self):
215
        # this branch is to ensure consistent behaviour, whether we're run
216
        # inside a branch, or not.
217
        os.mkdir('empty_branch')
218
        os.chdir('empty_branch')
219
        self.runbzr('init')
220
        os.mkdir('branch')
221
        os.chdir('branch')
222
        self.runbzr('init')
223
        file('foo.c', 'wt').write('int main() {}')
224
        file('bar.c', 'wt').write('int main() {}')
225
        os.chdir('..')
226
        self.runbzr(['add', 'branch/foo.c'])
227
        self.runbzr(['add', 'branch'])
228
        # can't commit files in different trees; sane error
229
        self.runbzr('commit -m newstuff branch/foo.c .', retcode=3)
230
        self.runbzr('commit -m newstuff branch/foo.c')
231
        self.runbzr('commit -m newstuff branch')
232
        self.runbzr('commit -m newstuff branch', retcode=3)
233
234
    def test_out_of_date_tree_commit(self):
235
        # check we get an error code and a clear message committing with an out
236
        # of date checkout
237
        self.make_branch_and_tree('branch')
238
        # make a checkout
1587.1.14 by Robert Collins
Make bound branch creation happen via 'checkout'
239
        self.runbzr('checkout --lightweight branch checkout')
1508.1.22 by Robert Collins
implement out of date working tree checks in commit.
240
        # commit to the original branch to make the checkout out of date
241
        self.runbzr('commit --unchanged -m message branch')
242
        # now commit to the checkout should emit
243
        # ERROR: Out of date with the branch, 'bzr update' is suggested
244
        output = self.runbzr('commit --unchanged -m checkout_message '
245
                             'checkout', retcode=3)
246
        self.assertEqual(output,
247
                         ('',
248
                          "bzr: ERROR: Working tree is out of date, please run "
249
                          "'bzr update'.\n"))
1587.1.8 by Robert Collins
Local commits on unbound branches fail.
250
251
    def test_local_commit_unbound(self):
252
        # a --local commit on an unbound branch is an error
253
        self.make_branch_and_tree('.')
254
        out, err = self.run_bzr('commit', '--local', retcode=3)
255
        self.assertEqualDiff('', out)
256
        self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
257
                             'on unbound branches.\n', err)
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
258
259
    def test_commit_a_text_merge_in_a_checkout(self):
260
        # checkouts perform multiple actions in a transaction across bond
261
        # branches and their master, and have been observed to fail in the
262
        # past. This is a user story reported to fail in bug #43959 where 
263
        # a merge done in a checkout (using the update command) failed to
264
        # commit correctly.
265
        self.run_bzr('init', 'trunk')
266
267
        self.run_bzr('checkout', 'trunk', 'u1')
268
        self.build_tree_contents([('u1/hosts', 'initial contents')])
269
        self.run_bzr('add', 'u1/hosts')
270
        self.run_bzr('commit', '-m', 'add hosts', 'u1')
271
272
        self.run_bzr('checkout', 'trunk', 'u2')
273
        self.build_tree_contents([('u2/hosts', 'altered in u2')])
274
        self.run_bzr('commit', '-m', 'checkin from u2', 'u2')
275
276
        # make an offline commits
277
        self.build_tree_contents([('u1/hosts', 'first offline change in u1')])
278
        self.run_bzr('commit', '-m', 'checkin offline', '--local', 'u1')
279
280
        # now try to pull in online work from u2, and then commit our offline
281
        # work as a merge
282
        # retcode 1 as we expect a text conflict
283
        self.run_bzr('update', 'u1', retcode=1)
284
        self.run_bzr('resolved', 'u1/hosts')
285
        # add a text change here to represent resolving the merge conflicts in
286
        # favour of a new version of the file not identical to either the u1
287
        # version or the u2 version.
288
        self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
1704.2.12 by Martin Pool
use the correct transaction when committing snapshot (Malone: #43959)
289
        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
290
291
    def test_commit_respects_spec_for_removals(self):
292
        """Commit with a file spec should only commit removals that match"""
293
        t = self.make_branch_and_tree('.')
294
        self.build_tree(['file-a', 'dir-a/', 'dir-a/file-b'])
295
        t.add(['file-a', 'dir-a', 'dir-a/file-b'])
296
        t.commit('Create')
297
        t.remove(['file-a', 'dir-a/file-b'])
298
        os.chdir('dir-a')
299
        result = self.run_bzr('commit', '.', '-m' 'removed file-b')[1]
300
        self.assertNotContainsRe(result, 'file-a')
301
        result = self.run_bzr('status')[0]
302
        self.assertContainsRe(result, 'removed:\n  file-a')