【问题标题】:How to pull from the remote using dulwich?如何使用德威从遥控器拉出?
【发布时间】:2012-08-15 07:22:55
【问题描述】:

如何在 python dulwich 库中执行git pull 之类的操作。

【问题讨论】:

    标签: python git dulwich


    【解决方案1】:

    我没用过德威,但来自these doc's,可能是这样的:

    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目录下的一个pack文件。而且我只是不知道如何将它合并到主分支中。
    • 听起来 fetch() 应该将包导入到与 repo 相同的分支中。是否可以使用 do_commit() 将其合并到主分支中? stackoverflow.com/questions/6904734/…
    • 试过了,发现我需要先设置参考,比如 local["HEAD"] = remote_refs["refs/heads/master"] - 也看到了:stackoverflow.com/questions/6640546/…
    【解决方案2】:

    完整示例。适用于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'
    猜你喜欢
    • 2014-08-11
    • 2021-04-16
    • 1970-01-01
    • 2013-01-08
    • 2016-02-06
    • 1970-01-01
    • 2014-10-28
    • 2017-08-18
    • 2017-09-20
    相关资源
    最近更新 更多