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);
33 our $our_version = 'UNRELEASED'; ###substituted###
35 our $isuite = 'unstable';
41 our $dryrun_level = 0;
43 our $buildproductsdir = '..';
48 our $existing_package = 'dpkg';
49 our $cleanmode = 'dpkg-source';
50 our $changes_since_version;
51 our $we_are_responder;
52 our $initiator_tempdir;
54 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
56 our $suite_re = '[-+.0-9a-z]+';
59 our (@dget) = qw(dget);
60 our (@curl) = qw(curl -f);
61 our (@dput) = qw(dput);
62 our (@debsign) = qw(debsign);
64 our (@sbuild) = qw(sbuild -A);
66 our (@dgit) = qw(dgit);
67 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
68 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
69 our (@dpkggenchanges) = qw(dpkg-genchanges);
70 our (@mergechanges) = qw(mergechanges -f);
71 our (@changesopts) = ('');
73 our %opts_opt_map = ('dget' => \@dget, # accept for compatibility
76 'debsign' => \@debsign,
81 'dpkg-source' => \@dpkgsource,
82 'dpkg-buildpackage' => \@dpkgbuildpackage,
83 'dpkg-genchanges' => \@dpkggenchanges,
84 'ch' => \@changesopts,
85 'mergechanges' => \@mergechanges);
87 our %opts_opt_cmdonly = ('gpg' => 1);
92 open DEBUG, ">/dev/null" or die $!;
96 our $remotename = 'dgit';
97 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
98 our $branchprefix = 'dgit';
101 sub lbranch () { return "$branchprefix/$csuite"; }
102 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
103 sub lref () { return "refs/heads/".lbranch(); }
104 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
105 sub rrref () { return "refs/$branchprefix/$csuite"; }
120 return "${package}_".(stripepoch $vsn).".dsc";
124 our $debugprefix = '';
129 foreach my $f (@end) {
131 warn "$us: cleanup: $@" if length $@;
135 sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
138 die $us.($we_are_responder ? " (build host)" : "").": @_\n";
141 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
143 sub no_such_package () {
144 print STDERR "$us: package $package does not exist in suite $isuite\n";
150 return "+".rrref().":".lrref();
155 printdebug "CD $newdir\n";
156 chdir $newdir or die "chdir: $newdir: $!";
159 #---------- remote protocol support, common ----------
161 # remote push initiator/responder protocol:
162 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
164 # > file parsed-changelog
165 # [indicates that output of dpkg-parsechangelog follows]
166 # > data-block NBYTES
167 # > [NBYTES bytes of data (no newline)]
168 # [maybe some more blocks]
180 # [indicates that signed tag is wanted]
181 # < data-block NBYTES
182 # < [NBYTES bytes of data (no newline)]
183 # [maybe some more blocks]
187 # > want signed-dsc-changes
188 # < data-block NBYTES [transfer of signed dsc]
190 # < data-block NBYTES [transfer of signed changes]
198 fail "connection lost: $!" if $fh->error;
199 fail "protocol violation; $m not expected";
202 sub protocol_expect (&$) {
203 my ($match, $fh) = @_;
206 defined && chomp or badproto $fh, "eof";
214 badproto $fh, "\`$_'";
217 sub protocol_send_file ($$) {
218 my ($fh, $ourfn) = @_;
219 open PF, "<", $ourfn or die "$ourfn: $!";
222 my $got = read PF, $d, 65536;
223 die "$ourfn: $!" unless defined $got;
225 print $fh "data-block ".length($d)."\n" or die $!;
226 print $fh $d or die $!;
228 PF->error and die "$ourfn $!";
229 print $fh "data-end\n" or die $!;
233 sub protocol_read_bytes ($$) {
234 my ($fh, $nbytes) = @_;
235 $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
237 my $got = read $fh, $d, $nbytes;
238 $got==$nbytes or badproto $fh, "eof during data block";
242 sub protocol_receive_file ($$) {
243 my ($fh, $ourfn) = @_;
244 printdebug "() $ourfn\n";
245 open PF, ">", $ourfn or die "$ourfn: $!";
247 my ($y,$l) = protocol_expect {
248 m/^data-block (.*)$/ ? (1,$1) :
249 m/^data-end$/ ? (0,) :
253 my $d = protocol_read_bytes $fh, $l;
254 print PF $d or die $!;
259 #---------- remote protocol support, responder ----------
261 sub responder_send_command ($) {
263 return unless $we_are_responder;
264 # called even without $we_are_responder
265 printdebug ">> $command\n";
266 print PO $command, "\n" or die $!;
269 sub responder_send_file ($$) {
270 my ($keyword, $ourfn) = @_;
271 return unless $we_are_responder;
272 printdebug "]] $keyword $ourfn\n";
273 responder_send_command "file $keyword";
274 protocol_send_file \*PO, $ourfn;
277 sub responder_receive_files ($@) {
278 my ($keyword, @ourfns) = @_;
279 die unless $we_are_responder;
280 printdebug "[[ $keyword @ourfns\n";
281 responder_send_command "want $keyword";
282 foreach my $fn (@ourfns) {
283 protocol_receive_file \*PI, $fn;
286 protocol_expect { m/^files-end$/ } \*PI;
289 #---------- remote protocol support, initiator ----------
291 sub initiator_expect (&) {
293 protocol_expect { &$match } \*RO;
296 #---------- end remote code ----------
299 if ($we_are_responder) {
301 responder_send_command "progress ".length($m) or die $!;
302 print PO $m or die $!;
312 $ua = LWP::UserAgent->new();
316 progress "downloading $what...";
317 my $r = $ua->get(@_) or die $!;
318 return undef if $r->code == 404;
319 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
320 return $r->decoded_content();
323 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
330 if (m{[^-=_./0-9a-z]}i) {
337 return join ' ', @out;
342 my $intro = shift @_;
343 print $fh $intro," " or die $!;
344 print $fh shellquote @_ or die $!;
345 print $fh "\n" or die $!;
349 { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
351 fail "failed to fork/exec: $!";
352 } elsif (!($? & 0xff)) {
353 fail "subprocess failed with error exit status ".($?>>8);
355 fail "subprocess crashed (wait status $?)";
357 fail "subprocess produced invalid output";
362 printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
364 failedcmd @_ if system @_;
367 sub act_local () { return $dryrun_level <= 1; }
368 sub act_scary () { return !$dryrun_level; }
371 if (!$dryrun_level) {
372 progress "dgit ok: @_";
374 progress "would be ok: @_ (but dry run only)";
378 sub cmdoutput_errok {
379 die Dumper(\@_)." ?" if grep { !defined } @_;
380 printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
381 open P, "-|", @_ or die $!;
384 { local $/ = undef; $d = <P>; }
386 if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
389 printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
394 my $d = cmdoutput_errok @_;
395 defined $d or failedcmd @_;
400 printcmd(\*STDERR,$debugprefix."#",@_);
403 sub runcmd_ordryrun {
411 sub runcmd_ordryrun_local {
420 my ($first_shell, @cmd) = @_;
421 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
424 our $helpmsg = <<END;
426 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
427 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
428 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
429 dgit [dgit-opts] push [dgit-opts] [suite]
430 dgit [dgit-opts] rpush build-host:build-dir ...
431 important dgit options:
432 -k<keyid> sign tag and package with <keyid> instead of default
433 --dry-run -n do not change anything, but go through the motions
434 --damp-run -L like --dry-run but make local changes, without signing
435 --new -N allow introducing a new package
436 --debug -D increase debug level
437 -c<name>=<value> set git config option (used directly by dgit too)
440 our $later_warning_msg = <<END;
441 Perhaps the upload is stuck in incoming. Using the version from git.
445 print STDERR "$us: @_\n", $helpmsg or die $!;
450 @ARGV or badusage "too few arguments";
451 return scalar shift @ARGV;
455 print $helpmsg or die $!;
459 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
461 our %defcfg = ('dgit.default.distro' => 'debian',
462 'dgit.default.username' => '',
463 'dgit.default.archive-query-default-component' => 'main',
464 'dgit.default.ssh' => 'ssh',
465 'dgit-distro.debian.git-host' => 'git.debian.org',
466 'dgit-distro.debian.git-proto' => 'git+ssh://',
467 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
468 'dgit-distro.debian.git-check' => 'ssh-cmd',
469 'dgit-distro.debian.git-create' => 'ssh-cmd',
470 'dgit-distro.debian.sshpsql-host' => 'mirror.ftp-master.debian.org',
471 'dgit-distro.debian.sshpsql-dbname' => 'service=projectb',
472 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
473 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
474 'dgit-distro.debian.backports-quirk' => '(squeeze)-backports*',
475 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
476 'dgit-distro.ubuntu.git-check' => 'false',
477 'dgit-distro.ubuntu.mirror' => 'http://archive.ubuntu.com/ubuntu',
478 'dgit-distro.test-dummy.ssh' => "$td/ssh",
479 'dgit-distro.test-dummy.username' => "alice",
480 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
481 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
482 'dgit-distro.test-dummy.git-url' => "$td/git",
483 'dgit-distro.test-dummy.git-host' => "git",
484 'dgit-distro.test-dummy.git-path' => "$td/git",
485 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
486 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
487 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
492 return undef if $c =~ /RETURN-UNDEF/;
493 my @cmd = (@git, qw(config --), $c);
496 local ($debug) = $debug-1;
497 $v = cmdoutput_errok @cmd;
504 my $dv = $defcfg{$c};
505 return $dv if defined $dv;
507 badcfg "need value for one of: @_\n".
508 "$us: distro or suite appears not to be (properly) supported";
511 sub access_basedistro () {
512 if (defined $idistro) {
513 return cfg("dgit-distro.basedistro.distro",
514 "dgit-suite.$isuite.distro",
515 'RETURN-UNDEF') // $idistro;
517 return cfg("dgit-suite.$isuite.distro",
518 "dgit.default.distro");
522 sub access_quirk () {
523 # returns (quirk name, distro to use instead, quirk-specific info)
524 my $basedistro = access_basedistro();
525 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
527 if (defined $backports_quirk) {
528 my $re = $backports_quirk;
529 $re =~ s/[^-0-9a-z_\%*()]/\\$&/ig;
531 $re =~ s/\%/([-0-9a-z_]+)/
532 or $re =~ m/[()]/ or badcfg "backports-quirk needs \% or ( )";
533 if ($isuite =~ m/^$re$/) {
534 return ('backports',"$basedistro-backports",$1);
537 return ('none',$basedistro);
540 sub access_distro () {
541 return (access_quirk())[1];
546 my $basedistro = access_basedistro();
547 my $distro = $idistro || access_distro();
548 my $value = cfg(map {
549 ("dgit-distro.$distro.$_",
550 "dgit-distro.$basedistro.$_",
556 sub string_to_ssh ($) {
558 if ($spec =~ m/\s/) {
559 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
565 sub access_cfg_ssh () {
566 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
567 if (!defined $gitssh) {
570 return string_to_ssh $gitssh;
574 sub access_someuserhost ($) {
576 my $user = access_cfg("$some-user",'username');
577 my $host = access_cfg("$some-host");
578 return length($user) ? "$user\@$host" : $host;
581 sub access_gituserhost () {
582 return access_someuserhost('git');
585 sub access_giturl () {
586 my $url = access_cfg('git-url','RETURN-UNDEF');
589 access_cfg('git-proto').
590 access_gituserhost().
591 access_cfg('git-path');
593 return "$url/$package.git";
596 sub parsecontrolfh ($$@) {
597 my ($fh, $desc, @opts) = @_;
598 my %opts = ('name' => $desc, @opts);
599 my $c = Dpkg::Control::Hash->new(%opts);
600 $c->parse($fh) or die "parsing of $desc failed";
605 my ($file, $desc) = @_;
606 my $fh = new IO::Handle;
607 open $fh, '<', $file or die "$file: $!";
608 my $c = parsecontrolfh($fh,$desc);
609 $fh->error and die $!;
615 my ($dctrl,$field) = @_;
616 my $v = $dctrl->{$field};
617 return $v if defined $v;
618 fail "missing field $field in ".$v->get_option('name');
622 my $c = Dpkg::Control::Hash->new();
623 my $p = new IO::Handle;
624 my @cmd = (qw(dpkg-parsechangelog), @_);
625 open $p, '-|', @cmd or die $!;
627 $?=0; $!=0; close $p or failedcmd @cmd;
631 sub git_get_ref ($) {
633 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
635 $?==256 or fail "git show-ref failed (status $?)";
636 printdebug "ref $refname= [show-ref exited 1]\n";
639 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
640 printdebug "ref $refname=$1\n";
643 printdebug "ref $refname= [no match]\n";
650 defined $d or fail "getcwd failed: $!";
656 sub archive_query ($) {
658 my $query = access_cfg('archive-query','RETURN-UNDEF');
659 if (!defined $query) {
660 my $distro = access_basedistro();
661 if ($distro eq 'debian') {
663 access_someuserhost('sshpsql').':'.
664 access_cfg('sshpsql-dbname');
666 $query = "madison:$distro";
669 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
672 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
675 sub pool_dsc_subpath ($$) {
676 my ($vsn,$component) = @_; # $package is implict arg
677 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
678 return "/pool/$component/$prefix/$package/".dscfn($vsn);
681 sub archive_query_madison ($$) {
682 my ($proto,$data) = @_;
683 die unless $proto eq 'madison';
684 $rmad{$package} ||= cmdoutput
685 qw(rmadison -asource),"-s$isuite","-u$data",$package;
686 my $rmad = $rmad{$package};
687 return madison_parse($rmad);
690 sub madison_parse ($) {
693 foreach my $l (split /\n/, $rmad) {
694 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
695 \s*( [^ \t|]+ )\s* \|
696 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
697 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
698 $1 eq $package or die "$rmad $package ?";
705 $component = access_cfg('archive-query-default-component');
707 $5 eq 'source' or die "$rmad ?";
708 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
710 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
713 sub canonicalise_suite_madison ($$) {
714 # madison canonicalises for us
715 my @r = archive_query_madison($_[0],$_[1]);
717 "unable to canonicalise suite using package $package".
718 " which does not appear to exist in suite $isuite;".
719 " --existing-package may help";
724 my ($data,$sql) = @_;
725 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
726 my ($userhost,$dbname) = ($`,$'); #';
728 my @cmd = (access_cfg_ssh, $userhost,
729 "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
730 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
731 open P, "-|", @cmd or die $!;
734 printdebug("$debugprefix>|$_|\n");
737 $!=0; $?=0; close P or failedcmd @cmd;
739 my $nrows = pop @rows;
740 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
741 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
742 @rows = map { [ split /\|/, $_ ] } @rows;
743 my $ncols = scalar @{ shift @rows };
744 die if grep { scalar @$_ != $ncols } @rows;
748 sub sql_injection_check {
749 foreach (@_) { die "$_ $& ?" if m/[']/; }
752 sub archive_query_sshpsql ($$) {
753 my ($proto,$data) = @_;
754 sql_injection_check $isuite, $package;
755 my @rows = sshpsql($data, <<END);
756 SELECT source.version, component.name, files.filename
758 JOIN src_associations ON source.id = src_associations.source
759 JOIN suite ON suite.id = src_associations.suite
760 JOIN dsc_files ON dsc_files.source = source.id
761 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
762 JOIN component ON component.id = files_archive_map.component_id
763 JOIN files ON files.id = dsc_files.file
764 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
765 AND source.source='$package'
766 AND files.filename LIKE '%.dsc';
768 @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
770 my ($vsn,$component,$filename) = @$_;
771 [ $vsn, "/pool/$component/$filename" ];
776 sub canonicalise_suite_sshpsql ($$) {
777 my ($proto,$data) = @_;
778 sql_injection_check $isuite;
779 my @rows = sshpsql($data, <<END);
780 SELECT suite.codename
781 FROM suite where suite_name='$isuite' or codename='$isuite';
783 @rows = map { $_->[0] } @rows;
784 fail "unknown suite $isuite" unless @rows;
785 die "ambiguous $isuite: @rows ?" if @rows>1;
789 sub canonicalise_suite_dummycat ($$) {
790 my ($proto,$data) = @_;
791 my $dpath = "$data/suite.$isuite";
792 if (!open C, "<", $dpath) {
793 $!==ENOENT or die "$dpath: $!";
794 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
798 chomp or die "$dpath: $!";
800 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
804 sub archive_query_dummycat ($$) {
805 my ($proto,$data) = @_;
806 canonicalise_suite();
807 my $dpath = "$data/package.$csuite.$package";
808 if (!open C, "<", $dpath) {
809 $!==ENOENT or die "$dpath: $!";
810 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
818 printdebug "dummycat query $csuite $package $dpath | $_\n";
819 my @row = split /\s+/, $_;
820 @row==2 or die "$dpath: $_ ?";
823 C->error and die "$dpath: $!";
825 return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
828 sub canonicalise_suite () {
829 return if defined $csuite;
830 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
831 $csuite = archive_query('canonicalise_suite');
832 if ($isuite ne $csuite) {
833 progress "canonical suite name for $isuite is $csuite";
837 sub get_archive_dsc () {
838 canonicalise_suite();
839 my @vsns = archive_query('archive_query');
840 foreach my $vinfo (@vsns) {
841 my ($vsn,$subpath) = @$vinfo;
842 $dscurl = access_cfg('mirror').$subpath;
843 $dscdata = url_get($dscurl);
845 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
848 my $dscfh = new IO::File \$dscdata, '<' or die $!;
849 printdebug Dumper($dscdata) if $debug>1;
850 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
851 printdebug Dumper($dsc) if $debug>1;
852 my $fmt = getfield $dsc, 'Format';
853 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
859 sub check_for_git () {
861 my $how = access_cfg('git-check');
862 if ($how eq 'ssh-cmd') {
864 (access_cfg_ssh, access_gituserhost(),
865 " set -e; cd ".access_cfg('git-path').";".
866 " if test -d $package.git; then echo 1; else echo 0; fi");
867 my $r= cmdoutput @cmd;
868 failedcmd @cmd unless $r =~ m/^[01]$/;
870 } elsif ($how eq 'true') {
872 } elsif ($how eq 'false') {
875 badcfg "unknown git-check \`$how'";
879 sub create_remote_git_repo () {
880 my $how = access_cfg('git-create');
881 if ($how eq 'ssh-cmd') {
883 (access_cfg_ssh, access_gituserhost(),
884 "set -e; cd ".access_cfg('git-path').";".
885 " cp -a _template $package.git");
886 } elsif ($how eq 'true') {
889 badcfg "unknown git-create \`$how'";
893 our ($dsc_hash,$lastpush_hash);
895 our $ud = '.git/dgit/unpack';
903 sub mktree_in_ud_from_only_subdir () {
904 # changes into the subdir
907 $dirs[0] =~ m#^([^/]+)/\.$# or die;
910 fail "source package contains .git directory" if stat '.git';
911 die $! unless $!==&ENOENT;
912 runcmd qw(git init -q);
913 rmtree('.git/objects');
914 symlink '../../../../objects','.git/objects' or die $!;
915 runcmd @git, qw(add -Af);
916 my $tree = cmdoutput @git, qw(write-tree);
917 $tree =~ m/^\w+$/ or die "$tree ?";
921 sub dsc_files_info () {
922 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
923 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
924 ['Files', 'Digest::MD5', 'new()']) {
925 my ($fname, $module, $method) = @$csumi;
926 my $field = $dsc->{$fname};
927 next unless defined $field;
928 eval "use $module; 1;" or die $@;
930 foreach (split /\n/, $field) {
932 m/^(\w+) (\d+) (\S+)$/ or
933 fail "could not parse .dsc $fname line \`$_'";
934 my $digester = eval "$module"."->$method;" or die $@;
939 Digester => $digester,
944 fail "missing any supported Checksums-* or Files field in ".
945 $dsc->get_option('name');
949 map { $_->{Filename} } dsc_files_info();
952 sub is_orig_file ($) {
954 m/\.orig(?:-\w+)?\.tar\.\w+$/;
957 sub make_commit ($) {
959 return cmdoutput @git, qw(hash-object -w -t commit), $file;
962 sub clogp_authline ($) {
964 my $author = getfield $clogp, 'Maintainer';
965 $author =~ s#,.*##ms;
966 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
967 my $authline = "$author $date";
968 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
969 fail "unexpected commit author line format \`$authline'".
970 " (was generated from changelog Maintainer field)";
974 sub generate_commit_from_dsc () {
978 foreach my $fi (dsc_files_info()) {
979 my $f = $fi->{Filename};
980 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
982 link "../../../$f", $f
986 complete_file_from_dsc('.', $fi);
988 if (is_orig_file($f)) {
989 link $f, "../../../../$f"
995 my $dscfn = "$package.dsc";
997 open D, ">", $dscfn or die "$dscfn: $!";
998 print D $dscdata or die "$dscfn: $!";
999 close D or die "$dscfn: $!";
1000 my @cmd = qw(dpkg-source);
1001 push @cmd, qw(-x --), $dscfn;
1004 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1005 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
1006 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
1007 my $authline = clogp_authline $clogp;
1008 my $changes = getfield $clogp, 'Changes';
1009 open C, ">../commit.tmp" or die $!;
1010 print C <<END or die $!;
1017 # imported from the archive
1020 my $outputhash = make_commit qw(../commit.tmp);
1021 my $cversion = getfield $clogp, 'Version';
1022 progress "synthesised git commit from .dsc $cversion";
1023 if ($lastpush_hash) {
1024 runcmd @git, qw(reset --hard), $lastpush_hash;
1025 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
1026 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
1027 my $oversion = getfield $oldclogp, 'Version';
1029 version_compare_string($oversion, $cversion);
1031 # git upload/ is earlier vsn than archive, use archive
1032 open C, ">../commit2.tmp" or die $!;
1033 print C <<END or die $!;
1035 parent $lastpush_hash
1040 Record $package ($cversion) in archive suite $csuite
1042 $outputhash = make_commit qw(../commit2.tmp);
1043 } elsif ($vcmp > 0) {
1044 print STDERR <<END or die $!;
1046 Version actually in archive: $cversion (older)
1047 Last allegedly pushed/uploaded: $oversion (newer or same)
1050 $outputhash = $lastpush_hash;
1052 $outputhash = $lastpush_hash;
1055 changedir '../../../..';
1056 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1057 'DGIT_ARCHIVE', $outputhash;
1058 cmdoutput @git, qw(log -n2), $outputhash;
1059 # ... gives git a chance to complain if our commit is malformed
1064 sub complete_file_from_dsc ($$) {
1065 our ($dstdir, $fi) = @_;
1066 # Ensures that we have, in $dir, the file $fi, with the correct
1067 # contents. (Downloading it from alongside $dscurl if necessary.)
1069 my $f = $fi->{Filename};
1070 my $tf = "$dstdir/$f";
1074 progress "using existing $f";
1076 die "$tf $!" unless $!==&ENOENT;
1079 $furl =~ s{/[^/]+$}{};
1081 die "$f ?" unless $f =~ m/^${package}_/;
1082 die "$f ?" if $f =~ m#/#;
1083 runcmd_ordryrun_local @curl,qw(-o),$tf,'--',"$furl";
1084 next if !act_local();
1088 open F, "<", "$tf" or die "$tf: $!";
1089 $fi->{Digester}->reset();
1090 $fi->{Digester}->addfile(*F);
1091 F->error and die $!;
1092 my $got = $fi->{Digester}->hexdigest();
1093 $got eq $fi->{Hash} or
1094 fail "file $f has hash $got but .dsc".
1095 " demands hash $fi->{Hash} ".
1096 ($downloaded ? "(got wrong file from archive!)"
1097 : "(perhaps you should delete this file?)");
1100 sub ensure_we_have_orig () {
1101 foreach my $fi (dsc_files_info()) {
1102 my $f = $fi->{Filename};
1103 next unless is_orig_file($f);
1104 complete_file_from_dsc('..', $fi);
1109 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1112 sub is_fast_fwd ($$) {
1113 my ($ancestor,$child) = @_;
1114 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1115 my $mb = cmdoutput_errok @cmd;
1117 return rev_parse($mb) eq rev_parse($ancestor);
1119 $?==256 or failedcmd @cmd;
1124 sub git_fetch_us () {
1125 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1128 sub fetch_from_archive () {
1129 # ensures that lrref() is what is actually in the archive,
1130 # one way or another
1134 foreach my $field (@ourdscfield) {
1135 $dsc_hash = $dsc->{$field};
1136 last if defined $dsc_hash;
1138 if (defined $dsc_hash) {
1139 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1141 progress "last upload to archive specified git hash";
1143 progress "last upload to archive has NO git hash";
1146 progress "no version available from the archive";
1149 $lastpush_hash = git_get_ref(lrref());
1150 printdebug "previous reference hash=$lastpush_hash\n";
1152 if (defined $dsc_hash) {
1153 fail "missing remote git history even though dsc has hash -".
1154 " could not find ref ".lrref().
1155 " (should have been fetched from ".access_giturl()."#".rrref().")"
1156 unless $lastpush_hash;
1158 ensure_we_have_orig();
1159 if ($dsc_hash eq $lastpush_hash) {
1160 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1161 print STDERR <<END or die $!;
1163 Git commit in archive is behind the last version allegedly pushed/uploaded.
1164 Commit referred to by archive: $dsc_hash
1165 Last allegedly pushed/uploaded: $lastpush_hash
1168 $hash = $lastpush_hash;
1170 fail "archive's .dsc refers to ".$dsc_hash.
1171 " but this is an ancestor of ".$lastpush_hash;
1174 $hash = generate_commit_from_dsc();
1175 } elsif ($lastpush_hash) {
1176 # only in git, not in the archive yet
1177 $hash = $lastpush_hash;
1178 print STDERR <<END or die $!;
1180 Package not found in the archive, but has allegedly been pushed using dgit.
1184 printdebug "nothing found!\n";
1185 if (defined $skew_warning_vsn) {
1186 print STDERR <<END or die $!;
1188 Warning: relevant archive skew detected.
1189 Archive allegedly contains $skew_warning_vsn
1190 But we were not able to obtain any version from the archive or git.
1196 printdebug "current hash=$hash\n";
1197 if ($lastpush_hash) {
1198 fail "not fast forward on last upload branch!".
1199 " (archive's version left in DGIT_ARCHIVE)"
1200 unless is_fast_fwd($lastpush_hash, $hash);
1202 if (defined $skew_warning_vsn) {
1204 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1205 my $clogf = ".git/dgit/changelog.tmp";
1206 runcmd shell_cmd "exec >$clogf",
1207 @git, qw(cat-file blob), "$hash:debian/changelog";
1208 my $gotclogp = parsechangelog("-l$clogf");
1209 my $got_vsn = getfield $gotclogp, 'Version';
1210 printdebug "SKEW CHECK GOT $got_vsn\n";
1211 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
1212 print STDERR <<END or die $!;
1214 Warning: archive skew detected. Using the available version:
1215 Archive allegedly contains $skew_warning_vsn
1216 We were able to obtain only $got_vsn
1221 if ($lastpush_hash ne $hash) {
1222 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1226 dryrun_report @upd_cmd;
1234 canonicalise_suite();
1235 badusage "dry run makes no sense with clone" unless act_local();
1236 mkdir $dstdir or die "$dstdir $!";
1238 runcmd @git, qw(init -q);
1239 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1240 open H, "> .git/HEAD" or die $!;
1241 print H "ref: ".lref()."\n" or die $!;
1243 runcmd @git, qw(remote add), 'origin', access_giturl();
1244 if (check_for_git()) {
1245 progress "fetching existing git history";
1247 runcmd_ordryrun_local @git, qw(fetch origin);
1249 progress "starting new git history";
1251 fetch_from_archive() or no_such_package;
1252 runcmd @git, qw(reset --hard), lrref();
1253 printdone "ready for work in $dstdir";
1257 if (check_for_git()) {
1260 fetch_from_archive() or no_such_package();
1261 printdone "fetched into ".lrref();
1266 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1268 printdone "fetched to ".lrref()." and merged into HEAD";
1271 sub check_not_dirty () {
1272 return if $ignoredirty;
1273 my @cmd = (@git, qw(diff --quiet HEAD));
1274 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1275 $!=0; $?=0; system @cmd;
1276 return if !$! && !$?;
1277 if (!$! && $?==256) {
1278 fail "working tree is dirty (does not match HEAD)";
1284 sub commit_quilty_patch () {
1285 my $output = cmdoutput @git, qw(status --porcelain);
1287 foreach my $l (split /\n/, $output) {
1288 next unless $l =~ m/\S/;
1289 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1294 progress "nothing quilty to commit, ok.";
1297 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1298 my $m = "Commit Debian 3.0 (quilt) metadata";
1300 runcmd_ordryrun_local @git, qw(commit -m), $m;
1305 return 0 unless $format eq '3.0 (quilt)';
1306 progress "Format \`$format', urgh";
1308 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1314 sub push_parse_changelog ($) {
1317 my $clogp = Dpkg::Control::Hash->new();
1318 $clogp->load($clogpfn) or die;
1320 $package = getfield $clogp, 'Source';
1321 my $cversion = getfield $clogp, 'Version';
1322 my $tag = debiantag($cversion);
1323 runcmd @git, qw(check-ref-format), $tag;
1325 my $dscfn = dscfn($cversion);
1327 return ($clogp, $cversion, $tag, $dscfn);
1330 sub push_parse_dsc ($$$) {
1331 my ($dscfn,$dscfnwhat, $cversion) = @_;
1332 $dsc = parsecontrol($dscfn,$dscfnwhat);
1333 my $dversion = getfield $dsc, 'Version';
1334 my $dscpackage = getfield $dsc, 'Source';
1335 ($dscpackage eq $package && $dversion eq $cversion) or
1336 fail "$dscfn is for $dscpackage $dversion".
1337 " but debian/changelog is for $package $cversion";
1340 sub push_mktag ($$$$$$$) {
1341 my ($head,$clogp,$tag,
1343 $changesfile,$changesfilewhat,
1346 $dsc->{$ourdscfield[0]} = $head;
1347 $dsc->save("$dscfn.tmp") or die $!;
1349 my $changes = parsecontrol($changesfile,$changesfilewhat);
1350 foreach my $field (qw(Source Distribution Version)) {
1351 $changes->{$field} eq $clogp->{$field} or
1352 fail "changes field $field \`$changes->{$field}'".
1353 " does not match changelog \`$clogp->{$field}'";
1356 my $cversion = getfield $clogp, 'Version';
1357 my $clogsuite = getfield $clogp, 'Distribution';
1359 # We make the git tag by hand because (a) that makes it easier
1360 # to control the "tagger" (b) we can do remote signing
1361 my $authline = clogp_authline $clogp;
1362 open TO, '>', $tfn->('.tmp') or die $!;
1363 print TO <<END or die $!;
1369 $package release $cversion for $clogsuite ($csuite) [dgit]
1373 my $tagobjfn = $tfn->('.tmp');
1375 if (!defined $keyid) {
1376 $keyid = access_cfg('keyid','RETURN-UNDEF');
1378 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1379 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1380 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1381 push @sign_cmd, $tfn->('.tmp');
1382 runcmd_ordryrun @sign_cmd;
1384 $tagobjfn = $tfn->('.signed.tmp');
1385 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1386 $tfn->('.tmp'), $tfn->('.tmp.asc');
1393 sub sign_changes ($) {
1394 my ($changesfile) = @_;
1396 my @debsign_cmd = @debsign;
1397 push @debsign_cmd, "-k$keyid" if defined $keyid;
1398 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1399 push @debsign_cmd, $changesfile;
1400 runcmd_ordryrun @debsign_cmd;
1405 printdebug "actually entering push\n";
1408 access_giturl(); # check that success is vaguely likely
1410 my $clogpfn = ".git/dgit/changelog.822.tmp";
1411 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1413 responder_send_file('parsed-changelog', $clogpfn);
1415 my ($clogp, $cversion, $tag, $dscfn) =
1416 push_parse_changelog("$clogpfn");
1418 my $dscpath = "$buildproductsdir/$dscfn";
1420 fail "looked for .dsc $dscfn, but $!;".
1421 " maybe you forgot to build";
1423 responder_send_file('dsc', $dscpath);
1425 push_parse_dsc($dscpath, $dscfn, $cversion);
1427 my $format = getfield $dsc, 'Format';
1428 printdebug "format $format\n";
1429 if (madformat($format)) {
1430 commit_quilty_patch();
1434 progress "checking that $dscfn corresponds to HEAD";
1435 runcmd qw(dpkg-source -x --),
1436 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1437 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1438 changedir '../../../..';
1439 my $diffopt = $debug>0 ? '--exit-code' : '--quiet';
1440 my @diffcmd = (@git, qw(diff), $diffopt, $tree);
1441 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1443 my $r = system @diffcmd;
1446 fail "$dscfn specifies a different tree to your HEAD commit;".
1447 " perhaps you forgot to build".
1448 ($diffopt eq '--exit-code' ? "" :
1449 " (run with -D to see full diff output)");
1455 #do fast forward check and maybe fake merge
1456 # if (!is_fast_fwd(mainbranch
1457 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1458 # map { lref($_).":".rref($_) }
1460 my $head = rev_parse('HEAD');
1461 if (!$changesfile) {
1462 my $multi = "$buildproductsdir/".
1463 "${package}_".(stripepoch $cversion)."_multi.changes";
1464 if (stat "$multi") {
1465 $changesfile = $multi;
1467 $!==&ENOENT or die "$multi: $!";
1468 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1469 my @cs = glob "$buildproductsdir/$pat";
1470 fail "failed to find unique changes file".
1471 " (looked for $pat in $buildproductsdir, or $multi);".
1472 " perhaps you need to use dgit -C"
1474 ($changesfile) = @cs;
1477 $changesfile = "$buildproductsdir/$changesfile";
1480 responder_send_file('changes',$changesfile);
1481 responder_send_command("param head $head");
1482 responder_send_command("param csuite $csuite");
1484 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1487 if ($we_are_responder) {
1488 $tagobjfn = $tfn->('.signed.tmp');
1489 responder_receive_files('signed-tag', $tagobjfn);
1492 push_mktag($head,$clogp,$tag,
1494 $changesfile,$changesfile,
1498 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1499 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1500 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1501 runcmd_ordryrun @git, qw(tag -v --), $tag;
1503 if (!check_for_git()) {
1504 create_remote_git_repo();
1506 runcmd_ordryrun @git, qw(push),access_giturl(),
1507 "HEAD:".rrref(), "refs/tags/$tag";
1508 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1510 if ($we_are_responder) {
1511 my $dryrunsuffix = act_local() ? "" : ".tmp";
1512 responder_receive_files('signed-dsc-changes',
1513 "$dscpath$dryrunsuffix",
1514 "$changesfile$dryrunsuffix");
1517 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1519 progress "[new .dsc left in $dscpath.tmp]";
1521 sign_changes $changesfile;
1524 my $host = access_cfg('upload-host','RETURN-UNDEF');
1525 my @hostarg = defined($host) ? ($host,) : ();
1526 runcmd_ordryrun @dput, @hostarg, $changesfile;
1527 printdone "pushed and uploaded $cversion";
1529 responder_send_command("complete");
1535 badusage "-p is not allowed with clone; specify as argument instead"
1536 if defined $package;
1539 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1540 ($package,$isuite) = @ARGV;
1541 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1542 ($package,$dstdir) = @ARGV;
1543 } elsif (@ARGV==3) {
1544 ($package,$isuite,$dstdir) = @ARGV;
1546 badusage "incorrect arguments to dgit clone";
1548 $dstdir ||= "$package";
1551 fail "$dstdir already exists";
1552 } elsif ($! != &ENOENT) {
1557 if ($rmonerror && !$dryrun_level) {
1558 $cwd_remove= getcwd();
1560 return unless defined $cwd_remove;
1561 if (!chdir "$cwd_remove") {
1562 return if $!==&ENOENT;
1563 die "chdir $cwd_remove: $!";
1565 rmtree($dstdir) or die "remove $dstdir: $!\n";
1570 $cwd_remove = undef;
1573 sub branchsuite () {
1574 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1575 if ($branch =~ m#$lbranch_re#o) {
1582 sub fetchpullargs () {
1583 if (!defined $package) {
1584 my $sourcep = parsecontrol('debian/control','debian/control');
1585 $package = getfield $sourcep, 'Source';
1588 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1590 my $clogp = parsechangelog();
1591 $isuite = getfield $clogp, 'Distribution';
1593 canonicalise_suite();
1594 progress "fetching from suite $csuite";
1595 } elsif (@ARGV==1) {
1597 canonicalise_suite();
1599 badusage "incorrect arguments to dgit fetch or dgit pull";
1617 badusage "-p is not allowed with dgit push" if defined $package;
1619 my $clogp = parsechangelog();
1620 $package = getfield $clogp, 'Source';
1623 } elsif (@ARGV==1) {
1624 ($specsuite) = (@ARGV);
1626 badusage "incorrect arguments to dgit push";
1628 $isuite = getfield $clogp, 'Distribution';
1630 local ($package) = $existing_package; # this is a hack
1631 canonicalise_suite();
1633 if (defined $specsuite && $specsuite ne $isuite) {
1634 canonicalise_suite();
1635 $csuite eq $specsuite or
1636 fail "dgit push: changelog specifies $isuite ($csuite)".
1637 " but command line specifies $specsuite";
1639 if (check_for_git()) {
1642 if (fetch_from_archive()) {
1643 is_fast_fwd(lrref(), 'HEAD') or
1644 fail "dgit push: HEAD is not a descendant".
1645 " of the archive's version.\n".
1646 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1649 fail "package appears to be new in this suite;".
1650 " if this is intentional, use --new";
1655 #---------- remote commands' implementation ----------
1657 sub cmd_remote_push_responder {
1658 my ($nrargs) = shift @ARGV;
1659 my (@rargs) = @ARGV[0..$nrargs-1];
1660 @ARGV = @ARGV[$nrargs..$#ARGV];
1664 $we_are_responder = 1;
1666 open PI, "<&STDIN" or die $!;
1667 open STDIN, "/dev/null" or die $!;
1668 open PO, ">&STDOUT" or die $!;
1670 open STDOUT, ">&STDERR" or die $!;
1673 responder_send_command("dgit-remote-push-ready");
1685 printdebug "(killing remote child $i_child_pid)\n";
1686 kill 15, $i_child_pid;
1688 if (defined $i_tmp && !defined $initiator_tempdir) {
1690 eval { rmtree $i_tmp; };
1694 END { i_cleanup(); }
1697 my ($base,$selector,@args) = @_;
1698 $selector =~ s/\-/_/g;
1699 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1705 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1715 push @rdgit, @ropts;
1716 push @rdgit, qw(remote-push-responder), (scalar @rargs), @rargs;
1718 my @cmd = (@ssh, $host, shellquote @rdgit);
1719 printcmd \*DEBUG,$debugprefix."+",@cmd;
1721 if (defined $initiator_tempdir) {
1722 rmtree $initiator_tempdir;
1723 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1724 $i_tmp = $initiator_tempdir;
1728 $i_child_pid = open2(\*RO, \*RI, @cmd);
1730 initiator_expect { m/^dgit-remote-push-ready/ };
1732 my ($icmd,$iargs) = initiator_expect {
1733 m/^(\S+)(?: (.*))?$/;
1736 i_method "i_resp", $icmd, $iargs;
1740 sub i_resp_progress ($) {
1742 my $msg = protocol_read_bytes \*RO, $rhs;
1746 sub i_resp_complete {
1747 my $pid = $i_child_pid;
1748 $i_child_pid = undef; # prevents killing some other process with same pid
1749 printdebug "waiting for remote child $pid...\n";
1750 my $got = waitpid $pid, 0;
1751 die $! unless $got == $pid;
1752 die "remote child failed $?" if $?;
1755 printdebug "all done\n";
1759 sub i_resp_file ($) {
1761 my $localname = i_method "i_localname", $keyword;
1762 my $localpath = "$i_tmp/$localname";
1763 stat $localpath and badproto \*RO, "file $keyword ($localpath) twice";
1764 protocol_receive_file \*RO, $localpath;
1765 i_method "i_file", $keyword;
1770 sub i_resp_param ($) {
1771 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1777 sub i_resp_want ($) {
1779 die "$keyword ?" if $i_wanted{$keyword}++;
1780 my @localpaths = i_method "i_want", $keyword;
1781 printdebug "[[ $keyword @localpaths\n";
1782 foreach my $localpath (@localpaths) {
1783 protocol_send_file \*RI, $localpath;
1785 print RI "files-end\n" or die $!;
1788 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
1790 sub i_localname_parsed_changelog {
1791 return "remote-changelog.822";
1793 sub i_file_parsed_changelog {
1794 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
1795 push_parse_changelog "$i_tmp/remote-changelog.822";
1796 die if $i_dscfn =~ m#/|^\W#;
1799 sub i_localname_dsc {
1800 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1805 sub i_localname_changes {
1806 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1807 $i_changesfn = $i_dscfn;
1808 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
1809 return $i_changesfn;
1811 sub i_file_changes { }
1813 sub i_want_signed_tag {
1814 printdebug Dumper(\%i_param, $i_dscfn);
1815 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
1816 && defined $i_param{'csuite'}
1817 or badproto \*RO, "premature desire for signed-tag";
1818 my $head = $i_param{'head'};
1819 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
1821 die unless $i_param{'csuite'} =~ m/^$suite_re$/;
1823 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
1826 push_mktag $head, $i_clogp, $i_tag,
1828 $i_changesfn, 'remote changes',
1829 sub { "tag$_[0]"; };
1834 sub i_want_signed_dsc_changes {
1835 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
1836 sign_changes $i_changesfn;
1837 return ($i_dscfn, $i_changesfn);
1840 #---------- building etc. ----------
1846 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1848 sub build_maybe_quilt_fixup () {
1849 if (!open F, "debian/source/format") {
1850 die $! unless $!==&ENOENT;
1854 F->error and die $!;
1856 return unless madformat($_);
1859 my @cmd = (@git, qw(ls-files --exclude-standard -iodm));
1860 my $problems = cmdoutput @cmd;
1861 if (length $problems) {
1862 print STDERR "problematic files:\n";
1863 print STDERR " $_\n" foreach split /\n/, $problems;
1864 fail "Cannot do quilt fixup in tree containing ignored files. ".
1865 "Perhaps your package's clean target is broken, in which".
1866 " case -wg (which says to use git-clean -xdf) may help.";
1869 my $clogp = parsechangelog();
1870 my $version = getfield $clogp, 'Version';
1871 my $author = getfield $clogp, 'Maintainer';
1872 my $headref = rev_parse('HEAD');
1875 my $patchname = "auto-$version-$headref-$time";
1876 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1878 my $descfn = ".git/dgit/quilt-description.tmp";
1879 open O, '>', $descfn or die "$descfn: $!";
1881 $msg =~ s/^\s+$/ ./mg;
1882 print O <<END or die $!;
1883 Description: Automatically generated patch ($clogp->{Version})
1884 Last (up to) $ncommits git changes, FYI:
1894 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1895 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1896 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1897 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
1900 if (!open P, '>>', ".pc/applied-patches") {
1901 $!==&ENOENT or die $!;
1906 commit_quilty_patch();
1909 sub quilt_fixup_editor () {
1910 my $descfn = $ENV{$fakeeditorenv};
1911 my $editing = $ARGV[$#ARGV];
1912 open I1, '<', $descfn or die "$descfn: $!";
1913 open I2, '<', $editing or die "$editing: $!";
1914 unlink $editing or die "$editing: $!";
1915 open O, '>', $editing or die "$editing: $!";
1916 while (<I1>) { print O or die $!; } I1->error and die $!;
1919 $copying ||= m/^\-\-\- /;
1920 next unless $copying;
1923 I2->error and die $!;
1929 if ($cleanmode eq 'dpkg-source') {
1930 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
1931 } elsif ($cleanmode eq 'git') {
1932 runcmd_ordryrun_local @git, qw(clean -xdf);
1933 } elsif ($cleanmode eq 'none') {
1940 badusage "-p is not allowed when building" if defined $package;
1943 my $clogp = parsechangelog();
1944 $isuite = getfield $clogp, 'Distribution';
1945 $package = getfield $clogp, 'Source';
1946 $version = getfield $clogp, 'Version';
1947 build_maybe_quilt_fixup();
1950 sub changesopts () {
1951 my @opts =@changesopts[1..$#changesopts];
1952 if (!defined $changes_since_version) {
1953 my @vsns = archive_query('archive_query');
1954 my @quirk = access_quirk();
1955 if ($quirk[0] eq 'backports') {
1956 local $isuite = $quirk[2];
1958 canonicalise_suite();
1959 push @vsns, archive_query('archive_query');
1962 @vsns = map { $_->[0] } @vsns;
1963 @vsns = sort { -version_compare_string($a, $b) } @vsns;
1964 $changes_since_version = $vsns[0];
1965 progress "changelog will contain changes since $vsns[0]";
1967 $changes_since_version = '_';
1968 progress "package seems new, not specifying -v<version>";
1971 if ($changes_since_version ne '_') {
1972 unshift @opts, "-v$changes_since_version";
1979 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1980 printdone "build successful\n";
1986 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1987 "--git-builder=@dpkgbuildpackage");
1988 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1989 canonicalise_suite();
1990 push @cmd, "--git-debian-branch=".lbranch();
1992 push @cmd, changesopts();
1993 runcmd_ordryrun_local @cmd, @ARGV;
1994 printdone "build successful\n";
1999 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
2000 $dscfn = dscfn($version);
2001 if ($cleanmode eq 'dpkg-source') {
2002 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
2005 my $pwd = must_getcwd();
2006 my $leafdir = basename $pwd;
2008 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
2010 runcmd_ordryrun_local qw(sh -ec),
2011 'exec >$1; shift; exec "$@"','x',
2012 "../$sourcechanges",
2013 @dpkggenchanges, qw(-S), changesopts();
2017 sub cmd_build_source {
2018 badusage "build-source takes no additional arguments" if @ARGV;
2020 printdone "source built, results in $dscfn and $sourcechanges";
2026 my $pat = "${package}_".(stripepoch $version)."_*.changes";
2028 stat $dscfn or fail "$dscfn (in parent directory): $!";
2029 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
2030 foreach my $cf (glob $pat) {
2031 next if $cf eq $sourcechanges;
2032 unlink $cf or fail "remove $cf: $!";
2035 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
2036 my @changesfiles = glob $pat;
2037 @changesfiles = sort {
2038 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
2041 fail "wrong number of different changes files (@changesfiles)"
2042 unless @changesfiles;
2043 runcmd_ordryrun_local @mergechanges, @changesfiles;
2044 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
2046 stat $multichanges or fail "$multichanges: $!";
2048 printdone "build successful, results in $multichanges\n" or die $!;
2051 sub cmd_quilt_fixup {
2052 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
2053 my $clogp = parsechangelog();
2054 $version = getfield $clogp, 'Version';
2055 build_maybe_quilt_fixup();
2058 #---------- argument parsing and main program ----------
2061 print "dgit version $our_version\n" or die $!;
2068 if (defined $ENV{'DGIT_SSH'}) {
2069 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
2070 } elsif (defined $ENV{'GIT_SSH'}) {
2071 @ssh = ($ENV{'GIT_SSH'});
2075 last unless $ARGV[0] =~ m/^-/;
2079 if (m/^--dry-run$/) {
2082 } elsif (m/^--damp-run$/) {
2085 } elsif (m/^--no-sign$/) {
2088 } elsif (m/^--help$/) {
2090 } elsif (m/^--version$/) {
2092 } elsif (m/^--new$/) {
2095 } elsif (m/^--since-version=([^_]+|_)$/) {
2097 $changes_since_version = $1;
2098 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
2099 ($om = $opts_opt_map{$1}) &&
2103 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
2104 !$opts_opt_cmdonly{$1} &&
2105 ($om = $opts_opt_map{$1})) {
2108 } elsif (m/^--existing-package=(.*)/s) {
2110 $existing_package = $1;
2111 } elsif (m/^--initiator-tempdir=(.*)/s) {
2112 $initiator_tempdir = $1;
2113 $initiator_tempdir =~ m#^/# or
2114 badusage "--initiator-tempdir must be used specify an".
2115 " absolute, not relative, directory."
2116 } elsif (m/^--distro=(.*)/s) {
2119 } elsif (m/^--build-products-dir=(.*)/s) {
2121 $buildproductsdir = $1;
2122 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
2125 } elsif (m/^--clean=(.*)$/s) {
2126 badusage "unknown cleaning mode \`$1'";
2127 } elsif (m/^--ignore-dirty$/s) {
2130 } elsif (m/^--no-quilt-fixup$/s) {
2133 } elsif (m/^--no-rm-on-error$/s) {
2137 badusage "unknown long option \`$_'";
2144 } elsif (s/^-L/-/) {
2147 } elsif (s/^-h/-/) {
2149 } elsif (s/^-D/-/) {
2151 open DEBUG, ">&STDERR" or die $!;
2154 } elsif (s/^-N/-/) {
2157 } elsif (s/^-v([^_]+|_)$//s) {
2159 $changes_since_version = $1;
2162 push @changesopts, $_;
2164 } elsif (s/^-c(.*=.*)//s) {
2166 push @git, '-c', $1;
2167 } elsif (s/^-d(.*)//s) {
2170 } elsif (s/^-C(.*)//s) {
2173 if ($changesfile =~ s#^(.*)/##) {
2174 $buildproductsdir = $1;
2176 } elsif (s/^-k(.*)//s) {
2178 } elsif (s/^-wn//s) {
2180 $cleanmode = 'none';
2181 } elsif (s/^-wg//s) {
2184 } elsif (s/^-wd//s) {
2186 $cleanmode = 'dpkg-source';
2188 badusage "unknown short option \`$_'";
2195 if ($ENV{$fakeeditorenv}) {
2196 quilt_fixup_editor();
2200 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2201 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2202 if $dryrun_level == 1;
2204 print STDERR $helpmsg or die $!;
2207 my $cmd = shift @ARGV;
2209 { no strict qw(refs); &{"cmd_$cmd"}(); }