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);
38 our $our_version = 'UNRELEASED'; ###substituted###
40 our $rpushprotovsn = 2;
42 our $isuite = 'unstable';
48 our $dryrun_level = 0;
50 our $buildproductsdir = '..';
55 our $existing_package = 'dpkg';
56 our $cleanmode = 'dpkg-source';
57 our $changes_since_version;
59 our $quilt_modes_re = 'linear|smash|auto|nofix|nocheck';
60 our $we_are_responder;
61 our $initiator_tempdir;
63 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
65 our $suite_re = '[-+.0-9a-z]+';
68 our (@dget) = qw(dget);
69 our (@curl) = qw(curl -f);
70 our (@dput) = qw(dput);
71 our (@debsign) = qw(debsign);
73 our (@sbuild) = qw(sbuild -A);
75 our (@dgit) = qw(dgit);
76 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
77 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
78 our (@dpkggenchanges) = qw(dpkg-genchanges);
79 our (@mergechanges) = qw(mergechanges -f);
80 our (@changesopts) = ('');
82 our %opts_opt_map = ('dget' => \@dget, # accept for compatibility
85 'debsign' => \@debsign,
90 'dpkg-source' => \@dpkgsource,
91 'dpkg-buildpackage' => \@dpkgbuildpackage,
92 'dpkg-genchanges' => \@dpkggenchanges,
93 'ch' => \@changesopts,
94 'mergechanges' => \@mergechanges);
96 our %opts_opt_cmdonly = ('gpg' => 1);
101 open DEBUG, ">/dev/null" or die $!;
105 our $remotename = 'dgit';
106 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
110 sub lbranch () { return "$branchprefix/$csuite"; }
111 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
112 sub lref () { return "refs/heads/".lbranch(); }
113 sub lrref () { return "refs/remotes/$remotename/".server_branch($csuite); }
114 sub rrref () { return server_ref($csuite); }
124 return "${package}_".(stripepoch $vsn).$sfx
129 return srcfn($vsn,".dsc");
133 our $debugprefix = '';
138 foreach my $f (@end) {
140 warn "$us: cleanup: $@" if length $@;
144 our @signames = split / /, $Config{sig_name};
146 sub waitstatusmsg () {
148 return "terminated, reporting successful completion";
149 } elsif (!($? & 255)) {
150 return "failed with error exit status ".WEXITSTATUS($?);
151 } elsif (WIFSIGNALED($?)) {
152 my $signum=WTERMSIG($?);
153 return "died due to fatal signal ".
154 ($signames[$signum] // "number $signum").
155 ($? & 128 ? " (core dumped)" : ""); # POSIX(3pm) has no WCOREDUMP
157 return "failed with unknown wait status ".$?;
161 sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
165 my $prefix = $us.($we_are_responder ? " (build host)" : "").": ";
166 $s =~ s/^/$prefix/gm;
170 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
172 sub no_such_package () {
173 print STDERR "$us: package $package does not exist in suite $isuite\n";
179 return "+".rrref().":".lrref();
184 printdebug "CD $newdir\n";
185 chdir $newdir or die "chdir: $newdir: $!";
188 sub deliberately ($) {
189 return !!grep { $_[0] eq $_ } @deliberatelies;
192 #---------- remote protocol support, common ----------
194 # remote push initiator/responder protocol:
195 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
197 # > file parsed-changelog
198 # [indicates that output of dpkg-parsechangelog follows]
199 # > data-block NBYTES
200 # > [NBYTES bytes of data (no newline)]
201 # [maybe some more blocks]
213 # [indicates that signed tag is wanted]
214 # < data-block NBYTES
215 # < [NBYTES bytes of data (no newline)]
216 # [maybe some more blocks]
220 # > want signed-dsc-changes
221 # < data-block NBYTES [transfer of signed dsc]
223 # < data-block NBYTES [transfer of signed changes]
231 sub i_child_report () {
232 # Sees if our child has died, and reap it if so. Returns a string
233 # describing how it died if it failed, or undef otherwise.
234 return undef unless $i_child_pid;
235 my $got = waitpid $i_child_pid, WNOHANG;
236 return undef if $got <= 0;
237 die unless $got == $i_child_pid;
238 $i_child_pid = undef;
239 return undef unless $?;
240 return "build host child ".waitstatusmsg();
245 fail "connection lost: $!" if $fh->error;
246 fail "protocol violation; $m not expected";
249 sub badproto_badread ($$) {
251 fail "connection lost: $!" if $!;
252 my $report = i_child_report();
253 fail $report if defined $report;
254 badproto $fh, "eof (reading $wh)";
257 sub protocol_expect (&$) {
258 my ($match, $fh) = @_;
261 defined && chomp or badproto_badread $fh, "protocol message";
269 badproto $fh, "\`$_'";
272 sub protocol_send_file ($$) {
273 my ($fh, $ourfn) = @_;
274 open PF, "<", $ourfn or die "$ourfn: $!";
277 my $got = read PF, $d, 65536;
278 die "$ourfn: $!" unless defined $got;
280 print $fh "data-block ".length($d)."\n" or die $!;
281 print $fh $d or die $!;
283 PF->error and die "$ourfn $!";
284 print $fh "data-end\n" or die $!;
288 sub protocol_read_bytes ($$) {
289 my ($fh, $nbytes) = @_;
290 $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
292 my $got = read $fh, $d, $nbytes;
293 $got==$nbytes or badproto_badread $fh, "data block";
297 sub protocol_receive_file ($$) {
298 my ($fh, $ourfn) = @_;
299 printdebug "() $ourfn\n";
300 open PF, ">", $ourfn or die "$ourfn: $!";
302 my ($y,$l) = protocol_expect {
303 m/^data-block (.*)$/ ? (1,$1) :
304 m/^data-end$/ ? (0,) :
308 my $d = protocol_read_bytes $fh, $l;
309 print PF $d or die $!;
314 #---------- remote protocol support, responder ----------
316 sub responder_send_command ($) {
318 return unless $we_are_responder;
319 # called even without $we_are_responder
320 printdebug ">> $command\n";
321 print PO $command, "\n" or die $!;
324 sub responder_send_file ($$) {
325 my ($keyword, $ourfn) = @_;
326 return unless $we_are_responder;
327 printdebug "]] $keyword $ourfn\n";
328 responder_send_command "file $keyword";
329 protocol_send_file \*PO, $ourfn;
332 sub responder_receive_files ($@) {
333 my ($keyword, @ourfns) = @_;
334 die unless $we_are_responder;
335 printdebug "[[ $keyword @ourfns\n";
336 responder_send_command "want $keyword";
337 foreach my $fn (@ourfns) {
338 protocol_receive_file \*PI, $fn;
341 protocol_expect { m/^files-end$/ } \*PI;
344 #---------- remote protocol support, initiator ----------
346 sub initiator_expect (&) {
348 protocol_expect { &$match } \*RO;
351 #---------- end remote code ----------
354 if ($we_are_responder) {
356 responder_send_command "progress ".length($m) or die $!;
357 print PO $m or die $!;
367 $ua = LWP::UserAgent->new();
371 progress "downloading $what...";
372 my $r = $ua->get(@_) or die $!;
373 return undef if $r->code == 404;
374 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
375 return $r->decoded_content(charset => 'none');
378 our ($dscdata,$dscurl,$dsc,$dsc_checked,$skew_warning_vsn);
385 if (m{[^-=_./0-9a-z]}i) {
392 return join ' ', @out;
397 my $intro = shift @_;
398 print $fh $intro," " or die $!;
399 print $fh shellquote @_ or die $!;
400 print $fh "\n" or die $!;
404 { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
406 fail "failed to fork/exec: $!";
408 fail "subprocess ".waitstatusmsg();
410 fail "subprocess produced invalid output";
415 printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
417 failedcmd @_ if system @_;
420 sub act_local () { return $dryrun_level <= 1; }
421 sub act_scary () { return !$dryrun_level; }
424 if (!$dryrun_level) {
425 progress "dgit ok: @_";
427 progress "would be ok: @_ (but dry run only)";
431 sub cmdoutput_errok {
432 die Dumper(\@_)." ?" if grep { !defined } @_;
433 printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
434 open P, "-|", @_ or die $!;
437 { local $/ = undef; $d = <P>; }
439 if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
442 printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
447 my $d = cmdoutput_errok @_;
448 defined $d or failedcmd @_;
453 printcmd(\*STDERR,$debugprefix."#",@_);
456 sub runcmd_ordryrun {
464 sub runcmd_ordryrun_local {
473 my ($first_shell, @cmd) = @_;
474 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
477 our $helpmsg = <<END;
479 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
480 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
481 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
482 dgit [dgit-opts] push [dgit-opts] [suite]
483 dgit [dgit-opts] rpush build-host:build-dir ...
484 important dgit options:
485 -k<keyid> sign tag and package with <keyid> instead of default
486 --dry-run -n do not change anything, but go through the motions
487 --damp-run -L like --dry-run but make local changes, without signing
488 --new -N allow introducing a new package
489 --debug -D increase debug level
490 -c<name>=<value> set git config option (used directly by dgit too)
493 our $later_warning_msg = <<END;
494 Perhaps the upload is stuck in incoming. Using the version from git.
498 print STDERR "$us: @_\n", $helpmsg or die $!;
503 @ARGV or badusage "too few arguments";
504 return scalar shift @ARGV;
508 print $helpmsg or die $!;
512 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
514 our %defcfg = ('dgit.default.distro' => 'debian',
515 'dgit.default.username' => '',
516 'dgit.default.archive-query-default-component' => 'main',
517 'dgit.default.ssh' => 'ssh',
518 'dgit.default.archive-query' => 'madison:',
519 'dgit.default.sshpsql-dbname' => 'service=projectb',
520 'dgit-distro.debian.archive-query' => 'sshpsql:',
521 'dgit-distro.debian.git-host' => 'dgit-git.debian.net',
522 'dgit-distro.debian.git-user-force' => 'dgit',
523 'dgit-distro.debian.git-proto' => 'git+ssh://',
524 'dgit-distro.debian.git-path' => '/dgit/debian/repos',
525 'dgit-distro.debian.git-check' => 'ssh-cmd',
526 'dgit-distro.debian.archive-query-url', 'https://api.ftp-master.debian.org/',
527 'dgit-distro.debian.archive-query-tls-key',
528 '/etc/ssl/certs/%HOST%.pem:/etc/dgit/%HOST%.pem',
529 'dgit-distro.debian.diverts.alioth' => '/alioth',
530 'dgit-distro.debian/alioth.git-host' => 'git.debian.org',
531 'dgit-distro.debian/alioth.git-user-force' => '',
532 'dgit-distro.debian/alioth.git-proto' => 'git+ssh://',
533 'dgit-distro.debian/alioth.git-path' => '/git/dgit-repos/repos',
534 'dgit-distro.debian/alioth.git-create' => 'ssh-cmd',
535 'dgit-distro.debian.sshpsql-host' => 'mirror.ftp-master.debian.org',
536 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
537 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
538 'dgit-distro.debian.backports-quirk' => '(squeeze)-backports*',
539 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
540 'dgit-distro.ubuntu.git-check' => 'false',
541 'dgit-distro.ubuntu.mirror' => 'http://archive.ubuntu.com/ubuntu',
542 'dgit-distro.test-dummy.ssh' => "$td/ssh",
543 'dgit-distro.test-dummy.username' => "alice",
544 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
545 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
546 'dgit-distro.test-dummy.git-url' => "$td/git",
547 'dgit-distro.test-dummy.git-host' => "git",
548 'dgit-distro.test-dummy.git-path' => "$td/git",
549 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
550 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
551 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
556 return undef if $c =~ /RETURN-UNDEF/;
557 my @cmd = (@git, qw(config --), $c);
560 local ($debug) = $debug-1;
561 $v = cmdoutput_errok @cmd;
568 my $dv = $defcfg{$c};
569 return $dv if defined $dv;
571 badcfg "need value for one of: @_\n".
572 "$us: distro or suite appears not to be (properly) supported";
575 sub access_basedistro () {
576 if (defined $idistro) {
579 return cfg("dgit-suite.$isuite.distro",
580 "dgit.default.distro");
584 sub access_quirk () {
585 # returns (quirk name, distro to use instead or undef, quirk-specific info)
586 my $basedistro = access_basedistro();
587 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
589 if (defined $backports_quirk) {
590 my $re = $backports_quirk;
591 $re =~ s/[^-0-9a-z_\%*()]/\\$&/ig;
593 $re =~ s/\%/([-0-9a-z_]+)/
594 or $re =~ m/[()]/ or badcfg "backports-quirk needs \% or ( )";
595 if ($isuite =~ m/^$re$/) {
596 return ('backports',"$basedistro-backports",$1);
599 return ('none',undef);
602 sub access_distros () {
603 # Returns list of distros to try, in order
606 # 0. `instead of' distro name(s) we have been pointed to
607 # 1. the access_quirk distro, if any
608 # 2a. the user's specified distro, or failing that } basedistro
609 # 2b. the distro calculated from the suite }
610 my @l = access_basedistro();
612 my (undef,$quirkdistro) = access_quirk();
613 unshift @l, $quirkdistro;
614 unshift @l, $instead_distro;
615 return grep { defined } @l;
621 # The nesting of these loops determines the search order. We put
622 # the key loop on the outside so that we search all the distros
623 # for each key, before going on to the next key. That means that
624 # if access_cfg is called with a more specific, and then a less
625 # specific, key, an earlier distro can override the less specific
626 # without necessarily overriding any more specific keys. (If the
627 # distro wants to override the more specific keys it can simply do
628 # so; whereas if we did the loop the other way around, it would be
629 # impossible to for an earlier distro to override a less specific
630 # key but not the more specific ones without restating the unknown
631 # values of the more specific keys.
634 # We have to deal with RETURN-UNDEF specially, so that we don't
635 # terminate the search prematurely.
637 if (m/RETURN-UNDEF/) { push @rundef, $_; last; }
640 foreach my $d (access_distros()) {
641 push @cfgs, map { "dgit-distro.$d.$_" } @realkeys;
643 push @cfgs, map { "dgit.default.$_" } @realkeys;
645 my $value = cfg(@cfgs);
649 sub string_to_ssh ($) {
651 if ($spec =~ m/\s/) {
652 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
658 sub access_cfg_ssh () {
659 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
660 if (!defined $gitssh) {
663 return string_to_ssh $gitssh;
667 sub access_runeinfo ($) {
669 return ": dgit ".access_basedistro()." $info ;";
672 sub access_someuserhost ($) {
674 my $user = access_cfg("$some-user-force", 'RETURN-UNDEF');
675 defined($user) && length($user) or
676 $user = access_cfg("$some-user",'username');
677 my $host = access_cfg("$some-host");
678 return length($user) ? "$user\@$host" : $host;
681 sub access_gituserhost () {
682 return access_someuserhost('git');
685 sub access_giturl (;$) {
687 my $url = access_cfg('git-url','RETURN-UNDEF');
689 my $proto = access_cfg('git-proto', 'RETURN-UNDEF');
690 return undef unless defined $proto;
693 access_gituserhost().
694 access_cfg('git-path');
696 return "$url/$package.git";
699 sub parsecontrolfh ($$;$) {
700 my ($fh, $desc, $allowsigned) = @_;
701 our $dpkgcontrolhash_noissigned;
704 my %opts = ('name' => $desc);
705 $opts{allow_pgp}= $allowsigned || !$dpkgcontrolhash_noissigned;
706 $c = Dpkg::Control::Hash->new(%opts);
707 $c->parse($fh,$desc) or die "parsing of $desc failed";
708 last if $allowsigned;
709 last if $dpkgcontrolhash_noissigned;
710 my $issigned= $c->get_option('is_pgp_signed');
711 if (!defined $issigned) {
712 $dpkgcontrolhash_noissigned= 1;
713 seek $fh, 0,0 or die "seek $desc: $!";
714 } elsif ($issigned) {
715 fail "control file $desc is (already) PGP-signed. ".
716 " Note that dgit push needs to modify the .dsc and then".
717 " do the signature itself";
726 my ($file, $desc) = @_;
727 my $fh = new IO::Handle;
728 open $fh, '<', $file or die "$file: $!";
729 my $c = parsecontrolfh($fh,$desc);
730 $fh->error and die $!;
736 my ($dctrl,$field) = @_;
737 my $v = $dctrl->{$field};
738 return $v if defined $v;
739 fail "missing field $field in ".$v->get_option('name');
743 my $c = Dpkg::Control::Hash->new();
744 my $p = new IO::Handle;
745 my @cmd = (qw(dpkg-parsechangelog), @_);
746 open $p, '-|', @cmd or die $!;
748 $?=0; $!=0; close $p or failedcmd @cmd;
752 sub git_get_ref ($) {
754 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
756 $?==256 or fail "git show-ref failed (status $?)";
757 printdebug "ref $refname= [show-ref exited 1]\n";
760 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
761 printdebug "ref $refname=$1\n";
764 printdebug "ref $refname= [no match]\n";
771 defined $d or fail "getcwd failed: $!";
775 sub archive_api_query_cmd ($) {
777 my @cmd = qw(curl -sS);
778 my $url = access_cfg('archive-query-url');
779 if ($url =~ m#^https://([-.0-9a-z]+)/#) {
781 my $keys = access_cfg('archive-query-tls-key','RETURN-UNDEF');
782 foreach my $key (split /\:/, $keys) {
783 $key =~ s/\%HOST\%/$host/g;
785 fail "for $url: stat $key: $!" unless $!==ENOENT;
788 push @cmd, "--ca-certificate=$key", "--ca-directory=/dev/enoent";
792 push @cmd, $url.$subpath;
798 sub archive_query ($) {
800 my $query = access_cfg('archive-query','RETURN-UNDEF');
801 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
804 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
807 sub pool_dsc_subpath ($$) {
808 my ($vsn,$component) = @_; # $package is implict arg
809 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
810 return "/pool/$component/$prefix/$package/".dscfn($vsn);
813 sub archive_query_madison {
814 return map { [ @$_[0..1] ] } madison_get_parse(@_);
817 sub madison_get_parse {
818 my ($proto,$data) = @_;
819 die unless $proto eq 'madison';
821 $data= access_cfg('madison-distro','RETURN-UNDEF');
822 $data //= access_basedistro();
824 $rmad{$proto,$data,$package} ||= cmdoutput
825 qw(rmadison -asource),"-s$isuite","-u$data",$package;
826 my $rmad = $rmad{$proto,$data,$package};
829 foreach my $l (split /\n/, $rmad) {
830 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
831 \s*( [^ \t|]+ )\s* \|
832 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
833 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
834 $1 eq $package or die "$rmad $package ?";
841 $component = access_cfg('archive-query-default-component');
843 $5 eq 'source' or die "$rmad ?";
844 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
846 return sort { -version_compare($a->[0],$b->[0]); } @out;
849 sub canonicalise_suite_madison {
850 # madison canonicalises for us
851 my @r = madison_get_parse(@_);
853 "unable to canonicalise suite using package $package".
854 " which does not appear to exist in suite $isuite;".
855 " --existing-package may help";
860 my ($data,$runeinfo,$sql) = @_;
862 $data= access_someuserhost('sshpsql').':'.
863 access_cfg('sshpsql-dbname');
865 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
866 my ($userhost,$dbname) = ($`,$'); #';
868 my @cmd = (access_cfg_ssh, $userhost,
869 access_runeinfo("ssh-psql $runeinfo").
871 " ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
872 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
873 open P, "-|", @cmd or die $!;
876 printdebug("$debugprefix>|$_|\n");
879 $!=0; $?=0; close P or failedcmd @cmd;
881 my $nrows = pop @rows;
882 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
883 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
884 @rows = map { [ split /\|/, $_ ] } @rows;
885 my $ncols = scalar @{ shift @rows };
886 die if grep { scalar @$_ != $ncols } @rows;
890 sub sql_injection_check {
891 foreach (@_) { die "$_ $& ?" if m{[^-+=:_.,/0-9a-zA-Z]}; }
894 sub archive_query_sshpsql ($$) {
895 my ($proto,$data) = @_;
896 sql_injection_check $isuite, $package;
897 my @rows = sshpsql($data, "archive-query $isuite $package", <<END);
898 SELECT source.version, component.name, files.filename, files.sha256sum
900 JOIN src_associations ON source.id = src_associations.source
901 JOIN suite ON suite.id = src_associations.suite
902 JOIN dsc_files ON dsc_files.source = source.id
903 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
904 JOIN component ON component.id = files_archive_map.component_id
905 JOIN files ON files.id = dsc_files.file
906 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
907 AND source.source='$package'
908 AND files.filename LIKE '%.dsc';
910 @rows = sort { -version_compare($a->[0],$b->[0]) } @rows;
911 my $digester = Digest::SHA->new(256);
913 my ($vsn,$component,$filename,$sha256sum) = @$_;
914 [ $vsn, "/pool/$component/$filename",$digester,$sha256sum ];
919 sub canonicalise_suite_sshpsql ($$) {
920 my ($proto,$data) = @_;
921 sql_injection_check $isuite;
922 my @rows = sshpsql($data, "canonicalise-suite $isuite", <<END);
923 SELECT suite.codename
924 FROM suite where suite_name='$isuite' or codename='$isuite';
926 @rows = map { $_->[0] } @rows;
927 fail "unknown suite $isuite" unless @rows;
928 die "ambiguous $isuite: @rows ?" if @rows>1;
932 sub canonicalise_suite_dummycat ($$) {
933 my ($proto,$data) = @_;
934 my $dpath = "$data/suite.$isuite";
935 if (!open C, "<", $dpath) {
936 $!==ENOENT or die "$dpath: $!";
937 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
941 chomp or die "$dpath: $!";
943 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
947 sub archive_query_dummycat ($$) {
948 my ($proto,$data) = @_;
949 canonicalise_suite();
950 my $dpath = "$data/package.$csuite.$package";
951 if (!open C, "<", $dpath) {
952 $!==ENOENT or die "$dpath: $!";
953 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
961 printdebug "dummycat query $csuite $package $dpath | $_\n";
962 my @row = split /\s+/, $_;
963 @row==2 or die "$dpath: $_ ?";
966 C->error and die "$dpath: $!";
968 return sort { -version_compare($a->[0],$b->[0]); } @rows;
971 sub canonicalise_suite () {
972 return if defined $csuite;
973 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
974 $csuite = archive_query('canonicalise_suite');
975 if ($isuite ne $csuite) {
976 progress "canonical suite name for $isuite is $csuite";
980 sub get_archive_dsc () {
981 canonicalise_suite();
982 my @vsns = archive_query('archive_query');
983 foreach my $vinfo (@vsns) {
984 my ($vsn,$subpath,$digester,$digest) = @$vinfo;
985 $dscurl = access_cfg('mirror').$subpath;
986 $dscdata = url_get($dscurl);
988 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
993 $digester->add($dscdata);
994 my $got = $digester->hexdigest();
996 fail "$dscurl has hash $got but".
997 " archive told us to expect $digest";
999 my $dscfh = new IO::File \$dscdata, '<' or die $!;
1000 printdebug Dumper($dscdata) if $debug>1;
1001 $dsc = parsecontrolfh($dscfh,$dscurl,1);
1002 printdebug Dumper($dsc) if $debug>1;
1003 my $fmt = getfield $dsc, 'Format';
1004 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
1005 $dsc_checked = !!$digester;
1011 sub check_for_git ();
1012 sub check_for_git () {
1014 my $how = access_cfg('git-check');
1015 if ($how eq 'ssh-cmd') {
1017 (access_cfg_ssh, access_gituserhost(),
1018 access_runeinfo("git-check $package").
1019 " set -e; cd ".access_cfg('git-path').";".
1020 " if test -d $package.git; then echo 1; else echo 0; fi");
1021 my $r= cmdoutput @cmd;
1022 if ($r =~ m/^divert (\w+)$/) {
1024 my ($usedistro,) = access_distros();
1025 $instead_distro= cfg("dgit-distro.$usedistro.diverts.$divert");
1026 $instead_distro =~ s{^/}{ access_basedistro()."/" }e;
1027 printdebug "diverting $divert so using distro $instead_distro\n";
1028 return check_for_git();
1030 failedcmd @cmd unless $r =~ m/^[01]$/;
1032 } elsif ($how eq 'true') {
1034 } elsif ($how eq 'false') {
1037 badcfg "unknown git-check \`$how'";
1041 sub create_remote_git_repo () {
1042 my $how = access_cfg('git-create');
1043 if ($how eq 'ssh-cmd') {
1045 (access_cfg_ssh, access_gituserhost(),
1046 access_runeinfo("git-create $package").
1047 "set -e; cd ".access_cfg('git-path').";".
1048 " cp -a _template $package.git");
1049 } elsif ($how eq 'true') {
1052 badcfg "unknown git-create \`$how'";
1056 our ($dsc_hash,$lastpush_hash);
1058 our $ud = '.git/dgit/unpack';
1063 mkdir $ud or die $!;
1066 sub mktree_in_ud_here () {
1067 runcmd qw(git init -q);
1068 rmtree('.git/objects');
1069 symlink '../../../../objects','.git/objects' or die $!;
1072 sub git_write_tree () {
1073 my $tree = cmdoutput @git, qw(write-tree);
1074 $tree =~ m/^\w+$/ or die "$tree ?";
1078 sub mktree_in_ud_from_only_subdir () {
1079 # changes into the subdir
1081 die unless @dirs==1;
1082 $dirs[0] =~ m#^([^/]+)/\.$# or die;
1085 fail "source package contains .git directory" if stat_exists '.git';
1086 mktree_in_ud_here();
1087 my $format=get_source_format();
1088 if (madformat($format)) {
1091 runcmd @git, qw(add -Af);
1092 my $tree=git_write_tree();
1093 return ($tree,$dir);
1096 sub dsc_files_info () {
1097 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
1098 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
1099 ['Files', 'Digest::MD5', 'new()']) {
1100 my ($fname, $module, $method) = @$csumi;
1101 my $field = $dsc->{$fname};
1102 next unless defined $field;
1103 eval "use $module; 1;" or die $@;
1105 foreach (split /\n/, $field) {
1107 m/^(\w+) (\d+) (\S+)$/ or
1108 fail "could not parse .dsc $fname line \`$_'";
1109 my $digester = eval "$module"."->$method;" or die $@;
1114 Digester => $digester,
1119 fail "missing any supported Checksums-* or Files field in ".
1120 $dsc->get_option('name');
1124 map { $_->{Filename} } dsc_files_info();
1127 sub is_orig_file ($;$) {
1130 m/\.orig(?:-\w+)?\.tar\.\w+$/ or return 0;
1131 defined $base or return 1;
1135 sub make_commit ($) {
1137 return cmdoutput @git, qw(hash-object -w -t commit), $file;
1140 sub clogp_authline ($) {
1142 my $author = getfield $clogp, 'Maintainer';
1143 $author =~ s#,.*##ms;
1144 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
1145 my $authline = "$author $date";
1146 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
1147 fail "unexpected commit author line format \`$authline'".
1148 " (was generated from changelog Maintainer field)";
1152 sub generate_commit_from_dsc () {
1156 foreach my $fi (dsc_files_info()) {
1157 my $f = $fi->{Filename};
1158 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
1160 link "../../../$f", $f
1164 complete_file_from_dsc('.', $fi);
1166 if (is_orig_file($f)) {
1167 link $f, "../../../../$f"
1173 my $dscfn = "$package.dsc";
1175 open D, ">", $dscfn or die "$dscfn: $!";
1176 print D $dscdata or die "$dscfn: $!";
1177 close D or die "$dscfn: $!";
1178 my @cmd = qw(dpkg-source);
1179 push @cmd, '--no-check' if $dsc_checked;
1180 push @cmd, qw(-x --), $dscfn;
1183 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1184 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
1185 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
1186 my $authline = clogp_authline $clogp;
1187 my $changes = getfield $clogp, 'Changes';
1188 open C, ">../commit.tmp" or die $!;
1189 print C <<END or die $!;
1196 # imported from the archive
1199 my $outputhash = make_commit qw(../commit.tmp);
1200 my $cversion = getfield $clogp, 'Version';
1201 progress "synthesised git commit from .dsc $cversion";
1202 if ($lastpush_hash) {
1203 runcmd @git, qw(reset --hard), $lastpush_hash;
1204 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
1205 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
1206 my $oversion = getfield $oldclogp, 'Version';
1208 version_compare($oversion, $cversion);
1210 # git upload/ is earlier vsn than archive, use archive
1211 open C, ">../commit2.tmp" or die $!;
1212 print C <<END or die $!;
1214 parent $lastpush_hash
1219 Record $package ($cversion) in archive suite $csuite
1221 $outputhash = make_commit qw(../commit2.tmp);
1222 } elsif ($vcmp > 0) {
1223 print STDERR <<END or die $!;
1225 Version actually in archive: $cversion (older)
1226 Last allegedly pushed/uploaded: $oversion (newer or same)
1229 $outputhash = $lastpush_hash;
1231 $outputhash = $lastpush_hash;
1234 changedir '../../../..';
1235 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1236 'DGIT_ARCHIVE', $outputhash;
1237 cmdoutput @git, qw(log -n2), $outputhash;
1238 # ... gives git a chance to complain if our commit is malformed
1243 sub complete_file_from_dsc ($$) {
1244 our ($dstdir, $fi) = @_;
1245 # Ensures that we have, in $dir, the file $fi, with the correct
1246 # contents. (Downloading it from alongside $dscurl if necessary.)
1248 my $f = $fi->{Filename};
1249 my $tf = "$dstdir/$f";
1252 if (stat_exists $tf) {
1253 progress "using existing $f";
1256 $furl =~ s{/[^/]+$}{};
1258 die "$f ?" unless $f =~ m/^${package}_/;
1259 die "$f ?" if $f =~ m#/#;
1260 runcmd_ordryrun_local @curl,qw(-o),$tf,'--',"$furl";
1261 next if !act_local();
1265 open F, "<", "$tf" or die "$tf: $!";
1266 $fi->{Digester}->reset();
1267 $fi->{Digester}->addfile(*F);
1268 F->error and die $!;
1269 my $got = $fi->{Digester}->hexdigest();
1270 $got eq $fi->{Hash} or
1271 fail "file $f has hash $got but .dsc".
1272 " demands hash $fi->{Hash} ".
1273 ($downloaded ? "(got wrong file from archive!)"
1274 : "(perhaps you should delete this file?)");
1277 sub ensure_we_have_orig () {
1278 foreach my $fi (dsc_files_info()) {
1279 my $f = $fi->{Filename};
1280 next unless is_orig_file($f);
1281 complete_file_from_dsc('..', $fi);
1286 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1289 sub is_fast_fwd ($$) {
1290 my ($ancestor,$child) = @_;
1291 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1292 my $mb = cmdoutput_errok @cmd;
1294 return rev_parse($mb) eq rev_parse($ancestor);
1296 $?==256 or failedcmd @cmd;
1301 sub git_fetch_us () {
1302 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1305 sub fetch_from_archive () {
1306 # ensures that lrref() is what is actually in the archive,
1307 # one way or another
1311 foreach my $field (@ourdscfield) {
1312 $dsc_hash = $dsc->{$field};
1313 last if defined $dsc_hash;
1315 if (defined $dsc_hash) {
1316 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1318 progress "last upload to archive specified git hash";
1320 progress "last upload to archive has NO git hash";
1323 progress "no version available from the archive";
1326 $lastpush_hash = git_get_ref(lrref());
1327 printdebug "previous reference hash=$lastpush_hash\n";
1329 if (defined $dsc_hash) {
1330 fail "missing remote git history even though dsc has hash -".
1331 " could not find ref ".lrref().
1332 " (should have been fetched from ".access_giturl()."#".rrref().")"
1333 unless $lastpush_hash;
1335 ensure_we_have_orig();
1336 if ($dsc_hash eq $lastpush_hash) {
1337 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1338 print STDERR <<END or die $!;
1340 Git commit in archive is behind the last version allegedly pushed/uploaded.
1341 Commit referred to by archive: $dsc_hash
1342 Last allegedly pushed/uploaded: $lastpush_hash
1345 $hash = $lastpush_hash;
1347 fail "archive's .dsc refers to ".$dsc_hash.
1348 " but this is an ancestor of ".$lastpush_hash;
1351 $hash = generate_commit_from_dsc();
1352 } elsif ($lastpush_hash) {
1353 # only in git, not in the archive yet
1354 $hash = $lastpush_hash;
1355 print STDERR <<END or die $!;
1357 Package not found in the archive, but has allegedly been pushed using dgit.
1361 printdebug "nothing found!\n";
1362 if (defined $skew_warning_vsn) {
1363 print STDERR <<END or die $!;
1365 Warning: relevant archive skew detected.
1366 Archive allegedly contains $skew_warning_vsn
1367 But we were not able to obtain any version from the archive or git.
1373 printdebug "current hash=$hash\n";
1374 if ($lastpush_hash) {
1375 fail "not fast forward on last upload branch!".
1376 " (archive's version left in DGIT_ARCHIVE)"
1377 unless is_fast_fwd($lastpush_hash, $hash);
1379 if (defined $skew_warning_vsn) {
1381 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1382 my $clogf = ".git/dgit/changelog.tmp";
1383 runcmd shell_cmd "exec >$clogf",
1384 @git, qw(cat-file blob), "$hash:debian/changelog";
1385 my $gotclogp = parsechangelog("-l$clogf");
1386 my $got_vsn = getfield $gotclogp, 'Version';
1387 printdebug "SKEW CHECK GOT $got_vsn\n";
1388 if (version_compare($got_vsn, $skew_warning_vsn) < 0) {
1389 print STDERR <<END or die $!;
1391 Warning: archive skew detected. Using the available version:
1392 Archive allegedly contains $skew_warning_vsn
1393 We were able to obtain only $got_vsn
1398 if ($lastpush_hash ne $hash) {
1399 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1403 dryrun_report @upd_cmd;
1411 canonicalise_suite();
1412 badusage "dry run makes no sense with clone" unless act_local();
1413 my $hasgit = check_for_git();
1414 mkdir $dstdir or die "$dstdir $!";
1416 runcmd @git, qw(init -q);
1417 my $giturl = access_giturl(1);
1418 if (defined $giturl) {
1419 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1420 open H, "> .git/HEAD" or die $!;
1421 print H "ref: ".lref()."\n" or die $!;
1423 runcmd @git, qw(remote add), 'origin', $giturl;
1426 progress "fetching existing git history";
1428 runcmd_ordryrun_local @git, qw(fetch origin);
1430 progress "starting new git history";
1432 fetch_from_archive() or no_such_package;
1433 my $vcsgiturl = $dsc->{'Vcs-Git'};
1434 $vcsgiturl =~ s/\s+-b\s+\S+//g;
1435 if (length $vcsgiturl) {
1436 runcmd @git, qw(remote add vcs-git), $vcsgiturl;
1438 runcmd @git, qw(reset --hard), lrref();
1439 printdone "ready for work in $dstdir";
1443 if (check_for_git()) {
1446 fetch_from_archive() or no_such_package();
1447 printdone "fetched into ".lrref();
1452 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1454 printdone "fetched to ".lrref()." and merged into HEAD";
1457 sub check_not_dirty () {
1458 return if $ignoredirty;
1459 my @cmd = (@git, qw(diff --quiet HEAD));
1460 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1461 $!=0; $?=0; system @cmd;
1462 return if !$! && !$?;
1463 if (!$! && $?==256) {
1464 fail "working tree is dirty (does not match HEAD)";
1470 sub commit_admin ($) {
1473 runcmd_ordryrun_local @git, qw(commit -m), $m;
1476 sub commit_quilty_patch () {
1477 my $output = cmdoutput @git, qw(status --porcelain);
1479 foreach my $l (split /\n/, $output) {
1480 next unless $l =~ m/\S/;
1481 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1485 delete $adds{'.pc'}; # if there wasn't one before, don't add it
1487 progress "nothing quilty to commit, ok.";
1490 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1491 commit_admin "Commit Debian 3.0 (quilt) metadata";
1494 sub get_source_format () {
1495 if (!open F, "debian/source/format") {
1496 die $! unless $!==&ENOENT;
1500 F->error and die $!;
1507 return 0 unless $format eq '3.0 (quilt)';
1508 if ($quilt_mode eq 'nocheck') {
1509 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1512 progress "Format \`$format', checking/updating patch stack";
1516 sub push_parse_changelog ($) {
1519 my $clogp = Dpkg::Control::Hash->new();
1520 $clogp->load($clogpfn) or die;
1522 $package = getfield $clogp, 'Source';
1523 my $cversion = getfield $clogp, 'Version';
1524 my $tag = debiantag($cversion);
1525 runcmd @git, qw(check-ref-format), $tag;
1527 my $dscfn = dscfn($cversion);
1529 return ($clogp, $cversion, $tag, $dscfn);
1532 sub push_parse_dsc ($$$) {
1533 my ($dscfn,$dscfnwhat, $cversion) = @_;
1534 $dsc = parsecontrol($dscfn,$dscfnwhat);
1535 my $dversion = getfield $dsc, 'Version';
1536 my $dscpackage = getfield $dsc, 'Source';
1537 ($dscpackage eq $package && $dversion eq $cversion) or
1538 fail "$dscfn is for $dscpackage $dversion".
1539 " but debian/changelog is for $package $cversion";
1542 sub push_mktag ($$$$$$$) {
1543 my ($head,$clogp,$tag,
1545 $changesfile,$changesfilewhat,
1548 $dsc->{$ourdscfield[0]} = $head;
1549 $dsc->save("$dscfn.tmp") or die $!;
1551 my $changes = parsecontrol($changesfile,$changesfilewhat);
1552 foreach my $field (qw(Source Distribution Version)) {
1553 $changes->{$field} eq $clogp->{$field} or
1554 fail "changes field $field \`$changes->{$field}'".
1555 " does not match changelog \`$clogp->{$field}'";
1558 my $cversion = getfield $clogp, 'Version';
1559 my $clogsuite = getfield $clogp, 'Distribution';
1561 # We make the git tag by hand because (a) that makes it easier
1562 # to control the "tagger" (b) we can do remote signing
1563 my $authline = clogp_authline $clogp;
1564 my $delibs = join(" ", "",@deliberatelies);
1565 open TO, '>', $tfn->('.tmp') or die $!;
1566 print TO <<END or die $!;
1572 $package release $cversion for $clogsuite ($csuite) [dgit]
1573 [dgit distro=$distro$delibs]
1577 my $tagobjfn = $tfn->('.tmp');
1579 if (!defined $keyid) {
1580 $keyid = access_cfg('keyid','RETURN-UNDEF');
1582 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1583 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1584 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1585 push @sign_cmd, $tfn->('.tmp');
1586 runcmd_ordryrun @sign_cmd;
1588 $tagobjfn = $tfn->('.signed.tmp');
1589 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1590 $tfn->('.tmp'), $tfn->('.tmp.asc');
1597 sub sign_changes ($) {
1598 my ($changesfile) = @_;
1600 my @debsign_cmd = @debsign;
1601 push @debsign_cmd, "-k$keyid" if defined $keyid;
1602 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1603 push @debsign_cmd, $changesfile;
1604 runcmd_ordryrun @debsign_cmd;
1609 printdebug "actually entering push\n";
1612 access_giturl(); # check that success is vaguely likely
1614 my $clogpfn = ".git/dgit/changelog.822.tmp";
1615 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1617 responder_send_file('parsed-changelog', $clogpfn);
1619 my ($clogp, $cversion, $tag, $dscfn) =
1620 push_parse_changelog("$clogpfn");
1622 my $dscpath = "$buildproductsdir/$dscfn";
1623 stat_exists $dscpath or
1624 fail "looked for .dsc $dscfn, but $!;".
1625 " maybe you forgot to build";
1627 responder_send_file('dsc', $dscpath);
1629 push_parse_dsc($dscpath, $dscfn, $cversion);
1631 my $format = getfield $dsc, 'Format';
1632 printdebug "format $format\n";
1633 if (madformat($format)) {
1634 commit_quilty_patch();
1638 progress "checking that $dscfn corresponds to HEAD";
1639 runcmd qw(dpkg-source -x --),
1640 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1641 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1642 changedir '../../../..';
1643 my $diffopt = $debug>0 ? '--exit-code' : '--quiet';
1644 my @diffcmd = (@git, qw(diff), $diffopt, $tree);
1645 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1647 my $r = system @diffcmd;
1650 fail "$dscfn specifies a different tree to your HEAD commit;".
1651 " perhaps you forgot to build".
1652 ($diffopt eq '--exit-code' ? "" :
1653 " (run with -D to see full diff output)");
1659 #do fast forward check and maybe fake merge
1660 # if (!is_fast_fwd(mainbranch
1661 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1662 # map { lref($_).":".rref($_) }
1664 my $head = rev_parse('HEAD');
1665 if (!$changesfile) {
1666 my $multi = "$buildproductsdir/".
1667 "${package}_".(stripepoch $cversion)."_multi.changes";
1668 if (stat_exists "$multi") {
1669 $changesfile = $multi;
1671 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1672 my @cs = glob "$buildproductsdir/$pat";
1673 fail "failed to find unique changes file".
1674 " (looked for $pat in $buildproductsdir, or $multi);".
1675 " perhaps you need to use dgit -C"
1677 ($changesfile) = @cs;
1680 $changesfile = "$buildproductsdir/$changesfile";
1683 responder_send_file('changes',$changesfile);
1684 responder_send_command("param head $head");
1685 responder_send_command("param csuite $csuite");
1687 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1690 if ($we_are_responder) {
1691 $tagobjfn = $tfn->('.signed.tmp');
1692 responder_receive_files('signed-tag', $tagobjfn);
1695 push_mktag($head,$clogp,$tag,
1697 $changesfile,$changesfile,
1701 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1702 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1703 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1704 runcmd_ordryrun @git, qw(tag -v --), $tag;
1706 if (!check_for_git()) {
1707 create_remote_git_repo();
1709 runcmd_ordryrun @git, qw(push),access_giturl(),
1710 "HEAD:".rrref(), "refs/tags/$tag";
1711 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1713 if ($we_are_responder) {
1714 my $dryrunsuffix = act_local() ? "" : ".tmp";
1715 responder_receive_files('signed-dsc-changes',
1716 "$dscpath$dryrunsuffix",
1717 "$changesfile$dryrunsuffix");
1720 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1722 progress "[new .dsc left in $dscpath.tmp]";
1724 sign_changes $changesfile;
1727 my $host = access_cfg('upload-host','RETURN-UNDEF');
1728 my @hostarg = defined($host) ? ($host,) : ();
1729 runcmd_ordryrun @dput, @hostarg, $changesfile;
1730 printdone "pushed and uploaded $cversion";
1732 responder_send_command("complete");
1738 badusage "-p is not allowed with clone; specify as argument instead"
1739 if defined $package;
1742 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1743 ($package,$isuite) = @ARGV;
1744 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1745 ($package,$dstdir) = @ARGV;
1746 } elsif (@ARGV==3) {
1747 ($package,$isuite,$dstdir) = @ARGV;
1749 badusage "incorrect arguments to dgit clone";
1751 $dstdir ||= "$package";
1753 if (stat_exists $dstdir) {
1754 fail "$dstdir already exists";
1758 if ($rmonerror && !$dryrun_level) {
1759 $cwd_remove= getcwd();
1761 return unless defined $cwd_remove;
1762 if (!chdir "$cwd_remove") {
1763 return if $!==&ENOENT;
1764 die "chdir $cwd_remove: $!";
1766 rmtree($dstdir) or die "remove $dstdir: $!\n";
1771 $cwd_remove = undef;
1774 sub branchsuite () {
1775 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1776 if ($branch =~ m#$lbranch_re#o) {
1783 sub fetchpullargs () {
1784 if (!defined $package) {
1785 my $sourcep = parsecontrol('debian/control','debian/control');
1786 $package = getfield $sourcep, 'Source';
1789 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1791 my $clogp = parsechangelog();
1792 $isuite = getfield $clogp, 'Distribution';
1794 canonicalise_suite();
1795 progress "fetching from suite $csuite";
1796 } elsif (@ARGV==1) {
1798 canonicalise_suite();
1800 badusage "incorrect arguments to dgit fetch or dgit pull";
1818 badusage "-p is not allowed with dgit push" if defined $package;
1820 my $clogp = parsechangelog();
1821 $package = getfield $clogp, 'Source';
1824 } elsif (@ARGV==1) {
1825 ($specsuite) = (@ARGV);
1827 badusage "incorrect arguments to dgit push";
1829 $isuite = getfield $clogp, 'Distribution';
1831 local ($package) = $existing_package; # this is a hack
1832 canonicalise_suite();
1834 if (defined $specsuite && $specsuite ne $isuite) {
1835 canonicalise_suite();
1836 $csuite eq $specsuite or
1837 fail "dgit push: changelog specifies $isuite ($csuite)".
1838 " but command line specifies $specsuite";
1840 if (check_for_git()) {
1843 if (fetch_from_archive()) {
1844 is_fast_fwd(lrref(), 'HEAD') or
1845 fail "dgit push: HEAD is not a descendant".
1846 " of the archive's version.\n".
1847 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1850 fail "package appears to be new in this suite;".
1851 " if this is intentional, use --new";
1856 #---------- remote commands' implementation ----------
1858 sub cmd_remote_push_build_host {
1859 my ($nrargs) = shift @ARGV;
1860 my (@rargs) = @ARGV[0..$nrargs-1];
1861 @ARGV = @ARGV[$nrargs..$#ARGV];
1863 my ($dir,$vsnwant) = @rargs;
1864 # vsnwant is a comma-separated list; we report which we have
1865 # chosen in our ready response (so other end can tell if they
1868 $we_are_responder = 1;
1870 open PI, "<&STDIN" or die $!;
1871 open STDIN, "/dev/null" or die $!;
1872 open PO, ">&STDOUT" or die $!;
1874 open STDOUT, ">&STDERR" or die $!;
1878 fail "build host has dgit rpush protocol version".
1879 " $rpushprotovsn but invocation host has $vsnwant"
1880 unless grep { $rpushprotovsn eq $_ } split /,/, $vsnwant;
1882 responder_send_command("dgit-remote-push-ready $rpushprotovsn");
1888 sub cmd_remote_push_responder { cmd_remote_push_build_host(); }
1889 # ... for compatibility with proto vsn.1 dgit (just so that user gets
1890 # a good error message)
1896 my $report = i_child_report();
1897 if (defined $report) {
1898 printdebug "($report)\n";
1899 } elsif ($i_child_pid) {
1900 printdebug "(killing build host child $i_child_pid)\n";
1901 kill 15, $i_child_pid;
1903 if (defined $i_tmp && !defined $initiator_tempdir) {
1905 eval { rmtree $i_tmp; };
1909 END { i_cleanup(); }
1912 my ($base,$selector,@args) = @_;
1913 $selector =~ s/\-/_/g;
1914 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1920 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1927 my @rargs = ($dir,$rpushprotovsn);
1930 push @rdgit, @ropts;
1931 push @rdgit, qw(remote-push-build-host), (scalar @rargs), @rargs;
1933 my @cmd = (@ssh, $host, shellquote @rdgit);
1934 printcmd \*DEBUG,$debugprefix."+",@cmd;
1936 if (defined $initiator_tempdir) {
1937 rmtree $initiator_tempdir;
1938 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1939 $i_tmp = $initiator_tempdir;
1943 $i_child_pid = open2(\*RO, \*RI, @cmd);
1945 initiator_expect { m/^dgit-remote-push-ready/ };
1947 my ($icmd,$iargs) = initiator_expect {
1948 m/^(\S+)(?: (.*))?$/;
1951 i_method "i_resp", $icmd, $iargs;
1955 sub i_resp_progress ($) {
1957 my $msg = protocol_read_bytes \*RO, $rhs;
1961 sub i_resp_complete {
1962 my $pid = $i_child_pid;
1963 $i_child_pid = undef; # prevents killing some other process with same pid
1964 printdebug "waiting for build host child $pid...\n";
1965 my $got = waitpid $pid, 0;
1966 die $! unless $got == $pid;
1967 die "build host child failed $?" if $?;
1970 printdebug "all done\n";
1974 sub i_resp_file ($) {
1976 my $localname = i_method "i_localname", $keyword;
1977 my $localpath = "$i_tmp/$localname";
1978 stat_exists $localpath and
1979 badproto \*RO, "file $keyword ($localpath) twice";
1980 protocol_receive_file \*RO, $localpath;
1981 i_method "i_file", $keyword;
1986 sub i_resp_param ($) {
1987 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1993 sub i_resp_want ($) {
1995 die "$keyword ?" if $i_wanted{$keyword}++;
1996 my @localpaths = i_method "i_want", $keyword;
1997 printdebug "[[ $keyword @localpaths\n";
1998 foreach my $localpath (@localpaths) {
1999 protocol_send_file \*RI, $localpath;
2001 print RI "files-end\n" or die $!;
2004 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
2006 sub i_localname_parsed_changelog {
2007 return "remote-changelog.822";
2009 sub i_file_parsed_changelog {
2010 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
2011 push_parse_changelog "$i_tmp/remote-changelog.822";
2012 die if $i_dscfn =~ m#/|^\W#;
2015 sub i_localname_dsc {
2016 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
2021 sub i_localname_changes {
2022 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
2023 $i_changesfn = $i_dscfn;
2024 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
2025 return $i_changesfn;
2027 sub i_file_changes { }
2029 sub i_want_signed_tag {
2030 printdebug Dumper(\%i_param, $i_dscfn);
2031 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
2032 && defined $i_param{'csuite'}
2033 or badproto \*RO, "premature desire for signed-tag";
2034 my $head = $i_param{'head'};
2035 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
2037 die unless $i_param{'csuite'} =~ m/^$suite_re$/;
2039 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
2042 push_mktag $head, $i_clogp, $i_tag,
2044 $i_changesfn, 'remote changes',
2045 sub { "tag$_[0]"; };
2050 sub i_want_signed_dsc_changes {
2051 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
2052 sign_changes $i_changesfn;
2053 return ($i_dscfn, $i_changesfn);
2056 #---------- building etc. ----------
2062 #----- `3.0 (quilt)' handling -----
2064 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
2066 sub quiltify_dpkg_commit ($$$;$) {
2067 my ($patchname,$author,$msg, $xinfo) = @_;
2071 my $descfn = ".git/dgit/quilt-description.tmp";
2072 open O, '>', $descfn or die "$descfn: $!";
2075 $msg =~ s/^\s+$/ ./mg;
2076 print O <<END or die $!;
2086 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
2087 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
2088 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
2089 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
2093 sub quiltify_trees_differ ($$) {
2095 # returns 1 iff the two tree objects differ other than in debian/
2097 my @cmd = (@git, qw(diff-tree --name-only -z), $x, $y);
2098 my $diffs= cmdoutput @cmd;
2099 foreach my $f (split /\0/, $diffs) {
2100 next if $f eq 'debian';
2106 sub quiltify_tree_sentinelfiles ($) {
2107 # lists the `sentinel' files present in the tree
2109 my $r = cmdoutput @git, qw(ls-tree --name-only), $x,
2110 qw(-- debian/rules debian/control);
2116 my ($clogp,$target) = @_;
2118 # Quilt patchification algorithm
2120 # We search backwards through the history of the main tree's HEAD
2121 # (T) looking for a start commit S whose tree object is identical
2122 # to to the patch tip tree (ie the tree corresponding to the
2123 # current dpkg-committed patch series). For these purposes
2124 # `identical' disregards anything in debian/ - this wrinkle is
2125 # necessary because dpkg-source treates debian/ specially.
2127 # We can only traverse edges where at most one of the ancestors'
2128 # trees differs (in changes outside in debian/). And we cannot
2129 # handle edges which change .pc/ or debian/patches. To avoid
2130 # going down a rathole we avoid traversing edges which introduce
2131 # debian/rules or debian/control. And we set a limit on the
2132 # number of edges we are willing to look at.
2134 # If we succeed, we walk forwards again. For each traversed edge
2135 # PC (with P parent, C child) (starting with P=S and ending with
2136 # C=T) to we do this:
2138 # - dpkg-source --commit with a patch name and message derived from C
2139 # After traversing PT, we git commit the changes which
2140 # should be contained within debian/patches.
2142 changedir '../fake';
2143 mktree_in_ud_here();
2145 runcmd @git, 'add', '.';
2146 my $oldtiptree=git_write_tree();
2147 changedir '../work';
2149 # The search for the path S..T is breadth-first. We maintain a
2150 # todo list containing search nodes. A search node identifies a
2151 # commit, and looks something like this:
2153 # Commit => $git_commit_id,
2154 # Child => $c, # or undef if P=T
2155 # Whynot => $reason_edge_PC_unsuitable, # in @nots only
2156 # Nontrivial => true iff $p..$c has relevant changes
2163 my %considered; # saves being exponential on some weird graphs
2165 my $t_sentinels = quiltify_tree_sentinelfiles $target;
2168 my ($search,$whynot) = @_;
2169 printdebug " search NOT $search->{Commit} $whynot\n";
2170 $search->{Whynot} = $whynot;
2171 push @nots, $search;
2172 no warnings qw(exiting);
2181 my $c = shift @todo;
2182 next if $considered{$c->{Commit}}++;
2184 $not->($c, "maximum search space exceeded") if --$max_work <= 0;
2186 printdebug "quiltify investigate $c->{Commit}\n";
2189 if (!quiltify_trees_differ $c->{Commit}, $oldtiptree) {
2190 printdebug " search finished hooray!\n";
2195 if ($quilt_mode eq 'nofix') {
2196 fail "quilt fixup required but quilt mode is \`nofix'\n".
2197 "HEAD commit $c->{Commit} differs from tree implied by ".
2198 " debian/patches (tree object $oldtiptree)";
2200 if ($quilt_mode eq 'smash') {
2201 printdebug " search quitting smash\n";
2205 my $c_sentinels = quiltify_tree_sentinelfiles $c->{Commit};
2206 $not->($c, "has $c_sentinels not $t_sentinels")
2207 if $c_sentinels ne $t_sentinels;
2209 my $commitdata = cmdoutput @git, qw(cat-file commit), $c->{Commit};
2210 $commitdata =~ m/\n\n/;
2212 my @parents = ($commitdata =~ m/^parent (\w+)$/gm);
2213 @parents = map { { Commit => $_, Child => $c } } @parents;
2215 $not->($c, "root commit") if !@parents;
2217 foreach my $p (@parents) {
2218 $p->{Nontrivial}= quiltify_trees_differ $p->{Commit},$c->{Commit};
2220 my $ndiffers = grep { $_->{Nontrivial} } @parents;
2221 $not->($c, "merge ($ndiffers nontrivial parents)") if $ndiffers > 1;
2223 foreach my $p (@parents) {
2224 printdebug "considering C=$c->{Commit} P=$p->{Commit}\n";
2226 my @cmd= (@git, qw(diff-tree -r --name-only),
2227 $p->{Commit},$c->{Commit}, qw(-- debian/patches .pc));
2228 my $patchstackchange = cmdoutput @cmd;
2229 if (length $patchstackchange) {
2230 $patchstackchange =~ s/\n/,/g;
2231 $not->($p, "changed $patchstackchange");
2234 printdebug " search queue P=$p->{Commit} ",
2235 ($p->{Nontrivial} ? "NT" : "triv"),"\n";
2241 printdebug "quiltify want to smash\n";
2244 my $x = $_[0]{Commit};
2245 $x =~ s/(.*?[0-9a-z]{8})[0-9a-z]*$/$1/;
2248 my $reportnot = sub {
2250 my $s = $abbrev->($notp);
2251 my $c = $notp->{Child};
2252 $s .= "..".$abbrev->($c) if $c;
2253 $s .= ": ".$c->{Whynot};
2256 if ($quilt_mode eq 'linear') {
2257 print STDERR "$us: quilt fixup cannot be linear. Stopped at:\n";
2258 foreach my $notp (@nots) {
2259 print STDERR "$us: ", $reportnot->($notp), "\n";
2261 fail "quilt fixup naive history linearisation failed.\n".
2262 "Use dpkg-source --commit by hand; or, --quilt=smash for one ugly patch";
2263 } elsif ($quilt_mode eq 'smash') {
2264 } elsif ($quilt_mode eq 'auto') {
2265 progress "quilt fixup cannot be linear, smashing...";
2267 die "$quilt_mode ?";
2272 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
2274 quiltify_dpkg_commit "auto-$version-$target-$time",
2275 (getfield $clogp, 'Maintainer'),
2276 "Automatically generated patch ($clogp->{Version})\n".
2277 "Last (up to) $ncommits git changes, FYI:\n\n". $msg;
2281 progress "quiltify linearisation planning successful, executing...";
2283 for (my $p = $sref_S;
2284 my $c = $p->{Child};
2286 printdebug "quiltify traverse $p->{Commit}..$c->{Commit}\n";
2287 next unless $p->{Nontrivial};
2289 my $cc = $c->{Commit};
2291 my $commitdata = cmdoutput @git, qw(cat-file commit), $cc;
2292 $commitdata =~ m/\n\n/ or die "$c ?";
2295 $commitdata =~ m/^author (.*) \d+ [-+0-9]+$/m or die "$cc ?";
2298 $msg =~ s/^(.*)\n*/$1\n/ or die "$cc $msg ?";
2301 my $patchname = $title;
2302 $patchname =~ s/[.:]$//;
2303 $patchname =~ y/ A-Z/-a-z/;
2304 $patchname =~ y/-a-z0-9_.+=~//cd;
2305 $patchname =~ s/^\W/x-$&/;
2306 $patchname = substr($patchname,0,40);
2309 stat "debian/patches/$patchname$index";
2311 $!==ENOENT or die "$patchname$index $!";
2313 runcmd @git, qw(checkout -q), $cc;
2315 # We use the tip's changelog so that dpkg-source doesn't
2316 # produce complaining messages from dpkg-parsechangelog. None
2317 # of the information dpkg-source gets from the changelog is
2318 # actually relevant - it gets put into the original message
2319 # which dpkg-source provides our stunt editor, and then
2321 runcmd @git, qw(checkout -q), $target, qw(debian/changelog);
2323 quiltify_dpkg_commit "$patchname$index", $author, $msg,
2324 "X-Dgit-Generated: $clogp->{Version} $cc\n";
2326 runcmd @git, qw(checkout -q), $cc, qw(debian/changelog);
2329 runcmd @git, qw(checkout -q master);
2332 sub build_maybe_quilt_fixup () {
2333 my $format=get_source_format;
2334 return unless madformat $format;
2338 # - honour any existing .pc in case it has any strangeness
2339 # - determine the git commit corresponding to the tip of
2340 # the patch stack (if there is one)
2341 # - if there is such a git commit, convert each subsequent
2342 # git commit into a quilt patch with dpkg-source --commit
2343 # - otherwise convert all the differences in the tree into
2344 # a single git commit
2348 # Our git tree doesn't necessarily contain .pc. (Some versions of
2349 # dgit would include the .pc in the git tree.) If there isn't
2350 # one, we need to generate one by unpacking the patches that we
2353 # We first look for a .pc in the git tree. If there is one, we
2354 # will use it. (This is not the normal case.)
2356 # Otherwise need to regenerate .pc so that dpkg-source --commit
2357 # can work. We do this as follows:
2358 # 1. Collect all relevant .orig from parent directory
2359 # 2. Generate a debian.tar.gz out of
2360 # debian/{patches,rules,source/format}
2361 # 3. Generate a fake .dsc containing just these fields:
2362 # Format Source Version Files
2363 # 4. Extract the fake .dsc
2364 # Now the fake .dsc has a .pc directory.
2365 # (In fact we do this in every case, because in future we will
2366 # want to search for a good base commit for generating patches.)
2368 # Then we can actually do the dpkg-source --commit
2369 # 1. Make a new working tree with the same object
2370 # store as our main tree and check out the main
2372 # 2. Copy .pc from the fake's extraction, if necessary
2373 # 3. Run dpkg-source --commit
2374 # 4. If the result has changes to debian/, then
2375 # - git-add them them
2376 # - git-add .pc if we had a .pc in-tree
2378 # 5. If we had a .pc in-tree, delete it, and git-commit
2379 # 6. Back in the main tree, fast forward to the new HEAD
2381 my $clogp = parsechangelog();
2382 my $headref = rev_parse('HEAD');
2387 my $upstreamversion=$version;
2388 $upstreamversion =~ s/-[^-]*$//;
2390 my $fakeversion="$upstreamversion-~~DGITFAKE";
2392 my $fakedsc=new IO::File 'fake.dsc', '>' or die $!;
2393 print $fakedsc <<END or die $!;
2396 Version: $fakeversion
2400 my $dscaddfile=sub {
2403 my $md = new Digest::MD5;
2405 my $fh = new IO::File $b, '<' or die "$b $!";
2410 print $fakedsc " ".$md->hexdigest." $size $b\n" or die $!;
2413 foreach my $f (<../../../../*>) { #/){
2414 my $b=$f; $b =~ s{.*/}{};
2415 next unless is_orig_file $b, srcfn $upstreamversion,'';
2416 link $f, $b or die "$b $!";
2420 my @files=qw(debian/source/format debian/rules);
2421 if (stat_exists '../../../debian/patches') {
2422 push @files, 'debian/patches';
2425 my $debtar= srcfn $fakeversion,'.debian.tar.gz';
2426 runcmd qw(env GZIP=-1 tar -zcf), "./$debtar", qw(-C ../../..), @files;
2428 $dscaddfile->($debtar);
2429 close $fakedsc or die $!;
2431 runcmd qw(sh -ec), 'exec dpkg-source --no-check -x fake.dsc >/dev/null';
2433 my $fakexdir= $package.'-'.(stripepoch $upstreamversion);
2434 rename $fakexdir, "fake" or die "$fakexdir $!";
2436 mkdir "work" or die $!;
2438 mktree_in_ud_here();
2439 runcmd @git, qw(reset --hard), $headref;
2442 if (stat_exists ".pc") {
2444 progress "Tree already contains .pc - will use it then delete it.";
2447 rename '../fake/.pc','.pc' or die $!;
2450 quiltify($clogp,$headref);
2452 if (!open P, '>>', ".pc/applied-patches") {
2453 $!==&ENOENT or die $!;
2458 commit_quilty_patch();
2460 if ($mustdeletepc) {
2461 runcmd @git, qw(rm -rq .pc);
2462 commit_admin "Commit removal of .pc (quilt series tracking data)";
2465 changedir '../../../..';
2466 runcmd @git, qw(pull --ff-only -q .git/dgit/unpack/work master);
2469 sub quilt_fixup_editor () {
2470 my $descfn = $ENV{$fakeeditorenv};
2471 my $editing = $ARGV[$#ARGV];
2472 open I1, '<', $descfn or die "$descfn: $!";
2473 open I2, '<', $editing or die "$editing: $!";
2474 unlink $editing or die "$editing: $!";
2475 open O, '>', $editing or die "$editing: $!";
2476 while (<I1>) { print O or die $!; } I1->error and die $!;
2479 $copying ||= m/^\-\-\- /;
2480 next unless $copying;
2483 I2->error and die $!;
2488 #----- other building -----
2491 if ($cleanmode eq 'dpkg-source') {
2492 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
2493 } elsif ($cleanmode eq 'git') {
2494 runcmd_ordryrun_local @git, qw(clean -xdf);
2495 } elsif ($cleanmode eq 'none') {
2502 badusage "clean takes no additional arguments" if @ARGV;
2507 badusage "-p is not allowed when building" if defined $package;
2510 my $clogp = parsechangelog();
2511 $isuite = getfield $clogp, 'Distribution';
2512 $package = getfield $clogp, 'Source';
2513 $version = getfield $clogp, 'Version';
2514 build_maybe_quilt_fixup();
2517 sub changesopts () {
2518 my @opts =@changesopts[1..$#changesopts];
2519 if (!defined $changes_since_version) {
2520 my @vsns = archive_query('archive_query');
2521 my @quirk = access_quirk();
2522 if ($quirk[0] eq 'backports') {
2523 local $isuite = $quirk[2];
2525 canonicalise_suite();
2526 push @vsns, archive_query('archive_query');
2529 @vsns = map { $_->[0] } @vsns;
2530 @vsns = sort { -version_compare($a, $b) } @vsns;
2531 $changes_since_version = $vsns[0];
2532 progress "changelog will contain changes since $vsns[0]";
2534 $changes_since_version = '_';
2535 progress "package seems new, not specifying -v<version>";
2538 if ($changes_since_version ne '_') {
2539 unshift @opts, "-v$changes_since_version";
2546 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
2547 printdone "build successful\n";
2553 (qw(git-buildpackage -us -uc --git-no-sign-tags),
2554 "--git-builder=@dpkgbuildpackage");
2555 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
2556 canonicalise_suite();
2557 push @cmd, "--git-debian-branch=".lbranch();
2559 push @cmd, changesopts();
2560 runcmd_ordryrun_local @cmd, @ARGV;
2561 printdone "build successful\n";
2566 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
2567 $dscfn = dscfn($version);
2568 if ($cleanmode eq 'dpkg-source') {
2569 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
2572 my $pwd = must_getcwd();
2573 my $leafdir = basename $pwd;
2575 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
2577 runcmd_ordryrun_local qw(sh -ec),
2578 'exec >$1; shift; exec "$@"','x',
2579 "../$sourcechanges",
2580 @dpkggenchanges, qw(-S), changesopts();
2584 sub cmd_build_source {
2585 badusage "build-source takes no additional arguments" if @ARGV;
2587 printdone "source built, results in $dscfn and $sourcechanges";
2593 my $pat = "${package}_".(stripepoch $version)."_*.changes";
2595 stat_exist $dscfn or fail "$dscfn (in parent directory): $!";
2596 stat_exists $sourcechanges
2597 or fail "$sourcechanges (in parent directory): $!";
2598 foreach my $cf (glob $pat) {
2599 next if $cf eq $sourcechanges;
2600 unlink $cf or fail "remove $cf: $!";
2603 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
2604 my @changesfiles = glob $pat;
2605 @changesfiles = sort {
2606 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
2609 fail "wrong number of different changes files (@changesfiles)"
2610 unless @changesfiles;
2611 runcmd_ordryrun_local @mergechanges, @changesfiles;
2612 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
2614 stat_exists $multichanges or fail "$multichanges: $!";
2616 printdone "build successful, results in $multichanges\n" or die $!;
2619 sub cmd_quilt_fixup {
2620 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
2621 my $clogp = parsechangelog();
2622 $version = getfield $clogp, 'Version';
2623 $package = getfield $clogp, 'Source';
2624 build_maybe_quilt_fixup();
2627 sub cmd_archive_api_query {
2628 badusage "need only 1 subpath argument" unless @ARGV==1;
2629 my ($subpath) = @ARGV;
2630 my @cmd = archive_api_query_cmd($subpath);
2631 exec @cmd or fail "exec curl: $!\n";
2634 #---------- argument parsing and main program ----------
2637 print "dgit version $our_version\n" or die $!;
2644 if (defined $ENV{'DGIT_SSH'}) {
2645 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
2646 } elsif (defined $ENV{'GIT_SSH'}) {
2647 @ssh = ($ENV{'GIT_SSH'});
2651 last unless $ARGV[0] =~ m/^-/;
2655 if (m/^--dry-run$/) {
2658 } elsif (m/^--damp-run$/) {
2661 } elsif (m/^--no-sign$/) {
2664 } elsif (m/^--help$/) {
2666 } elsif (m/^--version$/) {
2668 } elsif (m/^--new$/) {
2671 } elsif (m/^--since-version=([^_]+|_)$/) {
2673 $changes_since_version = $1;
2674 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
2675 ($om = $opts_opt_map{$1}) &&
2679 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
2680 !$opts_opt_cmdonly{$1} &&
2681 ($om = $opts_opt_map{$1})) {
2684 } elsif (m/^--existing-package=(.*)/s) {
2686 $existing_package = $1;
2687 } elsif (m/^--initiator-tempdir=(.*)/s) {
2688 $initiator_tempdir = $1;
2689 $initiator_tempdir =~ m#^/# or
2690 badusage "--initiator-tempdir must be used specify an".
2691 " absolute, not relative, directory."
2692 } elsif (m/^--distro=(.*)/s) {
2695 } elsif (m/^--build-products-dir=(.*)/s) {
2697 $buildproductsdir = $1;
2698 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
2701 } elsif (m/^--clean=(.*)$/s) {
2702 badusage "unknown cleaning mode \`$1'";
2703 } elsif (m/^--quilt=($quilt_modes_re)$/s) {
2706 } elsif (m/^--quilt=(.*)$/s) {
2707 badusage "unknown quilt fixup mode \`$1'";
2708 } elsif (m/^--ignore-dirty$/s) {
2711 } elsif (m/^--no-quilt-fixup$/s) {
2713 $quilt_mode = 'nocheck';
2714 } elsif (m/^--no-rm-on-error$/s) {
2717 } elsif (m/^--deliberately-($suite_re)$/s) {
2719 push @deliberatelies, $&;
2721 badusage "unknown long option \`$_'";
2728 } elsif (s/^-L/-/) {
2731 } elsif (s/^-h/-/) {
2733 } elsif (s/^-D/-/) {
2735 open DEBUG, ">&STDERR" or die $!;
2738 } elsif (s/^-N/-/) {
2741 } elsif (s/^-v([^_]+|_)$//s) {
2743 $changes_since_version = $1;
2746 push @changesopts, $_;
2748 } elsif (s/^-c(.*=.*)//s) {
2750 push @git, '-c', $1;
2751 } elsif (s/^-d(.+)//s) {
2754 } elsif (s/^-C(.+)//s) {
2757 if ($changesfile =~ s#^(.*)/##) {
2758 $buildproductsdir = $1;
2760 } elsif (s/^-k(.+)//s) {
2762 } elsif (m/^-[vdCk]$/) {
2764 "option \`$_' requires an argument (and no space before the argument)";
2765 } elsif (s/^-wn$//s) {
2767 $cleanmode = 'none';
2768 } elsif (s/^-wg$//s) {
2771 } elsif (s/^-wd$//s) {
2773 $cleanmode = 'dpkg-source';
2775 badusage "unknown short option \`$_'";
2782 if ($ENV{$fakeeditorenv}) {
2783 quilt_fixup_editor();
2787 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2788 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2789 if $dryrun_level == 1;
2791 print STDERR $helpmsg or die $!;
2794 my $cmd = shift @ARGV;
2797 if (!defined $quilt_mode) {
2798 $quilt_mode = cfg('dgit.force.quilt-mode', 'RETURN-UNDEF')
2799 // access_cfg('quilt-mode', 'RETURN-UNDEF')
2801 $quilt_mode =~ m/^($quilt_modes_re)$/
2802 or badcfg "unknown quilt-mode \`$quilt_mode'";
2806 my $fn = ${*::}{"cmd_$cmd"};
2807 $fn or badusage "unknown operation $cmd";