chiark / gitweb /
New --initiator-tempdir option.
[dgit.git] / dgit
diff --git a/dgit b/dgit
index e46da88a00d4e8906722d4a288bd32e3a5061b94..60020fc2fe8d09ede9d17bc7d08c8900706b8037 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -38,7 +38,7 @@ our $package;
 our @ropts;
 
 our $sign = 1;
-our $dryrun = 0;
+our $dryrun_level = 0;
 our $changesfile;
 our $new_package = 0;
 our $ignoredirty = 0;
@@ -46,6 +46,7 @@ our $noquilt = 0;
 our $existing_package = 'dpkg';
 our $cleanmode = 'dpkg-source';
 our $we_are_responder;
+our $initiator_tempdir;
 
 our %format_ok = map { $_=>1 } ("1.0","3.0 (native)","3.0 (quilt)");
 
@@ -133,6 +134,12 @@ sub fetchspec () {
     return  "+".rrref().":".lrref();
 }
 
+sub changedir ($) {
+    my ($newdir) = @_;
+    printdebug "CD $newdir\n";
+    chdir $newdir or die "chdir: $newdir: $!";
+}
+
 #---------- remote protocol support, common ----------
 
 # remote push initiator/responder protocol:
@@ -223,9 +230,9 @@ sub protocol_receive_file ($$) {
        my ($y,$l) = protocol_expect {
            m/^data-block (.*})$|data-end$/;
            length $1 ? (1,$1) : (0);
-       } \*STDIN;
+       } $fh;
        last unless $y;
-       my $d = protocol_read_bytes \*STDIN, $1;
+       my $d = protocol_read_bytes $fh, $1;
        print PF $d or die $!;
     }
     printdebug "received into $ourfn\n";
@@ -337,8 +344,11 @@ sub runcmd {
     failedcmd @_ if system @_;
 }
 
