59
60
('post_commit', local_base, master.base, old_revno, old_revid,
60
61
new_revno, new_revid, local_locked, master.is_locked()))
63
def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
65
tree_delta, future_tree):
66
self.hook_calls.append(('pre_commit', old_revno, old_revid,
67
new_revno, new_revid, tree_delta))
62
69
def test_post_commit_to_origin(self):
63
70
tree = self.make_branch_and_memory_tree('branch')
64
71
Branch.hooks.install_hook('post_commit',
123
def test_pre_commit_passes(self):
124
empty_delta = TreeDelta()
125
root_delta = TreeDelta()
126
root_delta.added = [('', '', 'directory')]
127
tree = self.make_branch_and_memory_tree('branch')
130
Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
131
revid1 = tree.commit('first revision')
132
revid2 = tree.commit('second revision')
134
('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
135
('pre_commit', 1, revid1, 2, revid2, empty_delta)
140
def test_pre_commit_fails(self):
141
empty_delta = TreeDelta()
142
root_delta = TreeDelta()
143
root_delta.added = [('', '', 'directory')]
144
tree = self.make_branch_and_memory_tree('branch')
147
class PreCommitException(Exception): pass
148
def hook_func(local, master,
149
old_revno, old_revid, new_revno, new_revid,
150
tree_delta, future_tree):
151
raise PreCommitException(new_revid)
152
Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
153
Branch.hooks.install_hook("pre_commit", hook_func)
154
revids = [None, None, None]
155
# this commit will raise an exception
156
# so the commit is rolled back and revno unchanged
157
err = self.assertRaises(PreCommitException, tree.commit, 'message')
158
# we have to record the revid to use in assertEqual later
160
# unregister all pre_commit hooks
161
Branch.hooks["pre_commit"] = []
162
# and re-register the capture hook
163
Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
164
# now these commits should go through
165
for i in range(1, 3):
166
revids[i] = tree.commit('message')
168
('pre_commit', 0, NULL_REVISION, 1, revids[0], root_delta),
169
('pre_commit', 0, NULL_REVISION, 1, revids[1], root_delta),
170
('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
175
def test_pre_commit_delta(self):
176
# This tests the TreeDelta object passed to pre_commit hook.
177
# This does not try to validate data correctness in the delta.
178
self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
179
tree = self.make_branch_and_tree('.')
182
# setting up a playground
183
tree.set_root_id('root_id')
184
tree.add('rootfile', 'rootfile_id')
185
tree.put_file_bytes_non_atomic('rootfile_id', 'abc')
186
tree.add('dir', 'dir_id')
187
tree.add('dir/subfile', 'dir_subfile_id')
188
tree.mkdir('to_be_unversioned', 'to_be_unversioned_id')
189
tree.put_file_bytes_non_atomic('dir_subfile_id', 'def')
190
revid1 = tree.commit('first revision')
197
tree.put_file_bytes_non_atomic('rootfile_id', 'jkl')
198
tree.rename_one('dir/subfile', 'dir/subfile_renamed')
199
tree.unversion(['to_be_unversioned_id'])
200
tree.mkdir('added_dir', 'added_dir_id')
201
# start to capture pre_commit delta
202
Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
203
revid2 = tree.commit('second revision')
207
expected_delta = TreeDelta()
208
expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
209
expected_delta.removed = [('to_be_unversioned',
210
'to_be_unversioned_id', 'directory')]
211
expected_delta.renamed = [('dir/subfile', 'dir/subfile_renamed',
212
'dir_subfile_id', 'file', False, False)]
213
expected_delta.modified=[('rootfile', 'rootfile_id', 'file', True,
215
self.assertEqual([('pre_commit', 1, revid1, 2, revid2,
216
expected_delta)], self.hook_calls)