Git.pm: refactor command_close_bidi_pipe to use _cmd_close

The body of the loop in command_close_bidi_pipe sub is identical to
what _cmd_close sub does.

Instead of duplicating, refactor _cmd_close so that it accepts a
list of file handles to be closed, which makes it usable with
command_close_bidi_pipe.

Signed-off-by: Michal Nazarewicz <mina86@mina86.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Michal Nazarewicz 2013-02-12 15:02:30 +01:00 коммит произвёл Junio C Hamano
Родитель 8a2cc51b6f
Коммит 1323dba6af
1 изменённых файлов: 11 добавлений и 19 удалений

Просмотреть файл

@ -267,13 +267,13 @@ sub command {
if (not defined wantarray) { if (not defined wantarray) {
# Nothing to pepper the possible exception with. # Nothing to pepper the possible exception with.
_cmd_close($fh, $ctx); _cmd_close($ctx, $fh);
} elsif (not wantarray) { } elsif (not wantarray) {
local $/; local $/;
my $text = <$fh>; my $text = <$fh>;
try { try {
_cmd_close($fh, $ctx); _cmd_close($ctx, $fh);
} catch Git::Error::Command with { } catch Git::Error::Command with {
# Pepper with the output: # Pepper with the output:
my $E = shift; my $E = shift;
@ -286,7 +286,7 @@ sub command {
my @lines = <$fh>; my @lines = <$fh>;
defined and chomp for @lines; defined and chomp for @lines;
try { try {
_cmd_close($fh, $ctx); _cmd_close($ctx, $fh);
} catch Git::Error::Command with { } catch Git::Error::Command with {
my $E = shift; my $E = shift;
$E->{'-outputref'} = \@lines; $E->{'-outputref'} = \@lines;
@ -313,7 +313,7 @@ sub command_oneline {
my $line = <$fh>; my $line = <$fh>;
defined $line and chomp $line; defined $line and chomp $line;
try { try {
_cmd_close($fh, $ctx); _cmd_close($ctx, $fh);
} catch Git::Error::Command with { } catch Git::Error::Command with {
# Pepper with the output: # Pepper with the output:
my $E = shift; my $E = shift;
@ -381,7 +381,7 @@ have more complicated structure.
sub command_close_pipe { sub command_close_pipe {
my ($self, $fh, $ctx) = _maybe_self(@_); my ($self, $fh, $ctx) = _maybe_self(@_);
$ctx ||= '<unknown>'; $ctx ||= '<unknown>';
_cmd_close($fh, $ctx); _cmd_close($ctx, $fh);
} }
=item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] ) =item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] )
@ -431,18 +431,8 @@ have more complicated structure.
sub command_close_bidi_pipe { sub command_close_bidi_pipe {
local $?; local $?;
my ($self, $pid, $in, $out, $ctx) = _maybe_self(@_); my ($self, $pid, $in, $out, $ctx) = _maybe_self(@_);
foreach my $fh ($in, $out) { _cmd_close($ctx, $in, $out);
unless (close $fh) {
if ($!) {
carp "error closing pipe: $!";
} elsif ($? >> 8) {
throw Git::Error::Command($ctx, $? >>8);
}
}
}
waitpid $pid, 0; waitpid $pid, 0;
if ($? >> 8) { if ($? >> 8) {
throw Git::Error::Command($ctx, $? >>8); throw Git::Error::Command($ctx, $? >>8);
} }
@ -1355,9 +1345,11 @@ sub _execv_git_cmd { exec('git', @_); }
# Close pipe to a subprocess. # Close pipe to a subprocess.
sub _cmd_close { sub _cmd_close {
my ($fh, $ctx) = @_; my $ctx = shift @_;
if (not close $fh) { foreach my $fh (@_) {
if ($!) { if (close $fh) {
# nop
} elsif ($!) {
# It's just close, no point in fatalities # It's just close, no point in fatalities
carp "error closing pipe: $!"; carp "error closing pipe: $!";
} elsif ($? >> 8) { } elsif ($? >> 8) {