chiark / gitweb /
dgit-repos-server: Break out mode_ssh and generalise --ssh option (nfc)
[dgit.git] / infra / dgit-repos-server
index baaf99197198a603b3ecddd101d16bd1f8eb5e56..67a6fc5118348cbb82f865248ecdd37a9866af95 100755 (executable)
@@ -2,10 +2,13 @@
 # dgit-repos-server
 #
 # usages:
-#  .../dgit-repos-server DISTRO SUITES KEYRING-AUTH-SPEC \
-#      DGIT-REPOS-DIR POLICY-HOOK-SCRIPT --ssh
-#  .../dgit-repos-server DISTRO SUITES KEYRING-AUTH-SPEC \
-#      DGIT-REPOS-DIR POLICY-HOOK-SCRIPT --cron
+#   dgit-repos-server DISTRO DISTRO-DIR AUTH-SPEC [<settings>] --ssh
+#   dgit-repos-server DISTRO DISTRO-DIR AUTH-SPEC [<settings>] --cron
+# settings
+#   --repos=GIT-REPOS-DIR      default DISTRO-DIR/repos/
+#   --suites=SUITES-FILE       default DISTRO-DIR/suites
+#   --policy-hook=POLICY-HOOK  default DISTRO-DIR/policy-hook
+# (DISTRO-DIR is not used other than as default)
 # internal usage:
 #  .../dgit-repos-server --pre-receive-hook PACKAGE
 #
@@ -16,7 +19,7 @@
 # SUITES is the name of a file which lists the permissible suites
 # one per line (#-comments and blank lines ignored)
 #
-# KEYRING-AUTH-SPEC is a :-separated list of
+# AUTH-SPEC is a :-separated list of
 #   KEYRING.GPG,AUTH-SPEC
 # where AUTH-SPEC is one of
 #   a
@@ -783,48 +786,18 @@ sub argval () {
     return $v;
 }
 
-sub parseargsdispatch () {
-    die unless @ARGV;
-
-    delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
-    delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
-
-    if ($ENV{'DGIT_DRS_DEBUG'}) {
-       $debug='=';
-       open DEBUG, ">&STDERR" or die $!;
-    }
-
-    if ($ARGV[0] eq '--pre-receive-hook') {
-       if ($debug) { $debug.="="; }
-       shift @ARGV;
-       @ARGV == 1 or die;
-       $package = shift @ARGV;
-       defined($distro = $ENV{'DGIT_DRS_DISTRO'}) or die;
-       defined($dgitrepos = $ENV{'DGIT_DRS_REPOS'}) or die;
-       defined($suitesfile = $ENV{'DGIT_DRS_SUITES'}) or die;
-       defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
-       defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
-       defined($keyrings = $ENV{'DGIT_DRS_KEYRINGS'}) or die $!;
-       defined($policyhook = $ENV{'DGIT_DRS_POLICYHOOK'}) or die $!;
-       open STDOUT, ">&STDERR" or die $!;
-       eval {
-           stunthook();
-       };
-       if ($@) {
-           recorderror "$@" or die;
-           die $@;
-       }
-       exit 0;
-    }
+our %indistrodir = (
+    # keys are used for DGIT_DRS_XXX too
+    'repos' => \$dgitrepos,
+    'suites' => \$suitesfile,
+    'policy-hook' => \$policyhook,
+    );
 
-    $ENV{'DGIT_DRS_DISTRO'} = $distro = argval();
-    $ENV{'DGIT_DRS_SUITES'} = argval();
-    $ENV{'DGIT_DRS_KEYRINGS'} = argval();
-    $ENV{'DGIT_DRS_REPOS'} = $dgitrepos = argval();
-    $ENV{'DGIT_DRS_POLICYHOOK'} = $policyhook = argval();
+our @hookenvs = qw(distro suitesfile policyhook keyrings dgitrepos);
 
-    die unless @ARGV==1 && $ARGV[0] eq '--ssh';
+# workrepo and destrepo handled ad-hoc
 
+sub mode_ssh () {
     my $cmd = $ENV{'SSH_ORIGINAL_COMMAND'};
     $cmd =~ m{
        ^
@@ -867,6 +840,60 @@ sub parseargsdispatch () {
     &$mainfunc;
 }
 
+sub parseargsdispatch () {
+    die unless @ARGV;
+
+    delete $ENV{'GIT_DIR'}; # if not run via ssh, our parent git process
+    delete $ENV{'GIT_PREFIX'}; # sets these and they mess things up
+
+    if ($ENV{'DGIT_DRS_DEBUG'}) {
+       $debug='=';
+       open DEBUG, ">&STDERR" or die $!;
+    }
+
+    if ($ARGV[0] eq '--pre-receive-hook') {
+       if ($debug) { $debug.="="; }
+       shift @ARGV;
+       @ARGV == 1 or die;
+       $package = shift @ARGV;
+       ${ $main::{$_} } = $ENV{"DGIT_DRS_\U$_"} foreach @hookenvs;
+       defined($workrepo = $ENV{'DGIT_DRS_WORK'}) or die;
+       defined($destrepo = $ENV{'DGIT_DRS_DEST'}) or die;
+       open STDOUT, ">&STDERR" or die $!;
+       eval {
+           stunthook();
+       };
+       if ($@) {
+           recorderror "$@" or die;
+           die $@;
+       }
+       exit 0;
+    }
+
+    $distro = $ENV{'DGIT_DRS_DISTRO'}     = argval();
+    my $distrodir                         = argval();
+    $keyrings = $ENV{'DGIT_DRS_KEYRINGS'} = argval();
+
+    foreach my $dk (keys %indistrodir) {
+       ${ $indistrodir{$dk} } = "$distrodir/$dk";
+    }
+
+    while (@ARGV && $ARGV[0] =~ m/^--([-0-9a-z]+)=/ && $indistrodir{$1}) {
+       ${ $indistrodir{$1} } = $'; #';
+       shift @ARGV;
+    }
+
+    $ENV{"DGIT_DRS_\U$_"} = ${ $main::{$_} } foreach @hookenvs;
+
+    die unless @ARGV==1;
+
+    my $mode = shift @ARGV;
+    die unless $mode =~ m/^--(\w+)$/;
+    my $fn = ${*::}{"mode_$1"};
+    die unless $fn;
+    $fn->();
+}
+
 sub unlockall () {
     while (my $fh = pop @lockfhs) { close $fh; }
 }