1

I have a working checkout function based on dulwich:

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)]

But how would I amend that to checkout an individual file or directory?

Is there something that could replace the build_index_from_tree line?

meatballs
  • 3,917
  • 1
  • 15
  • 19
  • I've just found ```dulwich.index.build_file_from_blob``` which looks interesting. Just got to work out how to use it.... – meatballs Jan 14 '16 at 13:03

2 Answers2

0

It looks like jelmer may have answered this one previously: http://www.aaronheld.com/post/using-python-dulwich-to-load-any-version-of-a-file-from-a-local-git-repo

meatballs
  • 3,917
  • 1
  • 15
  • 19
0

Inline the contents of build_index_from_tree(), then add an if statement to filter out any entry that does not have an entry.path that starts with your 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)
jelmer
  • 2,405
  • 14
  • 27