mirror of
https://github.com/git/git.git
synced 2026-02-25 17:33:34 +00:00
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>
This commit is contained in:
committed by
Junio C Hamano
parent
041903fa70
commit
4c40cd5ebb
28
config.c
28
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)
|
||||
{
|
||||
|
||||
13
config.h
13
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.
|
||||
*/
|
||||
|
||||
9
parse.c
9
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;
|
||||
|
||||
1
parse.h
1
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);
|
||||
|
||||
Reference in New Issue
Block a user