git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 7/9] Git.pm: Add command_bidi_pipe and command_close_bidi_pipe

From
Adam Roben <aroben@apple.com>
Date
Oct 25, 2007, 10:25 UTC
Message-ID
<1193307927-3592-8-git-send-email-aroben@apple.com>
In-Reply-To
<1193307927-3592-7-git-send-email-aroben@apple.com>

command_bidi_pipe hands back the stdin and stdout file handles from the executed command. command_close_bidi_pipe closes these handles and terminates the process.

Signed-off-by: Adam Roben <aroben@apple.com>
---
 perl/Git.pm |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index 3f4080c..46c5d10 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -51,6 +51,7 @@ require Exporter;
 # Methods which can be called as standalone functions as well:
 @EXPORT_OK = qw(command command_oneline command_noisy
                 command_output_pipe command_input_pipe command_close_pipe
+                command_bidi_pipe command_close_bidi_pipe
                 version exec_path hash_object git_cmd_try);
 
 
@@ -92,6 +93,7 @@ increate nonwithstanding).
 use Carp qw(carp croak); # but croak is bad - throw instead
 use Error qw(:try);
 use Cwd qw(abs_path);
+use IPC::Open2 qw(open2);
 
 }
 
@@ -375,6 +377,60 @@ sub command_close_pipe {
 	_cmd_close($fh, $ctx);
 }
 
+=item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] )
+
+Execute the given C<COMMAND> in the same way as command_output_pipe()
+does but return both an input pipe filehandle and an output pipe filehandle.
+
+The function will return return C<($pid, $pipe_in, $pipe_out, $ctx)>.
+See C<command_close_bidi_pipe()> for details.
+
+=cut
+
+sub command_bidi_pipe {
+	my ($pid, $in, $out);
+	$pid = open2($in, $out, 'git', @_);
+	return ($pid, $in, $out, join(' ', @_));
+}
+
+=item command_close_bidi_pipe ( PID, PIPE_IN, PIPE_OUT [, CTX] )
+
+Close the C<PIPE_IN> and C<PIPE_OUT> as returned from C<command_bidi_pipe()>,
+checking whether the command finished successfully. The optional C<CTX>
+argument is required if you want to see the command name in the error message,
+and it is the fourth value returned by C<command_bidi_pipe()>.  The call idiom
+is:
+
+	my ($pid, $in, $out, $ctx) = $r->command_bidi_pipe('cat-file --stdin');
+	print "000000000\n" $out;
+	while (<$in>) { ... }
+	$r->command_close_bidi_pipe($pid, $in, $out, $ctx);
+
+Note that you should not rely on whatever actually is in C<CTX>;
+currently it is simply the command name but in future the context might
+have more complicated structure.
+
+=cut
+
+sub command_close_bidi_pipe {
+	my ($pid, $in, $out, $ctx) = @_;
+	foreach my $fh ($in, $out) {
+		if (not close $fh) {
+			if ($!) {
+				carp "error closing pipe: $!";
+			} elsif ($? >> 8) {
+				throw Git::Error::Command($ctx, $? >>8);
+			}
+		}
+	}
+
+	waitpid $pid, 0;
+
+	if ($? >> 8) {
+		throw Git::Error::Command($ctx, $? >>8);
+	}
+}
+
 
 =item command_noisy ( COMMAND [, ARGUMENTS... ] )
 
-- 
1.5.3.4.1337.g8e67d-dirty
Previous: Adam RobenNext: Adam Roben
Message 8 of 17 in “Make git-svn fetch ~1.7x faster”
  1. 0/9 Make git-svn fetch ~1.7x fasterAdam Roben, Oct 25, 2007
  2. 1/9 Add tests for git cat-fileAdam Roben, Oct 25, 2007
  3. 2/9 git-cat-file: Small refactor of cmd_cat_fileAdam Roben, Oct 25, 2007
  4. 3/9 git-cat-file: Make option parsing a little more flexibleAdam Roben, Oct 25, 2007
  5. 4/9 git-cat-file: Add --stdin optionAdam Roben, Oct 25, 2007
  6. 5/9 Add tests for git hash-objectAdam Roben, Oct 25, 2007
  7. 6/9 git-hash-object: Add --stdin-paths optionAdam Roben, Oct 25, 2007
  8. 7/9 Git.pm: Add command_bidi_pipe and command_close_bidi_pipeAdam Roben, Oct 25, 2007
  9. 8/9 Git.pm: Add hash_and_insert_object and cat_blobAdam Roben, Oct 25, 2007
  10. 9/9 git-svn: Make fetch ~1.7x fasterAdam Roben, Oct 25, 2007
  11. Eric WongOct 26, 2007
  12. Junio C HamanoOct 26, 2007
  13. Brian DowningOct 26, 2007
  14. Junio C HamanoOct 27, 2007
  15. Junio C HamanoOct 26, 2007
  16. Junio C HamanoOct 26, 2007
  17. Junio C HamanoOct 26, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.