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