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 fixme should be in manual
54 fixme should pass this to debsign
55 our (@sbuild) = qw(sbuild -A);
56 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
57 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
58 our (@dpkggenchanges) = qw(dpkg-genchanges);
59 our (@mergechanges) = qw(mergechanges -f);
60 our (@changesopts) = ('');
62 our %opts_opt_map = ('dget' => \@dget,
64 'debsign' => \@debsign,
67 'dpkg-source' => \@dpkgsource,
68 'dpkg-buildpackage' => \@dpkgbuildpackage,
69 'dpkg-genchanges' => \@dpkggenchanges,
70 'ch' => \@changesopts,
71 'mergechanges' => \@mergechanges);
76 open DEBUG, ">/dev/null" or die $!;
78 our $remotename = 'dgit';
79 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
80 our $branchprefix = 'dgit';
83 sub lbranch () { return "$branchprefix/$csuite"; }
84 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
85 sub lref () { return "refs/heads/".lbranch(); }
86 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
87 sub rrref () { return "refs/$branchprefix/$csuite"; }
102 return "${package}_".(stripepoch $vsn).".dsc";
105 sub changesopts () { return @changesopts[1..$#changesopts]; }
109 sub fail { die "$us: @_\n"; }
111 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
113 sub no_such_package () {
114 print STDERR "$us: package $package does not exist in suite $isuite\n";
120 return "+".rrref().":".lrref();
127 $ua = LWP::UserAgent->new();
131 print "downloading $what...\n";
132 my $r = $ua->get(@_) or die $!;
133 return undef if $r->code == 404;
134 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
135 return $r->decoded_content();
138 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
142 my $intro = shift @_;
143 print $fh $intro or die $!;
147 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
148 print $fh " '$_'" or die $!;
150 print $fh " $_" or die $!;
153 print $fh "\n" or die $!;
157 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
159 fail "failed to fork/exec: $!";
160 } elsif (!($? & 0xff)) {
161 fail "subprocess failed with error exit status ".($?>>8);
163 fail "subprocess crashed (wait status $?)";
165 fail "subprocess produced invalid output";
170 printcmd(\*DEBUG,"+",@_) if $debug>0;
172 failedcmd @_ if system @_;
177 print "dgit ok: @_\n";
179 print "would be ok: @_ (but dry run only)\n";
183 sub cmdoutput_errok {
184 die Dumper(\@_)." ?" if grep { !defined } @_;
185 printcmd(\*DEBUG,"|",@_) if $debug>0;
186 open P, "-|", @_ or die $!;
189 { local $/ = undef; $d = <P>; }
191 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
194 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
199 my $d = cmdoutput_errok @_;
200 defined $d or failedcmd @_;
205 printcmd(\*STDOUT,"#",@_);
208 sub runcmd_ordryrun {
217 my ($first_shell, @cmd) = @_;
218 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
221 our $helpmsg = <<END;
223 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
224 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
225 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
226 dgit [dgit-opts] push [dgit-opts] [suite]
227 important dgit options:
228 -k<keyid> sign tag and package with <keyid> instead of default
229 --dry-run -n do not change anything, but go through the motions
230 --new -N allow introducing a new package
231 --debug -D increase debug level
232 -c<name>=<value> set git config option (used directly by dgit too)
235 our $later_warning_msg = <<END;
236 Perhaps the upload is stuck in incoming. Using the version from git.
240 print STDERR "$us: @_\n", $helpmsg or die $!;
245 print $helpmsg or die $!;
249 our %defcfg = ('dgit.default.distro' => 'debian',
250 'dgit.default.username' => '',
251 'dgit.default.archive-query-default-component' => 'main',
252 'dgit.default.ssh' => 'ssh',
253 'dgit-distro.debian.git-host' => 'git.debian.org',
254 'dgit-distro.debian.git-proto' => 'git+ssh://',
255 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
256 'dgit-distro.debian.git-check' => 'ssh-cmd',
257 'dgit-distro.debian.git-create' => 'ssh-cmd',
258 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
259 'dgit-distro.debian.sshdakls-dir' =>
260 '/srv/ftp-master.debian.org/ftp/dists',
261 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
262 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
266 return undef if $c =~ /RETURN-UNDEF/;
267 my @cmd = (@git, qw(config --), $c);
270 local ($debug) = $debug-1;
271 $v = cmdoutput_errok @cmd;
278 my $dv = $defcfg{$c};
279 return $dv if defined $dv;
281 badcfg "need value for one of: @_";
284 sub access_distro () {
285 return cfg("dgit-suite.$isuite.distro",
286 "dgit.default.distro");
291 my $distro = $idistro || access_distro();
292 my $value = cfg(map { ("dgit-distro.$distro.$_",
293 "dgit.default.$_") } @keys);
297 sub access_someuserhost ($) {
299 my $user = access_cfg("$some-user",'username');
300 my $host = access_cfg("$some-host");
301 return length($user) ? "$user\@$host" : $host;
304 sub access_gituserhost () {
305 return access_someuserhost('git');
308 sub access_giturl () {
309 my $url = access_cfg('git-url','RETURN-UNDEF');
312 access_cfg('git-proto').
313 access_gituserhost().
314 access_cfg('git-path');
316 return "$url/$package.git";
319 sub parsecontrolfh ($$@) {
320 my ($fh, $desc, @opts) = @_;
321 my %opts = ('name' => $desc, @opts);
322 my $c = Dpkg::Control::Hash->new(%opts);
323 $c->parse($fh) or die "parsing of $desc failed";
328 my ($file, $desc) = @_;
329 my $fh = new IO::Handle;
330 open $fh, '<', $file or die "$file: $!";
331 my $c = parsecontrolfh($fh,$desc);
332 $fh->error and die $!;
338 my ($dctrl,$field) = @_;
339 my $v = $dctrl->{$field};
340 return $v if defined $v;
341 fail "missing field $field in ".$v->get_option('name');
345 my $c = Dpkg::Control::Hash->new();
346 my $p = new IO::Handle;
347 my @cmd = (qw(dpkg-parsechangelog), @_);
348 open $p, '-|', @cmd or die $!;
350 $?=0; $!=0; close $p or failedcmd @cmd;
356 sub archive_query ($) {
358 my $query = access_cfg('archive-query','RETURN-UNDEF');
359 if (!defined $query) {
360 my $distro = access_distro();
361 if ($distro eq 'debian') {
362 $query = "sshdakls:".
363 access_someuserhost('sshdakls').':'.
364 access_cfg('sshdakls-dir');
366 $query = "madison:$distro";
369 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
372 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
375 sub archive_query_madison ($$) {
376 my ($proto,$data) = @_;
377 die unless $proto eq 'madison';
378 $rmad{$package} ||= cmdoutput
379 qw(rmadison -asource),"-s$isuite","-u$data",$package;
380 my $rmad = $rmad{$package};
381 return madison_parse($rmad);
384 sub archive_query_sshdakls ($$) {
385 my ($proto,$data) = @_;
386 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
387 my $dakls = cmdoutput
388 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
389 return madison_parse($dakls);
392 sub canonicalise_suite_sshdakls ($$) {
393 my ($proto,$data) = @_;
394 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
396 (access_cfg('ssh'), $`,
398 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
399 " if test -d $isuite; then echo $isuite; exit 0; fi;".
401 my $dakls = cmdoutput @cmd;
402 failedcmd @cmd unless $dakls =~ m/^\w/;
406 sub madison_parse ($) {
409 foreach my $l (split /\n/, $rmad) {
410 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
411 \s*( [^ \t|]+ )\s* \|
412 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
413 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
414 $1 eq $package or die "$rmad $package ?";
421 $component = access_cfg('archive-query-default-component');
423 $5 eq 'source' or die "$rmad ?";
424 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
425 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
426 push @out, [$vsn,$subpath,$newsuite];
428 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
431 sub canonicalise_suite_madison ($$) {
432 my @r = archive_query_madison($_[0],$_[1]);
434 "unable to canonicalise suite using package $package".
435 " which does not appear to exist in suite $isuite;".
436 " --existing-package may help";
440 sub canonicalise_suite () {
441 return if defined $csuite;
442 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
443 $csuite = archive_query('canonicalise_suite');
444 if ($isuite ne $csuite) {
445 # madison canonicalises for us
446 print "canonical suite name for $isuite is $csuite\n";
450 sub get_archive_dsc () {
451 canonicalise_suite();
452 my @vsns = archive_query('archive_query');
453 foreach my $vinfo (@vsns) {
454 my ($vsn,$subpath) = @$vinfo;
455 $dscurl = access_cfg('mirror').$subpath;
456 $dscdata = url_get($dscurl);
458 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
461 my $dscfh = new IO::File \$dscdata, '<' or die $!;
462 print DEBUG Dumper($dscdata) if $debug>1;
463 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
464 print DEBUG Dumper($dsc) if $debug>1;
465 my $fmt = getfield $dsc, 'Format';
466 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
472 sub check_for_git () {
474 my $how = access_cfg('git-check');
475 if ($how eq 'ssh-cmd') {
477 (access_cfg('ssh'),access_gituserhost(),
478 " set -e; cd ".access_cfg('git-path').";".
479 " if test -d $package.git; then echo 1; else echo 0; fi");
480 my $r= cmdoutput @cmd;
481 failedcmd @cmd unless $r =~ m/^[01]$/;
484 badcfg "unknown git-check \`$how'";
488 sub create_remote_git_repo () {
489 my $how = access_cfg('git-create');
490 if ($how eq 'ssh-cmd') {
492 (access_cfg('ssh'),access_gituserhost(),
493 "set -e; cd ".access_cfg('git-path').";".
494 " cp -a _template $package.git");
496 badcfg "unknown git-create \`$how'";
500 our ($dsc_hash,$lastpush_hash);
502 our $ud = '.git/dgit/unpack';
510 sub mktree_in_ud_from_only_subdir () {
511 # changes into the subdir
514 $dirs[0] =~ m#^([^/]+)/\.$# or die;
516 chdir $dir or die "$dir $!";
517 fail "source package contains .git directory" if stat '.git';
518 die $! unless $!==&ENOENT;
519 runcmd qw(git init -q);
520 rmtree('.git/objects');
521 symlink '../../../../objects','.git/objects' or die $!;
522 runcmd @git, qw(add -Af);
523 my $tree = cmdoutput @git, qw(write-tree);
524 $tree =~ m/^\w+$/ or die "$tree ?";
528 sub dsc_files_info () {
529 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
530 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
531 ['Files', 'Digest::MD5', 'new()']) {
532 my ($fname, $module, $method) = @$csumi;
533 my $field = $dsc->{$fname};
534 next unless defined $field;
535 eval "use $module; 1;" or die $@;
537 foreach (split /\n/, $field) {
539 m/^(\w+) (\d+) (\S+)$/ or
540 fail "could not parse .dsc $fname line \`$_'";
541 my $digester = eval "$module"."->$method;" or die $@;
546 Digester => $digester,
551 fail "missing any supported Checksums-* or Files field in ".
552 $dsc->get_option('name');
556 map { $_->{Filename} } dsc_files_info();
559 sub is_orig_file ($) {
561 m/\.orig(?:-\w+)?\.tar\.\w+$/;
564 sub make_commit ($) {
566 return cmdoutput @git, qw(hash-object -w -t commit), $file;
569 sub clogp_authline ($) {
571 my $author = getfield $clogp, 'Maintainer';
572 $author =~ s#,.*##ms;
573 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
574 my $authline = "$author $date";
575 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
576 fail "unexpected commit author line format \`$authline'".
577 " (was generated from changelog Maintainer field)";
581 sub generate_commit_from_dsc () {
585 foreach my $f (dsc_files()) {
586 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
588 link "../../../$f", $f
592 runcmd @dget, qw(--), $dscurl;
593 foreach my $f (grep { is_orig_file($_) } @files) {
594 link $f, "../../../../$f"
598 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
599 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
600 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
601 my $authline = clogp_authline $clogp;
602 my $changes = getfield $clogp, 'Changes';
603 open C, ">../commit.tmp" or die $!;
604 print C <<END or die $!;
611 # imported from the archive
614 my $outputhash = make_commit qw(../commit.tmp);
615 my $cversion = getfield $clogp, 'Version';
616 print "synthesised git commit from .dsc $cversion\n";
617 if ($lastpush_hash) {
618 runcmd @git, qw(reset --hard), $lastpush_hash;
619 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
620 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
621 my $oversion = getfield $oldclogp, 'Version';
623 version_compare_string($oversion, $cversion);
625 # git upload/ is earlier vsn than archive, use archive
626 open C, ">../commit2.tmp" or die $!;
627 print C <<END or die $!;
629 parent $lastpush_hash
634 Record $package ($cversion) in archive suite $csuite
636 $outputhash = make_commit qw(../commit2.tmp);
637 } elsif ($vcmp > 0) {
638 print STDERR <<END or die $!;
640 Version actually in archive: $cversion (older)
641 Last allegedly pushed/uploaded: $oversion (newer or same)
644 $outputhash = $lastpush_hash;
646 $outputhash = $lastpush_hash;
649 chdir '../../../..' or die $!;
650 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
651 'DGIT_ARCHIVE', $outputhash;
652 cmdoutput @git, qw(log -n2), $outputhash;
653 # ... gives git a chance to complain if our commit is malformed
658 sub ensure_we_have_orig () {
659 foreach my $fi (dsc_files_info()) {
660 my $f = $fi->{Filename};
661 next unless is_orig_file($f);
662 if (open F, "<", "../$f") {
663 $fi->{Digester}->reset();
664 $fi->{Digester}->addfile(*F);
666 my $got = $fi->{Digester}->hexdigest();
667 $got eq $fi->{Hash} or
668 fail "existing file $f has hash $got but .dsc".
669 " demands hash $fi->{Hash}".
670 " (perhaps you should delete this file?)";
671 print "using existing $f\n";
674 die "$f $!" unless $!==&ENOENT;
676 my $origurl = $dscurl;
677 $origurl =~ s{/[^/]+$}{};
679 die "$f ?" unless $f =~ m/^${package}_/;
680 die "$f ?" if $f =~ m#/#;
681 runcmd_ordryrun shell_cmd 'cd ..', @dget,'--',$origurl;
686 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
689 sub is_fast_fwd ($$) {
690 my ($ancestor,$child) = @_;
691 my @cmd = (@git, qw(merge-base), $ancestor, $child);
692 my $mb = cmdoutput_errok @cmd;
694 return rev_parse($mb) eq rev_parse($ancestor);
696 $?==256 or failedcmd @cmd;
701 sub git_fetch_us () {
702 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
705 sub fetch_from_archive () {
706 # ensures that lrref() is what is actually in the archive,
711 foreach my $field (@ourdscfield) {
712 $dsc_hash = $dsc->{$field};
713 last if defined $dsc_hash;
715 if (defined $dsc_hash) {
716 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
718 print "last upload to archive specified git hash\n";
720 print "last upload to archive has NO git hash\n";
723 print "no version available from the archive\n";
726 my $lrref_fn = ".git/".lrref();
727 if (open H, $lrref_fn) {
728 $lastpush_hash = <H>;
729 chomp $lastpush_hash;
730 die "$lrref_fn $lastpush_hash ?" unless $lastpush_hash =~ m/^\w+$/;
731 } elsif ($! == &ENOENT) {
736 print DEBUG "previous reference hash=$lastpush_hash\n";
738 if (defined $dsc_hash) {
739 fail "missing git history even though dsc has hash -".
740 " could not find commit $dsc_hash".
741 " (should be in ".access_giturl()."#".rrref().")"
742 unless $lastpush_hash;
744 ensure_we_have_orig();
745 if ($dsc_hash eq $lastpush_hash) {
746 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
747 print STDERR <<END or die $!;
749 Git commit in archive is behind the last version allegedly pushed/uploaded.
750 Commit referred to by archive: $dsc_hash
751 Last allegedly pushed/uploaded: $lastpush_hash
754 $hash = $lastpush_hash;
756 fail "archive's .dsc refers to ".$dsc_hash.
757 " but this is an ancestor of ".$lastpush_hash;
760 $hash = generate_commit_from_dsc();
761 } elsif ($lastpush_hash) {
762 # only in git, not in the archive yet
763 $hash = $lastpush_hash;
764 print STDERR <<END or die $!;
766 Package not found in the archive, but has allegedly been pushed using dgit.
770 print DEBUG "nothing found!\n";
771 if (defined $skew_warning_vsn) {
772 print STDERR <<END or die $!;
774 Warning: relevant archive skew detected.
775 Archive allegedly contains $skew_warning_vsn
776 But we were not able to obtain any version from the archive or git.
782 print DEBUG "current hash=$hash\n";
783 if ($lastpush_hash) {
784 fail "not fast forward on last upload branch!".
785 " (archive's version left in DGIT_ARCHIVE)"
786 unless is_fast_fwd($lastpush_hash, $hash);
788 if (defined $skew_warning_vsn) {
790 print DEBUG "SKEW CHECK WANT $skew_warning_vsn\n";
791 my $clogf = ".git/dgit/changelog.tmp";
792 runcmd shell_cmd "exec >$clogf",
793 @git, qw(cat-file blob), "$hash:debian/changelog";
794 my $gotclogp = parsechangelog("-l$clogf");
795 my $got_vsn = getfield $gotclogp, 'Version';
796 print DEBUG "SKEW CHECK GOT $got_vsn\n";
797 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
798 print STDERR <<END or die $!;
800 Warning: archive skew detected. Using the available version:
801 Archive allegedly contains $skew_warning_vsn
802 We were able to obtain only $got_vsn
807 if ($lastpush_hash ne $hash) {
808 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
812 dryrun_report @upd_cmd;
820 canonicalise_suite();
821 badusage "dry run makes no sense with clone" if $dryrun;
822 mkdir $dstdir or die "$dstdir $!";
823 chdir "$dstdir" or die "$dstdir $!";
824 runcmd @git, qw(init -q);
825 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
826 open H, "> .git/HEAD" or die $!;
827 print H "ref: ".lref()."\n" or die $!;
829 runcmd @git, qw(remote add), 'origin', access_giturl();
830 if (check_for_git()) {
831 print "fetching existing git history\n";
833 runcmd_ordryrun @git, qw(fetch origin);
835 print "starting new git history\n";
837 fetch_from_archive() or no_such_package;
838 runcmd @git, qw(reset --hard), lrref();
839 printdone "ready for work in $dstdir";
843 if (check_for_git()) {
846 fetch_from_archive() or no_such_package();
847 printdone "fetched into ".lrref();
852 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
854 printdone "fetched to ".lrref()." and merged into HEAD";
857 sub check_not_dirty () {
858 return if $ignoredirty;
859 my @cmd = (@git, qw(diff --quiet HEAD));
860 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
861 $!=0; $?=0; system @cmd;
862 return if !$! && !$?;
863 if (!$! && $?==256) {
864 fail "working tree is dirty (does not match HEAD)";
870 sub commit_quilty_patch () {
871 my $output = cmdoutput @git, qw(status --porcelain);
874 foreach my $l (split /\n/, $output) {
875 next unless $l =~ m/\S/;
876 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
879 print STDERR "git status: $l\n";
883 fail "unexpected output from git status (is tree clean?)" if $bad;
885 print "nothing quilty to commit, ok.\n";
888 runcmd_ordryrun @git, qw(add), sort keys %adds;
889 my $m = "Commit Debian 3.0 (quilt) metadata";
891 runcmd_ordryrun @git, qw(commit -m), $m;
896 return 0 unless $format eq '3.0 (quilt)';
897 print "Format \`$format', urgh\n";
899 print "Not doing any fixup of \`$format' due to --no-quilt-fixup";
906 print DEBUG "actually entering push\n";
907 my $clogp = parsechangelog();
908 $package = getfield $clogp, 'Source';
909 my $cversion = getfield $clogp, 'Version';
910 my $dscfn = dscfn($cversion);
912 fail "looked for .dsc $dscfn, but $!;".
913 " maybe you forgot to build";
914 $dsc = parsecontrol("../$dscfn","$dscfn");
915 my $dscpackage = getfield $dsc, 'Source';
916 my $format = getfield $dsc, 'Format';
917 my $dversion = getfield $dsc, 'Version';
918 ($dscpackage eq $package && $dversion eq $cversion) or
919 fail "$dsc is for $dscpackage $dversion".
920 " but debian/changelog is for $package $cversion";
921 print DEBUG "format $format\n";
922 if (madformat($format)) {
923 commit_quilty_patch();
928 print "checking that $dscfn corresponds to HEAD\n";
929 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
930 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
931 chdir '../../../..' or die $!;
932 printcmd \*DEBUG,"+",@_;
933 my @diffcmd = (@git, qw(diff --exit-code), $tree);
935 if (system @diffcmd) {
937 fail "$dscfn specifies a different tree to your HEAD commit;".
938 " perhaps you forgot to build";
944 #do fast forward check and maybe fake merge
945 # if (!is_fast_fwd(mainbranch
946 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
947 # map { lref($_).":".rref($_) }
949 my $head = rev_parse('HEAD');
950 $dsc->{$ourdscfield[0]} = $head;
951 $dsc->save("../$dscfn.tmp") or die $!;
953 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
955 $changesfile = $multi;
957 $!==&ENOENT or die "$multi: $!";
958 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
959 my @cs = glob "../$pat";
960 fail "failed to find unique changes file".
961 " (looked for $pat in .., or $multi);".
962 " perhaps you need to use dgit -C"
964 ($changesfile) = @cs;
967 my $changes = parsecontrol($changesfile,$changesfile);
968 foreach my $field (qw(Source Distribution Version)) {
969 $changes->{$field} eq $clogp->{$field} or
970 fail "changes field $field \`$changes->{$field}'".
971 " does not match changelog \`$clogp->{$field}'";
973 my $tag = debiantag($dversion);
974 runcmd @git, qw(check-ref-format), $tag;
976 # We make the git tag by hand because (a) that makes it easier
977 # to control the "tagger" (b) we can do remote signing
978 my $authline = clogp_authline $clogp;
979 my $tfn = sub { ".git/dgit/tag$_[0]"; };
980 open TO, '>', $tfn->('.tmp') or die $!;
981 print TO <<END or die $!;
987 $package release $dversion for $csuite [dgit]
991 my $tagobjfn = $tfn->('.tmp');
993 if (!defined $keyid) {
994 $keyid = access_cfg('keyid','RETURN-UNDEF');
996 unlink $tfn->('.tmp.asc') or $!==&ENOENT or die $!;
997 my @sign_cmd = (@gpg, qw(--clearsign --armor));
998 push @sign_cmd, qw(-u),$keyid if defined $keyid;
999 push @sign_cmd, $tfn->('.tmp');
1000 runcmd_ordryrun @sign_cmd;
1004 if (!check_for_git()) {
1005 create_remote_git_repo();
1007 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
1008 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
1010 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
1012 print "[new .dsc left in $dscfn.tmp]\n";
1017 my @tag_cmd = (@git, qw(tag -a -m),
1019 push @tag_cmd, $tag;
1020 runcmd_ordryrun @tag_cmd;
1024 push @tag_cmd, qw(-u),$keyid if defined $keyid;
1026 my @debsign_cmd = @debsign;
1027 push @debsign_cmd, "-k$keyid" if defined $keyid;
1028 push @debsign_cmd, $changesfile;
1029 runcmd_ordryrun @debsign_cmd;
1031 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
1032 my $host = access_cfg('upload-host','RETURN-UNDEF');
1033 my @hostarg = defined($host) ? ($host,) : ();
1034 runcmd_ordryrun @dput, @hostarg, $changesfile;
1035 printdone "pushed and uploaded $dversion";
1041 badusage "-p is not allowed with clone; specify as argument instead"
1042 if defined $package;
1045 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1046 ($package,$isuite) = @ARGV;
1047 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1048 ($package,$dstdir) = @ARGV;
1049 } elsif (@ARGV==3) {
1050 ($package,$isuite,$dstdir) = @ARGV;
1052 badusage "incorrect arguments to dgit clone";
1054 $dstdir ||= "$package";
1058 sub branchsuite () {
1059 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1060 if ($branch =~ m#$lbranch_re#o) {
1067 sub fetchpullargs () {
1068 if (!defined $package) {
1069 my $sourcep = parsecontrol('debian/control','debian/control');
1070 $package = getfield $sourcep, 'Source';
1073 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1075 my $clogp = parsechangelog();
1076 $isuite = getfield $clogp, 'Distribution';
1078 canonicalise_suite();
1079 print "fetching from suite $csuite\n";
1080 } elsif (@ARGV==1) {
1082 canonicalise_suite();
1084 badusage "incorrect arguments to dgit fetch or dgit pull";
1102 badusage "-p is not allowed with dgit push" if defined $package;
1104 my $clogp = parsechangelog();
1105 $package = getfield $clogp, 'Source';
1107 $isuite = getfield $clogp, 'Distribution';
1109 local ($package) = $existing_package; # this is a hack
1110 canonicalise_suite();
1113 badusage "incorrect arguments to dgit push";
1115 if (check_for_git()) {
1118 if (fetch_from_archive()) {
1119 is_fast_fwd(lrref(), 'HEAD') or
1120 fail "dgit push: HEAD is not a descendant".
1121 " of the archive's version.\n".
1122 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1125 fail "package appears to be new in this suite;".
1126 " if this is intentional, use --new";
1135 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1137 sub build_maybe_quilt_fixup () {
1138 if (!open F, "debian/source/format") {
1139 die $! unless $!==&ENOENT;
1143 F->error and die $!;
1145 return unless madformat($_);
1147 my $clogp = parsechangelog();
1148 my $version = getfield $clogp, 'Version';
1149 my $author = getfield $clogp, 'Maintainer';
1150 my $headref = rev_parse('HEAD');
1153 my $patchname = "auto-$version-$headref-$time";
1154 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1156 my $descfn = ".git/dgit/quilt-description.tmp";
1157 open O, '>', $descfn or die "$descfn: $!";
1159 $msg =~ s/^\s+$/ ./mg;
1160 print O <<END or die $!;
1161 Description: Automatically generated patch ($clogp->{Version})
1162 Last (up to) $ncommits git changes, FYI:
1172 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1173 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1174 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1175 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1178 if (!open P, '>>', ".pc/applied-patches") {
1179 $!==&ENOENT or die $!;
1184 commit_quilty_patch();
1187 sub quilt_fixup_editor () {
1188 my $descfn = $ENV{$fakeeditorenv};
1189 my $editing = $ARGV[$#ARGV];
1190 open I1, '<', $descfn or die "$descfn: $!";
1191 open I2, '<', $editing or die "$editing: $!";
1192 unlink $editing or die "$editing: $!";
1193 open O, '>', $editing or die "$editing: $!";
1194 while (<I1>) { print O or die $!; } I1->error and die $!;
1197 $copying ||= m/^\-\-\- /;
1198 next unless $copying;
1201 I2->error and die $!;
1207 badusage "-p is not allowed when building" if defined $package;
1209 my $clogp = parsechangelog();
1210 $isuite = getfield $clogp, 'Distribution';
1211 $package = getfield $clogp, 'Source';
1212 $version = getfield $clogp, 'Version';
1213 build_maybe_quilt_fixup();
1217 badusage "dgit build implies --clean=dpkg-source"
1218 if $cleanmode ne 'dpkg-source';
1220 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1221 printdone "build successful\n";
1225 badusage "dgit git-build implies --clean=dpkg-source"
1226 if $cleanmode ne 'dpkg-source';
1229 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1230 "--git-builder=@dpkgbuildpackage");
1231 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1232 canonicalise_suite();
1233 push @cmd, "--git-debian-branch=".lbranch();
1235 push @cmd, changesopts();
1236 runcmd_ordryrun @cmd, @ARGV;
1237 printdone "build successful\n";
1242 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1243 $dscfn = dscfn($version);
1244 if ($cleanmode eq 'dpkg-source') {
1245 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1247 if ($cleanmode eq 'git') {
1248 runcmd_ordryrun @git, qw(clean -xdf);
1249 } elsif ($cleanmode eq 'none') {
1253 my $pwd = cmdoutput qw(env - pwd);
1254 my $leafdir = basename $pwd;
1255 chdir ".." or die $!;
1256 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1257 chdir $pwd or die $!;
1258 runcmd_ordryrun qw(sh -ec),
1259 'exec >$1; shift; exec "$@"','x',
1260 "../$sourcechanges",
1261 @dpkggenchanges, qw(-S), changesopts();
1265 sub cmd_build_source {
1266 badusage "build-source takes no additional arguments" if @ARGV;
1268 printdone "source built, results in $dscfn and $sourcechanges";
1273 chdir ".." or die $!;
1274 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1276 stat $dscfn or fail "$dscfn (in parent directory): $!";
1277 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1278 foreach my $cf (glob $pat) {
1279 next if $cf eq $sourcechanges;
1280 unlink $cf or fail "remove $cf: $!";
1283 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1284 runcmd_ordryrun @mergechanges, glob $pat;
1285 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1287 stat $multichanges or fail "$multichanges: $!";
1289 printdone "build successful, results in $multichanges\n" or die $!;
1292 sub cmd_quilt_fixup {
1293 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1294 my $clogp = parsechangelog();
1295 $version = getfield $clogp, 'Version';
1296 build_maybe_quilt_fixup();
1300 print "dgit version $our_version\n" or die $!;
1307 last unless $ARGV[0] =~ m/^-/;
1311 if (m/^--dry-run$/) {
1313 } elsif (m/^--no-sign$/) {
1315 } elsif (m/^--help$/) {
1317 } elsif (m/^--version$/) {
1319 } elsif (m/^--new$/) {
1321 } elsif (m/^--(\w+)=(.*)/s &&
1322 ($om = $opts_opt_map{$1}) &&
1325 } elsif (m/^--(\w+):(.*)/s &&
1326 ($om = $opts_opt_map{$1})) {
1328 } elsif (m/^--existing-package=(.*)/s) {
1329 $existing_package = $1;
1330 } elsif (m/^--distro=(.*)/s) {
1332 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1334 } elsif (m/^--clean=(.*)$/s) {
1335 badusage "unknown cleaning mode \`$1'";
1336 } elsif (m/^--ignore-dirty$/s) {
1338 } elsif (m/^--no-quilt-fixup$/s) {
1341 badusage "unknown long option \`$_'";
1347 } elsif (s/^-h/-/) {
1349 } elsif (s/^-D/-/) {
1350 open DEBUG, ">&STDERR" or die $!;
1352 } elsif (s/^-N/-/) {
1354 } elsif (m/^-[vm]/) {
1355 push @changesopts, $_;
1357 } elsif (s/^-c(.*=.*)//s) {
1358 push @git, '-c', $1;
1359 } elsif (s/^-d(.*)//s) {
1361 } elsif (s/^-C(.*)//s) {
1363 } elsif (s/^-k(.*)//s) {
1365 } elsif (s/^-wn//s) {
1366 $cleanmode = 'none';
1367 } elsif (s/^-wg//s) {
1369 } elsif (s/^-wd//s) {
1370 $cleanmode = 'dpkg-source';
1372 badusage "unknown short option \`$_'";
1379 if ($ENV{$fakeeditorenv}) {
1380 quilt_fixup_editor();
1383 delete $ENV{'DGET_UNPACK'};
1386 print STDERR "DRY RUN ONLY\n" if $dryrun;
1388 print STDERR $helpmsg or die $!;
1391 my $cmd = shift @ARGV;
1393 { no strict qw(refs); &{"cmd_$cmd"}(); }