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