Currently if an external command returns error exit code, a generic exception
is thrown and there is no chance for the caller to retrieve the command's
output.
This patch introduces a Git::Error::Command exception class which is thrown
in this case and contains both the error code and the captured command output.
You can use the new git_cmd_try statement to fatally catch the exception
while producing a user-friendly message.
It also adds command_close_pipe() for easier checking of exit status of
a command we have just a pipe handle of. It has partial forward dependency
on the next patch, but basically only in the area of documentation.
Signed-off-by: Petr Baudis <pasky@suse.cz>
Signed-off-by: Junio C Hamano <junkio@cox.net>
use Carp qw(carp croak); # but croak is bad - throw instead
use Error qw(:try);
require XSLoader;
@ -193,21 +194,35 @@ In both cases, the command's stdin and stderr are the same as the caller's.
@@ -193,21 +194,35 @@ In both cases, the command's stdin and stderr are the same as the caller's.
=cut
sub command {
my $fh = command_pipe(@_);
my ($fh, $ctx) = command_pipe(@_);
if (not defined wantarray) {
_cmd_close($fh);
# Nothing to pepper the possible exception with.
_cmd_close($fh, $ctx);
} elsif (not wantarray) {
local $/;
my $text = <$fh>;
_cmd_close($fh);
try {
_cmd_close($fh, $ctx);
} catch Git::Error::Command with {
# Pepper with the output:
my $E = shift;
$E->{'-outputref'} = \$text;
throw $E;
};
return $text;
} else {
my @lines = <$fh>;
_cmd_close($fh);
chomp @lines;
try {
_cmd_close($fh, $ctx);
} catch Git::Error::Command with {
my $E = shift;
$E->{'-outputref'} = \@lines;
throw $E;
};
return @lines;
}
}
@ -222,12 +237,18 @@ of the command's standard output.
@@ -222,12 +237,18 @@ of the command's standard output.
=cut
sub command_oneline {
my $fh = command_pipe(@_);
my ($fh, $ctx) = command_pipe(@_);
my $line = <$fh>;
_cmd_close($fh);
chomp $line;
try {
_cmd_close($fh, $ctx);
} catch Git::Error::Command with {
# Pepper with the output:
my $E = shift;
$E->{'-outputref'} = \$line;
throw $E;
};
return $line;
}
@ -251,7 +272,32 @@ sub command_pipe {
@@ -251,7 +272,32 @@ sub command_pipe {