@@ -570,30 +570,10 @@ does. In scalar context requires the variable to be set only one time
570
570
(exception is thrown otherwise), in array context returns allows the
571
571
variable to be set multiple times and returns all the values.
572
572
573
- This currently wraps command('config') so it is not so fast.
574
-
575
573
=cut
576
574
577
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({}, @_ );
597
577
}
598
578
599
579
@@ -603,28 +583,18 @@ Retrieve the bool configuration C<VARIABLE>. The return value
603
583
is usable as a boolean in perl (and C<undef > if it's not defined,
604
584
of course).
605
585
606
- This currently wraps command('config') so it is not so fast.
607
-
608
586
=cut
609
587
610
588
sub config_bool {
611
- my ( $self , $var ) = _maybe_self( @_ );
589
+ my $val = scalar _config_common({ ' kind ' => ' --bool ' }, @_ );
612
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 {
618
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
+ }
628
598
}
629
599
630
600
@@ -633,32 +603,13 @@ sub config_bool {
633
603
Retrieve the path configuration C<VARIABLE > . The return value
634
604
is an expanded path or C<undef > if it's not defined.
635
605
636
- This currently wraps command('config') so it is not so fast.
637
-
638
606
=cut
639
607
640
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' }, @_ );
660
610
}
661
611
612
+
662
613
=item config_int ( VARIABLE )
663
614
664
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
667
618
by 1024, 1048576 (1024^2), or 1073741824 (1024^3) prior to output.
668
619
It would return C<undef > if configuration variable is not defined,
669
620
670
- This currently wraps command('config') so it is not so fast.
671
-
672
621
=cut
673
622
674
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 @_ ;
675
631
my ($self , $var ) = _maybe_self(@_ );
676
632
677
633
try {
678
- my @cmd = (' config' , ' --int ' , ' --get ' , $var );
634
+ my @cmd = (' config' , $opts -> { ' kind ' } ? $opts -> { ' kind ' } : () );
679
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
+ }
681
641
} catch Git::Error::Command with {
682
642
my $E = shift ;
683
643
if ($E -> value() == 1) {
684
644
# Key not found.
685
- return undef ;
645
+ return ;
686
646
} else {
687
647
throw $E ;
688
648
}
0 commit comments