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 $isuite = 'unstable';
39 our $existing_package = 'dpkg';
40 our $cleanmode = 'dpkg-source';
42 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
45 our (@dget) = qw(dget);
46 our (@dput) = qw(dput);
47 our (@debsign) = qw(debsign);
48 our (@sbuild) = qw(sbuild -A);
49 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
50 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
51 our (@dpkggenchanges) = qw(dpkg-genchanges);
52 our (@mergechanges) = qw(mergechanges -f);
53 our (@changesopts) = ('');
55 our %opts_opt_map = ('dget' => \@dget,
57 'debsign' => \@debsign,
59 'dpkg-source' => \@dpkgsource,
60 'dpkg-buildpackage' => \@dpkgbuildpackage,
61 'dpkg-genchanges' => \@dpkggenchanges,
62 'ch' => \@changesopts,
63 'mergechanges' => \@mergechanges);
68 open DEBUG, ">/dev/null" or die $!;
70 our $remotename = 'dgit';
71 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
72 our $branchprefix = 'dgit';
75 sub lbranch () { return "$branchprefix/$csuite"; }
76 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
77 sub lref () { return "refs/heads/".lbranch(); }
78 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
79 sub rrref () { return "refs/$branchprefix/$csuite"; }
94 return "${package}_".(stripepoch $vsn).".dsc";
97 sub changesopts () { return @changesopts[1..$#changesopts]; }
101 sub fail { die "$us: @_\n"; }
103 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
105 sub no_such_package () {
106 print STDERR "$us: package $package does not exist in suite $isuite\n";
112 return "+".rrref().":".lrref();
119 $ua = LWP::UserAgent->new();
123 print "downloading $what...\n";
124 my $r = $ua->get(@_) or die $!;
125 return undef if $r->code == 404;
126 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
127 return $r->decoded_content();
130 our ($dscdata,$dscurl,$dsc);
134 my $intro = shift @_;
135 print $fh $intro or die $!;
139 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
140 print $fh " '$_'" or die $!;
142 print $fh " $_" or die $!;
145 print $fh "\n" or die $!;
149 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
151 fail "failed to fork/exec: $!";
152 } elsif (!($? & 0xff)) {
153 fail "subprocess failed with error exit status ".($?>>8);
155 fail "subprocess crashed (wait status $?)";
157 fail "subprocess produced invalid output";
162 printcmd(\*DEBUG,"+",@_) if $debug>0;
164 failedcmd @_ if system @_;
169 print "dgit ok: @_\n";
171 print "would be ok: @_ (but dry run only)\n";
175 sub cmdoutput_errok {
176 die Dumper(\@_)." ?" if grep { !defined } @_;
177 printcmd(\*DEBUG,"|",@_) if $debug>0;
178 open P, "-|", @_ or die $!;
181 { local $/ = undef; $d = <P>; }
183 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
186 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
191 my $d = cmdoutput_errok @_;
192 defined $d or failedcmd @_;
197 printcmd(\*STDOUT,"#",@_);
200 sub runcmd_ordryrun {
208 our $helpmsg = <<END;
210 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
211 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
212 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
213 dgit [dgit-opts] push [dgit-opts] [suite]
214 important dgit options:
215 -k<keyid> sign tag and package with <keyid> instead of default
216 --dry-run -n do not change anything, but go through the motions
217 --new -N allow introducing a new package
218 --debug -D increase debug level
219 -c<name>=<value> set git config option (used directly by dgit too)
222 our $later_warning_msg = <<END;
223 Perhaps the upload is stuck in incoming. Using the version from git.
227 print STDERR "$us: @_\n", $helpmsg or die $!;
232 print $helpmsg or die $!;
236 our %defcfg = ('dgit.default.distro' => 'debian',
237 'dgit.default.username' => '',
238 'dgit.default.archive-query-default-component' => 'main',
239 'dgit.default.ssh' => 'ssh',
240 'dgit-distro.debian.git-host' => 'git.debian.org',
241 'dgit-distro.debian.git-proto' => 'git+ssh://',
242 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
243 'dgit-distro.debian.git-check' => 'ssh-cmd',
244 'dgit-distro.debian.git-create' => 'ssh-cmd',
245 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
246 'dgit-distro.debian.sshdakls-dir' =>
247 '/srv/ftp-master.debian.org/ftp/dists',
248 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
252 return undef if $c =~ /RETURN-UNDEF/;
253 my @cmd = (@git, qw(config --), $c);
256 local ($debug) = $debug-1;
257 $v = cmdoutput_errok @cmd;
264 my $dv = $defcfg{$c};
265 return $dv if defined $dv;
267 badcfg "need value for one of: @_";
270 sub access_distro () {
271 return cfg("dgit-suite.$isuite.distro",
272 "dgit.default.distro");
277 my $distro = $idistro || access_distro();
278 my $value = cfg(map { ("dgit-distro.$distro.$_",
279 "dgit.default.$_") } @keys);
283 sub access_someuserhost ($) {
285 my $user = access_cfg("$some-user",'username');
286 my $host = access_cfg("$some-host");
287 return length($user) ? "$user\@$host" : $host;
290 sub access_gituserhost () {
291 return access_someuserhost('git');
294 sub access_giturl () {
295 my $url = access_cfg('git-url','RETURN-UNDEF');
298 access_cfg('git-proto').
299 access_gituserhost().
300 access_cfg('git-path');
302 return "$url/$package.git";
305 sub parsecontrolfh ($$@) {
306 my ($fh, $desc, @opts) = @_;
307 my %opts = ('name' => $desc, @opts);
308 my $c = Dpkg::Control::Hash->new(%opts);
309 $c->parse($fh) or die "parsing of $desc failed";
314 my ($file, $desc) = @_;
315 my $fh = new IO::Handle;
316 open $fh, '<', $file or die "$file: $!";
317 my $c = parsecontrolfh($fh,$desc);
318 $fh->error and die $!;
324 my ($dctrl,$field) = @_;
325 my $v = $dctrl->{$field};
326 return $v if defined $v;
327 fail "missing field $field in ".$v->get_option('name');
331 my $c = Dpkg::Control::Hash->new();
332 my $p = new IO::Handle;
333 my @cmd = (qw(dpkg-parsechangelog));
334 open $p, '-|', @cmd or die $!;
336 $?=0; $!=0; close $p or failedcmd @cmd;
342 sub archive_query ($) {
344 my $query = access_cfg('archive-query','RETURN-UNDEF');
345 if (!defined $query) {
346 my $distro = access_distro();
347 if ($distro eq 'debian') {
348 $query = "sshdakls:".
349 access_someuserhost('sshdakls').':'.
350 access_cfg('sshdakls-dir');
352 $query = "madison:$distro";
355 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
358 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
361 sub archive_query_madison ($$) {
362 my ($proto,$data) = @_;
363 die unless $proto eq 'madison';
364 $rmad{$package} ||= cmdoutput
365 qw(rmadison -asource),"-s$isuite","-u$data",$package;
366 my $rmad = $rmad{$package};
367 return madison_parse($rmad);
370 sub archive_query_sshdakls ($$) {
371 my ($proto,$data) = @_;
372 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
373 my $dakls = cmdoutput
374 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
375 return madison_parse($dakls);
378 sub canonicalise_suite_sshdakls ($$) {
379 my ($proto,$data) = @_;
380 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
382 (access_cfg('ssh'), $`,
384 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
385 " if test -d $isuite; then echo $isuite; exit 0; fi;".
387 my $dakls = cmdoutput @cmd;
388 failedcmd @cmd unless $dakls =~ m/^\w/;
392 sub madison_parse ($) {
395 foreach my $l (split /\n/, $rmad) {
396 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
397 \s*( [^ \t|]+ )\s* \|
398 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
399 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
400 $1 eq $package or die "$rmad $package ?";
407 $component = access_cfg('archive-query-default-component');
409 $5 eq 'source' or die "$rmad ?";
410 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
411 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
412 push @out, [$vsn,$subpath,$newsuite];
414 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
417 sub canonicalise_suite_madison ($$) {
418 my @r = archive_query_madison($_[0],$_[1]);
420 "unable to canonicalise suite using package $package".
421 " which does not appear to exist in suite $isuite;".
422 " --existing-package may help";
426 sub canonicalise_suite () {
427 return if defined $csuite;
428 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
429 $csuite = archive_query('canonicalise_suite');
430 if ($isuite ne $csuite) {
431 # madison canonicalises for us
432 print "canonical suite name for $isuite is $csuite\n";
436 sub get_archive_dsc () {
437 canonicalise_suite();
438 my @vsns = archive_query('archive_query');
439 foreach my $vinfo (@vsns) {
440 my ($vsn,$subpath) = @$vinfo;
441 $dscurl = access_cfg('mirror').$subpath;
442 $dscdata = url_get($dscurl);
443 next unless defined $dscdata;
444 $dscurl = access_cfg('mirror').$subpath;
445 $dscdata = url_get($dscurl);
446 my $dscfh = new IO::File \$dscdata, '<' or die $!;
447 print DEBUG Dumper($dscdata) if $debug>1;
448 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
449 print DEBUG Dumper($dsc) if $debug>1;
450 my $fmt = getfield $dsc, 'Format';
451 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
457 sub check_for_git () {
459 my $how = access_cfg('git-check');
460 if ($how eq 'ssh-cmd') {
462 (access_cfg('ssh'),access_gituserhost(),
463 " set -e; cd ".access_cfg('git-path').";".
464 " if test -d $package.git; then echo 1; else echo 0; fi");
465 my $r= cmdoutput @cmd;
466 failedcmd @cmd unless $r =~ m/^[01]$/;
469 badcfg "unknown git-check \`$how'";
473 sub create_remote_git_repo () {
474 my $how = access_cfg('git-create');
475 if ($how eq 'ssh-cmd') {
477 (access_cfg('ssh'),access_gituserhost(),
478 "set -e; cd ".access_cfg('git-path').";".
479 " cp -a _template $package.git");
481 badcfg "unknown git-create \`$how'";
485 our ($dsc_hash,$upload_hash);
487 our $ud = '.git/dgit/unpack';
495 sub mktree_in_ud_from_only_subdir () {
496 # changes into the subdir
499 $dirs[0] =~ m#^([^/]+)/\.$# or die;
501 chdir $dir or die "$dir $!";
502 fail "source package contains .git directory" if stat '.git';
503 die $! unless $!==&ENOENT;
504 runcmd qw(git init -q);
505 rmtree('.git/objects');
506 symlink '../../../../objects','.git/objects' or die $!;
507 runcmd @git, qw(add -Af);
508 my $tree = cmdoutput @git, qw(write-tree);
509 $tree =~ m/^\w+$/ or die "$tree ?";
514 my $field = $dsc->{'Checksums-Sha256'} || $dsc->{Files};
516 fail "missing both Checksums-Sha256 and Files in ".
517 $dsc->get_option('name');
519 m/^\w+ \d+ (\S+)$/ or
520 fail "could not parse .dsc Files/Checksums line \`$_'";
522 } grep m/\S/, split /\n/, $field;
525 sub is_orig_file ($) {
527 m/\.orig(?:-\w+)?\.tar\.\w+$/;
530 sub make_commit ($) {
532 return cmdoutput @git, qw(hash-object -w -t commit), $file;
535 sub generate_commit_from_dsc () {
539 foreach my $f (dsc_files()) {
540 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
542 link "../../../$f", $f
546 runcmd @dget, qw(--), $dscurl;
547 foreach my $f (grep { is_orig_file($_) } @files) {
548 link $f, "../../../../$f"
552 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
553 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
554 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
555 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
556 my $author = getfield $clogp, 'Maintainer';
557 $author =~ s#,.*##ms;
558 my $authline = "$author $date";
559 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
560 fail "unexpected commit author line format \`$authline'".
561 " (was generated from changelog Maintainer field)";
562 my $changes = getfield $clogp, 'Changes';
563 open C, ">../commit.tmp" or die $!;
564 print C <<END or die $!;
571 # imported from the archive
574 my $outputhash = make_commit qw(../commit.tmp);
575 my $cversion = getfield $clogp, 'Version';
576 print "synthesised git commit from .dsc $cversion\n";
578 runcmd @git, qw(reset --hard), $upload_hash;
579 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
580 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
581 my $oversion = getfield $oldclogp, 'Version';
583 version_compare_string($oversion, $cversion);
585 # git upload/ is earlier vsn than archive, use archive
586 open C, ">../commit2.tmp" or die $!;
587 print C <<END or die $!;
594 Record $package ($cversion) in archive suite $csuite
596 $outputhash = make_commit qw(../commit2.tmp);
597 } elsif ($vcmp > 0) {
598 print STDERR <<END or die $!;
600 Version actually in archive: $cversion (older)
601 Last allegedly pushed/uploaded: $oversion (newer or same)
604 $outputhash = $upload_hash;
606 $outputhash = $upload_hash;
609 chdir '../../../..' or die $!;
610 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
611 'DGIT_ARCHIVE', $outputhash;
612 cmdoutput @git, qw(log -n2), $outputhash;
613 # ... gives git a chance to complain if our commit is malformed
618 sub ensure_we_have_orig () {
619 foreach my $f (dsc_files()) {
620 next unless is_orig_file($f);
622 die "$f ?" unless -f _;
624 die "$f $!" unless $!==&ENOENT;
626 my $origurl = $dscurl;
627 $origurl =~ s{/[^/]+$}{};
629 die "$f ?" unless $f =~ m/^${package}_/;
630 die "$f ?" if $f =~ m#/#;
631 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
637 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
640 sub is_fast_fwd ($$) {
641 my ($ancestor,$child) = @_;
642 my $mb = cmdoutput @git, qw(merge-base), $ancestor, $child;
643 return rev_parse($mb) eq rev_parse($ancestor);
646 sub git_fetch_us () {
647 badusage "cannot dry run with fetch" if $dryrun;
648 runcmd @git, qw(fetch),access_giturl(),fetchspec();
651 sub fetch_from_archive () {
652 # ensures that lrref() is what is actually in the archive,
654 get_archive_dsc() or return 0;
655 foreach my $field (@ourdscfield) {
656 $dsc_hash = $dsc->{$field};
657 last if defined $dsc_hash;
659 if (defined $dsc_hash) {
660 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
662 print "last upload to archive specified git hash\n";
664 print "last upload to archive has NO git hash\n";
667 my $lrref_fn = ".git/".lrref();
668 if (open H, $lrref_fn) {
671 die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
672 } elsif ($! == &ENOENT) {
677 print DEBUG "previous reference hash=$upload_hash\n";
679 if (defined $dsc_hash) {
680 fail "missing git history even though dsc has hash -".
681 " could not find commit $dsc_hash".
682 " (should be in ".access_giturl()."#".rrref().")"
685 ensure_we_have_orig();
686 if ($dsc_hash eq $upload_hash) {
687 } elsif (is_fast_fwd($dsc_hash,$upload_hash)) {
688 print STDERR <<END or die $!;
690 Git commit in archive is behind the last version allegedly pushed/uploaded.
691 Commit referred to by archive: $dsc_hash
692 Last allegedly pushed/uploaded: $upload_hash
695 $hash = $upload_hash;
697 fail "archive's .dsc refers to ".$dsc_hash.
698 " but this is an ancestor of ".$upload_hash;
701 $hash = generate_commit_from_dsc();
703 print DEBUG "current hash=$hash\n";
705 fail "not fast forward on last upload branch!".
706 " (archive's version left in DGIT_ARCHIVE)"
707 unless is_fast_fwd($upload_hash, $hash);
709 if ($upload_hash ne $hash) {
710 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
714 dryrun_report @upd_cmd;
722 canonicalise_suite();
723 badusage "dry run makes no sense with clone" if $dryrun;
724 mkdir $dstdir or die "$dstdir $!";
725 chdir "$dstdir" or die "$dstdir $!";
726 runcmd @git, qw(init -q);
727 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
728 open H, "> .git/HEAD" or die $!;
729 print H "ref: ".lref()."\n" or die $!;
731 runcmd @git, qw(remote add), 'origin', access_giturl();
732 if (check_for_git()) {
733 print "fetching existing git history\n";
735 runcmd @git, qw(fetch origin);
737 print "starting new git history\n";
739 fetch_from_archive() or no_such_package;
740 runcmd @git, qw(reset --hard), lrref();
741 printdone "ready for work in $dstdir";
745 if (check_for_git()) {
748 fetch_from_archive() or no_such_package();
749 printdone "fetched into ".lrref();
754 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
756 printdone "fetched to ".lrref()." and merged into HEAD";
759 sub check_not_dirty () {
760 my @cmd = (@git, qw(diff --quiet HEAD));
761 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
762 $!=0; $?=0; system @cmd;
763 return if !$! && !$?;
764 if (!$! && $?==256) {
765 fail "working tree is dirty (does not match HEAD)";
771 sub commit_quilty_patch () {
772 my $output = cmdoutput @git, qw(status --porcelain);
775 foreach my $l (split /\n/, $output) {
776 next unless $l =~ m/\S/;
777 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
780 print STDERR "git status: $l\n";
784 fail "unexpected output from git status (is tree clean?)" if $bad;
786 print "nothing quilty to commit, ok.\n";
789 runcmd_ordryrun @git, qw(add), sort keys %adds;
790 my $m = "Commit Debian 3.0 (quilt) metadata";
792 runcmd_ordryrun @git, qw(commit -m), $m;
797 return 0 unless $format eq '3.0 (quilt)';
798 print "Format \`$format', urgh\n";
803 print DEBUG "actually entering push\n";
804 my $clogp = parsechangelog();
805 $package = getfield $clogp, 'Source';
806 my $cversion = getfield $clogp, 'Version';
807 my $dscfn = dscfn($cversion);
809 fail "looked for .dsc $dscfn, but $!;".
810 " maybe you forgot to build";
811 $dsc = parsecontrol("../$dscfn","$dscfn");
812 my $dscpackage = getfield $dsc, 'Source';
813 my $format = getfield $dsc, 'Format';
814 my $dversion = getfield $dsc, 'Version';
815 ($dscpackage eq $package && $dversion eq $cversion) or
816 fail "$dsc is for $dscpackage $dversion".
817 " but debian/changelog is for $package $cversion";
818 print DEBUG "format $format\n";
819 if (madformat($format)) {
820 commit_quilty_patch();
825 print "checking that $dscfn corresponds to HEAD\n";
826 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
827 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
828 chdir '../../../..' or die $!;
829 printcmd \*DEBUG,"+",@_;
830 my @diffcmd = (@git, qw(diff --exit-code), $tree);
832 if (system @diffcmd) {
834 fail "$dscfn specifies a different tree to your HEAD commit;".
835 " perhaps you forgot to build";
841 #do fast forward check and maybe fake merge
842 # if (!is_fast_fwd(mainbranch
843 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
844 # map { lref($_).":".rref($_) }
846 $dsc->{$ourdscfield[0]} = rev_parse('HEAD');
847 $dsc->save("../$dscfn.tmp") or die $!;
849 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
851 $changesfile = $multi;
853 $!==&ENOENT or die "$multi: $!";
854 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
855 my @cs = glob "../$pat";
856 fail "failed to find unique changes file".
857 " (looked for $pat in .., or $multi);".
858 " perhaps you need to use dgit -C"
860 ($changesfile) = @cs;
863 my $changes = parsecontrol($changesfile,$changesfile);
864 foreach my $field (qw(Source Distribution Version)) {
865 $changes->{$field} eq $clogp->{$field} or
866 fail "changes field $field \`$changes->{$field}'".
867 " does not match changelog \`$clogp->{$field}'";
869 my $tag = debiantag($dversion);
870 if (!check_for_git()) {
871 create_remote_git_repo();
873 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
875 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
877 print "[new .dsc left in $dscfn.tmp]\n";
880 if (!defined $keyid) {
881 $keyid = access_cfg('keyid','RETURN-UNDEF');
883 my @tag_cmd = (@git, qw(tag -s -m),
884 "Release $dversion for $csuite [dgit]");
885 push @tag_cmd, qw(-u),$keyid if defined $keyid;
887 runcmd_ordryrun @tag_cmd;
888 my @debsign_cmd = @debsign;
889 push @debsign_cmd, "-k$keyid" if defined $keyid;
890 push @debsign_cmd, $changesfile;
891 runcmd_ordryrun @debsign_cmd;
893 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
894 my $host = access_cfg('upload-host','RETURN-UNDEF');
895 my @hostarg = defined($host) ? ($host,) : ();
896 runcmd_ordryrun @dput, @hostarg, $changesfile;
897 printdone "pushed and uploaded $dversion";
903 badusage "-p is not allowed with clone; specify as argument instead"
907 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
908 ($package,$isuite) = @ARGV;
909 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
910 ($package,$dstdir) = @ARGV;
912 ($package,$isuite,$dstdir) = @ARGV;
914 badusage "incorrect arguments to dgit clone";
916 $dstdir ||= "$package";
921 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
922 if ($branch =~ m#$lbranch_re#o) {
929 sub fetchpullargs () {
930 if (!defined $package) {
931 my $sourcep = parsecontrol('debian/control','debian/control');
932 $package = getfield $sourcep, 'Source';
935 # $isuite = branchsuite(); # this doesn't work because dak hates canons
937 my $clogp = parsechangelog();
938 $isuite = getfield $clogp, 'Distribution';
940 canonicalise_suite();
941 print "fetching from suite $csuite\n";
944 canonicalise_suite();
946 badusage "incorrect arguments to dgit fetch or dgit pull";
964 badusage "-p is not allowed with dgit push" if defined $package;
966 my $clogp = parsechangelog();
967 $package = getfield $clogp, 'Source';
969 $isuite = getfield $clogp, 'Distribution';
971 local ($package) = $existing_package; # this is a hack
972 canonicalise_suite();
975 badusage "incorrect arguments to dgit push";
977 if (check_for_git()) {
980 if (fetch_from_archive()) {
981 is_fast_fwd(lrref(), 'HEAD') or die;
984 fail "package appears to be new in this suite;".
985 " if this is intentional, use --new";
994 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
996 sub build_maybe_quilt_fixup () {
997 if (!open F, "debian/source/format") {
998 die $! unless $!==&ENOENT;
1002 F->error and die $!;
1004 return unless madformat($_);
1006 my $clogp = parsechangelog();
1007 my $version = getfield $clogp, 'Version';
1008 my $author = getfield $clogp, 'Maintainer';
1009 my $headref = rev_parse('HEAD');
1012 my $patchname = "auto-$version-$headref-$time";
1013 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1014 my $descfn = ".git/dgit/quilt-description.tmp";
1015 open O, '>', $descfn or die "$descfn: $!";
1017 $msg =~ s/^\s+$/ ./mg;
1018 print O <<END or die $!;
1019 Description: Automatically generated patch ($clogp->{Version})
1020 Last (up to) $ncommits git changes, FYI:
1030 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1031 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1032 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1033 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1036 if (!open P, '>>', ".pc/applied-patches") {
1037 $!==&ENOENT or die $!;
1042 commit_quilty_patch();
1045 sub quilt_fixup_editor () {
1046 my $descfn = $ENV{$fakeeditorenv};
1047 my $editing = $ARGV[$#ARGV];
1048 open I1, '<', $descfn or die "$descfn: $!";
1049 open I2, '<', $editing or die "$editing: $!";
1050 unlink $editing or die "$editing: $!";
1051 open O, '>', $editing or die "$editing: $!";
1052 while (<I1>) { print O or die $!; } I1->error and die $!;
1055 $copying ||= m/^\-\-\- /;
1056 next unless $copying;
1059 I2->error and die $!;
1065 badusage "-p is not allowed when building" if defined $package;
1067 my $clogp = parsechangelog();
1068 $isuite = getfield $clogp, 'Distribution';
1069 $package = getfield $clogp, 'Source';
1070 $version = getfield $clogp, 'Version';
1071 build_maybe_quilt_fixup();
1075 badusage "dgit build implies --clean=dpkg-source"
1076 if $cleanmode ne 'dpkg-source';
1078 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1079 printdone "build successful\n";
1083 badusage "dgit git-build implies --clean=dpkg-source"
1084 if $cleanmode ne 'dpkg-source';
1087 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1088 "--git-builder=@dpkgbuildpackage");
1089 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1090 canonicalise_suite();
1091 push @cmd, "--git-debian-branch=".lbranch();
1093 push @cmd, changesopts();
1094 runcmd_ordryrun @cmd, @ARGV;
1095 printdone "build successful\n";
1100 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1101 $dscfn = dscfn($version);
1102 if ($cleanmode eq 'dpkg-source') {
1103 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1105 if ($cleanmode eq 'git') {
1106 runcmd_ordryrun @git, qw(clean -xdf);
1107 } elsif ($cleanmode eq 'none') {
1111 my $pwd = cmdoutput qw(env - pwd);
1112 my $leafdir = basename $pwd;
1113 chdir ".." or die $!;
1114 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1115 chdir $pwd or die $!;
1116 runcmd_ordryrun qw(sh -ec),
1117 'exec >$1; shift; exec "$@"','x',
1118 "../$sourcechanges",
1119 @dpkggenchanges, qw(-S), changesopts();
1123 sub cmd_build_source {
1124 badusage "build-source takes no additional arguments" if @ARGV;
1126 printdone "source built, results in $dscfn and $sourcechanges";
1131 chdir ".." or die $!;
1132 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1134 stat $dscfn or fail "$dscfn (in parent directory): $!";
1135 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1136 foreach my $cf (glob $pat) {
1137 next if $cf eq $sourcechanges;
1138 unlink $cf or fail "remove $cf: $!";
1141 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1142 runcmd_ordryrun @mergechanges, glob $pat;
1143 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1145 stat $multichanges or fail "$multichanges: $!";
1147 printdone "build successful, results in $multichanges\n" or die $!;
1150 sub cmd_quilt_fixup {
1151 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1152 my $clogp = parsechangelog();
1153 $version = getfield $clogp, 'Version';
1154 build_maybe_quilt_fixup();
1160 last unless $ARGV[0] =~ m/^-/;
1164 if (m/^--dry-run$/) {
1166 } elsif (m/^--no-sign$/) {
1168 } elsif (m/^--help$/) {
1170 } elsif (m/^--new$/) {
1172 } elsif (m/^--(\w+)=(.*)/s &&
1173 ($om = $opts_opt_map{$1}) &&
1176 } elsif (m/^--(\w+):(.*)/s &&
1177 ($om = $opts_opt_map{$1})) {
1179 } elsif (m/^--existing-package=(.*)/s) {
1180 $existing_package = $1;
1181 } elsif (m/^--distro=(.*)/s) {
1183 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1185 } elsif (m/^--clean=(.*)$/s) {
1186 badusage "unknown cleaning mode \`$1'";
1188 badusage "unknown long option \`$_'";
1194 } elsif (s/^-h/-/) {
1196 } elsif (s/^-D/-/) {
1197 open DEBUG, ">&STDERR" or die $!;
1199 } elsif (s/^-N/-/) {
1201 } elsif (m/^-[vm]/) {
1202 push @changesopts, $_;
1204 } elsif (s/^-c(.*=.*)//s) {
1205 push @git, '-c', $1;
1206 } elsif (s/^-d(.*)//s) {
1208 } elsif (s/^-C(.*)//s) {
1210 } elsif (s/^-k(.*)//s) {
1212 } elsif (s/^-wn//s) {
1213 $cleanmode = 'none';
1214 } elsif (s/^-wg//s) {
1216 } elsif (s/^-wd//s) {
1217 $cleanmode = 'dpkg-source';
1219 badusage "unknown short option \`$_'";
1226 if ($ENV{$fakeeditorenv}) {
1227 quilt_fixup_editor();
1231 print STDERR "DRY RUN ONLY\n" if $dryrun;
1233 print STDERR $helpmsg or die $!;
1236 my $cmd = shift @ARGV;
1238 { no strict qw(refs); &{"cmd_$cmd"}(); }