diff options
author | Vicent Martà <vicent@github.com> | 2013-03-19 03:17:14 +0400 |
---|---|---|
committer | Vicent Martà <vicent@github.com> | 2013-03-19 03:17:14 +0400 |
commit | 5b27bf7e5bfd5c2f92a15c0058c801d49faf8403 (patch) | |
tree | eb4e79c950ec3260ad8a532b4d84dae2ad60ae5d /src/config.c | |
parent | 50eb8520d06ac0c484a0ce3a3fba6c5da25ec976 (diff) | |
parent | 324602514fec5ba04fa236c67d633f9b18ad9845 (diff) |
Merge pull request #1417 from arrbee/opts-for-paths
Implement opts interface for global/system file search paths
Diffstat (limited to 'src/config.c')
-rw-r--r-- | src/config.c | 88 |
1 files changed, 29 insertions, 59 deletions
diff --git a/src/config.c b/src/config.c index 4439a68a0..5379b0ec5 100644 --- a/src/config.c +++ b/src/config.c @@ -515,62 +515,48 @@ int git_config_set_multivar(git_config *cfg, const char *name, const char *regex return file->set_multivar(file, name, regexp, value); } -int git_config_find_global_r(git_buf *path) +static int git_config__find_file_to_path( + char *out, size_t outlen, int (*find)(git_buf *buf)) { - int error = git_futils_find_global_file(path, GIT_CONFIG_FILENAME); + int error = 0; + git_buf path = GIT_BUF_INIT; + + if ((error = find(&path)) < 0) + goto done; + + if (path.size >= outlen) { + giterr_set(GITERR_NOMEMORY, "Buffer is too short for the path"); + error = GIT_EBUFS; + goto done; + } + git_buf_copy_cstr(out, outlen, &path); + +done: + git_buf_free(&path); return error; } -int git_config_find_xdg_r(git_buf *path) +int git_config_find_global_r(git_buf *path) { - int error = git_futils_find_global_file(path, GIT_CONFIG_FILENAME_ALT); - - return error; + return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL); } int git_config_find_global(char *global_config_path, size_t length) { - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_global_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } + return git_config__find_file_to_path( + global_config_path, length, git_config_find_global_r); +} - git_buf_copy_cstr(global_config_path, length, &path); - git_buf_free(&path); - return 0; +int git_config_find_xdg_r(git_buf *path) +{ + return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG); } int git_config_find_xdg(char *xdg_config_path, size_t length) { - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_xdg_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } - - git_buf_copy_cstr(xdg_config_path, length, &path); - git_buf_free(&path); - return 0; + return git_config__find_file_to_path( + xdg_config_path, length, git_config_find_xdg_r); } int git_config_find_system_r(git_buf *path) @@ -580,24 +566,8 @@ int git_config_find_system_r(git_buf *path) int git_config_find_system(char *system_config_path, size_t length) { - git_buf path = GIT_BUF_INIT; - int ret = git_config_find_system_r(&path); - - if (ret < 0) { - git_buf_free(&path); - return ret; - } - - if (path.size >= length) { - git_buf_free(&path); - giterr_set(GITERR_NOMEMORY, - "Path is to long to fit on the given buffer"); - return -1; - } - - git_buf_copy_cstr(system_config_path, length, &path); - git_buf_free(&path); - return 0; + return git_config__find_file_to_path( + system_config_path, length, git_config_find_system_r); } int git_config_open_default(git_config **out) |