chiark / gitweb /
With -DDDD, print out all gitcfg references (copious!)
[dgit.git] / dgit
diff --git a/dgit b/dgit
index f9a67ce4ff7939a9c00e0b05e49a2e0c6dd4c26f..56cee1140814185072007d6acf2f2e9336b9f6dd 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -98,7 +98,11 @@ our %opts_opt_map = ('dget' => \@dget, # accept for compatibility
                      'mergechanges' => \@mergechanges);
 
 our %opts_opt_cmdonly = ('gpg' => 1);
-our %opts_opt_cmdline_opts;
+our %opts_cfg_insertpos = map {
+    $_,
+    scalar @{ $opts_opt_map{$_} }
+} keys %opts_opt_map;
+
 sub finalise_opts_opts();
 
 our $keyid;
@@ -499,28 +503,36 @@ our %defcfg = ('dgit.default.distro' => 'debian',
               'dgit-distro.test-dummy.upload-host' => 'test-dummy',
                );
 
-sub git_get_config ($) {
-    my ($c) = @_;
+our %gitcfg;
 
-    our %git_get_config_memo;
-    if (exists $git_get_config_memo{$c}) {
-       return $git_get_config_memo{$c};
-    }
+sub git_slurp_config () {
+    local ($debuglevel) = $debuglevel-2;
+    local $/="\0";
 
-    my $v;
-    my @cmd = (@git, qw(config --), $c);
-    {
-       local ($debuglevel) = $debuglevel-2;
-       $v = cmdoutput_errok @cmd;
-    };
-    if ($?==0) {
-    } elsif ($?==256) {
-       $v = undef;
-    } else {
-       failedcmd @cmd;
+    my @cmd = (@git, qw(config -z --get-regexp .*));
+    debugcmd "|",@cmd;
+
+    open GITS, "-|", @cmd or failedcmd @cmd;
+    while (<GITS>) {
+       chomp or die;
+       printdebug "=> ", (messagequote $_), "\n";
+       m/\n/ or die "$_ ?";
+       push @{ $gitcfg{$`} }, $'; #';
     }
-    $git_get_config_memo{$c} = $v;
-    return $v;
+    $!=0; $?=0;
+    close GITS
+       or ($!==0 && $?==256)
+       or failedcmd @cmd;
+}
+
+sub git_get_config ($) {
+    my ($c) = @_;
+    my $l = $gitcfg{$c};
+    printdebug"C $c ".(defined $l ? messagequote "'$l'" : "undef")."\n"
+       if $debuglevel >= 4;
+    $l or return undef;
+    @$l==1 or badcfg "multiple values for $c" if @$l > 1;
+    return $l->[0];
 }
 
 sub cfg {
@@ -3056,7 +3068,7 @@ sub parseopts () {
                     !$opts_opt_cmdonly{$1} &&
                     ($om = $opts_opt_map{$1})) {
                push @ropts, $_;
-               push @{ $opts_opt_cmdline_opts{$1} }, $2;
+               push @$om, $2;
            } elsif (m/^--existing-package=(.*)/s) {
                push @ropts, $_;
                $existing_package = $1;
@@ -3175,31 +3187,28 @@ sub finalise_opts_opts () {
        }
 
        foreach my $c (access_cfg_cfgs("opts-$k")) {
-           local ($debuglevel) = $debuglevel-2;
-           my @cmd = (@git, qw(config -z --get-all), $c);
-           my $vs = cmdoutput_errok @cmd;
-           if ($?==0) {
-               badcfg "cannot configure options for $k"
-                   if $opts_opt_cmdonly{$k};
-               push @$om, split /\0/, $vs;
-           } elsif ($?==256) {
-               die "$k $c ?" if length $vs;
-           } else {
-               failedcmd @cmd;
-           }
+           my $vl = $gitcfg{$c};
+           printdebug "CL $c ",
+               ($vl ? join " ", map { shellquote } @$vl : ""),
+               "\n" if $debuglevel >= 4;
+           next unless $vl;
+           badcfg "cannot configure options for $k"
+               if $opts_opt_cmdonly{$k};
+           my $insertpos = $opts_cfg_insertpos{$k};
+           @$om = ( @$om[0..$insertpos-1],
+                    @$vl,
+                    @$om[$insertpos..$#$om] );
        }
     }
-
-    foreach my $k (keys %opts_opt_cmdline_opts) {
-       push @{ $opts_opt_map{$k} }, @{ $opts_opt_cmdline_opts{$k} };
-    }
 }
 
 if ($ENV{$fakeeditorenv}) {
+    git_slurp_config();
     quilt_fixup_editor();
 }
 
 parseopts();
+git_slurp_config();
 
 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"