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);
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 {
212 our $helpmsg = <<END;
214 dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
215 dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
216 dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
217 dgit [dgit-opts] push [dgit-opts] [suite]
218 important dgit options:
219 -k<keyid> sign tag and package with <keyid> instead of default
220 --dry-run -n do not change anything, but go through the motions
221 --new -N allow introducing a new package
222 --debug -D increase debug level
223 -c<name>=<value> set git config option (used directly by dgit too)
226 our $later_warning_msg = <<END;
227 Perhaps the upload is stuck in incoming. Using the version from git.
231 print STDERR "$us: @_\n", $helpmsg or die $!;
236 print $helpmsg or die $!;
240 our %defcfg = ('dgit.default.distro' => 'debian',
241 'dgit.default.username' => '',
242 'dgit.default.archive-query-default-component' => 'main',
243 'dgit.default.ssh' => 'ssh',
244 'dgit-distro.debian.git-host' => 'git.debian.org',
245 'dgit-distro.debian.git-proto' => 'git+ssh://',
246 'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
247 'dgit-distro.debian.git-check' => 'ssh-cmd',
248 'dgit-distro.debian.git-create' => 'ssh-cmd',
249 'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
250 'dgit-distro.debian.sshdakls-dir' =>
251 '/srv/ftp-master.debian.org/ftp/dists',
252 'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
253 'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
257 return undef if $c =~ /RETURN-UNDEF/;
258 my @cmd = (@git, qw(config --), $c);
261 local ($debug) = $debug-1;
262 $v = cmdoutput_errok @cmd;
269 my $dv = $defcfg{$c};
270 return $dv if defined $dv;
272 badcfg "need value for one of: @_";
275 sub access_distro () {
276 return cfg("dgit-suite.$isuite.distro",
277 "dgit.default.distro");
282 my $distro = $idistro || access_distro();
283 my $value = cfg(map { ("dgit-distro.$distro.$_",
284 "dgit.default.$_") } @keys);
288 sub access_someuserhost ($) {
290 my $user = access_cfg("$some-user",'username');
291 my $host = access_cfg("$some-host");
292 return length($user) ? "$user\@$host" : $host;
295 sub access_gituserhost () {
296 return access_someuserhost('git');
299 sub access_giturl () {
300 my $url = access_cfg('git-url','RETURN-UNDEF');
303 access_cfg('git-proto').
304 access_gituserhost().
305 access_cfg('git-path');
307 return "$url/$package.git";
310 sub parsecontrolfh ($$@) {
311 my ($fh, $desc, @opts) = @_;
312 my %opts = ('name' => $desc, @opts);
313 my $c = Dpkg::Control::Hash->new(%opts);
314 $c->parse($fh) or die "parsing of $desc failed";
319 my ($file, $desc) = @_;
320 my $fh = new IO::Handle;
321 open $fh, '<', $file or die "$file: $!";
322 my $c = parsecontrolfh($fh,$desc);
323 $fh->error and die $!;
329 my ($dctrl,$field) = @_;
330 my $v = $dctrl->{$field};
331 return $v if defined $v;
332 fail "missing field $field in ".$v->get_option('name');
336 my $c = Dpkg::Control::Hash->new();
337 my $p = new IO::Handle;
338 my @cmd = (qw(dpkg-parsechangelog));
339 open $p, '-|', @cmd or die $!;
341 $?=0; $!=0; close $p or failedcmd @cmd;
347 sub archive_query ($) {
349 my $query = access_cfg('archive-query','RETURN-UNDEF');
350 if (!defined $query) {
351 my $distro = access_distro();
352 if ($distro eq 'debian') {
353 $query = "sshdakls:".
354 access_someuserhost('sshdakls').':'.
355 access_cfg('sshdakls-dir');
357 $query = "madison:$distro";
360 $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
363 { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
366 sub archive_query_madison ($$) {
367 my ($proto,$data) = @_;
368 die unless $proto eq 'madison';
369 $rmad{$package} ||= cmdoutput
370 qw(rmadison -asource),"-s$isuite","-u$data",$package;
371 my $rmad = $rmad{$package};
372 return madison_parse($rmad);
375 sub archive_query_sshdakls ($$) {
376 my ($proto,$data) = @_;
377 $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
378 my $dakls = cmdoutput
379 access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
380 return madison_parse($dakls);
383 sub canonicalise_suite_sshdakls ($$) {
384 my ($proto,$data) = @_;
385 $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
387 (access_cfg('ssh'), $`,
389 " if test -h $isuite; then readlink $isuite; exit 0; fi;".
390 " if test -d $isuite; then echo $isuite; exit 0; fi;".
392 my $dakls = cmdoutput @cmd;
393 failedcmd @cmd unless $dakls =~ m/^\w/;
397 sub madison_parse ($) {
400 foreach my $l (split /\n/, $rmad) {
401 $l =~ m{^ \s*( [^ \t|]+ )\s* \|
402 \s*( [^ \t|]+ )\s* \|
403 \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
404 \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
405 $1 eq $package or die "$rmad $package ?";
412 $component = access_cfg('archive-query-default-component');
414 $5 eq 'source' or die "$rmad ?";
415 my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
416 my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
417 push @out, [$vsn,$subpath,$newsuite];
419 return sort { -version_compare_string($a->[0],$b->[0]); } @out;
422 sub canonicalise_suite_madison ($$) {
423 my @r = archive_query_madison($_[0],$_[1]);
425 "unable to canonicalise suite using package $package".
426 " which does not appear to exist in suite $isuite;".
427 " --existing-package may help";
431 sub canonicalise_suite () {
432 return if defined $csuite;
433 fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
434 $csuite = archive_query('canonicalise_suite');
435 if ($isuite ne $csuite) {
436 # madison canonicalises for us
437 print "canonical suite name for $isuite is $csuite\n";
441 sub get_archive_dsc () {
442 canonicalise_suite();
443 my @vsns = archive_query('archive_query');
444 foreach my $vinfo (@vsns) {
445 my ($vsn,$subpath) = @$vinfo;
446 $dscurl = access_cfg('mirror').$subpath;
447 $dscdata = url_get($dscurl);
448 next unless defined $dscdata;
449 my $dscfh = new IO::File \$dscdata, '<' or die $!;
450 print DEBUG Dumper($dscdata) if $debug>1;
451 $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
452 print DEBUG Dumper($dsc) if $debug>1;
453 my $fmt = getfield $dsc, 'Format';
454 fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
460 sub check_for_git () {
462 my $how = access_cfg('git-check');
463 if ($how eq 'ssh-cmd') {
465 (access_cfg('ssh'),access_gituserhost(),
466 " set -e; cd ".access_cfg('git-path').";".
467 " if test -d $package.git; then echo 1; else echo 0; fi");
468 my $r= cmdoutput @cmd;
469 failedcmd @cmd unless $r =~ m/^[01]$/;
472 badcfg "unknown git-check \`$how'";
476 sub create_remote_git_repo () {
477 my $how = access_cfg('git-create');
478 if ($how eq 'ssh-cmd') {
480 (access_cfg('ssh'),access_gituserhost(),
481 "set -e; cd ".access_cfg('git-path').";".
482 " cp -a _template $package.git");
484 badcfg "unknown git-create \`$how'";
488 our ($dsc_hash,$upload_hash);
490 our $ud = '.git/dgit/unpack';
498 sub mktree_in_ud_from_only_subdir () {
499 # changes into the subdir
502 $dirs[0] =~ m#^([^/]+)/\.$# or die;
504 chdir $dir or die "$dir $!";
505 fail "source package contains .git directory" if stat '.git';
506 die $! unless $!==&ENOENT;
507 runcmd qw(git init -q);
508 rmtree('.git/objects');
509 symlink '../../../../objects','.git/objects' or die $!;
510 runcmd @git, qw(add -Af);
511 my $tree = cmdoutput @git, qw(write-tree);
512 $tree =~ m/^\w+$/ or die "$tree ?";
516 sub dsc_files_info () {
517 foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
518 ['Checksums-Sha1', 'Digest::SHA', 'new(1)'],
519 ['Files', 'Digest::MD5', 'new()']) {
520 my ($fname, $module, $method) = @$csumi;
521 my $field = $dsc->{$fname};
522 next unless defined $field;
523 eval "use $module; 1;" or die $@;
525 foreach (split /\n/, $field) {
527 m/^(\w+) (\d+) (\S+)$/ or
528 fail "could not parse .dsc $fname line \`$_'";
529 my $digester = eval "$module"."->$method;" or die $@;
534 Digester => $digester,
539 fail "missing any supported Checksums-* or Files field in ".
540 $dsc->get_option('name');
544 map { $_->{Filename} } dsc_files_info();
547 sub is_orig_file ($) {
549 m/\.orig(?:-\w+)?\.tar\.\w+$/;
552 sub make_commit ($) {
554 return cmdoutput @git, qw(hash-object -w -t commit), $file;
557 sub generate_commit_from_dsc () {
561 foreach my $f (dsc_files()) {
562 die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
564 link "../../../$f", $f
568 runcmd @dget, qw(--), $dscurl;
569 foreach my $f (grep { is_orig_file($_) } @files) {
570 link $f, "../../../../$f"
574 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
575 runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
576 my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
577 my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
578 my $author = getfield $clogp, 'Maintainer';
579 $author =~ s#,.*##ms;
580 my $authline = "$author $date";
581 $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
582 fail "unexpected commit author line format \`$authline'".
583 " (was generated from changelog Maintainer field)";
584 my $changes = getfield $clogp, 'Changes';
585 open C, ">../commit.tmp" or die $!;
586 print C <<END or die $!;
593 # imported from the archive
596 my $outputhash = make_commit qw(../commit.tmp);
597 my $cversion = getfield $clogp, 'Version';
598 print "synthesised git commit from .dsc $cversion\n";
600 runcmd @git, qw(reset --hard), $upload_hash;
601 runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
602 my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
603 my $oversion = getfield $oldclogp, 'Version';
605 version_compare_string($oversion, $cversion);
607 # git upload/ is earlier vsn than archive, use archive
608 open C, ">../commit2.tmp" or die $!;
609 print C <<END or die $!;
616 Record $package ($cversion) in archive suite $csuite
618 $outputhash = make_commit qw(../commit2.tmp);
619 } elsif ($vcmp > 0) {
620 print STDERR <<END or die $!;
622 Version actually in archive: $cversion (older)
623 Last allegedly pushed/uploaded: $oversion (newer or same)
626 $outputhash = $upload_hash;
628 $outputhash = $upload_hash;
631 chdir '../../../..' or die $!;
632 runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
633 'DGIT_ARCHIVE', $outputhash;
634 cmdoutput @git, qw(log -n2), $outputhash;
635 # ... gives git a chance to complain if our commit is malformed
640 sub ensure_we_have_orig () {
641 foreach my $fi (dsc_files_info()) {
642 my $f = $fi->{Filename};
643 next unless is_orig_file($f);
644 if (open F, "<", "../$f") {
645 $fi->{Digester}->reset();
646 $fi->{Digester}->addfile(*F);
648 my $got = $fi->{Digester}->hexdigest();
649 $got eq $fi->{Hash} or
650 fail "existing file $f has hash $got but .dsc".
651 " demands hash $fi->{Hash}".
652 " (perhaps you should delete this file?)";
653 print "using existing $f\n";
656 die "$f $!" unless $!==&ENOENT;
658 my $origurl = $dscurl;
659 $origurl =~ s{/[^/]+$}{};
661 die "$f ?" unless $f =~ m/^${package}_/;
662 die "$f ?" if $f =~ m#/#;
663 runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
669 return cmdoutput @git, qw(rev-parse), "$_[0]~0";
672 sub is_fast_fwd ($$) {
673 my ($ancestor,$child) = @_;
674 my @cmd = (@git, qw(merge-base), $ancestor, $child);
675 my $mb = cmdoutput_errok @cmd;
677 return rev_parse($mb) eq rev_parse($ancestor);
679 $?==256 or failedcmd @cmd;
684 sub git_fetch_us () {
685 runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
688 sub fetch_from_archive () {
689 # ensures that lrref() is what is actually in the archive,
691 get_archive_dsc() or return 0;
692 foreach my $field (@ourdscfield) {
693 $dsc_hash = $dsc->{$field};
694 last if defined $dsc_hash;
696 if (defined $dsc_hash) {
697 $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
699 print "last upload to archive specified git hash\n";
701 print "last upload to archive has NO git hash\n";
704 my $lrref_fn = ".git/".lrref();
705 if (open H, $lrref_fn) {
708 die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
709 } elsif ($! == &ENOENT) {
714 print DEBUG "previous reference hash=$upload_hash\n";
716 if (defined $dsc_hash) {
717 fail "missing git history even though dsc has hash -".
718 " could not find commit $dsc_hash".
719 " (should be in ".access_giturl()."#".rrref().")"
722 ensure_we_have_orig();
723 if ($dsc_hash eq $upload_hash) {
724 } elsif (is_fast_fwd($dsc_hash,$upload_hash)) {
725 print STDERR <<END or die $!;
727 Git commit in archive is behind the last version allegedly pushed/uploaded.
728 Commit referred to by archive: $dsc_hash
729 Last allegedly pushed/uploaded: $upload_hash
732 $hash = $upload_hash;
734 fail "archive's .dsc refers to ".$dsc_hash.
735 " but this is an ancestor of ".$upload_hash;
738 $hash = generate_commit_from_dsc();
740 print DEBUG "current hash=$hash\n";
742 fail "not fast forward on last upload branch!".
743 " (archive's version left in DGIT_ARCHIVE)"
744 unless is_fast_fwd($upload_hash, $hash);
746 if ($upload_hash ne $hash) {
747 my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
751 dryrun_report @upd_cmd;
759 canonicalise_suite();
760 badusage "dry run makes no sense with clone" if $dryrun;
761 mkdir $dstdir or die "$dstdir $!";
762 chdir "$dstdir" or die "$dstdir $!";
763 runcmd @git, qw(init -q);
764 runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
765 open H, "> .git/HEAD" or die $!;
766 print H "ref: ".lref()."\n" or die $!;
768 runcmd @git, qw(remote add), 'origin', access_giturl();
769 if (check_for_git()) {
770 print "fetching existing git history\n";
772 runcmd_ordryrun @git, qw(fetch origin);
774 print "starting new git history\n";
776 fetch_from_archive() or no_such_package;
777 runcmd @git, qw(reset --hard), lrref();
778 printdone "ready for work in $dstdir";
782 if (check_for_git()) {
785 fetch_from_archive() or no_such_package();
786 printdone "fetched into ".lrref();
791 runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
793 printdone "fetched to ".lrref()." and merged into HEAD";
796 sub check_not_dirty () {
797 return if $ignoredirty;
798 my @cmd = (@git, qw(diff --quiet HEAD));
799 printcmd(\*DEBUG,"+",@cmd) if $debug>0;
800 $!=0; $?=0; system @cmd;
801 return if !$! && !$?;
802 if (!$! && $?==256) {
803 fail "working tree is dirty (does not match HEAD)";
809 sub commit_quilty_patch () {
810 my $output = cmdoutput @git, qw(status --porcelain);
813 foreach my $l (split /\n/, $output) {
814 next unless $l =~ m/\S/;
815 if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
818 print STDERR "git status: $l\n";
822 fail "unexpected output from git status (is tree clean?)" if $bad;
824 print "nothing quilty to commit, ok.\n";
827 runcmd_ordryrun @git, qw(add), sort keys %adds;
828 my $m = "Commit Debian 3.0 (quilt) metadata";
830 runcmd_ordryrun @git, qw(commit -m), $m;
835 return 0 unless $format eq '3.0 (quilt)';
836 print "Format \`$format', urgh\n";
838 print "Not doing any fixup of \`$format' due to --no-quilt-fixup";
845 print DEBUG "actually entering push\n";
846 my $clogp = parsechangelog();
847 $package = getfield $clogp, 'Source';
848 my $cversion = getfield $clogp, 'Version';
849 my $dscfn = dscfn($cversion);
851 fail "looked for .dsc $dscfn, but $!;".
852 " maybe you forgot to build";
853 $dsc = parsecontrol("../$dscfn","$dscfn");
854 my $dscpackage = getfield $dsc, 'Source';
855 my $format = getfield $dsc, 'Format';
856 my $dversion = getfield $dsc, 'Version';
857 ($dscpackage eq $package && $dversion eq $cversion) or
858 fail "$dsc is for $dscpackage $dversion".
859 " but debian/changelog is for $package $cversion";
860 print DEBUG "format $format\n";
861 if (madformat($format)) {
862 commit_quilty_patch();
867 print "checking that $dscfn corresponds to HEAD\n";
868 runcmd qw(dpkg-source -x --), "../../../../$dscfn";
869 my ($tree,$dir) = mktree_in_ud_from_only_subdir();
870 chdir '../../../..' or die $!;
871 printcmd \*DEBUG,"+",@_;
872 my @diffcmd = (@git, qw(diff --exit-code), $tree);
874 if (system @diffcmd) {
876 fail "$dscfn specifies a different tree to your HEAD commit;".
877 " perhaps you forgot to build";
883 #do fast forward check and maybe fake merge
884 # if (!is_fast_fwd(mainbranch
885 # runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
886 # map { lref($_).":".rref($_) }
888 $dsc->{$ourdscfield[0]} = rev_parse('HEAD');
889 $dsc->save("../$dscfn.tmp") or die $!;
891 my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
893 $changesfile = $multi;
895 $!==&ENOENT or die "$multi: $!";
896 my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
897 my @cs = glob "../$pat";
898 fail "failed to find unique changes file".
899 " (looked for $pat in .., or $multi);".
900 " perhaps you need to use dgit -C"
902 ($changesfile) = @cs;
905 my $changes = parsecontrol($changesfile,$changesfile);
906 foreach my $field (qw(Source Distribution Version)) {
907 $changes->{$field} eq $clogp->{$field} or
908 fail "changes field $field \`$changes->{$field}'".
909 " does not match changelog \`$clogp->{$field}'";
911 my $tag = debiantag($dversion);
912 if (!check_for_git()) {
913 create_remote_git_repo();
915 runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
916 runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
918 rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
920 print "[new .dsc left in $dscfn.tmp]\n";
923 if (!defined $keyid) {
924 $keyid = access_cfg('keyid','RETURN-UNDEF');
926 my @tag_cmd = (@git, qw(tag -s -m),
927 "$package release $dversion for $csuite [dgit]");
928 push @tag_cmd, qw(-u),$keyid if defined $keyid;
930 runcmd_ordryrun @tag_cmd;
931 my @debsign_cmd = @debsign;
932 push @debsign_cmd, "-k$keyid" if defined $keyid;
933 push @debsign_cmd, $changesfile;
934 runcmd_ordryrun @debsign_cmd;
936 runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
937 my $host = access_cfg('upload-host','RETURN-UNDEF');
938 my @hostarg = defined($host) ? ($host,) : ();
939 runcmd_ordryrun @dput, @hostarg, $changesfile;
940 printdone "pushed and uploaded $dversion";
946 badusage "-p is not allowed with clone; specify as argument instead"
950 } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
951 ($package,$isuite) = @ARGV;
952 } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
953 ($package,$dstdir) = @ARGV;
955 ($package,$isuite,$dstdir) = @ARGV;
957 badusage "incorrect arguments to dgit clone";
959 $dstdir ||= "$package";
964 my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
965 if ($branch =~ m#$lbranch_re#o) {
972 sub fetchpullargs () {
973 if (!defined $package) {
974 my $sourcep = parsecontrol('debian/control','debian/control');
975 $package = getfield $sourcep, 'Source';
978 # $isuite = branchsuite(); # this doesn't work because dak hates canons
980 my $clogp = parsechangelog();
981 $isuite = getfield $clogp, 'Distribution';
983 canonicalise_suite();
984 print "fetching from suite $csuite\n";
987 canonicalise_suite();
989 badusage "incorrect arguments to dgit fetch or dgit pull";
1007 badusage "-p is not allowed with dgit push" if defined $package;
1009 my $clogp = parsechangelog();
1010 $package = getfield $clogp, 'Source';
1012 $isuite = getfield $clogp, 'Distribution';
1014 local ($package) = $existing_package; # this is a hack
1015 canonicalise_suite();
1018 badusage "incorrect arguments to dgit push";
1020 if (check_for_git()) {
1023 if (fetch_from_archive()) {
1024 is_fast_fwd(lrref(), 'HEAD') or
1025 fail "dgit push: HEAD is not a descendant".
1026 " of the archive's version.\n".
1027 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1030 fail "package appears to be new in this suite;".
1031 " if this is intentional, use --new";
1040 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1042 sub build_maybe_quilt_fixup () {
1043 if (!open F, "debian/source/format") {
1044 die $! unless $!==&ENOENT;
1048 F->error and die $!;
1050 return unless madformat($_);
1052 my $clogp = parsechangelog();
1053 my $version = getfield $clogp, 'Version';
1054 my $author = getfield $clogp, 'Maintainer';
1055 my $headref = rev_parse('HEAD');
1058 my $patchname = "auto-$version-$headref-$time";
1059 my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1061 my $descfn = ".git/dgit/quilt-description.tmp";
1062 open O, '>', $descfn or die "$descfn: $!";
1064 $msg =~ s/^\s+$/ ./mg;
1065 print O <<END or die $!;
1066 Description: Automatically generated patch ($clogp->{Version})
1067 Last (up to) $ncommits git changes, FYI:
1077 local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1078 local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1079 local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1080 runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1083 if (!open P, '>>', ".pc/applied-patches") {
1084 $!==&ENOENT or die $!;
1089 commit_quilty_patch();
1092 sub quilt_fixup_editor () {
1093 my $descfn = $ENV{$fakeeditorenv};
1094 my $editing = $ARGV[$#ARGV];
1095 open I1, '<', $descfn or die "$descfn: $!";
1096 open I2, '<', $editing or die "$editing: $!";
1097 unlink $editing or die "$editing: $!";
1098 open O, '>', $editing or die "$editing: $!";
1099 while (<I1>) { print O or die $!; } I1->error and die $!;
1102 $copying ||= m/^\-\-\- /;
1103 next unless $copying;
1106 I2->error and die $!;
1112 badusage "-p is not allowed when building" if defined $package;
1114 my $clogp = parsechangelog();
1115 $isuite = getfield $clogp, 'Distribution';
1116 $package = getfield $clogp, 'Source';
1117 $version = getfield $clogp, 'Version';
1118 build_maybe_quilt_fixup();
1122 badusage "dgit build implies --clean=dpkg-source"
1123 if $cleanmode ne 'dpkg-source';
1125 runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1126 printdone "build successful\n";
1130 badusage "dgit git-build implies --clean=dpkg-source"
1131 if $cleanmode ne 'dpkg-source';
1134 (qw(git-buildpackage -us -uc --git-no-sign-tags),
1135 "--git-builder=@dpkgbuildpackage");
1136 unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1137 canonicalise_suite();
1138 push @cmd, "--git-debian-branch=".lbranch();
1140 push @cmd, changesopts();
1141 runcmd_ordryrun @cmd, @ARGV;
1142 printdone "build successful\n";
1147 $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1148 $dscfn = dscfn($version);
1149 if ($cleanmode eq 'dpkg-source') {
1150 runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1152 if ($cleanmode eq 'git') {
1153 runcmd_ordryrun @git, qw(clean -xdf);
1154 } elsif ($cleanmode eq 'none') {
1158 my $pwd = cmdoutput qw(env - pwd);
1159 my $leafdir = basename $pwd;
1160 chdir ".." or die $!;
1161 runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1162 chdir $pwd or die $!;
1163 runcmd_ordryrun qw(sh -ec),
1164 'exec >$1; shift; exec "$@"','x',
1165 "../$sourcechanges",
1166 @dpkggenchanges, qw(-S), changesopts();
1170 sub cmd_build_source {
1171 badusage "build-source takes no additional arguments" if @ARGV;
1173 printdone "source built, results in $dscfn and $sourcechanges";
1178 chdir ".." or die $!;
1179 my $pat = "${package}_".(stripepoch $version)."_*.changes";
1181 stat $dscfn or fail "$dscfn (in parent directory): $!";
1182 stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1183 foreach my $cf (glob $pat) {
1184 next if $cf eq $sourcechanges;
1185 unlink $cf or fail "remove $cf: $!";
1188 runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1189 runcmd_ordryrun @mergechanges, glob $pat;
1190 my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1192 stat $multichanges or fail "$multichanges: $!";
1194 printdone "build successful, results in $multichanges\n" or die $!;
1197 sub cmd_quilt_fixup {
1198 badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1199 my $clogp = parsechangelog();
1200 $version = getfield $clogp, 'Version';
1201 build_maybe_quilt_fixup();
1205 print "dgit version $our_version\n" or die $!;
1212 last unless $ARGV[0] =~ m/^-/;
1216 if (m/^--dry-run$/) {
1218 } elsif (m/^--no-sign$/) {
1220 } elsif (m/^--help$/) {
1222 } elsif (m/^--version$/) {
1224 } elsif (m/^--new$/) {
1226 } elsif (m/^--(\w+)=(.*)/s &&
1227 ($om = $opts_opt_map{$1}) &&
1230 } elsif (m/^--(\w+):(.*)/s &&
1231 ($om = $opts_opt_map{$1})) {
1233 } elsif (m/^--existing-package=(.*)/s) {
1234 $existing_package = $1;
1235 } elsif (m/^--distro=(.*)/s) {
1237 } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1239 } elsif (m/^--clean=(.*)$/s) {
1240 badusage "unknown cleaning mode \`$1'";
1241 } elsif (m/^--ignore-dirty$/s) {
1243 } elsif (m/^--no-quilt-fixup$/s) {
1246 badusage "unknown long option \`$_'";
1252 } elsif (s/^-h/-/) {
1254 } elsif (s/^-D/-/) {
1255 open DEBUG, ">&STDERR" or die $!;
1257 } elsif (s/^-N/-/) {
1259 } elsif (m/^-[vm]/) {
1260 push @changesopts, $_;
1262 } elsif (s/^-c(.*=.*)//s) {
1263 push @git, '-c', $1;
1264 } elsif (s/^-d(.*)//s) {
1266 } elsif (s/^-C(.*)//s) {
1268 } elsif (s/^-k(.*)//s) {
1270 } elsif (s/^-wn//s) {
1271 $cleanmode = 'none';
1272 } elsif (s/^-wg//s) {
1274 } elsif (s/^-wd//s) {
1275 $cleanmode = 'dpkg-source';
1277 badusage "unknown short option \`$_'";
1284 if ($ENV{$fakeeditorenv}) {
1285 quilt_fixup_editor();
1288 delete $ENV{'DGET_UNPACK'};
1291 print STDERR "DRY RUN ONLY\n" if $dryrun;
1293 print STDERR $helpmsg or die $!;
1296 my $cmd = shift @ARGV;
1298 { no strict qw(refs); &{"cmd_$cmd"}(); }