~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
# Copyright (C) 2008, 2009, 2011, 2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Test uncommit."""


from bzrlib import (
    errors,
    tests,
    uncommit,
    )


class TestUncommit(tests.TestCaseWithTransport):

    def make_linear_tree(self):
        tree = self.make_branch_and_tree('tree')
        tree.lock_write()
        try:
            self.build_tree(['tree/one'])
            tree.add('one')
            rev_id1 = tree.commit('one')
            self.build_tree(['tree/two'])
            tree.add('two')
            rev_id2 = tree.commit('two')
        finally:
            tree.unlock()
        return tree, [rev_id1, rev_id2]

    def test_uncommit(self):
        tree, history = self.make_linear_tree()
        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((2, history[1]), tree.branch.last_revision_info())
        uncommit.uncommit(tree.branch, tree=tree)
        self.assertEqual(history[0], tree.last_revision())
        self.assertEqual((1, history[0]), tree.branch.last_revision_info())

        # The file should not be removed
        self.assertPathExists('tree/two')
        # And it should still be listed as added
        self.assertIsNot(None, tree.path2id('two'))

    def test_uncommit_bound(self):
        tree, history = self.make_linear_tree()
        child = tree.bzrdir.sprout('child').open_workingtree()
        child.branch.bind(tree.branch)

        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((2, history[1]), tree.branch.last_revision_info())
        self.assertEqual(history[1], child.last_revision())
        self.assertEqual((2, history[1]), child.branch.last_revision_info())

        # Uncommit in a bound branch should uncommit the master branch, but not
        # touch the other working tree.
        uncommit.uncommit(child.branch, tree=child)

        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((1, history[0]), tree.branch.last_revision_info())
        self.assertEqual(history[0], child.last_revision())
        self.assertEqual((1, history[0]), child.branch.last_revision_info())

    def test_uncommit_bound_local(self):
        tree, history = self.make_linear_tree()
        child = tree.bzrdir.sprout('child').open_workingtree()
        child.branch.bind(tree.branch)

        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((2, history[1]), tree.branch.last_revision_info())
        self.assertEqual(history[1], child.last_revision())
        self.assertEqual((2, history[1]), child.branch.last_revision_info())

        # Uncommit local=True should only affect the local branch
        uncommit.uncommit(child.branch, tree=child, local=True)

        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((2, history[1]), tree.branch.last_revision_info())
        self.assertEqual(history[0], child.last_revision())
        self.assertEqual((1, history[0]), child.branch.last_revision_info())

    def test_uncommit_unbound_local(self):
        tree, history = self.make_linear_tree()

        # If this tree isn't bound, local=True raises an exception
        self.assertRaises(errors.LocalRequiresBoundBranch,
            uncommit.uncommit, tree.branch, tree=tree, local=True)

    def test_uncommit_remove_tags(self):
        tree, history = self.make_linear_tree()
        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((2, history[1]), tree.branch.last_revision_info())
        tree.branch.tags.set_tag(u"pointsatexisting", history[0])
        tree.branch.tags.set_tag(u"pointsatremoved", history[1])
        uncommit.uncommit(tree.branch, tree=tree)
        self.assertEqual(history[0], tree.last_revision())
        self.assertEqual((1, history[0]), tree.branch.last_revision_info())
        self.assertEqual({
            "pointsatexisting": history[0]
            }, tree.branch.tags.get_tag_dict())

    def test_uncommit_remove_tags_keeps_pending_merges(self):
        tree, history = self.make_linear_tree()
        copy = tree.bzrdir.sprout('copyoftree').open_workingtree()
        copy.commit(message='merged', rev_id='merged')
        tree.merge_from_branch(copy.branch)
        tree.branch.tags.set_tag('pointsatmerged', 'merged')
        history.append(tree.commit('merge'))
        self.assertEqual('merged', tree.branch.tags.lookup_tag('pointsatmerged'))
        self.assertEqual(history[2], tree.last_revision())
        self.assertEqual((3, history[2]), tree.branch.last_revision_info())
        tree.branch.tags.set_tag(u"pointsatexisting", history[1])
        tree.branch.tags.set_tag(u"pointsatremoved", history[2])
        uncommit.uncommit(tree.branch, tree=tree)
        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((2, history[1]), tree.branch.last_revision_info())
        self.assertEqual([history[1], 'merged'], tree.get_parent_ids())
        self.assertEqual({
            "pointsatexisting": history[1],
            "pointsatmerged": 'merged',
            }, tree.branch.tags.get_tag_dict())

    def test_uncommit_keep_tags(self):
        tree, history = self.make_linear_tree()
        self.assertEqual(history[1], tree.last_revision())
        self.assertEqual((2, history[1]), tree.branch.last_revision_info())
        tree.branch.tags.set_tag(u"pointsatexisting", history[0])
        tree.branch.tags.set_tag(u"pointsatremoved", history[1])
        uncommit.uncommit(tree.branch, tree=tree, keep_tags=True)
        self.assertEqual(history[0], tree.last_revision())
        self.assertEqual((1, history[0]), tree.branch.last_revision_info())
        self.assertEqual({
            "pointsatexisting": history[0],
            "pointsatremoved": history[1],
            }, tree.branch.tags.get_tag_dict())