summaryrefslogtreecommitdiff
path: root/bzrlib/tests/per_branch/test_commit.py
blob: de34dca0a1c0a4ffcdd073e488d921c08a36221f (plain)
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
# Copyright (C) 2007-2010 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

"""Tests for the contract of commit on branches."""

from bzrlib import (
    branch,
    delta,
    errors,
    revision,
    transport,
    )
from bzrlib.tests import per_branch


class TestCommit(per_branch.TestCaseWithBranch):

    def test_commit_nicks(self):
        """Nicknames are committed to the revision"""
        self.get_transport().mkdir('bzr.dev')
        wt = self.make_branch_and_tree('bzr.dev')
        branch = wt.branch
        branch.nick = "My happy branch"
        wt.commit('My commit respect da nick.')
        committed = branch.repository.get_revision(branch.last_revision())
        self.assertEqual(committed.properties["branch-nick"],
                         "My happy branch")


class TestCommitHook(per_branch.TestCaseWithBranch):

    def setUp(self):
        self.hook_calls = []
        super(TestCommitHook, self).setUp()

    def capture_post_commit_hook(self, local, master, old_revno,
        old_revid, new_revno, new_revid):
        """Capture post commit hook calls to self.hook_calls.

        The call is logged, as is some state of the two branches.
        """
        if local:
            local_locked = local.is_locked()
            local_base = local.base
        else:
            local_locked = None
            local_base = None
        self.hook_calls.append(
            ('post_commit', local_base, master.base, old_revno, old_revid,
             new_revno, new_revid, local_locked, master.is_locked()))

    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
                                new_revno, new_revid,
                                tree_delta, future_tree):
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
                                new_revno, new_revid, tree_delta))

    def test_post_commit_to_origin(self):
        tree = self.make_branch_and_memory_tree('branch')
        branch.Branch.hooks.install_named_hook(
            'post_commit', self.capture_post_commit_hook, None)
        tree.lock_write()
        tree.add('')
        revid = tree.commit('a revision')
        # should have had one notification, from origin, and
        # have the branch locked at notification time.
        self.assertEqual([
            ('post_commit', None, tree.branch.base, 0, revision.NULL_REVISION,
             1, revid, None, True)
            ],
            self.hook_calls)
        tree.unlock()

    def test_post_commit_bound(self):
        master = self.make_branch('master')
        tree = self.make_branch_and_memory_tree('local')
        try:
            tree.branch.bind(master)
        except errors.UpgradeRequired:
            # cant bind this format, the test is irrelevant.
            return
        branch.Branch.hooks.install_named_hook(
            'post_commit', self.capture_post_commit_hook, None)
        tree.lock_write()
        tree.add('')
        revid = tree.commit('a revision')
        # with a bound branch, local is set.
        self.assertEqual([
            ('post_commit', tree.branch.base, master.base, 0,
             revision.NULL_REVISION, 1, revid, True, True)
            ],
            self.hook_calls)
        tree.unlock()

    def test_post_commit_not_to_origin(self):
        tree = self.make_branch_and_memory_tree('branch')
        tree.lock_write()
        tree.add('')
        revid = tree.commit('first revision')
        branch.Branch.hooks.install_named_hook(
            'post_commit', self.capture_post_commit_hook, None)
        revid2 = tree.commit('second revision')
        # having committed from up the branch, we should get the
        # before and after revnos and revids correctly.
        self.assertEqual([
            ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
             None, True)
            ],
            self.hook_calls)
        tree.unlock()

    def test_pre_commit_passes(self):
        empty_delta = delta.TreeDelta()
        root_delta = delta.TreeDelta()
        tree = self.make_branch_and_memory_tree('branch')
        tree.lock_write()
        tree.add('')
        root_delta.added = [('', tree.path2id(''), 'directory')]
        branch.Branch.hooks.install_named_hook(
            "pre_commit", self.capture_pre_commit_hook, None)
        revid1 = tree.commit('first revision')
        revid2 = tree.commit('second revision')
        self.assertEqual([
            ('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
            ],
            self.hook_calls)
        tree.unlock()

    def test_pre_commit_fails(self):
        empty_delta = delta.TreeDelta()
        root_delta = delta.TreeDelta()
        tree = self.make_branch_and_memory_tree('branch')
        tree.lock_write()
        tree.add('')
        root_delta.added = [('', tree.path2id(''), 'directory')]
        class PreCommitException(Exception): pass
        def hook_func(local, master,
                      old_revno, old_revid, new_revno, new_revid,
                      tree_delta, future_tree):
            raise PreCommitException(new_revid)
        branch.Branch.hooks.install_named_hook(
            "pre_commit", self.capture_pre_commit_hook, None)
        branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None)
        revids = [None, None, None]
        # this commit will raise an exception
        # so the commit is rolled back and revno unchanged
        err = self.assertRaises(PreCommitException, tree.commit, 'message')
        # we have to record the revid to use in assertEqual later
        revids[0] = str(err)
        # unregister all pre_commit hooks
        branch.Branch.hooks["pre_commit"] = []
        # and re-register the capture hook
        branch.Branch.hooks.install_named_hook(
            "pre_commit", self.capture_pre_commit_hook, None)
        # now these commits should go through
        for i in range(1, 3):
            revids[i] = tree.commit('message')
        self.assertEqual([
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[0], root_delta),
            ('pre_commit', 0, revision.NULL_REVISION, 1, revids[1], root_delta),
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
            ],
            self.hook_calls)
        tree.unlock()

    def test_pre_commit_delta(self):
        # This tests the TreeDelta object passed to pre_commit hook.
        # This does not try to validate data correctness in the delta.
        self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
        tree = self.make_branch_and_tree('.')
        tree.lock_write()
        try:
            # setting up a playground
            tree.set_root_id('root_id')
            tree.add('rootfile', 'rootfile_id')
            tree.put_file_bytes_non_atomic('rootfile_id', 'abc')
            tree.add('dir', 'dir_id')
            tree.add('dir/subfile', 'dir_subfile_id')
            tree.mkdir('to_be_unversioned', 'to_be_unversioned_id')
            tree.put_file_bytes_non_atomic('dir_subfile_id', 'def')
            revid1 = tree.commit('first revision')
        finally:
            tree.unlock()

        tree.lock_write()
        try:
            # making changes
            tree.put_file_bytes_non_atomic('rootfile_id', 'jkl')
            tree.rename_one('dir/subfile', 'dir/subfile_renamed')
            tree.unversion(['to_be_unversioned_id'])
            tree.mkdir('added_dir', 'added_dir_id')
            # start to capture pre_commit delta
            branch.Branch.hooks.install_named_hook(
                "pre_commit", self.capture_pre_commit_hook, None)
            revid2 = tree.commit('second revision')
        finally:
            tree.unlock()

        expected_delta = delta.TreeDelta()
        expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
        expected_delta.removed = [('to_be_unversioned',
                                   'to_be_unversioned_id', 'directory')]
        expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed',
                                   'dir_subfile_id', 'file', False, False)]
        expected_delta.modified=[('rootfile', 'rootfile_id', 'file', True,
                                  False)]
        self.assertEqual([('pre_commit', 1, revid1, 2, revid2,
                           expected_delta)], self.hook_calls)