chiark / gitweb /
success msgs
[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 Dpkg::Version;
28 use POSIX;
29
30 our $suite = 'sid';
31 our $package;
32
33 our $sign = 1;
34 our $dryrun = 0;
35 our $changesfile;
36 our $new_package = 0;
37 our $existing_package = 'dpkg';
38
39 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
40
41 our (@git) = qw(git);
42 our (@dget) = qw(dget);
43 our (@dput) = qw(dput);
44 our (@debsign) = qw(debsign);
45 our $keyid;
46
47 our $debug = 0;
48 open DEBUG, ">/dev/null" or die $!;
49
50 our %opts_opt_map = ('dget' => \@dget,
51                      'dput' => \@dput,
52                      'debsign' => \@debsign);
53
54 our $remotename = 'dgit';
55 our $ourdscfield = 'Vcs-Dgit-Master';
56 our $branchprefix = 'dgit';
57
58 sub lbranch () { return "$branchprefix/$suite"; }
59 my $lbranch_re = '^refs/heads/'.$branchprefix.'/([^/.]+)$';
60 sub lref () { return "refs/heads/".lbranch(); }
61 sub lrref () { return "refs/remotes/$remotename/$suite"; }
62 sub rrref () { return "refs/$branchprefix/$suite"; }
63 sub debiantag ($) { return "debian/$_[0]"; }
64
65 sub fetchspec () {
66     local $suite = '*';
67     return  "+".rrref().":".lrref();
68 }
69
70 our $ua;
71
72 sub url_get {
73     if (!$ua) {
74         $ua = LWP::UserAgent->new();
75         $ua->env_proxy;
76     }
77     print "downloading @_...\n";
78     my $r = $ua->get(@_) or die $!;
79     die "$_[0]: ".$r->status_line."; failed.\n" unless $r->is_success;
80     return $r->decoded_content();
81 }
82
83 our ($dscdata,$dscurl,$dsc);
84
85 sub printcmd {
86     my $fh = shift @_;
87     my $intro = shift @_;
88     print $fh $intro or die $!;
89     local $_;
90     foreach my $a (@_) {
91         $_ = $a;
92         if (s{['\\]}{\\$&}g || m{\s} || m{[^-_./0-9a-z]}i) {
93             print $fh " '$_'" or die $!;
94         } else {
95             print $fh " $_" or die $!;
96         }
97     }
98     print $fh "\n" or die $!;
99 }
100
101 sub runcmd {
102     printcmd(\*DEBUG,"+",@_) if $debug>0;
103     $!=0; $?=0;
104     die "@_ $! $?" if system @_;
105 }
106
107 sub cmdoutput_errok {
108     die Dumper(\@_)." ?" if grep { !defined } @_;
109     printcmd(\*DEBUG,"|",@_) if $debug>0;
110     open P, "-|", @_ or die $!;
111     my $d;
112     $!=0; $?=0;
113     { local $/ = undef; $d = <P>; }
114     die if P->error;
115     if (!close P) { print DEBUG "=>!$?\n" if $debug>0; return undef; }
116     chomp $d;
117     $d =~ m/^.*/;
118     print DEBUG "=> \`$&'",(length $' ? '...' : ''),"\n" if $debug>0; #';
119     return $d;
120 }
121
122 sub cmdoutput {
123     my $d = cmdoutput_errok @_;
124     defined $d or die "@_ $? $!";
125     return $d;
126 }
127
128 sub dryrun_report {
129     printcmd(\*STDOUT,"#",@_);
130 }
131
132 sub runcmd_ordryrun {
133     if (!$dryrun) {
134         runcmd @_;
135     } else {
136         dryrun_report @_;
137     }
138 }
139
140 our %defcfg = ('dgit.default.distro' => 'debian',
141                'dgit.default.username' => '',
142                'dgit.default.archive-query-default-component' => 'main',
143                'dgit.default.ssh' => 'ssh',
144                'dgit-distro.debian.git-host' => 'git.debian.org',
145                'dgit-distro.debian.git-proto' => 'git+ssh://',
146                'dgit-distro.debian.git-path' => '/git/dgit-repos',
147                'dgit-distro.debian.git-check' => 'ssh-cmd',
148                'dgit-distro.debian.git-create' => 'ssh-cmd',
149                'dgit-distro.debian.mirror' => 'http://ftp.debian.org/debian/');
150
151 sub cfg {
152     foreach my $c (@_) {
153         my $v;
154         {
155             local ($debug) = $debug-1;
156             $v = cmdoutput_errok(@git, qw(config --), $c);
157         };
158         if ($?==0) {
159             return $v;
160         } elsif ($?!=256) {
161             die "$c $?";
162         }
163         my $dv = $defcfg{$c};
164         return $dv if defined $dv;
165     }
166     return undef;
167 }
168
169 sub access_distro () {
170     return cfg("dgit-suite.$suite.distro",
171                "dgit.default.distro");
172 }
173
174 sub access_cfg ($) {
175     my ($key) = @_;
176     my $distro = access_distro();
177     my $value = cfg("dgit-distro.$distro.$key",
178                     "dgit.default.$key");
179     return $value;
180 }
181
182 sub access_gituserhost () {
183     my $user = access_cfg('git-user');
184     my $host = access_cfg('git-host');
185     return defined($user) && length($user) ? "$user\@$host" : $host;
186 }
187
188 sub access_giturl () {
189     my $url = access_cfg('git-url');
190     if (!defined $url) {
191         $url =
192             access_cfg('git-proto').
193             access_gituserhost().
194             access_cfg('git-path');
195     }
196     return "$url/$package.git";
197 }              
198
199 sub parsecontrol {
200     my $c = Dpkg::Control::Hash->new();
201     $c->load(@_) or return undef;
202     return $c;
203 }
204
205 sub parsechangelog {
206     my $c = Dpkg::Control::Hash->new();
207     my $p = new IO::Handle;
208     open $p, '-|', qw(dpkg-parsechangelog) or die $!;
209     $c->parse($p);
210     $?=0; $!=0; close $p or die "$! $?";
211     return $c;
212 }
213
214 our %rmad;
215
216 sub archive_query () {
217     my $query = access_cfg('archive-query');
218     $query ||= "madison:".access_distro();
219     $query =~ s/^(\w+):// or die "$query ?";
220     my $proto = $1;
221     my $url = $'; #';
222     die unless $proto eq 'madison';
223     $rmad{$package} ||= cmdoutput
224         qw(rmadison -asource),"-s$suite","-u$url",$package;
225     my $rmad = $rmad{$package};
226     if (!length $rmad) {
227         return ();
228     }
229     $rmad =~ m{^ \s*( [^ \t|]+ )\s* \|
230                  \s*( [^ \t|]+ )\s* \|
231                  \s*( [^ \t|/]+ )(?:/([^ \t|/]+))? \s* \|
232                  \s*( [^ \t|]+ )\s* }x or die "$rmad $?";
233     $1 eq $package or die "$rmad $package ?";
234     my $vsn = $2;
235     if ($suite ne $3) {
236         # madison canonicalises for us
237         print "canonical suite name for $suite is $3\n";
238         $suite = $3;
239     }
240     my $component;
241     if (defined $4) {
242         $component = $4;
243     } else {
244         $component = access_cfg('archive-query-default-component');
245     }
246     $5 eq 'source' or die "$rmad ?";
247     my $prefix = substr($package, 0, $package =~ m/^l/ ? 4 : 1);
248     my $subpath = "/pool/$component/$prefix/$package/${package}_$vsn.dsc";
249     return ($vsn,$subpath);
250 }
251
252 sub canonicalise_suite () {
253     archive_query() or die;
254 }
255
256 sub get_archive_dsc () {
257     my ($vsn,$subpath) = archive_query();
258     if (!defined $vsn) { $dsc=undef; return undef; }
259     $dscurl = access_cfg('mirror').$subpath;
260     $dscdata = url_get($dscurl);
261     my $dscfh = new IO::File \$dscdata, '<' or die $!;
262     print DEBUG Dumper($dscdata) if $debug>1;
263     $dsc = Dpkg::Control::Hash->new(allow_pgp=>1);
264     $dsc->parse($dscfh, 'dsc') or die "parsing of $dscurl failed\n";
265     print DEBUG Dumper($dsc) if $debug>1;
266     my $fmt = $dsc->{Format};
267     die "unsupported format $fmt, sorry\n" unless $format_ok{$fmt};
268 }
269
270 sub check_for_git () {
271     # returns 0 or 1
272     my $how = access_cfg('git-check');
273     if ($how eq 'ssh-cmd') {
274         my $r= cmdoutput
275             (access_cfg('ssh'),access_gituserhost(),
276              " set -e; cd ".access_cfg('git-path').";".
277              " if test -d $package.git; then echo 1; else echo 0; fi");
278         die "$r $! $?" unless $r =~ m/^[01]$/;
279         return $r+0;
280     } else {
281         die "unknown git-check $how ?";
282     }
283 }
284
285 sub create_remote_git_repo () {
286     my $how = access_cfg('git-create');
287     if ($how eq 'ssh-cmd') {
288         runcmd_ordryrun
289             (access_cfg('ssh'),access_gituserhost(),
290              "set -e; cd ".access_cfg('git-path').";".
291              " mkdir -p $package.git;".
292              " cd $package.git;".
293              " if ! test -d objects; then git init --bare; fi");
294     } else {
295         die "unknown git-create $how ?";
296     }
297 }
298
299 our ($dsc_hash,$upload_hash);
300
301 our $ud = '.git/dgit/unpack';
302
303 sub prep_ud () {
304     rmtree($ud);
305     mkpath '.git/dgit';
306     mkdir $ud or die $!;
307 }
308
309 sub mktree_in_ud_from_only_subdir () {
310     # changes into the subdir
311     my (@dirs) = <*/.>;
312     die unless @dirs==1;
313     $dirs[0] =~ m#^([^/]+)/\.$# or die;
314     my $dir = $1;
315     chdir $dir or die "$dir $!";
316     die if stat '.git';
317     die $! unless $!==&ENOENT;
318     runcmd qw(git init -q);
319     rmtree('.git/objects');
320     symlink '../../../../objects','.git/objects' or die $!;
321     runcmd @git, qw(add -Af);
322     my $tree = cmdoutput @git, qw(write-tree);
323     $tree =~ m/^\w+$/ or die "$tree ?";
324     return ($tree,$dir);
325 }
326
327 sub dsc_files () {
328     map {
329         m/^\w+ \d+ (\S+)$/ or die "$_ ?";
330         $1;
331     } grep m/\S/, split /\n/, ($dsc->{'Checksums-Sha256'} || $dsc->{Files});
332 }
333
334 sub is_orig_file ($) {
335     local ($_) = @_;
336     m/\.orig(?:-\w+)?\.tar\.\w+$/;
337 }
338
339 sub make_commit ($) {
340     my ($file) = @_;
341     return cmdoutput @git, qw(hash-object -w -t commit), $file;
342 }
343
344 sub generate_commit_from_dsc () {
345     prep_ud();
346     chdir $ud or die $!;
347     my @files;
348     foreach my $f (dsc_files()) {
349         die if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
350         push @files, $f;
351         link "../../../$f", $f
352             or $!==&ENOENT
353             or die "$f $!";
354     }
355     runcmd @dget, qw(--), $dscurl;
356     foreach my $f (grep { is_orig_file($_) } @files) {
357         link $f, "../../../../$f"
358             or $!==&EEXIST
359             or die "$f $!";
360     }
361     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
362     runcmd qw(sh -ec), 'dpkg-parsechangelog >../changelog.tmp';
363     my $clogp = parsecontrol('../changelog.tmp','changelog') or die;
364     my $date = cmdoutput qw(date), '+%s %z', qw(-d),$clogp->{Date};
365     my $author = $clogp->{Maintainer};
366     $author =~ s#,.*##ms;
367     my $authline = "$author $date";
368     $authline =~ m/^[^<>]+ \<\S+\> \d+ [-+]\d+$/ or die $authline;
369     open C, ">../commit.tmp" or die $!;
370     print C <<END or die $!;
371 tree $tree
372 author $authline
373 committer $authline
374
375 $clogp->{Changes}
376
377 # imported from the archive
378 END
379     close C or die $!;
380     my $outputhash = make_commit qw(../commit.tmp);
381     print "synthesised git commit from .dsc $clogp->{Version}\n";
382     if ($upload_hash) {
383         runcmd @git, qw(reset --hard), $upload_hash;
384         runcmd qw(sh -ec), 'dpkg-parsechangelog >>../changelogold.tmp';
385         my $oldclogp = Dpkg::Control::Hash->new();
386         $oldclogp->load('../changelogold.tmp','previous changelog') or die;
387         my $vcmp =
388             version_compare_string($oldclogp->{Version}, $clogp->{Version});
389         if ($vcmp < 0) {
390             # git upload/ is earlier vsn than archive, use archive
391             open C, ">../commit2.tmp" or die $!;
392             print C <<END or die $!;
393 tree $tree
394 parent $upload_hash
395 parent $outputhash
396 author $authline
397 committer $authline
398
399 Record $package ($clogp->{Version}) in archive suite $suite
400 END
401             $outputhash = make_commit qw(../commit2.tmp);
402         } elsif ($vcmp > 0) {
403             print STDERR <<END or die $!;
404 Version actually in archive:    $clogp->{Version} (older)
405 Last allegedly pushed/uploaded: $oldclogp->{Version} (newer or same)
406 Perhaps the upload is stuck in incoming.  Using the version from git.
407 END
408             $outputhash = $upload_hash;
409         } else {
410             die "version in archive is same as version in git".
411                 " to-be-uploaded (upload/) branch but archive".
412                 " version hash no commit hash?!\n";
413         }
414     }
415     chdir '../../../..' or die $!;
416     runcmd @git, qw(update-ref -m),"dgit fetch import $clogp->{Version}",
417             'DGIT_ARCHIVE', $outputhash;
418     cmdoutput @git, qw(log -n2), $outputhash;
419     # ... gives git a chance to complain if our commit is malformed
420     rmtree($ud);
421     return $outputhash;
422 }
423
424 sub ensure_we_have_orig () {
425     foreach my $f (dsc_files()) {
426         next unless is_orig_file($f);
427         if (stat "../$f") {
428             die "$f ?" unless -f _;
429         } else {
430             die "$f $!" unless $!==&ENOENT;
431         }
432         my $origurl = $dscurl;
433         $origurl =~ s{/[^/]+$}{};
434         $origurl .= "/$f";
435         die "$f ?" unless $f =~ m/^${package}_/;
436         die "$f ?" if $f =~ m#/#;
437         runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
438             @dget,'--',$origurl;
439     }
440 }
441
442 sub rev_parse ($) {
443     return cmdoutput @git, qw(rev-parse), "$_[0]~0";
444 }
445
446 sub is_fast_fwd ($$) {
447     my ($ancestor,$child) = @_;
448     my $mb = cmdoutput @git, qw(merge-base), $ancestor, $child;
449     return rev_parse($mb) eq rev_parse($ancestor);
450 }
451
452 sub git_fetch_us () {
453     die "cannot dry run with fetch" if $dryrun;
454     runcmd @git, qw(fetch),access_giturl(),fetchspec();
455 }
456
457 sub fetch_from_archive () {
458     # ensures that lrref() is what is actually in the archive,
459     #  one way or another
460     get_archive_dsc() or return 0;
461     $dsc_hash = $dsc->{$ourdscfield};
462     if (defined $dsc_hash) {
463         $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
464         $dsc_hash = $&;
465         print "last upload to archive specified git hash\n";
466     } else {
467         print "last upload to archive has NO git hash\n";
468     }
469
470     my $lrref_fn = ".git/".lrref();
471     if (open H, $lrref_fn) {
472         $upload_hash = <H>;
473         chomp $upload_hash;
474         die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
475     } elsif ($! == &ENOENT) {
476         $upload_hash = '';
477     } else {
478         die "$lrref_fn $!";
479     }
480     print DEBUG "last upload hash $upload_hash\n";
481     my $hash;
482     if (defined $dsc_hash) {
483         die "missing git history even though dsc has hash"
484             unless $upload_hash;
485         $hash = $dsc_hash;
486         ensure_we_have_orig();
487     } else {
488         $hash = generate_commit_from_dsc();
489     }
490     print DEBUG "current hash $hash\n";
491     if ($upload_hash) {
492         die "not fast forward on last upload branch!".
493             " (archive's version left in DGIT_ARCHIVE)"
494             unless is_fast_fwd($upload_hash, $hash);
495     }
496     if ($upload_hash ne $hash) {
497         my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
498         if (!$dryrun) {
499             cmdoutput @upd_cmd;
500         } else {
501             dryrun_report @upd_cmd;
502         }
503     }
504     return 1;
505 }
506
507 sub clone ($) {
508     my ($dstdir) = @_;
509     die "dry run makes no sense with clone" if $dryrun;
510     mkdir $dstdir or die "$dstdir $!";
511     chdir "$dstdir" or die "$dstdir $!";
512     runcmd @git, qw(init -q);
513     runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
514     open H, "> .git/HEAD" or die $!;
515     print H "ref: ".lref()."\n" or die $!;
516     close H or die $!;
517     runcmd @git, qw(remote add), 'origin', access_giturl();
518     if (check_for_git()) {
519         print "fetching existing git history\n";
520         git_fetch_us();
521         runcmd @git, qw(fetch origin);
522     } else {
523         print "starting new git history\n";
524     }
525     fetch_from_archive() or die;
526     runcmd @git, qw(reset --hard), lrref();
527     print "dgit ok: ready for work in $dstdir\n";
528 }
529
530 sub fetch () {
531     if (check_for_git()) {
532         git_fetch_us();
533     }
534     fetch_from_archive() or die;
535     print "dgit ok: fetched into ".lrref()."\n";
536 }
537
538 sub pull () {
539     fetch();
540     runcmd_ordryrun @git, qw(merge -m),"Merge from $suite [dgit]",
541         lrref();
542     print "dgit ok: fetched to ".lrref()." and merged into HEAD\n";
543 }
544
545 sub dopush () {
546     runcmd @git, qw(diff --quiet HEAD);
547     my $clogp = parsechangelog();
548     $package = $clogp->{Source};
549     my $dscfn = "${package}_$clogp->{Version}.dsc";
550     stat "../$dscfn" or die "$dscfn $!";
551     $dsc = parsecontrol("../$dscfn");
552     prep_ud();
553     chdir $ud or die $!;
554     print "checking that $dscfn corresponds to HEAD\n";
555     runcmd qw(dpkg-source -x --), "../../../../$dscfn";
556     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
557     chdir '../../../..' or die $!;
558     runcmd @git, qw(diff --exit-code), $tree;
559 #fetch from alioth
560 #do fast forward check and maybe fake merge
561 #    if (!is_fast_fwd(mainbranch
562 #    runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
563 #        map { lref($_).":".rref($_) }
564 #        (uploadbranch());
565     $dsc->{$ourdscfield} = rev_parse('HEAD');
566     $dsc->save("../$dscfn.tmp") or die $!;
567     if (!$dryrun) {
568         rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
569     } else {
570         print "[new .dsc left in $dscfn.tmp]\n";
571     }
572     if (!$changesfile) {
573         my $pat = "../${package}_$clogp->{Version}_*.changes";
574         my @cs = glob $pat;
575         die "$pat ?" unless @cs==1;
576         ($changesfile) = @cs;
577     }
578     my $tag = debiantag($dsc->{Version});
579     if (!check_for_git()) {
580         create_remote_git_repo();
581     }
582     runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
583     if ($sign) {
584         my @tag_cmd = (@git, qw(tag -s -m),
585                        "Release $dsc->{Version} for $suite [dgit]");
586         push @tag_cmd, qw(-u),$keyid if defined $keyid;
587         push @tag_cmd, $tag;
588         runcmd_ordryrun @tag_cmd;
589         my @debsign_cmd = @debsign;
590         push @debsign_cmd, "-k$keyid" if defined $keyid;
591         push @debsign_cmd, $changesfile;
592         runcmd_ordryrun @debsign_cmd;
593     }
594     runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
595     my $host = access_cfg('upload-host');
596     my @hostarg = defined($host) ? ($host,) : ();
597     runcmd_ordryrun @dput, @hostarg, $changesfile;
598     print "dgit ok: pushed and uploaded $dsc->{Version}\n";
599 }
600
601 sub cmd_clone {
602     parseopts();
603     my $dstdir;
604     die if defined $package;
605     if (@ARGV==1) {
606         ($package) = @ARGV;
607     } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
608         ($package,$suite) = @ARGV;
609     } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
610         ($package,$dstdir) = @ARGV;
611     } elsif (@ARGV==3) {
612         ($package,$suite,$dstdir) = @ARGV;
613     } else {
614         die;
615     }
616     $dstdir ||= "$package";
617     clone($dstdir);
618 }
619
620 sub branchsuite () {
621     my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
622     if ($branch =~ m#$lbranch_re#o) {
623         return $1;
624     } else {
625         return undef;
626     }
627 }
628
629 sub fetchpullargs () {
630     if (!defined $package) {
631         my $sourcep = parsecontrol('debian/control');
632         $package = $sourcep->{Source};
633     }
634     if (@ARGV==0) {
635         $suite = branchsuite();
636         if (!$suite) {
637             my $clogp = parsechangelog();
638             $suite = $clogp->{Distribution};
639         }
640         canonicalise_suite();
641         print "fetching from suite $suite\n";
642     } elsif (@ARGV==1) {
643         ($suite) = @ARGV;
644         canonicalise_suite();
645     } else {
646         die;
647     }
648 }
649
650 sub cmd_fetch {
651     parseopts();
652     fetchpullargs();
653     fetch();
654 }
655
656 sub cmd_pull {
657     parseopts();
658     fetchpullargs();
659     pull();
660 }
661
662 sub cmd_push {
663     parseopts();
664     die if defined $package;
665     my $clogp = parsechangelog();
666     $package = $clogp->{Source};
667     if (@ARGV==0) {
668         $suite = $clogp->{Distribution};
669         if ($new_package) {
670             local ($package) = $existing_package; # this is a hack
671             canonicalise_suite();
672         }
673     } else {
674         die;
675     }
676     if (fetch_from_archive()) {
677         is_fast_fwd(lrref(), 'HEAD') or die;
678     } else {
679         die unless $new_package;
680     }
681     dopush();
682 }
683
684 sub cmd_build {
685     # we pass further options and args to git-buildpackage
686     die if defined $package;
687     my $clogp = parsechangelog();
688     $suite = $clogp->{Distribution};
689     $package = $clogp->{Source};
690     runcmd_ordryrun
691         qw(git-buildpackage -us -uc --git-no-sign-tags),
692         '--git-builder=dpkg-buildpackage -i\.git/ -I.git',
693         "--git-debian-branch=".lbranch(),
694         @ARGV;
695 }
696
697 sub parseopts () {
698     my $om;
699     while (@ARGV) {
700         last unless $ARGV[0] =~ m/^-/;
701         $_ = shift @ARGV;
702         last if m/^--?$/;
703         if (m/^--/) {
704             if (m/^--dry-run$/) {
705                 $dryrun=1;
706             } elsif (m/^--no-sign$/) {
707                 $sign=0;
708             } elsif (m/^--new$/) {
709                 $new_package=1;
710             } elsif (m/^--(\w+)=(.*)/s && ($om = $opts_opt_map{$1})) {
711                 $om->[0] = $2;
712             } elsif (m/^--(\w+):(.*)/s && ($om = $opts_opt_map{$1})) {
713                 push @$om, $2;
714             } elsif (m/^--existing-package=(.*)/s) {
715                 $existing_package = $1;
716             } else {
717                 die "$_ ?";
718             }
719         } else {
720             while (m/^-./s) {
721                 if (s/^-n/-/) {
722                     $dryrun=1;
723                 } elsif (s/^-D/-/) {
724                     open DEBUG, ">&STDERR" or die $!;
725                     $debug++;
726                 } elsif (s/^-N/-/) {
727                     $new_package=1;
728                 } elsif (s/^-c(.*=.*)//s) {
729                     push @git, '-c', $1;
730                 } elsif (s/^-C(.*)//s) {
731                     $changesfile = $1;
732                 } elsif (s/^-k(.*)//s) {
733                     $keyid=$1;
734                 } else {
735                     die "$_ ?";
736                 }
737             }
738         }
739     }
740 }
741
742 parseopts();
743 die unless @ARGV;
744 my $cmd = shift @ARGV;
745
746 { no strict qw(refs); &{"cmd_$cmd"}(); }