使用下面的代码
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说是这样)。
git checkout .可以修复它。
我的问题是:如何以编程方式使用dulwich进行git checkout .?
发布于 2012-09-18 02:33:46
现在可以使用dulwich.index.build_index_from_tree()方法进行release 0.8.4了。
它将树同时写入索引文件和文件系统(工作副本),这是一种非常基本的检出形式。
请看笔记
现有索引被擦除,并且内容不会合并到工作目录中。仅适用于新克隆
我可以用下面的代码让它工作
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)发布于 2011-07-10 19:20:25
Git状态显示它被删除是因为工作副本中不存在该文件,这就是为什么签出它会修复该状态的原因。
看起来在dulwich中还没有对高级工作拷贝类和函数的支持。您将不得不处理树和斑点以及拆包对象。
好的,我接受了挑战:我可以对达利奇做一个基本的检查:
#get repository object of current directory
repo = Repo('.')
#get tree corresponding to the head commit
tree_id = repo["HEAD"].tree
#iterate over tree content, giving path and blob sha.
for entry in repo.object_store.iter_tree_contents(tree_id):
path = entry.in_path(repo.path).path
dulwich.file.ensure_dir_exists(os.path.split(path)[0])
with open(path, 'wb') as file:
#write blob's content to file
file.write(repo[entry.sha].as_raw_string()) 它不会删除必须删除的文件,也不会关心你的索引等等。
另请参阅Mark Mikofski's github project以获取基于此的更完整的代码。
发布于 2011-07-10 19:29:42
from dulwich.repo import Repo
repo = Repo.init('myrepo', mkdir=True)
f = open('myrepo/spam', 'w+')
f.write('my file content\n')
f.close()
repo.stage(['spam'])
repo.do_commit('initial commit', 'Flav <foo@bar.com>')通过查看dulwich/tests/test_repository.py:371找到的。dulwich很强大,但不幸的是,文档有点缺乏。
您可能还想考虑改用GitFile。
https://stackoverflow.com/questions/6640546
复制相似问题