]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: add a repo_config_get_uint() helper
authorAdrian Ratiu <adrian.ratiu@collabora.com>
Fri, 10 Apr 2026 09:05:57 +0000 (12:05 +0300)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Apr 2026 14:58:53 +0000 (07:58 -0700)
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>
config.c
config.h
parse.c
parse.h

index 156f2a24fa00271c6d6adafac16527e674f5ccd4..a1b92fe083cf435b1326529463c6a85de342886d 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1212,6 +1212,15 @@ int git_config_int(const char *name, const char *value,
        return ret;
 }
 
+unsigned int git_config_uint(const char *name, const char *value,
+                            const struct key_value_info *kvi)
+{
+       unsigned int ret;
+       if (!git_parse_uint(value, &ret))
+               die_bad_number(name, value, kvi);
+       return ret;
+}
+
 int64_t git_config_int64(const char *name, const char *value,
                         const struct key_value_info *kvi)
 {
@@ -1907,6 +1916,18 @@ int git_configset_get_int(struct config_set *set, const char *key, int *dest)
                return 1;
 }
 
+int git_configset_get_uint(struct config_set *set, const char *key, unsigned int *dest)
+{
+       const char *value;
+       struct key_value_info kvi;
+
+       if (!git_configset_get_value(set, key, &value, &kvi)) {
+               *dest = git_config_uint(key, value, &kvi);
+               return 0;
+       } else
+               return 1;
+}
+
 int git_configset_get_ulong(struct config_set *set, const char *key, unsigned long *dest)
 {
        const char *value;
@@ -2356,6 +2377,13 @@ int repo_config_get_int(struct repository *repo,
        return git_configset_get_int(repo->config, key, dest);
 }
 
+int repo_config_get_uint(struct repository *repo,
+                        const char *key, unsigned int *dest)
+{
+       git_config_check_init(repo);
+       return git_configset_get_uint(repo->config, key, dest);
+}
+
 int repo_config_get_ulong(struct repository *repo,
                          const char *key, unsigned long *dest)
 {
index ba426a960af9f4c10ea7102cea34384920270cb8..bf47fb3afc61bfecc0b76a165030ba89db658133 100644 (file)
--- a/config.h
+++ b/config.h
@@ -267,6 +267,12 @@ int git_config_int(const char *, const char *, const struct key_value_info *);
 int64_t git_config_int64(const char *, const char *,
                         const struct key_value_info *);
 
+/**
+ * Identical to `git_config_int`, but for unsigned ints.
+ */
+unsigned int git_config_uint(const char *, const char *,
+                            const struct key_value_info *);
+
 /**
  * Identical to `git_config_int`, but for unsigned longs.
  */
@@ -560,6 +566,7 @@ int git_configset_get_value(struct config_set *cs, const char *key,
 
 int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
+int git_configset_get_uint(struct config_set *cs, const char *key, unsigned int *dest);
 int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
 int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
 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,
  */
 int repo_config_get_int(struct repository *r, const char *key, int *dest);
 
+/**
+ * Similar to `repo_config_get_int` but for unsigned ints.
+ */
+int repo_config_get_uint(struct repository *r,
+                        const char *key, unsigned int *dest);
+
 /**
  * Similar to `repo_config_get_int` but for unsigned longs.
  */
diff --git a/parse.c b/parse.c
index 48313571aab1290924fe2b0019bbeba012898e98..d77f28046a0916de60e2539d472659b14d236002 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -107,6 +107,15 @@ int git_parse_int64(const char *value, int64_t *ret)
        return 1;
 }
 
+int git_parse_uint(const char *value, unsigned int *ret)
+{
+       uintmax_t tmp;
+       if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(unsigned int)))
+               return 0;
+       *ret = tmp;
+       return 1;
+}
+
 int git_parse_ulong(const char *value, unsigned long *ret)
 {
        uintmax_t tmp;
diff --git a/parse.h b/parse.h
index ea32de9a91fbfbc3cbe4894937852e398e24dde6..a6dd37c4cba273bce528c1b82604cb8369476ce9 100644 (file)
--- a/parse.h
+++ b/parse.h
@@ -5,6 +5,7 @@ int git_parse_signed(const char *value, intmax_t *ret, intmax_t max);
 int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max);
 int git_parse_ssize_t(const char *, ssize_t *);
 int git_parse_ulong(const char *, unsigned long *);
+int git_parse_uint(const char *value, unsigned int *ret);
 int git_parse_int(const char *value, int *ret);
 int git_parse_int64(const char *value, int64_t *ret);
 int git_parse_double(const char *value, double *ret);