39
45
>>> from breezy.transport.memory import MemoryTransport
40
46
>>> builder = BranchBuilder(MemoryTransport("memory:///"))
41
47
>>> builder.start_series()
42
>>> builder.build_snapshot(None, [
43
... ('add', ('', b'root-id', 'directory', '')),
44
... ('add', ('filename', b'f-id', 'file', b'content\n'))],
45
... revision_id=b'rev-id')
47
>>> builder.build_snapshot([b'rev-id'],
48
... [('modify', ('filename', b'new-content\n'))],
49
... revision_id=b'rev2-id')
48
>>> builder.build_snapshot('rev-id', None, [
49
... ('add', ('', 'root-id', 'directory', '')),
50
... ('add', ('filename', 'f-id', 'file', 'content\n'))])
52
>>> builder.build_snapshot('rev2-id', ['rev-id'],
53
... [('modify', ('f-id', 'new-content\n'))])
51
55
>>> builder.finish_series()
52
56
>>> branch = builder.get_branch()
105
109
base_id = parent_ids[0]
106
110
if base_id != self._branch.last_revision():
107
self._move_branch_pointer(
108
base_id, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
109
tree = self._branch.create_memorytree()
110
with tree.lock_write():
111
self._move_branch_pointer(base_id,
112
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
113
tree = memorytree.MemoryTree.create_on_branch(self._branch)
111
116
if parent_ids is not None:
117
tree.set_parent_ids(parent_ids,
114
118
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
116
120
return self._do_commit(tree, **commit_kwargs)
118
124
def _do_commit(self, tree, message=None, message_callback=None, **kwargs):
119
125
reporter = commit.NullCommitReporter()
120
126
if message is None and message_callback is None:
121
127
message = u'commit %d' % (self._branch.revno() + 1,)
122
128
return tree.commit(message, message_callback=message_callback,
123
reporter=reporter, **kwargs)
125
132
def _move_branch_pointer(self, new_revision_id,
126
allow_leftmost_as_ghost=False):
133
allow_leftmost_as_ghost=False):
127
134
"""Point self._branch to a different revision id."""
128
with self._branch.lock_write():
135
self._branch.lock_write()
129
137
# We don't seem to have a simple set_last_revision(), so we
130
138
# implement it here.
131
139
cur_revno, cur_revision_id = self._branch.last_revision_info()
133
141
g = self._branch.repository.get_graph()
134
new_revno = g.find_distance_to_null(
135
new_revision_id, [(cur_revision_id, cur_revno)])
142
new_revno = g.find_distance_to_null(new_revision_id,
143
[(cur_revision_id, cur_revno)])
136
144
self._branch.set_last_revision_info(new_revno, new_revision_id)
137
145
except errors.GhostRevisionsHaveNoRevno:
138
146
if not allow_leftmost_as_ghost:
150
self._branch.unlock()
141
151
if self._tree is not None:
142
152
# We are currently processing a series, but when switching branch
143
153
# pointers, it is easiest to just create a new memory tree.
168
178
self._tree.unlock()
169
179
self._tree = None
171
def build_snapshot(self, parent_ids, actions, message=None, timestamp=None,
172
allow_leftmost_as_ghost=False, committer=None,
173
timezone=None, message_callback=None, revision_id=None):
181
def build_snapshot(self, revision_id, parent_ids, actions,
182
message=None, timestamp=None, allow_leftmost_as_ghost=False,
183
committer=None, timezone=None, message_callback=None):
174
184
"""Build a commit, shaped in a specific way.
176
186
Most of the actions are self-explanatory. 'flush' is special action to
178
188
(such as unversioning a file-id and re-adding it with a different kind)
179
189
can be expressed in a way that will clearly work.
191
:param revision_id: The handle for the new commit, can be None
181
192
:param parent_ids: A list of parent_ids to use for the commit.
182
193
It can be None, which indicates to use the last commit.
183
194
:param actions: A list of actions to perform. Supported actions are:
184
('add', ('path', b'file-id', 'kind', b'content' or None))
185
('modify', ('path', b'new-content'))
186
('unversion', 'path')
195
('add', ('path', 'file-id', 'kind', 'content' or None))
196
('modify', ('file-id', 'new-content'))
197
('unversion', 'file-id')
187
198
('rename', ('orig-path', 'new-path'))
189
200
:param message: An optional commit message, if not supplied, a default
206
216
base_id = parent_ids[0]
207
217
if base_id != self._branch.last_revision():
208
self._move_branch_pointer(
209
base_id, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
218
self._move_branch_pointer(base_id,
219
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
211
221
if self._tree is not None:
212
222
tree = self._tree
214
tree = self._branch.create_memorytree()
215
with tree.lock_write():
224
tree = memorytree.MemoryTree.create_on_branch(self._branch)
216
227
if parent_ids is not None:
228
tree.set_parent_ids(parent_ids,
219
229
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
220
230
# Unfortunately, MemoryTree.add(directory) just creates an
221
231
# inventory entry. And the only public function to create a
232
242
pending.to_add_file_ids.append(file_id)
233
243
pending.to_add_kinds.append(kind)
234
244
if content is not None:
235
pending.new_contents[path] = content
245
pending.new_contents[file_id] = content
236
246
elif action == 'modify':
238
pending.new_contents[path] = content
247
file_id, content = info
248
pending.new_contents[file_id] = content
239
249
elif action == 'unversion':
240
pending.to_unversion_paths.add(info)
250
pending.to_unversion_ids.add(info)
241
251
elif action == 'rename':
242
252
from_relpath, to_relpath = info
243
253
pending.to_rename.append((from_relpath, to_relpath))
248
258
raise ValueError('Unknown build action: "%s"' % (action,))
249
259
self._flush_pending(tree, pending)
250
return self._do_commit(
251
tree, message=message, rev_id=revision_id,
260
return self._do_commit(tree, message=message, rev_id=revision_id,
252
261
timestamp=timestamp, timezone=timezone, committer=committer,
253
262
message_callback=message_callback)
255
266
def _flush_pending(self, tree, pending):
256
"""Flush the pending actions in 'pending', i.e. apply them to tree."""
267
"""Flush the pending actions in 'pending', i.e. apply them to 'tree'."""
257
268
for path, file_id in pending.to_add_directories:
259
if tree.has_filename(path) \
260
and path in pending.to_unversion_paths:
270
old_id = tree.path2id(path)
271
if old_id is not None and old_id in pending.to_unversion_ids:
261
272
# We're overwriting this path, no need to unversion
262
pending.to_unversion_paths.discard(path)
273
pending.to_unversion_ids.discard(old_id)
263
274
# Special case, because the path already exists
264
275
tree.add([path], [file_id], ['directory'])
266
277
tree.mkdir(path, file_id)
267
278
for from_relpath, to_relpath in pending.to_rename:
268
279
tree.rename_one(from_relpath, to_relpath)
269
if pending.to_unversion_paths:
270
tree.unversion(pending.to_unversion_paths)
271
tree.add(pending.to_add_files, pending.to_add_file_ids,
272
pending.to_add_kinds)
273
for path, content in pending.new_contents.items():
274
tree.put_file_bytes_non_atomic(path, content)
280
if pending.to_unversion_ids:
281
tree.unversion(pending.to_unversion_ids)
282
tree.add(pending.to_add_files, pending.to_add_file_ids, pending.to_add_kinds)
283
for file_id, content in viewitems(pending.new_contents):
284
tree.put_file_bytes_non_atomic(file_id, content)
276
286
def get_branch(self):
277
287
"""Return the branch created by the builder."""