libperl-git: refactor Git::config_*

Move common parts of Git::config(), Git::config_bool(), Git::config_int() and Git::config_path() into _config_common() helper. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 18, 2011 at 11:47 UTC 6942a3d796ce7a824bd9a69e88cf0a9578225bb2
1 file changed +24 -64
perl/Git.pm
+24 -64
@@ -570,30 +570,10 @@ does. In scalar context requires the variable to be set only one time
570 (exception is thrown otherwise), in array context returns allows the
571 variable to be set multiple times and returns all the values.
572
573 -This currently wraps command('config') so it is not so fast.
574 -
573 =cut
574
575 sub config {
578 - my ($self, $var) = _maybe_self(@_);
579 -
580 - try {
581 - my @cmd = ('config');
582 - unshift @cmd, $self if $self;
583 - if (wantarray) {
584 - return command(@cmd, '--get-all', $var);
585 - } else {
586 - return command_oneline(@cmd, '--get', $var);
587 - }
588 - } catch Git::Error::Command with {
589 - my $E = shift;
590 - if ($E->value() == 1) {
591 - # Key not found.
592 - return;
593 - } else {
594 - throw $E;
595 - }
596 - };
576 + return _config_common({}, @_);
577 }
578
579
@@ -603,28 +583,18 @@ Retrieve the bool configuration C<VARIABLE>. The return value
583 is usable as a boolean in perl (and C<undef> if it's not defined,
584 of course).
585
606 -This currently wraps command('config') so it is not so fast.
607 -
586 =cut
587
588 sub config_bool {
611 - my ($self, $var) = _maybe_self(@_);
589 + my $val = scalar _config_common({'kind' => '--bool'}, @_);
590
613 - try {
614 - my @cmd = ('config', '--bool', '--get', $var);
615 - unshift @cmd, $self if $self;
616 - my $val = command_oneline(@cmd);
617 - return undef unless defined $val;
591 + # Do not rewrite this as return (defined $val && $val eq 'true')
592 + # as some callers do care what kind of falsehood they receive.
593 + if (!defined $val) {
594 + return undef;
595 + } else {
596 return $val eq 'true';
619 - } catch Git::Error::Command with {
620 - my $E = shift;
621 - if ($E->value() == 1) {
622 - # Key not found.
623 - return undef;
624 - } else {
625 - throw $E;
626 - }
627 - };
597 + }
598 }
599
600
@@ -633,32 +603,13 @@ sub config_bool {
603 Retrieve the path configuration C<VARIABLE>. The return value
604 is an expanded path or C<undef> if it's not defined.
605
636 -This currently wraps command('config') so it is not so fast.
637 -
606 =cut
607
608 sub config_path {
641 - my ($self, $var) = _maybe_self(@_);
642 -
643 - try {
644 - my @cmd = ('config', '--path');
645 - unshift @cmd, $self if $self;
646 - if (wantarray) {
647 - return command(@cmd, '--get-all', $var);
648 - } else {
649 - return command_oneline(@cmd, '--get', $var);
650 - }
651 - } catch Git::Error::Command with {
652 - my $E = shift;
653 - if ($E->value() == 1) {
654 - # Key not found.
655 - return undef;
656 - } else {
657 - throw $E;
658 - }
659 - };
609 + return _config_common({'kind' => '--path'}, @_);
610 }
611
612 +
613 =item config_int ( VARIABLE )
614
615 Retrieve the integer configuration C<VARIABLE>. The return value
@@ -667,22 +618,31 @@ or 'g' in the config file will cause the value to be multiplied
618 by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.
619 It would return C<undef> if configuration variable is not defined,
620
670 -This currently wraps command('config') so it is not so fast.
671 -
621 =cut
622
623 sub config_int {
624 + return scalar _config_common({'kind' => '--int'}, @_);
625 +}
626 +
627 +# Common subroutine to implement bulk of what the config* family of methods
628 +# do. This curently wraps command('config') so it is not so fast.
629 +sub _config_common {
630 + my ($opts) = shift @_;
631 my ($self, $var) = _maybe_self(@_);
632
633 try {
678 - my @cmd = ('config', '--int', '--get', $var);
634 + my @cmd = ('config', $opts->{'kind'} ? $opts->{'kind'} : ());
635 unshift @cmd, $self if $self;
680 - return command_oneline(@cmd);
636 + if (wantarray) {
637 + return command(@cmd, '--get-all', $var);
638 + } else {
639 + return command_oneline(@cmd, '--get', $var);
640 + }
641 } catch Git::Error::Command with {
642 my $E = shift;
643 if ($E->value() == 1) {
644 # Key not found.
685 - return undef;
645 + return;
646 } else {
647 throw $E;
648 }