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';
41 our $existing_package = 'dpkg';
42 our $cleanmode = 'dpkg-source';
44 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
47 our (@dget) = qw(dget);
48 our (@dput) = qw(dput);
49 our (@debsign) = qw(debsign);
50 our (@sbuild) = qw(sbuild -A);
51 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
52 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
53 our (@dpkggenchanges) = qw(dpkg-genchanges);
54 our (@mergechanges) = qw(mergechanges -f);
55 our (@changesopts) = ('');
57 our %opts_opt_map = ('dget' => \@dget,
59 'debsign' => \@debsign,
61 'dpkg-source' => \@dpkgsource,
62 'dpkg-buildpackage' => \@dpkgbuildpackage,
63 'dpkg-genchanges' => \@dpkggenchanges,
64 'ch' => \@changesopts,
65 'mergechanges' => \@mergechanges);
70 open DEBUG, ">/dev/null" or die $!;
72 our $remotename = 'dgit';
73 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
74 our $branchprefix = 'dgit';
77 sub lbranch () { return "$branchprefix/$csuite"; }
78 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
79 sub lref () { return "refs/heads/".lbranch(); }
80 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
81 sub rrref () { return "refs/$branchprefix/$csuite"; }
96 return "${package}_".(stripepoch $vsn).".dsc";
99 sub changesopts () { return @changesopts[1..$#changesopts]; }
103 sub fail { die "$us: @_\n"; }
105 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
107 sub no_such_package () {
108 print STDERR "$us: package $package does not exist in suite $isuite\n";
114 return "+".rrref().":".lrref();
121 $ua = LWP::UserAgent->new();
125 print "downloading $what...\n";
126 my $r = $ua->get(@_) or die $!;
127 return undef if $r->code == 404;
128 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
129 return $r->decoded_content();
132 our ($dscdata,$dscurl,$dsc);
136 my $intro = shift @_;
137 print $fh $intro or die $!;
141 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
142 print $fh " '$_'" or die $!;
144 print $fh " $_" or die $!;
147 print $fh "\n" or die $!;
151 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
153 fail "failed to fork/exec: $!";
154 } elsif (!($? & 0xff)) {
155 fail "subprocess failed with error exit status ".($?>>8);
157 fail "subprocess crashed (wait status $?)";
159 fail "subprocess produced invalid output";
164 printcmd(\*DEBUG,"+",@_) if $debug>0;
166 failedcmd @_ if system @_;
171 print "dgit ok: @_\n";
173 print "would be ok: @_ (but dry run only)\n";
177 sub cmdoutput_errok {
178 die Dumper(\@_)." ?" if grep { !defined } @_;
179 printcmd(\*DEBUG,"|",@_) if $debug>0;
180 open P, "-|", @_ or die $!;
183 { local $/ = undef; $d = <P>; }
185 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
188 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
193 my $d = cmdoutput_errok @_;
194 defined $d or failedcmd @_;
199 printcmd(\*STDOUT,"#",@_);
202 sub runcmd_ordryrun {
210 our $helpmsg = <<END;
212 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
213 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
214 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
215 dgit [dgit-opts] push [dgit-opts] [suite]
216 important dgit options:
217 -k<keyid> sign tag and package with <keyid> instead of default
218 --dry-run -n do not change anything, but go through the motions
219 --new -N allow introducing a new package
220 --debug -D increase debug level
221 -c<name>=<value> set git config option (used directly by dgit too)
224 our $later_warning_msg = <<END;
225 Perhaps the upload is stuck in incoming. Using the version from git.
229 print STDERR "$us: @_\n", $helpmsg or die $!;
234 print $helpmsg or die $!;
238 our %defcfg = ('dgit.default.distro' => 'debian',
239 'dgit.default.username' => '',
240 'dgit.default.archive-query-default-component' => 'main',
241 'dgit.default.ssh' => 'ssh',
242 'dgit-distro.debian.git-host' => 'git.debian.org',
243 'dgit-distro.debian.git-proto' => 'git+ssh://',
244 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
245 'dgit-distro.debian.git-check' => 'ssh-cmd',
246 'dgit-distro.debian.git-create' => 'ssh-cmd',
247 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
248 'dgit-distro.debian.sshdakls-dir' =>
249 '/srv/ftp-master.debian.org/ftp/dists',
250 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
251 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
255 return undef if $c =~ /RETURN-UNDEF/;
256 my @cmd = (@git, qw(config --), $c);
259 local ($debug) = $debug-1;
260 $v = cmdoutput_errok @cmd;
267 my $dv = $defcfg{$c};
268 return $dv if defined $dv;
270 badcfg "need value for one of: @_";
273 sub access_distro () {
274 return cfg("dgit-suite.$isuite.distro",
275 "dgit.default.distro");
280 my $distro = $idistro || access_distro();
281 my $value = cfg(map { ("dgit-distro.$distro.$_",
282 "dgit.default.$_") } @keys);
286 sub access_someuserhost ($) {
288 my $user = access_cfg("$some-user",'username');
289 my $host = access_cfg("$some-host");
290 return length($user) ? "$user\@$host" : $host;
293 sub access_gituserhost () {
294 return access_someuserhost('git');
297 sub access_giturl () {
298 my $url = access_cfg('git-url','RETURN-UNDEF');
301 access_cfg('git-proto').
302 access_gituserhost().
303 access_cfg('git-path');
305 return "$url/$package.git";
308 sub parsecontrolfh ($$@) {
309 my ($fh, $desc, @opts) = @_;
310 my %opts = ('name' => $desc, @opts);
311 my $c = Dpkg::Control::Hash->new(%opts);
312 $c->parse($fh) or die "parsing of $desc failed";
317 my ($file, $desc) = @_;
318 my $fh = new IO::Handle;
319 open $fh, '<', $file or die "$file: $!";
320 my $c = parsecontrolfh($fh,$desc);
321 $fh->error and die $!;
327 my ($dctrl,$field) = @_;
328 my $v = $dctrl->{$field};
329 return $v if defined $v;
330 fail "missing field $field in ".$v->get_option('name');
334 my $c = Dpkg::Control::Hash->new();
335 my $p = new IO::Handle;
336 my @cmd = (qw(dpkg-parsechangelog));
337 open $p, '-|', @cmd or die $!;
339 $?=0; $!=0; close $p or failedcmd @cmd;
345 sub archive_query ($) {
347 my $query = access_cfg('archive-query','RETURN-UNDEF');
348 if (!defined $query) {
349 my $distro = access_distro();
350 if ($distro eq 'debian') {
351 $query = "sshdakls:".
352 access_someuserhost('sshdakls').':'.
353 access_cfg('sshdakls-dir');
355 $query = "madison:$distro";
358 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
361 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
364 sub archive_query_madison ($$) {
365 my ($proto,$data) = @_;
366 die unless $proto eq 'madison';
367 $rmad{$package} ||= cmdoutput
368 qw(rmadison -asource),"-s$isuite","-u$data",$package;
369 my $rmad = $rmad{$package};
370 return madison_parse($rmad);
373 sub archive_query_sshdakls ($$) {
374 my ($proto,$data) = @_;
375 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
376 my $dakls = cmdoutput
377 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
378 return madison_parse($dakls);
381 sub canonicalise_suite_sshdakls ($$) {
382 my ($proto,$data) = @_;
383 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
385 (access_cfg('ssh'), $`,
387 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
388 " if test -d $isuite; then echo $isuite; exit 0; fi;".
390 my $dakls = cmdoutput @cmd;
391 failedcmd @cmd unless $dakls =~ m/^\w/;
395 sub madison_parse ($) {
398 foreach my $l (split /\n/, $rmad) {
399 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
400 \s*( [^ \t|]+ )\s* \|
401 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
402 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
403 $1 eq $package or die "$rmad $package ?";
410 $component = access_cfg('archive-query-default-component');
412 $5 eq 'source' or die "$rmad ?";
413 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
414 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
415 push @out, [$vsn,$subpath,$newsuite];
417 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
420 sub canonicalise_suite_madison ($$) {
421 my @r = archive_query_madison($_[0],$_[1]);
423 "unable to canonicalise suite using package $package".
424 " which does not appear to exist in suite $isuite;".
425 " --existing-package may help";
429 sub canonicalise_suite () {
430 return if defined $csuite;
431 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
432 $csuite = archive_query('canonicalise_suite');
433 if ($isuite ne $csuite) {
434 # madison canonicalises for us
435 print "canonical suite name for $isuite is $csuite\n";
439 sub get_archive_dsc () {
440 canonicalise_suite();
441 my @vsns = archive_query('archive_query');
442 foreach my $vinfo (@vsns) {
443 my ($vsn,$subpath) = @$vinfo;
444 $dscurl = access_cfg('mirror').$subpath;
445 $dscdata = url_get($dscurl);
446 next unless defined $dscdata;
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 ?";
514 sub dsc_files_info () {
515 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
516 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
517 ['Files', 'Digest::MD5', 'new()']) {
518 my ($fname, $module, $method) = @$csumi;
519 my $field = $dsc->{$fname};
520 next unless defined $field;
521 eval "use $module; 1;" or die $@;
523 foreach (split /\n/, $field) {
525 m/^(\w+) (\d+) (\S+)$/ or
526 fail "could not parse .dsc $fname line \`$_'";
527 my $digester = eval "$module"."->$method;" or die $@;
532 Digester => $digester,
537 fail "missing any supported Checksums-* or Files field in ".
538 $dsc->get_option('name');
542 map { $_->{Filename} } dsc_files_info();
545 sub is_orig_file ($) {
547 m/\.orig(?:-\w+)?\.tar\.\w+$/;
550 sub make_commit ($) {
552 return cmdoutput @git, qw(hash-object -w -t commit), $file;
555 sub generate_commit_from_dsc () {
559 foreach my $f (dsc_files()) {
560 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
562 link "../../../$f", $f
566 runcmd @dget, qw(--), $dscurl;
567 foreach my $f (grep { is_orig_file($_) } @files) {
568 link $f, "../../../../$f"
572 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
573 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
574 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
575 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
576 my $author = getfield $clogp, 'Maintainer';
577 $author =~ s#,.*##ms;
578 my $authline = "$author $date";
579 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
580 fail "unexpected commit author line format \`$authline'".
581 " (was generated from changelog Maintainer field)";
582 my $changes = getfield $clogp, 'Changes';
583 open C, ">../commit.tmp" or die $!;
584 print C <<END or die $!;
591 # imported from the archive
594 my $outputhash = make_commit qw(../commit.tmp);
595 my $cversion = getfield $clogp, 'Version';
596 print "synthesised git commit from .dsc $cversion\n";
598 runcmd @git, qw(reset --hard), $upload_hash;
599 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
600 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
601 my $oversion = getfield $oldclogp, 'Version';
603 version_compare_string($oversion, $cversion);
605 # git upload/ is earlier vsn than archive, use archive
606 open C, ">../commit2.tmp" or die $!;
607 print C <<END or die $!;
614 Record $package ($cversion) in archive suite $csuite
616 $outputhash = make_commit qw(../commit2.tmp);
617 } elsif ($vcmp > 0) {
618 print STDERR <<END or die $!;
620 Version actually in archive: $cversion (older)
621 Last allegedly pushed/uploaded: $oversion (newer or same)
624 $outputhash = $upload_hash;
626 $outputhash = $upload_hash;
629 chdir '../../../..' or die $!;
630 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
631 'DGIT_ARCHIVE', $outputhash;
632 cmdoutput @git, qw(log -n2), $outputhash;
633 # ... gives git a chance to complain if our commit is malformed
638 sub ensure_we_have_orig () {
639 foreach my $fi (dsc_files_info()) {
640 my $f = $fi->{Filename};
641 next unless is_orig_file($f);
642 if (open F, "<", "../$f") {
643 $fi->{Digester}->reset();
644 $fi->{Digester}->addfile(*F);
646 my $got = $fi->{Digester}->hexdigest();
647 $got eq $fi->{Hash} or
648 fail "existing file $f has hash $got but .dsc".
649 " demands hash $fi->{Hash}".
650 " (perhaps you should delete this file?)";
651 print "using existing $f\n";
654 die "$f $!" unless $!==&ENOENT;
656 my $origurl = $dscurl;
657 $origurl =~ s{/[^/]+$}{};
659 die "$f ?" unless $f =~ m/^${package}_/;
660 die "$f ?" if $f =~ m#/#;
661 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
667 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
670 sub is_fast_fwd ($$) {
671 my ($ancestor,$child) = @_;
672 my @cmd = (@git, qw(merge-base), $ancestor, $child);
673 my $mb = cmdoutput_errok @cmd;
675 return rev_parse($mb) eq rev_parse($ancestor);
677 $?==256 or failedcmd @cmd;
682 sub git_fetch_us () {
683 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
686 sub fetch_from_archive () {
687 # ensures that lrref() is what is actually in the archive,
689 get_archive_dsc() or return 0;
690 foreach my $field (@ourdscfield) {
691 $dsc_hash = $dsc->{$field};
692 last if defined $dsc_hash;
694 if (defined $dsc_hash) {
695 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
697 print "last upload to archive specified git hash\n";
699 print "last upload to archive has NO git hash\n";
702 my $lrref_fn = ".git/".lrref();
703 if (open H, $lrref_fn) {
706 die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
707 } elsif ($! == &ENOENT) {
712 print DEBUG "previous reference hash=$upload_hash\n";
714 if (defined $dsc_hash) {
715 fail "missing git history even though dsc has hash -".
716 " could not find commit $dsc_hash".
717 " (should be in ".access_giturl()."#".rrref().")"
720 ensure_we_have_orig();
721 if ($dsc_hash eq $upload_hash) {
722 } elsif (is_fast_fwd($dsc_hash,$upload_hash)) {
723 print STDERR <<END or die $!;
725 Git commit in archive is behind the last version allegedly pushed/uploaded.
726 Commit referred to by archive: $dsc_hash
727 Last allegedly pushed/uploaded: $upload_hash
730 $hash = $upload_hash;
732 fail "archive's .dsc refers to ".$dsc_hash.
733 " but this is an ancestor of ".$upload_hash;
736 $hash = generate_commit_from_dsc();
738 print DEBUG "current hash=$hash\n";
740 fail "not fast forward on last upload branch!".
741 " (archive's version left in DGIT_ARCHIVE)"
742 unless is_fast_fwd($upload_hash, $hash);
744 if ($upload_hash ne $hash) {
745 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
749 dryrun_report @upd_cmd;
757 canonicalise_suite();
758 badusage "dry run makes no sense with clone" if $dryrun;
759 mkdir $dstdir or die "$dstdir $!";
760 chdir "$dstdir" or die "$dstdir $!";
761 runcmd @git, qw(init -q);
762 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
763 open H, "> .git/HEAD" or die $!;
764 print H "ref: ".lref()."\n" or die $!;
766 runcmd @git, qw(remote add), 'origin', access_giturl();
767 if (check_for_git()) {
768 print "fetching existing git history\n";
770 runcmd_ordryrun @git, qw(fetch origin);
772 print "starting new git history\n";
774 fetch_from_archive() or no_such_package;
775 runcmd @git, qw(reset --hard), lrref();
776 printdone "ready for work in $dstdir";
780 if (check_for_git()) {
783 fetch_from_archive() or no_such_package();
784 printdone "fetched into ".lrref();
789 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
791 printdone "fetched to ".lrref()." and merged into HEAD";
794 sub check_not_dirty () {
795 return if $ignoredirty;
796 my @cmd = (@git, qw(diff --quiet HEAD));
797 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
798 $!=0; $?=0; system @cmd;
799 return if !$! && !$?;
800 if (!$! && $?==256) {
801 fail "working tree is dirty (does not match HEAD)";
807 sub commit_quilty_patch () {
808 my $output = cmdoutput @git, qw(status --porcelain);
811 foreach my $l (split /\n/, $output) {
812 next unless $l =~ m/\S/;
813 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
816 print STDERR "git status: $l\n";
820 fail "unexpected output from git status (is tree clean?)" if $bad;
822 print "nothing quilty to commit, ok.\n";
825 runcmd_ordryrun @git, qw(add), sort keys %adds;
826 my $m = "Commit Debian 3.0 (quilt) metadata";
828 runcmd_ordryrun @git, qw(commit -m), $m;
833 return 0 unless $format eq '3.0 (quilt)';
834 print "Format \`$format', urgh\n";
836 print "Not doing any fixup of \`$format' due to --no-quilt-fixup";
843 print DEBUG "actually entering push\n";
844 my $clogp = parsechangelog();
845 $package = getfield $clogp, 'Source';
846 my $cversion = getfield $clogp, 'Version';
847 my $dscfn = dscfn($cversion);
849 fail "looked for .dsc $dscfn, but $!;".
850 " maybe you forgot to build";
851 $dsc = parsecontrol("../$dscfn","$dscfn");
852 my $dscpackage = getfield $dsc, 'Source';
853 my $format = getfield $dsc, 'Format';
854 my $dversion = getfield $dsc, 'Version';
855 ($dscpackage eq $package && $dversion eq $cversion) or
856 fail "$dsc is for $dscpackage $dversion".
857 " but debian/changelog is for $package $cversion";
858 print DEBUG "format $format\n";
859 if (madformat($format)) {
860 commit_quilty_patch();
865 print "checking that $dscfn corresponds to HEAD\n";
866 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
867 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
868 chdir '../../../..' or die $!;
869 printcmd \*DEBUG,"+",@_;
870 my @diffcmd = (@git, qw(diff --exit-code), $tree);
872 if (system @diffcmd) {
874 fail "$dscfn specifies a different tree to your HEAD commit;".
875 " perhaps you forgot to build";
881 #do fast forward check and maybe fake merge
882 # if (!is_fast_fwd(mainbranch
883 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
884 # map { lref($_).":".rref($_) }
886 $dsc->{$ourdscfield[0]} = rev_parse('HEAD');
887 $dsc->save("../$dscfn.tmp") or die $!;
889 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
891 $changesfile = $multi;
893 $!==&ENOENT or die "$multi: $!";
894 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
895 my @cs = glob "../$pat";
896 fail "failed to find unique changes file".
897 " (looked for $pat in .., or $multi);".
898 " perhaps you need to use dgit -C"
900 ($changesfile) = @cs;
903 my $changes = parsecontrol($changesfile,$changesfile);
904 foreach my $field (qw(Source Distribution Version)) {
905 $changes->{$field} eq $clogp->{$field} or
906 fail "changes field $field \`$changes->{$field}'".
907 " does not match changelog \`$clogp->{$field}'";
909 my $tag = debiantag($dversion);
910 if (!check_for_git()) {
911 create_remote_git_repo();
913 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
914 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
916 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
918 print "[new .dsc left in $dscfn.tmp]\n";
921 if (!defined $keyid) {
922 $keyid = access_cfg('keyid','RETURN-UNDEF');
924 my @tag_cmd = (@git, qw(tag -s -m),
925 "$package release $dversion for $csuite [dgit]");
926 push @tag_cmd, qw(-u),$keyid if defined $keyid;
928 runcmd_ordryrun @tag_cmd;
929 my @debsign_cmd = @debsign;
930 push @debsign_cmd, "-k$keyid" if defined $keyid;
931 push @debsign_cmd, $changesfile;
932 runcmd_ordryrun @debsign_cmd;
934 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
935 my $host = access_cfg('upload-host','RETURN-UNDEF');
936 my @hostarg = defined($host) ? ($host,) : ();
937 runcmd_ordryrun @dput, @hostarg, $changesfile;
938 printdone "pushed and uploaded $dversion";
944 badusage "-p is not allowed with clone; specify as argument instead"
948 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
949 ($package,$isuite) = @ARGV;
950 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
951 ($package,$dstdir) = @ARGV;
953 ($package,$isuite,$dstdir) = @ARGV;
955 badusage "incorrect arguments to dgit clone";
957 $dstdir ||= "$package";
962 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
963 if ($branch =~ m#$lbranch_re#o) {
970 sub fetchpullargs () {
971 if (!defined $package) {
972 my $sourcep = parsecontrol('debian/control','debian/control');
973 $package = getfield $sourcep, 'Source';
976 # $isuite = branchsuite(); # this doesn't work because dak hates canons
978 my $clogp = parsechangelog();
979 $isuite = getfield $clogp, 'Distribution';
981 canonicalise_suite();
982 print "fetching from suite $csuite\n";
985 canonicalise_suite();
987 badusage "incorrect arguments to dgit fetch or dgit pull";
1005 badusage "-p is not allowed with dgit push" if defined $package;
1007 my $clogp = parsechangelog();
1008 $package = getfield $clogp, 'Source';
1010 $isuite = getfield $clogp, 'Distribution';
1012 local ($package) = $existing_package; # this is a hack
1013 canonicalise_suite();
1016 badusage "incorrect arguments to dgit push";
1018 if (check_for_git()) {
1021 if (fetch_from_archive()) {
1022 is_fast_fwd(lrref(), 'HEAD') or
1023 fail "dgit push: HEAD is not a descendant".
1024 " of the archive's version.\n".
1025 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1028 fail "package appears to be new in this suite;".
1029 " if this is intentional, use --new";
1038 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1040 sub build_maybe_quilt_fixup () {
1041 if (!open F, "debian/source/format") {
1042 die $! unless $!==&ENOENT;
1046 F->error and die $!;
1048 return unless madformat($_);
1050 my $clogp = parsechangelog();
1051 my $version = getfield $clogp, 'Version';
1052 my $author = getfield $clogp, 'Maintainer';
1053 my $headref = rev_parse('HEAD');
1056 my $patchname = "auto-$version-$headref-$time";
1057 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1059 my $descfn = ".git/dgit/quilt-description.tmp";
1060 open O, '>', $descfn or die "$descfn: $!";
1062 $msg =~ s/^\s+$/ ./mg;
1063 print O <<END or die $!;
1064 Description: Automatically generated patch ($clogp->{Version})
1065 Last (up to) $ncommits git changes, FYI:
1075 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1076 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1077 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1078 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1081 if (!open P, '>>', ".pc/applied-patches") {
1082 $!==&ENOENT or die $!;
1087 commit_quilty_patch();
1090 sub quilt_fixup_editor () {
1091 my $descfn = $ENV{$fakeeditorenv};
1092 my $editing = $ARGV[$#ARGV];
1093 open I1, '<', $descfn or die "$descfn: $!";
1094 open I2, '<', $editing or die "$editing: $!";
1095 unlink $editing or die "$editing: $!";
1096 open O, '>', $editing or die "$editing: $!";
1097 while (<I1>) { print O or die $!; } I1->error and die $!;
1100 $copying ||= m/^\-\-\- /;
1101 next unless $copying;
1104 I2->error and die $!;
1110 badusage "-p is not allowed when building" if defined $package;
1112 my $clogp = parsechangelog();
1113 $isuite = getfield $clogp, 'Distribution';
1114 $package = getfield $clogp, 'Source';
1115 $version = getfield $clogp, 'Version';
1116 build_maybe_quilt_fixup();
1120 badusage "dgit build implies --clean=dpkg-source"
1121 if $cleanmode ne 'dpkg-source';
1123 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1124 printdone "build successful\n";
1128 badusage "dgit git-build implies --clean=dpkg-source"
1129 if $cleanmode ne 'dpkg-source';
1132 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1133 "--git-builder=@dpkgbuildpackage");
1134 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1135 canonicalise_suite();
1136 push @cmd, "--git-debian-branch=".lbranch();
1138 push @cmd, changesopts();
1139 runcmd_ordryrun @cmd, @ARGV;
1140 printdone "build successful\n";
1145 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1146 $dscfn = dscfn($version);
1147 if ($cleanmode eq 'dpkg-source') {
1148 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1150 if ($cleanmode eq 'git') {
1151 runcmd_ordryrun @git, qw(clean -xdf);
1152 } elsif ($cleanmode eq 'none') {
1156 my $pwd = cmdoutput qw(env - pwd);
1157 my $leafdir = basename $pwd;
1158 chdir ".." or die $!;
1159 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1160 chdir $pwd or die $!;
1161 runcmd_ordryrun qw(sh -ec),
1162 'exec >$1; shift; exec "$@"','x',
1163 "../$sourcechanges",
1164 @dpkggenchanges, qw(-S), changesopts();
1168 sub cmd_build_source {
1169 badusage "build-source takes no additional arguments" if @ARGV;
1171 printdone "source built, results in $dscfn and $sourcechanges";
1176 chdir ".." or die $!;
1177 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1179 stat $dscfn or fail "$dscfn (in parent directory): $!";
1180 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1181 foreach my $cf (glob $pat) {
1182 next if $cf eq $sourcechanges;
1183 unlink $cf or fail "remove $cf: $!";
1186 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1187 runcmd_ordryrun @mergechanges, glob $pat;
1188 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1190 stat $multichanges or fail "$multichanges: $!";
1192 printdone "build successful, results in $multichanges\n" or die $!;
1195 sub cmd_quilt_fixup {
1196 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1197 my $clogp = parsechangelog();
1198 $version = getfield $clogp, 'Version';
1199 build_maybe_quilt_fixup();
1205 last unless $ARGV[0] =~ m/^-/;
1209 if (m/^--dry-run$/) {
1211 } elsif (m/^--no-sign$/) {
1213 } elsif (m/^--help$/) {
1215 } elsif (m/^--new$/) {
1217 } elsif (m/^--(\w+)=(.*)/s &&
1218 ($om = $opts_opt_map{$1}) &&
1221 } elsif (m/^--(\w+):(.*)/s &&
1222 ($om = $opts_opt_map{$1})) {
1224 } elsif (m/^--existing-package=(.*)/s) {
1225 $existing_package = $1;
1226 } elsif (m/^--distro=(.*)/s) {
1228 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1230 } elsif (m/^--clean=(.*)$/s) {
1231 badusage "unknown cleaning mode \`$1'";
1232 } elsif (m/^--ignore-dirty$/s) {
1234 } elsif (m/^--no-quilt-fixup$/s) {
1237 badusage "unknown long option \`$_'";
1243 } elsif (s/^-h/-/) {
1245 } elsif (s/^-D/-/) {
1246 open DEBUG, ">&STDERR" or die $!;
1248 } elsif (s/^-N/-/) {
1250 } elsif (m/^-[vm]/) {
1251 push @changesopts, $_;
1253 } elsif (s/^-c(.*=.*)//s) {
1254 push @git, '-c', $1;
1255 } elsif (s/^-d(.*)//s) {
1257 } elsif (s/^-C(.*)//s) {
1259 } elsif (s/^-k(.*)//s) {
1261 } elsif (s/^-wn//s) {
1262 $cleanmode = 'none';
1263 } elsif (s/^-wg//s) {
1265 } elsif (s/^-wd//s) {
1266 $cleanmode = 'dpkg-source';
1268 badusage "unknown short option \`$_'";
1275 if ($ENV{$fakeeditorenv}) {
1276 quilt_fixup_editor();
1279 delete $ENV{'DGET_UNPACK'};
1282 print STDERR "DRY RUN ONLY\n" if $dryrun;
1284 print STDERR $helpmsg or die $!;
1287 my $cmd = shift @ARGV;
1289 { no strict qw(refs); &{"cmd_$cmd"}(); }