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';
40 our $existing_package = 'dpkg';
41 our $cleanmode = 'dpkg-source';
43 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
46 our (@dget) = qw(dget);
47 our (@dput) = qw(dput);
48 our (@debsign) = qw(debsign);
49 our (@sbuild) = qw(sbuild -A);
50 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
51 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
52 our (@dpkggenchanges) = qw(dpkg-genchanges);
53 our (@mergechanges) = qw(mergechanges -f);
54 our (@changesopts) = ('');
56 our %opts_opt_map = ('dget' => \@dget,
58 'debsign' => \@debsign,
60 'dpkg-source' => \@dpkgsource,
61 'dpkg-buildpackage' => \@dpkgbuildpackage,
62 'dpkg-genchanges' => \@dpkggenchanges,
63 'ch' => \@changesopts,
64 'mergechanges' => \@mergechanges);
69 open DEBUG, ">/dev/null" or die $!;
71 our $remotename = 'dgit';
72 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
73 our $branchprefix = 'dgit';
76 sub lbranch () { return "$branchprefix/$csuite"; }
77 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
78 sub lref () { return "refs/heads/".lbranch(); }
79 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
80 sub rrref () { return "refs/$branchprefix/$csuite"; }
95 return "${package}_".(stripepoch $vsn).".dsc";
98 sub changesopts () { return @changesopts[1..$#changesopts]; }
102 sub fail { die "$us: @_\n"; }
104 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
106 sub no_such_package () {
107 print STDERR "$us: package $package does not exist in suite $isuite\n";
113 return "+".rrref().":".lrref();
120 $ua = LWP::UserAgent->new();
124 print "downloading $what...\n";
125 my $r = $ua->get(@_) or die $!;
126 return undef if $r->code == 404;
127 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
128 return $r->decoded_content();
131 our ($dscdata,$dscurl,$dsc);
135 my $intro = shift @_;
136 print $fh $intro or die $!;
140 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
141 print $fh " '$_'" or die $!;
143 print $fh " $_" or die $!;
146 print $fh "\n" or die $!;
150 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
152 fail "failed to fork/exec: $!";
153 } elsif (!($? & 0xff)) {
154 fail "subprocess failed with error exit status ".($?>>8);
156 fail "subprocess crashed (wait status $?)";
158 fail "subprocess produced invalid output";
163 printcmd(\*DEBUG,"+",@_) if $debug>0;
165 failedcmd @_ if system @_;
170 print "dgit ok: @_\n";
172 print "would be ok: @_ (but dry run only)\n";
176 sub cmdoutput_errok {
177 die Dumper(\@_)." ?" if grep { !defined } @_;
178 printcmd(\*DEBUG,"|",@_) if $debug>0;
179 open P, "-|", @_ or die $!;
182 { local $/ = undef; $d = <P>; }
184 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
187 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
192 my $d = cmdoutput_errok @_;
193 defined $d or failedcmd @_;
198 printcmd(\*STDOUT,"#",@_);
201 sub runcmd_ordryrun {
209 our $helpmsg = <<END;
211 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
212 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
213 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
214 dgit [dgit-opts] push [dgit-opts] [suite]
215 important dgit options:
216 -k<keyid> sign tag and package with <keyid> instead of default
217 --dry-run -n do not change anything, but go through the motions
218 --new -N allow introducing a new package
219 --debug -D increase debug level
220 -c<name>=<value> set git config option (used directly by dgit too)
223 our $later_warning_msg = <<END;
224 Perhaps the upload is stuck in incoming. Using the version from git.
228 print STDERR "$us: @_\n", $helpmsg or die $!;
233 print $helpmsg or die $!;
237 our %defcfg = ('dgit.default.distro' => 'debian',
238 'dgit.default.username' => '',
239 'dgit.default.archive-query-default-component' => 'main',
240 'dgit.default.ssh' => 'ssh',
241 'dgit-distro.debian.git-host' => 'git.debian.org',
242 'dgit-distro.debian.git-proto' => 'git+ssh://',
243 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
244 'dgit-distro.debian.git-check' => 'ssh-cmd',
245 'dgit-distro.debian.git-create' => 'ssh-cmd',
246 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
247 'dgit-distro.debian.sshdakls-dir' =>
248 '/srv/ftp-master.debian.org/ftp/dists',
249 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
253 return undef if $c =~ /RETURN-UNDEF/;
254 my @cmd = (@git, qw(config --), $c);
257 local ($debug) = $debug-1;
258 $v = cmdoutput_errok @cmd;
265 my $dv = $defcfg{$c};
266 return $dv if defined $dv;
268 badcfg "need value for one of: @_";
271 sub access_distro () {
272 return cfg("dgit-suite.$isuite.distro",
273 "dgit.default.distro");
278 my $distro = $idistro || access_distro();
279 my $value = cfg(map { ("dgit-distro.$distro.$_",
280 "dgit.default.$_") } @keys);
284 sub access_someuserhost ($) {
286 my $user = access_cfg("$some-user",'username');
287 my $host = access_cfg("$some-host");
288 return length($user) ? "$user\@$host" : $host;
291 sub access_gituserhost () {
292 return access_someuserhost('git');
295 sub access_giturl () {
296 my $url = access_cfg('git-url','RETURN-UNDEF');
299 access_cfg('git-proto').
300 access_gituserhost().
301 access_cfg('git-path');
303 return "$url/$package.git";
306 sub parsecontrolfh ($$@) {
307 my ($fh, $desc, @opts) = @_;
308 my %opts = ('name' => $desc, @opts);
309 my $c = Dpkg::Control::Hash->new(%opts);
310 $c->parse($fh) or die "parsing of $desc failed";
315 my ($file, $desc) = @_;
316 my $fh = new IO::Handle;
317 open $fh, '<', $file or die "$file: $!";
318 my $c = parsecontrolfh($fh,$desc);
319 $fh->error and die $!;
325 my ($dctrl,$field) = @_;
326 my $v = $dctrl->{$field};
327 return $v if defined $v;
328 fail "missing field $field in ".$v->get_option('name');
332 my $c = Dpkg::Control::Hash->new();
333 my $p = new IO::Handle;
334 my @cmd = (qw(dpkg-parsechangelog));
335 open $p, '-|', @cmd or die $!;
337 $?=0; $!=0; close $p or failedcmd @cmd;
343 sub archive_query ($) {
345 my $query = access_cfg('archive-query','RETURN-UNDEF');
346 if (!defined $query) {
347 my $distro = access_distro();
348 if ($distro eq 'debian') {
349 $query = "sshdakls:".
350 access_someuserhost('sshdakls').':'.
351 access_cfg('sshdakls-dir');
353 $query = "madison:$distro";
356 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
359 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
362 sub archive_query_madison ($$) {
363 my ($proto,$data) = @_;
364 die unless $proto eq 'madison';
365 $rmad{$package} ||= cmdoutput
366 qw(rmadison -asource),"-s$isuite","-u$data",$package;
367 my $rmad = $rmad{$package};
368 return madison_parse($rmad);
371 sub archive_query_sshdakls ($$) {
372 my ($proto,$data) = @_;
373 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
374 my $dakls = cmdoutput
375 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
376 return madison_parse($dakls);
379 sub canonicalise_suite_sshdakls ($$) {
380 my ($proto,$data) = @_;
381 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
383 (access_cfg('ssh'), $`,
385 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
386 " if test -d $isuite; then echo $isuite; exit 0; fi;".
388 my $dakls = cmdoutput @cmd;
389 failedcmd @cmd unless $dakls =~ m/^\w/;
393 sub madison_parse ($) {
396 foreach my $l (split /\n/, $rmad) {
397 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
398 \s*( [^ \t|]+ )\s* \|
399 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
400 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
401 $1 eq $package or die "$rmad $package ?";
408 $component = access_cfg('archive-query-default-component');
410 $5 eq 'source' or die "$rmad ?";
411 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
412 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
413 push @out, [$vsn,$subpath,$newsuite];
415 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
418 sub canonicalise_suite_madison ($$) {
419 my @r = archive_query_madison($_[0],$_[1]);
421 "unable to canonicalise suite using package $package".
422 " which does not appear to exist in suite $isuite;".
423 " --existing-package may help";
427 sub canonicalise_suite () {
428 return if defined $csuite;
429 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
430 $csuite = archive_query('canonicalise_suite');
431 if ($isuite ne $csuite) {
432 # madison canonicalises for us
433 print "canonical suite name for $isuite is $csuite\n";
437 sub get_archive_dsc () {
438 canonicalise_suite();
439 my @vsns = archive_query('archive_query');
440 foreach my $vinfo (@vsns) {
441 my ($vsn,$subpath) = @$vinfo;
442 $dscurl = access_cfg('mirror').$subpath;
443 $dscdata = url_get($dscurl);
444 next unless defined $dscdata;
445 $dscurl = access_cfg('mirror').$subpath;
446 $dscdata = url_get($dscurl);
447 my $dscfh = new IO::File \$dscdata, '<' or die $!;
448 print DEBUG Dumper($dscdata) if $debug>1;
449 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
450 print DEBUG Dumper($dsc) if $debug>1;
451 my $fmt = getfield $dsc, 'Format';
452 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
458 sub check_for_git () {
460 my $how = access_cfg('git-check');
461 if ($how eq 'ssh-cmd') {
463 (access_cfg('ssh'),access_gituserhost(),
464 " set -e; cd ".access_cfg('git-path').";".
465 " if test -d $package.git; then echo 1; else echo 0; fi");
466 my $r= cmdoutput @cmd;
467 failedcmd @cmd unless $r =~ m/^[01]$/;
470 badcfg "unknown git-check \`$how'";
474 sub create_remote_git_repo () {
475 my $how = access_cfg('git-create');
476 if ($how eq 'ssh-cmd') {
478 (access_cfg('ssh'),access_gituserhost(),
479 "set -e; cd ".access_cfg('git-path').";".
480 " cp -a _template $package.git");
482 badcfg "unknown git-create \`$how'";
486 our ($dsc_hash,$upload_hash);
488 our $ud = '.git/dgit/unpack';
496 sub mktree_in_ud_from_only_subdir () {
497 # changes into the subdir
500 $dirs[0] =~ m#^([^/]+)/\.$# or die;
502 chdir $dir or die "$dir $!";
503 fail "source package contains .git directory" if stat '.git';
504 die $! unless $!==&ENOENT;
505 runcmd qw(git init -q);
506 rmtree('.git/objects');
507 symlink '../../../../objects','.git/objects' or die $!;
508 runcmd @git, qw(add -Af);
509 my $tree = cmdoutput @git, qw(write-tree);
510 $tree =~ m/^\w+$/ or die "$tree ?";
515 my $field = $dsc->{'Checksums-Sha256'} || $dsc->{Files};
517 fail "missing both Checksums-Sha256 and Files in ".
518 $dsc->get_option('name');
520 m/^\w+ \d+ (\S+)$/ or
521 fail "could not parse .dsc Files/Checksums line \`$_'";
523 } grep m/\S/, split /\n/, $field;
526 sub is_orig_file ($) {
528 m/\.orig(?:-\w+)?\.tar\.\w+$/;
531 sub make_commit ($) {
533 return cmdoutput @git, qw(hash-object -w -t commit), $file;
536 sub generate_commit_from_dsc () {
540 foreach my $f (dsc_files()) {
541 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
543 link "../../../$f", $f
547 runcmd @dget, qw(--), $dscurl;
548 foreach my $f (grep { is_orig_file($_) } @files) {
549 link $f, "../../../../$f"
553 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
554 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
555 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
556 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
557 my $author = getfield $clogp, 'Maintainer';
558 $author =~ s#,.*##ms;
559 my $authline = "$author $date";
560 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
561 fail "unexpected commit author line format \`$authline'".
562 " (was generated from changelog Maintainer field)";
563 my $changes = getfield $clogp, 'Changes';
564 open C, ">../commit.tmp" or die $!;
565 print C <<END or die $!;
572 # imported from the archive
575 my $outputhash = make_commit qw(../commit.tmp);
576 my $cversion = getfield $clogp, 'Version';
577 print "synthesised git commit from .dsc $cversion\n";
579 runcmd @git, qw(reset --hard), $upload_hash;
580 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
581 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
582 my $oversion = getfield $oldclogp, 'Version';
584 version_compare_string($oversion, $cversion);
586 # git upload/ is earlier vsn than archive, use archive
587 open C, ">../commit2.tmp" or die $!;
588 print C <<END or die $!;
595 Record $package ($cversion) in archive suite $csuite
597 $outputhash = make_commit qw(../commit2.tmp);
598 } elsif ($vcmp > 0) {
599 print STDERR <<END or die $!;
601 Version actually in archive: $cversion (older)
602 Last allegedly pushed/uploaded: $oversion (newer or same)
605 $outputhash = $upload_hash;
607 $outputhash = $upload_hash;
610 chdir '../../../..' or die $!;
611 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
612 'DGIT_ARCHIVE', $outputhash;
613 cmdoutput @git, qw(log -n2), $outputhash;
614 # ... gives git a chance to complain if our commit is malformed
619 sub ensure_we_have_orig () {
620 foreach my $f (dsc_files()) {
621 next unless is_orig_file($f);
623 die "$f ?" unless -f _;
625 die "$f $!" unless $!==&ENOENT;
627 my $origurl = $dscurl;
628 $origurl =~ s{/[^/]+$}{};
630 die "$f ?" unless $f =~ m/^${package}_/;
631 die "$f ?" if $f =~ m#/#;
632 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
638 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
641 sub is_fast_fwd ($$) {
642 my ($ancestor,$child) = @_;
643 my @cmd = (@git, qw(merge-base), $ancestor, $child);
644 my $mb = cmdoutput_errok @cmd;
646 return rev_parse($mb) eq rev_parse($ancestor);
648 $?==256 or failedcmd @cmd;
653 sub git_fetch_us () {
654 badusage "cannot dry run with fetch" if $dryrun;
655 runcmd @git, qw(fetch),access_giturl(),fetchspec();
658 sub fetch_from_archive () {
659 # ensures that lrref() is what is actually in the archive,
661 get_archive_dsc() or return 0;
662 foreach my $field (@ourdscfield) {
663 $dsc_hash = $dsc->{$field};
664 last if defined $dsc_hash;
666 if (defined $dsc_hash) {
667 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
669 print "last upload to archive specified git hash\n";
671 print "last upload to archive has NO git hash\n";
674 my $lrref_fn = ".git/".lrref();
675 if (open H, $lrref_fn) {
678 die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
679 } elsif ($! == &ENOENT) {
684 print DEBUG "previous reference hash=$upload_hash\n";
686 if (defined $dsc_hash) {
687 fail "missing git history even though dsc has hash -".
688 " could not find commit $dsc_hash".
689 " (should be in ".access_giturl()."#".rrref().")"
692 ensure_we_have_orig();
693 if ($dsc_hash eq $upload_hash) {
694 } elsif (is_fast_fwd($dsc_hash,$upload_hash)) {
695 print STDERR <<END or die $!;
697 Git commit in archive is behind the last version allegedly pushed/uploaded.
698 Commit referred to by archive: $dsc_hash
699 Last allegedly pushed/uploaded: $upload_hash
702 $hash = $upload_hash;
704 fail "archive's .dsc refers to ".$dsc_hash.
705 " but this is an ancestor of ".$upload_hash;
708 $hash = generate_commit_from_dsc();
710 print DEBUG "current hash=$hash\n";
712 fail "not fast forward on last upload branch!".
713 " (archive's version left in DGIT_ARCHIVE)"
714 unless is_fast_fwd($upload_hash, $hash);
716 if ($upload_hash ne $hash) {
717 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
721 dryrun_report @upd_cmd;
729 canonicalise_suite();
730 badusage "dry run makes no sense with clone" if $dryrun;
731 mkdir $dstdir or die "$dstdir $!";
732 chdir "$dstdir" or die "$dstdir $!";
733 runcmd @git, qw(init -q);
734 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
735 open H, "> .git/HEAD" or die $!;
736 print H "ref: ".lref()."\n" or die $!;
738 runcmd @git, qw(remote add), 'origin', access_giturl();
739 if (check_for_git()) {
740 print "fetching existing git history\n";
742 runcmd @git, qw(fetch origin);
744 print "starting new git history\n";
746 fetch_from_archive() or no_such_package;
747 runcmd @git, qw(reset --hard), lrref();
748 printdone "ready for work in $dstdir";
752 if (check_for_git()) {
755 fetch_from_archive() or no_such_package();
756 printdone "fetched into ".lrref();
761 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
763 printdone "fetched to ".lrref()." and merged into HEAD";
766 sub check_not_dirty () {
767 return if $ignoredirty;
768 my @cmd = (@git, qw(diff --quiet HEAD));
769 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
770 $!=0; $?=0; system @cmd;
771 return if !$! && !$?;
772 if (!$! && $?==256) {
773 fail "working tree is dirty (does not match HEAD)";
779 sub commit_quilty_patch () {
780 my $output = cmdoutput @git, qw(status --porcelain);
783 foreach my $l (split /\n/, $output) {
784 next unless $l =~ m/\S/;
785 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
788 print STDERR "git status: $l\n";
792 fail "unexpected output from git status (is tree clean?)" if $bad;
794 print "nothing quilty to commit, ok.\n";
797 runcmd_ordryrun @git, qw(add), sort keys %adds;
798 my $m = "Commit Debian 3.0 (quilt) metadata";
800 runcmd_ordryrun @git, qw(commit -m), $m;
805 return 0 unless $format eq '3.0 (quilt)';
806 print "Format \`$format', urgh\n";
811 print DEBUG "actually entering push\n";
812 my $clogp = parsechangelog();
813 $package = getfield $clogp, 'Source';
814 my $cversion = getfield $clogp, 'Version';
815 my $dscfn = dscfn($cversion);
817 fail "looked for .dsc $dscfn, but $!;".
818 " maybe you forgot to build";
819 $dsc = parsecontrol("../$dscfn","$dscfn");
820 my $dscpackage = getfield $dsc, 'Source';
821 my $format = getfield $dsc, 'Format';
822 my $dversion = getfield $dsc, 'Version';
823 ($dscpackage eq $package && $dversion eq $cversion) or
824 fail "$dsc is for $dscpackage $dversion".
825 " but debian/changelog is for $package $cversion";
826 print DEBUG "format $format\n";
827 if (madformat($format)) {
828 commit_quilty_patch();
833 print "checking that $dscfn corresponds to HEAD\n";
834 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
835 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
836 chdir '../../../..' or die $!;
837 printcmd \*DEBUG,"+",@_;
838 my @diffcmd = (@git, qw(diff --exit-code), $tree);
840 if (system @diffcmd) {
842 fail "$dscfn specifies a different tree to your HEAD commit;".
843 " perhaps you forgot to build";
849 #do fast forward check and maybe fake merge
850 # if (!is_fast_fwd(mainbranch
851 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
852 # map { lref($_).":".rref($_) }
854 $dsc->{$ourdscfield[0]} = rev_parse('HEAD');
855 $dsc->save("../$dscfn.tmp") or die $!;
857 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
859 $changesfile = $multi;
861 $!==&ENOENT or die "$multi: $!";
862 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
863 my @cs = glob "../$pat";
864 fail "failed to find unique changes file".
865 " (looked for $pat in .., or $multi);".
866 " perhaps you need to use dgit -C"
868 ($changesfile) = @cs;
871 my $changes = parsecontrol($changesfile,$changesfile);
872 foreach my $field (qw(Source Distribution Version)) {
873 $changes->{$field} eq $clogp->{$field} or
874 fail "changes field $field \`$changes->{$field}'".
875 " does not match changelog \`$clogp->{$field}'";
877 my $tag = debiantag($dversion);
878 if (!check_for_git()) {
879 create_remote_git_repo();
881 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
883 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
885 print "[new .dsc left in $dscfn.tmp]\n";
888 if (!defined $keyid) {
889 $keyid = access_cfg('keyid','RETURN-UNDEF');
891 my @tag_cmd = (@git, qw(tag -s -m),
892 "Release $dversion for $csuite [dgit]");
893 push @tag_cmd, qw(-u),$keyid if defined $keyid;
895 runcmd_ordryrun @tag_cmd;
896 my @debsign_cmd = @debsign;
897 push @debsign_cmd, "-k$keyid" if defined $keyid;
898 push @debsign_cmd, $changesfile;
899 runcmd_ordryrun @debsign_cmd;
901 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
902 my $host = access_cfg('upload-host','RETURN-UNDEF');
903 my @hostarg = defined($host) ? ($host,) : ();
904 runcmd_ordryrun @dput, @hostarg, $changesfile;
905 printdone "pushed and uploaded $dversion";
911 badusage "-p is not allowed with clone; specify as argument instead"
915 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
916 ($package,$isuite) = @ARGV;
917 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
918 ($package,$dstdir) = @ARGV;
920 ($package,$isuite,$dstdir) = @ARGV;
922 badusage "incorrect arguments to dgit clone";
924 $dstdir ||= "$package";
929 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
930 if ($branch =~ m#$lbranch_re#o) {
937 sub fetchpullargs () {
938 if (!defined $package) {
939 my $sourcep = parsecontrol('debian/control','debian/control');
940 $package = getfield $sourcep, 'Source';
943 # $isuite = branchsuite(); # this doesn't work because dak hates canons
945 my $clogp = parsechangelog();
946 $isuite = getfield $clogp, 'Distribution';
948 canonicalise_suite();
949 print "fetching from suite $csuite\n";
952 canonicalise_suite();
954 badusage "incorrect arguments to dgit fetch or dgit pull";
972 badusage "-p is not allowed with dgit push" if defined $package;
974 my $clogp = parsechangelog();
975 $package = getfield $clogp, 'Source';
977 $isuite = getfield $clogp, 'Distribution';
979 local ($package) = $existing_package; # this is a hack
980 canonicalise_suite();
983 badusage "incorrect arguments to dgit push";
985 if (check_for_git()) {
988 if (fetch_from_archive()) {
989 is_fast_fwd(lrref(), 'HEAD') or
990 fail "dgit push: HEAD is not a descendant".
991 " of the archive's version.\n".
992 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
995 fail "package appears to be new in this suite;".
996 " if this is intentional, use --new";
1005 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1007 sub build_maybe_quilt_fixup () {
1008 if (!open F, "debian/source/format") {
1009 die $! unless $!==&ENOENT;
1013 F->error and die $!;
1015 return unless madformat($_);
1017 my $clogp = parsechangelog();
1018 my $version = getfield $clogp, 'Version';
1019 my $author = getfield $clogp, 'Maintainer';
1020 my $headref = rev_parse('HEAD');
1023 my $patchname = "auto-$version-$headref-$time";
1024 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1025 my $descfn = ".git/dgit/quilt-description.tmp";
1026 open O, '>', $descfn or die "$descfn: $!";
1028 $msg =~ s/^\s+$/ ./mg;
1029 print O <<END or die $!;
1030 Description: Automatically generated patch ($clogp->{Version})
1031 Last (up to) $ncommits git changes, FYI:
1041 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1042 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1043 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1044 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1047 if (!open P, '>>', ".pc/applied-patches") {
1048 $!==&ENOENT or die $!;
1053 commit_quilty_patch();
1056 sub quilt_fixup_editor () {
1057 my $descfn = $ENV{$fakeeditorenv};
1058 my $editing = $ARGV[$#ARGV];
1059 open I1, '<', $descfn or die "$descfn: $!";
1060 open I2, '<', $editing or die "$editing: $!";
1061 unlink $editing or die "$editing: $!";
1062 open O, '>', $editing or die "$editing: $!";
1063 while (<I1>) { print O or die $!; } I1->error and die $!;
1066 $copying ||= m/^\-\-\- /;
1067 next unless $copying;
1070 I2->error and die $!;
1076 badusage "-p is not allowed when building" if defined $package;
1078 my $clogp = parsechangelog();
1079 $isuite = getfield $clogp, 'Distribution';
1080 $package = getfield $clogp, 'Source';
1081 $version = getfield $clogp, 'Version';
1082 build_maybe_quilt_fixup();
1086 badusage "dgit build implies --clean=dpkg-source"
1087 if $cleanmode ne 'dpkg-source';
1089 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1090 printdone "build successful\n";
1094 badusage "dgit git-build implies --clean=dpkg-source"
1095 if $cleanmode ne 'dpkg-source';
1098 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1099 "--git-builder=@dpkgbuildpackage");
1100 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1101 canonicalise_suite();
1102 push @cmd, "--git-debian-branch=".lbranch();
1104 push @cmd, changesopts();
1105 runcmd_ordryrun @cmd, @ARGV;
1106 printdone "build successful\n";
1111 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1112 $dscfn = dscfn($version);
1113 if ($cleanmode eq 'dpkg-source') {
1114 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1116 if ($cleanmode eq 'git') {
1117 runcmd_ordryrun @git, qw(clean -xdf);
1118 } elsif ($cleanmode eq 'none') {
1122 my $pwd = cmdoutput qw(env - pwd);
1123 my $leafdir = basename $pwd;
1124 chdir ".." or die $!;
1125 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1126 chdir $pwd or die $!;
1127 runcmd_ordryrun qw(sh -ec),
1128 'exec >$1; shift; exec "$@"','x',
1129 "../$sourcechanges",
1130 @dpkggenchanges, qw(-S), changesopts();
1134 sub cmd_build_source {
1135 badusage "build-source takes no additional arguments" if @ARGV;
1137 printdone "source built, results in $dscfn and $sourcechanges";
1142 chdir ".." or die $!;
1143 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1145 stat $dscfn or fail "$dscfn (in parent directory): $!";
1146 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1147 foreach my $cf (glob $pat) {
1148 next if $cf eq $sourcechanges;
1149 unlink $cf or fail "remove $cf: $!";
1152 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1153 runcmd_ordryrun @mergechanges, glob $pat;
1154 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1156 stat $multichanges or fail "$multichanges: $!";
1158 printdone "build successful, results in $multichanges\n" or die $!;
1161 sub cmd_quilt_fixup {
1162 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1163 my $clogp = parsechangelog();
1164 $version = getfield $clogp, 'Version';
1165 build_maybe_quilt_fixup();
1171 last unless $ARGV[0] =~ m/^-/;
1175 if (m/^--dry-run$/) {
1177 } elsif (m/^--no-sign$/) {
1179 } elsif (m/^--help$/) {
1181 } elsif (m/^--new$/) {
1183 } elsif (m/^--(\w+)=(.*)/s &&
1184 ($om = $opts_opt_map{$1}) &&
1187 } elsif (m/^--(\w+):(.*)/s &&
1188 ($om = $opts_opt_map{$1})) {
1190 } elsif (m/^--existing-package=(.*)/s) {
1191 $existing_package = $1;
1192 } elsif (m/^--distro=(.*)/s) {
1194 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1196 } elsif (m/^--clean=(.*)$/s) {
1197 badusage "unknown cleaning mode \`$1'";
1198 } elsif (m/^--ignore-dirty$/s) {
1201 badusage "unknown long option \`$_'";
1207 } elsif (s/^-h/-/) {
1209 } elsif (s/^-D/-/) {
1210 open DEBUG, ">&STDERR" or die $!;
1212 } elsif (s/^-N/-/) {
1214 } elsif (m/^-[vm]/) {
1215 push @changesopts, $_;
1217 } elsif (s/^-c(.*=.*)//s) {
1218 push @git, '-c', $1;
1219 } elsif (s/^-d(.*)//s) {
1221 } elsif (s/^-C(.*)//s) {
1223 } elsif (s/^-k(.*)//s) {
1225 } elsif (s/^-wn//s) {
1226 $cleanmode = 'none';
1227 } elsif (s/^-wg//s) {
1229 } elsif (s/^-wd//s) {
1230 $cleanmode = 'dpkg-source';
1232 badusage "unknown short option \`$_'";
1239 if ($ENV{$fakeeditorenv}) {
1240 quilt_fixup_editor();
1244 print STDERR "DRY RUN ONLY\n" if $dryrun;
1246 print STDERR $helpmsg or die $!;
1249 my $cmd = shift @ARGV;
1251 { no strict qw(refs); &{"cmd_$cmd"}(); }