Skip to content

Instantly share code, notes, and snippets.

@motemen
Created February 4, 2010 08:23
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motemen/294447 to your computer and use it in GitHub Desktop.
Save motemen/294447 to your computer and use it in GitHub Desktop.
#!/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