Merge branch 'jn/libperl-git-config'
* jn/libperl-git-config: Add simple test for Git::config_path() in t/t9700-perl-git.sh libperl-git: refactor Git::config_*
This commit is contained in:
commit
3b6a5d2d05
152
perl/Git.pm
152
perl/Git.pm
@ -570,15 +570,68 @@ 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.
|
||||
|
||||
=cut
|
||||
|
||||
sub config {
|
||||
return _config_common({}, @_);
|
||||
}
|
||||
|
||||
|
||||
=item config_bool ( VARIABLE )
|
||||
|
||||
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).
|
||||
|
||||
=cut
|
||||
|
||||
sub config_bool {
|
||||
my $val = scalar _config_common({'kind' => '--bool'}, @_);
|
||||
|
||||
# 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';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
=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.
|
||||
|
||||
=cut
|
||||
|
||||
sub config_path {
|
||||
return _config_common({'kind' => '--path'}, @_);
|
||||
}
|
||||
|
||||
|
||||
=item config_int ( VARIABLE )
|
||||
|
||||
Retrieve the integer configuration C<VARIABLE>. The return value
|
||||
is simple decimal number. An optional value suffix of 'k', 'm',
|
||||
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,
|
||||
|
||||
=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');
|
||||
my @cmd = ('config', $opts->{'kind'} ? $opts->{'kind'} : ());
|
||||
unshift @cmd, $self if $self;
|
||||
if (wantarray) {
|
||||
return command(@cmd, '--get-all', $var);
|
||||
@ -596,99 +649,6 @@ sub config {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
=item config_bool ( VARIABLE )
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
=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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
=item config_int ( VARIABLE )
|
||||
|
||||
Retrieve the integer configuration C<VARIABLE>. The return value
|
||||
is simple decimal number. An optional value suffix of 'k', 'm',
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
=item get_colorbool ( NAME )
|
||||
|
||||
Finds if color should be used for NAMEd operation from the configuration,
|
||||
|
@ -43,7 +43,11 @@ test_expect_success \
|
||||
git config --add test.booltrue true &&
|
||||
git config --add test.boolfalse no &&
|
||||
git config --add test.boolother other &&
|
||||
git config --add test.int 2k
|
||||
git config --add test.int 2k &&
|
||||
git config --add test.path "~/foo" &&
|
||||
git config --add test.pathexpanded "$HOME/foo" &&
|
||||
git config --add test.pathmulti foo &&
|
||||
git config --add test.pathmulti bar
|
||||
'
|
||||
|
||||
# The external test will outputs its own plan
|
||||
|
@ -33,6 +33,10 @@ is($r->config_int("test.int"), 2048, "config_int: integer");
|
||||
is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
|
||||
ok($r->config_bool("test.booltrue"), "config_bool: true");
|
||||
ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
|
||||
is($r->config_path("test.path"), $r->config("test.pathexpanded"),
|
||||
"config_path: ~/foo expansion");
|
||||
is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"],
|
||||
"config_path: multiple values");
|
||||
our $ansi_green = "\x1b[32m";
|
||||
is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
|
||||
# Cannot test $r->get_colorbool("color.foo")) because we do not
|
||||
|
Loading…
Reference in New Issue
Block a user