3 # Integration between git and Debian-style archives
5 # Copyright (C)2013 Ian Jackson
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.
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.
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/>.
25 use Dpkg::Control::Hash;
29 open DEBUG, ">&STDERR" or die $!;
31 our $mirror = 'http://mirror.relativity.greenend.org.uk/mirror/debian-ftp/';
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];
40 our (@dget_opts) = qw(-u);
42 our $remotename = 'dgit';
44 sub mainbranch () { return "$suite"; }
45 sub uploadbranch () { return "upload/$suite"; }
51 $ua = LWP::UserAgent->new();
54 print DEBUG "fetching @_...\n";
55 my $r = $ua->get(@_) or die $!;
56 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
57 return $r->decoded_content();
60 our ($dscdata,$dscurl,$dsc);
64 die "@_ $! $?" if system @_;
68 open P, "-|", @_ or die $!;
71 { local $/ = undef; $d = <P>; }
73 close P or die "@_ $? $!";
79 my $c = Dpkg::Control::Hash->new();
80 $c->load(@_) or return undef;
85 my $c = Dpkg::Control::Hash->new();
86 my $p = new IO::File '-|', qw(dpkg-parsechangelog) or die $!;
88 $?=0; $!=0; close $p or die "$! $?";
92 sub get_archive_dsc () {
93 my $rmad = cmdoutput qw(rmadison -asource),"-s$suite",$package;
94 $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
97 \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
98 $1 eq $package or die "$rmad $package ?";
100 $3 eq $suite or die "$rmad $suite ?";
101 $4 eq 'source' or die "$rmad ?";
102 # fixme it does not show us the component ?
103 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
104 $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
105 #print DEBUG Dumper($pdodata, $&, $dscurl);
106 $dscdata = url_get($dscurl);
107 my $dscfh = new IO::File \$dscdata, '<' or die $!;
108 #print DEBUG Dumper($dscdata, $dscfh);
109 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
110 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
111 #print DEBUG Dumper($dsc);
112 my $fmt = $dsc->{Format};
113 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
116 sub check_for_git () {
119 "ssh $alioth_sshtestbodge->[0] '".
120 " set -e; cd /git/dgit-test;".
121 " if test -d $package.git; then echo 1; else echo 0; fi".
123 #print DEBUG "$cmd\n";
124 open P, "$cmd |" or die $!;
126 my $r = <P>; close P;
127 #print STDERR ">$r<\n";
128 die "$r $! $?" unless $r =~ m/^[01]$/;
132 our ($dsc_hash,$lastupl_hash);
134 our $ud = '.git/dgit/unpack';
142 sub mktree_in_ud_from_only_subdir () {
143 # changes into the subdir
146 $dirs[0] =~ m#^([^/]+)/\.$# or die;
148 chdir $dir or die "$dir $!";
150 die $! unless $!==&ENOENT;
151 runcmd qw(git init -q);
152 rmtree('.git/objects');
153 symlink '../../../../objects','.git/objects' or die $!;
154 runcmd qw(git add -Af);
155 my $tree = cmdoutput qw(git write-tree);
156 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
160 sub generate_commit_from_dsc () {
164 foreach (split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files})) {
166 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
168 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
170 link "../../../$f", $f
174 runcmd qw(dget), @dget_opts, qw(--), $dscurl;
175 foreach my $f (grep { m/\.tar\.gz$/ } @files) {
176 link $f, "../../../$f"
180 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
181 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
182 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
183 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
184 my $author = $clogp->{Maintainer};
185 $author =~ s#,.*##ms;
186 my $authline = "$author $date";
187 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
188 open C, ">../commit.tmp" or die $!;
189 print C "tree $tree\n" or die $!;
190 print C "parent $lastupl_hash\n" or die $! if defined $lastupl_hash;
191 print C <<END or die $!;
197 # imported by dgit from the archive
200 print "synthesised git commit from .dsc $clogp->{Version}\n";
201 my $commithash = cmdoutput qw(git hash-object -w -t commit ../commit.tmp);
202 chdir '../../../..' or die $!;
203 cmdoutput qw(git update-ref -m),"dgit synthesise $clogp->{Version}",
204 'DGIT_ARCHIVE', $commithash;
205 cmdoutput qw(git log -n2), $commithash;
206 # ... gives git a chance to complain if our commit is malformed
207 my $outputhash = $commithash;
208 if (defined $lastupl_hash) {
209 chdir "$ud/$dir" or die $!;
210 runcmd qw(git reset --hard), $lastupl_hash;
211 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
212 my $oldclogp = Dpkg::Control::Hash->new();
213 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
215 version_compare_string($oldclogp->{Version}, $clogp->{Version});
217 # git upload/ is earlier vsn than archive, use archive
218 } elsif ($vcmp >= 0) {
219 print STDERR <<END or die $!;
220 Version actually in archive: $clogp->{Version} (older)
221 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
222 Perhaps the upload is stuck in incoming. Using the version from git.
225 die "version in archive is same as version in git".
226 " to-be-uploaded (upload/) branch but archive".
227 " version hash no commit hash?!\n";
229 chdir '../../../..' or die $!;
235 my $lastupl_ref = "refs/remotes/$remotename/upload/$suite";
238 return cmdoutput qw(git rev-parse --), "$_[0]~0";
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);
247 sub fetch_from_archive () {
248 # ensures that $lastupl_ref is what is actually in the archive,
250 $!=0; $lastupl_hash = `git show-ref --heads $lastupl_ref`;
252 die $? unless ($?==0 && chomp $lastupl_hash)
253 or ($?==128 && !length $lastupl_hash);
255 if (defined $dsc_hash) {
258 $hash = generate_commit_from_dsc();
261 die "not fast forward on last upload branch!".
262 " (archive's version left in DGIT_ARCHIVE)"
263 unless is_fast_fwd($lastupl_hash, $dsc_hash);
265 if ($lastupl_ref ne $hash) {
266 cmdoutput qw(git update-ref -m), 'dgit fetch', $lastupl_ref, $hash;
272 $dsc_hash = $dsc->{'Vcs-git-master'};
273 if (defined $dsc_hash) {
274 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
276 print "last upload to archive specified git hash\n";
278 print "last upload to archive has NO git hash\n";
280 my $dstdir = "$package";
281 if (check_for_git()) {
282 print "cloning existing git history\n";
283 runcmd qw(git clone --origin),$remotename, qw(-b), $suite, '--',
284 $alioth_git, $dstdir;
285 chdir "$dstdir" or die "$dstdir $!";
286 fetch_from_archive();
287 runcmd qw(git reset --hard), $lastupl_ref;
289 die "missing git history even though dsc has hash" if defined $dsc_hash;
290 print "starting new git history\n";
291 mkdir $dstdir or die "$dstdir $!";
292 chdir "$dstdir" or die "$dstdir $!";
293 runcmd qw(git init -q);
294 open H, "> .git/HEAD" or die $!;
295 print H "ref: refs/heads/$suite\n" or die $!;
297 runcmd qw(git remote add), $remotename, $alioth_git;
298 runcmd "git config branch.$suite.remote $remotename";
299 runcmd "git config branch.$suite.merge refs/heads/$suite";
300 my $newhash = generate_commit_from_dsc();
301 runcmd qw(git reset --hard), $newhash;
303 print "ready for work in $dstdir\n";
311 runcmd qw(git diff --quiet HEAD);
312 my $clogp = parsechangelog();
313 $package = $clogp->{Source};
314 my $dscfn = "${package}_$clogp->{Version}.dsc";
315 stat $dscfn or die "$dscfn $!";
316 $dsc = parsecontrol("../$dscfn");
319 print "checking that $dscfn corresponds to HEAD\n";
320 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
321 my $tree = mktree_in_ud_from_only_subdir();
322 chdir '../../../..' or die $!;
323 runcmd qw(git diff --exit-code), $tree;
324 runcmd qw(git fetch), $alioth_git,
325 map { "$_:refs/remotes/$remotename/$_" }
326 (mainbranch(), uploadbranch());
328 if (!is_fast_fwd(mainbranch
329 xxx introduce remote_lref
330 xxx use remote_lref everywhere
333 do fast forward check and maybe fake merge
344 ($package,$suite) = @ARGV;
352 die if @ARGV && $ARGV[0] =~ m/^\-/;
357 my $cmd = shift @ARGV;
360 { no strict qw(refs); &{"cmd_$cmd"}(); }