From 16d371440df14473a38522534b3dc7532168f999 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 15 Nov 2016 21:40:37 -0800 Subject: [PATCH] Util.pm: add git_bool function Most helpful for interpreting a "boolean" value according to Git semantics when dealing with read_config_file/read_config_file_hash output. Translates a value according to Git boolean semantics into either 0 or 1 or returns undef if it's not a boolean according to Git. The "undef" value can be treated as true by passing an optional second parameter that's true. This can be helpful when dealing with raw read_config_file output where valueless booleans present with an undef value. Signed-off-by: Kyle J. McKay --- Girocco/Util.pm | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Girocco/Util.pm b/Girocco/Util.pm index bc235ba..94dd35e 100644 --- a/Girocco/Util.pm +++ b/Girocco/Util.pm @@ -22,7 +22,7 @@ BEGIN { calc_bigfilethreshold has_reserved_suffix noFatalsToBrowser calc_redeltathreshold clean_email_multi read_HEAD_symref read_config_file - read_config_file_hash is_git_dir); + read_config_file_hash is_git_dir git_bool); } my $encoder; @@ -1172,4 +1172,14 @@ sub is_git_dir { return $hv =~ /^[0-9a-f]{40}/; } +# Returns 0 for false, 1 for true, undef for unrecognized or undef +# Unless the optional second argument is true in which case undef returns 1 +sub git_bool { + defined($_[0]) or return $_[1] ? 1 : undef; + my $v = lc($_[0]); + return 0 if $v eq 'false' || $v eq 'off' || $v eq 'no' || $v eq '' || $v =~ /^[-+]?0+$/; + return 1 if $v eq 'true' || $v eq 'on' || $v eq 'yes' || $v =~ /^[-+]?0*[1-9][0-9]*$/; + return undef; +} + 1; -- 2.11.4.GIT