+sub act_local () { return $dryrun_level <= 1; }
+sub act_scary () { return !$dryrun_level; }
+
 sub printdone {
-    if (!$dryrun) {
+    if (!$dryrun_level) {
        progress "dgit ok: @_";
     } else {
        progress "would be ok: @_ (but dry run only)";
@@ -371,7 +381,7 @@ sub dryrun_report {
 }
 
 sub runcmd_ordryrun {
-    if (!$dryrun) {
+    if (act_scary()) {
        runcmd @_;
     } else {
        dryrun_report @_;
@@ -379,7 +389,7 @@ sub runcmd_ordryrun {
 }
 
 sub runcmd_ordryrun_local {
-    if ($dryrun <= 1) {
+    if (act_local()) {
        runcmd @_;
     } else {
        dryrun_report @_;
@@ -711,7 +721,7 @@ sub mktree_in_ud_from_only_subdir () {
     die unless @dirs==1;
     $dirs[0] =~ m#^([^/]+)/\.$# or die;
     my $dir = $1;
-    chdir $dir or die "$dir $!";
+    changedir $dir;
     fail "source package contains .git directory" if stat '.git';
     die $! unless $!==&ENOENT;
     runcmd qw(git init -q);
@@ -778,7 +788,7 @@ sub clogp_authline ($) {
 
 sub generate_commit_from_dsc () {
     prep_ud();
-    chdir $ud or die $!;
+    changedir $ud;
     my @files;
     foreach my $f (dsc_files()) {
        die "$f ?" if $f =~ m#/|^\.|\.dsc$|\.tmp$#;
@@ -844,7 +854,7 @@ END
            $outputhash = $lastpush_hash;
        }
     }
-    chdir '../../../..' or die $!;
+    changedir '../../../..';
     runcmd @git, qw(update-ref -m),"dgit fetch import $cversion",
             'DGIT_ARCHIVE', $outputhash;
     cmdoutput @git, qw(log -n2), $outputhash;
@@ -1004,7 +1014,7 @@ END
     }
     if ($lastpush_hash ne $hash) {
        my @upd_cmd = (@git, qw(update-ref -m), 'dgit fetch', lrref(), $hash);
-       if ($dryrun <= 1) {
+       if (act_local()) {
            cmdoutput @upd_cmd;
        } else {
            dryrun_report @upd_cmd;
@@ -1016,9 +1026,9 @@ END
 sub clone ($) {
     my ($dstdir) = @_;
     canonicalise_suite();
-    badusage "dry run makes no sense with clone" if $dryrun > 1;
+    badusage "dry run makes no sense with clone" unless act_local();
     mkdir $dstdir or die "$dstdir $!";
-    chdir "$dstdir" or die "$dstdir $!";
+    changedir $dstdir;
     runcmd @git, qw(init -q);
     runcmd @git, qw(config), "remote.$remotename.fetch", fetchspec();
     open H, "> .git/HEAD" or die $!;
@@ -1163,7 +1173,7 @@ END
        push @sign_cmd, qw(-u),$keyid if defined $keyid;
        push @sign_cmd, $tfn->('.tmp');
        runcmd_ordryrun @sign_cmd;
-       if (!$dryrun) {
+       if (act_scary()) {
            $tagobjfn = $tfn->('.signed.tmp');
            runcmd shell_cmd "exec >$tagobjfn", qw(cat --),
                $tfn->('.tmp'), $tfn->('.tmp.asc');
@@ -1210,11 +1220,11 @@ sub dopush () {
        commit_quilty_patch();
     }
     check_not_dirty();
-    chdir $ud or die $!;
+    changedir $ud;
     progress "checking that $dscfn corresponds to HEAD";
     runcmd qw(dpkg-source -x --), "../../../../$dscfn";
     my ($tree,$dir) = mktree_in_ud_from_only_subdir();
-    chdir '../../../..' or die $!;
+    changedir '../../../..';
     my @diffcmd = (@git, qw(diff --exit-code), $tree);
     printcmd \*DEBUG,$debugprefix."+",@diffcmd;
     $!=0; $?=0;
@@ -1272,7 +1282,7 @@ sub dopush () {
     runcmd_ordryrun @git, qw(update-ref -m), 'dgit push', lrref(), 'HEAD';
 
     if (!$we_are_responder) {
-       if ($dryrun <= 1) {
+       if (act_local()) {
            rename "../$dscfn.tmp","../$dscfn" or die "$dscfn $!";
        } else {
            progress "[new .dsc left in $dscfn.tmp]";
@@ -1280,7 +1290,7 @@ sub dopush () {
     }
 
     if ($we_are_responder) {
-       my $dryrunsuffix = $dryrun > 1 ? ".tmp" : "";
+       my $dryrunsuffix = act_local() ? "" : ".tmp";
        responder_receive_files('signed-dsc-changes',
                                "../$dscfn$dryrunsuffix",
                                "$changesfile$dryrunsuffix");
@@ -1407,7 +1417,7 @@ sub cmd_remote_push_responder {
     @ARGV = @ARGV[$nrargs..$#ARGV];
     die unless @rargs;
     my ($dir) = @rargs;
-    chdir $dir or die "$dir: $!";
+    changedir $dir;
     $we_are_responder = 1;
     $debugprefix = ' ';
     autoflush STDOUT 1;
@@ -1420,7 +1430,8 @@ our $i_tmp;
 sub i_cleanup {
     local ($@);
     return unless defined $i_tmp;
-    chdir "/" or die $!;
+    return if defined $initiator_tempdir;
+    changedir "/";
     eval { rmtree $i_tmp; };
 }
 
@@ -1449,9 +1460,15 @@ sub cmd_rpush {
     my @cmd = (@ssh, $host, shellquote @rdgit);
     printcmd \*DEBUG,$debugprefix."+",@cmd;
     eval {
-       $i_tmp = tempdir();
+       if (defined $initiator_tempdir) {
+           rmtree $initiator_tempdir;
+           mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
+           $i_tmp = $initiator_tempdir;
+       } else {
+           $i_tmp = tempdir();
+       }
        my $pid = open2(\*RO, \*RI, @cmd);
-       chdir $i_tmp or die "$i_tmp $!";
+       changedir $i_tmp;
        initiator_expect { m/^dgit-remote-push-ready/ };
        for (;;) {
            my ($icmd,$iargs) = initiator_expect {
@@ -1664,9 +1681,9 @@ sub build_source {
        }
        my $pwd = cmdoutput qw(env - pwd);
        my $leafdir = basename $pwd;
-       chdir ".." or die $!;
+       changedir "..";
        runcmd_ordryrun_local @dpkgsource, qw(-b --), $leafdir;
-       chdir $pwd or die $!;
+       changedir $pwd;
        runcmd_ordryrun_local qw(sh -ec),
            'exec >$1; shift; exec "$@"','x',
            "../$sourcechanges",
@@ -1682,9 +1699,9 @@ sub cmd_build_source {
 
 sub cmd_sbuild {
     build_source();
-    chdir ".." or die $!;
+    changedir "..";
     my $pat = "${package}_".(stripepoch $version)."_*.changes";
-    if ($dryrun <= 1) {
+    if (act_local()) {
        stat $dscfn or fail "$dscfn (in parent directory): $!";
        stat $sourcechanges or fail "$sourcechanges (in parent directory): $!";
        foreach my $cf (glob $pat) {
@@ -1692,10 +1709,10 @@ sub cmd_sbuild {
            unlink $cf or fail "remove $cf: $!";
        }
     }
-    runcmd_ordryrun @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
-    runcmd_ordryrun @mergechanges, glob $pat;
+    runcmd_ordryrun_local @sbuild, @ARGV, qw(-d), $isuite, $dscfn;
+    runcmd_ordryrun_local @mergechanges, glob $pat;
     my $multichanges = "${package}_".(stripepoch $version)."_multi.changes";
-    if ($dryrun <= 1) {
+    if (act_local()) {
        stat $multichanges or fail "$multichanges: $!";
     }
     printdone "build successful, results in $multichanges\n" or die $!;
@@ -1731,10 +1748,10 @@ sub parseopts () {
        if (m/^--/) {
            if (m/^--dry-run$/) {
                push @ropts, $_;
-               $dryrun=2;
+               $dryrun_level=2;
            } elsif (m/^--damp-run$/) {
                push @ropts, $_;
-               $dryrun=1;
+               $dryrun_level=1;
            } elsif (m/^--no-sign$/) {
                push @ropts, $_;
                $sign=0;
@@ -1758,6 +1775,11 @@ sub parseopts () {
            } elsif (m/^--existing-package=(.*)/s) {
                push @ropts, $_;
                $existing_package = $1;
+           } elsif (m/^--initiator-tempdir=(.*)/s) {
+               $initiator_tempdir = $1;
+               $initiator_tempdir =~ m#^/# or
+                   badusage "--initiator-tempdir must be used specify an".
+                       " absolute, not relative, directory."
            } elsif (m/^--distro=(.*)/s) {
                push @ropts, $_;
                $idistro = $1;
@@ -1779,10 +1801,10 @@ sub parseopts () {
            while (m/^-./s) {
                if (s/^-n/-/) {
                    push @ropts, $&;
-                   $dryrun=2;
+                   $dryrun_level=2;
                } elsif (s/^-L/-/) {
                    push @ropts, $&;
-                   $dryrun=1;
+                   $dryrun_level=1;
                } elsif (s/^-h/-/) {
                    cmd_help();
                } elsif (s/^-D/-/) {
@@ -1832,8 +1854,9 @@ if ($ENV{$fakeeditorenv}) {
 delete $ENV{'DGET_UNPACK'};
 
 parseopts();
-print STDERR "DRY RUN ONLY\n" if $dryrun > 1;
-print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n" if $dryrun == 1;
+print STDERR "DRY RUN ONLY\n" if $dryrun_level > 1;
+print STDERR "DAMP RUN - WILL MAKE LOCAL (UNSIGNED) CHANGES\n"
+    if $dryrun_level == 1;
 if (!@ARGV) {
     print STDERR $helpmsg or die $!;
     exit 8;