Commit e1d7f0035e8683423271f1f63b57fd9c663e4fb0

Carlos Martín Nieto 2014-01-26T16:32:49

messsage: use git_buf in prettify() A lot of the tests were checking for overflow, which we don't have anymore, so we can remove them.

diff --git a/include/git2/message.h b/include/git2/message.h
index 395c886..bcdb72f 100644
--- a/include/git2/message.h
+++ b/include/git2/message.h
@@ -8,6 +8,7 @@
 #define INCLUDE_git_message_h__
 
 #include "common.h"
+#include "buffer.h"
 
 /**
  * @file git2/message.h
@@ -23,25 +24,17 @@ GIT_BEGIN_DECL
  *
  * Optionally, can remove lines starting with a "#".
  *
- * @param out The user-allocated buffer which will be filled with the
- *     cleaned up message. Pass NULL if you just want to get the needed
- *     size of the prettified message as the output value.
- *
- * @param out_size Size of the `out` buffer in bytes.
+ * @param out The user-allocated git_buf which will be filled with the
+ *     cleaned up message.
  *
  * @param message The message to be prettified.
  *
  * @param strip_comments Non-zero to remove lines starting with "#", 0 to
  *     leave them in.
  *
- * @return -1 on error, else number of characters in prettified message
- *     including the trailing NUL byte
+ * @return 0 or an error code.
  */
-GIT_EXTERN(int) git_message_prettify(
-	char *out,
-	size_t out_size,
-	const char *message,
-	int strip_comments);
+GIT_EXTERN(int) git_message_prettify(git_buf *out, const char *message, int strip_comments);
 
 /** @} */
 GIT_END_DECL
diff --git a/src/message.c b/src/message.c
index 0eff426..07b2569 100644
--- a/src/message.c
+++ b/src/message.c
@@ -21,7 +21,7 @@ static size_t line_length_without_trailing_spaces(const char *line, size_t len)
 
 /* Greatly inspired from git.git "stripspace" */
 /* see https://github.com/git/git/blob/497215d8811ac7b8955693ceaad0899ecd894ed2/builtin/stripspace.c#L4-67 */
