Skip to content

Commit d27a441

Browse files
committed
commit: Trim message leading newlines
Fix libgit2/libgit2sharp#522
1 parent 27c8eb2 commit d27a441

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
240240
buffer_end = buffer + git_odb_object_size(odb_obj);
241241

242242
buffer += header_len;
243-
if (*buffer == '\n')
243+
while (buffer < buffer_end && *buffer == '\n')
244244
++buffer;
245245

246246
/* extract commit message */

tests-clar/commit/parse.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ void test_commit_parse__details0(void) {
349349
cl_assert_equal_s("Scott Chacon", committer->name);
350350
cl_assert_equal_s("schacon@gmail.com", committer->email);
351351
cl_assert(message != NULL);
352-
cl_assert(strchr(message, '\n') != NULL);
353352
cl_assert(commit_time > 0);
354353
cl_assert(parents <= 2);
355354
for (p = 0;p < parents;p++) {
@@ -382,9 +381,25 @@ committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
382381
\n\
383382
This commit has a few LF at the start of the commit message";
384383
const char *message =
385-
"\n\
384+
"This commit has a few LF at the start of the commit message";
385+
386+
cl_git_pass(parse_commit(&commit, buffer));
387+
cl_assert_equal_s(message, git_commit_message(commit));
388+
git_commit__free(commit);
389+
}
390+
391+
void test_commit_parse__only_lf(void)
392+
{
393+
git_commit *commit;
394+
const char *buffer =
395+
"tree 1810dff58d8a660512d4832e740f692884338ccd\n\
396+
parent e90810b8df3e80c413d903f631643c716887138d\n\
397+
author Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
398+
committer Vicent Marti <tanoku@gmail.com> 1273848544 +0200\n\
386399
\n\
387-
This commit has a few LF at the start of the commit message";
400+
\n\
401+
\n";
402+
const char *message = "";
388403

389404
cl_git_pass(parse_commit(&commit, buffer));
390405
cl_assert_equal_s(message, git_commit_message(commit));

0 commit comments

Comments
 (0)