Skip to content

Commit 1e01508

Browse files
committed
git_commit_summary: ignore lines with spaces
Fixes libgit2#6065
1 parent f9c4dc1 commit 1e01508

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/commit.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ const char *git_commit_message(const git_commit *commit)
546546
const char *git_commit_summary(git_commit *commit)
547547
{
548548
git_str summary = GIT_STR_INIT;
549-
const char *msg, *space;
549+
const char *msg, *space, *next;
550550
bool space_contains_newline = false;
551551

552552
GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL);
@@ -555,10 +555,21 @@ const char *git_commit_summary(git_commit *commit)
555555
for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) {
556556
char next_character = msg[0];
557557
/* stop processing at the end of the first paragraph */
558-
if (next_character == '\n' && (!msg[1] || msg[1] == '\n'))
559-
break;
558+
if (next_character == '\n') {
559+
if (!msg[1])
560+
break;
561+
if (msg[1] == '\n')
562+
break;
563+
/* stop processing if next line contains only whitespace */
564+
next = msg + 1;
565+
while (*next && git__isspace_nonlf(*next)) {
566+
++next;
567+
}
568+
if (!*next || *next == '\n')
569+
break;
570+
}
560571
/* record the beginning of contiguous whitespace runs */
561-
else if (git__isspace(next_character)) {
572+
if (git__isspace(next_character)) {
562573
if(space == NULL) {
563574
space = msg;
564575
space_contains_newline = false;

tests/commit/commit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,11 @@ void test_commit_commit__summary(void)
139139
{
140140
assert_commit_summary("One-liner with no trailing newline", "One-liner with no trailing newline");
141141
assert_commit_summary("One-liner with trailing newline", "One-liner with trailing newline\n");
142+
assert_commit_summary("One-liner with trailing newline and space", "One-liner with trailing newline and space\n ");
142143
assert_commit_summary("Trimmed leading&trailing newlines", "\n\nTrimmed leading&trailing newlines\n\n");
143144
assert_commit_summary("First paragraph only", "\nFirst paragraph only\n\n(There are more!)");
145+
assert_commit_summary("First paragraph only with space in the next line", "\nFirst paragraph only with space in the next line\n \n(There are more!)");
146+
assert_commit_summary("First paragraph only with spaces in the next line", "\nFirst paragraph only with spaces in the next line\n \n(There are more!)");
144147
assert_commit_summary("First paragraph with unwrapped trailing\tlines", "\nFirst paragraph\nwith unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
145148
assert_commit_summary("\tLeading tabs", "\tLeading\n\ttabs\n\nare preserved"); /* tabs around newlines are collapsed down to a single space */
146149
assert_commit_summary(" Leading Spaces", " Leading\n Spaces\n\nare preserved"); /* spaces around newlines are collapsed down to a single space */

0 commit comments

Comments
 (0)