git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Sam Vilain <sam.vilain@catalyst.net.nz>
To: git@vger.kernel.org
Cc: Petr Baudis <pasky@suse.cz>, Sam Vilain <sam.vilain@catalyst.net.nz>
Subject: [PATCH] perl: make Git.pm use new Git::Config module
Date: Mon,  6 Apr 2009 11:46:16 +1200	[thread overview]
Message-ID: <1238975176-14354-2-git-send-email-sam.vilain@catalyst.net.nz> (raw)
In-Reply-To: <1238975176-14354-1-git-send-email-sam.vilain@catalyst.net.nz>

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@catalyst.net.nz>
---
 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

  reply	other threads:[~2009-04-06  0:23 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-05 23:46 [PATCH] perl: add new module Git::Config for cached 'git config' access Sam Vilain
2009-04-05 23:46 ` Sam Vilain [this message]
2009-04-06  9:29 ` Frank Lichtenheld
2009-04-06 22:50   ` Sam Vilain
2009-04-07 12:01     ` Jakub Narebski
2009-04-08  5:49       ` Sam Vilain
2009-04-08 10:18         ` Jakub Narebski
2009-04-08 10:44           ` Sam Vilain
2009-04-08 23:13             ` Jakub Narebski
2009-04-08  6:29       ` Junio C Hamano
2009-04-08  9:50         ` Sam Vilain
2009-04-08 18:51           ` Junio C Hamano
2009-04-08  8:12 ` Petr Baudis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1238975176-14354-2-git-send-email-sam.vilain@catalyst.net.nz \
    --to=sam.vilain@catalyst.net.nz \
    --cc=git@vger.kernel.org \
    --cc=pasky@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).