/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/per_branch/test_commit.py

  • Committer: Jelmer Vernooij
  • Date: 2019-12-23 01:39:21 UTC
  • mfrom: (7424 work)
  • mto: This revision was merged to the branch mainline in revision 7425.
  • Revision ID: jelmer@jelmer.uk-20191223013921-2kzd0wlcoylgxksk
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
110
110
 
111
111
    def test_post_commit_not_to_origin(self):
112
112
        tree = self.make_branch_and_memory_tree('branch')
113
 
        tree.lock_write()
114
 
        tree.add('')
115
 
        revid = tree.commit('first revision')
116
 
        branch.Branch.hooks.install_named_hook(
117
 
            'post_commit', self.capture_post_commit_hook, None)
118
 
        revid2 = tree.commit('second revision')
119
 
        # having committed from up the branch, we should get the
120
 
        # before and after revnos and revids correctly.
121
 
        self.assertEqual([
122
 
            ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
123
 
             None, True)
124
 
            ],
125
 
            self.hook_calls)
126
 
        tree.unlock()
 
113
        with tree.lock_write():
 
114
            tree.add('')
 
115
            revid = tree.commit('first revision')
 
116
            branch.Branch.hooks.install_named_hook(
 
117
                'post_commit', self.capture_post_commit_hook, None)
 
118
            revid2 = tree.commit('second revision')
 
119
            # having committed from up the branch, we should get the
 
120
            # before and after revnos and revids correctly.
 
121
            self.assertEqual([
 
122
                ('post_commit', None, tree.branch.base, 1, revid, 2, revid2,
 
123
                 None, True)
 
124
                ],
 
125
                self.hook_calls)
 
126
 
 
127
    def get_rootfull_delta(self, repository, revid):
 
128
        tree = repository.revision_tree(revid)
 
129
        with repository.lock_read():
 
130
            parent_revid = repository.get_parent_map([revid])[revid][0]
 
131
            basis_tree = repository.revision_tree(parent_revid)
 
132
            tree = repository.revision_tree(revid)
 
133
            return tree.changes_from(basis_tree, include_root=True)
127
134
 
128
135
    def test_pre_commit_passes(self):
129
 
        empty_delta = delta.TreeDelta()
130
 
        root_delta = delta.TreeDelta()
131
136
        tree = self.make_branch_and_memory_tree('branch')
132
 
        tree.lock_write()
133
 
        tree.add('')
134
 
        root_delta.added = [('', tree.path2id(''), 'directory')]
135
 
        branch.Branch.hooks.install_named_hook(
136
 
            "pre_commit", self.capture_pre_commit_hook, None)
137
 
        revid1 = tree.commit('first revision')
138
 
        revid2 = tree.commit('second revision')
