Git.pm: Enhance the command_pipe() mechanism
Rename command_pipe() to command_output_pipe(), outsource the functionality to _command_common_pipe(). Add command_input_pipe(). Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>
Petr Baudis committed
Jun 24, 2006 at 04:34 UTC
d79850e1fd22abd44e447214a64e5a774ada311e
1 file changed
+53
-23
perl/Git.pm
+53
-23
@@ -32,7 +32,7 @@ $VERSION = '0.01';
32
33
my @revs = $repo->command('rev-list', '--since=last monday', '--all');
34
35
- my ($fh, $c) = $repo->command_pipe('rev-list', '--since=last monday', '--all');
35
+ my ($fh, $c) = $repo->command_output_pipe('rev-list', '--since=last monday', '--all');
36
my $lastrev = <$fh>; chomp $lastrev;
37
$repo->command_close_pipe($fh, $c);
38
@@ -48,7 +48,8 @@ require Exporter;
48
@EXPORT = qw(git_cmd_try);
49
50
# Methods which can be called as standalone functions as well:
51
-@EXPORT_OK = qw(command command_oneline command_pipe command_noisy
51
+@EXPORT_OK = qw(command command_oneline command_noisy
52
+ command_output_pipe command_input_pipe command_close_pipe
53
version exec_path hash_object git_cmd_try);
54
55
@@ -194,7 +195,7 @@ In both cases, the command's stdin and stderr are the same as the caller's.
195
=cut
196
197
sub command {
197
- my ($fh, $ctx) = command_pipe(@_);
198
+ my ($fh, $ctx) = command_output_pipe(@_);
199
200
if (not defined wantarray) {
201
# Nothing to pepper the possible exception with.
@@ -237,7 +238,7 @@ of the command's standard output.
238
=cut
239
240
sub command_oneline {
240
- my ($fh, $ctx) = command_pipe(@_);
241
+ my ($fh, $ctx) = command_output_pipe(@_);
242
243
my $line = <$fh>;
244
chomp $line;
@@ -253,40 +254,49 @@ sub command_oneline {
254
}
255
256
256
-=item command_pipe ( COMMAND [, ARGUMENTS... ] )
257
+=item command_output_pipe ( COMMAND [, ARGUMENTS... ] )
258
259
Execute the given C<COMMAND> in the same way as command()
260
does but return a pipe filehandle from which the command output can be
261
read.
262
263
+The function can return C<($pipe, $ctx)> in array context.
264
+See C<command_close_pipe()> for details.
265
+
266
=cut
267
264
-sub command_pipe {
265
- my ($self, $cmd, @args) = _maybe_self(@_);
268
+sub command_output_pipe {
269
+ _command_common_pipe('-|', @_);
270
+}
271
267
- $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
272
269
- my $pid = open(my $fh, "-|");
270
- if (not defined $pid) {
271
- throw Error::Simple("open failed: $!");
272
- } elsif ($pid == 0) {
273
- _cmd_exec($self, $cmd, @args);
274
- }
275
- return wantarray ? ($fh, join(' ', $cmd, @args)) : $fh;
273
+=item command_input_pipe ( COMMAND [, ARGUMENTS... ] )
274
+
275
+Execute the given C<COMMAND> in the same way as command_output_pipe()
276
+does but return an input pipe filehandle instead; the command output
277
+is not captured.
278
+
279
+The function can return C<($pipe, $ctx)> in array context.
280
+See C<command_close_pipe()> for details.
281
+
282
+=cut
283
+
284
+sub command_input_pipe {
285
+ _command_common_pipe('|-', @_);
286
}
287
288
289
=item command_close_pipe ( PIPE [, CTX ] )
290
281
-Close the C<PIPE> as returned from C<command_pipe()>, checking
291
+Close the C<PIPE> as returned from C<command_*_pipe()>, checking
292
whether the command finished successfuly. The optional C<CTX> argument
293
is required if you want to see the command name in the error message,
284
-and it is the second value returned by C<command_pipe()> when
294
+and it is the second value returned by C<command_*_pipe()> when
295
called in array context. The call idiom is:
296
287
- my ($fh, $ctx) = $r->command_pipe('status');
288
- while (<$fh>) { ... }
289
- $r->command_close_pipe($fh, $ctx);
297
+ my ($fh, $ctx) = $r->command_output_pipe('status');
298
+ while (<$fh>) { ... }
299
+ $r->command_close_pipe($fh, $ctx);
300
301
Note that you should not rely on whatever actually is in C<CTX>;
302
currently it is simply the command name but in future the context might
@@ -317,8 +327,7 @@ The function returns only after the command has finished running.
327
328
sub command_noisy {
329
my ($self, $cmd, @args) = _maybe_self(@_);
320
-
321
- $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
330
+ _check_valid_cmd($cmd);
331
332
my $pid = fork;
333
if (not defined $pid) {
@@ -404,7 +413,7 @@ string with the captured command output (depending on the original function
413
call context; C<command_noisy()> returns C<undef>) and $<cmdline> which
414
returns the command and its arguments (but without proper quoting).
415
407
-Note that the C<command_pipe()> function cannot throw this exception since
416
+Note that the C<command_*_pipe()> functions cannot throw this exception since
417
it has no idea whether the command failed or not. You will only find out
418
at the time you C<close> the pipe; if you want to have that automated,
419
use C<command_close_pipe()>, which can throw the exception.
@@ -516,6 +525,27 @@ sub _maybe_self {
525
ref $_[0] eq 'Git' ? @_ : (undef, @_);
526
}
527
528
+# Check if the command id is something reasonable.
529
+sub _check_valid_cmd {
530
+ my ($cmd) = @_;
531
+ $cmd =~ /^[a-z0-9A-Z_-]+$/ or throw Error::Simple("bad command: $cmd");
532
+}
533
+
534
+# Common backend for the pipe creators.
535
+sub _command_common_pipe {
536
+ my $direction = shift;
537
+ my ($self, $cmd, @args) = _maybe_self(@_);
538
+ _check_valid_cmd($cmd);
539
+
540
+ my $pid = open(my $fh, $direction);
541
+ if (not defined $pid) {
542
+ throw Error::Simple("open failed: $!");
543
+ } elsif ($pid == 0) {
544
+ _cmd_exec($self, $cmd, @args);
545
+ }
546
+ return wantarray ? ($fh, join(' ', $cmd, @args)) : $fh;
547
+}
548
+
549
# When already in the subprocess, set up the appropriate state
550
# for the given repository and execute the git command.
551
sub _cmd_exec {