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"; }
86 sub dscfn ($) { return "${package}_$_[0].dsc"; }
88 sub changesopts () { return @changesopts[1..$#changesopts]; }
92 sub fail { die "$us: @_\n"; }
94 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
96 sub no_such_package () {
97 print STDERR "$us: package $package does not exist in suite $isuite\n";
103 return "+".rrref().":".lrref();
110 $ua = LWP::UserAgent->new();
114 print "downloading $what...\n";
115 my $r = $ua->get(@_) or die $!;
116 return undef if $r->code == 404;
117 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
118 return $r->decoded_content();
121 our ($dscdata,$dscurl,$dsc);
125 my $intro = shift @_;
126 print $fh $intro or die $!;
130 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
131 print $fh " '$_'" or die $!;
133 print $fh " $_" or die $!;
136 print $fh "\n" or die $!;
140 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
142 fail "failed to fork/exec: $!";
143 } elsif (!($? & 0xff)) {
144 fail "subprocess failed with error exit status ".($?>>8);
146 fail "subprocess crashed (wait status $?)";
148 fail "subprocess produced invalid output";
153 printcmd(\*DEBUG,"+",@_) if $debug>0;
155 failedcmd @_ if system @_;
160 print "dgit ok: @_\n";
162 print "would be ok: @_ (but dry run only)\n";
166 sub cmdoutput_errok {
167 die Dumper(\@_)." ?" if grep { !defined } @_;
168 printcmd(\*DEBUG,"|",@_) if $debug>0;
169 open P, "-|", @_ or die $!;
172 { local $/ = undef; $d = <P>; }
174 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
177 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
182 my $d = cmdoutput_errok @_;
183 defined $d or failedcmd @_;
188 printcmd(\*STDOUT,"#",@_);
191 sub runcmd_ordryrun {
199 our $helpmsg = <<END;
201 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
202 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
203 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
204 dgit [dgit-opts] push [dgit-opts] [suite]
205 important dgit options:
206 -k<keyid> sign tag and package with <keyid> instead of default
207 --dry-run -n do not change anything, but go through the motions
208 --new -N allow introducing a new package
209 --debug -D increase debug level
210 -c<name>=<value> set git config option (used directly by dgit too)
213 our $later_warning_msg = <<END;
214 Perhaps the upload is stuck in incoming. Using the version from git.
218 print STDERR "$us: @_\n", $helpmsg or die $!;
223 print $helpmsg or die $!;
227 our %defcfg = ('dgit.default.distro' => 'debian',
228 'dgit.default.username' => '',
229 'dgit.default.archive-query-default-component' => 'main',
230 'dgit.default.ssh' => 'ssh',
231 'dgit-distro.debian.git-host' => 'git.debian.org',
232 'dgit-distro.debian.git-proto' => 'git+ssh://',
233 'dgit-distro.debian.git-path' => '/git/dgit-repos',
234 'dgit-distro.debian.git-check' => 'ssh-cmd',
235 'dgit-distro.debian.git-create' => 'ssh-cmd',
236 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
237 'dgit-distro.debian.sshdakls-dir' =>
238 '/srv/ftp-master.debian.org/ftp/dists',
239 'dgit-distro.debian.mirror' => 'http://http.debian.net/debian/');
243 return undef if $c =~ /RETURN-UNDEF/;
244 my @cmd = (@git, qw(config --), $c);
247 local ($debug) = $debug-1;
248 $v = cmdoutput_errok @cmd;
255 my $dv = $defcfg{$c};
256 return $dv if defined $dv;
258 badcfg "need value for one of: @_";
261 sub access_distro () {
262 return cfg("dgit-suite.$isuite.distro",
263 "dgit.default.distro");
268 my $distro = $idistro || access_distro();
269 my $value = cfg(map { ("dgit-distro.$distro.$_",
270 "dgit.default.$_") } @keys);
274 sub access_someuserhost ($) {
276 my $user = access_cfg("$some-user",'username');
277 my $host = access_cfg("$some-host");
278 return length($user) ? "$user\@$host" : $host;
281 sub access_gituserhost () {
282 return access_someuserhost('git');
285 sub access_giturl () {
286 my $url = access_cfg('git-url','RETURN-UNDEF');
289 access_cfg('git-proto').
290 access_gituserhost().
291 access_cfg('git-path');
293 return "$url/$package.git";
296 sub parsecontrolfh ($$@) {
297 my ($fh, $desc, @opts) = @_;
298 my %opts = ('name' => $desc, @opts);
299 my $c = Dpkg::Control::Hash->new(%opts);
300 $c->parse($fh) or die "parsing of $desc failed";
305 my ($file, $desc) = @_;
306 my $fh = new IO::Handle;
307 open $fh, '<', $file or die "$file: $!";
308 my $c = parsecontrolfh($fh,$desc);
309 $fh->error and die $!;
315 my ($dctrl,$field) = @_;
316 my $v = $dctrl->{$field};
317 return $v if defined $v;
318 fail "missing field $field in ".$v->get_option('name');
322 my $c = Dpkg::Control::Hash->new();
323 my $p = new IO::Handle;
324 my @cmd = (qw(dpkg-parsechangelog));
325 open $p, '-|', @cmd or die $!;
327 $?=0; $!=0; close $p or failedcmd @cmd;
333 sub archive_query ($) {
335 my $query = access_cfg('archive-query','RETURN-UNDEF');
336 if (!defined $query) {
337 my $distro = access_distro();
338 if ($distro eq 'debian') {
339 $query = "sshdakls:".
340 access_someuserhost('sshdakls').':'.
341 access_cfg('sshdakls-dir');
343 $query = "madison:$distro";
346 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
349 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
352 sub archive_query_madison ($$) {
353 my ($proto,$data) = @_;
354 die unless $proto eq 'madison';
355 $rmad{$package} ||= cmdoutput
356 qw(rmadison -asource),"-s$isuite","-u$data",$package;
357 my $rmad = $rmad{$package};
358 return madison_parse($rmad);
361 sub archive_query_sshdakls ($$) {
362 my ($proto,$data) = @_;
363 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
364 my $dakls = cmdoutput
365 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
366 return madison_parse($dakls);
369 sub canonicalise_suite_sshdakls ($$) {
370 my ($proto,$data) = @_;
371 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
373 (access_cfg('ssh'), $`,
375 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
376 " if test -d $isuite; then echo $isuite; exit 0; fi;".
378 my $dakls = cmdoutput @cmd;
379 failedcmd @cmd unless $dakls =~ m/^\w/;
383 sub madison_parse ($) {
386 foreach my $l (split /\n/, $rmad) {
387 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
388 \s*( [^ \t|]+ )\s* \|
389 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
390 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
391 $1 eq $package or die "$rmad $package ?";
398 $component = access_cfg('archive-query-default-component');
400 $5 eq 'source' or die "$rmad ?";
401 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
402 my $subpath = "/pool/$component/$prefix/$package/${package}_$vsn.dsc";
403 push @out, [$vsn,$subpath,$newsuite];
405 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
408 sub canonicalise_suite_madison ($$) {
409 my @r = archive_query_madison($_[0],$_[1]);
411 "unable to canonicalise suite using package $package".
412 " which does not appear to exist in suite $isuite;".
413 " --existing-package may help";
417 sub canonicalise_suite () {
418 $csuite = archive_query('canonicalise_suite');
419 if ($isuite ne $csuite) {
420 # madison canonicalises for us
421 print "canonical suite name for $isuite is $csuite\n";
425 sub get_archive_dsc () {
426 canonicalise_suite();
427 my @vsns = archive_query('archive_query');
428 foreach my $vinfo (@vsns) {
429 my ($vsn,$subpath) = @$vinfo;
430 $dscurl = access_cfg('mirror').$subpath;
431 $dscdata = url_get($dscurl);
432 next unless defined $dscdata;
433 $dscurl = access_cfg('mirror').$subpath;
434 $dscdata = url_get($dscurl);
435 my $dscfh = new IO::File \$dscdata, '<' or die $!;
436 print DEBUG Dumper($dscdata) if $debug>1;
437 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
438 print DEBUG Dumper($dsc) if $debug>1;
439 my $fmt = getfield $dsc, 'Format';
440 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
446 sub check_for_git () {
448 my $how = access_cfg('git-check');
449 if ($how eq 'ssh-cmd') {
451 (access_cfg('ssh'),access_gituserhost(),
452 " set -e; cd ".access_cfg('git-path').";".
453 " if test -d $package.git; then echo 1; else echo 0; fi");
454 my $r= cmdoutput @cmd;
455 failedcmd @cmd unless $r =~ m/^[01]$/;
458 badcfg "unknown git-check \`$how'";
462 sub create_remote_git_repo () {
463 my $how = access_cfg('git-create');
464 if ($how eq 'ssh-cmd') {
466 (access_cfg('ssh'),access_gituserhost(),
467 "set -e; cd ".access_cfg('git-path').";".
468 " cp -a _template $package.git");
470 badcfg "unknown git-create \`$how'";
474 our ($dsc_hash,$upload_hash);
476 our $ud = '.git/dgit/unpack';
484 sub mktree_in_ud_from_only_subdir () {
485 # changes into the subdir
488 $dirs[0] =~ m#^([^/]+)/\.$# or die;
490 chdir $dir or die "$dir $!";
491 fail "source package contains .git directory" if stat '.git';
492 die $! unless $!==&ENOENT;
493 runcmd qw(git init -q);
494 rmtree('.git/objects');
495 symlink '../../../../objects','.git/objects' or die $!;
496 runcmd @git, qw(add -Af);
497 my $tree = cmdoutput @git, qw(write-tree);
498 $tree =~ m/^\w+$/ or die "$tree ?";
503 my $field = $dsc->{'Checksums-Sha256'} || $dsc->{Files};
505 fail "missing both Checksums-Sha256 and Files in ".
506 $dsc->get_option('name');
508 m/^\w+ \d+ (\S+)$/ or
509 fail "could not parse .dsc Files/Checksums line \`$_'";
511 } grep m/\S/, split /\n/, $field;
514 sub is_orig_file ($) {
516 m/\.orig(?:-\w+)?\.tar\.\w+$/;
519 sub make_commit ($) {
521 return cmdoutput @git, qw(hash-object -w -t commit), $file;
524 sub generate_commit_from_dsc () {
528 foreach my $f (dsc_files()) {
529 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
531 link "../../../$f", $f
535 runcmd @dget, qw(--), $dscurl;
536 foreach my $f (grep { is_orig_file($_) } @files) {
537 link $f, "../../../../$f"
541 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
542 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
543 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
544 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
545 my $author = getfield $clogp, 'Maintainer';
546 $author =~ s#,.*##ms;
547 my $authline = "$author $date";
548 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
549 fail "unexpected commit author line format \`$authline'".
550 " (was generated from changelog Maintainer field)";
551 my $changes = getfield $clogp, 'Changes';
552 open C, ">../commit.tmp" or die $!;
553 print C <<END or die $!;
560 # imported from the archive
563 my $outputhash = make_commit qw(../commit.tmp);
564 my $cversion = getfield $clogp, 'Version';
565 print "synthesised git commit from .dsc $cversion\n";
567 runcmd @git, qw(reset --hard), $upload_hash;
568 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
569 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
570 my $oversion = getfield $oldclogp, 'Version';
572 version_compare_string($oversion, $cversion);
574 # git upload/ is earlier vsn than archive, use archive
575 open C, ">../commit2.tmp" or die $!;
576 print C <<END or die $!;
583 Record $package ($cversion) in archive suite $csuite
585 $outputhash = make_commit qw(../commit2.tmp);
586 } elsif ($vcmp > 0) {
587 print STDERR <<END or die $!;
589 Version actually in archive: $cversion (older)
590 Last allegedly pushed/uploaded: $oversion (newer or same)
593 $outputhash = $upload_hash;
594 } elsif ($outputhash ne $upload_hash) {
595 fail "version in archive ($cversion)".
596 " is same as version in git".
597 " to-be-uploaded (upload/) branch ($oversion)".
598 " but archive version hash no commit hash?!";
601 chdir '../../../..' or die $!;
602 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
603 'DGIT_ARCHIVE', $outputhash;
604 cmdoutput @git, qw(log -n2), $outputhash;
605 # ... gives git a chance to complain if our commit is malformed
610 sub ensure_we_have_orig () {
611 foreach my $f (dsc_files()) {
612 next unless is_orig_file($f);
614 die "$f ?" unless -f _;
616 die "$f $!" unless $!==&ENOENT;
618 my $origurl = $dscurl;
619 $origurl =~ s{/[^/]+$}{};
621 die "$f ?" unless $f =~ m/^${package}_/;
622 die "$f ?" if $f =~ m#/#;
623 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
629 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
632 sub is_fast_fwd ($$) {
633 my ($ancestor,$child) = @_;
634 my $mb = cmdoutput @git, qw(merge-base), $ancestor, $child;
635 return rev_parse($mb) eq rev_parse($ancestor);
638 sub git_fetch_us () {
639 badusage "cannot dry run with fetch" if $dryrun;
640 runcmd @git, qw(fetch),access_giturl(),fetchspec();
643 sub fetch_from_archive () {
644 # ensures that lrref() is what is actually in the archive,
646 get_archive_dsc() or return 0;
647 foreach my $field (@ourdscfield) {
648 $dsc_hash = $dsc->{$field};
649 last if defined $dsc_hash;
651 if (defined $dsc_hash) {
652 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
654 print "last upload to archive specified git hash\n";
656 print "last upload to archive has NO git hash\n";
659 my $lrref_fn = ".git/".lrref();
660 if (open H, $lrref_fn) {
663 die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
664 } elsif ($! == &ENOENT) {
669 print DEBUG "previous reference hash=$upload_hash\n";
671 if (defined $dsc_hash) {
672 fail "missing git history even though dsc has hash -".
673 " could not find commit $dsc_hash".
674 " (should be in ".access_giturl()."#".rrref().")"
677 ensure_we_have_orig();
678 if ($dsc_hash eq $upload_hash) {
679 } elsif (is_fast_fwd($dsc_hash,$upload_hash)) {
680 print STDERR <<END or die $!;
682 Git commit in archive is behind the last version allegedly pushed/uploaded.
683 Commit referred to by archive: $dsc_hash
684 Last allegedly pushed/uploaded: $upload_hash
687 $hash = $upload_hash;
689 fail "archive's .dsc refers to ".$dsc_hash.
690 " but this is an ancestor of ".$upload_hash;
693 $hash = generate_commit_from_dsc();
695 print DEBUG "current hash=$hash\n";
697 fail "not fast forward on last upload branch!".
698 " (archive's version left in DGIT_ARCHIVE)"
699 unless is_fast_fwd($upload_hash, $hash);
701 if ($upload_hash ne $hash) {
702 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
706 dryrun_report @upd_cmd;
714 canonicalise_suite();
715 badusage "dry run makes no sense with clone" if $dryrun;
716 mkdir $dstdir or die "$dstdir $!";
717 chdir "$dstdir" or die "$dstdir $!";
718 runcmd @git, qw(init -q);
719 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
720 open H, "> .git/HEAD" or die $!;
721 print H "ref: ".lref()."\n" or die $!;
723 runcmd @git, qw(remote add), 'origin', access_giturl();
724 if (check_for_git()) {
725 print "fetching existing git history\n";
727 runcmd @git, qw(fetch origin);
729 print "starting new git history\n";
731 fetch_from_archive() or no_such_package;
732 runcmd @git, qw(reset --hard), lrref();
733 printdone "ready for work in $dstdir";
737 if (check_for_git()) {
740 fetch_from_archive() or no_such_package();
741 printdone "fetched into ".lrref();
746 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
748 printdone "fetched to ".lrref()." and merged into HEAD";
751 sub check_not_dirty () {
752 my @cmd = (@git, qw(diff --quiet HEAD));
753 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
754 $!=0; $?=0; system @cmd;
755 return if !$! && !$?;
756 if (!$! && $?==256) {
757 fail "working tree is dirty (does not match HEAD)";
763 sub commit_quilty_patch () {
764 my $output = cmdoutput @git, qw(status --porcelain);
767 foreach my $l (split /\n/, $output) {
768 next unless $l =~ m/\S/;
769 if ($l =~ m{^\?\? (.pc|debian/patches)}) {
772 print STDERR "git status: $l\n";
776 fail "unexpected output from git status (is tree clean?)" if $bad;
778 print "nothing quilty to commit, ok.\n";
781 runcmd_ordryrun @git, qw(add), sort keys %adds;
782 my $m = "Commit Debian 3.0 (quilt) metadata";
784 runcmd_ordryrun @git, qw(commit -m), $m;
789 return 0 unless $format eq '3.0 (quilt)';
790 print "Format \`$format', urgh\n";
795 print DEBUG "actually entering push\n";
796 my $clogp = parsechangelog();
797 $package = getfield $clogp, 'Source';
798 my $cversion = getfield $clogp, 'Version';
799 my $dscfn = dscfn($cversion);
801 fail "looked for .dsc $dscfn, but $!;".
802 " maybe you forgot to build";
803 $dsc = parsecontrol("../$dscfn","$dscfn");
804 my $dscpackage = getfield $dsc, 'Source';
805 my $format = getfield $dsc, 'Format';
806 my $dversion = getfield $dsc, 'Version';
807 ($dscpackage eq $package && $dversion eq $cversion) or
808 fail "$dsc is for $dscpackage $dversion".
809 " but debian/changelog is for $package $cversion";
810 print DEBUG "format $format\n";
811 if (madformat($format)) {
812 commit_quilty_patch();
817 print "checking that $dscfn corresponds to HEAD\n";
818 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
819 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
820 chdir '../../../..' or die $!;
821 printcmd \*DEBUG,"+",@_;
822 my @diffcmd = (@git, qw(diff --exit-code), $tree);
824 if (system @diffcmd) {
826 fail "$dscfn specifies a different tree to your HEAD commit;".
827 " perhaps you forgot to build";
833 #do fast forward check and maybe fake merge
834 # if (!is_fast_fwd(mainbranch
835 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
836 # map { lref($_).":".rref($_) }
838 $dsc->{$ourdscfield[0]} = rev_parse('HEAD');
839 $dsc->save("../$dscfn.tmp") or die $!;
841 my $multi = "../${package}_${cversion}_multi.changes";
843 $changesfile = $multi;
845 $!==&ENOENT or die "$multi: $!";
846 my $pat = "${package}_${cversion}_*.changes";
847 my @cs = glob "../$pat";
848 fail "failed to find unique changes file".
849 " (looked for $pat in .., or $multi);".
850 " perhaps you need to use dgit -C"
852 ($changesfile) = @cs;
855 my $changes = parsecontrol($changesfile,$changesfile);
856 foreach my $field (qw(Source Distribution Version)) {
857 $changes->{$field} eq $clogp->{$field} or
858 fail "changes field $field \`$changes->{$field}'".
859 " does not match changelog \`$clogp->{$field}'";
861 my $tag = debiantag($dversion);
862 if (!check_for_git()) {
863 create_remote_git_repo();
865 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
867 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
869 print "[new .dsc left in $dscfn.tmp]\n";
872 if (!defined $keyid) {
873 $keyid = access_cfg('keyid','RETURN-UNDEF');
875 my @tag_cmd = (@git, qw(tag -s -m),
876 "Release $dversion for $csuite [dgit]");
877 push @tag_cmd, qw(-u),$keyid if defined $keyid;
879 runcmd_ordryrun @tag_cmd;
880 my @debsign_cmd = @debsign;
881 push @debsign_cmd, "-k$keyid" if defined $keyid;
882 push @debsign_cmd, $changesfile;
883 runcmd_ordryrun @debsign_cmd;
885 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
886 my $host = access_cfg('upload-host','RETURN-UNDEF');
887 my @hostarg = defined($host) ? ($host,) : ();
888 runcmd_ordryrun @dput, @hostarg, $changesfile;
889 printdone "pushed and uploaded $dversion";
895 badusage "-p is not allowed with clone; specify as argument instead"
899 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
900 ($package,$isuite) = @ARGV;
901 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
902 ($package,$dstdir) = @ARGV;
904 ($package,$isuite,$dstdir) = @ARGV;
906 badusage "incorrect arguments to dgit clone";
908 $dstdir ||= "$package";
913 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
914 if ($branch =~ m#$lbranch_re#o) {
921 sub fetchpullargs () {
922 if (!defined $package) {
923 my $sourcep = parsecontrol('debian/control','debian/control');
924 $package = getfield $sourcep, 'Source';
927 # $isuite = branchsuite(); # this doesn't work because dak hates canons
929 my $clogp = parsechangelog();
930 $isuite = getfield $clogp, 'Distribution';
932 canonicalise_suite();
933 print "fetching from suite $csuite\n";
936 canonicalise_suite();
938 badusage "incorrect arguments to dgit fetch or dgit pull";
956 badusage "-p is not allowed with dgit push" if defined $package;
957 runcmd @git, qw(diff --quiet HEAD);
958 my $clogp = parsechangelog();
959 $package = getfield $clogp, 'Source';
961 $isuite = getfield $clogp, 'Distribution';
963 local ($package) = $existing_package; # this is a hack
964 canonicalise_suite();
967 badusage "incorrect arguments to dgit push";
969 if (check_for_git()) {
972 if (fetch_from_archive()) {
973 is_fast_fwd(lrref(), 'HEAD') or die;
976 fail "package appears to be new in this suite;".
977 " if this is intentional, use --new";
986 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
988 sub build_maybe_quilt_fixup () {
989 if (!open F, "debian/source/format") {
990 die $! unless $!==&ENOENT;
996 return unless madformat($_);
998 my $headref = rev_parse('HEAD');
1000 my $patchname = "auto-$version-$headref-$time";
1001 my $author = cmdoutput @git, qw(log -n1), '--pretty=format:%an <%ae>';
1002 my $msg = cmdoutput @git, qw(log -n1), "--pretty=format:%s\n%b";
1003 my $descfn = ".git/dgit/quilt-description.tmp";
1004 open O, '>', $descfn or die "$descfn: $!";
1006 $msg =~ s/^\s+$/ ./mg;
1007 print O <<END or die $!;
1009 [generated from git commit $headref]
1017 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1018 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1019 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1020 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1022 commit_quilty_patch();
1025 sub quilt_fixup_editor () {
1026 my $descfn = $ENV{$fakeeditorenv};
1027 my $editing = $ARGV[$#ARGV];
1028 open I1, '<', $descfn or die "$descfn: $!";
1029 open I2, '<', $editing or die "$editing: $!";
1030 unlink $editing or die "$editing: $!";
1031 open O, '>', $editing or die "$editing: $!";
1032 while (<I1>) { print O or die $!; } I1->error and die $!;
1035 $copying ||= m/^\-\-\- /;
1036 next unless $copying;
1039 I2->error and die $!;
1045 # we pass further options and args to git-buildpackage
1046 badusage "-p is not allowed with dgit build" if defined $package;
1047 badusage "dgit build implies --clean=dpkg-source" if defined $package;
1048 my $clogp = parsechangelog();
1049 $isuite = getfield $clogp, 'Distribution';
1050 $package = getfield $clogp, 'Source';
1051 $version = getfield $clogp, 'Version';
1052 build_maybe_quilt_fixup();
1054 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1055 "--git-builder=@dpkgbuildpackage");
1056 unless (grep { m/^--git-debian-branch/ } @ARGV) {
1057 canonicalise_suite();
1058 push @cmd, "--git-debian-branch=".lbranch();
1060 push @cmd, changesopts();
1061 runcmd_ordryrun @cmd, @ARGV;
1062 printdone "build successful\n";
1066 badusage "-p is not allowed with this action" if defined $package;
1068 my $clogp = parsechangelog();
1069 $package = getfield $clogp, 'Source';
1070 $isuite = getfield $clogp, 'Distribution';
1071 $version = getfield $clogp, 'Version';
1072 $sourcechanges = "${package}_${version}_source.changes";
1073 $dscfn = dscfn($version);
1074 build_maybe_quilt_fixup();
1075 if ($cleanmode eq 'dpkg-source') {
1076 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1078 if ($cleanmode eq 'git') {
1079 runcmd_ordryrun @git, qw(clean -xdf);
1080 } elsif ($cleanmode eq 'none') {
1084 my $pwd = cmdoutput qw(env - pwd);
1085 my $leafdir = basename $pwd;
1086 chdir ".." or die $!;
1087 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1088 chdir $pwd or die $!;
1089 runcmd_ordryrun qw(sh -ec),
1090 'exec >$1; shift; exec "$@"','x',
1091 "../$sourcechanges",
1092 @dpkggenchanges, qw(-S), changesopts();
1096 sub cmd_build_source {
1097 badusage "build-source takes no additional arguments" if @ARGV;
1099 printdone "source built, results in $dscfn and $sourcechanges";
1104 chdir ".." or die $!;
1105 my $pat = "${package}_${version}_*.changes";
1107 stat $dscfn or fail "$dscfn (in parent directory): $!";
1108 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1109 foreach my $cf (glob $pat) {
1110 next if $cf eq $sourcechanges;
1111 unlink $cf or fail "remove $cf: $!";
1114 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1115 runcmd_ordryrun @mergechanges, glob $pat;
1116 my $multichanges = "${package}_${version}_multi.changes";
1118 stat $multichanges or fail "$multichanges: $!";
1120 printdone "build successful, results in $multichanges\n" or die $!;
1123 sub cmd_quilt_fixup {
1124 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1125 my $clogp = parsechangelog();
1126 $version = getfield $clogp, 'Version';
1127 build_maybe_quilt_fixup();
1133 last unless $ARGV[0] =~ m/^-/;
1137 if (m/^--dry-run$/) {
1139 } elsif (m/^--no-sign$/) {
1141 } elsif (m/^--help$/) {
1143 } elsif (m/^--new$/) {
1145 } elsif (m/^--(\w+)=(.*)/s &&
1146 ($om = $opts_opt_map{$1}) &&
1149 } elsif (m/^--(\w+):(.*)/s &&
1150 ($om = $opts_opt_map{$1})) {
1152 } elsif (m/^--existing-package=(.*)/s) {
1153 $existing_package = $1;
1154 } elsif (m/^--distro=(.*)/s) {
1156 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1158 } elsif (m/^--clean=(.*)$/s) {
1159 badusage "unknown cleaning mode \`$1'";
1161 badusage "unknown long option \`$_'";
1167 } elsif (s/^-h/-/) {
1169 } elsif (s/^-D/-/) {
1170 open DEBUG, ">&STDERR" or die $!;
1172 } elsif (s/^-N/-/) {
1174 } elsif (m/^-[vm]/) {
1175 push @changesopts, $_;
1177 } elsif (s/^-c(.*=.*)//s) {
1178 push @git, '-c', $1;
1179 } elsif (s/^-d(.*)//s) {
1181 } elsif (s/^-C(.*)//s) {
1183 } elsif (s/^-k(.*)//s) {
1185 } elsif (s/^-wn//s) {
1186 $cleanmode = 'none';
1187 } elsif (s/^-wg//s) {
1189 } elsif (s/^-wd//s) {
1190 $cleanmode = 'dpkg-source';
1192 badusage "unknown short option \`$_'";
1199 if ($ENV{$fakeeditorenv}) {
1200 quilt_fixup_editor();
1204 print STDERR "DRY RUN ONLY\n" if $dryrun;
1206 print STDERR $helpmsg or die $!;
1209 my $cmd = shift @ARGV;
1211 { no strict qw(refs); &{"cmd_$cmd"}(); }