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;
27 use File::Temp qw(tempdir);
35 our $our_version = 'UNRELEASED'; ###substituted###
37 our $rpushprotovsn = 2;
39 our $isuite = 'unstable';
45 our $dryrun_level = 0;
47 our $buildproductsdir = '..';
52 our $existing_package = 'dpkg';
53 our $cleanmode = 'dpkg-source';
54 our $changes_since_version;
55 our $we_are_responder;
56 our $initiator_tempdir;
58 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
60 our $suite_re = '[-+.0-9a-z]+';
63 our (@dget) = qw(dget);
64 our (@curl) = qw(curl -f);
65 our (@dput) = qw(dput);
66 our (@debsign) = qw(debsign);
68 our (@sbuild) = qw(sbuild -A);
70 our (@dgit) = qw(dgit);
71 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
72 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
73 our (@dpkggenchanges) = qw(dpkg-genchanges);
74 our (@mergechanges) = qw(mergechanges -f);
75 our (@changesopts) = ('');
77 our %opts_opt_map = ('dget' => \@dget, # accept for compatibility
80 'debsign' => \@debsign,
85 'dpkg-source' => \@dpkgsource,
86 'dpkg-buildpackage' => \@dpkgbuildpackage,
87 'dpkg-genchanges' => \@dpkggenchanges,
88 'ch' => \@changesopts,
89 'mergechanges' => \@mergechanges);
91 our %opts_opt_cmdonly = ('gpg' => 1);
96 open DEBUG, ">/dev/null" or die $!;
100 our $remotename = 'dgit';
101 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
102 our $branchprefix = 'dgit';
105 sub lbranch () { return "$branchprefix/$csuite"; }
106 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
107 sub lref () { return "refs/heads/".lbranch(); }
108 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
109 sub rrref () { return "refs/$branchprefix/$csuite"; }
124 return "${package}_".(stripepoch $vsn).".dsc";
128 our $debugprefix = '';
133 foreach my $f (@end) {
135 warn "$us: cleanup: $@" if length $@;
139 our @signames = split / /, $Config{sig_name};
141 sub waitstatusmsg () {
143 return "terminated, reporting successful completion";
144 } elsif (!($? & 255)) {
145 return "failed with error exit status ".WEXITSTATUS($?);
146 } elsif (WIFSIGNALED($?)) {
147 my $signum=WTERMSIG($?);
148 return "died due to fatal signal ".
149 ($signames[$signum] // "number $signum").
150 ($? & 128 ? " (core dumped)" : ""); # POSIX(3pm) has no WCOREDUMP
152 return "failed with unknown wait status ".$?;
156 sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
159 die $us.($we_are_responder ? " (build host)" : "").": @_\n";
162 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
164 sub no_such_package () {
165 print STDERR "$us: package $package does not exist in suite $isuite\n";
171 return "+".rrref().":".lrref();
176 printdebug "CD $newdir\n";
177 chdir $newdir or die "chdir: $newdir: $!";
180 #---------- remote protocol support, common ----------
182 # remote push initiator/responder protocol:
183 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
185 # > file parsed-changelog
186 # [indicates that output of dpkg-parsechangelog follows]
187 # > data-block NBYTES
188 # > [NBYTES bytes of data (no newline)]
189 # [maybe some more blocks]
201 # [indicates that signed tag is wanted]
202 # < data-block NBYTES
203 # < [NBYTES bytes of data (no newline)]
204 # [maybe some more blocks]
208 # > want signed-dsc-changes
209 # < data-block NBYTES [transfer of signed dsc]
211 # < data-block NBYTES [transfer of signed changes]
219 sub i_child_report () {
220 # Sees if our child has died, and reap it if so. Returns a string
221 # describing how it died if it failed, or undef otherwise.
222 return undef unless $i_child_pid;
223 my $got = waitpid $i_child_pid, WNOHANG;
224 return undef if $got <= 0;
225 die unless $got == $i_child_pid;
226 $i_child_pid = undef;
227 return undef unless $?;
228 return "build host child ".waitstatusmsg();
233 fail "connection lost: $!" if $fh->error;
234 fail "protocol violation; $m not expected";
237 sub badproto_badread ($$) {
239 fail "connection lost: $!" if $!;
240 my $report = i_child_report();
241 fail $report if defined $report;
242 badproto $fh, "eof (reading $wh)";
245 sub protocol_expect (&$) {
246 my ($match, $fh) = @_;
249 defined && chomp or badproto_badread $fh, "protocol message";
257 badproto $fh, "\`$_'";
260 sub protocol_send_file ($$) {
261 my ($fh, $ourfn) = @_;
262 open PF, "<", $ourfn or die "$ourfn: $!";
265 my $got = read PF, $d, 65536;
266 die "$ourfn: $!" unless defined $got;
268 print $fh "data-block ".length($d)."\n" or die $!;
269 print $fh $d or die $!;
271 PF->error and die "$ourfn $!";
272 print $fh "data-end\n" or die $!;
276 sub protocol_read_bytes ($$) {
277 my ($fh, $nbytes) = @_;
278 $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
280 my $got = read $fh, $d, $nbytes;
281 $got==$nbytes or badproto_badread $fh, "data block";
285 sub protocol_receive_file ($$) {
286 my ($fh, $ourfn) = @_;
287 printdebug "() $ourfn\n";
288 open PF, ">", $ourfn or die "$ourfn: $!";
290 my ($y,$l) = protocol_expect {
291 m/^data-block (.*)$/ ? (1,$1) :
292 m/^data-end$/ ? (0,) :
296 my $d = protocol_read_bytes $fh, $l;
297 print PF $d or die $!;
302 #---------- remote protocol support, responder ----------
304 sub responder_send_command ($) {
306 return unless $we_are_responder;
307 # called even without $we_are_responder
308 printdebug ">> $command\n";
309 print PO $command, "\n" or die $!;
312 sub responder_send_file ($$) {
313 my ($keyword, $ourfn) = @_;
314 return unless $we_are_responder;
315 printdebug "]] $keyword $ourfn\n";
316 responder_send_command "file $keyword";
317 protocol_send_file \*PO, $ourfn;
320 sub responder_receive_files ($@) {
321 my ($keyword, @ourfns) = @_;
322 die unless $we_are_responder;
323 printdebug "[[ $keyword @ourfns\n";
324 responder_send_command "want $keyword";
325 foreach my $fn (@ourfns) {
326 protocol_receive_file \*PI, $fn;
329 protocol_expect { m/^files-end$/ } \*PI;
332 #---------- remote protocol support, initiator ----------
334 sub initiator_expect (&) {
336 protocol_expect { &$match } \*RO;
339 #---------- end remote code ----------
342 if ($we_are_responder) {
344 responder_send_command "progress ".length($m) or die $!;
345 print PO $m or die $!;
355 $ua = LWP::UserAgent->new();
359 progress "downloading $what...";
360 my $r = $ua->get(@_) or die $!;
361 return undef if $r->code == 404;
362 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
363 return $r->decoded_content(charset => 'none');
366 our ($dscdata,$dscurl,$dsc,$dsc_checked,$skew_warning_vsn);
373 if (m{[^-=_./0-9a-z]}i) {
380 return join ' ', @out;
385 my $intro = shift @_;
386 print $fh $intro," " or die $!;
387 print $fh shellquote @_ or die $!;
388 print $fh "\n" or die $!;
392 { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
394 fail "failed to fork/exec: $!";
396 fail "subprocess ".waitstatusmsg();
398 fail "subprocess produced invalid output";
403 printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
405 failedcmd @_ if system @_;
408 sub act_local () { return $dryrun_level <= 1; }
409 sub act_scary () { return !$dryrun_level; }
412 if (!$dryrun_level) {
413 progress "dgit ok: @_";
415 progress "would be ok: @_ (but dry run only)";
419 sub cmdoutput_errok {
420 die Dumper(\@_)." ?" if grep { !defined } @_;
421 printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
422 open P, "-|", @_ or die $!;
425 { local $/ = undef; $d = <P>; }
427 if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
430 printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
435 my $d = cmdoutput_errok @_;
436 defined $d or failedcmd @_;
441 printcmd(\*STDERR,$debugprefix."#",@_);
444 sub runcmd_ordryrun {
452 sub runcmd_ordryrun_local {
461 my ($first_shell, @cmd) = @_;
462 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
465 our $helpmsg = <<END;
467 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
468 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
469 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
470 dgit [dgit-opts] push [dgit-opts] [suite]
471 dgit [dgit-opts] rpush build-host:build-dir ...
472 important dgit options:
473 -k<keyid> sign tag and package with <keyid> instead of default
474 --dry-run -n do not change anything, but go through the motions
475 --damp-run -L like --dry-run but make local changes, without signing
476 --new -N allow introducing a new package
477 --debug -D increase debug level
478 -c<name>=<value> set git config option (used directly by dgit too)
481 our $later_warning_msg = <<END;
482 Perhaps the upload is stuck in incoming. Using the version from git.
486 print STDERR "$us: @_\n", $helpmsg or die $!;
491 @ARGV or badusage "too few arguments";
492 return scalar shift @ARGV;
496 print $helpmsg or die $!;
500 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
502 our %defcfg = ('dgit.default.distro' => 'debian',
503 'dgit.default.username' => '',
504 'dgit.default.archive-query-default-component' => 'main',
505 'dgit.default.ssh' => 'ssh',
506 'dgit-distro.debian.git-host' => 'git.debian.org',
507 'dgit-distro.debian.git-proto' => 'git+ssh://',
508 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
509 'dgit-distro.debian.git-check' => 'ssh-cmd',
510 'dgit-distro.debian.git-create' => 'ssh-cmd',
511 'dgit-distro.debian.sshpsql-host' => 'mirror.ftp-master.debian.org',
512 'dgit-distro.debian.sshpsql-dbname' => 'service=projectb',
513 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
514 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
515 'dgit-distro.debian.backports-quirk' => '(squeeze)-backports*',
516 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
517 'dgit-distro.ubuntu.git-check' => 'false',
518 'dgit-distro.ubuntu.mirror' => 'http://archive.ubuntu.com/ubuntu',
519 'dgit-distro.test-dummy.ssh' => "$td/ssh",
520 'dgit-distro.test-dummy.username' => "alice",
521 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
522 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
523 'dgit-distro.test-dummy.git-url' => "$td/git",
524 'dgit-distro.test-dummy.git-host' => "git",
525 'dgit-distro.test-dummy.git-path' => "$td/git",
526 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
527 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
528 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
533 return undef if $c =~ /RETURN-UNDEF/;
534 my @cmd = (@git, qw(config --), $c);
537 local ($debug) = $debug-1;
538 $v = cmdoutput_errok @cmd;
545 my $dv = $defcfg{$c};
546 return $dv if defined $dv;
548 badcfg "need value for one of: @_\n".
549 "$us: distro or suite appears not to be (properly) supported";
552 sub access_basedistro () {
553 if (defined $idistro) {
554 return cfg("dgit-distro.basedistro.distro",
555 "dgit-suite.$isuite.distro",
556 'RETURN-UNDEF') // $idistro;
558 return cfg("dgit-suite.$isuite.distro",
559 "dgit.default.distro");
563 sub access_quirk () {
564 # returns (quirk name, distro to use instead, quirk-specific info)
565 my $basedistro = access_basedistro();
566 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
568 if (defined $backports_quirk) {
569 my $re = $backports_quirk;
570 $re =~ s/[^-0-9a-z_\%*()]/\\$&/ig;
572 $re =~ s/\%/([-0-9a-z_]+)/
573 or $re =~ m/[()]/ or badcfg "backports-quirk needs \% or ( )";
574 if ($isuite =~ m/^$re$/) {
575 return ('backports',"$basedistro-backports",$1);
578 return ('none',$basedistro);
581 sub access_distro () {
582 return (access_quirk())[1];
587 my $basedistro = access_basedistro();
588 my $distro = $idistro || access_distro();
589 my $value = cfg(map {
590 ("dgit-distro.$distro.$_",
591 "dgit-distro.$basedistro.$_",
597 sub string_to_ssh ($) {
599 if ($spec =~ m/\s/) {
600 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
606 sub access_cfg_ssh () {
607 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
608 if (!defined $gitssh) {
611 return string_to_ssh $gitssh;
615 sub access_someuserhost ($) {
617 my $user = access_cfg("$some-user",'username');
618 my $host = access_cfg("$some-host");
619 return length($user) ? "$user\@$host" : $host;
622 sub access_gituserhost () {
623 return access_someuserhost('git');
626 sub access_giturl () {
627 my $url = access_cfg('git-url','RETURN-UNDEF');
630 access_cfg('git-proto').
631 access_gituserhost().
632 access_cfg('git-path');
634 return "$url/$package.git";
637 sub parsecontrolfh ($$;$) {
638 my ($fh, $desc, $allowsigned) = @_;
639 our $dpkgcontrolhash_noissigned;
642 my %opts = ('name' => $desc);
643 $opts{allow_pgp}= $allowsigned || !$dpkgcontrolhash_noissigned;
644 print STDERR Dumper(\%opts);
645 $c = Dpkg::Control::Hash->new(%opts);
646 $c->parse($fh,$desc) or die "parsing of $desc failed";
647 last if $allowsigned;
648 last if $dpkgcontrolhash_noissigned;
649 my $issigned= $c->get_option('is_pgp_signed');
650 if (!defined $issigned) {
651 $dpkgcontrolhash_noissigned= 1;
652 seek $fh, 0,0 or die "seek $desc: $!";
653 } elsif ($issigned) {
654 fail "control file $desc is (already) PGP-signed. ".
655 " Note that dgit push needs to modify the .dsc and then".
656 " do the signature itself";
665 my ($file, $desc) = @_;
666 my $fh = new IO::Handle;
667 open $fh, '<', $file or die "$file: $!";
668 my $c = parsecontrolfh($fh,$desc);
669 $fh->error and die $!;
675 my ($dctrl,$field) = @_;
676 my $v = $dctrl->{$field};
677 return $v if defined $v;
678 fail "missing field $field in ".$v->get_option('name');
682 my $c = Dpkg::Control::Hash->new();
683 my $p = new IO::Handle;
684 my @cmd = (qw(dpkg-parsechangelog), @_);
685 open $p, '-|', @cmd or die $!;
687 $?=0; $!=0; close $p or failedcmd @cmd;
691 sub git_get_ref ($) {
693 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
695 $?==256 or fail "git show-ref failed (status $?)";
696 printdebug "ref $refname= [show-ref exited 1]\n";
699 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
700 printdebug "ref $refname=$1\n";
703 printdebug "ref $refname= [no match]\n";
710 defined $d or fail "getcwd failed: $!";
716 sub archive_query ($) {
718 my $query = access_cfg('archive-query','RETURN-UNDEF');
719 if (!defined $query) {
720 my $distro = access_basedistro();
721 if ($distro eq 'debian') {
723 access_someuserhost('sshpsql').':'.
724 access_cfg('sshpsql-dbname');
726 $query = "madison:$distro";
729 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
732 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
735 sub pool_dsc_subpath ($$) {
736 my ($vsn,$component) = @_; # $package is implict arg
737 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
738 return "/pool/$component/$prefix/$package/".dscfn($vsn);
741 sub archive_query_madison ($$) {
742 my ($proto,$data) = @_;
743 die unless $proto eq 'madison';
744 $rmad{$package} ||= cmdoutput
745 qw(rmadison -asource),"-s$isuite","-u$data",$package;
746 my $rmad = $rmad{$package};
747 return madison_parse($rmad);
750 sub madison_parse ($) {
753 foreach my $l (split /\n/, $rmad) {
754 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
755 \s*( [^ \t|]+ )\s* \|
756 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
757 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
758 $1 eq $package or die "$rmad $package ?";
765 $component = access_cfg('archive-query-default-component');
767 $5 eq 'source' or die "$rmad ?";
768 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
770 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
773 sub canonicalise_suite_madison ($$) {
774 # madison canonicalises for us
775 my @r = archive_query_madison($_[0],$_[1]);
777 "unable to canonicalise suite using package $package".
778 " which does not appear to exist in suite $isuite;".
779 " --existing-package may help";
784 my ($data,$sql) = @_;
785 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
786 my ($userhost,$dbname) = ($`,$'); #';
788 my @cmd = (access_cfg_ssh, $userhost,
789 "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
790 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
791 open P, "-|", @cmd or die $!;
794 printdebug("$debugprefix>|$_|\n");
797 $!=0; $?=0; close P or failedcmd @cmd;
799 my $nrows = pop @rows;
800 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
801 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
802 @rows = map { [ split /\|/, $_ ] } @rows;
803 my $ncols = scalar @{ shift @rows };
804 die if grep { scalar @$_ != $ncols } @rows;
808 sub sql_injection_check {
809 foreach (@_) { die "$_ $& ?" if m/[']/; }
812 sub archive_query_sshpsql ($$) {
813 my ($proto,$data) = @_;
814 sql_injection_check $isuite, $package;
815 my @rows = sshpsql($data, <<END);
816 SELECT source.version, component.name, files.filename, files.sha256sum
818 JOIN src_associations ON source.id = src_associations.source
819 JOIN suite ON suite.id = src_associations.suite
820 JOIN dsc_files ON dsc_files.source = source.id
821 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
822 JOIN component ON component.id = files_archive_map.component_id
823 JOIN files ON files.id = dsc_files.file
824 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
825 AND source.source='$package'
826 AND files.filename LIKE '%.dsc';
828 @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
829 my $digester = Digest::SHA->new(256);
831 my ($vsn,$component,$filename,$sha256sum) = @$_;
832 [ $vsn, "/pool/$component/$filename",$digester,$sha256sum ];
837 sub canonicalise_suite_sshpsql ($$) {
838 my ($proto,$data) = @_;
839 sql_injection_check $isuite;
840 my @rows = sshpsql($data, <<END);
841 SELECT suite.codename
842 FROM suite where suite_name='$isuite' or codename='$isuite';
844 @rows = map { $_->[0] } @rows;
845 fail "unknown suite $isuite" unless @rows;
846 die "ambiguous $isuite: @rows ?" if @rows>1;
850 sub canonicalise_suite_dummycat ($$) {
851 my ($proto,$data) = @_;
852 my $dpath = "$data/suite.$isuite";
853 if (!open C, "<", $dpath) {
854 $!==ENOENT or die "$dpath: $!";
855 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
859 chomp or die "$dpath: $!";
861 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
865 sub archive_query_dummycat ($$) {
866 my ($proto,$data) = @_;
867 canonicalise_suite();
868 my $dpath = "$data/package.$csuite.$package";
869 if (!open C, "<", $dpath) {
870 $!==ENOENT or die "$dpath: $!";
871 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
879 printdebug "dummycat query $csuite $package $dpath | $_\n";
880 my @row = split /\s+/, $_;
881 @row==2 or die "$dpath: $_ ?";
884 C->error and die "$dpath: $!";
886 return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
889 sub canonicalise_suite () {
890 return if defined $csuite;
891 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
892 $csuite = archive_query('canonicalise_suite');
893 if ($isuite ne $csuite) {
894 progress "canonical suite name for $isuite is $csuite";
898 sub get_archive_dsc () {
899 canonicalise_suite();
900 my @vsns = archive_query('archive_query');
901 foreach my $vinfo (@vsns) {
902 my ($vsn,$subpath,$digester,$digest) = @$vinfo;
903 $dscurl = access_cfg('mirror').$subpath;
904 $dscdata = url_get($dscurl);
906 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
911 $digester->add($dscdata);
912 my $got = $digester->hexdigest();
914 fail "$dscurl has hash $got but".
915 " archive told us to expect $digest";
917 my $dscfh = new IO::File \$dscdata, '<' or die $!;
918 printdebug Dumper($dscdata) if $debug>1;
919 $dsc = parsecontrolfh($dscfh,$dscurl,1);
920 printdebug Dumper($dsc) if $debug>1;
921 my $fmt = getfield $dsc, 'Format';
922 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
923 $dsc_checked = !!$digester;
929 sub check_for_git () {
931 my $how = access_cfg('git-check');
932 if ($how eq 'ssh-cmd') {
934 (access_cfg_ssh, access_gituserhost(),
935 " set -e; cd ".access_cfg('git-path').";".
936 " if test -d $package.git; then echo 1; else echo 0; fi");
937 my $r= cmdoutput @cmd;
938 failedcmd @cmd unless $r =~ m/^[01]$/;
940 } elsif ($how eq 'true') {
942 } elsif ($how eq 'false') {
945 badcfg "unknown git-check \`$how'";
949 sub create_remote_git_repo () {
950 my $how = access_cfg('git-create');
951 if ($how eq 'ssh-cmd') {
953 (access_cfg_ssh, access_gituserhost(),
954 "set -e; cd ".access_cfg('git-path').";".
955 " cp -a _template $package.git");
956 } elsif ($how eq 'true') {
959 badcfg "unknown git-create \`$how'";
963 our ($dsc_hash,$lastpush_hash);
965 our $ud = '.git/dgit/unpack';
973 sub mktree_in_ud_from_only_subdir () {
974 # changes into the subdir
977 $dirs[0] =~ m#^([^/]+)/\.$# or die;
980 fail "source package contains .git directory" if stat '.git';
981 die $! unless $!==&ENOENT;
982 runcmd qw(git init -q);
983 rmtree('.git/objects');
984 symlink '../../../../objects','.git/objects' or die $!;
985 runcmd @git, qw(add -Af);
986 my $tree = cmdoutput @git, qw(write-tree);
987 $tree =~ m/^\w+$/ or die "$tree ?";
991 sub dsc_files_info () {
992 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
993 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
994 ['Files', 'Digest::MD5', 'new()']) {
995 my ($fname, $module, $method) = @$csumi;
996 my $field = $dsc->{$fname};
997 next unless defined $field;
998 eval "use $module; 1;" or die $@;
1000 foreach (split /\n/, $field) {
1002 m/^(\w+) (\d+) (\S+)$/ or
1003 fail "could not parse .dsc $fname line \`$_'";
1004 my $digester = eval "$module"."->$method;" or die $@;
1009 Digester => $digester,
1014 fail "missing any supported Checksums-* or Files field in ".
1015 $dsc->get_option('name');
1019 map { $_->{Filename} } dsc_files_info();
1022 sub is_orig_file ($) {
1024 m/\.orig(?:-\w+)?\.tar\.\w+$/;
1027 sub make_commit ($) {
1029 return cmdoutput @git, qw(hash-object -w -t commit), $file;
1032 sub clogp_authline ($) {
1034 my $author = getfield $clogp, 'Maintainer';
1035 $author =~ s#,.*##ms;
1036 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
1037 my $authline = "$author $date";
1038 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
1039 fail "unexpected commit author line format \`$authline'".
1040 " (was generated from changelog Maintainer field)";
1044 sub generate_commit_from_dsc () {
1048 foreach my $fi (dsc_files_info()) {
1049 my $f = $fi->{Filename};
1050 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
1052 link "../../../$f", $f
1056 complete_file_from_dsc('.', $fi);
1058 if (is_orig_file($f)) {
1059 link $f, "../../../../$f"
1065 my $dscfn = "$package.dsc";
1067 open D, ">", $dscfn or die "$dscfn: $!";
1068 print D $dscdata or die "$dscfn: $!";
1069 close D or die "$dscfn: $!";
1070 my @cmd = qw(dpkg-source);
1071 push @cmd, '--no-check' if $dsc_checked;
1072 push @cmd, qw(-x --), $dscfn;
1075 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1076 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
1077 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
1078 my $authline = clogp_authline $clogp;
1079 my $changes = getfield $clogp, 'Changes';
1080 open C, ">../commit.tmp" or die $!;
1081 print C <<END or die $!;
1088 # imported from the archive
1091 my $outputhash = make_commit qw(../commit.tmp);
1092 my $cversion = getfield $clogp, 'Version';
1093 progress "synthesised git commit from .dsc $cversion";
1094 if ($lastpush_hash) {
1095 runcmd @git, qw(reset --hard), $lastpush_hash;
1096 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
1097 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
1098 my $oversion = getfield $oldclogp, 'Version';
1100 version_compare_string($oversion, $cversion);
1102 # git upload/ is earlier vsn than archive, use archive
1103 open C, ">../commit2.tmp" or die $!;
1104 print C <<END or die $!;
1106 parent $lastpush_hash
1111 Record $package ($cversion) in archive suite $csuite
1113 $outputhash = make_commit qw(../commit2.tmp);
1114 } elsif ($vcmp > 0) {
1115 print STDERR <<END or die $!;
1117 Version actually in archive: $cversion (older)
1118 Last allegedly pushed/uploaded: $oversion (newer or same)
1121 $outputhash = $lastpush_hash;
1123 $outputhash = $lastpush_hash;
1126 changedir '../../../..';
1127 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1128 'DGIT_ARCHIVE', $outputhash;
1129 cmdoutput @git, qw(log -n2), $outputhash;
1130 # ... gives git a chance to complain if our commit is malformed
1135 sub complete_file_from_dsc ($$) {
1136 our ($dstdir, $fi) = @_;
1137 # Ensures that we have, in $dir, the file $fi, with the correct
1138 # contents. (Downloading it from alongside $dscurl if necessary.)
1140 my $f = $fi->{Filename};
1141 my $tf = "$dstdir/$f";
1145 progress "using existing $f";
1147 die "$tf $!" unless $!==&ENOENT;
1150 $furl =~ s{/[^/]+$}{};
1152 die "$f ?" unless $f =~ m/^${package}_/;
1153 die "$f ?" if $f =~ m#/#;
1154 runcmd_ordryrun_local @curl,qw(-o),$tf,'--',"$furl";
1155 next if !act_local();
1159 open F, "<", "$tf" or die "$tf: $!";
1160 $fi->{Digester}->reset();
1161 $fi->{Digester}->addfile(*F);
1162 F->error and die $!;
1163 my $got = $fi->{Digester}->hexdigest();
1164 $got eq $fi->{Hash} or
1165 fail "file $f has hash $got but .dsc".
1166 " demands hash $fi->{Hash} ".
1167 ($downloaded ? "(got wrong file from archive!)"
1168 : "(perhaps you should delete this file?)");
1171 sub ensure_we_have_orig () {
1172 foreach my $fi (dsc_files_info()) {
1173 my $f = $fi->{Filename};
1174 next unless is_orig_file($f);
1175 complete_file_from_dsc('..', $fi);
1180 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1183 sub is_fast_fwd ($$) {
1184 my ($ancestor,$child) = @_;
1185 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1186 my $mb = cmdoutput_errok @cmd;
1188 return rev_parse($mb) eq rev_parse($ancestor);
1190 $?==256 or failedcmd @cmd;
1195 sub git_fetch_us () {
1196 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1199 sub fetch_from_archive () {
1200 # ensures that lrref() is what is actually in the archive,
1201 # one way or another
1205 foreach my $field (@ourdscfield) {
1206 $dsc_hash = $dsc->{$field};
1207 last if defined $dsc_hash;
1209 if (defined $dsc_hash) {
1210 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1212 progress "last upload to archive specified git hash";
1214 progress "last upload to archive has NO git hash";
1217 progress "no version available from the archive";
1220 $lastpush_hash = git_get_ref(lrref());
1221 printdebug "previous reference hash=$lastpush_hash\n";
1223 if (defined $dsc_hash) {
1224 fail "missing remote git history even though dsc has hash -".
1225 " could not find ref ".lrref().
1226 " (should have been fetched from ".access_giturl()."#".rrref().")"
1227 unless $lastpush_hash;
1229 ensure_we_have_orig();
1230 if ($dsc_hash eq $lastpush_hash) {
1231 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1232 print STDERR <<END or die $!;
1234 Git commit in archive is behind the last version allegedly pushed/uploaded.
1235 Commit referred to by archive: $dsc_hash
1236 Last allegedly pushed/uploaded: $lastpush_hash
1239 $hash = $lastpush_hash;
1241 fail "archive's .dsc refers to ".$dsc_hash.
1242 " but this is an ancestor of ".$lastpush_hash;
1245 $hash = generate_commit_from_dsc();
1246 } elsif ($lastpush_hash) {
1247 # only in git, not in the archive yet
1248 $hash = $lastpush_hash;
1249 print STDERR <<END or die $!;
1251 Package not found in the archive, but has allegedly been pushed using dgit.
1255 printdebug "nothing found!\n";
1256 if (defined $skew_warning_vsn) {
1257 print STDERR <<END or die $!;
1259 Warning: relevant archive skew detected.
1260 Archive allegedly contains $skew_warning_vsn
1261 But we were not able to obtain any version from the archive or git.
1267 printdebug "current hash=$hash\n";
1268 if ($lastpush_hash) {
1269 fail "not fast forward on last upload branch!".
1270 " (archive's version left in DGIT_ARCHIVE)"
1271 unless is_fast_fwd($lastpush_hash, $hash);
1273 if (defined $skew_warning_vsn) {
1275 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1276 my $clogf = ".git/dgit/changelog.tmp";
1277 runcmd shell_cmd "exec >$clogf",
1278 @git, qw(cat-file blob), "$hash:debian/changelog";
1279 my $gotclogp = parsechangelog("-l$clogf");
1280 my $got_vsn = getfield $gotclogp, 'Version';
1281 printdebug "SKEW CHECK GOT $got_vsn\n";
1282 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
1283 print STDERR <<END or die $!;
1285 Warning: archive skew detected. Using the available version:
1286 Archive allegedly contains $skew_warning_vsn
1287 We were able to obtain only $got_vsn
1292 if ($lastpush_hash ne $hash) {
1293 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1297 dryrun_report @upd_cmd;
1305 canonicalise_suite();
1306 badusage "dry run makes no sense with clone" unless act_local();
1307 mkdir $dstdir or die "$dstdir $!";
1309 runcmd @git, qw(init -q);
1310 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1311 open H, "> .git/HEAD" or die $!;
1312 print H "ref: ".lref()."\n" or die $!;
1314 runcmd @git, qw(remote add), 'origin', access_giturl();
1315 if (check_for_git()) {
1316 progress "fetching existing git history";
1318 runcmd_ordryrun_local @git, qw(fetch origin);
1320 progress "starting new git history";
1322 fetch_from_archive() or no_such_package;
1323 my $vcsgiturl = $dsc->{'Vcs-Git'};
1324 if (length $vcsgiturl) {
1325 runcmd @git, qw(remote add vcs-git), $vcsgiturl;
1327 runcmd @git, qw(reset --hard), lrref();
1328 printdone "ready for work in $dstdir";
1332 if (check_for_git()) {
1335 fetch_from_archive() or no_such_package();
1336 printdone "fetched into ".lrref();
1341 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1343 printdone "fetched to ".lrref()." and merged into HEAD";
1346 sub check_not_dirty () {
1347 return if $ignoredirty;
1348 my @cmd = (@git, qw(diff --quiet HEAD));
1349 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1350 $!=0; $?=0; system @cmd;
1351 return if !$! && !$?;
1352 if (!$! && $?==256) {
1353 fail "working tree is dirty (does not match HEAD)";
1359 sub commit_quilty_patch () {
1360 my $output = cmdoutput @git, qw(status --porcelain);
1362 foreach my $l (split /\n/, $output) {
1363 next unless $l =~ m/\S/;
1364 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1369 progress "nothing quilty to commit, ok.";
1372 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1373 my $m = "Commit Debian 3.0 (quilt) metadata";
1375 runcmd_ordryrun_local @git, qw(commit -m), $m;
1380 return 0 unless $format eq '3.0 (quilt)';
1381 progress "Format \`$format', urgh";
1383 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1389 sub push_parse_changelog ($) {
1392 my $clogp = Dpkg::Control::Hash->new();
1393 $clogp->load($clogpfn) or die;
1395 $package = getfield $clogp, 'Source';
1396 my $cversion = getfield $clogp, 'Version';
1397 my $tag = debiantag($cversion);
1398 runcmd @git, qw(check-ref-format), $tag;
1400 my $dscfn = dscfn($cversion);
1402 return ($clogp, $cversion, $tag, $dscfn);
1405 sub push_parse_dsc ($$$) {
1406 my ($dscfn,$dscfnwhat, $cversion) = @_;
1407 $dsc = parsecontrol($dscfn,$dscfnwhat);
1408 my $dversion = getfield $dsc, 'Version';
1409 my $dscpackage = getfield $dsc, 'Source';
1410 ($dscpackage eq $package && $dversion eq $cversion) or
1411 fail "$dscfn is for $dscpackage $dversion".
1412 " but debian/changelog is for $package $cversion";
1415 sub push_mktag ($$$$$$$) {
1416 my ($head,$clogp,$tag,
1418 $changesfile,$changesfilewhat,
1421 $dsc->{$ourdscfield[0]} = $head;
1422 $dsc->save("$dscfn.tmp") or die $!;
1424 my $changes = parsecontrol($changesfile,$changesfilewhat);
1425 foreach my $field (qw(Source Distribution Version)) {
1426 $changes->{$field} eq $clogp->{$field} or
1427 fail "changes field $field \`$changes->{$field}'".
1428 " does not match changelog \`$clogp->{$field}'";
1431 my $cversion = getfield $clogp, 'Version';
1432 my $clogsuite = getfield $clogp, 'Distribution';
1434 # We make the git tag by hand because (a) that makes it easier
1435 # to control the "tagger" (b) we can do remote signing
1436 my $authline = clogp_authline $clogp;
1437 open TO, '>', $tfn->('.tmp') or die $!;
1438 print TO <<END or die $!;
1444 $package release $cversion for $clogsuite ($csuite) [dgit]
1448 my $tagobjfn = $tfn->('.tmp');
1450 if (!defined $keyid) {
1451 $keyid = access_cfg('keyid','RETURN-UNDEF');
1453 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1454 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1455 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1456 push @sign_cmd, $tfn->('.tmp');
1457 runcmd_ordryrun @sign_cmd;
1459 $tagobjfn = $tfn->('.signed.tmp');
1460 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1461 $tfn->('.tmp'), $tfn->('.tmp.asc');
1468 sub sign_changes ($) {
1469 my ($changesfile) = @_;
1471 my @debsign_cmd = @debsign;
1472 push @debsign_cmd, "-k$keyid" if defined $keyid;
1473 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1474 push @debsign_cmd, $changesfile;
1475 runcmd_ordryrun @debsign_cmd;
1480 printdebug "actually entering push\n";
1483 access_giturl(); # check that success is vaguely likely
1485 my $clogpfn = ".git/dgit/changelog.822.tmp";
1486 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1488 responder_send_file('parsed-changelog', $clogpfn);
1490 my ($clogp, $cversion, $tag, $dscfn) =
1491 push_parse_changelog("$clogpfn");
1493 my $dscpath = "$buildproductsdir/$dscfn";
1495 fail "looked for .dsc $dscfn, but $!;".
1496 " maybe you forgot to build";
1498 responder_send_file('dsc', $dscpath);
1500 push_parse_dsc($dscpath, $dscfn, $cversion);
1502 my $format = getfield $dsc, 'Format';
1503 printdebug "format $format\n";
1504 if (madformat($format)) {
1505 commit_quilty_patch();
1509 progress "checking that $dscfn corresponds to HEAD";
1510 runcmd qw(dpkg-source -x --),
1511 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1512 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1513 changedir '../../../..';
1514 my $diffopt = $debug>0 ? '--exit-code' : '--quiet';
1515 my @diffcmd = (@git, qw(diff), $diffopt, $tree);
1516 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1518 my $r = system @diffcmd;
1521 fail "$dscfn specifies a different tree to your HEAD commit;".
1522 " perhaps you forgot to build".
1523 ($diffopt eq '--exit-code' ? "" :
1524 " (run with -D to see full diff output)");
1530 #do fast forward check and maybe fake merge
1531 # if (!is_fast_fwd(mainbranch
1532 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1533 # map { lref($_).":".rref($_) }
1535 my $head = rev_parse('HEAD');
1536 if (!$changesfile) {
1537 my $multi = "$buildproductsdir/".
1538 "${package}_".(stripepoch $cversion)."_multi.changes";
1539 if (stat "$multi") {
1540 $changesfile = $multi;
1542 $!==&ENOENT or die "$multi: $!";
1543 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1544 my @cs = glob "$buildproductsdir/$pat";
1545 fail "failed to find unique changes file".
1546 " (looked for $pat in $buildproductsdir, or $multi);".
1547 " perhaps you need to use dgit -C"
1549 ($changesfile) = @cs;
1552 $changesfile = "$buildproductsdir/$changesfile";
1555 responder_send_file('changes',$changesfile);
1556 responder_send_command("param head $head");
1557 responder_send_command("param csuite $csuite");
1559 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1562 if ($we_are_responder) {
1563 $tagobjfn = $tfn->('.signed.tmp');
1564 responder_receive_files('signed-tag', $tagobjfn);
1567 push_mktag($head,$clogp,$tag,
1569 $changesfile,$changesfile,
1573 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1574 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1575 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1576 runcmd_ordryrun @git, qw(tag -v --), $tag;
1578 if (!check_for_git()) {
1579 create_remote_git_repo();
1581 runcmd_ordryrun @git, qw(push),access_giturl(),
1582 "HEAD:".rrref(), "refs/tags/$tag";
1583 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1585 if ($we_are_responder) {
1586 my $dryrunsuffix = act_local() ? "" : ".tmp";
1587 responder_receive_files('signed-dsc-changes',
1588 "$dscpath$dryrunsuffix",
1589 "$changesfile$dryrunsuffix");
1592 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1594 progress "[new .dsc left in $dscpath.tmp]";
1596 sign_changes $changesfile;
1599 my $host = access_cfg('upload-host','RETURN-UNDEF');
1600 my @hostarg = defined($host) ? ($host,) : ();
1601 runcmd_ordryrun @dput, @hostarg, $changesfile;
1602 printdone "pushed and uploaded $cversion";
1604 responder_send_command("complete");
1610 badusage "-p is not allowed with clone; specify as argument instead"
1611 if defined $package;
1614 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1615 ($package,$isuite) = @ARGV;
1616 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1617 ($package,$dstdir) = @ARGV;
1618 } elsif (@ARGV==3) {
1619 ($package,$isuite,$dstdir) = @ARGV;
1621 badusage "incorrect arguments to dgit clone";
1623 $dstdir ||= "$package";
1626 fail "$dstdir already exists";
1627 } elsif ($! != &ENOENT) {
1632 if ($rmonerror && !$dryrun_level) {
1633 $cwd_remove= getcwd();
1635 return unless defined $cwd_remove;
1636 if (!chdir "$cwd_remove") {
1637 return if $!==&ENOENT;
1638 die "chdir $cwd_remove: $!";
1640 rmtree($dstdir) or die "remove $dstdir: $!\n";
1645 $cwd_remove = undef;
1648 sub branchsuite () {
1649 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1650 if ($branch =~ m#$lbranch_re#o) {
1657 sub fetchpullargs () {
1658 if (!defined $package) {
1659 my $sourcep = parsecontrol('debian/control','debian/control');
1660 $package = getfield $sourcep, 'Source';
1663 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1665 my $clogp = parsechangelog();
1666 $isuite = getfield $clogp, 'Distribution';
1668 canonicalise_suite();
1669 progress "fetching from suite $csuite";
1670 } elsif (@ARGV==1) {
1672 canonicalise_suite();
1674 badusage "incorrect arguments to dgit fetch or dgit pull";
1692 badusage "-p is not allowed with dgit push" if defined $package;
1694 my $clogp = parsechangelog();
1695 $package = getfield $clogp, 'Source';
1698 } elsif (@ARGV==1) {
1699 ($specsuite) = (@ARGV);
1701 badusage "incorrect arguments to dgit push";
1703 $isuite = getfield $clogp, 'Distribution';
1705 local ($package) = $existing_package; # this is a hack
1706 canonicalise_suite();
1708 if (defined $specsuite && $specsuite ne $isuite) {
1709 canonicalise_suite();
1710 $csuite eq $specsuite or
1711 fail "dgit push: changelog specifies $isuite ($csuite)".
1712 " but command line specifies $specsuite";
1714 if (check_for_git()) {
1717 if (fetch_from_archive()) {
1718 is_fast_fwd(lrref(), 'HEAD') or
1719 fail "dgit push: HEAD is not a descendant".
1720 " of the archive's version.\n".
1721 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1724 fail "package appears to be new in this suite;".
1725 " if this is intentional, use --new";
1730 #---------- remote commands' implementation ----------
1732 sub cmd_remote_push_build_host {
1733 my ($nrargs) = shift @ARGV;
1734 my (@rargs) = @ARGV[0..$nrargs-1];
1735 @ARGV = @ARGV[$nrargs..$#ARGV];
1737 my ($dir,$vsnwant) = @rargs;
1738 # vsnwant is a comma-separated list; we report which we have
1739 # chosen in our ready response (so other end can tell if they
1742 $we_are_responder = 1;
1744 open PI, "<&STDIN" or die $!;
1745 open STDIN, "/dev/null" or die $!;
1746 open PO, ">&STDOUT" or die $!;
1748 open STDOUT, ">&STDERR" or die $!;
1752 fail "build host has dgit rpush protocol version".
1753 " $rpushprotovsn but invocation host has $vsnwant"
1754 unless grep { $rpushprotovsn eq $_ } split /,/, $vsnwant;
1756 responder_send_command("dgit-remote-push-ready $rpushprotovsn");
1762 sub cmd_remote_push_responder { cmd_remote_push_build_host(); }
1763 # ... for compatibility with proto vsn.1 dgit (just so that user gets
1764 # a good error message)
1770 my $report = i_child_report();
1771 if (defined $report) {
1772 printdebug "($report)\n";
1773 } elsif ($i_child_pid) {
1774 printdebug "(killing build host child $i_child_pid)\n";
1775 kill 15, $i_child_pid;
1777 if (defined $i_tmp && !defined $initiator_tempdir) {
1779 eval { rmtree $i_tmp; };
1783 END { i_cleanup(); }
1786 my ($base,$selector,@args) = @_;
1787 $selector =~ s/\-/_/g;
1788 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1794 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1801 my @rargs = ($dir,$rpushprotovsn);
1804 push @rdgit, @ropts;
1805 push @rdgit, qw(remote-push-build-host), (scalar @rargs), @rargs;
1807 my @cmd = (@ssh, $host, shellquote @rdgit);
1808 printcmd \*DEBUG,$debugprefix."+",@cmd;
1810 if (defined $initiator_tempdir) {
1811 rmtree $initiator_tempdir;
1812 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1813 $i_tmp = $initiator_tempdir;
1817 $i_child_pid = open2(\*RO, \*RI, @cmd);
1819 initiator_expect { m/^dgit-remote-push-ready/ };
1821 my ($icmd,$iargs) = initiator_expect {
1822 m/^(\S+)(?: (.*))?$/;
1825 i_method "i_resp", $icmd, $iargs;
1829 sub i_resp_progress ($) {
1831 my $msg = protocol_read_bytes \*RO, $rhs;
1835 sub i_resp_complete {
1836 my $pid = $i_child_pid;
1837 $i_child_pid = undef; # prevents killing some other process with same pid
1838 printdebug "waiting for build host child $pid...\n";
1839 my $got = waitpid $pid, 0;
1840 die $! unless $got == $pid;
1841 die "build host child failed $?" if $?;
1844 printdebug "all done\n";
1848 sub i_resp_file ($) {
1850 my $localname = i_method "i_localname", $keyword;
1851 my $localpath = "$i_tmp/$localname";
1852 stat $localpath and badproto \*RO, "file $keyword ($localpath) twice";
1853 protocol_receive_file \*RO, $localpath;
1854 i_method "i_file", $keyword;
1859 sub i_resp_param ($) {
1860 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1866 sub i_resp_want ($) {
1868 die "$keyword ?" if $i_wanted{$keyword}++;
1869 my @localpaths = i_method "i_want", $keyword;
1870 printdebug "[[ $keyword @localpaths\n";
1871 foreach my $localpath (@localpaths) {
1872 protocol_send_file \*RI, $localpath;
1874 print RI "files-end\n" or die $!;
1877 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
1879 sub i_localname_parsed_changelog {
1880 return "remote-changelog.822";
1882 sub i_file_parsed_changelog {
1883 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
1884 push_parse_changelog "$i_tmp/remote-changelog.822";
1885 die if $i_dscfn =~ m#/|^\W#;
1888 sub i_localname_dsc {
1889 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1894 sub i_localname_changes {
1895 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1896 $i_changesfn = $i_dscfn;
1897 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
1898 return $i_changesfn;
1900 sub i_file_changes { }
1902 sub i_want_signed_tag {
1903 printdebug Dumper(\%i_param, $i_dscfn);
1904 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
1905 && defined $i_param{'csuite'}
1906 or badproto \*RO, "premature desire for signed-tag";
1907 my $head = $i_param{'head'};
1908 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
1910 die unless $i_param{'csuite'} =~ m/^$suite_re$/;
1912 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
1915 push_mktag $head, $i_clogp, $i_tag,
1917 $i_changesfn, 'remote changes',
1918 sub { "tag$_[0]"; };
1923 sub i_want_signed_dsc_changes {
1924 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
1925 sign_changes $i_changesfn;
1926 return ($i_dscfn, $i_changesfn);
1929 #---------- building etc. ----------
1935 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1937 sub build_maybe_quilt_fixup () {
1938 if (!open F, "debian/source/format") {
1939 die $! unless $!==&ENOENT;
1943 F->error and die $!;
1945 return unless madformat($_);
1948 my @cmd = (@git, qw(ls-files --exclude-standard -iodm));
1949 my $problems = cmdoutput @cmd;
1950 if (length $problems) {
1951 print STDERR "problematic files:\n";
1952 print STDERR " $_\n" foreach split /\n/, $problems;
1953 fail "Cannot do quilt fixup in tree containing ignored files. ".
1954 "Perhaps your package's clean target is broken, in which".
1955 " case -wg (which says to use git-clean -xdf) may help.";
1958 my $clogp = parsechangelog();
1959 my $version = getfield $clogp, 'Version';
1960 my $author = getfield $clogp, 'Maintainer';
1961 my $headref = rev_parse('HEAD');
1964 my $patchname = "auto-$version-$headref-$time";
1965 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1967 my $descfn = ".git/dgit/quilt-description.tmp";
1968 open O, '>', $descfn or die "$descfn: $!";
1970 $msg =~ s/^\s+$/ ./mg;
1971 print O <<END or die $!;
1972 Description: Automatically generated patch ($clogp->{Version})
1973 Last (up to) $ncommits git changes, FYI:
1983 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1984 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1985 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1986 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
1989 if (!open P, '>>', ".pc/applied-patches") {
1990 $!==&ENOENT or die $!;
1995 commit_quilty_patch();
1998 sub quilt_fixup_editor () {
1999 my $descfn = $ENV{$fakeeditorenv};
2000 my $editing = $ARGV[$#ARGV];
2001 open I1, '<', $descfn or die "$descfn: $!";
2002 open I2, '<', $editing or die "$editing: $!";
2003 unlink $editing or die "$editing: $!";
2004 open O, '>', $editing or die "$editing: $!";
2005 while (<I1>) { print O or die $!; } I1->error and die $!;
2008 $copying ||= m/^\-\-\- /;
2009 next unless $copying;
2012 I2->error and die $!;
2018 if ($cleanmode eq 'dpkg-source') {
2019 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
2020 } elsif ($cleanmode eq 'git') {
2021 runcmd_ordryrun_local @git, qw(clean -xdf);
2022 } elsif ($cleanmode eq 'none') {
2029 badusage "clean takes no additional arguments" if @ARGV;
2034 badusage "-p is not allowed when building" if defined $package;
2037 my $clogp = parsechangelog();
2038 $isuite = getfield $clogp, 'Distribution';
2039 $package = getfield $clogp, 'Source';
2040 $version = getfield $clogp, 'Version';
2041 build_maybe_quilt_fixup();
2044 sub changesopts () {
2045 my @opts =@changesopts[1..$#changesopts];
2046 if (!defined $changes_since_version) {
2047 my @vsns = archive_query('archive_query');
2048 my @quirk = access_quirk();
2049 if ($quirk[0] eq 'backports') {
2050 local $isuite = $quirk[2];
2052 canonicalise_suite();
2053 push @vsns, archive_query('archive_query');
2056 @vsns = map { $_->[0] } @vsns;
2057 @vsns = sort { -version_compare_string($a, $b) } @vsns;
2058 $changes_since_version = $vsns[0];
2059 progress "changelog will contain changes since $vsns[0]";
2061 $changes_since_version = '_';
2062 progress "package seems new, not specifying -v<version>";
2065 if ($changes_since_version ne '_') {
2066 unshift @opts, "-v$changes_since_version";
2073 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
2074 printdone "build successful\n";
2080 (qw(git-buildpackage -us -uc --git-no-sign-tags),
2081 "--git-builder=@dpkgbuildpackage");
2082 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
2083 canonicalise_suite();
2084 push @cmd, "--git-debian-branch=".lbranch();
2086 push @cmd, changesopts();
2087 runcmd_ordryrun_local @cmd, @ARGV;
2088 printdone "build successful\n";
2093 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
2094 $dscfn = dscfn($version);
2095 if ($cleanmode eq 'dpkg-source') {
2096 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
2099 my $pwd = must_getcwd();
2100 my $leafdir = basename $pwd;
2102 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
2104 runcmd_ordryrun_local qw(sh -ec),
2105 'exec >$1; shift; exec "$@"','x',
2106 "../$sourcechanges",
2107 @dpkggenchanges, qw(-S), changesopts();
2111 sub cmd_build_source {
2112 badusage "build-source takes no additional arguments" if @ARGV;
2114 printdone "source built, results in $dscfn and $sourcechanges";
2120 my $pat = "${package}_".(stripepoch $version)."_*.changes";
2122 stat $dscfn or fail "$dscfn (in parent directory): $!";
2123 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
2124 foreach my $cf (glob $pat) {
2125 next if $cf eq $sourcechanges;
2126 unlink $cf or fail "remove $cf: $!";
2129 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
2130 my @changesfiles = glob $pat;
2131 @changesfiles = sort {
2132 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
2135 fail "wrong number of different changes files (@changesfiles)"
2136 unless @changesfiles;
2137 runcmd_ordryrun_local @mergechanges, @changesfiles;
2138 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
2140 stat $multichanges or fail "$multichanges: $!";
2142 printdone "build successful, results in $multichanges\n" or die $!;
2145 sub cmd_quilt_fixup {
2146 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
2147 my $clogp = parsechangelog();
2148 $version = getfield $clogp, 'Version';
2149 build_maybe_quilt_fixup();
2152 #---------- argument parsing and main program ----------
2155 print "dgit version $our_version\n" or die $!;
2162 if (defined $ENV{'DGIT_SSH'}) {
2163 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
2164 } elsif (defined $ENV{'GIT_SSH'}) {
2165 @ssh = ($ENV{'GIT_SSH'});
2169 last unless $ARGV[0] =~ m/^-/;
2173 if (m/^--dry-run$/) {
2176 } elsif (m/^--damp-run$/) {
2179 } elsif (m/^--no-sign$/) {
2182 } elsif (m/^--help$/) {
2184 } elsif (m/^--version$/) {
2186 } elsif (m/^--new$/) {
2189 } elsif (m/^--since-version=([^_]+|_)$/) {
2191 $changes_since_version = $1;
2192 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
2193 ($om = $opts_opt_map{$1}) &&
2197 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
2198 !$opts_opt_cmdonly{$1} &&
2199 ($om = $opts_opt_map{$1})) {
2202 } elsif (m/^--existing-package=(.*)/s) {
2204 $existing_package = $1;
2205 } elsif (m/^--initiator-tempdir=(.*)/s) {
2206 $initiator_tempdir = $1;
2207 $initiator_tempdir =~ m#^/# or
2208 badusage "--initiator-tempdir must be used specify an".
2209 " absolute, not relative, directory."
2210 } elsif (m/^--distro=(.*)/s) {
2213 } elsif (m/^--build-products-dir=(.*)/s) {
2215 $buildproductsdir = $1;
2216 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
2219 } elsif (m/^--clean=(.*)$/s) {
2220 badusage "unknown cleaning mode \`$1'";
2221 } elsif (m/^--ignore-dirty$/s) {
2224 } elsif (m/^--no-quilt-fixup$/s) {
2227 } elsif (m/^--no-rm-on-error$/s) {
2231 badusage "unknown long option \`$_'";
2238 } elsif (s/^-L/-/) {
2241 } elsif (s/^-h/-/) {
2243 } elsif (s/^-D/-/) {
2245 open DEBUG, ">&STDERR" or die $!;
2248 } elsif (s/^-N/-/) {
2251 } elsif (s/^-v([^_]+|_)$//s) {
2253 $changes_since_version = $1;
2256 push @changesopts, $_;
2258 } elsif (s/^-c(.*=.*)//s) {
2260 push @git, '-c', $1;
2261 } elsif (s/^-d(.*)//s) {
2264 } elsif (s/^-C(.*)//s) {
2267 if ($changesfile =~ s#^(.*)/##) {
2268 $buildproductsdir = $1;
2270 } elsif (s/^-k(.*)//s) {
2272 } elsif (s/^-wn//s) {
2274 $cleanmode = 'none';
2275 } elsif (s/^-wg//s) {
2278 } elsif (s/^-wd//s) {
2280 $cleanmode = 'dpkg-source';
2282 badusage "unknown short option \`$_'";
2289 if ($ENV{$fakeeditorenv}) {
2290 quilt_fixup_editor();
2294 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2295 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2296 if $dryrun_level == 1;
2298 print STDERR $helpmsg or die $!;
2301 my $cmd = shift @ARGV;
2304 my $fn = ${*::}{"cmd_$cmd"};
2305 $fn or badusage "unknown operation $cmd";