Quotes needed for later customisation as a path
[buildfarm-client.git] / run_branches
index e837aa92727c71e75f0362c88cb2e3356888562d..1ef72e544fed48d52646825e0f04ae0167c0b15b 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
 
 =comment
 
@@ -12,21 +12,38 @@ use vars qw($VERSION); $VERSION = 'REL_0.1';
 
 use strict;
 use warnings;
+use 5.010;
+
 use Fcntl qw(:flock :seek);
-use EximBuild::Options;
 use File::Basename;
+use FindBin qw($RealBin);
+use Cwd;
+
+use lib $RealBin;
+use EximBuild::Options;
+
+sub branch_last_sort;
 
+# Complain on old-style use (.pl extension), but only if a
+# terminal is available.
 if ($0 =~ /(.*)\.pl$/) {
     die "$0: Please use `@{[join ' ' => $1, @ARGV]}' instead.\n"
         if -t;
     exec $1, @ARGV;
 }
 
-my %branch_last;
-sub branch_last_sort;
+# Most of the client code assumes that our working directory
+# is the client code directory.
+my %CALLED = (
+    cwd => cwd(),
+    argv0 => $0,
+    argv => [@ARGV],   # get a copy!
+);
+chdir $RealBin or die "$0: Can't chdir to '$RealBin': $!\n";
+#say "Changed working directory to '$RealBin'" if -t;
 
-my $run_build;
-($run_build = $0) =~ s/run_branches/run_build/;
+my %branch_last;
+my $run_build = './run_build';
 
 my($run_all, $run_one);
 my %extra_options =(
@@ -41,14 +58,14 @@ EximBuild::Options::fetch_options(%extra_options);
 die("$0: non-option arguments not permitted")
   if @ARGV;
 
-die "only one of --run-all and --run-one permitted"
+die "$0: only one of --run-all and --run-one permitted"
   if ($run_all && $run_one);
 
-die "need one of --run-all and --run-one"
+die "$0: need one of --run-all and --run-one"
   unless ($run_all || $run_one);
 
 # common mistake
-die "need group searchable homedir"
+die "$0: need group searchable homedir"
   unless (stat($ENV{HOME}) & 0550 == 0550);
 
 # set up a "branch" variable for processing the config file
@@ -60,6 +77,48 @@ $branch = 'global';
 #
 require $buildconf;
 
+# Check if auto-update is wanted and possible
+eval {
+    if (not exists $EximBuild::Conf{auto_update} or $EximBuild::Conf{auto_update})
+    {
+       my $remote = $EximBuild::Conf{auto_update} // 'origin';
+
+       die "auto-update not possible: need write permissions in @{[cwd]}\n"
+           if not -w '.';
+
+       # Get information about our remote and calculate the chance for a
+       # successfull auto-updat. Based on:
+       # http://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git
+       system("git fetch $remote") == 0 or die "'git fetch $remote' failed\n";
+       my ($upstream, $local, $base) = qx'git rev-parse ...@{upstream}' or die "'git rev-parse' failed\n";
+
+       $base =~ s/^\^//;
+
+       if ($upstream ne $local) {
+
+           if ($base ne $local) {
+               die "the merge base is not local anymore. Refusing to `git pull`\n"
+           }
+
+           # if we're the merge base, the ff-only should work
+           # except if there are local changes. We won't stop, if we
+           # fail to update, but we'll issue a warning
+           system("git pull --ff-only $remote") == 0 or die "git pull --ff-only\n";
+
+           say "re-execute after update";
+           chdir $CALLED{cwd} or die "Can't chdir to $CALLED{cwd}: $!\n";
+           exec $CALLED{argv0}, @{$CALLED{argv}};
+           die "Can't re-exec\n";
+       }
+    }
+};
+
+if ($@) {
+    chomp $@;
+    warn "Automatic updated failed with `$@'\n"
+       ."Continue anyway\n";
+}
+
 unless (
     (
         ref $EximBuild::conf{branches_to_build} eq 'ARRAY'
@@ -86,13 +145,11 @@ elsif ($EximBuild::conf{branches_to_build} =~
     # i.e. specially *not* the MinGW SDK perl that is invoked for the
     # build script, which means we need to put the path back the way it was
     # when we're done
-    my $save_path = $ENV{PATH};
-    $ENV{PATH} = $EximBuild::conf{build_env}->{PATH}
+    local $ENV{PATH} = $EximBuild::conf{build_env}->{PATH}
       if ($EximBuild::conf{build_env}->{PATH});
     (my $url = $EximBuild::conf{target}) =~s/cgi-bin.*/branches_of_interest.txt/;
     my $branches_of_interest = `perl -MLWP::Simple -e "getprint(q{$url})"`;
     die "getting branches of interest" unless $branches_of_interest;
-    $ENV{PATH} = $save_path;
     push(@branches,$_)foreach (split(/\s+/,$branches_of_interest));
     #splice(@branches,0,-2)
     #  if $EximBuild::conf{branches_to_build} eq 'HEAD_PLUS_LATEST';
@@ -102,15 +159,12 @@ elsif ($EximBuild::conf{branches_to_build} =~
 
 @branches = apply_throttle(@branches);
 
-my $global_lock_dir =
-    $EximBuild::conf{global_lock_dir}
-  ||$EximBuild::conf{build_root}
-  ||'';
+my $global_lock_dir = $EximBuild::conf{global_lock_dir}
+  // $EximBuild::conf{build_root}
+  // die "$0: need global_lock_dir\n";
 
-unless ($global_lock_dir && -d $global_lock_dir)
-{
-    die "no global lock directory: $global_lock_dir";
-}
+die "$0: need r/w permissions for directory '$global_lock_dir'\n"
+    if not -d -w $global_lock_dir;
 
 # acquire the lock