def build_path(repo, path, entries=None, root=None):
    """
    Builds out a tree path, starting with the leaf node, and updating all
    trees up the parent chain, resulting in (potentially) a new OID for the
    root tree.

    If ``entries`` is provided, those entries are inserted (or updated)
    in the tree for the given path.

    If ``root`` is provided, the path will be built based off of that
    tree. Otherwise, it is built off of an empty tree. Accepts an OID or a
    pygit2.Tree object.

    The root tree OID is returned, so that it can be included in a commit
    or stage.
    """
    path = path.strip(os.path.sep)
    if path is not None and path != '':
        parent, name = os.path.split(path)
    else:
        parent, name = None, None

    if root is None:
        # use an empty tree
        root_id = repo.TreeBuilder().write()
        root = repo[root_id]

    if isinstance(root, (basestring, pygit2.Oid)):
        root = repo[root]

    if parent is None:
        # we're at the root tree
        tb_args = (root.oid,)
    else:
        # see if current path exists
        try:
            tree = root[path]
        except __HOLE__:
            tb_args = ()
        else:
            tb_args = (tree.oid,)

    # build tree
    tb = repo.TreeBuilder(*tb_args)

    for entry in entries:
        tb.insert(*entry)
    oid = tb.write()

    if parent is None:
        # we're at the root tree
        return oid

    entry = (name, oid, pygit2.GIT_FILEMODE_TREE)

    if parent == '':
        # parent is the root tree
        return build_path(repo, '', (entry,), root)

    return build_path(repo, parent, (entry,), root)
dataset/ETHPy150Open bendavis78/python-gitmodel/gitmodel/utils/path.py/build_path