X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=dgit.git;a=blobdiff_plain;f=Debian%2FDgit.pm;h=fb983c1a023b2e44d49048255c89dd3ea7797e4a;hp=d66b712bc86af9ba104a88cc0bbac06e375d2370;hb=2fe42d36da42a62e8082d0d1c4c35943e8281e9e;hpb=d6be829bf56e699d27d388026d4af7ab710ba328 diff --git a/Debian/Dgit.pm b/Debian/Dgit.pm index d66b712b..fb983c1a 100644 --- a/Debian/Dgit.pm +++ b/Debian/Dgit.pm @@ -5,9 +5,12 @@ package Debian::Dgit; use strict; use warnings; +use Carp; use POSIX; use IO::Handle; use Config; +use Digest::SHA; +use Data::Dumper; BEGIN { use Exporter (); @@ -16,8 +19,11 @@ BEGIN { $VERSION = 1.00; @ISA = qw(Exporter); @EXPORT = qw(setup_sigwarn - debiantag server_branch server_ref - stat_exists fail ensuredir waitstatusmsg failedcmd + debiantag_old debiantag_new server_branch server_ref + stat_exists link_ltarget + hashfile + fail ensuredir executable_on_path + waitstatusmsg failedcmd cmdoutput cmdoutput_errok git_rev_parse git_get_ref git_for_each_ref git_for_each_tag_referring is_fast_fwd @@ -26,7 +32,7 @@ BEGIN { initdebug enabledebug enabledebuglevel printdebug debugcmd $debugprefix *debuglevel *DEBUG - shellquote printcmd); + shellquote printcmd messagequote); # implicitly uses $main::us %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO)] ); @EXPORT_OK = @{ $EXPORT_TAGS{policyflags} }; @@ -81,12 +87,22 @@ sub printdebug { print DEBUG $debugprefix, @_ or die $! if $debuglevel>0; } +sub messagequote ($) { + local ($_) = @_; + s{\\}{\\\\}g; + s{\n}{\\n}g; + s{\x08}{\\b}g; + s{\t}{\\t}g; + s{[\000-\037\177]}{ sprintf "\\x%02x", ord $& }ge; + $_; +} + sub shellquote { my @out; local $_; foreach my $a (@_) { $_ = $a; - if (!length || m{[^-=_./0-9a-z]}i) { + if (!length || m{[^-=_./:0-9a-z]}i) { s{['\\]}{'\\$&'}g; push @out, "'$_'"; } else { @@ -109,12 +125,18 @@ sub debugcmd { printcmd(\*DEBUG,$debugprefix.$extraprefix,@_) if $debuglevel>0; } -sub debiantag ($$) { +sub debiantag_old ($$) { my ($v,$distro) = @_; $v =~ y/~:/_%/; return "$distro/$v"; } +sub debiantag_new ($$) { + my ($v,$distro) = @_; + $v =~ y/~:/_%/; + return "archive/$distro/$v"; +} + sub server_branch ($) { return "$branchprefix/$_[0]"; } sub server_ref ($) { return "refs/".server_branch($_[0]); } @@ -143,6 +165,17 @@ sub ensuredir ($) { die "mkdir $dir: $!"; } +sub executable_on_path ($) { + my ($program) = @_; + return 1 if $program =~ m{/}; + my @path = split /:/, ($ENV{PATH} // "/usr/local/bin:/bin:/usr/bin"); + foreach my $pe (@path) { + my $here = "$pe/$program"; + return $here if stat_exists $here && -x _; + } + return undef; +} + our @signames = split / /, $Config{sig_name}; sub waitstatusmsg () { @@ -161,8 +194,19 @@ sub waitstatusmsg () { } sub failedcmd { + # Expects $!,$? as set by close - see below. + # To use with system(), set $?=-1 first. + # + # Actual behaviour of perl operations: + # success $!==0 $?==0 close of piped open + # program failed $!==0 $? >0 close of piped open + # syscall failure $! >0 $?=-1 close of piped open + # failure $! >0 unchanged close of something else + # success trashed $?==0 system + # program failed trashed $? >0 system + # syscall failure $! >0 unchanged system { local ($!); printcmd \*STDERR, _us().": failed command:", @_ or die $!; }; - if ($!) { + if ($? < 0) { fail "failed to fork/exec: $!"; } elsif ($?) { fail "subprocess ".waitstatusmsg(); @@ -172,9 +216,9 @@ sub failedcmd { } sub cmdoutput_errok { - die Dumper(\@_)." ?" if grep { !defined } @_; + confess Dumper(\@_)." ?" if grep { !defined } @_; debugcmd "|",@_; - open P, "-|", @_ or die $!; + open P, "-|", @_ or die "$_[0] $!"; my $d; $!=0; $?=0; { local $/ = undef; $d =

; } @@ -197,6 +241,22 @@ sub cmdoutput { return $d; } +sub link_ltarget ($$) { + my ($old,$new) = @_; + lstat $old or return undef; + if (-l _) { + $old = cmdoutput qw(realpath --), $old; + } + link $old, $new or die "link $old $new: $!"; +} + +sub hashfile ($) { + my ($fn) = @_; + my $h = Digest::SHA->new(256); + $h->addfile($fn); + return $h->hexdigest(); +} + sub git_rev_parse ($) { return cmdoutput qw(git rev-parse), "$_[0]~0"; }