46
47
>>> builder.start_series()
47
48
>>> builder.build_snapshot(None, [
48
49
... ('add', ('', b'root-id', 'directory', '')),
49
... ('add', ('filename', b'f-id', 'file', b'content\n'))],
50
... ('add', ('filename', b'f-id', 'file', 'content\n'))],
50
51
... revision_id=b'rev-id')
52
>>> builder.build_snapshot([b'rev-id'],
53
... [('modify', ('filename', b'new-content\n'))],
53
>>> builder.build_snapshot(['rev-id'],
54
... [('modify', (b'f-id', 'new-content\n'))],
54
55
... revision_id=b'rev2-id')
56
57
>>> builder.finish_series()
110
111
base_id = parent_ids[0]
111
112
if base_id != self._branch.last_revision():
112
self._move_branch_pointer(
113
base_id, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
113
self._move_branch_pointer(base_id,
114
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
114
115
tree = self._branch.create_memorytree()
115
with tree.lock_write():
116
118
if parent_ids is not None:
119
tree.set_parent_ids(parent_ids,
119
120
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
121
122
return self._do_commit(tree, **commit_kwargs)
123
126
def _do_commit(self, tree, message=None, message_callback=None, **kwargs):
124
127
reporter = commit.NullCommitReporter()
125
128
if message is None and message_callback is None:
126
129
message = u'commit %d' % (self._branch.revno() + 1,)
127
130
return tree.commit(message, message_callback=message_callback,
128
reporter=reporter, **kwargs)
130
134
def _move_branch_pointer(self, new_revision_id,
131
allow_leftmost_as_ghost=False):
135
allow_leftmost_as_ghost=False):
132
136
"""Point self._branch to a different revision id."""
133
with self._branch.lock_write():
137
self._branch.lock_write()
134
139
# We don't seem to have a simple set_last_revision(), so we
135
140
# implement it here.
136
141
cur_revno, cur_revision_id = self._branch.last_revision_info()
138
143
g = self._branch.repository.get_graph()
139
new_revno = g.find_distance_to_null(
140
new_revision_id, [(cur_revision_id, cur_revno)])
144
new_revno = g.find_distance_to_null(new_revision_id,
145
[(cur_revision_id, cur_revno)])
141
146
self._branch.set_last_revision_info(new_revno, new_revision_id)
142
147
except errors.GhostRevisionsHaveNoRevno:
143
148
if not allow_leftmost_as_ghost:
152
self._branch.unlock()
146
153
if self._tree is not None:
147
154
# We are currently processing a series, but when switching branch
148
155
# pointers, it is easiest to just create a new memory tree.
174
181
self._tree = None
176
183
def build_snapshot(self, parent_ids, actions, message=None, timestamp=None,
177
allow_leftmost_as_ghost=False, committer=None,
178
timezone=None, message_callback=None, revision_id=None):
184
allow_leftmost_as_ghost=False, committer=None, timezone=None,
185
message_callback=None, revision_id=None):
179
186
"""Build a commit, shaped in a specific way.
181
188
Most of the actions are self-explanatory. 'flush' is special action to
186
193
:param parent_ids: A list of parent_ids to use for the commit.
187
194
It can be None, which indicates to use the last commit.
188
195
:param actions: A list of actions to perform. Supported actions are:
189
('add', ('path', b'file-id', 'kind', b'content' or None))
190
('modify', ('path', b'new-content'))
196
('add', ('path', 'file-id', 'kind', 'content' or None))
197
('modify', ('path', 'new-content'))
191
198
('unversion', 'path')
192
199
('rename', ('orig-path', 'new-path'))
211
218
base_id = parent_ids[0]
212
219
if base_id != self._branch.last_revision():
213
self._move_branch_pointer(
214
base_id, allow_leftmost_as_ghost=allow_leftmost_as_ghost)
220
self._move_branch_pointer(base_id,
221
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
216
223
if self._tree is not None:
217
224
tree = self._tree
219
226
tree = self._branch.create_memorytree()
220
227
with tree.lock_write():
221
228
if parent_ids is not None:
229
tree.set_parent_ids(parent_ids,
224
230
allow_leftmost_as_ghost=allow_leftmost_as_ghost)
225
231
# Unfortunately, MemoryTree.add(directory) just creates an
226
232
# inventory entry. And the only public function to create a
253
259
raise ValueError('Unknown build action: "%s"' % (action,))
254
260
self._flush_pending(tree, pending)
255
return self._do_commit(
256
tree, message=message, rev_id=revision_id,
261
return self._do_commit(tree, message=message, rev_id=revision_id,
257
262
timestamp=timestamp, timezone=timezone, committer=committer,
258
263
message_callback=message_callback)
260
265
def _flush_pending(self, tree, pending):
261
"""Flush the pending actions in 'pending', i.e. apply them to tree."""
266
"""Flush the pending actions in 'pending', i.e. apply them to 'tree'."""
262
267
for path, file_id in pending.to_add_directories:
264
if tree.has_filename(path) \
265
and path in pending.to_unversion_paths:
269
if tree.has_filename(path) and path in pending.to_unversion_paths:
266
270
# We're overwriting this path, no need to unversion
267
271
pending.to_unversion_paths.discard(path)
268
272
# Special case, because the path already exists
273
277
tree.rename_one(from_relpath, to_relpath)
274
278
if pending.to_unversion_paths:
275
279
tree.unversion(pending.to_unversion_paths)
276
tree.add(pending.to_add_files, pending.to_add_file_ids,
277
pending.to_add_kinds)
280
tree.add(pending.to_add_files, pending.to_add_file_ids, pending.to_add_kinds)
278
281
for path, content in viewitems(pending.new_contents):
279
282
tree.put_file_bytes_non_atomic(path, content)