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 our $mirror = 'http://mirror.relativity.greenend.org.uk/mirror/debian-ftp/';
37 our $aliothname = 'iwj@git.debian.org';
38 our $aliothpath = '/git/dgit-repos';
39 our $alioth_git = "git+ssh://$aliothname/$aliothpath";
40 our $alioth_sshtestbodge = [$aliothname,$aliothpath];
43 our (@dget) = qw(dget);
44 our (@dput) = qw(dput);
45 our (@debsign) = qw(debsign);
48 open DEBUG, ">/dev/null" or die $!;
50 our %opts_opt_map = ('dget' => \@dget,
52 'debsign' => \@debsign);
54 our $remotename = 'dgit';
55 our $ourdscfield = 'Vcs-Git-Master';
56 our $branchprefix = 'dgit';
58 sub uploadbranch () { return "$branchprefix/$suite"; }
59 sub lref ($) { return "refs/heads/$_[0]"; }
60 sub rref ($) { return "refs/remotes/$remotename/$_[0]"; }
61 sub debiantag ($) { return "debian/$_[0]"; }
67 $ua = LWP::UserAgent->new();
70 print "fetching @_...\n";
71 my $r = $ua->get(@_) or die $!;
72 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
73 return $r->decoded_content();
76 our ($dscdata,$dscurl,$dsc);
81 print $fh $intro or die $!;
85 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
86 print $fh " '$_'" or die $!;
88 print $fh " $_" or die $!;
91 print $fh "\n" or die $!;
95 printcmd(\*DEBUG,"+",@_);
97 die "@_ $! $?" if system @_;
100 sub cmdoutput_errok {
101 printcmd(\*DEBUG,"|",@_);
102 open P, "-|", @_ or die $!;
105 { local $/ = undef; $d = <P>; }
107 close P or return undef;
113 my $d = cmdoutput_errok @_;
114 defined $d or die "@_ $? $!";
119 printcmd(\*STDOUT,"#",@_);
122 sub runcmd_ordryrun {
131 my $c = Dpkg::Control::Hash->new();
132 $c->load(@_) or return undef;
137 my $c = Dpkg::Control::Hash->new();
138 my $p = new IO::Handle;
139 open $p, '-|', qw(dpkg-parsechangelog) or die $!;
141 $?=0; $!=0; close $p or die "$! $?";
148 $rmad ||= cmdoutput qw(rmadison -asource),"-s$suite",$package;
149 $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
150 \s*( [^ \t|]+ )\s* \|
151 \s*( [^ \t|]+ )\s* \|
152 \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
153 $1 eq $package or die "$rmad $package ?";
156 # madison canonicalises for us
157 print "canonical suite name for $suite is $3\n";
160 $4 eq 'source' or die "$rmad ?";
164 sub canonicalise_suite () {
168 sub get_archive_dsc () {
169 my $vsn = askmadison();
170 # fixme madison does not show us the component
171 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
172 $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
173 $dscdata = url_get($dscurl);
174 my $dscfh = new IO::File \$dscdata, '<' or die $!;
175 print DEBUG Dumper($dscdata);
176 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
177 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
178 print DEBUG Dumper($dsc);
179 my $fmt = $dsc->{Format};
180 die "unsupported format $fmt, sorry\n" unless $fmt eq '1.0';
183 sub check_for_git () {
186 "ssh $alioth_sshtestbodge->[0] '".
187 " set -e; cd $alioth_sshtestbodge->[1];".
188 " if test -d $package.git; then echo 1; else echo 0; fi".
190 print DEBUG "$cmd\n";
191 open P, "$cmd |" or die $!;
193 my $r = <P>; close P;
194 print DEBUG ">$r<\n";
195 die "$r $! $?" unless $r =~ m/^[01]$/;
199 our ($dsc_hash,$upload_hash);
201 our $ud = '.git/dgit/unpack';
209 sub mktree_in_ud_from_only_subdir () {
210 # changes into the subdir
213 $dirs[0] =~ m#^([^/]+)/\.$# or die;
215 chdir $dir or die "$dir $!";
217 die $! unless $!==&ENOENT;
218 runcmd qw(git init -q);
219 rmtree('.git/objects');
220 symlink '../../../../objects','.git/objects' or die $!;
221 runcmd @git, qw(add -Af);
222 my $tree = cmdoutput @git, qw(write-tree);
223 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
229 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
231 } grep m/\S/, split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files});
234 sub is_orig_file ($) {
236 m/\.orig\.tar\.\w+$/;
239 sub generate_commit_from_dsc () {
243 foreach my $f (dsc_files()) {
244 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
246 link "../../../$f", $f
250 runcmd @dget, qw(--), $dscurl;
251 foreach my $f (grep { is_orig_file($_) } @files) {
252 link $f, "../../../../$f"
256 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
257 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
258 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
259 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
260 my $author = $clogp->{Maintainer};
261 $author =~ s#,.*##ms;
262 my $authline = "$author $date";
263 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
264 open C, ">../commit.tmp" or die $!;
265 print C "tree $tree\n" or die $!;
266 print C "parent $upload_hash\n" or die $! if $upload_hash;
267 print C <<END or die $!;
273 # imported by dgit from the archive
276 print "synthesised git commit from .dsc $clogp->{Version}\n";
277 my $commithash = cmdoutput @git, qw(hash-object -w -t commit ../commit.tmp);
278 chdir '../../../..' or die $!;
279 cmdoutput @git, qw(update-ref -m),"dgit synthesise $clogp->{Version}",
280 'DGIT_ARCHIVE', $commithash;
281 cmdoutput @git, qw(log -n2), $commithash;
282 # ... gives git a chance to complain if our commit is malformed
283 my $outputhash = $commithash;
285 chdir "$ud/$dir" or die $!;
286 runcmd @git, qw(reset --hard), $upload_hash;
287 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
288 my $oldclogp = Dpkg::Control::Hash->new();
289 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
291 version_compare_string($oldclogp->{Version}, $clogp->{Version});
293 # git upload/ is earlier vsn than archive, use archive
294 } elsif ($vcmp >= 0) {
295 print STDERR <<END or die $!;
296 Version actually in archive: $clogp->{Version} (older)
297 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
298 Perhaps the upload is stuck in incoming. Using the version from git.
301 die "version in archive is same as version in git".
302 " to-be-uploaded (upload/) branch but archive".
303 " version hash no commit hash?!\n";
305 chdir '../../../..' or die $!;
311 sub ensure_we_have_orig () {
312 foreach my $f (dsc_files()) {
313 next unless is_orig_file($f);
315 die "$f ?" unless -f _;
317 die "$f $!" unless $!==&ENOENT;
319 my $origurl = $dscurl;
320 $origurl =~ s{/[^/]+$}{};
322 die "$f ?" unless $f =~ m/^${package}_/;
323 die "$f ?" if $f =~ m#/#;
324 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
330 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
333 sub is_fast_fwd ($$) {
334 my ($ancestor,$child) = @_;
335 my $mb = cmdoutput @git, qw(merge-base), $dsc_hash, $upload_hash;
336 return rev_parse($mb) eq rev_parse($ancestor);
339 sub fetch_from_archive () {
340 # ensures that rref(uploadbranch()) is what is actually in the archive,
342 my $upload_ref = rref(uploadbranch());
344 cmdoutput_errok @git, qw(show-ref --heads), $upload_ref;
346 die $? unless ($?==0 && chomp $upload_hash)
347 or ($?==256 && !length $upload_hash);
350 if (defined $dsc_hash) {
351 die "missing git history even though dsc has hash"
354 ensure_we_have_orig();
356 $hash = generate_commit_from_dsc();
359 die "not fast forward on last upload branch!".
360 " (archive's version left in DGIT_ARCHIVE)"
361 unless is_fast_fwd($dsc_hash, $upload_hash);
363 if ($upload_hash ne $hash) {
364 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', $upload_ref, $hash);
368 dryrun_report @upd_cmd;
374 die "dry run makes no sense with clone" if $dryrun;
376 $dsc_hash = $dsc->{$ourdscfield};
377 if (defined $dsc_hash) {
378 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
380 print "last upload to archive specified git hash\n";
382 print "last upload to archive has NO git hash\n";
384 my $dstdir = "$package";
385 my $branch = uploadbranch();
386 if (check_for_git()) {
387 print "cloning existing git history\n";
388 runcmd @git, qw(clone --origin),$remotename, qw(-b), $branch, '--',
389 "$alioth_git/$package.git", $dstdir;
390 chdir "$dstdir" or die "$dstdir $!";
391 fetch_from_archive();
392 runcmd @git, qw(reset --hard), rref(uploadbranch());
394 print "starting new git history\n";
395 mkdir $dstdir or die "$dstdir $!";
396 chdir "$dstdir" or die "$dstdir $!";
397 runcmd @git, qw(init -q);
398 open H, "> .git/HEAD" or die $!;
399 print H "ref: ".lref(uploadbranch())."\n" or die $!;
401 runcmd @git, qw(remote add), $remotename, "$alioth_git/$package.git";
402 runcmd "git config branch.$branch.remote $remotename";
403 runcmd "git config branch.$branch.merge ".lref(uploadbranch());
404 my $newhash = generate_commit_from_dsc();
405 runcmd @git, qw(reset --hard), $newhash;
407 print "ready for work in $dstdir\n";
412 if (check_for_git()) {
413 runcmd_ordryrun @git, qw(fetch -p),$remotename,
414 '+refs/heads/*:refs/remotes/origin/*';
416 fetch_from_archive();
421 runcmd_ordryrun @git, qw(merge -m),"Merge from $suite [dgit]",
422 lref(uploadbranch());
426 runcmd @git, qw(diff --quiet HEAD);
427 my $clogp = parsechangelog();
428 $package = $clogp->{Source};
429 my $dscfn = "${package}_$clogp->{Version}.dsc";
430 stat "../$dscfn" or die "$dscfn $!";
431 $dsc = parsecontrol("../$dscfn");
434 print "checking that $dscfn corresponds to HEAD\n";
435 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
436 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
437 chdir '../../../..' or die $!;
438 runcmd @git, qw(diff --exit-code), $tree;
440 #do fast forward check and maybe fake merge
441 # if (!is_fast_fwd(mainbranch
442 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
443 # map { lref($_).":".rref($_) }
445 $dsc->{$ourdscfield} = rev_parse('HEAD');
446 $dsc->save("../$dscfn.tmp") or die $!;
448 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
450 print "[new .dsc left in $dscfn.tmp]\n";
453 my $pat = "../${package}_$clogp->{Version}_*.changes";
455 die "$pat ?" unless @cs==1;
456 ($changesfile) = @cs;
458 my $tag = debiantag($dsc->{Version});
459 if (!check_for_git()) {
460 runcmd_ordryrun qw(ssh),$alioth_sshtestbodge->[0],
461 "set -e; cd $alioth_sshtestbodge->[1];".
462 " mkdir -p $package.git;".
464 " if ! test -d objects; then git init --bare; fi";
466 runcmd_ordryrun @git, qw(push),$remotename,"HEAD:".lref(uploadbranch());
468 my @tag_cmd = (@git, qw(tag -s -m),
469 "Release $dsc->{Version} for $suite [dgit]");
470 push @tag_cmd, qw(-u),$keyid if defined $keyid;
472 runcmd_ordryrun @tag_cmd;
473 my @debsign_cmd = @debsign;
474 push @debsign_cmd, "-k$keyid" if defined $keyid;
475 push @debsign_cmd, $changesfile;
476 runcmd_ordryrun @debsign_cmd;
478 runcmd_ordryrun @git, qw(push),$remotename,"refs/tags/$tag";
479 runcmd_ordryrun @dput, $changesfile;
486 ($package,$suite) = @ARGV;
494 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
496 if ($branch =~ m#^refs/heads/$branchprefix/([^/.]+)$#o) {
503 sub fetchpullargs () {
504 my $clogp = parsechangelog();
505 $package = $clogp->{Source};
507 $suite = branchsuite();
508 $suite ||= $clogp->{Distribution};
509 canonicalise_suite();
510 print "fetching from suite $suite\n";
513 canonicalise_suite();
530 my $clogp = parsechangelog();
531 $package = $clogp->{Source};
533 $suite = $clogp->{Distribution};
534 canonicalise_suite();
542 my $clogp = parsechangelog();
543 $suite = $clogp->{Distribution};
544 $package = $clogp->{Source};
545 canonicalise_suite();
547 qw(git-buildpackage -us -uc --git-no-sign-tags),
548 "--git-debian-branch=".uploadbranch(),
555 last unless $ARGV[0] =~ m/^-/;
559 if (m/^--dry-run$/) {
561 } elsif (m/^--no-sign$/) {
563 } elsif (m/^--(\w+)=(.*)/s && ($om = $opts_opt_map{$1})) {
565 } elsif (m/^--(\w+):(.*)/s && ($om = $opts_opt_map{$1})) {
575 open DEBUG, ">&STDERR" or die $!;
576 } elsif (s/^-c(.*=.*)//s) {
578 } elsif (s/^-C(.*)//s) {
580 } elsif (s/^-k(.*)//s) {
592 my $cmd = shift @ARGV;
595 { no strict qw(refs); &{"cmd_$cmd"}(); }