chiark / gitweb /
remote protocol fixes
[dgit.git] / dgit
diff --git a/dgit b/dgit
index 2972a9ece86a8e52db94e2bf73fe7e8734e2493b..4c3d224431e748ab04c183c1523800a2c201fc05 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -180,7 +180,6 @@ sub changedir ($) {
 sub badproto ($$) {
     my ($fh, $m) = @_;
     fail "connection lost: $!" if $fh->error;
-    fail "connection terminated" if $fh->eof;
     fail "protocol violation; $m not expected";
 }
 
@@ -208,9 +207,8 @@ sub protocol_send_file ($$) {
        die "$ourfn: $!" unless defined $got;
        last if !$got;
        print $fh "data-block ".length($d)."\n" or die $!;
-       print $d or die $!;
+       print $fh $d or die $!;
     }
-    PF->eof or die "$ourfn $!";
     PF->error and die "$ourfn $!";
     print $fh "data-end\n" or die $!;
     close PF;
@@ -249,7 +247,7 @@ sub responder_send_command ($) {
     return unless $we_are_responder;
     # called even without $we_are_responder
     printdebug "<< $command\n";
-    print $command, "\n" or die $!;
+    print PO $command, "\n" or die $!;
 }    
 
 sub responder_send_file ($$) {
@@ -257,7 +255,7 @@ sub responder_send_file ($$) {
     return unless $we_are_responder;
     printdebug "[[ $keyword $ourfn\n";
     responder_send_command "file $keyword";
-    protocol_send_file \*STDOUT, $ourfn;
+    protocol_send_file \*PO, $ourfn;
 }
 
 sub responder_receive_files ($@) {
@@ -266,9 +264,9 @@ sub responder_receive_files ($@) {
     printdebug "]] $keyword @ourfns\n";
     responder_send_command "want $keyword";
     foreach my $fn (@ourfns) {
-       protocol_receive_file \*STDIN, $fn;
+       protocol_receive_file \*PI, $fn;
     }
-    protocol_expect { m/^files-end$/ } \*STDIN;
+    protocol_expect { m/^files-end$/ } \*PI;
 }
 
 #---------- remote protocol support, initiator ----------
@@ -284,7 +282,7 @@ sub progress {
     if ($we_are_responder) {
        my $m = join '', @_;
        responder_send_command "progress ".length($m) or die $!;
-       print $m or die $!;
+       print PO $m or die $!;
     } else {
        print @_, "\n";
     }
@@ -1264,6 +1262,7 @@ sub dopush () {
     }
 
     responder_send_file('changes',$changesfile);
+    responder_send_command("param head $head");
 
     my $tfn = sub { ".git/dgit/tag$_[0]"; };
     my ($tagobjfn) =
@@ -1422,21 +1421,34 @@ sub cmd_remote_push_responder {
     die unless @rargs;
     my ($dir) = @rargs;
     $debugprefix = ' ';
-    changedir $dir;
     $we_are_responder = 1;
+
+    open PI, "<&STDIN" or die $!;
+    open STDIN, "/dev/null" or die $!;
+    open PO, ">&STDOUT" or die $!;
+    autoflush PO 1;
+    open STDOUT, ">&STDERR" or die $!;
     autoflush STDOUT 1;
+
     responder_send_command("dgit-remote-push-ready");
+
+    changedir $dir;
     &cmd_push;
 }
 
 our $i_tmp;
+our $i_child_pid;
 
 sub i_cleanup {
     local ($@);
-    return unless defined $i_tmp;
-    return if defined $initiator_tempdir;
-    changedir "/";
-    eval { rmtree $i_tmp; };
+    if ($i_child_pid) {
+       printdebug "(killing remote child $i_child_pid)\n";
+       kill 15, $i_child_pid;
+    }
+    if (defined $i_tmp && !defined $initiator_tempdir) {
+       changedir "/";
+       eval { rmtree $i_tmp; };
+    }
 }
 
 END { i_cleanup(); }
@@ -1465,27 +1477,34 @@ sub cmd_rpush {
     push @rdgit, @ARGV;
     my @cmd = (@ssh, $host, shellquote @rdgit);
     printcmd \*DEBUG,$debugprefix."+",@cmd;
-    eval {
-       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);
-       changedir $i_tmp;
-       initiator_expect { m/^dgit-remote-push-ready/ };
-       for (;;) {
-           my ($icmd,$iargs) = initiator_expect {
-               m/^(\S+)(?: (.*))?$/;
-               ($1,$2);
-           };
-           i_method "i_resp", $icmd, $iargs;
-       }
-    };
+
+    if (defined $initiator_tempdir) {
+       rmtree $initiator_tempdir;
+       mkdir $initiator_tempdir, 0700 or die "$initiator_tempdir: $!";
+       $i_tmp = $initiator_tempdir;
+    } else {
+       $i_tmp = tempdir();
+    }
+    $i_child_pid = open2(\*RO, \*RI, @cmd);
+    changedir $i_tmp;
+    initiator_expect { m/^dgit-remote-push-ready/ };
+    for (;;) {
+       my ($icmd,$iargs) = initiator_expect {
+           m/^(\S+)(?: (.*))?$/;
+           ($1,$2);
+       };
+       i_method "i_resp", $icmd, $iargs;
+    }
+
+    my $pid = $i_child_pid;
+    $i_child_pid = undef; # prevents killing some other process with same pid
+    printdebug "waiting for remote child $pid...";
+    my $got = waitpid $pid, 0;
+    die $! unless $got == $pid;
+    die "remote child failed $?" if $?;
+
     i_cleanup();
-    die $@;
+    exit 0;
 }
 
 sub i_resp_progress ($) {
@@ -1509,8 +1528,8 @@ sub i_resp_file ($) {
 
 our %i_param;
 
-sub i_param ($) {
-    $_[0] =~ m/^(\S+) (.*)$/;
+sub i_resp_param ($) {
+    $_[0] =~ m/^(\S+) (.*)$/ or badproto \*RO, "bad param spec";
     $i_param{$1} = $2;
 }
 
@@ -1539,6 +1558,7 @@ sub i_localname_dsc {
 }
 
 sub i_want_signed_tag {
+    printdebug Dumper(\%i_param, $i_dscfn);
     defined $i_param{'head'} && defined $i_dscfn
        or badproto \*RO, "sequencing error";
     my $head = $i_param{'head'};