+our ($dsc_hash,$upload_hash);
+
+our $ud = '.git/dgit/unpack';
+
+sub prep_ud () {
+ rmtree($ud);
+ mkpath '.git/dgit';
+ mkdir $ud or die $!;
+}
+
+sub mktree_in_ud_from_only_subdir () {
+ # changes into the subdir
+ my (@dirs) = <*/.>;
+ die unless @dirs==1;
+ $dirs[0] =~ m#^([^/]+)/\.$# or die;
+ my $dir = $1;
+ chdir $dir or die "$dir $!";
+ die if stat '.git';
+ die $! unless $!==&ENOENT;
+ runcmd qw(git init -q);
+ rmtree('.git/objects');
+ symlink '../../../../objects','.git/objects' or die $!;
+ runcmd qw(git add -Af);
+ my $tree = cmdoutput qw(git write-tree);
+ chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
+ return ($tree,$dir);
+}
+
+sub dsc_files () {
+ map {
+ m/^\w+ \d+ (\S+)$/ or die "$_ ?";
+ $1;
+ } grep m/\S/, split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files});
+}
+
+sub is_orig_file ($) {
+ local ($_) = @_;
+ m/\.orig\.tar\.\w+$/;
+}
+
+sub generate_commit_from_dsc () {
+ prep_ud();
+ chdir $ud or die $!;
+ my @files;
+ foreach my $f (dsc_files()) {
+ die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
+ push @files, $f;
+ link "../../../$f", $f
+ or $!==&ENOENT
+ or die "$f $!";
+ }
+ runcmd qw(dget), @dget_opts, qw(--), $dscurl;
+ foreach my $f (grep { is_orig_file($_) } @files) {
+ link $f, "../../../$f"
+ or $!==&EEXIST
+ or die "$f $!";
+ }
+ my ($tree,$dir) = mktree_in_ud_from_only_subdir();
+ runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
+ my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
+ my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
+ my $author = $clogp->{Maintainer};
+ $author =~ s#,.*##ms;
+ my $authline = "$author $date";
+ $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
+ open C, ">../commit.tmp" or die $!;
+ print C "tree $tree\n" or die $!;
+ print C "parent $upload_hash\n" or die $! if defined $upload_hash;
+ print C <<END or die $!;
+author $authline
+committer $authline
+
+$clogp->{Changes}
+
+# imported by dgit from the archive
+END
+ close C or die $!;
+ print "synthesised git commit from .dsc $clogp->{Version}\n";
+ my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
+ chdir '../../../..' or die $!;
+ cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
+ 'DGIT_ARCHIVE', $commithash;
+ cmdoutput qw(git log -n2), $commithash;
+ # ... gives git a chance to complain if our commit is malformed
+ my $outputhash = $commithash;
+ if (defined $upload_hash) {
+ chdir "$ud/$dir" or die $!;
+ runcmd qw(git reset --hard), $upload_hash;
+ runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
+ my $oldclogp = Dpkg::Control::Hash->new();
+ $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
+ my $vcmp =
+ version_compare_string($oldclogp->{Version}, $clogp->{Version});
+ if ($vcmp < 0) {
+ # git upload/ is earlier vsn than archive, use archive
+ } elsif ($vcmp >= 0) {
+ print STDERR <<END or die $!;
+Version actually in archive: $clogp->{Version} (older)
+Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
+Perhaps the upload is stuck in incoming. Using the version from git.
+END
+ } else {
+ die "version in archive is same as version in git".
+ " to-be-uploaded (upload/) branch but archive".
+ " version hash no commit hash?!\n";
+ }
+ chdir '../../../..' or die $!;
+ }
+ rmtree($ud);
+ return $outputhash;