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];
42 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
45 our (@dget) = qw(dget);
46 our (@dput) = qw(dput);
47 our (@debsign) = qw(debsign);
50 open DEBUG, ">/dev/null" or die $!;
52 our %opts_opt_map = ('dget' => \@dget,
54 'debsign' => \@debsign);
56 our $remotename = 'dgit';
57 our $ourdscfield = 'Vcs-Git-Master';
58 our $branchprefix = 'dgit';
60 sub lbranch () { return "$branchprefix/$suite"; }
61 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
62 sub lref () { return "refs/heads/".lbranch(); }
63 sub lrref () { return "refs/remotes/$remotename/$suite"; }
64 sub rrref () { return "refs/$branchprefix/$suite"; }
65 sub debiantag ($) { return "debian/$_[0]"; }
69 return "+".rrref().":".lrref();
76 $ua = LWP::UserAgent->new();
79 print "downloading @_...\n";
80 my $r = $ua->get(@_) or die $!;
81 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
82 return $r->decoded_content();
85 our ($dscdata,$dscurl,$dsc);
90 print $fh $intro or die $!;
94 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
95 print $fh " '$_'" or die $!;
97 print $fh " $_" or die $!;
100 print $fh "\n" or die $!;
104 printcmd(\*DEBUG,"+",@_);
106 die "@_ $! $?" if system @_;
109 sub cmdoutput_errok {
110 printcmd(\*DEBUG,"|",@_);
111 open P, "-|", @_ or die $!;
114 { local $/ = undef; $d = <P>; }
116 close P or return undef;
122 my $d = cmdoutput_errok @_;
123 defined $d or die "@_ $? $!";
128 printcmd(\*STDOUT,"#",@_);
131 sub runcmd_ordryrun {
140 my $c = Dpkg::Control::Hash->new();
141 $c->load(@_) or return undef;
146 my $c = Dpkg::Control::Hash->new();
147 my $p = new IO::Handle;
148 open $p, '-|', qw(dpkg-parsechangelog) or die $!;
150 $?=0; $!=0; close $p or die "$! $?";
157 $rmad ||= cmdoutput qw(rmadison -asource),"-s$suite",$package;
158 $rmad =~ m/^ \s*( [^ \t|]+ )\s* \|
159 \s*( [^ \t|]+ )\s* \|
160 \s*( [^ \t|]+ )\s* \|
161 \s*( [^ \t|]+ )\s* /x or die "$rmad $?";
162 $1 eq $package or die "$rmad $package ?";
165 # madison canonicalises for us
166 print "canonical suite name for $suite is $3\n";
169 $4 eq 'source' or die "$rmad ?";
173 sub canonicalise_suite () {
177 sub get_archive_dsc () {
178 my $vsn = askmadison();
179 # fixme madison does not show us the component
180 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
181 $dscurl = "$mirror/pool/main/$prefix/$package/${package}_$vsn.dsc";
182 $dscdata = url_get($dscurl);
183 my $dscfh = new IO::File \$dscdata, '<' or die $!;
184 print DEBUG Dumper($dscdata);
185 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
186 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
187 print DEBUG Dumper($dsc);
188 my $fmt = $dsc->{Format};
189 die "unsupported format $fmt, sorry\n" unless $format_ok{$fmt};
192 sub check_for_git () {
195 "ssh $alioth_sshtestbodge->[0] '".
196 " set -e; cd $alioth_sshtestbodge->[1];".
197 " if test -d $package.git; then echo 1; else echo 0; fi".
199 print DEBUG "$cmd\n";
200 open P, "$cmd |" or die $!;
202 my $r = <P>; close P;
203 print DEBUG ">$r<\n";
204 die "$r $! $?" unless $r =~ m/^[01]$/;
208 our ($dsc_hash,$upload_hash);
210 our $ud = '.git/dgit/unpack';
218 sub mktree_in_ud_from_only_subdir () {
219 # changes into the subdir
222 $dirs[0] =~ m#^([^/]+)/\.$# or die;
224 chdir $dir or die "$dir $!";
226 die $! unless $!==&ENOENT;
227 runcmd qw(git init -q);
228 rmtree('.git/objects');
229 symlink '../../../../objects','.git/objects' or die $!;
230 runcmd @git, qw(add -Af);
231 my $tree = cmdoutput @git, qw(write-tree);
232 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
238 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
240 } grep m/\S/, split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files});
243 sub is_orig_file ($) {
245 m/\.orig(?:-\w+)?\.tar\.\w+$/;
248 sub generate_commit_from_dsc () {
252 foreach my $f (dsc_files()) {
253 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
255 link "../../../$f", $f
259 runcmd @dget, qw(--), $dscurl;
260 foreach my $f (grep { is_orig_file($_) } @files) {
261 link $f, "../../../../$f"
265 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
266 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
267 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
268 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
269 my $author = $clogp->{Maintainer};
270 $author =~ s#,.*##ms;
271 my $authline = "$author $date";
272 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
273 open C, ">../commit.tmp" or die $!;
274 print C "tree $tree\n" or die $!;
275 print C "parent $upload_hash\n" or die $! if $upload_hash;
276 print C <<END or die $!;
282 # imported by dgit from the archive
285 my $commithash = cmdoutput @git, qw(hash-object -w -t commit ../commit.tmp);
286 print "synthesised git commit from .dsc $clogp->{Version}\n";
287 chdir '../../../..' or die $!;
288 cmdoutput @git, qw(update-ref -m),"dgit synthesise $clogp->{Version}",
289 'DGIT_ARCHIVE', $commithash;
290 cmdoutput @git, qw(log -n2), $commithash;
291 # ... gives git a chance to complain if our commit is malformed
292 my $outputhash = $commithash;
294 chdir "$ud/$dir" or die $!;
295 runcmd @git, qw(reset --hard), $upload_hash;
296 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
297 my $oldclogp = Dpkg::Control::Hash->new();
298 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
300 version_compare_string($oldclogp->{Version}, $clogp->{Version});
302 # git upload/ is earlier vsn than archive, use archive
303 } elsif ($vcmp >= 0) {
304 print STDERR <<END or die $!;
305 Version actually in archive: $clogp->{Version} (older)
306 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
307 Perhaps the upload is stuck in incoming. Using the version from git.
310 die "version in archive is same as version in git".
311 " to-be-uploaded (upload/) branch but archive".
312 " version hash no commit hash?!\n";
314 chdir '../../../..' or die $!;
320 sub ensure_we_have_orig () {
321 foreach my $f (dsc_files()) {
322 next unless is_orig_file($f);
324 die "$f ?" unless -f _;
326 die "$f $!" unless $!==&ENOENT;
328 my $origurl = $dscurl;
329 $origurl =~ s{/[^/]+$}{};
331 die "$f ?" unless $f =~ m/^${package}_/;
332 die "$f ?" if $f =~ m#/#;
333 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
339 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
342 sub is_fast_fwd ($$) {
343 my ($ancestor,$child) = @_;
344 my $mb = cmdoutput @git, qw(merge-base), $dsc_hash, $upload_hash;
345 return rev_parse($mb) eq rev_parse($ancestor);
348 sub git_fetch_us () {
349 runcmd_ordryrun @git, qw(fetch),$remotename,fetchspec();
352 sub fetch_from_archive () {
353 # ensures that lrref() is what is actually in the archive,
356 $dsc_hash = $dsc->{$ourdscfield};
357 if (defined $dsc_hash) {
358 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
360 print "last upload to archive specified git hash\n";
362 print "last upload to archive has NO git hash\n";
366 cmdoutput_errok @git, qw(show-ref --heads), lrref();
368 die $? unless ($?==0 && chomp $upload_hash)
369 or ($?==256 && !length $upload_hash);
372 if (defined $dsc_hash) {
373 die "missing git history even though dsc has hash"
376 ensure_we_have_orig();
378 $hash = generate_commit_from_dsc();
381 die "not fast forward on last upload branch!".
382 " (archive's version left in DGIT_ARCHIVE)"
383 unless is_fast_fwd($dsc_hash, $upload_hash);
385 if ($upload_hash ne $hash) {
386 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
390 dryrun_report @upd_cmd;
397 die "dry run makes no sense with clone" if $dryrun;
398 mkdir $dstdir or die "$dstdir $!";
399 chdir "$dstdir" or die "$dstdir $!";
400 runcmd @git, qw(init -q);
401 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
402 open H, "> .git/HEAD" or die $!;
403 print H "ref: ".lref()."\n" or die $!;
405 runcmd @git, qw(remote add), 'origin', "$alioth_git/$package.git";
406 if (check_for_git()) {
407 print "fetching existing git history\n";
409 runcmd @git, qw(fetch origin);
411 print "starting new git history\n";
413 fetch_from_archive();
414 runcmd @git, qw(reset --hard), lrref();
415 print "ready for work in $dstdir\n";
419 if (check_for_git()) {
422 fetch_from_archive();
427 runcmd_ordryrun @git, qw(merge -m),"Merge from $suite [dgit]",
432 runcmd @git, qw(diff --quiet HEAD);
433 my $clogp = parsechangelog();
434 $package = $clogp->{Source};
435 my $dscfn = "${package}_$clogp->{Version}.dsc";
436 stat "../$dscfn" or die "$dscfn $!";
437 $dsc = parsecontrol("../$dscfn");
440 print "checking that $dscfn corresponds to HEAD\n";
441 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
442 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
443 chdir '../../../..' or die $!;
444 runcmd @git, qw(diff --exit-code), $tree;
446 #do fast forward check and maybe fake merge
447 # if (!is_fast_fwd(mainbranch
448 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
449 # map { lref($_).":".rref($_) }
451 $dsc->{$ourdscfield} = rev_parse('HEAD');
452 $dsc->save("../$dscfn.tmp") or die $!;
454 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
456 print "[new .dsc left in $dscfn.tmp]\n";
459 my $pat = "../${package}_$clogp->{Version}_*.changes";
461 die "$pat ?" unless @cs==1;
462 ($changesfile) = @cs;
464 my $tag = debiantag($dsc->{Version});
465 if (!check_for_git()) {
466 runcmd_ordryrun qw(ssh),$alioth_sshtestbodge->[0],
467 "set -e; cd $alioth_sshtestbodge->[1];".
468 " mkdir -p $package.git;".
470 " if ! test -d objects; then git init --bare; fi";
472 runcmd_ordryrun @git, qw(push),$remotename,"HEAD:".rrref();
474 my @tag_cmd = (@git, qw(tag -s -m),
475 "Release $dsc->{Version} for $suite [dgit]");
476 push @tag_cmd, qw(-u),$keyid if defined $keyid;
478 runcmd_ordryrun @tag_cmd;
479 my @debsign_cmd = @debsign;
480 push @debsign_cmd, "-k$keyid" if defined $keyid;
481 push @debsign_cmd, $changesfile;
482 runcmd_ordryrun @debsign_cmd;
484 runcmd_ordryrun @git, qw(push),$remotename,"refs/tags/$tag";
485 runcmd_ordryrun @dput, $changesfile;
492 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
493 ($package,$suite) = @ARGV;
494 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
495 ($package,$dstdir) = @ARGV;
497 ($package,$suite,$dstdir) = @ARGV;
501 $dstdir ||= "$package";
506 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
508 if ($branch =~ m#$lbranch_re#o) {
515 sub fetchpullargs () {
516 my $clogp = parsechangelog();
517 $package = $clogp->{Source};
519 $suite = branchsuite();
520 $suite ||= $clogp->{Distribution};
521 canonicalise_suite();
522 print "fetching from suite $suite\n";
525 canonicalise_suite();
542 my $clogp = parsechangelog();
543 $package = $clogp->{Source};
545 $suite = $clogp->{Distribution};
546 canonicalise_suite();
554 my $clogp = parsechangelog();
555 $suite = $clogp->{Distribution};
556 $package = $clogp->{Source};
557 canonicalise_suite();
559 qw(git-buildpackage -us -uc --git-no-sign-tags),
560 "--git-debian-branch=".lbranch(),
567 last unless $ARGV[0] =~ m/^-/;
571 if (m/^--dry-run$/) {
573 } elsif (m/^--no-sign$/) {
575 } elsif (m/^--(\w+)=(.*)/s && ($om = $opts_opt_map{$1})) {
577 } elsif (m/^--(\w+):(.*)/s && ($om = $opts_opt_map{$1})) {
587 open DEBUG, ">&STDERR" or die $!;
588 } elsif (s/^-c(.*=.*)//s) {
590 } elsif (s/^-C(.*)//s) {
592 } elsif (s/^-k(.*)//s) {
604 my $cmd = shift @ARGV;
607 { no strict qw(refs); &{"cmd_$cmd"}(); }