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;
37 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
40 our (@dget) = qw(dget);
41 our (@dput) = qw(dput);
42 our (@debsign) = qw(debsign);
46 open DEBUG, ">/dev/null" or die $!;
48 our %opts_opt_map = ('dget' => \@dget,
50 'debsign' => \@debsign);
52 our $remotename = 'dgit';
53 our $ourdscfield = 'Vcs-Dgit-Master';
54 our $branchprefix = 'dgit';
56 sub lbranch () { return "$branchprefix/$suite"; }
57 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
58 sub lref () { return "refs/heads/".lbranch(); }
59 sub lrref () { return "refs/remotes/$remotename/$suite"; }
60 sub rrref () { return "refs/$branchprefix/$suite"; }
61 sub debiantag ($) { return "debian/$_[0]"; }
65 return "+".rrref().":".lrref();
72 $ua = LWP::UserAgent->new();
75 print "downloading @_...\n";
76 my $r = $ua->get(@_) or die $!;
77 die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
78 return $r->decoded_content();
81 our ($dscdata,$dscurl,$dsc);
86 print $fh $intro or die $!;
90 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
91 print $fh " '$_'" or die $!;
93 print $fh " $_" or die $!;
96 print $fh "\n" or die $!;
100 printcmd(\*DEBUG,"+",@_) if $debug>0;
102 die "@_ $! $?" if system @_;
105 sub cmdoutput_errok {
106 die Dumper(\@_)." ?" if grep { !defined } @_;
107 printcmd(\*DEBUG,"|",@_) if $debug>0;
108 open P, "-|", @_ or die $!;
111 { local $/ = undef; $d = <P>; }
113 close P or return undef;
119 my $d = cmdoutput_errok @_;
120 defined $d or die "@_ $? $!";
125 printcmd(\*STDOUT,"#",@_);
128 sub runcmd_ordryrun {
136 our %defcfg = ('dgit.default.distro' => 'debian',
137 'dgit.default.username' => '',
138 'dgit.default.archive-query-default-component' => 'main',
139 'dgit.default.ssh' => 'ssh',
140 'dgit-distro.debian.git-host' => 'git.debian.org',
141 'dgit-distro.debian.git-proto' => 'git+ssh://',
142 'dgit-distro.debian.git-path' => '/git/dgit-repos',
143 'dgit-distro.debian.git-check' => 'ssh-cmd',
144 'dgit-distro.debian.git-create' => 'ssh-cmd',
145 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
151 local ($debug) = $debug-1;
152 $v = cmdoutput_errok(@git, qw(config --), $c);
160 my $dv = $defcfg{$c};
161 return $dv if defined $dv;
166 sub access_distro () {
167 return cfg("dgit-suite.$suite.distro",
168 "dgit.default.distro");
173 my $distro = access_distro();
174 my $value = cfg("dgit-distro.$distro.$key",
175 "dgit.default.$key");
179 sub access_gituserhost () {
180 my $user = access_cfg('git-user');
181 my $host = access_cfg('git-host');
182 return defined($user) && length($user) ? "$user\@$host" : $host;
185 sub access_giturl () {
186 my $url = access_cfg('git-url');
189 access_cfg('git-proto').
190 access_gituserhost().
191 access_cfg('git-path');
193 return "$url/$package.git";
197 my $c = Dpkg::Control::Hash->new();
198 $c->load(@_) or return undef;
203 my $c = Dpkg::Control::Hash->new();
204 my $p = new IO::Handle;
205 open $p, '-|', qw(dpkg-parsechangelog) or die $!;
207 $?=0; $!=0; close $p or die "$! $?";
213 sub archive_query () {
214 my $query = access_cfg('archive-query');
215 $query ||= "madison:".access_distro();
216 $query =~ s/^(\w+):// or die "$query ?";
219 die unless $proto eq 'madison';
220 $rmad ||= cmdoutput qw(rmadison -asource),"-s$suite","-u$url",$package;
224 $rmad =~ m{^ \s*( [^ \t|]+ )\s* \|
225 \s*( [^ \t|]+ )\s* \|
226 \s*( [^ \t|/]+ )(?:/([^ \t|/]+)) \s* \|
227 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
228 $1 eq $package or die "$rmad $package ?";
231 # madison canonicalises for us
232 print "canonical suite name for $suite is $3\n";
239 $component = access_cfg('archive-query-default-component');
241 $5 eq 'source' or die "$rmad ?";
242 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
243 my $subpath = "/pool/$component/$prefix/$package/${package}_$vsn.dsc";
244 return ($vsn,$subpath);
247 sub canonicalise_suite () {
251 sub get_archive_dsc () {
252 my ($vsn,$subpath) = archive_query();
253 if (!defined $vsn) { $dsc=undef; return undef; }
254 $dscurl = access_cfg('mirror').$subpath;
255 $dscdata = url_get($dscurl);
256 my $dscfh = new IO::File \$dscdata, '<' or die $!;
257 print DEBUG Dumper($dscdata) if $debug>1;
258 $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
259 $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
260 print DEBUG Dumper($dsc) if $debug>1;
261 my $fmt = $dsc->{Format};
262 die "unsupported format $fmt, sorry\n" unless $format_ok{$fmt};
265 sub check_for_git () {
267 my $how = access_cfg('git-check');
268 if ($how eq 'ssh-cmd') {
270 (access_cfg('ssh'),access_gituserhost(),
271 " set -e; cd ".access_cfg('git-path').";".
272 " if test -d $package.git; then echo 1; else echo 0; fi");
273 print DEBUG "got \`$r'\n";
274 die "$r $! $?" unless $r =~ m/^[01]$/;
277 die "unknown git-check $how ?";
281 sub create_remote_git_repo () {
282 my $how = access_cfg('git-create');
283 if ($how eq 'ssh-cmd') {
285 (access_cfg('ssh'),access_gituserhost(),
286 "set -e; cd ".access_cfg('git-path').";".
287 " mkdir -p $package.git;".
289 " if ! test -d objects; then git init --bare; fi");
291 die "unknown git-create $how ?";
295 our ($dsc_hash,$upload_hash);
297 our $ud = '.git/dgit/unpack';
305 sub mktree_in_ud_from_only_subdir () {
306 # changes into the subdir
309 $dirs[0] =~ m#^([^/]+)/\.$# or die;
311 chdir $dir or die "$dir $!";
313 die $! unless $!==&ENOENT;
314 runcmd qw(git init -q);
315 rmtree('.git/objects');
316 symlink '../../../../objects','.git/objects' or die $!;
317 runcmd @git, qw(add -Af);
318 my $tree = cmdoutput @git, qw(write-tree);
319 chomp $tree; $tree =~ m/^\w+$/ or die "$tree ?";
325 m/^\w+ \d+ (\S+)$/ or die "$_ ?";
327 } grep m/\S/, split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files});
330 sub is_orig_file ($) {
332 m/\.orig(?:-\w+)?\.tar\.\w+$/;
335 sub generate_commit_from_dsc () {
339 foreach my $f (dsc_files()) {
340 die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
342 link "../../../$f", $f
346 runcmd @dget, qw(--), $dscurl;
347 foreach my $f (grep { is_orig_file($_) } @files) {
348 link $f, "../../../../$f"
352 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
353 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
354 my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
355 my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
356 my $author = $clogp->{Maintainer};
357 $author =~ s#,.*##ms;
358 my $authline = "$author $date";
359 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
360 open C, ">../commit.tmp" or die $!;
361 print C "tree $tree\n" or die $!;
362 print C "parent $upload_hash\n" or die $! if $upload_hash;
363 print C <<END or die $!;
369 # imported by dgit from the archive
372 my $commithash = cmdoutput @git, qw(hash-object -w -t commit ../commit.tmp);
373 print "synthesised git commit from .dsc $clogp->{Version}\n";
374 chdir '../../../..' or die $!;
375 cmdoutput @git, qw(update-ref -m),"dgit synthesise $clogp->{Version}",
376 'DGIT_ARCHIVE', $commithash;
377 cmdoutput @git, qw(log -n2), $commithash;
378 # ... gives git a chance to complain if our commit is malformed
379 my $outputhash = $commithash;
381 chdir "$ud/$dir" or die $!;
382 runcmd @git, qw(reset --hard), $upload_hash;
383 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
384 my $oldclogp = Dpkg::Control::Hash->new();
385 $oldclogp->parse('../changelogold.tmp','previous changelog') or die;
387 version_compare_string($oldclogp->{Version}, $clogp->{Version});
389 # git upload/ is earlier vsn than archive, use archive
390 } elsif ($vcmp >= 0) {
391 print STDERR <<END or die $!;
392 Version actually in archive: $clogp->{Version} (older)
393 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
394 Perhaps the upload is stuck in incoming. Using the version from git.
397 die "version in archive is same as version in git".
398 " to-be-uploaded (upload/) branch but archive".
399 " version hash no commit hash?!\n";
401 chdir '../../../..' or die $!;
407 sub ensure_we_have_orig () {
408 foreach my $f (dsc_files()) {
409 next unless is_orig_file($f);
411 die "$f ?" unless -f _;
413 die "$f $!" unless $!==&ENOENT;
415 my $origurl = $dscurl;
416 $origurl =~ s{/[^/]+$}{};
418 die "$f ?" unless $f =~ m/^${package}_/;
419 die "$f ?" if $f =~ m#/#;
420 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
426 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
429 sub is_fast_fwd ($$) {
430 my ($ancestor,$child) = @_;
431 my $mb = cmdoutput @git, qw(merge-base), $dsc_hash, $upload_hash;
432 return rev_parse($mb) eq rev_parse($ancestor);
435 sub git_fetch_us () {
436 die "cannot dry run with fetch" if $dryrun;
437 runcmd @git, qw(fetch),access_giturl(),fetchspec();
440 sub fetch_from_archive () {
441 # ensures that lrref() is what is actually in the archive,
443 get_archive_dsc() or return 0;
444 $dsc_hash = $dsc->{$ourdscfield};
445 if (defined $dsc_hash) {
446 $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
448 print "last upload to archive specified git hash\n";
450 print "last upload to archive has NO git hash\n";
454 cmdoutput_errok @git, qw(show-ref --heads), lrref();
456 die unless chomp $upload_hash;
463 if (defined $dsc_hash) {
464 die "missing git history even though dsc has hash"
467 ensure_we_have_orig();
469 $hash = generate_commit_from_dsc();
472 die "not fast forward on last upload branch!".
473 " (archive's version left in DGIT_ARCHIVE)"
474 unless is_fast_fwd($dsc_hash, $upload_hash);
476 if ($upload_hash ne $hash) {
477 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
481 dryrun_report @upd_cmd;
489 die "dry run makes no sense with clone" if $dryrun;
490 mkdir $dstdir or die "$dstdir $!";
491 chdir "$dstdir" or die "$dstdir $!";
492 runcmd @git, qw(init -q);
493 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
494 open H, "> .git/HEAD" or die $!;
495 print H "ref: ".lref()."\n" or die $!;
497 runcmd @git, qw(remote add), 'origin', access_giturl();
498 if (check_for_git()) {
499 print "fetching existing git history\n";
501 runcmd @git, qw(fetch origin);
503 print "starting new git history\n";
505 fetch_from_archive() or die;
506 runcmd @git, qw(reset --hard), lrref();
507 print "ready for work in $dstdir\n";
511 if (check_for_git()) {
514 fetch_from_archive() or die;
519 runcmd_ordryrun @git, qw(merge -m),"Merge from $suite [dgit]",
524 runcmd @git, qw(diff --quiet HEAD);
525 my $clogp = parsechangelog();
526 $package = $clogp->{Source};
527 my $dscfn = "${package}_$clogp->{Version}.dsc";
528 stat "../$dscfn" or die "$dscfn $!";
529 $dsc = parsecontrol("../$dscfn");
532 print "checking that $dscfn corresponds to HEAD\n";
533 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
534 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
535 chdir '../../../..' or die $!;
536 runcmd @git, qw(diff --exit-code), $tree;
538 #do fast forward check and maybe fake merge
539 # if (!is_fast_fwd(mainbranch
540 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
541 # map { lref($_).":".rref($_) }
543 $dsc->{$ourdscfield} = rev_parse('HEAD');
544 $dsc->save("../$dscfn.tmp") or die $!;
546 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
548 print "[new .dsc left in $dscfn.tmp]\n";
551 my $pat = "../${package}_$clogp->{Version}_*.changes";
553 die "$pat ?" unless @cs==1;
554 ($changesfile) = @cs;
556 my $tag = debiantag($dsc->{Version});
557 if (!check_for_git()) {
558 create_remote_git_repo();
560 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
562 my @tag_cmd = (@git, qw(tag -s -m),
563 "Release $dsc->{Version} for $suite [dgit]");
564 push @tag_cmd, qw(-u),$keyid if defined $keyid;
566 runcmd_ordryrun @tag_cmd;
567 my @debsign_cmd = @debsign;
568 push @debsign_cmd, "-k$keyid" if defined $keyid;
569 push @debsign_cmd, $changesfile;
570 runcmd_ordryrun @debsign_cmd;
572 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
573 my $host = access_cfg('upload-host');
574 my @hostarg = defined($host) ? ($host,) : ();
575 runcmd_ordryrun @dput, @hostarg, $changesfile;
581 die if defined $package;
584 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
585 ($package,$suite) = @ARGV;
586 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
587 ($package,$dstdir) = @ARGV;
589 ($package,$suite,$dstdir) = @ARGV;
593 $dstdir ||= "$package";
598 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
600 if ($branch =~ m#$lbranch_re#o) {
607 sub fetchpullargs () {
608 if (!defined $package) {
609 my $sourcep = parsecontrol('debian/control');
610 $package = $sourcep->{Source};
613 $suite = branchsuite();
615 my $clogp = parsechangelog();
616 $suite = $clogp->{Distribution};
618 canonicalise_suite();
619 print "fetching from suite $suite\n";
622 canonicalise_suite();
642 die if defined $package;
643 my $clogp = parsechangelog();
644 $package = $clogp->{Source};
646 $suite = $clogp->{Distribution};
648 local ($package) = 'dpkg';
649 canonicalise_suite();
654 if (fetch_from_archive()) {
655 is_fast_fwd(lrref(), 'HEAD') or die;
657 die unless $new_package;
663 # we pass further options and args to git-buildpackage
664 die if defined $package;
665 my $clogp = parsechangelog();
666 $suite = $clogp->{Distribution};
667 $package = $clogp->{Source};
668 canonicalise_suite();
670 qw(git-buildpackage -us -uc --git-no-sign-tags),
671 '--git-builder=dpkg-buildpackage -i\.git/ -I.git',
672 "--git-debian-branch=".lbranch(),
679 last unless $ARGV[0] =~ m/^-/;
683 if (m/^--dry-run$/) {
685 } elsif (m/^--no-sign$/) {
687 } elsif (m/^--new$/) {
689 } elsif (m/^--(\w+)=(.*)/s && ($om = $opts_opt_map{$1})) {
691 } elsif (m/^--(\w+):(.*)/s && ($om = $opts_opt_map{$1})) {
701 open DEBUG, ">&STDERR" or die $!;
705 } elsif (s/^-c(.*=.*)//s) {
707 } elsif (s/^-C(.*)//s) {
709 } elsif (s/^-k(.*)//s) {
721 my $cmd = shift @ARGV;
723 { no strict qw(refs); &{"cmd_$cmd"}(); }