Skip to contentSkip to conversation

Teach Git how to parse standard power of 2 suffixes.

PATCH In review Shawn O. Pearce 2 participants 8 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 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.

---

This is a resend of the prior version of this patch. Junio convinced me on #git that this version might be better. :-)

Meant for the top of sp/mmap, but may be useful elsewhere.

 Documentation/git-repo-config.txt |    5 ++++-
 config.c                          |    8 ++++++++
 2 files changed, 12 insertions(+), 1 deletions(-)
Documentation/git-repo-config.txt +4 -1
index b379ec5..c55a8ba 100644
@@ -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
config.c +8 -0
index 2e0d5a8..83ce9e1 100644
@@ -236,8 +236,16 @@ int git_config_int(const char *name, const char *value)
 	if (value && *value) {
 		char *end;
 		int val = strtol(value, &end, 0);
+		while (isspace(*end))
+			end++;
 		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);
 }
Signature · 1 line
1.5.0.rc0.g6bb1
#1 lore raw

Documentation/git-repo-config.txt OPTIONS · line 93

Quoted 13 lines · Shawn O. Pearce
Documentation/git-repo-config.txt +4 -1
index b379ec5..c55a8ba 100644
@@ -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.

Thanks. I think this is applicable on top of 'master'.

config.c int git_config_int(const char *name, const char *value) · line 240

Quoted 11 lines
Documentation/git-repo-config.txt +0 -0
 ENVIRONMENT
config.c +2 -0
index 2e0d5a8..83ce9e1 100644
@@ -236,8 +236,16 @@ int git_config_int(const char *name, const char *value)
 	if (value && *value) {
 		char *end;
 		int val = strtol(value, &end, 0);
+		while (isspace(*end))
+			end++;

Why? Are you allowing "1024 k"? Do we want to?

Quoted 13 lines
config.c +6 -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);
 }
Signature · 1 line
1.5.0.rc0.g6bb1
#2 lore raw
Quoted 4 lines · Junio C Hamano
Quoted 4 lines
config.c +0 -0
index 2e0d5a8..83ce9e1 100644
Quoted 8 lines

config.c int git_config_int(const char *name, const char *value) · line 240

Quoted 6 lines
config.c +2 -0
@@ -236,8 +236,16 @@ int git_config_int(const char *name, const char *value)
 	if (value && *value) {
 		char *end;
 		int val = strtol(value, &end, 0);
+		while (isspace(*end))
+			end++;

Why? Are you allowing "1024 k"? Do we want to?

Yes. Why not?

Signature · 1 line
Shawn.
#3 lore raw
Quoted 5 lines · Shawn O. Pearce
Quoted 1 line · Junio C Hamano

Why? Are you allowing "1024 k"? Do we want to?

Yes. Why not?

Stricter input checking. Allowing extra things later is far easier than later finding problems with a looser way we started from and having to tighten it.

#4 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 5 lines · Shawn O. Pearce
Quoted 1 line · Junio C Hamano

Why? Are you allowing "1024 k"? Do we want to?

Yes. Why not?

Stricter input checking. Allowing extra things later is far easier than later finding problems with a looser way we started from and having to tighten it.

Good point. I'm currently writing tests for this btw.

Signature · 1 line
Shawn.
#5 lore raw
Quoted 7 lines · Shawn O. Pearce
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Shawn O. Pearce
Quoted 1 line · Junio C Hamano

Why? Are you allowing "1024 k"? Do we want to?

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Yes. Why not?

Stricter input checking. Allowing extra things later is far easier than later finding problems with a looser way we started from and having to tighten it.

Good point. I'm currently writing tests for this btw.

I have this already in the private 'master' I am prepareing for pushout:

t/t1300-repo-config.sh +10 -0
index e48a4ec..a29caa0 100755
@@ -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
 
Signature · 1 line
1.5.0.rc0.g81760
#6 lore raw
Quoted 2 lines · Junio C Hamano

I have this already in the private 'master' I am prepareing for pushout:

Does it pass? I can't get something similar to work here... I must be a moron or something.

Signature · 1 line
Shawn.
#7 lore raw
In reply to Junio C Hamano

t/t1300-repo-config.sh EOF · line 403

Quoted 10 lines · Junio C Hamano
t/t1300-repo-config.sh +10 -0
+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"
+'
+

I'm a moron. I copied and pasted test_expect_failure here in my own version. Commit and push yours, its just as good as mine, but is shorter. :-)

Signature · 1 line
Shawn.