Skip to content

Commit

Permalink
Change update-submodules.pl to not use the shell at all
Browse files Browse the repository at this point in the history
This should make this more platform agnostic and a bit safer.
As a side effect of this the detailed error reporting that checked the
STDERR and reported based on that is now gone, because it's not easily
possible in perl to capture STDERR without using backticks.
  • Loading branch information
PatZim committed Mar 7, 2020
1 parent 9f4a0f1 commit 0ae8b4e
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions tools/build/update-submodules.pl
Expand Up @@ -8,8 +8,6 @@
use Cwd;
use File::Spec;

my $msg;

my $repo = shift @ARGV;
chdir $repo;

Expand All @@ -21,11 +19,11 @@

print 'Updating submodules .................................... ';

exec_and_check('git submodule sync --quiet 2>&1', 'Submodule sync failed for an unknown reason.');
exec_and_check('git submodule --quiet init 2>&1', 'Submodule init failed for an unknown reason.');
exec_and_check('git', 'submodule', 'sync', '--quiet', 'Submodule sync failed for an unknown reason.');
exec_and_check('git', 'submodule', '--quiet', 'init', 'Submodule init failed for an unknown reason.');

if ($git_cache_dir) {
my $out = qx{git submodule status 2>&1};
my $out = exec_with_output('git', 'submodule', 'status');
if ($? >> 8 != 0) {
print "\n===SORRY=== ERROR: Submodule status failed for an unknown reason.\n";
print "The error message was: $out\n";
Expand All @@ -41,60 +39,51 @@
my $smodpath = $1;
my $smodname = (File::Spec->splitdir($smodpath))[-1];
my $modrefdir = File::Spec->catdir($git_cache_dir, $smodname);
my $url = qx{git config submodule.$smodpath.url 2>&1};
my $url = exec_with_output('git', 'config', "submodule.$smodpath.url");
chomp $url;
if (!$url) {
print "Couldn't retrieve submodule URL for submodule $smodname\n";
exit 1;
}
if (!-e $modrefdir) {
exec_and_check("git clone --quiet --bare $url \"$modrefdir\"", "Git clone of $url failed.");
exec_and_check('git', 'clone', '--quiet', '--bare', $url, $modrefdir, "Git clone of $url failed.");
}
else {
my $back = Cwd::cwd();
chdir $modrefdir;
exec_and_check('git fetch --quiet --all', "Git fetch in $modrefdir failed.");
exec_and_check('git', 'fetch', '--quiet', '--all', "Git fetch in $modrefdir failed.");
chdir $back;
}
$msg = qx{git submodule --quiet update --reference "$modrefdir" "$smodpath" 2>&1};
check_update_ok($?, $msg);
exec_and_check('git', 'submodule', '--quiet', 'update', '--reference', $modrefdir, $smodpath);
}
}
else {
$msg = qx{git submodule --quiet update 2>&1};
check_update_ok($?, $msg);
exec_and_check('git', 'submodule', '--quiet', 'update');
}

print "OK\n";


# Helper subs.

sub exec_and_check {
my ($command, $msg) = @_;
my $out = qx{$command};
if ($? >> 8 != 0) {
print "\n===SORRY=== ERROR: $msg\n";
print "The error message was: $out\n";
exit 1;
sub exec_with_output {
my @command = @_;
open(my $handle, '-|', @command);
my $out;
while(<$handle>) {
$out .= $_;
}
close $handle;
return $out;
}

sub check_update_ok {
my ($code, $msg) = @_;
if ($code >> 8 != 0) {
if ( $msg =~
/[']([^']+)[']\s+already exists and is not an empty/ )
{
print "\n===SORRY=== ERROR: "
. "Cannot update submodule because directory exists and is not empty.\n"
. ">>> Please delete the following folder and try again:\n$1\n\n";
}
else {
print "\n===SORRY=== ERROR: "
. "Updating the submodule failed for an unknown reason. The error message was:\n"
. $msg;
}
sub exec_and_check {
my $msg = pop;
my @command = @_;
my $out = exec_with_output(@command);
if ($? >> 8 != 0) {
print "\n===SORRY=== ERROR: $msg\n";
print "The programs output was: $out\n";
exit 1;
}
}
Expand Down

0 comments on commit 0ae8b4e

Please sign in to comment.