chiark / gitweb /
Move a $debug check into printdebug (nfc)
[dgit.git] / dgit
diff --git a/dgit b/dgit
index 68d8b42d1da15ee921157f67f70aa7b793308f6a..e99df73b161280fc5396431fcc4d63592f8c754f 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -18,6 +18,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 use strict;
+$SIG{__WARN__} = sub { die $_[0]; };
 
 use IO::Handle;
 use Data::Dumper;
@@ -98,9 +99,6 @@ our %opts_opt_cmdonly = ('gpg' => 1);
 
 our $keyid;
 
-our $debug = 0;
-open DEBUG, ">/dev/null" or die $!;
-
 autoflush STDOUT 1;
 
 our $remotename = 'dgit';
@@ -131,7 +129,7 @@ sub dscfn ($) {
 }
 
 our $us = 'dgit';
-our $debugprefix = '';
+initdebug('');
 
 our @end;
 END { 
@@ -159,8 +157,6 @@ sub waitstatusmsg () {
     }
 }
 
-sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
-
 sub fail { 
     my $s = "@_\n";
     my $prefix = $us.($we_are_responder ? " (build host)" : "").": ";
@@ -378,29 +374,6 @@ sub url_get {
 
 our ($dscdata,$dscurl,$dsc,$dsc_checked,$skew_warning_vsn);
 
-sub shellquote {
-    my @out;
-    local $_;
-    foreach my $a (@_) {
-       $_ = $a;
-       if (m{[^-=_./0-9a-z]}i) {
-           s{['\\]}{'\\$&'}g;
-           push @out, "'$_'";
-       } else {
-           push @out, $_;
-       }
-    }
-    return join ' ', @out;
-}
-
-sub printcmd {
-    my $fh = shift @_;
-    my $intro = shift @_;
-    print $fh $intro," " or die $!;
-    print $fh shellquote @_ or die $!;
-    print $fh "\n" or die $!;
-}
-
 sub failedcmd {
     { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
     if ($!) {
@@ -413,7 +386,7 @@ sub failedcmd {
 }
 
 sub runcmd {
-    printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
+    debugcmd "+",@_;
     $!=0; $?=0;
     failedcmd @_ if system @_;
 }
@@ -431,13 +404,13 @@ sub printdone {
 
 sub cmdoutput_errok {
     die Dumper(\@_)." ?" if grep { !defined } @_;
-    printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
+    debugcmd "|",@_;
     open P, "-|", @_ or die $!;
     my $d;
     $!=0; $?=0;
     { local $/ = undef; $d = <P>; }
     die $! if P->error;
-    if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
+    if (!close P) { printdebug "=>!$?\n"; return undef; }
     chomp $d;
     $d =~ m/^.*/;
     printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
@@ -558,7 +531,8 @@ sub cfg {
        my @cmd = (@git, qw(config --), $c);
        my $v;
        {
-           local ($debug) = $debug-1;
+           local ($Debian::Dgit::debug) = $debug-1;
+           *debug = *Debian::Dgit::debug; # nnng
            $v = cmdoutput_errok @cmd;
        };
        if ($?==0) {
@@ -937,7 +911,7 @@ sub sshpsql ($$$) {
               access_runeinfo("ssh-psql $runeinfo").
               " export LC_MESSAGES=C; export LC_CTYPE=C;".
               " ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
-    printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
+    debugcmd "|",@cmd;
     open P, "-|", @cmd or die $!;
     while (<P>) {
        chomp or die;
@@ -1529,7 +1503,7 @@ sub pull () {
 sub check_not_dirty () {
     return if $ignoredirty;
     my @cmd = (@git, qw(diff --quiet HEAD));
-    printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
+    debugcmd "+",@cmd;
     $!=0; $?=0; system @cmd;
     return if !$! && !$?;
     if (!$! && $?==256) {
@@ -1721,7 +1695,7 @@ sub dopush () {
     changedir '../../../..';
     my $diffopt = $debug>0 ? '--exit-code' : '--quiet';
     my @diffcmd = (@git, qw(diff), $diffopt, $tree);
-    printcmd \*DEBUG,$debugprefix."+",@diffcmd;
+    debugcmd "+",@diffcmd;
     $!=0; $?=0;
     my $r = system @diffcmd;
     if ($r) {
@@ -2019,7 +1993,7 @@ sub cmd_rpush {
     push @rdgit, qw(remote-push-build-host), (scalar @rargs), @rargs;
     push @rdgit, @ARGV;
     my @cmd = (@ssh, $host, shellquote @rdgit);
-    printcmd \*DEBUG,$debugprefix."+",@cmd;
+    debugcmd "+",@cmd;
 
     if (defined $initiator_tempdir) {
        rmtree $initiator_tempdir;
@@ -2828,9 +2802,8 @@ sub parseopts () {
                    cmd_help();
                } elsif (s/^-D/-/) {
                    push @ropts, $&;
-                   open DEBUG, ">&STDERR" or die $!;
-                   autoflush DEBUG 1;
                    $debug++;
+                   enabledebug();
                } elsif (s/^-N/-/) {
                    push @ropts, $&;
                    $new_package=1;