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>
Michal Nazarewicz committed
Feb 12, 2013 at 15:02 UTC
1323dba6afad29d3cd07e9da85b947bfc2b912de
1 file changed
+11
-19
perl/Git.pm
+11
-19
@@ -267,13 +267,13 @@ sub command {
267
268
if (not defined wantarray) {
269
# Nothing to pepper the possible exception with.
270
- _cmd_close($fh, $ctx);
270
+ _cmd_close($ctx, $fh);
271
272
} elsif (not wantarray) {
273
local $/;
274
my $text = <$fh>;
275
try {
276
- _cmd_close($fh, $ctx);
276
+ _cmd_close($ctx, $fh);
277
} catch Git::Error::Command with {
278
# Pepper with the output:
279
my $E = shift;
@@ -286,7 +286,7 @@ sub command {
286
my @lines = <$fh>;
287
defined and chomp for @lines;
288
try {
289
- _cmd_close($fh, $ctx);
289
+ _cmd_close($ctx, $fh);
290
} catch Git::Error::Command with {
291
my $E = shift;
292
$E->{'-outputref'} = \@lines;
@@ -313,7 +313,7 @@ sub command_oneline {
313
my $line = <$fh>;
314
defined $line and chomp $line;
315
try {
316
- _cmd_close($fh, $ctx);
316
+ _cmd_close($ctx, $fh);
317
} catch Git::Error::Command with {
318
# Pepper with the output:
319
my $E = shift;
@@ -381,7 +381,7 @@ have more complicated structure.
381
sub command_close_pipe {
382
my ($self, $fh, $ctx) = _maybe_self(@_);
383
$ctx ||= '<unknown>';
384
- _cmd_close($fh, $ctx);
384
+ _cmd_close($ctx, $fh);
385
}
386
387
=item command_bidi_pipe ( COMMAND [, ARGUMENTS... ] )
@@ -431,18 +431,8 @@ have more complicated structure.
431
sub command_close_bidi_pipe {
432
local $?;
433
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);
435
waitpid $pid, 0;
445
-
436
if ($? >> 8) {
437
throw Git::Error::Command($ctx, $? >>8);
438
}
@@ -1355,9 +1345,11 @@ sub _execv_git_cmd { exec('git', @_); }
1345
1346
# Close pipe to a subprocess.
1347
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 ($!) {
1353
# It's just close, no point in fatalities
1354
carp "error closing pipe: $!";
1355
} elsif ($? >> 8) {