chiark / gitweb /
break out is_fast_ff
[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 is_fast_ff ($$) {
220     my ($ancestor,$child) = @_;
221     my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
222     return $mb eq $ancestor;
223 }
224
225 sub fetch_from_archive () {
226     # ensures that $lastupl_ref is what is actually in the archive,
227     #  one way or another
228     $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
229     die $! if $!;
230     die $? unless ($?==0 && chomp $lastupl_hash) 
231         or ($?==128 && !length $lastupl_hash);
232     my $hash;
233     if (defined $dsc_hash) {
234         $hash = $dsc_hash;
235     } else {
236         $hash = generate_commit_from_dsc();
237     }
238     if ($lastupl_hash) {
239         die "not fast forward on last upload branch!".
240             " (archive's version left in DGIT_ARCHIVE)"
241             unless is_fast_ff($lastupl_hash, $dsc_hash);
242     }
243     if ($lastupl_ref ne $hash) {
244         cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
245     }
246 }
247
248 sub clone () {
249     get_archive_dsc();
250     $dsc_hash = $dsc->{'Vcs-git-master'};
251     if (defined $dsc_hash) {
252         $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
253         $dsc_hash = $&;
254         print "last upload to archive specified git hash\n";
255     } else {
256         print "last upload to archive has NO git hash\n";
257     }
258     my $dstdir = "$package";
259     if (check_for_git()) {
260         print "cloning existing git history\n";
261         runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
262             $alioth_git, $dstdir;
263         chdir "$dstdir" or die "$dstdir $!";
264         fetch_from_archive();
265         runcmd qw(git reset --hard), $lastupl_ref;
266     } else {
267         die "missing git history even though dsc has hash" if defined $dsc_hash;
268         print "starting new git history\n";
269         mkdir $dstdir or die "$dstdir $!";
270         chdir "$dstdir" or die "$dstdir $!";
271         runcmd qw(git init -q);
272         open H, "> .git/HEAD" or die $!;
273         print H "ref: refs/heads/$suite\n" or die $!;
274         close H or die $!;
275         runcmd qw(git remote add), $remotename, $alioth_git;
276         runcmd "git config branch.$suite.remote $remotename";
277         runcmd "git config branch.$suite.merge refs/heads/$suite";
278         my $newhash = generate_commit_from_dsc();
279         runcmd qw(git reset --hard), $newhash;
280     }
281     print "ready for work in $dstdir\n";
282 }
283
284 sub fetch () {
285     
286 }
287
288 sub push () {
289     runcmd qw(git diff --quiet HEAD);
290     my $clogp = parsechangelog();
291     $package = $clogp->{Source};
292     my $dscfn = "${package}_$clogp->{Version}.dsc";
293     stat $dscfn or die "$dscfn $!";
294     $dsc = parsecontrol("../$dscfn");
295     prep_ud();
296     chdir $ud or die $!;
297     print "checking that $dscfn corresponds to HEAD\n";
298     runcmd qw(dpkg-source -x --), "../../../../$dscfn";
299     my $tree = mktree_in_ud_from_only_subdir();
300     chdir '../../../..' or die $!;
301     runcmd qw(git diff --exit-code), $tree;
302     runcmd qw(git fetch), $alioth_git,
303         map { "$_:refs/remotes/$remotename/$_" }
304         (mainbranch(), uploadbranch());
305     
306     die <<END;
307 fetch from alioth
308 do fast forward check and maybe fake merge
309 push to uploading
310 dput
311 push to main
312 END
313 }
314
315 sub cmd_clone {
316     if (@ARGV==1) {
317         ($package) = @ARGV;
318     } elsif (@ARGV==2) {
319         ($package,$suite) = @ARGV;
320     } else {
321         die;
322     }
323     clone();
324 }
325
326 sub parseopts () {
327     die if @ARGV && $ARGV[0] =~ m/^\-/;
328 }
329
330 parseopts();
331 die unless @ARGV;
332 my $cmd = shift @ARGV;
333 parseopts();
334
335 { no strict qw(refs); &{"cmd_$cmd"}(); }