1
# Copyright (C) 2007, 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for the BranchBuilder class."""
20
branch as _mod_branch,
22
revision as _mod_revision,
25
from bzrlib.branchbuilder import BranchBuilder
28
class TestBranchBuilder(tests.TestCaseWithMemoryTransport):
30
def test_create(self):
31
"""Test the constructor api."""
32
builder = BranchBuilder(self.get_transport().clone('foo'))
33
# we dont care if the branch has been built or not at this point.
35
def test_get_branch(self):
36
"""get_branch returns the created branch."""
37
builder = BranchBuilder(self.get_transport().clone('foo'))
38
branch = builder.get_branch()
39
self.assertIsInstance(branch, _mod_branch.Branch)
40
self.assertEqual(self.get_transport().clone('foo').base,
43
(0, _mod_revision.NULL_REVISION),
44
branch.last_revision_info())
46
def test_format(self):
47
"""Making a BranchBuilder with a format option sets the branch type."""
48
builder = BranchBuilder(self.get_transport(), format='dirstate-tags')
49
branch = builder.get_branch()
50
self.assertIsInstance(branch, _mod_branch.BzrBranch6)
52
def test_build_one_commit(self):
53
"""doing build_commit causes a commit to happen."""
54
builder = BranchBuilder(self.get_transport().clone('foo'))
55
rev_id = builder.build_commit()
56
branch = builder.get_branch()
57
self.assertEqual((1, rev_id), branch.last_revision_info())
60
branch.repository.get_revision(branch.last_revision()).message)
62
def test_build_commit_timestamp(self):
63
"""You can set a date when committing."""
64
builder = self.make_branch_builder('foo')
65
rev_id = builder.build_commit(timestamp=1236043340)
66
branch = builder.get_branch()
67
self.assertEqual((1, rev_id), branch.last_revision_info())
68
rev = branch.repository.get_revision(branch.last_revision())
76
def test_build_two_commits(self):
77
"""The second commit has the right parents and message."""
78
builder = BranchBuilder(self.get_transport().clone('foo'))
79
rev_id1 = builder.build_commit()
80
rev_id2 = builder.build_commit()
81
branch = builder.get_branch()
82
self.assertEqual((2, rev_id2), branch.last_revision_info())
85
branch.repository.get_revision(branch.last_revision()).message)
88
branch.repository.get_revision(branch.last_revision()).parent_ids)
91
class TestBranchBuilderBuildSnapshot(tests.TestCaseWithMemoryTransport):
93
def assertTreeShape(self, expected_shape, tree):
94
"""Check that the tree shape matches expectations."""
97
entries = [(path, ie.file_id, ie.kind)
98
for path, ie in tree.iter_entries_by_dir()]
101
self.assertEqual(expected_shape, entries)
103
def build_a_rev(self):
104
builder = BranchBuilder(self.get_transport().clone('foo'))
105
rev_id1 = builder.build_snapshot('A-id', None,
106
[('add', ('', 'a-root-id', 'directory', None)),
107
('add', ('a', 'a-id', 'file', 'contents'))])
108
self.assertEqual('A-id', rev_id1)
111
def test_add_one_file(self):
112
builder = self.build_a_rev()
113
branch = builder.get_branch()
114
self.assertEqual((1, 'A-id'), branch.last_revision_info())
115
rev_tree = branch.repository.revision_tree('A-id')
117
self.addCleanup(rev_tree.unlock)
118
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
119
(u'a', 'a-id', 'file')], rev_tree)
120
self.assertEqual('contents', rev_tree.get_file_text('a-id'))
122
def test_add_second_file(self):
123
builder = self.build_a_rev()
124
rev_id2 = builder.build_snapshot('B-id', None,
125
[('add', ('b', 'b-id', 'file', 'content_b'))])
126
self.assertEqual('B-id', rev_id2)
127
branch = builder.get_branch()
128
self.assertEqual((2, rev_id2), branch.last_revision_info())
129
rev_tree = branch.repository.revision_tree(rev_id2)
131
self.addCleanup(rev_tree.unlock)
132
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
133
(u'a', 'a-id', 'file'),
134
(u'b', 'b-id', 'file')], rev_tree)
135
self.assertEqual('content_b', rev_tree.get_file_text('b-id'))
137
def test_add_empty_dir(self):
138
builder = self.build_a_rev()
139
rev_id2 = builder.build_snapshot('B-id', None,
140
[('add', ('b', 'b-id', 'directory', None))])
141
rev_tree = builder.get_branch().repository.revision_tree('B-id')
142
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
143
(u'a', 'a-id', 'file'),
144
(u'b', 'b-id', 'directory'),
147
def test_commit_timestamp(self):
148
builder = self.make_branch_builder('foo')
149
rev_id = builder.build_snapshot(None, None,
150
[('add', (u'', None, 'directory', None))],
151
timestamp=1234567890)
152
rev = builder.get_branch().repository.get_revision(rev_id)
157
def test_commit_message_default(self):
158
builder = BranchBuilder(self.get_transport().clone('foo'))
159
rev_id = builder.build_snapshot(None, None,
160
[('add', (u'', None, 'directory', None))])
161
branch = builder.get_branch()
162
rev = branch.repository.get_revision(rev_id)
163
self.assertEqual(u'commit 1', rev.message)
165
def test_commit_message_supplied(self):
166
builder = BranchBuilder(self.get_transport().clone('foo'))
167
rev_id = builder.build_snapshot(None, None,
168
[('add', (u'', None, 'directory', None))],
170
branch = builder.get_branch()
171
rev = branch.repository.get_revision(rev_id)
172
self.assertEqual(u'Foo', rev.message)
174
def test_commit_message_callback(self):
175
builder = BranchBuilder(self.get_transport().clone('foo'))
176
rev_id = builder.build_snapshot(None, None,
177
[('add', (u'', None, 'directory', None))],
178
message_callback=lambda x:u'Foo')
179
branch = builder.get_branch()
180
rev = branch.repository.get_revision(rev_id)
181
self.assertEqual(u'Foo', rev.message)
183
def test_modify_file(self):
184
builder = self.build_a_rev()
185
rev_id2 = builder.build_snapshot('B-id', None,
186
[('modify', ('a-id', 'new\ncontent\n'))])
187
self.assertEqual('B-id', rev_id2)
188
branch = builder.get_branch()
189
rev_tree = branch.repository.revision_tree(rev_id2)
191
self.addCleanup(rev_tree.unlock)
192
self.assertEqual('new\ncontent\n', rev_tree.get_file_text('a-id'))
194
def test_delete_file(self):
195
builder = self.build_a_rev()
196
rev_id2 = builder.build_snapshot('B-id', None,
197
[('unversion', 'a-id')])
198
self.assertEqual('B-id', rev_id2)
199
branch = builder.get_branch()
200
rev_tree = branch.repository.revision_tree(rev_id2)
202
self.addCleanup(rev_tree.unlock)
203
self.assertTreeShape([(u'', 'a-root-id', 'directory')], rev_tree)
205
def test_delete_directory(self):
206
builder = self.build_a_rev()
207
rev_id2 = builder.build_snapshot('B-id', None,
208
[('add', ('b', 'b-id', 'directory', None)),
209
('add', ('b/c', 'c-id', 'file', 'foo\n')),
210
('add', ('b/d', 'd-id', 'directory', None)),
211
('add', ('b/d/e', 'e-id', 'file', 'eff\n')),
213
rev_tree = builder.get_branch().repository.revision_tree('B-id')
214
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
215
(u'a', 'a-id', 'file'),
216
(u'b', 'b-id', 'directory'),
217
(u'b/c', 'c-id', 'file'),
218
(u'b/d', 'd-id', 'directory'),
219
(u'b/d/e', 'e-id', 'file')], rev_tree)
220
# Removing a directory removes all child dirs
221
builder.build_snapshot('C-id', None, [('unversion', 'b-id')])
222
rev_tree = builder.get_branch().repository.revision_tree('C-id')
223
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
224
(u'a', 'a-id', 'file'),
227
def test_unknown_action(self):
228
builder = self.build_a_rev()
229
e = self.assertRaises(ValueError,
230
builder.build_snapshot, 'B-id', None, [('weirdo', ('foo',))])
231
self.assertEqual('Unknown build action: "weirdo"', str(e))
233
def test_rename(self):
234
builder = self.build_a_rev()
235
builder.build_snapshot('B-id', None,
236
[('rename', ('a', 'b'))])
237
rev_tree = builder.get_branch().repository.revision_tree('B-id')
238
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
239
(u'b', 'a-id', 'file')], rev_tree)
241
def test_rename_into_subdir(self):
242
builder = self.build_a_rev()
243
builder.build_snapshot('B-id', None,
244
[('add', ('dir', 'dir-id', 'directory', None)),
245
('rename', ('a', 'dir/a'))])
246
rev_tree = builder.get_branch().repository.revision_tree('B-id')
247
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
248
(u'dir', 'dir-id', 'directory'),
249
(u'dir/a', 'a-id', 'file')], rev_tree)
251
def test_set_parent(self):
252
builder = self.build_a_rev()
253
builder.start_series()
254
self.addCleanup(builder.finish_series)
255
builder.build_snapshot('B-id', ['A-id'],
256
[('modify', ('a-id', 'new\ncontent\n'))])
257
builder.build_snapshot('C-id', ['A-id'],
258
[('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
259
# We should now have a graph:
263
# And not A => B => C
264
repo = builder.get_branch().repository
265
self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',)},
266
repo.get_parent_map(['B-id', 'C-id']))
267
b_tree = repo.revision_tree('B-id')
268
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
269
(u'a', 'a-id', 'file'),
271
self.assertEqual('new\ncontent\n', b_tree.get_file_text('a-id'))
273
# We should still be using the content from A in C, not from B
274
c_tree = repo.revision_tree('C-id')
275
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
276
(u'a', 'a-id', 'file'),
277
(u'c', 'c-id', 'file'),
279
self.assertEqual('contents', c_tree.get_file_text('a-id'))
280
self.assertEqual('alt\ncontent\n', c_tree.get_file_text('c-id'))
282
def test_set_merge_parent(self):
283
builder = self.build_a_rev()
284
builder.start_series()
285
self.addCleanup(builder.finish_series)
286
builder.build_snapshot('B-id', ['A-id'],
287
[('add', ('b', 'b-id', 'file', 'b\ncontent\n'))])
288
builder.build_snapshot('C-id', ['A-id'],
289
[('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
290
builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
291
repo = builder.get_branch().repository
292
self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',),
293
'D-id': ('B-id', 'C-id')},
294
repo.get_parent_map(['B-id', 'C-id', 'D-id']))
295
d_tree = repo.revision_tree('D-id')
296
# Note: by default a merge node does *not* pull in the changes from the
297
# merged tree, you have to supply it yourself.
298
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
299
(u'a', 'a-id', 'file'),
300
(u'b', 'b-id', 'file'),
303
def test_set_merge_parent_and_contents(self):
304
builder = self.build_a_rev()
305
builder.start_series()
306
self.addCleanup(builder.finish_series)
307
builder.build_snapshot('B-id', ['A-id'],
308
[('add', ('b', 'b-id', 'file', 'b\ncontent\n'))])
309
builder.build_snapshot('C-id', ['A-id'],
310
[('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
311
builder.build_snapshot('D-id', ['B-id', 'C-id'],
312
[('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
313
repo = builder.get_branch().repository
314
self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',),
315
'D-id': ('B-id', 'C-id')},
316
repo.get_parent_map(['B-id', 'C-id', 'D-id']))
317
d_tree = repo.revision_tree('D-id')
318
self.assertTreeShape([(u'', 'a-root-id', 'directory'),
319
(u'a', 'a-id', 'file'),
320
(u'b', 'b-id', 'file'),
321
(u'c', 'c-id', 'file'),
323
# Because we copied the exact text into *this* tree, the 'c' file
324
# should look like it was not modified in the merge
325
self.assertEqual('C-id', d_tree.inventory['c-id'].revision)
327
def test_start_finish_series(self):
328
builder = BranchBuilder(self.get_transport().clone('foo'))
329
builder.start_series()
331
self.assertIsNot(None, builder._tree)
332
self.assertEqual('w', builder._tree._lock_mode)
333
self.assertTrue(builder._branch.is_locked())
335
builder.finish_series()
336
self.assertIs(None, builder._tree)
337
self.assertFalse(builder._branch.is_locked())
339
def test_ghost_mainline_history(self):
340
builder = BranchBuilder(self.get_transport().clone('foo'))
341
builder.start_series()
343
builder.build_snapshot('tip', ['ghost'],
344
[('add', ('', 'ROOT_ID', 'directory', ''))],
345
allow_leftmost_as_ghost=True)
347
builder.finish_series()
348
b = builder.get_branch()
350
self.addCleanup(b.unlock)
351
self.assertEqual(('ghost',),
352
b.repository.get_graph().get_parent_map(['tip'])['tip'])