@matheusbsbs / matheus-bagliano / commits / d77a64d353
Fork 0

Teach Git how to parse standard power of 2 suffixes.

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>

Shawn O. Pearce committed Dec 30, 2006 at 22:13 UTC d77a64d353cea0f72655b86dd04bcf9f86cbbea6
3 files changed +20 -1
Documentation/git-repo-config.txt
+4 -1
@@ -87,7 +87,10 @@ OPTIONS
87 git-repo-config will ensure that the output is "true" or "false"
88
89 --int::
90 - git-repo-config will ensure that the output is a simple decimal number
90 + git-repo-config will ensure that the output is a simple
91 + decimal number. An optional value suffix of 'k', 'm', or 'g'
92 + in the config file will cause the value to be multiplied
93 + by 1024, 1048576, or 1073741824 prior to output.
94
95
96 ENVIRONMENT
config.c
+6
@@ -238,6 +238,12 @@ int git_config_int(const char *name, const char *value)
238 int val = strtol(value, &end, 0);
239 if (!*end)
240 return val;
241 + if (!strcasecmp(end, "k"))
242 + return val * 1024;
243 + if (!strcasecmp(end, "m"))
244 + return val * 1024 * 1024;
245 + if (!strcasecmp(end, "g"))
246 + return val * 1024 * 1024 * 1024;
247 }
248 die("bad config value for '%s' in %s", name, config_file_name);
249 }
t/t1300-repo-config.sh
+10
@@ -391,5 +391,15 @@ EOF
391
392 test_expect_success "rename succeeded" "diff -u expect .git/config"
393
394 +test_expect_success numbers '
395 +
396 + git-repo-config kilo.gram 1k &&
397 + git-repo-config mega.ton 1m &&
398 + k=$(git-repo-config --int --get kilo.gram) &&
399 + test z1024 = "z$k" &&
400 + m=$(git-repo-config --int --get mega.ton) &&
401 + test z1048576 = "z$m"
402 +'
403 +
404 test_done
405