Commit f57cc63811db2717e9ba42e720a75f71557f9451

Vicent Marti 2014-03-24T13:52:02

Merge pull request #2200 from libgit2/cmn/opts-buf settings: use git_buf for returning strings

diff --git a/include/git2/common.h b/include/git2/common.h
index 1dca8e8..32237ef 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)
  *
@@ -195,7 +195,7 @@ typedef enum {
  *		> across all repositories before libgit2 starts evicting objects
  *		> from the cache.  This is a soft limit, in that the library might
  *		> briefly exceed it, but will start aggressively evicting objects
- *		> from cache when that happens.  The default cache size is 256Mb.
+ *		> from cache when that happens.  The default cache size is 256MB.
  *
  *	* opts(GIT_OPT_ENABLE_CACHING, int enabled)
  *
@@ -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)