chiark / gitweb /
TODOs
[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 get_archive_dsc () {
67     my $rmad = cmdoutput qw(rmadison -asource),"-s$suite",$package;
68     $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
69                  \s*( [^ \t|]+ )\s* \|
70                  \s*( [^ \t|]+ )\s* \|
71                  \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
72     $1 eq $package or die "$rmad $package ?";
73     my $vsn = $2;
74     $3 eq $suite or die "$rmad $suite ?";
75     $4 eq 'source' or die "$rmad ?";
76     # fixme it does not show us the component ?
77     my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
78     $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
79 #print DEBUG Dumper($pdodata, $&, $dscurl);
80     $dscdata = url_get($dscurl);
81     my $dscfh = new IO::File \$dscdata, '<' or die $!;
82 #print DEBUG Dumper($dscdata, $dscfh);
83     $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
84     $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
85 #print DEBUG Dumper($dsc);
86     my $fmt = $dsc->{Format};
87     die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
88 }
89
90 sub check_for_git () {
91     # returns 0 or 1
92     my $cmd= 
93         "ssh $alioth_sshtestbodge->[0] '".
94         " set -e; cd /git/dgit-test;".
95         " if test -d $package.git; then echo 1; else echo 0; fi".
96         "'";
97     #print DEBUG "$cmd\n";
98     open P, "$cmd |" or die $!;
99     $!=0; $?=0;
100     my $r = <P>; close P;
101 #print STDERR ">$r<\n";
102     die "$r $! $?" unless $r =~ m/^[01]$/;
103     return $r+0;
104 }
105
106 our ($dsc_hash,$lastupl_hash);
107
108 sub generate_commit_from_dsc () {
109     my $ud = '.git/dgit/unpack';
110     rmtree($ud);
111     mkpath '.git/dgit';
112     mkdir $ud or die $!;
113     chdir $ud or die $!;
114     my @files;
115     foreach (split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files})) {
116         next unless m/\S/;
117         m/^\w+ \d+ (\S+)$/ or die "$_ ?";
118         my $f = $1;
119         die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
120         push @files, $f;
121         link "../../../$f", $f
122             or $!==&ENOENT
123             or die "$f $!";
124     }
125     runcmd qw(dget), @dget_opts, qw(--), $dscurl;
126     foreach my $f (grep { m/\.tar\.gz$/ } @files) {
127         link $f, "../../../$f"
128             or $!==&EEXIST
129             or die "$f $!";
130     }
131     my (@dirs) = <*/.>;
132     die unless @dirs==1;
133     $dirs[0] =~ m#^([^/]+)/\.$# or die;
134     my $dir = $1;
135     chdir $dir or die "$dir $!";
136     die if stat '.git';
137     die $! unless $!==&ENOENT;
138     runcmd qw(git init -q);
139     rmtree('.git/objects');
140     symlink '../../../../objects','.git/objects' or die $!;
141     runcmd qw(git add -Af);
142     my $tree = cmdoutput qw(git write-tree);
143     chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
144     runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
145     my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
146     my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
147     my $author = $clogp->{Maintainer};
148     $author =~ s#,.*##ms;
149     my $authline = "$author $date";
150     $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
151     open C, ">../commit.tmp" or die $!;
152     print C "tree $tree\n" or die $!;
153     print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
154     print C <<END or die $!;
155 author $authline
156 committer $authline
157
158 $clogp->{Changes}
159
160 # imported by dgit from the archive
161 END
162     close C or die $!;
163     print "synthesised git commit from .dsc $clogp->{Version}\n";
164     my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
165     chdir '../../../..' or die $!;
166     cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
167               'DGIT_ARCHIVE', $commithash;
168     cmdoutput qw(git log -n2), $commithash;
169     # ... gives git a chance to complain if our commit is malformed
170     my $outputhash = $commithash;
171     if (defined $lastupl_hash) {
172         chdir "$ud/$dir" or die $!;
173         runcmd qw(git reset --hard), $lastupl_hash;
174         runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
175         my $oldclogp = Dpkg::Control::Hash->new();
176         $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
177         my $vcmp =
178             version_compare_string($oldclogp->{Version}, $clogp->{Version});
179         if ($vcmp < 0) {
180             # git upload/ is earlier vsn than archive, use archive
181         } elsif ($vcmp >= 0) {
182             print STDERR <<END or die $!;
183 Version actually in archive:    $clogp->{Version} (older)
184 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
185 Perhaps the upload is stuck in incoming.  Using the version from git.
186 END
187         } else {
188             die "version in archive is same as version in git".
189                 " to-be-uploaded (upload/) branch but archive".
190                 " version hash no commit hash?!\n";
191         }
192         chdir '../../../..' or die $!;
193     }
194     rmtree($ud);
195     return $outputhash;
196 }
197
198 my $lastupl_ref = "refs/remotes/$remotename/upload/$suite";
199
200 sub fetch_from_archive () {
201     # ensures that $lastupl_ref is what is actually in the archive,
202     #  one way or another
203     $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
204     die $! if $!;
205     die $? unless ($?==0 && chomp $lastupl_hash) 
206         or ($?==128 && !length $lastupl_hash);
207     my $hash;
208     if (defined $dsc_hash) {
209         $hash = $dsc_hash;
210     } else {
211         $hash = generate_commit_from_dsc();
212     }
213     if ($lastupl_hash) {
214         my $mb = cmdoutput qw(git merge-base), $dsc_hash, $lastupl_hash;
215         die "not fast forward on last upload branch!".
216             " (archive's version left in DGIT_ARCHIVE)"
217             unless $mb eq $lastupl_hash;
218     }
219     if ($lastupl_ref ne $hash) {
220         cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
221     }
222 }
223
224 sub clone () {
225     get_archive_dsc();
226     $dsc_hash = $dsc->{'Vcs-git-master'};
227     if (defined $dsc_hash) {
228         $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
229         $dsc_hash = $&;
230         print "last upload to archive specified git hash\n";
231     } else {
232         print "last upload to archive has NO git hash\n";
233     }
234     my $dstdir = "$package";
235     if (check_for_git()) {
236         print "cloning existing git history\n";
237         runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
238             $alioth_git, $dstdir;
239         chdir "$dstdir" or die "$dstdir $!";
240         fetch_from_archive();
241         runcmd qw(git reset --hard), $lastupl_ref;
242     } else {
243         die "missing git history even though dsc has hash" if defined $dsc_hash;
244         print "starting new git history\n";
245         mkdir $dstdir or die "$dstdir $!";
246         chdir "$dstdir" or die "$dstdir $!";
247         runcmd qw(git init -q);
248         open H, "> .git/HEAD" or die $!;
249         print H "ref: refs/heads/$suite\n" or die $!;
250         close H or die $!;
251         runcmd qw(git remote add), $remotename, $alioth_git;
252         runcmd "git config branch.$suite.remote $remotename";
253         runcmd "git config branch.$suite.merge refs/heads/$suite";
254         my $newhash = generate_commit_from_dsc();
255         runcmd qw(git reset --hard), $newhash;
256     }
257     print "ready for work in $dstdir\n";
258 }
259
260 sub fetch () {
261     
262 }
263
264 sub cmd_clone {
265     if (@ARGV==1) {
266         ($package) = @ARGV;
267     } elsif (@ARGV==2) {
268         ($package,$suite) = @ARGV;
269     } else {
270         die;
271     }
272     clone();
273 }
274
275 sub parseopts () {
276     die if @ARGV && $ARGV[0] =~ m/^\-/;
277 }
278
279 parseopts();
280 die unless @ARGV;
281 my $cmd = shift @ARGV;
282 parseopts();
283
284 { no strict qw(refs); &{"cmd_$cmd"}(); }