Skip to content

Commit 3cca14b

Browse files
authored
Merge pull request libgit2#6125 from stforek/git_commit_summary_spaces
git_commit_summary: ignore lines with spaces
2 parents dca31d2 + 1e01508 commit 3cca14b

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
@@ -547,7 +547,7 @@ const char *git_commit_message(const git_commit *commit)
547547
const char *git_commit_summary(git_commit *commit)
548548
{
549549
git_str summary = GIT_STR_INIT;
550-
const char *msg, *space;
550+
const char *msg, *space, *next;
551551
bool space_contains_newline = false;
552552

553553
GIT_ASSERT_ARG_WITH_RETVAL(commit, NULL);
@@ -556,10 +556,21 @@ const char *git_commit_summary(git_commit *commit)
556556
for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) {
557557
char next_character = msg[0];
558558
/* stop processing at the end of the first paragraph */
559-
if (next_character == '\n' && (!msg[1] || msg[1] == '\n'))
560-
break;
559+
if (next_character == '\n') {
560+
if (!msg[1])
561+
break;
562+
if (msg[1] == '\n')
563+
break;
564+
/* stop processing if next line contains only whitespace */
565+
next = msg + 1;
566+
while (*next && git__isspace_nonlf(*next)) {
567+
++next;
568+
}
569+
if (!*next || *next == '\n')
570+
break;
571+
}
561572
/* record the beginning of contiguous whitespace runs */
562-
else if (git__isspace(next_character)) {
573+
if (git__isspace(next_character)) {
563574
if(space == NULL) {
564575
space = msg;
565576
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)