Plug these two modules together. The only minor API difference is that in void context (ie, return value is not being used), the new Git::Config function does not try to unpack the value. So, the exist test - which was testing an error condition - must be changed to actually try to retrieve the values so that the exceptions can happen. Signed-off-by: Sam Vilain <sam.vilain@xxxxxxxxxxxxxxx> --- perl/Git.pm | 103 +++++++++++++++++++++++++++---------------------------- t/t9700/test.pl | 4 +- 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/perl/Git.pm b/perl/Git.pm index 7d7f2b1..3141b41 100644 --- a/perl/Git.pm +++ b/perl/Git.pm @@ -550,32 +550,38 @@ 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. +This delegates via L<Git::Config> for cached config file access. To +force a re-read of the configuration, call C<$git-E<gt>config->read> -=cut +=item config -sub config { - my ($self, $var) = _maybe_self(@_); +With no arguments, the C<config> method returns a L<Git::Config> +object. See L<Git::Config> for the API information. - 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; - } +=item config ( VARIABLE => value ) + +With two arguments, the configuration is updated with the passed +value. + +=cut + +sub _config { + my ($self) = _maybe_self_new(@_); + $self->{config} ||= do { + require Git::Config; + Git::Config->new($self); }; } +sub config { + (my $self, @_) = _maybe_self_new(@_); + if (@_) { + return $self->_config->config(@_); + } + else { + return $self->_config; + } +} =item config_bool ( VARIABLE ) @@ -583,28 +589,21 @@ 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. +This delegates via L<Git::Config> for cached config file access. + +=item config_bool ( VARIABLE => value ) + +Sets a boolean slot to the given value. This always writes 'true' or +'false' to the configuration file, regardless of the value passed. =cut sub config_bool { - my ($self, $var) = _maybe_self(@_); + (my ($self, $var), @_) = _maybe_self_new(@_); - 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 $conf = $self->_config; + $conf->type($var => "boolean"); + $conf->config($var, @_); } =item config_int ( VARIABLE ) @@ -615,26 +614,22 @@ 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. +This delegates via L<Git::Config> for cached config file access. + +=item config_int ( VARIABLE => value ) + +Sets a integer slot to the given value. This method will reduce the +written value to the shortest way to express the given number; eg +1024 is written as C<1k> and 16777216 will be written as C<16M>. =cut sub config_int { - my ($self, $var) = _maybe_self(@_); + (my ($self, $var), @_) = _maybe_self_new(@_); - 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 $conf = $self->_config; + $conf->type($var => "integer"); + $conf->config($var, @_); } =item get_colorbool ( NAME ) @@ -1204,6 +1199,7 @@ either version 2, or (at your option) any later version. =cut +our $_self; # Take raw method argument list and return ($obj, @args) in case # the method was called upon an instance and (undef, @args) if @@ -1211,6 +1207,9 @@ either version 2, or (at your option) any later version. sub _maybe_self { UNIVERSAL::isa($_[0], 'Git') ? @_ : (undef, @_); } +sub _maybe_self_new { + UNIVERSAL::isa($_[0], 'Git') ? @_ : ($_self||=Git->repository, @_); +} # Check if the command id is something reasonable. sub _check_valid_cmd { diff --git a/t/t9700/test.pl b/t/t9700/test.pl index 697daf3..1b94633 100755 --- a/t/t9700/test.pl +++ b/t/t9700/test.pl @@ -35,9 +35,9 @@ is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color"); # Save and restore STDERR; we will probably extract this into a # "dies_ok" method and possibly move the STDERR handling to Git.pm. open our $tmpstderr, ">&STDERR" or die "cannot save STDERR"; close STDERR; -eval { $r->config("test.dupstring") }; +eval { my $x = $r->config("test.dupstring") }; ok($@, "config: duplicate entry in scalar context fails"); -eval { $r->config_bool("test.boolother") }; +eval { my $x = $r->config_bool("test.boolother") }; ok($@, "config_bool: non-boolean values fail"); open STDERR, ">&", $tmpstderr or die "cannot restore STDERR"; -- 1.6.0 -- 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