121
123
def test_pre_commit_passes(self):
124
empty_delta = TreeDelta()
125
root_delta = TreeDelta()
126
root_delta.added = [('', '', 'directory')]
122
127
tree = self.make_branch_and_memory_tree('branch')
123
128
tree.lock_write()
125
130
Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
126
131
revid1 = tree.commit('first revision')
127
132
revid2 = tree.commit('second revision')
128
133
self.assertEqual([
129
('pre_commit', 0, NULL_REVISION, 1, revid1, {}),
130
('pre_commit', 1, revid1, 2, revid2, {})
134
('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
135
('pre_commit', 1, revid1, 2, revid2, empty_delta)
135
140
def test_pre_commit_fails(self):
141
empty_delta = TreeDelta()
142
root_delta = TreeDelta()
143
root_delta.added = [('', '', 'directory')]
136
144
tree = self.make_branch_and_memory_tree('branch')
137
145
tree.lock_write()
139
147
class PreCommitException(Exception): pass
140
def hook_func(_1, _2, _3, _4, _5, new_revid, _7, _8):
148
def hook_func(local, master,
149
old_revno, old_revid, new_revno, new_revid,
150
tree_delta, future_tree):
141
151
raise PreCommitException(new_revid)
142
152
Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
143
153
Branch.hooks.install_hook("pre_commit", hook_func)
144
154
revids = [None, None, None]
145
# this commit will raise exception
146
# so the commit is rollbacked and revno unchanged
155
# this commit will raise an exception
156
# so the commit is rolled back and revno unchanged
147
157
err = self.assertRaises(PreCommitException, tree.commit, 'message')
148
158
# we have to record the revid to use in assertEqual later
149
159
revids[0] = err.message
155
165
for i in range(1, 3):
156
166
revids[i] = tree.commit('message')
157
167
self.assertEqual([
158
('pre_commit', 0, NULL_REVISION, 1, revids[0], {}),
159
('pre_commit', 0, NULL_REVISION, 1, revids[1], {}),
160
('pre_commit', 1, revids[1], 2, revids[2], {})
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)
165
def test_pre_commit_ids(self):
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.
166
178
self.build_tree(['rootfile', 'dir/', 'dir/subfile'])
167
179
tree = self.make_branch_and_tree('.')
168
180
tree.lock_write()
169
tree.set_root_id('root_id')
170
tree.add('rootfile', 'rootfile_id')
171
tree.put_file_bytes_non_atomic('rootfile_id', 'abc')
172
tree.add('dir', 'dir_id')
173
tree.add('dir/subfile', 'dir_subfile_id')
174
tree.put_file_bytes_non_atomic('dir_subfile_id', 'def')
175
Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
176
rev1 = tree.commit('first revision')
177
tree.unversion(['dir_id'])
178
rev2 = tree.commit('second revision')
179
tree.put_file_bytes_non_atomic('rootfile_id', 'ghi')
180
rev3 = tree.commit('third revision')
183
tree.rename_one('rootfile', 'renamed')
184
rev4 = tree.commit('fourth revision')
187
tree.put_file_bytes_non_atomic('rootfile_id', 'jkl')
188
tree.rename_one('renamed', 'rootfile')
189
rev5 = tree.commit('fifth revision')
192
('pre_commit', 0, NULL_REVISION, 1, rev1,
193
{'added': ['dir_id', 'dir_subfile_id', 'rootfile_id']} ),
194
('pre_commit', 1, rev1, 2, rev2,
195
{'deleted': ['dir_id', 'dir_subfile_id']} ),
196
('pre_commit', 2, rev2, 3, rev3,
197
{'modified': ['rootfile_id']} ),
198
('pre_commit', 3, rev3, 4, rev4,
199
{'renamed': ['rootfile_id']} ),
200
('pre_commit', 4, rev4, 5, rev5,
201
{'modified and renamed': ['rootfile_id']} )
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)
b'\\ No newline at end of file'