如何使用Dulwich从远程拉取代码?

6
如何在Python的Dulwich库中执行类似于“git pull”的操作。
2个回答

6

我没有使用过dulwich,但根据这些文档,可能需要执行以下操作:

from dulwich.repo import Repo
from dulwich.client import HttpGitClient
local = Repo.init("local", mkdir=True)
client = HttpGitClient('http://github.com/adammorris/')
remote_refs = client.fetch("history.js.git",local)
local["HEAD"] = remote_refs["refs/heads/master"]

目前为止,它没有加载文件,但我可以从本地路径执行“git checkout”,并更新了文件。

此外,还看到了以下内容:


是的,fetch函数将在.git目录下拉取一个包文件。我只是不知道如何将其合并到主分支中。 - Determinant
听起来fetch()应该将包导入到与repo相同的分支中。是否可以使用do_commit()将其合并到主分支中?https://dev59.com/Q1nUa4cB1Zd3GeqPeNOY - Adam Morris
尝试了一下,发现我需要先设置引用,例如local["HEAD"] = remote_refs["refs/heads/master"] - 还看到了这个:https://dev59.com/wlnUa4cB1Zd3GeqPXBXr - Adam Morris

3

完整示例。与Bitbucket一起使用。

from dulwich import index
from dulwich.client import HttpGitClient
from dulwich.repo import Repo

local_repo = Repo.init(LOCAL_FOLDER, mkdir=True)
remote_repo = HttpGitClient(REMOTE_URL, username=USERNAME, password=PASSWORD)
remote_refs = remote_repo.fetch(REMOTE_URL, local_repo)
local_repo[b"HEAD"] = remote_refs[b"refs/heads/master"]

index_file = local_repo.index_path()
tree = local_repo[b"HEAD"].tree
index.build_index_from_tree(local_repo.path, index_file, local_repo.object_store, tree)

请使用您的数据替换LOCAL_FOLDER,REMOTE_URL,USERNAME,PASSWORD。

如果远程仓库未初始化,remote_refs[b"refs/heads/master"]会引发KeyError: b'refs/heads/master'异常。 - CS QGB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接