1

我有一个基于德威的工作结帐功能:

def checkout(repo, ref=None):
    if ref is None:
        ref = repo.head()
    index = repo.index_path()
    tree_id = repo[ref].tree
    build_index_from_tree(repo.path, index, repo.object_store, tree_id)
    return [repo.object_store.iter_tree_contents(tree_id)]

但是我将如何修改它以签出单个文件或目录?

有什么东西可以代替这build_index_from_tree条线吗?

4

2 回答 2

0

内联 build_index_from_tree() 的内容,然后添加 if 语句以过滤掉任何没有以您的路径开头的 entry.path 的条目:

if subpath[-1] != "/":
    subpath += "/"

if not isinstance(root_path, bytes):
    root_path = root_path.encode(sys.getfilesystemencoding())

for entry in object_store.iter_tree_contents(tree_id):
    if not validate_path(entry.path, validate_path_element_default):
        continue
    # new lines added here:
    if not entry.path.startswith(subpath):
        continue
    full_path = _tree_to_fs_path(root_path, entry.path[len(subpath):])

    if not os.path.exists(os.path.dirname(full_path)):
        os.makedirs(os.path.dirname(full_path))

    obj = object_store[entry.sha]
    build_file_from_blob(obj, entry.mode, full_path)
于 2016-01-14T15:07:33.880 回答
0

看起来 jelmer 之前可能已经回答过这个问题:http ://www.aaronheld.com/post/using-python-dulwich-to-load-any-version-of-a-file-from-a-local-git -回购

于 2016-01-14T15:03:03.590 回答