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