git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Shawn O. Pearce <spearce@spearce.org>
Date
Dec 31, 2006, 03:13 UTC
Message-ID
<20061231031305.GA5620@spearce.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
Next: Junio C Hamano
Message 1 of 8 in “Teach Git how to parse standard power of 2 suffixes.”
  1. 1/2 Teach Git how to parse standard power of 2 suffixes.Shawn O. Pearce, Dec 31, 2006
  2. Junio C HamanoDec 31, 2006
  3. Shawn O. PearceDec 31, 2006
  4. Junio C HamanoDec 31, 2006
  5. Shawn O. PearceDec 31, 2006
  6. Junio C HamanoDec 31, 2006
  7. Shawn O. PearceDec 31, 2006
  8. Shawn O. PearceDec 31, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.