From: Jakub Narebski <jnareb@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Eric Wong <normalperson@yhbt.net>,
Cord Seele <cowose@googlemail.com>,
Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>,
git@vger.kernel.org, Cord Seele <cowose@gmail.com>
Subject: [PATCH/RFC 3/2 v3] Refactor Git::config_*
Date: Wed, 19 Oct 2011 00:09:39 +0200 [thread overview]
Message-ID: <201110190009.40470.jnareb@gmail.com> (raw)
In-Reply-To: <7vty76f57d.fsf@alter.siamese.dyndns.org>
On Tue, 18 Oct 2011, Junio C Hamano wrote:
> Jakub Narebski <jnareb@gmail.com> writes:
> > From: Junio C Hamano <gitster@pobox.com>
> >
> > There is, especially with addition of Git::config_path(), much code
> > repetition in the Git::config_* family of subroutines.
> >
> > Refactor common parts of Git::config(), Git::config_bool(),
> > Git::config_int() and Git::config_path() into _config_common()
> > helper method, reducing code duplication.
> >
> > Signed-off-by: Junio C Hamano <gitster@pobox.com>
> > Signed-off-by: Jakub Narebski <jnareb@gmail.com>
> > ---
> > Jakub Narebski wrote:
> > >
> > > I'll resend amended commit.
> >
> > Here it is.
>
> Well, this breaks t9001 and I ended up spending an hour and half figuring
> out why. Admittedly, I was doing something else on the side, so it is not
> like the whole 90 minutes is the billable hour for reviewing this patch,
> but as far as the Git project is concerned, I didn't have any other branch
> checked out in my working tree, so that whole time was what ended up
> costing.
I'm sorry about that. I have checked t9700-perl-git.sh and
t9100-git-svn-basic.sh that the version before fixup had problems with,
but for some reason I had many spurious test failures, so I have not run
the full test suite (well, it would be enough to run those that require
Git.pm).
Nb. I still have:
Test Summary Report
-------------------
t1402-check-ref-format.sh (Wstat: 256 Tests: 93 Failed: 1)
Failed test: 31
Non-zero exit status: 1
t4034-diff-words.sh (Wstat: 256 Tests: 34 Failed: 2)
Failed tests: 21, 25
Non-zero exit status: 1
> The real problem was here.
>
> > @@ -609,25 +592,10 @@ This currently wraps command('config') so it is not so fast.
> >
> > 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';
> > ...
> > - };
> > + my $val = scalar _config_common($self, $var, {'kind' => '--bool'});
> > + return (defined $val && $val eq 'true');
> > }
>
> Can you spot the difference?
Damn.
I really should have done refactoring from scratch myself, instead of basing
it on "how about that" throwaway patch.
Fixed now in the version below.
> This is the reason why I do not particularly like a rewrite for the sake
> of rewriting.
The goal was to reduce code duplication to _avoid_ errors.
What I have noticed is that there is slight difference between original
Git::config_path and the one after refactoring. In the version before
this patch the error catching part of config_path() looks like this:
} catch Git::Error::Command with {
my $E = shift;
if ($E->value() == 1) {
# Key not found.
return undef;
} else {
throw $E;
}
};
while after this patch (and in config()) it looks like this:
} catch Git::Error::Command with {
my $E = shift;
if ($E->value() == 1) {
# Key not found.
return;
} else {
throw $E;
}
};
I am not sure which one is right, but I suspect the latter.
Cord?
-- >8 ------------ >8 -------------- >8 --
From: Junio C Hamano <gitster@pobox.com>
There is, especially with addition of Git::config_path(), much code
repetition in the Git::config_* family of subroutines.
Refactor common parts of Git::config(), Git::config_bool(),
Git::config_int() and Git::config_path() into _config_common()
helper method, reducing code duplication.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
perl/Git.pm | 73 ++++++++++++++++------------------------------------------
1 files changed, 20 insertions(+), 53 deletions(-)
diff --git a/perl/Git.pm b/perl/Git.pm
index c279bfb..b7035ad 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -577,23 +577,7 @@ This currently wraps command('config') so it is not so fast.
sub config {
my ($self, $var) = _maybe_self(@_);
- 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;
- }
- };
+ return _config_common($self, $var);
}
@@ -610,24 +594,11 @@ This currently wraps command('config') so it is not so fast.
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;
- }
- };
+ my $val = scalar _config_common($self, $var, {'kind' => '--bool'});
+ return undef unless defined $val;
+ return $val eq 'true';
}
-
=item config_path ( VARIABLE )
Retrieve the path configuration C<VARIABLE>. The return value
@@ -640,23 +611,7 @@ This currently wraps command('config') so it is not so fast.
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;
- }
- };
+ return _config_common($self, $var, {'kind' => '--path'});
}
=item config_int ( VARIABLE )
@@ -674,15 +629,27 @@ This currently wraps command('config') so it is not so fast.
sub config_int {
my ($self, $var) = _maybe_self(@_);
+ return scalar _config_common($self, $var, {'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 ($self, $var, $opts) = @_;
+
try {
- my @cmd = ('config', '--int', '--get', $var);
+ my @cmd = ('config', $opts->{'kind'} ? $opts->{'kind'} : ());
unshift @cmd, $self if $self;
- return command_oneline(@cmd);
+ 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;
+ return;
} else {
throw $E;
}
--
1.7.6
next prev parent reply other threads:[~2011-10-18 22:09 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-09-28 13:13 [PATCH] git-send-email.perl: expand filename of aliasesfile Cord Seele
2011-09-28 13:42 ` Matthieu Moy
2011-09-28 14:40 ` Cord Seele
2011-09-28 14:47 ` Matthieu Moy
2011-09-30 10:52 ` [PATCH v2] git-send-email: allow filename expansion Cord Seele
2011-09-30 10:52 ` [PATCH 1/2] Add Git::config_path() Cord Seele
2011-10-07 20:32 ` Jakub Narebski
2011-10-07 21:35 ` Junio C Hamano
2011-10-07 21:44 ` Jakub Narebski
2011-10-07 22:26 ` Junio C Hamano
2011-09-30 10:52 ` [PATCH 2/2] use new Git::config_path() for aliasesfile Cord Seele
2011-09-30 19:55 ` Junio C Hamano
2011-09-30 21:16 ` Cord Seele
2011-09-30 22:00 ` Jakub Narebski
2011-09-30 22:18 ` Junio C Hamano
2011-10-07 21:17 ` [PATCH/RFC 3/2] Refactor Git::config_* Jakub Narebski
2011-10-17 20:50 ` Junio C Hamano
2011-10-17 21:47 ` Jakub Narebski
2011-10-17 23:25 ` Junio C Hamano
2011-10-18 9:47 ` [PATCH/RFC 3/2 (fixed)] " Jakub Narebski
2011-10-18 19:52 ` Junio C Hamano
2011-10-18 22:09 ` Jakub Narebski [this message]
2011-10-18 23:25 ` [PATCH/RFC 3/2 v3] " Junio C Hamano
2011-10-19 0:19 ` Jakub Narebski
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201110190009.40470.jnareb@gmail.com \
--to=jnareb@gmail.com \
--cc=Matthieu.Moy@grenoble-inp.fr \
--cc=cowose@gmail.com \
--cc=cowose@googlemail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=normalperson@yhbt.net \
/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.
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 NNTP newsgroup(s).