Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw(any);
sub git;
chdir git('rev-parse --git-dir') . '/..';
my @submodules = map [split /\s+/]->[-1], grep /^160000 /, git 'ls-files --stage';
foreach my $submodule (@submodules) {
if (@ARGV) {
next unless any { $submodule eq $_ } @ARGV;
}
print "Upgrading $submodule... ";
my $branch = git "config --file .gitmodules submodule.$submodule.track";
unless ($branch) {
print "tracking branch not found; skip.\n";
next;
}
print "checkout $branch.\n";
if (my ($remote, $branch) = split m</>, $branch) {
system "cd $submodule; git fetch origin && ((git rev-parse --verify $branch > /dev/null 2> /dev/null && git checkout $branch) || git checkout -b $branch $remote/$branch) && git pull";
} else {
system "cd $submodule; git fetch origin && git checkout $branch";
}
}
sub git {
my $command = join ' ', 'git', @_;
my $result = `$command`;
return split /\n/, $result if wantarray;
chomp $result;
$result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment