3 # Integration between git and Debian-style archives
5 # Copyright (C)2013-2015 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/>.
28 use Dpkg::Control::Hash;
30 use File::Temp qw(tempdir);
40 our $our_version = 'UNRELEASED'; ###substituted###
42 our @rpushprotovsn_support = qw(3 2);
45 our $isuite = 'unstable';
51 our $dryrun_level = 0;
53 our $buildproductsdir = '..';
59 our $existing_package = 'dpkg';
61 our $changes_since_version;
64 our $quilt_modes_re = 'linear|smash|auto|nofix|nocheck';
65 our $we_are_responder;
66 our $initiator_tempdir;
68 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
70 our $suite_re = '[-+.0-9a-z]+';
71 our $cleanmode_re = 'dpkg-source(?:-d)?|git|git-ff|check|none';
74 our (@dget) = qw(dget);
75 our (@curl) = qw(curl -f);
76 our (@dput) = qw(dput);
77 our (@debsign) = qw(debsign);
79 our (@sbuild) = qw(sbuild);
81 our (@dgit) = qw(dgit);
82 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
83 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
84 our (@dpkggenchanges) = qw(dpkg-genchanges);
85 our (@mergechanges) = qw(mergechanges -f);
86 our (@changesopts) = ('');
88 our %opts_opt_map = ('dget' => \@dget, # accept for compatibility
91 'debsign' => \@debsign,
97 'dpkg-source' => \@dpkgsource,
98 'dpkg-buildpackage' => \@dpkgbuildpackage,
99 'dpkg-genchanges' => \@dpkggenchanges,
100 'ch' => \@changesopts,
101 'mergechanges' => \@mergechanges);
103 our %opts_opt_cmdonly = ('gpg' => 1, 'git' => 1);
104 our %opts_cfg_insertpos = map {
106 scalar @{ $opts_opt_map{$_} }
107 } keys %opts_opt_map;
109 sub finalise_opts_opts();
115 our $supplementary_message = '';
116 our $need_split_build_invocation = 0;
120 print STDERR "! $_\n" foreach $supplementary_message =~ m/^.+$/mg;
123 our $remotename = 'dgit';
124 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
128 sub lbranch () { return "$branchprefix/$csuite"; }
129 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
130 sub lref () { return "refs/heads/".lbranch(); }
131 sub lrref () { return "refs/remotes/$remotename/".server_branch($csuite); }
132 sub rrref () { return server_ref($csuite); }
134 sub lrfetchrefs () { return "refs/dgit-fetch/$csuite"; }
144 return "${package}_".(stripepoch $vsn).$sfx
149 return srcfn($vsn,".dsc");
152 sub changespat ($;$) {
153 my ($vsn, $arch) = @_;
154 return "${package}_".(stripepoch $vsn)."_".($arch//'*').".changes";
163 foreach my $f (@end) {
165 print STDERR "$us: cleanup: $@" if length $@;
169 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
171 sub no_such_package () {
172 print STDERR "$us: package $package does not exist in suite $isuite\n";
178 return "+".rrref().":".lrref();
183 printdebug "CD $newdir\n";
184 chdir $newdir or die "chdir: $newdir: $!";
187 sub deliberately ($) {
189 return !!grep { $_ eq "--deliberately-$enquiry" } @deliberatelies;
192 sub deliberately_not_fast_forward () {
193 foreach (qw(not-fast-forward fresh-repo)) {
194 return 1 if deliberately($_) || deliberately("TEST-dgit-only-$_");
198 #---------- remote protocol support, common ----------
200 # remote push initiator/responder protocol:
201 # $ dgit remote-push-build-host <n-rargs> <rargs>... <push-args>...
202 # where <rargs> is <push-host-dir> <supported-proto-vsn>,... ...
203 # < dgit-remote-push-ready <actual-proto-vsn>
205 # > file parsed-changelog
206 # [indicates that output of dpkg-parsechangelog follows]
207 # > data-block NBYTES
208 # > [NBYTES bytes of data (no newline)]
209 # [maybe some more blocks]
221 # [indicates that signed tag is wanted]
222 # < data-block NBYTES
223 # < [NBYTES bytes of data (no newline)]
224 # [maybe some more blocks]
228 # > want signed-dsc-changes
229 # < data-block NBYTES [transfer of signed dsc]
231 # < data-block NBYTES [transfer of signed changes]
239 sub i_child_report () {
240 # Sees if our child has died, and reap it if so. Returns a string
241 # describing how it died if it failed, or undef otherwise.
242 return undef unless $i_child_pid;
243 my $got = waitpid $i_child_pid, WNOHANG;
244 return undef if $got <= 0;
245 die unless $got == $i_child_pid;
246 $i_child_pid = undef;
247 return undef unless $?;
248 return "build host child ".waitstatusmsg();
253 fail "connection lost: $!" if $fh->error;
254 fail "protocol violation; $m not expected";
257 sub badproto_badread ($$) {
259 fail "connection lost: $!" if $!;
260 my $report = i_child_report();
261 fail $report if defined $report;
262 badproto $fh, "eof (reading $wh)";
265 sub protocol_expect (&$) {
266 my ($match, $fh) = @_;
269 defined && chomp or badproto_badread $fh, "protocol message";
277 badproto $fh, "\`$_'";
280 sub protocol_send_file ($$) {
281 my ($fh, $ourfn) = @_;
282 open PF, "<", $ourfn or die "$ourfn: $!";
285 my $got = read PF, $d, 65536;
286 die "$ourfn: $!" unless defined $got;
288 print $fh "data-block ".length($d)."\n" or die $!;
289 print $fh $d or die $!;
291 PF->error and die "$ourfn $!";
292 print $fh "data-end\n" or die $!;
296 sub protocol_read_bytes ($$) {
297 my ($fh, $nbytes) = @_;
298 $nbytes =~ m/^[1-9]\d{0,5}$|^0$/ or badproto \*RO, "bad byte count";
300 my $got = read $fh, $d, $nbytes;
301 $got==$nbytes or badproto_badread $fh, "data block";
305 sub protocol_receive_file ($$) {
306 my ($fh, $ourfn) = @_;
307 printdebug "() $ourfn\n";
308 open PF, ">", $ourfn or die "$ourfn: $!";
310 my ($y,$l) = protocol_expect {
311 m/^data-block (.*)$/ ? (1,$1) :
312 m/^data-end$/ ? (0,) :
316 my $d = protocol_read_bytes $fh, $l;
317 print PF $d or die $!;
322 #---------- remote protocol support, responder ----------
324 sub responder_send_command ($) {
326 return unless $we_are_responder;
327 # called even without $we_are_responder
328 printdebug ">> $command\n";
329 print PO $command, "\n" or die $!;
332 sub responder_send_file ($$) {
333 my ($keyword, $ourfn) = @_;
334 return unless $we_are_responder;
335 printdebug "]] $keyword $ourfn\n";
336 responder_send_command "file $keyword";
337 protocol_send_file \*PO, $ourfn;
340 sub responder_receive_files ($@) {
341 my ($keyword, @ourfns) = @_;
342 die unless $we_are_responder;
343 printdebug "[[ $keyword @ourfns\n";
344 responder_send_command "want $keyword";
345 foreach my $fn (@ourfns) {
346 protocol_receive_file \*PI, $fn;
349 protocol_expect { m/^files-end$/ } \*PI;
352 #---------- remote protocol support, initiator ----------
354 sub initiator_expect (&) {
356 protocol_expect { &$match } \*RO;
359 #---------- end remote code ----------
362 if ($we_are_responder) {
364 responder_send_command "progress ".length($m) or die $!;
365 print PO $m or die $!;
375 $ua = LWP::UserAgent->new();
379 progress "downloading $what...";
380 my $r = $ua->get(@_) or die $!;
381 return undef if $r->code == 404;
382 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
383 return $r->decoded_content(charset => 'none');
386 our ($dscdata,$dscurl,$dsc,$dsc_checked,$skew_warning_vsn);
391 failedcmd @_ if system @_;
394 sub act_local () { return $dryrun_level <= 1; }
395 sub act_scary () { return !$dryrun_level; }
398 if (!$dryrun_level) {
399 progress "dgit ok: @_";
401 progress "would be ok: @_ (but dry run only)";
406 printcmd(\*STDERR,$debugprefix."#",@_);
409 sub runcmd_ordryrun {
417 sub runcmd_ordryrun_local {
426 my ($first_shell, @cmd) = @_;
427 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
430 our $helpmsg = <<END;
432 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
433 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
434 dgit [dgit-opts] build [dpkg-buildpackage-opts]
435 dgit [dgit-opts] sbuild [sbuild-opts]
436 dgit [dgit-opts] push [dgit-opts] [suite]
437 dgit [dgit-opts] rpush build-host:build-dir ...
438 important dgit options:
439 -k<keyid> sign tag and package with <keyid> instead of default
440 --dry-run -n do not change anything, but go through the motions
441 --damp-run -L like --dry-run but make local changes, without signing
442 --new -N allow introducing a new package
443 --debug -D increase debug level
444 -c<name>=<value> set git config option (used directly by dgit too)
447 our $later_warning_msg = <<END;
448 Perhaps the upload is stuck in incoming. Using the version from git.
452 print STDERR "$us: @_\n", $helpmsg or die $!;
457 @ARGV or badusage "too few arguments";
458 return scalar shift @ARGV;
462 print $helpmsg or die $!;
466 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
468 our %defcfg = ('dgit.default.distro' => 'debian',
469 'dgit.default.username' => '',
470 'dgit.default.archive-query-default-component' => 'main',
471 'dgit.default.ssh' => 'ssh',
472 'dgit.default.archive-query' => 'madison:',
473 'dgit.default.sshpsql-dbname' => 'service=projectb',
474 'dgit-distro.debian.archive-query' => 'ftpmasterapi:',
475 'dgit-distro.debian.git-check' => 'url',
476 'dgit-distro.debian.git-check-suffix' => '/info/refs',
477 'dgit-distro.debian.new-private-pushers' => 't',
478 'dgit-distro.debian/push.git-url' => '',
479 'dgit-distro.debian/push.git-host' => 'push.dgit.debian.org',
480 'dgit-distro.debian/push.git-user-force' => 'dgit',
481 'dgit-distro.debian/push.git-proto' => 'git+ssh://',
482 'dgit-distro.debian/push.git-path' => '/dgit/debian/repos',
483 'dgit-distro.debian/push.git-create' => 'true',
484 'dgit-distro.debian/push.git-check' => 'ssh-cmd',
485 'dgit-distro.debian.archive-query-url', 'https://api.ftp-master.debian.org/',
486 # 'dgit-distro.debian.archive-query-tls-key',
487 # '/etc/ssl/certs/%HOST%.pem:/etc/dgit/%HOST%.pem',
488 # ^ this does not work because curl is broken nowadays
489 # Fixing #790093 properly will involve providing providing the key
490 # in some pacagke and maybe updating these paths.
492 # 'dgit-distro.debian.archive-query-tls-curl-args',
493 # '--ca-path=/etc/ssl/ca-debian',
494 # ^ this is a workaround but works (only) on DSA-administered machines
495 'dgit-distro.debian.git-url' => 'https://git.dgit.debian.org',
496 'dgit-distro.debian.git-url-suffix' => '',
497 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
498 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
499 'dgit-distro.debian.backports-quirk' => '(squeeze)-backports*',
500 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
501 'dgit-distro.ubuntu.git-check' => 'false',
502 'dgit-distro.ubuntu.mirror' => 'http://archive.ubuntu.com/ubuntu',
503 'dgit-distro.test-dummy.ssh' => "$td/ssh",
504 'dgit-distro.test-dummy.username' => "alice",
505 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
506 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
507 'dgit-distro.test-dummy.git-url' => "$td/git",
508 'dgit-distro.test-dummy.git-host' => "git",
509 'dgit-distro.test-dummy.git-path' => "$td/git",
510 'dgit-distro.test-dummy.archive-query' => "ftpmasterapi:",
511 'dgit-distro.test-dummy.archive-query-url' => "file://$td/aq/",
512 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
513 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
518 sub git_slurp_config () {
519 local ($debuglevel) = $debuglevel-2;
522 my @cmd = (@git, qw(config -z --get-regexp .*));
525 open GITS, "-|", @cmd or failedcmd @cmd;
528 printdebug "=> ", (messagequote $_), "\n";
530 push @{ $gitcfg{$`} }, $'; #';
534 or ($!==0 && $?==256)
538 sub git_get_config ($) {
541 printdebug"C $c ".(defined $l ? messagequote "'$l'" : "undef")."\n"
544 @$l==1 or badcfg "multiple values for $c" if @$l > 1;
550 return undef if $c =~ /RETURN-UNDEF/;
551 my $v = git_get_config($c);
552 return $v if defined $v;
553 my $dv = $defcfg{$c};
554 return $dv if defined $dv;
556 badcfg "need value for one of: @_\n".
557 "$us: distro or suite appears not to be (properly) supported";
560 sub access_basedistro () {
561 if (defined $idistro) {
564 return cfg("dgit-suite.$isuite.distro",
565 "dgit.default.distro");
569 sub access_quirk () {
570 # returns (quirk name, distro to use instead or undef, quirk-specific info)
571 my $basedistro = access_basedistro();
572 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
574 if (defined $backports_quirk) {
575 my $re = $backports_quirk;
576 $re =~ s/[^-0-9a-z_\%*()]/\\$&/ig;
578 $re =~ s/\%/([-0-9a-z_]+)/
579 or $re =~ m/[()]/ or badcfg "backports-quirk needs \% or ( )";
580 if ($isuite =~ m/^$re$/) {
581 return ('backports',"$basedistro-backports",$1);
584 return ('none',undef);
589 sub parse_cfg_bool ($$$) {
590 my ($what,$def,$v) = @_;
593 $v =~ m/^[ty1]/ ? 1 :
594 $v =~ m/^[fn0]/ ? 0 :
595 badcfg "$what needs t (true, y, 1) or f (false, n, 0) not \`$v'";
598 sub access_forpush_config () {
599 my $d = access_basedistro();
603 parse_cfg_bool('new-private-pushers', 0,
604 cfg("dgit-distro.$d.new-private-pushers",
607 my $v = cfg("dgit-distro.$d.readonly", 'RETURN-UNDEF');
610 $v =~ m/^[ty1]/ ? 0 : # force readonly, forpush = 0
611 $v =~ m/^[fn0]/ ? 1 : # force nonreadonly, forpush = 1
612 $v =~ m/^[a]/ ? '' : # auto, forpush = ''
613 badcfg "readonly needs t (true, y, 1) or f (false, n, 0) or a (auto)";
616 sub access_forpush () {
617 $access_forpush //= access_forpush_config();
618 return $access_forpush;
622 die "$access_forpush ?" if ($access_forpush // 1) ne 1;
623 badcfg "pushing but distro is configured readonly"
624 if access_forpush_config() eq '0';
626 $supplementary_message = <<'END' unless $we_are_responder;
627 Push failed, before we got started.
628 You can retry the push, after fixing the problem, if you like.
630 finalise_opts_opts();
634 finalise_opts_opts();
637 sub supplementary_message ($) {
639 if (!$we_are_responder) {
640 $supplementary_message = $msg;
642 } elsif ($protovsn >= 3) {
643 responder_send_command "supplementary-message ".length($msg)
645 print PO $msg or die $!;
649 sub access_distros () {
650 # Returns list of distros to try, in order
653 # 0. `instead of' distro name(s) we have been pointed to
654 # 1. the access_quirk distro, if any
655 # 2a. the user's specified distro, or failing that } basedistro
656 # 2b. the distro calculated from the suite }
657 my @l = access_basedistro();
659 my (undef,$quirkdistro) = access_quirk();
660 unshift @l, $quirkdistro;
661 unshift @l, $instead_distro;
662 @l = grep { defined } @l;
664 if (access_forpush()) {
665 @l = map { ("$_/push", $_) } @l;
670 sub access_cfg_cfgs (@) {
673 # The nesting of these loops determines the search order. We put
674 # the key loop on the outside so that we search all the distros
675 # for each key, before going on to the next key. That means that
676 # if access_cfg is called with a more specific, and then a less
677 # specific, key, an earlier distro can override the less specific
678 # without necessarily overriding any more specific keys. (If the
679 # distro wants to override the more specific keys it can simply do
680 # so; whereas if we did the loop the other way around, it would be
681 # impossible to for an earlier distro to override a less specific
682 # key but not the more specific ones without restating the unknown
683 # values of the more specific keys.
686 # We have to deal with RETURN-UNDEF specially, so that we don't
687 # terminate the search prematurely.
689 if (m/RETURN-UNDEF/) { push @rundef, $_; last; }
692 foreach my $d (access_distros()) {
693 push @cfgs, map { "dgit-distro.$d.$_" } @realkeys;
695 push @cfgs, map { "dgit.default.$_" } @realkeys;
702 my (@cfgs) = access_cfg_cfgs(@keys);
703 my $value = cfg(@cfgs);
707 sub access_cfg_bool ($$) {
708 my ($def, @keys) = @_;
709 parse_cfg_bool($keys[0], $def, access_cfg(@keys, 'RETURN-UNDEF'));
712 sub string_to_ssh ($) {
714 if ($spec =~ m/\s/) {
715 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
721 sub access_cfg_ssh () {
722 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
723 if (!defined $gitssh) {
726 return string_to_ssh $gitssh;
730 sub access_runeinfo ($) {
732 return ": dgit ".access_basedistro()." $info ;";
735 sub access_someuserhost ($) {
737 my $user = access_cfg("$some-user-force", 'RETURN-UNDEF');
738 defined($user) && length($user) or
739 $user = access_cfg("$some-user",'username');
740 my $host = access_cfg("$some-host");
741 return length($user) ? "$user\@$host" : $host;
744 sub access_gituserhost () {
745 return access_someuserhost('git');
748 sub access_giturl (;$) {
750 my $url = access_cfg('git-url','RETURN-UNDEF');
753 my $proto = access_cfg('git-proto', 'RETURN-UNDEF');
754 return undef unless defined $proto;
757 access_gituserhost().
758 access_cfg('git-path');
760 $suffix = access_cfg('git-url-suffix','RETURN-UNDEF');
763 return "$url/$package$suffix";
766 sub parsecontrolfh ($$;$) {
767 my ($fh, $desc, $allowsigned) = @_;
768 our $dpkgcontrolhash_noissigned;
771 my %opts = ('name' => $desc);
772 $opts{allow_pgp}= $allowsigned || !$dpkgcontrolhash_noissigned;
773 $c = Dpkg::Control::Hash->new(%opts);
774 $c->parse($fh,$desc) or die "parsing of $desc failed";
775 last if $allowsigned;
776 last if $dpkgcontrolhash_noissigned;
777 my $issigned= $c->get_option('is_pgp_signed');
778 if (!defined $issigned) {
779 $dpkgcontrolhash_noissigned= 1;
780 seek $fh, 0,0 or die "seek $desc: $!";
781 } elsif ($issigned) {
782 fail "control file $desc is (already) PGP-signed. ".
783 " Note that dgit push needs to modify the .dsc and then".
784 " do the signature itself";
793 my ($file, $desc) = @_;
794 my $fh = new IO::Handle;
795 open $fh, '<', $file or die "$file: $!";
796 my $c = parsecontrolfh($fh,$desc);
797 $fh->error and die $!;
803 my ($dctrl,$field) = @_;
804 my $v = $dctrl->{$field};
805 return $v if defined $v;
806 fail "missing field $field in ".$v->get_option('name');
810 my $c = Dpkg::Control::Hash->new();
811 my $p = new IO::Handle;
812 my @cmd = (qw(dpkg-parsechangelog), @_);
813 open $p, '-|', @cmd or die $!;
815 $?=0; $!=0; close $p or failedcmd @cmd;
821 defined $d or fail "getcwd failed: $!";
827 sub archive_query ($) {
829 my $query = access_cfg('archive-query','RETURN-UNDEF');
830 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
833 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
836 sub pool_dsc_subpath ($$) {
837 my ($vsn,$component) = @_; # $package is implict arg
838 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
839 return "/pool/$component/$prefix/$package/".dscfn($vsn);
842 #---------- `ftpmasterapi' archive query method (nascent) ----------
844 sub archive_api_query_cmd ($) {
846 my @cmd = qw(curl -sS);
847 my $url = access_cfg('archive-query-url');
848 if ($url =~ m#^https://([-.0-9a-z]+)/#) {
850 my $keys = access_cfg('archive-query-tls-key','RETURN-UNDEF') //'';
851 foreach my $key (split /\:/, $keys) {
852 $key =~ s/\%HOST\%/$host/g;
854 fail "for $url: stat $key: $!" unless $!==ENOENT;
857 fail "config requested specific TLS key but do not know".
858 " how to get curl to use exactly that EE key ($key)";
859 # push @cmd, "--cacert", $key, "--capath", "/dev/enoent";
860 # # Sadly the above line does not work because of changes
861 # # to gnutls. The real fix for #790093 may involve
862 # # new curl options.
865 # Fixing #790093 properly will involve providing a value
866 # for this on clients.
867 my $kargs = access_cfg('archive-query-tls-curl-ca-args','RETURN-UNDEF');
868 push @cmd, split / /, $kargs if defined $kargs;
870 push @cmd, $url.$subpath;
876 my ($data, $subpath) = @_;
877 badcfg "ftpmasterapi archive query method takes no data part"
879 my @cmd = archive_api_query_cmd($subpath);
880 my $json = cmdoutput @cmd;
881 return decode_json($json);
884 sub canonicalise_suite_ftpmasterapi () {
885 my ($proto,$data) = @_;
886 my $suites = api_query($data, 'suites');
888 foreach my $entry (@$suites) {
890 my $v = $entry->{$_};
891 defined $v && $v eq $isuite;
893 push @matched, $entry;
895 fail "unknown suite $isuite" unless @matched;
898 @matched==1 or die "multiple matches for suite $isuite\n";
899 $cn = "$matched[0]{codename}";
900 defined $cn or die "suite $isuite info has no codename\n";
901 $cn =~ m/^$suite_re$/ or die "suite $isuite maps to bad codename\n";
903 die "bad ftpmaster api response: $@\n".Dumper(\@matched)
908 sub archive_query_ftpmasterapi () {
909 my ($proto,$data) = @_;
910 my $info = api_query($data, "dsc_in_suite/$isuite/$package");
912 my $digester = Digest::SHA->new(256);
913 foreach my $entry (@$info) {
915 my $vsn = "$entry->{version}";
916 my ($ok,$msg) = version_check $vsn;
917 die "bad version: $msg\n" unless $ok;
918 my $component = "$entry->{component}";
919 $component =~ m/^$component_re$/ or die "bad component";
920 my $filename = "$entry->{filename}";
921 $filename && $filename !~ m#[^-+:._~0-9a-zA-Z/]|^[/.]|/[/.]#
922 or die "bad filename";
923 my $sha256sum = "$entry->{sha256sum}";
924 $sha256sum =~ m/^[0-9a-f]+$/ or die "bad sha256sum";
925 push @rows, [ $vsn, "/pool/$component/$filename",
926 $digester, $sha256sum ];
928 die "bad ftpmaster api response: $@\n".Dumper($entry)
931 @rows = sort { -version_compare($a->[0],$b->[0]) } @rows;
935 #---------- `madison' archive query method ----------
937 sub archive_query_madison {
938 return map { [ @$_[0..1] ] } madison_get_parse(@_);
941 sub madison_get_parse {
942 my ($proto,$data) = @_;
943 die unless $proto eq 'madison';
945 $data= access_cfg('madison-distro','RETURN-UNDEF');
946 $data //= access_basedistro();
948 $rmad{$proto,$data,$package} ||= cmdoutput
949 qw(rmadison -asource),"-s$isuite","-u$data",$package;
950 my $rmad = $rmad{$proto,$data,$package};
953 foreach my $l (split /\n/, $rmad) {
954 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
955 \s*( [^ \t|]+ )\s* \|
956 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
957 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
958 $1 eq $package or die "$rmad $package ?";
965 $component = access_cfg('archive-query-default-component');
967 $5 eq 'source' or die "$rmad ?";
968 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
970 return sort { -version_compare($a->[0],$b->[0]); } @out;
973 sub canonicalise_suite_madison {
974 # madison canonicalises for us
975 my @r = madison_get_parse(@_);
977 "unable to canonicalise suite using package $package".
978 " which does not appear to exist in suite $isuite;".
979 " --existing-package may help";
983 #---------- `sshpsql' archive query method ----------
986 my ($data,$runeinfo,$sql) = @_;
988 $data= access_someuserhost('sshpsql').':'.
989 access_cfg('sshpsql-dbname');
991 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
992 my ($userhost,$dbname) = ($`,$'); #';
994 my @cmd = (access_cfg_ssh, $userhost,
995 access_runeinfo("ssh-psql $runeinfo").
996 " export LC_MESSAGES=C; export LC_CTYPE=C;".
997 " ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
999 open P, "-|", @cmd or die $!;
1002 printdebug(">|$_|\n");
1005 $!=0; $?=0; close P or failedcmd @cmd;
1007 my $nrows = pop @rows;
1008 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
1009 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
1010 @rows = map { [ split /\|/, $_ ] } @rows;
1011 my $ncols = scalar @{ shift @rows };
1012 die if grep { scalar @$_ != $ncols } @rows;
1016 sub sql_injection_check {
1017 foreach (@_) { die "$_ $& ?" if m{[^-+=:_.,/0-9a-zA-Z]}; }
1020 sub archive_query_sshpsql ($$) {
1021 my ($proto,$data) = @_;
1022 sql_injection_check $isuite, $package;
1023 my @rows = sshpsql($data, "archive-query $isuite $package", <<END);
1024 SELECT source.version, component.name, files.filename, files.sha256sum
1026 JOIN src_associations ON source.id = src_associations.source
1027 JOIN suite ON suite.id = src_associations.suite
1028 JOIN dsc_files ON dsc_files.source = source.id
1029 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
1030 JOIN component ON component.id = files_archive_map.component_id
1031 JOIN files ON files.id = dsc_files.file
1032 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
1033 AND source.source='$package'
1034 AND files.filename LIKE '%.dsc';
1036 @rows = sort { -version_compare($a->[0],$b->[0]) } @rows;
1037 my $digester = Digest::SHA->new(256);
1039 my ($vsn,$component,$filename,$sha256sum) = @$_;
1040 [ $vsn, "/pool/$component/$filename",$digester,$sha256sum ];
1045 sub canonicalise_suite_sshpsql ($$) {
1046 my ($proto,$data) = @_;
1047 sql_injection_check $isuite;
1048 my @rows = sshpsql($data, "canonicalise-suite $isuite", <<END);
1049 SELECT suite.codename
1050 FROM suite where suite_name='$isuite' or codename='$isuite';
1052 @rows = map { $_->[0] } @rows;
1053 fail "unknown suite $isuite" unless @rows;
1054 die "ambiguous $isuite: @rows ?" if @rows>1;
1058 #---------- `dummycat' archive query method ----------
1060 sub canonicalise_suite_dummycat ($$) {
1061 my ($proto,$data) = @_;
1062 my $dpath = "$data/suite.$isuite";
1063 if (!open C, "<", $dpath) {
1064 $!==ENOENT or die "$dpath: $!";
1065 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
1069 chomp or die "$dpath: $!";
1071 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
1075 sub archive_query_dummycat ($$) {
1076 my ($proto,$data) = @_;
1077 canonicalise_suite();
1078 my $dpath = "$data/package.$csuite.$package";
1079 if (!open C, "<", $dpath) {
1080 $!==ENOENT or die "$dpath: $!";
1081 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
1089 printdebug "dummycat query $csuite $package $dpath | $_\n";
1090 my @row = split /\s+/, $_;
1091 @row==2 or die "$dpath: $_ ?";
1094 C->error and die "$dpath: $!";
1096 return sort { -version_compare($a->[0],$b->[0]); } @rows;
1099 #---------- archive query entrypoints and rest of program ----------
1101 sub canonicalise_suite () {
1102 return if defined $csuite;
1103 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
1104 $csuite = archive_query('canonicalise_suite');
1105 if ($isuite ne $csuite) {
1106 progress "canonical suite name for $isuite is $csuite";
1110 sub get_archive_dsc () {
1111 canonicalise_suite();
1112 my @vsns = archive_query('archive_query');
1113 foreach my $vinfo (@vsns) {
1114 my ($vsn,$subpath,$digester,$digest) = @$vinfo;
1115 $dscurl = access_cfg('mirror').$subpath;
1116 $dscdata = url_get($dscurl);
1118 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
1123 $digester->add($dscdata);
1124 my $got = $digester->hexdigest();
1126 fail "$dscurl has hash $got but".
1127 " archive told us to expect $digest";
1129 my $dscfh = new IO::File \$dscdata, '<' or die $!;
1130 printdebug Dumper($dscdata) if $debuglevel>1;
1131 $dsc = parsecontrolfh($dscfh,$dscurl,1);
1132 printdebug Dumper($dsc) if $debuglevel>1;
1133 my $fmt = getfield $dsc, 'Format';
1134 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
1135 $dsc_checked = !!$digester;
1141 sub check_for_git ();
1142 sub check_for_git () {
1144 my $how = access_cfg('git-check');
1145 if ($how eq 'ssh-cmd') {
1147 (access_cfg_ssh, access_gituserhost(),
1148 access_runeinfo("git-check $package").
1149 " set -e; cd ".access_cfg('git-path').";".
1150 " if test -d $package.git; then echo 1; else echo 0; fi");
1151 my $r= cmdoutput @cmd;
1152 if ($r =~ m/^divert (\w+)$/) {
1154 my ($usedistro,) = access_distros();
1155 # NB that if we are pushing, $usedistro will be $distro/push
1156 $instead_distro= cfg("dgit-distro.$usedistro.diverts.$divert");
1157 $instead_distro =~ s{^/}{ access_basedistro()."/" }e;
1158 progress "diverting to $divert (using config for $instead_distro)";
1159 return check_for_git();
1161 failedcmd @cmd unless $r =~ m/^[01]$/;
1163 } elsif ($how eq 'url') {
1164 my $prefix = access_cfg('git-check-url','git-url');
1165 my $suffix = access_cfg('git-check-suffix','git-suffix',
1166 'RETURN-UNDEF') // '.git';
1167 my $url = "$prefix/$package$suffix";
1168 my @cmd = (qw(curl -sS -I), $url);
1169 my $result = cmdoutput @cmd;
1170 $result =~ s/^\S+ 200 .*\n\r?\n//;
1171 # curl -sS -I with https_proxy prints
1172 # HTTP/1.0 200 Connection established
1173 $result =~ m/^\S+ (404|200) /s or
1174 fail "unexpected results from git check query - ".
1175 Dumper($prefix, $result);
1177 if ($code eq '404') {
1179 } elsif ($code eq '200') {
1184 } elsif ($how eq 'true') {
1186 } elsif ($how eq 'false') {
1189 badcfg "unknown git-check \`$how'";
1193 sub create_remote_git_repo () {
1194 my $how = access_cfg('git-create');
1195 if ($how eq 'ssh-cmd') {
1197 (access_cfg_ssh, access_gituserhost(),
1198 access_runeinfo("git-create $package").
1199 "set -e; cd ".access_cfg('git-path').";".
1200 " cp -a _template $package.git");
1201 } elsif ($how eq 'true') {
1204 badcfg "unknown git-create \`$how'";
1208 our ($dsc_hash,$lastpush_hash);
1210 our $ud = '.git/dgit/unpack';
1215 mkdir $ud or die $!;
1218 sub mktree_in_ud_here () {
1219 runcmd qw(git init -q);
1220 rmtree('.git/objects');
1221 symlink '../../../../objects','.git/objects' or die $!;
1224 sub git_write_tree () {
1225 my $tree = cmdoutput @git, qw(write-tree);
1226 $tree =~ m/^\w+$/ or die "$tree ?";
1230 sub remove_stray_gits () {
1231 my @gitscmd = qw(find -name .git -prune -print0);
1232 debugcmd "|",@gitscmd;
1233 open GITS, "-|", @gitscmd or failedcmd @gitscmd;
1238 print STDERR "$us: warning: removing from source package: ",
1239 (messagequote $_), "\n";
1243 $!=0; $?=0; close GITS or failedcmd @gitscmd;
1246 sub mktree_in_ud_from_only_subdir () {
1247 # changes into the subdir
1249 die unless @dirs==1;
1250 $dirs[0] =~ m#^([^/]+)/\.$# or die;
1254 remove_stray_gits();
1255 mktree_in_ud_here();
1256 my ($format, $fopts) = get_source_format();
1257 if (madformat($format)) {
1260 runcmd @git, qw(add -Af);
1261 my $tree=git_write_tree();
1262 return ($tree,$dir);
1265 sub dsc_files_info () {
1266 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
1267 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
1268 ['Files', 'Digest::MD5', 'new()']) {
1269 my ($fname, $module, $method) = @$csumi;
1270 my $field = $dsc->{$fname};
1271 next unless defined $field;
1272 eval "use $module; 1;" or die $@;
1274 foreach (split /\n/, $field) {
1276 m/^(\w+) (\d+) (\S+)$/ or
1277 fail "could not parse .dsc $fname line \`$_'";
1278 my $digester = eval "$module"."->$method;" or die $@;
1283 Digester => $digester,
1288 fail "missing any supported Checksums-* or Files field in ".
1289 $dsc->get_option('name');
1293 map { $_->{Filename} } dsc_files_info();
1296 sub is_orig_file ($;$) {
1299 m/\.orig(?:-\w+)?\.tar\.\w+$/ or return 0;
1300 defined $base or return 1;
1304 sub make_commit ($) {
1306 return cmdoutput @git, qw(hash-object -w -t commit), $file;
1309 sub clogp_authline ($) {
1311 my $author = getfield $clogp, 'Maintainer';
1312 $author =~ s#,.*##ms;
1313 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
1314 my $authline = "$author $date";
1315 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
1316 fail "unexpected commit author line format \`$authline'".
1317 " (was generated from changelog Maintainer field)";
1321 sub vendor_patches_distro ($$) {
1322 my ($checkdistro, $what) = @_;
1323 return unless defined $checkdistro;
1325 my $series = "debian/patches/\L$checkdistro\E.series";
1326 printdebug "checking for vendor-specific $series ($what)\n";
1328 if (!open SERIES, "<", $series) {
1329 die "$series $!" unless $!==ENOENT;
1338 Unfortunately, this source package uses a feature of dpkg-source where
1339 the same source package unpacks to different source code on different
1340 distros. dgit cannot safely operate on such packages on affected
1341 distros, because the meaning of source packages is not stable.
1343 Please ask the distro/maintainer to remove the distro-specific series
1344 files and use a different technique (if necessary, uploading actually
1345 different packages, if different distros are supposed to have
1349 fail "Found active distro-specific series file for".
1350 " $checkdistro ($what): $series, cannot continue";
1352 die "$series $!" if SERIES->error;
1356 sub check_for_vendor_patches () {
1357 # This dpkg-source feature doesn't seem to be documented anywhere!
1358 # But it can be found in the changelog (reformatted):
1360 # commit 4fa01b70df1dc4458daee306cfa1f987b69da58c
1361 # Author: Raphael Hertzog <hertzog@debian.org>
1362 # Date: Sun Oct 3 09:36:48 2010 +0200
1364 # dpkg-source: correctly create .pc/.quilt_series with alternate
1367 # If you have debian/patches/ubuntu.series and you were
1368 # unpacking the source package on ubuntu, quilt was still
1369 # directed to debian/patches/series instead of
1370 # debian/patches/ubuntu.series.
1372 # debian/changelog | 3 +++
1373 # scripts/Dpkg/Source/Package/V3/quilt.pm | 4 +++-
1374 # 2 files changed, 6 insertions(+), 1 deletion(-)
1377 vendor_patches_distro($ENV{DEB_VENDOR}, "DEB_VENDOR");
1378 vendor_patches_distro(Dpkg::Vendor::get_current_vendor(),
1379 "Dpkg::Vendor \`current vendor'");
1380 vendor_patches_distro(access_basedistro(),
1381 "distro being accessed");
1384 sub generate_commit_from_dsc () {
1388 foreach my $fi (dsc_files_info()) {
1389 my $f = $fi->{Filename};
1390 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
1392 link_ltarget "../../../$f", $f
1396 complete_file_from_dsc('.', $fi)
1399 if (is_orig_file($f)) {
1400 link $f, "../../../../$f"
1406 my $dscfn = "$package.dsc";
1408 open D, ">", $dscfn or die "$dscfn: $!";
1409 print D $dscdata or die "$dscfn: $!";
1410 close D or die "$dscfn: $!";
1411 my @cmd = qw(dpkg-source);
1412 push @cmd, '--no-check' if $dsc_checked;
1413 push @cmd, qw(-x --), $dscfn;
1416 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1417 check_for_vendor_patches() if madformat($dsc->{format});
1418 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
1419 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
1420 my $authline = clogp_authline $clogp;
1421 my $changes = getfield $clogp, 'Changes';
1422 open C, ">../commit.tmp" or die $!;
1423 print C <<END or die $!;
1430 # imported from the archive
1433 my $outputhash = make_commit qw(../commit.tmp);
1434 my $cversion = getfield $clogp, 'Version';
1435 progress "synthesised git commit from .dsc $cversion";
1436 if ($lastpush_hash) {
1437 runcmd @git, qw(reset --hard), $lastpush_hash;
1438 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
1439 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
1440 my $oversion = getfield $oldclogp, 'Version';
1442 version_compare($oversion, $cversion);
1444 # git upload/ is earlier vsn than archive, use archive
1445 open C, ">../commit2.tmp" or die $!;
1446 print C <<END or die $!;
1448 parent $lastpush_hash
1453 Record $package ($cversion) in archive suite $csuite
1455 $outputhash = make_commit qw(../commit2.tmp);
1456 } elsif ($vcmp > 0) {
1457 print STDERR <<END or die $!;
1459 Version actually in archive: $cversion (older)
1460 Last allegedly pushed/uploaded: $oversion (newer or same)
1463 $outputhash = $lastpush_hash;
1465 $outputhash = $lastpush_hash;
1468 changedir '../../../..';
1469 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1470 'DGIT_ARCHIVE', $outputhash;
1471 cmdoutput @git, qw(log -n2), $outputhash;
1472 # ... gives git a chance to complain if our commit is malformed
1477 sub complete_file_from_dsc ($$) {
1478 our ($dstdir, $fi) = @_;
1479 # Ensures that we have, in $dir, the file $fi, with the correct
1480 # contents. (Downloading it from alongside $dscurl if necessary.)
1482 my $f = $fi->{Filename};
1483 my $tf = "$dstdir/$f";
1486 if (stat_exists $tf) {
1487 progress "using existing $f";
1490 $furl =~ s{/[^/]+$}{};
1492 die "$f ?" unless $f =~ m/^\Q${package}\E_/;
1493 die "$f ?" if $f =~ m#/#;
1494 runcmd_ordryrun_local @curl,qw(-o),$tf,'--',"$furl";
1495 return 0 if !act_local();
1499 open F, "<", "$tf" or die "$tf: $!";
1500 $fi->{Digester}->reset();
1501 $fi->{Digester}->addfile(*F);
1502 F->error and die $!;
1503 my $got = $fi->{Digester}->hexdigest();
1504 $got eq $fi->{Hash} or
1505 fail "file $f has hash $got but .dsc".
1506 " demands hash $fi->{Hash} ".
1507 ($downloaded ? "(got wrong file from archive!)"
1508 : "(perhaps you should delete this file?)");
1513 sub ensure_we_have_orig () {
1514 foreach my $fi (dsc_files_info()) {
1515 my $f = $fi->{Filename};
1516 next unless is_orig_file($f);
1517 complete_file_from_dsc('..', $fi)
1522 sub git_fetch_us () {
1523 my @specs = (fetchspec());
1525 map { "+refs/$_/*:".lrfetchrefs."/$_/*" }
1527 runcmd_ordryrun_local @git, qw(fetch -p -n -q), access_giturl(), @specs;
1530 my $tagpat = debiantag('*',access_basedistro);
1532 git_for_each_ref("refs/tags/".$tagpat, sub {
1533 my ($objid,$objtype,$fullrefname,$reftail) = @_;
1534 printdebug "currently $fullrefname=$objid\n";
1535 $here{$fullrefname} = $objid;
1537 git_for_each_ref(lrfetchrefs."/tags/".$tagpat, sub {
1538 my ($objid,$objtype,$fullrefname,$reftail) = @_;
1539 my $lref = "refs".substr($fullrefname, length lrfetchrefs);
1540 printdebug "offered $lref=$objid\n";
1541 if (!defined $here{$lref}) {
1542 my @upd = (@git, qw(update-ref), $lref, $objid, '');
1543 runcmd_ordryrun_local @upd;
1544 } elsif ($here{$lref} eq $objid) {
1547 "Not updateting $lref from $here{$lref} to $objid.\n";
1552 sub fetch_from_archive () {
1553 # ensures that lrref() is what is actually in the archive,
1554 # one way or another
1558 foreach my $field (@ourdscfield) {
1559 $dsc_hash = $dsc->{$field};
1560 last if defined $dsc_hash;
1562 if (defined $dsc_hash) {
1563 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1565 progress "last upload to archive specified git hash";
1567 progress "last upload to archive has NO git hash";
1570 progress "no version available from the archive";
1573 $lastpush_hash = git_get_ref(lrref());
1574 printdebug "previous reference hash=$lastpush_hash\n";
1576 if (defined $dsc_hash) {
1577 fail "missing remote git history even though dsc has hash -".
1578 " could not find ref ".lrref().
1579 " (should have been fetched from ".access_giturl()."#".rrref().")"
1580 unless $lastpush_hash;
1582 ensure_we_have_orig();
1583 if ($dsc_hash eq $lastpush_hash) {
1584 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1585 print STDERR <<END or die $!;
1587 Git commit in archive is behind the last version allegedly pushed/uploaded.
1588 Commit referred to by archive: $dsc_hash
1589 Last allegedly pushed/uploaded: $lastpush_hash
1592 $hash = $lastpush_hash;
1594 fail "git head (".lrref()."=$lastpush_hash) is not a ".
1595 "descendant of archive's .dsc hash ($dsc_hash)";
1598 $hash = generate_commit_from_dsc();
1599 } elsif ($lastpush_hash) {
1600 # only in git, not in the archive yet
1601 $hash = $lastpush_hash;
1602 print STDERR <<END or die $!;
1604 Package not found in the archive, but has allegedly been pushed using dgit.
1608 printdebug "nothing found!\n";
1609 if (defined $skew_warning_vsn) {
1610 print STDERR <<END or die $!;
1612 Warning: relevant archive skew detected.
1613 Archive allegedly contains $skew_warning_vsn
1614 But we were not able to obtain any version from the archive or git.
1620 printdebug "current hash=$hash\n";
1621 if ($lastpush_hash) {
1622 fail "not fast forward on last upload branch!".
1623 " (archive's version left in DGIT_ARCHIVE)"
1624 unless is_fast_fwd($lastpush_hash, $hash);
1626 if (defined $skew_warning_vsn) {
1628 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1629 my $clogf = ".git/dgit/changelog.tmp";
1630 runcmd shell_cmd "exec >$clogf",
1631 @git, qw(cat-file blob), "$hash:debian/changelog";
1632 my $gotclogp = parsechangelog("-l$clogf");
1633 my $got_vsn = getfield $gotclogp, 'Version';
1634 printdebug "SKEW CHECK GOT $got_vsn\n";
1635 if (version_compare($got_vsn, $skew_warning_vsn) < 0) {
1636 print STDERR <<END or die $!;
1638 Warning: archive skew detected. Using the available version:
1639 Archive allegedly contains $skew_warning_vsn
1640 We were able to obtain only $got_vsn
1645 if ($lastpush_hash ne $hash) {
1646 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1650 dryrun_report @upd_cmd;
1656 sub set_local_git_config ($$) {
1658 runcmd @git, qw(config), $k, $v;
1661 sub setup_mergechangelogs (;$) {
1663 return unless $always || access_cfg_bool(1, 'setup-mergechangelogs');
1665 my $driver = 'dpkg-mergechangelogs';
1666 my $cb = "merge.$driver";
1667 my $attrs = '.git/info/attributes';
1668 ensuredir '.git/info';
1670 open NATTRS, ">", "$attrs.new" or die "$attrs.new $!";
1671 if (!open ATTRS, "<", $attrs) {
1672 $!==ENOENT or die "$attrs: $!";
1676 next if m{^debian/changelog\s};
1677 print NATTRS $_, "\n" or die $!;
1679 ATTRS->error and die $!;
1682 print NATTRS "debian/changelog merge=$driver\n" or die $!;
1685 set_local_git_config "$cb.name", 'debian/changelog merge driver';
1686 set_local_git_config "$cb.driver", 'dpkg-mergechangelogs -m %O %A %B %A';
1688 rename "$attrs.new", "$attrs" or die "$attrs: $!";
1691 sub setup_useremail (;$) {
1693 return unless $always || access_cfg_bool(1, 'setup-useremail');
1696 my ($k, $envvar) = @_;
1697 my $v = access_cfg("user-$k", 'RETURN-UNDEF') // $ENV{$envvar};
1698 return unless defined $v;
1699 set_local_git_config "user.$k", $v;
1702 $setup->('email', 'DEBEMAIL');
1703 $setup->('name', 'DEBFULLNAME');
1706 sub setup_new_tree () {
1707 setup_mergechangelogs();
1713 canonicalise_suite();
1714 badusage "dry run makes no sense with clone" unless act_local();
1715 my $hasgit = check_for_git();
1716 mkdir $dstdir or fail "create \`$dstdir': $!";
1718 runcmd @git, qw(init -q);
1719 my $giturl = access_giturl(1);
1720 if (defined $giturl) {
1721 set_local_git_config "remote.$remotename.fetch", fetchspec();
1722 open H, "> .git/HEAD" or die $!;
1723 print H "ref: ".lref()."\n" or die $!;
1725 runcmd @git, qw(remote add), 'origin', $giturl;
1728 progress "fetching existing git history";
1730 runcmd_ordryrun_local @git, qw(fetch origin);
1732 progress "starting new git history";
1734 fetch_from_archive() or no_such_package;
1735 my $vcsgiturl = $dsc->{'Vcs-Git'};
1736 if (length $vcsgiturl) {
1737 $vcsgiturl =~ s/\s+-b\s+\S+//g;
1738 runcmd @git, qw(remote add vcs-git), $vcsgiturl;
1741 runcmd @git, qw(reset --hard), lrref();
1742 printdone "ready for work in $dstdir";
1746 if (check_for_git()) {
1749 fetch_from_archive() or no_such_package();
1750 printdone "fetched into ".lrref();
1755 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1757 printdone "fetched to ".lrref()." and merged into HEAD";
1760 sub check_not_dirty () {
1761 foreach my $f (qw(local-options local-patch-header)) {
1762 if (stat_exists "debian/source/$f") {
1763 fail "git tree contains debian/source/$f";
1767 return if $ignoredirty;
1769 my @cmd = (@git, qw(diff --quiet HEAD));
1771 $!=0; $?=0; system @cmd;
1772 return if !$! && !$?;
1773 if (!$! && $?==256) {
1774 fail "working tree is dirty (does not match HEAD)";
1780 sub commit_admin ($) {
1783 runcmd_ordryrun_local @git, qw(commit -m), $m;
1786 sub commit_quilty_patch () {
1787 my $output = cmdoutput @git, qw(status --porcelain);
1789 foreach my $l (split /\n/, $output) {
1790 next unless $l =~ m/\S/;
1791 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1795 delete $adds{'.pc'}; # if there wasn't one before, don't add it
1797 progress "nothing quilty to commit, ok.";
1800 my @adds = map { s/[][*?\\]/\\$&/g; $_; } sort keys %adds;
1801 runcmd_ordryrun_local @git, qw(add -f), @adds;
1802 commit_admin "Commit Debian 3.0 (quilt) metadata";
1805 sub get_source_format () {
1807 if (open F, "debian/source/options") {
1811 s/\s+$//; # ignore missing final newline
1813 my ($k, $v) = ($`, $'); #');
1814 $v =~ s/^"(.*)"$/$1/;
1820 F->error and die $!;
1823 die $! unless $!==&ENOENT;
1826 if (!open F, "debian/source/format") {
1827 die $! unless $!==&ENOENT;
1831 F->error and die $!;
1833 return ($_, \%options);
1838 return 0 unless $format eq '3.0 (quilt)';
1839 if ($quilt_mode eq 'nocheck') {
1840 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1843 progress "Format \`$format', checking/updating patch stack";
1847 sub push_parse_changelog ($) {
1850 my $clogp = Dpkg::Control::Hash->new();
1851 $clogp->load($clogpfn) or die;
1853 $package = getfield $clogp, 'Source';
1854 my $cversion = getfield $clogp, 'Version';
1855 my $tag = debiantag($cversion, access_basedistro);
1856 runcmd @git, qw(check-ref-format), $tag;
1858 my $dscfn = dscfn($cversion);
1860 return ($clogp, $cversion, $tag, $dscfn);
1863 sub push_parse_dsc ($$$) {
1864 my ($dscfn,$dscfnwhat, $cversion) = @_;
1865 $dsc = parsecontrol($dscfn,$dscfnwhat);
1866 my $dversion = getfield $dsc, 'Version';
1867 my $dscpackage = getfield $dsc, 'Source';
1868 ($dscpackage eq $package && $dversion eq $cversion) or
1869 fail "$dscfn is for $dscpackage $dversion".
1870 " but debian/changelog is for $package $cversion";
1873 sub push_mktag ($$$$$$$) {
1874 my ($head,$clogp,$tag,
1876 $changesfile,$changesfilewhat,
1879 $dsc->{$ourdscfield[0]} = $head;
1880 $dsc->save("$dscfn.tmp") or die $!;
1882 my $changes = parsecontrol($changesfile,$changesfilewhat);
1883 foreach my $field (qw(Source Distribution Version)) {
1884 $changes->{$field} eq $clogp->{$field} or
1885 fail "changes field $field \`$changes->{$field}'".
1886 " does not match changelog \`$clogp->{$field}'";
1889 my $cversion = getfield $clogp, 'Version';
1890 my $clogsuite = getfield $clogp, 'Distribution';
1892 # We make the git tag by hand because (a) that makes it easier
1893 # to control the "tagger" (b) we can do remote signing
1894 my $authline = clogp_authline $clogp;
1895 my $delibs = join(" ", "",@deliberatelies);
1896 my $declaredistro = access_basedistro();
1897 open TO, '>', $tfn->('.tmp') or die $!;
1898 print TO <<END or die $!;
1904 $package release $cversion for $clogsuite ($csuite) [dgit]
1905 [dgit distro=$declaredistro$delibs]
1907 foreach my $ref (sort keys %previously) {
1908 print TO <<END or die $!;
1909 [dgit previously:$ref=$previously{$ref}]
1915 my $tagobjfn = $tfn->('.tmp');
1917 if (!defined $keyid) {
1918 $keyid = access_cfg('keyid','RETURN-UNDEF');
1920 if (!defined $keyid) {
1921 $keyid = getfield $clogp, 'Maintainer';
1923 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1924 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1925 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1926 push @sign_cmd, $tfn->('.tmp');
1927 runcmd_ordryrun @sign_cmd;
1929 $tagobjfn = $tfn->('.signed.tmp');
1930 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1931 $tfn->('.tmp'), $tfn->('.tmp.asc');
1938 sub sign_changes ($) {
1939 my ($changesfile) = @_;
1941 my @debsign_cmd = @debsign;
1942 push @debsign_cmd, "-k$keyid" if defined $keyid;
1943 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1944 push @debsign_cmd, $changesfile;
1945 runcmd_ordryrun @debsign_cmd;
1950 my ($forceflag) = @_;
1951 printdebug "actually entering push\n";
1952 supplementary_message(<<'END');
1953 Push failed, while preparing your push.
1954 You can retry the push, after fixing the problem, if you like.
1958 access_giturl(); # check that success is vaguely likely
1960 my $clogpfn = ".git/dgit/changelog.822.tmp";
1961 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1963 responder_send_file('parsed-changelog', $clogpfn);
1965 my ($clogp, $cversion, $tag, $dscfn) =
1966 push_parse_changelog("$clogpfn");
1968 my $dscpath = "$buildproductsdir/$dscfn";
1969 stat_exists $dscpath or
1970 fail "looked for .dsc $dscfn, but $!;".
1971 " maybe you forgot to build";
1973 responder_send_file('dsc', $dscpath);
1975 push_parse_dsc($dscpath, $dscfn, $cversion);
1977 my $format = getfield $dsc, 'Format';
1978 printdebug "format $format\n";
1979 if (madformat($format)) {
1980 # user might have not used dgit build, so maybe do this now:
1981 commit_quilty_patch();
1985 progress "checking that $dscfn corresponds to HEAD";
1986 runcmd qw(dpkg-source -x --),
1987 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1988 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1989 check_for_vendor_patches() if madformat($dsc->{format});
1990 changedir '../../../..';
1991 my $diffopt = $debuglevel>0 ? '--exit-code' : '--quiet';
1992 my @diffcmd = (@git, qw(diff), $diffopt, $tree);
1993 debugcmd "+",@diffcmd;
1995 my $r = system @diffcmd;
1998 fail "$dscfn specifies a different tree to your HEAD commit;".
1999 " perhaps you forgot to build".
2000 ($diffopt eq '--exit-code' ? "" :
2001 " (run with -D to see full diff output)");
2006 my $head = git_rev_parse('HEAD');
2007 if (!$changesfile) {
2008 my $pat = changespat $cversion;
2009 my @cs = glob "$buildproductsdir/$pat";
2010 fail "failed to find unique changes file".
2011 " (looked for $pat in $buildproductsdir);".
2012 " perhaps you need to use dgit -C"
2014 ($changesfile) = @cs;
2016 $changesfile = "$buildproductsdir/$changesfile";
2019 responder_send_file('changes',$changesfile);
2020 responder_send_command("param head $head");
2021 responder_send_command("param csuite $csuite");
2023 if (deliberately_not_fast_forward) {
2024 git_for_each_ref(lrfetchrefs, sub {
2025 my ($objid,$objtype,$lrfetchrefname,$reftail) = @_;
2026 my $rrefname= substr($lrfetchrefname, length(lrfetchrefs) + 1);
2027 responder_send_command("previously $rrefname=$objid");
2028 $previously{$rrefname} = $objid;
2032 my $tfn = sub { ".git/dgit/tag$_[0]"; };
2035 supplementary_message(<<'END');
2036 Push failed, while signing the tag.
2037 You can retry the push, after fixing the problem, if you like.
2039 # If we manage to sign but fail to record it anywhere, it's fine.
2040 if ($we_are_responder) {
2041 $tagobjfn = $tfn->('.signed.tmp');
2042 responder_receive_files('signed-tag', $tagobjfn);
2045 push_mktag($head,$clogp,$tag,
2047 $changesfile,$changesfile,
2050 supplementary_message(<<'END');
2051 Push failed, *after* signing the tag.
2052 If you want to try again, you should use a new version number.
2055 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
2056 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
2057 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
2059 supplementary_message(<<'END');
2060 Push failed, while updating the remote git repository - see messages above.
2061 If you want to try again, you should use a new version number.
2063 if (!check_for_git()) {
2064 create_remote_git_repo();
2066 runcmd_ordryrun @git, qw(push),access_giturl(),
2067 $forceflag."HEAD:".rrref(), $forceflag."refs/tags/$tag";
2068 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
2070 supplementary_message(<<'END');
2071 Push failed, after updating the remote git repository.
2072 If you want to try again, you must use a new version number.
2074 if ($we_are_responder) {
2075 my $dryrunsuffix = act_local() ? "" : ".tmp";
2076 responder_receive_files('signed-dsc-changes',
2077 "$dscpath$dryrunsuffix",
2078 "$changesfile$dryrunsuffix");
2081 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
2083 progress "[new .dsc left in $dscpath.tmp]";
2085 sign_changes $changesfile;
2088 supplementary_message(<<END);
2089 Push failed, while uploading package(s) to the archive server.
2090 You can retry the upload of exactly these same files with dput of:
2092 If that .changes file is broken, you will need to use a new version
2093 number for your next attempt at the upload.
2095 my $host = access_cfg('upload-host','RETURN-UNDEF');
2096 my @hostarg = defined($host) ? ($host,) : ();
2097 runcmd_ordryrun @dput, @hostarg, $changesfile;
2098 printdone "pushed and uploaded $cversion";
2100 supplementary_message('');
2101 responder_send_command("complete");
2108 badusage "-p is not allowed with clone; specify as argument instead"
2109 if defined $package;
2112 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
2113 ($package,$isuite) = @ARGV;
2114 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
2115 ($package,$dstdir) = @ARGV;
2116 } elsif (@ARGV==3) {
2117 ($package,$isuite,$dstdir) = @ARGV;
2119 badusage "incorrect arguments to dgit clone";
2121 $dstdir ||= "$package";
2123 if (stat_exists $dstdir) {
2124 fail "$dstdir already exists";
2128 if ($rmonerror && !$dryrun_level) {
2129 $cwd_remove= getcwd();
2131 return unless defined $cwd_remove;
2132 if (!chdir "$cwd_remove") {
2133 return if $!==&ENOENT;
2134 die "chdir $cwd_remove: $!";
2137 rmtree($dstdir) or die "remove $dstdir: $!\n";
2138 } elsif (!grep { $! == $_ }
2139 (ENOENT, ENOTDIR, EACCES, EPERM, ELOOP)) {
2141 print STDERR "check whether to remove $dstdir: $!\n";
2147 $cwd_remove = undef;
2150 sub branchsuite () {
2151 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
2152 if ($branch =~ m#$lbranch_re#o) {
2159 sub fetchpullargs () {
2161 if (!defined $package) {
2162 my $sourcep = parsecontrol('debian/control','debian/control');
2163 $package = getfield $sourcep, 'Source';
2166 # $isuite = branchsuite(); # this doesn't work because dak hates canons
2168 my $clogp = parsechangelog();
2169 $isuite = getfield $clogp, 'Distribution';
2171 canonicalise_suite();
2172 progress "fetching from suite $csuite";
2173 } elsif (@ARGV==1) {
2175 canonicalise_suite();
2177 badusage "incorrect arguments to dgit fetch or dgit pull";
2196 badusage "-p is not allowed with dgit push" if defined $package;
2198 my $clogp = parsechangelog();
2199 $package = getfield $clogp, 'Source';
2202 } elsif (@ARGV==1) {
2203 ($specsuite) = (@ARGV);
2205 badusage "incorrect arguments to dgit push";
2207 $isuite = getfield $clogp, 'Distribution';
2209 local ($package) = $existing_package; # this is a hack
2210 canonicalise_suite();
2212 canonicalise_suite();
2214 if (defined $specsuite &&
2215 $specsuite ne $isuite &&
2216 $specsuite ne $csuite) {
2217 fail "dgit push: changelog specifies $isuite ($csuite)".
2218 " but command line specifies $specsuite";
2220 supplementary_message(<<'END');
2221 Push failed, while checking state of the archive.
2222 You can retry the push, after fixing the problem, if you like.
2224 if (check_for_git()) {
2228 if (fetch_from_archive()) {
2229 if (is_fast_fwd(lrref(), 'HEAD')) {
2231 } elsif (deliberately_not_fast_forward) {
2234 fail "dgit push: HEAD is not a descendant".
2235 " of the archive's version.\n".
2236 "dgit: To overwrite its contents,".
2237 " use git merge -s ours ".lrref().".\n".
2238 "dgit: To rewind history, if permitted by the archive,".
2239 " use --deliberately-not-fast-forward";
2243 fail "package appears to be new in this suite;".
2244 " if this is intentional, use --new";
2249 #---------- remote commands' implementation ----------
2251 sub cmd_remote_push_build_host {
2252 my ($nrargs) = shift @ARGV;
2253 my (@rargs) = @ARGV[0..$nrargs-1];
2254 @ARGV = @ARGV[$nrargs..$#ARGV];
2256 my ($dir,$vsnwant) = @rargs;
2257 # vsnwant is a comma-separated list; we report which we have
2258 # chosen in our ready response (so other end can tell if they
2261 $we_are_responder = 1;
2262 $us .= " (build host)";
2266 open PI, "<&STDIN" or die $!;
2267 open STDIN, "/dev/null" or die $!;
2268 open PO, ">&STDOUT" or die $!;
2270 open STDOUT, ">&STDERR" or die $!;
2274 ($protovsn) = grep {
2275 $vsnwant =~ m{^(?:.*,)?$_(?:,.*)?$}
2276 } @rpushprotovsn_support;
2278 fail "build host has dgit rpush protocol versions ".
2279 (join ",", @rpushprotovsn_support).
2280 " but invocation host has $vsnwant"
2281 unless defined $protovsn;
2283 responder_send_command("dgit-remote-push-ready $protovsn");
2289 sub cmd_remote_push_responder { cmd_remote_push_build_host(); }
2290 # ... for compatibility with proto vsn.1 dgit (just so that user gets
2291 # a good error message)
2297 my $report = i_child_report();
2298 if (defined $report) {
2299 printdebug "($report)\n";
2300 } elsif ($i_child_pid) {
2301 printdebug "(killing build host child $i_child_pid)\n";
2302 kill 15, $i_child_pid;
2304 if (defined $i_tmp && !defined $initiator_tempdir) {
2306 eval { rmtree $i_tmp; };
2310 END { i_cleanup(); }
2313 my ($base,$selector,@args) = @_;
2314 $selector =~ s/\-/_/g;
2315 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
2322 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
2330 push @rargs, join ",", @rpushprotovsn_support;
2333 push @rdgit, @ropts;
2334 push @rdgit, qw(remote-push-build-host), (scalar @rargs), @rargs;
2336 my @cmd = (@ssh, $host, shellquote @rdgit);
2339 if (defined $initiator_tempdir) {
2340 rmtree $initiator_tempdir;
2341 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
2342 $i_tmp = $initiator_tempdir;
2346 $i_child_pid = open2(\*RO, \*RI, @cmd);
2348 ($protovsn) = initiator_expect { m/^dgit-remote-push-ready (\S+)/ };
2349 die "$protovsn ?" unless grep { $_ eq $protovsn } @rpushprotovsn_support;
2350 $supplementary_message = '' unless $protovsn >= 3;
2352 my ($icmd,$iargs) = initiator_expect {
2353 m/^(\S+)(?: (.*))?$/;
2356 i_method "i_resp", $icmd, $iargs;
2360 sub i_resp_progress ($) {
2362 my $msg = protocol_read_bytes \*RO, $rhs;
2366 sub i_resp_supplementary_message ($) {
2368 $supplementary_message = protocol_read_bytes \*RO, $rhs;
2371 sub i_resp_complete {
2372 my $pid = $i_child_pid;
2373 $i_child_pid = undef; # prevents killing some other process with same pid
2374 printdebug "waiting for build host child $pid...\n";
2375 my $got = waitpid $pid, 0;
2376 die $! unless $got == $pid;
2377 die "build host child failed $?" if $?;
2380 printdebug "all done\n";
2384 sub i_resp_file ($) {
2386 my $localname = i_method "i_localname", $keyword;
2387 my $localpath = "$i_tmp/$localname";
2388 stat_exists $localpath and
2389 badproto \*RO, "file $keyword ($localpath) twice";
2390 protocol_receive_file \*RO, $localpath;
2391 i_method "i_file", $keyword;
2396 sub i_resp_param ($) {
2397 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
2401 sub i_resp_previously ($) {
2402 $_[0] =~ m#^(refs/tags/\S+)=(\w+)$#
2403 or badproto \*RO, "bad previously spec";
2404 my $r = system qw(git check-ref-format), $1;
2405 die "bad previously ref spec ($r)" if $r;
2406 $previously{$1} = $2;
2411 sub i_resp_want ($) {
2413 die "$keyword ?" if $i_wanted{$keyword}++;
2414 my @localpaths = i_method "i_want", $keyword;
2415 printdebug "[[ $keyword @localpaths\n";
2416 foreach my $localpath (@localpaths) {
2417 protocol_send_file \*RI, $localpath;
2419 print RI "files-end\n" or die $!;
2422 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
2424 sub i_localname_parsed_changelog {
2425 return "remote-changelog.822";
2427 sub i_file_parsed_changelog {
2428 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
2429 push_parse_changelog "$i_tmp/remote-changelog.822";
2430 die if $i_dscfn =~ m#/|^\W#;
2433 sub i_localname_dsc {
2434 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
2439 sub i_localname_changes {
2440 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
2441 $i_changesfn = $i_dscfn;
2442 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
2443 return $i_changesfn;
2445 sub i_file_changes { }
2447 sub i_want_signed_tag {
2448 printdebug Dumper(\%i_param, $i_dscfn);
2449 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
2450 && defined $i_param{'csuite'}
2451 or badproto \*RO, "premature desire for signed-tag";
2452 my $head = $i_param{'head'};
2453 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
2455 die unless $i_param{'csuite'} =~ m/^$suite_re$/;
2457 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
2460 push_mktag $head, $i_clogp, $i_tag,
2462 $i_changesfn, 'remote changes',
2463 sub { "tag$_[0]"; };
2468 sub i_want_signed_dsc_changes {
2469 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
2470 sign_changes $i_changesfn;
2471 return ($i_dscfn, $i_changesfn);
2474 #---------- building etc. ----------
2480 #----- `3.0 (quilt)' handling -----
2482 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
2484 sub quiltify_dpkg_commit ($$$;$) {
2485 my ($patchname,$author,$msg, $xinfo) = @_;
2489 my $descfn = ".git/dgit/quilt-description.tmp";
2490 open O, '>', $descfn or die "$descfn: $!";
2493 $msg =~ s/^\s+$/ ./mg;
2494 print O <<END or die $!;
2504 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
2505 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
2506 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
2507 runcmd @dpkgsource, qw(--commit .), $patchname;
2511 sub quiltify_trees_differ ($$) {
2513 # returns 1 iff the two tree objects differ other than in debian/
2515 my @cmd = (@git, qw(diff-tree --name-only -z), $x, $y);
2516 my $diffs= cmdoutput @cmd;
2517 foreach my $f (split /\0/, $diffs) {
2518 next if $f eq 'debian';
2524 sub quiltify_tree_sentinelfiles ($) {
2525 # lists the `sentinel' files present in the tree
2527 my $r = cmdoutput @git, qw(ls-tree --name-only), $x,
2528 qw(-- debian/rules debian/control);
2534 my ($clogp,$target) = @_;
2536 # Quilt patchification algorithm
2538 # We search backwards through the history of the main tree's HEAD
2539 # (T) looking for a start commit S whose tree object is identical
2540 # to to the patch tip tree (ie the tree corresponding to the
2541 # current dpkg-committed patch series). For these purposes
2542 # `identical' disregards anything in debian/ - this wrinkle is
2543 # necessary because dpkg-source treates debian/ specially.
2545 # We can only traverse edges where at most one of the ancestors'
2546 # trees differs (in changes outside in debian/). And we cannot
2547 # handle edges which change .pc/ or debian/patches. To avoid
2548 # going down a rathole we avoid traversing edges which introduce
2549 # debian/rules or debian/control. And we set a limit on the
2550 # number of edges we are willing to look at.
2552 # If we succeed, we walk forwards again. For each traversed edge
2553 # PC (with P parent, C child) (starting with P=S and ending with
2554 # C=T) to we do this:
2556 # - dpkg-source --commit with a patch name and message derived from C
2557 # After traversing PT, we git commit the changes which
2558 # should be contained within debian/patches.
2560 changedir '../fake';
2561 remove_stray_gits();
2562 mktree_in_ud_here();
2564 runcmd @git, qw(add -Af .);
2565 my $oldtiptree=git_write_tree();
2566 changedir '../work';
2568 # The search for the path S..T is breadth-first. We maintain a
2569 # todo list containing search nodes. A search node identifies a
2570 # commit, and looks something like this:
2572 # Commit => $git_commit_id,
2573 # Child => $c, # or undef if P=T
2574 # Whynot => $reason_edge_PC_unsuitable, # in @nots only
2575 # Nontrivial => true iff $p..$c has relevant changes
2582 my %considered; # saves being exponential on some weird graphs
2584 my $t_sentinels = quiltify_tree_sentinelfiles $target;
2587 my ($search,$whynot) = @_;
2588 printdebug " search NOT $search->{Commit} $whynot\n";
2589 $search->{Whynot} = $whynot;
2590 push @nots, $search;
2591 no warnings qw(exiting);
2600 my $c = shift @todo;
2601 next if $considered{$c->{Commit}}++;
2603 $not->($c, "maximum search space exceeded") if --$max_work <= 0;
2605 printdebug "quiltify investigate $c->{Commit}\n";
2608 if (!quiltify_trees_differ $c->{Commit}, $oldtiptree) {
2609 printdebug " search finished hooray!\n";
2614 if ($quilt_mode eq 'nofix') {
2615 fail "quilt fixup required but quilt mode is \`nofix'\n".
2616 "HEAD commit $c->{Commit} differs from tree implied by ".
2617 " debian/patches (tree object $oldtiptree)";
2619 if ($quilt_mode eq 'smash') {
2620 printdebug " search quitting smash\n";
2624 my $c_sentinels = quiltify_tree_sentinelfiles $c->{Commit};
2625 $not->($c, "has $c_sentinels not $t_sentinels")
2626 if $c_sentinels ne $t_sentinels;
2628 my $commitdata = cmdoutput @git, qw(cat-file commit), $c->{Commit};
2629 $commitdata =~ m/\n\n/;
2631 my @parents = ($commitdata =~ m/^parent (\w+)$/gm);
2632 @parents = map { { Commit => $_, Child => $c } } @parents;
2634 $not->($c, "root commit") if !@parents;
2636 foreach my $p (@parents) {
2637 $p->{Nontrivial}= quiltify_trees_differ $p->{Commit},$c->{Commit};
2639 my $ndiffers = grep { $_->{Nontrivial} } @parents;
2640 $not->($c, "merge ($ndiffers nontrivial parents)") if $ndiffers > 1;
2642 foreach my $p (@parents) {
2643 printdebug "considering C=$c->{Commit} P=$p->{Commit}\n";
2645 my @cmd= (@git, qw(diff-tree -r --name-only),
2646 $p->{Commit},$c->{Commit}, qw(-- debian/patches .pc));
2647 my $patchstackchange = cmdoutput @cmd;
2648 if (length $patchstackchange) {
2649 $patchstackchange =~ s/\n/,/g;
2650 $not->($p, "changed $patchstackchange");
2653 printdebug " search queue P=$p->{Commit} ",
2654 ($p->{Nontrivial} ? "NT" : "triv"),"\n";
2660 printdebug "quiltify want to smash\n";
2663 my $x = $_[0]{Commit};
2664 $x =~ s/(.*?[0-9a-z]{8})[0-9a-z]*$/$1/;
2667 my $reportnot = sub {
2669 my $s = $abbrev->($notp);
2670 my $c = $notp->{Child};
2671 $s .= "..".$abbrev->($c) if $c;
2672 $s .= ": ".$notp->{Whynot};
2675 if ($quilt_mode eq 'linear') {
2676 print STDERR "$us: quilt fixup cannot be linear. Stopped at:\n";
2677 foreach my $notp (@nots) {
2678 print STDERR "$us: ", $reportnot->($notp), "\n";
2680 fail "quilt fixup naive history linearisation failed.\n".
2681 "Use dpkg-source --commit by hand; or, --quilt=smash for one ugly patch";
2682 } elsif ($quilt_mode eq 'smash') {
2683 } elsif ($quilt_mode eq 'auto') {
2684 progress "quilt fixup cannot be linear, smashing...";
2686 die "$quilt_mode ?";
2691 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
2693 quiltify_dpkg_commit "auto-$version-$target-$time",
2694 (getfield $clogp, 'Maintainer'),
2695 "Automatically generated patch ($clogp->{Version})\n".
2696 "Last (up to) $ncommits git changes, FYI:\n\n". $msg;
2700 progress "quiltify linearisation planning successful, executing...";
2702 for (my $p = $sref_S;
2703 my $c = $p->{Child};
2705 printdebug "quiltify traverse $p->{Commit}..$c->{Commit}\n";
2706 next unless $p->{Nontrivial};
2708 my $cc = $c->{Commit};
2710 my $commitdata = cmdoutput @git, qw(cat-file commit), $cc;
2711 $commitdata =~ m/\n\n/ or die "$c ?";
2714 $commitdata =~ m/^author (.*) \d+ [-+0-9]+$/m or die "$cc ?";
2717 $msg =~ s/^(.*)\n*/$1\n/ or die "$cc $msg ?";
2720 my $patchname = $title;
2721 $patchname =~ s/[.:]$//;
2722 $patchname =~ y/ A-Z/-a-z/;
2723 $patchname =~ y/-a-z0-9_.+=~//cd;
2724 $patchname =~ s/^\W/x-$&/;
2725 $patchname = substr($patchname,0,40);
2728 stat "debian/patches/$patchname$index";
2730 $!==ENOENT or die "$patchname$index $!";
2732 runcmd @git, qw(checkout -q), $cc;
2734 # We use the tip's changelog so that dpkg-source doesn't
2735 # produce complaining messages from dpkg-parsechangelog. None
2736 # of the information dpkg-source gets from the changelog is
2737 # actually relevant - it gets put into the original message
2738 # which dpkg-source provides our stunt editor, and then
2740 runcmd @git, qw(checkout -q), $target, qw(debian/changelog);
2742 quiltify_dpkg_commit "$patchname$index", $author, $msg,
2743 "X-Dgit-Generated: $clogp->{Version} $cc\n";
2745 runcmd @git, qw(checkout -q), $cc, qw(debian/changelog);
2748 runcmd @git, qw(checkout -q master);
2751 sub build_maybe_quilt_fixup () {
2752 my ($format,$fopts) = get_source_format;
2753 return unless madformat $format;
2756 check_for_vendor_patches();
2758 my $clogp = parsechangelog();
2759 my $headref = git_rev_parse('HEAD');
2764 my $upstreamversion=$version;
2765 $upstreamversion =~ s/-[^-]*$//;
2767 if ($fopts->{'single-debian-patch'}) {
2768 quilt_fixup_singlepatch($clogp, $headref, $upstreamversion);
2770 quilt_fixup_multipatch($clogp, $headref, $upstreamversion);
2773 changedir '../../../..';
2774 runcmd_ordryrun_local
2775 @git, qw(pull --ff-only -q .git/dgit/unpack/work master);
2778 sub quilt_fixup_mkwork ($) {
2781 mkdir "work" or die $!;
2783 mktree_in_ud_here();
2784 runcmd @git, qw(reset -q --hard), $headref;
2787 sub quilt_fixup_linkorigs ($$) {
2788 my ($upstreamversion, $fn) = @_;
2789 # calls $fn->($leafname);
2791 foreach my $f (<../../../../*>) { #/){
2792 my $b=$f; $b =~ s{.*/}{};
2794 local ($debuglevel) = $debuglevel-1;
2795 printdebug "QF linkorigs $b, $f ?\n";
2797 next unless is_orig_file $b, srcfn $upstreamversion,'';
2798 printdebug "QF linkorigs $b, $f Y\n";
2799 link_ltarget $f, $b or die "$b $!";
2804 sub quilt_fixup_delete_pc () {
2805 runcmd @git, qw(rm -rqf .pc);
2806 commit_admin "Commit removal of .pc (quilt series tracking data)";
2809 sub quilt_fixup_singlepatch ($$$) {
2810 my ($clogp, $headref, $upstreamversion) = @_;
2812 progress "starting quiltify (single-debian-patch)";
2814 # dpkg-source --commit generates new patches even if
2815 # single-debian-patch is in debian/source/options. In order to
2816 # get it to generate debian/patches/debian-changes, it is
2817 # necessary to build the source package.
2819 quilt_fixup_linkorigs($upstreamversion, sub { });
2820 quilt_fixup_mkwork($headref);
2822 rmtree("debian/patches");
2824 runcmd @dpkgsource, qw(-b .);
2826 runcmd @dpkgsource, qw(-x), (srcfn $version, ".dsc");
2827 rename srcfn("$upstreamversion", "/debian/patches"),
2828 "work/debian/patches";
2831 commit_quilty_patch();
2836 sub quilt_fixup_multipatch ($$$) {
2837 my ($clogp, $headref, $upstreamversion) = @_;
2839 progress "starting quiltify (multiple patches, $quilt_mode mode)";
2842 # - honour any existing .pc in case it has any strangeness
2843 # - determine the git commit corresponding to the tip of
2844 # the patch stack (if there is one)
2845 # - if there is such a git commit, convert each subsequent
2846 # git commit into a quilt patch with dpkg-source --commit
2847 # - otherwise convert all the differences in the tree into
2848 # a single git commit
2852 # Our git tree doesn't necessarily contain .pc. (Some versions of
2853 # dgit would include the .pc in the git tree.) If there isn't
2854 # one, we need to generate one by unpacking the patches that we
2857 # We first look for a .pc in the git tree. If there is one, we
2858 # will use it. (This is not the normal case.)
2860 # Otherwise need to regenerate .pc so that dpkg-source --commit
2861 # can work. We do this as follows:
2862 # 1. Collect all relevant .orig from parent directory
2863 # 2. Generate a debian.tar.gz out of
2864 # debian/{patches,rules,source/format,source/options}
2865 # 3. Generate a fake .dsc containing just these fields:
2866 # Format Source Version Files
2867 # 4. Extract the fake .dsc
2868 # Now the fake .dsc has a .pc directory.
2869 # (In fact we do this in every case, because in future we will
2870 # want to search for a good base commit for generating patches.)
2872 # Then we can actually do the dpkg-source --commit
2873 # 1. Make a new working tree with the same object
2874 # store as our main tree and check out the main
2876 # 2. Copy .pc from the fake's extraction, if necessary
2877 # 3. Run dpkg-source --commit
2878 # 4. If the result has changes to debian/, then
2879 # - git-add them them
2880 # - git-add .pc if we had a .pc in-tree
2882 # 5. If we had a .pc in-tree, delete it, and git-commit
2883 # 6. Back in the main tree, fast forward to the new HEAD
2885 my $fakeversion="$upstreamversion-~~DGITFAKE";
2887 my $fakedsc=new IO::File 'fake.dsc', '>' or die $!;
2888 print $fakedsc <<END or die $!;
2891 Version: $fakeversion
2895 my $dscaddfile=sub {
2898 my $md = new Digest::MD5;
2900 my $fh = new IO::File $b, '<' or die "$b $!";
2905 print $fakedsc " ".$md->hexdigest." $size $b\n" or die $!;
2908 quilt_fixup_linkorigs($upstreamversion, $dscaddfile);
2910 my @files=qw(debian/source/format debian/rules);
2911 foreach my $maybe (qw(debian/patches debian/source/options)) {
2912 next unless stat_exists "../../../$maybe";
2913 push @files, $maybe;
2916 my $debtar= srcfn $fakeversion,'.debian.tar.gz';
2917 runcmd qw(env GZIP=-1 tar -zcf), "./$debtar", qw(-C ../../..), @files;
2919 $dscaddfile->($debtar);
2920 close $fakedsc or die $!;
2922 runcmd qw(sh -ec), 'exec dpkg-source --no-check -x fake.dsc >/dev/null';
2924 my $fakexdir= $package.'-'.(stripepoch $upstreamversion);
2925 rename $fakexdir, "fake" or die "$fakexdir $!";
2927 quilt_fixup_mkwork($headref);
2930 if (stat_exists ".pc") {
2932 progress "Tree already contains .pc - will use it then delete it.";
2935 rename '../fake/.pc','.pc' or die $!;
2938 quiltify($clogp,$headref);
2940 if (!open P, '>>', ".pc/applied-patches") {
2941 $!==&ENOENT or die $!;
2946 commit_quilty_patch();
2948 if ($mustdeletepc) {
2949 quilt_fixup_delete_pc();
2953 sub quilt_fixup_editor () {
2954 my $descfn = $ENV{$fakeeditorenv};
2955 my $editing = $ARGV[$#ARGV];
2956 open I1, '<', $descfn or die "$descfn: $!";
2957 open I2, '<', $editing or die "$editing: $!";
2958 unlink $editing or die "$editing: $!";
2959 open O, '>', $editing or die "$editing: $!";
2960 while (<I1>) { print O or die $!; } I1->error and die $!;
2963 $copying ||= m/^\-\-\- /;
2964 next unless $copying;
2967 I2->error and die $!;
2972 #----- other building -----
2974 our $suppress_clean;
2977 return if $suppress_clean;
2978 if ($cleanmode eq 'dpkg-source') {
2979 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
2980 } elsif ($cleanmode eq 'dpkg-source-d') {
2981 runcmd_ordryrun_local @dpkgbuildpackage, qw(-d -T clean);
2982 } elsif ($cleanmode eq 'git') {
2983 runcmd_ordryrun_local @git, qw(clean -xdf);
2984 } elsif ($cleanmode eq 'git-ff') {
2985 runcmd_ordryrun_local @git, qw(clean -xdff);
2986 } elsif ($cleanmode eq 'check') {
2987 my $leftovers = cmdoutput @git, qw(clean -xdn);
2988 if (length $leftovers) {
2989 print STDERR $leftovers, "\n" or die $!;
2990 fail "tree contains uncommitted files and --clean=check specified";
2992 } elsif ($cleanmode eq 'none') {
2999 badusage "clean takes no additional arguments" if @ARGV;
3006 badusage "-p is not allowed when building" if defined $package;
3009 my $clogp = parsechangelog();
3010 $isuite = getfield $clogp, 'Distribution';
3011 $package = getfield $clogp, 'Source';
3012 $version = getfield $clogp, 'Version';
3013 build_maybe_quilt_fixup();
3015 my $pat = changespat $version;
3016 foreach my $f (glob "$buildproductsdir/$pat") {
3018 unlink $f or fail "remove old changes file $f: $!";
3020 progress "would remove $f";
3026 sub changesopts_initial () {
3027 my @opts =@changesopts[1..$#changesopts];
3030 sub changesopts_version () {
3031 if (!defined $changes_since_version) {
3032 my @vsns = archive_query('archive_query');
3033 my @quirk = access_quirk();
3034 if ($quirk[0] eq 'backports') {
3035 local $isuite = $quirk[2];
3037 canonicalise_suite();
3038 push @vsns, archive_query('archive_query');
3041 @vsns = map { $_->[0] } @vsns;
3042 @vsns = sort { -version_compare($a, $b) } @vsns;
3043 $changes_since_version = $vsns[0];
3044 progress "changelog will contain changes since $vsns[0]";
3046 $changes_since_version = '_';
3047 progress "package seems new, not specifying -v<version>";
3050 if ($changes_since_version ne '_') {
3051 return ("-v$changes_since_version");
3057 sub changesopts () {
3058 return (changesopts_initial(), changesopts_version());
3061 sub massage_dbp_args ($;$) {
3062 my ($cmd,$xargs) = @_;
3065 # - if we're going to split the source build out so we can
3066 # do strange things to it, massage the arguments to dpkg-buildpackage
3067 # so that the main build doessn't build source (or add an argument
3068 # to stop it building source by default).
3070 # - add -nc to stop dpkg-source cleaning the source tree,
3071 # unless we're not doing a split build and want dpkg-source
3072 # as cleanmode, in which case we can do nothing
3075 # 0 - source will NOT need to be built separately by caller
3076 # +1 - source will need to be built separately by caller
3077 # +2 - source will need to be built separately by caller AND
3078 # dpkg-buildpackage should not in fact be run at all!
3079 debugcmd '#massaging#', @$cmd if $debuglevel>1;
3080 #print STDERR "MASS0 ",Dumper($cmd, $xargs, $need_split_build_invocation);
3081 if ($cleanmode eq 'dpkg-source' && !$need_split_build_invocation) {
3082 $suppress_clean = 1;
3085 # -nc has the side effect of specifying -b if nothing else specified
3086 # and some combinations of -S, -b, et al, are errors, rather than
3087 # later simply overriding earlie. So we need to:
3088 # - search the command line for these options
3089 # - pick the last one
3090 # - perhaps add our own as a default
3091 # - perhaps adjust it to the corresponding non-source-building version
3093 foreach my $l ($cmd, $xargs) {
3095 @$l = grep { !(m/^-[SgGFABb]$/s and $dmode=$_) } @$l;
3098 #print STDERR "MASS1 ",Dumper($cmd, $xargs, $dmode);
3100 if ($need_split_build_invocation) {
3101 $r = $dmode =~ m/[S]/ ? +2 :
3102 $dmode =~ y/gGF/ABb/ ? +1 :
3103 $dmode =~ m/[ABb]/ ? 0 :
3107 #print STDERR "MASS2 ",Dumper($cmd, $xargs, $r);
3112 my @dbp = (@dpkgbuildpackage, qw(-us -uc), changesopts_initial(), @ARGV);
3113 my $wantsrc = massage_dbp_args \@dbp;
3120 push @dbp, changesopts_version();
3121 runcmd_ordryrun_local @dbp;
3123 printdone "build successful\n";
3127 my @dbp = @dpkgbuildpackage;
3129 my $wantsrc = massage_dbp_args \@dbp, \@ARGV;
3132 if (length executable_on_path('git-buildpackage')) {
3133 @cmd = qw(git-buildpackage);
3135 @cmd = qw(gbp buildpackage);
3137 push @cmd, (qw(-us -uc --git-no-sign-tags), "--git-builder=@dbp");
3142 if (!$suppress_clean) {
3143 push @cmd, '--git-cleaner=true';
3148 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
3149 canonicalise_suite();
3150 push @cmd, "--git-debian-branch=".lbranch();
3152 push @cmd, changesopts();
3153 runcmd_ordryrun_local @cmd, @ARGV;
3155 printdone "build successful\n";
3157 sub cmd_git_build { cmd_gbp_build(); } # compatibility with <= 1.0
3160 if ($cleanmode =~ m/^dpkg-source/) {
3161 # dpkg-source will clean, so we shouldn't
3162 $suppress_clean = 1;
3165 $sourcechanges = changespat $version,'source';
3167 unlink "../$sourcechanges" or $!==ENOENT
3168 or fail "remove $sourcechanges: $!";
3170 $dscfn = dscfn($version);
3171 if ($cleanmode eq 'dpkg-source') {
3172 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc -S),
3174 } elsif ($cleanmode eq 'dpkg-source-d') {
3175 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc -S -d),
3178 my $pwd = must_getcwd();
3179 my $leafdir = basename $pwd;
3181 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
3183 runcmd_ordryrun_local qw(sh -ec),
3184 'exec >$1; shift; exec "$@"','x',
3185 "../$sourcechanges",
3186 @dpkggenchanges, qw(-S), changesopts();
3190 sub cmd_build_source {
3191 badusage "build-source takes no additional arguments" if @ARGV;
3193 printdone "source built, results in $dscfn and $sourcechanges";
3198 my $pat = changespat $version;
3200 my @unwanted = map { s#^\.\./##; $_; } glob "../$pat";
3201 @unwanted = grep { $_ ne changespat $version,'source' } @unwanted;
3202 fail "changes files other than source matching $pat".
3203 " already present (@unwanted);".
3204 " building would result in ambiguity about the intended results"
3209 stat_exists $dscfn or fail "$dscfn (in parent directory): $!";
3210 stat_exists $sourcechanges
3211 or fail "$sourcechanges (in parent directory): $!";
3213 runcmd_ordryrun_local @sbuild, qw(-d), $isuite, @ARGV, $dscfn;
3214 my @changesfiles = glob $pat;
3215 @changesfiles = sort {
3216 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
3219 fail "wrong number of different changes files (@changesfiles)"
3220 unless @changesfiles==2;
3221 my $binchanges = parsecontrol($changesfiles[1], "binary changes file");
3222 foreach my $l (split /\n/, getfield $binchanges, 'Files') {
3223 fail "$l found in binaries changes file $binchanges"
3226 runcmd_ordryrun_local @mergechanges, @changesfiles;
3227 my $multichanges = changespat $version,'multi';
3229 stat_exists $multichanges or fail "$multichanges: $!";
3230 foreach my $cf (glob $pat) {
3231 next if $cf eq $multichanges;
3232 rename "$cf", "$cf.inmulti" or fail "$cf\{,.inmulti}: $!";
3235 printdone "build successful, results in $multichanges\n" or die $!;
3238 sub cmd_quilt_fixup {
3239 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
3240 my $clogp = parsechangelog();
3241 $version = getfield $clogp, 'Version';
3242 $package = getfield $clogp, 'Source';
3245 build_maybe_quilt_fixup();
3248 sub cmd_archive_api_query {
3249 badusage "need only 1 subpath argument" unless @ARGV==1;
3250 my ($subpath) = @ARGV;
3251 my @cmd = archive_api_query_cmd($subpath);
3253 exec @cmd or fail "exec curl: $!\n";
3256 sub cmd_clone_dgit_repos_server {
3257 badusage "need destination argument" unless @ARGV==1;
3258 my ($destdir) = @ARGV;
3259 $package = '_dgit-repos-server';
3260 my @cmd = (@git, qw(clone), access_giturl(), $destdir);
3262 exec @cmd or fail "exec git clone: $!\n";
3265 sub cmd_setup_mergechangelogs {
3266 badusage "no arguments allowed to dgit setup-mergechangelogs" if @ARGV;
3267 setup_mergechangelogs(1);
3270 sub cmd_setup_useremail {
3271 badusage "no arguments allowed to dgit setup-mergechangelogs" if @ARGV;
3275 sub cmd_setup_new_tree {
3276 badusage "no arguments allowed to dgit setup-tree" if @ARGV;
3280 #---------- argument parsing and main program ----------
3283 print "dgit version $our_version\n" or die $!;
3287 our (%valopts_long, %valopts_short);
3290 sub defvalopt ($$$$) {
3291 my ($long,$short,$val_re,$how) = @_;
3292 my $oi = { Long => $long, Short => $short, Re => $val_re, How => $how };
3293 $valopts_long{$long} = $oi;
3294 $valopts_short{$short} = $oi;
3295 # $how subref should:
3296 # do whatever assignemnt or thing it likes with $_[0]
3297 # if the option should not be passed on to remote, @rvalopts=()
3298 # or $how can be a scalar ref, meaning simply assign the value
3301 defvalopt '--since-version', '-v', '[^_]+|_', \$changes_since_version;
3302 defvalopt '--distro', '-d', '.+', \$idistro;
3303 defvalopt '', '-k', '.+', \$keyid;
3304 defvalopt '--existing-package','', '.*', \$existing_package;
3305 defvalopt '--build-products-dir','','.*', \$buildproductsdir;
3306 defvalopt '--clean', '', $cleanmode_re, \$cleanmode;
3307 defvalopt '--quilt', '', $quilt_modes_re, \$quilt_mode;
3309 defvalopt '', '-c', '.*=.*', sub { push @git, '-c', @_; };
3311 defvalopt '', '-C', '.+', sub {
3312 ($changesfile) = (@_);
3313 if ($changesfile =~ s#^(.*)/##) {
3314 $buildproductsdir = $1;
3318 defvalopt '--initiator-tempdir','','.*', sub {
3319 ($initiator_tempdir) = (@_);
3320 $initiator_tempdir =~ m#^/# or
3321 badusage "--initiator-tempdir must be used specify an".
3322 " absolute, not relative, directory."
3328 if (defined $ENV{'DGIT_SSH'}) {
3329 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
3330 } elsif (defined $ENV{'GIT_SSH'}) {
3331 @ssh = ($ENV{'GIT_SSH'});
3339 if (!defined $val) {
3340 badusage "$what needs a value" unless @ARGV;
3342 push @rvalopts, $val;
3344 badusage "bad value \`$val' for $what" unless
3345 $val =~ m/^$oi->{Re}$(?!\n)/s;
3346 my $how = $oi->{How};
3347 if (ref($how) eq 'SCALAR') {
3352 push @ropts, @rvalopts;
3356 last unless $ARGV[0] =~ m/^-/;
3360 if (m/^--dry-run$/) {
3363 } elsif (m/^--damp-run$/) {
3366 } elsif (m/^--no-sign$/) {
3369 } elsif (m/^--help$/) {
3371 } elsif (m/^--version$/) {
3373 } elsif (m/^--new$/) {
3376 } elsif (m/^--([-0-9a-z]+)=(.+)/s &&
3377 ($om = $opts_opt_map{$1}) &&
3381 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
3382 !$opts_opt_cmdonly{$1} &&
3383 ($om = $opts_opt_map{$1})) {
3386 } elsif (m/^--ignore-dirty$/s) {
3389 } elsif (m/^--no-quilt-fixup$/s) {
3391 $quilt_mode = 'nocheck';
3392 } elsif (m/^--no-rm-on-error$/s) {
3395 } elsif (m/^--(no-)?rm-old-changes$/s) {
3398 } elsif (m/^--deliberately-($deliberately_re)$/s) {
3400 push @deliberatelies, $&;
3401 } elsif (m/^--always-split-source-build$/s) {
3402 # undocumented, for testing
3404 $need_split_build_invocation = 1;
3405 } elsif (m/^(--[-0-9a-z]+)(=|$)/ && ($oi = $valopts_long{$1})) {
3406 $val = $2 ? $' : undef; #';
3407 $valopt->($oi->{Long});
3409 badusage "unknown long option \`$_'";
3416 } elsif (s/^-L/-/) {
3419 } elsif (s/^-h/-/) {
3421 } elsif (s/^-D/-/) {
3425 } elsif (s/^-N/-/) {
3430 push @changesopts, $_;
3432 } elsif (s/^-wn$//s) {
3434 $cleanmode = 'none';
3435 } elsif (s/^-wg$//s) {
3438 } elsif (s/^-wgf$//s) {
3440 $cleanmode = 'git-ff';
3441 } elsif (s/^-wd$//s) {
3443 $cleanmode = 'dpkg-source';
3444 } elsif (s/^-wdd$//s) {
3446 $cleanmode = 'dpkg-source-d';
3447 } elsif (s/^-wc$//s) {
3449 $cleanmode = 'check';
3450 } elsif (m/^-[a-zA-Z]/ && ($oi = $valopts_short{$&})) {
3452 $val = undef unless length $val;
3453 $valopt->($oi->{Short});
3456 badusage "unknown short option \`$_'";
3463 sub finalise_opts_opts () {
3464 foreach my $k (keys %opts_opt_map) {
3465 my $om = $opts_opt_map{$k};
3467 my $v = access_cfg("cmd-$k", 'RETURN-UNDEF');
3469 badcfg "cannot set command for $k"
3470 unless length $om->[0];
3474 foreach my $c (access_cfg_cfgs("opts-$k")) {
3475 my $vl = $gitcfg{$c};
3476 printdebug "CL $c ",
3477 ($vl ? join " ", map { shellquote } @$vl : ""),
3478 "\n" if $debuglevel >= 4;
3480 badcfg "cannot configure options for $k"
3481 if $opts_opt_cmdonly{$k};
3482 my $insertpos = $opts_cfg_insertpos{$k};
3483 @$om = ( @$om[0..$insertpos-1],
3485 @$om[$insertpos..$#$om] );
3490 if ($ENV{$fakeeditorenv}) {
3492 quilt_fixup_editor();
3498 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
3499 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
3500 if $dryrun_level == 1;
3502 print STDERR $helpmsg or die $!;
3505 my $cmd = shift @ARGV;
3508 if (!defined $rmchanges) {
3509 local $access_forpush;
3510 $rmchanges = access_cfg_bool(0, 'rm-old-changes');
3513 if (!defined $quilt_mode) {
3514 local $access_forpush;
3515 $quilt_mode = cfg('dgit.force.quilt-mode', 'RETURN-UNDEF')
3516 // access_cfg('quilt-mode', 'RETURN-UNDEF')
3518 $quilt_mode =~ m/^($quilt_modes_re)$/
3519 or badcfg "unknown quilt-mode \`$quilt_mode'";
3523 if (!defined $cleanmode) {
3524 local $access_forpush;
3525 $cleanmode = access_cfg('clean-mode', 'RETURN-UNDEF');
3526 $cleanmode //= 'dpkg-source';
3528 badcfg "unknown clean-mode \`$cleanmode'" unless
3529 $cleanmode =~ m/^($cleanmode_re)$(?!\n)/s;
3532 my $fn = ${*::}{"cmd_$cmd"};
3533 $fn or badusage "unknown operation $cmd";