Created
February 4, 2010 08:23
-
-
Save motemen/294447 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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