config: use git_buf for returning paths Again, we already did this internally, so simply remove the conversions.
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
diff --git a/include/git2/config.h b/include/git2/config.h
index f650f1b..663b4f6 100644
--- a/include/git2/config.h
+++ b/include/git2/config.h
@@ -9,6 +9,7 @@
#include "common.h"
#include "types.h"
+#include "buffer.h"
/**
* @file git2/config.h
@@ -90,11 +91,10 @@ typedef struct {
* This method will not guess the path to the xdg compatible
* config file (.config/git/config).
*
- * @param out Buffer to store the path in
- * @param length size of the buffer in bytes
- * @return 0 if a global configuration file has been found. Its path will be stored in `buffer`.
+ * @param out Pointer to a user-allocated git_buf in which to store the path
+ * @return 0 if a global configuration file has been found. Its path will be stored in `out`.
*/
-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
@@ -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
* xdg compatible configuration file.
*
- * @param out Buffer to store the path in
- * @param length size of the buffer in bytes
+ * @param out Pointer to a user-allocated git_buf in which to store the path
* @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
*
* If /etc/gitconfig doesn't exist, it will look for
* %PROGRAMFILES%\Git\etc\gitconfig.
-
- * @param out Buffer to store the path in
- * @param length size of the buffer in bytes
+ *
+ * @param out Pointer to a user-allocated git_buf in which to store the path
* @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
diff --git a/src/config.c b/src/config.c
index fa1dd81..6aa7146 100644
--- a/src/config.c
+++ b/src/config.c
@@ -935,61 +935,21 @@ void git_config_iterator_free(git_config_iterator *iter)
iter->free(iter);
}
-static int git_config__find_file_to_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)
+int git_config_find_global(git_buf *path)
{
return git_futils_find_global_file(path, GIT_CONFIG_FILENAME_GLOBAL);
}
-int git_config_find_global(char *global_config_path, size_t length)
-{
- 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)
+int git_config_find_xdg(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)
-{
- 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)
+int git_config_find_system(git_buf *path)
{
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)
{
const git_buf *paths;
@@ -1026,16 +986,16 @@ int git_config_open_default(git_config **out)
if ((error = git_config_new(&cfg)) < 0)
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,
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,
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,
GIT_CONFIG_LEVEL_SYSTEM, 0);
diff --git a/src/config.h b/src/config.h
index 3cd888c..03d9106 100644
--- a/src/config.h
+++ b/src/config.h
@@ -24,11 +24,6 @@ struct git_config {
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_rename_section(
diff --git a/src/repository.c b/src/repository.c
index db66e6b..285d889 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -582,9 +582,9 @@ int git_repository_config__weakptr(git_config **out, git_repository *repo)
git_buf system_buf = GIT_BUF_INIT;
git_config *config;
- git_config_find_global_r(&global_buf);
- git_config_find_xdg_r(&xdg_buf);
- git_config_find_system_r(&system_buf);
+ git_config_find_global(&global_buf);
+ git_config_find_xdg(&xdg_buf);
+ git_config_find_system(&system_buf);
/* If there is no global file, open a backend for it anyway */
if (git_buf_len(&global_buf) == 0)