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;
36 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
39 our (@dget) = qw(dget);
40 our (@dput) = qw(dput);
41 our (@debsign) = qw(debsign);
45 open DEBUG, ">/dev/null" or die $!;
47 our %opts_opt_map = ('dget' => \@dget,
49 'debsign' => \@debsign);
51 our $remotename = 'dgit';
52 our $ourdscfield = 'Vcs-Dgit-Master';
53 our $branchprefix = 'dgit';
55 sub lbranch () { return "$branchprefix/$suite"; }
56 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
57 sub lref () { return "refs/heads/".lbranch(); }
58 sub lrref () { return "refs/remotes/$remotename/$suite"; }
59 sub rrref () { return "refs/$branchprefix/$suite"; }
60 sub debiantag ($) { return "debian/$_[0]"; }
64 return "+".rrref().":".lrref();
71 $ua = LWP::UserAgent->new();
74 print "downloading @_...\n";
75 my $r = $ua->get(@_) or die $!;
76 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
77 return $r->decoded_content();
80 our ($dscdata,$dscurl,$dsc);
85 print $fh $intro or die $!;
89 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
90 print $fh " '$_'" or die $!;
92 print $fh " $_" or die $!;
95 print $fh "\n" or die $!;
99 printcmd(\*DEBUG,"+",@_) if $debug>0;
101 die "@_ $! $?" if system @_;
104 sub cmdoutput_errok {
105 die Dumper(\@_)." ?" if grep { !defined } @_;
106 printcmd(\*DEBUG,"|",@_) if $debug>0;
107 open P, "-|", @_ or die $!;
110 { local $/ = undef; $d = <P>; }
112 close P or return undef;
118 my $d = cmdoutput_errok @_;
119 defined $d or die "@_ $? $!";
124 printcmd(\*STDOUT,"#",@_);
127 sub runcmd_ordryrun {
135 our %defcfg = ('dgit.default.distro' => 'debian',
136 'dgit.default.username' => '',
137 'dgit.default.archive-query-default-component' => 'main',
138 'dgit.default.ssh' => 'ssh',
139 'dgit-distro.debian.git-host' => 'git.debian.org',
140 'dgit-distro.debian.git-proto' => 'git+ssh://',
141 'dgit-distro.debian.git-path' => '/git/dgit-repos',
142 'dgit-distro.debian.git-check' => 'ssh-cmd',
143 'dgit-distro.debian.git-create' => 'ssh-cmd',
144 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
150 local ($debug) = $debug-1;
151 $v = cmdoutput_errok(@git, qw(config --), $c);
159 my $dv = $defcfg{$c};
160 return $dv if defined $dv;
165 sub access_distro () {
166 return cfg("dgit-suite.$suite.distro",
167 "dgit.default.distro");
172 my $distro = access_distro();
173 my $value = cfg("dgit-distro.$distro.$key",
174 "dgit.default.$key");
178 sub access_gituserhost () {
179 my $user = access_cfg('git-user');
180 my $host = access_cfg('git-host');
181 return defined($user) && length($user) ? "$user\@$host" : $host;
184 sub access_giturl () {
185 my $url = access_cfg('git-url');
188 access_cfg('git-proto').
189 access_gituserhost().
190 access_cfg('git-path');
192 return "$url/$package.git";
196 my $c = Dpkg::Control::Hash->new();
197 $c->load(@_) or return undef;
202 my $c = Dpkg::Control::Hash->new();
203 my $p = new IO::Handle;
204 open $p, '-|', qw(dpkg-parsechangelog) or die $!;
206 $?=0; $!=0; close $p or die "$! $?";
212 sub archive_query () {
213 my $query = access_cfg('archive-query');
214 $query ||= "madison:".access_distro();
215 $query =~ s/^(\w+):// or die "$query ?";
218 die unless $proto eq 'madison';
219 $rmad ||= cmdoutput qw(rmadison -asource),"-s$suite","-u$url",$package;
220 $rmad =~ m{^ \s*( [^ \t|]+ )\s* \|
221 \s*( [^ \t|]+ )\s* \|
222 \s*( [^ \t|/]+ )(?:/([^ \t|/]+)) \s* \|
223 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
224 $1 eq $package or die "$rmad $package ?";
227 # madison canonicalises for us
228 print "canonical suite name for $suite is $3\n";
235 $component = access_cfg('archive-query-default-component');
237 $5 eq 'source' or die "$rmad ?";
238 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
239 my $subpath = "/pool/$component/$prefix/$package/${package}_$vsn.dsc";
240 return ($vsn,$subpath);
243 sub canonicalise_suite () {
247 sub get_archive_dsc () {
248 my ($vsn,$subpath) = archive_query();
249 # fixme madison does not show us the component
250 $dscurl = access_cfg('mirror').$subpath;
251 $dscdata = url_get($dscurl);
252 my $dscfh = new IO::File \$dscdata, '<' or die $!;
253 print DEBUG Dumper($dscdata) if $debug>1;
254 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
255 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
256 print DEBUG Dumper($dsc) if $debug>1;
257 my $fmt = $dsc->{Format};
258 die "unsupported format $fmt, sorry\n" unless $format_ok{$fmt};
261 sub check_for_git () {
263 my $how = access_cfg('git-check');
264 if ($how eq 'ssh-cmd') {
266 (access_cfg('ssh'),access_gituserhost(),
267 " set -e; cd ".access_cfg('git-path').";".
268 " if test -d $package.git; then echo 1; else echo 0; fi");
269 print DEBUG "got \`$r'\n";
270 die "$r $! $?" unless $r =~ m/^[01]$/;
273 die "unknown git-check $how ?";
277 sub create_remote_git_repo () {
278 my $how = access_cfg('git-create');
279 if ($how eq 'ssh-cmd') {
281 (access_cfg('ssh'),access_gituserhost(),
282 "set -e; cd ".access_cfg('git-path').";".
283 " mkdir -p $package.git;".
285 " if ! test -d objects; then git init --bare; fi");
287 die "unknown git-create $how ?";
291 our ($dsc_hash,$upload_hash);
293 our $ud = '.git/dgit/unpack';
301 sub mktree_in_ud_from_only_subdir () {
302 # changes into the subdir
305 $dirs[0] =~ m#^([^/]+)/\.$# or die;
307 chdir $dir or die "$dir $!";
309 die $! unless $!==&ENOENT;
310 runcmd qw(git init -q);
311 rmtree('.git/objects');
312 symlink '../../../../objects','.git/objects' or die $!;
313 runcmd @git, qw(add -Af);
314 my $tree = cmdoutput @git, qw(write-tree);
315 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
321 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
323 } grep m/\S/, split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files});
326 sub is_orig_file ($) {
328 m/\.orig(?:-\w+)?\.tar\.\w+$/;
331 sub generate_commit_from_dsc () {
335 foreach my $f (dsc_files()) {
336 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
338 link "../../../$f", $f
342 runcmd @dget, qw(--), $dscurl;
343 foreach my $f (grep { is_orig_file($_) } @files) {
344 link $f, "../../../../$f"
348 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
349 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
350 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
351 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
352 my $author = $clogp->{Maintainer};
353 $author =~ s#,.*##ms;
354 my $authline = "$author $date";
355 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
356 open C, ">../commit.tmp" or die $!;
357 print C "tree $tree\n" or die $!;
358 print C "parent $upload_hash\n" or die $! if $upload_hash;
359 print C <<END or die $!;
365 # imported by dgit from the archive
368 my $commithash = cmdoutput @git, qw(hash-object -w -t commit ../commit.tmp);
369 print "synthesised git commit from .dsc $clogp->{Version}\n";
370 chdir '../../../..' or die $!;
371 cmdoutput @git, qw(update-ref -m),"dgit synthesise $clogp->{Version}",
372 'DGIT_ARCHIVE', $commithash;
373 cmdoutput @git, qw(log -n2), $commithash;
374 # ... gives git a chance to complain if our commit is malformed
375 my $outputhash = $commithash;
377 chdir "$ud/$dir" or die $!;
378 runcmd @git, qw(reset --hard), $upload_hash;
379 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
380 my $oldclogp = Dpkg::Control::Hash->new();
381 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
383 version_compare_string($oldclogp->{Version}, $clogp->{Version});
385 # git upload/ is earlier vsn than archive, use archive
386 } elsif ($vcmp >= 0) {
387 print STDERR <<END or die $!;
388 Version actually in archive: $clogp->{Version} (older)
389 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
390 Perhaps the upload is stuck in incoming. Using the version from git.
393 die "version in archive is same as version in git".
394 " to-be-uploaded (upload/) branch but archive".
395 " version hash no commit hash?!\n";
397 chdir '../../../..' or die $!;
403 sub ensure_we_have_orig () {
404 foreach my $f (dsc_files()) {
405 next unless is_orig_file($f);
407 die "$f ?" unless -f _;
409 die "$f $!" unless $!==&ENOENT;
411 my $origurl = $dscurl;
412 $origurl =~ s{/[^/]+$}{};
414 die "$f ?" unless $f =~ m/^${package}_/;
415 die "$f ?" if $f =~ m#/#;
416 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
422 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
425 sub is_fast_fwd ($$) {
426 my ($ancestor,$child) = @_;
427 my $mb = cmdoutput @git, qw(merge-base), $dsc_hash, $upload_hash;
428 return rev_parse($mb) eq rev_parse($ancestor);
431 sub git_fetch_us () {
432 die "cannot dry run with fetch" if $dryrun;
433 runcmd @git, qw(fetch),access_giturl(),fetchspec();
436 sub fetch_from_archive () {
437 # ensures that lrref() is what is actually in the archive,
440 $dsc_hash = $dsc->{$ourdscfield};
441 if (defined $dsc_hash) {
442 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
444 print "last upload to archive specified git hash\n";
446 print "last upload to archive has NO git hash\n";
450 cmdoutput_errok @git, qw(show-ref --heads), lrref();
452 die unless chomp $upload_hash;
459 if (defined $dsc_hash) {
460 die "missing git history even though dsc has hash"
463 ensure_we_have_orig();
465 $hash = generate_commit_from_dsc();
468 die "not fast forward on last upload branch!".
469 " (archive's version left in DGIT_ARCHIVE)"
470 unless is_fast_fwd($dsc_hash, $upload_hash);
472 if ($upload_hash ne $hash) {
473 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
477 dryrun_report @upd_cmd;
484 die "dry run makes no sense with clone" if $dryrun;
485 mkdir $dstdir or die "$dstdir $!";
486 chdir "$dstdir" or die "$dstdir $!";
487 runcmd @git, qw(init -q);
488 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
489 open H, "> .git/HEAD" or die $!;
490 print H "ref: ".lref()."\n" or die $!;
492 runcmd @git, qw(remote add), 'origin', access_giturl();
493 if (check_for_git()) {
494 print "fetching existing git history\n";
496 runcmd @git, qw(fetch origin);
498 print "starting new git history\n";
500 fetch_from_archive();
501 runcmd @git, qw(reset --hard), lrref();
502 print "ready for work in $dstdir\n";
506 if (check_for_git()) {
509 fetch_from_archive();
514 runcmd_ordryrun @git, qw(merge -m),"Merge from $suite [dgit]",
519 runcmd @git, qw(diff --quiet HEAD);
520 my $clogp = parsechangelog();
521 $package = $clogp->{Source};
522 my $dscfn = "${package}_$clogp->{Version}.dsc";
523 stat "../$dscfn" or die "$dscfn $!";
524 $dsc = parsecontrol("../$dscfn");
527 print "checking that $dscfn corresponds to HEAD\n";
528 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
529 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
530 chdir '../../../..' or die $!;
531 runcmd @git, qw(diff --exit-code), $tree;
533 #do fast forward check and maybe fake merge
534 # if (!is_fast_fwd(mainbranch
535 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
536 # map { lref($_).":".rref($_) }
538 $dsc->{$ourdscfield} = rev_parse('HEAD');
539 $dsc->save("../$dscfn.tmp") or die $!;
541 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
543 print "[new .dsc left in $dscfn.tmp]\n";
546 my $pat = "../${package}_$clogp->{Version}_*.changes";
548 die "$pat ?" unless @cs==1;
549 ($changesfile) = @cs;
551 my $tag = debiantag($dsc->{Version});
552 if (!check_for_git()) {
553 create_remote_git_repo();
555 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
557 my @tag_cmd = (@git, qw(tag -s -m),
558 "Release $dsc->{Version} for $suite [dgit]");
559 push @tag_cmd, qw(-u),$keyid if defined $keyid;
561 runcmd_ordryrun @tag_cmd;
562 my @debsign_cmd = @debsign;
563 push @debsign_cmd, "-k$keyid" if defined $keyid;
564 push @debsign_cmd, $changesfile;
565 runcmd_ordryrun @debsign_cmd;
567 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
568 my $host = access_cfg('upload-host');
569 my @hostarg = defined($host) ? ($host,) : ();
570 runcmd_ordryrun @dput, @hostarg, $changesfile;
575 die if defined $package;
578 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
579 ($package,$suite) = @ARGV;
580 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
581 ($package,$dstdir) = @ARGV;
583 ($package,$suite,$dstdir) = @ARGV;
587 $dstdir ||= "$package";
592 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
594 if ($branch =~ m#$lbranch_re#o) {
601 sub fetchpullargs () {
602 if (!defined $package) {
603 my $sourcep = parsecontrol('debian/control');
604 $package = $sourcep->{Source};
607 $suite = branchsuite();
609 my $clogp = parsechangelog();
610 $suite = $clogp->{Distribution};
612 canonicalise_suite();
613 print "fetching from suite $suite\n";
616 canonicalise_suite();
633 die if defined $package;
634 my $clogp = parsechangelog();
635 $package = $clogp->{Source};
637 $suite = $clogp->{Distribution};
638 canonicalise_suite();
646 die if defined $package;
647 my $clogp = parsechangelog();
648 $suite = $clogp->{Distribution};
649 $package = $clogp->{Source};
650 canonicalise_suite();
652 qw(git-buildpackage -us -uc --git-no-sign-tags),
653 "--git-debian-branch=".lbranch(),
660 last unless $ARGV[0] =~ m/^-/;
664 if (m/^--dry-run$/) {
666 } elsif (m/^--no-sign$/) {
668 } elsif (m/^--(\w+)=(.*)/s && ($om = $opts_opt_map{$1})) {
670 } elsif (m/^--(\w+):(.*)/s && ($om = $opts_opt_map{$1})) {
680 open DEBUG, ">&STDERR" or die $!;
682 } elsif (s/^-c(.*=.*)//s) {
684 } elsif (s/^-C(.*)//s) {
686 } elsif (s/^-k(.*)//s) {
698 my $cmd = shift @ARGV;
701 { no strict qw(refs); &{"cmd_$cmd"}(); }