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