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;
32 our $our_version = 'UNRELEASED'; ###substituted###
34 our $isuite = 'unstable';
45 our $existing_package = 'dpkg';
46 our $cleanmode = 'dpkg-source';
47 our $we_are_responder;
49 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
52 our (@dget) = qw(dget);
53 our (@dput) = qw(dput);
54 our (@debsign) = qw(debsign);
56 our (@sbuild) = qw(sbuild -A);
58 our (@dgit) = qw(dgit);
59 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
60 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
61 our (@dpkggenchanges) = qw(dpkg-genchanges);
62 our (@mergechanges) = qw(mergechanges -f);
63 our (@changesopts) = ('');
65 our %opts_opt_map = ('dget' => \@dget,
67 'debsign' => \@debsign,
72 'dpkg-source' => \@dpkgsource,
73 'dpkg-buildpackage' => \@dpkgbuildpackage,
74 'dpkg-genchanges' => \@dpkggenchanges,
75 'ch' => \@changesopts,
76 'mergechanges' => \@mergechanges);
81 open DEBUG, ">/dev/null" or die $!;
83 our $remotename = 'dgit';
84 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
85 our $branchprefix = 'dgit';
88 sub lbranch () { return "$branchprefix/$csuite"; }
89 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
90 sub lref () { return "refs/heads/".lbranch(); }
91 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
92 sub rrref () { return "refs/$branchprefix/$csuite"; }
107 return "${package}_".(stripepoch $vsn).".dsc";
110 sub changesopts () { return @changesopts[1..$#changesopts]; }
114 sub fail { die "$us: @_\n"; }
116 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
118 sub no_such_package () {
119 print STDERR "$us: package $package does not exist in suite $isuite\n";
125 return "+".rrref().":".lrref();
130 # remote push initiator/responder protocol:
131 # < dgit-remote-push-ready [optional extra info ignored by old initiators]
133 # > file begin parsed-changelog
134 # [indicates that output of dpkg-parsechangelog follows]
135 # > data-block NBYTES
136 # > [NBYTES bytes of data (no newline)]
137 # [maybe some more blocks]
143 # > file begin changes
147 # [indicates that signed tag is wanted]
148 # < data-block NBYTES
149 # < [NBYTES bytes of data (no newline)]
150 # [maybe some more blocks]
154 # > want signed-changes-dsc
155 # < data-block NBYTES [transfer of signed changes]
157 # < data-block NBYTES [transfer of signed dsc]
163 sub responder_send_command ($) {
165 return unless $we_are_responder;
166 # called even without $we_are_responder
167 print DEBUG "<< $command\n";
168 print $command, "\n" or die $!;
172 if ($we_are_responder) {
174 responder_send_command "progress ".length($m) or die $!;
183 fail "connection lost: $!" if $fh->error;
184 fail "connection terminated" if $fh->eof;
185 fail "protocol violation; $m not expected";
188 sub protocol_expect ($&) {
189 my ($fh, $match) = @_;
192 defined && chomp or badproto $fh, "eof";
194 badproto $fh, "\`$_'";
197 sub protocol_send_file ($$) {
198 my ($fh, $ourfn) = @_;
199 open PF, "<", $ourfn or die "$ourfn: $!";
202 my $got = read PF, $d, 65536;
203 die "$ourfn: $!" unless defined $got;
205 print $fh "data-block ".length($d)."\n" or die $!;
208 print $fh "data-end\n" or die $!;
212 sub protocol_receive_file ($$) {
213 my ($fh, $ourfn) = @_;
214 open PF, ">", $ourfn or die "$ourfn: $!";
216 protocol_expect \*STDIN, { m/^data-block (\d{1,6})$|data-end$/ };
219 my $got = read $fh, $d, $1;
220 $got==$1 or badproto $fh, "eof during data block";
221 print PF $d or die $!;
225 sub responder_send_file ($$) {
226 my ($keyword, $ourfn) = @_;
227 return unless $we_are_responder;
228 responder_send_command "file begin $cmdprefix";
229 protocol_send_file \*STDOUT, $ourfn;
232 sub responder_receive_files ($@) {
233 my ($keyword, @ourfns) = @_;
234 die unless $we_are_responder;
235 responder_send_command "want $keyword";
236 foreach my $fn (@ourfns) {
237 protocol_receive_file \*STDIN, $fn;
239 protocol_expect \*STDIN, { m/^files-end$/ };
244 $ua = LWP::UserAgent->new();
248 progress "downloading $what...";
249 my $r = $ua->get(@_) or die $!;
250 return undef if $r->code == 404;
251 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
252 return $r->decoded_content();
255 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
262 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
268 return join '', @out;
273 my $intro = shift @_;
274 print $fh $intro or die $!;
275 print $fh shellquote @_ or die $!;
276 print $fh "\n" or die $!;
280 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
282 fail "failed to fork/exec: $!";
283 } elsif (!($? & 0xff)) {
284 fail "subprocess failed with error exit status ".($?>>8);
286 fail "subprocess crashed (wait status $?)";
288 fail "subprocess produced invalid output";
293 printcmd(\*DEBUG,"+",@_) if $debug>0;
295 failedcmd @_ if system @_;
300 progress "dgit ok: @_";
302 progress "would be ok: @_ (but dry run only)";
306 sub cmdoutput_errok {
307 die Dumper(\@_)." ?" if grep { !defined } @_;
308 printcmd(\*DEBUG,"|",@_) if $debug>0;
309 open P, "-|", @_ or die $!;
312 { local $/ = undef; $d = <P>; }
314 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
317 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
322 my $d = cmdoutput_errok @_;
323 defined $d or failedcmd @_;
328 printcmd(\*STDERR,"#",@_);
331 sub runcmd_ordryrun {
340 my ($first_shell, @cmd) = @_;
341 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
344 our $helpmsg = <<END;
346 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
347 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
348 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
349 dgit [dgit-opts] push [dgit-opts] [suite]
350 important dgit options:
351 -k<keyid> sign tag and package with <keyid> instead of default
352 --dry-run -n do not change anything, but go through the motions
353 --new -N allow introducing a new package
354 --debug -D increase debug level
355 -c<name>=<value> set git config option (used directly by dgit too)
358 our $later_warning_msg = <<END;
359 Perhaps the upload is stuck in incoming. Using the version from git.
363 print STDERR "$us: @_\n", $helpmsg or die $!;
368 @ARGV or badusage "too few arguments";
369 return scalar shift @ARGV;
373 print $helpmsg or die $!;
377 our %defcfg = ('dgit.default.distro' => 'debian',
378 'dgit.default.username' => '',
379 'dgit.default.archive-query-default-component' => 'main',
380 'dgit.default.ssh' => 'ssh',
381 'dgit-distro.debian.git-host' => 'git.debian.org',
382 'dgit-distro.debian.git-proto' => 'git+ssh://',
383 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
384 'dgit-distro.debian.git-check' => 'ssh-cmd',
385 'dgit-distro.debian.git-create' => 'ssh-cmd',
386 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
387 'dgit-distro.debian.sshdakls-dir' =>
388 '/srv/ftp-master.debian.org/ftp/dists',
389 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
390 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
394 return undef if $c =~ /RETURN-UNDEF/;
395 my @cmd = (@git, qw(config --), $c);
398 local ($debug) = $debug-1;
399 $v = cmdoutput_errok @cmd;
406 my $dv = $defcfg{$c};
407 return $dv if defined $dv;
409 badcfg "need value for one of: @_";
412 sub access_distro () {
413 return cfg("dgit-suite.$isuite.distro",
414 "dgit.default.distro");
419 my $distro = $idistro || access_distro();
420 my $value = cfg(map { ("dgit-distro.$distro.$_",
421 "dgit.default.$_") } @keys);
425 sub access_someuserhost ($) {
427 my $user = access_cfg("$some-user",'username');
428 my $host = access_cfg("$some-host");
429 return length($user) ? "$user\@$host" : $host;
432 sub access_gituserhost () {
433 return access_someuserhost('git');
436 sub access_giturl () {
437 my $url = access_cfg('git-url','RETURN-UNDEF');
440 access_cfg('git-proto').
441 access_gituserhost().
442 access_cfg('git-path');
444 return "$url/$package.git";
447 sub parsecontrolfh ($$@) {
448 my ($fh, $desc, @opts) = @_;
449 my %opts = ('name' => $desc, @opts);
450 my $c = Dpkg::Control::Hash->new(%opts);
451 $c->parse($fh) or die "parsing of $desc failed";
456 my ($file, $desc) = @_;
457 my $fh = new IO::Handle;
458 open $fh, '<', $file or die "$file: $!";
459 my $c = parsecontrolfh($fh,$desc);
460 $fh->error and die $!;
466 my ($dctrl,$field) = @_;
467 my $v = $dctrl->{$field};
468 return $v if defined $v;
469 fail "missing field $field in ".$v->get_option('name');
473 my $c = Dpkg::Control::Hash->new();
474 my $p = new IO::Handle;
475 my @cmd = (qw(dpkg-parsechangelog), @_);
476 open $p, '-|', @cmd or die $!;
478 $?=0; $!=0; close $p or failedcmd @cmd;
484 sub archive_query ($) {
486 my $query = access_cfg('archive-query','RETURN-UNDEF');
487 if (!defined $query) {
488 my $distro = access_distro();
489 if ($distro eq 'debian') {
490 $query = "sshdakls:".
491 access_someuserhost('sshdakls').':'.
492 access_cfg('sshdakls-dir');
494 $query = "madison:$distro";
497 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
500 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
503 sub archive_query_madison ($$) {
504 my ($proto,$data) = @_;
505 die unless $proto eq 'madison';
506 $rmad{$package} ||= cmdoutput
507 qw(rmadison -asource),"-s$isuite","-u$data",$package;
508 my $rmad = $rmad{$package};
509 return madison_parse($rmad);
512 sub archive_query_sshdakls ($$) {
513 my ($proto,$data) = @_;
514 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
515 my $dakls = cmdoutput
516 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
517 return madison_parse($dakls);
520 sub canonicalise_suite_sshdakls ($$) {
521 my ($proto,$data) = @_;
522 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
524 (access_cfg('ssh'), $`,
526 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
527 " if test -d $isuite; then echo $isuite; exit 0; fi;".
529 my $dakls = cmdoutput @cmd;
530 failedcmd @cmd unless $dakls =~ m/^\w/;
534 sub madison_parse ($) {
537 foreach my $l (split /\n/, $rmad) {
538 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
539 \s*( [^ \t|]+ )\s* \|
540 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
541 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
542 $1 eq $package or die "$rmad $package ?";
549 $component = access_cfg('archive-query-default-component');
551 $5 eq 'source' or die "$rmad ?";
552 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
553 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
554 push @out, [$vsn,$subpath,$newsuite];
556 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
559 sub canonicalise_suite_madison ($$) {
560 my @r = archive_query_madison($_[0],$_[1]);
562 "unable to canonicalise suite using package $package".
563 " which does not appear to exist in suite $isuite;".
564 " --existing-package may help";
568 sub canonicalise_suite () {
569 return if defined $csuite;
570 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
571 $csuite = archive_query('canonicalise_suite');
572 if ($isuite ne $csuite) {
573 # madison canonicalises for us
574 progress "canonical suite name for $isuite is $csuite";
578 sub get_archive_dsc () {
579 canonicalise_suite();
580 my @vsns = archive_query('archive_query');
581 foreach my $vinfo (@vsns) {
582 my ($vsn,$subpath) = @$vinfo;
583 $dscurl = access_cfg('mirror').$subpath;
584 $dscdata = url_get($dscurl);
586 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
589 my $dscfh = new IO::File \$dscdata, '<' or die $!;
590 print DEBUG Dumper($dscdata) if $debug>1;
591 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
592 print DEBUG Dumper($dsc) if $debug>1;
593 my $fmt = getfield $dsc, 'Format';
594 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
600 sub check_for_git () {
602 my $how = access_cfg('git-check');
603 if ($how eq 'ssh-cmd') {
605 (access_cfg('ssh'),access_gituserhost(),
606 " set -e; cd ".access_cfg('git-path').";".
607 " if test -d $package.git; then echo 1; else echo 0; fi");
608 my $r= cmdoutput @cmd;
609 failedcmd @cmd unless $r =~ m/^[01]$/;
612 badcfg "unknown git-check \`$how'";
616 sub create_remote_git_repo () {
617 my $how = access_cfg('git-create');
618 if ($how eq 'ssh-cmd') {
620 (access_cfg('ssh'),access_gituserhost(),
621 "set -e; cd ".access_cfg('git-path').";".
622 " cp -a _template $package.git");
624 badcfg "unknown git-create \`$how'";
628 our ($dsc_hash,$lastpush_hash);
630 our $ud = '.git/dgit/unpack';
638 sub mktree_in_ud_from_only_subdir () {
639 # changes into the subdir
642 $dirs[0] =~ m#^([^/]+)/\.$# or die;
644 chdir $dir or die "$dir $!";
645 fail "source package contains .git directory" if stat '.git';
646 die $! unless $!==&ENOENT;
647 runcmd qw(git init -q);
648 rmtree('.git/objects');
649 symlink '../../../../objects','.git/objects' or die $!;
650 runcmd @git, qw(add -Af);
651 my $tree = cmdoutput @git, qw(write-tree);
652 $tree =~ m/^\w+$/ or die "$tree ?";
656 sub dsc_files_info () {
657 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
658 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
659 ['Files', 'Digest::MD5', 'new()']) {
660 my ($fname, $module, $method) = @$csumi;
661 my $field = $dsc->{$fname};
662 next unless defined $field;
663 eval "use $module; 1;" or die $@;
665 foreach (split /\n/, $field) {
667 m/^(\w+) (\d+) (\S+)$/ or
668 fail "could not parse .dsc $fname line \`$_'";
669 my $digester = eval "$module"."->$method;" or die $@;
674 Digester => $digester,
679 fail "missing any supported Checksums-* or Files field in ".
680 $dsc->get_option('name');
684 map { $_->{Filename} } dsc_files_info();
687 sub is_orig_file ($) {
689 m/\.orig(?:-\w+)?\.tar\.\w+$/;
692 sub make_commit ($) {
694 return cmdoutput @git, qw(hash-object -w -t commit), $file;
697 sub clogp_authline ($) {
699 my $author = getfield $clogp, 'Maintainer';
700 $author =~ s#,.*##ms;
701 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
702 my $authline = "$author $date";
703 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
704 fail "unexpected commit author line format \`$authline'".
705 " (was generated from changelog Maintainer field)";
709 sub generate_commit_from_dsc () {
713 foreach my $f (dsc_files()) {
714 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
716 link "../../../$f", $f
720 runcmd @dget, qw(--), $dscurl;
721 foreach my $f (grep { is_orig_file($_) } @files) {
722 link $f, "../../../../$f"
726 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
727 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
728 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
729 my $authline = clogp_authline $clogp;
730 my $changes = getfield $clogp, 'Changes';
731 open C, ">../commit.tmp" or die $!;
732 print C <<END or die $!;
739 # imported from the archive
742 my $outputhash = make_commit qw(../commit.tmp);
743 my $cversion = getfield $clogp, 'Version';
744 progress "synthesised git commit from .dsc $cversion";
745 if ($lastpush_hash) {
746 runcmd @git, qw(reset --hard), $lastpush_hash;
747 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
748 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
749 my $oversion = getfield $oldclogp, 'Version';
751 version_compare_string($oversion, $cversion);
753 # git upload/ is earlier vsn than archive, use archive
754 open C, ">../commit2.tmp" or die $!;
755 print C <<END or die $!;
757 parent $lastpush_hash
762 Record $package ($cversion) in archive suite $csuite
764 $outputhash = make_commit qw(../commit2.tmp);
765 } elsif ($vcmp > 0) {
766 print STDERR <<END or die $!;
768 Version actually in archive: $cversion (older)
769 Last allegedly pushed/uploaded: $oversion (newer or same)
772 $outputhash = $lastpush_hash;
774 $outputhash = $lastpush_hash;
777 chdir '../../../..' or die $!;
778 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
779 'DGIT_ARCHIVE', $outputhash;
780 cmdoutput @git, qw(log -n2), $outputhash;
781 # ... gives git a chance to complain if our commit is malformed
786 sub ensure_we_have_orig () {
787 foreach my $fi (dsc_files_info()) {
788 my $f = $fi->{Filename};
789 next unless is_orig_file($f);
790 if (open F, "<", "../$f") {
791 $fi->{Digester}->reset();
792 $fi->{Digester}->addfile(*F);
794 my $got = $fi->{Digester}->hexdigest();
795 $got eq $fi->{Hash} or
796 fail "existing file $f has hash $got but .dsc".
797 " demands hash $fi->{Hash}".
798 " (perhaps you should delete this file?)";
799 progress "using existing $f";
802 die "$f $!" unless $!==&ENOENT;
804 my $origurl = $dscurl;
805 $origurl =~ s{/[^/]+$}{};
807 die "$f ?" unless $f =~ m/^${package}_/;
808 die "$f ?" if $f =~ m#/#;
809 runcmd_ordryrun shell_cmd 'cd ..', @dget,'--',$origurl;
814 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
817 sub is_fast_fwd ($$) {
818 my ($ancestor,$child) = @_;
819 my @cmd = (@git, qw(merge-base), $ancestor, $child);
820 my $mb = cmdoutput_errok @cmd;
822 return rev_parse($mb) eq rev_parse($ancestor);
824 $?==256 or failedcmd @cmd;
829 sub git_fetch_us () {
830 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
833 sub fetch_from_archive () {
834 # ensures that lrref() is what is actually in the archive,
839 foreach my $field (@ourdscfield) {
840 $dsc_hash = $dsc->{$field};
841 last if defined $dsc_hash;
843 if (defined $dsc_hash) {
844 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
846 progress "last upload to archive specified git hash";
848 progress "last upload to archive has NO git hash";
851 progress "no version available from the archive";
854 my $lrref_fn = ".git/".lrref();
855 if (open H, $lrref_fn) {
856 $lastpush_hash = <H>;
857 chomp $lastpush_hash;
858 die "$lrref_fn $lastpush_hash ?" unless $lastpush_hash =~ m/^\w+$/;
859 } elsif ($! == &ENOENT) {
864 print DEBUG "previous reference hash=$lastpush_hash\n";
866 if (defined $dsc_hash) {
867 fail "missing git history even though dsc has hash -".
868 " could not find commit $dsc_hash".
869 " (should be in ".access_giturl()."#".rrref().")"
870 unless $lastpush_hash;
872 ensure_we_have_orig();
873 if ($dsc_hash eq $lastpush_hash) {
874 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
875 print STDERR <<END or die $!;
877 Git commit in archive is behind the last version allegedly pushed/uploaded.
878 Commit referred to by archive: $dsc_hash
879 Last allegedly pushed/uploaded: $lastpush_hash
882 $hash = $lastpush_hash;
884 fail "archive's .dsc refers to ".$dsc_hash.
885 " but this is an ancestor of ".$lastpush_hash;
888 $hash = generate_commit_from_dsc();
889 } elsif ($lastpush_hash) {
890 # only in git, not in the archive yet
891 $hash = $lastpush_hash;
892 print STDERR <<END or die $!;
894 Package not found in the archive, but has allegedly been pushed using dgit.
898 print DEBUG "nothing found!\n";
899 if (defined $skew_warning_vsn) {
900 print STDERR <<END or die $!;
902 Warning: relevant archive skew detected.
903 Archive allegedly contains $skew_warning_vsn
904 But we were not able to obtain any version from the archive or git.
910 print DEBUG "current hash=$hash\n";
911 if ($lastpush_hash) {
912 fail "not fast forward on last upload branch!".
913 " (archive's version left in DGIT_ARCHIVE)"
914 unless is_fast_fwd($lastpush_hash, $hash);
916 if (defined $skew_warning_vsn) {
918 print DEBUG "SKEW CHECK WANT $skew_warning_vsn\n";
919 my $clogf = ".git/dgit/changelog.tmp";
920 runcmd shell_cmd "exec >$clogf",
921 @git, qw(cat-file blob), "$hash:debian/changelog";
922 my $gotclogp = parsechangelog("-l$clogf");
923 my $got_vsn = getfield $gotclogp, 'Version';
924 print DEBUG "SKEW CHECK GOT $got_vsn\n";
925 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
926 print STDERR <<END or die $!;
928 Warning: archive skew detected. Using the available version:
929 Archive allegedly contains $skew_warning_vsn
930 We were able to obtain only $got_vsn
935 if ($lastpush_hash ne $hash) {
936 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
940 dryrun_report @upd_cmd;
948 canonicalise_suite();
949 badusage "dry run makes no sense with clone" if $dryrun;
950 mkdir $dstdir or die "$dstdir $!";
951 chdir "$dstdir" or die "$dstdir $!";
952 runcmd @git, qw(init -q);
953 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
954 open H, "> .git/HEAD" or die $!;
955 print H "ref: ".lref()."\n" or die $!;
957 runcmd @git, qw(remote add), 'origin', access_giturl();
958 if (check_for_git()) {
959 progress "fetching existing git history";
961 runcmd_ordryrun @git, qw(fetch origin);
963 progress "starting new git history";
965 fetch_from_archive() or no_such_package;
966 runcmd @git, qw(reset --hard), lrref();
967 printdone "ready for work in $dstdir";
971 if (check_for_git()) {
974 fetch_from_archive() or no_such_package();
975 printdone "fetched into ".lrref();
980 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
982 printdone "fetched to ".lrref()." and merged into HEAD";
985 sub check_not_dirty () {
986 return if $ignoredirty;
987 my @cmd = (@git, qw(diff --quiet HEAD));
988 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
989 $!=0; $?=0; system @cmd;
990 return if !$! && !$?;
991 if (!$! && $?==256) {
992 fail "working tree is dirty (does not match HEAD)";
998 sub commit_quilty_patch () {
999 my $output = cmdoutput @git, qw(status --porcelain);
1002 foreach my $l (split /\n/, $output) {
1003 next unless $l =~ m/\S/;
1004 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
1007 print STDERR "git status: $l\n";
1011 fail "unexpected output from git status (is tree clean?)" if $bad;
1013 progress "nothing quilty to commit, ok.";
1016 runcmd_ordryrun @git, qw(add), sort keys %adds;
1017 my $m = "Commit Debian 3.0 (quilt) metadata";
1019 runcmd_ordryrun @git, qw(commit -m), $m;
1024 return 0 unless $format eq '3.0 (quilt)';
1025 progress "Format \`$format', urgh";
1027 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
1033 sub push_parse_changelog ($) {
1036 my $clogp = Dpkg::Control::Hash->new();
1037 $clogp->load($clogpfn);
1039 $package = getfield $clogp, 'Source';
1040 my $cversion = getfield $clogp, 'Version';
1041 my $tag = debiantag($cversion);
1042 runcmd @git, qw(check-ref-format), $tag;
1044 my $dscfn = dscfn($cversion);
1046 return ($clogp, $cversion, $tag, $dscfn);
1049 sub push_parse_dsc ($$) {
1050 my ($dscfn,$dscfnwhat, $cversion) = @_;
1051 $dsc = parsecontrol($dscfn,$dscfnwhat);
1052 my $dversion = getfield $dsc, 'Version';
1053 my $dscpackage = getfield $dsc, 'Source';
1054 ($dscpackage eq $package && $dversion eq $cversion) or
1055 fail "$dsc is for $dscpackage $dversion".
1056 " but debian/changelog is for $package $cversion";
1059 sub push_mktag ($$$$$$$$) {
1060 my ($head,$clogp,$tag,
1062 $changesfile,$changesfilewhat,
1065 $dsc->{$ourdscfield[0]} = $head;
1066 $dsc->save("$dscfn.tmp") or die $!;
1068 my $changes = parsecontrol($changesfile,$changesfilewhat);
1069 foreach my $field (qw(Source Distribution Version)) {
1070 $changes->{$field} eq $clogp->{$field} or
1071 fail "changes field $field \`$changes->{$field}'".
1072 " does not match changelog \`$clogp->{$field}'";
1075 # We make the git tag by hand because (a) that makes it easier
1076 # to control the "tagger" (b) we can do remote signing
1077 my $authline = clogp_authline $clogp;
1078 open TO, '>', $tfn->('.tmp') or die $!;
1079 print TO <<END or die $!;
1085 $package release $cversion for $csuite [dgit]
1089 my $tagobjfn = $tfn->('.tmp');
1091 if (!defined $keyid) {
1092 $keyid = access_cfg('keyid','RETURN-UNDEF');
1094 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1095 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1096 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1097 push @sign_cmd, $tfn->('.tmp');
1098 runcmd_ordryrun @sign_cmd;
1100 $tagobjfn = $tfn->('.signed.tmp');
1101 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1102 $tfn->('.tmp'), $tfn->('.tmp.asc');
1110 print DEBUG "actually entering push\n";
1113 my $clogpfn = ".git/dgit/changelog.822.tmp";
1114 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1116 responder_send_file('parsed-changelog', $clogpfn);
1118 my ($clogp, $cversion, $tag, $dscfn) =
1119 push_parse_changelog("$clogpfn");
1122 fail "looked for .dsc $dscfn, but $!;".
1123 " maybe you forgot to build";
1125 responder_send_file('dsc', "../$dscfn");
1127 push_parse_dsc("../$dscfn", $dscfn, $cversion);
1129 my $format = getfield $dsc, 'Format';
1130 print DEBUG "format $format\n";
1131 if (madformat($format)) {
1132 commit_quilty_patch();
1135 chdir $ud or die $!;
1136 progress "checking that $dscfn corresponds to HEAD";
1137 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
1138 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1139 chdir '../../../..' or die $!;
1140 printcmd \*DEBUG,"+",@_;
1141 my @diffcmd = (@git, qw(diff --exit-code), $tree);
1143 if (system @diffcmd) {
1144 if ($! && $?==256) {
1145 fail "$dscfn specifies a different tree to your HEAD commit;".
1146 " perhaps you forgot to build";
1152 #do fast forward check and maybe fake merge
1153 # if (!is_fast_fwd(mainbranch
1154 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1155 # map { lref($_).":".rref($_) }
1157 my $head = rev_parse('HEAD');
1158 if (!$changesfile) {
1159 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
1160 if (stat "$multi") {
1161 $changesfile = $multi;
1163 $!==&ENOENT or die "$multi: $!";
1164 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1165 my @cs = glob "../$pat";
1166 fail "failed to find unique changes file".
1167 " (looked for $pat in .., or $multi);".
1168 " perhaps you need to use dgit -C"
1170 ($changesfile) = @cs;
1174 responder_send_file('changes',$changesfn);
1176 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1179 ? responder_receive_files('signed-tag', $tfn->('.signed.tmp'))
1180 : push_mktag($head,$clogp,$tag,
1182 $changesfile,$changesfile,
1185 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1186 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1187 runcmd_ordryrun @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1188 runcmd_ordryrun @git, qw(tag -v --), $tag;
1190 if (!check_for_git()) {
1191 create_remote_git_repo();
1193 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
1194 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1196 if (!$we_are_responder) {
1198 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
1200 progress "[new .dsc left in $dscfn.tmp]";
1205 if ($we_are_responder) {
1206 my $dryrunsuffix = $dryrun ? ".tmp" : "";
1207 responder_receive_files('signed-changes-dsc',
1208 "$changesfile$dryrunsuffix",
1209 "../$dscfn$dryrunsuffix");
1211 my @debsign_cmd = @debsign;
1212 push @debsign_cmd, "-k$keyid" if defined $keyid;
1213 push @debsign_cmd, $changesfile;
1214 runcmd_ordryrun @debsign_cmd;
1217 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
1218 my $host = access_cfg('upload-host','RETURN-UNDEF');
1219 my @hostarg = defined($host) ? ($host,) : ();
1220 runcmd_ordryrun @dput, @hostarg, $changesfile;
1221 printdone "pushed and uploaded $cversion";
1223 responder_send_command("complete");
1229 badusage "-p is not allowed with clone; specify as argument instead"
1230 if defined $package;
1233 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1234 ($package,$isuite) = @ARGV;
1235 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1236 ($package,$dstdir) = @ARGV;
1237 } elsif (@ARGV==3) {
1238 ($package,$isuite,$dstdir) = @ARGV;
1240 badusage "incorrect arguments to dgit clone";
1242 $dstdir ||= "$package";
1246 sub branchsuite () {
1247 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1248 if ($branch =~ m#$lbranch_re#o) {
1255 sub fetchpullargs () {
1256 if (!defined $package) {
1257 my $sourcep = parsecontrol('debian/control','debian/control');
1258 $package = getfield $sourcep, 'Source';
1261 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1263 my $clogp = parsechangelog();
1264 $isuite = getfield $clogp, 'Distribution';
1266 canonicalise_suite();
1267 progress "fetching from suite $csuite";
1268 } elsif (@ARGV==1) {
1270 canonicalise_suite();
1272 badusage "incorrect arguments to dgit fetch or dgit pull";
1290 badusage "-p is not allowed with dgit push" if defined $package;
1292 my $clogp = parsechangelog();
1293 $package = getfield $clogp, 'Source';
1296 } elsif (@ARGV==1) {
1297 ($specsuite) = (@ARGV);
1299 badusage "incorrect arguments to dgit push";
1301 $isuite = getfield $clogp, 'Distribution';
1303 local ($package) = $existing_package; # this is a hack
1304 canonicalise_suite();
1306 if (defined $specsuite && $specsuite ne $isuite) {
1307 canonicalise_suite();
1308 $csuite eq $specsuite or
1309 fail "dgit push: changelog specifies $isuite ($csuite)".
1310 " but command line specifies $specsuite";
1312 if (check_for_git()) {
1315 if (fetch_from_archive()) {
1316 is_fast_fwd(lrref(), 'HEAD') or
1317 fail "dgit push: HEAD is not a descendant".
1318 " of the archive's version.\n".
1319 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1322 fail "package appears to be new in this suite;".
1323 " if this is intentional, use --new";
1328 sub cmd_remote_push_responder {
1329 my ($nrargs) = shift @ARGV;
1330 my (@rargs) = @ARGV[0..$nrargs-1];
1331 @ARGV = @ARGV[$nrargs..$#ARGV];
1334 chdir $dir or die "$dir: $!";
1337 responder_send_command("dgit-remote-push-ready");
1344 if ($host =~ m/^((?:[^][]|\[[^][]*\])*)\:/) {
1354 push @rdgit, @ropts;
1355 push @rdgit, (scalar @rargs), @rargs;
1357 my @cmd = (@ssh, $host, shellquote @rdgit);
1358 my $pid = open2(\*RO, \*RI, @cmd);
1359 initiator_expect { m/^dgit-remote-push-ready/ };
1361 initiator_expect { m/^(\S+)\s+(.*)$/ };
1362 my ($icmd,$iargs) = ($1, $2);
1364 { no strict qw(refs); &{"i_resp_$icmd"}($iargs); }
1372 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1374 sub build_maybe_quilt_fixup () {
1375 if (!open F, "debian/source/format") {
1376 die $! unless $!==&ENOENT;
1380 F->error and die $!;
1382 return unless madformat($_);
1384 my $clogp = parsechangelog();
1385 my $version = getfield $clogp, 'Version';
1386 my $author = getfield $clogp, 'Maintainer';
1387 my $headref = rev_parse('HEAD');
1390 my $patchname = "auto-$version-$headref-$time";
1391 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1393 my $descfn = ".git/dgit/quilt-description.tmp";
1394 open O, '>', $descfn or die "$descfn: $!";
1396 $msg =~ s/^\s+$/ ./mg;
1397 print O <<END or die $!;
1398 Description: Automatically generated patch ($clogp->{Version})
1399 Last (up to) $ncommits git changes, FYI:
1409 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1410 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1411 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1412 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1415 if (!open P, '>>', ".pc/applied-patches") {
1416 $!==&ENOENT or die $!;
1421 commit_quilty_patch();
1424 sub quilt_fixup_editor () {
1425 my $descfn = $ENV{$fakeeditorenv};
1426 my $editing = $ARGV[$#ARGV];
1427 open I1, '<', $descfn or die "$descfn: $!";
1428 open I2, '<', $editing or die "$editing: $!";
1429 unlink $editing or die "$editing: $!";
1430 open O, '>', $editing or die "$editing: $!";
1431 while (<I1>) { print O or die $!; } I1->error and die $!;
1434 $copying ||= m/^\-\-\- /;
1435 next unless $copying;
1438 I2->error and die $!;
1444 badusage "-p is not allowed when building" if defined $package;
1446 my $clogp = parsechangelog();
1447 $isuite = getfield $clogp, 'Distribution';
1448 $package = getfield $clogp, 'Source';
1449 $version = getfield $clogp, 'Version';
1450 build_maybe_quilt_fixup();
1454 badusage "dgit build implies --clean=dpkg-source"
1455 if $cleanmode ne 'dpkg-source';
1457 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1458 printdone "build successful\n";
1462 badusage "dgit git-build implies --clean=dpkg-source"
1463 if $cleanmode ne 'dpkg-source';
1466 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1467 "--git-builder=@dpkgbuildpackage");
1468 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1469 canonicalise_suite();
1470 push @cmd, "--git-debian-branch=".lbranch();
1472 push @cmd, changesopts();
1473 runcmd_ordryrun @cmd, @ARGV;
1474 printdone "build successful\n";
1479 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1480 $dscfn = dscfn($version);
1481 if ($cleanmode eq 'dpkg-source') {
1482 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1484 if ($cleanmode eq 'git') {
1485 runcmd_ordryrun @git, qw(clean -xdf);
1486 } elsif ($cleanmode eq 'none') {
1490 my $pwd = cmdoutput qw(env - pwd);
1491 my $leafdir = basename $pwd;
1492 chdir ".." or die $!;
1493 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1494 chdir $pwd or die $!;
1495 runcmd_ordryrun qw(sh -ec),
1496 'exec >$1; shift; exec "$@"','x',
1497 "../$sourcechanges",
1498 @dpkggenchanges, qw(-S), changesopts();
1502 sub cmd_build_source {
1503 badusage "build-source takes no additional arguments" if @ARGV;
1505 printdone "source built, results in $dscfn and $sourcechanges";
1510 chdir ".." or die $!;
1511 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1513 stat $dscfn or fail "$dscfn (in parent directory): $!";
1514 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1515 foreach my $cf (glob $pat) {
1516 next if $cf eq $sourcechanges;
1517 unlink $cf or fail "remove $cf: $!";
1520 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1521 runcmd_ordryrun @mergechanges, glob $pat;
1522 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1524 stat $multichanges or fail "$multichanges: $!";
1526 printdone "build successful, results in $multichanges\n" or die $!;
1529 sub cmd_quilt_fixup {
1530 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1531 my $clogp = parsechangelog();
1532 $version = getfield $clogp, 'Version';
1533 build_maybe_quilt_fixup();
1537 print "dgit version $our_version\n" or die $!;
1544 last unless $ARGV[0] =~ m/^-/;
1548 if (m/^--dry-run$/) {
1551 } elsif (m/^--no-sign$/) {
1554 } elsif (m/^--help$/) {
1556 } elsif (m/^--version$/) {
1558 } elsif (m/^--new$/) {
1561 } elsif (m/^--(\w+)=(.*)/s &&
1562 ($om = $opts_opt_map{$1}) &&
1566 } elsif (m/^--(\w+):(.*)/s &&
1567 ($om = $opts_opt_map{$1})) {
1570 } elsif (m/^--existing-package=(.*)/s) {
1572 $existing_package = $1;
1573 } elsif (m/^--distro=(.*)/s) {
1576 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1579 } elsif (m/^--clean=(.*)$/s) {
1580 badusage "unknown cleaning mode \`$1'";
1581 } elsif (m/^--ignore-dirty$/s) {
1584 } elsif (m/^--no-quilt-fixup$/s) {
1588 badusage "unknown long option \`$_'";
1595 } elsif (s/^-h/-/) {
1597 } elsif (s/^-D/-/) {
1599 open DEBUG, ">&STDERR" or die $!;
1601 } elsif (s/^-N/-/) {
1604 } elsif (m/^-[vm]/) {
1606 push @changesopts, $_;
1608 } elsif (s/^-c(.*=.*)//s) {
1610 push @git, '-c', $1;
1611 } elsif (s/^-d(.*)//s) {
1614 } elsif (s/^-C(.*)//s) {
1617 } elsif (s/^-k(.*)//s) {
1619 } elsif (s/^-wn//s) {
1621 $cleanmode = 'none';
1622 } elsif (s/^-wg//s) {
1625 } elsif (s/^-wd//s) {
1627 $cleanmode = 'dpkg-source';
1629 badusage "unknown short option \`$_'";
1636 if ($ENV{$fakeeditorenv}) {
1637 quilt_fixup_editor();
1640 delete $ENV{'DGET_UNPACK'};
1643 print STDERR "DRY RUN ONLY\n" if $dryrun;
1645 print STDERR $helpmsg or die $!;
1648 my $cmd = shift @ARGV;
1650 { no strict qw(refs); &{"cmd_$cmd"}(); }