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