使用dulwich以编程方式“git checkout .”

2023-12-02

有了这个代码

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 <[email protected]>"
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这么说)。

A git checkout .修复它。

我的问题是:怎么办git checkout .以编程方式与德威?


Git 状态显示它已被删除,因为该文件不存在于工作副本中,这就是检查它可以修复状态的原因。

看起来德威奇还不支持高级工作副本类和函数。你必须处理树木和斑点并拆开物体。

好的,接受挑战:我可以向德威进行基本结账:

#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 的 github 项目基于此的更完整的代码。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用dulwich以编程方式“git checkout .” 的相关文章

随机推荐