Skip to content

Commit 1323dba

Browse files
mina86gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8a2cc51 commit 1323dba

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

perl/Git.pm

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,13 @@ sub command {
267267

268268
if (not defined wantarray) {
269269
# Nothing to pepper the possible exception with.
270-
_cmd_close($fh, $ctx);
270+
_cmd_close($ctx, $fh);
271271

272272
} elsif (not wantarray) {
273273
local $/;
274274
my $text = <$fh>;
275275
try {
276-
_cmd_close($fh, $ctx);
276+
_cmd_close($ctx, $fh);
277277
} catch Git::Error::Command with {
278278
# Pepper with the output:
279279
my $E = shift;
@@ -286,7 +286,7 @@ sub command {
286286
my @lines = <$fh>;
287287
defined and chomp for @lines;
288288
try {
289-
_cmd_close($fh, $ctx);
289+
_cmd_close($ctx, $fh);
290290
} catch Git::Error::Command with {
291291
my $E = shift;
292292
$E->{'-outputref'} = \@lines;
@@ -313,7 +313,7 @@ sub command_oneline {
313313
my $line = <$fh>;
314314
defined $line and chomp $line;
315315
try {
316-
_cmd_close($fh, $ctx);
316+
_cmd_close($ctx, $fh);
317317
} catch Git::Error::Command with {
318318
# Pepper with the output:
319319
my $E = shift;
@@ -381,7 +381,7 @@ have more complicated structure.
381381
sub command_close_pipe {
382382
my ($self, $fh, $ctx) = _maybe_self(@_);
383383
$ctx ||= '<unknown>';
384-
_cmd_close($fh, $ctx);
384+
_cmd_close($ctx, $fh);
385385
}
386386

387387
=item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] )
@@ -431,18 +431,8 @@ have more complicated structure.
431431
sub command_close_bidi_pipe {
432432
local $?;
433433
my ($self, $pid, $in, $out, $ctx) = _maybe_self(@_);
434-
foreach my $fh ($in, $out) {
435-
unless (close $fh) {
436-
if ($!) {
437-
carp "error closing pipe: $!";
438-
} elsif ($? >> 8) {
439-
throw Git::Error::Command($ctx, $? >>8);
440-
}
441-
}
442-
}
443-
434+
_cmd_close($ctx, $in, $out);
444435
waitpid $pid, 0;
445-
446436
if ($? >> 8) {
447437
throw Git::Error::Command($ctx, $? >>8);
448438
}
@@ -1355,9 +1345,11 @@ sub _execv_git_cmd { exec('git', @_); }
13551345

13561346
# Close pipe to a subprocess.
13571347
sub _cmd_close {
1358-
my ($fh, $ctx) = @_;
1359-
if (not close $fh) {
1360-
if ($!) {
1348+
my $ctx = shift @_;
1349+
foreach my $fh (@_) {
1350+
if (close $fh) {
1351+
# nop
1352+
} elsif ($!) {
13611353
# It's just close, no point in fatalities
13621354
carp "error closing pipe: $!";
13631355
} elsif ($? >> 8) {

0 commit comments

Comments
 (0)