Git.pm: Add config() method
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>
Petr Baudis committed
Jul 3, 2006 at 22:47 UTC
dc2613de8633cecb1c0759657eadf6a637cebfa5
3 files changed
+39
-3
Documentation/git-repo-config.txt
+2
-1
@@ -54,7 +54,8 @@ OPTIONS
54
55
--get::
56
Get the value for a given key (optionally filtered by a regex
57
- matching the value).
57
+ matching the value). Returns error code 1 if the key was not
58
+ found and error code 2 if multiple key values were found.
59
60
--get-all::
61
Like get, but does not fail if the number of values for the key
perl/Git.pm
+36
-1
@@ -473,7 +473,6 @@ and the directory must exist.
473
474
sub wc_chdir {
475
my ($self, $subdir) = @_;
476
-
476
$self->wc_path()
477
or throw Error::Simple("bare repository");
478
@@ -486,6 +485,42 @@ sub wc_chdir {
485
}
486
487
488
+=item config ( VARIABLE )
489
+
490
+Retrieve the configuration C<VARIABLE> in the same manner as C<repo-config>
491
+does. In scalar context requires the variable to be set only one time
492
+(exception is thrown otherwise), in array context returns allows the
493
+variable to be set multiple times and returns all the values.
494
+
495
+Must be called on a repository instance.
496
+
497
+This currently wraps command('repo-config') so it is not so fast.
498
+
499
+=cut
500
+
501
+sub config {
502
+ my ($self, $var) = @_;
503
+ $self->repo_path()
504
+ or throw Error::Simple("not a repository");
505
+
506
+ try {
507
+ if (wantarray) {
508
+ return $self->command('repo-config', '--get-all', $var);
509
+ } else {
510
+ return $self->command_oneline('repo-config', '--get', $var);
511
+ }
512
+ } catch Git::Error::Command with {
513
+ my $E = shift;
514
+ if ($E->value() == 1) {
515
+ # Key not found.
516
+ return undef;
517
+ } else {
518
+ throw $E;
519
+ }
520
+ };
521
+}
522
+
523
+
524
=item hash_object ( TYPE, FILENAME )
525
526
=item hash_object ( TYPE, FILEHANDLE )
repo-config.c
+1
-1
@@ -118,7 +118,7 @@ static int get_value(const char* key_, const char* regex_)
118
if (do_all)
119
ret = !seen;
120
else
121
- ret = (seen == 1) ? 0 : 1;
121
+ ret = (seen == 1) ? 0 : seen > 1 ? 2 : 1;
122
123
free_strings:
124
if (repo_config)