threads / patch / 6166

patch, 2 partsTeach Git how to parse standard power of 2 suffixes.

Subject: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

## tl;dr

8 messages between Dec 31, 2006 and Dec 31, 2006. Diffs are folded; open one to read it.

replies: 7people: 2as markdown or json

Shawn O. Pearce· Dec 31, 2006, 03:13 UTC · lore

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`.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 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(-)
Show changes to 2 files +12 −1

Documentation/git-repo-config.txt, config.c

diff --git a/Documentation/git-repo-config.txt b/Documentation/git-repo-config.txt
index b379ec5..c55a8ba 100644
--- a/Documentation/git-repo-config.txt
+++ b/Documentation/git-repo-config.txt
@@ -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
diff --git a/config.c b/config.c
index 2e0d5a8..83ce9e1 100644
--- a/config.c
+++ b/config.c
@@ -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);
 }
-- 
1.5.0.rc0.g6bb1
Junio C Hamano· Dec 31, 2006, 05:56 UTC · re: Shawn O. Pearce · lore

Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

"Shawn O. Pearce" <spearce@spearce.org> writes:
Show 13 quoted lines
> diff --git a/Documentation/git-repo-config.txt b/Documentation/git-repo-config.txt
> index b379ec5..c55a8ba 100644
> --- a/Documentation/git-repo-config.txt
> +++ b/Documentation/git-repo-config.txt
> @@ -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'.
Show 11 quoted lines
>  ENVIRONMENT
> diff --git a/config.c b/config.c
> index 2e0d5a8..83ce9e1 100644
> --- a/config.c
> +++ b/config.c
> @@ -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?
Show 13 quoted lines
>  		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);
>  }
> -- 
> 1.5.0.rc0.g6bb1
Shawn O. Pearce· Dec 31, 2006, 06:12 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

Junio C Hamano <junkio@cox.net> wrote:
Show 12 quoted lines
> > diff --git a/config.c b/config.c
> > index 2e0d5a8..83ce9e1 100644
> > --- a/config.c
> > +++ b/config.c
> > @@ -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?
-- 
Shawn.
Junio C Hamano· Dec 31, 2006, 06:23 UTC · re: Shawn O. Pearce · lore

Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

"Shawn O. Pearce" <spearce@spearce.org> writes:
Show 5 quoted lines
> Junio C Hamano <junkio@cox.net> wrote:
>
>> 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.

Shawn O. Pearce· Dec 31, 2006, 06:26 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

Junio C Hamano <junkio@cox.net> wrote:
Show 11 quoted lines
> "Shawn O. Pearce" <spearce@spearce.org> writes:
> 
> > Junio C Hamano <junkio@cox.net> wrote:
> >
> >> 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.
-- 
Shawn.
Junio C Hamano· Dec 31, 2006, 06:30 UTC · re: Shawn O. Pearce · lore

Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

"Shawn O. Pearce" <spearce@spearce.org> writes:
Show 14 quoted lines
> Junio C Hamano <junkio@cox.net> wrote:
>> "Shawn O. Pearce" <spearce@spearce.org> writes:
>> 
>> > Junio C Hamano <junkio@cox.net> wrote:
>> >
>> >> 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.

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

Show changes to t/t1300-repo-config.sh +10 −0
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index e48a4ec..a29caa0 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -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
 
-- 
1.5.0.rc0.g81760
Shawn O. Pearce· Dec 31, 2006, 06:36 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

Junio C Hamano <junkio@cox.net> wrote:
> 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.
 
-- 
Shawn.
Shawn O. Pearce· Dec 31, 2006, 06:48 UTC · re: Junio C Hamano · lore

Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.

Junio C Hamano <junkio@cox.net> wrote:
Show 10 quoted lines
> +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. :-)

-- 
Shawn.

← back to recent threads