~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Test the uncommit command."""

import os

from bzrlib import uncommit, workingtree
from bzrlib.bzrdir import BzrDirMetaFormat1
from bzrlib.errors import BzrError, BoundBranchOutOfDate
from bzrlib.tests import TestCaseWithTransport


class TestUncommit(TestCaseWithTransport):

    def create_simple_tree(self):
        wt = self.make_branch_and_tree('tree')
        self.build_tree(['tree/a', 'tree/b', 'tree/c'])
        wt.add(['a', 'b', 'c'])
        wt.commit('initial commit', rev_id='a1')

        self.build_tree_contents([('tree/a', 'new contents of a\n')])
        wt.commit('second commit', rev_id='a2')

        return wt

    def test_uncommit(self):
        """Test uncommit functionality."""
        wt = self.create_simple_tree()

        os.chdir('tree')
        out, err = self.run_bzr('uncommit --dry-run --force')
        self.assertContainsRe(out, 'Dry-run')
        self.assertNotContainsRe(out, 'initial commit')
        self.assertContainsRe(out, 'second commit')

        # Nothing has changed
        self.assertEqual(['a2'], wt.get_parent_ids())

        # Uncommit, don't prompt
        out, err = self.run_bzr('uncommit --force')
        self.assertNotContainsRe(out, 'initial commit')
        self.assertContainsRe(out, 'second commit')

        # This should look like we are back in revno 1
        self.assertEqual(['a1'], wt.get_parent_ids())
        out, err = self.run_bzr('status')
        self.assertEquals(out, 'modified:\n  a\n')

    def test_uncommit_no_history(self):
        wt = self.make_branch_and_tree('tree')
        out, err = self.run_bzr('uncommit --force', retcode=1)
        self.assertEqual('', err)
        self.assertEqual('No revisions to uncommit.\n', out)

    def test_uncommit_checkout(self):
        wt = self.create_simple_tree()
        checkout_tree = wt.branch.create_checkout('checkout')

        self.assertEqual(['a2'], checkout_tree.get_parent_ids())

        os.chdir('checkout')
        out, err = self.run_bzr('uncommit --dry-run --force')
        self.assertContainsRe(out, 'Dry-run')
        self.assertNotContainsRe(out, 'initial commit')
        self.assertContainsRe(out, 'second commit')

        self.assertEqual(['a2'], checkout_tree.get_parent_ids())

        out, err = self.run_bzr('uncommit --force')
        self.assertNotContainsRe(out, 'initial commit')
        self.assertContainsRe(out, 'second commit')

        # uncommit in a checkout should uncommit the parent branch
        # (but doesn't effect the other working tree)
        self.assertEquals(['a1'], checkout_tree.get_parent_ids())
        self.assertEquals('a1', wt.branch.last_revision())
        self.assertEquals(['a2'], wt.get_parent_ids())

    def test_uncommit_bound(self):
        os.mkdir('a')
        a = BzrDirMetaFormat1().initialize('a')
        a.create_repository()
        a.create_branch()
        t_a = a.create_workingtree()
        t_a.commit('commit 1')
        t_a.commit('commit 2')
        t_a.commit('commit 3')
        b = t_a.branch.create_checkout('b').branch
        uncommit.uncommit(b)
        self.assertEqual(len(b.revision_history()), 2)
        self.assertEqual(len(t_a.branch.revision_history()), 2)
        # update A's tree to not have the uncomitted revision referenced.
        t_a.update()
        t_a.commit('commit 3b')
        self.assertRaises(BoundBranchOutOfDate, uncommit.uncommit, b)
        b.pull(t_a.branch)
        uncommit.uncommit(b)

    def test_uncommit_bound_local(self):
        t_a = self.make_branch_and_tree('a')
        rev_id1 = t_a.commit('commit 1')
        rev_id2 = t_a.commit('commit 2')
        rev_id3 = t_a.commit('commit 3')
        b = t_a.branch.create_checkout('b').branch

        out, err = self.run_bzr(['uncommit', '--local', 'b', '--force'])
        self.assertEqual(rev_id3, t_a.last_revision())
        self.assertEqual((3, rev_id3), t_a.branch.last_revision_info())
        self.assertEqual((2, rev_id2), b.last_revision_info())

    def test_uncommit_revision(self):
        wt = self.create_simple_tree()

        os.chdir('tree')
        out, err = self.run_bzr('uncommit -r1 --force')

        self.assertNotContainsRe(out, 'initial commit')
        self.assertContainsRe(out, 'second commit')
        self.assertEqual(['a1'], wt.get_parent_ids())
        self.assertEqual('a1', wt.branch.last_revision())

    def test_uncommit_neg_1(self):
        wt = self.create_simple_tree()
        os.chdir('tree')
        out, err = self.run_bzr('uncommit -r -1', retcode=1)
        self.assertEqual('No revisions to uncommit.\n', out)

    def test_uncommit_merges(self):
        wt = self.create_simple_tree()

        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()

        tree2.commit('unchanged', rev_id='b3')
        tree2.commit('unchanged', rev_id='b4')

        wt.merge_from_branch(tree2.branch)
        wt.commit('merge b4', rev_id='a3')

        self.assertEqual(['a3'], wt.get_parent_ids())

        os.chdir('tree')
        out, err = self.run_bzr('uncommit --force')

        self.assertEqual(['a2', 'b4'], wt.get_parent_ids())

    def test_uncommit_pending_merge(self):
        wt = self.create_simple_tree()
        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
        tree2.commit('unchanged', rev_id='b3')

        wt.branch.fetch(tree2.branch)
        wt.set_pending_merges(['b3'])

        os.chdir('tree')
        out, err = self.run_bzr('uncommit --force')
        self.assertEqual(['a1', 'b3'], wt.get_parent_ids())

    def test_uncommit_multiple_merge(self):
        wt = self.create_simple_tree()

        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
        tree2.commit('unchanged', rev_id='b3')

        tree3 = wt.bzrdir.sprout('tree3').open_workingtree()
        tree3.commit('unchanged', rev_id='c3')

        wt.merge_from_branch(tree2.branch)
        wt.commit('merge b3', rev_id='a3')

        wt.merge_from_branch(tree3.branch)
        wt.commit('merge c3', rev_id='a4')

        self.assertEqual(['a4'], wt.get_parent_ids())

        os.chdir('tree')
        out, err = self.run_bzr('uncommit --force -r 2')

        self.assertEqual(['a2', 'b3', 'c3'], wt.get_parent_ids())

    def test_uncommit_merge_plus_pending(self):
        wt = self.create_simple_tree()

        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
        tree2.commit('unchanged', rev_id='b3')
        tree3 = wt.bzrdir.sprout('tree3').open_workingtree()
        tree3.commit('unchanged', rev_id='c3')

        wt.branch.fetch(tree2.branch)
        wt.set_pending_merges(['b3'])
        wt.commit('merge b3', rev_id='a3')


        wt.merge_from_branch(tree3.branch)

        self.assertEqual(['a3', 'c3'], wt.get_parent_ids())

        os.chdir('tree')
        out, err = self.run_bzr('uncommit --force -r 2')

        self.assertEqual(['a2', 'b3', 'c3'], wt.get_parent_ids())

    def test_uncommit_shows_log_with_revision_id(self):
        wt = self.create_simple_tree()

        out, err = self.run_bzr('uncommit --force', working_dir='tree')
        self.assertContainsRe(out, r'second commit')
        self.assertContainsRe(err, r'You can restore the old tip by running')
        self.assertContainsRe(err, r'bzr pull . -r revid:a2')

    def test_uncommit_octopus_merge(self):
        # Check that uncommit keeps the pending merges in the same order
        # though it will also filter out ones in the ancestry
        wt = self.create_simple_tree()

        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
        tree3 = wt.bzrdir.sprout('tree3').open_workingtree()

        tree2.commit('unchanged', rev_id='b3')
        tree3.commit('unchanged', rev_id='c3')

        wt.merge_from_branch(tree2.branch)
        wt.merge_from_branch(tree3.branch)
        wt.commit('merge b3, c3', rev_id='a3')

        tree2.commit('unchanged', rev_id='b4')
        tree3.commit('unchanged', rev_id='c4')

        wt.merge_from_branch(tree3.branch)
        wt.merge_from_branch(tree2.branch)
        wt.commit('merge b4, c4', rev_id='a4')

        self.assertEqual(['a4'], wt.get_parent_ids())

        os.chdir('tree')
        out, err = self.run_bzr('uncommit --force -r 2')

        self.assertEqual(['a2', 'c4', 'b4'], wt.get_parent_ids())

    def test_uncommit_nonascii(self):
        tree = self.make_branch_and_tree('tree')
        tree.commit(u'\u1234 message')
        out, err = self.run_bzr('uncommit --force tree', encoding='ascii')
        self.assertContainsRe(out, r'\? message')