到目前为止,我的代码正在执行以下操作。我想摆脱 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/