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