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

8 messages from 2006-12-31 to 2006-12-31. Participants: Shawn O. Pearce, Junio C Hamano.
Thread: https://gitlist.dev/t/6166

## Shawn O. Pearce, 2006-12-31 03:13

Subject: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <20061231031305.GA5620@spearce.org>
URL: https://gitlist.dev/e/20061231031305.GA5620%40spearce.org

```
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(-)

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, 2006-12-31 05:56

Subject: Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <7vy7ooip6y.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vy7ooip6y.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20061231031305.GA5620@spearce.org>

```
"Shawn O. Pearce" <spearce@spearce.org> writes:

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

>  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?

>  		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, 2006-12-31 06:12

Subject: Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <20061231061232.GC6106@spearce.org>
URL: https://gitlist.dev/e/20061231061232.GC6106%40spearce.org
In-Reply-To: <7vy7ooip6y.fsf@assigned-by-dhcp.cox.net>

```
Junio C Hamano <junkio@cox.net> wrote:
> > 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, 2006-12-31 06:23

Subject: Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <7vpsa0inwr.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vpsa0inwr.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20061231061232.GC6106@spearce.org>

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

```

## Shawn O. Pearce, 2006-12-31 06:26

Subject: Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <20061231062618.GD6106@spearce.org>
URL: https://gitlist.dev/e/20061231062618.GD6106%40spearce.org
In-Reply-To: <7vpsa0inwr.fsf@assigned-by-dhcp.cox.net>

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

-- 
Shawn.

```

## Junio C Hamano, 2006-12-31 06:30

Subject: Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <7vlkkoinla.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vlkkoinla.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <20061231062618.GD6106@spearce.org>

```
"Shawn O. Pearce" <spearce@spearce.org> writes:

> 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:

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, 2006-12-31 06:36

Subject: Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <20061231063625.GE6106@spearce.org>
URL: https://gitlist.dev/e/20061231063625.GE6106%40spearce.org
In-Reply-To: <7vlkkoinla.fsf@assigned-by-dhcp.cox.net>

```
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, 2006-12-31 06:48

Subject: Re: [PATCH 1/2] Teach Git how to parse standard power of 2 suffixes.
Message-ID: <20061231064839.GF6106@spearce.org>
URL: https://gitlist.dev/e/20061231064839.GF6106%40spearce.org
In-Reply-To: <7vlkkoinla.fsf@assigned-by-dhcp.cox.net>

```
Junio C Hamano <junkio@cox.net> wrote:
> +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.

```
