Git.pm: allow pipes to be closed prior to calling command_close_bidi_pipe
The command_close_bidi_pipe() function will insist on closing both input and output pipes returned by command_bidi_pipe(). With this change it is possible to close one of the pipes in advance and pass undef as an argument. Signed-off-by: Michal Nazarewicz <mina86@mina86.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michal Nazarewicz committed
Feb 12, 2013 at 15:02 UTC
f4c0035de660f5be4d78b0ba0aa8a7863b89c72f
1 file changed
+14
-1
perl/Git.pm
+14
-1
@@ -426,12 +426,25 @@ Note that you should not rely on whatever actually is in C<CTX>;
426
currently it is simply the command name but in future the context might
427
have more complicated structure.
428
429
+C<PIPE_IN> and C<PIPE_OUT> may be C<undef> if they have been closed prior to
430
+calling this function. This may be useful in a query-response type of
431
+commands where caller first writes a query and later reads response, eg:
432
+
433
+ my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --batch-check');
434
+ print $out "000000000\n";
435
+ close $out;
436
+ while (<$in>) { ... }
437
+ $r->command_close_bidi_pipe($pid, $in, undef, $ctx);
438
+
439
+This idiom may prevent potential dead locks caused by data sent to the output
440
+pipe not being flushed and thus not reaching the executed command.
441
+
442
=cut
443
444
sub command_close_bidi_pipe {
445
local $?;
446
my ($self, $pid, $in, $out, $ctx) = _maybe_self(@_);
434
- _cmd_close($ctx, $in, $out);
447
+ _cmd_close($ctx, (grep { defined } ($in, $out)));
448
waitpid $pid, 0;
449
if ($? >> 8) {
450
throw Git::Error::Command($ctx, $? >>8);