chiark / gitweb /
wip changes for remote push - provide i_tmp
[dgit.git] / dgit
diff --git a/dgit b/dgit
index 848238134d5de95ba0b8ab3c5f4981300923bf67..bac0baf0237880ce10948c60a9a6258af278fb5e 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -28,6 +28,7 @@ use File::Basename;
 use Dpkg::Version;
 use POSIX;
 use IPC::Open2;
+use File::Temp;
 
 our $our_version = 'UNRELEASED'; ###substituted###
 
@@ -191,15 +192,22 @@ sub protocol_send_file ($$) {
     close PF;
 }
 
+sub protocol_read_bytes ($$) {
+    my ($fh, $nbytes) = @_;
+    $nbytes =~ m/^\d{1,6}$/ or badproto \*RO, "bad byte count";
+    my $d;
+    my $got = read $fh, $d, $nbytes;
+    $got==$nbytes or badproto $fh, "eof during data block";
+    return $d;
+}
+
 sub protocol_receive_file ($$) {
     my ($fh, $ourfn) = @_;
     open PF, ">", $ourfn or die "$ourfn: $!";
     for (;;) {
-       protocol_expect \*STDIN, { m/^data-block (\d{1,6})$|data-end$/ };
+       protocol_expect \*STDIN, { m/^data-block (.*})$|data-end$/ };
        length $1 or last;
-       my $d;
-       my $got = read $fh, $d, $1;
-       $got==$1 or badproto $fh, "eof during data block";
+       my $d = protocol_read_bytes \*STDIN, $1;
        print PF $d or die $!;
     }
 }
@@ -233,7 +241,10 @@ sub responder_receive_files ($@) {
 
 #---------- remote protocol support, initiator ----------
 
-
+sub initiator_expect (&) {
+    my ($match) = @_;
+    protocol_expect \*RO, &$match;
+}
 
 #---------- end remote code ----------
 
@@ -1350,6 +1361,15 @@ sub cmd_remote_push_responder {
     &cmd_push;
 }
 
+our $i_tmp;
+
+sub i_cleanup {
+    local ($@);
+    return unless defined $i_tmp;
+    chdir "/" or die $!;
+    eval { rmtree $i_tmp; };
+}
+
 sub cmd_rpush {
     my $host = nextarg;
     my $dir;
@@ -1368,12 +1388,18 @@ sub cmd_rpush {
     push @rdgit, @ARGV;
     my @cmd = (@ssh, $host, shellquote @rdgit);
     my $pid = open2(\*RO, \*RI, @cmd);
-    initiator_expect { m/^dgit-remote-push-ready/ };
-    for (;;) {
-       initiator_expect { m/^(\S+)\s+(.*)$/ };
-       my ($icmd,$iargs) = ($1, $2);
-       $icmd =~ s/\-/_/g;
-       { no strict qw(refs); &{"i_resp_$icmd"}($iargs); }
+    eval {
+       $i_tmp = tempdir();
+       chdir $i_tmp or die "$i_tmp $!";
+       initiator_expect { m/^dgit-remote-push-ready/ };
+       for (;;) {
+           initiator_expect { m/^(\S+)(?: (.*))?$/ };
+           my ($icmd,$iargs) = ($1, $2);
+           i_method "i_resp_", $icmd, $iargs;
+       }
+    };
+    i_cleanup();
+    die $@;
     }
 }