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 our $remotename = 'dgit';
25 sub mainbranch () { return "$suite"; }
26 sub uploadbranch () { return "upload/$suite"; }
32 $ua = LWP::UserAgent->new();
35 print DEBUG "fetching @_...\n";
36 my $r = $ua->get(@_) or die $!;
37 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
38 return $r->decoded_content();
41 our ($dscdata,$dscurl);
43 sub get_archive_dsc () {
44 my $pdourl = "$pdo/source/$suite/$package";
45 my $pdodata = url_get($pdourl);
46 # FFS. The Debian archive has no sane way to find what
47 # version is currently the tip in any branch (aka, what
48 # is the current version in any suite).
50 Download\ \Q$package\E .*
51 \<a\ href=\"([^"&]+([^"/]+\.dsc))\"\>\2\</a\>
53 or die "screenscraping of $pdourl failed :-(\n";
55 print DEBUG Dumper($pdodata, $&, $dscurl);
56 $dscdata = url_get($dscurl);
57 my $dscfh = new IO::File \$dscdata, '<' or die $!;
58 print DEBUG Dumper($dscdata, $dscfh);
59 my $dscp = Dpkg::Control::Hash->new(allow_pgp=>1);
60 $dscp->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
61 # my $dscf = $dscp->{'fields'};
63 print DEBUG Dumper($dscp,$dscf);
64 my $fmt = $dscf->{Format};
65 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
69 sub check_for_git () {
71 open P, "ssh $alioth_sshtestbodge->[0] '".
72 "set -e; cd /git/dgit-test;".
73 "if test -d $package.git; then echo 1; else echo 0; fi".
78 die "$r $! $?" unless $r =~ m/^[01]$/;
84 die "$! $?" if system @_;
87 our ($dsc,$dsc_hash,$lastupl_hash);
89 sub generate_commit_from_dsc () {
90 my $ud = '.git/dgit/unpack';
96 foreach (split /\n/, ($dsch->{Checksums-Sha256} || $dsch->{Files})) {
98 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
100 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
102 link "../../../$f", $f
106 runcmd qw(dget --), $dscurl;
107 foreach my $f (grep { m/\.tar\.gz$/ } @files) {
108 link $f, "../../../$f"
114 $dirs[0] =~ m#^([^/]+)/\.$# or die;
116 chdir $dir or die "$dir $!";
118 die $! unless $!==&ENOENT;
120 remove_tree(.git/objects);
121 symlink '../../objects','.git/objects' or die $!;
122 runcmd qw(git add -Af);
123 my $tree = cmdoutput qw(git write-tree);
125 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
126 my $clogp = Dpkg::Control::Hash->new();
127 $clogp->parse('../changelog.tmp','changelog') or die;
128 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
129 my $author = $clogp->{Maintainer};
130 $author =~ s#,.*##ms;
131 my $authline = "$author $date";
132 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
133 open C, ">../commit.tmp" or die $!;
134 print C "tree $tree\n" or die $!;
135 print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
136 print C <<END or die $!;
141 # imported by dgit from the archive
144 print "synthesised git commit from .dsc $clogp->{Version}\n";
145 my $commithash = runcmd qw(git hash-object -w -t commit ../commit.tmp);
146 chdir '../../..' or die $!;
147 cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
148 'DGIT_ARCHIVE', $hash;
149 cmdoutput qw(git log -n2), $commithash;
150 # ... gives git a chance to complain if our commit is malformed
151 my $outputhash = $commithash;
152 if (defined $lastupl_hash) {
154 runcmd qw(git reset --hard), $lastupl_hash;
155 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
156 my $oldclogp = Dpkg::Control::Hash->new();
157 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
158 $vcmp = version_compare_string($oldclogp->{Version}, $clogp->{Version});
160 # git upload/ is earlier vsn than archive, use archive
161 } elsif ($vcmp >= 0) {
162 print STDERR <<END or die $!;
163 Version actually in archive: $clogp->{Version} (older)
164 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
165 Perhaps the upload is stuck in incoming. Using the version from git.
168 die "version in archive is same as version in git".
169 " to-be-uploaded (upload/) branch but archive".
170 " version hash no commit hash?!\n";
172 chdir '../../..' or die $!;
178 my $lastupl_ref = "refs/remotes/$remotename/upload/$suite";
180 sub fetch_from_archive () {
181 # ensures that $lastupl_ref is what is actually in the archive,
183 $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
185 die $? unless ($?==0 && chomp $lastupl_hash)
186 or ($?==128 && !length $lastupl_hash);
188 if (defined $dsc_hash) {
191 $hash = generate_commit_from_dsc();
194 my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
195 die "not fast forward on last upload branch!"
196 " (archive's version left in DGIT_ARCHIVE)"
197 unless $mb eq $lastupl_hash;
199 if ($lastupl_ref ne $hash) {
200 cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
207 # runcmd qw(git write-tree
210 runcmd qw(mkdir -p '');
211 # chdir '.git/dgit/unpack' or die $!;
220 # open P, "-|", qw(git rev-parse --), $dsc_hash;
225 $dsc = get_archive_dsc();
226 $dsc_hash = $dsc->{'Vcs-git-master'};
227 if (defined $dsc_hash) {
228 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
230 print "last upload to archive specified git hash\n";
232 print "last upload to archive has NO git hash\n";
234 my $dstdir = "$package";
235 if (check_for_git()) {
236 print "cloning existing git history\n";
237 runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
238 $alioth_git, $dstdir;
239 chdir "$dstdir" or die "$dstdir $!";
240 record_suite_origin();
241 fetch_from_archive();
242 runcmd qw(git reset --hard), $lastupl_ref;
244 die "missing git history even though dsc has hash" if defined $dsc_hash;
245 print "starting new git history\n";
246 mkdir $dstdir or die "$dstdir $!";
247 chdir "$dstdir" or die "$dstdir $!";
249 open H, "> .git/refs/HEAD" or die $!;
250 print H "ref: refs/heads/$suite\n" or die $!;
252 runcmd qw(git remote add), $remotename, $alioth_git;
253 record_suite_origin();
254 runcmd "git config branch.$suite.remote $remotename";
255 runcmd "git config branch.$suite.merge refs/heads/$suite";
256 my $newhash = generate_commit_from_dsc();
257 runcmd "git reset --hard", $newhash;
265 print Dumper(get_archive_dsc());