config: Cleanup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
diff --git a/src/config.c b/src/config.c
index 061eba6..2ee49e0 100644
--- a/src/config.c
+++ b/src/config.c
@@ -78,7 +78,7 @@ int git_config_open_global(git_config **out)
home = getenv("HOME");
if (home == NULL)
- return git__throw(GIT_EOSERR, "Failed to find $HOME variable");
+ return git__throw(GIT_EOSERR, "Failed to open global config file. Cannot find $HOME variable");
git__joinpath(full_path, home, GIT_CONFIG_FILENAME);
@@ -185,22 +185,9 @@ int git_config_foreach(git_config *cfg, int (*fn)(const char *, void *), void *d
int git_config_set_long(git_config *cfg, const char *name, long int value)
{
- char str_value[5]; /* Most numbers should fit in here */
- int buf_len = sizeof(str_value), ret;
- char *help_buf = NULL;
-
- if ((ret = snprintf(str_value, buf_len, "%ld", value)) >= buf_len - 1){
- /* The number is too large, we need to allocate more memory */
- buf_len = ret + 1;
- help_buf = git__malloc(buf_len);
- snprintf(help_buf, buf_len, "%ld", value);
- ret = git_config_set_string(cfg, name, help_buf);
- free(help_buf);
- } else {
- ret = git_config_set_string(cfg, name, str_value);
- }
-
- return ret;
+ char str_value[32]; /* All numbers should fit in here */
+ snprintf(str_value, sizeof(str_value), "%ld", value);
+ return git_config_set_string(cfg, name, str_value);
}
int git_config_set_int(git_config *cfg, const char *name, int value)
@@ -210,14 +197,7 @@ int git_config_set_int(git_config *cfg, const char *name, int value)
int git_config_set_bool(git_config *cfg, const char *name, int value)
{
- const char *str_value;
-
- if (value == 0)
- str_value = "false";
- else
- str_value = "true";
-
- return git_config_set_string(cfg, name, str_value);
+ return git_config_set_string(cfg, name, value ? "true" : "false");
}
int git_config_set_string(git_config *cfg, const char *name, const char *value)
@@ -246,11 +226,11 @@ int git_config_get_long(git_config *cfg, const char *name, long int *out)
ret = git_config_get_string(cfg, name, &value);
if (ret < GIT_SUCCESS)
- return ret;
+ return git__rethrow(ret, "Failed to get value for %s", name);
ret = git__strtol32(&num, value, &num_end, 0);
if (ret < GIT_SUCCESS)
- return ret;
+ return git__rethrow(ret, "Failed to get value for %s", name);
switch (*num_end) {
case '\0':
@@ -268,7 +248,7 @@ int git_config_get_long(git_config *cfg, const char *name, long int *out)
num *= 1024 * 1024 * 1024;
break;
default:
- return GIT_EINVALIDTYPE;
+ return git__throw(GIT_EINVALIDTYPE, "Failed to get value for %s. Value is of invalid type");
}
*out = num;
@@ -295,7 +275,7 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out)
error = git_config_get_string(cfg, name, &value);
if (error < GIT_SUCCESS)
- return error;
+ return git__rethrow(error, "Failed to get value for %s", name);
/* A missing value means true */
if (value == NULL) {
@@ -321,6 +301,8 @@ int git_config_get_bool(git_config *cfg, const char *name, int *out)
if (error == GIT_SUCCESS)
*out = !!(*out);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to get value for %s", name);
return error;
}