gpt4 book ai didi

使用 GitPython 的 git archive --remote 命令

转载 作者:行者123 更新时间:2023-12-03 21:21:57 25 4
gpt4 key购买 nike

如何使用 GitPython 使用命令 (git archive --remote)?根据 GitPython 文档,我们可以直接使用 git。我正在做类似的事情:

git = repo.git
git.archive(远程= ' http://path ')

但得到一个错误
“异常是:Cmd('git') 由于:退出代码(1)而失败”

是否有任何示例可以查看以在 python 脚本中执行 git archive --remote ?

谢谢

最佳答案

这个问题很老了,但我遇到了同样的问题,所以这是我的解决方案:

import git
import shutil

url = 'ssh://url-to.my/repo.git'
remote_ref = 'master'
tmprepo = 'temprepo'
tarball = 'contents.tar'

try:
repo = git.Repo.init(tmprepo)
repo.create_remote('origin', url)
repo.remote().fetch(remote_ref)

with open(tarball, 'wb') as f:
repo.archive(f, f'remotes/origin/{remote_ref}', path=None)
print('Success')
finally:
shutil.rmtree(tmprepo)
一些注意事项:
  • 此解决方案创建一个临时存储库,获取请求的远程引用并将其存档。理想情况下,我们不需要所有这些额外的步骤,但我找不到更好的解决方案。请提出改进​​意见!
  • 设置 path如果您只想包含目录的子集
  • 因为我们不需要任何历史记录,fetch()调用可能可以优化。 **kwargs函数所采用的在这里可能会有所帮助(请参阅 man git-fetch )
  • 关于使用 GitPython 的 git archive --remote 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51047785/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com