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);
34 our $our_version = 'UNRELEASED'; ###substituted###
36 our $isuite = 'unstable';
42 our $dryrun_level = 0;
44 our $buildproductsdir = '..';
49 our $existing_package = 'dpkg';
50 our $cleanmode = 'dpkg-source';
51 our $changes_since_version;
52 our $we_are_responder;
53 our $initiator_tempdir;
55 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
57 our $suite_re = '[-+.0-9a-z]+';
60 our (@dget) = qw(dget);
61 our (@curl) = qw(curl -f);
62 our (@dput) = qw(dput);
63 our (@debsign) = qw(debsign);
65 our (@sbuild) = qw(sbuild -A);
67 our (@dgit) = qw(dgit);
68 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
69 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
70 our (@dpkggenchanges) = qw(dpkg-genchanges);
71 our (@mergechanges) = qw(mergechanges -f);
72 our (@changesopts) = ('');
74 our %opts_opt_map = ('dget' => \@dget, # accept for compatibility
77 'debsign' => \@debsign,
82 'dpkg-source' => \@dpkgsource,
83 'dpkg-buildpackage' => \@dpkgbuildpackage,
84 'dpkg-genchanges' => \@dpkggenchanges,
85 'ch' => \@changesopts,
86 'mergechanges' => \@mergechanges);
88 our %opts_opt_cmdonly = ('gpg' => 1);
93 open DEBUG, ">/dev/null" or die $!;
97 our $remotename = 'dgit';
98 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
99 our $branchprefix = 'dgit';
102 sub lbranch () { return "$branchprefix/$csuite"; }
103 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
104 sub lref () { return "refs/heads/".lbranch(); }
105 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
106 sub rrref () { return "refs/$branchprefix/$csuite"; }
121 return "${package}_".(stripepoch $vsn).".dsc";
125 our $debugprefix = '';
130 foreach my $f (@end) {
132 warn "$us: cleanup: $@" if length $@;
136 sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
139 die $us.($we_are_responder ? " (build host)" : "").": @_\n";
142 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
144 sub no_such_package () {
145 print STDERR "$us: package $package does not exist in suite $isuite\n";
151 return "+".rrref().":".lrref();
156 printdebug "CD $newdir\n";
157 chdir $newdir or die "chdir: $newdir: $!";
160 #---------- remote protocol support, common ----------
162 # remote push initiator/responder protocol:
163 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
165 # > file parsed-changelog
166 # [indicates that output of dpkg-parsechangelog follows]
167 # > data-block NBYTES
168 # > [NBYTES bytes of data (no newline)]
169 # [maybe some more blocks]
181 # [indicates that signed tag is wanted]
182 # < data-block NBYTES
183 # < [NBYTES bytes of data (no newline)]
184 # [maybe some more blocks]
188 # > want signed-dsc-changes
189 # < data-block NBYTES [transfer of signed dsc]
191 # < data-block NBYTES [transfer of signed changes]
199 fail "connection lost: $!" if $fh->error;
200 fail "protocol violation; $m not expected";
203 sub protocol_expect (&$) {
204 my ($match, $fh) = @_;
207 defined && chomp or badproto $fh, "eof";
215 badproto $fh, "\`$_'";
218 sub protocol_send_file ($$) {
219 my ($fh, $ourfn) = @_;
220 open PF, "<", $ourfn or die "$ourfn: $!";
223 my $got = read PF, $d, 65536;
224 die "$ourfn: $!" unless defined $got;
226 print $fh "data-block ".length($d)."\n" or die $!;
227 print $fh $d or die $!;
229 PF->error and die "$ourfn $!";
230 print $fh "data-end\n" or die $!;
234 sub protocol_read_bytes ($$) {
235 my ($fh, $nbytes) = @_;
236 $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
238 my $got = read $fh, $d, $nbytes;
239 $got==$nbytes or badproto $fh, "eof during data block";
243 sub protocol_receive_file ($$) {
244 my ($fh, $ourfn) = @_;
245 printdebug "() $ourfn\n";
246 open PF, ">", $ourfn or die "$ourfn: $!";
248 my ($y,$l) = protocol_expect {
249 m/^data-block (.*)$/ ? (1,$1) :
250 m/^data-end$/ ? (0,) :
254 my $d = protocol_read_bytes $fh, $l;
255 print PF $d or die $!;
260 #---------- remote protocol support, responder ----------
262 sub responder_send_command ($) {
264 return unless $we_are_responder;
265 # called even without $we_are_responder
266 printdebug ">> $command\n";
267 print PO $command, "\n" or die $!;
270 sub responder_send_file ($$) {
271 my ($keyword, $ourfn) = @_;
272 return unless $we_are_responder;
273 printdebug "]] $keyword $ourfn\n";
274 responder_send_command "file $keyword";
275 protocol_send_file \*PO, $ourfn;
278 sub responder_receive_files ($@) {
279 my ($keyword, @ourfns) = @_;
280 die unless $we_are_responder;
281 printdebug "[[ $keyword @ourfns\n";
282 responder_send_command "want $keyword";
283 foreach my $fn (@ourfns) {
284 protocol_receive_file \*PI, $fn;
287 protocol_expect { m/^files-end$/ } \*PI;
290 #---------- remote protocol support, initiator ----------
292 sub initiator_expect (&) {
294 protocol_expect { &$match } \*RO;
297 #---------- end remote code ----------
300 if ($we_are_responder) {
302 responder_send_command "progress ".length($m) or die $!;
303 print PO $m or die $!;
313 $ua = LWP::UserAgent->new();
317 progress "downloading $what...";
318 my $r = $ua->get(@_) or die $!;
319 return undef if $r->code == 404;
320 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
321 return $r->decoded_content(charset => 'none');
324 our ($dscdata,$dscurl,$dsc,$dsc_checked,$skew_warning_vsn);
331 if (m{[^-=_./0-9a-z]}i) {
338 return join ' ', @out;
343 my $intro = shift @_;
344 print $fh $intro," " or die $!;
345 print $fh shellquote @_ or die $!;
346 print $fh "\n" or die $!;
350 { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
352 fail "failed to fork/exec: $!";
353 } elsif (!($? & 0xff)) {
354 fail "subprocess failed with error exit status ".($?>>8);
356 fail "subprocess crashed (wait status $?)";
358 fail "subprocess produced invalid output";
363 printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
365 failedcmd @_ if system @_;
368 sub act_local () { return $dryrun_level <= 1; }
369 sub act_scary () { return !$dryrun_level; }
372 if (!$dryrun_level) {
373 progress "dgit ok: @_";
375 progress "would be ok: @_ (but dry run only)";
379 sub cmdoutput_errok {
380 die Dumper(\@_)." ?" if grep { !defined } @_;
381 printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
382 open P, "-|", @_ or die $!;
385 { local $/ = undef; $d = <P>; }
387 if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
390 printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
395 my $d = cmdoutput_errok @_;
396 defined $d or failedcmd @_;
401 printcmd(\*STDERR,$debugprefix."#",@_);
404 sub runcmd_ordryrun {
412 sub runcmd_ordryrun_local {
421 my ($first_shell, @cmd) = @_;
422 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
425 our $helpmsg = <<END;
427 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
428 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
429 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
430 dgit [dgit-opts] push [dgit-opts] [suite]
431 dgit [dgit-opts] rpush build-host:build-dir ...
432 important dgit options:
433 -k<keyid> sign tag and package with <keyid> instead of default
434 --dry-run -n do not change anything, but go through the motions
435 --damp-run -L like --dry-run but make local changes, without signing
436 --new -N allow introducing a new package
437 --debug -D increase debug level
438 -c<name>=<value> set git config option (used directly by dgit too)
441 our $later_warning_msg = <<END;
442 Perhaps the upload is stuck in incoming. Using the version from git.
446 print STDERR "$us: @_\n", $helpmsg or die $!;
451 @ARGV or badusage "too few arguments";
452 return scalar shift @ARGV;
456 print $helpmsg or die $!;
460 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
462 our %defcfg = ('dgit.default.distro' => 'debian',
463 'dgit.default.username' => '',
464 'dgit.default.archive-query-default-component' => 'main',
465 'dgit.default.ssh' => 'ssh',
466 'dgit-distro.debian.git-host' => 'git.debian.org',
467 'dgit-distro.debian.git-proto' => 'git+ssh://',
468 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
469 'dgit-distro.debian.git-check' => 'ssh-cmd',
470 'dgit-distro.debian.git-create' => 'ssh-cmd',
471 'dgit-distro.debian.sshpsql-host' => 'mirror.ftp-master.debian.org',
472 'dgit-distro.debian.sshpsql-dbname' => 'service=projectb',
473 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
474 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
475 'dgit-distro.debian.backports-quirk' => '(squeeze)-backports*',
476 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
477 'dgit-distro.ubuntu.git-check' => 'false',
478 'dgit-distro.ubuntu.mirror' => 'http://archive.ubuntu.com/ubuntu',
479 'dgit-distro.test-dummy.ssh' => "$td/ssh",
480 'dgit-distro.test-dummy.username' => "alice",
481 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
482 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
483 'dgit-distro.test-dummy.git-url' => "$td/git",
484 'dgit-distro.test-dummy.git-host' => "git",
485 'dgit-distro.test-dummy.git-path' => "$td/git",
486 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
487 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
488 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
493 return undef if $c =~ /RETURN-UNDEF/;
494 my @cmd = (@git, qw(config --), $c);
497 local ($debug) = $debug-1;
498 $v = cmdoutput_errok @cmd;
505 my $dv = $defcfg{$c};
506 return $dv if defined $dv;
508 badcfg "need value for one of: @_\n".
509 "$us: distro or suite appears not to be (properly) supported";
512 sub access_basedistro () {
513 if (defined $idistro) {
514 return cfg("dgit-distro.basedistro.distro",
515 "dgit-suite.$isuite.distro",
516 'RETURN-UNDEF') // $idistro;
518 return cfg("dgit-suite.$isuite.distro",
519 "dgit.default.distro");
523 sub access_quirk () {
524 # returns (quirk name, distro to use instead, quirk-specific info)
525 my $basedistro = access_basedistro();
526 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
528 if (defined $backports_quirk) {
529 my $re = $backports_quirk;
530 $re =~ s/[^-0-9a-z_\%*()]/\\$&/ig;
532 $re =~ s/\%/([-0-9a-z_]+)/
533 or $re =~ m/[()]/ or badcfg "backports-quirk needs \% or ( )";
534 if ($isuite =~ m/^$re$/) {
535 return ('backports',"$basedistro-backports",$1);
538 return ('none',$basedistro);
541 sub access_distro () {
542 return (access_quirk())[1];
547 my $basedistro = access_basedistro();
548 my $distro = $idistro || access_distro();
549 my $value = cfg(map {
550 ("dgit-distro.$distro.$_",
551 "dgit-distro.$basedistro.$_",
557 sub string_to_ssh ($) {
559 if ($spec =~ m/\s/) {
560 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
566 sub access_cfg_ssh () {
567 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
568 if (!defined $gitssh) {
571 return string_to_ssh $gitssh;
575 sub access_someuserhost ($) {
577 my $user = access_cfg("$some-user",'username');
578 my $host = access_cfg("$some-host");
579 return length($user) ? "$user\@$host" : $host;
582 sub access_gituserhost () {
583 return access_someuserhost('git');
586 sub access_giturl () {
587 my $url = access_cfg('git-url','RETURN-UNDEF');
590 access_cfg('git-proto').
591 access_gituserhost().
592 access_cfg('git-path');
594 return "$url/$package.git";
597 sub parsecontrolfh ($$@) {
598 my ($fh, $desc, @opts) = @_;
599 my %opts = ('name' => $desc, @opts);
600 my $c = Dpkg::Control::Hash->new(%opts);
601 $c->parse($fh) or die "parsing of $desc failed";
606 my ($file, $desc) = @_;
607 my $fh = new IO::Handle;
608 open $fh, '<', $file or die "$file: $!";
609 my $c = parsecontrolfh($fh,$desc);
610 $fh->error and die $!;
616 my ($dctrl,$field) = @_;
617 my $v = $dctrl->{$field};
618 return $v if defined $v;
619 fail "missing field $field in ".$v->get_option('name');
623 my $c = Dpkg::Control::Hash->new();
624 my $p = new IO::Handle;
625 my @cmd = (qw(dpkg-parsechangelog), @_);
626 open $p, '-|', @cmd or die $!;
628 $?=0; $!=0; close $p or failedcmd @cmd;
632 sub git_get_ref ($) {
634 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
636 $?==256 or fail "git show-ref failed (status $?)";
637 printdebug "ref $refname= [show-ref exited 1]\n";
640 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
641 printdebug "ref $refname=$1\n";
644 printdebug "ref $refname= [no match]\n";
651 defined $d or fail "getcwd failed: $!";
657 sub archive_query ($) {
659 my $query = access_cfg('archive-query','RETURN-UNDEF');
660 if (!defined $query) {
661 my $distro = access_basedistro();
662 if ($distro eq 'debian') {
664 access_someuserhost('sshpsql').':'.
665 access_cfg('sshpsql-dbname');
667 $query = "madison:$distro";
670 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
673 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
676 sub pool_dsc_subpath ($$) {
677 my ($vsn,$component) = @_; # $package is implict arg
678 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
679 return "/pool/$component/$prefix/$package/".dscfn($vsn);
682 sub archive_query_madison ($$) {
683 my ($proto,$data) = @_;
684 die unless $proto eq 'madison';
685 $rmad{$package} ||= cmdoutput
686 qw(rmadison -asource),"-s$isuite","-u$data",$package;
687 my $rmad = $rmad{$package};
688 return madison_parse($rmad);
691 sub madison_parse ($) {
694 foreach my $l (split /\n/, $rmad) {
695 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
696 \s*( [^ \t|]+ )\s* \|
697 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
698 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
699 $1 eq $package or die "$rmad $package ?";
706 $component = access_cfg('archive-query-default-component');
708 $5 eq 'source' or die "$rmad ?";
709 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
711 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
714 sub canonicalise_suite_madison ($$) {
715 # madison canonicalises for us
716 my @r = archive_query_madison($_[0],$_[1]);
718 "unable to canonicalise suite using package $package".
719 " which does not appear to exist in suite $isuite;".
720 " --existing-package may help";
725 my ($data,$sql) = @_;
726 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
727 my ($userhost,$dbname) = ($`,$'); #';
729 my @cmd = (access_cfg_ssh, $userhost,
730 "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
731 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
732 open P, "-|", @cmd or die $!;
735 printdebug("$debugprefix>|$_|\n");
738 $!=0; $?=0; close P or failedcmd @cmd;
740 my $nrows = pop @rows;
741 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
742 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
743 @rows = map { [ split /\|/, $_ ] } @rows;
744 my $ncols = scalar @{ shift @rows };
745 die if grep { scalar @$_ != $ncols } @rows;
749 sub sql_injection_check {
750 foreach (@_) { die "$_ $& ?" if m/[']/; }
753 sub archive_query_sshpsql ($$) {
754 my ($proto,$data) = @_;
755 sql_injection_check $isuite, $package;
756 my @rows = sshpsql($data, <<END);
757 SELECT source.version, component.name, files.filename, files.sha256sum
759 JOIN src_associations ON source.id = src_associations.source
760 JOIN suite ON suite.id = src_associations.suite
761 JOIN dsc_files ON dsc_files.source = source.id
762 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
763 JOIN component ON component.id = files_archive_map.component_id
764 JOIN files ON files.id = dsc_files.file
765 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
766 AND source.source='$package'
767 AND files.filename LIKE '%.dsc';
769 @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
770 my $digester = Digest::SHA->new(256);
772 my ($vsn,$component,$filename,$sha256sum) = @$_;
773 [ $vsn, "/pool/$component/$filename",$digester,$sha256sum ];
778 sub canonicalise_suite_sshpsql ($$) {
779 my ($proto,$data) = @_;
780 sql_injection_check $isuite;
781 my @rows = sshpsql($data, <<END);
782 SELECT suite.codename
783 FROM suite where suite_name='$isuite' or codename='$isuite';
785 @rows = map { $_->[0] } @rows;
786 fail "unknown suite $isuite" unless @rows;
787 die "ambiguous $isuite: @rows ?" if @rows>1;
791 sub canonicalise_suite_dummycat ($$) {
792 my ($proto,$data) = @_;
793 my $dpath = "$data/suite.$isuite";
794 if (!open C, "<", $dpath) {
795 $!==ENOENT or die "$dpath: $!";
796 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
800 chomp or die "$dpath: $!";
802 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
806 sub archive_query_dummycat ($$) {
807 my ($proto,$data) = @_;
808 canonicalise_suite();
809 my $dpath = "$data/package.$csuite.$package";
810 if (!open C, "<", $dpath) {
811 $!==ENOENT or die "$dpath: $!";
812 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
820 printdebug "dummycat query $csuite $package $dpath | $_\n";
821 my @row = split /\s+/, $_;
822 @row==2 or die "$dpath: $_ ?";
825 C->error and die "$dpath: $!";
827 return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
830 sub canonicalise_suite () {
831 return if defined $csuite;
832 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
833 $csuite = archive_query('canonicalise_suite');
834 if ($isuite ne $csuite) {
835 progress "canonical suite name for $isuite is $csuite";
839 sub get_archive_dsc () {
840 canonicalise_suite();
841 my @vsns = archive_query('archive_query');
842 foreach my $vinfo (@vsns) {
843 my ($vsn,$subpath,$digester,$digest) = @$vinfo;
844 $dscurl = access_cfg('mirror').$subpath;
845 $dscdata = url_get($dscurl);
847 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
852 $digester->add($dscdata);
853 my $got = $digester->hexdigest();
855 fail "$dscurl has hash $got but".
856 " archive told us to expect $digest";
858 my $dscfh = new IO::File \$dscdata, '<' or die $!;
859 printdebug Dumper($dscdata) if $debug>1;
860 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
861 printdebug Dumper($dsc) if $debug>1;
862 my $fmt = getfield $dsc, 'Format';
863 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
864 $dsc_checked = !!$digester;
870 sub check_for_git () {
872 my $how = access_cfg('git-check');
873 if ($how eq 'ssh-cmd') {
875 (access_cfg_ssh, access_gituserhost(),
876 " set -e; cd ".access_cfg('git-path').";".
877 " if test -d $package.git; then echo 1; else echo 0; fi");
878 my $r= cmdoutput @cmd;
879 failedcmd @cmd unless $r =~ m/^[01]$/;
881 } elsif ($how eq 'true') {
883 } elsif ($how eq 'false') {
886 badcfg "unknown git-check \`$how'";
890 sub create_remote_git_repo () {
891 my $how = access_cfg('git-create');
892 if ($how eq 'ssh-cmd') {
894 (access_cfg_ssh, access_gituserhost(),
895 "set -e; cd ".access_cfg('git-path').";".
896 " cp -a _template $package.git");
897 } elsif ($how eq 'true') {
900 badcfg "unknown git-create \`$how'";
904 our ($dsc_hash,$lastpush_hash);
906 our $ud = '.git/dgit/unpack';
914 sub mktree_in_ud_from_only_subdir () {
915 # changes into the subdir
918 $dirs[0] =~ m#^([^/]+)/\.$# or die;
921 fail "source package contains .git directory" if stat '.git';
922 die $! unless $!==&ENOENT;
923 runcmd qw(git init -q);
924 rmtree('.git/objects');
925 symlink '../../../../objects','.git/objects' or die $!;
926 runcmd @git, qw(add -Af);
927 my $tree = cmdoutput @git, qw(write-tree);
928 $tree =~ m/^\w+$/ or die "$tree ?";
932 sub dsc_files_info () {
933 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
934 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
935 ['Files', 'Digest::MD5', 'new()']) {
936 my ($fname, $module, $method) = @$csumi;
937 my $field = $dsc->{$fname};
938 next unless defined $field;
939 eval "use $module; 1;" or die $@;
941 foreach (split /\n/, $field) {
943 m/^(\w+) (\d+) (\S+)$/ or
944 fail "could not parse .dsc $fname line \`$_'";
945 my $digester = eval "$module"."->$method;" or die $@;
950 Digester => $digester,
955 fail "missing any supported Checksums-* or Files field in ".
956 $dsc->get_option('name');
960 map { $_->{Filename} } dsc_files_info();
963 sub is_orig_file ($) {
965 m/\.orig(?:-\w+)?\.tar\.\w+$/;
968 sub make_commit ($) {
970 return cmdoutput @git, qw(hash-object -w -t commit), $file;
973 sub clogp_authline ($) {
975 my $author = getfield $clogp, 'Maintainer';
976 $author =~ s#,.*##ms;
977 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
978 my $authline = "$author $date";
979 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
980 fail "unexpected commit author line format \`$authline'".
981 " (was generated from changelog Maintainer field)";
985 sub generate_commit_from_dsc () {
989 foreach my $fi (dsc_files_info()) {
990 my $f = $fi->{Filename};
991 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
993 link "../../../$f", $f
997 complete_file_from_dsc('.', $fi);
999 if (is_orig_file($f)) {
1000 link $f, "../../../../$f"
1006 my $dscfn = "$package.dsc";
1008 open D, ">", $dscfn or die "$dscfn: $!";
1009 print D $dscdata or die "$dscfn: $!";
1010 close D or die "$dscfn: $!";
1011 my @cmd = qw(dpkg-source);
1012 push @cmd, '--no-check' if $dsc_checked;
1013 push @cmd, qw(-x --), $dscfn;
1016 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1017 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
1018 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
1019 my $authline = clogp_authline $clogp;
1020 my $changes = getfield $clogp, 'Changes';
1021 open C, ">../commit.tmp" or die $!;
1022 print C <<END or die $!;
1029 # imported from the archive
1032 my $outputhash = make_commit qw(../commit.tmp);
1033 my $cversion = getfield $clogp, 'Version';
1034 progress "synthesised git commit from .dsc $cversion";
1035 if ($lastpush_hash) {
1036 runcmd @git, qw(reset --hard), $lastpush_hash;
1037 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
1038 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
1039 my $oversion = getfield $oldclogp, 'Version';
1041 version_compare_string($oversion, $cversion);
1043 # git upload/ is earlier vsn than archive, use archive
1044 open C, ">../commit2.tmp" or die $!;
1045 print C <<END or die $!;
1047 parent $lastpush_hash
1052 Record $package ($cversion) in archive suite $csuite
1054 $outputhash = make_commit qw(../commit2.tmp);
1055 } elsif ($vcmp > 0) {
1056 print STDERR <<END or die $!;
1058 Version actually in archive: $cversion (older)
1059 Last allegedly pushed/uploaded: $oversion (newer or same)
1062 $outputhash = $lastpush_hash;
1064 $outputhash = $lastpush_hash;
1067 changedir '../../../..';
1068 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1069 'DGIT_ARCHIVE', $outputhash;
1070 cmdoutput @git, qw(log -n2), $outputhash;
1071 # ... gives git a chance to complain if our commit is malformed
1076 sub complete_file_from_dsc ($$) {
1077 our ($dstdir, $fi) = @_;
1078 # Ensures that we have, in $dir, the file $fi, with the correct
1079 # contents. (Downloading it from alongside $dscurl if necessary.)
1081 my $f = $fi->{Filename};
1082 my $tf = "$dstdir/$f";
1086 progress "using existing $f";
1088 die "$tf $!" unless $!==&ENOENT;
1091 $furl =~ s{/[^/]+$}{};
1093 die "$f ?" unless $f =~ m/^${package}_/;
1094 die "$f ?" if $f =~ m#/#;
1095 runcmd_ordryrun_local @curl,qw(-o),$tf,'--',"$furl";
1096 next if !act_local();
1100 open F, "<", "$tf" or die "$tf: $!";
1101 $fi->{Digester}->reset();
1102 $fi->{Digester}->addfile(*F);
1103 F->error and die $!;
1104 my $got = $fi->{Digester}->hexdigest();
1105 $got eq $fi->{Hash} or
1106 fail "file $f has hash $got but .dsc".
1107 " demands hash $fi->{Hash} ".
1108 ($downloaded ? "(got wrong file from archive!)"
1109 : "(perhaps you should delete this file?)");
1112 sub ensure_we_have_orig () {
1113 foreach my $fi (dsc_files_info()) {
1114 my $f = $fi->{Filename};
1115 next unless is_orig_file($f);
1116 complete_file_from_dsc('..', $fi);
1121 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1124 sub is_fast_fwd ($$) {
1125 my ($ancestor,$child) = @_;
1126 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1127 my $mb = cmdoutput_errok @cmd;
1129 return rev_parse($mb) eq rev_parse($ancestor);
1131 $?==256 or failedcmd @cmd;
1136 sub git_fetch_us () {
1137 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1140 sub fetch_from_archive () {
1141 # ensures that lrref() is what is actually in the archive,
1142 # one way or another
1146 foreach my $field (@ourdscfield) {
1147 $dsc_hash = $dsc->{$field};
1148 last if defined $dsc_hash;
1150 if (defined $dsc_hash) {
1151 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1153 progress "last upload to archive specified git hash";
1155 progress "last upload to archive has NO git hash";
1158 progress "no version available from the archive";
1161 $lastpush_hash = git_get_ref(lrref());
1162 printdebug "previous reference hash=$lastpush_hash\n";
1164 if (defined $dsc_hash) {
1165 fail "missing remote git history even though dsc has hash -".
1166 " could not find ref ".lrref().
1167 " (should have been fetched from ".access_giturl()."#".rrref().")"
1168 unless $lastpush_hash;
1170 ensure_we_have_orig();
1171 if ($dsc_hash eq $lastpush_hash) {
1172 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1173 print STDERR <<END or die $!;
1175 Git commit in archive is behind the last version allegedly pushed/uploaded.
1176 Commit referred to by archive: $dsc_hash
1177 Last allegedly pushed/uploaded: $lastpush_hash
1180 $hash = $lastpush_hash;
1182 fail "archive's .dsc refers to ".$dsc_hash.
1183 " but this is an ancestor of ".$lastpush_hash;
1186 $hash = generate_commit_from_dsc();
1187 } elsif ($lastpush_hash) {
1188 # only in git, not in the archive yet
1189 $hash = $lastpush_hash;
1190 print STDERR <<END or die $!;
1192 Package not found in the archive, but has allegedly been pushed using dgit.
1196 printdebug "nothing found!\n";
1197 if (defined $skew_warning_vsn) {
1198 print STDERR <<END or die $!;
1200 Warning: relevant archive skew detected.
1201 Archive allegedly contains $skew_warning_vsn
1202 But we were not able to obtain any version from the archive or git.
1208 printdebug "current hash=$hash\n";
1209 if ($lastpush_hash) {
1210 fail "not fast forward on last upload branch!".
1211 " (archive's version left in DGIT_ARCHIVE)"
1212 unless is_fast_fwd($lastpush_hash, $hash);
1214 if (defined $skew_warning_vsn) {
1216 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1217 my $clogf = ".git/dgit/changelog.tmp";
1218 runcmd shell_cmd "exec >$clogf",
1219 @git, qw(cat-file blob), "$hash:debian/changelog";
1220 my $gotclogp = parsechangelog("-l$clogf");
1221 my $got_vsn = getfield $gotclogp, 'Version';
1222 printdebug "SKEW CHECK GOT $got_vsn\n";
1223 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
1224 print STDERR <<END or die $!;
1226 Warning: archive skew detected. Using the available version:
1227 Archive allegedly contains $skew_warning_vsn
1228 We were able to obtain only $got_vsn
1233 if ($lastpush_hash ne $hash) {
1234 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1238 dryrun_report @upd_cmd;
1246 canonicalise_suite();
1247 badusage "dry run makes no sense with clone" unless act_local();
1248 mkdir $dstdir or die "$dstdir $!";
1250 runcmd @git, qw(init -q);
1251 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1252 open H, "> .git/HEAD" or die $!;
1253 print H "ref: ".lref()."\n" or die $!;
1255 runcmd @git, qw(remote add), 'origin', access_giturl();
1256 if (check_for_git()) {
1257 progress "fetching existing git history";
1259 runcmd_ordryrun_local @git, qw(fetch origin);
1261 progress "starting new git history";
1263 fetch_from_archive() or no_such_package;
1264 runcmd @git, qw(reset --hard), lrref();
1265 printdone "ready for work in $dstdir";
1269 if (check_for_git()) {
1272 fetch_from_archive() or no_such_package();
1273 printdone "fetched into ".lrref();
1278 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1280 printdone "fetched to ".lrref()." and merged into HEAD";
1283 sub check_not_dirty () {
1284 return if $ignoredirty;
1285 my @cmd = (@git, qw(diff --quiet HEAD));
1286 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1287 $!=0; $?=0; system @cmd;
1288 return if !$! && !$?;
1289 if (!$! && $?==256) {
1290 fail "working tree is dirty (does not match HEAD)";
1296 sub commit_quilty_patch () {
1297 my $output = cmdoutput @git, qw(status --porcelain);
1299 foreach my $l (split /\n/, $output) {
1300 next unless $l =~ m/\S/;
1301 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1306 progress "nothing quilty to commit, ok.";
1309 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1310 my $m = "Commit Debian 3.0 (quilt) metadata";
1312 runcmd_ordryrun_local @git, qw(commit -m), $m;
1317 return 0 unless $format eq '3.0 (quilt)';
1318 progress "Format \`$format', urgh";
1320 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1326 sub push_parse_changelog ($) {
1329 my $clogp = Dpkg::Control::Hash->new();
1330 $clogp->load($clogpfn) or die;
1332 $package = getfield $clogp, 'Source';
1333 my $cversion = getfield $clogp, 'Version';
1334 my $tag = debiantag($cversion);
1335 runcmd @git, qw(check-ref-format), $tag;
1337 my $dscfn = dscfn($cversion);
1339 return ($clogp, $cversion, $tag, $dscfn);
1342 sub push_parse_dsc ($$$) {
1343 my ($dscfn,$dscfnwhat, $cversion) = @_;
1344 $dsc = parsecontrol($dscfn,$dscfnwhat);
1345 my $dversion = getfield $dsc, 'Version';
1346 my $dscpackage = getfield $dsc, 'Source';
1347 ($dscpackage eq $package && $dversion eq $cversion) or
1348 fail "$dscfn is for $dscpackage $dversion".
1349 " but debian/changelog is for $package $cversion";
1352 sub push_mktag ($$$$$$$) {
1353 my ($head,$clogp,$tag,
1355 $changesfile,$changesfilewhat,
1358 $dsc->{$ourdscfield[0]} = $head;
1359 $dsc->save("$dscfn.tmp") or die $!;
1361 my $changes = parsecontrol($changesfile,$changesfilewhat);
1362 foreach my $field (qw(Source Distribution Version)) {
1363 $changes->{$field} eq $clogp->{$field} or
1364 fail "changes field $field \`$changes->{$field}'".
1365 " does not match changelog \`$clogp->{$field}'";
1368 my $cversion = getfield $clogp, 'Version';
1369 my $clogsuite = getfield $clogp, 'Distribution';
1371 # We make the git tag by hand because (a) that makes it easier
1372 # to control the "tagger" (b) we can do remote signing
1373 my $authline = clogp_authline $clogp;
1374 open TO, '>', $tfn->('.tmp') or die $!;
1375 print TO <<END or die $!;
1381 $package release $cversion for $clogsuite ($csuite) [dgit]
1385 my $tagobjfn = $tfn->('.tmp');
1387 if (!defined $keyid) {
1388 $keyid = access_cfg('keyid','RETURN-UNDEF');
1390 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1391 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1392 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1393 push @sign_cmd, $tfn->('.tmp');
1394 runcmd_ordryrun @sign_cmd;
1396 $tagobjfn = $tfn->('.signed.tmp');
1397 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1398 $tfn->('.tmp'), $tfn->('.tmp.asc');
1405 sub sign_changes ($) {
1406 my ($changesfile) = @_;
1408 my @debsign_cmd = @debsign;
1409 push @debsign_cmd, "-k$keyid" if defined $keyid;
1410 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1411 push @debsign_cmd, $changesfile;
1412 runcmd_ordryrun @debsign_cmd;
1417 printdebug "actually entering push\n";
1420 access_giturl(); # check that success is vaguely likely
1422 my $clogpfn = ".git/dgit/changelog.822.tmp";
1423 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1425 responder_send_file('parsed-changelog', $clogpfn);
1427 my ($clogp, $cversion, $tag, $dscfn) =
1428 push_parse_changelog("$clogpfn");
1430 my $dscpath = "$buildproductsdir/$dscfn";
1432 fail "looked for .dsc $dscfn, but $!;".
1433 " maybe you forgot to build";
1435 responder_send_file('dsc', $dscpath);
1437 push_parse_dsc($dscpath, $dscfn, $cversion);
1439 my $format = getfield $dsc, 'Format';
1440 printdebug "format $format\n";
1441 if (madformat($format)) {
1442 commit_quilty_patch();
1446 progress "checking that $dscfn corresponds to HEAD";
1447 runcmd qw(dpkg-source -x --),
1448 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1449 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1450 changedir '../../../..';
1451 my $diffopt = $debug>0 ? '--exit-code' : '--quiet';
1452 my @diffcmd = (@git, qw(diff), $diffopt, $tree);
1453 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1455 my $r = system @diffcmd;
1458 fail "$dscfn specifies a different tree to your HEAD commit;".
1459 " perhaps you forgot to build".
1460 ($diffopt eq '--exit-code' ? "" :
1461 " (run with -D to see full diff output)");
1467 #do fast forward check and maybe fake merge
1468 # if (!is_fast_fwd(mainbranch
1469 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1470 # map { lref($_).":".rref($_) }
1472 my $head = rev_parse('HEAD');
1473 if (!$changesfile) {
1474 my $multi = "$buildproductsdir/".
1475 "${package}_".(stripepoch $cversion)."_multi.changes";
1476 if (stat "$multi") {
1477 $changesfile = $multi;
1479 $!==&ENOENT or die "$multi: $!";
1480 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1481 my @cs = glob "$buildproductsdir/$pat";
1482 fail "failed to find unique changes file".
1483 " (looked for $pat in $buildproductsdir, or $multi);".
1484 " perhaps you need to use dgit -C"
1486 ($changesfile) = @cs;
1489 $changesfile = "$buildproductsdir/$changesfile";
1492 responder_send_file('changes',$changesfile);
1493 responder_send_command("param head $head");
1494 responder_send_command("param csuite $csuite");
1496 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1499 if ($we_are_responder) {
1500 $tagobjfn = $tfn->('.signed.tmp');
1501 responder_receive_files('signed-tag', $tagobjfn);
1504 push_mktag($head,$clogp,$tag,
1506 $changesfile,$changesfile,
1510 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1511 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1512 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1513 runcmd_ordryrun @git, qw(tag -v --), $tag;
1515 if (!check_for_git()) {
1516 create_remote_git_repo();
1518 runcmd_ordryrun @git, qw(push),access_giturl(),
1519 "HEAD:".rrref(), "refs/tags/$tag";
1520 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1522 if ($we_are_responder) {
1523 my $dryrunsuffix = act_local() ? "" : ".tmp";
1524 responder_receive_files('signed-dsc-changes',
1525 "$dscpath$dryrunsuffix",
1526 "$changesfile$dryrunsuffix");
1529 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1531 progress "[new .dsc left in $dscpath.tmp]";
1533 sign_changes $changesfile;
1536 my $host = access_cfg('upload-host','RETURN-UNDEF');
1537 my @hostarg = defined($host) ? ($host,) : ();
1538 runcmd_ordryrun @dput, @hostarg, $changesfile;
1539 printdone "pushed and uploaded $cversion";
1541 responder_send_command("complete");
1547 badusage "-p is not allowed with clone; specify as argument instead"
1548 if defined $package;
1551 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1552 ($package,$isuite) = @ARGV;
1553 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1554 ($package,$dstdir) = @ARGV;
1555 } elsif (@ARGV==3) {
1556 ($package,$isuite,$dstdir) = @ARGV;
1558 badusage "incorrect arguments to dgit clone";
1560 $dstdir ||= "$package";
1563 fail "$dstdir already exists";
1564 } elsif ($! != &ENOENT) {
1569 if ($rmonerror && !$dryrun_level) {
1570 $cwd_remove= getcwd();
1572 return unless defined $cwd_remove;
1573 if (!chdir "$cwd_remove") {
1574 return if $!==&ENOENT;
1575 die "chdir $cwd_remove: $!";
1577 rmtree($dstdir) or die "remove $dstdir: $!\n";
1582 $cwd_remove = undef;
1585 sub branchsuite () {
1586 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1587 if ($branch =~ m#$lbranch_re#o) {
1594 sub fetchpullargs () {
1595 if (!defined $package) {
1596 my $sourcep = parsecontrol('debian/control','debian/control');
1597 $package = getfield $sourcep, 'Source';
1600 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1602 my $clogp = parsechangelog();
1603 $isuite = getfield $clogp, 'Distribution';
1605 canonicalise_suite();
1606 progress "fetching from suite $csuite";
1607 } elsif (@ARGV==1) {
1609 canonicalise_suite();
1611 badusage "incorrect arguments to dgit fetch or dgit pull";
1629 badusage "-p is not allowed with dgit push" if defined $package;
1631 my $clogp = parsechangelog();
1632 $package = getfield $clogp, 'Source';
1635 } elsif (@ARGV==1) {
1636 ($specsuite) = (@ARGV);
1638 badusage "incorrect arguments to dgit push";
1640 $isuite = getfield $clogp, 'Distribution';
1642 local ($package) = $existing_package; # this is a hack
1643 canonicalise_suite();
1645 if (defined $specsuite && $specsuite ne $isuite) {
1646 canonicalise_suite();
1647 $csuite eq $specsuite or
1648 fail "dgit push: changelog specifies $isuite ($csuite)".
1649 " but command line specifies $specsuite";
1651 if (check_for_git()) {
1654 if (fetch_from_archive()) {
1655 is_fast_fwd(lrref(), 'HEAD') or
1656 fail "dgit push: HEAD is not a descendant".
1657 " of the archive's version.\n".
1658 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1661 fail "package appears to be new in this suite;".
1662 " if this is intentional, use --new";
1667 #---------- remote commands' implementation ----------
1669 sub cmd_remote_push_responder {
1670 my ($nrargs) = shift @ARGV;
1671 my (@rargs) = @ARGV[0..$nrargs-1];
1672 @ARGV = @ARGV[$nrargs..$#ARGV];
1676 $we_are_responder = 1;
1678 open PI, "<&STDIN" or die $!;
1679 open STDIN, "/dev/null" or die $!;
1680 open PO, ">&STDOUT" or die $!;
1682 open STDOUT, ">&STDERR" or die $!;
1685 responder_send_command("dgit-remote-push-ready");
1697 printdebug "(killing remote child $i_child_pid)\n";
1698 kill 15, $i_child_pid;
1700 if (defined $i_tmp && !defined $initiator_tempdir) {
1702 eval { rmtree $i_tmp; };
1706 END { i_cleanup(); }
1709 my ($base,$selector,@args) = @_;
1710 $selector =~ s/\-/_/g;
1711 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1717 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1727 push @rdgit, @ropts;
1728 push @rdgit, qw(remote-push-responder), (scalar @rargs), @rargs;
1730 my @cmd = (@ssh, $host, shellquote @rdgit);
1731 printcmd \*DEBUG,$debugprefix."+",@cmd;
1733 if (defined $initiator_tempdir) {
1734 rmtree $initiator_tempdir;
1735 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1736 $i_tmp = $initiator_tempdir;
1740 $i_child_pid = open2(\*RO, \*RI, @cmd);
1742 initiator_expect { m/^dgit-remote-push-ready/ };
1744 my ($icmd,$iargs) = initiator_expect {
1745 m/^(\S+)(?: (.*))?$/;
1748 i_method "i_resp", $icmd, $iargs;
1752 sub i_resp_progress ($) {
1754 my $msg = protocol_read_bytes \*RO, $rhs;
1758 sub i_resp_complete {
1759 my $pid = $i_child_pid;
1760 $i_child_pid = undef; # prevents killing some other process with same pid
1761 printdebug "waiting for remote child $pid...\n";
1762 my $got = waitpid $pid, 0;
1763 die $! unless $got == $pid;
1764 die "remote child failed $?" if $?;
1767 printdebug "all done\n";
1771 sub i_resp_file ($) {
1773 my $localname = i_method "i_localname", $keyword;
1774 my $localpath = "$i_tmp/$localname";
1775 stat $localpath and badproto \*RO, "file $keyword ($localpath) twice";
1776 protocol_receive_file \*RO, $localpath;
1777 i_method "i_file", $keyword;
1782 sub i_resp_param ($) {
1783 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1789 sub i_resp_want ($) {
1791 die "$keyword ?" if $i_wanted{$keyword}++;
1792 my @localpaths = i_method "i_want", $keyword;
1793 printdebug "[[ $keyword @localpaths\n";
1794 foreach my $localpath (@localpaths) {
1795 protocol_send_file \*RI, $localpath;
1797 print RI "files-end\n" or die $!;
1800 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
1802 sub i_localname_parsed_changelog {
1803 return "remote-changelog.822";
1805 sub i_file_parsed_changelog {
1806 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
1807 push_parse_changelog "$i_tmp/remote-changelog.822";
1808 die if $i_dscfn =~ m#/|^\W#;
1811 sub i_localname_dsc {
1812 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1817 sub i_localname_changes {
1818 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1819 $i_changesfn = $i_dscfn;
1820 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
1821 return $i_changesfn;
1823 sub i_file_changes { }
1825 sub i_want_signed_tag {
1826 printdebug Dumper(\%i_param, $i_dscfn);
1827 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
1828 && defined $i_param{'csuite'}
1829 or badproto \*RO, "premature desire for signed-tag";
1830 my $head = $i_param{'head'};
1831 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
1833 die unless $i_param{'csuite'} =~ m/^$suite_re$/;
1835 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
1838 push_mktag $head, $i_clogp, $i_tag,
1840 $i_changesfn, 'remote changes',
1841 sub { "tag$_[0]"; };
1846 sub i_want_signed_dsc_changes {
1847 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
1848 sign_changes $i_changesfn;
1849 return ($i_dscfn, $i_changesfn);
1852 #---------- building etc. ----------
1858 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1860 sub build_maybe_quilt_fixup () {
1861 if (!open F, "debian/source/format") {
1862 die $! unless $!==&ENOENT;
1866 F->error and die $!;
1868 return unless madformat($_);
1871 my @cmd = (@git, qw(ls-files --exclude-standard -iodm));
1872 my $problems = cmdoutput @cmd;
1873 if (length $problems) {
1874 print STDERR "problematic files:\n";
1875 print STDERR " $_\n" foreach split /\n/, $problems;
1876 fail "Cannot do quilt fixup in tree containing ignored files. ".
1877 "Perhaps your package's clean target is broken, in which".
1878 " case -wg (which says to use git-clean -xdf) may help.";
1881 my $clogp = parsechangelog();
1882 my $version = getfield $clogp, 'Version';
1883 my $author = getfield $clogp, 'Maintainer';
1884 my $headref = rev_parse('HEAD');
1887 my $patchname = "auto-$version-$headref-$time";
1888 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1890 my $descfn = ".git/dgit/quilt-description.tmp";
1891 open O, '>', $descfn or die "$descfn: $!";
1893 $msg =~ s/^\s+$/ ./mg;
1894 print O <<END or die $!;
1895 Description: Automatically generated patch ($clogp->{Version})
1896 Last (up to) $ncommits git changes, FYI:
1906 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1907 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1908 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1909 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
1912 if (!open P, '>>', ".pc/applied-patches") {
1913 $!==&ENOENT or die $!;
1918 commit_quilty_patch();
1921 sub quilt_fixup_editor () {
1922 my $descfn = $ENV{$fakeeditorenv};
1923 my $editing = $ARGV[$#ARGV];
1924 open I1, '<', $descfn or die "$descfn: $!";
1925 open I2, '<', $editing or die "$editing: $!";
1926 unlink $editing or die "$editing: $!";
1927 open O, '>', $editing or die "$editing: $!";
1928 while (<I1>) { print O or die $!; } I1->error and die $!;
1931 $copying ||= m/^\-\-\- /;
1932 next unless $copying;
1935 I2->error and die $!;
1941 if ($cleanmode eq 'dpkg-source') {
1942 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
1943 } elsif ($cleanmode eq 'git') {
1944 runcmd_ordryrun_local @git, qw(clean -xdf);
1945 } elsif ($cleanmode eq 'none') {
1952 badusage "clean takes no additional arguments" if @ARGV;
1957 badusage "-p is not allowed when building" if defined $package;
1960 my $clogp = parsechangelog();
1961 $isuite = getfield $clogp, 'Distribution';
1962 $package = getfield $clogp, 'Source';
1963 $version = getfield $clogp, 'Version';
1964 build_maybe_quilt_fixup();
1967 sub changesopts () {
1968 my @opts =@changesopts[1..$#changesopts];
1969 if (!defined $changes_since_version) {
1970 my @vsns = archive_query('archive_query');
1971 my @quirk = access_quirk();
1972 if ($quirk[0] eq 'backports') {
1973 local $isuite = $quirk[2];
1975 canonicalise_suite();
1976 push @vsns, archive_query('archive_query');
1979 @vsns = map { $_->[0] } @vsns;
1980 @vsns = sort { -version_compare_string($a, $b) } @vsns;
1981 $changes_since_version = $vsns[0];
1982 progress "changelog will contain changes since $vsns[0]";
1984 $changes_since_version = '_';
1985 progress "package seems new, not specifying -v<version>";
1988 if ($changes_since_version ne '_') {
1989 unshift @opts, "-v$changes_since_version";
1996 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1997 printdone "build successful\n";
2003 (qw(git-buildpackage -us -uc --git-no-sign-tags),
2004 "--git-builder=@dpkgbuildpackage");
2005 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
2006 canonicalise_suite();
2007 push @cmd, "--git-debian-branch=".lbranch();
2009 push @cmd, changesopts();
2010 runcmd_ordryrun_local @cmd, @ARGV;
2011 printdone "build successful\n";
2016 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
2017 $dscfn = dscfn($version);
2018 if ($cleanmode eq 'dpkg-source') {
2019 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
2022 my $pwd = must_getcwd();
2023 my $leafdir = basename $pwd;
2025 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
2027 runcmd_ordryrun_local qw(sh -ec),
2028 'exec >$1; shift; exec "$@"','x',
2029 "../$sourcechanges",
2030 @dpkggenchanges, qw(-S), changesopts();
2034 sub cmd_build_source {
2035 badusage "build-source takes no additional arguments" if @ARGV;
2037 printdone "source built, results in $dscfn and $sourcechanges";
2043 my $pat = "${package}_".(stripepoch $version)."_*.changes";
2045 stat $dscfn or fail "$dscfn (in parent directory): $!";
2046 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
2047 foreach my $cf (glob $pat) {
2048 next if $cf eq $sourcechanges;
2049 unlink $cf or fail "remove $cf: $!";
2052 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
2053 my @changesfiles = glob $pat;
2054 @changesfiles = sort {
2055 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
2058 fail "wrong number of different changes files (@changesfiles)"
2059 unless @changesfiles;
2060 runcmd_ordryrun_local @mergechanges, @changesfiles;
2061 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
2063 stat $multichanges or fail "$multichanges: $!";
2065 printdone "build successful, results in $multichanges\n" or die $!;
2068 sub cmd_quilt_fixup {
2069 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
2070 my $clogp = parsechangelog();
2071 $version = getfield $clogp, 'Version';
2072 build_maybe_quilt_fixup();
2075 #---------- argument parsing and main program ----------
2078 print "dgit version $our_version\n" or die $!;
2085 if (defined $ENV{'DGIT_SSH'}) {
2086 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
2087 } elsif (defined $ENV{'GIT_SSH'}) {
2088 @ssh = ($ENV{'GIT_SSH'});
2092 last unless $ARGV[0] =~ m/^-/;
2096 if (m/^--dry-run$/) {
2099 } elsif (m/^--damp-run$/) {
2102 } elsif (m/^--no-sign$/) {
2105 } elsif (m/^--help$/) {
2107 } elsif (m/^--version$/) {
2109 } elsif (m/^--new$/) {
2112 } elsif (m/^--since-version=([^_]+|_)$/) {
2114 $changes_since_version = $1;
2115 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
2116 ($om = $opts_opt_map{$1}) &&
2120 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
2121 !$opts_opt_cmdonly{$1} &&
2122 ($om = $opts_opt_map{$1})) {
2125 } elsif (m/^--existing-package=(.*)/s) {
2127 $existing_package = $1;
2128 } elsif (m/^--initiator-tempdir=(.*)/s) {
2129 $initiator_tempdir = $1;
2130 $initiator_tempdir =~ m#^/# or
2131 badusage "--initiator-tempdir must be used specify an".
2132 " absolute, not relative, directory."
2133 } elsif (m/^--distro=(.*)/s) {
2136 } elsif (m/^--build-products-dir=(.*)/s) {
2138 $buildproductsdir = $1;
2139 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
2142 } elsif (m/^--clean=(.*)$/s) {
2143 badusage "unknown cleaning mode \`$1'";
2144 } elsif (m/^--ignore-dirty$/s) {
2147 } elsif (m/^--no-quilt-fixup$/s) {
2150 } elsif (m/^--no-rm-on-error$/s) {
2154 badusage "unknown long option \`$_'";
2161 } elsif (s/^-L/-/) {
2164 } elsif (s/^-h/-/) {
2166 } elsif (s/^-D/-/) {
2168 open DEBUG, ">&STDERR" or die $!;
2171 } elsif (s/^-N/-/) {
2174 } elsif (s/^-v([^_]+|_)$//s) {
2176 $changes_since_version = $1;
2179 push @changesopts, $_;
2181 } elsif (s/^-c(.*=.*)//s) {
2183 push @git, '-c', $1;
2184 } elsif (s/^-d(.*)//s) {
2187 } elsif (s/^-C(.*)//s) {
2190 if ($changesfile =~ s#^(.*)/##) {
2191 $buildproductsdir = $1;
2193 } elsif (s/^-k(.*)//s) {
2195 } elsif (s/^-wn//s) {
2197 $cleanmode = 'none';
2198 } elsif (s/^-wg//s) {
2201 } elsif (s/^-wd//s) {
2203 $cleanmode = 'dpkg-source';
2205 badusage "unknown short option \`$_'";
2212 if ($ENV{$fakeeditorenv}) {
2213 quilt_fixup_editor();
2217 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2218 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2219 if $dryrun_level == 1;
2221 print STDERR $helpmsg or die $!;
2224 my $cmd = shift @ARGV;
2227 my $fn = ${*::}{"cmd_$cmd"};
2228 $fn or badusage "unknown operation $cmd";