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;
31 our $our_version = 'UNRELEASED'; ###substituted###
33 our $isuite = 'unstable';
43 our $existing_package = 'dpkg';
44 our $cleanmode = 'dpkg-source';
45 our $we_are_responder;
47 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
50 our (@dget) = qw(dget);
51 our (@dput) = qw(dput);
52 our (@debsign) = qw(debsign);
54 our (@sbuild) = qw(sbuild -A);
55 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
56 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
57 our (@dpkggenchanges) = qw(dpkg-genchanges);
58 our (@mergechanges) = qw(mergechanges -f);
59 our (@changesopts) = ('');
61 our %opts_opt_map = ('dget' => \@dget,
63 'debsign' => \@debsign,
66 'dpkg-source' => \@dpkgsource,
67 'dpkg-buildpackage' => \@dpkgbuildpackage,
68 'dpkg-genchanges' => \@dpkggenchanges,
69 'ch' => \@changesopts,
70 'mergechanges' => \@mergechanges);
75 open DEBUG, ">/dev/null" or die $!;
77 our $remotename = 'dgit';
78 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
79 our $branchprefix = 'dgit';
82 sub lbranch () { return "$branchprefix/$csuite"; }
83 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
84 sub lref () { return "refs/heads/".lbranch(); }
85 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
86 sub rrref () { return "refs/$branchprefix/$csuite"; }
101 return "${package}_".(stripepoch $vsn).".dsc";
104 sub changesopts () { return @changesopts[1..$#changesopts]; }
108 sub fail { die "$us: @_\n"; }
110 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
112 sub no_such_package () {
113 print STDERR "$us: package $package does not exist in suite $isuite\n";
119 return "+".rrref().":".lrref();
124 sub responder_send_command ($) {
126 return unless $we_are_responder;
127 # called even without $we_are_responder
128 print DEBUG "<< $command\n";
129 print $command, "\n" or die $!;
133 if ($we_are_responder) {
135 responder_send_command "progress ".length($m) or die $!;
142 sub protocol_send_file ($) {
143 my ($fh, $cmdprefix, $ourfn) = @_;
144 open PF, "<", $ourfn or die "$ourfn: $!";
145 print $fh "$cmdprefix begin\n" or die $!;
148 my $got = read PF, $d, 65536;
149 die "$ourfn: $!" unless defined $got;
151 print $fh "$keyword block ".length($d)."\n" or die $!;
154 print $fh "$keyword end\n" or die $!;
158 sub responder_send_file ($$) {
159 my ($keyword, $ourfn) = @_;
160 return unless $we_are_responder;
161 print DEBUG "responder sending $keyword $ourfn\n";
162 protocol_send_file(\*STDOUT, "upload $keyword");
165 sub responder_receive_files ($@) {
166 my ($keyword, @ourfns) = @_;
167 die unless $we_are_responder;
173 $ua = LWP::UserAgent->new();
177 progress "downloading $what...";
178 my $r = $ua->get(@_) or die $!;
179 return undef if $r->code == 404;
180 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
181 return $r->decoded_content();
184 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
188 my $intro = shift @_;
189 print $fh $intro or die $!;
193 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
194 print $fh " '$_'" or die $!;
196 print $fh " $_" or die $!;
199 print $fh "\n" or die $!;
203 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
205 fail "failed to fork/exec: $!";
206 } elsif (!($? & 0xff)) {
207 fail "subprocess failed with error exit status ".($?>>8);
209 fail "subprocess crashed (wait status $?)";
211 fail "subprocess produced invalid output";
216 printcmd(\*DEBUG,"+",@_) if $debug>0;
218 failedcmd @_ if system @_;
223 progress "dgit ok: @_";
225 progress "would be ok: @_ (but dry run only)";
229 sub cmdoutput_errok {
230 die Dumper(\@_)." ?" if grep { !defined } @_;
231 printcmd(\*DEBUG,"|",@_) if $debug>0;
232 open P, "-|", @_ or die $!;
235 { local $/ = undef; $d = <P>; }
237 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
240 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
245 my $d = cmdoutput_errok @_;
246 defined $d or failedcmd @_;
251 printcmd(\*STDERR,"#",@_);
254 sub runcmd_ordryrun {
263 my ($first_shell, @cmd) = @_;
264 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
267 our $helpmsg = <<END;
269 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
270 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
271 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
272 dgit [dgit-opts] push [dgit-opts] [suite]
273 important dgit options:
274 -k<keyid> sign tag and package with <keyid> instead of default
275 --dry-run -n do not change anything, but go through the motions
276 --new -N allow introducing a new package
277 --debug -D increase debug level
278 -c<name>=<value> set git config option (used directly by dgit too)
281 our $later_warning_msg = <<END;
282 Perhaps the upload is stuck in incoming. Using the version from git.
286 print STDERR "$us: @_\n", $helpmsg or die $!;
291 print $helpmsg or die $!;
295 our %defcfg = ('dgit.default.distro' => 'debian',
296 'dgit.default.username' => '',
297 'dgit.default.archive-query-default-component' => 'main',
298 'dgit.default.ssh' => 'ssh',
299 'dgit-distro.debian.git-host' => 'git.debian.org',
300 'dgit-distro.debian.git-proto' => 'git+ssh://',
301 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
302 'dgit-distro.debian.git-check' => 'ssh-cmd',
303 'dgit-distro.debian.git-create' => 'ssh-cmd',
304 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
305 'dgit-distro.debian.sshdakls-dir' =>
306 '/srv/ftp-master.debian.org/ftp/dists',
307 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
308 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
312 return undef if $c =~ /RETURN-UNDEF/;
313 my @cmd = (@git, qw(config --), $c);
316 local ($debug) = $debug-1;
317 $v = cmdoutput_errok @cmd;
324 my $dv = $defcfg{$c};
325 return $dv if defined $dv;
327 badcfg "need value for one of: @_";
330 sub access_distro () {
331 return cfg("dgit-suite.$isuite.distro",
332 "dgit.default.distro");
337 my $distro = $idistro || access_distro();
338 my $value = cfg(map { ("dgit-distro.$distro.$_",
339 "dgit.default.$_") } @keys);
343 sub access_someuserhost ($) {
345 my $user = access_cfg("$some-user",'username');
346 my $host = access_cfg("$some-host");
347 return length($user) ? "$user\@$host" : $host;
350 sub access_gituserhost () {
351 return access_someuserhost('git');
354 sub access_giturl () {
355 my $url = access_cfg('git-url','RETURN-UNDEF');
358 access_cfg('git-proto').
359 access_gituserhost().
360 access_cfg('git-path');
362 return "$url/$package.git";
365 sub parsecontrolfh ($$@) {
366 my ($fh, $desc, @opts) = @_;
367 my %opts = ('name' => $desc, @opts);
368 my $c = Dpkg::Control::Hash->new(%opts);
369 $c->parse($fh) or die "parsing of $desc failed";
374 my ($file, $desc) = @_;
375 my $fh = new IO::Handle;
376 open $fh, '<', $file or die "$file: $!";
377 my $c = parsecontrolfh($fh,$desc);
378 $fh->error and die $!;
384 my ($dctrl,$field) = @_;
385 my $v = $dctrl->{$field};
386 return $v if defined $v;
387 fail "missing field $field in ".$v->get_option('name');
391 my $c = Dpkg::Control::Hash->new();
392 my $p = new IO::Handle;
393 my @cmd = (qw(dpkg-parsechangelog), @_);
394 open $p, '-|', @cmd or die $!;
396 $?=0; $!=0; close $p or failedcmd @cmd;
402 sub archive_query ($) {
404 my $query = access_cfg('archive-query','RETURN-UNDEF');
405 if (!defined $query) {
406 my $distro = access_distro();
407 if ($distro eq 'debian') {
408 $query = "sshdakls:".
409 access_someuserhost('sshdakls').':'.
410 access_cfg('sshdakls-dir');
412 $query = "madison:$distro";
415 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
418 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
421 sub archive_query_madison ($$) {
422 my ($proto,$data) = @_;
423 die unless $proto eq 'madison';
424 $rmad{$package} ||= cmdoutput
425 qw(rmadison -asource),"-s$isuite","-u$data",$package;
426 my $rmad = $rmad{$package};
427 return madison_parse($rmad);
430 sub archive_query_sshdakls ($$) {
431 my ($proto,$data) = @_;
432 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
433 my $dakls = cmdoutput
434 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
435 return madison_parse($dakls);
438 sub canonicalise_suite_sshdakls ($$) {
439 my ($proto,$data) = @_;
440 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
442 (access_cfg('ssh'), $`,
444 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
445 " if test -d $isuite; then echo $isuite; exit 0; fi;".
447 my $dakls = cmdoutput @cmd;
448 failedcmd @cmd unless $dakls =~ m/^\w/;
452 sub madison_parse ($) {
455 foreach my $l (split /\n/, $rmad) {
456 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
457 \s*( [^ \t|]+ )\s* \|
458 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
459 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
460 $1 eq $package or die "$rmad $package ?";
467 $component = access_cfg('archive-query-default-component');
469 $5 eq 'source' or die "$rmad ?";
470 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
471 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
472 push @out, [$vsn,$subpath,$newsuite];
474 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
477 sub canonicalise_suite_madison ($$) {
478 my @r = archive_query_madison($_[0],$_[1]);
480 "unable to canonicalise suite using package $package".
481 " which does not appear to exist in suite $isuite;".
482 " --existing-package may help";
486 sub canonicalise_suite () {
487 return if defined $csuite;
488 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
489 $csuite = archive_query('canonicalise_suite');
490 if ($isuite ne $csuite) {
491 # madison canonicalises for us
492 progress "canonical suite name for $isuite is $csuite";
496 sub get_archive_dsc () {
497 canonicalise_suite();
498 my @vsns = archive_query('archive_query');
499 foreach my $vinfo (@vsns) {
500 my ($vsn,$subpath) = @$vinfo;
501 $dscurl = access_cfg('mirror').$subpath;
502 $dscdata = url_get($dscurl);
504 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
507 my $dscfh = new IO::File \$dscdata, '<' or die $!;
508 print DEBUG Dumper($dscdata) if $debug>1;
509 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
510 print DEBUG Dumper($dsc) if $debug>1;
511 my $fmt = getfield $dsc, 'Format';
512 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
518 sub check_for_git () {
520 my $how = access_cfg('git-check');
521 if ($how eq 'ssh-cmd') {
523 (access_cfg('ssh'),access_gituserhost(),
524 " set -e; cd ".access_cfg('git-path').";".
525 " if test -d $package.git; then echo 1; else echo 0; fi");
526 my $r= cmdoutput @cmd;
527 failedcmd @cmd unless $r =~ m/^[01]$/;
530 badcfg "unknown git-check \`$how'";
534 sub create_remote_git_repo () {
535 my $how = access_cfg('git-create');
536 if ($how eq 'ssh-cmd') {
538 (access_cfg('ssh'),access_gituserhost(),
539 "set -e; cd ".access_cfg('git-path').";".
540 " cp -a _template $package.git");
542 badcfg "unknown git-create \`$how'";
546 our ($dsc_hash,$lastpush_hash);
548 our $ud = '.git/dgit/unpack';
556 sub mktree_in_ud_from_only_subdir () {
557 # changes into the subdir
560 $dirs[0] =~ m#^([^/]+)/\.$# or die;
562 chdir $dir or die "$dir $!";
563 fail "source package contains .git directory" if stat '.git';
564 die $! unless $!==&ENOENT;
565 runcmd qw(git init -q);
566 rmtree('.git/objects');
567 symlink '../../../../objects','.git/objects' or die $!;
568 runcmd @git, qw(add -Af);
569 my $tree = cmdoutput @git, qw(write-tree);
570 $tree =~ m/^\w+$/ or die "$tree ?";
574 sub dsc_files_info () {
575 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
576 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
577 ['Files', 'Digest::MD5', 'new()']) {
578 my ($fname, $module, $method) = @$csumi;
579 my $field = $dsc->{$fname};
580 next unless defined $field;
581 eval "use $module; 1;" or die $@;
583 foreach (split /\n/, $field) {
585 m/^(\w+) (\d+) (\S+)$/ or
586 fail "could not parse .dsc $fname line \`$_'";
587 my $digester = eval "$module"."->$method;" or die $@;
592 Digester => $digester,
597 fail "missing any supported Checksums-* or Files field in ".
598 $dsc->get_option('name');
602 map { $_->{Filename} } dsc_files_info();
605 sub is_orig_file ($) {
607 m/\.orig(?:-\w+)?\.tar\.\w+$/;
610 sub make_commit ($) {
612 return cmdoutput @git, qw(hash-object -w -t commit), $file;
615 sub clogp_authline ($) {
617 my $author = getfield $clogp, 'Maintainer';
618 $author =~ s#,.*##ms;
619 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
620 my $authline = "$author $date";
621 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
622 fail "unexpected commit author line format \`$authline'".
623 " (was generated from changelog Maintainer field)";
627 sub generate_commit_from_dsc () {
631 foreach my $f (dsc_files()) {
632 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
634 link "../../../$f", $f
638 runcmd @dget, qw(--), $dscurl;
639 foreach my $f (grep { is_orig_file($_) } @files) {
640 link $f, "../../../../$f"
644 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
645 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
646 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
647 my $authline = clogp_authline $clogp;
648 my $changes = getfield $clogp, 'Changes';
649 open C, ">../commit.tmp" or die $!;
650 print C <<END or die $!;
657 # imported from the archive
660 my $outputhash = make_commit qw(../commit.tmp);
661 my $cversion = getfield $clogp, 'Version';
662 progress "synthesised git commit from .dsc $cversion";
663 if ($lastpush_hash) {
664 runcmd @git, qw(reset --hard), $lastpush_hash;
665 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
666 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
667 my $oversion = getfield $oldclogp, 'Version';
669 version_compare_string($oversion, $cversion);
671 # git upload/ is earlier vsn than archive, use archive
672 open C, ">../commit2.tmp" or die $!;
673 print C <<END or die $!;
675 parent $lastpush_hash
680 Record $package ($cversion) in archive suite $csuite
682 $outputhash = make_commit qw(../commit2.tmp);
683 } elsif ($vcmp > 0) {
684 print STDERR <<END or die $!;
686 Version actually in archive: $cversion (older)
687 Last allegedly pushed/uploaded: $oversion (newer or same)
690 $outputhash = $lastpush_hash;
692 $outputhash = $lastpush_hash;
695 chdir '../../../..' or die $!;
696 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
697 'DGIT_ARCHIVE', $outputhash;
698 cmdoutput @git, qw(log -n2), $outputhash;
699 # ... gives git a chance to complain if our commit is malformed
704 sub ensure_we_have_orig () {
705 foreach my $fi (dsc_files_info()) {
706 my $f = $fi->{Filename};
707 next unless is_orig_file($f);
708 if (open F, "<", "../$f") {
709 $fi->{Digester}->reset();
710 $fi->{Digester}->addfile(*F);
712 my $got = $fi->{Digester}->hexdigest();
713 $got eq $fi->{Hash} or
714 fail "existing file $f has hash $got but .dsc".
715 " demands hash $fi->{Hash}".
716 " (perhaps you should delete this file?)";
717 progress "using existing $f";
720 die "$f $!" unless $!==&ENOENT;
722 my $origurl = $dscurl;
723 $origurl =~ s{/[^/]+$}{};
725 die "$f ?" unless $f =~ m/^${package}_/;
726 die "$f ?" if $f =~ m#/#;
727 runcmd_ordryrun shell_cmd 'cd ..', @dget,'--',$origurl;
732 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
735 sub is_fast_fwd ($$) {
736 my ($ancestor,$child) = @_;
737 my @cmd = (@git, qw(merge-base), $ancestor, $child);
738 my $mb = cmdoutput_errok @cmd;
740 return rev_parse($mb) eq rev_parse($ancestor);
742 $?==256 or failedcmd @cmd;
747 sub git_fetch_us () {
748 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
751 sub fetch_from_archive () {
752 # ensures that lrref() is what is actually in the archive,
757 foreach my $field (@ourdscfield) {
758 $dsc_hash = $dsc->{$field};
759 last if defined $dsc_hash;
761 if (defined $dsc_hash) {
762 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
764 progress "last upload to archive specified git hash";
766 progress "last upload to archive has NO git hash";
769 progress "no version available from the archive";
772 my $lrref_fn = ".git/".lrref();
773 if (open H, $lrref_fn) {
774 $lastpush_hash = <H>;
775 chomp $lastpush_hash;
776 die "$lrref_fn $lastpush_hash ?" unless $lastpush_hash =~ m/^\w+$/;
777 } elsif ($! == &ENOENT) {
782 print DEBUG "previous reference hash=$lastpush_hash\n";
784 if (defined $dsc_hash) {
785 fail "missing git history even though dsc has hash -".
786 " could not find commit $dsc_hash".
787 " (should be in ".access_giturl()."#".rrref().")"
788 unless $lastpush_hash;
790 ensure_we_have_orig();
791 if ($dsc_hash eq $lastpush_hash) {
792 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
793 print STDERR <<END or die $!;
795 Git commit in archive is behind the last version allegedly pushed/uploaded.
796 Commit referred to by archive: $dsc_hash
797 Last allegedly pushed/uploaded: $lastpush_hash
800 $hash = $lastpush_hash;
802 fail "archive's .dsc refers to ".$dsc_hash.
803 " but this is an ancestor of ".$lastpush_hash;
806 $hash = generate_commit_from_dsc();
807 } elsif ($lastpush_hash) {
808 # only in git, not in the archive yet
809 $hash = $lastpush_hash;
810 print STDERR <<END or die $!;
812 Package not found in the archive, but has allegedly been pushed using dgit.
816 print DEBUG "nothing found!\n";
817 if (defined $skew_warning_vsn) {
818 print STDERR <<END or die $!;
820 Warning: relevant archive skew detected.
821 Archive allegedly contains $skew_warning_vsn
822 But we were not able to obtain any version from the archive or git.
828 print DEBUG "current hash=$hash\n";
829 if ($lastpush_hash) {
830 fail "not fast forward on last upload branch!".
831 " (archive's version left in DGIT_ARCHIVE)"
832 unless is_fast_fwd($lastpush_hash, $hash);
834 if (defined $skew_warning_vsn) {
836 print DEBUG "SKEW CHECK WANT $skew_warning_vsn\n";
837 my $clogf = ".git/dgit/changelog.tmp";
838 runcmd shell_cmd "exec >$clogf",
839 @git, qw(cat-file blob), "$hash:debian/changelog";
840 my $gotclogp = parsechangelog("-l$clogf");
841 my $got_vsn = getfield $gotclogp, 'Version';
842 print DEBUG "SKEW CHECK GOT $got_vsn\n";
843 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
844 print STDERR <<END or die $!;
846 Warning: archive skew detected. Using the available version:
847 Archive allegedly contains $skew_warning_vsn
848 We were able to obtain only $got_vsn
853 if ($lastpush_hash ne $hash) {
854 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
858 dryrun_report @upd_cmd;
866 canonicalise_suite();
867 badusage "dry run makes no sense with clone" if $dryrun;
868 mkdir $dstdir or die "$dstdir $!";
869 chdir "$dstdir" or die "$dstdir $!";
870 runcmd @git, qw(init -q);
871 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
872 open H, "> .git/HEAD" or die $!;
873 print H "ref: ".lref()."\n" or die $!;
875 runcmd @git, qw(remote add), 'origin', access_giturl();
876 if (check_for_git()) {
877 progress "fetching existing git history";
879 runcmd_ordryrun @git, qw(fetch origin);
881 progress "starting new git history";
883 fetch_from_archive() or no_such_package;
884 runcmd @git, qw(reset --hard), lrref();
885 printdone "ready for work in $dstdir";
889 if (check_for_git()) {
892 fetch_from_archive() or no_such_package();
893 printdone "fetched into ".lrref();
898 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
900 printdone "fetched to ".lrref()." and merged into HEAD";
903 sub check_not_dirty () {
904 return if $ignoredirty;
905 my @cmd = (@git, qw(diff --quiet HEAD));
906 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
907 $!=0; $?=0; system @cmd;
908 return if !$! && !$?;
909 if (!$! && $?==256) {
910 fail "working tree is dirty (does not match HEAD)";
916 sub commit_quilty_patch () {
917 my $output = cmdoutput @git, qw(status --porcelain);
920 foreach my $l (split /\n/, $output) {
921 next unless $l =~ m/\S/;
922 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
925 print STDERR "git status: $l\n";
929 fail "unexpected output from git status (is tree clean?)" if $bad;
931 progress "nothing quilty to commit, ok.";
934 runcmd_ordryrun @git, qw(add), sort keys %adds;
935 my $m = "Commit Debian 3.0 (quilt) metadata";
937 runcmd_ordryrun @git, qw(commit -m), $m;
942 return 0 unless $format eq '3.0 (quilt)';
943 progress "Format \`$format', urgh";
945 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
951 sub push_parse_changelog ($) {
954 my $clogp = Dpkg::Control::Hash->new();
955 $clogp->load($clogpfn);
957 $package = getfield $clogp, 'Source';
958 my $cversion = getfield $clogp, 'Version';
959 my $tag = debiantag($cversion);
960 runcmd @git, qw(check-ref-format), $tag;
962 my $dscfn = dscfn($cversion);
964 return ($clogp, $cversion, $tag, $dscfn);
967 sub push_parse_dsc ($$) {
968 my ($dscfn,$dscfnwhat, $cversion) = @_;
969 $dsc = parsecontrol($dscfn,$dscfnwhat);
970 my $dversion = getfield $dsc, 'Version';
971 my $dscpackage = getfield $dsc, 'Source';
972 ($dscpackage eq $package && $dversion eq $cversion) or
973 fail "$dsc is for $dscpackage $dversion".
974 " but debian/changelog is for $package $cversion";
977 sub push_mktag ($$$$$$$$) {
978 my ($head,$clogp,$tag,
980 $changesfile,$changesfilewhat,
983 $dsc->{$ourdscfield[0]} = $head;
984 $dsc->save("$dscfn.tmp") or die $!;
986 my $changes = parsecontrol($changesfile,$changesfilewhat);
987 foreach my $field (qw(Source Distribution Version)) {
988 $changes->{$field} eq $clogp->{$field} or
989 fail "changes field $field \`$changes->{$field}'".
990 " does not match changelog \`$clogp->{$field}'";
993 # We make the git tag by hand because (a) that makes it easier
994 # to control the "tagger" (b) we can do remote signing
995 my $authline = clogp_authline $clogp;
996 open TO, '>', $tfn->('.tmp') or die $!;
997 print TO <<END or die $!;
1003 $package release $cversion for $csuite [dgit]
1007 my $tagobjfn = $tfn->('.tmp');
1009 if (!defined $keyid) {
1010 $keyid = access_cfg('keyid','RETURN-UNDEF');
1012 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1013 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1014 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1015 push @sign_cmd, $tfn->('.tmp');
1016 runcmd_ordryrun @sign_cmd;
1018 $tagobjfn = $tfn->('.signed.tmp');
1019 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1020 $tfn->('.tmp'), $tfn->('.tmp.asc');
1028 print DEBUG "actually entering push\n";
1031 my $clogpfn = ".git/dgit/changelog.822.tmp";
1032 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1034 responder_send_file('parsed-changelog', $clogpfn);
1036 my ($clogp, $cversion, $tag, $dscfn) =
1037 push_parse_changelog("$clogpfn");
1040 fail "looked for .dsc $dscfn, but $!;".
1041 " maybe you forgot to build";
1043 responder_send_file('dsc', "../$dscfn");
1045 push_parse_dsc("../$dscfn", $dscfn, $cversion);
1047 my $format = getfield $dsc, 'Format';
1048 print DEBUG "format $format\n";
1049 if (madformat($format)) {
1050 commit_quilty_patch();
1053 chdir $ud or die $!;
1054 progress "checking that $dscfn corresponds to HEAD";
1055 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
1056 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1057 chdir '../../../..' or die $!;
1058 printcmd \*DEBUG,"+",@_;
1059 my @diffcmd = (@git, qw(diff --exit-code), $tree);
1061 if (system @diffcmd) {
1062 if ($! && $?==256) {
1063 fail "$dscfn specifies a different tree to your HEAD commit;".
1064 " perhaps you forgot to build";
1070 #do fast forward check and maybe fake merge
1071 # if (!is_fast_fwd(mainbranch
1072 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1073 # map { lref($_).":".rref($_) }
1075 my $head = rev_parse('HEAD');
1076 if (!$changesfile) {
1077 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
1078 if (stat "$multi") {
1079 $changesfile = $multi;
1081 $!==&ENOENT or die "$multi: $!";
1082 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1083 my @cs = glob "../$pat";
1084 fail "failed to find unique changes file".
1085 " (looked for $pat in .., or $multi);".
1086 " perhaps you need to use dgit -C"
1088 ($changesfile) = @cs;
1092 responder_send_file('changes',$changesfn);
1094 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1097 ? responder_receive_files('signed-tag', $tfn->('.signed.tmp'))
1098 : push_mktag($head,$clogp,$tag,
1100 $changesfile,$changesfile,
1103 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1104 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1105 runcmd_ordryrun @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1106 runcmd_ordryrun @git, qw(tag -v --), $tag;
1108 if (!check_for_git()) {
1109 create_remote_git_repo();
1111 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
1112 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1114 if (!$we_are_responder) {
1116 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
1118 progress "[new .dsc left in $dscfn.tmp]";
1123 if ($we_are_responder) {
1124 my $dryrunsuffix = $dryrun ? ".tmp" : "";
1125 responder_receive_files('signed-changes-dsc',
1126 "$changesfile$dryrunsuffix",
1127 "../$dscfn$dryrunsuffix");
1129 my @debsign_cmd = @debsign;
1130 push @debsign_cmd, "-k$keyid" if defined $keyid;
1131 push @debsign_cmd, $changesfile;
1132 runcmd_ordryrun @debsign_cmd;
1135 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
1136 my $host = access_cfg('upload-host','RETURN-UNDEF');
1137 my @hostarg = defined($host) ? ($host,) : ();
1138 runcmd_ordryrun @dput, @hostarg, $changesfile;
1139 printdone "pushed and uploaded $cversion";
1141 responder_send_command("complete");
1147 badusage "-p is not allowed with clone; specify as argument instead"
1148 if defined $package;
1151 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1152 ($package,$isuite) = @ARGV;
1153 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1154 ($package,$dstdir) = @ARGV;
1155 } elsif (@ARGV==3) {
1156 ($package,$isuite,$dstdir) = @ARGV;
1158 badusage "incorrect arguments to dgit clone";
1160 $dstdir ||= "$package";
1164 sub branchsuite () {
1165 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1166 if ($branch =~ m#$lbranch_re#o) {
1173 sub fetchpullargs () {
1174 if (!defined $package) {
1175 my $sourcep = parsecontrol('debian/control','debian/control');
1176 $package = getfield $sourcep, 'Source';
1179 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1181 my $clogp = parsechangelog();
1182 $isuite = getfield $clogp, 'Distribution';
1184 canonicalise_suite();
1185 progress "fetching from suite $csuite";
1186 } elsif (@ARGV==1) {
1188 canonicalise_suite();
1190 badusage "incorrect arguments to dgit fetch or dgit pull";
1208 badusage "-p is not allowed with dgit push" if defined $package;
1210 my $clogp = parsechangelog();
1211 $package = getfield $clogp, 'Source';
1214 } elsif (@ARGV==1) {
1215 ($specsuite) = (@ARGV);
1217 badusage "incorrect arguments to dgit push";
1219 $isuite = getfield $clogp, 'Distribution';
1221 local ($package) = $existing_package; # this is a hack
1222 canonicalise_suite();
1224 if (defined $specsuite && $specsuite ne $isuite) {
1225 canonicalise_suite();
1226 $csuite eq $specsuite or
1227 fail "dgit push: changelog specifies $isuite ($csuite)".
1228 " but command line specifies $specsuite";
1230 if (check_for_git()) {
1233 if (fetch_from_archive()) {
1234 is_fast_fwd(lrref(), 'HEAD') or
1235 fail "dgit push: HEAD is not a descendant".
1236 " of the archive's version.\n".
1237 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1240 fail "package appears to be new in this suite;".
1241 " if this is intentional, use --new";
1246 sub cmd_remote_push_responder {
1247 my ($nrargs) = shift @ARGV;
1248 my (@rargs) = @ARGV[0..$nrargs-1];
1249 @ARGV = @ARGV[$nrargs..$#ARGV];
1252 chdir $dir or die "$dir: $!";
1254 responder_send_command("dgit-remote-push-ready");
1262 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1264 sub build_maybe_quilt_fixup () {
1265 if (!open F, "debian/source/format") {
1266 die $! unless $!==&ENOENT;
1270 F->error and die $!;
1272 return unless madformat($_);
1274 my $clogp = parsechangelog();
1275 my $version = getfield $clogp, 'Version';
1276 my $author = getfield $clogp, 'Maintainer';
1277 my $headref = rev_parse('HEAD');
1280 my $patchname = "auto-$version-$headref-$time";
1281 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1283 my $descfn = ".git/dgit/quilt-description.tmp";
1284 open O, '>', $descfn or die "$descfn: $!";
1286 $msg =~ s/^\s+$/ ./mg;
1287 print O <<END or die $!;
1288 Description: Automatically generated patch ($clogp->{Version})
1289 Last (up to) $ncommits git changes, FYI:
1299 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1300 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1301 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1302 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1305 if (!open P, '>>', ".pc/applied-patches") {
1306 $!==&ENOENT or die $!;
1311 commit_quilty_patch();
1314 sub quilt_fixup_editor () {
1315 my $descfn = $ENV{$fakeeditorenv};
1316 my $editing = $ARGV[$#ARGV];
1317 open I1, '<', $descfn or die "$descfn: $!";
1318 open I2, '<', $editing or die "$editing: $!";
1319 unlink $editing or die "$editing: $!";
1320 open O, '>', $editing or die "$editing: $!";
1321 while (<I1>) { print O or die $!; } I1->error and die $!;
1324 $copying ||= m/^\-\-\- /;
1325 next unless $copying;
1328 I2->error and die $!;
1334 badusage "-p is not allowed when building" if defined $package;
1336 my $clogp = parsechangelog();
1337 $isuite = getfield $clogp, 'Distribution';
1338 $package = getfield $clogp, 'Source';
1339 $version = getfield $clogp, 'Version';
1340 build_maybe_quilt_fixup();
1344 badusage "dgit build implies --clean=dpkg-source"
1345 if $cleanmode ne 'dpkg-source';
1347 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1348 printdone "build successful\n";
1352 badusage "dgit git-build implies --clean=dpkg-source"
1353 if $cleanmode ne 'dpkg-source';
1356 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1357 "--git-builder=@dpkgbuildpackage");
1358 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1359 canonicalise_suite();
1360 push @cmd, "--git-debian-branch=".lbranch();
1362 push @cmd, changesopts();
1363 runcmd_ordryrun @cmd, @ARGV;
1364 printdone "build successful\n";
1369 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1370 $dscfn = dscfn($version);
1371 if ($cleanmode eq 'dpkg-source') {
1372 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1374 if ($cleanmode eq 'git') {
1375 runcmd_ordryrun @git, qw(clean -xdf);
1376 } elsif ($cleanmode eq 'none') {
1380 my $pwd = cmdoutput qw(env - pwd);
1381 my $leafdir = basename $pwd;
1382 chdir ".." or die $!;
1383 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1384 chdir $pwd or die $!;
1385 runcmd_ordryrun qw(sh -ec),
1386 'exec >$1; shift; exec "$@"','x',
1387 "../$sourcechanges",
1388 @dpkggenchanges, qw(-S), changesopts();
1392 sub cmd_build_source {
1393 badusage "build-source takes no additional arguments" if @ARGV;
1395 printdone "source built, results in $dscfn and $sourcechanges";
1400 chdir ".." or die $!;
1401 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1403 stat $dscfn or fail "$dscfn (in parent directory): $!";
1404 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1405 foreach my $cf (glob $pat) {
1406 next if $cf eq $sourcechanges;
1407 unlink $cf or fail "remove $cf: $!";
1410 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1411 runcmd_ordryrun @mergechanges, glob $pat;
1412 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1414 stat $multichanges or fail "$multichanges: $!";
1416 printdone "build successful, results in $multichanges\n" or die $!;
1419 sub cmd_quilt_fixup {
1420 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1421 my $clogp = parsechangelog();
1422 $version = getfield $clogp, 'Version';
1423 build_maybe_quilt_fixup();
1427 print "dgit version $our_version\n" or die $!;
1434 last unless $ARGV[0] =~ m/^-/;
1438 if (m/^--dry-run$/) {
1440 } elsif (m/^--no-sign$/) {
1442 } elsif (m/^--help$/) {
1444 } elsif (m/^--version$/) {
1446 } elsif (m/^--new$/) {
1448 } elsif (m/^--(\w+)=(.*)/s &&
1449 ($om = $opts_opt_map{$1}) &&
1452 } elsif (m/^--(\w+):(.*)/s &&
1453 ($om = $opts_opt_map{$1})) {
1455 } elsif (m/^--existing-package=(.*)/s) {
1456 $existing_package = $1;
1457 } elsif (m/^--distro=(.*)/s) {
1459 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1461 } elsif (m/^--clean=(.*)$/s) {
1462 badusage "unknown cleaning mode \`$1'";
1463 } elsif (m/^--ignore-dirty$/s) {
1465 } elsif (m/^--no-quilt-fixup$/s) {
1468 badusage "unknown long option \`$_'";
1474 } elsif (s/^-h/-/) {
1476 } elsif (s/^-D/-/) {
1477 open DEBUG, ">&STDERR" or die $!;
1479 } elsif (s/^-N/-/) {
1481 } elsif (m/^-[vm]/) {
1482 push @changesopts, $_;
1484 } elsif (s/^-c(.*=.*)//s) {
1485 push @git, '-c', $1;
1486 } elsif (s/^-d(.*)//s) {
1488 } elsif (s/^-C(.*)//s) {
1490 } elsif (s/^-k(.*)//s) {
1492 } elsif (s/^-wn//s) {
1493 $cleanmode = 'none';
1494 } elsif (s/^-wg//s) {
1496 } elsif (s/^-wd//s) {
1497 $cleanmode = 'dpkg-source';
1499 badusage "unknown short option \`$_'";
1506 if ($ENV{$fakeeditorenv}) {
1507 quilt_fixup_editor();
1510 delete $ENV{'DGET_UNPACK'};
1513 print STDERR "DRY RUN ONLY\n" if $dryrun;
1515 print STDERR $helpmsg or die $!;
1518 my $cmd = shift @ARGV;
1520 { no strict qw(refs); &{"cmd_$cmd"}(); }