45
>>> from breezy.transport.memory import MemoryTransport
42
>>> from bzrlib.transport.memory import MemoryTransport
46
43
>>> builder = BranchBuilder(MemoryTransport("memory:///"))
47
44
>>> builder.start_series()
48
>>> builder.build_snapshot(None, [
49
... ('add', ('', b'root-id', 'directory', '')),
50
... ('add', ('filename', b'f-id', 'file', 'content\n'))],
51
... revision_id=b'rev-id')
45
>>> builder.build_snapshot('rev-id', None, [
46
... ('add', ('', 'root-id', 'directory', '')),
47
... ('add', ('filename', 'f-id', 'file', 'content\n'))])
53
>>> builder.build_snapshot(['rev-id'],
54
... [('modify', (b'f-id', 'new-content\n'))],
55
... revision_id=b'rev2-id')
49
>>> builder.build_snapshot('rev2-id', ['rev-id'],
50
... [('modify', ('f-id', 'new-content\n'))])
57
52
>>> builder.finish_series()
58
53
>>> branch = builder.get_branch()
91
86
if isinstance(format, str):
92
format = controldir.format_registry.make_controldir(format)
87
format = controldir.format_registry.make_bzrdir(format)
93
88
self._branch = controldir.ControlDir.create_branch_convenience(
94
89
transport.base, format=format, force_new_tree=False)
112
107
if base_id != self._branch.last_revision():
113
108
self._move_branch_pointer(base_id,
114
109
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
115
tree = self._branch.create_memorytree()
110
tree = memorytree.MemoryTree.create_on_branch(self._branch)
116
111
tree.lock_write()
118
113
if parent_ids is not None:
157
152
# We are cheating a little bit here, and locking the new tree
158
153
# before the old tree is unlocked. But that way the branch stays
159
154
# locked throughout.
160
new_tree = self._branch.create_memorytree()
155
new_tree = memorytree.MemoryTree.create_on_branch(self._branch)
161
156
new_tree.lock_write()
162
157
self._tree.unlock()
163
158
self._tree = new_tree
172
167
if self._tree is not None:
173
168
raise AssertionError('You cannot start a new series while a'
174
169
' series is already going.')
175
self._tree = self._branch.create_memorytree()
170
self._tree = memorytree.MemoryTree.create_on_branch(self._branch)
176
171
self._tree.lock_write()
178
173
def finish_series(self):
180
175
self._tree.unlock()
181
176
self._tree = None
183
def build_snapshot(self, parent_ids, actions, message=None, timestamp=None,
184
allow_leftmost_as_ghost=False, committer=None, timezone=None,
185
message_callback=None, revision_id=None):
178
def build_snapshot(self, revision_id, parent_ids, actions,
179
message=None, timestamp=None, allow_leftmost_as_ghost=False,
180
committer=None, timezone=None, message_callback=None):
186
181
"""Build a commit, shaped in a specific way.
188
183
Most of the actions are self-explanatory. 'flush' is special action to
190
185
(such as unversioning a file-id and re-adding it with a different kind)
191
186
can be expressed in a way that will clearly work.
188
:param revision_id: The handle for the new commit, can be None
193
189
:param parent_ids: A list of parent_ids to use for the commit.
194
190
It can be None, which indicates to use the last commit.
195
191
:param actions: A list of actions to perform. Supported actions are:
196
192
('add', ('path', 'file-id', 'kind', 'content' or None))
197
('modify', ('path', 'new-content'))
198
('unversion', 'path')
193
('modify', ('file-id', 'new-content'))
194
('unversion', 'file-id')
199
195
('rename', ('orig-path', 'new-path'))
201
197
:param message: An optional commit message, if not supplied, a default
208
204
:param committer: An optional username to use for commit
209
205
:param allow_leftmost_as_ghost: True if the leftmost parent should be
210
206
permitted to be a ghost.
211
:param revision_id: The handle for the new commit, can be None
212
207
:return: The revision_id of the new commit
214
209
if parent_ids is not None:
223
218
if self._tree is not None:
224
219
tree = self._tree
226
tree = self._branch.create_memorytree()
227
with tree.lock_write():
221
tree = memorytree.MemoryTree.create_on_branch(self._branch)
228
224
if parent_ids is not None:
229
225
tree.set_parent_ids(parent_ids,
230
226
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
243
239
pending.to_add_file_ids.append(file_id)
244
240
pending.to_add_kinds.append(kind)
245
241
if content is not None:
246
pending.new_contents[path] = content
242
pending.new_contents[file_id] = content
247
243
elif action == 'modify':
249
pending.new_contents[path] = content
244
file_id, content = info
245
pending.new_contents[file_id] = content
250
246
elif action == 'unversion':
251
pending.to_unversion_paths.add(info)
247
pending.to_unversion_ids.add(info)
252
248
elif action == 'rename':
253
249
from_relpath, to_relpath = info
254
250
pending.to_rename.append((from_relpath, to_relpath))
261
257
return self._do_commit(tree, message=message, rev_id=revision_id,
262
258
timestamp=timestamp, timezone=timezone, committer=committer,
263
259
message_callback=message_callback)
265
263
def _flush_pending(self, tree, pending):
266
264
"""Flush the pending actions in 'pending', i.e. apply them to 'tree'."""
267
265
for path, file_id in pending.to_add_directories:
269
if tree.has_filename(path) and path in pending.to_unversion_paths:
267
old_id = tree.path2id(path)
268
if old_id is not None and old_id in pending.to_unversion_ids:
270
269
# We're overwriting this path, no need to unversion
271
pending.to_unversion_paths.discard(path)
270
pending.to_unversion_ids.discard(old_id)
272
271
# Special case, because the path already exists
273
272
tree.add([path], [file_id], ['directory'])
275
274
tree.mkdir(path, file_id)
276
275
for from_relpath, to_relpath in pending.to_rename:
277
276
tree.rename_one(from_relpath, to_relpath)
278
if pending.to_unversion_paths:
279
tree.unversion(pending.to_unversion_paths)
277
if pending.to_unversion_ids:
278
tree.unversion(pending.to_unversion_ids)
280
279
tree.add(pending.to_add_files, pending.to_add_file_ids, pending.to_add_kinds)
281
for path, content in viewitems(pending.new_contents):
282
tree.put_file_bytes_non_atomic(path, content)
280
for file_id, content in pending.new_contents.iteritems():
281
tree.put_file_bytes_non_atomic(file_id, content)
284
283
def get_branch(self):
285
284
"""Return the branch created by the builder."""