Using GitPython, how do I do git submodule update --init
Asked Answered
P

2

9

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'])
Pathos answered 8/4, 2014 at 22:1 Comment(0)
F
26

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).

Fascia answered 4/6, 2015 at 12:48 Comment(0)
M
-2

Short answer: You can’t.

Full answer: You can’t, and there is also no point. GitPython is not a complete implementation of the whole Git. It just provides a high-level interface to some common things. While a few operations are implemented directly in Python, a lot calls actually use the Git command line interface to process stuff.

Your fetch line for example does this. Under the hood, there is some trick used to make some calls look like Python although they call the Git executable to process the result—using subprocess as well.

So you could try to figure out how to use the git cmd interface GitPython offers works to support those calls (you can access the instance of that cmd handler using repo.git), or you just continue using the “boring” subprocess calls directly.

Mcquoid answered 8/4, 2014 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.