chiark / gitweb /
487c1be7aa1ffb663636fe3678820c95d5434392
[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     runcmd @git, qw(diff --quiet);
556 }
557
558 sub commit_quilty_patch () {
559     my $output = cmdoutput @git, qw(status --porcelain);
560     my $vsn = $dsc->{Version};
561     my %fixups = map {$_=>1}
562         (".pc/debian-changes-$vsn/","debian/patches/debian-changes-$vsn");
563     my @files;
564     foreach my $l (split /\n/, $output) {
565         next unless $l =~ s/^\?\? //;
566         next unless $fixups{$l};
567         push @files, $l;
568     }
569     print DEBUG "checking for quilty\n", Dumper(\@files);
570     if (@files == 2) {
571         my $m = "Commit Debian 3.0 (quilt) metadata";
572         print "$m\n";
573         runcmd_ordryrun @git, qw(add), @files;
574         runcmd_ordryrun @git, qw(commit -m), $m;
575     }
576 }
577
578 sub dopush () {
579     print DEBUG "actually entering push\n";
580     my $clogp = parsechangelog();
581     $package = $clogp->{Source};
582     my $dscfn = "${package}_$clogp->{Version}.dsc";
583     stat "../$dscfn" or die "$dscfn $!";
584     $dsc = parsecontrol("../$dscfn");
585     print DEBUG "format $dsc->{Format}\n";
586     if ($dsc->{Format} eq '3.0 (quilt)') {
587         print "Format \`$dsc->{Format}', urgh\n";
588         commit_quilty_patch();
589     }
590     check_not_dirty();
591     prep_ud();
592     chdir $ud or die $!;
593     print "checking that $dscfn corresponds to HEAD\n";
594     runcmd qw(dpkg-source -x --), "../../../../$dscfn";
595     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
596     chdir '../../../..' or die $!;
597     runcmd @git, qw(diff --exit-code), $tree;
598 #fetch from alioth
599 #do fast forward check and maybe fake merge
600 #    if (!is_fast_fwd(mainbranch
601 #    runcmd @git, qw(fetch -p ), "$alioth_git/$package.git",
602 #        map { lref($_).":".rref($_) }
603 #        (uploadbranch());
604     $dsc->{$ourdscfield} = rev_parse('HEAD');
605     $dsc->save("../$dscfn.tmp") or die $!;
606     if (!$dryrun) {
607         rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
608     } else {
609         print "[new .dsc left in $dscfn.tmp]\n";
610     }
611     if (!$changesfile) {
612         my $pat = "../${package}_$clogp->{Version}_*.changes";
613         my @cs = glob $pat;
614         die "$pat ?" unless @cs==1;
615         ($changesfile) = @cs;
616     }
617     my $tag = debiantag($dsc->{Version});
618     if (!check_for_git()) {
619         create_remote_git_repo();
620     }
621     runcmd_ordryrun @git, qw(push),access_giturl(),"HEAD:".rrref();
622     if ($sign) {
623         my @tag_cmd = (@git, qw(tag -s -m),
624                        "Release $dsc->{Version} for $suite [dgit]");
625         push @tag_cmd, qw(-u),$keyid if defined $keyid;
626         push @tag_cmd, $tag;
627         runcmd_ordryrun @tag_cmd;
628         my @debsign_cmd = @debsign;
629         push @debsign_cmd, "-k$keyid" if defined $keyid;
630         push @debsign_cmd, $changesfile;
631         runcmd_ordryrun @debsign_cmd;
632     }
633     runcmd_ordryrun @git, qw(push),access_giturl(),"refs/tags/$tag";
634     my $host = access_cfg('upload-host');
635     my @hostarg = defined($host) ? ($host,) : ();
636     runcmd_ordryrun @dput, @hostarg, $changesfile;
637     printdone "pushed and uploaded $dsc->{Version}";
638 }
639
640 sub cmd_clone {
641     parseopts();
642     my $dstdir;
643     die if defined $package;
644     if (@ARGV==1) {
645         ($package) = @ARGV;
646     } elsif (@ARGV==2 && $ARGV[1] =~ m#^\w#) {
647         ($package,$suite) = @ARGV;
648     } elsif (@ARGV==2 && $ARGV[1] =~ m#^[./]#) {
649         ($package,$dstdir) = @ARGV;
650     } elsif (@ARGV==3) {
651         ($package,$suite,$dstdir) = @ARGV;
652     } else {
653         die;
654     }
655     $dstdir ||= "$package";
656     clone($dstdir);
657 }
658
659 sub branchsuite () {
660     my $branch = cmdoutput_errok @git, qw(symbolic-ref HEAD);
661     if ($branch =~ m#$lbranch_re#o) {
662         return $1;
663     } else {
664         return undef;
665     }
666 }
667
668 sub fetchpullargs () {
669     if (!defined $package) {
670         my $sourcep = parsecontrol('debian/control');
671         $package = $sourcep->{Source};
672     }
673     if (@ARGV==0) {
674         $suite = branchsuite();
675         if (!$suite) {
676             my $clogp = parsechangelog();
677             $suite = $clogp->{Distribution};
678         }
679         canonicalise_suite();
680         print "fetching from suite $suite\n";
681     } elsif (@ARGV==1) {
682         ($suite) = @ARGV;
683         canonicalise_suite();
684     } else {
685         die;
686     }
687 }
688
689 sub cmd_fetch {
690     parseopts();
691     fetchpullargs();
692     fetch();
693 }
694
695 sub cmd_pull {
696     parseopts();
697     fetchpullargs();
698     pull();
699 }
700
701 sub cmd_push {
702     parseopts();
703     die if defined $package;
704     runcmd @git, qw(diff --quiet HEAD);
705     my $clogp = parsechangelog();
706     $package = $clogp->{Source};
707     if (@ARGV==0) {
708         $suite = $clogp->{Distribution};
709         if ($new_package) {
710             local ($package) = $existing_package; # this is a hack
711             canonicalise_suite();
712         }
713     } else {
714         die;
715     }
716     if (fetch_from_archive()) {
717         is_fast_fwd(lrref(), 'HEAD') or die;
718     } else {
719         die unless $new_package;
720     }
721     dopush();
722 }
723
724 sub cmd_build {
725     # we pass further options and args to git-buildpackage
726     die if defined $package;
727     my $clogp = parsechangelog();
728     $suite = $clogp->{Distribution};
729     $package = $clogp->{Source};
730     canonicalise_suite() unless grep { m/^--git-debian-branch/ } @ARGV;
731     runcmd_ordryrun
732         qw(git-buildpackage -us -uc --git-no-sign-tags),
733         '--git-builder=dpkg-buildpackage -i\.git/ -I.git',
734         "--git-debian-branch=".lbranch(),
735         @ARGV;
736 }
737
738 sub parseopts () {
739     my $om;
740     while (@ARGV) {
741         last unless $ARGV[0] =~ m/^-/;
742         $_ = shift @ARGV;
743         last if m/^--?$/;
744         if (m/^--/) {
745             if (m/^--dry-run$/) {
746                 $dryrun=1;
747             } elsif (m/^--no-sign$/) {
748                 $sign=0;
749             } elsif (m/^--new$/) {
750                 $new_package=1;
751             } elsif (m/^--(\w+)=(.*)/s && ($om = $opts_opt_map{$1})) {
752                 $om->[0] = $2;
753             } elsif (m/^--(\w+):(.*)/s && ($om = $opts_opt_map{$1})) {
754                 push @$om, $2;
755             } elsif (m/^--existing-package=(.*)/s) {
756                 $existing_package = $1;
757             } else {
758                 die "$_ ?";
759             }
760         } else {
761             while (m/^-./s) {
762                 if (s/^-n/-/) {
763                     $dryrun=1;
764                 } elsif (s/^-D/-/) {
765                     open DEBUG, ">&STDERR" or die $!;
766                     $debug++;
767                 } elsif (s/^-N/-/) {
768                     $new_package=1;
769                 } elsif (s/^-c(.*=.*)//s) {
770                     push @git, '-c', $1;
771                 } elsif (s/^-C(.*)//s) {
772                     $changesfile = $1;
773                 } elsif (s/^-k(.*)//s) {
774                     $keyid=$1;
775                 } else {
776                     die "$_ ?";
777                 }
778             }
779         }
780     }
781 }
782
783 parseopts();
784 print STDERR "DRY RUN ONLY\n" if $dryrun;
785 die unless @ARGV;
786 my $cmd = shift @ARGV;
787
788 { no strict qw(refs); &{"cmd_$cmd"}(); }