Having this code
from dulwich.objects import Blob, Tree, Commit, parse_timezone
from dulwich.repo import Repo
from time import time
repo = Repo.init("myrepo", mkdir=True)
blob = Blob.from_string("my file content\n")
tree = Tree()
tree.add("spam", 0100644, blob.id)
commit = Commit()
commit.tree = tree.id
author = "Flav <foo@bar.com>"
commit.author = commit.committer = author
commit.commit_time = commit.author_time = int(time())
tz = parse_timezone('+0200')[0]
commit.commit_timezone = commit.author_timezone = tz
commit.encoding = "UTF-8"
commit.message = "initial commit"
o_sto = repo.object_store
o_sto.add_object(blob)
o_sto.add_object(tree)
o_sto.add_object(commit)
repo.refs["HEAD"] = commit.id
I end up with the commit in the history, BUT the created file is pending for deletion (git status
says so).
A git checkout .
fixes it.
My question is: how to do git checkout .
programmatically with dulwich?
It is now possible since release 0.8.4, with the method dulwich.index.build_index_from_tree()
.
It writes a tree to both the index file and the filesystem (working copy), which is a very basic form of checkout.
See the note
existing index is wiped and contents are not merged in a working dir. Suiteable only for fresh clones
I could get it work with the following code
from dulwich import index, repo
#get repository object of current directory
repo = repo.Repo('.')
indexfile = repo.index_path()
#we want to checkout HEAD
tree = repo["HEAD"].tree
index.build_index_from_tree(repo.path, indexfile, repo.object_store, tree)