+# workrepo and destrepo handled ad-hoc
+
+sub mode_tag2upload () {
+ # CALLER MUST PREVENT MULTIPLE CONCURRENT RUNS IN SAME CWD
+ # If we fail (exit nonzero), caller should capture our stderr,
+ # and retry some bounded number of times in some appropriate way
+ # Uses whatever ambient gpg key is available
+ @ARGV==2 or die;
+
+ my $url;
+ ($url,$tagval) = @ARGV;
+
+ $ENV{DGIT_DRS_EMAIL_NOREPLY} // die;
+
+ my $start = time // die;
+ my @t = gmtime $start;
+
+ die if $url =~ m/[^[:graph:]]/;
+ die if $tagval =~ m/[^[:graph:]]/;
+
+ open OL, ">>overall.log" or die $!;
+ autoflush OL 1;
+ my $quit = sub {
+ printf OL "%04d-%02d-%02d %02d:%02d:%02d (%04ds): %s %s: %s\n",
+ $t[5] + 1900, @t[4,3,2,1,0], (time-$start), $url, $tagval, $_[0];
+ exit 0;
+ };
+
+ $ENV{DGIT_DRS_ANY_URL} or $url =~ m{^https://}s
+ or $quit->("url scheme not as expected");
+
+ $tagval =~ m{^$distro/($versiontag_re)$}s
+ or $quit->("tag name not for us");
+
+ $version = $1;
+ $version =~ y/_\%\#/:~/d;
+
+ my $work = 'work';
+
+ my $tagref = "refs/tags/$tagval";
+
+ rmtree $work;
+ rmtree 'bpd';
+ mkdir $work or die $!;
+ mkdir 'bpd' or die $!;
+ unlink <*.orig*>;
+ dif $! if <*.orig*>;
+ changedir $work;
+ runcmd qw(git init -q);
+ runcmd qw(git remote add origin), $url;
+ runcmd qw(git fetch --depth=1 origin), "$tagref:$tagref";
+ changedir ".git";
+ mkdir 'dgit-tmp' or die $!;
+
+ my $tagger;
+ open T, "-|", qw(git cat-file tag), $tagval or die $!;
+ {
+ local $/ = undef;
+ $!=0; $_=<T>; defined or die $!;
+
+ # quick and dirty check, will check properly later
+ m/^\[dgit[^"]* please-upload(?:\]| )/m or
+ $quit->("tag missing please-upload request");
+
+ m/^tagger (.*) \d+ [-+]\d+$/m or
+ $quit->("failed to fish tagger out of tag");
+ $tagger = $1;
+ };
+
+ readtag();
+ m/^($package_re) release (\S+) for ($suite_re)$/ or
+ $quit->("tag headline not for us");
+ $package = $1;
+ my $tagmversion = $2;
+ $suite = $3;
+
+
+ # This is for us. From now on, we will capture errors to
+ # be emailed to the tagger.
+
+ open H, ">>dgit-tmp/tagupl.email" or die $!;
+ print H <<END or die $!;
+Subject: push-to-upload failed, $package $version ($distro)
+X-Debian-Push-Distro: $distro
+X-Debian-Push-Package: $package
+END
+ printf H "To: %s", $tagger or die $!; # no newline
+ flush H or die $!;
+
+ open L, ">>dgit-tmp/tagupl.log" or die $!;
+
+ my $child = fork() // die $!;
+ if ($child) {
+ # we are the parent
+ # if child exits 0, it has called $quit->()
+ $!=0; waitpid $child, 0 == $child or die $!;
+ printdebug "child $child ?=$?\n";
+ exit 0 unless $?;
+ print L "execution child: ", waitstatusmsg(), "\n" or die $!;
+ close L or die $!;
+ print H <<END or die $!;
+
+
+Processing of tag $tagval
+From url $url
+Was not successful:
+
+END
+ $ENV{DGIT_DRS_SENDMAIL} //= '/usr/lib/sendmail';
+
+ close H or die $!;
+ runcmd qw(sh -ec), <<"END";
+ cd dgit-tmp
+ cat tagupl.log >>tagupl.email
+ $ENV{DGIT_DRS_SENDMAIL} -oee -odb -oi -t \\
+ -f$ENV{DGIT_DRS_EMAIL_NOREPLY} \\
+ <tagupl.email
+END
+ $quit->("failed, emailed");