[PATCH/RFC 3/2 (fixed)] Refactor Git::config_*
- From
Jakub Narebski <jnareb@gmail.com>
- Date
- Oct 18, 2011, 09:47 UTC
- Message-ID
- <201110181147.02397.jnareb@gmail.com>
- In-Reply-To
- <201110172347.42568.jnareb@gmail.com>
From: Junio C Hamano <gitster@pobox.com>
There is, especially with addition of Git::config_path(), much code repetition in the Git::config_* family of subroutines.
Refactor common parts of Git::config(), Git::config_bool(), Git::config_int() and Git::config_path() into _config_common() helper method, reducing code duplication.
Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Jakub Narebski <jnareb@gmail.com> --- Jakub Narebski wrote:
> > I'll resend amended commit.
Here it is.
perl/Git.pm | 74 ++++++++++++++-------------------------------------------- 1 files changed, 18 insertions(+), 56 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm index c775b4f..8e1e2fd 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -576,24 +576,7 @@ This currently wraps command('config') so it is not so fast. 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($self, $var); } @@ -609,25 +592,10 @@ This currently wraps command('config') so it is not so fast. 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 @@ -639,24 +607,7 @@ This currently wraps command('config') so it is not so fast. 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'}); } @@ -705,16 +656,27 @@ This currently wraps command('config') so it is not so fast. sub config_int { my ($self, $var) = _maybe_self(@_); + return scalar _config_common($self, $var, {'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 ($self, $var, $opts) = @_; 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; }
-- 1.7.6