chiark / gitweb /
Set autoflush on stdout, to get better ordering of debugging etc. output when stdout...
[dgit.git] / dgit
1 #!/usr/bin/perl -w
2 # dgit
3 # Integration between git and Debian-style archives
4 #
5 # Copyright (C)2013 Ian Jackson
6 #
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.
11 #
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.
16 #
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/>.
19
20 use strict;
21
22 use IO::Handle;
23 use Data::Dumper;
24 use LWP::UserAgent;
25 use Dpkg::Control::Hash;
26 use File::Path;
27 use File::Basename;
28 use Dpkg::Version;
29 use POSIX;
30
31 our $our_version = 'UNRELEASED'; ###substituted###
32
33 our $isuite = 'unstable';
34 our $idistro;
35 our $package;
36
37 our $sign = 1;
38 our $dryrun = 0;
39 our $changesfile;
40 our $new_package = 0;
41 our $ignoredirty = 0;
42 our $noquilt = 0;
43 our $existing_package = 'dpkg';
44 our $cleanmode = 'dpkg-source';
45
46 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
47
48 our (@git) = qw(git);
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) = ('');
58
59 our %opts_opt_map = ('dget' => \@dget,
60                      'dput' => \@dput,
61                      'debsign' => \@debsign,
62                      'sbuild' => \@sbuild,
63                      'dpkg-source' => \@dpkgsource,
64                      'dpkg-buildpackage' => \@dpkgbuildpackage,
65                      'dpkg-genchanges' => \@dpkggenchanges,
66                      'ch' => \@changesopts,
67                      'mergechanges' => \@mergechanges);
68
69 our $keyid;
70
71 our $debug = 0;
72 open DEBUG, ">/dev/null" or die $!;
73
74 autoflush STDOUT 1;
75
76 our $remotename = 'dgit';
77 our @ourdscfield = qw(Dgit Vcs-Dgit-Master);
78 our $branchprefix = 'dgit';
79 our $csuite;
80
81 sub lbranch () { return "$branchprefix/$csuite"; }
82 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
83 sub lref () { return "refs/heads/".lbranch(); }
84 sub lrref () { return "refs/remotes/$remotename/$branchprefix/$csuite"; }
85 sub rrref () { return "refs/$branchprefix/$csuite"; }
86 sub debiantag ($) { 
87     my ($v) = @_;
88     $v =~ y/~:/_%/;
89     return "debian/$v";
90 }
91
92 sub stripepoch ($) {
93     my ($vsn) = @_;
94     $vsn =~ s/^\d+\://;
95     return $vsn;
96 }
97
98 sub dscfn ($) {
99     my ($vsn) = @_;
100     return "${package}_".(stripepoch $vsn).".dsc";
101 }
102
103 sub changesopts () { return @changesopts[1..$#changesopts]; }
104
105 our $us = 'dgit';
106
107 sub fail { die "$us: @_\n"; }
108
109 sub badcfg { print STDERR "$us: invalid configuration: @_\n"; exit 12; }
110
111 sub no_such_package () {
112     print STDERR "$us: package $package does not exist in suite $isuite\n";
113     exit 4;
114 }
115
116 sub fetchspec () {
117     local $csuite = '*';
118     return  "+".rrref().":".lrref();
119 }
120
121 our $ua;
122
123 sub url_get {
124     if (!$ua) {
125         $ua = LWP::UserAgent->new();
126         $ua->env_proxy;
127     }
128     my $what = $_[$#_];
129     print "downloading $what...\n";
130     my $r = $ua->get(@_) or die $!;
131     return undef if $r->code == 404;
132     $r->is_success or fail "failed to fetch $what: ".$r->status_line;
133     return $r->decoded_content();
134 }
135
136 our ($dscdata,$dscurl,$dsc,$skew_warning_vsn);
137
138 sub printcmd {
139     my $fh = shift @_;
140     my $intro = shift @_;
141     print $fh $intro or die $!;
142     local $_;
143     foreach my $a (@_) {
144         $_ = $a;
145         if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
146             print $fh " '$_'" or die $!;
147         } else {
148             print $fh " $_" or die $!;
149         }
150     }
151     print $fh "\n" or die $!;
152 }
153
154 sub failedcmd {
155     { local ($!); printcmd \*STDERR, "$_[0]: failed command:", @_ or die $!; };
156     if ($!) {
157         fail "failed to fork/exec: $!";
158     } elsif (!($? & 0xff)) {
159         fail "subprocess failed with error exit status ".($?>>8);
160     } elsif ($?) {
161         fail "subprocess crashed (wait status $?)";
162     } else {
163         fail "subprocess produced invalid output";
164     }
165 }
166
167 sub runcmd {
168     printcmd(\*DEBUG,"+",@_) if $debug>0;
169     $!=0; $?=0;
170     failedcmd @_ if system @_;
171 }
172
173 sub printdone {
174     if (!$dryrun) {
175         print "dgit ok: @_\n";
176     } else {
177         print "would be ok: @_ (but dry run only)\n";
178     }
179 }
180
181 sub cmdoutput_errok {
182     die Dumper(\@_)." ?" if grep { !defined } @_;
183     printcmd(\*DEBUG,"|",@_) if $debug>0;
184     open P, "-|", @_ or die $!;
185     my $d;
186     $!=0; $?=0;
187     { local $/ = undef; $d = <P>; }
188     die $! if P->error;
189     if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
190     chomp $d;
191     $d =~ m/^.*/;
192     print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
193     return $d;
194 }
195
196 sub cmdoutput {
197     my $d = cmdoutput_errok @_;
198     defined $d or failedcmd @_;
199     return $d;
200 }
201
202 sub dryrun_report {
203     printcmd(\*STDOUT,"#",@_);
204 }
205
206 sub runcmd_ordryrun {
207     if (!$dryrun) {
208         runcmd @_;
209     } else {
210         dryrun_report @_;
211     }
212 }
213
214 sub shell_cmd {
215     my ($first_shell, @cmd) = @_;
216     return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
217 }
218
219 our $helpmsg = <<END;
220 main usages:
221   dgit [dgit-opts] clone [dgit-opts] package [suite] [./dir|/dir]
222   dgit [dgit-opts] fetch|pull [dgit-opts] [suite]
223   dgit [dgit-opts] build [git-buildpackage-opts|dpkg-buildpackage-opts]
224   dgit [dgit-opts] push [dgit-opts] [suite]
225 important dgit options:
226   -k<keyid>           sign tag and package with <keyid> instead of default
227   --dry-run -n        do not change anything, but go through the motions
228   --new -N            allow introducing a new package
229   --debug -D          increase debug level
230   -c<name>=<value>    set git config option (used directly by dgit too)
231 END
232
233 our $later_warning_msg = <<END;
234 Perhaps the upload is stuck in incoming.  Using the version from git.
235 END
236
237 sub badusage {
238     print STDERR "$us: @_\n", $helpmsg or die $!;
239     exit 8;
240 }
241
242 sub cmd_help () {
243     print $helpmsg or die $!;
244     exit 0;
245 }
246
247 our %defcfg = ('dgit.default.distro' => 'debian',
248                'dgit.default.username' => '',
249                'dgit.default.archive-query-default-component' => 'main',
250                'dgit.default.ssh' => 'ssh',
251                'dgit-distro.debian.git-host' => 'git.debian.org',
252                'dgit-distro.debian.git-proto' => 'git+ssh://',
253                'dgit-distro.debian.git-path' => '/git/dgit-repos/repos',
254                'dgit-distro.debian.git-check' => 'ssh-cmd',
255                'dgit-distro.debian.git-create' => 'ssh-cmd',
256                'dgit-distro.debian.sshdakls-host' => 'coccia.debian.org',
257                'dgit-distro.debian.sshdakls-dir' =>
258                    '/srv/ftp-master.debian.org/ftp/dists',
259                'dgit-distro.debian.upload-host' => 'ftp-master', # for dput
260                'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
261
262 sub cfg {
263     foreach my $c (@_) {
264         return undef if $c =~ /RETURN-UNDEF/;
265         my @cmd = (@git, qw(config --), $c);
266         my $v;
267         {
268             local ($debug) = $debug-1;
269             $v = cmdoutput_errok @cmd;
270         };
271         if ($?==0) {
272             return $v;
273         } elsif ($?!=256) {
274             failedcmd @cmd;
275         }
276         my $dv = $defcfg{$c};
277         return $dv if defined $dv;
278     }
279     badcfg "need value for one of: @_";
280 }
281
282 sub access_distro () {
283     return cfg("dgit-suite.$isuite.distro",
284                "dgit.default.distro");
285 }
286
287 sub access_cfg (@) {
288     my (@keys) = @_;
289     my $distro = $idistro || access_distro();
290     my $value = cfg(map { ("dgit-distro.$distro.$_",
291                            "dgit.default.$_") } @keys);
292     return $value;
293 }
294
295 sub access_someuserhost ($) {
296     my ($some) = @_;
297     my $user = access_cfg("$some-user",'username');
298     my $host = access_cfg("$some-host");
299     return length($user) ? "$user\@$host" : $host;
300 }
301
302 sub access_gituserhost () {
303     return access_someuserhost('git');
304 }
305
306 sub access_giturl () {
307     my $url = access_cfg('git-url','RETURN-UNDEF');
308     if (!defined $url) {
309         $url =
310             access_cfg('git-proto').
311             access_gituserhost().
312             access_cfg('git-path');
313     }
314     return "$url/$package.git";
315 }              
316
317 sub parsecontrolfh ($$@) {
318     my ($fh, $desc, @opts) = @_;
319     my %opts = ('name' => $desc, @opts);
320     my $c = Dpkg::Control::Hash->new(%opts);
321     $c->parse($fh) or die "parsing of $desc failed";
322     return $c;
323 }
324
325 sub parsecontrol {
326     my ($file, $desc) = @_;
327     my $fh = new IO::Handle;
328     open $fh, '<', $file or die "$file: $!";
329     my $c = parsecontrolfh($fh,$desc);
330     $fh->error and die $!;
331     close $fh;
332     return $c;
333 }
334
335 sub getfield ($$) {
336     my ($dctrl,$field) = @_;
337     my $v = $dctrl->{$field};
338     return $v if defined $v;
339     fail "missing field $field in ".$v->get_option('name');
340 }
341
342 sub parsechangelog {
343     my $c = Dpkg::Control::Hash->new();
344     my $p = new IO::Handle;
345     my @cmd = (qw(dpkg-parsechangelog), @_);
346     open $p, '-|', @cmd or die $!;
347     $c->parse($p);
348     $?=0; $!=0; close $p or failedcmd @cmd;
349     return $c;
350 }
351
352 our %rmad;
353
354 sub archive_query ($) {
355     my ($method) = @_;
356     my $query = access_cfg('archive-query','RETURN-UNDEF');
357     if (!defined $query) {
358         my $distro = access_distro();
359         if ($distro eq 'debian') {
360             $query = "sshdakls:".
361                 access_someuserhost('sshdakls').':'.
362                 access_cfg('sshdakls-dir');
363         } else {
364             $query = "madison:$distro";
365         }
366     }
367     $query =~ s/^(\w+):// or badcfg "invalid archive-query method \`$query'";
368     my $proto = $1;
369     my $data = $'; #';
370     { no strict qw(refs); &{"${method}_${proto}"}($proto,$data); }
371 }
372
373 sub archive_query_madison ($$) {
374     my ($proto,$data) = @_;
375     die unless $proto eq 'madison';
376     $rmad{$package} ||= cmdoutput
377         qw(rmadison -asource),"-s$isuite","-u$data",$package;
378     my $rmad = $rmad{$package};
379     return madison_parse($rmad);
380 }
381
382 sub archive_query_sshdakls ($$) {
383     my ($proto,$data) = @_;
384     $data =~ s/:.*// or badcfg "invalid sshdakls method string \`$data'";
385     my $dakls = cmdoutput
386         access_cfg('ssh'), $data, qw(dak ls -asource),"-s$isuite",$package;
387     return madison_parse($dakls);
388 }
389
390 sub canonicalise_suite_sshdakls ($$) {
391     my ($proto,$data) = @_;
392     $data =~ m/:/ or badcfg "invalid sshdakls method string \`$data'";
393     my @cmd =
394         (access_cfg('ssh'), $`,
395          "set -e; cd $';".
396          " if test -h $isuite; then readlink $isuite; exit 0; fi;".
397          " if test -d $isuite; then echo $isuite; exit 0; fi;".
398          " exit 1");
399     my $dakls = cmdoutput @cmd;
400     failedcmd @cmd unless $dakls =~ m/^\w/;
401     return $dakls;
402 }
403
404 sub madison_parse ($) {
405     my ($rmad) = @_;
406     my @out;
407     foreach my $l (split /\n/, $rmad) {
408         $l =~ m{^ \s*( [^ \t|]+ )\s* \|
409                   \s*( [^ \t|]+ )\s* \|
410                   \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
411                   \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
412         $1 eq $package or die "$rmad $package ?";
413         my $vsn = $2;
414         my $newsuite = $3;
415         my $component;
416         if (defined $4) {
417             $component = $4;
418         } else {
419             $component = access_cfg('archive-query-default-component');
420         }
421         $5 eq 'source' or die "$rmad ?";
422         my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
423         my $subpath = "/pool/$component/$prefix/$package/".dscfn($vsn);
424         push @out, [$vsn,$subpath,$newsuite];
425     }
426     return sort { -version_compare_string($a->[0],$b->[0]); } @out;
427 }
428
429 sub canonicalise_suite_madison ($$) {
430     my @r = archive_query_madison($_[0],$_[1]);
431     @r or fail
432         "unable to canonicalise suite using package $package".
433         " which does not appear to exist in suite $isuite;".
434         " --existing-package may help";
435     return $r[0][2];
436 }
437
438 sub canonicalise_suite () {
439     return if defined $csuite;
440     fail "cannot operate on $isuite suite" if $isuite eq 'UNRELEASED';
441     $csuite = archive_query('canonicalise_suite');
442     if ($isuite ne $csuite) {
443         # madison canonicalises for us
444         print "canonical suite name for $isuite is $csuite\n";
445     }
446 }
447
448 sub get_archive_dsc () {
449     canonicalise_suite();
450     my @vsns = archive_query('archive_query');
451     foreach my $vinfo (@vsns) {
452         my ($vsn,$subpath) = @$vinfo;
453         $dscurl = access_cfg('mirror').$subpath;
454         $dscdata = url_get($dscurl);
455         if (!$dscdata) {
456             $skew_warning_vsn = $vsn if !defined $skew_warning_vsn;
457             next;
458         }
459         my $dscfh = new IO::File \$dscdata, '<' or die $!;
460         print DEBUG Dumper($dscdata) if $debug>1;
461         $dsc = parsecontrolfh($dscfh,$dscurl, allow_pgp=>1);
462         print DEBUG Dumper($dsc) if $debug>1;
463         my $fmt = getfield $dsc, 'Format';
464         fail "unsupported source format $fmt, sorry" unless $format_ok{$fmt};
465         return;
466     }
467     $dsc = undef;
468 }
469
470 sub check_for_git () {
471     # returns 0 or 1
472     my $how = access_cfg('git-check');
473     if ($how eq 'ssh-cmd') {
474         my @cmd =
475             (access_cfg('ssh'),access_gituserhost(),
476              " set -e; cd ".access_cfg('git-path').";".
477              " if test -d $package.git; then echo 1; else echo 0; fi");
478         my $r= cmdoutput @cmd;
479         failedcmd @cmd unless $r =~ m/^[01]$/;
480         return $r+0;
481     } else {
482         badcfg "unknown git-check \`$how'";
483     }
484 }
485
486 sub create_remote_git_repo () {
487     my $how = access_cfg('git-create');
488     if ($how eq 'ssh-cmd') {
489         runcmd_ordryrun
490             (access_cfg('ssh'),access_gituserhost(),
491              "set -e; cd ".access_cfg('git-path').";".
492              " cp -a _template $package.git");
493     } else {
494         badcfg "unknown git-create \`$how'";
495     }
496 }
497
498 our ($dsc_hash,$lastpush_hash);
499
500 our $ud = '.git/dgit/unpack';
501
502 sub prep_ud () {
503     rmtree($ud);
504     mkpath '.git/dgit';
505     mkdir $ud or die $!;
506 }
507
508 sub mktree_in_ud_from_only_subdir () {
509     # changes into the subdir
510     my (@dirs) = <*/.>;
511     die unless @dirs==1;
512     $dirs[0] =~ m#^([^/]+)/\.$# or die;
513     my $dir = $1;
514     chdir $dir or die "$dir $!";
515     fail "source package contains .git directory" if stat '.git';
516     die $! unless $!==&ENOENT;
517     runcmd qw(git init -q);
518     rmtree('.git/objects');
519     symlink '../../../../objects','.git/objects' or die $!;
520     runcmd @git, qw(add -Af);
521     my $tree = cmdoutput @git, qw(write-tree);
522     $tree =~ m/^\w+$/ or die "$tree ?";
523     return ($tree,$dir);
524 }
525
526 sub dsc_files_info () {
527     foreach my $csumi (['Checksums-Sha256','Digest::SHA', 'new(256)'],
528                        ['Checksums-Sha1',  'Digest::SHA', 'new(1)'],
529                        ['Files',           'Digest::MD5', 'new()']) {
530         my ($fname, $module, $method) = @$csumi;
531         my $field = $dsc->{$fname};
532         next unless defined $field;
533         eval "use $module; 1;" or die $@;
534         my @out;
535         foreach (split /\n/, $field) {
536             next unless m/\S/;
537             m/^(\w+) (\d+) (\S+)$/ or
538                 fail "could not parse .dsc $fname line \`$_'";
539             my $digester = eval "$module"."->$method;" or die $@;
540             push @out, {
541                 Hash => $1,
542                 Bytes => $2,
543                 Filename => $3,
544                 Digester => $digester,
545             };
546         }
547         return @out;
548     }
549     fail "missing any supported Checksums-* or Files field in ".
550         $dsc->get_option('name');
551 }
552
553 sub dsc_files () {
554     map { $_->{Filename} } dsc_files_info();
555 }
556
557 sub is_orig_file ($) {
558     local ($_) = @_;
559     m/\.orig(?:-\w+)?\.tar\.\w+$/;
560 }
561
562 sub make_commit ($) {
563     my ($file) = @_;
564     return cmdoutput @git, qw(hash-object -w -t commit), $file;
565 }
566
567 sub generate_commit_from_dsc () {
568     prep_ud();
569     chdir $ud or die $!;
570     my @files;
571     foreach my $f (dsc_files()) {
572         die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
573         push @files, $f;
574         link "../../../$f", $f
575             or $!==&ENOENT
576             or die "$f $!";
577     }
578     runcmd @dget, qw(--), $dscurl;
579     foreach my $f (grep { is_orig_file($_) } @files) {
580         link $f, "../../../../$f"
581             or $!==&EEXIST
582             or die "$f $!";
583     }
584     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
585     runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
586     my $clogp = parsecontrol('../changelog.tmp',"commit's changelog");
587     my $date = cmdoutput qw(date), '+%s %z', qw(-d), getfield($clogp,'Date');
588     my $author = getfield $clogp, 'Maintainer';
589     $author =~ s#,.*##ms;
590     my $authline = "$author $date";
591     $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or
592         fail "unexpected commit author line format \`$authline'".
593             " (was generated from changelog Maintainer field)";
594     my $changes = getfield $clogp, 'Changes';
595     open C, ">../commit.tmp" or die $!;
596     print C <<END or die $!;
597 tree $tree
598 author $authline
599 committer $authline
600
601 $changes
602
603 # imported from the archive
604 END
605     close C or die $!;
606     my $outputhash = make_commit qw(../commit.tmp);
607     my $cversion = getfield $clogp, 'Version';
608     print "synthesised git commit from .dsc $cversion\n";
609     if ($lastpush_hash) {
610         runcmd @git, qw(reset --hard), $lastpush_hash;
611         runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
612         my $oldclogp = parsecontrol('../changelogold.tmp','previous changelog');
613         my $oversion = getfield $oldclogp, 'Version';
614         my $vcmp =
615             version_compare_string($oversion, $cversion);
616         if ($vcmp < 0) {
617             # git upload/ is earlier vsn than archive, use archive
618             open C, ">../commit2.tmp" or die $!;
619             print C <<END or die $!;
620 tree $tree
621 parent $lastpush_hash
622 parent $outputhash
623 author $authline
624 committer $authline
625
626 Record $package ($cversion) in archive suite $csuite
627 END
628             $outputhash = make_commit qw(../commit2.tmp);
629         } elsif ($vcmp > 0) {
630             print STDERR <<END or die $!;
631
632 Version actually in archive:    $cversion (older)
633 Last allegedly pushed/uploaded: $oversion (newer or same)
634 $later_warning_msg
635 END
636             $outputhash = $lastpush_hash;
637         } else {
638             $outputhash = $lastpush_hash;
639         }
640     }
641     chdir '../../../..' or die $!;
642     runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
643             'DGIT_ARCHIVE', $outputhash;
644     cmdoutput @git, qw(log -n2), $outputhash;
645     # ... gives git a chance to complain if our commit is malformed
646     rmtree($ud);
647     return $outputhash;
648 }
649
650 sub ensure_we_have_orig () {
651     foreach my $fi (dsc_files_info()) {
652         my $f = $fi->{Filename};
653         next unless is_orig_file($f);
654         if (open F, "<", "../$f") {
655             $fi->{Digester}->reset();
656             $fi->{Digester}->addfile(*F);
657             F->error and die $!;
658             my $got = $fi->{Digester}->hexdigest();
659             $got eq $fi->{Hash} or
660                 fail "existing file $f has hash $got but .dsc".
661                     " demands hash $fi->{Hash}".
662                     " (perhaps you should delete this file?)";
663             print "using existing $f\n";
664             next;
665         } else {
666             die "$f $!" unless $!==&ENOENT;
667         }
668         my $origurl = $dscurl;
669         $origurl =~ s{/[^/]+$}{};
670         $origurl .= "/$f";
671         die "$f ?" unless $f =~ m/^${package}_/;
672         die "$f ?" if $f =~ m#/#;
673         runcmd_ordryrun shell_cmd 'cd ..', @dget,'--',$origurl;
674     }
675 }
676
677 sub rev_parse ($) {
678     return cmdoutput @git, qw(rev-parse), "$_[0]~0";
679 }
680
681 sub is_fast_fwd ($$) {
682     my ($ancestor,$child) = @_;
683     my @cmd = (@git, qw(merge-base), $ancestor, $child);
684     my $mb = cmdoutput_errok @cmd;
685     if (defined $mb) {
686         return rev_parse($mb) eq rev_parse($ancestor);
687     } else {
688         $?==256 or failedcmd @cmd;
689         return 0;
690     }
691 }
692
693 sub git_fetch_us () {
694     runcmd_ordryrun @git, qw(fetch),access_giturl(),fetchspec();
695 }
696
697 sub fetch_from_archive () {
698     # ensures that lrref() is what is actually in the archive,
699     #  one way or another
700     get_archive_dsc();
701
702     if ($dsc) {
703         foreach my $field (@ourdscfield) {
704             $dsc_hash = $dsc->{$field};
705             last if defined $dsc_hash;
706         }
707         if (defined $dsc_hash) {
708             $dsc_hash =~ m/\w+/ or fail "invalid hash in .dsc \`$dsc_hash'";
709             $dsc_hash = $&;
710             print "last upload to archive specified git hash\n";
711         } else {
712             print "last upload to archive has NO git hash\n";
713         }
714     } else {
715         print "no version available from the archive\n";
716     }
717
718     my $lrref_fn = ".git/".lrref();
719     if (open H, $lrref_fn) {
720         $lastpush_hash = <H>;
721         chomp $lastpush_hash;
722         die "$lrref_fn $lastpush_hash ?" unless $lastpush_hash =~ m/^\w+$/;
723     } elsif ($! == &ENOENT) {
724         $lastpush_hash = '';
725     } else {
726         die "$lrref_fn $!";
727     }
728     print DEBUG "previous reference hash=$lastpush_hash\n";
729     my $hash;
730     if (defined $dsc_hash) {
731         fail "missing git history even though dsc has hash -".
732             " could not find commit $dsc_hash".
733             " (should be in ".access_giturl()."#".rrref().")"
734             unless $lastpush_hash;
735         $hash = $dsc_hash;
736         ensure_we_have_orig();
737         if ($dsc_hash eq $lastpush_hash) {
738         } elsif (is_fast_fwd($dsc_hash,$lastpush_hash)) {
739             print STDERR <<END or die $!;
740
741 Git commit in archive is behind the last version allegedly pushed/uploaded.
742 Commit referred to by archive:  $dsc_hash
743 Last allegedly pushed/uploaded: $lastpush_hash
744 $later_warning_msg
745 END
746             $hash = $lastpush_hash;
747         } else {
748             fail "archive's .dsc refers to ".$dsc_hash.
749                 " but this is an ancestor of ".$lastpush_hash;
750         }
751     } elsif ($dsc) {
752         $hash = generate_commit_from_dsc();
753     } elsif ($lastpush_hash) {
754         # only in git, not in the archive yet
755         $hash = $lastpush_hash;
756         print STDERR <<END or die $!;
757
758 Package not found in the archive, but has allegedly been pushed using dgit.
759 $later_warning_msg
760 END
761     } else {
762         print DEBUG "nothing found!\n";
763         if (defined $skew_warning_vsn) {
764             print STDERR <<END or die $!;
765
766 Warning: relevant archive skew detected.
767 Archive allegedly contains $skew_warning_vsn
768 But we were not able to obtain any version from the archive or git.
769
770 END
771         }
772         return 0;
773     }
774     print DEBUG "current hash=$hash\n";
775     if ($lastpush_hash) {
776         fail "not fast forward on last upload branch!".
777             " (archive's version left in DGIT_ARCHIVE)"
778             unless is_fast_fwd($lastpush_hash, $hash);
779     }
780     if (defined $skew_warning_vsn) {
781         mkpath '.git/dgit';
782         print DEBUG "SKEW CHECK WANT $skew_warning_vsn\n";
783         my $clogf = ".git/dgit/changelog.tmp";
784         runcmd shell_cmd "exec >$clogf",
785             @git, qw(cat-file blob), "$hash:debian/changelog";
786         my $gotclogp = parsechangelog("-l$clogf");
787         my $got_vsn = getfield $gotclogp, 'Version';
788         print DEBUG "SKEW CHECK GOT $got_vsn\n";
789         if (version_compare_string($got_vsn, $skew_warning_vsn) < 0) {
790             print STDERR <<END or die $!;
791
792 Warning: archive skew detected.  Using the available version:
793 Archive allegedly contains    $skew_warning_vsn
794 We were able to obtain only   $got_vsn
795
796 END
797         }
798     }
799     if ($lastpush_hash ne $hash) {
800         my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
801         if (!$dryrun) {
802             cmdoutput @upd_cmd;
803         } else {
804             dryrun_report @upd_cmd;
805         }
806     }
807     return 1;
808 }
809
810 sub clone ($) {
811     my ($dstdir) = @_;
812     canonicalise_suite();
813     badusage "dry run makes no sense with clone" if $dryrun;
814     mkdir $dstdir or die "$dstdir $!";
815     chdir "$dstdir" or die "$dstdir $!";
816     runcmd @git, qw(init -q);
817     runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
818     open H, "> .git/HEAD" or die $!;
819     print H "ref: ".lref()."\n" or die $!;
820     close H or die $!;
821     runcmd @git, qw(remote add), 'origin', access_giturl();
822     if (check_for_git()) {
823         print "fetching existing git history\n";
824         git_fetch_us();
825         runcmd_ordryrun @git, qw(fetch origin);
826     } else {
827         print "starting new git history\n";
828     }
829     fetch_from_archive() or no_such_package;
830     runcmd @git, qw(reset --hard), lrref();
831     printdone "ready for work in $dstdir";
832 }
833
834 sub fetch () {
835     if (check_for_git()) {
836         git_fetch_us();
837     }
838     fetch_from_archive() or no_such_package();
839     printdone "fetched into ".lrref();
840 }
841
842 sub pull () {
843     fetch();
844     runcmd_ordryrun @git, qw(merge -m),"Merge from $csuite [dgit]",
845         lrref();
846     printdone "fetched to ".lrref()." and merged into HEAD";
847 }
848
849 sub check_not_dirty () {
850     return if $ignoredirty;
851     my @cmd = (@git, qw(diff --quiet HEAD));
852     printcmd(\*DEBUG,"+",@cmd) if $debug>0;
853     $!=0; $?=0; system @cmd;
854     return if !$! && !$?;
855     if (!$! && $?==256) {
856         fail "working tree is dirty (does not match HEAD)";
857     } else {
858         failedcmd @cmd;
859     }
860 }
861
862 sub commit_quilty_patch () {
863     my $output = cmdoutput @git, qw(status --porcelain);
864     my %adds;
865     my $bad=0;
866     foreach my $l (split /\n/, $output) {
867         next unless $l =~ m/\S/;
868         if ($l =~ m{^(?:\?\?| M) (.pc|debian/patches)}) {
869             $adds{$1}++;
870         } else {
871             print STDERR "git status: $l\n";
872             $bad++;
873         }
874     }
875     fail "unexpected output from git status (is tree clean?)" if $bad;
876     if (!%adds) {
877         print "nothing quilty to commit, ok.\n";
878         return;
879     }
880     runcmd_ordryrun @git, qw(add), sort keys %adds;
881     my $m = "Commit Debian 3.0 (quilt) metadata";
882     print "$m\n";
883     runcmd_ordryrun @git, qw(commit -m), $m;
884 }
885
886 sub madformat ($) {
887     my ($format) = @_;
888     return 0 unless $format eq '3.0 (quilt)';
889     print "Format \`$format', urgh\n";
890     if ($noquilt) {
891         print "Not doing any fixup of \`$format' due to --no-quilt-fixup";
892         return 0;
893     }
894     return 1;
895 }
896
897 sub dopush () {
898     print DEBUG "actually entering push\n";
899     my $clogp = parsechangelog();
900     $package = getfield $clogp, 'Source';
901     my $cversion = getfield $clogp, 'Version';
902     my $dscfn = dscfn($cversion);
903     stat "../$dscfn" or
904         fail "looked for .dsc $dscfn, but $!;".
905             " maybe you forgot to build";
906     $dsc = parsecontrol("../$dscfn","$dscfn");
907     my $dscpackage = getfield $dsc, 'Source';
908     my $format = getfield $dsc, 'Format';
909     my $dversion = getfield $dsc, 'Version';
910     ($dscpackage eq $package && $dversion eq $cversion) or
911         fail "$dsc is for $dscpackage $dversion".
912             " but debian/changelog is for $package $cversion";
913     print DEBUG "format $format\n";
914     if (madformat($format)) {
915         commit_quilty_patch();
916     }
917     check_not_dirty();
918     prep_ud();
919     chdir $ud or die $!;
920     print "checking that $dscfn corresponds to HEAD\n";
921     runcmd qw(dpkg-source -x --), "../../../../$dscfn";
922     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
923     chdir '../../../..' or die $!;
924     printcmd \*DEBUG,"+",@_;
925     my @diffcmd = (@git, qw(diff --exit-code), $tree);
926     $!=0; $?=0;
927     if (system @diffcmd) {
928         if ($! && $?==256) {
929             fail "$dscfn specifies a different tree to your HEAD commit;".
930                 " perhaps you forgot to build";
931         } else {
932             failedcmd @diffcmd;
933         }
934     }
935 #fetch from alioth
936 #do fast forward check and maybe fake merge
937 #    if (!is_fast_fwd(mainbranch
938 #    runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
939 #        map { lref($_).":".rref($_) }
940 #        (uploadbranch());
941     $dsc->{$ourdscfield[0]} = rev_parse('HEAD');
942     $dsc->save("../$dscfn.tmp") or die $!;
943     if (!$changesfile) {
944         my $multi = "../${package}_".(stripepoch $cversion)."_multi.changes";
945         if (stat "$multi") {
946             $changesfile = $multi;
947         } else {
948             $!==&ENOENT or die "$multi: $!";
949             my $pat = "${package}_".(stripepoch $cversion)."_*.changes";
950             my @cs = glob "../$pat";
951             fail "failed to find unique changes file".
952                 " (looked for $pat in .., or $multi);".
953                 " perhaps you need to use dgit -C"
954                 unless @cs==1;
955             ($changesfile) = @cs;
956         }
957     }
958     my $changes = parsecontrol($changesfile,$changesfile);
959     foreach my $field (qw(Source Distribution Version)) {
960         $changes->{$field} eq $clogp->{$field} or
961             fail "changes field $field \`$changes->{$field}'".
962                 " does not match changelog \`$clogp->{$field}'";
963     }
964     my $tag = debiantag($dversion);
965     if (!check_for_git()) {
966         create_remote_git_repo();
967     }
968     runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
969     runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
970     if (!$dryrun) {
971         rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
972     } else {
973         print "[new .dsc left in $dscfn.tmp]\n";
974     }
975     if ($sign) {
976         if (!defined $keyid) {
977             $keyid = access_cfg('keyid','RETURN-UNDEF');
978         }
979         my @tag_cmd = (@git, qw(tag -s -m),
980                        "$package release $dversion for $csuite [dgit]");
981         push @tag_cmd, qw(-u),$keyid if defined $keyid;
982         push @tag_cmd, $tag;
983         runcmd_ordryrun @tag_cmd;
984         my @debsign_cmd = @debsign;
985         push @debsign_cmd, "-k$keyid" if defined $keyid;
986         push @debsign_cmd, $changesfile;
987         runcmd_ordryrun @debsign_cmd;
988     }
989     runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
990     my $host = access_cfg('upload-host','RETURN-UNDEF');
991     my @hostarg = defined($host) ? ($host,) : ();
992     runcmd_ordryrun @dput, @hostarg, $changesfile;
993     printdone "pushed and uploaded $dversion";
994 }
995
996 sub cmd_clone {
997     parseopts();
998     my $dstdir;
999     badusage "-p is not allowed with clone; specify as argument instead"
1000         if defined $package;
1001     if (@ARGV==1) {
1002         ($package) = @ARGV;
1003     } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
1004         ($package,$isuite) = @ARGV;
1005     } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
1006         ($package,$dstdir) = @ARGV;
1007     } elsif (@ARGV==3) {
1008         ($package,$isuite,$dstdir) = @ARGV;
1009     } else {
1010         badusage "incorrect arguments to dgit clone";
1011     }
1012     $dstdir ||= "$package";
1013     clone($dstdir);
1014 }
1015
1016 sub branchsuite () {
1017     my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
1018     if ($branch =~ m#$lbranch_re#o) {
1019         return $1;
1020     } else {
1021         return undef;
1022     }
1023 }
1024
1025 sub fetchpullargs () {
1026     if (!defined $package) {
1027         my $sourcep = parsecontrol('debian/control','debian/control');
1028         $package = getfield $sourcep, 'Source';
1029     }
1030     if (@ARGV==0) {
1031 #       $isuite = branchsuite();  # this doesn't work because dak hates canons
1032         if (!$isuite) {
1033             my $clogp = parsechangelog();
1034             $isuite = getfield $clogp, 'Distribution';
1035         }
1036         canonicalise_suite();
1037         print "fetching from suite $csuite\n";
1038     } elsif (@ARGV==1) {
1039         ($isuite) = @ARGV;
1040         canonicalise_suite();
1041     } else {
1042         badusage "incorrect arguments to dgit fetch or dgit pull";
1043     }
1044 }
1045
1046 sub cmd_fetch {
1047     parseopts();
1048     fetchpullargs();
1049     fetch();
1050 }
1051
1052 sub cmd_pull {
1053     parseopts();
1054     fetchpullargs();
1055     pull();
1056 }
1057
1058 sub cmd_push {
1059     parseopts();
1060     badusage "-p is not allowed with dgit push" if defined $package;
1061     check_not_dirty();
1062     my $clogp = parsechangelog();
1063     $package = getfield $clogp, 'Source';
1064     if (@ARGV==0) {
1065         $isuite = getfield $clogp, 'Distribution';
1066         if ($new_package) {
1067             local ($package) = $existing_package; # this is a hack
1068             canonicalise_suite();
1069         }
1070     } else {
1071         badusage "incorrect arguments to dgit push";
1072     }
1073     if (check_for_git()) {
1074         git_fetch_us();
1075     }
1076     if (fetch_from_archive()) {
1077         is_fast_fwd(lrref(), 'HEAD') or
1078             fail "dgit push: HEAD is not a descendant".
1079                 " of the archive's version.\n".
1080                 "$us: To overwrite it, use git-merge -s ours ".lrref().".";
1081     } else {
1082         $new_package or
1083             fail "package appears to be new in this suite;".
1084                 " if this is intentional, use --new";
1085     }
1086     dopush();
1087 }
1088
1089 our $version;
1090 our $sourcechanges;
1091 our $dscfn;
1092
1093 our $fakeeditorenv = 'DGIT_FAKE_EDITOR_QUILT';
1094
1095 sub build_maybe_quilt_fixup () {
1096     if (!open F, "debian/source/format") {
1097         die $! unless $!==&ENOENT;
1098         return;
1099     }
1100     $_ = <F>;
1101     F->error and die $!;
1102     chomp;
1103     return unless madformat($_);
1104     # sigh
1105     my $clogp = parsechangelog();
1106     my $version = getfield $clogp, 'Version';
1107     my $author = getfield $clogp, 'Maintainer';
1108     my $headref = rev_parse('HEAD');
1109     my $time = time;
1110     my $ncommits = 3;
1111     my $patchname = "auto-$version-$headref-$time";
1112     my $msg = cmdoutput @git, qw(log), "-n$ncommits";
1113     mkpath '.git/dgit';
1114     my $descfn = ".git/dgit/quilt-description.tmp";
1115     open O, '>', $descfn or die "$descfn: $!";
1116     $msg =~ s/\n/\n /g;
1117     $msg =~ s/^\s+$/ ./mg;
1118     print O <<END or die $!;
1119 Description: Automatically generated patch ($clogp->{Version})
1120  Last (up to) $ncommits git changes, FYI:
1121  .
1122  $msg
1123 Author: $author
1124
1125 ---
1126
1127 END
1128     close O or die $!;
1129     {
1130         local $ENV{'EDITOR'} = cmdoutput qw(realpath --), $0;
1131         local $ENV{'VISUAL'} = $ENV{'EDITOR'};
1132         local $ENV{$fakeeditorenv} = cmdoutput qw(realpath --), $descfn;
1133         runcmd_ordryrun @dpkgsource, qw(--commit .), $patchname;
1134     }
1135
1136     if (!open P, '>>', ".pc/applied-patches") {
1137         $!==&ENOENT or die $!;
1138     } else {
1139         close P;
1140     }
1141
1142     commit_quilty_patch();
1143 }
1144
1145 sub quilt_fixup_editor () {
1146     my $descfn = $ENV{$fakeeditorenv};
1147     my $editing = $ARGV[$#ARGV];
1148     open I1, '<', $descfn or die "$descfn: $!";
1149     open I2, '<', $editing or die "$editing: $!";
1150     unlink $editing or die "$editing: $!";
1151     open O, '>', $editing or die "$editing: $!";
1152     while (<I1>) { print O or die $!; } I1->error and die $!;
1153     my $copying = 0;
1154     while (<I2>) {
1155         $copying ||= m/^\-\-\- /;
1156         next unless $copying;
1157         print O or die $!;
1158     }
1159     I2->error and die $!;
1160     close O or die $1;
1161     exit 0;
1162 }
1163
1164 sub build_prep () {
1165     badusage "-p is not allowed when building" if defined $package;
1166     check_not_dirty();
1167     my $clogp = parsechangelog();
1168     $isuite = getfield $clogp, 'Distribution';
1169     $package = getfield $clogp, 'Source';
1170     $version = getfield $clogp, 'Version';
1171     build_maybe_quilt_fixup();
1172 }
1173
1174 sub cmd_build {
1175     badusage "dgit build implies --clean=dpkg-source"
1176         if $cleanmode ne 'dpkg-source';
1177     build_prep();
1178     runcmd_ordryrun @dpkgbuildpackage, qw(-us -uc), changesopts(), @ARGV;
1179     printdone "build successful\n";
1180 }
1181
1182 sub cmd_git_build {
1183     badusage "dgit git-build implies --clean=dpkg-source"
1184         if $cleanmode ne 'dpkg-source';
1185     build_prep();
1186     my @cmd =
1187         (qw(git-buildpackage -us -uc --git-no-sign-tags),
1188          "--git-builder=@dpkgbuildpackage");
1189     unless (grep { m/^--git-debian-branch|^--git-ignore-branch/ } @ARGV) {
1190         canonicalise_suite();
1191         push @cmd, "--git-debian-branch=".lbranch();
1192     }
1193     push @cmd, changesopts();
1194     runcmd_ordryrun @cmd, @ARGV;
1195     printdone "build successful\n";
1196 }
1197
1198 sub build_source {
1199     build_prep();
1200     $sourcechanges = "${package}_".(stripepoch $version)."_source.changes";
1201     $dscfn = dscfn($version);
1202     if ($cleanmode eq 'dpkg-source') {
1203         runcmd_ordryrun (@dpkgbuildpackage, qw(-us -uc -S)), changesopts();
1204     } else {
1205         if ($cleanmode eq 'git') {
1206             runcmd_ordryrun @git, qw(clean -xdf);
1207         } elsif ($cleanmode eq 'none') {
1208         } else {
1209             die "$cleanmode ?";
1210         }
1211         my $pwd = cmdoutput qw(env - pwd);
1212         my $leafdir = basename $pwd;
1213         chdir ".." or die $!;
1214         runcmd_ordryrun @dpkgsource, qw(-b --), $leafdir;
1215         chdir $pwd or die $!;
1216         runcmd_ordryrun qw(sh -ec),
1217             'exec >$1; shift; exec "$@"','x',
1218             "../$sourcechanges",
1219             @dpkggenchanges, qw(-S), changesopts();
1220     }
1221 }
1222
1223 sub cmd_build_source {
1224     badusage "build-source takes no additional arguments" if @ARGV;
1225     build_source();
1226     printdone "source built, results in $dscfn and $sourcechanges";
1227 }
1228
1229 sub cmd_sbuild {
1230     build_source();
1231     chdir ".." or die $!;
1232     my $pat = "${package}_".(stripepoch $version)."_*.changes";
1233     if (!$dryrun) {
1234         stat $dscfn or fail "$dscfn (in parent directory): $!";
1235         stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
1236         foreach my $cf (glob $pat) {
1237             next if $cf eq $sourcechanges;
1238             unlink $cf or fail "remove $cf: $!";
1239         }
1240     }
1241     runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
1242     runcmd_ordryrun @mergechanges, glob $pat;
1243     my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
1244     if (!$dryrun) {
1245         stat $multichanges or fail "$multichanges: $!";
1246     }
1247     printdone "build successful, results in $multichanges\n" or die $!;
1248 }    
1249
1250 sub cmd_quilt_fixup {
1251     badusage "incorrect arguments to dgit quilt-fixup" if @ARGV;
1252     my $clogp = parsechangelog();
1253     $version = getfield $clogp, 'Version';
1254     build_maybe_quilt_fixup();
1255 }
1256
1257 sub cmd_version {
1258     print "dgit version $our_version\n" or die $!;
1259     exit 0;
1260 }
1261
1262 sub parseopts () {
1263     my $om;
1264     while (@ARGV) {
1265         last unless $ARGV[0] =~ m/^-/;
1266         $_ = shift @ARGV;
1267         last if m/^--?$/;
1268         if (m/^--/) {
1269             if (m/^--dry-run$/) {
1270                 $dryrun=1;
1271             } elsif (m/^--no-sign$/) {
1272                 $sign=0;
1273             } elsif (m/^--help$/) {
1274                 cmd_help();
1275             } elsif (m/^--version$/) {
1276                 cmd_version();
1277             } elsif (m/^--new$/) {
1278                 $new_package=1;
1279             } elsif (m/^--(\w+)=(.*)/s &&
1280                      ($om = $opts_opt_map{$1}) &&
1281                      length $om->[0]) {
1282                 $om->[0] = $2;
1283             } elsif (m/^--(\w+):(.*)/s &&
1284                      ($om = $opts_opt_map{$1})) {
1285                 push @$om, $2;
1286             } elsif (m/^--existing-package=(.*)/s) {
1287                 $existing_package = $1;
1288             } elsif (m/^--distro=(.*)/s) {
1289                 $idistro = $1;
1290             } elsif (m/^--clean=(dpkg-source|git|none)$/s) {
1291                 $cleanmode = $1;
1292             } elsif (m/^--clean=(.*)$/s) {
1293                 badusage "unknown cleaning mode \`$1'";
1294             } elsif (m/^--ignore-dirty$/s) {
1295                 $ignoredirty = 1;
1296             } elsif (m/^--no-quilt-fixup$/s) {
1297                 $noquilt = 1;
1298             } else {
1299                 badusage "unknown long option \`$_'";
1300             }
1301         } else {
1302             while (m/^-./s) {
1303                 if (s/^-n/-/) {
1304                     $dryrun=1;
1305                 } elsif (s/^-h/-/) {
1306                     cmd_help();
1307                 } elsif (s/^-D/-/) {
1308                     open DEBUG, ">&STDERR" or die $!;
1309                     $debug++;
1310                 } elsif (s/^-N/-/) {
1311                     $new_package=1;
1312                 } elsif (m/^-[vm]/) {
1313                     push @changesopts, $_;
1314                     $_ = '';
1315                 } elsif (s/^-c(.*=.*)//s) {
1316                     push @git, '-c', $1;
1317                 } elsif (s/^-d(.*)//s) {
1318                     $idistro = $1;
1319                 } elsif (s/^-C(.*)//s) {
1320                     $changesfile = $1;
1321                 } elsif (s/^-k(.*)//s) {
1322                     $keyid=$1;
1323                 } elsif (s/^-wn//s) {
1324                     $cleanmode = 'none';
1325                 } elsif (s/^-wg//s) {
1326                     $cleanmode = 'git';
1327                 } elsif (s/^-wd//s) {
1328                     $cleanmode = 'dpkg-source';
1329                 } else {
1330                     badusage "unknown short option \`$_'";
1331                 }
1332             }
1333         }
1334     }
1335 }
1336
1337 if ($ENV{$fakeeditorenv}) {
1338     quilt_fixup_editor();
1339 }
1340
1341 delete $ENV{'DGET_UNPACK'};
1342
1343 parseopts();
1344 print STDERR "DRY RUN ONLY\n" if $dryrun;
1345 if (!@ARGV) {
1346     print STDERR $helpmsg or die $!;
1347     exit 8;
1348 }
1349 my $cmd = shift @ARGV;
1350 $cmd =~ y/-/_/;
1351 { no strict qw(refs); &{"cmd_$cmd"}(); }