Ad

Using GitPython, How Do I Do Git Submodule Update --init

- 1 answer

My code so far is working doing the following. I'd like to get rid of the subprocess.call() stuff

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'])
Ad

Answer

My short answer: it's convenient and simple.

Full answer follows. Suppose you have your repo variable:

repo = git.Repo(repo_path)

Then, simply do:

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

And you can do all the things with your submodule that you do with your ordinary repo via submodule.module() (which is of type git.Repo) like this:

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

I use such things in my own porcelain over git porcelain that I use to manage some projects.


Also, to do it more directly, you can, instead of using call() or subprocess, just do this:

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

You can print it because the method returns output that you usually get by runnning git submodule update --init (obviously the print() part depends on Python version).

Ad
source: stackoverflow.com
Ad