1
# Copyright (C) 2007-2010 Canonical Ltd
1
# Copyright (C) 2007 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
"""Tests for the contract of commit on branches."""
26
from bzrlib.tests import per_branch
29
class TestCommit(per_branch.TestCaseWithBranch):
19
from bzrlib.branch import Branch
20
from bzrlib import errors
21
from bzrlib.tests.per_branch.test_branch import TestCaseWithBranch
22
from bzrlib.revision import NULL_REVISION
23
from bzrlib.transport import get_transport
24
from bzrlib.delta import TreeDelta
27
class TestCommit(TestCaseWithBranch):
31
29
def test_commit_nicks(self):
32
30
"""Nicknames are committed to the revision"""
33
self.get_transport().mkdir('bzr.dev')
31
get_transport(self.get_url()).mkdir('bzr.dev')
34
32
wt = self.make_branch_and_tree('bzr.dev')
36
34
branch.nick = "My happy branch"
43
class TestCommitHook(per_branch.TestCaseWithBranch):
41
class TestCommitHook(TestCaseWithBranch):
46
44
self.hook_calls = []
47
super(TestCommitHook, self).setUp()
45
TestCaseWithBranch.setUp(self)
49
47
def capture_post_commit_hook(self, local, master, old_revno,
50
48
old_revid, new_revno, new_revid):
71
69
def test_post_commit_to_origin(self):
72
70
tree = self.make_branch_and_memory_tree('branch')
73
branch.Branch.hooks.install_named_hook(
74
'post_commit', self.capture_post_commit_hook, None)
71
Branch.hooks.install_named_hook('post_commit',
72
self.capture_post_commit_hook, None)
77
75
revid = tree.commit('a revision')
78
76
# should have had one notification, from origin, and
79
77
# have the branch locked at notification time.
81
('post_commit', None, tree.branch.base, 0, revision.NULL_REVISION,
79
('post_commit', None, tree.branch.base, 0, NULL_REVISION, 1, revid,
92
90
except errors.UpgradeRequired:
93
91
# cant bind this format, the test is irrelevant.
95
branch.Branch.hooks.install_named_hook(
96
'post_commit', self.capture_post_commit_hook, None)
93
Branch.hooks.install_named_hook('post_commit',
94
self.capture_post_commit_hook, None)
99
97
revid = tree.commit('a revision')
100
98
# with a bound branch, local is set.
101
99
self.assertEqual([
102
('post_commit', tree.branch.base, master.base, 0,
103
revision.NULL_REVISION, 1, revid, True, True)
100
('post_commit', tree.branch.base, master.base, 0, NULL_REVISION,
101
1, revid, True, True)
110
108
tree.lock_write()
112
110
revid = tree.commit('first revision')
113
branch.Branch.hooks.install_named_hook(
114
'post_commit', self.capture_post_commit_hook, None)
111
Branch.hooks.install_named_hook('post_commit',
112
self.capture_post_commit_hook, None)
115
113
revid2 = tree.commit('second revision')
116
114
# having committed from up the branch, we should get the
117
115
# before and after revnos and revids correctly.
125
123
def test_pre_commit_passes(self):
126
empty_delta = delta.TreeDelta()
127
root_delta = delta.TreeDelta()
124
empty_delta = TreeDelta()
125
root_delta = TreeDelta()
128
126
tree = self.make_branch_and_memory_tree('branch')
129
127
tree.lock_write()
131
129
root_delta.added = [('', tree.path2id(''), 'directory')]
132
branch.Branch.hooks.install_named_hook(
133
"pre_commit", self.capture_pre_commit_hook, None)
130
Branch.hooks.install_named_hook("pre_commit",
131
self.capture_pre_commit_hook, None)
134
132
revid1 = tree.commit('first revision')
135
133
revid2 = tree.commit('second revision')
136
134
self.assertEqual([
137
('pre_commit', 0, revision.NULL_REVISION, 1, revid1, root_delta),
135
('pre_commit', 0, NULL_REVISION, 1, revid1, root_delta),
138
136
('pre_commit', 1, revid1, 2, revid2, empty_delta)
143
141
def test_pre_commit_fails(self):
144
empty_delta = delta.TreeDelta()
145
root_delta = delta.TreeDelta()
142
empty_delta = TreeDelta()
143
root_delta = TreeDelta()
146
144
tree = self.make_branch_and_memory_tree('branch')
147
145
tree.lock_write()
152
150
old_revno, old_revid, new_revno, new_revid,
153
151
tree_delta, future_tree):
154
152
raise PreCommitException(new_revid)
155
branch.Branch.hooks.install_named_hook(
156
"pre_commit", self.capture_pre_commit_hook, None)
157
branch.Branch.hooks.install_named_hook("pre_commit", hook_func, None)
153
Branch.hooks.install_named_hook("pre_commit",
154
self.capture_pre_commit_hook, None)
155
Branch.hooks.install_named_hook("pre_commit", hook_func, None)
158
156
revids = [None, None, None]
159
157
# this commit will raise an exception
160
158
# so the commit is rolled back and revno unchanged
162
160
# we have to record the revid to use in assertEqual later
163
161
revids[0] = str(err)
164
162
# unregister all pre_commit hooks
165
branch.Branch.hooks["pre_commit"] = []
163
Branch.hooks["pre_commit"] = []
166
164
# and re-register the capture hook
167
branch.Branch.hooks.install_named_hook(
168
"pre_commit", self.capture_pre_commit_hook, None)
165
Branch.hooks.install_named_hook("pre_commit",
166
self.capture_pre_commit_hook, None)
169
167
# now these commits should go through
170
168
for i in range(1, 3):
171
169
revids[i] = tree.commit('message')
172
170
self.assertEqual([
173
('pre_commit', 0, revision.NULL_REVISION, 1, revids[0], root_delta),
174
('pre_commit', 0, revision.NULL_REVISION, 1, revids[1], root_delta),
171
('pre_commit', 0, NULL_REVISION, 1, revids[0], root_delta),
172
('pre_commit', 0, NULL_REVISION, 1, revids[1], root_delta),
175
173
('pre_commit', 1, revids[1], 2, revids[2], empty_delta)
204
202
tree.unversion(['to_be_unversioned_id'])
205
203
tree.mkdir('added_dir', 'added_dir_id')
206
204
# start to capture pre_commit delta
207
branch.Branch.hooks.install_named_hook(
208
"pre_commit", self.capture_pre_commit_hook, None)
205
Branch.hooks.install_named_hook("pre_commit",
206
self.capture_pre_commit_hook, None)
209
207
revid2 = tree.commit('second revision')
213
expected_delta = delta.TreeDelta()
211
expected_delta = TreeDelta()
214
212
expected_delta.added = [('added_dir', 'added_dir_id', 'directory')]
215
213
expected_delta.removed = [('to_be_unversioned',
216
214
'to_be_unversioned_id', 'directory')]