config: add a repo_config_get_uint() helper

Next commits add a 'hook.jobs' config option of type 'unsigned int', so add a helper to parse it since the API only supports int and ulong. An alternative is to make 'hook.jobs' an 'int' or parse it as an 'int' then cast it to unsigned, however it's better to use proper helpers for the type. Using 'ulong' is another option which already has helpers, but it's a bit excessive in size for just the jobs number. Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adrian Ratiu committed Apr 10, 2026 at 12:05 UTC 1c9e5b3fa235e0da6f62359af36afea8e7617074
4 files changed +51
config.c
+28
@@ -1212,6 +1212,15 @@ int git_config_int(const char *name, const char *value,
1212 return ret;
1213 }
1214
1215 +unsigned int git_config_uint(const char *name, const char *value,
1216 + const struct key_value_info *kvi)
1217 +{
1218 + unsigned int ret;
1219 + if (!git_parse_uint(value, &ret))
1220 + die_bad_number(name, value, kvi);
1221 + return ret;
1222 +}
1223 +
1224 int64_t git_config_int64(const char *name, const char *value,
1225 const struct key_value_info *kvi)
1226 {
@@ -1907,6 +1916,18 @@ int git_configset_get_int(struct config_set *set, const char *key, int *dest)
1916 return 1;
1917 }
1918
1919 +int git_configset_get_uint(struct config_set *set, const char *key, unsigned int *dest)
1920 +{
1921 + const char *value;
1922 + struct key_value_info kvi;
1923 +
1924 + if (!git_configset_get_value(set, key, &value, &kvi)) {
1925 + *dest = git_config_uint(key, value, &kvi);
1926 + return 0;
1927 + } else
1928 + return 1;
1929 +}
1930 +
1931 int git_configset_get_ulong(struct config_set *set, const char *key, unsigned long *dest)
1932 {
1933 const char *value;
@@ -2356,6 +2377,13 @@ int repo_config_get_int(struct repository *repo,
2377 return git_configset_get_int(repo->config, key, dest);
2378 }
2379
2380 +int repo_config_get_uint(struct repository *repo,
2381 + const char *key, unsigned int *dest)
2382 +{
2383 + git_config_check_init(repo);
2384 + return git_configset_get_uint(repo->config, key, dest);
2385 +}
2386 +
2387 int repo_config_get_ulong(struct repository *repo,
2388 const char *key, unsigned long *dest)
2389 {
config.h
+13
@@ -267,6 +267,12 @@ int git_config_int(const char *, const char *, const struct key_value_info *);
267 int64_t git_config_int64(const char *, const char *,
268 const struct key_value_info *);
269
270 +/**
271 + * Identical to `git_config_int`, but for unsigned ints.
272 + */
273 +unsigned int git_config_uint(const char *, const char *,
274 + const struct key_value_info *);
275 +
276 /**
277 * Identical to `git_config_int`, but for unsigned longs.
278 */
@@ -560,6 +566,7 @@ int git_configset_get_value(struct config_set *cs, const char *key,
566
567 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
568 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
569 +int git_configset_get_uint(struct config_set *cs, const char *key, unsigned int *dest);
570 int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
571 int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
572 int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
@@ -650,6 +657,12 @@ int repo_config_get_string_tmp(struct repository *r,
657 */
658 int repo_config_get_int(struct repository *r, const char *key, int *dest);
659
660 +/**
661 + * Similar to `repo_config_get_int` but for unsigned ints.
662 + */
663 +int repo_config_get_uint(struct repository *r,
664 + const char *key, unsigned int *dest);
665 +
666 /**
667 * Similar to `repo_config_get_int` but for unsigned longs.
668 */
parse.c
+9
@@ -107,6 +107,15 @@ int git_parse_int64(const char *value, int64_t *ret)
107 return 1;
108 }
109
110 +int git_parse_uint(const char *value, unsigned int *ret)
111 +{
112 + uintmax_t tmp;
113 + if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(unsigned int)))
114 + return 0;
115 + *ret = tmp;
116 + return 1;
117 +}
118 +
119 int git_parse_ulong(const char *value, unsigned long *ret)
120 {
121 uintmax_t tmp;
parse.h
+1
@@ -5,6 +5,7 @@ int git_parse_signed(const char *value, intmax_t *ret, intmax_t max);
5 int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max);
6 int git_parse_ssize_t(const char *, ssize_t *);
7 int git_parse_ulong(const char *, unsigned long *);
8 +int git_parse_uint(const char *value, unsigned int *ret);
9 int git_parse_int(const char *value, int *ret);
10 int git_parse_int64(const char *value, int64_t *ret);
11 int git_parse_double(const char *value, double *ret);