Skip to content

Commit f57cc63

Browse files
author
Vicent Marti
committed
Merge pull request libgit2#2200 from libgit2/cmn/opts-buf
settings: use git_buf for returning strings
2 parents 0deb534 + 6057c4a commit f57cc63

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

include/git2/common.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ typedef enum {
161161
* >Set the maximum amount of memory that can be mapped at any time
162162
* by the library
163163
*
164-
* * opts(GIT_OPT_GET_SEARCH_PATH, int level, char *out, size_t len)
164+
* * opts(GIT_OPT_GET_SEARCH_PATH, int level, git_buf *buf)
165165
*
166166
* > Get the search path for a given level of config data. "level" must
167167
* > be one of `GIT_CONFIG_LEVEL_SYSTEM`, `GIT_CONFIG_LEVEL_GLOBAL`, or
168168
* > `GIT_CONFIG_LEVEL_XDG`. The search path is written to the `out`
169-
* > buffer up to size `len`. Returns GIT_EBUFS if buffer is too small.
169+
* > buffer.
170170
*
171171
* * opts(GIT_OPT_SET_SEARCH_PATH, int level, const char *path)
172172
*
@@ -195,7 +195,7 @@ typedef enum {
195195
* > across all repositories before libgit2 starts evicting objects
196196
* > from the cache. This is a soft limit, in that the library might
197197
* > briefly exceed it, but will start aggressively evicting objects
198-
* > from cache when that happens. The default cache size is 256Mb.
198+
* > from cache when that happens. The default cache size is 256MB.
199199
*
200200
* * opts(GIT_OPT_ENABLE_CACHING, int enabled)
201201
*
@@ -210,11 +210,10 @@ typedef enum {
210210
* > Get the current bytes in cache and the maximum that would be
211211
* > allowed in the cache.
212212
*
213-
* * opts(GIT_OPT_GET_TEMPLATE_PATH, char *out, size_t len)
213+
* * opts(GIT_OPT_GET_TEMPLATE_PATH, git_buf *out)
214214
*
215215
* > Get the default template path.
216-
* > The path is written to the `out`
217-
* > buffer up to size `len`. Returns GIT_EBUFS if buffer is too small.
216+
* > The path is written to the `out` buffer.
218217
*
219218
* * opts(GIT_OPT_SET_TEMPLATE_PATH, const char *path)
220219
*

src/settings.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,14 @@ int git_libgit2_opts(int key, ...)
7878

7979
case GIT_OPT_GET_SEARCH_PATH:
8080
if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0) {
81-
char *out = va_arg(ap, char *);
82-
size_t outlen = va_arg(ap, size_t);
81+
git_buf *out = va_arg(ap, git_buf *);
82+
const git_buf *tmp;
8383

84-
error = git_sysdir_get_str(out, outlen, error);
84+
git_buf_sanitize(out);
85+
if ((error = git_sysdir_get(&tmp, error)) < 0)
86+
break;
87+
88+
error = git_buf_sets(out, tmp->ptr);
8589
}
8690
break;
8791

@@ -113,10 +117,14 @@ int git_libgit2_opts(int key, ...)
113117

114118
case GIT_OPT_GET_TEMPLATE_PATH:
115119
{
116-
char *out = va_arg(ap, char *);
117-
size_t outlen = va_arg(ap, size_t);
120+
git_buf *out = va_arg(ap, git_buf *);
121+
const git_buf *tmp;
122+
123+
git_buf_sanitize(out);
124+
if ((error = git_sysdir_get(&tmp, GIT_SYSDIR_TEMPLATE)) < 0)
125+
break;
118126

119-
error = git_sysdir_get_str(out, outlen, GIT_SYSDIR_TEMPLATE);
127+
error = git_buf_sets(out, tmp->ptr);
120128
}
121129
break;
122130

tests/core/env.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ void test_core_env__1(void)
218218
static void check_global_searchpath(
219219
const char *path, int position, const char *file, git_buf *temp)
220220
{
221-
char out[GIT_PATH_MAX];
221+
git_buf out = GIT_BUF_INIT;
222222

223223
/* build and set new path */
224224
if (position < 0)
@@ -233,12 +233,12 @@ static void check_global_searchpath(
233233

234234
/* get path and make sure $PATH expansion worked */
235235
cl_git_pass(git_libgit2_opts(
236-
GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out)));
236+
GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
237237

238238
if (position < 0)
239-
cl_assert(git__prefixcmp(out, path) == 0);
239+
cl_assert(git__prefixcmp(out.ptr, path) == 0);
240240
else if (position > 0)
241-
cl_assert(git__suffixcmp(out, path) == 0);
241+
cl_assert(git__suffixcmp(out.ptr, path) == 0);
242242
else
243243
cl_assert_equal_s(out, path);
244244

@@ -250,6 +250,8 @@ static void check_global_searchpath(
250250
GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
251251
cl_assert_equal_i(
252252
GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
253+
254+
git_buf_free(&out);
253255
}
254256

255257
void test_core_env__2(void)

0 commit comments

Comments
 (0)