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 $our_version = 'UNRELEASED'; ###substituted###
33 our $isuite = 'unstable';
43 our $existing_package = 'dpkg';
44 our $cleanmode = 'dpkg-source';
46 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
49 our (@dget) = qw(dget);
50 our (@dput) = qw(dput);
51 our (@debsign) = qw(debsign);
52 our (@sbuild) = qw(sbuild -A);
53 our (@dpkgbuildpackage) = qw(dpkg-buildpackage -i\.git/ -I.git);
54 our (@dpkgsource) = qw(dpkg-source -i\.git/ -I.git);
55 our (@dpkggenchanges) = qw(dpkg-genchanges);
56 our (@mergechanges) = qw(mergechanges -f);
57 our (@changesopts) = ('');
59 our %opts_opt_map = ('dget' => \@dget,
61 'debsign' => \@debsign,
63 'dpkg-source' => \@dpkgsource,
64 'dpkg-buildpackage' => \@dpkgbuildpackage,
65 'dpkg-genchanges' => \@dpkggenchanges,
66 'ch' => \@changesopts,
67 'mergechanges' => \@mergechanges);
72 open DEBUG, ">/dev/null" or die $!;
74 our $remotename = 'dgit';
75 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
76 our $branchprefix = 'dgit';
79 sub lbranch () { return "$branchprefix/$csuite"; }
80 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
81 sub lref () { return "refs/heads/".lbranch(); }
82 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
83 sub rrref () { return "refs/$branchprefix/$csuite"; }
98 return "${package}_".(stripepoch $vsn).".dsc";
101 sub changesopts () { return @changesopts[1..$#changesopts]; }
105 sub fail { die "$us: @_\n"; }
107 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
109 sub no_such_package () {
110 print STDERR "$us: package $package does not exist in suite $isuite\n";
116 return "+".rrref().":".lrref();
123 $ua = LWP::UserAgent->new();
127 print "downloading $what...\n";
128 my $r = $ua->get(@_) or die $!;
129 return undef if $r->code == 404;
130 $r->is_success or fail "failed to fetch $what: ".$r->status_line;
131 return $r->decoded_content();
134 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
138 my $intro = shift @_;
139 print $fh $intro or die $!;
143 if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
144 print $fh " '$_'" or die $!;
146 print $fh " $_" or die $!;
149 print $fh "\n" or die $!;
153 { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
155 fail "failed to fork/exec: $!";
156 } elsif (!($? & 0xff)) {
157 fail "subprocess failed with error exit status ".($?>>8);
159 fail "subprocess crashed (wait status $?)";
161 fail "subprocess produced invalid output";
166 printcmd(\*DEBUG,"+",@_) if $debug>0;
168 failedcmd @_ if system @_;
173 print "dgit ok: @_\n";
175 print "would be ok: @_ (but dry run only)\n";
179 sub cmdoutput_errok {
180 die Dumper(\@_)." ?" if grep { !defined } @_;
181 printcmd(\*DEBUG,"|",@_) if $debug>0;
182 open P, "-|", @_ or die $!;
185 { local $/ = undef; $d = <P>; }
187 if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
190 print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
195 my $d = cmdoutput_errok @_;
196 defined $d or failedcmd @_;
201 printcmd(\*STDOUT,"#",@_);
204 sub runcmd_ordryrun {
213 my ($first_shell, @cmd) = @_;
214 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
217 our $helpmsg = <<END;
219 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
220 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
221 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
222 dgit [dgit-opts] push [dgit-opts] [suite]
223 important dgit options:
224 -k<keyid> sign tag and package with <keyid> instead of default
225 --dry-run -n do not change anything, but go through the motions
226 --new -N allow introducing a new package
227 --debug -D increase debug level
228 -c<name>=<value> set git config option (used directly by dgit too)
231 our $later_warning_msg = <<END;
232 Perhaps the upload is stuck in incoming. Using the version from git.
236 print STDERR "$us: @_\n", $helpmsg or die $!;
241 print $helpmsg or die $!;
245 our %defcfg = ('dgit.default.distro' => 'debian',
246 'dgit.default.username' => '',
247 'dgit.default.archive-query-default-component' => 'main',
248 'dgit.default.ssh' => 'ssh',
249 'dgit-distro.debian.git-host' => 'git.debian.org',
250 'dgit-distro.debian.git-proto' => 'git+ssh://',
251 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
252 'dgit-distro.debian.git-check' => 'ssh-cmd',
253 'dgit-distro.debian.git-create' => 'ssh-cmd',
254 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
255 'dgit-distro.debian.sshdakls-dir' =>
256 '/srv/ftp-master.debian.org/ftp/dists',
257 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
258 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
262 return undef if $c =~ /RETURN-UNDEF/;
263 my @cmd = (@git, qw(config --), $c);
266 local ($debug) = $debug-1;
267 $v = cmdoutput_errok @cmd;
274 my $dv = $defcfg{$c};
275 return $dv if defined $dv;
277 badcfg "need value for one of: @_";
280 sub access_distro () {
281 return cfg("dgit-suite.$isuite.distro",
282 "dgit.default.distro");
287 my $distro = $idistro || access_distro();
288 my $value = cfg(map { ("dgit-distro.$distro.$_",
289 "dgit.default.$_") } @keys);
293 sub access_someuserhost ($) {
295 my $user = access_cfg("$some-user",'username');
296 my $host = access_cfg("$some-host");
297 return length($user) ? "$user\@$host" : $host;
300 sub access_gituserhost () {
301 return access_someuserhost('git');
304 sub access_giturl () {
305 my $url = access_cfg('git-url','RETURN-UNDEF');
308 access_cfg('git-proto').
309 access_gituserhost().
310 access_cfg('git-path');
312 return "$url/$package.git";
315 sub parsecontrolfh ($$@) {
316 my ($fh, $desc, @opts) = @_;
317 my %opts = ('name' => $desc, @opts);
318 my $c = Dpkg::Control::Hash->new(%opts);
319 $c->parse($fh) or die "parsing of $desc failed";
324 my ($file, $desc) = @_;
325 my $fh = new IO::Handle;
326 open $fh, '<', $file or die "$file: $!";
327 my $c = parsecontrolfh($fh,$desc);
328 $fh->error and die $!;
334 my ($dctrl,$field) = @_;
335 my $v = $dctrl->{$field};
336 return $v if defined $v;
337 fail "missing field $field in ".$v->get_option('name');
341 my $c = Dpkg::Control::Hash->new();
342 my $p = new IO::Handle;
343 my @cmd = (qw(dpkg-parsechangelog), @_);
344 open $p, '-|', @cmd or die $!;
346 $?=0; $!=0; close $p or failedcmd @cmd;
352 sub archive_query ($) {
354 my $query = access_cfg('archive-query','RETURN-UNDEF');
355 if (!defined $query) {
356 my $distro = access_distro();
357 if ($distro eq 'debian') {
358 $query = "sshdakls:".
359 access_someuserhost('sshdakls').':'.
360 access_cfg('sshdakls-dir');
362 $query = "madison:$distro";
365 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
368 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
371 sub archive_query_madison ($$) {
372 my ($proto,$data) = @_;
373 die unless $proto eq 'madison';
374 $rmad{$package} ||= cmdoutput
375 qw(rmadison -asource),"-s$isuite","-u$data",$package;
376 my $rmad = $rmad{$package};
377 return madison_parse($rmad);
380 sub archive_query_sshdakls ($$) {
381 my ($proto,$data) = @_;
382 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
383 my $dakls = cmdoutput
384 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
385 return madison_parse($dakls);
388 sub canonicalise_suite_sshdakls ($$) {
389 my ($proto,$data) = @_;
390 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
392 (access_cfg('ssh'), $`,
394 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
395 " if test -d $isuite; then echo $isuite; exit 0; fi;".
397 my $dakls = cmdoutput @cmd;
398 failedcmd @cmd unless $dakls =~ m/^\w/;
402 sub madison_parse ($) {
405 foreach my $l (split /\n/, $rmad) {
406 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
407 \s*( [^ \t|]+ )\s* \|
408 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
409 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
410 $1 eq $package or die "$rmad $package ?";
417 $component = access_cfg('archive-query-default-component');
419 $5 eq 'source' or die "$rmad ?";
420 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
421 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
422 push @out, [$vsn,$subpath,$newsuite];
424 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
427 sub canonicalise_suite_madison ($$) {
428 my @r = archive_query_madison($_[0],$_[1]);
430 "unable to canonicalise suite using package $package".
431 " which does not appear to exist in suite $isuite;".
432 " --existing-package may help";
436 sub canonicalise_suite () {
437 return if defined $csuite;
438 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
439 $csuite = archive_query('canonicalise_suite');
440 if ($isuite ne $csuite) {
441 # madison canonicalises for us
442 print "canonical suite name for $isuite is $csuite\n";
446 sub get_archive_dsc () {
447 canonicalise_suite();
448 my @vsns = archive_query('archive_query');
449 foreach my $vinfo (@vsns) {
450 my ($vsn,$subpath) = @$vinfo;
451 $dscurl = access_cfg('mirror').$subpath;
452 $dscdata = url_get($dscurl);
454 $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
457 my $dscfh = new IO::File \$dscdata, '<' or die $!;
458 print DEBUG Dumper($dscdata) if $debug>1;
459 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
460 print DEBUG Dumper($dsc) if $debug>1;
461 my $fmt = getfield $dsc, 'Format';
462 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
468 sub check_for_git () {
470 my $how = access_cfg('git-check');
471 if ($how eq 'ssh-cmd') {
473 (access_cfg('ssh'),access_gituserhost(),
474 " set -e; cd ".access_cfg('git-path').";".
475 " if test -d $package.git; then echo 1; else echo 0; fi");
476 my $r= cmdoutput @cmd;
477 failedcmd @cmd unless $r =~ m/^[01]$/;
480 badcfg "unknown git-check \`$how'";
484 sub create_remote_git_repo () {
485 my $how = access_cfg('git-create');
486 if ($how eq 'ssh-cmd') {
488 (access_cfg('ssh'),access_gituserhost(),
489 "set -e; cd ".access_cfg('git-path').";".
490 " cp -a _template $package.git");
492 badcfg "unknown git-create \`$how'";
496 our ($dsc_hash,$lastpush_hash);
498 our $ud = '.git/dgit/unpack';
506 sub mktree_in_ud_from_only_subdir () {
507 # changes into the subdir
510 $dirs[0] =~ m#^([^/]+)/\.$# or die;
512 chdir $dir or die "$dir $!";
513 fail "source package contains .git directory" if stat '.git';
514 die $! unless $!==&ENOENT;
515 runcmd qw(git init -q);
516 rmtree('.git/objects');
517 symlink '../../../../objects','.git/objects' or die $!;
518 runcmd @git, qw(add -Af);
519 my $tree = cmdoutput @git, qw(write-tree);
520 $tree =~ m/^\w+$/ or die "$tree ?";
524 sub dsc_files_info () {
525 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
526 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
527 ['Files', 'Digest::MD5', 'new()']) {
528 my ($fname, $module, $method) = @$csumi;
529 my $field = $dsc->{$fname};
530 next unless defined $field;
531 eval "use $module; 1;" or die $@;
533 foreach (split /\n/, $field) {
535 m/^(\w+) (\d+) (\S+)$/ or
536 fail "could not parse .dsc $fname line \`$_'";
537 my $digester = eval "$module"."->$method;" or die $@;
542 Digester => $digester,
547 fail "missing any supported Checksums-* or Files field in ".
548 $dsc->get_option('name');
552 map { $_->{Filename} } dsc_files_info();
555 sub is_orig_file ($) {
557 m/\.orig(?:-\w+)?\.tar\.\w+$/;
560 sub make_commit ($) {
562 return cmdoutput @git, qw(hash-object -w -t commit), $file;
565 sub generate_commit_from_dsc () {
569 foreach my $f (dsc_files()) {
570 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
572 link "../../../$f", $f
576 runcmd @dget, qw(--), $dscurl;
577 foreach my $f (grep { is_orig_file($_) } @files) {
578 link $f, "../../../../$f"
582 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
583 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
584 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
585 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
586 my $author = getfield $clogp, 'Maintainer';
587 $author =~ s#,.*##ms;
588 my $authline = "$author $date";
589 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
590 fail "unexpected commit author line format \`$authline'".
591 " (was generated from changelog Maintainer field)";
592 my $changes = getfield $clogp, 'Changes';
593 open C, ">../commit.tmp" or die $!;
594 print C <<END or die $!;
601 # imported from the archive
604 my $outputhash = make_commit qw(../commit.tmp);
605 my $cversion = getfield $clogp, 'Version';
606 print "synthesised git commit from .dsc $cversion\n";
607 if ($lastpush_hash) {
608 runcmd @git, qw(reset --hard), $lastpush_hash;
609 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
610 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
611 my $oversion = getfield $oldclogp, 'Version';
613 version_compare_string($oversion, $cversion);
615 # git upload/ is earlier vsn than archive, use archive
616 open C, ">../commit2.tmp" or die $!;
617 print C <<END or die $!;
619 parent $lastpush_hash
624 Record $package ($cversion) in archive suite $csuite
626 $outputhash = make_commit qw(../commit2.tmp);
627 } elsif ($vcmp > 0) {
628 print STDERR <<END or die $!;
630 Version actually in archive: $cversion (older)
631 Last allegedly pushed/uploaded: $oversion (newer or same)
634 $outputhash = $lastpush_hash;
636 $outputhash = $lastpush_hash;
639 chdir '../../../..' or die $!;
640 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
641 'DGIT_ARCHIVE', $outputhash;
642 cmdoutput @git, qw(log -n2), $outputhash;
643 # ... gives git a chance to complain if our commit is malformed
648 sub ensure_we_have_orig () {
649 foreach my $fi (dsc_files_info()) {
650 my $f = $fi->{Filename};
651 next unless is_orig_file($f);
652 if (open F, "<", "../$f") {
653 $fi->{Digester}->reset();
654 $fi->{Digester}->addfile(*F);
656 my $got = $fi->{Digester}->hexdigest();
657 $got eq $fi->{Hash} or
658 fail "existing file $f has hash $got but .dsc".
659 " demands hash $fi->{Hash}".
660 " (perhaps you should delete this file?)";
661 print "using existing $f\n";
664 die "$f $!" unless $!==&ENOENT;
666 my $origurl = $dscurl;
667 $origurl =~ s{/[^/]+$}{};
669 die "$f ?" unless $f =~ m/^${package}_/;
670 die "$f ?" if $f =~ m#/#;
671 runcmd_ordryrun shell_cmd 'cd ..', @dget,'--',$origurl;
676 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
679 sub is_fast_fwd ($$) {
680 my ($ancestor,$child) = @_;
681 my @cmd = (@git, qw(merge-base), $ancestor, $child);
682 my $mb = cmdoutput_errok @cmd;
684 return rev_parse($mb) eq rev_parse($ancestor);
686 $?==256 or failedcmd @cmd;
691 sub git_fetch_us () {
692 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
695 sub fetch_from_archive () {
696 # ensures that lrref() is what is actually in the archive,
701 foreach my $field (@ourdscfield) {
702 $dsc_hash = $dsc->{$field};
703 last if defined $dsc_hash;
705 if (defined $dsc_hash) {
706 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
708 print "last upload to archive specified git hash\n";
710 print "last upload to archive has NO git hash\n";
713 print "no version available from the archive\n";
716 my $lrref_fn = ".git/".lrref();
717 if (open H, $lrref_fn) {
718 $lastpush_hash = <H>;
719 chomp $lastpush_hash;
720 die "$lrref_fn $lastpush_hash ?" unless $lastpush_hash =~ m/^\w+$/;
721 } elsif ($! == &ENOENT) {
726 print DEBUG "previous reference hash=$lastpush_hash\n";
728 if (defined $dsc_hash) {
729 fail "missing git history even though dsc has hash -".
730 " could not find commit $dsc_hash".
731 " (should be in ".access_giturl()."#".rrref().")"
732 unless $lastpush_hash;
734 ensure_we_have_orig();
735 if ($dsc_hash eq $lastpush_hash) {
736 } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
737 print STDERR <<END or die $!;
739 Git commit in archive is behind the last version allegedly pushed/uploaded.
740 Commit referred to by archive: $dsc_hash
741 Last allegedly pushed/uploaded: $lastpush_hash
744 $hash = $lastpush_hash;
746 fail "archive's .dsc refers to ".$dsc_hash.
747 " but this is an ancestor of ".$lastpush_hash;
750 $hash = generate_commit_from_dsc();
751 } elsif ($lastpush_hash) {
752 # only in git, not in the archive yet
753 $hash = $lastpush_hash;
754 print STDERR <<END or die $!;
756 Package not found in the archive, but has allegedly been pushed using dgit.
760 print DEBUG "nothing found!\n";
761 if (defined $skew_warning_vsn) {
762 print STDERR <<END or die $!;
764 Warning: relevant archive skew detected.
765 Archive allegedly contains $skew_warning_vsn
766 But we were not able to obtain any version from the archive or git.
772 print DEBUG "current hash=$hash\n";
773 if ($lastpush_hash) {
774 fail "not fast forward on last upload branch!".
775 " (archive's version left in DGIT_ARCHIVE)"
776 unless is_fast_fwd($lastpush_hash, $hash);
778 if (defined $skew_warning_vsn) {
780 print DEBUG "SKEW CHECK WANT $skew_warning_vsn\n";
781 my $clogf = ".git/dgit/changelog.tmp";
782 runcmd shell_cmd "exec >$clogf",
783 @git, qw(cat-file blob), "$hash:debian/changelog";
784 my $gotclogp = parsechangelog("-l$clogf");
785 my $got_vsn = getfield $gotclogp, 'Version';
786 print DEBUG "SKEW CHECK GOT $got_vsn\n";
787 if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
788 print STDERR <<END or die $!;
790 Warning: archive skew detected. Using the available version:
791 Archive allegedly contains $skew_warning_vsn
792 We were able to obtain only $got_vsn
797 if ($lastpush_hash ne $hash) {
798 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
802 dryrun_report @upd_cmd;
810 canonicalise_suite();
811 badusage "dry run makes no sense with clone" if $dryrun;
812 mkdir $dstdir or die "$dstdir $!";
813 chdir "$dstdir" or die "$dstdir $!";
814 runcmd @git, qw(init -q);
815 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
816 open H, "> .git/HEAD" or die $!;
817 print H "ref: ".lref()."\n" or die $!;
819 runcmd @git, qw(remote add), 'origin', access_giturl();
820 if (check_for_git()) {
821 print "fetching existing git history\n";
823 runcmd_ordryrun @git, qw(fetch origin);
825 print "starting new git history\n";
827 fetch_from_archive() or no_such_package;
828 runcmd @git, qw(reset --hard), lrref();
829 printdone "ready for work in $dstdir";
833 if (check_for_git()) {
836 fetch_from_archive() or no_such_package();
837 printdone "fetched into ".lrref();
842 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
844 printdone "fetched to ".lrref()." and merged into HEAD";
847 sub check_not_dirty () {
848 return if $ignoredirty;
849 my @cmd = (@git, qw(diff --quiet HEAD));
850 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
851 $!=0; $?=0; system @cmd;
852 return if !$! && !$?;
853 if (!$! && $?==256) {
854 fail "working tree is dirty (does not match HEAD)";
860 sub commit_quilty_patch () {
861 my $output = cmdoutput @git, qw(status --porcelain);
864 foreach my $l (split /\n/, $output) {
865 next unless $l =~ m/\S/;
866 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
869 print STDERR "git status: $l\n";
873 fail "unexpected output from git status (is tree clean?)" if $bad;
875 print "nothing quilty to commit, ok.\n";
878 runcmd_ordryrun @git, qw(add), sort keys %adds;
879 my $m = "Commit Debian 3.0 (quilt) metadata";
881 runcmd_ordryrun @git, qw(commit -m), $m;
886 return 0 unless $format eq '3.0 (quilt)';
887 print "Format \`$format', urgh\n";
889 print "Not doing any fixup of \`$format' due to --no-quilt-fixup";
896 print DEBUG "actually entering push\n";
897 my $clogp = parsechangelog();
898 $package = getfield $clogp, 'Source';
899 my $cversion = getfield $clogp, 'Version';
900 my $dscfn = dscfn($cversion);
902 fail "looked for .dsc $dscfn, but $!;".
903 " maybe you forgot to build";
904 $dsc = parsecontrol("../$dscfn","$dscfn");
905 my $dscpackage = getfield $dsc, 'Source';
906 my $format = getfield $dsc, 'Format';
907 my $dversion = getfield $dsc, 'Version';
908 ($dscpackage eq $package && $dversion eq $cversion) or
909 fail "$dsc is for $dscpackage $dversion".
910 " but debian/changelog is for $package $cversion";
911 print DEBUG "format $format\n";
912 if (madformat($format)) {
913 commit_quilty_patch();
918 print "checking that $dscfn corresponds to HEAD\n";
919 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
920 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
921 chdir '../../../..' or die $!;
922 printcmd \*DEBUG,"+",@_;
923 my @diffcmd = (@git, qw(diff --exit-code), $tree);
925 if (system @diffcmd) {
927 fail "$dscfn specifies a different tree to your HEAD commit;".
928 " perhaps you forgot to build";
934 #do fast forward check and maybe fake merge
935 # if (!is_fast_fwd(mainbranch
936 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
937 # map { lref($_).":".rref($_) }
939 my $head = rev_parse('HEAD');
940 $dsc->{$ourdscfield[0]} = $head;
941 $dsc->save("../$dscfn.tmp") or die $!;
943 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
945 $changesfile = $multi;
947 $!==&ENOENT or die "$multi: $!";
948 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
949 my @cs = glob "../$pat";
950 fail "failed to find unique changes file".
951 " (looked for $pat in .., or $multi);".
952 " perhaps you need to use dgit -C"
954 ($changesfile) = @cs;
957 my $changes = parsecontrol($changesfile,$changesfile);
958 foreach my $field (qw(Source Distribution Version)) {
959 $changes->{$field} eq $clogp->{$field} or
960 fail "changes field $field \`$changes->{$field}'".
961 " does not match changelog \`$clogp->{$field}'";
963 my $tag = debiantag($dversion);
964 runcmd @git, qw(check-ref-format), $tag;
965 if (!check_for_git()) {
966 create_remote_git_repo();
968 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
969 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
971 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
973 print "[new .dsc left in $dscfn.tmp]\n";
976 if (!defined $keyid) {
977 $keyid = access_cfg('keyid','RETURN-UNDEF');
979 my @tag_cmd = (@git, qw(tag -a -m),
980 "$package release $dversion for $csuite [dgit]");
982 runcmd_ordryrun @tag_cmd;
985 push @tag_cmd, qw(-u),$keyid if defined $keyid;
986 push @tag_cmd, qw(-u),$keyid if defined $keyid;
988 my @debsign_cmd = @debsign;
989 push @debsign_cmd, "-k$keyid" if defined $keyid;
990 push @debsign_cmd, $changesfile;
991 runcmd_ordryrun @debsign_cmd;
993 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
994 my $host = access_cfg('upload-host','RETURN-UNDEF');
995 my @hostarg = defined($host) ? ($host,) : ();
996 runcmd_ordryrun @dput, @hostarg, $changesfile;
997 printdone "pushed and uploaded $dversion";
1003 badusage "-p is not allowed with clone; specify as argument instead"
1004 if defined $package;
1007 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1008 ($package,$isuite) = @ARGV;
1009 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1010 ($package,$dstdir) = @ARGV;
1011 } elsif (@ARGV==3) {
1012 ($package,$isuite,$dstdir) = @ARGV;
1014 badusage "incorrect arguments to dgit clone";
1016 $dstdir ||= "$package";
1020 sub branchsuite () {
1021 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1022 if ($branch =~ m#$lbranch_re#o) {
1029 sub fetchpullargs () {
1030 if (!defined $package) {
1031 my $sourcep = parsecontrol('debian/control','debian/control');
1032 $package = getfield $sourcep, 'Source';
1035 # $isuite = branchsuite(); # this doesn't work because dak hates canons
1037 my $clogp = parsechangelog();
1038 $isuite = getfield $clogp, 'Distribution';
1040 canonicalise_suite();
1041 print "fetching from suite $csuite\n";
1042 } elsif (@ARGV==1) {
1044 canonicalise_suite();
1046 badusage "incorrect arguments to dgit fetch or dgit pull";
1064 badusage "-p is not allowed with dgit push" if defined $package;
1066 my $clogp = parsechangelog();
1067 $package = getfield $clogp, 'Source';
1069 $isuite = getfield $clogp, 'Distribution';
1071 local ($package) = $existing_package; # this is a hack
1072 canonicalise_suite();
1075 badusage "incorrect arguments to dgit push";
1077 if (check_for_git()) {
1080 if (fetch_from_archive()) {
1081 is_fast_fwd(lrref(), 'HEAD') or
1082 fail "dgit push: HEAD is not a descendant".
1083 " of the archive's version.\n".
1084 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1087 fail "package appears to be new in this suite;".
1088 " if this is intentional, use --new";
1097 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1099 sub build_maybe_quilt_fixup () {
1100 if (!open F, "debian/source/format") {
1101 die $! unless $!==&ENOENT;
1105 F->error and die $!;
1107 return unless madformat($_);
1109 my $clogp = parsechangelog();
1110 my $version = getfield $clogp, 'Version';
1111 my $author = getfield $clogp, 'Maintainer';
1112 my $headref = rev_parse('HEAD');
1115 my $patchname = "auto-$version-$headref-$time";
1116 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1118 my $descfn = ".git/dgit/quilt-description.tmp";
1119 open O, '>', $descfn or die "$descfn: $!";
1121 $msg =~ s/^\s+$/ ./mg;
1122 print O <<END or die $!;
1123 Description: Automatically generated patch ($clogp->{Version})
1124 Last (up to) $ncommits git changes, FYI:
1134 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1135 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1136 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1137 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1140 if (!open P, '>>', ".pc/applied-patches") {
1141 $!==&ENOENT or die $!;
1146 commit_quilty_patch();
1149 sub quilt_fixup_editor () {
1150 my $descfn = $ENV{$fakeeditorenv};
1151 my $editing = $ARGV[$#ARGV];
1152 open I1, '<', $descfn or die "$descfn: $!";
1153 open I2, '<', $editing or die "$editing: $!";
1154 unlink $editing or die "$editing: $!";
1155 open O, '>', $editing or die "$editing: $!";
1156 while (<I1>) { print O or die $!; } I1->error and die $!;
1159 $copying ||= m/^\-\-\- /;
1160 next unless $copying;
1163 I2->error and die $!;
1169 badusage "-p is not allowed when building" if defined $package;
1171 my $clogp = parsechangelog();
1172 $isuite = getfield $clogp, 'Distribution';
1173 $package = getfield $clogp, 'Source';
1174 $version = getfield $clogp, 'Version';
1175 build_maybe_quilt_fixup();
1179 badusage "dgit build implies --clean=dpkg-source"
1180 if $cleanmode ne 'dpkg-source';
1182 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1183 printdone "build successful\n";
1187 badusage "dgit git-build implies --clean=dpkg-source"
1188 if $cleanmode ne 'dpkg-source';
1191 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1192 "--git-builder=@dpkgbuildpackage");
1193 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1194 canonicalise_suite();
1195 push @cmd, "--git-debian-branch=".lbranch();
1197 push @cmd, changesopts();
1198 runcmd_ordryrun @cmd, @ARGV;
1199 printdone "build successful\n";
1204 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1205 $dscfn = dscfn($version);
1206 if ($cleanmode eq 'dpkg-source') {
1207 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1209 if ($cleanmode eq 'git') {
1210 runcmd_ordryrun @git, qw(clean -xdf);
1211 } elsif ($cleanmode eq 'none') {
1215 my $pwd = cmdoutput qw(env - pwd);
1216 my $leafdir = basename $pwd;
1217 chdir ".." or die $!;
1218 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1219 chdir $pwd or die $!;
1220 runcmd_ordryrun qw(sh -ec),
1221 'exec >$1; shift; exec "$@"','x',
1222 "../$sourcechanges",
1223 @dpkggenchanges, qw(-S), changesopts();
1227 sub cmd_build_source {
1228 badusage "build-source takes no additional arguments" if @ARGV;
1230 printdone "source built, results in $dscfn and $sourcechanges";
1235 chdir ".." or die $!;
1236 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1238 stat $dscfn or fail "$dscfn (in parent directory): $!";
1239 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1240 foreach my $cf (glob $pat) {
1241 next if $cf eq $sourcechanges;
1242 unlink $cf or fail "remove $cf: $!";
1245 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1246 runcmd_ordryrun @mergechanges, glob $pat;
1247 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1249 stat $multichanges or fail "$multichanges: $!";
1251 printdone "build successful, results in $multichanges\n" or die $!;
1254 sub cmd_quilt_fixup {
1255 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1256 my $clogp = parsechangelog();
1257 $version = getfield $clogp, 'Version';
1258 build_maybe_quilt_fixup();
1262 print "dgit version $our_version\n" or die $!;
1269 last unless $ARGV[0] =~ m/^-/;
1273 if (m/^--dry-run$/) {
1275 } elsif (m/^--no-sign$/) {
1277 } elsif (m/^--help$/) {
1279 } elsif (m/^--version$/) {
1281 } elsif (m/^--new$/) {
1283 } elsif (m/^--(\w+)=(.*)/s &&
1284 ($om = $opts_opt_map{$1}) &&
1287 } elsif (m/^--(\w+):(.*)/s &&
1288 ($om = $opts_opt_map{$1})) {
1290 } elsif (m/^--existing-package=(.*)/s) {
1291 $existing_package = $1;
1292 } elsif (m/^--distro=(.*)/s) {
1294 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1296 } elsif (m/^--clean=(.*)$/s) {
1297 badusage "unknown cleaning mode \`$1'";
1298 } elsif (m/^--ignore-dirty$/s) {
1300 } elsif (m/^--no-quilt-fixup$/s) {
1303 badusage "unknown long option \`$_'";
1309 } elsif (s/^-h/-/) {
1311 } elsif (s/^-D/-/) {
1312 open DEBUG, ">&STDERR" or die $!;
1314 } elsif (s/^-N/-/) {
1316 } elsif (m/^-[vm]/) {
1317 push @changesopts, $_;
1319 } elsif (s/^-c(.*=.*)//s) {
1320 push @git, '-c', $1;
1321 } elsif (s/^-d(.*)//s) {
1323 } elsif (s/^-C(.*)//s) {
1325 } elsif (s/^-k(.*)//s) {
1327 } elsif (s/^-wn//s) {
1328 $cleanmode = 'none';
1329 } elsif (s/^-wg//s) {
1331 } elsif (s/^-wd//s) {
1332 $cleanmode = 'dpkg-source';
1334 badusage "unknown short option \`$_'";
1341 if ($ENV{$fakeeditorenv}) {
1342 quilt_fixup_editor();
1345 delete $ENV{'DGET_UNPACK'};
1348 print STDERR "DRY RUN ONLY\n" if $dryrun;
1350 print STDERR $helpmsg or die $!;
1353 my $cmd = shift @ARGV;
1355 { no strict qw(refs); &{"cmd_$cmd"}(); }