~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Patch Queue Manager
  • Date: 2011-09-22 14:12:18 UTC
  • mfrom: (6155.3.1 jam)
  • Revision ID: pqm@pqm.ubuntu.com-20110922141218-86s4uu6nqvourw4f
(jameinel) Cleanup comments bzrlib/smart/__init__.py (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""\
2
 
Test the uncommit command.
3
 
"""
 
1
# Copyright (C) 2005-2010 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
"""Test the uncommit command."""
4
18
 
5
19
import os
6
20
 
 
21
from bzrlib import uncommit
7
22
from bzrlib.bzrdir import BzrDirMetaFormat1
8
 
from bzrlib.errors import BzrError, BoundBranchOutOfDate
9
 
from bzrlib.uncommit import uncommit
10
 
from bzrlib.tests import TestCaseInTempDir
11
 
 
12
 
class TestUncommit(TestCaseInTempDir):
 
23
from bzrlib.errors import BoundBranchOutOfDate
 
24
from bzrlib.tests import TestCaseWithTransport
 
25
from bzrlib.tests.script import (
 
26
    run_script,
 
27
    ScriptRunner,
 
28
    )
 
29
 
 
30
 
 
31
class TestUncommit(TestCaseWithTransport):
 
32
 
 
33
    def create_simple_tree(self):
 
34
        wt = self.make_branch_and_tree('tree')
 
35
        self.build_tree(['tree/a', 'tree/b', 'tree/c'])
 
36
        wt.add(['a', 'b', 'c'])
 
37
        wt.commit('initial commit', rev_id='a1')
 
38
 
 
39
        self.build_tree_contents([('tree/a', 'new contents of a\n')])
 
40
        wt.commit('second commit', rev_id='a2')
 
41
 
 
42
        return wt
 
43
 
13
44
    def test_uncommit(self):
14
45
        """Test uncommit functionality."""
15
 
        bzr = self.capture 
16
 
        os.mkdir('branch')
17
 
        os.chdir('branch')
18
 
        bzr('init')
19
 
        self.build_tree(['a', 'b', 'c'])
20
 
 
21
 
        bzr('add')
22
 
        bzr('commit -m initial')
23
 
 
24
 
        self.assertEquals(bzr('revno'), '1\n')
25
 
 
26
 
        open('a', 'wb').write('new contents of a\n')
27
 
        self.assertEquals(bzr('status'), 'modified:\n  a\n')
28
 
        bzr('commit -m second')
29
 
 
30
 
        self.assertEquals(bzr('status'), '')
31
 
        self.assertEquals(bzr('revno'), '2\n')
32
 
 
33
 
        txt = bzr('uncommit --dry-run --force')
34
 
        self.failIfEqual(txt.find('Dry-run'), -1)
35
 
 
36
 
        self.assertEquals(bzr('status'), '')
37
 
        self.assertEquals(bzr('revno'), '2\n')
38
 
 
39
 
        txt = bzr('uncommit --force')
40
 
 
41
 
        self.assertEquals(bzr('revno'), '1\n')
42
 
        self.assertEquals(bzr('status'), 'modified:\n  a\n')
43
 
        
44
 
        bzr('checkout . ../checkout')
45
 
        os.chdir('../checkout')
46
 
        self.assertEquals("", bzr('status'))
47
 
        self.assertEquals(bzr('revno'), '1\n')
48
 
 
49
 
        open('a', 'wb').write('new contents of a\n')
50
 
        self.assertEquals(bzr('status'), 'modified:\n  a\n')
51
 
        bzr('commit -m second')
52
 
 
53
 
        self.assertEquals(bzr('status'), '')
54
 
        self.assertEquals(bzr('revno'), '2\n')
55
 
 
56
 
        txt = bzr('uncommit --dry-run --force')
57
 
        self.failIfEqual(txt.find('Dry-run'), -1)
58
 
 
59
 
        self.assertEquals(bzr('status'), '')
60
 
        self.assertEquals(bzr('revno'), '2\n')
61
 
 
62
 
        txt = bzr('uncommit --force')
63
 
 
64
 
        self.assertEquals(bzr('revno'), '1\n')
65
 
        self.assertEquals(bzr('status'), 'modified:\n  a\n')
 
46
        wt = self.create_simple_tree()
 
47
 
 
48
        os.chdir('tree')
 
49
        out, err = self.run_bzr('uncommit --dry-run --force')
 
50
        self.assertContainsRe(out, 'Dry-run')
 
51
        self.assertNotContainsRe(out, 'initial commit')
 
52
        self.assertContainsRe(out, 'second commit')
 
53
 
 
54
        # Nothing has changed
 
55
        self.assertEqual(['a2'], wt.get_parent_ids())
 
56
 
 
57
        # Uncommit, don't prompt
 
58
        out, err = self.run_bzr('uncommit --force')
 
59
        self.assertNotContainsRe(out, 'initial commit')
 
60
        self.assertContainsRe(out, 'second commit')
 
61
 
 
62
        # This should look like we are back in revno 1
 
63
        self.assertEqual(['a1'], wt.get_parent_ids())
 
64
        out, err = self.run_bzr('status')
 
65
        self.assertEquals(out, 'modified:\n  a\n')
 
66
 
 
67
    def test_uncommit_interactive(self):
 
68
        """Uncommit seeks confirmation, and doesn't proceed without it."""
 
69
        wt = self.create_simple_tree()
 
70
        os.chdir('tree')
 
71
        run_script(self, """    
 
72
        $ bzr uncommit
 
73
        ...
 
74
        The above revision(s) will be removed.
 
75
        2>Uncommit these revisions? [y/n]: 
 
76
        <n
 
77
        Canceled
 
78
        """)
 
79
        self.assertEqual(['a2'], wt.get_parent_ids())
 
80
 
 
81
    def test_uncommit_no_history(self):
 
82
        wt = self.make_branch_and_tree('tree')
 
83
        out, err = self.run_bzr('uncommit --force', retcode=1)
 
84
        self.assertEqual('', err)
 
85
        self.assertEqual('No revisions to uncommit.\n', out)
 
86
 
 
87
    def test_uncommit_checkout(self):
 
88
        wt = self.create_simple_tree()
 
89
        checkout_tree = wt.branch.create_checkout('checkout')
 
90
 
 
91
        self.assertEqual(['a2'], checkout_tree.get_parent_ids())
 
92
 
 
93
        os.chdir('checkout')
 
94
        out, err = self.run_bzr('uncommit --dry-run --force')
 
95
        self.assertContainsRe(out, 'Dry-run')
 
96
        self.assertNotContainsRe(out, 'initial commit')
 
97
        self.assertContainsRe(out, 'second commit')
 
98
 
 
99
        self.assertEqual(['a2'], checkout_tree.get_parent_ids())
 
100
 
 
101
        out, err = self.run_bzr('uncommit --force')
 
102
        self.assertNotContainsRe(out, 'initial commit')
 
103
        self.assertContainsRe(out, 'second commit')
 
104
 
 
105
        # uncommit in a checkout should uncommit the parent branch
 
106
        # (but doesn't effect the other working tree)
 
107
        self.assertEquals(['a1'], checkout_tree.get_parent_ids())
 
108
        self.assertEquals('a1', wt.branch.last_revision())
 
109
        self.assertEquals(['a2'], wt.get_parent_ids())
66
110
 
67
111
    def test_uncommit_bound(self):
68
112
        os.mkdir('a')
69
113
        a = BzrDirMetaFormat1().initialize('a')
70
114
        a.create_repository()
71
115
        a.create_branch()
72
 
        t = a.create_workingtree()
73
 
        t.commit('commit 1')
74
 
        t.commit('commit 2')
75
 
        t.commit('commit 3')
76
 
        b = t.bzrdir.sprout('b').open_branch()
77
 
        b.bind(t.branch)
78
 
        uncommit(b)
79
 
        t.set_last_revision(t.branch.last_revision())
 
116
        t_a = a.create_workingtree()
 
117
        t_a.commit('commit 1')
 
118
        t_a.commit('commit 2')
 
119
        t_a.commit('commit 3')
 
120
        b = t_a.branch.create_checkout('b').branch
 
121
        uncommit.uncommit(b)
80
122
        self.assertEqual(len(b.revision_history()), 2)
81
 
        self.assertEqual(len(t.branch.revision_history()), 2)
82
 
        t.commit('commit 3b')
83
 
        self.assertRaises(BoundBranchOutOfDate, uncommit, b)
84
 
        b.pull(t.branch)
85
 
        uncommit(b)
86
 
 
 
123
        self.assertEqual(len(t_a.branch.revision_history()), 2)
 
124
        # update A's tree to not have the uncommitted revision referenced.
 
125
        t_a.update()
 
126
        t_a.commit('commit 3b')
 
127
        self.assertRaises(BoundBranchOutOfDate, uncommit.uncommit, b)
 
128
        b.pull(t_a.branch)
 
129
        uncommit.uncommit(b)
 
130
 
 
131
    def test_uncommit_bound_local(self):
 
132
        t_a = self.make_branch_and_tree('a')
 
133
        rev_id1 = t_a.commit('commit 1')
 
134
        rev_id2 = t_a.commit('commit 2')
 
135
        rev_id3 = t_a.commit('commit 3')
 
136
        b = t_a.branch.create_checkout('b').branch
 
137
 
 
138
        out, err = self.run_bzr(['uncommit', '--local', 'b', '--force'])
 
139
        self.assertEqual(rev_id3, t_a.last_revision())
 
140
        self.assertEqual((3, rev_id3), t_a.branch.last_revision_info())
 
141
        self.assertEqual((2, rev_id2), b.last_revision_info())
 
142
 
 
143
    def test_uncommit_revision(self):
 
144
        wt = self.create_simple_tree()
 
145
 
 
146
        os.chdir('tree')
 
147
        out, err = self.run_bzr('uncommit -r1 --force')
 
148
 
 
149
        self.assertNotContainsRe(out, 'initial commit')
 
150
        self.assertContainsRe(out, 'second commit')
 
151
        self.assertEqual(['a1'], wt.get_parent_ids())
 
152
        self.assertEqual('a1', wt.branch.last_revision())
 
153
 
 
154
    def test_uncommit_neg_1(self):
 
155
        wt = self.create_simple_tree()
 
156
        os.chdir('tree')
 
157
        out, err = self.run_bzr('uncommit -r -1', retcode=1)
 
158
        self.assertEqual('No revisions to uncommit.\n', out)
 
159
 
 
160
    def test_uncommit_merges(self):
 
161
        wt = self.create_simple_tree()
 
162
 
 
163
        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
 
164
 
 
165
        tree2.commit('unchanged', rev_id='b3')
 
166
        tree2.commit('unchanged', rev_id='b4')
 
167
 
 
168
        wt.merge_from_branch(tree2.branch)
 
169
        wt.commit('merge b4', rev_id='a3')
 
170
 
 
171
        self.assertEqual(['a3'], wt.get_parent_ids())
 
172
 
 
173
        os.chdir('tree')
 
174
        out, err = self.run_bzr('uncommit --force')
 
175
 
 
176
        self.assertEqual(['a2', 'b4'], wt.get_parent_ids())
 
177
 
 
178
    def test_uncommit_pending_merge(self):
 
179
        wt = self.create_simple_tree()
 
180
        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
 
181
        tree2.commit('unchanged', rev_id='b3')
 
182
 
 
183
        wt.branch.fetch(tree2.branch)
 
184
        wt.set_pending_merges(['b3'])
 
185
 
 
186
        os.chdir('tree')
 
187
        out, err = self.run_bzr('uncommit --force')
 
188
        self.assertEqual(['a1', 'b3'], wt.get_parent_ids())
 
189
 
 
190
    def test_uncommit_multiple_merge(self):
 
191
        wt = self.create_simple_tree()
 
192
 
 
193
        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
 
194
        tree2.commit('unchanged', rev_id='b3')
 
195
 
 
196
        tree3 = wt.bzrdir.sprout('tree3').open_workingtree()
 
197
        tree3.commit('unchanged', rev_id='c3')
 
198
 
 
199
        wt.merge_from_branch(tree2.branch)
 
200
        wt.commit('merge b3', rev_id='a3')
 
201
 
 
202
        wt.merge_from_branch(tree3.branch)
 
203
        wt.commit('merge c3', rev_id='a4')
 
204
 
 
205
        self.assertEqual(['a4'], wt.get_parent_ids())
 
206
 
 
207
        os.chdir('tree')
 
208
        out, err = self.run_bzr('uncommit --force -r 2')
 
209
 
 
210
        self.assertEqual(['a2', 'b3', 'c3'], wt.get_parent_ids())
 
211
 
 
212
    def test_uncommit_merge_plus_pending(self):
 
213
        wt = self.create_simple_tree()
 
214
 
 
215
        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
 
216
        tree2.commit('unchanged', rev_id='b3')
 
217
        tree3 = wt.bzrdir.sprout('tree3').open_workingtree()
 
218
        tree3.commit('unchanged', rev_id='c3')
 
219
 
 
220
        wt.branch.fetch(tree2.branch)
 
221
        wt.set_pending_merges(['b3'])
 
222
        wt.commit('merge b3', rev_id='a3')
 
223
 
 
224
 
 
225
        wt.merge_from_branch(tree3.branch)
 
226
 
 
227
        self.assertEqual(['a3', 'c3'], wt.get_parent_ids())
 
228
 
 
229
        os.chdir('tree')
 
230
        out, err = self.run_bzr('uncommit --force -r 2')
 
231
 
 
232
        self.assertEqual(['a2', 'b3', 'c3'], wt.get_parent_ids())
 
233
 
 
234
    def test_uncommit_shows_log_with_revision_id(self):
 
235
        wt = self.create_simple_tree()
 
236
 
 
237
        script = ScriptRunner()
 
238
        script.run_script(self, """
 
239
$ cd tree
 
240
$ bzr uncommit --force 
 
241
    2 ...
 
242
      second commit
 
243
...
 
244
The above revision(s) will be removed.
 
245
You can restore the old tip by running:
 
246
  bzr pull . -r revid:a2
 
247
""")
 
248
 
 
249
    def test_uncommit_octopus_merge(self):
 
250
        # Check that uncommit keeps the pending merges in the same order
 
251
        # though it will also filter out ones in the ancestry
 
252
        wt = self.create_simple_tree()
 
253
 
 
254
        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
 
255
        tree3 = wt.bzrdir.sprout('tree3').open_workingtree()
 
256
 
 
257
        tree2.commit('unchanged', rev_id='b3')
 
258
        tree3.commit('unchanged', rev_id='c3')
 
259
 
 
260
        wt.merge_from_branch(tree2.branch)
 
261
        wt.merge_from_branch(tree3.branch, force=True)
 
262
        wt.commit('merge b3, c3', rev_id='a3')
 
263
 
 
264
        tree2.commit('unchanged', rev_id='b4')
 
265
        tree3.commit('unchanged', rev_id='c4')
 
266
 
 
267
        wt.merge_from_branch(tree3.branch)
 
268
        wt.merge_from_branch(tree2.branch, force=True)
 
269
        wt.commit('merge b4, c4', rev_id='a4')
 
270
 
 
271
        self.assertEqual(['a4'], wt.get_parent_ids())
 
272
 
 
273
        os.chdir('tree')
 
274
        out, err = self.run_bzr('uncommit --force -r 2')
 
275
 
 
276
        self.assertEqual(['a2', 'c4', 'b4'], wt.get_parent_ids())
 
277
 
 
278
    def test_uncommit_nonascii(self):
 
279
        tree = self.make_branch_and_tree('tree')
 
280
        tree.commit(u'\u1234 message')
 
281
        out, err = self.run_bzr('uncommit --force tree', encoding='ascii')
 
282
        self.assertContainsRe(out, r'\? message')
 
283
 
 
284
    def test_uncommit_removes_tags(self):
 
285
        tree = self.make_branch_and_tree('tree')
 
286
        revid = tree.commit('message')
 
287
        tree.branch.tags.set_tag("atag", revid)
 
288
        out, err = self.run_bzr('uncommit --force tree')
 
289
        self.assertEquals({}, tree.branch.tags.get_tag_dict())
 
290
 
 
291
    def test_uncommit_keep_tags(self):
 
292
        tree = self.make_branch_and_tree('tree')
 
293
        revid = tree.commit('message')
 
294
        tree.branch.tags.set_tag("atag", revid)
 
295
        out, err = self.run_bzr('uncommit --keep-tags --force tree')
 
296
        self.assertEquals({"atag": revid}, tree.branch.tags.get_tag_dict())