139
 
        self.assertEqual([
140
 
            ('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
141
 
            ('pre_commit', 1, revid1, 2, revid2, empty_delta)
142
 
            ],
143
 
            self.hook_calls)
144
 
        tree.unlock()
 
137
        with tree.lock_write():
 
138
            tree.add('')
 
139
            branch.Branch.hooks.install_named_hook(
 
140
                "pre_commit", self.capture_pre_commit_hook, None)
 
141
            revid1 = tree.commit('first revision')
 
142
            revid2 = tree.commit('second revision')
 
143
            root_delta = self.get_rootfull_delta(tree.branch.repository, revid1)
 
144
            empty_delta = tree.branch.repository.get_revision_delta(revid2)
 
145
            self.assertEqual([
 
146
                ('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
 
147
                ('pre_commit', 1, revid1, 2, revid2, empty_delta)
 
148
                ],
 
149
                self.hook_calls)
145
150
 
146
151
    def test_pre_commit_fails(self):
147
 
        empty_delta = delta.TreeDelta()
148
 
        root_delta = delta.TreeDelta()
149
152
        tree = self.make_branch_and_memory_tree('branch')
150
 
        tree.lock_write()
151
 
        tree.add('')
152
 
        root_delta.added = [('', tree.path2id(''), 'directory')]
153
 
 
154
 
        class PreCommitException(Exception):
155
 
 
156
 
            def __init__(self, revid):
157
 
                self.revid = revid
158
 
 
159
 
        def hook_func(local, master,
160
 
                      old_revno, old_revid, new_revno, new_revid,
161
 
                      tree_delta, future_tree):
162
 
            raise PreCommitException(new_revid)
163
 
        branch.Branch.hooks.install_named_hook(
164
 
            "pre_commit", self.capture_pre_commit_hook, None)
165
 
        branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None)
166
 
        revids = [None, None, None]
167
 
        # this commit will raise an exception
168
 
        # so the commit is rolled back and revno unchanged
169
 
        err = self.assertRaises(PreCommitException, tree.commit, 'message')
170
 
        # we have to record the revid to use in assertEqual later
171
 
        revids[0] = err.revid
172
 
        # unregister all pre_commit hooks
173
 
        branch.Branch.hooks["pre_commit"] = []
174
 
        # and re-register the capture hook
175
 
        branch.Branch.hooks.install_named_hook(
176
 
            "pre_commit", self.capture_pre_commit_hook, None)
177
 
        # now these commits should go through
178
 
        for i in range(1, 3):
179
 
            revids[i] = tree.commit('message')
180
 
        self.assertEqual([
181
 
            ('pre_commit', 0, revision.NULL_REVISION,
182
 
             1, revids[0], root_delta),
183
 
            ('pre_commit', 0, revision.NULL_REVISION,
184
 
             1, revids[1], root_delta),
185
 
            ('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
186
 
            ],
187
 
            self.hook_calls)
188
 
        tree.unlock()
 
153
        with tree.lock_write():
 
154
            tree.add('')
 
155
 
 
156
            class PreCommitException(Exception):
 
157
 
 
158
                def __init__(self, revid):
 
159
                    self.revid = revid
 
160
 
 
161
            def hook_func(local, master,
 
162
                          old_revno, old_revid, new_revno, new_revid,
 
163
                          tree_delta, future_tree):
 
164
                raise PreCommitException(new_revid)
 
165
            branch.Branch.hooks.install_named_hook(
 
166
                "pre_commit", self.capture_pre_commit_hook, None)
 
167
            branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None)
 
168
            revids = [None, None, None]
 
169
            # this commit will raise an exception
 
170
            # so the commit is rolled back and revno unchanged
 
171
            err = self.assertRaises(PreCommitException, tree.commit, 'message')
 
172
            # we have to record the revid to use in assertEqual later
 
173
            revids[0] = err.revid
 
174
            # unregister all pre_commit hooks
 
175
            branch.Branch.hooks["pre_commit"] = []
 
176
            # and re-register the capture hook
 
177
            branch.Branch.hooks.install_named_hook(
 
178
                "pre_commit", self.capture_pre_commit_hook, None)
 
179
            # now these commits should go through
 
180
            for i in range(1, 3):
 
181
                revids[i] = tree.commit('message')
 
182
            self.assertEqual([
 
183
                ('pre_commit', 0, revision.NULL_REVISION,
 
184
                 1, revids[0], self.get_rootfull_delta(tree.branch.repository, revids[0])),
 
185
                ('pre_commit', 0, revision.NULL_REVISION,
 
186
                 1, revids[1], self.get_rootfull_delta(tree.branch.repository, revids[1])),
 
187
                ('pre_commit', 1, revids[1], 2, revids[2],
 
188
                 self.get_rootfull_delta(tree.branch.repository, revids[2]))
 
189
                ],
 
190
                self.hook_calls)
189
191
 
190
192
    def test_pre_commit_delta(self):
191
193
        # This tests the TreeDelta object passed to pre_commit hook.
219
221
                "pre_commit", self.capture_pre_commit_hook, None)
220
222
            revid2 = tree.commit('second revision')
221
223
 
222
 
        expected_delta = delta.TreeDelta()
223
 
        if tree.has_versioned_directories():
224
 
            expected_delta.added.append(
225
 
                ('added_dir', added_dir_id, 'directory'))
226
 
        if tree.supports_rename_tracking():
227
 
            expected_delta.removed = [('to_be_unversioned',
228
 
                                       to_be_unversioned_id, 'file')]
229
 
            expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed',
230
 
                                       dir_subfile_id, 'file', False, False)]
231
 
        else:
232
 
            expected_delta.added.append(('dir/subfile_renamed',
233
 
                                         tree.path2id('dir/subfile_renamed'), 'file'))
234
 
            expected_delta.removed = [
235
 
                ('dir/subfile', dir_subfile_id, 'file'),
236
 
                ('to_be_unversioned', to_be_unversioned_id, 'file')]
237
 
        expected_delta.modified = [('rootfile', rootfile_id, 'file', True,
238
 
                                    False)]
239
224
        self.assertEqual([('pre_commit', 1, revid1, 2, revid2,
240
 
                           expected_delta)], self.hook_calls)
 
225
                           self.get_rootfull_delta(tree.branch.repository, revid2))], self.hook_calls)