@matheusbsbs / matheus-bagliano / commits / d43ba46807
Fork 0

Git.pm: Implement options for the command interface

This gives the user a way to easily pass options to the command routines. Currently only the STDERR option is implemented and can be used to adjust what shall be done with error output of the called command (most usefully, it can be used to silence it). 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 d43ba4680754c150124b6ac3cd9c6e52765c6881
1 file changed +35 -2
perl/Git.pm
+35 -2
@@ -36,7 +36,8 @@ $VERSION = '0.01';
36 my $lastrev = <$fh>; chomp $lastrev;
37 $repo->command_close_pipe($fh, $c);
38
39 - my $lastrev = $repo->command_oneline('rev-list', '--all');
39 + my $lastrev = $repo->command_oneline( [ 'rev-list', '--all' ],
40 + STDERR => 0 );
41
42 =cut
43
@@ -178,9 +179,21 @@ sub repository {
179
180 =item command ( COMMAND [, ARGUMENTS... ] )
181
182 +=item command ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
183 +
184 Execute the given Git C<COMMAND> (specify it without the 'git-'
185 prefix), optionally with the specified extra C<ARGUMENTS>.
186
187 +The second more elaborate form can be used if you want to further adjust
188 +the command execution. Currently, only one option is supported:
189 +
190 +B<STDERR> - How to deal with the command's error output. By default (C<undef>)
191 +it is delivered to the caller's C<STDERR>. A false value (0 or '') will cause
192 +it to be thrown away. If you want to process it, you can get it in a filehandle
193 +you specify, but you must be extremely careful; if the error output is not
194 +very short and you want to read it in the same process as where you called
195 +C<command()>, you are set up for a nice deadlock!
196 +
197 The method can be called without any instance or on a specified Git repository
198 (in that case the command will be run in the repository context).
199
@@ -231,6 +244,8 @@ sub command {
244
245 =item command_oneline ( COMMAND [, ARGUMENTS... ] )
246
247 +=item command_oneline ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
248 +
249 Execute the given C<COMMAND> in the same way as command()
250 does but always return a scalar string containing the first line
251 of the command's standard output.
@@ -256,6 +271,8 @@ sub command_oneline {
271
272 =item command_output_pipe ( COMMAND [, ARGUMENTS... ] )
273
274 +=item command_output_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
275 +
276 Execute the given C<COMMAND> in the same way as command()
277 does but return a pipe filehandle from which the command output can be
278 read.
@@ -272,6 +289,8 @@ sub command_output_pipe {
289
290 =item command_input_pipe ( COMMAND [, ARGUMENTS... ] )
291
292 +=item command_input_pipe ( [ COMMAND, ARGUMENTS... ], { Opt => Val ... } )
293 +
294 Execute the given C<COMMAND> in the same way as command_output_pipe()
295 does but return an input pipe filehandle instead; the command output
296 is not captured.
@@ -534,13 +553,27 @@ sub _check_valid_cmd {
553 # Common backend for the pipe creators.
554 sub _command_common_pipe {
555 my $direction = shift;
537 - my ($self, $cmd, @args) = _maybe_self(@_);
556 + my ($self, @p) = _maybe_self(@_);
557 + my (%opts, $cmd, @args);
558 + if (ref $p[0]) {
559 + ($cmd, @args) = @{shift @p};
560 + %opts = ref $p[0] ? %{$p[0]} : @p;
561 + } else {
562 + ($cmd, @args) = @p;
563 + }
564 _check_valid_cmd($cmd);
565
566 my $pid = open(my $fh, $direction);
567 if (not defined $pid) {
568 throw Error::Simple("open failed: $!");
569 } elsif ($pid == 0) {
570 + if (defined $opts{STDERR}) {
571 + close STDERR;
572 + }
573 + if ($opts{STDERR}) {
574 + open (STDERR, '>&', $opts{STDERR})
575 + or die "dup failed: $!";
576 + }
577 _cmd_exec($self, $cmd, @args);
578 }
579 return wantarray ? ($fh, join(' ', $cmd, @args)) : $fh;