Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5f96a2

Browse files
srajkoEdward Thomson
authored and
Edward Thomson
committed
Fix git_commit_summary to convert newlines to spaces even after
whitespace. Collapse spaces around newlines for the summary.
1 parent 3ce6cd4 commit f5f96a2

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/commit.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,22 +431,37 @@ const char *git_commit_summary(git_commit *commit)
431431
{
432432
git_buf summary = GIT_BUF_INIT;
433433
const char *msg, *space;
434+
bool space_contains_newline = false;
434435

435436
assert(commit);
436437

437438
if (!commit->summary) {
438439
for (msg = git_commit_message(commit), space = NULL; *msg; ++msg) {
439-
if (msg[0] == '\n' && (!msg[1] || msg[1] == '\n'))
440+
char next_character = msg[0];
441+
/* stop processing at the end of the first paragraph */
442+
if (next_character == '\n' && (!msg[1] || msg[1] == '\n'))
440443
break;
441-
else if (msg[0] == '\n')
442-
git_buf_putc(&summary, ' ');
443-
else if (git__isspace(msg[0]))
444-
space = space ? space : msg;
445-
else if (space) {
446-
git_buf_put(&summary, space, (msg - space) + 1);
447-
space = NULL;
448-
} else
449-
git_buf_putc(&summary, *msg);
444+
/* record the beginning of contiguous whitespace runs */
445+
else if (git__isspace(next_character)) {
446+
if(space == NULL) {
447+
space = msg;
448+
space_contains_newline = false;
449+
}
450+
space_contains_newline |= next_character == '\n';
451+
}
452+
/* the next character is non-space */
453+
else {
454+
/* process any recorded whitespace */
455+
if (space) {
456+
if(space_contains_newline)
457+
git_buf_putc(&summary, ' '); /* if the space contains a newline, collapse to ' ' */
458+
else
459+
git_buf_put(&summary, space, (msg - space)); /* otherwise copy it */
460+
space = NULL;
461+
}
462+
/* copy the next character */
463+
git_buf_putc(&summary, next_character);
464+
}
450465
}
451466

452467
commit->summary = git_buf_detach(&summary);

tests/commit/commit.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,16 @@ void test_commit_commit__summary(void)
7070
assert_commit_summary("Trimmed leading&trailing newlines", "\n\nTrimmed leading&trailing newlines\n\n");
7171
assert_commit_summary("First paragraph only", "\nFirst paragraph only\n\n(There are more!)");
7272
assert_commit_summary("First paragraph with unwrapped trailing\tlines", "\nFirst paragraph\nwith unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
73-
assert_commit_summary("\tLeading \ttabs", "\tLeading\n\ttabs\n\nis preserved");
74-
assert_commit_summary(" Leading Spaces", " Leading\n Spaces\n\nare preserved");
73+
assert_commit_summary("\tLeading tabs", "\tLeading\n\ttabs\n\nare preserved"); /* tabs around newlines are collapsed down to a single space */
74+
assert_commit_summary(" Leading Spaces", " Leading\n Spaces\n\nare preserved"); /* spaces around newlines are collapsed down to a single space */
7575
assert_commit_summary("Trailing tabs\tare removed", "Trailing tabs\tare removed\t\t");
7676
assert_commit_summary("Trailing spaces are removed", "Trailing spaces are removed ");
7777
assert_commit_summary("Trailing tabs", "Trailing tabs\t\n\nare removed");
7878
assert_commit_summary("Trailing spaces", "Trailing spaces \n\nare removed");
79+
assert_commit_summary("Newlines are replaced by spaces", "Newlines\nare\nreplaced by spaces\n");
80+
assert_commit_summary(" Spaces after newlines are collapsed", "\n Spaces after newlines\n are\n collapsed\n "); /* newlines at the very beginning are ignored and not collapsed */
81+
assert_commit_summary(" Spaces before newlines are collapsed", " \nSpaces before newlines \nare \ncollapsed \n");
82+
assert_commit_summary(" Spaces around newlines are collapsed", " \n Spaces around newlines \n are \n collapsed \n ");
7983
assert_commit_summary("", "");
8084
assert_commit_summary("", " ");
8185
assert_commit_summary("", "\n");

tests/commit/write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
88
static const char *root_commit_message = "This is a root commit\n\
99
This is a root commit and should be the only one in this branch\n";
1010
static const char *root_reflog_message = "commit (initial): This is a root commit \
11-
This is a root commit and should be the only one in this branch";
11+
This is a root commit and should be the only one in this branch";
1212
static char *head_old;
1313
static git_reference *head, *branch;
1414
static git_commit *commit;

0 commit comments

Comments
 (0)