+our ($dsc,$dsc_hash,$lastupl_hash);
+
+sub generate_commit_from_dsc () {
+ my $ud = '.git/dgit/unpack';
+ remove_tree($ud);
+ mkpath '.git/dgit';
+ mkdir $ud or die $!;
+ chdir $ud or die $!;
+ my @files;
+ foreach (split /\n/, ($dsch->{Checksums-Sha256} || $dsch->{Files})) {
+ next unless m/\S/;
+ m/^\w+ \d+ (\S+)$/ or die "$_ ?";
+ my $f = $1;
+ die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
+ push @files, $f;
+ link "../../../$f", $f
+ or $!==&ENOENT
+ or die "$f $!";
+ }
+ runcmd qw(dget --), $dscurl;
+ foreach my $f (grep { m/\.tar\.gz$/ } @files) {
+ link $f, "../../../$f"
+ or $!==&EEXIST
+ or die "$f $!";
+ }
+ 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);
+ remove_tree(.git/objects);
+ symlink '../../objects','.git/objects' or die $!;
+ runcmd qw(git add -Af);
+ my $tree = cmdoutput qw(git write-tree);
+ chomp $tree or die;
+ runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
+ my $clogp = Dpkg::Control::Hash->new();
+ $clogp->parse('../changelog.tmp','changelog');
+ 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 $lastupl_hash\n" or die $! if defined $lastupl_hash;
+ print C <<END or die $!;
+author $authline
+committer $authline
+
+$clogp->{Changes}
+# generated by dgit
+END
+ close C or die $!;
+ my $commithash = runcmd qw(git hash-object -w -t commit ../commit.tmp);
+ chdir '../../..' or die $!;
+ remove_tree($ud);
+ cmdoutput qw(git log -n1), $commithash;
+ return $commithash;
+}
+
+sub fetch_from_archive () {
+ my $hash;
+ if (defined $dsc_hash) {
+ $hash = $dsc_hash;
+ } else {
+ $hash = generate_commit_from_dsc();
+ }
+ cmdoutput qw(git update-ref FETCH_HEAD) $hash;
+}