chiark / gitweb /
da4e57c3d9f3542b83ebb60c526307a83241193f
[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         } elsif ($outputhash ne $upload_hash) {
418             die "version in archive ($clogp->{Version})".
419                 " is same as version in git".
420                 " to-be-uploaded (upload/) branch ($oldclogp->{Version})".
421                 " but archive version hash no commit hash?!\n";
422         }
423     }
424     chdir '../../../..' or die $!;
425     runcmd @git, qw(update-ref -m),"dgit fetch import $clogp->{Version}",
426             'DGIT_ARCHIVE', $outputhash;
427     cmdoutput @git, qw(log -n2), $outputhash;
428     # ... gives git a chance to complain if our commit is malformed
429     rmtree($ud);
430     return $outputhash;
431 }
432
433 sub ensure_we_have_orig () {
434     foreach my $f (dsc_files()) {
435         next unless is_orig_file($f);
436         if (stat "../$f") {
437             die "$f ?" unless -f _;
438         } else {
439             die "$f $!" unless $!==&ENOENT;
440         }
441         my $origurl = $dscurl;
442         $origurl =~ s{/[^/]+$}{};
443         $origurl .= "/$f";
444         die "$f ?" unless $f =~ m/^${package}_/;
445         die "$f ?" if $f =~ m#/#;
446         runcmd_ordryrun qw(sh -ec),'cd ..; exec "$@"','x',
447             @dget,'--',$origurl;
448     }
449 }
450
451 sub rev_parse ($) {
452     return cmdoutput @git, qw(rev-parse), "$_[0]~0";
453 }
454
455 sub is_fast_fwd ($$) {
456     my ($ancestor,$child) = @_;
457     my $mb = cmdoutput @git, qw(merge-base), $ancestor, $child;
458     return rev_parse($mb) eq rev_parse($ancestor);
459 }
460
461 sub git_fetch_us () {
462     die "cannot dry run with fetch" if $dryrun;
463     runcmd @git, qw(fetch),access_giturl(),fetchspec();
464 }
465
466 sub fetch_from_archive () {
467     # ensures that lrref() is what is actually in the archive,
468     #  one way or another
469     get_archive_dsc() or return 0;
470     $dsc_hash = $dsc->{$ourdscfield};
471     if (defined $dsc_hash) {
472         $dsc_hash =~ m/\w+/ or die "$dsc_hash $?";
473         $dsc_hash = $&;
474         print "last upload to archive specified git hash\n";
475     } else {
476         print "last upload to archive has NO git hash\n";
477     }
478
479     my $lrref_fn = ".git/".lrref();
480     if (open H, $lrref_fn) {
481         $upload_hash = <H>;
482         chomp $upload_hash;
483         die "$lrref_fn $upload_hash ?" unless $upload_hash =~ m/^\w+$/;
484     } elsif ($! == &ENOENT) {
485         $upload_hash = '';
486     } else {
487         die "$lrref_fn $!";
488     }
489     print DEBUG "previous reference hash $upload_hash\n";
490     my $hash;
491     if (defined $dsc_hash) {
492         die "missing git history even though dsc has hash"
493             unless $upload_hash;
494         $hash = $dsc_hash;
495         ensure_we_have_orig();
496     } else {
497         $hash = generate_commit_from_dsc();
498     }
499     print DEBUG "current hash $hash\n";
500     if ($upload_hash) {
501         die "not fast forward on last upload branch!".
502             " (archive's version left in DGIT_ARCHIVE)"
503             unless is_fast_fwd($upload_hash, $hash);
504     }
505     if ($upload_hash ne $hash) {
506         my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
507         if (!$dryrun) {
508             cmdoutput @upd_cmd;
509         } else {
510             dryrun_report @upd_cmd;
511         }
512     }
513     return 1;
514 }
515
516 sub clone ($) {
517     my ($dstdir) = @_;
518     die "dry run makes no sense with clone" if $dryrun;
519     mkdir $dstdir or die "$dstdir $!";
520     chdir "$dstdir" or die "$dstdir $!";
521     runcmd @git, qw(init -q);
522     runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
523     open H, "> .git/HEAD" or die $!;
524     print H "ref: ".lref()."\n" or die $!;
525     close H or die $!;
526     runcmd @git, qw(remote add), 'origin', access_giturl();
527     if (check_for_git()) {
528         print "fetching existing git history\n";
529         git_fetch_us();
530         runcmd @git, qw(fetch origin);
531     } else {
532         print "starting new git history\n";
533     }
534     fetch_from_archive() or die;
535     runcmd @git, qw(reset --hard), lrref();
536     printdone "ready for work in $dstdir";
537 }
538
539 sub fetch () {
540     if (check_for_git()) {
541         git_fetch_us();
542     }
543     fetch_from_archive() or die;
544     printdone "fetched into ".lrref();
545 }
546
547 sub pull () {
548     fetch();
549     runcmd_ordryrun @git, qw(merge -m),"Merge from $suite [dgit]",
550         lrref();
551     printdone "fetched to ".lrref()." and merged into HEAD";
552 }
553
554 sub check_not_dirty () {
555     my $output = cmdoutput @git, qw(status --porcelain);
556     if (length $output) {
557         my $m = "tree dirty:\n$output\n";
558         if (!$dryrun) {
559             die $m;
560         } else {
561             warn $m;
562         }
563     }
564 }
565
566 sub dopush () {
567     runcmd @git, qw(diff --quiet HEAD);
568     my $clogp = parsechangelog();
569     $package = $clogp->{Source};
570     my $dscfn = "${package}_$clogp->{Version}.dsc";
571     stat "../$dscfn" or die "$dscfn $!";
572     $dsc = parsecontrol("../$dscfn");
573     check_not_dirty();
574     prep_ud();
575     chdir $ud or die $!;
576     print "checking that $dscfn corresponds to HEAD\n";
577     runcmd qw(dpkg-source -x --), "../../../../$dscfn";
578     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
579     chdir '../../../..' or die $!;
580     runcmd @git, qw(diff --exit-code), $tree;
581 #fetch from alioth
582 #do fast forward check and maybe fake merge
583 #    if (!is_fast_fwd(mainbranch
584 #    runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
585 #        map { lref($_).":".rref($_) }
586 #        (uploadbranch());
587     $dsc->{$ourdscfield} = rev_parse('HEAD');
588     $dsc->save("../$dscfn.tmp") or die $!;
589     if (!$dryrun) {
590         rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
591     } else {
592         print "[new .dsc left in $dscfn.tmp]\n";
593     }
594     if (!$changesfile) {
595         my $pat = "../${package}_$clogp->{Version}_*.changes";
596         my @cs = glob $pat;
597         die "$pat ?" unless @cs==1;
598         ($changesfile) = @cs;
599     }
600     my $tag = debiantag($dsc->{Version});
601     if (!check_for_git()) {
602         create_remote_git_repo();
603     }
604     runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
605     if ($sign) {
606         my @tag_cmd = (@git, qw(tag -s -m),
607                        "Release $dsc->{Version} for $suite [dgit]");
608         push @tag_cmd, qw(-u),$keyid if defined $keyid;
609         push @tag_cmd, $tag;
610         runcmd_ordryrun @tag_cmd;
611         my @debsign_cmd = @debsign;
612         push @debsign_cmd, "-k$keyid" if defined $keyid;
613         push @debsign_cmd, $changesfile;
614         runcmd_ordryrun @debsign_cmd;
615     }
616     runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
617     my $host = access_cfg('upload-host');
618     my @hostarg = defined($host) ? ($host,) : ();
619     runcmd_ordryrun @dput, @hostarg, $changesfile;
620     printdone "pushed and uploaded $dsc->{Version}";
621 }
622
623 sub cmd_clone {
624     parseopts();
625     my $dstdir;
626     die if defined $package;
627     if (@ARGV==1) {
628         ($package) = @ARGV;
629     } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
630         ($package,$suite) = @ARGV;
631     } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
632         ($package,$dstdir) = @ARGV;
633     } elsif (@ARGV==3) {
634         ($package,$suite,$dstdir) = @ARGV;
635     } else {
636         die;
637     }
638     $dstdir ||= "$package";
639     clone($dstdir);
640 }
641
642 sub branchsuite () {
643     my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
644     if ($branch =~ m#$lbranch_re#o) {
645         return $1;
646     } else {
647         return undef;
648     }
649 }
650
651 sub fetchpullargs () {
652     if (!defined $package) {
653         my $sourcep = parsecontrol('debian/control');
654         $package = $sourcep->{Source};
655     }
656     if (@ARGV==0) {
657         $suite = branchsuite();
658         if (!$suite) {
659             my $clogp = parsechangelog();
660             $suite = $clogp->{Distribution};
661         }
662         canonicalise_suite();
663         print "fetching from suite $suite\n";
664     } elsif (@ARGV==1) {
665         ($suite) = @ARGV;
666         canonicalise_suite();
667     } else {
668         die;
669     }
670 }
671
672 sub cmd_fetch {
673     parseopts();
674     fetchpullargs();
675     fetch();
676 }
677
678 sub cmd_pull {
679     parseopts();
680     fetchpullargs();
681     pull();
682 }
683
684 sub cmd_push {
685     parseopts();
686     die if defined $package;
687     runcmd @git, qw(diff --quiet HEAD);
688     my $clogp = parsechangelog();
689     $package = $clogp->{Source};
690     if (@ARGV==0) {
691         $suite = $clogp->{Distribution};
692         if ($new_package) {
693             local ($package) = $existing_package; # this is a hack
694             canonicalise_suite();
695         }
696     } else {
697         die;
698     }
699     if (fetch_from_archive()) {
700         is_fast_fwd(lrref(), 'HEAD') or die;
701     } else {
702         die unless $new_package;
703     }
704     dopush();
705 }
706
707 sub cmd_build {
708     # we pass further options and args to git-buildpackage
709     die if defined $package;
710     my $clogp = parsechangelog();
711     $suite = $clogp->{Distribution};
712     $package = $clogp->{Source};
713     runcmd_ordryrun
714         qw(git-buildpackage -us -uc --git-no-sign-tags),
715         '--git-builder=dpkg-buildpackage -i\.git/ -I.git',
716         "--git-debian-branch=".lbranch(),
717         @ARGV;
718 }
719
720 sub parseopts () {
721     my $om;
722     while (@ARGV) {
723         last unless $ARGV[0] =~ m/^-/;
724         $_ = shift @ARGV;
725         last if m/^--?$/;
726         if (m/^--/) {
727             if (m/^--dry-run$/) {
728                 $dryrun=1;
729             } elsif (m/^--no-sign$/) {
730                 $sign=0;
731             } elsif (m/^--new$/) {
732                 $new_package=1;
733             } elsif (m/^--(\w+)=(.*)/s && ($om = $opts_opt_map{$1})) {
734                 $om->[0] = $2;
735             } elsif (m/^--(\w+):(.*)/s && ($om = $opts_opt_map{$1})) {
736                 push @$om, $2;
737             } elsif (m/^--existing-package=(.*)/s) {
738                 $existing_package = $1;
739             } else {
740                 die "$_ ?";
741             }
742         } else {
743             while (m/^-./s) {
744                 if (s/^-n/-/) {
745                     $dryrun=1;
746                 } elsif (s/^-D/-/) {
747                     open DEBUG, ">&STDERR" or die $!;
748                     $debug++;
749                 } elsif (s/^-N/-/) {
750                     $new_package=1;
751                 } elsif (s/^-c(.*=.*)//s) {
752                     push @git, '-c', $1;
753                 } elsif (s/^-C(.*)//s) {
754                     $changesfile = $1;
755                 } elsif (s/^-k(.*)//s) {
756                     $keyid=$1;
757                 } else {
758                     die "$_ ?";
759                 }
760             }
761         }
762     }
763 }
764
765 parseopts();
766 print STDERR "DRY RUN ONLY\n" if $dryrun;
767 die unless @ARGV;
768 my $cmd = shift @ARGV;
769
770 { no strict qw(refs); &{"cmd_$cmd"}(); }