Skip to content

Commit

Permalink
Git.pm: refactor command_close_bidi_pipe to use _cmd_close
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Michal Nazarewicz authored and Junio C Hamano committed Feb 12, 2013
1 parent 8a2cc51 commit 1323dba
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions perl/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ sub command {

if (not defined wantarray) {
# Nothing to pepper the possible exception with.
_cmd_close($fh, $ctx);
_cmd_close($ctx, $fh);

} elsif (not wantarray) {
local $/;
my $text = <$fh>;
try {
_cmd_close($fh, $ctx);
_cmd_close($ctx, $fh);
} catch Git::Error::Command with {
# Pepper with the output:
my $E = shift;
Expand All @@ -286,7 +286,7 @@ sub command {
my @lines = <$fh>;
defined and chomp for @lines;
try {
_cmd_close($fh, $ctx);
_cmd_close($ctx, $fh);
} catch Git::Error::Command with {
my $E = shift;
$E->{'-outputref'} = \@lines;
Expand All @@ -313,7 +313,7 @@ sub command_oneline {
my $line = <$fh>;
defined $line and chomp $line;
try {
_cmd_close($fh, $ctx);
_cmd_close($ctx, $fh);
} catch Git::Error::Command with {
# Pepper with the output:
my $E = shift;
Expand Down Expand Up @@ -381,7 +381,7 @@ have more complicated structure.
sub command_close_pipe {
my ($self, $fh, $ctx) = _maybe_self(@_);
$ctx ||= '<unknown>';
_cmd_close($fh, $ctx);
_cmd_close($ctx, $fh);
}

=item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] )
Expand Down Expand Up @@ -431,18 +431,8 @@ have more complicated structure.
sub command_close_bidi_pipe {
local $?;
my ($self, $pid, $in, $out, $ctx) = _maybe_self(@_);
foreach my $fh ($in, $out) {
unless (close $fh) {
if ($!) {
carp "error closing pipe: $!";
} elsif ($? >> 8) {
throw Git::Error::Command($ctx, $? >>8);
}
}
}

_cmd_close($ctx, $in, $out);
waitpid $pid, 0;

if ($? >> 8) {
throw Git::Error::Command($ctx, $? >>8);
}
Expand Down Expand Up @@ -1355,9 +1345,11 @@ sub _execv_git_cmd { exec('git', @_); }

# Close pipe to a subprocess.
sub _cmd_close {
my ($fh, $ctx) = @_;
if (not close $fh) {
if ($!) {
my $ctx = shift @_;
foreach my $fh (@_) {
if (close $fh) {
# nop
} elsif ($!) {
# It's just close, no point in fatalities
carp "error closing pipe: $!";
} elsif ($? >> 8) {
Expand Down

0 comments on commit 1323dba

Please sign in to comment.