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