chiark / gitweb /
a18f45cad6cf2235c2d8e7befc8fc523d40bf400
[dgit.git] / dgit
1 #!/usr/bin/perl -w
2 # dgit
3 # Integration between git and Debian-style archives
4 #
5 # Copyright (C)2013 Ian Jackson
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 use strict;
21
22 use IO::Handle;
23 use Data::Dumper;
24 use LWP::UserAgent;
25 use Dpkg::Control::Hash;
26 use File::Path;
27 use POSIX;
28
29 open DEBUG, ">&STDERR" or die $!;
30
31 our $mirror = 'http://mirror.relativity.greenend.org.uk/mirror/debian-ftp/';
32 our $suite = 'sid';
33 our $package;
34
35 our $aliothname = 'iwj@git.debian.org';
36 our $aliothpath = '/git/dgit-test';
37 our $alioth_git = "git+ssh://$aliothname/$aliothpath";
38 our $alioth_sshtestbodge = [$aliothname,$aliothpath];
39
40 our (@dget_opts) = qw(-u);
41
42 our $remotename = 'dgit';
43
44 sub mainbranch () { return "$suite"; }
45 sub uploadbranch () { return "upload/$suite"; }
46
47 our $ua;
48
49 sub url_get {
50     if (!$ua) {
51         $ua = LWP::UserAgent->new();
52         $ua->env_proxy;
53     }
54     print DEBUG "fetching @_...\n";
55     my $r = $ua->get(@_) or die $!;
56     die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
57     return $r->decoded_content();
58 }
59
60 our ($dscdata,$dscurl,$dsc);
61
62 sub runcmd {
63     $!=0; $?=0;
64     die "@_ $! $?" if system @_;
65 }
66
67 sub cmdoutput {
68     open P, "-|", @_ or die $!;
69     my $d;
70     $!=0; $?=0;
71     { local $/ = undef; $d = <P>; }
72     die if P->error;
73     close P or die "@_ $? $!";
74     chomp $d;
75     return $d;
76 }
77
78 sub parsecontrol {
79     my $c = Dpkg::Control::Hash->new();
80     $c->load(@_) or return undef;
81     return $c;
82 }
83
84 sub parsechangelog {
85     my $c = Dpkg::Control::Hash->new();
86     my $p = new IO::File '-|', qw(dpkg-parsechangelog) or die $!;
87     $c->parse($p);
88     $?=0; $!=0; close $p or die "$! $?";
89     return $c;
90 }
91
92 sub get_archive_dsc () {
93     my $rmad = cmdoutput qw(rmadison -asource),"-s$suite",$package;
94     $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
95                  \s*( [^ \t|]+ )\s* \|
96                  \s*( [^ \t|]+ )\s* \|
97                  \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
98     $1 eq $package or die "$rmad $package ?";
99     my $vsn = $2;
100     $3 eq $suite or die "$rmad $suite ?";
101     $4 eq 'source' or die "$rmad ?";
102     # fixme it does not show us the component ?
103     my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
104     $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
105 #print DEBUG Dumper($pdodata, $&, $dscurl);
106     $dscdata = url_get($dscurl);
107     my $dscfh = new IO::File \$dscdata, '<' or die $!;
108 #print DEBUG Dumper($dscdata, $dscfh);
109     $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
110     $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
111 #print DEBUG Dumper($dsc);
112     my $fmt = $dsc->{Format};
113     die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
114 }
115
116 sub check_for_git () {
117     # returns 0 or 1
118     my $cmd= 
119         "ssh $alioth_sshtestbodge->[0] '".
120         " set -e; cd /git/dgit-test;".
121         " if test -d $package.git; then echo 1; else echo 0; fi".
122         "'";
123     #print DEBUG "$cmd\n";
124     open P, "$cmd |" or die $!;
125     $!=0; $?=0;
126     my $r = <P>; close P;
127 #print STDERR ">$r<\n";
128     die "$r $! $?" unless $r =~ m/^[01]$/;
129     return $r+0;
130 }
131
132 our ($dsc_hash,$lastupl_hash);
133
134 our $ud = '.git/dgit/unpack';
135
136 sub prep_ud () {
137     rmtree($ud);
138     mkpath '.git/dgit';
139     mkdir $ud or die $!;
140 }
141
142 sub mktree_in_ud_from_only_subdir () {
143     # changes into the subdir
144     my (@dirs) = <*/.>;
145     die unless @dirs==1;
146     $dirs[0] =~ m#^([^/]+)/\.$# or die;
147     my $dir = $1;
148     chdir $dir or die "$dir $!";
149     die if stat '.git';
150     die $! unless $!==&ENOENT;
151     runcmd qw(git init -q);
152     rmtree('.git/objects');
153     symlink '../../../../objects','.git/objects' or die $!;
154     runcmd qw(git add -Af);
155     my $tree = cmdoutput qw(git write-tree);
156     chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
157     return ($tree,$dir);
158 }
159
160 sub generate_commit_from_dsc () {
161     prep_ud();
162     chdir $ud or die $!;
163     my @files;
164     foreach (split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files})) {
165         next unless m/\S/;
166         m/^\w+ \d+ (\S+)$/ or die "$_ ?";
167         my $f = $1;
168         die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
169         push @files, $f;
170         link "../../../$f", $f
171             or $!==&ENOENT
172             or die "$f $!";
173     }
174     runcmd qw(dget), @dget_opts, qw(--), $dscurl;
175     foreach my $f (grep { m/\.tar\.gz$/ } @files) {
176         link $f, "../../../$f"
177             or $!==&EEXIST
178             or die "$f $!";
179     }
180     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
181     runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
182     my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
183     my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
184     my $author = $clogp->{Maintainer};
185     $author =~ s#,.*##ms;
186     my $authline = "$author $date";
187     $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
188     open C, ">../commit.tmp" or die $!;
189     print C "tree $tree\n" or die $!;
190     print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
191     print C <<END or die $!;
192 author $authline
193 committer $authline
194
195 $clogp->{Changes}
196
197 # imported by dgit from the archive
198 END
199     close C or die $!;
200     print "synthesised git commit from .dsc $clogp->{Version}\n";
201     my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
202     chdir '../../../..' or die $!;
203     cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
204               'DGIT_ARCHIVE', $commithash;
205     cmdoutput qw(git log -n2), $commithash;
206     # ... gives git a chance to complain if our commit is malformed
207     my $outputhash = $commithash;
208     if (defined $lastupl_hash) {
209         chdir "$ud/$dir" or die $!;
210         runcmd qw(git reset --hard), $lastupl_hash;
211         runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
212         my $oldclogp = Dpkg::Control::Hash->new();
213         $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
214         my $vcmp =
215             version_compare_string($oldclogp->{Version}, $clogp->{Version});
216         if ($vcmp < 0) {
217             # git upload/ is earlier vsn than archive, use archive
218         } elsif ($vcmp >= 0) {
219             print STDERR <<END or die $!;
220 Version actually in archive:    $clogp->{Version} (older)
221 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
222 Perhaps the upload is stuck in incoming.  Using the version from git.
223 END
224         } else {
225             die "version in archive is same as version in git".
226                 " to-be-uploaded (upload/) branch but archive".
227                 " version hash no commit hash?!\n";
228         }
229         chdir '../../../..' or die $!;
230     }
231     rmtree($ud);
232     return $outputhash;
233 }
234
235 my $lastupl_ref = "refs/remotes/$remotename/upload/$suite";
236
237 sub rev_parse ($) {
238     return cmdoutput qw(git rev-parse --), "$_[0]~0";
239 }
240
241 sub is_fast_fwd ($$) {
242     my ($ancestor,$child) = @_;
243     my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
244     return rev_parse($mb) eq rev_parse($ancestor);
245 }
246
247 sub fetch_from_archive () {
248     # ensures that $lastupl_ref is what is actually in the archive,
249     #  one way or another
250     $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
251     die $! if $!;
252     die $? unless ($?==0 && chomp $lastupl_hash) 
253         or ($?==128 && !length $lastupl_hash);
254     my $hash;
255     if (defined $dsc_hash) {
256         $hash = $dsc_hash;
257     } else {
258         $hash = generate_commit_from_dsc();
259     }
260     if ($lastupl_hash) {
261         die "not fast forward on last upload branch!".
262             " (archive's version left in DGIT_ARCHIVE)"
263             unless is_fast_fwd($lastupl_hash, $dsc_hash);
264     }
265     if ($lastupl_ref ne $hash) {
266         cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
267     }
268 }
269
270 sub clone () {
271     get_archive_dsc();
272     $dsc_hash = $dsc->{'Vcs-git-master'};
273     if (defined $dsc_hash) {
274         $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
275         $dsc_hash = $&;
276         print "last upload to archive specified git hash\n";
277     } else {
278         print "last upload to archive has NO git hash\n";
279     }
280     my $dstdir = "$package";
281     if (check_for_git()) {
282         print "cloning existing git history\n";
283         runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
284             $alioth_git, $dstdir;
285         chdir "$dstdir" or die "$dstdir $!";
286         fetch_from_archive();
287         runcmd qw(git reset --hard), $lastupl_ref;
288     } else {
289         die "missing git history even though dsc has hash" if defined $dsc_hash;
290         print "starting new git history\n";
291         mkdir $dstdir or die "$dstdir $!";
292         chdir "$dstdir" or die "$dstdir $!";
293         runcmd qw(git init -q);
294         open H, "> .git/HEAD" or die $!;
295         print H "ref: refs/heads/$suite\n" or die $!;
296         close H or die $!;
297         runcmd qw(git remote add), $remotename, $alioth_git;
298         runcmd "git config branch.$suite.remote $remotename";
299         runcmd "git config branch.$suite.merge refs/heads/$suite";
300         my $newhash = generate_commit_from_dsc();
301         runcmd qw(git reset --hard), $newhash;
302     }
303     print "ready for work in $dstdir\n";
304 }
305
306 sub fetch () {
307     
308 }
309
310 sub push () {
311     runcmd qw(git diff --quiet HEAD);
312     my $clogp = parsechangelog();
313     $package = $clogp->{Source};
314     my $dscfn = "${package}_$clogp->{Version}.dsc";
315     stat $dscfn or die "$dscfn $!";
316     $dsc = parsecontrol("../$dscfn");
317     prep_ud();
318     chdir $ud or die $!;
319     print "checking that $dscfn corresponds to HEAD\n";
320     runcmd qw(dpkg-source -x --), "../../../../$dscfn";
321     my $tree = mktree_in_ud_from_only_subdir();
322     chdir '../../../..' or die $!;
323     runcmd qw(git diff --exit-code), $tree;
324     runcmd qw(git fetch), $alioth_git,
325         map { "$_:refs/remotes/$remotename/$_" }
326         (mainbranch(), uploadbranch());
327     die <<END;
328     if (!is_fast_fwd(mainbranch
329 xxx introduce remote_lref
330 xxx use remote_lref everywhere
331
332 fetch from alioth
333 do fast forward check and maybe fake merge
334 push to uploading
335 dput
336 push to main
337 END
338 }
339
340 sub cmd_clone {
341     if (@ARGV==1) {
342         ($package) = @ARGV;
343     } elsif (@ARGV==2) {
344         ($package,$suite) = @ARGV;
345     } else {
346         die;
347     }
348     clone();
349 }
350
351 sub parseopts () {
352     die if @ARGV && $ARGV[0] =~ m/^\-/;
353 }
354
355 parseopts();
356 die unless @ARGV;
357 my $cmd = shift @ARGV;
358 parseopts();
359
360 { no strict qw(refs); &{"cmd_$cmd"}(); }