chiark / gitweb /
Move dgit's debugging arrangements into Dgit.pm
[dgit.git] / Debian / Dgit.pm
index 1ab65a8f24c6fd1da07d25bd03e1916f81f16f4d..5c079a68d3abfa8e4ce89b969027337244d4c53b 100644 (file)
@@ -6,6 +6,7 @@ use strict;
 use warnings;
 
 use POSIX;
+use IO::Handle;
 
 BEGIN {
     use Exporter   ();
@@ -15,7 +16,9 @@ BEGIN {
     @ISA         = qw(Exporter);
     @EXPORT      = qw(debiantag server_branch server_ref
                       stat_exists git_for_each_ref
-                      $package_re $component_re $branchprefix);
+                      $package_re $component_re $branchprefix
+                      initdebug enabledebug printdebug $debugprefix $debug
+                      shellquote printcmd);
     %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO)] );
     @EXPORT_OK   = @{ $EXPORT_TAGS{policyflags} };
 }
@@ -73,4 +76,45 @@ sub git_for_each_tag_referring ($$) {
     });
 }
 
+our $debugprefix;
+our $debug = 0;
+
+sub initdebug ($) { 
+    ($debugprefix) = @_;
+    open ::DEBUG, ">/dev/null" or die $!;
+}
+
+sub enabledebug () {
+    open ::DEBUG, ">&STDERR" or die $!;
+    ::DEBUG->autoflush(1);
+    $debug ||= 1;
+}
+    
+sub printdebug {
+    print ::DEBUG $debugprefix, @_ or die $!;
+}
+
+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 $!;
+}
+
 1;