python - 使用 GitPython,我该怎么做 git submodule update --init

标签 python gitpython

到目前为止,我的代码正在执行以下操作。我想摆脱 subprocess.call() 的东西

import git
from subprocess import call

repo = git.Repo(repo_path)
repo.remotes.origin.fetch(prune=True)
repo.head.reset(commit='origin/master', index=True, working_tree=True)

# I don't know how to do this using GitPython yet.
os.chdir(repo_path)
call(['git', 'submodule', 'update', '--init'])

最佳答案

我的简短回答:它既方便又简单。

完整答案如下。假设你有你的 repo 变量:

repo = git.Repo(repo_path)

然后,简单地做:

for submodule in repo.submodules:
    submodule.update(init=True)

您可以通过 submodule.module()(属于 git.Repo 类型)对您的子模块执行您对普通存储库执行的所有操作,例如这个:

sub_repo = submodule.module()
sub_repo.git.checkout('devel')
sub_repo.git.remote('maybeorigin').fetch()

我在自己的瓷器中使用这些东西,而不是 git 瓷器,我用来管理一些项目。


此外,为了更直接地做到这一点,您可以不使用 call()subprocess,而是这样做:

repo = git.Repo(repo_path)
output = repo.git.submodule('update', '--init')
print(output)

您可以打印它,因为该方法返回通常通过运行 git submodule update --init(显然是 print() 部分 depends on Python version)获得的输出。

关于python - 使用 GitPython,我该怎么做 git submodule update --init,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22949068/

相关文章:

python - Linkedin Luminol 异常检测和关联库的工作示例

python - python(pycrypto) 和 nodejs(crypto) 之间的加密 (aes-128-cbc) 不匹配

python - 使用 Python 套接字将数据作为实例发送

python - 如何使用python将NPZ文件转换为文本文件

python - GitPython 如何克隆所有分支?

python - Git Python 找不到提交

python - libpng 警告 : interlace handling should be turned on when using png_read_image in Python/PyGame

python - 使用 GitPython 检查合并是否存在冲突

Python 通过 SSH 身份验证克隆私有(private) GitHub 存储库,无需访问 ssh 二进制文件(在 Azure Functions 下)

python - 使用 GitPython 从远程存储库中克隆所有分支的存储库