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/';
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 (@dget_opts) = qw(-u);
25 our $remotename = 'dgit';
27 sub mainbranch () { return "$suite"; }
28 sub uploadbranch () { return "upload/$suite"; }
34 $ua = LWP::UserAgent->new();
37 print DEBUG "fetching @_...\n";
38 my $r = $ua->get(@_) or die $!;
39 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
40 return $r->decoded_content();
43 our ($dscdata,$dscurl,$dsc);
46 my $c = Dpkg::Control::Hash->new();
47 $c->load(@_) or return undef;
51 sub get_archive_dsc () {
52 my $pdourl = "$pdo/source/$suite/$package";
53 my $pdodata = url_get($pdourl);
54 # FFS. The Debian archive has no sane way to find what
55 # version is currently the tip in any branch (aka, what
56 # is the current version in any suite).
58 Download\ \Q$package\E .*
59 \<a\ href=\"([^"&]+([^"/]+\.dsc))\"\>\2\</a\>
61 or die "screenscraping of $pdourl failed :-(\n";
63 #print DEBUG Dumper($pdodata, $&, $dscurl);
64 $dscdata = url_get($dscurl);
65 my $dscfh = new IO::File \$dscdata, '<' or die $!;
66 #print DEBUG Dumper($dscdata, $dscfh);
67 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
68 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
69 #print DEBUG Dumper($dsc);
70 my $fmt = $dsc->{Format};
71 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
74 sub check_for_git () {
77 "ssh $alioth_sshtestbodge->[0] '".
78 " set -e; cd /git/dgit-test;".
79 " if test -d $package.git; then echo 1; else echo 0; fi".
81 #print DEBUG "$cmd\n";
82 open P, "$cmd |" or die $!;
85 #print STDERR ">$r<\n";
86 die "$r $! $?" unless $r =~ m/^[01]$/;
92 die "@_ $! $?" if system @_;
96 open P, "-|", @_ or die $!;
99 { local $/ = undef; $d = <P>; }
101 close P or die "@_ $? $!";
106 our ($dsc_hash,$lastupl_hash);
108 sub generate_commit_from_dsc () {
109 my $ud = '.git/dgit/unpack';
115 foreach (split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files})) {
117 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
119 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
121 link "../../../$f", $f
125 runcmd qw(dget), @dget_opts, qw(--), $dscurl;
126 foreach my $f (grep { m/\.tar\.gz$/ } @files) {
127 link $f, "../../../$f"
133 $dirs[0] =~ m#^([^/]+)/\.$# or die;
135 chdir $dir or die "$dir $!";
137 die $! unless $!==&ENOENT;
138 runcmd qw(git init -q);
139 rmtree('.git/objects');
140 symlink '../../../../objects','.git/objects' or die $!;
141 runcmd qw(git add -Af);
142 my $tree = cmdoutput qw(git write-tree);
143 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
144 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
145 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
146 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
147 my $author = $clogp->{Maintainer};
148 $author =~ s#,.*##ms;
149 my $authline = "$author $date";
150 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
151 open C, ">../commit.tmp" or die $!;
152 print C "tree $tree\n" or die $!;
153 print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
154 print C <<END or die $!;
160 # imported by dgit from the archive
163 print "synthesised git commit from .dsc $clogp->{Version}\n";
164 my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
165 chdir '../../../..' or die $!;
166 cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
167 'DGIT_ARCHIVE', $commithash;
168 cmdoutput qw(git log -n2), $commithash;
169 # ... gives git a chance to complain if our commit is malformed
170 my $outputhash = $commithash;
171 if (defined $lastupl_hash) {
172 chdir "$ud/$dir" or die $!;
173 runcmd qw(git reset --hard), $lastupl_hash;
174 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
175 my $oldclogp = Dpkg::Control::Hash->new();
176 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
178 version_compare_string($oldclogp->{Version}, $clogp->{Version});
180 # git upload/ is earlier vsn than archive, use archive
181 } elsif ($vcmp >= 0) {
182 print STDERR <<END or die $!;
183 Version actually in archive: $clogp->{Version} (older)
184 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
185 Perhaps the upload is stuck in incoming. Using the version from git.
188 die "version in archive is same as version in git".
189 " to-be-uploaded (upload/) branch but archive".
190 " version hash no commit hash?!\n";
192 chdir '../../../..' or die $!;
198 my $lastupl_ref = "refs/remotes/$remotename/upload/$suite";
200 sub fetch_from_archive () {
201 # ensures that $lastupl_ref is what is actually in the archive,
203 $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
205 die $? unless ($?==0 && chomp $lastupl_hash)
206 or ($?==128 && !length $lastupl_hash);
208 if (defined $dsc_hash) {
211 $hash = generate_commit_from_dsc();
214 my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
215 die "not fast forward on last upload branch!".
216 " (archive's version left in DGIT_ARCHIVE)"
217 unless $mb eq $lastupl_hash;
219 if ($lastupl_ref ne $hash) {
220 cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
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 fetch_from_archive();
241 runcmd qw(git reset --hard), $lastupl_ref;
243 die "missing git history even though dsc has hash" if defined $dsc_hash;
244 print "starting new git history\n";
245 mkdir $dstdir or die "$dstdir $!";
246 chdir "$dstdir" or die "$dstdir $!";
247 runcmd qw(git init -q);
248 open H, "> .git/HEAD" or die $!;
249 print H "ref: refs/heads/$suite\n" or die $!;
251 runcmd qw(git remote add), $remotename, $alioth_git;
252 runcmd "git config branch.$suite.remote $remotename";
253 runcmd "git config branch.$suite.merge refs/heads/$suite";
254 my $newhash = generate_commit_from_dsc();
255 runcmd qw(git reset --hard), $newhash;
257 print "ready for work in $dstdir\n";
268 ($package,$suite) = @ARGV;
276 die if @ARGV && $ARGV[0] =~ m/^\-/;
281 my $cmd = shift @ARGV;
284 { no strict qw(refs); &{"cmd_$cmd"}(); }