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

[PATCH 2/2] teach git-config to output large integers

From
Jeff King <peff@peff.net>
Date
Aug 20, 2013, 22:47 UTC
Message-ID
<20130820224710.GB24766@sigill.intra.peff.net>
In-Reply-To
<20130820223953.GA3429@sigill.intra.peff.net>

Internally we use "unsigned long" to represent large config values like packed window memory sizes, packfile size limits, etc. On 32-bit systems, this limits these config options to 4G (and we detect and complain about overflow). On 64-bit systems, they get the full 64-bit range.

However, there is no way for script callers of git-config to see the numeric values (they cannot treat them as pure strings, since they would want git-config to process "3g" into "3221225472").

This patch teaches git-config a "--ulong" type that can output these unsigned long values, allowing scripts to examine the values as the internal code would.

Signed-off-by: Jeff King <peff@peff.net>
---
I kind of hate the name "--ulong". I wanted to call it "--size" or
something and abstract away the actual platform representation, and just
make it "big enough for file sizes". But internally, the git options
for dealing with such things use "unsigned long". We could change that
(we are limiting sizes to 4GB on 32-bit systems, while most modern
32-bit systems can handle >4GB files), but I don't know if anybody
actually cares, and it would be a lot of error-prone work (we have to
change the type to off_t or similar, but then try to handle all of the
fallout throughout the code where we use "unsigned long").

So rather than try for something abstract, I tried to keep it to what git does internally, which works pretty well in practice. As with --int, you'll get an error if you try to use too large an integer.

 Documentation/git-config.txt | 6 ++++++
 builtin/config.c             | 8 ++++++++
 t/t1300-repo-config.sh       | 7 +++++++
 3 files changed, 21 insertions(+)
diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index 2dbe486..a8b6626 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -165,6 +165,12 @@ See also <<FILES>>.
 	'git config' will ensure that the output matches the format of
 	either --bool or --int, as described above.
 
+--ulong::
+	As with `--int`, 'git config' will ensure that the output is a
+	simple decimal number, and will respect value suffixes. Unlike
+	`--int`, the range of acceptable integers will be non-negative
+	and go higher (depending on your platform, but at least 2^32-1).
+
 --path::
 	'git-config' will expand leading '{tilde}' to the value of
 	'$HOME', and '{tilde}user' to the home directory for the
diff --git a/builtin/config.c b/builtin/config.c
index 4010c43..f7a46bf 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -47,6 +47,7 @@ static int respect_includes = -1;
 #define TYPE_INT (1<<1)
 #define TYPE_BOOL_OR_INT (1<<2)
 #define TYPE_PATH (1<<3)
+#define TYPE_ULONG (1<<4)
 
 static struct option builtin_config_options[] = {
 	OPT_GROUP(N_("Config file location")),
@@ -73,6 +74,7 @@ static struct option builtin_config_options[] = {
 	OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
 	OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
 	OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
+	OPT_BIT(0, "ulong", &types, N_("value is large unsigned decimal number"), TYPE_ULONG),
 	OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
 	OPT_GROUP(N_("Other")),
 	OPT_BOOLEAN('z', "null", &end_null, N_("terminate values with NUL byte")),
@@ -129,6 +131,8 @@ static int collect_config(const char *key_, const char *value_, void *cb)
 	}
 	if (types == TYPE_INT)
 		sprintf(value, "%d", git_config_int(key_, value_?value_:""));
+	else if (types == TYPE_ULONG)
+		sprintf(value, "%lu", git_config_ulong(key_, value_ ? value_ : ""));
 	else if (types == TYPE_BOOL)
 		vptr = git_config_bool(key_, value_) ? "true" : "false";
 	else if (types == TYPE_BOOL_OR_INT) {
@@ -268,6 +272,10 @@ static char *normalize_value(const char *key, const char *value)
 			int v = git_config_int(key, value);
 			sprintf(normalized, "%d", v);
 		}
+		else if (types == TYPE_ULONG)
+			sprintf(normalized, "%lu",
+				git_config_ulong(key, value));
+
 		else if (types == TYPE_BOOL)
 			sprintf(normalized, "%s",
 				git_config_bool(key, value) ? "true" : "false");
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 3c5ec4b..f57fb27 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -669,6 +669,13 @@ test_expect_success 'invalid unit' '
 	test_cmp actual expect
 '
 
+test_expect_success 'large numbers via --ulong' '
+	git config big.file 3g &&
+	echo 3221225472 >expect &&
+	git config --ulong big.file >actual &&
+	test_cmp expect actual
+'
+
 cat > expect << EOF
 true
 false
-- 
1.8.4.rc2.28.g6bb5f3f
Previous: Jeff KingNext: Jonathan Nieder
Message 7 of 25 in “git-config and large integers”
  1. 0/2 git-config and large integersJeff King, Aug 20, 2013
  2. 1/2 config: properly range-check integer valuesJeff King, Aug 20, 2013
  3. Jonathan NiederAug 20, 2013
  4. Jeff KingAug 21, 2013
  5. Stefan BellerAug 20, 2013
  6. Jeff KingAug 20, 2013
  7. 2/2 teach git-config to output large integersJeff King, Aug 20, 2013
  8. Jonathan NiederAug 20, 2013
  9. Jeff KingAug 21, 2013
  10. Jonathan NiederAug 21, 2013
  11. Jeff KingAug 21, 2013
  12. Jonathan NiederAug 21, 2013
  13. Junio C HamanoAug 20, 2013
  14. Junio C HamanoAug 20, 2013
  15. Jeff KingAug 21, 2013
  16. Jeff KingAug 21, 2013
  17. 0/5 git-config and large integersJeff King, Sep 8, 2013
  18. 1/5 config: factor out integer parsing from range checksJeff King, Sep 8, 2013
  19. 2/5 config: properly range-check integer valuesJeff King, Sep 8, 2013
  20. 3/5 config: set errno in numeric git_parse_* functionsJeff King, Sep 8, 2013
  21. Eric SunshineSep 9, 2013
  22. Jeff KingSep 9, 2013
  23. 4/5 config: make numeric parsing errors more clearJeff King, Sep 8, 2013
  24. 5/5 git-config: always treat --int as 64-bit internallyJeff King, Sep 8, 2013
  25. Junio C HamanoSep 9, 2013

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.