Skip to content

Instantly share code, notes, and snippets.

@chrisbliss18
Last active April 27, 2016 15:50
Show Gist options
  • Save chrisbliss18/4531354 to your computer and use it in GitHub Desktop.
Save chrisbliss18/4531354 to your computer and use it in GitHub Desktop.
Recursively init and update submodules in a Git repo.
#!/usr/bin/perl
use strict;
use Cwd;
init_and_update();
exit;
sub init_and_update
{
my $start_path = cwd();
my %paths;
my $updated;
do
{
my $data = `find . -name '.gitmodules'`;
chomp($data);
$data =~ s/\/\.gitmodules//g;
foreach my $path (split(/\n/, $data))
{
if($paths{$path} eq '')
{
$paths{$path} = '';
}
}
$updated = 0;
foreach my $path (sort keys %paths)
{
if($paths{$path} eq '')
{
chdir($path);
`git submodule init 2>&1`;
`git submodule update 2>&1`;
`git submodule foreach 'git checkout master' 2>&1`;
`git submodule foreach 'git pull' 2>&1`;
chdir($start_path);
if($ARGV[0] eq '--remove-gitmodules')
{
unlink("$path/.gitmodules");
}
$paths{$path} = 1;
$updated++;
}
}
} while($updated);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment