Skip to content

Commit 9eea518

Browse files
10ne1gitster
authored andcommitted
config: add a repo_config_get_uint() helper
Next commit adds 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>
1 parent e8b6eea commit 9eea518

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

config.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,15 @@ int git_config_int(const char *name, const char *value,
12121212
return ret;
12131213
}
12141214

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+
12151224
int64_t git_config_int64(const char *name, const char *value,
12161225
const struct key_value_info *kvi)
12171226
{
@@ -1907,6 +1916,18 @@ int git_configset_get_int(struct config_set *set, const char *key, int *dest)
19071916
return 1;
19081917
}
19091918

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+
19101931
int git_configset_get_ulong(struct config_set *set, const char *key, unsigned long *dest)
19111932
{
19121933
const char *value;
@@ -2356,6 +2377,13 @@ int repo_config_get_int(struct repository *repo,
23562377
return git_configset_get_int(repo->config, key, dest);
23572378
}
23582379

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+
23592387
int repo_config_get_ulong(struct repository *repo,
23602388
const char *key, unsigned long *dest)
23612389
{

config.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ int git_config_int(const char *, const char *, const struct key_value_info *);
267267
int64_t git_config_int64(const char *, const char *,
268268
const struct key_value_info *);
269269

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+
270276
/**
271277
* Identical to `git_config_int`, but for unsigned longs.
272278
*/
@@ -560,6 +566,7 @@ int git_configset_get_value(struct config_set *cs, const char *key,
560566

561567
int git_configset_get_string(struct config_set *cs, const char *key, char **dest);
562568
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);
563570
int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
564571
int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
565572
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,
650657
*/
651658
int repo_config_get_int(struct repository *r, const char *key, int *dest);
652659

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+
653666
/**
654667
* Similar to `repo_config_get_int` but for unsigned longs.
655668
*/

parse.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ int git_parse_int64(const char *value, int64_t *ret)
107107
return 1;
108108
}
109109

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+
110119
int git_parse_ulong(const char *value, unsigned long *ret)
111120
{
112121
uintmax_t tmp;

parse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ int git_parse_signed(const char *value, intmax_t *ret, intmax_t max);
55
int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max);
66
int git_parse_ssize_t(const char *, ssize_t *);
77
int git_parse_ulong(const char *, unsigned long *);
8+
int git_parse_uint(const char *value, unsigned int *ret);
89
int git_parse_int(const char *value, int *ret);
910
int git_parse_int64(const char *value, int64_t *ret);
1011
int git_parse_double(const char *value, double *ret);

0 commit comments

Comments
 (0)