7 use Dpkg::Control::Hash;
11 open DEBUG, ">&STDERR" or die $!;
13 our $pdo = 'http://packages.debian.org/';
14 #our $mirror = 'http://mirror.relativity.greenend.org.uk/mirror/debian-ftp/';
16 our $package = '2vcard';
18 our $aliothname = 'iwj@git.debian.org';
19 our $aliothpath = '/git/dgit-test';
20 our $alioth_git = 'git+ssh://$aliothname/$aliothpath';
21 our $alioth_sshtestbodge = [$aliothname,$aliothpath];
23 sub mainbranch () { return "$suite"; }
24 sub uploadingbranch () { return "uploading/$suite"; }
30 $ua = LWP::UserAgent->new();
33 print DEBUG "fetching @_...\n";
34 my $r = $ua->get(@_) or die $!;
35 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
36 return $r->decoded_content();
39 our ($dscdata,$dscurl);
41 sub get_archive_dsc () {
42 my $pdourl = "$pdo/source/$suite/$package";
43 my $pdodata = url_get($pdourl);
44 # FFS. The Debian archive has no sane way to find what
45 # version is currently the tip in any branch (aka, what
46 # is the current version in any suite).
48 Download\ \Q$package\E .*
49 \<a\ href=\"([^"&]+([^"/]+\.dsc))\"\>\2\</a\>
51 or die "screenscraping of $pdourl failed :-(\n";
53 print DEBUG Dumper($pdodata, $&, $dscurl);
54 $dscdata = url_get($dscurl);
55 my $dscfh = new IO::File \$dscdata, '<' or die $!;
56 print DEBUG Dumper($dscdata, $dscfh);
57 my $dscp = Dpkg::Control::Hash->new(allow_pgp=>1);
58 $dscp->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
59 # my $dscf = $dscp->{'fields'};
61 print DEBUG Dumper($dscp,$dscf);
62 my $fmt = $dscf->{Format};
63 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
67 sub check_for_git () {
69 open P, "ssh $alioth_sshtestbodge->[0] '".
70 "set -e; cd /git/dgit-test;".
71 "if test -d $package.git; then echo 1; else echo 0; fi".
76 die "$r $! $?" unless $r =~ m/^[01]$/;
82 die "$! $?" if system @_;
85 our ($dsc,$dsc_hash,$lastupl_hash);
87 sub generate_commit_from_dsc () {
88 my $ud = '.git/dgit/unpack';
94 foreach (split /\n/, ($dsch->{Checksums-Sha256} || $dsch->{Files})) {
96 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
98 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
100 link "../../../$f", $f
104 runcmd qw(dget --), $dscurl;
105 foreach my $f (grep { m/\.tar\.gz$/ } @files) {
106 link $f, "../../../$f"
112 $dirs[0] =~ m#^([^/]+)/\.$# or die;
114 chdir $dir or die "$dir $!";
116 die $! unless $!==&ENOENT;
118 remove_tree(.git/objects);
119 symlink '../../objects','.git/objects' or die $!;
120 runcmd qw(git add -Af);
121 my $tree = cmdoutput qw(git write-tree);
123 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
124 my $clogp = Dpkg::Control::Hash->new();
125 $clogp->parse('../changelog.tmp','changelog');
126 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
127 my $author = $clogp->{Maintainer};
128 $author =~ s#,.*##ms;
129 my $authline = "$author $date";
130 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
131 open C, ">../commit.tmp" or die $!;
132 print C "tree $tree\n" or die $!;
133 print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
134 print C <<END or die $!;
142 my $commithash = runcmd qw(git hash-object -w -t commit ../commit.tmp);
143 chdir '../../..' or die $!;
145 cmdoutput qw(git log -n1), $commithash;
149 sub fetch_from_archive () {
151 if (defined $dsc_hash) {
154 $hash = generate_commit_from_dsc();
156 cmdoutput qw(git update-ref FETCH_HEAD) $hash;
162 # runcmd qw(git write-tree
165 runcmd qw(mkdir -p '');
166 # chdir '.git/dgit/unpack' or die $!;
175 # open P, "-|", qw(git rev-parse --), $dsc_hash;
180 $dsc = get_archive_dsc();
181 $dsc_hash = $dsc->{'Vcs-git-master'};
182 if (defined $dsc_hash) {
183 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
186 my $dstdir = "$package";
187 if (check_for_git()) {
188 runcmd qw(git clone --origin dgit -b), $suite, '--',
189 $alioth_git, $dstdir;
190 chdir "$dstdir" or die "$dstdir $!";
191 update_from_archive();
193 mkdir $dstdir or die "$dstdir $!";
194 chdir "$dstdir" or die "$dstdir $!";
196 open H, "> .git/refs/HEAD" or die $!;
197 print H "ref: refs/heads/$suite\n" or die $!;
199 runcmd qw(git remote add dgit), $alioth_git;
200 runcmd "git config branch.$suite.remote dgit";
201 runcmd "git config branch.$suite.merge refs/heads/$suite";
202 update_from_archive();
207 my ($archive_or_mirror, $suite, $package) = @_;
208 my $dsc = get_archive_dsc();
211 print Dumper(get_archive_dsc());