241
def build_commit_graph(object_store, commit_spec, trees=None, attrs=None):
242
"""Build a commit graph from a concise specification.
245
>>> c1, c2, c3 = build_commit_graph(store, [[1], [2, 1], [3, 1, 2]])
246
>>> store[store[c3].parents[0]] == c1
248
>>> store[store[c3].parents[1]] == c2
251
If not otherwise specified, commits will refer to the empty tree and have
252
commit times increasing in the same order as the commit spec.
254
:param object_store: An ObjectStore to commit objects to.
255
:param commit_spec: An iterable of iterables of ints defining the commit
256
graph. Each entry defines one commit, and entries must be in topological
257
order. The first element of each entry is a commit number, and the
258
remaining elements are its parents. The commit numbers are only
259
meaningful for the call to make_commits; since real commit objects are
260
created, they will get created with real, opaque SHAs.
261
:param trees: An optional dict of commit number -> tree spec for building
262
trees for commits. The tree spec is an iterable of (path, blob, mode) or
263
(path, blob) entries; if mode is omitted, it defaults to the normal file
265
:param attrs: A dict of commit number -> (dict of attribute -> value) for
266
assigning additional values to the commits.
267
:return: The list of commit objects created.
268
:raise ValueError: If an undefined commit identifier is listed as a parent.
278
for commit in commit_spec:
279
commit_num = commit[0]
281
parent_ids = [nums[pn] for pn in commit[1:]]
283
missing_parent, = e.args
284
raise ValueError('Unknown parent %i' % missing_parent)
287
for entry in trees.get(commit_num, []):
290
entry = (path, blob, F)
291
path, blob, mode = entry
292
blobs.append((path, blob.id, mode))
293
object_store.add_object(blob)
294
tree_id = commit_tree(object_store, blobs)
297
'message': 'Commit %i' % commit_num,
298
'parents': parent_ids,
300
'commit_time': commit_time,
302
commit_attrs.update(attrs.get(commit_num, {}))
303
commit_obj = make_commit(**commit_attrs)
305
# By default, increment the time by a lot. Out-of-order commits should
306
# be closer together than this because their main cause is clock skew.
307
commit_time = commit_attrs['commit_time'] + 100
308
nums[commit_num] = commit_obj.id
309
object_store.add_object(commit_obj)
310
commits.append(commit_obj)