]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Teach Git how to parse standard power of 2 suffixes.
authorShawn O. Pearce <spearce@spearce.org>
Sun, 31 Dec 2006 03:13:05 +0000 (22:13 -0500)
committerJunio C Hamano <junkio@cox.net>
Sun, 31 Dec 2006 06:22:14 +0000 (22:22 -0800)
Sometimes its necessary to supply a value as a power of two in a
configuration parameter.  In this case the user may want to use the
standard suffixes such as K, M, or G to indicate that the numerical
value should be multiplied by a constant base before being used.

Shell scripts/etc. can also benefit from this automatic option
parsing with `git repo-config --int`.

[jc: with a couple of test and a slight input tightening]

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Documentation/git-repo-config.txt
config.c
t/t1300-repo-config.sh

index b379ec5075981347e75d0795e44e57620d89399b..c55a8ba0dcfc63110920bf6b5e8db91732d11853 100644 (file)
@@ -87,7 +87,10 @@ OPTIONS
        git-repo-config will ensure that the output is "true" or "false"
 
 --int::
-       git-repo-config will ensure that the output is a simple decimal number
+       git-repo-config will ensure that the output is a simple
+       decimal number.  An optional value suffix of 'k', 'm', or 'g'
+       in the config file will cause the value to be multiplied
+       by 1024, 1048576, or 1073741824 prior to output.
 
 
 ENVIRONMENT
index fcccf7e2a4f3b7487af10d4f7b505c7ef492b9e8..458ae512f3b644979ddc1f6bb581fee2907dbc2f 100644 (file)
--- a/config.c
+++ b/config.c
@@ -238,6 +238,12 @@ int git_config_int(const char *name, const char *value)
                int val = strtol(value, &end, 0);
                if (!*end)
                        return val;
+               if (!strcasecmp(end, "k"))
+                       return val * 1024;
+               if (!strcasecmp(end, "m"))
+                       return val * 1024 * 1024;
+               if (!strcasecmp(end, "g"))
+                       return val * 1024 * 1024 * 1024;
        }
        die("bad config value for '%s' in %s", name, config_file_name);
 }
index e48a4ecdcf7129da1431928bdb942eae8c3e6515..a29caa06dc6545b7fc23b3446a713b75f49cd146 100755 (executable)
@@ -391,5 +391,15 @@ EOF
 
 test_expect_success "rename succeeded" "diff -u expect .git/config"
 
+test_expect_success numbers '
+
+       git-repo-config kilo.gram 1k &&
+       git-repo-config mega.ton 1m &&
+       k=$(git-repo-config --int --get kilo.gram) &&
+       test z1024 = "z$k" &&
+       m=$(git-repo-config --int --get mega.ton) &&
+       test z1048576 = "z$m"
+'
+
 test_done