使用GitPython执行git archive --remote命令

4

我如何使用GitPython中的命令(git archive --remote)?根据GitPython文档,我们可以直接使用git。我的操作如下:

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

但是会出现错误 "Exception is : Cmd('git') failed due to: exit code(1)"

有没有可以参考的示例来在Python脚本中执行git archive --remote命令?

谢谢

1个回答

0
这个问题很旧了,但我遇到了同样的问题,所以这是我的解决方案:
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

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