Skip to content

Commit

Permalink
Git.pm: Add config() method
Browse files Browse the repository at this point in the history
This accessor will retrieve value(s) of the given configuration variable.

Signed-off-by: Petr Baudis <pasky@suse.cz>
Signed-off-by: Junio C Hamano <junkio@cox.net>
  • Loading branch information
Petr Baudis authored and Junio C Hamano committed Jul 4, 2006
1 parent 6fcca93 commit dc2613d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Documentation/git-repo-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ OPTIONS

--get::
Get the value for a given key (optionally filtered by a regex
matching the value).
matching the value). Returns error code 1 if the key was not
found and error code 2 if multiple key values were found.

--get-all::
Like get, but does not fail if the number of values for the key
Expand Down
37 changes: 36 additions & 1 deletion perl/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ and the directory must exist.

sub wc_chdir {
my ($self, $subdir) = @_;

$self->wc_path()
or throw Error::Simple("bare repository");

Expand All @@ -486,6 +485,42 @@ sub wc_chdir {
}


=item config ( VARIABLE )
Retrieve the configuration C<VARIABLE> in the same manner as C<repo-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.
Must be called on a repository instance.
This currently wraps command('repo-config') so it is not so fast.
=cut

sub config {
my ($self, $var) = @_;
$self->repo_path()
or throw Error::Simple("not a repository");

try {
if (wantarray) {
return $self->command('repo-config', '--get-all', $var);
} else {
return $self->command_oneline('repo-config', '--get', $var);
}
} catch Git::Error::Command with {
my $E = shift;
if ($E->value() == 1) {
# Key not found.
return undef;
} else {
throw $E;
}
};
}


=item hash_object ( TYPE, FILENAME )
=item hash_object ( TYPE, FILEHANDLE )
Expand Down
2 changes: 1 addition & 1 deletion repo-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static int get_value(const char* key_, const char* regex_)
if (do_all)
ret = !seen;
else
ret = (seen == 1) ? 0 : 1;
ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;

free_strings:
if (repo_config)
Expand Down

0 comments on commit dc2613d

Please sign in to comment.