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);
191 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
197 return join '', @out;
202 my $intro = shift @_;
203 print $fh $intro or die $!;
204 print $fh shellquote @_ or die $!;
205 print $fh "\n" or die $!;
209 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
211 fail "failed to fork/exec: $!";
212 } elsif (!($? & 0xff)) {
213 fail "subprocess failed with error exit status ".($?>>8);
215 fail "subprocess crashed (wait status $?)";
217 fail "subprocess produced invalid output";
222 printcmd(\*DEBUG,"+",@_) if $debug>0;
224 failedcmd @_ if system @_;
229 progress "dgit ok: @_";
231 progress "would be ok: @_ (but dry run only)";
235 sub cmdoutput_errok {
236 die Dumper(\@_)." ?" if grep { !defined } @_;
237 printcmd(\*DEBUG,"|",@_) if $debug>0;
238 open P, "-|", @_ or die $!;
241 { local $/ = undef; $d = <P>; }
243 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
246 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
251 my $d = cmdoutput_errok @_;
252 defined $d or failedcmd @_;
257 printcmd(\*STDERR,"#",@_);
260 sub runcmd_ordryrun {
269 my ($first_shell, @cmd) = @_;
270 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
273 our $helpmsg = <<END;
275 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
276 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
277 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
278 dgit [dgit-opts] push [dgit-opts] [suite]
279 important dgit options:
280 -k<keyid> sign tag and package with <keyid> instead of default
281 --dry-run -n do not change anything, but go through the motions
282 --new -N allow introducing a new package
283 --debug -D increase debug level
284 -c<name>=<value> set git config option (used directly by dgit too)
287 our $later_warning_msg = <<END;
288 Perhaps the upload is stuck in incoming. Using the version from git.
292 print STDERR "$us: @_\n", $helpmsg or die $!;
297 print $helpmsg or die $!;
301 our %defcfg = ('dgit.default.distro' => 'debian',
302 'dgit.default.username' => '',
303 'dgit.default.archive-query-default-component' => 'main',
304 'dgit.default.ssh' => 'ssh',
305 'dgit-distro.debian.git-host' => 'git.debian.org',
306 'dgit-distro.debian.git-proto' => 'git+ssh://',
307 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
308 'dgit-distro.debian.git-check' => 'ssh-cmd',
309 'dgit-distro.debian.git-create' => 'ssh-cmd',
310 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
311 'dgit-distro.debian.sshdakls-dir' =>
312 '/srv/ftp-master.debian.org/ftp/dists',
313 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
314 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
318 return undef if $c =~ /RETURN-UNDEF/;
319 my @cmd = (@git, qw(config --), $c);
322 local ($debug) = $debug-1;
323 $v = cmdoutput_errok @cmd;
330 my $dv = $defcfg{$c};
331 return $dv if defined $dv;
333 badcfg "need value for one of: @_";
336 sub access_distro () {
337 return cfg("dgit-suite.$isuite.distro",
338 "dgit.default.distro");
343 my $distro = $idistro || access_distro();
344 my $value = cfg(map { ("dgit-distro.$distro.$_",
345 "dgit.default.$_") } @keys);
349 sub access_someuserhost ($) {
351 my $user = access_cfg("$some-user",'username');
352 my $host = access_cfg("$some-host");
353 return length($user) ? "$user\@$host" : $host;
356 sub access_gituserhost () {
357 return access_someuserhost('git');
360 sub access_giturl () {
361 my $url = access_cfg('git-url','RETURN-UNDEF');
364 access_cfg('git-proto').
365 access_gituserhost().
366 access_cfg('git-path');
368 return "$url/$package.git";
371 sub parsecontrolfh ($$@) {
372 my ($fh, $desc, @opts) = @_;
373 my %opts = ('name' => $desc, @opts);
374 my $c = Dpkg::Control::Hash->new(%opts);
375 $c->parse($fh) or die "parsing of $desc failed";
380 my ($file, $desc) = @_;
381 my $fh = new IO::Handle;
382 open $fh, '<', $file or die "$file: $!";
383 my $c = parsecontrolfh($fh,$desc);
384 $fh->error and die $!;
390 my ($dctrl,$field) = @_;
391 my $v = $dctrl->{$field};
392 return $v if defined $v;
393 fail "missing field $field in ".$v->get_option('name');
397 my $c = Dpkg::Control::Hash->new();
398 my $p = new IO::Handle;
399 my @cmd = (qw(dpkg-parsechangelog), @_);
400 open $p, '-|', @cmd or die $!;
402 $?=0; $!=0; close $p or failedcmd @cmd;
408 sub archive_query ($) {
410 my $query = access_cfg('archive-query','RETURN-UNDEF');
411 if (!defined $query) {
412 my $distro = access_distro();
413 if ($distro eq 'debian') {
414 $query = "sshdakls:".
415 access_someuserhost('sshdakls').':'.
416 access_cfg('sshdakls-dir');
418 $query = "madison:$distro";
421 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
424 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
427 sub archive_query_madison ($$) {
428 my ($proto,$data) = @_;
429 die unless $proto eq 'madison';
430 $rmad{$package} ||= cmdoutput
431 qw(rmadison -asource),"-s$isuite","-u$data",$package;
432 my $rmad = $rmad{$package};
433 return madison_parse($rmad);
436 sub archive_query_sshdakls ($$) {
437 my ($proto,$data) = @_;
438 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
439 my $dakls = cmdoutput
440 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
441 return madison_parse($dakls);
444 sub canonicalise_suite_sshdakls ($$) {
445 my ($proto,$data) = @_;
446 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
448 (access_cfg('ssh'), $`,
450 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
451 " if test -d $isuite; then echo $isuite; exit 0; fi;".
453 my $dakls = cmdoutput @cmd;
454 failedcmd @cmd unless $dakls =~ m/^\w/;
458 sub madison_parse ($) {
461 foreach my $l (split /\n/, $rmad) {
462 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
463 \s*( [^ \t|]+ )\s* \|
464 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
465 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
466 $1 eq $package or die "$rmad $package ?";
473 $component = access_cfg('archive-query-default-component');
475 $5 eq 'source' or die "$rmad ?";
476 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
477 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
478 push @out, [$vsn,$subpath,$newsuite];
480 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
483 sub canonicalise_suite_madison ($$) {
484 my @r = archive_query_madison($_[0],$_[1]);
486 "unable to canonicalise suite using package $package".
487 " which does not appear to exist in suite $isuite;".
488 " --existing-package may help";
492 sub canonicalise_suite () {
493 return if defined $csuite;
494 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
495 $csuite = archive_query('canonicalise_suite');
496 if ($isuite ne $csuite) {
497 # madison canonicalises for us
498 progress "canonical suite name for $isuite is $csuite";
502 sub get_archive_dsc () {
503 canonicalise_suite();
504 my @vsns = archive_query('archive_query');
505 foreach my $vinfo (@vsns) {
506 my ($vsn,$subpath) = @$vinfo;
507 $dscurl = access_cfg('mirror').$subpath;
508 $dscdata = url_get($dscurl);
510 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
513 my $dscfh = new IO::File \$dscdata, '<' or die $!;
514 print DEBUG Dumper($dscdata) if $debug>1;
515 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
516 print DEBUG Dumper($dsc) if $debug>1;
517 my $fmt = getfield $dsc, 'Format';
518 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
524 sub check_for_git () {
526 my $how = access_cfg('git-check');
527 if ($how eq 'ssh-cmd') {
529 (access_cfg('ssh'),access_gituserhost(),
530 " set -e; cd ".access_cfg('git-path').";".
531 " if test -d $package.git; then echo 1; else echo 0; fi");
532 my $r= cmdoutput @cmd;
533 failedcmd @cmd unless $r =~ m/^[01]$/;
536 badcfg "unknown git-check \`$how'";
540 sub create_remote_git_repo () {
541 my $how = access_cfg('git-create');
542 if ($how eq 'ssh-cmd') {
544 (access_cfg('ssh'),access_gituserhost(),
545 "set -e; cd ".access_cfg('git-path').";".
546 " cp -a _template $package.git");
548 badcfg "unknown git-create \`$how'";
552 our ($dsc_hash,$lastpush_hash);
554 our $ud = '.git/dgit/unpack';
562 sub mktree_in_ud_from_only_subdir () {
563 # changes into the subdir
566 $dirs[0] =~ m#^([^/]+)/\.$# or die;
568 chdir $dir or die "$dir $!";
569 fail "source package contains .git directory" if stat '.git';
570 die $! unless $!==&ENOENT;
571 runcmd qw(git init -q);
572 rmtree('.git/objects');
573 symlink '../../../../objects','.git/objects' or die $!;
574 runcmd @git, qw(add -Af);
575 my $tree = cmdoutput @git, qw(write-tree);
576 $tree =~ m/^\w+$/ or die "$tree ?";
580 sub dsc_files_info () {
581 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
582 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
583 ['Files', 'Digest::MD5', 'new()']) {
584 my ($fname, $module, $method) = @$csumi;
585 my $field = $dsc->{$fname};
586 next unless defined $field;
587 eval "use $module; 1;" or die $@;
589 foreach (split /\n/, $field) {
591 m/^(\w+) (\d+) (\S+)$/ or
592 fail "could not parse .dsc $fname line \`$_'";
593 my $digester = eval "$module"."->$method;" or die $@;
598 Digester => $digester,
603 fail "missing any supported Checksums-* or Files field in ".
604 $dsc->get_option('name');
608 map { $_->{Filename} } dsc_files_info();
611 sub is_orig_file ($) {
613 m/\.orig(?:-\w+)?\.tar\.\w+$/;
616 sub make_commit ($) {
618 return cmdoutput @git, qw(hash-object -w -t commit), $file;
621 sub clogp_authline ($) {
623 my $author = getfield $clogp, 'Maintainer';
624 $author =~ s#,.*##ms;
625 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
626 my $authline = "$author $date";
627 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
628 fail "unexpected commit author line format \`$authline'".
629 " (was generated from changelog Maintainer field)";
633 sub generate_commit_from_dsc () {
637 foreach my $f (dsc_files()) {
638 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
640 link "../../../$f", $f
644 runcmd @dget, qw(--), $dscurl;
645 foreach my $f (grep { is_orig_file($_) } @files) {
646 link $f, "../../../../$f"
650 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
651 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
652 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
653 my $authline = clogp_authline $clogp;
654 my $changes = getfield $clogp, 'Changes';
655 open C, ">../commit.tmp" or die $!;
656 print C <<END or die $!;
663 # imported from the archive
666 my $outputhash = make_commit qw(../commit.tmp);
667 my $cversion = getfield $clogp, 'Version';
668 progress "synthesised git commit from .dsc $cversion";
669 if ($lastpush_hash) {
670 runcmd @git, qw(reset --hard), $lastpush_hash;
671 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
672 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
673 my $oversion = getfield $oldclogp, 'Version';
675 version_compare_string($oversion, $cversion);
677 # git upload/ is earlier vsn than archive, use archive
678 open C, ">../commit2.tmp" or die $!;
679 print C <<END or die $!;
681 parent $lastpush_hash
686 Record $package ($cversion) in archive suite $csuite
688 $outputhash = make_commit qw(../commit2.tmp);
689 } elsif ($vcmp > 0) {
690 print STDERR <<END or die $!;
692 Version actually in archive: $cversion (older)
693 Last allegedly pushed/uploaded: $oversion (newer or same)
696 $outputhash = $lastpush_hash;
698 $outputhash = $lastpush_hash;
701 chdir '../../../..' or die $!;
702 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
703 'DGIT_ARCHIVE', $outputhash;
704 cmdoutput @git, qw(log -n2), $outputhash;
705 # ... gives git a chance to complain if our commit is malformed
710 sub ensure_we_have_orig () {
711 foreach my $fi (dsc_files_info()) {
712 my $f = $fi->{Filename};
713 next unless is_orig_file($f);
714 if (open F, "<", "../$f") {
715 $fi->{Digester}->reset();
716 $fi->{Digester}->addfile(*F);
718 my $got = $fi->{Digester}->hexdigest();
719 $got eq $fi->{Hash} or
720 fail "existing file $f has hash $got but .dsc".
721 " demands hash $fi->{Hash}".
722 " (perhaps you should delete this file?)";
723 progress "using existing $f";
726 die "$f $!" unless $!==&ENOENT;
728 my $origurl = $dscurl;
729 $origurl =~ s{/[^/]+$}{};
731 die "$f ?" unless $f =~ m/^${package}_/;
732 die "$f ?" if $f =~ m#/#;
733 runcmd_ordryrun shell_cmd 'cd ..', @dget,'--',$origurl;
738 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
741 sub is_fast_fwd ($$) {
742 my ($ancestor,$child) = @_;
743 my @cmd = (@git, qw(merge-base), $ancestor, $child);
744 my $mb = cmdoutput_errok @cmd;
746 return rev_parse($mb) eq rev_parse($ancestor);
748 $?==256 or failedcmd @cmd;
753 sub git_fetch_us () {
754 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
757 sub fetch_from_archive () {
758 # ensures that lrref() is what is actually in the archive,
763 foreach my $field (@ourdscfield) {
764 $dsc_hash = $dsc->{$field};
765 last if defined $dsc_hash;
767 if (defined $dsc_hash) {
768 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
770 progress "last upload to archive specified git hash";
772 progress "last upload to archive has NO git hash";
775 progress "no version available from the archive";
778 my $lrref_fn = ".git/".lrref();
779 if (open H, $lrref_fn) {
780 $lastpush_hash = <H>;
781 chomp $lastpush_hash;
782 die "$lrref_fn $lastpush_hash ?" unless $lastpush_hash =~ m/^\w+$/;
783 } elsif ($! == &ENOENT) {
788 print DEBUG "previous reference hash=$lastpush_hash\n";
790 if (defined $dsc_hash) {
791 fail "missing git history even though dsc has hash -".
792 " could not find commit $dsc_hash".
793 " (should be in ".access_giturl()."#".rrref().")"
794 unless $lastpush_hash;
796 ensure_we_have_orig();
797 if ($dsc_hash eq $lastpush_hash) {
798 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
799 print STDERR <<END or die $!;
801 Git commit in archive is behind the last version allegedly pushed/uploaded.
802 Commit referred to by archive: $dsc_hash
803 Last allegedly pushed/uploaded: $lastpush_hash
806 $hash = $lastpush_hash;
808 fail "archive's .dsc refers to ".$dsc_hash.
809 " but this is an ancestor of ".$lastpush_hash;
812 $hash = generate_commit_from_dsc();
813 } elsif ($lastpush_hash) {
814 # only in git, not in the archive yet
815 $hash = $lastpush_hash;
816 print STDERR <<END or die $!;
818 Package not found in the archive, but has allegedly been pushed using dgit.
822 print DEBUG "nothing found!\n";
823 if (defined $skew_warning_vsn) {
824 print STDERR <<END or die $!;
826 Warning: relevant archive skew detected.
827 Archive allegedly contains $skew_warning_vsn
828 But we were not able to obtain any version from the archive or git.
834 print DEBUG "current hash=$hash\n";
835 if ($lastpush_hash) {
836 fail "not fast forward on last upload branch!".
837 " (archive's version left in DGIT_ARCHIVE)"
838 unless is_fast_fwd($lastpush_hash, $hash);
840 if (defined $skew_warning_vsn) {
842 print DEBUG "SKEW CHECK WANT $skew_warning_vsn\n";
843 my $clogf = ".git/dgit/changelog.tmp";
844 runcmd shell_cmd "exec >$clogf",
845 @git, qw(cat-file blob), "$hash:debian/changelog";
846 my $gotclogp = parsechangelog("-l$clogf");
847 my $got_vsn = getfield $gotclogp, 'Version';
848 print DEBUG "SKEW CHECK GOT $got_vsn\n";
849 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
850 print STDERR <<END or die $!;
852 Warning: archive skew detected. Using the available version:
853 Archive allegedly contains $skew_warning_vsn
854 We were able to obtain only $got_vsn
859 if ($lastpush_hash ne $hash) {
860 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
864 dryrun_report @upd_cmd;
872 canonicalise_suite();
873 badusage "dry run makes no sense with clone" if $dryrun;
874 mkdir $dstdir or die "$dstdir $!";
875 chdir "$dstdir" or die "$dstdir $!";
876 runcmd @git, qw(init -q);
877 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
878 open H, "> .git/HEAD" or die $!;
879 print H "ref: ".lref()."\n" or die $!;
881 runcmd @git, qw(remote add), 'origin', access_giturl();
882 if (check_for_git()) {
883 progress "fetching existing git history";
885 runcmd_ordryrun @git, qw(fetch origin);
887 progress "starting new git history";
889 fetch_from_archive() or no_such_package;
890 runcmd @git, qw(reset --hard), lrref();
891 printdone "ready for work in $dstdir";
895 if (check_for_git()) {
898 fetch_from_archive() or no_such_package();
899 printdone "fetched into ".lrref();
904 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
906 printdone "fetched to ".lrref()." and merged into HEAD";
909 sub check_not_dirty () {
910 return if $ignoredirty;
911 my @cmd = (@git, qw(diff --quiet HEAD));
912 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
913 $!=0; $?=0; system @cmd;
914 return if !$! && !$?;
915 if (!$! && $?==256) {
916 fail "working tree is dirty (does not match HEAD)";
922 sub commit_quilty_patch () {
923 my $output = cmdoutput @git, qw(status --porcelain);
926 foreach my $l (split /\n/, $output) {
927 next unless $l =~ m/\S/;
928 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
931 print STDERR "git status: $l\n";
935 fail "unexpected output from git status (is tree clean?)" if $bad;
937 progress "nothing quilty to commit, ok.";
940 runcmd_ordryrun @git, qw(add), sort keys %adds;
941 my $m = "Commit Debian 3.0 (quilt) metadata";
943 runcmd_ordryrun @git, qw(commit -m), $m;
948 return 0 unless $format eq '3.0 (quilt)';
949 progress "Format \`$format', urgh";
951 progress "Not doing any fixup of \`$format' due to --no-quilt-fixup";
957 sub push_parse_changelog ($) {
960 my $clogp = Dpkg::Control::Hash->new();
961 $clogp->load($clogpfn);
963 $package = getfield $clogp, 'Source';
964 my $cversion = getfield $clogp, 'Version';
965 my $tag = debiantag($cversion);
966 runcmd @git, qw(check-ref-format), $tag;
968 my $dscfn = dscfn($cversion);
970 return ($clogp, $cversion, $tag, $dscfn);
973 sub push_parse_dsc ($$) {
974 my ($dscfn,$dscfnwhat, $cversion) = @_;
975 $dsc = parsecontrol($dscfn,$dscfnwhat);
976 my $dversion = getfield $dsc, 'Version';
977 my $dscpackage = getfield $dsc, 'Source';
978 ($dscpackage eq $package && $dversion eq $cversion) or
979 fail "$dsc is for $dscpackage $dversion".
980 " but debian/changelog is for $package $cversion";
983 sub push_mktag ($$$$$$$$) {
984 my ($head,$clogp,$tag,
986 $changesfile,$changesfilewhat,
989 $dsc->{$ourdscfield[0]} = $head;
990 $dsc->save("$dscfn.tmp") or die $!;
992 my $changes = parsecontrol($changesfile,$changesfilewhat);
993 foreach my $field (qw(Source Distribution Version)) {
994 $changes->{$field} eq $clogp->{$field} or
995 fail "changes field $field \`$changes->{$field}'".
996 " does not match changelog \`$clogp->{$field}'";
999 # We make the git tag by hand because (a) that makes it easier
1000 # to control the "tagger" (b) we can do remote signing
1001 my $authline = clogp_authline $clogp;
1002 open TO, '>', $tfn->('.tmp') or die $!;
1003 print TO <<END or die $!;
1009 $package release $cversion for $csuite [dgit]
1013 my $tagobjfn = $tfn->('.tmp');
1015 if (!defined $keyid) {
1016 $keyid = access_cfg('keyid','RETURN-UNDEF');
1018 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
1019 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
1020 push @sign_cmd, qw(-u),$keyid if defined $keyid;
1021 push @sign_cmd, $tfn->('.tmp');
1022 runcmd_ordryrun @sign_cmd;
1024 $tagobjfn = $tfn->('.signed.tmp');
1025 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
1026 $tfn->('.tmp'), $tfn->('.tmp.asc');
1034 print DEBUG "actually entering push\n";
1037 my $clogpfn = ".git/dgit/changelog.822.tmp";
1038 runcmd shell_cmd "exec >$clogpfn", qw(dpkg-parsechangelog);
1040 responder_send_file('parsed-changelog', $clogpfn);
1042 my ($clogp, $cversion, $tag, $dscfn) =
1043 push_parse_changelog("$clogpfn");
1046 fail "looked for .dsc $dscfn, but $!;".
1047 " maybe you forgot to build";
1049 responder_send_file('dsc', "../$dscfn");
1051 push_parse_dsc("../$dscfn", $dscfn, $cversion);
1053 my $format = getfield $dsc, 'Format';
1054 print DEBUG "format $format\n";
1055 if (madformat($format)) {
1056 commit_quilty_patch();
1059 chdir $ud or die $!;
1060 progress "checking that $dscfn corresponds to HEAD";
1061 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
1062 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1063 chdir '../../../..' or die $!;
1064 printcmd \*DEBUG,"+",@_;
1065 my @diffcmd = (@git, qw(diff --exit-code), $tree);
1067 if (system @diffcmd) {
1068 if ($! && $?==256) {
1069 fail "$dscfn specifies a different tree to your HEAD commit;".
1070 " perhaps you forgot to build";
1076 #do fast forward check and maybe fake merge
1077 # if (!is_fast_fwd(mainbranch
1078 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1079 # map { lref($_).":".rref($_) }
1081 my $head = rev_parse('HEAD');
1082 if (!$changesfile) {
1083 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
1084 if (stat "$multi") {
1085 $changesfile = $multi;
1087 $!==&ENOENT or die "$multi: $!";
1088 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1089 my @cs = glob "../$pat";
1090 fail "failed to find unique changes file".
1091 " (looked for $pat in .., or $multi);".
1092 " perhaps you need to use dgit -C"
1094 ($changesfile) = @cs;
1098 responder_send_file('changes',$changesfn);
1100 my $tfn = sub { ".git/dgit/tag$_[0]"; };
1103 ? responder_receive_files('signed-tag', $tfn->('.signed.tmp'))
1104 : push_mktag($head,$clogp,$tag,
1106 $changesfile,$changesfile,
1109 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1110 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1111 runcmd_ordryrun @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1112 runcmd_ordryrun @git, qw(tag -v --), $tag;
1114 if (!check_for_git()) {
1115 create_remote_git_repo();
1117 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
1118 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1120 if (!$we_are_responder) {
1122 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
1124 progress "[new .dsc left in $dscfn.tmp]";
1129 if ($we_are_responder) {
1130 my $dryrunsuffix = $dryrun ? ".tmp" : "";
1131 responder_receive_files('signed-changes-dsc',
1132 "$changesfile$dryrunsuffix",
1133 "../$dscfn$dryrunsuffix");
1135 my @debsign_cmd = @debsign;
1136 push @debsign_cmd, "-k$keyid" if defined $keyid;
1137 push @debsign_cmd, $changesfile;
1138 runcmd_ordryrun @debsign_cmd;
1141 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
1142 my $host = access_cfg('upload-host','RETURN-UNDEF');
1143 my @hostarg = defined($host) ? ($host,) : ();
1144 runcmd_ordryrun @dput, @hostarg, $changesfile;
1145 printdone "pushed and uploaded $cversion";
1147 responder_send_command("complete");
1153 badusage "-p is not allowed with clone; specify as argument instead"
1154 if defined $package;
1157 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1158 ($package,$isuite) = @ARGV;
1159 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1160 ($package,$dstdir) = @ARGV;
1161 } elsif (@ARGV==3) {
1162 ($package,$isuite,$dstdir) = @ARGV;
1164 badusage "incorrect arguments to dgit clone";
1166 $dstdir ||= "$package";
1170 sub branchsuite () {
1171 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1172 if ($branch =~ m#$lbranch_re#o) {
1179 sub fetchpullargs () {
1180 if (!defined $package) {
1181 my $sourcep = parsecontrol('debian/control','debian/control');
1182 $package = getfield $sourcep, 'Source';
1185 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1187 my $clogp = parsechangelog();
1188 $isuite = getfield $clogp, 'Distribution';
1190 canonicalise_suite();
1191 progress "fetching from suite $csuite";
1192 } elsif (@ARGV==1) {
1194 canonicalise_suite();
1196 badusage "incorrect arguments to dgit fetch or dgit pull";
1214 badusage "-p is not allowed with dgit push" if defined $package;
1216 my $clogp = parsechangelog();
1217 $package = getfield $clogp, 'Source';
1220 } elsif (@ARGV==1) {
1221 ($specsuite) = (@ARGV);
1223 badusage "incorrect arguments to dgit push";
1225 $isuite = getfield $clogp, 'Distribution';
1227 local ($package) = $existing_package; # this is a hack
1228 canonicalise_suite();
1230 if (defined $specsuite && $specsuite ne $isuite) {
1231 canonicalise_suite();
1232 $csuite eq $specsuite or
1233 fail "dgit push: changelog specifies $isuite ($csuite)".
1234 " but command line specifies $specsuite";
1236 if (check_for_git()) {
1239 if (fetch_from_archive()) {
1240 is_fast_fwd(lrref(), 'HEAD') or
1241 fail "dgit push: HEAD is not a descendant".
1242 " of the archive's version.\n".
1243 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1246 fail "package appears to be new in this suite;".
1247 " if this is intentional, use --new";
1252 sub cmd_remote_push_responder {
1253 my ($nrargs) = shift @ARGV;
1254 my (@rargs) = @ARGV[0..$nrargs-1];
1255 @ARGV = @ARGV[$nrargs..$#ARGV];
1258 chdir $dir or die "$dir: $!";
1260 responder_send_command("dgit-remote-push-ready");
1268 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1270 sub build_maybe_quilt_fixup () {
1271 if (!open F, "debian/source/format") {
1272 die $! unless $!==&ENOENT;
1276 F->error and die $!;
1278 return unless madformat($_);
1280 my $clogp = parsechangelog();
1281 my $version = getfield $clogp, 'Version';
1282 my $author = getfield $clogp, 'Maintainer';
1283 my $headref = rev_parse('HEAD');
1286 my $patchname = "auto-$version-$headref-$time";
1287 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1289 my $descfn = ".git/dgit/quilt-description.tmp";
1290 open O, '>', $descfn or die "$descfn: $!";
1292 $msg =~ s/^\s+$/ ./mg;
1293 print O <<END or die $!;
1294 Description: Automatically generated patch ($clogp->{Version})
1295 Last (up to) $ncommits git changes, FYI:
1305 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1306 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1307 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1308 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1311 if (!open P, '>>', ".pc/applied-patches") {
1312 $!==&ENOENT or die $!;
1317 commit_quilty_patch();
1320 sub quilt_fixup_editor () {
1321 my $descfn = $ENV{$fakeeditorenv};
1322 my $editing = $ARGV[$#ARGV];
1323 open I1, '<', $descfn or die "$descfn: $!";
1324 open I2, '<', $editing or die "$editing: $!";
1325 unlink $editing or die "$editing: $!";
1326 open O, '>', $editing or die "$editing: $!";
1327 while (<I1>) { print O or die $!; } I1->error and die $!;
1330 $copying ||= m/^\-\-\- /;
1331 next unless $copying;
1334 I2->error and die $!;
1340 badusage "-p is not allowed when building" if defined $package;
1342 my $clogp = parsechangelog();
1343 $isuite = getfield $clogp, 'Distribution';
1344 $package = getfield $clogp, 'Source';
1345 $version = getfield $clogp, 'Version';
1346 build_maybe_quilt_fixup();
1350 badusage "dgit build implies --clean=dpkg-source"
1351 if $cleanmode ne 'dpkg-source';
1353 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1354 printdone "build successful\n";
1358 badusage "dgit git-build implies --clean=dpkg-source"
1359 if $cleanmode ne 'dpkg-source';
1362 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1363 "--git-builder=@dpkgbuildpackage");
1364 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1365 canonicalise_suite();
1366 push @cmd, "--git-debian-branch=".lbranch();
1368 push @cmd, changesopts();
1369 runcmd_ordryrun @cmd, @ARGV;
1370 printdone "build successful\n";
1375 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1376 $dscfn = dscfn($version);
1377 if ($cleanmode eq 'dpkg-source') {
1378 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1380 if ($cleanmode eq 'git') {
1381 runcmd_ordryrun @git, qw(clean -xdf);
1382 } elsif ($cleanmode eq 'none') {
1386 my $pwd = cmdoutput qw(env - pwd);
1387 my $leafdir = basename $pwd;
1388 chdir ".." or die $!;
1389 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1390 chdir $pwd or die $!;
1391 runcmd_ordryrun qw(sh -ec),
1392 'exec >$1; shift; exec "$@"','x',
1393 "../$sourcechanges",
1394 @dpkggenchanges, qw(-S), changesopts();
1398 sub cmd_build_source {
1399 badusage "build-source takes no additional arguments" if @ARGV;
1401 printdone "source built, results in $dscfn and $sourcechanges";
1406 chdir ".." or die $!;
1407 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1409 stat $dscfn or fail "$dscfn (in parent directory): $!";
1410 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1411 foreach my $cf (glob $pat) {
1412 next if $cf eq $sourcechanges;
1413 unlink $cf or fail "remove $cf: $!";
1416 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1417 runcmd_ordryrun @mergechanges, glob $pat;
1418 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1420 stat $multichanges or fail "$multichanges: $!";
1422 printdone "build successful, results in $multichanges\n" or die $!;
1425 sub cmd_quilt_fixup {
1426 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1427 my $clogp = parsechangelog();
1428 $version = getfield $clogp, 'Version';
1429 build_maybe_quilt_fixup();
1433 print "dgit version $our_version\n" or die $!;
1440 last unless $ARGV[0] =~ m/^-/;
1444 if (m/^--dry-run$/) {
1446 } elsif (m/^--no-sign$/) {
1448 } elsif (m/^--help$/) {
1450 } elsif (m/^--version$/) {
1452 } elsif (m/^--new$/) {
1454 } elsif (m/^--(\w+)=(.*)/s &&
1455 ($om = $opts_opt_map{$1}) &&
1458 } elsif (m/^--(\w+):(.*)/s &&
1459 ($om = $opts_opt_map{$1})) {
1461 } elsif (m/^--existing-package=(.*)/s) {
1462 $existing_package = $1;
1463 } elsif (m/^--distro=(.*)/s) {
1465 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1467 } elsif (m/^--clean=(.*)$/s) {
1468 badusage "unknown cleaning mode \`$1'";
1469 } elsif (m/^--ignore-dirty$/s) {
1471 } elsif (m/^--no-quilt-fixup$/s) {
1474 badusage "unknown long option \`$_'";
1480 } elsif (s/^-h/-/) {
1482 } elsif (s/^-D/-/) {
1483 open DEBUG, ">&STDERR" or die $!;
1485 } elsif (s/^-N/-/) {
1487 } elsif (m/^-[vm]/) {
1488 push @changesopts, $_;
1490 } elsif (s/^-c(.*=.*)//s) {
1491 push @git, '-c', $1;
1492 } elsif (s/^-d(.*)//s) {
1494 } elsif (s/^-C(.*)//s) {
1496 } elsif (s/^-k(.*)//s) {
1498 } elsif (s/^-wn//s) {
1499 $cleanmode = 'none';
1500 } elsif (s/^-wg//s) {
1502 } elsif (s/^-wd//s) {
1503 $cleanmode = 'dpkg-source';
1505 badusage "unknown short option \`$_'";
1512 if ($ENV{$fakeeditorenv}) {
1513 quilt_fixup_editor();
1516 delete $ENV{'DGET_UNPACK'};
1519 print STDERR "DRY RUN ONLY\n" if $dryrun;
1521 print STDERR $helpmsg or die $!;
1524 my $cmd = shift @ARGV;
1526 { no strict qw(refs); &{"cmd_$cmd"}(); }