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 (@dput) = qw(dput);
61 our (@debsign) = qw(debsign);
63 our (@sbuild) = qw(sbuild -A);
65 our (@dgit) = qw(dgit);
66 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
67 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
68 our (@dpkggenchanges) = qw(dpkg-genchanges);
69 our (@mergechanges) = qw(mergechanges -f);
70 our (@changesopts) = ('');
72 our %opts_opt_map = ('dget' => \@dget,
74 'debsign' => \@debsign,
79 'dpkg-source' => \@dpkgsource,
80 'dpkg-buildpackage' => \@dpkgbuildpackage,
81 'dpkg-genchanges' => \@dpkggenchanges,
82 'ch' => \@changesopts,
83 'mergechanges' => \@mergechanges);
85 our %opts_opt_cmdonly = ('gpg' => 1);
90 open DEBUG, ">/dev/null" or die $!;
94 our $remotename = 'dgit';
95 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
96 our $branchprefix = 'dgit';
99 sub lbranch () { return "$branchprefix/$csuite"; }
100 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
101 sub lref () { return "refs/heads/".lbranch(); }
102 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
103 sub rrref () { return "refs/$branchprefix/$csuite"; }
118 return "${package}_".(stripepoch $vsn).".dsc";
122 our $debugprefix = '';
127 foreach my $f (@end) {
129 warn "$us: cleanup: $@" if length $@;
133 sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
136 die $us.($we_are_responder ? " (build host)" : "").": @_\n";
139 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
141 sub no_such_package () {
142 print STDERR "$us: package $package does not exist in suite $isuite\n";
148 return "+".rrref().":".lrref();
153 printdebug "CD $newdir\n";
154 chdir $newdir or die "chdir: $newdir: $!";
157 #---------- remote protocol support, common ----------
159 # remote push initiator/responder protocol:
160 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
162 # > file parsed-changelog
163 # [indicates that output of dpkg-parsechangelog follows]
164 # > data-block NBYTES
165 # > [NBYTES bytes of data (no newline)]
166 # [maybe some more blocks]
178 # [indicates that signed tag is wanted]
179 # < data-block NBYTES
180 # < [NBYTES bytes of data (no newline)]
181 # [maybe some more blocks]
185 # > want signed-dsc-changes
186 # < data-block NBYTES [transfer of signed dsc]
188 # < data-block NBYTES [transfer of signed changes]
196 fail "connection lost: $!" if $fh->error;
197 fail "protocol violation; $m not expected";
200 sub protocol_expect (&$) {
201 my ($match, $fh) = @_;
204 defined && chomp or badproto $fh, "eof";
212 badproto $fh, "\`$_'";
215 sub protocol_send_file ($$) {
216 my ($fh, $ourfn) = @_;
217 open PF, "<", $ourfn or die "$ourfn: $!";
220 my $got = read PF, $d, 65536;
221 die "$ourfn: $!" unless defined $got;
223 print $fh "data-block ".length($d)."\n" or die $!;
224 print $fh $d or die $!;
226 PF->error and die "$ourfn $!";
227 print $fh "data-end\n" or die $!;
231 sub protocol_read_bytes ($$) {
232 my ($fh, $nbytes) = @_;
233 $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
235 my $got = read $fh, $d, $nbytes;
236 $got==$nbytes or badproto $fh, "eof during data block";
240 sub protocol_receive_file ($$) {
241 my ($fh, $ourfn) = @_;
242 printdebug "() $ourfn\n";
243 open PF, ">", $ourfn or die "$ourfn: $!";
245 my ($y,$l) = protocol_expect {
246 m/^data-block (.*)$/ ? (1,$1) :
247 m/^data-end$/ ? (0,) :
251 my $d = protocol_read_bytes $fh, $l;
252 print PF $d or die $!;
257 #---------- remote protocol support, responder ----------
259 sub responder_send_command ($) {
261 return unless $we_are_responder;
262 # called even without $we_are_responder
263 printdebug ">> $command\n";
264 print PO $command, "\n" or die $!;
267 sub responder_send_file ($$) {
268 my ($keyword, $ourfn) = @_;
269 return unless $we_are_responder;
270 printdebug "]] $keyword $ourfn\n";
271 responder_send_command "file $keyword";
272 protocol_send_file \*PO, $ourfn;
275 sub responder_receive_files ($@) {
276 my ($keyword, @ourfns) = @_;
277 die unless $we_are_responder;
278 printdebug "[[ $keyword @ourfns\n";
279 responder_send_command "want $keyword";
280 foreach my $fn (@ourfns) {
281 protocol_receive_file \*PI, $fn;
284 protocol_expect { m/^files-end$/ } \*PI;
287 #---------- remote protocol support, initiator ----------
289 sub initiator_expect (&) {
291 protocol_expect { &$match } \*RO;
294 #---------- end remote code ----------
297 if ($we_are_responder) {
299 responder_send_command "progress ".length($m) or die $!;
300 print PO $m or die $!;
310 $ua = LWP::UserAgent->new();
314 progress "downloading $what...";
315 my $r = $ua->get(@_) or die $!;
316 return undef if $r->code == 404;
317 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
318 return $r->decoded_content();
321 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
328 if (m{[^-=_./0-9a-z]}i) {
335 return join ' ', @out;
340 my $intro = shift @_;
341 print $fh $intro," " or die $!;
342 print $fh shellquote @_ or die $!;
343 print $fh "\n" or die $!;
347 { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
349 fail "failed to fork/exec: $!";
350 } elsif (!($? & 0xff)) {
351 fail "subprocess failed with error exit status ".($?>>8);
353 fail "subprocess crashed (wait status $?)";
355 fail "subprocess produced invalid output";
360 printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
362 failedcmd @_ if system @_;
365 sub act_local () { return $dryrun_level <= 1; }
366 sub act_scary () { return !$dryrun_level; }
369 if (!$dryrun_level) {
370 progress "dgit ok: @_";
372 progress "would be ok: @_ (but dry run only)";
376 sub cmdoutput_errok {
377 die Dumper(\@_)." ?" if grep { !defined } @_;
378 printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
379 open P, "-|", @_ or die $!;
382 { local $/ = undef; $d = <P>; }
384 if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
387 printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
392 my $d = cmdoutput_errok @_;
393 defined $d or failedcmd @_;
398 printcmd(\*STDERR,$debugprefix."#",@_);
401 sub runcmd_ordryrun {
409 sub runcmd_ordryrun_local {
418 my ($first_shell, @cmd) = @_;
419 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
422 our $helpmsg = <<END;
424 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
425 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
426 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
427 dgit [dgit-opts] push [dgit-opts] [suite]
428 dgit [dgit-opts] rpush build-host:build-dir ...
429 important dgit options:
430 -k<keyid> sign tag and package with <keyid> instead of default
431 --dry-run -n do not change anything, but go through the motions
432 --damp-run -L like --dry-run but make local changes, without signing
433 --new -N allow introducing a new package
434 --debug -D increase debug level
435 -c<name>=<value> set git config option (used directly by dgit too)
438 our $later_warning_msg = <<END;
439 Perhaps the upload is stuck in incoming. Using the version from git.
443 print STDERR "$us: @_\n", $helpmsg or die $!;
448 @ARGV or badusage "too few arguments";
449 return scalar shift @ARGV;
453 print $helpmsg or die $!;
457 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
459 our %defcfg = ('dgit.default.distro' => 'debian',
460 'dgit.default.username' => '',
461 'dgit.default.archive-query-default-component' => 'main',
462 'dgit.default.ssh' => 'ssh',
463 'dgit-distro.debian.git-host' => 'git.debian.org',
464 'dgit-distro.debian.git-proto' => 'git+ssh://',
465 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
466 'dgit-distro.debian.git-check' => 'ssh-cmd',
467 'dgit-distro.debian.git-create' => 'ssh-cmd',
468 'dgit-distro.debian.sshpsql-host' => 'coccia.debian.org',
469 'dgit-distro.debian.sshpsql-dbname' => 'service=projectb',
470 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
471 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
472 'dgit-distro.debian.backports-quirk' => '(squeeze)-backports*',
473 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
474 'dgit-distro.test-dummy.ssh' => "$td/ssh",
475 'dgit-distro.test-dummy.username' => "alice",
476 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
477 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
478 'dgit-distro.test-dummy.git-url' => "$td/git",
479 'dgit-distro.test-dummy.git-host' => "git",
480 'dgit-distro.test-dummy.git-path' => "$td/git",
481 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
482 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
483 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
488 return undef if $c =~ /RETURN-UNDEF/;
489 my @cmd = (@git, qw(config --), $c);
492 local ($debug) = $debug-1;
493 $v = cmdoutput_errok @cmd;
500 my $dv = $defcfg{$c};
501 return $dv if defined $dv;
503 badcfg "need value for one of: @_\n".
504 "$us: distro or suite appears not to be (properly) supported";
507 sub access_basedistro () {
508 if (defined $idistro) {
509 return cfg("dgit-distro.basedistro.distro",
510 "dgit-suite.$isuite.distro",
511 'RETURN-UNDEF') // $idistro;
513 return cfg("dgit-suite.$isuite.distro",
514 "dgit.default.distro");
518 sub access_quirk () {
519 # returns (quirk name, distro to use instead, quirk-specific info)
520 my $basedistro = access_basedistro();
521 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
523 if (defined $backports_quirk) {
524 my $re = $backports_quirk;
525 $re =~ s/[^-0-9a-z_\%*()]/\\$&/ig;
527 $re =~ s/\%/([-0-9a-z_]+)/
528 or $re =~ m/[()]/ or badcfg "backports-quirk needs \% or ( )";
529 if ($isuite =~ m/^$re$/) {
530 return ('backports',"$basedistro-backports",$1);
533 return ('none',$basedistro);
536 sub access_distro () {
537 return (access_quirk())[1];
542 my $basedistro = access_basedistro();
543 my $distro = $idistro || access_distro();
544 my $value = cfg(map {
545 ("dgit-distro.$distro.$_",
546 "dgit-distro.$basedistro.$_",
552 sub string_to_ssh ($) {
554 if ($spec =~ m/\s/) {
555 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
561 sub access_cfg_ssh () {
562 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
563 if (!defined $gitssh) {
566 return string_to_ssh $gitssh;
570 sub access_someuserhost ($) {
572 my $user = access_cfg("$some-user",'username');
573 my $host = access_cfg("$some-host");
574 return length($user) ? "$user\@$host" : $host;
577 sub access_gituserhost () {
578 return access_someuserhost('git');
581 sub access_giturl () {
582 my $url = access_cfg('git-url','RETURN-UNDEF');
585 access_cfg('git-proto').
586 access_gituserhost().
587 access_cfg('git-path');
589 return "$url/$package.git";
592 sub parsecontrolfh ($$@) {
593 my ($fh, $desc, @opts) = @_;
594 my %opts = ('name' => $desc, @opts);
595 my $c = Dpkg::Control::Hash->new(%opts);
596 $c->parse($fh) or die "parsing of $desc failed";
601 my ($file, $desc) = @_;
602 my $fh = new IO::Handle;
603 open $fh, '<', $file or die "$file: $!";
604 my $c = parsecontrolfh($fh,$desc);
605 $fh->error and die $!;
611 my ($dctrl,$field) = @_;
612 my $v = $dctrl->{$field};
613 return $v if defined $v;
614 fail "missing field $field in ".$v->get_option('name');
618 my $c = Dpkg::Control::Hash->new();
619 my $p = new IO::Handle;
620 my @cmd = (qw(dpkg-parsechangelog), @_);
621 open $p, '-|', @cmd or die $!;
623 $?=0; $!=0; close $p or failedcmd @cmd;
627 sub git_get_ref ($) {
629 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
631 $?==256 or fail "git show-ref failed (status $?)";
632 printdebug "ref $refname= [show-ref exited 1]\n";
635 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
636 printdebug "ref $refname=$1\n";
639 printdebug "ref $refname= [no match]\n";
646 defined $d or fail "getcwd failed: $!";
652 sub archive_query ($) {
654 my $query = access_cfg('archive-query','RETURN-UNDEF');
655 if (!defined $query) {
656 my $distro = access_basedistro();
657 if ($distro eq 'debian') {
659 access_someuserhost('sshpsql').':'.
660 access_cfg('sshpsql-dbname');
662 $query = "madison:$distro";
665 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
668 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
671 sub pool_dsc_subpath ($$) {
672 my ($vsn,$component) = @_; # $package is implict arg
673 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
674 return "/pool/$component/$prefix/$package/".dscfn($vsn);
677 sub archive_query_madison ($$) {
678 my ($proto,$data) = @_;
679 die unless $proto eq 'madison';
680 $rmad{$package} ||= cmdoutput
681 qw(rmadison -asource),"-s$isuite","-u$data",$package;
682 my $rmad = $rmad{$package};
683 return madison_parse($rmad);
686 sub madison_parse ($) {
689 foreach my $l (split /\n/, $rmad) {
690 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
691 \s*( [^ \t|]+ )\s* \|
692 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
693 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
694 $1 eq $package or die "$rmad $package ?";
701 $component = access_cfg('archive-query-default-component');
703 $5 eq 'source' or die "$rmad ?";
704 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
706 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
709 sub canonicalise_suite_madison ($$) {
710 # madison canonicalises for us
711 my @r = archive_query_madison($_[0],$_[1]);
713 "unable to canonicalise suite using package $package".
714 " which does not appear to exist in suite $isuite;".
715 " --existing-package may help";
720 my ($data,$sql) = @_;
721 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
722 my ($userhost,$dbname) = ($`,$'); #';
724 my @cmd = (access_cfg_ssh, $userhost,
725 "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
726 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
727 open P, "-|", @cmd or die $!;
730 printdebug("$debugprefix>|$_|\n");
733 $!=0; $?=0; close P or failedcmd @cmd;
735 my $nrows = pop @rows;
736 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
737 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
738 @rows = map { [ split /\|/, $_ ] } @rows;
739 my $ncols = scalar @{ shift @rows };
740 die if grep { scalar @$_ != $ncols } @rows;
744 sub sql_injection_check {
745 foreach (@_) { die "$_ $& ?" if m/[']/; }
748 sub archive_query_sshpsql ($$) {
749 my ($proto,$data) = @_;
750 sql_injection_check $isuite, $package;
751 my @rows = sshpsql($data, <<END);
752 SELECT source.version, component.name, files.filename
754 JOIN src_associations ON source.id = src_associations.source
755 JOIN suite ON suite.id = src_associations.suite
756 JOIN dsc_files ON dsc_files.source = source.id
757 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
758 JOIN component ON component.id = files_archive_map.component_id
759 JOIN files ON files.id = dsc_files.file
760 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
761 AND source.source='$package'
762 AND files.filename LIKE '%.dsc';
764 @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
766 my ($vsn,$component,$filename) = @$_;
767 [ $vsn, "/pool/$component/$filename" ];
772 sub canonicalise_suite_sshpsql ($$) {
773 my ($proto,$data) = @_;
774 sql_injection_check $isuite;
775 my @rows = sshpsql($data, <<END);
776 SELECT suite.codename
777 FROM suite where suite_name='$isuite' or codename='$isuite';
779 @rows = map { $_->[0] } @rows;
780 fail "unknown suite $isuite" unless @rows;
781 die "ambiguous $isuite: @rows ?" if @rows>1;
785 sub canonicalise_suite_dummycat ($$) {
786 my ($proto,$data) = @_;
787 my $dpath = "$data/suite.$isuite";
788 if (!open C, "<", $dpath) {
789 $!==ENOENT or die "$dpath: $!";
790 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
794 chomp or die "$dpath: $!";
796 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
800 sub archive_query_dummycat ($$) {
801 my ($proto,$data) = @_;
802 canonicalise_suite();
803 my $dpath = "$data/package.$csuite.$package";
804 if (!open C, "<", $dpath) {
805 $!==ENOENT or die "$dpath: $!";
806 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
814 printdebug "dummycat query $csuite $package $dpath | $_\n";
815 my @row = split /\s+/, $_;
816 @row==2 or die "$dpath: $_ ?";
819 C->error and die "$dpath: $!";
821 return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
824 sub canonicalise_suite () {
825 return if defined $csuite;
826 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
827 $csuite = archive_query('canonicalise_suite');
828 if ($isuite ne $csuite) {
829 progress "canonical suite name for $isuite is $csuite";
833 sub get_archive_dsc () {
834 canonicalise_suite();
835 my @vsns = archive_query('archive_query');
836 foreach my $vinfo (@vsns) {
837 my ($vsn,$subpath) = @$vinfo;
838 $dscurl = access_cfg('mirror').$subpath;
839 $dscdata = url_get($dscurl);
841 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
844 my $dscfh = new IO::File \$dscdata, '<' or die $!;
845 printdebug Dumper($dscdata) if $debug>1;
846 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
847 printdebug Dumper($dsc) if $debug>1;
848 my $fmt = getfield $dsc, 'Format';
849 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
855 sub check_for_git () {
857 my $how = access_cfg('git-check');
858 if ($how eq 'ssh-cmd') {
860 (access_cfg_ssh, access_gituserhost(),
861 " set -e; cd ".access_cfg('git-path').";".
862 " if test -d $package.git; then echo 1; else echo 0; fi");
863 my $r= cmdoutput @cmd;
864 failedcmd @cmd unless $r =~ m/^[01]$/;
866 } elsif ($how eq 'true') {
869 badcfg "unknown git-check \`$how'";
873 sub create_remote_git_repo () {
874 my $how = access_cfg('git-create');
875 if ($how eq 'ssh-cmd') {
877 (access_cfg_ssh, access_gituserhost(),
878 "set -e; cd ".access_cfg('git-path').";".
879 " cp -a _template $package.git");
880 } elsif ($how eq 'true') {
883 badcfg "unknown git-create \`$how'";
887 our ($dsc_hash,$lastpush_hash);
889 our $ud = '.git/dgit/unpack';
897 sub mktree_in_ud_from_only_subdir () {
898 # changes into the subdir
901 $dirs[0] =~ m#^([^/]+)/\.$# or die;
904 fail "source package contains .git directory" if stat '.git';
905 die $! unless $!==&ENOENT;
906 runcmd qw(git init -q);
907 rmtree('.git/objects');
908 symlink '../../../../objects','.git/objects' or die $!;
909 runcmd @git, qw(add -Af);
910 my $tree = cmdoutput @git, qw(write-tree);
911 $tree =~ m/^\w+$/ or die "$tree ?";
915 sub dsc_files_info () {
916 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
917 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
918 ['Files', 'Digest::MD5', 'new()']) {
919 my ($fname, $module, $method) = @$csumi;
920 my $field = $dsc->{$fname};
921 next unless defined $field;
922 eval "use $module; 1;" or die $@;
924 foreach (split /\n/, $field) {
926 m/^(\w+) (\d+) (\S+)$/ or
927 fail "could not parse .dsc $fname line \`$_'";
928 my $digester = eval "$module"."->$method;" or die $@;
933 Digester => $digester,
938 fail "missing any supported Checksums-* or Files field in ".
939 $dsc->get_option('name');
943 map { $_->{Filename} } dsc_files_info();
946 sub is_orig_file ($) {
948 m/\.orig(?:-\w+)?\.tar\.\w+$/;
951 sub make_commit ($) {
953 return cmdoutput @git, qw(hash-object -w -t commit), $file;
956 sub clogp_authline ($) {
958 my $author = getfield $clogp, 'Maintainer';
959 $author =~ s#,.*##ms;
960 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
961 my $authline = "$author $date";
962 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
963 fail "unexpected commit author line format \`$authline'".
964 " (was generated from changelog Maintainer field)";
968 sub generate_commit_from_dsc () {
972 foreach my $f (dsc_files()) {
973 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
975 link "../../../$f", $f
979 runcmd @dget, qw(--), $dscurl;
980 foreach my $f (grep { is_orig_file($_) } @files) {
981 link $f, "../../../../$f"
985 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
986 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
987 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
988 my $authline = clogp_authline $clogp;
989 my $changes = getfield $clogp, 'Changes';
990 open C, ">../commit.tmp" or die $!;
991 print C <<END or die $!;
998 # imported from the archive
1001 my $outputhash = make_commit qw(../commit.tmp);
1002 my $cversion = getfield $clogp, 'Version';
1003 progress "synthesised git commit from .dsc $cversion";
1004 if ($lastpush_hash) {
1005 runcmd @git, qw(reset --hard), $lastpush_hash;
1006 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
1007 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
1008 my $oversion = getfield $oldclogp, 'Version';
1010 version_compare_string($oversion, $cversion);
1012 # git upload/ is earlier vsn than archive, use archive
1013 open C, ">../commit2.tmp" or die $!;
1014 print C <<END or die $!;
1016 parent $lastpush_hash
1021 Record $package ($cversion) in archive suite $csuite
1023 $outputhash = make_commit qw(../commit2.tmp);
1024 } elsif ($vcmp > 0) {
1025 print STDERR <<END or die $!;
1027 Version actually in archive: $cversion (older)
1028 Last allegedly pushed/uploaded: $oversion (newer or same)
1031 $outputhash = $lastpush_hash;
1033 $outputhash = $lastpush_hash;
1036 changedir '../../../..';
1037 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1038 'DGIT_ARCHIVE', $outputhash;
1039 cmdoutput @git, qw(log -n2), $outputhash;
1040 # ... gives git a chance to complain if our commit is malformed
1045 sub ensure_we_have_orig () {
1046 foreach my $fi (dsc_files_info()) {
1047 my $f = $fi->{Filename};
1048 next unless is_orig_file($f);
1049 if (open F, "<", "../$f") {
1050 $fi->{Digester}->reset();
1051 $fi->{Digester}->addfile(*F);
1052 F->error and die $!;
1053 my $got = $fi->{Digester}->hexdigest();
1054 $got eq $fi->{Hash} or
1055 fail "existing file $f has hash $got but .dsc".
1056 " demands hash $fi->{Hash}".
1057 " (perhaps you should delete this file?)";
1058 progress "using existing $f";
1061 die "$f $!" unless $!==&ENOENT;
1063 my $origurl = $dscurl;
1064 $origurl =~ s{/[^/]+$}{};
1066 die "$f ?" unless $f =~ m/^${package}_/;
1067 die "$f ?" if $f =~ m#/#;
1068 runcmd_ordryrun_local shell_cmd 'cd ..', @dget,'--',$origurl;
1073 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1076 sub is_fast_fwd ($$) {
1077 my ($ancestor,$child) = @_;
1078 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1079 my $mb = cmdoutput_errok @cmd;
1081 return rev_parse($mb) eq rev_parse($ancestor);
1083 $?==256 or failedcmd @cmd;
1088 sub git_fetch_us () {
1089 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1092 sub fetch_from_archive () {
1093 # ensures that lrref() is what is actually in the archive,
1094 # one way or another
1098 foreach my $field (@ourdscfield) {
1099 $dsc_hash = $dsc->{$field};
1100 last if defined $dsc_hash;
1102 if (defined $dsc_hash) {
1103 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1105 progress "last upload to archive specified git hash";
1107 progress "last upload to archive has NO git hash";
1110 progress "no version available from the archive";
1113 $lastpush_hash = git_get_ref(lrref());
1114 printdebug "previous reference hash=$lastpush_hash\n";
1116 if (defined $dsc_hash) {
1117 fail "missing remote git history even though dsc has hash -".
1118 " could not find ref ".lrref().
1119 " (should have been fetched from ".access_giturl()."#".rrref().")"
1120 unless $lastpush_hash;
1122 ensure_we_have_orig();
1123 if ($dsc_hash eq $lastpush_hash) {
1124 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1125 print STDERR <<END or die $!;
1127 Git commit in archive is behind the last version allegedly pushed/uploaded.
1128 Commit referred to by archive: $dsc_hash
1129 Last allegedly pushed/uploaded: $lastpush_hash
1132 $hash = $lastpush_hash;
1134 fail "archive's .dsc refers to ".$dsc_hash.
1135 " but this is an ancestor of ".$lastpush_hash;
1138 $hash = generate_commit_from_dsc();
1139 } elsif ($lastpush_hash) {
1140 # only in git, not in the archive yet
1141 $hash = $lastpush_hash;
1142 print STDERR <<END or die $!;
1144 Package not found in the archive, but has allegedly been pushed using dgit.
1148 printdebug "nothing found!\n";
1149 if (defined $skew_warning_vsn) {
1150 print STDERR <<END or die $!;
1152 Warning: relevant archive skew detected.
1153 Archive allegedly contains $skew_warning_vsn
1154 But we were not able to obtain any version from the archive or git.
1160 printdebug "current hash=$hash\n";
1161 if ($lastpush_hash) {
1162 fail "not fast forward on last upload branch!".
1163 " (archive's version left in DGIT_ARCHIVE)"
1164 unless is_fast_fwd($lastpush_hash, $hash);
1166 if (defined $skew_warning_vsn) {
1168 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1169 my $clogf = ".git/dgit/changelog.tmp";
1170 runcmd shell_cmd "exec >$clogf",
1171 @git, qw(cat-file blob), "$hash:debian/changelog";
1172 my $gotclogp = parsechangelog("-l$clogf");
1173 my $got_vsn = getfield $gotclogp, 'Version';
1174 printdebug "SKEW CHECK GOT $got_vsn\n";
1175 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
1176 print STDERR <<END or die $!;
1178 Warning: archive skew detected. Using the available version:
1179 Archive allegedly contains $skew_warning_vsn
1180 We were able to obtain only $got_vsn
1185 if ($lastpush_hash ne $hash) {
1186 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1190 dryrun_report @upd_cmd;
1198 canonicalise_suite();
1199 badusage "dry run makes no sense with clone" unless act_local();
1200 mkdir $dstdir or die "$dstdir $!";
1202 runcmd @git, qw(init -q);
1203 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1204 open H, "> .git/HEAD" or die $!;
1205 print H "ref: ".lref()."\n" or die $!;
1207 runcmd @git, qw(remote add), 'origin', access_giturl();
1208 if (check_for_git()) {
1209 progress "fetching existing git history";
1211 runcmd_ordryrun_local @git, qw(fetch origin);
1213 progress "starting new git history";
1215 fetch_from_archive() or no_such_package;
1216 runcmd @git, qw(reset --hard), lrref();
1217 printdone "ready for work in $dstdir";
1221 if (check_for_git()) {
1224 fetch_from_archive() or no_such_package();
1225 printdone "fetched into ".lrref();
1230 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1232 printdone "fetched to ".lrref()." and merged into HEAD";
1235 sub check_not_dirty () {
1236 return if $ignoredirty;
1237 my @cmd = (@git, qw(diff --quiet HEAD));
1238 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1239 $!=0; $?=0; system @cmd;
1240 return if !$! && !$?;
1241 if (!$! && $?==256) {
1242 fail "working tree is dirty (does not match HEAD)";
1248 sub commit_quilty_patch () {
1249 my $output = cmdoutput @git, qw(status --porcelain);
1251 foreach my $l (split /\n/, $output) {
1252 next unless $l =~ m/\S/;
1253 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1258 progress "nothing quilty to commit, ok.";
1261 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1262 my $m = "Commit Debian 3.0 (quilt) metadata";
1264 runcmd_ordryrun_local @git, qw(commit -m), $m;
1269 return 0 unless $format eq '3.0 (quilt)';
1270 progress "Format \`$format', urgh";
1272 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1278 sub push_parse_changelog ($) {
1281 my $clogp = Dpkg::Control::Hash->new();
1282 $clogp->load($clogpfn) or die;
1284 $package = getfield $clogp, 'Source';
1285 my $cversion = getfield $clogp, 'Version';
1286 my $tag = debiantag($cversion);
1287 runcmd @git, qw(check-ref-format), $tag;
1289 my $dscfn = dscfn($cversion);
1291 return ($clogp, $cversion, $tag, $dscfn);
1294 sub push_parse_dsc ($$$) {
1295 my ($dscfn,$dscfnwhat, $cversion) = @_;
1296 $dsc = parsecontrol($dscfn,$dscfnwhat);
1297 my $dversion = getfield $dsc, 'Version';
1298 my $dscpackage = getfield $dsc, 'Source';
1299 ($dscpackage eq $package && $dversion eq $cversion) or
1300 fail "$dscfn is for $dscpackage $dversion".
1301 " but debian/changelog is for $package $cversion";
1304 sub push_mktag ($$$$$$$) {
1305 my ($head,$clogp,$tag,
1307 $changesfile,$changesfilewhat,
1310 $dsc->{$ourdscfield[0]} = $head;
1311 $dsc->save("$dscfn.tmp") or die $!;
1313 my $changes = parsecontrol($changesfile,$changesfilewhat);
1314 foreach my $field (qw(Source Distribution Version)) {
1315 $changes->{$field} eq $clogp->{$field} or
1316 fail "changes field $field \`$changes->{$field}'".
1317 " does not match changelog \`$clogp->{$field}'";
1320 my $cversion = getfield $clogp, 'Version';
1321 my $clogsuite = getfield $clogp, 'Distribution';
1323 # We make the git tag by hand because (a) that makes it easier
1324 # to control the "tagger" (b) we can do remote signing
1325 my $authline = clogp_authline $clogp;
1326 open TO, '>', $tfn->('.tmp') or die $!;
1327 print TO <<END or die $!;
1333 $package release $cversion for $clogsuite ($csuite) [dgit]
1337 my $tagobjfn = $tfn->('.tmp');
1339 if (!defined $keyid) {
1340 $keyid = access_cfg('keyid','RETURN-UNDEF');
1342 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1343 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1344 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1345 push @sign_cmd, $tfn->('.tmp');
1346 runcmd_ordryrun @sign_cmd;
1348 $tagobjfn = $tfn->('.signed.tmp');
1349 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1350 $tfn->('.tmp'), $tfn->('.tmp.asc');
1357 sub sign_changes ($) {
1358 my ($changesfile) = @_;
1360 my @debsign_cmd = @debsign;
1361 push @debsign_cmd, "-k$keyid" if defined $keyid;
1362 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1363 push @debsign_cmd, $changesfile;
1364 runcmd_ordryrun @debsign_cmd;
1369 printdebug "actually entering push\n";
1372 my $clogpfn = ".git/dgit/changelog.822.tmp";
1373 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1375 responder_send_file('parsed-changelog', $clogpfn);
1377 my ($clogp, $cversion, $tag, $dscfn) =
1378 push_parse_changelog("$clogpfn");
1380 my $dscpath = "$buildproductsdir/$dscfn";
1382 fail "looked for .dsc $dscfn, but $!;".
1383 " maybe you forgot to build";
1385 responder_send_file('dsc', $dscpath);
1387 push_parse_dsc($dscpath, $dscfn, $cversion);
1389 my $format = getfield $dsc, 'Format';
1390 printdebug "format $format\n";
1391 if (madformat($format)) {
1392 commit_quilty_patch();
1396 progress "checking that $dscfn corresponds to HEAD";
1397 runcmd qw(dpkg-source -x --),
1398 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1399 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1400 changedir '../../../..';
1401 my $diffopt = $debug>0 ? '--exit-code' : '--quiet';
1402 my @diffcmd = (@git, qw(diff), $diffopt, $tree);
1403 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1405 my $r = system @diffcmd;
1408 fail "$dscfn specifies a different tree to your HEAD commit;".
1409 " perhaps you forgot to build".
1410 ($diffopt eq '--exit-code' ? "" :
1411 " (run with -D to see full diff output)");
1417 #do fast forward check and maybe fake merge
1418 # if (!is_fast_fwd(mainbranch
1419 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1420 # map { lref($_).":".rref($_) }
1422 my $head = rev_parse('HEAD');
1423 if (!$changesfile) {
1424 my $multi = "$buildproductsdir/".
1425 "${package}_".(stripepoch $cversion)."_multi.changes";
1426 if (stat "$multi") {
1427 $changesfile = $multi;
1429 $!==&ENOENT or die "$multi: $!";
1430 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1431 my @cs = glob "$buildproductsdir/$pat";
1432 fail "failed to find unique changes file".
1433 " (looked for $pat in $buildproductsdir, or $multi);".
1434 " perhaps you need to use dgit -C"
1436 ($changesfile) = @cs;
1439 $changesfile = "$buildproductsdir/$changesfile";
1442 responder_send_file('changes',$changesfile);
1443 responder_send_command("param head $head");
1444 responder_send_command("param csuite $csuite");
1446 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1449 if ($we_are_responder) {
1450 $tagobjfn = $tfn->('.signed.tmp');
1451 responder_receive_files('signed-tag', $tagobjfn);
1454 push_mktag($head,$clogp,$tag,
1456 $changesfile,$changesfile,
1460 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1461 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1462 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1463 runcmd_ordryrun @git, qw(tag -v --), $tag;
1465 if (!check_for_git()) {
1466 create_remote_git_repo();
1468 runcmd_ordryrun @git, qw(push),access_giturl(),
1469 "HEAD:".rrref(), "refs/tags/$tag";
1470 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1472 if ($we_are_responder) {
1473 my $dryrunsuffix = act_local() ? "" : ".tmp";
1474 responder_receive_files('signed-dsc-changes',
1475 "$dscpath$dryrunsuffix",
1476 "$changesfile$dryrunsuffix");
1479 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1481 progress "[new .dsc left in $dscpath.tmp]";
1483 sign_changes $changesfile;
1486 my $host = access_cfg('upload-host','RETURN-UNDEF');
1487 my @hostarg = defined($host) ? ($host,) : ();
1488 runcmd_ordryrun @dput, @hostarg, $changesfile;
1489 printdone "pushed and uploaded $cversion";
1491 responder_send_command("complete");
1497 badusage "-p is not allowed with clone; specify as argument instead"
1498 if defined $package;
1501 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1502 ($package,$isuite) = @ARGV;
1503 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1504 ($package,$dstdir) = @ARGV;
1505 } elsif (@ARGV==3) {
1506 ($package,$isuite,$dstdir) = @ARGV;
1508 badusage "incorrect arguments to dgit clone";
1510 $dstdir ||= "$package";
1513 if ($rmonerror && !$dryrun_level) {
1514 $cwd_remove= getcwd();
1516 return unless defined $cwd_remove;
1517 if (!chdir "$cwd_remove") {
1518 return if $!==&ENOENT;
1519 die "chdir $cwd_remove: $!";
1521 rmtree($dstdir) or die "remove $dstdir: $!\n";
1526 $cwd_remove = undef;
1529 sub branchsuite () {
1530 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1531 if ($branch =~ m#$lbranch_re#o) {
1538 sub fetchpullargs () {
1539 if (!defined $package) {
1540 my $sourcep = parsecontrol('debian/control','debian/control');
1541 $package = getfield $sourcep, 'Source';
1544 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1546 my $clogp = parsechangelog();
1547 $isuite = getfield $clogp, 'Distribution';
1549 canonicalise_suite();
1550 progress "fetching from suite $csuite";
1551 } elsif (@ARGV==1) {
1553 canonicalise_suite();
1555 badusage "incorrect arguments to dgit fetch or dgit pull";
1573 badusage "-p is not allowed with dgit push" if defined $package;
1575 my $clogp = parsechangelog();
1576 $package = getfield $clogp, 'Source';
1579 } elsif (@ARGV==1) {
1580 ($specsuite) = (@ARGV);
1582 badusage "incorrect arguments to dgit push";
1584 $isuite = getfield $clogp, 'Distribution';
1586 local ($package) = $existing_package; # this is a hack
1587 canonicalise_suite();
1589 if (defined $specsuite && $specsuite ne $isuite) {
1590 canonicalise_suite();
1591 $csuite eq $specsuite or
1592 fail "dgit push: changelog specifies $isuite ($csuite)".
1593 " but command line specifies $specsuite";
1595 if (check_for_git()) {
1598 if (fetch_from_archive()) {
1599 is_fast_fwd(lrref(), 'HEAD') or
1600 fail "dgit push: HEAD is not a descendant".
1601 " of the archive's version.\n".
1602 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1605 fail "package appears to be new in this suite;".
1606 " if this is intentional, use --new";
1611 #---------- remote commands' implementation ----------
1613 sub cmd_remote_push_responder {
1614 my ($nrargs) = shift @ARGV;
1615 my (@rargs) = @ARGV[0..$nrargs-1];
1616 @ARGV = @ARGV[$nrargs..$#ARGV];
1620 $we_are_responder = 1;
1622 open PI, "<&STDIN" or die $!;
1623 open STDIN, "/dev/null" or die $!;
1624 open PO, ">&STDOUT" or die $!;
1626 open STDOUT, ">&STDERR" or die $!;
1629 responder_send_command("dgit-remote-push-ready");
1641 printdebug "(killing remote child $i_child_pid)\n";
1642 kill 15, $i_child_pid;
1644 if (defined $i_tmp && !defined $initiator_tempdir) {
1646 eval { rmtree $i_tmp; };
1650 END { i_cleanup(); }
1653 my ($base,$selector,@args) = @_;
1654 $selector =~ s/\-/_/g;
1655 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1661 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1671 push @rdgit, @ropts;
1672 push @rdgit, qw(remote-push-responder), (scalar @rargs), @rargs;
1674 my @cmd = (@ssh, $host, shellquote @rdgit);
1675 printcmd \*DEBUG,$debugprefix."+",@cmd;
1677 if (defined $initiator_tempdir) {
1678 rmtree $initiator_tempdir;
1679 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1680 $i_tmp = $initiator_tempdir;
1684 $i_child_pid = open2(\*RO, \*RI, @cmd);
1686 initiator_expect { m/^dgit-remote-push-ready/ };
1688 my ($icmd,$iargs) = initiator_expect {
1689 m/^(\S+)(?: (.*))?$/;
1692 i_method "i_resp", $icmd, $iargs;
1696 sub i_resp_progress ($) {
1698 my $msg = protocol_read_bytes \*RO, $rhs;
1702 sub i_resp_complete {
1703 my $pid = $i_child_pid;
1704 $i_child_pid = undef; # prevents killing some other process with same pid
1705 printdebug "waiting for remote child $pid...\n";
1706 my $got = waitpid $pid, 0;
1707 die $! unless $got == $pid;
1708 die "remote child failed $?" if $?;
1711 printdebug "all done\n";
1715 sub i_resp_file ($) {
1717 my $localname = i_method "i_localname", $keyword;
1718 my $localpath = "$i_tmp/$localname";
1719 stat $localpath and badproto \*RO, "file $keyword ($localpath) twice";
1720 protocol_receive_file \*RO, $localpath;
1721 i_method "i_file", $keyword;
1726 sub i_resp_param ($) {
1727 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1733 sub i_resp_want ($) {
1735 die "$keyword ?" if $i_wanted{$keyword}++;
1736 my @localpaths = i_method "i_want", $keyword;
1737 printdebug "[[ $keyword @localpaths\n";
1738 foreach my $localpath (@localpaths) {
1739 protocol_send_file \*RI, $localpath;
1741 print RI "files-end\n" or die $!;
1744 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
1746 sub i_localname_parsed_changelog {
1747 return "remote-changelog.822";
1749 sub i_file_parsed_changelog {
1750 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
1751 push_parse_changelog "$i_tmp/remote-changelog.822";
1752 die if $i_dscfn =~ m#/|^\W#;
1755 sub i_localname_dsc {
1756 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1761 sub i_localname_changes {
1762 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1763 $i_changesfn = $i_dscfn;
1764 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
1765 return $i_changesfn;
1767 sub i_file_changes { }
1769 sub i_want_signed_tag {
1770 printdebug Dumper(\%i_param, $i_dscfn);
1771 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
1772 && defined $i_param{'csuite'}
1773 or badproto \*RO, "premature desire for signed-tag";
1774 my $head = $i_param{'head'};
1775 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
1777 die unless $i_param{'csuite'} =~ m/^$suite_re$/;
1779 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
1782 push_mktag $head, $i_clogp, $i_tag,
1784 $i_changesfn, 'remote changes',
1785 sub { "tag$_[0]"; };
1790 sub i_want_signed_dsc_changes {
1791 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
1792 sign_changes $i_changesfn;
1793 return ($i_dscfn, $i_changesfn);
1796 #---------- building etc. ----------
1802 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1804 sub build_maybe_quilt_fixup () {
1805 if (!open F, "debian/source/format") {
1806 die $! unless $!==&ENOENT;
1810 F->error and die $!;
1812 return unless madformat($_);
1815 my @cmd = (@git, qw(ls-files --exclude-standard -iodm));
1816 my $problems = cmdoutput @cmd;
1817 if (length $problems) {
1818 print STDERR "problematic files:\n";
1819 print STDERR " $_\n" foreach split /\n/, $problems;
1820 fail "Cannot do quilt fixup in tree containing ignored files. ".
1821 "Perhaps your package's clean target is broken, in which".
1822 " case -wg (which says to use git-clean -xdf) may help.";
1825 my $clogp = parsechangelog();
1826 my $version = getfield $clogp, 'Version';
1827 my $author = getfield $clogp, 'Maintainer';
1828 my $headref = rev_parse('HEAD');
1831 my $patchname = "auto-$version-$headref-$time";
1832 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1834 my $descfn = ".git/dgit/quilt-description.tmp";
1835 open O, '>', $descfn or die "$descfn: $!";
1837 $msg =~ s/^\s+$/ ./mg;
1838 print O <<END or die $!;
1839 Description: Automatically generated patch ($clogp->{Version})
1840 Last (up to) $ncommits git changes, FYI:
1850 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1851 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1852 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1853 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
1856 if (!open P, '>>', ".pc/applied-patches") {
1857 $!==&ENOENT or die $!;
1862 commit_quilty_patch();
1865 sub quilt_fixup_editor () {
1866 my $descfn = $ENV{$fakeeditorenv};
1867 my $editing = $ARGV[$#ARGV];
1868 open I1, '<', $descfn or die "$descfn: $!";
1869 open I2, '<', $editing or die "$editing: $!";
1870 unlink $editing or die "$editing: $!";
1871 open O, '>', $editing or die "$editing: $!";
1872 while (<I1>) { print O or die $!; } I1->error and die $!;
1875 $copying ||= m/^\-\-\- /;
1876 next unless $copying;
1879 I2->error and die $!;
1885 if ($cleanmode eq 'dpkg-source') {
1886 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
1887 } elsif ($cleanmode eq 'git') {
1888 runcmd_ordryrun_local @git, qw(clean -xdf);
1889 } elsif ($cleanmode eq 'none') {
1896 badusage "-p is not allowed when building" if defined $package;
1899 my $clogp = parsechangelog();
1900 $isuite = getfield $clogp, 'Distribution';
1901 $package = getfield $clogp, 'Source';
1902 $version = getfield $clogp, 'Version';
1903 build_maybe_quilt_fixup();
1906 sub changesopts () {
1907 my @opts =@changesopts[1..$#changesopts];
1908 if (!defined $changes_since_version) {
1909 my @vsns = archive_query('archive_query');
1910 my @quirk = access_quirk();
1911 if ($quirk[0] eq 'backports') {
1912 local $isuite = $quirk[2];
1914 canonicalise_suite();
1915 push @vsns, archive_query('archive_query');
1918 @vsns = map { $_->[0] } @vsns;
1919 @vsns = sort { -version_compare_string($a, $b) } @vsns;
1920 $changes_since_version = $vsns[0];
1921 progress "changelog will contain changes since $vsns[0]";
1923 $changes_since_version = '_';
1924 progress "package seems new, not specifying -v<version>";
1927 if ($changes_since_version ne '_') {
1928 unshift @opts, "-v$changes_since_version";
1935 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1936 printdone "build successful\n";
1942 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1943 "--git-builder=@dpkgbuildpackage");
1944 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1945 canonicalise_suite();
1946 push @cmd, "--git-debian-branch=".lbranch();
1948 push @cmd, changesopts();
1949 runcmd_ordryrun_local @cmd, @ARGV;
1950 printdone "build successful\n";
1955 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1956 $dscfn = dscfn($version);
1957 if ($cleanmode eq 'dpkg-source') {
1958 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
1961 my $pwd = must_getcwd();
1962 my $leafdir = basename $pwd;
1964 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
1966 runcmd_ordryrun_local qw(sh -ec),
1967 'exec >$1; shift; exec "$@"','x',
1968 "../$sourcechanges",
1969 @dpkggenchanges, qw(-S), changesopts();
1973 sub cmd_build_source {
1974 badusage "build-source takes no additional arguments" if @ARGV;
1976 printdone "source built, results in $dscfn and $sourcechanges";
1982 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1984 stat $dscfn or fail "$dscfn (in parent directory): $!";
1985 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1986 foreach my $cf (glob $pat) {
1987 next if $cf eq $sourcechanges;
1988 unlink $cf or fail "remove $cf: $!";
1991 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1992 my @changesfiles = glob $pat;
1993 @changesfiles = sort {
1994 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
1997 fail "wrong number of different changes files (@changesfiles)"
1998 unless @changesfiles;
1999 runcmd_ordryrun_local @mergechanges, @changesfiles;
2000 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
2002 stat $multichanges or fail "$multichanges: $!";
2004 printdone "build successful, results in $multichanges\n" or die $!;
2007 sub cmd_quilt_fixup {
2008 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
2009 my $clogp = parsechangelog();
2010 $version = getfield $clogp, 'Version';
2011 build_maybe_quilt_fixup();
2014 #---------- argument parsing and main program ----------
2017 print "dgit version $our_version\n" or die $!;
2024 if (defined $ENV{'DGIT_SSH'}) {
2025 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
2026 } elsif (defined $ENV{'GIT_SSH'}) {
2027 @ssh = ($ENV{'GIT_SSH'});
2031 last unless $ARGV[0] =~ m/^-/;
2035 if (m/^--dry-run$/) {
2038 } elsif (m/^--damp-run$/) {
2041 } elsif (m/^--no-sign$/) {
2044 } elsif (m/^--help$/) {
2046 } elsif (m/^--version$/) {
2048 } elsif (m/^--new$/) {
2051 } elsif (m/^--since-version=([^_]+|_)$/) {
2053 $changes_since_version = $1;
2054 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
2055 ($om = $opts_opt_map{$1}) &&
2059 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
2060 !$opts_opt_cmdonly{$1} &&
2061 ($om = $opts_opt_map{$1})) {
2064 } elsif (m/^--existing-package=(.*)/s) {
2066 $existing_package = $1;
2067 } elsif (m/^--initiator-tempdir=(.*)/s) {
2068 $initiator_tempdir = $1;
2069 $initiator_tempdir =~ m#^/# or
2070 badusage "--initiator-tempdir must be used specify an".
2071 " absolute, not relative, directory."
2072 } elsif (m/^--distro=(.*)/s) {
2075 } elsif (m/^--build-products-dir=(.*)/s) {
2077 $buildproductsdir = $1;
2078 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
2081 } elsif (m/^--clean=(.*)$/s) {
2082 badusage "unknown cleaning mode \`$1'";
2083 } elsif (m/^--ignore-dirty$/s) {
2086 } elsif (m/^--no-quilt-fixup$/s) {
2089 } elsif (m/^--no-rm-on-error$/s) {
2093 badusage "unknown long option \`$_'";
2100 } elsif (s/^-L/-/) {
2103 } elsif (s/^-h/-/) {
2105 } elsif (s/^-D/-/) {
2107 open DEBUG, ">&STDERR" or die $!;
2110 } elsif (s/^-N/-/) {
2113 } elsif (s/^-v([^_]+|_)$//s) {
2115 $changes_since_version = $1;
2118 push @changesopts, $_;
2120 } elsif (s/^-c(.*=.*)//s) {
2122 push @git, '-c', $1;
2123 } elsif (s/^-d(.*)//s) {
2126 } elsif (s/^-C(.*)//s) {
2129 if ($changesfile =~ s#^(.*)/##) {
2130 $buildproductsdir = $1;
2132 } elsif (s/^-k(.*)//s) {
2134 } elsif (s/^-wn//s) {
2136 $cleanmode = 'none';
2137 } elsif (s/^-wg//s) {
2140 } elsif (s/^-wd//s) {
2142 $cleanmode = 'dpkg-source';
2144 badusage "unknown short option \`$_'";
2151 if ($ENV{$fakeeditorenv}) {
2152 quilt_fixup_editor();
2155 delete $ENV{'DGET_UNPACK'};
2158 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2159 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2160 if $dryrun_level == 1;
2162 print STDERR $helpmsg or die $!;
2165 my $cmd = shift @ARGV;
2167 { no strict qw(refs); &{"cmd_$cmd"}(); }