3 # Integration between git and Debian-style archives
5 # Copyright (C)2013 Ian Jackson
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 use Dpkg::Control::Hash;
27 use File::Temp qw(tempdir);
33 our $our_version = 'UNRELEASED'; ###substituted###
35 our $isuite = 'unstable';
41 our $dryrun_level = 0;
43 our $buildproductsdir = '..';
47 our $existing_package = 'dpkg';
48 our $cleanmode = 'dpkg-source';
49 our $changes_since_version;
50 our $we_are_responder;
51 our $initiator_tempdir;
53 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
55 our $suite_re = '[-+.0-9a-z]+';
58 our (@dget) = qw(dget);
59 our (@dput) = qw(dput);
60 our (@debsign) = qw(debsign);
62 our (@sbuild) = qw(sbuild -A);
64 our (@dgit) = qw(dgit);
65 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
66 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
67 our (@dpkggenchanges) = qw(dpkg-genchanges);
68 our (@mergechanges) = qw(mergechanges -f);
69 our (@changesopts) = ('');
71 our %opts_opt_map = ('dget' => \@dget,
73 'debsign' => \@debsign,
78 'dpkg-source' => \@dpkgsource,
79 'dpkg-buildpackage' => \@dpkgbuildpackage,
80 'dpkg-genchanges' => \@dpkggenchanges,
81 'ch' => \@changesopts,
82 'mergechanges' => \@mergechanges);
84 our %opts_opt_cmdonly = ('gpg' => 1);
89 open DEBUG, ">/dev/null" or die $!;
93 our $remotename = 'dgit';
94 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
95 our $branchprefix = 'dgit';
98 sub lbranch () { return "$branchprefix/$csuite"; }
99 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
100 sub lref () { return "refs/heads/".lbranch(); }
101 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
102 sub rrref () { return "refs/$branchprefix/$csuite"; }
117 return "${package}_".(stripepoch $vsn).".dsc";
121 our $debugprefix = '';
123 sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
126 die $us.($we_are_responder ? " (build host)" : "").": @_\n";
129 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
131 sub no_such_package () {
132 print STDERR "$us: package $package does not exist in suite $isuite\n";
138 return "+".rrref().":".lrref();
143 printdebug "CD $newdir\n";
144 chdir $newdir or die "chdir: $newdir: $!";
147 #---------- remote protocol support, common ----------
149 # remote push initiator/responder protocol:
150 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
152 # > file parsed-changelog
153 # [indicates that output of dpkg-parsechangelog follows]
154 # > data-block NBYTES
155 # > [NBYTES bytes of data (no newline)]
156 # [maybe some more blocks]
168 # [indicates that signed tag is wanted]
169 # < data-block NBYTES
170 # < [NBYTES bytes of data (no newline)]
171 # [maybe some more blocks]
175 # > want signed-dsc-changes
176 # < data-block NBYTES [transfer of signed dsc]
178 # < data-block NBYTES [transfer of signed changes]
186 fail "connection lost: $!" if $fh->error;
187 fail "protocol violation; $m not expected";
190 sub protocol_expect (&$) {
191 my ($match, $fh) = @_;
194 defined && chomp or badproto $fh, "eof";
202 badproto $fh, "\`$_'";
205 sub protocol_send_file ($$) {
206 my ($fh, $ourfn) = @_;
207 open PF, "<", $ourfn or die "$ourfn: $!";
210 my $got = read PF, $d, 65536;
211 die "$ourfn: $!" unless defined $got;
213 print $fh "data-block ".length($d)."\n" or die $!;
214 print $fh $d or die $!;
216 PF->error and die "$ourfn $!";
217 print $fh "data-end\n" or die $!;
221 sub protocol_read_bytes ($$) {
222 my ($fh, $nbytes) = @_;
223 $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
225 my $got = read $fh, $d, $nbytes;
226 $got==$nbytes or badproto $fh, "eof during data block";
230 sub protocol_receive_file ($$) {
231 my ($fh, $ourfn) = @_;
232 printdebug "() $ourfn\n";
233 open PF, ">", $ourfn or die "$ourfn: $!";
235 my ($y,$l) = protocol_expect {
236 m/^data-block (.*)$/ ? (1,$1) :
237 m/^data-end$/ ? (0,) :
241 my $d = protocol_read_bytes $fh, $l;
242 print PF $d or die $!;
247 #---------- remote protocol support, responder ----------
249 sub responder_send_command ($) {
251 return unless $we_are_responder;
252 # called even without $we_are_responder
253 printdebug ">> $command\n";
254 print PO $command, "\n" or die $!;
257 sub responder_send_file ($$) {
258 my ($keyword, $ourfn) = @_;
259 return unless $we_are_responder;
260 printdebug "]] $keyword $ourfn\n";
261 responder_send_command "file $keyword";
262 protocol_send_file \*PO, $ourfn;
265 sub responder_receive_files ($@) {
266 my ($keyword, @ourfns) = @_;
267 die unless $we_are_responder;
268 printdebug "[[ $keyword @ourfns\n";
269 responder_send_command "want $keyword";
270 foreach my $fn (@ourfns) {
271 protocol_receive_file \*PI, $fn;
274 protocol_expect { m/^files-end$/ } \*PI;
277 #---------- remote protocol support, initiator ----------
279 sub initiator_expect (&) {
281 protocol_expect { &$match } \*RO;
284 #---------- end remote code ----------
287 if ($we_are_responder) {
289 responder_send_command "progress ".length($m) or die $!;
290 print PO $m or die $!;
300 $ua = LWP::UserAgent->new();
304 progress "downloading $what...";
305 my $r = $ua->get(@_) or die $!;
306 return undef if $r->code == 404;
307 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
308 return $r->decoded_content();
311 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
318 if (m{[^-=_./0-9a-z]}i) {
325 return join ' ', @out;
330 my $intro = shift @_;
331 print $fh $intro," " or die $!;
332 print $fh shellquote @_ or die $!;
333 print $fh "\n" or die $!;
337 { local ($!); printcmd \*STDERR, "$us: failed command:", @_ or die $!; };
339 fail "failed to fork/exec: $!";
340 } elsif (!($? & 0xff)) {
341 fail "subprocess failed with error exit status ".($?>>8);
343 fail "subprocess crashed (wait status $?)";
345 fail "subprocess produced invalid output";
350 printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
352 failedcmd @_ if system @_;
355 sub act_local () { return $dryrun_level <= 1; }
356 sub act_scary () { return !$dryrun_level; }
359 if (!$dryrun_level) {
360 progress "dgit ok: @_";
362 progress "would be ok: @_ (but dry run only)";
366 sub cmdoutput_errok {
367 die Dumper(\@_)." ?" if grep { !defined } @_;
368 printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
369 open P, "-|", @_ or die $!;
372 { local $/ = undef; $d = <P>; }
374 if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
377 printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
382 my $d = cmdoutput_errok @_;
383 defined $d or failedcmd @_;
388 printcmd(\*STDERR,$debugprefix."#",@_);
391 sub runcmd_ordryrun {
399 sub runcmd_ordryrun_local {
408 my ($first_shell, @cmd) = @_;
409 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
412 our $helpmsg = <<END;
414 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
415 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
416 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
417 dgit [dgit-opts] push [dgit-opts] [suite]
418 dgit [dgit-opts] rpush build-host:build-dir ...
419 important dgit options:
420 -k<keyid> sign tag and package with <keyid> instead of default
421 --dry-run -n do not change anything, but go through the motions
422 --damp-run -L like --dry-run but make local changes, without signing
423 --new -N allow introducing a new package
424 --debug -D increase debug level
425 -c<name>=<value> set git config option (used directly by dgit too)
428 our $later_warning_msg = <<END;
429 Perhaps the upload is stuck in incoming. Using the version from git.
433 print STDERR "$us: @_\n", $helpmsg or die $!;
438 @ARGV or badusage "too few arguments";
439 return scalar shift @ARGV;
443 print $helpmsg or die $!;
447 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
449 our %defcfg = ('dgit.default.distro' => 'debian',
450 'dgit.default.username' => '',
451 'dgit.default.archive-query-default-component' => 'main',
452 'dgit.default.ssh' => 'ssh',
453 'dgit-distro.debian.git-host' => 'git.debian.org',
454 'dgit-distro.debian.git-proto' => 'git+ssh://',
455 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
456 'dgit-distro.debian.git-check' => 'ssh-cmd',
457 'dgit-distro.debian.git-create' => 'ssh-cmd',
458 'dgit-distro.debian.sshpsql-host' => 'coccia.debian.org',
459 'dgit-distro.debian.sshpsql-dbname' => 'service=projectb',
460 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
461 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
462 'dgit-distro.debian.backports-quirk' => '%-backports*',
463 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
464 'dgit-distro.test-dummy.ssh' => "$td/ssh",
465 'dgit-distro.test-dummy.username' => "alice",
466 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
467 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
468 'dgit-distro.test-dummy.git-url' => "$td/git",
469 'dgit-distro.test-dummy.git-host' => "git",
470 'dgit-distro.test-dummy.git-path' => "$td/git",
471 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
472 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
473 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
478 return undef if $c =~ /RETURN-UNDEF/;
479 my @cmd = (@git, qw(config --), $c);
482 local ($debug) = $debug-1;
483 $v = cmdoutput_errok @cmd;
490 my $dv = $defcfg{$c};
491 return $dv if defined $dv;
493 badcfg "need value for one of: @_";
496 sub access_basedistro () {
497 return cfg("dgit-suite.$isuite.distro",
498 "dgit.default.distro");
501 sub access_quirk () {
502 # returns (quirk name, distro to use instead, quirk-specific info)
503 my $basedistro = access_basedistro();
504 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
506 if (defined $backports_quirk) {
507 my $re = $backports_quirk;
508 $re =~ s/[^-0-9a-z_\%*]/\\$&/ig;
510 $re =~ s/\%/([-0-9a-z_]+)/ or badcfg "backports-quirk needs \%";
511 if ($isuite =~ m/^$re$/) {
512 return ('backports',"$basedistro-backports",$1);
515 return ('none',$basedistro);
518 sub access_distro () {
519 return (access_quirk())[1];
524 my $basedistro = access_basedistro();
525 my $distro = $idistro || access_distro();
526 my $value = cfg(map {
527 ("dgit-distro.$distro.$_",
528 "dgit-distro.$basedistro.$_",
534 sub string_to_ssh ($) {
536 if ($spec =~ m/\s/) {
537 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
543 sub access_cfg_ssh () {
544 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
545 if (!defined $gitssh) {
548 return string_to_ssh $gitssh;
552 sub access_someuserhost ($) {
554 my $user = access_cfg("$some-user",'username');
555 my $host = access_cfg("$some-host");
556 return length($user) ? "$user\@$host" : $host;
559 sub access_gituserhost () {
560 return access_someuserhost('git');
563 sub access_giturl () {
564 my $url = access_cfg('git-url','RETURN-UNDEF');
567 access_cfg('git-proto').
568 access_gituserhost().
569 access_cfg('git-path');
571 return "$url/$package.git";
574 sub parsecontrolfh ($$@) {
575 my ($fh, $desc, @opts) = @_;
576 my %opts = ('name' => $desc, @opts);
577 my $c = Dpkg::Control::Hash->new(%opts);
578 $c->parse($fh) or die "parsing of $desc failed";
583 my ($file, $desc) = @_;
584 my $fh = new IO::Handle;
585 open $fh, '<', $file or die "$file: $!";
586 my $c = parsecontrolfh($fh,$desc);
587 $fh->error and die $!;
593 my ($dctrl,$field) = @_;
594 my $v = $dctrl->{$field};
595 return $v if defined $v;
596 fail "missing field $field in ".$v->get_option('name');
600 my $c = Dpkg::Control::Hash->new();
601 my $p = new IO::Handle;
602 my @cmd = (qw(dpkg-parsechangelog), @_);
603 open $p, '-|', @cmd or die $!;
605 $?=0; $!=0; close $p or failedcmd @cmd;
609 sub git_get_ref ($) {
611 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
613 $?==256 or fail "git show-ref failed (status $?)";
614 printdebug "ref $refname= [show-ref exited 1]\n";
617 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
618 printdebug "ref $refname=$1\n";
621 printdebug "ref $refname= [no match]\n";
628 defined $d or fail "getcwd failed: $!";
634 sub archive_query ($) {
636 my $query = access_cfg('archive-query','RETURN-UNDEF');
637 if (!defined $query) {
638 my $distro = access_basedistro();
639 if ($distro eq 'debian') {
641 access_someuserhost('sshpsql').':'.
642 access_cfg('sshpsql-dbname');
644 $query = "madison:$distro";
647 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
650 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
653 sub pool_dsc_subpath ($$) {
654 my ($vsn,$component) = @_; # $package is implict arg
655 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
656 return "/pool/$component/$prefix/$package/".dscfn($vsn);
659 sub archive_query_madison ($$) {
660 my ($proto,$data) = @_;
661 die unless $proto eq 'madison';
662 $rmad{$package} ||= cmdoutput
663 qw(rmadison -asource),"-s$isuite","-u$data",$package;
664 my $rmad = $rmad{$package};
665 return madison_parse($rmad);
668 sub madison_parse ($) {
671 foreach my $l (split /\n/, $rmad) {
672 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
673 \s*( [^ \t|]+ )\s* \|
674 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
675 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
676 $1 eq $package or die "$rmad $package ?";
683 $component = access_cfg('archive-query-default-component');
685 $5 eq 'source' or die "$rmad ?";
686 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
688 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
691 sub canonicalise_suite_madison ($$) {
692 # madison canonicalises for us
693 my @r = archive_query_madison($_[0],$_[1]);
695 "unable to canonicalise suite using package $package".
696 " which does not appear to exist in suite $isuite;".
697 " --existing-package may help";
702 my ($data,$sql) = @_;
703 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
704 my ($userhost,$dbname) = ($`,$'); #';
706 my @cmd = (access_cfg_ssh, $userhost,
707 "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
708 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
709 open P, "-|", @cmd or die $!;
712 printdebug("$debugprefix>|$_|\n");
715 $!=0; $?=0; close P or failedcmd @cmd;
717 my $nrows = pop @rows;
718 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
719 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
720 @rows = map { [ split /\|/, $_ ] } @rows;
721 my $ncols = scalar @{ shift @rows };
722 die if grep { scalar @$_ != $ncols } @rows;
726 sub sql_injection_check {
727 foreach (@_) { die "$_ $& ?" if m/[']/; }
730 sub archive_query_sshpsql ($$) {
731 my ($proto,$data) = @_;
732 sql_injection_check $isuite, $package;
733 my @rows = sshpsql($data, <<END);
734 SELECT source.version, component.name, files.filename
736 JOIN src_associations ON source.id = src_associations.source
737 JOIN suite ON suite.id = src_associations.suite
738 JOIN dsc_files ON dsc_files.source = source.id
739 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
740 JOIN component ON component.id = files_archive_map.component_id
741 JOIN files ON files.id = dsc_files.file
742 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
743 AND source.source='$package'
744 AND files.filename LIKE '%.dsc';
746 @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
748 my ($vsn,$component,$filename) = @$_;
749 [ $vsn, "/pool/$component/$filename" ];
754 sub canonicalise_suite_sshpsql ($$) {
755 my ($proto,$data) = @_;
756 sql_injection_check $isuite;
757 my @rows = sshpsql($data, <<END);
758 SELECT suite.codename
759 FROM suite where suite_name='$isuite' or codename='$isuite';
761 @rows = map { $_->[0] } @rows;
762 fail "unknown suite $isuite" unless @rows;
763 die "ambiguous $isuite: @rows ?" if @rows>1;
767 sub canonicalise_suite_dummycat ($$) {
768 my ($proto,$data) = @_;
769 my $dpath = "$data/suite.$isuite";
770 if (!open C, "<", $dpath) {
771 $!==ENOENT or die "$dpath: $!";
772 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
776 chomp or die "$dpath: $!";
778 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
782 sub archive_query_dummycat ($$) {
783 my ($proto,$data) = @_;
784 canonicalise_suite();
785 my $dpath = "$data/package.$csuite.$package";
786 if (!open C, "<", $dpath) {
787 $!==ENOENT or die "$dpath: $!";
788 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
796 printdebug "dummycat query $csuite $package $dpath | $_\n";
797 my @row = split /\s+/, $_;
798 @row==2 or die "$dpath: $_ ?";
801 C->error and die "$dpath: $!";
803 return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
806 sub canonicalise_suite () {
807 return if defined $csuite;
808 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
809 $csuite = archive_query('canonicalise_suite');
810 if ($isuite ne $csuite) {
811 progress "canonical suite name for $isuite is $csuite";
815 sub get_archive_dsc () {
816 canonicalise_suite();
817 my @vsns = archive_query('archive_query');
818 foreach my $vinfo (@vsns) {
819 my ($vsn,$subpath) = @$vinfo;
820 $dscurl = access_cfg('mirror').$subpath;
821 $dscdata = url_get($dscurl);
823 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
826 my $dscfh = new IO::File \$dscdata, '<' or die $!;
827 printdebug Dumper($dscdata) if $debug>1;
828 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
829 printdebug Dumper($dsc) if $debug>1;
830 my $fmt = getfield $dsc, 'Format';
831 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
837 sub check_for_git () {
839 my $how = access_cfg('git-check');
840 if ($how eq 'ssh-cmd') {
842 (access_cfg_ssh, access_gituserhost(),
843 " set -e; cd ".access_cfg('git-path').";".
844 " if test -d $package.git; then echo 1; else echo 0; fi");
845 my $r= cmdoutput @cmd;
846 failedcmd @cmd unless $r =~ m/^[01]$/;
848 } elsif ($how eq 'true') {
851 badcfg "unknown git-check \`$how'";
855 sub create_remote_git_repo () {
856 my $how = access_cfg('git-create');
857 if ($how eq 'ssh-cmd') {
859 (access_cfg_ssh, access_gituserhost(),
860 "set -e; cd ".access_cfg('git-path').";".
861 " cp -a _template $package.git");
862 } elsif ($how eq 'true') {
865 badcfg "unknown git-create \`$how'";
869 our ($dsc_hash,$lastpush_hash);
871 our $ud = '.git/dgit/unpack';
879 sub mktree_in_ud_from_only_subdir () {
880 # changes into the subdir
883 $dirs[0] =~ m#^([^/]+)/\.$# or die;
886 fail "source package contains .git directory" if stat '.git';
887 die $! unless $!==&ENOENT;
888 runcmd qw(git init -q);
889 rmtree('.git/objects');
890 symlink '../../../../objects','.git/objects' or die $!;
891 runcmd @git, qw(add -Af);
892 my $tree = cmdoutput @git, qw(write-tree);
893 $tree =~ m/^\w+$/ or die "$tree ?";
897 sub dsc_files_info () {
898 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
899 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
900 ['Files', 'Digest::MD5', 'new()']) {
901 my ($fname, $module, $method) = @$csumi;
902 my $field = $dsc->{$fname};
903 next unless defined $field;
904 eval "use $module; 1;" or die $@;
906 foreach (split /\n/, $field) {
908 m/^(\w+) (\d+) (\S+)$/ or
909 fail "could not parse .dsc $fname line \`$_'";
910 my $digester = eval "$module"."->$method;" or die $@;
915 Digester => $digester,
920 fail "missing any supported Checksums-* or Files field in ".
921 $dsc->get_option('name');
925 map { $_->{Filename} } dsc_files_info();
928 sub is_orig_file ($) {
930 m/\.orig(?:-\w+)?\.tar\.\w+$/;
933 sub make_commit ($) {
935 return cmdoutput @git, qw(hash-object -w -t commit), $file;
938 sub clogp_authline ($) {
940 my $author = getfield $clogp, 'Maintainer';
941 $author =~ s#,.*##ms;
942 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
943 my $authline = "$author $date";
944 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
945 fail "unexpected commit author line format \`$authline'".
946 " (was generated from changelog Maintainer field)";
950 sub generate_commit_from_dsc () {
954 foreach my $f (dsc_files()) {
955 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
957 link "../../../$f", $f
961 runcmd @dget, qw(--), $dscurl;
962 foreach my $f (grep { is_orig_file($_) } @files) {
963 link $f, "../../../../$f"
967 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
968 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
969 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
970 my $authline = clogp_authline $clogp;
971 my $changes = getfield $clogp, 'Changes';
972 open C, ">../commit.tmp" or die $!;
973 print C <<END or die $!;
980 # imported from the archive
983 my $outputhash = make_commit qw(../commit.tmp);
984 my $cversion = getfield $clogp, 'Version';
985 progress "synthesised git commit from .dsc $cversion";
986 if ($lastpush_hash) {
987 runcmd @git, qw(reset --hard), $lastpush_hash;
988 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
989 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
990 my $oversion = getfield $oldclogp, 'Version';
992 version_compare_string($oversion, $cversion);
994 # git upload/ is earlier vsn than archive, use archive
995 open C, ">../commit2.tmp" or die $!;
996 print C <<END or die $!;
998 parent $lastpush_hash
1003 Record $package ($cversion) in archive suite $csuite
1005 $outputhash = make_commit qw(../commit2.tmp);
1006 } elsif ($vcmp > 0) {
1007 print STDERR <<END or die $!;
1009 Version actually in archive: $cversion (older)
1010 Last allegedly pushed/uploaded: $oversion (newer or same)
1013 $outputhash = $lastpush_hash;
1015 $outputhash = $lastpush_hash;
1018 changedir '../../../..';
1019 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1020 'DGIT_ARCHIVE', $outputhash;
1021 cmdoutput @git, qw(log -n2), $outputhash;
1022 # ... gives git a chance to complain if our commit is malformed
1027 sub ensure_we_have_orig () {
1028 foreach my $fi (dsc_files_info()) {
1029 my $f = $fi->{Filename};
1030 next unless is_orig_file($f);
1031 if (open F, "<", "../$f") {
1032 $fi->{Digester}->reset();
1033 $fi->{Digester}->addfile(*F);
1034 F->error and die $!;
1035 my $got = $fi->{Digester}->hexdigest();
1036 $got eq $fi->{Hash} or
1037 fail "existing file $f has hash $got but .dsc".
1038 " demands hash $fi->{Hash}".
1039 " (perhaps you should delete this file?)";
1040 progress "using existing $f";
1043 die "$f $!" unless $!==&ENOENT;
1045 my $origurl = $dscurl;
1046 $origurl =~ s{/[^/]+$}{};
1048 die "$f ?" unless $f =~ m/^${package}_/;
1049 die "$f ?" if $f =~ m#/#;
1050 runcmd_ordryrun_local shell_cmd 'cd ..', @dget,'--',$origurl;
1055 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1058 sub is_fast_fwd ($$) {
1059 my ($ancestor,$child) = @_;
1060 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1061 my $mb = cmdoutput_errok @cmd;
1063 return rev_parse($mb) eq rev_parse($ancestor);
1065 $?==256 or failedcmd @cmd;
1070 sub git_fetch_us () {
1071 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1074 sub fetch_from_archive () {
1075 # ensures that lrref() is what is actually in the archive,
1076 # one way or another
1080 foreach my $field (@ourdscfield) {
1081 $dsc_hash = $dsc->{$field};
1082 last if defined $dsc_hash;
1084 if (defined $dsc_hash) {
1085 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1087 progress "last upload to archive specified git hash";
1089 progress "last upload to archive has NO git hash";
1092 progress "no version available from the archive";
1095 $lastpush_hash = git_get_ref(lrref());
1096 printdebug "previous reference hash=$lastpush_hash\n";
1098 if (defined $dsc_hash) {
1099 fail "missing remote git history even though dsc has hash -".
1100 " could not find ref ".lrref().
1101 " (should have been fetched from ".access_giturl()."#".rrref().")"
1102 unless $lastpush_hash;
1104 ensure_we_have_orig();
1105 if ($dsc_hash eq $lastpush_hash) {
1106 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1107 print STDERR <<END or die $!;
1109 Git commit in archive is behind the last version allegedly pushed/uploaded.
1110 Commit referred to by archive: $dsc_hash
1111 Last allegedly pushed/uploaded: $lastpush_hash
1114 $hash = $lastpush_hash;
1116 fail "archive's .dsc refers to ".$dsc_hash.
1117 " but this is an ancestor of ".$lastpush_hash;
1120 $hash = generate_commit_from_dsc();
1121 } elsif ($lastpush_hash) {
1122 # only in git, not in the archive yet
1123 $hash = $lastpush_hash;
1124 print STDERR <<END or die $!;
1126 Package not found in the archive, but has allegedly been pushed using dgit.
1130 printdebug "nothing found!\n";
1131 if (defined $skew_warning_vsn) {
1132 print STDERR <<END or die $!;
1134 Warning: relevant archive skew detected.
1135 Archive allegedly contains $skew_warning_vsn
1136 But we were not able to obtain any version from the archive or git.
1142 printdebug "current hash=$hash\n";
1143 if ($lastpush_hash) {
1144 fail "not fast forward on last upload branch!".
1145 " (archive's version left in DGIT_ARCHIVE)"
1146 unless is_fast_fwd($lastpush_hash, $hash);
1148 if (defined $skew_warning_vsn) {
1150 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1151 my $clogf = ".git/dgit/changelog.tmp";
1152 runcmd shell_cmd "exec >$clogf",
1153 @git, qw(cat-file blob), "$hash:debian/changelog";
1154 my $gotclogp = parsechangelog("-l$clogf");
1155 my $got_vsn = getfield $gotclogp, 'Version';
1156 printdebug "SKEW CHECK GOT $got_vsn\n";
1157 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
1158 print STDERR <<END or die $!;
1160 Warning: archive skew detected. Using the available version:
1161 Archive allegedly contains $skew_warning_vsn
1162 We were able to obtain only $got_vsn
1167 if ($lastpush_hash ne $hash) {
1168 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1172 dryrun_report @upd_cmd;
1180 canonicalise_suite();
1181 badusage "dry run makes no sense with clone" unless act_local();
1182 mkdir $dstdir or die "$dstdir $!";
1184 runcmd @git, qw(init -q);
1185 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1186 open H, "> .git/HEAD" or die $!;
1187 print H "ref: ".lref()."\n" or die $!;
1189 runcmd @git, qw(remote add), 'origin', access_giturl();
1190 if (check_for_git()) {
1191 progress "fetching existing git history";
1193 runcmd_ordryrun_local @git, qw(fetch origin);
1195 progress "starting new git history";
1197 fetch_from_archive() or no_such_package;
1198 runcmd @git, qw(reset --hard), lrref();
1199 printdone "ready for work in $dstdir";
1203 if (check_for_git()) {
1206 fetch_from_archive() or no_such_package();
1207 printdone "fetched into ".lrref();
1212 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1214 printdone "fetched to ".lrref()." and merged into HEAD";
1217 sub check_not_dirty () {
1218 return if $ignoredirty;
1219 my @cmd = (@git, qw(diff --quiet HEAD));
1220 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1221 $!=0; $?=0; system @cmd;
1222 return if !$! && !$?;
1223 if (!$! && $?==256) {
1224 fail "working tree is dirty (does not match HEAD)";
1230 sub commit_quilty_patch () {
1231 my $output = cmdoutput @git, qw(status --porcelain);
1233 foreach my $l (split /\n/, $output) {
1234 next unless $l =~ m/\S/;
1235 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1240 progress "nothing quilty to commit, ok.";
1243 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1244 my $m = "Commit Debian 3.0 (quilt) metadata";
1246 runcmd_ordryrun_local @git, qw(commit -m), $m;
1251 return 0 unless $format eq '3.0 (quilt)';
1252 progress "Format \`$format', urgh";
1254 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1260 sub push_parse_changelog ($) {
1263 my $clogp = Dpkg::Control::Hash->new();
1264 $clogp->load($clogpfn) or die;
1266 $package = getfield $clogp, 'Source';
1267 my $cversion = getfield $clogp, 'Version';
1268 my $tag = debiantag($cversion);
1269 runcmd @git, qw(check-ref-format), $tag;
1271 my $dscfn = dscfn($cversion);
1273 return ($clogp, $cversion, $tag, $dscfn);
1276 sub push_parse_dsc ($$$) {
1277 my ($dscfn,$dscfnwhat, $cversion) = @_;
1278 $dsc = parsecontrol($dscfn,$dscfnwhat);
1279 my $dversion = getfield $dsc, 'Version';
1280 my $dscpackage = getfield $dsc, 'Source';
1281 ($dscpackage eq $package && $dversion eq $cversion) or
1282 fail "$dscfn is for $dscpackage $dversion".
1283 " but debian/changelog is for $package $cversion";
1286 sub push_mktag ($$$$$$$) {
1287 my ($head,$clogp,$tag,
1289 $changesfile,$changesfilewhat,
1292 $dsc->{$ourdscfield[0]} = $head;
1293 $dsc->save("$dscfn.tmp") or die $!;
1295 my $changes = parsecontrol($changesfile,$changesfilewhat);
1296 foreach my $field (qw(Source Distribution Version)) {
1297 $changes->{$field} eq $clogp->{$field} or
1298 fail "changes field $field \`$changes->{$field}'".
1299 " does not match changelog \`$clogp->{$field}'";
1302 my $cversion = getfield $clogp, 'Version';
1303 my $clogsuite = getfield $clogp, 'Distribution';
1305 # We make the git tag by hand because (a) that makes it easier
1306 # to control the "tagger" (b) we can do remote signing
1307 my $authline = clogp_authline $clogp;
1308 open TO, '>', $tfn->('.tmp') or die $!;
1309 print TO <<END or die $!;
1315 $package release $cversion for $clogsuite ($csuite) [dgit]
1319 my $tagobjfn = $tfn->('.tmp');
1321 if (!defined $keyid) {
1322 $keyid = access_cfg('keyid','RETURN-UNDEF');
1324 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1325 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1326 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1327 push @sign_cmd, $tfn->('.tmp');
1328 runcmd_ordryrun @sign_cmd;
1330 $tagobjfn = $tfn->('.signed.tmp');
1331 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1332 $tfn->('.tmp'), $tfn->('.tmp.asc');
1339 sub sign_changes ($) {
1340 my ($changesfile) = @_;
1342 my @debsign_cmd = @debsign;
1343 push @debsign_cmd, "-k$keyid" if defined $keyid;
1344 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1345 push @debsign_cmd, $changesfile;
1346 runcmd_ordryrun @debsign_cmd;
1351 printdebug "actually entering push\n";
1354 my $clogpfn = ".git/dgit/changelog.822.tmp";
1355 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1357 responder_send_file('parsed-changelog', $clogpfn);
1359 my ($clogp, $cversion, $tag, $dscfn) =
1360 push_parse_changelog("$clogpfn");
1362 my $dscpath = "$buildproductsdir/$dscfn";
1364 fail "looked for .dsc $dscfn, but $!;".
1365 " maybe you forgot to build";
1367 responder_send_file('dsc', $dscpath);
1369 push_parse_dsc($dscpath, $dscfn, $cversion);
1371 my $format = getfield $dsc, 'Format';
1372 printdebug "format $format\n";
1373 if (madformat($format)) {
1374 commit_quilty_patch();
1378 progress "checking that $dscfn corresponds to HEAD";
1379 runcmd qw(dpkg-source -x --),
1380 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1381 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1382 changedir '../../../..';
1383 my @diffcmd = (@git, qw(diff --exit-code), $tree);
1384 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1386 if (system @diffcmd) {
1387 if ($! && $?==256) {
1388 fail "$dscfn specifies a different tree to your HEAD commit;".
1389 " perhaps you forgot to build";
1395 #do fast forward check and maybe fake merge
1396 # if (!is_fast_fwd(mainbranch
1397 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1398 # map { lref($_).":".rref($_) }
1400 my $head = rev_parse('HEAD');
1401 if (!$changesfile) {
1402 my $multi = "$buildproductsdir/".
1403 "${package}_".(stripepoch $cversion)."_multi.changes";
1404 if (stat "$multi") {
1405 $changesfile = $multi;
1407 $!==&ENOENT or die "$multi: $!";
1408 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1409 my @cs = glob "$buildproductsdir/$pat";
1410 fail "failed to find unique changes file".
1411 " (looked for $pat in $buildproductsdir, or $multi);".
1412 " perhaps you need to use dgit -C"
1414 ($changesfile) = @cs;
1417 $changesfile = "$buildproductsdir/$changesfile";
1420 responder_send_file('changes',$changesfile);
1421 responder_send_command("param head $head");
1422 responder_send_command("param csuite $csuite");
1424 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1427 if ($we_are_responder) {
1428 $tagobjfn = $tfn->('.signed.tmp');
1429 responder_receive_files('signed-tag', $tagobjfn);
1432 push_mktag($head,$clogp,$tag,
1434 $changesfile,$changesfile,
1438 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1439 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1440 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1441 runcmd_ordryrun @git, qw(tag -v --), $tag;
1443 if (!check_for_git()) {
1444 create_remote_git_repo();
1446 runcmd_ordryrun @git, qw(push),access_giturl(),
1447 "HEAD:".rrref(), "refs/tags/$tag";
1448 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1450 if ($we_are_responder) {
1451 my $dryrunsuffix = act_local() ? "" : ".tmp";
1452 responder_receive_files('signed-dsc-changes',
1453 "$dscpath$dryrunsuffix",
1454 "$changesfile$dryrunsuffix");
1457 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1459 progress "[new .dsc left in $dscpath.tmp]";
1461 sign_changes $changesfile;
1464 my $host = access_cfg('upload-host','RETURN-UNDEF');
1465 my @hostarg = defined($host) ? ($host,) : ();
1466 runcmd_ordryrun @dput, @hostarg, $changesfile;
1467 printdone "pushed and uploaded $cversion";
1469 responder_send_command("complete");
1475 badusage "-p is not allowed with clone; specify as argument instead"
1476 if defined $package;
1479 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1480 ($package,$isuite) = @ARGV;
1481 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1482 ($package,$dstdir) = @ARGV;
1483 } elsif (@ARGV==3) {
1484 ($package,$isuite,$dstdir) = @ARGV;
1486 badusage "incorrect arguments to dgit clone";
1488 $dstdir ||= "$package";
1492 sub branchsuite () {
1493 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1494 if ($branch =~ m#$lbranch_re#o) {
1501 sub fetchpullargs () {
1502 if (!defined $package) {
1503 my $sourcep = parsecontrol('debian/control','debian/control');
1504 $package = getfield $sourcep, 'Source';
1507 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1509 my $clogp = parsechangelog();
1510 $isuite = getfield $clogp, 'Distribution';
1512 canonicalise_suite();
1513 progress "fetching from suite $csuite";
1514 } elsif (@ARGV==1) {
1516 canonicalise_suite();
1518 badusage "incorrect arguments to dgit fetch or dgit pull";
1536 badusage "-p is not allowed with dgit push" if defined $package;
1538 my $clogp = parsechangelog();
1539 $package = getfield $clogp, 'Source';
1542 } elsif (@ARGV==1) {
1543 ($specsuite) = (@ARGV);
1545 badusage "incorrect arguments to dgit push";
1547 $isuite = getfield $clogp, 'Distribution';
1549 local ($package) = $existing_package; # this is a hack
1550 canonicalise_suite();
1552 if (defined $specsuite && $specsuite ne $isuite) {
1553 canonicalise_suite();
1554 $csuite eq $specsuite or
1555 fail "dgit push: changelog specifies $isuite ($csuite)".
1556 " but command line specifies $specsuite";
1558 if (check_for_git()) {
1561 if (fetch_from_archive()) {
1562 is_fast_fwd(lrref(), 'HEAD') or
1563 fail "dgit push: HEAD is not a descendant".
1564 " of the archive's version.\n".
1565 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1568 fail "package appears to be new in this suite;".
1569 " if this is intentional, use --new";
1574 #---------- remote commands' implementation ----------
1576 sub cmd_remote_push_responder {
1577 my ($nrargs) = shift @ARGV;
1578 my (@rargs) = @ARGV[0..$nrargs-1];
1579 @ARGV = @ARGV[$nrargs..$#ARGV];
1583 $we_are_responder = 1;
1585 open PI, "<&STDIN" or die $!;
1586 open STDIN, "/dev/null" or die $!;
1587 open PO, ">&STDOUT" or die $!;
1589 open STDOUT, ">&STDERR" or die $!;
1592 responder_send_command("dgit-remote-push-ready");
1604 printdebug "(killing remote child $i_child_pid)\n";
1605 kill 15, $i_child_pid;
1607 if (defined $i_tmp && !defined $initiator_tempdir) {
1609 eval { rmtree $i_tmp; };
1613 END { i_cleanup(); }
1616 my ($base,$selector,@args) = @_;
1617 $selector =~ s/\-/_/g;
1618 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1624 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1634 push @rdgit, @ropts;
1635 push @rdgit, qw(remote-push-responder), (scalar @rargs), @rargs;
1637 my @cmd = (@ssh, $host, shellquote @rdgit);
1638 printcmd \*DEBUG,$debugprefix."+",@cmd;
1640 if (defined $initiator_tempdir) {
1641 rmtree $initiator_tempdir;
1642 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1643 $i_tmp = $initiator_tempdir;
1647 $i_child_pid = open2(\*RO, \*RI, @cmd);
1649 initiator_expect { m/^dgit-remote-push-ready/ };
1651 my ($icmd,$iargs) = initiator_expect {
1652 m/^(\S+)(?: (.*))?$/;
1655 i_method "i_resp", $icmd, $iargs;
1659 sub i_resp_progress ($) {
1661 my $msg = protocol_read_bytes \*RO, $rhs;
1665 sub i_resp_complete {
1666 my $pid = $i_child_pid;
1667 $i_child_pid = undef; # prevents killing some other process with same pid
1668 printdebug "waiting for remote child $pid...\n";
1669 my $got = waitpid $pid, 0;
1670 die $! unless $got == $pid;
1671 die "remote child failed $?" if $?;
1674 printdebug "all done\n";
1678 sub i_resp_file ($) {
1680 my $localname = i_method "i_localname", $keyword;
1681 my $localpath = "$i_tmp/$localname";
1682 stat $localpath and badproto \*RO, "file $keyword ($localpath) twice";
1683 protocol_receive_file \*RO, $localpath;
1684 i_method "i_file", $keyword;
1689 sub i_resp_param ($) {
1690 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1696 sub i_resp_want ($) {
1698 die "$keyword ?" if $i_wanted{$keyword}++;
1699 my @localpaths = i_method "i_want", $keyword;
1700 printdebug "[[ $keyword @localpaths\n";
1701 foreach my $localpath (@localpaths) {
1702 protocol_send_file \*RI, $localpath;
1704 print RI "files-end\n" or die $!;
1707 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
1709 sub i_localname_parsed_changelog {
1710 return "remote-changelog.822";
1712 sub i_file_parsed_changelog {
1713 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
1714 push_parse_changelog "$i_tmp/remote-changelog.822";
1715 die if $i_dscfn =~ m#/|^\W#;
1718 sub i_localname_dsc {
1719 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1724 sub i_localname_changes {
1725 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1726 $i_changesfn = $i_dscfn;
1727 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
1728 return $i_changesfn;
1730 sub i_file_changes { }
1732 sub i_want_signed_tag {
1733 printdebug Dumper(\%i_param, $i_dscfn);
1734 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
1735 && defined $i_param{'csuite'}
1736 or badproto \*RO, "premature desire for signed-tag";
1737 my $head = $i_param{'head'};
1738 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
1740 die unless $i_param{'csuite'} =~ m/^$suite_re$/;
1742 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
1745 push_mktag $head, $i_clogp, $i_tag,
1747 $i_changesfn, 'remote changes',
1748 sub { "tag$_[0]"; };
1753 sub i_want_signed_dsc_changes {
1754 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
1755 sign_changes $i_changesfn;
1756 return ($i_dscfn, $i_changesfn);
1759 #---------- building etc. ----------
1765 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1767 sub build_maybe_quilt_fixup () {
1768 if (!open F, "debian/source/format") {
1769 die $! unless $!==&ENOENT;
1773 F->error and die $!;
1775 return unless madformat($_);
1778 my @cmd = (@git, qw(ls-files --exclude-standard -iodm));
1779 my $problems = cmdoutput @cmd;
1780 if (length $problems) {
1781 print STDERR "problematic files:\n";
1782 print STDERR " $_\n" foreach split /\n/, $problems;
1783 fail "Cannot do quilt fixup in tree containing ignored files. ".
1784 "Perhaps your package's clean target is broken, in which".
1785 " case -wg (which says to use git-clean -xdf) may help.";
1788 my $clogp = parsechangelog();
1789 my $version = getfield $clogp, 'Version';
1790 my $author = getfield $clogp, 'Maintainer';
1791 my $headref = rev_parse('HEAD');
1794 my $patchname = "auto-$version-$headref-$time";
1795 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1797 my $descfn = ".git/dgit/quilt-description.tmp";
1798 open O, '>', $descfn or die "$descfn: $!";
1800 $msg =~ s/^\s+$/ ./mg;
1801 print O <<END or die $!;
1802 Description: Automatically generated patch ($clogp->{Version})
1803 Last (up to) $ncommits git changes, FYI:
1813 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1814 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1815 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1816 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
1819 if (!open P, '>>', ".pc/applied-patches") {
1820 $!==&ENOENT or die $!;
1825 commit_quilty_patch();
1828 sub quilt_fixup_editor () {
1829 my $descfn = $ENV{$fakeeditorenv};
1830 my $editing = $ARGV[$#ARGV];
1831 open I1, '<', $descfn or die "$descfn: $!";
1832 open I2, '<', $editing or die "$editing: $!";
1833 unlink $editing or die "$editing: $!";
1834 open O, '>', $editing or die "$editing: $!";
1835 while (<I1>) { print O or die $!; } I1->error and die $!;
1838 $copying ||= m/^\-\-\- /;
1839 next unless $copying;
1842 I2->error and die $!;
1848 if ($cleanmode eq 'dpkg-source') {
1849 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
1850 } elsif ($cleanmode eq 'git') {
1851 runcmd_ordryrun_local @git, qw(clean -xdf);
1852 } elsif ($cleanmode eq 'none') {
1859 badusage "-p is not allowed when building" if defined $package;
1862 my $clogp = parsechangelog();
1863 $isuite = getfield $clogp, 'Distribution';
1864 $package = getfield $clogp, 'Source';
1865 $version = getfield $clogp, 'Version';
1866 build_maybe_quilt_fixup();
1869 sub changesopts () {
1870 my @opts =@changesopts[1..$#changesopts];
1871 if (!defined $changes_since_version) {
1872 my @vsns = archive_query('archive_query');
1873 my @quirk = access_quirk();
1874 if ($quirk[0] eq 'backports') {
1875 local $isuite = $quirk[2];
1877 canonicalise_suite();
1878 push @vsns, archive_query('archive_query');
1881 @vsns = map { $_->[0] } @vsns;
1882 @vsns = sort { -version_compare_string($a, $b) } @vsns;
1883 $changes_since_version = $vsns[0];
1884 progress "changelog will contain changes since $vsns[0]";
1886 $changes_since_version = '_';
1887 progress "package seems new, not specifying -v<version>";
1890 if ($changes_since_version ne '_') {
1891 unshift @opts, "-v$changes_since_version";
1898 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1899 printdone "build successful\n";
1905 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1906 "--git-builder=@dpkgbuildpackage");
1907 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1908 canonicalise_suite();
1909 push @cmd, "--git-debian-branch=".lbranch();
1911 push @cmd, changesopts();
1912 runcmd_ordryrun_local @cmd, @ARGV;
1913 printdone "build successful\n";
1918 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1919 $dscfn = dscfn($version);
1920 if ($cleanmode eq 'dpkg-source') {
1921 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
1924 my $pwd = must_getcwd();
1925 my $leafdir = basename $pwd;
1927 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
1929 runcmd_ordryrun_local qw(sh -ec),
1930 'exec >$1; shift; exec "$@"','x',
1931 "../$sourcechanges",
1932 @dpkggenchanges, qw(-S), changesopts();
1936 sub cmd_build_source {
1937 badusage "build-source takes no additional arguments" if @ARGV;
1939 printdone "source built, results in $dscfn and $sourcechanges";
1945 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1947 stat $dscfn or fail "$dscfn (in parent directory): $!";
1948 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1949 foreach my $cf (glob $pat) {
1950 next if $cf eq $sourcechanges;
1951 unlink $cf or fail "remove $cf: $!";
1954 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1955 my @changesfiles = glob $pat;
1956 @changesfiles = sort {
1957 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
1960 fail "wrong number of different changes files (@changesfiles)"
1961 unless @changesfiles;
1962 runcmd_ordryrun_local @mergechanges, @changesfiles;
1963 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1965 stat $multichanges or fail "$multichanges: $!";
1967 printdone "build successful, results in $multichanges\n" or die $!;
1970 sub cmd_quilt_fixup {
1971 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1972 my $clogp = parsechangelog();
1973 $version = getfield $clogp, 'Version';
1974 build_maybe_quilt_fixup();
1977 #---------- argument parsing and main program ----------
1980 print "dgit version $our_version\n" or die $!;
1987 if (defined $ENV{'DGIT_SSH'}) {
1988 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
1989 } elsif (defined $ENV{'GIT_SSH'}) {
1990 @ssh = ($ENV{'GIT_SSH'});
1994 last unless $ARGV[0] =~ m/^-/;
1998 if (m/^--dry-run$/) {
2001 } elsif (m/^--damp-run$/) {
2004 } elsif (m/^--no-sign$/) {
2007 } elsif (m/^--help$/) {
2009 } elsif (m/^--version$/) {
2011 } elsif (m/^--new$/) {
2014 } elsif (m/^--since-version=([^_]+|_)$/) {
2016 $changes_since_version = $1;
2017 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
2018 ($om = $opts_opt_map{$1}) &&
2022 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
2023 !$opts_opt_cmdonly{$1} &&
2024 ($om = $opts_opt_map{$1})) {
2027 } elsif (m/^--existing-package=(.*)/s) {
2029 $existing_package = $1;
2030 } elsif (m/^--initiator-tempdir=(.*)/s) {
2031 $initiator_tempdir = $1;
2032 $initiator_tempdir =~ m#^/# or
2033 badusage "--initiator-tempdir must be used specify an".
2034 " absolute, not relative, directory."
2035 } elsif (m/^--distro=(.*)/s) {
2038 } elsif (m/^--build-products-dir=(.*)/s) {
2040 $buildproductsdir = $1;
2041 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
2044 } elsif (m/^--clean=(.*)$/s) {
2045 badusage "unknown cleaning mode \`$1'";
2046 } elsif (m/^--ignore-dirty$/s) {
2049 } elsif (m/^--no-quilt-fixup$/s) {
2053 badusage "unknown long option \`$_'";
2060 } elsif (s/^-L/-/) {
2063 } elsif (s/^-h/-/) {
2065 } elsif (s/^-D/-/) {
2067 open DEBUG, ">&STDERR" or die $!;
2070 } elsif (s/^-N/-/) {
2073 } elsif (s/^-v([^_]+|_)$//s) {
2075 $changes_since_version = $1;
2078 push @changesopts, $_;
2080 } elsif (s/^-c(.*=.*)//s) {
2082 push @git, '-c', $1;
2083 } elsif (s/^-d(.*)//s) {
2086 } elsif (s/^-C(.*)//s) {
2089 if ($changesfile =~ s#^(.*)/##) {
2090 $buildproductsdir = $1;
2092 } elsif (s/^-k(.*)//s) {
2094 } elsif (s/^-wn//s) {
2096 $cleanmode = 'none';
2097 } elsif (s/^-wg//s) {
2100 } elsif (s/^-wd//s) {
2102 $cleanmode = 'dpkg-source';
2104 badusage "unknown short option \`$_'";
2111 if ($ENV{$fakeeditorenv}) {
2112 quilt_fixup_editor();
2115 delete $ENV{'DGET_UNPACK'};
2118 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2119 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2120 if $dryrun_level == 1;
2122 print STDERR $helpmsg or die $!;
2125 my $cmd = shift @ARGV;
2127 { no strict qw(refs); &{"cmd_$cmd"}(); }