~bzr/ubuntu/maverick/dulwich/bzr-ppa

« back to all changes in this revision

Viewing changes to dulwich/tests/utils.py

  • Committer: Jelmer Vernooij
  • Date: 2011-08-07 13:03:32 UTC
  • mfrom: (400.37.21)
  • Revision ID: git-v1:99161d0e7e8c238311b68f95c0232ba1fdf6fe8f
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import time
29
29
import types
30
30
 
 
31
from dulwich.index import (
 
32
    commit_tree,
 
33
    )
31
34
from dulwich.objects import (
32
35
    FixedSha,
33
36
    Commit,
47
50
    TestSkipped,
48
51
    )
49
52
 
 
53
# Plain files are very frequently used in tests, so let the mode be very short.
 
54
F = 0100644  # Shorthand mode for Files.
 
55
 
50
56
 
51
57
def open_repo(name):
52
58
    """Open a copy of a repo in a temporary directory.
230
236
    sf.write_sha()
231
237
    f.seek(0)
232
238
    return expected
 
239
 
 
240
 
 
241
def build_commit_graph(object_store, commit_spec, trees=None, attrs=None):
 
242
    """Build a commit graph from a concise specification.
 
243
 
 
244
    Sample usage:
 
245
    >>> c1, c2, c3 = build_commit_graph(store, [[1], [2, 1], [3, 1, 2]])
 
246
    >>> store[store[c3].parents[0]] == c1
 
247
    True
 
248
    >>> store[store[c3].parents[1]] == c2
 
249
    True
 
250
 
 
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.
 
253
 
 
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
 
264
        mode (0100644).
 
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.
 
269
    """
 
270
    if trees is None:
 
271
        trees = {}
 
272
    if attrs is None:
 
273
        attrs = {}
 
274
    commit_time = 0
 
275
    nums = {}
 
276
    commits = []
 
277
 
 
278
    for commit in commit_spec:
 
279
        commit_num = commit[0]
 
280
        try:
 
281
            parent_ids = [nums[pn] for pn in commit[1:]]
 
282
        except KeyError, e:
 
283
            missing_parent, = e.args
 
284
            raise ValueError('Unknown parent %i' % missing_parent)
 
285
 
 
286
        blobs = []
 
287
        for entry in trees.get(commit_num, []):
 
288
            if len(entry) == 2:
 
289
                path, blob = entry
 
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)
 
295
 
 
296
        commit_attrs = {
 
297
            'message': 'Commit %i' % commit_num,
 
298
            'parents': parent_ids,
 
299
            'tree': tree_id,
 
300
            'commit_time': commit_time,
 
301
            }
 
302
        commit_attrs.update(attrs.get(commit_num, {}))
 
303
        commit_obj = make_commit(**commit_attrs)
 
304
 
 
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)
 
311
 
 
312
    return commits