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)");
56 our (@dget) = qw(dget);
57 our (@dput) = qw(dput);
58 our (@debsign) = qw(debsign);
60 our (@sbuild) = qw(sbuild -A);
62 our (@dgit) = qw(dgit);
63 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
64 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
65 our (@dpkggenchanges) = qw(dpkg-genchanges);
66 our (@mergechanges) = qw(mergechanges -f);
67 our (@changesopts) = ('');
69 our %opts_opt_map = ('dget' => \@dget,
71 'debsign' => \@debsign,
76 'dpkg-source' => \@dpkgsource,
77 'dpkg-buildpackage' => \@dpkgbuildpackage,
78 'dpkg-genchanges' => \@dpkggenchanges,
79 'ch' => \@changesopts,
80 'mergechanges' => \@mergechanges);
82 our %opts_opt_cmdonly = ('gpg' => 1);
87 open DEBUG, ">/dev/null" or die $!;
91 our $remotename = 'dgit';
92 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
93 our $branchprefix = 'dgit';
96 sub lbranch () { return "$branchprefix/$csuite"; }
97 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
98 sub lref () { return "refs/heads/".lbranch(); }
99 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
100 sub rrref () { return "refs/$branchprefix/$csuite"; }
115 return "${package}_".(stripepoch $vsn).".dsc";
119 our $debugprefix = '';
121 sub printdebug { print DEBUG $debugprefix, @_ or die $!; }
124 die $us.($we_are_responder ? " (build host)" : "").": @_\n";
127 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
129 sub no_such_package () {
130 print STDERR "$us: package $package does not exist in suite $isuite\n";
136 return "+".rrref().":".lrref();
141 printdebug "CD $newdir\n";
142 chdir $newdir or die "chdir: $newdir: $!";
145 #---------- remote protocol support, common ----------
147 # remote push initiator/responder protocol:
148 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
150 # > file parsed-changelog
151 # [indicates that output of dpkg-parsechangelog follows]
152 # > data-block NBYTES
153 # > [NBYTES bytes of data (no newline)]
154 # [maybe some more blocks]
166 # [indicates that signed tag is wanted]
167 # < data-block NBYTES
168 # < [NBYTES bytes of data (no newline)]
169 # [maybe some more blocks]
173 # > want signed-dsc-changes
174 # < data-block NBYTES [transfer of signed dsc]
176 # < data-block NBYTES [transfer of signed changes]
184 fail "connection lost: $!" if $fh->error;
185 fail "protocol violation; $m not expected";
188 sub protocol_expect (&$) {
189 my ($match, $fh) = @_;
192 defined && chomp or badproto $fh, "eof";
200 badproto $fh, "\`$_'";
203 sub protocol_send_file ($$) {
204 my ($fh, $ourfn) = @_;
205 open PF, "<", $ourfn or die "$ourfn: $!";
208 my $got = read PF, $d, 65536;
209 die "$ourfn: $!" unless defined $got;
211 print $fh "data-block ".length($d)."\n" or die $!;
212 print $fh $d or die $!;
214 PF->error and die "$ourfn $!";
215 print $fh "data-end\n" or die $!;
219 sub protocol_read_bytes ($$) {
220 my ($fh, $nbytes) = @_;
221 $nbytes =~ m/^[1-9]\d{0,5}$/ or badproto \*RO, "bad byte count";
223 my $got = read $fh, $d, $nbytes;
224 $got==$nbytes or badproto $fh, "eof during data block";
228 sub protocol_receive_file ($$) {
229 my ($fh, $ourfn) = @_;
230 printdebug "() $ourfn\n";
231 open PF, ">", $ourfn or die "$ourfn: $!";
233 my ($y,$l) = protocol_expect {
234 m/^data-block (.*)$/ ? (1,$1) :
235 m/^data-end$/ ? (0,) :
239 my $d = protocol_read_bytes $fh, $l;
240 print PF $d or die $!;
245 #---------- remote protocol support, responder ----------
247 sub responder_send_command ($) {
249 return unless $we_are_responder;
250 # called even without $we_are_responder
251 printdebug ">> $command\n";
252 print PO $command, "\n" or die $!;
255 sub responder_send_file ($$) {
256 my ($keyword, $ourfn) = @_;
257 return unless $we_are_responder;
258 printdebug "]] $keyword $ourfn\n";
259 responder_send_command "file $keyword";
260 protocol_send_file \*PO, $ourfn;
263 sub responder_receive_files ($@) {
264 my ($keyword, @ourfns) = @_;
265 die unless $we_are_responder;
266 printdebug "[[ $keyword @ourfns\n";
267 responder_send_command "want $keyword";
268 foreach my $fn (@ourfns) {
269 protocol_receive_file \*PI, $fn;
272 protocol_expect { m/^files-end$/ } \*PI;
275 #---------- remote protocol support, initiator ----------
277 sub initiator_expect (&) {
279 protocol_expect { &$match } \*RO;
282 #---------- end remote code ----------
285 if ($we_are_responder) {
287 responder_send_command "progress ".length($m) or die $!;
288 print PO $m or die $!;
298 $ua = LWP::UserAgent->new();
302 progress "downloading $what...";
303 my $r = $ua->get(@_) or die $!;
304 return undef if $r->code == 404;
305 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
306 return $r->decoded_content();
309 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
316 if (m{[^-=_./0-9a-z]}i) {
323 return join ' ', @out;
328 my $intro = shift @_;
329 print $fh $intro," " or die $!;
330 print $fh shellquote @_ or die $!;
331 print $fh "\n" or die $!;
335 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
337 fail "failed to fork/exec: $!";
338 } elsif (!($? & 0xff)) {
339 fail "subprocess failed with error exit status ".($?>>8);
341 fail "subprocess crashed (wait status $?)";
343 fail "subprocess produced invalid output";
348 printcmd(\*DEBUG,$debugprefix."+",@_) if $debug>0;
350 failedcmd @_ if system @_;
353 sub act_local () { return $dryrun_level <= 1; }
354 sub act_scary () { return !$dryrun_level; }
357 if (!$dryrun_level) {
358 progress "dgit ok: @_";
360 progress "would be ok: @_ (but dry run only)";
364 sub cmdoutput_errok {
365 die Dumper(\@_)." ?" if grep { !defined } @_;
366 printcmd(\*DEBUG,$debugprefix."|",@_) if $debug>0;
367 open P, "-|", @_ or die $!;
370 { local $/ = undef; $d = <P>; }
372 if (!close P) { printdebug "=>!$?\n" if $debug>0; return undef; }
375 printdebug "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
380 my $d = cmdoutput_errok @_;
381 defined $d or failedcmd @_;
386 printcmd(\*STDERR,$debugprefix."#",@_);
389 sub runcmd_ordryrun {
397 sub runcmd_ordryrun_local {
406 my ($first_shell, @cmd) = @_;
407 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
410 our $helpmsg = <<END;
412 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
413 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
414 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
415 dgit [dgit-opts] push [dgit-opts] [suite]
416 dgit [dgit-opts] rpush build-host:build-dir ...
417 important dgit options:
418 -k<keyid> sign tag and package with <keyid> instead of default
419 --dry-run -n do not change anything, but go through the motions
420 --damp-run -L like --dry-run but make local changes, without signing
421 --new -N allow introducing a new package
422 --debug -D increase debug level
423 -c<name>=<value> set git config option (used directly by dgit too)
426 our $later_warning_msg = <<END;
427 Perhaps the upload is stuck in incoming. Using the version from git.
431 print STDERR "$us: @_\n", $helpmsg or die $!;
436 @ARGV or badusage "too few arguments";
437 return scalar shift @ARGV;
441 print $helpmsg or die $!;
445 our $td = $ENV{DGIT_TEST_DUMMY_DIR} || "DGIT_TEST_DUMMY_DIR-unset";
447 our %defcfg = ('dgit.default.distro' => 'debian',
448 'dgit.default.username' => '',
449 'dgit.default.archive-query-default-component' => 'main',
450 'dgit.default.ssh' => 'ssh',
451 'dgit-distro.debian.git-host' => 'git.debian.org',
452 'dgit-distro.debian.git-proto' => 'git+ssh://',
453 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
454 'dgit-distro.debian.git-check' => 'ssh-cmd',
455 'dgit-distro.debian.git-create' => 'ssh-cmd',
456 'dgit-distro.debian.sshpsql-host' => 'coccia.debian.org',
457 'dgit-distro.debian.sshpsql-dbname' => 'service=projectb',
458 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
459 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/',
460 'dgit-distro.test-dummy.ssh' => "$td/ssh",
461 'dgit-distro.test-dummy.username' => "alice",
462 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
463 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
464 'dgit-distro.test-dummy.git-url' => "$td/git",
465 'dgit-distro.test-dummy.git-host' => "git",
466 'dgit-distro.test-dummy.git-path' => "$td/git",
467 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
468 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
469 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
474 return undef if $c =~ /RETURN-UNDEF/;
475 my @cmd = (@git, qw(config --), $c);
478 local ($debug) = $debug-1;
479 $v = cmdoutput_errok @cmd;
486 my $dv = $defcfg{$c};
487 return $dv if defined $dv;
489 badcfg "need value for one of: @_";
492 sub access_distro () {
493 return cfg("dgit-suite.$isuite.distro",
494 "dgit.default.distro");
499 my $distro = $idistro || access_distro();
500 my $value = cfg(map { ("dgit-distro.$distro.$_",
501 "dgit.default.$_") } @keys);
505 sub string_to_ssh ($) {
507 if ($spec =~ m/\s/) {
508 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
514 sub access_cfg_ssh () {
515 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
516 if (!defined $gitssh) {
519 return string_to_ssh $gitssh;
523 sub access_someuserhost ($) {
525 my $user = access_cfg("$some-user",'username');
526 my $host = access_cfg("$some-host");
527 return length($user) ? "$user\@$host" : $host;
530 sub access_gituserhost () {
531 return access_someuserhost('git');
534 sub access_giturl () {
535 my $url = access_cfg('git-url','RETURN-UNDEF');
538 access_cfg('git-proto').
539 access_gituserhost().
540 access_cfg('git-path');
542 return "$url/$package.git";
545 sub parsecontrolfh ($$@) {
546 my ($fh, $desc, @opts) = @_;
547 my %opts = ('name' => $desc, @opts);
548 my $c = Dpkg::Control::Hash->new(%opts);
549 $c->parse($fh) or die "parsing of $desc failed";
554 my ($file, $desc) = @_;
555 my $fh = new IO::Handle;
556 open $fh, '<', $file or die "$file: $!";
557 my $c = parsecontrolfh($fh,$desc);
558 $fh->error and die $!;
564 my ($dctrl,$field) = @_;
565 my $v = $dctrl->{$field};
566 return $v if defined $v;
567 fail "missing field $field in ".$v->get_option('name');
571 my $c = Dpkg::Control::Hash->new();
572 my $p = new IO::Handle;
573 my @cmd = (qw(dpkg-parsechangelog), @_);
574 open $p, '-|', @cmd or die $!;
576 $?=0; $!=0; close $p or failedcmd @cmd;
580 sub git_get_ref ($) {
582 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
584 $?==256 or fail "git show-ref failed (status $?)";
585 printdebug "ref $refname= [show-ref exited 1]\n";
588 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
589 printdebug "ref $refname=$1\n";
592 printdebug "ref $refname= [no match]\n";
599 sub archive_query ($) {
601 my $query = access_cfg('archive-query','RETURN-UNDEF');
602 if (!defined $query) {
603 my $distro = access_distro();
604 if ($distro eq 'debian') {
606 access_someuserhost('sshpsql').':'.
607 access_cfg('sshpsql-dbname');
609 $query = "madison:$distro";
612 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
615 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
618 sub pool_dsc_subpath ($$) {
619 my ($vsn,$component) = @_; # $package is implict arg
620 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
621 return "/pool/$component/$prefix/$package/".dscfn($vsn);
624 sub archive_query_madison ($$) {
625 my ($proto,$data) = @_;
626 die unless $proto eq 'madison';
627 $rmad{$package} ||= cmdoutput
628 qw(rmadison -asource),"-s$isuite","-u$data",$package;
629 my $rmad = $rmad{$package};
630 return madison_parse($rmad);
633 sub madison_parse ($) {
636 foreach my $l (split /\n/, $rmad) {
637 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
638 \s*( [^ \t|]+ )\s* \|
639 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
640 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
641 $1 eq $package or die "$rmad $package ?";
648 $component = access_cfg('archive-query-default-component');
650 $5 eq 'source' or die "$rmad ?";
651 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
653 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
656 sub canonicalise_suite_madison ($$) {
657 # madison canonicalises for us
658 my @r = archive_query_madison($_[0],$_[1]);
660 "unable to canonicalise suite using package $package".
661 " which does not appear to exist in suite $isuite;".
662 " --existing-package may help";
667 my ($data,$sql) = @_;
668 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
669 my ($userhost,$dbname) = ($`,$'); #';
671 my @cmd = (access_cfg_ssh, $userhost,
672 "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
673 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
674 open P, "-|", @cmd or die $!;
677 printdebug("$debugprefix>|$_|\n");
680 $!=0; $?=0; close P or failedcmd @cmd;
682 my $nrows = pop @rows;
683 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
684 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
685 @rows = map { [ split /\|/, $_ ] } @rows;
686 my $ncols = scalar @{ shift @rows };
687 die if grep { scalar @$_ != $ncols } @rows;
691 sub sql_injection_check {
692 foreach (@_) { die "$_ $& ?" if m/[']/; }
695 sub archive_query_sshpsql ($$) {
696 my ($proto,$data) = @_;
697 sql_injection_check $isuite, $package;
698 my @rows = sshpsql($data, <<END);
699 SELECT source.version, component.name, files.filename
701 JOIN src_associations ON source.id = src_associations.source
702 JOIN suite ON suite.id = src_associations.suite
703 JOIN dsc_files ON dsc_files.source = source.id
704 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
705 JOIN component ON component.id = files_archive_map.component_id
706 JOIN files ON files.id = dsc_files.file
707 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
708 AND source.source='$package'
709 AND files.filename LIKE '%.dsc';
711 @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
713 my ($vsn,$component,$filename) = @$_;
714 [ $vsn, "/pool/$component/$filename" ];
719 sub canonicalise_suite_sshpsql ($$) {
720 my ($proto,$data) = @_;
721 sql_injection_check $isuite;
722 my @rows = sshpsql($data, <<END);
723 SELECT suite.codename
724 FROM suite where suite_name='$isuite' or codename='$isuite';
726 @rows = map { $_->[0] } @rows;
727 fail "unknown suite $isuite" unless @rows;
728 die "ambiguous $isuite: @rows ?" if @rows>1;
732 sub canonicalise_suite_dummycat ($$) {
733 my ($proto,$data) = @_;
734 my $dpath = "$data/suite.$isuite";
735 if (!open C, "<", $dpath) {
736 $!==ENOENT or die "$dpath: $!";
737 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
741 chomp or die "$dpath: $!";
743 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
747 sub archive_query_dummycat ($$) {
748 my ($proto,$data) = @_;
749 canonicalise_suite();
750 my $dpath = "$data/package.$csuite.$package";
751 if (!open C, "<", $dpath) {
752 $!==ENOENT or die "$dpath: $!";
753 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
761 printdebug "dummycat query $csuite $package $dpath | $_\n";
762 my @row = split /\s+/, $_;
763 @row==2 or die "$dpath: $_ ?";
766 C->error and die "$dpath: $!";
768 return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
771 sub canonicalise_suite () {
772 return if defined $csuite;
773 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
774 $csuite = archive_query('canonicalise_suite');
775 if ($isuite ne $csuite) {
776 progress "canonical suite name for $isuite is $csuite";
780 sub get_archive_dsc () {
781 canonicalise_suite();
782 my @vsns = archive_query('archive_query');
783 foreach my $vinfo (@vsns) {
784 my ($vsn,$subpath) = @$vinfo;
785 $dscurl = access_cfg('mirror').$subpath;
786 $dscdata = url_get($dscurl);
788 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
791 my $dscfh = new IO::File \$dscdata, '<' or die $!;
792 printdebug Dumper($dscdata) if $debug>1;
793 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
794 printdebug Dumper($dsc) if $debug>1;
795 my $fmt = getfield $dsc, 'Format';
796 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
802 sub check_for_git () {
804 my $how = access_cfg('git-check');
805 if ($how eq 'ssh-cmd') {
807 (access_cfg_ssh, access_gituserhost(),
808 " set -e; cd ".access_cfg('git-path').";".
809 " if test -d $package.git; then echo 1; else echo 0; fi");
810 my $r= cmdoutput @cmd;
811 failedcmd @cmd unless $r =~ m/^[01]$/;
814 badcfg "unknown git-check \`$how'";
818 sub create_remote_git_repo () {
819 my $how = access_cfg('git-create');
820 if ($how eq 'ssh-cmd') {
822 (access_cfg_ssh, access_gituserhost(),
823 "set -e; cd ".access_cfg('git-path').";".
824 " cp -a _template $package.git");
826 badcfg "unknown git-create \`$how'";
830 our ($dsc_hash,$lastpush_hash);
832 our $ud = '.git/dgit/unpack';
840 sub mktree_in_ud_from_only_subdir () {
841 # changes into the subdir
844 $dirs[0] =~ m#^([^/]+)/\.$# or die;
847 fail "source package contains .git directory" if stat '.git';
848 die $! unless $!==&ENOENT;
849 runcmd qw(git init -q);
850 rmtree('.git/objects');
851 symlink '../../../../objects','.git/objects' or die $!;
852 runcmd @git, qw(add -Af);
853 my $tree = cmdoutput @git, qw(write-tree);
854 $tree =~ m/^\w+$/ or die "$tree ?";
858 sub dsc_files_info () {
859 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
860 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
861 ['Files', 'Digest::MD5', 'new()']) {
862 my ($fname, $module, $method) = @$csumi;
863 my $field = $dsc->{$fname};
864 next unless defined $field;
865 eval "use $module; 1;" or die $@;
867 foreach (split /\n/, $field) {
869 m/^(\w+) (\d+) (\S+)$/ or
870 fail "could not parse .dsc $fname line \`$_'";
871 my $digester = eval "$module"."->$method;" or die $@;
876 Digester => $digester,
881 fail "missing any supported Checksums-* or Files field in ".
882 $dsc->get_option('name');
886 map { $_->{Filename} } dsc_files_info();
889 sub is_orig_file ($) {
891 m/\.orig(?:-\w+)?\.tar\.\w+$/;
894 sub make_commit ($) {
896 return cmdoutput @git, qw(hash-object -w -t commit), $file;
899 sub clogp_authline ($) {
901 my $author = getfield $clogp, 'Maintainer';
902 $author =~ s#,.*##ms;
903 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
904 my $authline = "$author $date";
905 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
906 fail "unexpected commit author line format \`$authline'".
907 " (was generated from changelog Maintainer field)";
911 sub generate_commit_from_dsc () {
915 foreach my $f (dsc_files()) {
916 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
918 link "../../../$f", $f
922 runcmd @dget, qw(--), $dscurl;
923 foreach my $f (grep { is_orig_file($_) } @files) {
924 link $f, "../../../../$f"
928 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
929 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
930 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
931 my $authline = clogp_authline $clogp;
932 my $changes = getfield $clogp, 'Changes';
933 open C, ">../commit.tmp" or die $!;
934 print C <<END or die $!;
941 # imported from the archive
944 my $outputhash = make_commit qw(../commit.tmp);
945 my $cversion = getfield $clogp, 'Version';
946 progress "synthesised git commit from .dsc $cversion";
947 if ($lastpush_hash) {
948 runcmd @git, qw(reset --hard), $lastpush_hash;
949 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
950 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
951 my $oversion = getfield $oldclogp, 'Version';
953 version_compare_string($oversion, $cversion);
955 # git upload/ is earlier vsn than archive, use archive
956 open C, ">../commit2.tmp" or die $!;
957 print C <<END or die $!;
959 parent $lastpush_hash
964 Record $package ($cversion) in archive suite $csuite
966 $outputhash = make_commit qw(../commit2.tmp);
967 } elsif ($vcmp > 0) {
968 print STDERR <<END or die $!;
970 Version actually in archive: $cversion (older)
971 Last allegedly pushed/uploaded: $oversion (newer or same)
974 $outputhash = $lastpush_hash;
976 $outputhash = $lastpush_hash;
979 changedir '../../../..';
980 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
981 'DGIT_ARCHIVE', $outputhash;
982 cmdoutput @git, qw(log -n2), $outputhash;
983 # ... gives git a chance to complain if our commit is malformed
988 sub ensure_we_have_orig () {
989 foreach my $fi (dsc_files_info()) {
990 my $f = $fi->{Filename};
991 next unless is_orig_file($f);
992 if (open F, "<", "../$f") {
993 $fi->{Digester}->reset();
994 $fi->{Digester}->addfile(*F);
996 my $got = $fi->{Digester}->hexdigest();
997 $got eq $fi->{Hash} or
998 fail "existing file $f has hash $got but .dsc".
999 " demands hash $fi->{Hash}".
1000 " (perhaps you should delete this file?)";
1001 progress "using existing $f";
1004 die "$f $!" unless $!==&ENOENT;
1006 my $origurl = $dscurl;
1007 $origurl =~ s{/[^/]+$}{};
1009 die "$f ?" unless $f =~ m/^${package}_/;
1010 die "$f ?" if $f =~ m#/#;
1011 runcmd_ordryrun_local shell_cmd 'cd ..', @dget,'--',$origurl;
1016 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1019 sub is_fast_fwd ($$) {
1020 my ($ancestor,$child) = @_;
1021 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1022 my $mb = cmdoutput_errok @cmd;
1024 return rev_parse($mb) eq rev_parse($ancestor);
1026 $?==256 or failedcmd @cmd;
1031 sub git_fetch_us () {
1032 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1035 sub fetch_from_archive () {
1036 # ensures that lrref() is what is actually in the archive,
1037 # one way or another
1041 foreach my $field (@ourdscfield) {
1042 $dsc_hash = $dsc->{$field};
1043 last if defined $dsc_hash;
1045 if (defined $dsc_hash) {
1046 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1048 progress "last upload to archive specified git hash";
1050 progress "last upload to archive has NO git hash";
1053 progress "no version available from the archive";
1056 $lastpush_hash = git_get_ref(lrref());
1057 printdebug "previous reference hash=$lastpush_hash\n";
1059 if (defined $dsc_hash) {
1060 fail "missing remote git history even though dsc has hash -".
1061 " could not find ref ".lrref().
1062 " (should have been fetched from ".access_giturl()."#".rrref().")"
1063 unless $lastpush_hash;
1065 ensure_we_have_orig();
1066 if ($dsc_hash eq $lastpush_hash) {
1067 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1068 print STDERR <<END or die $!;
1070 Git commit in archive is behind the last version allegedly pushed/uploaded.
1071 Commit referred to by archive: $dsc_hash
1072 Last allegedly pushed/uploaded: $lastpush_hash
1075 $hash = $lastpush_hash;
1077 fail "archive's .dsc refers to ".$dsc_hash.
1078 " but this is an ancestor of ".$lastpush_hash;
1081 $hash = generate_commit_from_dsc();
1082 } elsif ($lastpush_hash) {
1083 # only in git, not in the archive yet
1084 $hash = $lastpush_hash;
1085 print STDERR <<END or die $!;
1087 Package not found in the archive, but has allegedly been pushed using dgit.
1091 printdebug "nothing found!\n";
1092 if (defined $skew_warning_vsn) {
1093 print STDERR <<END or die $!;
1095 Warning: relevant archive skew detected.
1096 Archive allegedly contains $skew_warning_vsn
1097 But we were not able to obtain any version from the archive or git.
1103 printdebug "current hash=$hash\n";
1104 if ($lastpush_hash) {
1105 fail "not fast forward on last upload branch!".
1106 " (archive's version left in DGIT_ARCHIVE)"
1107 unless is_fast_fwd($lastpush_hash, $hash);
1109 if (defined $skew_warning_vsn) {
1111 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1112 my $clogf = ".git/dgit/changelog.tmp";
1113 runcmd shell_cmd "exec >$clogf",
1114 @git, qw(cat-file blob), "$hash:debian/changelog";
1115 my $gotclogp = parsechangelog("-l$clogf");
1116 my $got_vsn = getfield $gotclogp, 'Version';
1117 printdebug "SKEW CHECK GOT $got_vsn\n";
1118 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
1119 print STDERR <<END or die $!;
1121 Warning: archive skew detected. Using the available version:
1122 Archive allegedly contains $skew_warning_vsn
1123 We were able to obtain only $got_vsn
1128 if ($lastpush_hash ne $hash) {
1129 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1133 dryrun_report @upd_cmd;
1141 canonicalise_suite();
1142 badusage "dry run makes no sense with clone" unless act_local();
1143 mkdir $dstdir or die "$dstdir $!";
1145 runcmd @git, qw(init -q);
1146 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1147 open H, "> .git/HEAD" or die $!;
1148 print H "ref: ".lref()."\n" or die $!;
1150 runcmd @git, qw(remote add), 'origin', access_giturl();
1151 if (check_for_git()) {
1152 progress "fetching existing git history";
1154 runcmd_ordryrun_local @git, qw(fetch origin);
1156 progress "starting new git history";
1158 fetch_from_archive() or no_such_package;
1159 runcmd @git, qw(reset --hard), lrref();
1160 printdone "ready for work in $dstdir";
1164 if (check_for_git()) {
1167 fetch_from_archive() or no_such_package();
1168 printdone "fetched into ".lrref();
1173 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1175 printdone "fetched to ".lrref()." and merged into HEAD";
1178 sub check_not_dirty () {
1179 return if $ignoredirty;
1180 my @cmd = (@git, qw(diff --quiet HEAD));
1181 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1182 $!=0; $?=0; system @cmd;
1183 return if !$! && !$?;
1184 if (!$! && $?==256) {
1185 fail "working tree is dirty (does not match HEAD)";
1191 sub commit_quilty_patch () {
1192 my $output = cmdoutput @git, qw(status --porcelain);
1194 foreach my $l (split /\n/, $output) {
1195 next unless $l =~ m/\S/;
1196 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1201 progress "nothing quilty to commit, ok.";
1204 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1205 my $m = "Commit Debian 3.0 (quilt) metadata";
1207 runcmd_ordryrun_local @git, qw(commit -m), $m;
1212 return 0 unless $format eq '3.0 (quilt)';
1213 progress "Format \`$format', urgh";
1215 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1221 sub push_parse_changelog ($) {
1224 my $clogp = Dpkg::Control::Hash->new();
1225 $clogp->load($clogpfn) or die;
1227 $package = getfield $clogp, 'Source';
1228 my $cversion = getfield $clogp, 'Version';
1229 my $tag = debiantag($cversion);
1230 runcmd @git, qw(check-ref-format), $tag;
1232 my $dscfn = dscfn($cversion);
1234 return ($clogp, $cversion, $tag, $dscfn);
1237 sub push_parse_dsc ($$$) {
1238 my ($dscfn,$dscfnwhat, $cversion) = @_;
1239 $dsc = parsecontrol($dscfn,$dscfnwhat);
1240 my $dversion = getfield $dsc, 'Version';
1241 my $dscpackage = getfield $dsc, 'Source';
1242 ($dscpackage eq $package && $dversion eq $cversion) or
1243 fail "$dscfn is for $dscpackage $dversion".
1244 " but debian/changelog is for $package $cversion";
1247 sub push_mktag ($$$$$$$) {
1248 my ($head,$clogp,$tag,
1250 $changesfile,$changesfilewhat,
1253 $dsc->{$ourdscfield[0]} = $head;
1254 $dsc->save("$dscfn.tmp") or die $!;
1256 my $changes = parsecontrol($changesfile,$changesfilewhat);
1257 foreach my $field (qw(Source Distribution Version)) {
1258 $changes->{$field} eq $clogp->{$field} or
1259 fail "changes field $field \`$changes->{$field}'".
1260 " does not match changelog \`$clogp->{$field}'";
1263 my $cversion = getfield $clogp, 'Version';
1264 my $clogsuite = getfield $clogp, 'Distribution';
1266 # We make the git tag by hand because (a) that makes it easier
1267 # to control the "tagger" (b) we can do remote signing
1268 my $authline = clogp_authline $clogp;
1269 open TO, '>', $tfn->('.tmp') or die $!;
1270 print TO <<END or die $!;
1276 $package release $cversion for $clogsuite [dgit]
1280 my $tagobjfn = $tfn->('.tmp');
1282 if (!defined $keyid) {
1283 $keyid = access_cfg('keyid','RETURN-UNDEF');
1285 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1286 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1287 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1288 push @sign_cmd, $tfn->('.tmp');
1289 runcmd_ordryrun @sign_cmd;
1291 $tagobjfn = $tfn->('.signed.tmp');
1292 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1293 $tfn->('.tmp'), $tfn->('.tmp.asc');
1300 sub sign_changes ($) {
1301 my ($changesfile) = @_;
1303 my @debsign_cmd = @debsign;
1304 push @debsign_cmd, "-k$keyid" if defined $keyid;
1305 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1306 push @debsign_cmd, $changesfile;
1307 runcmd_ordryrun @debsign_cmd;
1312 printdebug "actually entering push\n";
1315 my $clogpfn = ".git/dgit/changelog.822.tmp";
1316 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1318 responder_send_file('parsed-changelog', $clogpfn);
1320 my ($clogp, $cversion, $tag, $dscfn) =
1321 push_parse_changelog("$clogpfn");
1323 my $dscpath = "$buildproductsdir/$dscfn";
1325 fail "looked for .dsc $dscfn, but $!;".
1326 " maybe you forgot to build";
1328 responder_send_file('dsc', $dscpath);
1330 push_parse_dsc($dscpath, $dscfn, $cversion);
1332 my $format = getfield $dsc, 'Format';
1333 printdebug "format $format\n";
1334 if (madformat($format)) {
1335 commit_quilty_patch();
1339 progress "checking that $dscfn corresponds to HEAD";
1340 runcmd qw(dpkg-source -x --),
1341 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1342 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1343 changedir '../../../..';
1344 my @diffcmd = (@git, qw(diff --exit-code), $tree);
1345 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1347 if (system @diffcmd) {
1348 if ($! && $?==256) {
1349 fail "$dscfn specifies a different tree to your HEAD commit;".
1350 " perhaps you forgot to build";
1356 #do fast forward check and maybe fake merge
1357 # if (!is_fast_fwd(mainbranch
1358 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1359 # map { lref($_).":".rref($_) }
1361 my $head = rev_parse('HEAD');
1362 if (!$changesfile) {
1363 my $multi = "$buildproductsdir/".
1364 "${package}_".(stripepoch $cversion)."_multi.changes";
1365 if (stat "$multi") {
1366 $changesfile = $multi;
1368 $!==&ENOENT or die "$multi: $!";
1369 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1370 my @cs = glob "$buildproductsdir/$pat";
1371 fail "failed to find unique changes file".
1372 " (looked for $pat in $buildproductsdir, or $multi);".
1373 " perhaps you need to use dgit -C"
1375 ($changesfile) = @cs;
1378 $changesfile = "$buildproductsdir/$changesfile";
1381 responder_send_file('changes',$changesfile);
1382 responder_send_command("param head $head");
1384 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1387 if ($we_are_responder) {
1388 $tagobjfn = $tfn->('.signed.tmp');
1389 responder_receive_files('signed-tag', $tagobjfn);
1392 push_mktag($head,$clogp,$tag,
1394 $changesfile,$changesfile,
1398 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1399 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1400 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1401 runcmd_ordryrun @git, qw(tag -v --), $tag;
1403 if (!check_for_git()) {
1404 create_remote_git_repo();
1406 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
1407 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1409 if (!$we_are_responder) {
1411 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1413 progress "[new .dsc left in $dscpath.tmp]";
1417 if ($we_are_responder) {
1418 my $dryrunsuffix = act_local() ? "" : ".tmp";
1419 responder_receive_files('signed-dsc-changes',
1420 "$dscpath$dryrunsuffix",
1421 "$changesfile$dryrunsuffix");
1423 sign_changes $changesfile;
1426 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
1427 my $host = access_cfg('upload-host','RETURN-UNDEF');
1428 my @hostarg = defined($host) ? ($host,) : ();
1429 runcmd_ordryrun @dput, @hostarg, $changesfile;
1430 printdone "pushed and uploaded $cversion";
1432 responder_send_command("complete");
1438 badusage "-p is not allowed with clone; specify as argument instead"
1439 if defined $package;
1442 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1443 ($package,$isuite) = @ARGV;
1444 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1445 ($package,$dstdir) = @ARGV;
1446 } elsif (@ARGV==3) {
1447 ($package,$isuite,$dstdir) = @ARGV;
1449 badusage "incorrect arguments to dgit clone";
1451 $dstdir ||= "$package";
1455 sub branchsuite () {
1456 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1457 if ($branch =~ m#$lbranch_re#o) {
1464 sub fetchpullargs () {
1465 if (!defined $package) {
1466 my $sourcep = parsecontrol('debian/control','debian/control');
1467 $package = getfield $sourcep, 'Source';
1470 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1472 my $clogp = parsechangelog();
1473 $isuite = getfield $clogp, 'Distribution';
1475 canonicalise_suite();
1476 progress "fetching from suite $csuite";
1477 } elsif (@ARGV==1) {
1479 canonicalise_suite();
1481 badusage "incorrect arguments to dgit fetch or dgit pull";
1499 badusage "-p is not allowed with dgit push" if defined $package;
1501 my $clogp = parsechangelog();
1502 $package = getfield $clogp, 'Source';
1505 } elsif (@ARGV==1) {
1506 ($specsuite) = (@ARGV);
1508 badusage "incorrect arguments to dgit push";
1510 $isuite = getfield $clogp, 'Distribution';
1512 local ($package) = $existing_package; # this is a hack
1513 canonicalise_suite();
1515 if (defined $specsuite && $specsuite ne $isuite) {
1516 canonicalise_suite();
1517 $csuite eq $specsuite or
1518 fail "dgit push: changelog specifies $isuite ($csuite)".
1519 " but command line specifies $specsuite";
1521 if (check_for_git()) {
1524 if (fetch_from_archive()) {
1525 is_fast_fwd(lrref(), 'HEAD') or
1526 fail "dgit push: HEAD is not a descendant".
1527 " of the archive's version.\n".
1528 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1531 fail "package appears to be new in this suite;".
1532 " if this is intentional, use --new";
1537 #---------- remote commands' implementation ----------
1539 sub cmd_remote_push_responder {
1540 my ($nrargs) = shift @ARGV;
1541 my (@rargs) = @ARGV[0..$nrargs-1];
1542 @ARGV = @ARGV[$nrargs..$#ARGV];
1546 $we_are_responder = 1;
1548 open PI, "<&STDIN" or die $!;
1549 open STDIN, "/dev/null" or die $!;
1550 open PO, ">&STDOUT" or die $!;
1552 open STDOUT, ">&STDERR" or die $!;
1555 responder_send_command("dgit-remote-push-ready");
1567 printdebug "(killing remote child $i_child_pid)\n";
1568 kill 15, $i_child_pid;
1570 if (defined $i_tmp && !defined $initiator_tempdir) {
1572 eval { rmtree $i_tmp; };
1576 END { i_cleanup(); }
1579 my ($base,$selector,@args) = @_;
1580 $selector =~ s/\-/_/g;
1581 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1587 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1597 push @rdgit, @ropts;
1598 push @rdgit, qw(remote-push-responder), (scalar @rargs), @rargs;
1600 my @cmd = (@ssh, $host, shellquote @rdgit);
1601 printcmd \*DEBUG,$debugprefix."+",@cmd;
1603 if (defined $initiator_tempdir) {
1604 rmtree $initiator_tempdir;
1605 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1606 $i_tmp = $initiator_tempdir;
1610 $i_child_pid = open2(\*RO, \*RI, @cmd);
1612 initiator_expect { m/^dgit-remote-push-ready/ };
1614 my ($icmd,$iargs) = initiator_expect {
1615 m/^(\S+)(?: (.*))?$/;
1618 i_method "i_resp", $icmd, $iargs;
1622 sub i_resp_progress ($) {
1624 my $msg = protocol_read_bytes \*RO, $rhs;
1628 sub i_resp_complete {
1629 my $pid = $i_child_pid;
1630 $i_child_pid = undef; # prevents killing some other process with same pid
1631 printdebug "waiting for remote child $pid...\n";
1632 my $got = waitpid $pid, 0;
1633 die $! unless $got == $pid;
1634 die "remote child failed $?" if $?;
1637 printdebug "all done\n";
1641 sub i_resp_file ($) {
1643 my $localname = i_method "i_localname", $keyword;
1644 my $localpath = "$i_tmp/$localname";
1645 stat $localpath and badproto \*RO, "file $keyword ($localpath) twice";
1646 protocol_receive_file \*RO, $localpath;
1647 i_method "i_file", $keyword;
1652 sub i_resp_param ($) {
1653 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1659 sub i_resp_want ($) {
1661 die "$keyword ?" if $i_wanted{$keyword}++;
1662 my @localpaths = i_method "i_want", $keyword;
1663 printdebug "[[ $keyword @localpaths\n";
1664 foreach my $localpath (@localpaths) {
1665 protocol_send_file \*RI, $localpath;
1667 print RI "files-end\n" or die $!;
1670 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
1672 sub i_localname_parsed_changelog {
1673 return "remote-changelog.822";
1675 sub i_file_parsed_changelog {
1676 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
1677 push_parse_changelog "$i_tmp/remote-changelog.822";
1678 die if $i_dscfn =~ m#/|^\W#;
1681 sub i_localname_dsc {
1682 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1687 sub i_localname_changes {
1688 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1689 $i_changesfn = $i_dscfn;
1690 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
1691 return $i_changesfn;
1693 sub i_file_changes { }
1695 sub i_want_signed_tag {
1696 printdebug Dumper(\%i_param, $i_dscfn);
1697 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
1698 or badproto \*RO, "premature desire for signed-tag";
1699 my $head = $i_param{'head'};
1700 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
1702 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
1705 push_mktag $head, $i_clogp, $i_tag,
1707 $i_changesfn, 'remote changes',
1708 sub { "tag$_[0]"; };
1713 sub i_want_signed_dsc_changes {
1714 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
1715 sign_changes $i_changesfn;
1716 return ($i_dscfn, $i_changesfn);
1719 #---------- building etc. ----------
1725 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1727 sub build_maybe_quilt_fixup () {
1728 if (!open F, "debian/source/format") {
1729 die $! unless $!==&ENOENT;
1733 F->error and die $!;
1735 return unless madformat($_);
1738 my @cmd = (@git, qw(ls-files --exclude-standard -iodm));
1739 my $problems = cmdoutput @cmd;
1740 if (length $problems) {
1741 print STDERR "problematic files:\n";
1742 print STDERR " $_\n" foreach split /\n/, $problems;
1743 fail "Cannot do quilt fixup in tree containing ignored files. ".
1744 "Perhaps your package's clean target is broken, in which".
1745 " case -wg (which says to use git-clean -xdf) may help.";
1748 my $clogp = parsechangelog();
1749 my $version = getfield $clogp, 'Version';
1750 my $author = getfield $clogp, 'Maintainer';
1751 my $headref = rev_parse('HEAD');
1754 my $patchname = "auto-$version-$headref-$time";
1755 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1757 my $descfn = ".git/dgit/quilt-description.tmp";
1758 open O, '>', $descfn or die "$descfn: $!";
1760 $msg =~ s/^\s+$/ ./mg;
1761 print O <<END or die $!;
1762 Description: Automatically generated patch ($clogp->{Version})
1763 Last (up to) $ncommits git changes, FYI:
1773 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1774 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1775 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1776 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
1779 if (!open P, '>>', ".pc/applied-patches") {
1780 $!==&ENOENT or die $!;
1785 commit_quilty_patch();
1788 sub quilt_fixup_editor () {
1789 my $descfn = $ENV{$fakeeditorenv};
1790 my $editing = $ARGV[$#ARGV];
1791 open I1, '<', $descfn or die "$descfn: $!";
1792 open I2, '<', $editing or die "$editing: $!";
1793 unlink $editing or die "$editing: $!";
1794 open O, '>', $editing or die "$editing: $!";
1795 while (<I1>) { print O or die $!; } I1->error and die $!;
1798 $copying ||= m/^\-\-\- /;
1799 next unless $copying;
1802 I2->error and die $!;
1808 if ($cleanmode eq 'dpkg-source') {
1809 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
1810 } elsif ($cleanmode eq 'git') {
1811 runcmd_ordryrun_local @git, qw(clean -xdf);
1812 } elsif ($cleanmode eq 'none') {
1819 badusage "-p is not allowed when building" if defined $package;
1822 my $clogp = parsechangelog();
1823 $isuite = getfield $clogp, 'Distribution';
1824 $package = getfield $clogp, 'Source';
1825 $version = getfield $clogp, 'Version';
1826 build_maybe_quilt_fixup();
1829 sub changesopts () {
1830 my @opts =@changesopts[1..$#changesopts];
1831 if (!defined $changes_since_version) {
1832 my @vsns = archive_query('archive_query');
1834 @vsns = map { $_->[0] } @vsns;
1835 @vsns = sort { -version_compare_string($a, $b) } @vsns;
1836 $changes_since_version = $vsns[0];
1837 progress "changelog will contain changes since $vsns[0]";
1839 $changes_since_version = '_';
1840 progress "package seems new, not specifying -v<version>";
1843 if ($changes_since_version ne '_') {
1844 unshift @opts, "-v$changes_since_version";
1851 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1852 printdone "build successful\n";
1858 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1859 "--git-builder=@dpkgbuildpackage");
1860 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1861 canonicalise_suite();
1862 push @cmd, "--git-debian-branch=".lbranch();
1864 push @cmd, changesopts();
1865 runcmd_ordryrun_local @cmd, @ARGV;
1866 printdone "build successful\n";
1871 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1872 $dscfn = dscfn($version);
1873 if ($cleanmode eq 'dpkg-source') {
1874 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
1877 my $pwd = cmdoutput qw(env - pwd);
1878 my $leafdir = basename $pwd;
1880 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
1882 runcmd_ordryrun_local qw(sh -ec),
1883 'exec >$1; shift; exec "$@"','x',
1884 "../$sourcechanges",
1885 @dpkggenchanges, qw(-S), changesopts();
1889 sub cmd_build_source {
1890 badusage "build-source takes no additional arguments" if @ARGV;
1892 printdone "source built, results in $dscfn and $sourcechanges";
1898 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1900 stat $dscfn or fail "$dscfn (in parent directory): $!";
1901 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1902 foreach my $cf (glob $pat) {
1903 next if $cf eq $sourcechanges;
1904 unlink $cf or fail "remove $cf: $!";
1907 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1908 my @changesfiles = glob $pat;
1909 @changesfiles = sort {
1910 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
1913 fail "wrong number of different changes files (@changesfiles)"
1914 unless @changesfiles;
1915 runcmd_ordryrun_local @mergechanges, @changesfiles;
1916 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1918 stat $multichanges or fail "$multichanges: $!";
1920 printdone "build successful, results in $multichanges\n" or die $!;
1923 sub cmd_quilt_fixup {
1924 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1925 my $clogp = parsechangelog();
1926 $version = getfield $clogp, 'Version';
1927 build_maybe_quilt_fixup();
1930 #---------- argument parsing and main program ----------
1933 print "dgit version $our_version\n" or die $!;
1940 if (defined $ENV{'DGIT_SSH'}) {
1941 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
1942 } elsif (defined $ENV{'GIT_SSH'}) {
1943 @ssh = ($ENV{'GIT_SSH'});
1947 last unless $ARGV[0] =~ m/^-/;
1951 if (m/^--dry-run$/) {
1954 } elsif (m/^--damp-run$/) {
1957 } elsif (m/^--no-sign$/) {
1960 } elsif (m/^--help$/) {
1962 } elsif (m/^--version$/) {
1964 } elsif (m/^--new$/) {
1967 } elsif (m/^--since-version=([^_]+|_)$/) {
1969 $changes_since_version = $1;
1970 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
1971 ($om = $opts_opt_map{$1}) &&
1975 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
1976 !$opts_opt_cmdonly{$1} &&
1977 ($om = $opts_opt_map{$1})) {
1980 } elsif (m/^--existing-package=(.*)/s) {
1982 $existing_package = $1;
1983 } elsif (m/^--initiator-tempdir=(.*)/s) {
1984 $initiator_tempdir = $1;
1985 $initiator_tempdir =~ m#^/# or
1986 badusage "--initiator-tempdir must be used specify an".
1987 " absolute, not relative, directory."
1988 } elsif (m/^--distro=(.*)/s) {
1991 } elsif (m/^--build-products-dir=(.*)/s) {
1993 $buildproductsdir = $1;
1994 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1997 } elsif (m/^--clean=(.*)$/s) {
1998 badusage "unknown cleaning mode \`$1'";
1999 } elsif (m/^--ignore-dirty$/s) {
2002 } elsif (m/^--no-quilt-fixup$/s) {
2006 badusage "unknown long option \`$_'";
2013 } elsif (s/^-L/-/) {
2016 } elsif (s/^-h/-/) {
2018 } elsif (s/^-D/-/) {
2020 open DEBUG, ">&STDERR" or die $!;
2023 } elsif (s/^-N/-/) {
2026 } elsif (s/^-v([^_]+|_)$//s) {
2028 $changes_since_version = $1;
2031 push @changesopts, $_;
2033 } elsif (s/^-c(.*=.*)//s) {
2035 push @git, '-c', $1;
2036 } elsif (s/^-d(.*)//s) {
2039 } elsif (s/^-C(.*)//s) {
2042 if ($changesfile =~ s#^(.*)/##) {
2043 $buildproductsdir = $1;
2045 } elsif (s/^-k(.*)//s) {
2047 } elsif (s/^-wn//s) {
2049 $cleanmode = 'none';
2050 } elsif (s/^-wg//s) {
2053 } elsif (s/^-wd//s) {
2055 $cleanmode = 'dpkg-source';
2057 badusage "unknown short option \`$_'";
2064 if ($ENV{$fakeeditorenv}) {
2065 quilt_fixup_editor();
2068 delete $ENV{'DGET_UNPACK'};
2071 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2072 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2073 if $dryrun_level == 1;
2075 print STDERR $helpmsg or die $!;
2078 my $cmd = shift @ARGV;
2080 { no strict qw(refs); &{"cmd_$cmd"}(); }