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';
46 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
49 our (@dget) = qw(dget);
50 our (@dput) = qw(dput);
51 our (@debsign) = qw(debsign);
53 our (@sbuild) = qw(sbuild -A);
54 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
55 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
56 our (@dpkggenchanges) = qw(dpkg-genchanges);
57 our (@mergechanges) = qw(mergechanges -f);
58 our (@changesopts) = ('');
60 our %opts_opt_map = ('dget' => \@dget,
62 'debsign' => \@debsign,
65 'dpkg-source' => \@dpkgsource,
66 'dpkg-buildpackage' => \@dpkgbuildpackage,
67 'dpkg-genchanges' => \@dpkggenchanges,
68 'ch' => \@changesopts,
69 'mergechanges' => \@mergechanges);
74 open DEBUG, ">/dev/null" or die $!;
76 our $remotename = 'dgit';
77 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
78 our $branchprefix = 'dgit';
81 sub lbranch () { return "$branchprefix/$csuite"; }
82 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
83 sub lref () { return "refs/heads/".lbranch(); }
84 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
85 sub rrref () { return "refs/$branchprefix/$csuite"; }
100 return "${package}_".(stripepoch $vsn).".dsc";
103 sub changesopts () { return @changesopts[1..$#changesopts]; }
107 sub fail { die "$us: @_\n"; }
109 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
111 sub no_such_package () {
112 print STDERR "$us: package $package does not exist in suite $isuite\n";
118 return "+".rrref().":".lrref();
125 $ua = LWP::UserAgent->new();
129 print "downloading $what...\n";
130 my $r = $ua->get(@_) or die $!;
131 return undef if $r->code == 404;
132 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
133 return $r->decoded_content();
136 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
140 my $intro = shift @_;
141 print $fh $intro or die $!;
145 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
146 print $fh " '$_'" or die $!;
148 print $fh " $_" or die $!;
151 print $fh "\n" or die $!;
155 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
157 fail "failed to fork/exec: $!";
158 } elsif (!($? & 0xff)) {
159 fail "subprocess failed with error exit status ".($?>>8);
161 fail "subprocess crashed (wait status $?)";
163 fail "subprocess produced invalid output";
168 printcmd(\*DEBUG,"+",@_) if $debug>0;
170 failedcmd @_ if system @_;
175 print "dgit ok: @_\n";
177 print "would be ok: @_ (but dry run only)\n";
181 sub cmdoutput_errok {
182 die Dumper(\@_)." ?" if grep { !defined } @_;
183 printcmd(\*DEBUG,"|",@_) if $debug>0;
184 open P, "-|", @_ or die $!;
187 { local $/ = undef; $d = <P>; }
189 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
192 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
197 my $d = cmdoutput_errok @_;
198 defined $d or failedcmd @_;
203 printcmd(\*STDOUT,"#",@_);
206 sub runcmd_ordryrun {
215 my ($first_shell, @cmd) = @_;
216 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
219 our $helpmsg = <<END;
221 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
222 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
223 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
224 dgit [dgit-opts] push [dgit-opts] [suite]
225 important dgit options:
226 -k<keyid> sign tag and package with <keyid> instead of default
227 --dry-run -n do not change anything, but go through the motions
228 --new -N allow introducing a new package
229 --debug -D increase debug level
230 -c<name>=<value> set git config option (used directly by dgit too)
233 our $later_warning_msg = <<END;
234 Perhaps the upload is stuck in incoming. Using the version from git.
238 print STDERR "$us: @_\n", $helpmsg or die $!;
243 print $helpmsg or die $!;
247 our %defcfg = ('dgit.default.distro' => 'debian',
248 'dgit.default.username' => '',
249 'dgit.default.archive-query-default-component' => 'main',
250 'dgit.default.ssh' => 'ssh',
251 'dgit-distro.debian.git-host' => 'git.debian.org',
252 'dgit-distro.debian.git-proto' => 'git+ssh://',
253 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
254 'dgit-distro.debian.git-check' => 'ssh-cmd',
255 'dgit-distro.debian.git-create' => 'ssh-cmd',
256 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
257 'dgit-distro.debian.sshdakls-dir' =>
258 '/srv/ftp-master.debian.org/ftp/dists',
259 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
260 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
264 return undef if $c =~ /RETURN-UNDEF/;
265 my @cmd = (@git, qw(config --), $c);
268 local ($debug) = $debug-1;
269 $v = cmdoutput_errok @cmd;
276 my $dv = $defcfg{$c};
277 return $dv if defined $dv;
279 badcfg "need value for one of: @_";
282 sub access_distro () {
283 return cfg("dgit-suite.$isuite.distro",
284 "dgit.default.distro");
289 my $distro = $idistro || access_distro();
290 my $value = cfg(map { ("dgit-distro.$distro.$_",
291 "dgit.default.$_") } @keys);
295 sub access_someuserhost ($) {
297 my $user = access_cfg("$some-user",'username');
298 my $host = access_cfg("$some-host");
299 return length($user) ? "$user\@$host" : $host;
302 sub access_gituserhost () {
303 return access_someuserhost('git');
306 sub access_giturl () {
307 my $url = access_cfg('git-url','RETURN-UNDEF');
310 access_cfg('git-proto').
311 access_gituserhost().
312 access_cfg('git-path');
314 return "$url/$package.git";
317 sub parsecontrolfh ($$@) {
318 my ($fh, $desc, @opts) = @_;
319 my %opts = ('name' => $desc, @opts);
320 my $c = Dpkg::Control::Hash->new(%opts);
321 $c->parse($fh) or die "parsing of $desc failed";
326 my ($file, $desc) = @_;
327 my $fh = new IO::Handle;
328 open $fh, '<', $file or die "$file: $!";
329 my $c = parsecontrolfh($fh,$desc);
330 $fh->error and die $!;
336 my ($dctrl,$field) = @_;
337 my $v = $dctrl->{$field};
338 return $v if defined $v;
339 fail "missing field $field in ".$v->get_option('name');
343 my $c = Dpkg::Control::Hash->new();
344 my $p = new IO::Handle;
345 my @cmd = (qw(dpkg-parsechangelog), @_);
346 open $p, '-|', @cmd or die $!;
348 $?=0; $!=0; close $p or failedcmd @cmd;
354 sub archive_query ($) {
356 my $query = access_cfg('archive-query','RETURN-UNDEF');
357 if (!defined $query) {
358 my $distro = access_distro();
359 if ($distro eq 'debian') {
360 $query = "sshdakls:".
361 access_someuserhost('sshdakls').':'.
362 access_cfg('sshdakls-dir');
364 $query = "madison:$distro";
367 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
370 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
373 sub archive_query_madison ($$) {
374 my ($proto,$data) = @_;
375 die unless $proto eq 'madison';
376 $rmad{$package} ||= cmdoutput
377 qw(rmadison -asource),"-s$isuite","-u$data",$package;
378 my $rmad = $rmad{$package};
379 return madison_parse($rmad);
382 sub archive_query_sshdakls ($$) {
383 my ($proto,$data) = @_;
384 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
385 my $dakls = cmdoutput
386 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
387 return madison_parse($dakls);
390 sub canonicalise_suite_sshdakls ($$) {
391 my ($proto,$data) = @_;
392 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
394 (access_cfg('ssh'), $`,
396 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
397 " if test -d $isuite; then echo $isuite; exit 0; fi;".
399 my $dakls = cmdoutput @cmd;
400 failedcmd @cmd unless $dakls =~ m/^\w/;
404 sub madison_parse ($) {
407 foreach my $l (split /\n/, $rmad) {
408 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
409 \s*( [^ \t|]+ )\s* \|
410 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
411 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
412 $1 eq $package or die "$rmad $package ?";
419 $component = access_cfg('archive-query-default-component');
421 $5 eq 'source' or die "$rmad ?";
422 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
423 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
424 push @out, [$vsn,$subpath,$newsuite];
426 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
429 sub canonicalise_suite_madison ($$) {
430 my @r = archive_query_madison($_[0],$_[1]);
432 "unable to canonicalise suite using package $package".
433 " which does not appear to exist in suite $isuite;".
434 " --existing-package may help";
438 sub canonicalise_suite () {
439 return if defined $csuite;
440 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
441 $csuite = archive_query('canonicalise_suite');
442 if ($isuite ne $csuite) {
443 # madison canonicalises for us
444 print "canonical suite name for $isuite is $csuite\n";
448 sub get_archive_dsc () {
449 canonicalise_suite();
450 my @vsns = archive_query('archive_query');
451 foreach my $vinfo (@vsns) {
452 my ($vsn,$subpath) = @$vinfo;
453 $dscurl = access_cfg('mirror').$subpath;
454 $dscdata = url_get($dscurl);
456 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
459 my $dscfh = new IO::File \$dscdata, '<' or die $!;
460 print DEBUG Dumper($dscdata) if $debug>1;
461 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
462 print DEBUG Dumper($dsc) if $debug>1;
463 my $fmt = getfield $dsc, 'Format';
464 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
470 sub check_for_git () {
472 my $how = access_cfg('git-check');
473 if ($how eq 'ssh-cmd') {
475 (access_cfg('ssh'),access_gituserhost(),
476 " set -e; cd ".access_cfg('git-path').";".
477 " if test -d $package.git; then echo 1; else echo 0; fi");
478 my $r= cmdoutput @cmd;
479 failedcmd @cmd unless $r =~ m/^[01]$/;
482 badcfg "unknown git-check \`$how'";
486 sub create_remote_git_repo () {
487 my $how = access_cfg('git-create');
488 if ($how eq 'ssh-cmd') {
490 (access_cfg('ssh'),access_gituserhost(),
491 "set -e; cd ".access_cfg('git-path').";".
492 " cp -a _template $package.git");
494 badcfg "unknown git-create \`$how'";
498 our ($dsc_hash,$lastpush_hash);
500 our $ud = '.git/dgit/unpack';
508 sub mktree_in_ud_from_only_subdir () {
509 # changes into the subdir
512 $dirs[0] =~ m#^([^/]+)/\.$# or die;
514 chdir $dir or die "$dir $!";
515 fail "source package contains .git directory" if stat '.git';
516 die $! unless $!==&ENOENT;
517 runcmd qw(git init -q);
518 rmtree('.git/objects');
519 symlink '../../../../objects','.git/objects' or die $!;
520 runcmd @git, qw(add -Af);
521 my $tree = cmdoutput @git, qw(write-tree);
522 $tree =~ m/^\w+$/ or die "$tree ?";
526 sub dsc_files_info () {
527 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
528 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
529 ['Files', 'Digest::MD5', 'new()']) {
530 my ($fname, $module, $method) = @$csumi;
531 my $field = $dsc->{$fname};
532 next unless defined $field;
533 eval "use $module; 1;" or die $@;
535 foreach (split /\n/, $field) {
537 m/^(\w+) (\d+) (\S+)$/ or
538 fail "could not parse .dsc $fname line \`$_'";
539 my $digester = eval "$module"."->$method;" or die $@;
544 Digester => $digester,
549 fail "missing any supported Checksums-* or Files field in ".
550 $dsc->get_option('name');
554 map { $_->{Filename} } dsc_files_info();
557 sub is_orig_file ($) {
559 m/\.orig(?:-\w+)?\.tar\.\w+$/;
562 sub make_commit ($) {
564 return cmdoutput @git, qw(hash-object -w -t commit), $file;
567 sub clogp_authline ($) {
569 my $author = getfield $clogp, 'Maintainer';
570 $author =~ s#,.*##ms;
571 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
572 my $authline = "$author $date";
573 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
574 fail "unexpected commit author line format \`$authline'".
575 " (was generated from changelog Maintainer field)";
579 sub generate_commit_from_dsc () {
583 foreach my $f (dsc_files()) {
584 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
586 link "../../../$f", $f
590 runcmd @dget, qw(--), $dscurl;
591 foreach my $f (grep { is_orig_file($_) } @files) {
592 link $f, "../../../../$f"
596 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
597 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
598 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
599 my $authline = clogp_authline $clogp;
600 my $changes = getfield $clogp, 'Changes';
601 open C, ">../commit.tmp" or die $!;
602 print C <<END or die $!;
609 # imported from the archive
612 my $outputhash = make_commit qw(../commit.tmp);
613 my $cversion = getfield $clogp, 'Version';
614 print "synthesised git commit from .dsc $cversion\n";
615 if ($lastpush_hash) {
616 runcmd @git, qw(reset --hard), $lastpush_hash;
617 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
618 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
619 my $oversion = getfield $oldclogp, 'Version';
621 version_compare_string($oversion, $cversion);
623 # git upload/ is earlier vsn than archive, use archive
624 open C, ">../commit2.tmp" or die $!;
625 print C <<END or die $!;
627 parent $lastpush_hash
632 Record $package ($cversion) in archive suite $csuite
634 $outputhash = make_commit qw(../commit2.tmp);
635 } elsif ($vcmp > 0) {
636 print STDERR <<END or die $!;
638 Version actually in archive: $cversion (older)
639 Last allegedly pushed/uploaded: $oversion (newer or same)
642 $outputhash = $lastpush_hash;
644 $outputhash = $lastpush_hash;
647 chdir '../../../..' or die $!;
648 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
649 'DGIT_ARCHIVE', $outputhash;
650 cmdoutput @git, qw(log -n2), $outputhash;
651 # ... gives git a chance to complain if our commit is malformed
656 sub ensure_we_have_orig () {
657 foreach my $fi (dsc_files_info()) {
658 my $f = $fi->{Filename};
659 next unless is_orig_file($f);
660 if (open F, "<", "../$f") {
661 $fi->{Digester}->reset();
662 $fi->{Digester}->addfile(*F);
664 my $got = $fi->{Digester}->hexdigest();
665 $got eq $fi->{Hash} or
666 fail "existing file $f has hash $got but .dsc".
667 " demands hash $fi->{Hash}".
668 " (perhaps you should delete this file?)";
669 print "using existing $f\n";
672 die "$f $!" unless $!==&ENOENT;
674 my $origurl = $dscurl;
675 $origurl =~ s{/[^/]+$}{};
677 die "$f ?" unless $f =~ m/^${package}_/;
678 die "$f ?" if $f =~ m#/#;
679 runcmd_ordryrun shell_cmd 'cd ..', @dget,'--',$origurl;
684 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
687 sub is_fast_fwd ($$) {
688 my ($ancestor,$child) = @_;
689 my @cmd = (@git, qw(merge-base), $ancestor, $child);
690 my $mb = cmdoutput_errok @cmd;
692 return rev_parse($mb) eq rev_parse($ancestor);
694 $?==256 or failedcmd @cmd;
699 sub git_fetch_us () {
700 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
703 sub fetch_from_archive () {
704 # ensures that lrref() is what is actually in the archive,
709 foreach my $field (@ourdscfield) {
710 $dsc_hash = $dsc->{$field};
711 last if defined $dsc_hash;
713 if (defined $dsc_hash) {
714 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
716 print "last upload to archive specified git hash\n";
718 print "last upload to archive has NO git hash\n";
721 print "no version available from the archive\n";
724 my $lrref_fn = ".git/".lrref();
725 if (open H, $lrref_fn) {
726 $lastpush_hash = <H>;
727 chomp $lastpush_hash;
728 die "$lrref_fn $lastpush_hash ?" unless $lastpush_hash =~ m/^\w+$/;
729 } elsif ($! == &ENOENT) {
734 print DEBUG "previous reference hash=$lastpush_hash\n";
736 if (defined $dsc_hash) {
737 fail "missing git history even though dsc has hash -".
738 " could not find commit $dsc_hash".
739 " (should be in ".access_giturl()."#".rrref().")"
740 unless $lastpush_hash;
742 ensure_we_have_orig();
743 if ($dsc_hash eq $lastpush_hash) {
744 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
745 print STDERR <<END or die $!;
747 Git commit in archive is behind the last version allegedly pushed/uploaded.
748 Commit referred to by archive: $dsc_hash
749 Last allegedly pushed/uploaded: $lastpush_hash
752 $hash = $lastpush_hash;
754 fail "archive's .dsc refers to ".$dsc_hash.
755 " but this is an ancestor of ".$lastpush_hash;
758 $hash = generate_commit_from_dsc();
759 } elsif ($lastpush_hash) {
760 # only in git, not in the archive yet
761 $hash = $lastpush_hash;
762 print STDERR <<END or die $!;
764 Package not found in the archive, but has allegedly been pushed using dgit.
768 print DEBUG "nothing found!\n";
769 if (defined $skew_warning_vsn) {
770 print STDERR <<END or die $!;
772 Warning: relevant archive skew detected.
773 Archive allegedly contains $skew_warning_vsn
774 But we were not able to obtain any version from the archive or git.
780 print DEBUG "current hash=$hash\n";
781 if ($lastpush_hash) {
782 fail "not fast forward on last upload branch!".
783 " (archive's version left in DGIT_ARCHIVE)"
784 unless is_fast_fwd($lastpush_hash, $hash);
786 if (defined $skew_warning_vsn) {
788 print DEBUG "SKEW CHECK WANT $skew_warning_vsn\n";
789 my $clogf = ".git/dgit/changelog.tmp";
790 runcmd shell_cmd "exec >$clogf",
791 @git, qw(cat-file blob), "$hash:debian/changelog";
792 my $gotclogp = parsechangelog("-l$clogf");
793 my $got_vsn = getfield $gotclogp, 'Version';
794 print DEBUG "SKEW CHECK GOT $got_vsn\n";
795 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
796 print STDERR <<END or die $!;
798 Warning: archive skew detected. Using the available version:
799 Archive allegedly contains $skew_warning_vsn
800 We were able to obtain only $got_vsn
805 if ($lastpush_hash ne $hash) {
806 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
810 dryrun_report @upd_cmd;
818 canonicalise_suite();
819 badusage "dry run makes no sense with clone" if $dryrun;
820 mkdir $dstdir or die "$dstdir $!";
821 chdir "$dstdir" or die "$dstdir $!";
822 runcmd @git, qw(init -q);
823 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
824 open H, "> .git/HEAD" or die $!;
825 print H "ref: ".lref()."\n" or die $!;
827 runcmd @git, qw(remote add), 'origin', access_giturl();
828 if (check_for_git()) {
829 print "fetching existing git history\n";
831 runcmd_ordryrun @git, qw(fetch origin);
833 print "starting new git history\n";
835 fetch_from_archive() or no_such_package;
836 runcmd @git, qw(reset --hard), lrref();
837 printdone "ready for work in $dstdir";
841 if (check_for_git()) {
844 fetch_from_archive() or no_such_package();
845 printdone "fetched into ".lrref();
850 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
852 printdone "fetched to ".lrref()." and merged into HEAD";
855 sub check_not_dirty () {
856 return if $ignoredirty;
857 my @cmd = (@git, qw(diff --quiet HEAD));
858 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
859 $!=0; $?=0; system @cmd;
860 return if !$! && !$?;
861 if (!$! && $?==256) {
862 fail "working tree is dirty (does not match HEAD)";
868 sub commit_quilty_patch () {
869 my $output = cmdoutput @git, qw(status --porcelain);
872 foreach my $l (split /\n/, $output) {
873 next unless $l =~ m/\S/;
874 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
877 print STDERR "git status: $l\n";
881 fail "unexpected output from git status (is tree clean?)" if $bad;
883 print "nothing quilty to commit, ok.\n";
886 runcmd_ordryrun @git, qw(add), sort keys %adds;
887 my $m = "Commit Debian 3.0 (quilt) metadata";
889 runcmd_ordryrun @git, qw(commit -m), $m;
894 return 0 unless $format eq '3.0 (quilt)';
895 print "Format \`$format', urgh\n";
897 print "Not doing any fixup of \`$format' due to --no-quilt-fixup";
903 sub push_parse_changelog ($) {
906 my $clogp = Dpkg::Control::Hash->new();
907 $clogp->load($clogpfn);
909 responder_send_file('parsed-changelog', $clogpfn);
911 $package = getfield $clogp, 'Source';
912 my $cversion = getfield $clogp, 'Version';
913 my $tag = debiantag($cversion);
914 runcmd @git, qw(check-ref-format), $tag;
916 my $dscfn = dscfn($cversion);
918 return ($clogp, $cversion, $tag, $dscfn);
921 sub push_parse_dsc ($$) {
922 my ($dscfn,$dscfnwhat, $cversion) = @_;
923 $dsc = parsecontrol($dscfn,$dscfnwhat);
924 my $dversion = getfield $dsc, 'Version';
925 my $dscpackage = getfield $dsc, 'Source';
926 ($dscpackage eq $package && $dversion eq $cversion) or
927 fail "$dsc is for $dscpackage $dversion".
928 " but debian/changelog is for $package $cversion";
931 sub push_mktag ($$$$$$$$) {
932 my ($head,$clogp,$tag,
934 $changesfile,$changesfilewhat,
937 $dsc->{$ourdscfield[0]} = $head;
938 $dsc->save("$dscfn.tmp") or die $!;
940 my $changes = parsecontrol($changesfile,$changesfilewhat);
941 foreach my $field (qw(Source Distribution Version)) {
942 $changes->{$field} eq $clogp->{$field} or
943 fail "changes field $field \`$changes->{$field}'".
944 " does not match changelog \`$clogp->{$field}'";
947 # We make the git tag by hand because (a) that makes it easier
948 # to control the "tagger" (b) we can do remote signing
949 my $authline = clogp_authline $clogp;
950 open TO, '>', $tfn->('.tmp') or die $!;
951 print TO <<END or die $!;
957 $package release $cversion for $csuite [dgit]
961 my $tagobjfn = $tfn->('.tmp');
963 if (!defined $keyid) {
964 $keyid = access_cfg('keyid','RETURN-UNDEF');
966 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
967 my @sign_cmd = (@gpg, qw(--detach-sign --armor));
968 push @sign_cmd, qw(-u),$keyid if defined $keyid;
969 push @sign_cmd, $tfn->('.tmp');
970 runcmd_ordryrun @sign_cmd;
972 $tagobjfn = $tfn->('.signed.tmp');
973 runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
974 $tfn->('.tmp'), $tfn->('.tmp.asc');
982 print DEBUG "actually entering push\n";
985 runcmd shell_cmd "exec >.git/dgit/changelog.822.tmp",
986 qw(dpkg-parsechangelog);
988 my ($clogp, $cversion, $tag, $dscfn) =
989 push_parse_changelog(".git/dgit/changelog.822.tmp");
992 fail "looked for .dsc $dscfn, but $!;".
993 " maybe you forgot to build";
995 push_parse_dsc("../$dscfn", $dscfn, $cversion);
997 my $format = getfield $dsc, 'Format';
998 print DEBUG "format $format\n";
999 if (madformat($format)) {
1000 commit_quilty_patch();
1003 chdir $ud or die $!;
1004 print "checking that $dscfn corresponds to HEAD\n";
1005 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
1006 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
1007 chdir '../../../..' or die $!;
1008 printcmd \*DEBUG,"+",@_;
1009 my @diffcmd = (@git, qw(diff --exit-code), $tree);
1011 if (system @diffcmd) {
1012 if ($! && $?==256) {
1013 fail "$dscfn specifies a different tree to your HEAD commit;".
1014 " perhaps you forgot to build";
1020 #do fast forward check and maybe fake merge
1021 # if (!is_fast_fwd(mainbranch
1022 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
1023 # map { lref($_).":".rref($_) }
1025 my $head = rev_parse('HEAD');
1026 if (!$changesfile) {
1027 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
1028 if (stat "$multi") {
1029 $changesfile = $multi;
1031 $!==&ENOENT or die "$multi: $!";
1032 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
1033 my @cs = glob "../$pat";
1034 fail "failed to find unique changes file".
1035 " (looked for $pat in .., or $multi);".
1036 " perhaps you need to use dgit -C"
1038 ($changesfile) = @cs;
1043 push_mktag($head,$clogp,$tag,
1045 $changesfile,$changesfile,
1046 sub { ".git/dgit/tag$_[0]"; });
1048 my $tag_obj_hash = cmdoutput @git, qw(hash-object -w -t tag), $tagobjfn;
1049 runcmd_ordryrun @git, qw(verify-tag), $tag_obj_hash;
1050 runcmd_ordryrun @git, qw(update-ref), "refs/tags/$tag", $tag_obj_hash;
1051 runcmd_ordryrun @git, qw(tag -v --), $tag;
1053 if (!check_for_git()) {
1054 create_remote_git_repo();
1056 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
1057 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1059 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
1061 print "[new .dsc left in $dscfn.tmp]\n";
1065 my @debsign_cmd = @debsign;
1066 push @debsign_cmd, "-k$keyid" if defined $keyid;
1067 push @debsign_cmd, $changesfile;
1068 runcmd_ordryrun @debsign_cmd;
1070 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
1071 my $host = access_cfg('upload-host','RETURN-UNDEF');
1072 my @hostarg = defined($host) ? ($host,) : ();
1073 runcmd_ordryrun @dput, @hostarg, $changesfile;
1074 printdone "pushed and uploaded $cversion";
1080 badusage "-p is not allowed with clone; specify as argument instead"
1081 if defined $package;
1084 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1085 ($package,$isuite) = @ARGV;
1086 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1087 ($package,$dstdir) = @ARGV;
1088 } elsif (@ARGV==3) {
1089 ($package,$isuite,$dstdir) = @ARGV;
1091 badusage "incorrect arguments to dgit clone";
1093 $dstdir ||= "$package";
1097 sub branchsuite () {
1098 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1099 if ($branch =~ m#$lbranch_re#o) {
1106 sub fetchpullargs () {
1107 if (!defined $package) {
1108 my $sourcep = parsecontrol('debian/control','debian/control');
1109 $package = getfield $sourcep, 'Source';
1112 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1114 my $clogp = parsechangelog();
1115 $isuite = getfield $clogp, 'Distribution';
1117 canonicalise_suite();
1118 print "fetching from suite $csuite\n";
1119 } elsif (@ARGV==1) {
1121 canonicalise_suite();
1123 badusage "incorrect arguments to dgit fetch or dgit pull";
1141 badusage "-p is not allowed with dgit push" if defined $package;
1143 my $clogp = parsechangelog();
1144 $package = getfield $clogp, 'Source';
1147 } elsif (@ARGV==1) {
1148 ($specsuite) = (@ARGV);
1150 badusage "incorrect arguments to dgit push";
1152 $isuite = getfield $clogp, 'Distribution';
1154 local ($package) = $existing_package; # this is a hack
1155 canonicalise_suite();
1157 if (defined $specsuite && $specsuite ne $isuite) {
1158 canonicalise_suite();
1159 $csuite eq $specsuite or
1160 fail "dgit push: changelog specifies $isuite ($csuite)".
1161 " but command line specifies $specsuite";
1163 if (check_for_git()) {
1166 if (fetch_from_archive()) {
1167 is_fast_fwd(lrref(), 'HEAD') or
1168 fail "dgit push: HEAD is not a descendant".
1169 " of the archive's version.\n".
1170 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1173 fail "package appears to be new in this suite;".
1174 " if this is intentional, use --new";
1183 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1185 sub build_maybe_quilt_fixup () {
1186 if (!open F, "debian/source/format") {
1187 die $! unless $!==&ENOENT;
1191 F->error and die $!;
1193 return unless madformat($_);
1195 my $clogp = parsechangelog();
1196 my $version = getfield $clogp, 'Version';
1197 my $author = getfield $clogp, 'Maintainer';
1198 my $headref = rev_parse('HEAD');
1201 my $patchname = "auto-$version-$headref-$time";
1202 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1204 my $descfn = ".git/dgit/quilt-description.tmp";
1205 open O, '>', $descfn or die "$descfn: $!";
1207 $msg =~ s/^\s+$/ ./mg;
1208 print O <<END or die $!;
1209 Description: Automatically generated patch ($clogp->{Version})
1210 Last (up to) $ncommits git changes, FYI:
1220 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1221 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1222 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1223 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1226 if (!open P, '>>', ".pc/applied-patches") {
1227 $!==&ENOENT or die $!;
1232 commit_quilty_patch();
1235 sub quilt_fixup_editor () {
1236 my $descfn = $ENV{$fakeeditorenv};
1237 my $editing = $ARGV[$#ARGV];
1238 open I1, '<', $descfn or die "$descfn: $!";
1239 open I2, '<', $editing or die "$editing: $!";
1240 unlink $editing or die "$editing: $!";
1241 open O, '>', $editing or die "$editing: $!";
1242 while (<I1>) { print O or die $!; } I1->error and die $!;
1245 $copying ||= m/^\-\-\- /;
1246 next unless $copying;
1249 I2->error and die $!;
1255 badusage "-p is not allowed when building" if defined $package;
1257 my $clogp = parsechangelog();
1258 $isuite = getfield $clogp, 'Distribution';
1259 $package = getfield $clogp, 'Source';
1260 $version = getfield $clogp, 'Version';
1261 build_maybe_quilt_fixup();
1265 badusage "dgit build implies --clean=dpkg-source"
1266 if $cleanmode ne 'dpkg-source';
1268 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1269 printdone "build successful\n";
1273 badusage "dgit git-build implies --clean=dpkg-source"
1274 if $cleanmode ne 'dpkg-source';
1277 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1278 "--git-builder=@dpkgbuildpackage");
1279 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1280 canonicalise_suite();
1281 push @cmd, "--git-debian-branch=".lbranch();
1283 push @cmd, changesopts();
1284 runcmd_ordryrun @cmd, @ARGV;
1285 printdone "build successful\n";
1290 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1291 $dscfn = dscfn($version);
1292 if ($cleanmode eq 'dpkg-source') {
1293 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1295 if ($cleanmode eq 'git') {
1296 runcmd_ordryrun @git, qw(clean -xdf);
1297 } elsif ($cleanmode eq 'none') {
1301 my $pwd = cmdoutput qw(env - pwd);
1302 my $leafdir = basename $pwd;
1303 chdir ".." or die $!;
1304 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1305 chdir $pwd or die $!;
1306 runcmd_ordryrun qw(sh -ec),
1307 'exec >$1; shift; exec "$@"','x',
1308 "../$sourcechanges",
1309 @dpkggenchanges, qw(-S), changesopts();
1313 sub cmd_build_source {
1314 badusage "build-source takes no additional arguments" if @ARGV;
1316 printdone "source built, results in $dscfn and $sourcechanges";
1321 chdir ".." or die $!;
1322 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1324 stat $dscfn or fail "$dscfn (in parent directory): $!";
1325 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1326 foreach my $cf (glob $pat) {
1327 next if $cf eq $sourcechanges;
1328 unlink $cf or fail "remove $cf: $!";
1331 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1332 runcmd_ordryrun @mergechanges, glob $pat;
1333 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1335 stat $multichanges or fail "$multichanges: $!";
1337 printdone "build successful, results in $multichanges\n" or die $!;
1340 sub cmd_quilt_fixup {
1341 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1342 my $clogp = parsechangelog();
1343 $version = getfield $clogp, 'Version';
1344 build_maybe_quilt_fixup();
1348 print "dgit version $our_version\n" or die $!;
1355 last unless $ARGV[0] =~ m/^-/;
1359 if (m/^--dry-run$/) {
1361 } elsif (m/^--no-sign$/) {
1363 } elsif (m/^--help$/) {
1365 } elsif (m/^--version$/) {
1367 } elsif (m/^--new$/) {
1369 } elsif (m/^--(\w+)=(.*)/s &&
1370 ($om = $opts_opt_map{$1}) &&
1373 } elsif (m/^--(\w+):(.*)/s &&
1374 ($om = $opts_opt_map{$1})) {
1376 } elsif (m/^--existing-package=(.*)/s) {
1377 $existing_package = $1;
1378 } elsif (m/^--distro=(.*)/s) {
1380 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1382 } elsif (m/^--clean=(.*)$/s) {
1383 badusage "unknown cleaning mode \`$1'";
1384 } elsif (m/^--ignore-dirty$/s) {
1386 } elsif (m/^--no-quilt-fixup$/s) {
1389 badusage "unknown long option \`$_'";
1395 } elsif (s/^-h/-/) {
1397 } elsif (s/^-D/-/) {
1398 open DEBUG, ">&STDERR" or die $!;
1400 } elsif (s/^-N/-/) {
1402 } elsif (m/^-[vm]/) {
1403 push @changesopts, $_;
1405 } elsif (s/^-c(.*=.*)//s) {
1406 push @git, '-c', $1;
1407 } elsif (s/^-d(.*)//s) {
1409 } elsif (s/^-C(.*)//s) {
1411 } elsif (s/^-k(.*)//s) {
1413 } elsif (s/^-wn//s) {
1414 $cleanmode = 'none';
1415 } elsif (s/^-wg//s) {
1417 } elsif (s/^-wd//s) {
1418 $cleanmode = 'dpkg-source';
1420 badusage "unknown short option \`$_'";
1427 if ($ENV{$fakeeditorenv}) {
1428 quilt_fixup_editor();
1431 delete $ENV{'DGET_UNPACK'};
1434 print STDERR "DRY RUN ONLY\n" if $dryrun;
1436 print STDERR $helpmsg or die $!;
1439 my $cmd = shift @ARGV;
1441 { no strict qw(refs); &{"cmd_$cmd"}(); }