-int git_message__prettify(git_buf *message_out, const char *message, int strip_comments)
+int git_message_prettify(git_buf *message_out, const char *message, int strip_comments)
 {
 	const size_t message_len = strlen(message);
 
@@ -29,6 +29,8 @@ int git_message__prettify(git_buf *message_out, const char *message, int strip_c
 	size_t i, line_length, rtrimmed_line_length;
 	char *next_newline;
 
+	git_buf_sanitize(message_out);
+
 	for (i = 0; i < strlen(message); i += line_length) {
 		next_newline = memchr(message + i, '\n', message_len - i);
 
@@ -58,29 +60,3 @@ int git_message__prettify(git_buf *message_out, const char *message, int strip_c
 
 	return git_buf_oom(message_out) ? -1 : 0;
 }
-
-int git_message_prettify(char *message_out, size_t buffer_size, const char *message, int strip_comments)
-{
-	git_buf buf = GIT_BUF_INIT;
-	ssize_t out_size = -1;
-
-	if (message_out && buffer_size)
-		*message_out = '\0';
-
-	if (git_message__prettify(&buf, message, strip_comments) < 0)
-		goto done;
-
-	if (message_out && buf.size + 1 > buffer_size) { /* +1 for NUL byte */
-		giterr_set(GITERR_INVALID, "Buffer too short to hold the cleaned message");
-		goto done;
-	}
-
-	if (message_out)
-		git_buf_copy_cstr(message_out, buffer_size, &buf);
-
-	out_size = buf.size + 1;
-
-done:
-	git_buf_free(&buf);
-	return (int)out_size;
-}
diff --git a/tests/object/commit/commitstagedfile.c b/tests/object/commit/commitstagedfile.c
index 9867ab4..1509277 100644
--- a/tests/object/commit/commitstagedfile.c
+++ b/tests/object/commit/commitstagedfile.c
@@ -25,7 +25,7 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
 	git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid;
 	git_signature *signature;
 	git_tree *tree;
-	char buffer[128];
+	git_buf buffer;
 
 	/*
 	 * The test below replicates the following git scenario
@@ -111,7 +111,8 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
 	cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
 	cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
 
-	cl_assert_equal_i(16, git_message_prettify(buffer, 128, "Initial commit", 0));
+	memset(&buffer, 0, sizeof(git_buf));
+	cl_git_pass(git_message_prettify(&buffer, "Initial commit", 0));
 
 	cl_git_pass(git_commit_create_v(
 		&commit_oid,
@@ -120,12 +121,13 @@ void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
 		signature,
 		signature,
 		NULL,
-		buffer,
+		buffer.ptr,
 		tree,
 		0));
 
 	cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0);
 
+	git_buf_free(&buffer);
 	git_signature_free(signature);
 	git_tree_free(tree);
 	git_index_free(index);
diff --git a/tests/object/message.c b/tests/object/message.c
index 7ef6374..ab55653 100644
--- a/tests/object/message.c
+++ b/tests/object/message.c
@@ -6,7 +6,7 @@ static void assert_message_prettifying(char *expected_output, char *input, int s
 {
 	git_buf prettified_message = GIT_BUF_INIT;
 
-	git_message__prettify(&prettified_message, input, strip_comments);
+	git_message_prettify(&prettified_message, input, strip_comments);
 	cl_assert_equal_s(expected_output, git_buf_cstr(&prettified_message));
 
 	git_buf_free(&prettified_message);
@@ -172,65 +172,28 @@ void test_object_message__keep_comments(void)
 
 void test_object_message__message_prettify(void)
 {
-	char buffer[100];
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 0) == 1);
-	cl_assert_equal_s(buffer, "");
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "", 1) == 1);
-	cl_assert_equal_s(buffer, "");
-
-	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 0));
-	cl_assert_equal_s("Short\n", buffer);
-	cl_assert_equal_i(7, git_message_prettify(buffer, sizeof(buffer), "Short", 1));
-	cl_assert_equal_s("Short\n", buffer);
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 0) > 0);
-	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n# with some comments still in\n");
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer), "This is longer\nAnd multiline\n# with some comments still in\n", 1) > 0);
-	cl_assert_equal_s(buffer, "This is longer\nAnd multiline\n");
-
-	/* try out overflow */
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678",
-		0) > 0);
-	cl_assert_equal_s(buffer,
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n",
-		0) > 0);
-	cl_assert_equal_s(buffer,
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "12345678\n");
-
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "123456789",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "123456789\n",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890",
-		0));
-	cl_git_fail(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890"
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890""x",
-		0));
-
-	cl_assert(git_message_prettify(buffer, sizeof(buffer),
-		"1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
-		"# 1234567890" "1234567890" "1234567890" "1234567890" "1234567890\n"
-		"1234567890",
-		1) > 0);
-
-	cl_assert(git_message_prettify(NULL, 0, "", 0) == 1);
-	cl_assert(git_message_prettify(NULL, 0, "Short test", 0) == 12);
-	cl_assert(git_message_prettify(NULL, 0, "Test\n# with\nComments", 1) == 15);
+	git_buf buffer;
+
+	memset(&buffer, 0, sizeof(buffer));
+	cl_git_pass(git_message_prettify(&buffer, "", 0));
+	cl_assert_equal_s(buffer.ptr, "");
+	git_buf_free(&buffer);
+	cl_git_pass(git_message_prettify(&buffer, "", 1));
+	cl_assert_equal_s(buffer.ptr, "");
+	git_buf_free(&buffer);
+
+	cl_git_pass(git_message_prettify(&buffer, "Short", 0));
+	cl_assert_equal_s("Short\n", buffer.ptr);
+	git_buf_free(&buffer);
+	cl_git_pass(git_message_prettify(&buffer, "Short", 1));
+	cl_assert_equal_s("Short\n", buffer.ptr);
+	git_buf_free(&buffer);
+
+	cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 0));
+	cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n# with some comments still in\n");
+	git_buf_free(&buffer);
+
+	cl_git_pass(git_message_prettify(&buffer, "This is longer\nAnd multiline\n# with some comments still in\n", 1));
+	cl_assert_equal_s(buffer.ptr, "This is longer\nAnd multiline\n");
+	git_buf_free(&buffer);
 }