Commit ee550477 by Carlos Martín Nieto

config: use git_buf for returning paths

Again, we already did this internally, so simply remove the conversions.
parent b25d87c9
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "common.h" #include "common.h"
#include "types.h" #include "types.h"
#include "buffer.h"
/** /**
* @file git2/config.h * @file git2/config.h
...@@ -90,11 +91,10 @@ typedef struct { ...@@ -90,11 +91,10 @@ typedef struct {
* This method will not guess the path to the xdg compatible * This method will not guess the path to the xdg compatible
* config file (.config/git/config). * config file (.config/git/config).
* *
* @param out Buffer to store the path in * @param out Pointer to a user-allocated git_buf in which to store the path
* @param length size of the buffer in bytes * @return 0 if a global configuration file has been found. Its path will be stored in `out`.
* @return 0 if a global configuration file has been found. Its path will be stored in `buffer`.
*/ */
GIT_EXTERN(int) git_config_find_global(char *out, size_t length); GIT_EXTERN(int) git_config_find_global(git_buf *out);
/** /**
* Locate the path to the global xdg compatible configuration file * Locate the path to the global xdg compatible configuration file
...@@ -107,25 +107,23 @@ GIT_EXTERN(int) git_config_find_global(char *out, size_t length); ...@@ -107,25 +107,23 @@ GIT_EXTERN(int) git_config_find_global(char *out, size_t length);
* may be used on any `git_config` call to load the * may be used on any `git_config` call to load the
* xdg compatible configuration file. * xdg compatible configuration file.
* *
* @param out Buffer to store the path in * @param out Pointer to a user-allocated git_buf in which to store the path
* @param length size of the buffer in bytes
* @return 0 if a xdg compatible configuration file has been * @return 0 if a xdg compatible configuration file has been
* found. Its path will be stored in `buffer`. * found. Its path will be stored in `out`.
*/ */
GIT_EXTERN(int) git_config_find_xdg(char *out, size_t length); GIT_EXTERN(int) git_config_find_xdg(git_buf *out);
/** /**
* Locate the path to the system configuration file * Locate the path to the system configuration file
* *
* If /etc/gitconfig doesn't exist, it will look for * If /etc/gitconfig doesn't exist, it will look for
* %PROGRAMFILES%\Git\etc\gitconfig. * %PROGRAMFILES%\Git\etc\gitconfig.
*
* @param out Buffer to store the path in * @param out Pointer to a user-allocated git_buf in which to store the path
* @param length size of the buffer in bytes
* @return 0 if a system configuration file has been * @return 0 if a system configuration file has been
* found. Its path will be stored in `buffer`. * found. Its path will be stored in `out`.
*/ */
GIT_EXTERN(int) git_config_find_system(char *out, size_t length); GIT_EXTERN(int) git_config_find_system(git_buf *out);
/** /**
* Open the global, XDG and system configuration files * Open the global, XDG and system configuration files
......
...@@ -935,61 +935,21 @@ void git_config_iterator_free(git_config_iterator *iter) ...@@ -935,61 +935,21 @@ void git_config_iterator_free(git_config_iterator *iter)
iter->free(iter); iter->free(iter);
} }
static int git_config__find_file_to_path( int git_config_find_global(git_buf *path)
char *out, size_t outlen, int (*find)(git_buf *buf))
{
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_global_r(git_buf *path)
{ {
return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL); return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
} }
int git_config_find_global(char *global_config_path, size_t length) int git_config_find_xdg(git_buf *path)
{
return git_config__find_file_to_path(
global_config_path, length, git_config_find_global_r);
}
int git_config_find_xdg_r(git_buf *path)
{ {
return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG); return git_futils_find_xdg_file(path, GIT_CONFIG_FILENAME_XDG);
} }
int git_config_find_xdg(char *xdg_config_path, size_t length) int git_config_find_system(git_buf *path)
{
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)
{ {
return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM); return git_futils_find_system_file(path, GIT_CONFIG_FILENAME_SYSTEM);
} }
int git_config_find_system(char *system_config_path, size_t length)
{
return git_config__find_file_to_path(
system_config_path, length, git_config_find_system_r);
}
int git_config__global_location(git_buf *buf) int git_config__global_location(git_buf *buf)
{ {
const git_buf *paths; const git_buf *paths;
...@@ -1026,16 +986,16 @@ int git_config_open_default(git_config **out) ...@@ -1026,16 +986,16 @@ int git_config_open_default(git_config **out)
if ((error = git_config_new(&cfg)) < 0) if ((error = git_config_new(&cfg)) < 0)
return error; return error;
if (!git_config_find_global_r(&buf) || !git_config__global_location(&buf)) { if (!git_config_find_global(&buf) || !git_config__global_location(&buf)) {
error = git_config_add_file_ondisk(cfg, buf.ptr, error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_GLOBAL, 0); GIT_CONFIG_LEVEL_GLOBAL, 0);
} }
if (!error && !git_config_find_xdg_r(&buf)) if (!error && !git_config_find_xdg(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr, error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_XDG, 0); GIT_CONFIG_LEVEL_XDG, 0);
if (!error && !git_config_find_system_r(&buf)) if (!error && !git_config_find_system(&buf))
error = git_config_add_file_ondisk(cfg, buf.ptr, error = git_config_add_file_ondisk(cfg, buf.ptr,
GIT_CONFIG_LEVEL_SYSTEM, 0); GIT_CONFIG_LEVEL_SYSTEM, 0);
......
...@@ -24,11 +24,6 @@ struct git_config { ...@@ -24,11 +24,6 @@ struct git_config {
git_vector files; git_vector files;
}; };
extern int git_config_find_global_r(git_buf *global_config_path);
extern int git_config_find_xdg_r(git_buf *system_config_path);
extern int git_config_find_system_r(git_buf *system_config_path);
extern int git_config__global_location(git_buf *buf); extern int git_config__global_location(git_buf *buf);
extern int git_config_rename_section( extern int git_config_rename_section(
......
...@@ -582,9 +582,9 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo) ...@@ -582,9 +582,9 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
git_buf system_buf = GIT_BUF_INIT; git_buf system_buf = GIT_BUF_INIT;
git_config *config; git_config *config;
git_config_find_global_r(&global_buf); git_config_find_global(&global_buf);
git_config_find_xdg_r(&xdg_buf); git_config_find_xdg(&xdg_buf);
git_config_find_system_r(&system_buf); git_config_find_system(&system_buf);
/* If there is no global file, open a backend for it anyway */ /* If there is no global file, open a backend for it anyway */
if (git_buf_len(&global_buf) == 0) if (git_buf_len(&global_buf) == 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment