chiark / gitweb /
Move various useful functions into Dgit.pm. Necessitates some slightly fancy footwor...
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 17 May 2015 13:45:04 +0000 (14:45 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 31 May 2015 10:54:15 +0000 (11:54 +0100)
Debian/Dgit.pm
dgit

index b21a431e2fa742b5d7da605383bd485d3c93e82d..00b4aa5460869611a42436de52f4a3ffe53df8fc 100644 (file)
@@ -7,6 +7,7 @@ use warnings;
 
 use POSIX;
 use IO::Handle;
+use Config;
 
 BEGIN {
     use Exporter   ();
@@ -15,7 +16,9 @@ BEGIN {
     $VERSION     = 1.00;
     @ISA         = qw(Exporter);
     @EXPORT      = qw(debiantag server_branch server_ref
-                      stat_exists git_for_each_ref
+                      stat_exists fail waitstatusmsg failedcmd
+                      cmdoutput cmdoutput_errok
+                      git_for_each_ref
                       git_for_each_tag_referring
                       $package_re $component_re $deliberately_re
                       $branchprefix
@@ -23,6 +26,7 @@ BEGIN {
                       printdebug debugcmd
                       $debugprefix *debuglevel *DEBUG
                       shellquote printcmd);
+    # implicitly uses $main::us
     %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO)] );
     @EXPORT_OK   = @{ $EXPORT_TAGS{policyflags} };
 }
@@ -113,6 +117,66 @@ sub stat_exists ($) {
     die "stat $f: $!";
 }
 
+sub _us () {
+    $::us // ($0 =~ m#[^/]*$#, $&);
+}
+
+sub fail { 
+    my $s = "@_\n";
+    my $prefix = _us().": ";
+    $s =~ s/^/$prefix/gm;
+    die $s;
+}
+
+our @signames = split / /, $Config{sig_name};
+
+sub waitstatusmsg () {
+    if (!$?) {
+       return "terminated, reporting successful completion";
+    } elsif (!($? & 255)) {
+       return "failed with error exit status ".WEXITSTATUS($?);
+    } elsif (WIFSIGNALED($?)) {
+       my $signum=WTERMSIG($?);
+       return "died due to fatal signal ".
+           ($signames[$signum] // "number $signum").
+           ($? & 128 ? " (core dumped)" : ""); # POSIX(3pm) has no WCOREDUMP
+    } else {
+       return "failed with unknown wait status ".$?;
+    }
+}
+
+sub failedcmd {
+    { local ($!); printcmd \*STDERR, _us().": failed command:", @_ or die $!; };
+    if ($!) {
+       fail "failed to fork/exec: $!";
+    } elsif ($?) {
+       fail "subprocess ".waitstatusmsg();
+    } else {
+       fail "subprocess produced invalid output";
+    }
+}
+
+sub cmdoutput_errok {
+    die Dumper(\@_)." ?" if grep { !defined } @_;
+    debugcmd "|",@_;
+    open P, "-|", @_ or die $!;
+    my $d;
+    $!=0; $?=0;
+    { local $/ = undef; $d = <P>; }
+    die $! if P->error;
+    if (!close P) { printdebug "=>!$?\n"; return undef; }
+    chomp $d;
+    $d =~ m/^.*/;
+    printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debuglevel>0; #';
+    return $d;
+}
+
+sub cmdoutput {
+    my $d = cmdoutput_errok @_;
+    defined $d or failedcmd @_;
+    return $d;
+}
+
 sub git_for_each_ref ($$) {
     my ($pattern,$func) = @_;
     # calls $func->($objid,$objtype,$fullrefname,$reftail);
diff --git a/dgit b/dgit
index 3d3628093e0d42e4ae8d9ac4ed22b05c429611d6..4f0329a49932b21caf94395785b6eb4bf167e551 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -32,7 +32,6 @@ use POSIX;
 use IPC::Open2;
 use Digest::SHA;
 use Digest::MD5;
-use Config;
 
 use Debian::Dgit;
 
@@ -140,30 +139,6 @@ END {
     }
 };
 
-our @signames = split / /, $Config{sig_name};
-
-sub waitstatusmsg () {
-    if (!$?) {
-       return "terminated, reporting successful completion";
-    } elsif (!($? & 255)) {
-       return "failed with error exit status ".WEXITSTATUS($?);
-    } elsif (WIFSIGNALED($?)) {
-       my $signum=WTERMSIG($?);
-       return "died due to fatal signal ".
-           ($signames[$signum] // "number $signum").
-           ($? & 128 ? " (core dumped)" : ""); # POSIX(3pm) has no WCOREDUMP
-    } else {
-       return "failed with unknown wait status ".$?;
-    }
-}
-
-sub fail { 
-    my $s = "@_\n";
-    my $prefix = $us.($we_are_responder ? " (build host)" : "").": ";
-    $s =~ s/^/$prefix/gm;
-    die $s;
-}
-
 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
 
 sub no_such_package () {
@@ -375,17 +350,6 @@ sub url_get {
 
 our ($dscdata,$dscurl,$dsc,$dsc_checked,$skew_warning_vsn);
 
-sub failedcmd {
-    { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
-    if ($!) {
-       fail "failed to fork/exec: $!";
-    } elsif ($?) {
-       fail "subprocess ".waitstatusmsg();
-    } else {
-       fail "subprocess produced invalid output";
-    }
-}
-
 sub runcmd {
     debugcmd "+",@_;
     $!=0; $?=0;
@@ -403,27 +367,6 @@ sub printdone {
     }
 }
 
-sub cmdoutput_errok {
-    die Dumper(\@_)." ?" if grep { !defined } @_;
-    debugcmd "|",@_;
-    open P, "-|", @_ or die $!;
-    my $d;
-    $!=0; $?=0;
-    { local $/ = undef; $d = <P>; }
-    die $! if P->error;
-    if (!close P) { printdebug "=>!$?\n"; return undef; }
-    chomp $d;
-    $d =~ m/^.*/;
-    printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debuglevel>0; #';
-    return $d;
-}
-
-sub cmdoutput {
-    my $d = cmdoutput_errok @_;
-    defined $d or failedcmd @_;
-    return $d;
-}
-
 sub dryrun_report {
     printcmd(\*STDERR,$debugprefix."#",@_);
 }
@@ -1912,9 +1855,9 @@ sub cmd_push {
        } else {
            fail "dgit push: HEAD is not a descendant".
                " of the archive's version.\n".
-               "$us: To overwrite its contents,".
+               "dgit: To overwrite its contents,".
                " use git merge -s ours ".lrref().".\n".
-               "$us: To rewind history, if permitted by the archive,".
+               "dgit: To rewind history, if permitted by the archive,".
                " use --deliberately-not-fast-forward";
        }
     } else {
@@ -1938,6 +1881,7 @@ sub cmd_remote_push_build_host {
     # offered several)
     $debugprefix = ' ';
     $we_are_responder = 1;
+    $us .= " (build host)";
 
     open PI, "<&STDIN" or die $!;
     open STDIN, "/dev/null" or die $!;