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, "$us: 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.debian.backports-quirk' => '%-backports*',
461 'dgit-distro.debian-backports.mirror' => 'http://backports.debian.org/debian-backports/',
462 'dgit-distro.test-dummy.ssh' => "$td/ssh",
463 'dgit-distro.test-dummy.username' => "alice",
464 'dgit-distro.test-dummy.git-check' => "ssh-cmd",
465 'dgit-distro.test-dummy.git-create' => "ssh-cmd",
466 'dgit-distro.test-dummy.git-url' => "$td/git",
467 'dgit-distro.test-dummy.git-host' => "git",
468 'dgit-distro.test-dummy.git-path' => "$td/git",
469 'dgit-distro.test-dummy.archive-query' => "dummycat:$td/aq",
470 'dgit-distro.test-dummy.mirror' => "file://$td/mirror/",
471 'dgit-distro.test-dummy.upload-host' => 'test-dummy',
476 return undef if $c =~ /RETURN-UNDEF/;
477 my @cmd = (@git, qw(config --), $c);
480 local ($debug) = $debug-1;
481 $v = cmdoutput_errok @cmd;
488 my $dv = $defcfg{$c};
489 return $dv if defined $dv;
491 badcfg "need value for one of: @_";
494 sub access_basedistro () {
495 return cfg("dgit-suite.$isuite.distro",
496 "dgit.default.distro");
499 sub access_quirk () {
500 # returns (quirk name, distro to use instead, quirk-specific info)
501 my $basedistro = access_basedistro();
502 my $backports_quirk = cfg("dgit-distro.$basedistro.backports-quirk",
504 if (defined $backports_quirk) {
505 my $re = $backports_quirk;
506 $re =~ s/[^-0-9a-z_\%*]/\\$&/ig;
508 $re =~ s/\%/([-0-9a-z_]+)/ or badcfg "backports-quirk needs \%";
509 if ($isuite =~ m/^$re$/) {
510 return ('backports',"$basedistro-backports",$1);
513 return ('none',$basedistro);
516 sub access_distro () {
517 return (access_quirk())[1];
522 my $basedistro = access_basedistro();
523 my $distro = $idistro || access_distro();
524 my $value = cfg(map {
525 ("dgit-distro.$distro.$_",
526 "dgit-distro.$basedistro.$_",
532 sub string_to_ssh ($) {
534 if ($spec =~ m/\s/) {
535 return qw(sh -ec), 'exec '.$spec.' "$@"', 'x';
541 sub access_cfg_ssh () {
542 my $gitssh = access_cfg('ssh', 'RETURN-UNDEF');
543 if (!defined $gitssh) {
546 return string_to_ssh $gitssh;
550 sub access_someuserhost ($) {
552 my $user = access_cfg("$some-user",'username');
553 my $host = access_cfg("$some-host");
554 return length($user) ? "$user\@$host" : $host;
557 sub access_gituserhost () {
558 return access_someuserhost('git');
561 sub access_giturl () {
562 my $url = access_cfg('git-url','RETURN-UNDEF');
565 access_cfg('git-proto').
566 access_gituserhost().
567 access_cfg('git-path');
569 return "$url/$package.git";
572 sub parsecontrolfh ($$@) {
573 my ($fh, $desc, @opts) = @_;
574 my %opts = ('name' => $desc, @opts);
575 my $c = Dpkg::Control::Hash->new(%opts);
576 $c->parse($fh) or die "parsing of $desc failed";
581 my ($file, $desc) = @_;
582 my $fh = new IO::Handle;
583 open $fh, '<', $file or die "$file: $!";
584 my $c = parsecontrolfh($fh,$desc);
585 $fh->error and die $!;
591 my ($dctrl,$field) = @_;
592 my $v = $dctrl->{$field};
593 return $v if defined $v;
594 fail "missing field $field in ".$v->get_option('name');
598 my $c = Dpkg::Control::Hash->new();
599 my $p = new IO::Handle;
600 my @cmd = (qw(dpkg-parsechangelog), @_);
601 open $p, '-|', @cmd or die $!;
603 $?=0; $!=0; close $p or failedcmd @cmd;
607 sub git_get_ref ($) {
609 my $got = cmdoutput_errok @git, qw(show-ref --), $refname;
611 $?==256 or fail "git show-ref failed (status $?)";
612 printdebug "ref $refname= [show-ref exited 1]\n";
615 if ($got =~ m/^(\w+) \Q$refname\E$/m) {
616 printdebug "ref $refname=$1\n";
619 printdebug "ref $refname= [no match]\n";
626 sub archive_query ($) {
628 my $query = access_cfg('archive-query','RETURN-UNDEF');
629 if (!defined $query) {
630 my $distro = access_basedistro();
631 if ($distro eq 'debian') {
633 access_someuserhost('sshpsql').':'.
634 access_cfg('sshpsql-dbname');
636 $query = "madison:$distro";
639 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
642 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
645 sub pool_dsc_subpath ($$) {
646 my ($vsn,$component) = @_; # $package is implict arg
647 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
648 return "/pool/$component/$prefix/$package/".dscfn($vsn);
651 sub archive_query_madison ($$) {
652 my ($proto,$data) = @_;
653 die unless $proto eq 'madison';
654 $rmad{$package} ||= cmdoutput
655 qw(rmadison -asource),"-s$isuite","-u$data",$package;
656 my $rmad = $rmad{$package};
657 return madison_parse($rmad);
660 sub madison_parse ($) {
663 foreach my $l (split /\n/, $rmad) {
664 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
665 \s*( [^ \t|]+ )\s* \|
666 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
667 \s*( [^ \t|]+ )\s* }x or die "$rmad ?";
668 $1 eq $package or die "$rmad $package ?";
675 $component = access_cfg('archive-query-default-component');
677 $5 eq 'source' or die "$rmad ?";
678 push @out, [$vsn,pool_dsc_subpath($vsn,$component),$newsuite];
680 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
683 sub canonicalise_suite_madison ($$) {
684 # madison canonicalises for us
685 my @r = archive_query_madison($_[0],$_[1]);
687 "unable to canonicalise suite using package $package".
688 " which does not appear to exist in suite $isuite;".
689 " --existing-package may help";
694 my ($data,$sql) = @_;
695 $data =~ m/:/ or badcfg "invalid sshpsql method string \`$data'";
696 my ($userhost,$dbname) = ($`,$'); #';
698 my @cmd = (access_cfg_ssh, $userhost,
699 "export LANG=C; ".shellquote qw(psql -A), $dbname, qw(-c), $sql);
700 printcmd(\*DEBUG,$debugprefix."|",@cmd) if $debug>0;
701 open P, "-|", @cmd or die $!;
704 printdebug("$debugprefix>|$_|\n");
707 $!=0; $?=0; close P or failedcmd @cmd;
709 my $nrows = pop @rows;
710 $nrows =~ s/^\((\d+) rows?\)$/$1/ or die "$nrows ?";
711 @rows == $nrows+1 or die "$nrows ".(scalar @rows)." ?";
712 @rows = map { [ split /\|/, $_ ] } @rows;
713 my $ncols = scalar @{ shift @rows };
714 die if grep { scalar @$_ != $ncols } @rows;
718 sub sql_injection_check {
719 foreach (@_) { die "$_ $& ?" if m/[']/; }
722 sub archive_query_sshpsql ($$) {
723 my ($proto,$data) = @_;
724 sql_injection_check $isuite, $package;
725 my @rows = sshpsql($data, <<END);
726 SELECT source.version, component.name, files.filename
728 JOIN src_associations ON source.id = src_associations.source
729 JOIN suite ON suite.id = src_associations.suite
730 JOIN dsc_files ON dsc_files.source = source.id
731 JOIN files_archive_map ON files_archive_map.file_id = dsc_files.file
732 JOIN component ON component.id = files_archive_map.component_id
733 JOIN files ON files.id = dsc_files.file
734 WHERE ( suite.suite_name='$isuite' OR suite.codename='$isuite' )
735 AND source.source='$package'
736 AND files.filename LIKE '%.dsc';
738 @rows = sort { -version_compare_string($a->[0],$b->[0]) } @rows;
740 my ($vsn,$component,$filename) = @$_;
741 [ $vsn, "/pool/$component/$filename" ];
746 sub canonicalise_suite_sshpsql ($$) {
747 my ($proto,$data) = @_;
748 sql_injection_check $isuite;
749 my @rows = sshpsql($data, <<END);
750 SELECT suite.codename
751 FROM suite where suite_name='$isuite' or codename='$isuite';
753 @rows = map { $_->[0] } @rows;
754 fail "unknown suite $isuite" unless @rows;
755 die "ambiguous $isuite: @rows ?" if @rows>1;
759 sub canonicalise_suite_dummycat ($$) {
760 my ($proto,$data) = @_;
761 my $dpath = "$data/suite.$isuite";
762 if (!open C, "<", $dpath) {
763 $!==ENOENT or die "$dpath: $!";
764 printdebug "dummycat canonicalise_suite $isuite $dpath ENOENT\n";
768 chomp or die "$dpath: $!";
770 printdebug "dummycat canonicalise_suite $isuite $dpath = $_\n";
774 sub archive_query_dummycat ($$) {
775 my ($proto,$data) = @_;
776 canonicalise_suite();
777 my $dpath = "$data/package.$csuite.$package";
778 if (!open C, "<", $dpath) {
779 $!==ENOENT or die "$dpath: $!";
780 printdebug "dummycat query $csuite $package $dpath ENOENT\n";
788 printdebug "dummycat query $csuite $package $dpath | $_\n";
789 my @row = split /\s+/, $_;
790 @row==2 or die "$dpath: $_ ?";
793 C->error and die "$dpath: $!";
795 return sort { -version_compare_string($a->[0],$b->[0]); } @rows;
798 sub canonicalise_suite () {
799 return if defined $csuite;
800 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
801 $csuite = archive_query('canonicalise_suite');
802 if ($isuite ne $csuite) {
803 progress "canonical suite name for $isuite is $csuite";
807 sub get_archive_dsc () {
808 canonicalise_suite();
809 my @vsns = archive_query('archive_query');
810 foreach my $vinfo (@vsns) {
811 my ($vsn,$subpath) = @$vinfo;
812 $dscurl = access_cfg('mirror').$subpath;
813 $dscdata = url_get($dscurl);
815 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
818 my $dscfh = new IO::File \$dscdata, '<' or die $!;
819 printdebug Dumper($dscdata) if $debug>1;
820 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
821 printdebug Dumper($dsc) if $debug>1;
822 my $fmt = getfield $dsc, 'Format';
823 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
829 sub check_for_git () {
831 my $how = access_cfg('git-check');
832 if ($how eq 'ssh-cmd') {
834 (access_cfg_ssh, access_gituserhost(),
835 " set -e; cd ".access_cfg('git-path').";".
836 " if test -d $package.git; then echo 1; else echo 0; fi");
837 my $r= cmdoutput @cmd;
838 failedcmd @cmd unless $r =~ m/^[01]$/;
841 badcfg "unknown git-check \`$how'";
845 sub create_remote_git_repo () {
846 my $how = access_cfg('git-create');
847 if ($how eq 'ssh-cmd') {
849 (access_cfg_ssh, access_gituserhost(),
850 "set -e; cd ".access_cfg('git-path').";".
851 " cp -a _template $package.git");
853 badcfg "unknown git-create \`$how'";
857 our ($dsc_hash,$lastpush_hash);
859 our $ud = '.git/dgit/unpack';
867 sub mktree_in_ud_from_only_subdir () {
868 # changes into the subdir
871 $dirs[0] =~ m#^([^/]+)/\.$# or die;
874 fail "source package contains .git directory" if stat '.git';
875 die $! unless $!==&ENOENT;
876 runcmd qw(git init -q);
877 rmtree('.git/objects');
878 symlink '../../../../objects','.git/objects' or die $!;
879 runcmd @git, qw(add -Af);
880 my $tree = cmdoutput @git, qw(write-tree);
881 $tree =~ m/^\w+$/ or die "$tree ?";
885 sub dsc_files_info () {
886 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
887 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
888 ['Files', 'Digest::MD5', 'new()']) {
889 my ($fname, $module, $method) = @$csumi;
890 my $field = $dsc->{$fname};
891 next unless defined $field;
892 eval "use $module; 1;" or die $@;
894 foreach (split /\n/, $field) {
896 m/^(\w+) (\d+) (\S+)$/ or
897 fail "could not parse .dsc $fname line \`$_'";
898 my $digester = eval "$module"."->$method;" or die $@;
903 Digester => $digester,
908 fail "missing any supported Checksums-* or Files field in ".
909 $dsc->get_option('name');
913 map { $_->{Filename} } dsc_files_info();
916 sub is_orig_file ($) {
918 m/\.orig(?:-\w+)?\.tar\.\w+$/;
921 sub make_commit ($) {
923 return cmdoutput @git, qw(hash-object -w -t commit), $file;
926 sub clogp_authline ($) {
928 my $author = getfield $clogp, 'Maintainer';
929 $author =~ s#,.*##ms;
930 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
931 my $authline = "$author $date";
932 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
933 fail "unexpected commit author line format \`$authline'".
934 " (was generated from changelog Maintainer field)";
938 sub generate_commit_from_dsc () {
942 foreach my $f (dsc_files()) {
943 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
945 link "../../../$f", $f
949 runcmd @dget, qw(--), $dscurl;
950 foreach my $f (grep { is_orig_file($_) } @files) {
951 link $f, "../../../../$f"
955 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
956 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
957 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
958 my $authline = clogp_authline $clogp;
959 my $changes = getfield $clogp, 'Changes';
960 open C, ">../commit.tmp" or die $!;
961 print C <<END or die $!;
968 # imported from the archive
971 my $outputhash = make_commit qw(../commit.tmp);
972 my $cversion = getfield $clogp, 'Version';
973 progress "synthesised git commit from .dsc $cversion";
974 if ($lastpush_hash) {
975 runcmd @git, qw(reset --hard), $lastpush_hash;
976 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
977 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
978 my $oversion = getfield $oldclogp, 'Version';
980 version_compare_string($oversion, $cversion);
982 # git upload/ is earlier vsn than archive, use archive
983 open C, ">../commit2.tmp" or die $!;
984 print C <<END or die $!;
986 parent $lastpush_hash
991 Record $package ($cversion) in archive suite $csuite
993 $outputhash = make_commit qw(../commit2.tmp);
994 } elsif ($vcmp > 0) {
995 print STDERR <<END or die $!;
997 Version actually in archive: $cversion (older)
998 Last allegedly pushed/uploaded: $oversion (newer or same)
1001 $outputhash = $lastpush_hash;
1003 $outputhash = $lastpush_hash;
1006 changedir '../../../..';
1007 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
1008 'DGIT_ARCHIVE', $outputhash;
1009 cmdoutput @git, qw(log -n2), $outputhash;
1010 # ... gives git a chance to complain if our commit is malformed
1015 sub ensure_we_have_orig () {
1016 foreach my $fi (dsc_files_info()) {
1017 my $f = $fi->{Filename};
1018 next unless is_orig_file($f);
1019 if (open F, "<", "../$f") {
1020 $fi->{Digester}->reset();
1021 $fi->{Digester}->addfile(*F);
1022 F->error and die $!;
1023 my $got = $fi->{Digester}->hexdigest();
1024 $got eq $fi->{Hash} or
1025 fail "existing file $f has hash $got but .dsc".
1026 " demands hash $fi->{Hash}".
1027 " (perhaps you should delete this file?)";
1028 progress "using existing $f";
1031 die "$f $!" unless $!==&ENOENT;
1033 my $origurl = $dscurl;
1034 $origurl =~ s{/[^/]+$}{};
1036 die "$f ?" unless $f =~ m/^${package}_/;
1037 die "$f ?" if $f =~ m#/#;
1038 runcmd_ordryrun_local shell_cmd 'cd ..', @dget,'--',$origurl;
1043 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
1046 sub is_fast_fwd ($$) {
1047 my ($ancestor,$child) = @_;
1048 my @cmd = (@git, qw(merge-base), $ancestor, $child);
1049 my $mb = cmdoutput_errok @cmd;
1051 return rev_parse($mb) eq rev_parse($ancestor);
1053 $?==256 or failedcmd @cmd;
1058 sub git_fetch_us () {
1059 runcmd_ordryrun_local @git, qw(fetch),access_giturl(),fetchspec();
1062 sub fetch_from_archive () {
1063 # ensures that lrref() is what is actually in the archive,
1064 # one way or another
1068 foreach my $field (@ourdscfield) {
1069 $dsc_hash = $dsc->{$field};
1070 last if defined $dsc_hash;
1072 if (defined $dsc_hash) {
1073 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
1075 progress "last upload to archive specified git hash";
1077 progress "last upload to archive has NO git hash";
1080 progress "no version available from the archive";
1083 $lastpush_hash = git_get_ref(lrref());
1084 printdebug "previous reference hash=$lastpush_hash\n";
1086 if (defined $dsc_hash) {
1087 fail "missing remote git history even though dsc has hash -".
1088 " could not find ref ".lrref().
1089 " (should have been fetched from ".access_giturl()."#".rrref().")"
1090 unless $lastpush_hash;
1092 ensure_we_have_orig();
1093 if ($dsc_hash eq $lastpush_hash) {
1094 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
1095 print STDERR <<END or die $!;
1097 Git commit in archive is behind the last version allegedly pushed/uploaded.
1098 Commit referred to by archive: $dsc_hash
1099 Last allegedly pushed/uploaded: $lastpush_hash
1102 $hash = $lastpush_hash;
1104 fail "archive's .dsc refers to ".$dsc_hash.
1105 " but this is an ancestor of ".$lastpush_hash;
1108 $hash = generate_commit_from_dsc();
1109 } elsif ($lastpush_hash) {
1110 # only in git, not in the archive yet
1111 $hash = $lastpush_hash;
1112 print STDERR <<END or die $!;
1114 Package not found in the archive, but has allegedly been pushed using dgit.
1118 printdebug "nothing found!\n";
1119 if (defined $skew_warning_vsn) {
1120 print STDERR <<END or die $!;
1122 Warning: relevant archive skew detected.
1123 Archive allegedly contains $skew_warning_vsn
1124 But we were not able to obtain any version from the archive or git.
1130 printdebug "current hash=$hash\n";
1131 if ($lastpush_hash) {
1132 fail "not fast forward on last upload branch!".
1133 " (archive's version left in DGIT_ARCHIVE)"
1134 unless is_fast_fwd($lastpush_hash, $hash);
1136 if (defined $skew_warning_vsn) {
1138 printdebug "SKEW CHECK WANT $skew_warning_vsn\n";
1139 my $clogf = ".git/dgit/changelog.tmp";
1140 runcmd shell_cmd "exec >$clogf",
1141 @git, qw(cat-file blob), "$hash:debian/changelog";
1142 my $gotclogp = parsechangelog("-l$clogf");
1143 my $got_vsn = getfield $gotclogp, 'Version';
1144 printdebug "SKEW CHECK GOT $got_vsn\n";
1145 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
1146 print STDERR <<END or die $!;
1148 Warning: archive skew detected. Using the available version:
1149 Archive allegedly contains $skew_warning_vsn
1150 We were able to obtain only $got_vsn
1155 if ($lastpush_hash ne $hash) {
1156 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
1160 dryrun_report @upd_cmd;
1168 canonicalise_suite();
1169 badusage "dry run makes no sense with clone" unless act_local();
1170 mkdir $dstdir or die "$dstdir $!";
1172 runcmd @git, qw(init -q);
1173 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
1174 open H, "> .git/HEAD" or die $!;
1175 print H "ref: ".lref()."\n" or die $!;
1177 runcmd @git, qw(remote add), 'origin', access_giturl();
1178 if (check_for_git()) {
1179 progress "fetching existing git history";
1181 runcmd_ordryrun_local @git, qw(fetch origin);
1183 progress "starting new git history";
1185 fetch_from_archive() or no_such_package;
1186 runcmd @git, qw(reset --hard), lrref();
1187 printdone "ready for work in $dstdir";
1191 if (check_for_git()) {
1194 fetch_from_archive() or no_such_package();
1195 printdone "fetched into ".lrref();
1200 runcmd_ordryrun_local @git, qw(merge -m),"Merge from $csuite [dgit]",
1202 printdone "fetched to ".lrref()." and merged into HEAD";
1205 sub check_not_dirty () {
1206 return if $ignoredirty;
1207 my @cmd = (@git, qw(diff --quiet HEAD));
1208 printcmd(\*DEBUG,$debugprefix."+",@cmd) if $debug>0;
1209 $!=0; $?=0; system @cmd;
1210 return if !$! && !$?;
1211 if (!$! && $?==256) {
1212 fail "working tree is dirty (does not match HEAD)";
1218 sub commit_quilty_patch () {
1219 my $output = cmdoutput @git, qw(status --porcelain);
1221 foreach my $l (split /\n/, $output) {
1222 next unless $l =~ m/\S/;
1223 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1228 progress "nothing quilty to commit, ok.";
1231 runcmd_ordryrun_local @git, qw(add), sort keys %adds;
1232 my $m = "Commit Debian 3.0 (quilt) metadata";
1234 runcmd_ordryrun_local @git, qw(commit -m), $m;
1239 return 0 unless $format eq '3.0 (quilt)';
1240 progress "Format \`$format', urgh";
1242 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1248 sub push_parse_changelog ($) {
1251 my $clogp = Dpkg::Control::Hash->new();
1252 $clogp->load($clogpfn) or die;
1254 $package = getfield $clogp, 'Source';
1255 my $cversion = getfield $clogp, 'Version';
1256 my $tag = debiantag($cversion);
1257 runcmd @git, qw(check-ref-format), $tag;
1259 my $dscfn = dscfn($cversion);
1261 return ($clogp, $cversion, $tag, $dscfn);
1264 sub push_parse_dsc ($$$) {
1265 my ($dscfn,$dscfnwhat, $cversion) = @_;
1266 $dsc = parsecontrol($dscfn,$dscfnwhat);
1267 my $dversion = getfield $dsc, 'Version';
1268 my $dscpackage = getfield $dsc, 'Source';
1269 ($dscpackage eq $package && $dversion eq $cversion) or
1270 fail "$dscfn is for $dscpackage $dversion".
1271 " but debian/changelog is for $package $cversion";
1274 sub push_mktag ($$$$$$$) {
1275 my ($head,$clogp,$tag,
1277 $changesfile,$changesfilewhat,
1280 $dsc->{$ourdscfield[0]} = $head;
1281 $dsc->save("$dscfn.tmp") or die $!;
1283 my $changes = parsecontrol($changesfile,$changesfilewhat);
1284 foreach my $field (qw(Source Distribution Version)) {
1285 $changes->{$field} eq $clogp->{$field} or
1286 fail "changes field $field \`$changes->{$field}'".
1287 " does not match changelog \`$clogp->{$field}'";
1290 my $cversion = getfield $clogp, 'Version';
1291 my $clogsuite = getfield $clogp, 'Distribution';
1293 # We make the git tag by hand because (a) that makes it easier
1294 # to control the "tagger" (b) we can do remote signing
1295 my $authline = clogp_authline $clogp;
1296 open TO, '>', $tfn->('.tmp') or die $!;
1297 print TO <<END or die $!;
1303 $package release $cversion for $clogsuite [dgit]
1307 my $tagobjfn = $tfn->('.tmp');
1309 if (!defined $keyid) {
1310 $keyid = access_cfg('keyid','RETURN-UNDEF');
1312 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1313 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1314 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1315 push @sign_cmd, $tfn->('.tmp');
1316 runcmd_ordryrun @sign_cmd;
1318 $tagobjfn = $tfn->('.signed.tmp');
1319 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1320 $tfn->('.tmp'), $tfn->('.tmp.asc');
1327 sub sign_changes ($) {
1328 my ($changesfile) = @_;
1330 my @debsign_cmd = @debsign;
1331 push @debsign_cmd, "-k$keyid" if defined $keyid;
1332 push @debsign_cmd, "-p$gpg[0]" if $gpg[0] ne 'gpg';
1333 push @debsign_cmd, $changesfile;
1334 runcmd_ordryrun @debsign_cmd;
1339 printdebug "actually entering push\n";
1342 my $clogpfn = ".git/dgit/changelog.822.tmp";
1343 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1345 responder_send_file('parsed-changelog', $clogpfn);
1347 my ($clogp, $cversion, $tag, $dscfn) =
1348 push_parse_changelog("$clogpfn");
1350 my $dscpath = "$buildproductsdir/$dscfn";
1352 fail "looked for .dsc $dscfn, but $!;".
1353 " maybe you forgot to build";
1355 responder_send_file('dsc', $dscpath);
1357 push_parse_dsc($dscpath, $dscfn, $cversion);
1359 my $format = getfield $dsc, 'Format';
1360 printdebug "format $format\n";
1361 if (madformat($format)) {
1362 commit_quilty_patch();
1366 progress "checking that $dscfn corresponds to HEAD";
1367 runcmd qw(dpkg-source -x --),
1368 $dscpath =~ m#^/# ? $dscpath : "../../../$dscpath";
1369 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1370 changedir '../../../..';
1371 my @diffcmd = (@git, qw(diff --exit-code), $tree);
1372 printcmd \*DEBUG,$debugprefix."+",@diffcmd;
1374 if (system @diffcmd) {
1375 if ($! && $?==256) {
1376 fail "$dscfn specifies a different tree to your HEAD commit;".
1377 " perhaps you forgot to build";
1383 #do fast forward check and maybe fake merge
1384 # if (!is_fast_fwd(mainbranch
1385 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1386 # map { lref($_).":".rref($_) }
1388 my $head = rev_parse('HEAD');
1389 if (!$changesfile) {
1390 my $multi = "$buildproductsdir/".
1391 "${package}_".(stripepoch $cversion)."_multi.changes";
1392 if (stat "$multi") {
1393 $changesfile = $multi;
1395 $!==&ENOENT or die "$multi: $!";
1396 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1397 my @cs = glob "$buildproductsdir/$pat";
1398 fail "failed to find unique changes file".
1399 " (looked for $pat in $buildproductsdir, or $multi);".
1400 " perhaps you need to use dgit -C"
1402 ($changesfile) = @cs;
1405 $changesfile = "$buildproductsdir/$changesfile";
1408 responder_send_file('changes',$changesfile);
1409 responder_send_command("param head $head");
1411 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1414 if ($we_are_responder) {
1415 $tagobjfn = $tfn->('.signed.tmp');
1416 responder_receive_files('signed-tag', $tagobjfn);
1419 push_mktag($head,$clogp,$tag,
1421 $changesfile,$changesfile,
1425 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1426 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1427 runcmd_ordryrun_local @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1428 runcmd_ordryrun @git, qw(tag -v --), $tag;
1430 if (!check_for_git()) {
1431 create_remote_git_repo();
1433 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
1434 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1436 if (!$we_are_responder) {
1438 rename "$dscpath.tmp",$dscpath or die "$dscfn $!";
1440 progress "[new .dsc left in $dscpath.tmp]";
1444 if ($we_are_responder) {
1445 my $dryrunsuffix = act_local() ? "" : ".tmp";
1446 responder_receive_files('signed-dsc-changes',
1447 "$dscpath$dryrunsuffix",
1448 "$changesfile$dryrunsuffix");
1450 sign_changes $changesfile;
1453 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
1454 my $host = access_cfg('upload-host','RETURN-UNDEF');
1455 my @hostarg = defined($host) ? ($host,) : ();
1456 runcmd_ordryrun @dput, @hostarg, $changesfile;
1457 printdone "pushed and uploaded $cversion";
1459 responder_send_command("complete");
1465 badusage "-p is not allowed with clone; specify as argument instead"
1466 if defined $package;
1469 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1470 ($package,$isuite) = @ARGV;
1471 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1472 ($package,$dstdir) = @ARGV;
1473 } elsif (@ARGV==3) {
1474 ($package,$isuite,$dstdir) = @ARGV;
1476 badusage "incorrect arguments to dgit clone";
1478 $dstdir ||= "$package";
1482 sub branchsuite () {
1483 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1484 if ($branch =~ m#$lbranch_re#o) {
1491 sub fetchpullargs () {
1492 if (!defined $package) {
1493 my $sourcep = parsecontrol('debian/control','debian/control');
1494 $package = getfield $sourcep, 'Source';
1497 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1499 my $clogp = parsechangelog();
1500 $isuite = getfield $clogp, 'Distribution';
1502 canonicalise_suite();
1503 progress "fetching from suite $csuite";
1504 } elsif (@ARGV==1) {
1506 canonicalise_suite();
1508 badusage "incorrect arguments to dgit fetch or dgit pull";
1526 badusage "-p is not allowed with dgit push" if defined $package;
1528 my $clogp = parsechangelog();
1529 $package = getfield $clogp, 'Source';
1532 } elsif (@ARGV==1) {
1533 ($specsuite) = (@ARGV);
1535 badusage "incorrect arguments to dgit push";
1537 $isuite = getfield $clogp, 'Distribution';
1539 local ($package) = $existing_package; # this is a hack
1540 canonicalise_suite();
1542 if (defined $specsuite && $specsuite ne $isuite) {
1543 canonicalise_suite();
1544 $csuite eq $specsuite or
1545 fail "dgit push: changelog specifies $isuite ($csuite)".
1546 " but command line specifies $specsuite";
1548 if (check_for_git()) {
1551 if (fetch_from_archive()) {
1552 is_fast_fwd(lrref(), 'HEAD') or
1553 fail "dgit push: HEAD is not a descendant".
1554 " of the archive's version.\n".
1555 "$us: To overwrite it, use git merge -s ours ".lrref().".";
1558 fail "package appears to be new in this suite;".
1559 " if this is intentional, use --new";
1564 #---------- remote commands' implementation ----------
1566 sub cmd_remote_push_responder {
1567 my ($nrargs) = shift @ARGV;
1568 my (@rargs) = @ARGV[0..$nrargs-1];
1569 @ARGV = @ARGV[$nrargs..$#ARGV];
1573 $we_are_responder = 1;
1575 open PI, "<&STDIN" or die $!;
1576 open STDIN, "/dev/null" or die $!;
1577 open PO, ">&STDOUT" or die $!;
1579 open STDOUT, ">&STDERR" or die $!;
1582 responder_send_command("dgit-remote-push-ready");
1594 printdebug "(killing remote child $i_child_pid)\n";
1595 kill 15, $i_child_pid;
1597 if (defined $i_tmp && !defined $initiator_tempdir) {
1599 eval { rmtree $i_tmp; };
1603 END { i_cleanup(); }
1606 my ($base,$selector,@args) = @_;
1607 $selector =~ s/\-/_/g;
1608 { no strict qw(refs); &{"${base}_${selector}"}(@args); }
1614 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1624 push @rdgit, @ropts;
1625 push @rdgit, qw(remote-push-responder), (scalar @rargs), @rargs;
1627 my @cmd = (@ssh, $host, shellquote @rdgit);
1628 printcmd \*DEBUG,$debugprefix."+",@cmd;
1630 if (defined $initiator_tempdir) {
1631 rmtree $initiator_tempdir;
1632 mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
1633 $i_tmp = $initiator_tempdir;
1637 $i_child_pid = open2(\*RO, \*RI, @cmd);
1639 initiator_expect { m/^dgit-remote-push-ready/ };
1641 my ($icmd,$iargs) = initiator_expect {
1642 m/^(\S+)(?: (.*))?$/;
1645 i_method "i_resp", $icmd, $iargs;
1649 sub i_resp_progress ($) {
1651 my $msg = protocol_read_bytes \*RO, $rhs;
1655 sub i_resp_complete {
1656 my $pid = $i_child_pid;
1657 $i_child_pid = undef; # prevents killing some other process with same pid
1658 printdebug "waiting for remote child $pid...\n";
1659 my $got = waitpid $pid, 0;
1660 die $! unless $got == $pid;
1661 die "remote child failed $?" if $?;
1664 printdebug "all done\n";
1668 sub i_resp_file ($) {
1670 my $localname = i_method "i_localname", $keyword;
1671 my $localpath = "$i_tmp/$localname";
1672 stat $localpath and badproto \*RO, "file $keyword ($localpath) twice";
1673 protocol_receive_file \*RO, $localpath;
1674 i_method "i_file", $keyword;
1679 sub i_resp_param ($) {
1680 $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
1686 sub i_resp_want ($) {
1688 die "$keyword ?" if $i_wanted{$keyword}++;
1689 my @localpaths = i_method "i_want", $keyword;
1690 printdebug "[[ $keyword @localpaths\n";
1691 foreach my $localpath (@localpaths) {
1692 protocol_send_file \*RI, $localpath;
1694 print RI "files-end\n" or die $!;
1697 our ($i_clogp, $i_version, $i_tag, $i_dscfn, $i_changesfn);
1699 sub i_localname_parsed_changelog {
1700 return "remote-changelog.822";
1702 sub i_file_parsed_changelog {
1703 ($i_clogp, $i_version, $i_tag, $i_dscfn) =
1704 push_parse_changelog "$i_tmp/remote-changelog.822";
1705 die if $i_dscfn =~ m#/|^\W#;
1708 sub i_localname_dsc {
1709 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1714 sub i_localname_changes {
1715 defined $i_dscfn or badproto \*RO, "dsc (before parsed-changelog)";
1716 $i_changesfn = $i_dscfn;
1717 $i_changesfn =~ s/\.dsc$/_dgit.changes/ or die;
1718 return $i_changesfn;
1720 sub i_file_changes { }
1722 sub i_want_signed_tag {
1723 printdebug Dumper(\%i_param, $i_dscfn);
1724 defined $i_param{'head'} && defined $i_dscfn && defined $i_clogp
1725 or badproto \*RO, "premature desire for signed-tag";
1726 my $head = $i_param{'head'};
1727 die if $head =~ m/[^0-9a-f]/ || $head !~ m/^../;
1729 push_parse_dsc $i_dscfn, 'remote dsc', $i_version;
1732 push_mktag $head, $i_clogp, $i_tag,
1734 $i_changesfn, 'remote changes',
1735 sub { "tag$_[0]"; };
1740 sub i_want_signed_dsc_changes {
1741 rename "$i_dscfn.tmp","$i_dscfn" or die "$i_dscfn $!";
1742 sign_changes $i_changesfn;
1743 return ($i_dscfn, $i_changesfn);
1746 #---------- building etc. ----------
1752 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1754 sub build_maybe_quilt_fixup () {
1755 if (!open F, "debian/source/format") {
1756 die $! unless $!==&ENOENT;
1760 F->error and die $!;
1762 return unless madformat($_);
1765 my @cmd = (@git, qw(ls-files --exclude-standard -iodm));
1766 my $problems = cmdoutput @cmd;
1767 if (length $problems) {
1768 print STDERR "problematic files:\n";
1769 print STDERR " $_\n" foreach split /\n/, $problems;
1770 fail "Cannot do quilt fixup in tree containing ignored files. ".
1771 "Perhaps your package's clean target is broken, in which".
1772 " case -wg (which says to use git-clean -xdf) may help.";
1775 my $clogp = parsechangelog();
1776 my $version = getfield $clogp, 'Version';
1777 my $author = getfield $clogp, 'Maintainer';
1778 my $headref = rev_parse('HEAD');
1781 my $patchname = "auto-$version-$headref-$time";
1782 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1784 my $descfn = ".git/dgit/quilt-description.tmp";
1785 open O, '>', $descfn or die "$descfn: $!";
1787 $msg =~ s/^\s+$/ ./mg;
1788 print O <<END or die $!;
1789 Description: Automatically generated patch ($clogp->{Version})
1790 Last (up to) $ncommits git changes, FYI:
1800 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1801 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1802 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1803 runcmd_ordryrun_local @dpkgsource, qw(--commit .), $patchname;
1806 if (!open P, '>>', ".pc/applied-patches") {
1807 $!==&ENOENT or die $!;
1812 commit_quilty_patch();
1815 sub quilt_fixup_editor () {
1816 my $descfn = $ENV{$fakeeditorenv};
1817 my $editing = $ARGV[$#ARGV];
1818 open I1, '<', $descfn or die "$descfn: $!";
1819 open I2, '<', $editing or die "$editing: $!";
1820 unlink $editing or die "$editing: $!";
1821 open O, '>', $editing or die "$editing: $!";
1822 while (<I1>) { print O or die $!; } I1->error and die $!;
1825 $copying ||= m/^\-\-\- /;
1826 next unless $copying;
1829 I2->error and die $!;
1835 if ($cleanmode eq 'dpkg-source') {
1836 runcmd_ordryrun_local @dpkgbuildpackage, qw(-T clean);
1837 } elsif ($cleanmode eq 'git') {
1838 runcmd_ordryrun_local @git, qw(clean -xdf);
1839 } elsif ($cleanmode eq 'none') {
1846 badusage "-p is not allowed when building" if defined $package;
1849 my $clogp = parsechangelog();
1850 $isuite = getfield $clogp, 'Distribution';
1851 $package = getfield $clogp, 'Source';
1852 $version = getfield $clogp, 'Version';
1853 build_maybe_quilt_fixup();
1856 sub changesopts () {
1857 my @opts =@changesopts[1..$#changesopts];
1858 if (!defined $changes_since_version) {
1859 my @vsns = archive_query('archive_query');
1860 my @quirk = access_quirk();
1861 if ($quirk[0] eq 'backports') {
1862 local $isuite = $quirk[2];
1864 canonicalise_suite();
1865 push @vsns, archive_query('archive_query');
1868 @vsns = map { $_->[0] } @vsns;
1869 @vsns = sort { -version_compare_string($a, $b) } @vsns;
1870 $changes_since_version = $vsns[0];
1871 progress "changelog will contain changes since $vsns[0]";
1873 $changes_since_version = '_';
1874 progress "package seems new, not specifying -v<version>";
1877 if ($changes_since_version ne '_') {
1878 unshift @opts, "-v$changes_since_version";
1885 runcmd_ordryrun_local @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1886 printdone "build successful\n";
1892 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1893 "--git-builder=@dpkgbuildpackage");
1894 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1895 canonicalise_suite();
1896 push @cmd, "--git-debian-branch=".lbranch();
1898 push @cmd, changesopts();
1899 runcmd_ordryrun_local @cmd, @ARGV;
1900 printdone "build successful\n";
1905 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1906 $dscfn = dscfn($version);
1907 if ($cleanmode eq 'dpkg-source') {
1908 runcmd_ordryrun_local (@dpkgbuildpackage, qw(-us -uc -S)),
1911 my $pwd = cmdoutput qw(env - pwd);
1912 my $leafdir = basename $pwd;
1914 runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
1916 runcmd_ordryrun_local qw(sh -ec),
1917 'exec >$1; shift; exec "$@"','x',
1918 "../$sourcechanges",
1919 @dpkggenchanges, qw(-S), changesopts();
1923 sub cmd_build_source {
1924 badusage "build-source takes no additional arguments" if @ARGV;
1926 printdone "source built, results in $dscfn and $sourcechanges";
1932 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1934 stat $dscfn or fail "$dscfn (in parent directory): $!";
1935 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1936 foreach my $cf (glob $pat) {
1937 next if $cf eq $sourcechanges;
1938 unlink $cf or fail "remove $cf: $!";
1941 runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1942 my @changesfiles = glob $pat;
1943 @changesfiles = sort {
1944 ($b =~ m/_source\.changes$/ <=> $a =~ m/_source\.changes$/)
1947 fail "wrong number of different changes files (@changesfiles)"
1948 unless @changesfiles;
1949 runcmd_ordryrun_local @mergechanges, @changesfiles;
1950 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1952 stat $multichanges or fail "$multichanges: $!";
1954 printdone "build successful, results in $multichanges\n" or die $!;
1957 sub cmd_quilt_fixup {
1958 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1959 my $clogp = parsechangelog();
1960 $version = getfield $clogp, 'Version';
1961 build_maybe_quilt_fixup();
1964 #---------- argument parsing and main program ----------
1967 print "dgit version $our_version\n" or die $!;
1974 if (defined $ENV{'DGIT_SSH'}) {
1975 @ssh = string_to_ssh $ENV{'DGIT_SSH'};
1976 } elsif (defined $ENV{'GIT_SSH'}) {
1977 @ssh = ($ENV{'GIT_SSH'});
1981 last unless $ARGV[0] =~ m/^-/;
1985 if (m/^--dry-run$/) {
1988 } elsif (m/^--damp-run$/) {
1991 } elsif (m/^--no-sign$/) {
1994 } elsif (m/^--help$/) {
1996 } elsif (m/^--version$/) {
1998 } elsif (m/^--new$/) {
2001 } elsif (m/^--since-version=([^_]+|_)$/) {
2003 $changes_since_version = $1;
2004 } elsif (m/^--([-0-9a-z]+)=(.*)/s &&
2005 ($om = $opts_opt_map{$1}) &&
2009 } elsif (m/^--([-0-9a-z]+):(.*)/s &&
2010 !$opts_opt_cmdonly{$1} &&
2011 ($om = $opts_opt_map{$1})) {
2014 } elsif (m/^--existing-package=(.*)/s) {
2016 $existing_package = $1;
2017 } elsif (m/^--initiator-tempdir=(.*)/s) {
2018 $initiator_tempdir = $1;
2019 $initiator_tempdir =~ m#^/# or
2020 badusage "--initiator-tempdir must be used specify an".
2021 " absolute, not relative, directory."
2022 } elsif (m/^--distro=(.*)/s) {
2025 } elsif (m/^--build-products-dir=(.*)/s) {
2027 $buildproductsdir = $1;
2028 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
2031 } elsif (m/^--clean=(.*)$/s) {
2032 badusage "unknown cleaning mode \`$1'";
2033 } elsif (m/^--ignore-dirty$/s) {
2036 } elsif (m/^--no-quilt-fixup$/s) {
2040 badusage "unknown long option \`$_'";
2047 } elsif (s/^-L/-/) {
2050 } elsif (s/^-h/-/) {
2052 } elsif (s/^-D/-/) {
2054 open DEBUG, ">&STDERR" or die $!;
2057 } elsif (s/^-N/-/) {
2060 } elsif (s/^-v([^_]+|_)$//s) {
2062 $changes_since_version = $1;
2065 push @changesopts, $_;
2067 } elsif (s/^-c(.*=.*)//s) {
2069 push @git, '-c', $1;
2070 } elsif (s/^-d(.*)//s) {
2073 } elsif (s/^-C(.*)//s) {
2076 if ($changesfile =~ s#^(.*)/##) {
2077 $buildproductsdir = $1;
2079 } elsif (s/^-k(.*)//s) {
2081 } elsif (s/^-wn//s) {
2083 $cleanmode = 'none';
2084 } elsif (s/^-wg//s) {
2087 } elsif (s/^-wd//s) {
2089 $cleanmode = 'dpkg-source';
2091 badusage "unknown short option \`$_'";
2098 if ($ENV{$fakeeditorenv}) {
2099 quilt_fixup_editor();
2102 delete $ENV{'DGET_UNPACK'};
2105 print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
2106 print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
2107 if $dryrun_level == 1;
2109 print STDERR $helpmsg or die $!;
2112 my $cmd = shift @ARGV;
2114 { no strict qw(refs); &{"cmd_$cmd"}(); }