+sub debiantag ($) {
+ my ($v) = @_;
+ $v =~ y/~:/_%/;
+ return "debian/$v";
+}
+
+sub server_branch ($) { return "$branchprefix/$_[0]"; }
+sub server_ref ($) { return "refs/".server_branch($_[0]); }
+
+sub stat_exists ($) {
+ my ($f) = @_;
+ return 1 if stat $f;
+ return 0 if $!==&ENOENT;
+ 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);
+ # $reftail is RHS of ref after refs/\w+/
+ # breaks if $pattern matches any ref `refs/blah' where blah has no `/'
+ my @cmd = (qw(git for-each-ref), $pattern);
+ open GFER, "-|", @cmd or die $!;
+ debugcmd "|", @cmd;
+ while (<GFER>) {
+ chomp or die "$_ ?";
+ printdebug "|> ", $_, "\n";
+ m#^(\w+)\s+(\w+)\s+(refs/\w+/(\S+))$# or die "$_ ?";
+ $func->($1,$2,$3,$4);
+ }
+ $!=0; $?=0; close GFER or die "$pattern $? $!";
+}
+
+sub git_for_each_tag_referring ($$) {
+ my ($objreferring, $func) = @_;
+ # calls $func->($objid,$fullrefname,$tagname);
+ printdebug "git_for_each_tag_referring ",
+ ($objreferring // 'UNDEF'),"\n";
+ git_for_each_ref('refs/tags', sub {
+ my ($objid,$objtype,$fullrefname,$tagname) = @_;
+ return unless $objtype eq 'tag';
+ return if defined $objreferring and $objid ne $objreferring;
+ $func->($objid,$fullrefname,$tagname);
+ });
+}
+