Commit 42dee8ecd78a10c91056ac80fdf0c67a4df57337

Carlos Martín Nieto 2014-03-23T13:34:33

settings: use git_buf for returning strings This survived the last round of culling, as the signature is only in the comments.

diff --git a/include/git2/common.h b/include/git2/common.h
index 1dca8e8..a357d98 100644
--- a/include/git2/common.h
+++ b/include/git2/common.h
@@ -161,12 +161,12 @@ typedef enum {
  *		>Set the maximum amount of memory that can be mapped at any time
  *		by the library
  *
- *	* opts(GIT_OPT_GET_SEARCH_PATH, int level, char *out, size_t len)
+ *	* opts(GIT_OPT_GET_SEARCH_PATH, int level, git_buf *buf)
  *
  *		> Get the search path for a given level of config data.  "level" must
  *		> be one of `GIT_CONFIG_LEVEL_SYSTEM`, `GIT_CONFIG_LEVEL_GLOBAL`, or
  *		> `GIT_CONFIG_LEVEL_XDG`.  The search path is written to the `out`
- *		> buffer up to size `len`.  Returns GIT_EBUFS if buffer is too small.
+ *		> buffer.
  *
  *	* opts(GIT_OPT_SET_SEARCH_PATH, int level, const char *path)
  *
@@ -210,11 +210,10 @@ typedef enum {
  *		> Get the current bytes in cache and the maximum that would be
  *		> allowed in the cache.
  *
- *	* opts(GIT_OPT_GET_TEMPLATE_PATH, char *out, size_t len)
+ *	* opts(GIT_OPT_GET_TEMPLATE_PATH, git_buf *out)
  *
  *		> Get the default template path.
- *		> The path is written to the `out`
- *		> buffer up to size `len`.  Returns GIT_EBUFS if buffer is too small.
+ *		> The path is written to the `out` buffer.
  *
  *	* opts(GIT_OPT_SET_TEMPLATE_PATH, const char *path)
  *
diff --git a/src/settings.c b/src/settings.c
index 9308f94..1a21ea0 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -78,10 +78,14 @@ int git_libgit2_opts(int key, ...)
 
 	case GIT_OPT_GET_SEARCH_PATH:
 		if ((error = config_level_to_sysdir(va_arg(ap, int))) >= 0) {
-			char *out = va_arg(ap, char *);
-			size_t outlen = va_arg(ap, size_t);
+			git_buf *out = va_arg(ap, git_buf *);
+			const git_buf *tmp;
 
-			error = git_sysdir_get_str(out, outlen, error);
+			git_buf_sanitize(out);
+			if ((error = git_sysdir_get(&tmp, error)) < 0)
+				break;
+
+			error = git_buf_sets(out, tmp->ptr);
 		}
 		break;
 
@@ -113,10 +117,14 @@ int git_libgit2_opts(int key, ...)
 
 	case GIT_OPT_GET_TEMPLATE_PATH:
 		{
-			char *out = va_arg(ap, char *);
-			size_t outlen = va_arg(ap, size_t);
+			git_buf *out = va_arg(ap, git_buf *);
+			const git_buf *tmp;
+
+			git_buf_sanitize(out);
+			if ((error = git_sysdir_get(&tmp, GIT_SYSDIR_TEMPLATE)) < 0)
+				break;
 
-			error = git_sysdir_get_str(out, outlen, GIT_SYSDIR_TEMPLATE);
+			error = git_buf_sets(out, tmp->ptr);
 		}
 		break;
 
diff --git a/tests/core/env.c b/tests/core/env.c
index a32f5ed..4383d96 100644
--- a/tests/core/env.c
+++ b/tests/core/env.c
@@ -218,7 +218,7 @@ void test_core_env__1(void)
 static void check_global_searchpath(
 	const char *path, int position, const char *file, git_buf *temp)
 {
-	char out[GIT_PATH_MAX];
+	git_buf out = GIT_BUF_INIT;
 
 	/* build and set new path */
 	if (position < 0)
@@ -233,12 +233,12 @@ static void check_global_searchpath(
 
 	/* get path and make sure $PATH expansion worked */
 	cl_git_pass(git_libgit2_opts(
-		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, out, sizeof(out)));
+		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
 
 	if (position < 0)
-		cl_assert(git__prefixcmp(out, path) == 0);
+		cl_assert(git__prefixcmp(out.ptr, path) == 0);
 	else if (position > 0)
-		cl_assert(git__suffixcmp(out, path) == 0);
+		cl_assert(git__suffixcmp(out.ptr, path) == 0);
 	else
 		cl_assert_equal_s(out, path);
 
@@ -250,6 +250,8 @@ static void check_global_searchpath(
 		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
 	cl_assert_equal_i(
 		GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
+
+	git_buf_free(&out);
 }
 
 void test_core_env__2(void)