【问题标题】:git archive --remote command using GitPython使用 GitPython 的 git archive --remote 命令
【发布时间】:2018-12-05 11:08:31
【问题描述】:
如何在 GitPython 中使用命令(git archive --remote)?根据 GitPython 文档,我们可以直接使用 git。我正在做类似的事情:
git = repo.git
git.archive(remote='http://path')
但出现错误
“例外是:Cmd('git') 失败,原因是:退出代码 (1)”
我可以查看任何示例以在 python 脚本中执行 git archive --remote 吗?
谢谢
【问题讨论】:
标签:
git
archive
git-remote
gitpython
git-archive
【解决方案1】:
这个问题很老了,但我遇到了同样的问题,所以这是我的解决方案:
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)
几点说明:
- 此解决方案创建一个临时存储库,获取请求的远程 ref 并将其存档。理想情况下,我们不需要所有这些额外的步骤,但我无法找到更好的解决方案。请提出改进建议!
- 将
path 参数设置为有意义的值,以防您只想包含目录的子集
- 因为我们不需要任何历史记录,所以
fetch() 调用可能会被优化。函数采用的**kwargs 在这里可能会有所帮助(请参阅man git-fetch)