Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GitPython, how do I do git submodule update --init

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'])
like image 495
Andrew Avatar asked Apr 08 '14 22:04

Andrew


People also ask

What does git submodule update -- init do?

The git submodule init command creates the local configuration file for the submodules, if this configuration does not exist. If you track branches in your submodules, you can update them via the --remote parameter of the git submodule update command.

How do I run a git submodule init?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

What is git submodule update -- init -- recursive?

git submodule update The command clones the missing submodules, fetches any new remote commits, and updates the directory tree. Adding the --init flag to the command eliminates the need to run git submodule init . The --recursive option tells Git to check the submodules for nested submodules and update them as well.


1 Answers

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

like image 175
mbdevpl Avatar answered Sep 27 '22 19:09

mbdevpl