以编程方式`与gul结帐.`与德威 [英] Programmatically `git checkout .` with dulwich
问题描述
从dulwich.objects获得此代码
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
结束历史提交,但创建的文件正在等待删除( git status
这样说)。
I end up with the commit in the history, BUT the created file is pending for deletion (git status
says so).
一个 git checkout。
修正了它。
我的问题是:如何执行 git checkout。
以编程方式使用dulwich?
My question is: how to do git checkout .
programmatically with dulwich?
推荐答案
现在可以自版本0.8.4 ,方法 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)
这篇关于以编程方式`与gul结帐.`与德威的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!