Skip to content

Commit 6942a3d

Browse files
committed
libperl-git: refactor Git::config_*
Move common parts of Git::config(), Git::config_bool(), Git::config_int() and Git::config_path() into _config_common() helper. Signed-off-by: Junio C Hamano <[email protected]>
1 parent cec5dae commit 6942a3d

File tree

1 file changed

+24
-64
lines changed

1 file changed

+24
-64
lines changed

perl/Git.pm

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -570,30 +570,10 @@ does. In scalar context requires the variable to be set only one time
570570
(exception is thrown otherwise), in array context returns allows the
571571
variable to be set multiple times and returns all the values.
572572
573-
This currently wraps command('config') so it is not so fast.
574-
575573
=cut
576574

577575
sub config {
578-
my ($self, $var) = _maybe_self(@_);
579-
580-
try {
581-
my @cmd = ('config');
582-
unshift @cmd, $self if $self;
583-
if (wantarray) {
584-
return command(@cmd, '--get-all', $var);
585-
} else {
586-
return command_oneline(@cmd, '--get', $var);
587-
}
588-
} catch Git::Error::Command with {
589-
my $E = shift;
590-
if ($E->value() == 1) {
591-
# Key not found.
592-
return;
593-
} else {
594-
throw $E;
595-
}
596-
};
576+
return _config_common({}, @_);
597577
}
598578

599579

@@ -603,28 +583,18 @@ Retrieve the bool configuration C<VARIABLE>. The return value
603583
is usable as a boolean in perl (and C<undef> if it's not defined,
604584
of course).
605585
606-
This currently wraps command('config') so it is not so fast.
607-
608586
=cut
609587

610588
sub config_bool {
611-
my ($self, $var) = _maybe_self(@_);
589+
my $val = scalar _config_common({'kind' => '--bool'}, @_);
612590

613-
try {
614-
my @cmd = ('config', '--bool', '--get', $var);
615-
unshift @cmd, $self if $self;
616-
my $val = command_oneline(@cmd);
617-
return undef unless defined $val;
591+
# Do not rewrite this as return (defined $val && $val eq 'true')
592+
# as some callers do care what kind of falsehood they receive.
593+
if (!defined $val) {
594+
return undef;
595+
} else {
618596
return $val eq 'true';
619-
} catch Git::Error::Command with {
620-
my $E = shift;
621-
if ($E->value() == 1) {
622-
# Key not found.
623-
return undef;
624-
} else {
625-
throw $E;
626-
}
627-
};
597+
}
628598
}
629599

630600

@@ -633,32 +603,13 @@ sub config_bool {
633603
Retrieve the path configuration C<VARIABLE>. The return value
634604
is an expanded path or C<undef> if it's not defined.
635605
636-
This currently wraps command('config') so it is not so fast.
637-
638606
=cut
639607

640608
sub config_path {
641-
my ($self, $var) = _maybe_self(@_);
642-
643-
try {
644-
my @cmd = ('config', '--path');
645-
unshift @cmd, $self if $self;
646-
if (wantarray) {
647-
return command(@cmd, '--get-all', $var);
648-
} else {
649-
return command_oneline(@cmd, '--get', $var);
650-
}
651-
} catch Git::Error::Command with {
652-
my $E = shift;
653-
if ($E->value() == 1) {
654-
# Key not found.
655-
return undef;
656-
} else {
657-
throw $E;
658-
}
659-
};
609+
return _config_common({'kind' => '--path'}, @_);
660610
}
661611

612+
662613
=item config_int ( VARIABLE )
663614
664615
Retrieve the integer configuration C<VARIABLE>. The return value
@@ -667,22 +618,31 @@ or 'g' in the config file will cause the value to be multiplied
667618
by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.
668619
It would return C<undef> if configuration variable is not defined,
669620
670-
This currently wraps command('config') so it is not so fast.
671-
672621
=cut
673622

674623
sub config_int {
624+
return scalar _config_common({'kind' => '--int'}, @_);
625+
}
626+
627+
# Common subroutine to implement bulk of what the config* family of methods
628+
# do. This curently wraps command('config') so it is not so fast.
629+
sub _config_common {
630+
my ($opts) = shift @_;
675631
my ($self, $var) = _maybe_self(@_);
676632

677633
try {
678-
my @cmd = ('config', '--int', '--get', $var);
634+
my @cmd = ('config', $opts->{'kind'} ? $opts->{'kind'} : ());
679635
unshift @cmd, $self if $self;
680-
return command_oneline(@cmd);
636+
if (wantarray) {
637+
return command(@cmd, '--get-all', $var);
638+
} else {
639+
return command_oneline(@cmd, '--get', $var);
640+
}
681641
} catch Git::Error::Command with {
682642
my $E = shift;
683643
if ($E->value() == 1) {
684644
# Key not found.
685-
return undef;
645+
return;
686646
} else {
687647
throw $E;
688648
}

0 commit comments

Comments
 (0)