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.mirror' => 'http://ftp.debian.org/debian/');
254 return undef if $c =~ /RETURN-UNDEF/;
255 my @cmd = (@git, qw(config --), $c);
258 local ($debug) = $debug-1;
259 $v = cmdoutput_errok @cmd;
266 my $dv = $defcfg{$c};
267 return $dv if defined $dv;
269 badcfg "need value for one of: @_";
272 sub access_distro () {
273 return cfg("dgit-suite.$isuite.distro",
274 "dgit.default.distro");
279 my $distro = $idistro || access_distro();
280 my $value = cfg(map { ("dgit-distro.$distro.$_",
281 "dgit.default.$_") } @keys);
285 sub access_someuserhost ($) {
287 my $user = access_cfg("$some-user",'username');
288 my $host = access_cfg("$some-host");
289 return length($user) ? "$user\@$host" : $host;
292 sub access_gituserhost () {
293 return access_someuserhost('git');
296 sub access_giturl () {
297 my $url = access_cfg('git-url','RETURN-UNDEF');
300 access_cfg('git-proto').
301 access_gituserhost().
302 access_cfg('git-path');
304 return "$url/$package.git";
307 sub parsecontrolfh ($$@) {
308 my ($fh, $desc, @opts) = @_;
309 my %opts = ('name' => $desc, @opts);
310 my $c = Dpkg::Control::Hash->new(%opts);
311 $c->parse($fh) or die "parsing of $desc failed";
316 my ($file, $desc) = @_;
317 my $fh = new IO::Handle;
318 open $fh, '<', $file or die "$file: $!";
319 my $c = parsecontrolfh($fh,$desc);
320 $fh->error and die $!;
326 my ($dctrl,$field) = @_;
327 my $v = $dctrl->{$field};
328 return $v if defined $v;
329 fail "missing field $field in ".$v->get_option('name');
333 my $c = Dpkg::Control::Hash->new();
334 my $p = new IO::Handle;
335 my @cmd = (qw(dpkg-parsechangelog));
336 open $p, '-|', @cmd or die $!;
338 $?=0; $!=0; close $p or failedcmd @cmd;
344 sub archive_query ($) {
346 my $query = access_cfg('archive-query','RETURN-UNDEF');
347 if (!defined $query) {
348 my $distro = access_distro();
349 if ($distro eq 'debian') {
350 $query = "sshdakls:".
351 access_someuserhost('sshdakls').':'.
352 access_cfg('sshdakls-dir');
354 $query = "madison:$distro";
357 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
360 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
363 sub archive_query_madison ($$) {
364 my ($proto,$data) = @_;
365 die unless $proto eq 'madison';
366 $rmad{$package} ||= cmdoutput
367 qw(rmadison -asource),"-s$isuite","-u$data",$package;
368 my $rmad = $rmad{$package};
369 return madison_parse($rmad);
372 sub archive_query_sshdakls ($$) {
373 my ($proto,$data) = @_;
374 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
375 my $dakls = cmdoutput
376 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
377 return madison_parse($dakls);
380 sub canonicalise_suite_sshdakls ($$) {
381 my ($proto,$data) = @_;
382 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
384 (access_cfg('ssh'), $`,
386 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
387 " if test -d $isuite; then echo $isuite; exit 0; fi;".
389 my $dakls = cmdoutput @cmd;
390 failedcmd @cmd unless $dakls =~ m/^\w/;
394 sub madison_parse ($) {
397 foreach my $l (split /\n/, $rmad) {
398 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
399 \s*( [^ \t|]+ )\s* \|
400 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
401 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
402 $1 eq $package or die "$rmad $package ?";
409 $component = access_cfg('archive-query-default-component');
411 $5 eq 'source' or die "$rmad ?";
412 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
413 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
414 push @out, [$vsn,$subpath,$newsuite];
416 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
419 sub canonicalise_suite_madison ($$) {
420 my @r = archive_query_madison($_[0],$_[1]);
422 "unable to canonicalise suite using package $package".
423 " which does not appear to exist in suite $isuite;".
424 " --existing-package may help";
428 sub canonicalise_suite () {
429 return if defined $csuite;
430 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
431 $csuite = archive_query('canonicalise_suite');
432 if ($isuite ne $csuite) {
433 # madison canonicalises for us
434 print "canonical suite name for $isuite is $csuite\n";
438 sub get_archive_dsc () {
439 canonicalise_suite();
440 my @vsns = archive_query('archive_query');
441 foreach my $vinfo (@vsns) {
442 my ($vsn,$subpath) = @$vinfo;
443 $dscurl = access_cfg('mirror').$subpath;
444 $dscdata = url_get($dscurl);
445 next unless defined $dscdata;
446 $dscurl = access_cfg('mirror').$subpath;
447 $dscdata = url_get($dscurl);
448 my $dscfh = new IO::File \$dscdata, '<' or die $!;
449 print DEBUG Dumper($dscdata) if $debug>1;
450 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
451 print DEBUG Dumper($dsc) if $debug>1;
452 my $fmt = getfield $dsc, 'Format';
453 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
459 sub check_for_git () {
461 my $how = access_cfg('git-check');
462 if ($how eq 'ssh-cmd') {
464 (access_cfg('ssh'),access_gituserhost(),
465 " set -e; cd ".access_cfg('git-path').";".
466 " if test -d $package.git; then echo 1; else echo 0; fi");
467 my $r= cmdoutput @cmd;
468 failedcmd @cmd unless $r =~ m/^[01]$/;
471 badcfg "unknown git-check \`$how'";
475 sub create_remote_git_repo () {
476 my $how = access_cfg('git-create');
477 if ($how eq 'ssh-cmd') {
479 (access_cfg('ssh'),access_gituserhost(),
480 "set -e; cd ".access_cfg('git-path').";".
481 " cp -a _template $package.git");
483 badcfg "unknown git-create \`$how'";
487 our ($dsc_hash,$upload_hash);
489 our $ud = '.git/dgit/unpack';
497 sub mktree_in_ud_from_only_subdir () {
498 # changes into the subdir
501 $dirs[0] =~ m#^([^/]+)/\.$# or die;
503 chdir $dir or die "$dir $!";
504 fail "source package contains .git directory" if stat '.git';
505 die $! unless $!==&ENOENT;
506 runcmd qw(git init -q);
507 rmtree('.git/objects');
508 symlink '../../../../objects','.git/objects' or die $!;
509 runcmd @git, qw(add -Af);
510 my $tree = cmdoutput @git, qw(write-tree);
511 $tree =~ m/^\w+$/ or die "$tree ?";
515 sub dsc_files_info () {
516 foreach my $csum (qw(Sha256 Sha1 MD5)) {
517 my $fname = $csum eq 'MD5' ? 'Files' : "Checksums-$csum";
518 my $field = $dsc->{$fname};
519 next unless defined $field;
520 my $digest = uc $csum;
521 if (!eval "use Digest::$digest; 1;") {
522 print DEBUG "ignoring $fname because $@\n";
526 foreach (split /\n/, $field) {
528 m/^(\w+) (\d+) (\S+)$/ or
529 fail "could not parse .dsc $fname line \`$_'";
530 my $digester = eval "Digest::$digest->new;" or die $@;
535 Digester => $digester,
540 fail "missing any supported Checksums-* or Files field in ".
541 $dsc->get_option('name');
545 map { $_->{Filename} } dsc_files_info();
548 sub is_orig_file ($) {
550 m/\.orig(?:-\w+)?\.tar\.\w+$/;
553 sub make_commit ($) {
555 return cmdoutput @git, qw(hash-object -w -t commit), $file;
558 sub generate_commit_from_dsc () {
562 foreach my $f (dsc_files()) {
563 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
565 link "../../../$f", $f
569 runcmd @dget, qw(--), $dscurl;
570 foreach my $f (grep { is_orig_file($_) } @files) {
571 link $f, "../../../../$f"
575 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
576 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
577 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
578 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
579 my $author = getfield $clogp, 'Maintainer';
580 $author =~ s#,.*##ms;
581 my $authline = "$author $date";
582 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
583 fail "unexpected commit author line format \`$authline'".
584 " (was generated from changelog Maintainer field)";
585 my $changes = getfield $clogp, 'Changes';
586 open C, ">../commit.tmp" or die $!;
587 print C <<END or die $!;
594 # imported from the archive
597 my $outputhash = make_commit qw(../commit.tmp);
598 my $cversion = getfield $clogp, 'Version';
599 print "synthesised git commit from .dsc $cversion\n";
601 runcmd @git, qw(reset --hard), $upload_hash;
602 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
603 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
604 my $oversion = getfield $oldclogp, 'Version';
606 version_compare_string($oversion, $cversion);
608 # git upload/ is earlier vsn than archive, use archive
609 open C, ">../commit2.tmp" or die $!;
610 print C <<END or die $!;
617 Record $package ($cversion) in archive suite $csuite
619 $outputhash = make_commit qw(../commit2.tmp);
620 } elsif ($vcmp > 0) {
621 print STDERR <<END or die $!;
623 Version actually in archive: $cversion (older)
624 Last allegedly pushed/uploaded: $oversion (newer or same)
627 $outputhash = $upload_hash;
629 $outputhash = $upload_hash;
632 chdir '../../../..' or die $!;
633 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
634 'DGIT_ARCHIVE', $outputhash;
635 cmdoutput @git, qw(log -n2), $outputhash;
636 # ... gives git a chance to complain if our commit is malformed
641 sub ensure_we_have_orig () {
642 foreach my $f (dsc_files()) {
643 next unless is_orig_file($f);
645 die "$f ?" unless -f _;
647 die "$f $!" unless $!==&ENOENT;
649 my $origurl = $dscurl;
650 $origurl =~ s{/[^/]+$}{};
652 die "$f ?" unless $f =~ m/^${package}_/;
653 die "$f ?" if $f =~ m#/#;
654 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
660 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
663 sub is_fast_fwd ($$) {
664 my ($ancestor,$child) = @_;
665 my @cmd = (@git, qw(merge-base), $ancestor, $child);
666 my $mb = cmdoutput_errok @cmd;
668 return rev_parse($mb) eq rev_parse($ancestor);
670 $?==256 or failedcmd @cmd;
675 sub git_fetch_us () {
676 badusage "cannot dry run with fetch" if $dryrun;
677 runcmd @git, qw(fetch),access_giturl(),fetchspec();
680 sub fetch_from_archive () {
681 # ensures that lrref() is what is actually in the archive,
683 get_archive_dsc() or return 0;
684 foreach my $field (@ourdscfield) {
685 $dsc_hash = $dsc->{$field};
686 last if defined $dsc_hash;
688 if (defined $dsc_hash) {
689 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
691 print "last upload to archive specified git hash\n";
693 print "last upload to archive has NO git hash\n";
696 my $lrref_fn = ".git/".lrref();
697 if (open H, $lrref_fn) {
700 die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
701 } elsif ($! == &ENOENT) {
706 print DEBUG "previous reference hash=$upload_hash\n";
708 if (defined $dsc_hash) {
709 fail "missing git history even though dsc has hash -".
710 " could not find commit $dsc_hash".
711 " (should be in ".access_giturl()."#".rrref().")"
714 ensure_we_have_orig();
715 if ($dsc_hash eq $upload_hash) {
716 } elsif (is_fast_fwd($dsc_hash,$upload_hash)) {
717 print STDERR <<END or die $!;
719 Git commit in archive is behind the last version allegedly pushed/uploaded.
720 Commit referred to by archive: $dsc_hash
721 Last allegedly pushed/uploaded: $upload_hash
724 $hash = $upload_hash;
726 fail "archive's .dsc refers to ".$dsc_hash.
727 " but this is an ancestor of ".$upload_hash;
730 $hash = generate_commit_from_dsc();
732 print DEBUG "current hash=$hash\n";
734 fail "not fast forward on last upload branch!".
735 " (archive's version left in DGIT_ARCHIVE)"
736 unless is_fast_fwd($upload_hash, $hash);
738 if ($upload_hash ne $hash) {
739 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
743 dryrun_report @upd_cmd;
751 canonicalise_suite();
752 badusage "dry run makes no sense with clone" if $dryrun;
753 mkdir $dstdir or die "$dstdir $!";
754 chdir "$dstdir" or die "$dstdir $!";
755 runcmd @git, qw(init -q);
756 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
757 open H, "> .git/HEAD" or die $!;
758 print H "ref: ".lref()."\n" or die $!;
760 runcmd @git, qw(remote add), 'origin', access_giturl();
761 if (check_for_git()) {
762 print "fetching existing git history\n";
764 runcmd @git, qw(fetch origin);
766 print "starting new git history\n";
768 fetch_from_archive() or no_such_package;
769 runcmd @git, qw(reset --hard), lrref();
770 printdone "ready for work in $dstdir";
774 if (check_for_git()) {
777 fetch_from_archive() or no_such_package();
778 printdone "fetched into ".lrref();
783 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
785 printdone "fetched to ".lrref()." and merged into HEAD";
788 sub check_not_dirty () {
789 return if $ignoredirty;
790 my @cmd = (@git, qw(diff --quiet HEAD));
791 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
792 $!=0; $?=0; system @cmd;
793 return if !$! && !$?;
794 if (!$! && $?==256) {
795 fail "working tree is dirty (does not match HEAD)";
801 sub commit_quilty_patch () {
802 my $output = cmdoutput @git, qw(status --porcelain);
805 foreach my $l (split /\n/, $output) {
806 next unless $l =~ m/\S/;
807 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
810 print STDERR "git status: $l\n";
814 fail "unexpected output from git status (is tree clean?)" if $bad;
816 print "nothing quilty to commit, ok.\n";
819 runcmd_ordryrun @git, qw(add), sort keys %adds;
820 my $m = "Commit Debian 3.0 (quilt) metadata";
822 runcmd_ordryrun @git, qw(commit -m), $m;
827 return 0 unless $format eq '3.0 (quilt)';
828 print "Format \`$format', urgh\n";
830 print "Not doing any fixup of \`$format' due to --no-quilt-fixup";
837 print DEBUG "actually entering push\n";
838 my $clogp = parsechangelog();
839 $package = getfield $clogp, 'Source';
840 my $cversion = getfield $clogp, 'Version';
841 my $dscfn = dscfn($cversion);
843 fail "looked for .dsc $dscfn, but $!;".
844 " maybe you forgot to build";
845 $dsc = parsecontrol("../$dscfn","$dscfn");
846 my $dscpackage = getfield $dsc, 'Source';
847 my $format = getfield $dsc, 'Format';
848 my $dversion = getfield $dsc, 'Version';
849 ($dscpackage eq $package && $dversion eq $cversion) or
850 fail "$dsc is for $dscpackage $dversion".
851 " but debian/changelog is for $package $cversion";
852 print DEBUG "format $format\n";
853 if (madformat($format)) {
854 commit_quilty_patch();
859 print "checking that $dscfn corresponds to HEAD\n";
860 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
861 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
862 chdir '../../../..' or die $!;
863 printcmd \*DEBUG,"+",@_;
864 my @diffcmd = (@git, qw(diff --exit-code), $tree);
866 if (system @diffcmd) {
868 fail "$dscfn specifies a different tree to your HEAD commit;".
869 " perhaps you forgot to build";
875 #do fast forward check and maybe fake merge
876 # if (!is_fast_fwd(mainbranch
877 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
878 # map { lref($_).":".rref($_) }
880 $dsc->{$ourdscfield[0]} = rev_parse('HEAD');
881 $dsc->save("../$dscfn.tmp") or die $!;
883 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
885 $changesfile = $multi;
887 $!==&ENOENT or die "$multi: $!";
888 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
889 my @cs = glob "../$pat";
890 fail "failed to find unique changes file".
891 " (looked for $pat in .., or $multi);".
892 " perhaps you need to use dgit -C"
894 ($changesfile) = @cs;
897 my $changes = parsecontrol($changesfile,$changesfile);
898 foreach my $field (qw(Source Distribution Version)) {
899 $changes->{$field} eq $clogp->{$field} or
900 fail "changes field $field \`$changes->{$field}'".
901 " does not match changelog \`$clogp->{$field}'";
903 my $tag = debiantag($dversion);
904 if (!check_for_git()) {
905 create_remote_git_repo();
907 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
909 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
911 print "[new .dsc left in $dscfn.tmp]\n";
914 if (!defined $keyid) {
915 $keyid = access_cfg('keyid','RETURN-UNDEF');
917 my @tag_cmd = (@git, qw(tag -s -m),
918 "Release $dversion for $csuite [dgit]");
919 push @tag_cmd, qw(-u),$keyid if defined $keyid;
921 runcmd_ordryrun @tag_cmd;
922 my @debsign_cmd = @debsign;
923 push @debsign_cmd, "-k$keyid" if defined $keyid;
924 push @debsign_cmd, $changesfile;
925 runcmd_ordryrun @debsign_cmd;
927 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
928 my $host = access_cfg('upload-host','RETURN-UNDEF');
929 my @hostarg = defined($host) ? ($host,) : ();
930 runcmd_ordryrun @dput, @hostarg, $changesfile;
931 printdone "pushed and uploaded $dversion";
937 badusage "-p is not allowed with clone; specify as argument instead"
941 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
942 ($package,$isuite) = @ARGV;
943 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
944 ($package,$dstdir) = @ARGV;
946 ($package,$isuite,$dstdir) = @ARGV;
948 badusage "incorrect arguments to dgit clone";
950 $dstdir ||= "$package";
955 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
956 if ($branch =~ m#$lbranch_re#o) {
963 sub fetchpullargs () {
964 if (!defined $package) {
965 my $sourcep = parsecontrol('debian/control','debian/control');
966 $package = getfield $sourcep, 'Source';
969 # $isuite = branchsuite(); # this doesn't work because dak hates canons
971 my $clogp = parsechangelog();
972 $isuite = getfield $clogp, 'Distribution';
974 canonicalise_suite();
975 print "fetching from suite $csuite\n";
978 canonicalise_suite();
980 badusage "incorrect arguments to dgit fetch or dgit pull";
998 badusage "-p is not allowed with dgit push" if defined $package;
1000 my $clogp = parsechangelog();
1001 $package = getfield $clogp, 'Source';
1003 $isuite = getfield $clogp, 'Distribution';
1005 local ($package) = $existing_package; # this is a hack
1006 canonicalise_suite();
1009 badusage "incorrect arguments to dgit push";
1011 if (check_for_git()) {
1014 if (fetch_from_archive()) {
1015 is_fast_fwd(lrref(), 'HEAD') or
1016 fail "dgit push: HEAD is not a descendant".
1017 " of the archive's version.\n".
1018 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1021 fail "package appears to be new in this suite;".
1022 " if this is intentional, use --new";
1031 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1033 sub build_maybe_quilt_fixup () {
1034 if (!open F, "debian/source/format") {
1035 die $! unless $!==&ENOENT;
1039 F->error and die $!;
1041 return unless madformat($_);
1043 my $clogp = parsechangelog();
1044 my $version = getfield $clogp, 'Version';
1045 my $author = getfield $clogp, 'Maintainer';
1046 my $headref = rev_parse('HEAD');
1049 my $patchname = "auto-$version-$headref-$time";
1050 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1051 my $descfn = ".git/dgit/quilt-description.tmp";
1052 open O, '>', $descfn or die "$descfn: $!";
1054 $msg =~ s/^\s+$/ ./mg;
1055 print O <<END or die $!;
1056 Description: Automatically generated patch ($clogp->{Version})
1057 Last (up to) $ncommits git changes, FYI:
1067 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1068 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1069 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1070 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1073 if (!open P, '>>', ".pc/applied-patches") {
1074 $!==&ENOENT or die $!;
1079 commit_quilty_patch();
1082 sub quilt_fixup_editor () {
1083 my $descfn = $ENV{$fakeeditorenv};
1084 my $editing = $ARGV[$#ARGV];
1085 open I1, '<', $descfn or die "$descfn: $!";
1086 open I2, '<', $editing or die "$editing: $!";
1087 unlink $editing or die "$editing: $!";
1088 open O, '>', $editing or die "$editing: $!";
1089 while (<I1>) { print O or die $!; } I1->error and die $!;
1092 $copying ||= m/^\-\-\- /;
1093 next unless $copying;
1096 I2->error and die $!;
1102 badusage "-p is not allowed when building" if defined $package;
1104 my $clogp = parsechangelog();
1105 $isuite = getfield $clogp, 'Distribution';
1106 $package = getfield $clogp, 'Source';
1107 $version = getfield $clogp, 'Version';
1108 build_maybe_quilt_fixup();
1112 badusage "dgit build implies --clean=dpkg-source"
1113 if $cleanmode ne 'dpkg-source';
1115 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1116 printdone "build successful\n";
1120 badusage "dgit git-build implies --clean=dpkg-source"
1121 if $cleanmode ne 'dpkg-source';
1124 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1125 "--git-builder=@dpkgbuildpackage");
1126 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1127 canonicalise_suite();
1128 push @cmd, "--git-debian-branch=".lbranch();
1130 push @cmd, changesopts();
1131 runcmd_ordryrun @cmd, @ARGV;
1132 printdone "build successful\n";
1137 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1138 $dscfn = dscfn($version);
1139 if ($cleanmode eq 'dpkg-source') {
1140 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1142 if ($cleanmode eq 'git') {
1143 runcmd_ordryrun @git, qw(clean -xdf);
1144 } elsif ($cleanmode eq 'none') {
1148 my $pwd = cmdoutput qw(env - pwd);
1149 my $leafdir = basename $pwd;
1150 chdir ".." or die $!;
1151 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1152 chdir $pwd or die $!;
1153 runcmd_ordryrun qw(sh -ec),
1154 'exec >$1; shift; exec "$@"','x',
1155 "../$sourcechanges",
1156 @dpkggenchanges, qw(-S), changesopts();
1160 sub cmd_build_source {
1161 badusage "build-source takes no additional arguments" if @ARGV;
1163 printdone "source built, results in $dscfn and $sourcechanges";
1168 chdir ".." or die $!;
1169 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1171 stat $dscfn or fail "$dscfn (in parent directory): $!";
1172 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1173 foreach my $cf (glob $pat) {
1174 next if $cf eq $sourcechanges;
1175 unlink $cf or fail "remove $cf: $!";
1178 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1179 runcmd_ordryrun @mergechanges, glob $pat;
1180 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1182 stat $multichanges or fail "$multichanges: $!";
1184 printdone "build successful, results in $multichanges\n" or die $!;
1187 sub cmd_quilt_fixup {
1188 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1189 my $clogp = parsechangelog();
1190 $version = getfield $clogp, 'Version';
1191 build_maybe_quilt_fixup();
1197 last unless $ARGV[0] =~ m/^-/;
1201 if (m/^--dry-run$/) {
1203 } elsif (m/^--no-sign$/) {
1205 } elsif (m/^--help$/) {
1207 } elsif (m/^--new$/) {
1209 } elsif (m/^--(\w+)=(.*)/s &&
1210 ($om = $opts_opt_map{$1}) &&
1213 } elsif (m/^--(\w+):(.*)/s &&
1214 ($om = $opts_opt_map{$1})) {
1216 } elsif (m/^--existing-package=(.*)/s) {
1217 $existing_package = $1;
1218 } elsif (m/^--distro=(.*)/s) {
1220 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1222 } elsif (m/^--clean=(.*)$/s) {
1223 badusage "unknown cleaning mode \`$1'";
1224 } elsif (m/^--ignore-dirty$/s) {
1226 } elsif (m/^--no-quilt-fixup$/s) {
1229 badusage "unknown long option \`$_'";
1235 } elsif (s/^-h/-/) {
1237 } elsif (s/^-D/-/) {
1238 open DEBUG, ">&STDERR" or die $!;
1240 } elsif (s/^-N/-/) {
1242 } elsif (m/^-[vm]/) {
1243 push @changesopts, $_;
1245 } elsif (s/^-c(.*=.*)//s) {
1246 push @git, '-c', $1;
1247 } elsif (s/^-d(.*)//s) {
1249 } elsif (s/^-C(.*)//s) {
1251 } elsif (s/^-k(.*)//s) {
1253 } elsif (s/^-wn//s) {
1254 $cleanmode = 'none';
1255 } elsif (s/^-wg//s) {
1257 } elsif (s/^-wd//s) {
1258 $cleanmode = 'dpkg-source';
1260 badusage "unknown short option \`$_'";
1267 if ($ENV{$fakeeditorenv}) {
1268 quilt_fixup_editor();
1271 delete $ENV{'DGET_UNPACK'};
1274 print STDERR "DRY RUN ONLY\n" if $dryrun;
1276 print STDERR $helpmsg or die $!;
1279 my $cmd = shift @ARGV;
1281 { no strict qw(refs); &{"cmd_$cmd"}(); }