Commit 5b09db15d1041032c446f6141a9b41265fb253bc

Vicent Martí 2013-09-30T09:40:15

Merge pull request #1883 from libgit2/ntk/fix/empty_first_commit_line Remove leading newlines from commit message

diff --git a/src/commit.c b/src/commit.c
index 15a195f..ab475a8 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -240,7 +240,7 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
 	buffer_end = buffer + git_odb_object_size(odb_obj);
 
 	buffer += header_len;
-	if (*buffer == '\n')
+	while (buffer < buffer_end && *buffer == '\n')
 		++buffer;
 
 	/* extract commit message */
diff --git a/tests-clar/commit/parse.c b/tests-clar/commit/parse.c
index 415860a..c191b34 100644
--- a/tests-clar/commit/parse.c
+++ b/tests-clar/commit/parse.c
@@ -7,77 +7,77 @@
 static git_repository *g_repo;
 void test_commit_parse__initialize(void)
 {
-   g_repo = cl_git_sandbox_init("testrepo");
+	g_repo = cl_git_sandbox_init("testrepo");
 }
 void test_commit_parse__cleanup(void)
 {
-   cl_git_sandbox_cleanup();
+	cl_git_sandbox_cleanup();
 }
 
 
 // Header parsing
 typedef struct {
-   const char *line;
-   const char *header;
+	const char *line;
+	const char *header;
 } parse_test_case;
 
 static parse_test_case passing_header_cases[] = {
-   { "parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
-   { "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
-   { "random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading " },
-   { "stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading" },
-   { "tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree " },
-   { "tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree " },
-   { "tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree " },
-   { "tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree " },
-   { NULL, NULL }
+	{ "parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
+	{ "tree 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
+	{ "random_heading 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "random_heading " },
+	{ "stuck_heading05452d6349abcd67aa396dfb28660d765d8b2a36\n", "stuck_heading" },
+	{ "tree 5F4BEFFC0759261D015AA63A3A85613FF2F235DE\n", "tree " },
+	{ "tree 1A669B8AB81B5EB7D9DB69562D34952A38A9B504\n", "tree " },
+	{ "tree 5B20DCC6110FCC75D31C6CEDEBD7F43ECA65B503\n", "tree " },
+	{ "tree 173E7BF00EA5C33447E99E6C1255954A13026BE4\n", "tree " },
+	{ NULL, NULL }
 };
 
 static parse_test_case failing_header_cases[] = {
-   { "parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent " },
-   { "05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
-   { "parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent " },
-   { "parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent " },
-   { "tree  05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
-   { "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
-   { "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent " },
-   { "", "tree " },
-   { "", "" },
-   { NULL, NULL }
+	{ "parent 05452d6349abcd67aa396dfb28660d765d8b2a36", "parent " },
+	{ "05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
+	{ "parent05452d6349abcd67aa396dfb28660d765d8b2a6a\n", "parent " },
+	{ "parent 05452d6349abcd67aa396dfb280d765d8b2a6\n", "parent " },
+	{ "tree  05452d6349abcd67aa396dfb28660d765d8b2a36\n", "tree " },
+	{ "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36\n", "parent " },
+	{ "parent 0545xd6349abcd67aa396dfb28660d765d8b2a36FF\n", "parent " },
+	{ "", "tree " },
+	{ "", "" },
+	{ NULL, NULL }
 };
 
 void test_commit_parse__header(void)
 {
-   git_oid oid;
+	git_oid oid;
 
-   parse_test_case *testcase;
-   for (testcase = passing_header_cases; testcase->line != NULL; testcase++)
-   {
-      const char *line = testcase->line;
-      const char *line_end = line + strlen(line);
+	parse_test_case *testcase;
+	for (testcase = passing_header_cases; testcase->line != NULL; testcase++)
+	{
+		const char *line = testcase->line;
+		const char *line_end = line + strlen(line);
 
-      cl_git_pass(git_oid__parse(&oid, &line, line_end, testcase->header));
-      cl_assert(line == line_end);
-   }
+		cl_git_pass(git_oid__parse(&oid, &line, line_end, testcase->header));
+		cl_assert(line == line_end);
+	}
 
-   for (testcase = failing_header_cases; testcase->line != NULL; testcase++)
-   {
-      const char *line = testcase->line;
-      const char *line_end = line + strlen(line);
+	for (testcase = failing_header_cases; testcase->line != NULL; testcase++)
+	{
+		const char *line = testcase->line;
+		const char *line_end = line + strlen(line);
 
-      cl_git_fail(git_oid__parse(&oid, &line, line_end, testcase->header));
-   }
+		cl_git_fail(git_oid__parse(&oid, &line, line_end, testcase->header));
+	}
 }
 
 
 // Signature parsing
 typedef struct {
-   const char *string;
-   const char *header;
-   const char *name;
-   const char *email;
-   git_time_t time;
-   int offset;
+	const char *string;
+	const char *header;
+	const char *name;
+	const char *email;
+	git_time_t time;
+	int offset;
 } passing_signature_test_case;
 
 passing_signature_test_case passing_signature_cases[] = {
@@ -122,12 +122,12 @@ passing_signature_test_case passing_signature_cases[] = {
 	{"author Vicent Marti <tanoku@gmail.com> 4294967296 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 4294967296, 0},
 	{"author Vicent Marti <tanoku@gmail.com> 8589934592 \n", "author ", "Vicent Marti", "tanoku@gmail.com", 8589934592, 0},
 
-   {NULL,NULL,NULL,NULL,0,0}
+	{NULL,NULL,NULL,NULL,0,0}
 };
 
 typedef struct {
-   const char *string;
-   const char *header;
+	const char *string;
+	const char *header;
 } failing_signature_test_case;
 
 failing_signature_test_case failing_signature_cases[] = {
@@ -143,31 +143,31 @@ failing_signature_test_case failing_signature_cases[] = {
 
 void test_commit_parse__signature(void)
 {
-   passing_signature_test_case *passcase;
-   failing_signature_test_case *failcase;
-
-   for (passcase = passing_signature_cases; passcase->string != NULL; passcase++)
-   {
-      const char *str = passcase->string;
-      size_t len = strlen(passcase->string);
-      struct git_signature person = {0};
-
-      cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n'));
-      cl_assert_equal_s(passcase->name, person.name);
-      cl_assert_equal_s(passcase->email, person.email);
-      cl_assert_equal_i((int)passcase->time, (int)person.when.time);
-      cl_assert_equal_i(passcase->offset, person.when.offset);
-      git__free(person.name); git__free(person.email);
-   }
-
-   for (failcase = failing_signature_cases; failcase->string != NULL; failcase++)
-   {
-      const char *str = failcase->string;
-      size_t len = strlen(failcase->string);
-      git_signature person = {0};
-      cl_git_fail(git_signature__parse(&person, &str, str + len, failcase->header, '\n'));
-      git__free(person.name); git__free(person.email);
-   }
+	passing_signature_test_case *passcase;
+	failing_signature_test_case *failcase;
+
+	for (passcase = passing_signature_cases; passcase->string != NULL; passcase++)
+	{
+		const char *str = passcase->string;
+		size_t len = strlen(passcase->string);
+		struct git_signature person = {0};
+
+		cl_git_pass(git_signature__parse(&person, &str, str + len, passcase->header, '\n'));
+		cl_assert_equal_s(passcase->name, person.name);
+		cl_assert_equal_s(passcase->email, person.email);
+		cl_assert_equal_i((int)passcase->time, (int)person.when.time);
+		cl_assert_equal_i(passcase->offset, person.when.offset);
+		git__free(person.name); git__free(person.email);
+	}
+
+	for (failcase = failing_signature_cases; failcase->string != NULL; failcase++)
+	{
+		const char *str = failcase->string;
+		size_t len = strlen(failcase->string);
+		git_signature person = {0};
+		cl_git_fail(git_signature__parse(&person, &str, str + len, failcase->header, '\n'));
+		git__free(person.name); git__free(person.email);
+	}
 }
 
 
@@ -312,17 +312,17 @@ void test_commit_parse__entire_commit(void)
 
 // query the details on a parsed commit
 void test_commit_parse__details0(void) {
-   static const char *commit_ids[] = {
-      "a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
-      "9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
-      "4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
-      "c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
-      "8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
-      "5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
-      "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", /* 6 */
-   };
+	static const char *commit_ids[] = {
+		"a4a7dce85cf63874e984719f4fdd239f5145052f", /* 0 */
+		"9fd738e8f7967c078dceed8190330fc8648ee56a", /* 1 */
+		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045", /* 2 */
+		"c47800c7266a2be04c571c04d5a6614691ea99bd", /* 3 */
+		"8496071c1b46c854b31185ea97743be6a8774479", /* 4 */
+		"5b5b025afb0b4c913b4c338a42934a3863bf3644", /* 5 */
+		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750", /* 6 */
+	};
 	const size_t commit_count = sizeof(commit_ids) / sizeof(const char *);
-   unsigned int i;
+	unsigned int i;
 
 	for (i = 0; i < commit_count; ++i) {
 		git_oid id;
@@ -349,7 +349,6 @@ void test_commit_parse__details0(void) {
 		cl_assert_equal_s("Scott Chacon", committer->name);
 		cl_assert_equal_s("schacon@gmail.com", committer->email);
 		cl_assert(message != NULL);
-		cl_assert(strchr(message, '\n') != NULL);
 		cl_assert(commit_time > 0);
 		cl_assert(parents <= 2);
 		for (p = 0;p < parents;p++) {
@@ -382,9 +381,25 @@ committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
 \n\
 This commit has a few LF at the start of the commit message";
 	const char *message =
-"\n\
+"This commit has a few LF at the start of the commit message";
+
+	cl_git_pass(parse_commit(&commit, buffer));
+	cl_assert_equal_s(message, git_commit_message(commit));
+	git_commit__free(commit);
+}
+
+void test_commit_parse__only_lf(void)
+{
+	git_commit *commit;
+	const char *buffer =
+"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
+parent e90810b8df3e80c413d903f631643c716887138d\n\
+author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
+committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
 \n\
-This commit has a few LF at the start of the commit message";
+\n\
+\n";
+	const char *message = "";
 
 	cl_git_pass(parse_commit(&commit, buffer));
 	cl_assert_equal_s(message, git_commit_message(commit));