I think the addition of "config --path" support is a good idea, but the resulting code suffers from too many cut&paste cruft across the config* family of methods. How about doing a bit of refactoring, perhaps something like this, on top as a separate patch? I tried to be careful to still forcing the "one value only" for config_bool and config_int, but extra sets of eyeballs would be needed. perl/Git.pm | 93 +++++++++++++++++----------------------------------------- 1 files changed, 27 insertions(+), 66 deletions(-) diff --git a/perl/Git.pm b/perl/Git.pm index c279bfb..f0a6e92 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -563,22 +563,18 @@ sub wc_chdir { } -=item config ( VARIABLE ) +=item _config_common ( VARIABLE ) -Retrieve the configuration C<VARIABLE> in the same manner as C<config> -does. In scalar context requires the variable to be set only one time -(exception is thrown otherwise), in array context returns allows the -variable to be set multiple times and returns all the values. - -This currently wraps command('config') so it is not so fast. +Common subroutine to implement bulk of what the config* family of methods +do. This wraps command('config') so it is not so fast. =cut -sub config { - my ($self, $var) = _maybe_self(@_); - +sub _config_common { + my ($self, $var, $opts) = _maybe_self(@_); + try { - my @cmd = ('config'); + my @cmd = ('config', $opts->{'kind'} ? @{$opts->{'kind'}} : ()); unshift @cmd, $self if $self; if (wantarray) { return command(@cmd, '--get-all', $var); @@ -594,6 +590,21 @@ sub config { throw $E; } }; + +} + +=item config ( VARIABLE ) + +Retrieve the configuration C<VARIABLE> in the same manner as C<config> +does. In scalar context requires the variable to be set only one time +(exception is thrown otherwise), in array context returns allows the +variable to be set multiple times and returns all the values. + +=cut + +sub config { + my ($self, $var) = _maybe_self(@_); + return _config_common($self, $var, +{}); } @@ -603,60 +614,24 @@ Retrieve the bool configuration C<VARIABLE>. The return value is usable as a boolean in perl (and C<undef> if it's not defined, of course). -This currently wraps command('config') so it is not so fast. - =cut sub config_bool { my ($self, $var) = _maybe_self(@_); - - try { - my @cmd = ('config', '--bool', '--get', $var); - unshift @cmd, $self if $self; - my $val = command_oneline(@cmd); - return undef unless defined $val; - return $val eq 'true'; - } catch Git::Error::Command with { - my $E = shift; - if ($E->value() == 1) { - # Key not found. - return undef; - } else { - throw $E; - } - }; + my $val = scalar _config_common($self, $var, {'kind' => '--bool'}); + return (defined $val && $val eq 'true'); } - =item config_path ( VARIABLE ) Retrieve the path configuration C<VARIABLE>. The return value is an expanded path or C<undef> if it's not defined. -This currently wraps command('config') so it is not so fast. - =cut sub config_path { my ($self, $var) = _maybe_self(@_); - - try { - my @cmd = ('config', '--path'); - unshift @cmd, $self if $self; - if (wantarray) { - return command(@cmd, '--get-all', $var); - } else { - return command_oneline(@cmd, '--get', $var); - } - } catch Git::Error::Command with { - my $E = shift; - if ($E->value() == 1) { - # Key not found. - return undef; - } else { - throw $E; - } - }; + return _config_common($self, $var, +{'kind' => '--path'}); } =item config_int ( VARIABLE ) @@ -667,26 +642,12 @@ or 'g' in the config file will cause the value to be multiplied by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output. It would return C<undef> if configuration variable is not defined, -This currently wraps command('config') so it is not so fast. - =cut sub config_int { my ($self, $var) = _maybe_self(@_); - - try { - my @cmd = ('config', '--int', '--get', $var); - unshift @cmd, $self if $self; - return command_oneline(@cmd); - } catch Git::Error::Command with { - my $E = shift; - if ($E->value() == 1) { - # Key not found. - return undef; - } else { - throw $E; - } - }; + my $val = scalar _config_common($self, $var, +{'kind' => '--int'}); + return $val; } =item get_colorbool ( NAME ) -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html