From: Junio C Hamano Date: Tue, 18 Oct 2011 09:47:01 +0000 (+0200) Subject: libperl-git: refactor Git::config_* X-Git-Tag: v1.7.8-rc0~12^2~1 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/6942a3d796ce7a824bd9a69e88cf0a9578225bb2 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 --- diff --git a/perl/Git.pm b/perl/Git.pm index c279bfb244..f7ce511bbb 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -570,30 +570,10 @@ sub wc_chdir { (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. - =cut sub config { - my ($self, $var) = _maybe_self(@_); - - try { - my @cmd = ('config'); - 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; - } else { - throw $E; - } - }; + return _config_common({}, @_); } @@ -603,28 +583,18 @@ sub config { is usable as a boolean in perl (and C 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(@_); + my $val = scalar _config_common({'kind' => '--bool'}, @_); - try { - my @cmd = ('config', '--bool', '--get', $var); - unshift @cmd, $self if $self; - my $val = command_oneline(@cmd); - return undef unless defined $val; + # Do not rewrite this as return (defined $val && $val eq 'true') + # as some callers do care what kind of falsehood they receive. + if (!defined $val) { + return undef; + } else { return $val eq 'true'; - } catch Git::Error::Command with { - my $E = shift; - if ($E->value() == 1) { - # Key not found. - return undef; - } else { - throw $E; - } - }; + } } @@ -633,32 +603,13 @@ sub config_bool { Retrieve the path configuration C. The return value is an expanded path or C 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({'kind' => '--path'}, @_); } + =item config_int ( VARIABLE ) Retrieve the integer configuration C. The return value @@ -667,22 +618,31 @@ sub config_path { by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output. It would return C if configuration variable is not defined, -This currently wraps command('config') so it is not so fast. - =cut sub config_int { + return scalar _config_common({'kind' => '--int'}, @_); +} + +# Common subroutine to implement bulk of what the config* family of methods +# do. This curently wraps command('config') so it is not so fast. +sub _config_common { + my ($opts) = shift @_; my ($self, $var) = _maybe_self(@_); try { - my @cmd = ('config', '--int', '--get', $var); + my @cmd = ('config', $opts->{'kind'} ? $opts->{'kind'} : ()); unshift @cmd, $self if $self; - return command_oneline(@cmd); + 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; + return; } else { throw $E; }