chiark / gitweb /
In access_quirk, filter by defined at the end. (nfc)
[dgit.git] / dgit
diff --git a/dgit b/dgit
index 25afa6734dfebd9a692c524954a537e2b847dfcc..57159b70fd62cdba67c3568736bdd594ccd6221f 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -566,9 +566,7 @@ sub cfg {
 
 sub access_basedistro () {
     if (defined $idistro) {
-       return cfg("dgit-distro.basedistro.distro",
-                  "dgit-suite.$isuite.distro",
-                  'RETURN-UNDEF') // $idistro;
+       return $idistro;
     } else {   
        return cfg("dgit-suite.$isuite.distro",
                   "dgit.default.distro");
@@ -576,7 +574,7 @@ sub access_basedistro () {
 }
 
 sub access_quirk () {
-    # returns (quirk name, distro to use instead, quirk-specific info)
+    # returns (quirk name, distro to use instead or undef, quirk-specific info)
     my $basedistro = access_basedistro();
     my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
                              'RETURN-UNDEF');
@@ -590,22 +588,51 @@ sub access_quirk () {
            return ('backports',"$basedistro-backports",$1);
        }
     }
-    return ('none',$basedistro);
+    return ('none',undef);
 }
 
-sub access_distro () {
-    return (access_quirk())[1];
+sub access_distros () {
+    # Returns list of distros to try, in order
+    #
+    # We want to try:
+    #    1. the access_quirk distro, if any
+    #    2a. the user's specified distro, or failing that  } basedistro
+    #    2b. the distro calculated from the suite          }
+    my @l = access_basedistro();
+
+    my (undef,$quirkdistro) = access_quirk();
+    unshift @l, $quirkdistro;
+    return grep { defined } @l;
 }
 
 sub access_cfg (@) {
     my (@keys) = @_;
-    my $basedistro = access_basedistro();
-    my $distro = $idistro || access_distro();
-    my $value = cfg(map {
-       ("dgit-distro.$distro.$_",
-        "dgit-distro.$basedistro.$_",
-        "dgit.default.$_")
-                   } @keys);
+    my @cfgs;
+    # The nesting of these loops determines the search order.  We put
+    # the key loop on the outside so that we search all the distros
+    # for each key, before going on to the next key.  That means that
+    # if access_cfg is called with a more specific, and then a less
+    # specific, key, an earlier distro can override the less specific
+    # without necessarily overriding any more specific keys.  (If the
+    # distro wants to override the more specific keys it can simply do
+    # so; whereas if we did the loop the other way around, it would be
+    # impossible to for an earlier distro to override a less specific
+    # key but not the more specific ones without restating the unknown
+    # values of the more specific keys.
+    my @realkeys;
+    my @rundef;
+    # We have to deal with RETURN-UNDEF specially, so that we don't
+    # terminate the search prematurely.
+    foreach (@keys) {
+       if (m/RETURN-UNDEF/) { push @rundef, $_; last; }
+       push @realkeys, $_
+    }
+    foreach my $d (access_distros()) {
+       push @cfgs, map { "dgit-distro.$d.$_" } @realkeys;
+    }
+    push @cfgs, map { "dgit.default.$_" } @realkeys;
+    push @cfgs, @rundef;
+    my $value = cfg(@cfgs);
     return $value;
 }
 
@@ -627,6 +654,11 @@ sub access_cfg_ssh () {
     }
 }
 
+sub access_runeinfo ($) {
+    my ($info) = @_;
+    return ": dgit ".access_basedistro()." $info ;";
+}
+
 sub access_someuserhost ($) {
     my ($some) = @_;
     my $user = access_cfg("$some-user",'username');
@@ -638,11 +670,14 @@ sub access_gituserhost () {
     return access_someuserhost('git');
 }
 
-sub access_giturl () {
+sub access_giturl (;$) {
+    my ($optional) = @_;
     my $url = access_cfg('git-url','RETURN-UNDEF');
     if (!defined $url) {
+       my $proto = access_cfg('git-proto', 'RETURN-UNDEF');
+       return undef unless defined $proto;
        $url =
-           access_cfg('git-proto').
+           $proto.
            access_gituserhost().
            access_cfg('git-path');
     }
@@ -742,20 +777,21 @@ sub pool_dsc_subpath ($$) {
     return "/pool/$component/$prefix/$package/".dscfn($vsn);
 }
 
-sub archive_query_madison ($$) {
+sub archive_query_madison {
+    return map { [ @$_[0..1] ] } madison_get_parse(@_);
+}
+
+sub madison_get_parse {
     my ($proto,$data) = @_;
     die unless $proto eq 'madison';
     if (!length $data) {
-       $data= access_cfg('madison-distro',access_basedistro());
+       $data= access_cfg('madison-distro','RETURN-UNDEF');
+       $data //= access_basedistro();
     }
-    $rmad{$package} ||= cmdoutput
+    $rmad{$proto,$data,$package} ||= cmdoutput
        qw(rmadison -asource),"-s$isuite","-u$data",$package;
-    my $rmad = $rmad{$package};
-    return madison_parse($rmad);
-}
+    my $rmad = $rmad{$proto,$data,$package};
 
-sub madison_parse ($) {
-    my ($rmad) = @_;
     my @out;
     foreach my $l (split /\n/, $rmad) {
        $l =~ m{^ \s*( [^ \t|]+ )\s* \|
@@ -777,9 +813,9 @@ sub madison_parse ($) {
     return sort { -version_compare($a->[0],$b->[0]); } @out;
 }
 
-sub canonicalise_suite_madison ($$) {
+sub canonicalise_suite_madison {
     # madison canonicalises for us
-    my @r = archive_query_madison($_[0],$_[1]);
+    my @r = madison_get_parse(@_);
     @r or fail
        "unable to canonicalise suite using package $package".
        " which does not appear to exist in suite $isuite;".
@@ -787,8 +823,8 @@ sub canonicalise_suite_madison ($$) {
     return $r[0][2];
 }
 
-sub sshpsql ($$) {
-    my ($data,$sql) = @_;
+sub sshpsql ($$$) {
+    my ($data,$runeinfo,$sql) = @_;
     if (!length $data) {
        $data= access_someuserhost('sshpsql').':'.
            access_cfg('sshpsql-dbname');
@@ -797,7 +833,9 @@ sub sshpsql ($$) {
     my ($userhost,$dbname) = ($`,$'); #';
     my @rows;
     my @cmd = (access_cfg_ssh, $userhost,
-              "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
+              access_runeinfo("ssh-psql $runeinfo").
+              " export LANG=C;".
+              " ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
     printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
     open P, "-|", @cmd or die $!;
     while (<P>) {
@@ -817,13 +855,13 @@ sub sshpsql ($$) {
 }
 
 sub sql_injection_check {
-    foreach (@_) { die "$_ $& ?" if m/[']/; }
+    foreach (@_) { die "$_ $& ?" if m{[^-+=:_.,/0-9a-zA-Z]}; }
 }
 
 sub archive_query_sshpsql ($$) {
     my ($proto,$data) = @_;
     sql_injection_check $isuite, $package;
-    my @rows = sshpsql($data, <<END);
+    my @rows = sshpsql($data, "archive-query $isuite $package", <<END);
         SELECT source.version, component.name, files.filename, files.sha256sum
           FROM source
           JOIN src_associations ON source.id = src_associations.source
@@ -848,7 +886,7 @@ END
 sub canonicalise_suite_sshpsql ($$) {
     my ($proto,$data) = @_;
     sql_injection_check $isuite;
-    my @rows = sshpsql($data, <<END);
+    my @rows = sshpsql($data, "canonicalise-suite $isuite", <<END);
         SELECT suite.codename
           FROM suite where suite_name='$isuite' or codename='$isuite';
 END
@@ -943,6 +981,7 @@ sub check_for_git () {
     if ($how eq 'ssh-cmd') {
        my @cmd =
            (access_cfg_ssh, access_gituserhost(),
+            access_runeinfo("git-check $package").
             " set -e; cd ".access_cfg('git-path').";".
             " if test -d $package.git; then echo 1; else echo 0; fi");
        my $r= cmdoutput @cmd;
@@ -962,6 +1001,7 @@ sub create_remote_git_repo () {
     if ($how eq 'ssh-cmd') {
        runcmd_ordryrun
            (access_cfg_ssh, access_gituserhost(),
+            access_runeinfo("git-create $package").
             "set -e; cd ".access_cfg('git-path').";".
             " cp -a _template $package.git");
     } elsif ($how eq 'true') {
@@ -1326,11 +1366,14 @@ sub clone ($) {
     mkdir $dstdir or die "$dstdir $!";
     changedir $dstdir;
     runcmd @git, qw(init -q);
-    runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
-    open H, "> .git/HEAD" or die $!;
-    print H "ref: ".lref()."\n" or die $!;
-    close H or die $!;
-    runcmd @git, qw(remote add), 'origin', access_giturl();
+    my $giturl = access_giturl(1);
+    if (defined $giturl) {
+       runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
+       open H, "> .git/HEAD" or die $!;
+       print H "ref: ".lref()."\n" or die $!;
+       close H or die $!;
+       runcmd @git, qw(remote add), 'origin', $giturl;
+    }
     if (check_for_git()) {
        progress "fetching existing git history";
        git_fetch_us();