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

Skip to content

Commit 7f8fe1d

Browse files
committed
commit: introduce git_commit_body
It is already possible to get a commit's summary with the `git_commit_summary` function. It is not possible to get the remaining part of the commit message, that is the commit message's body. Fix this by introducing a new function `git_commit_body`.
1 parent 337b2b0 commit 7f8fe1d

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

include/git2/commit.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ GIT_EXTERN(const char *) git_commit_message_raw(const git_commit *commit);
127127
*/
128128
GIT_EXTERN(const char *) git_commit_summary(git_commit *commit);
129129

130+
/**
131+
* Get the long "body" of the git commit message.
132+
*
133+
* The returned message is the body of the commit, comprising
134+
* everything but the first paragraph of the message. Leading and
135+
* trailing whitespaces are trimmed.
136+
*
137+
* @param commit a previously loaded commit.
138+
* @return the body of a commit or NULL when no the message only
139+
* consists of a summary
140+
*/
141+
GIT_EXTERN(const char *) git_commit_body(git_commit *commit);
142+
130143
/**
131144
* Get the commit time (i.e. committer time) of a commit.
132145
*

src/commit.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void git_commit__free(void *_commit)
3131
git__free(commit->raw_message);
3232
git__free(commit->message_encoding);
3333
git__free(commit->summary);
34+
git__free(commit->body);
3435

3536
git__free(commit);
3637
}
@@ -472,6 +473,33 @@ const char *git_commit_summary(git_commit *commit)
472473
return commit->summary;
473474
}
474475

476+
const char *git_commit_body(git_commit *commit)
477+
{
478+
const char *msg, *end;
479+
480+
assert(commit);
481+
482+
if (!commit->body) {
483+
/* search for end of summary */
484+
for (msg = git_commit_message(commit); *msg; ++msg)
485+
if (msg[0] == '\n' && (!msg[1] || msg[1] == '\n'))
486+
break;
487+
488+
/* trim leading and trailing whitespace */
489+
for (; *msg; ++msg)
490+
if (!git__isspace(*msg))
491+
break;
492+
for (end = msg + strlen(msg) - 1; msg <= end; --end)
493+
if (!git__isspace(*end))
494+
break;
495+
496+
if (*msg)
497+
commit->body = git__strndup(msg, end - msg + 1);
498+
}
499+
500+
return commit->body;
501+
}
502+
475503
int git_commit_tree(git_tree **tree_out, const git_commit *commit)
476504
{
477505
assert(commit);

src/commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct git_commit {
2828
char *raw_header;
2929

3030
char *summary;
31+
char *body;
3132
};
3233

3334
void git_commit__free(void *commit);

tests/commit/commit.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ void assert_commit_summary(const char *expected, const char *given)
6363
git_commit__free(dummy);
6464
}
6565

66+
void assert_commit_body(const char *expected, const char *given)
67+
{
68+
git_commit *dummy;
69+
70+
cl_assert(dummy = git__calloc(1, sizeof(struct git_commit)));
71+
72+
dummy->raw_message = git__strdup(given);
73+
cl_assert_equal_s(expected, git_commit_body(dummy));
74+
75+
git_commit_free(dummy);
76+
}
77+
6678
void test_commit_commit__summary(void)
6779
{
6880
assert_commit_summary("One-liner with no trailing newline", "One-liner with no trailing newline");
@@ -80,8 +92,35 @@ void test_commit_commit__summary(void)
8092
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 */
8193
assert_commit_summary(" Spaces before newlines are collapsed", " \nSpaces before newlines \nare \ncollapsed \n");
8294
assert_commit_summary(" Spaces around newlines are collapsed", " \n Spaces around newlines \n are \n collapsed \n ");
95+
assert_commit_summary(" Trailing newlines are" , " \n Trailing newlines \n are \n\n collapsed \n ");
96+
assert_commit_summary(" Trailing spaces are stripped", " \n Trailing spaces \n are stripped \n\n \n \t ");
8397
assert_commit_summary("", "");
8498
assert_commit_summary("", " ");
8599
assert_commit_summary("", "\n");
86100
assert_commit_summary("", "\n \n");
87101
}
102+
103+
void test_commit_commit__body(void)
104+
{
105+
assert_commit_body(NULL, "One-liner with no trailing newline");
106+
assert_commit_body(NULL, "One-liner with trailing newline\n");
107+
assert_commit_body(NULL, "\n\nTrimmed leading&trailing newlines\n\n");
108+
assert_commit_body("(There are more!)", "\nFirst paragraph only\n\n(There are more!)");
109+
assert_commit_body("(Yes, unwrapped!)", "\nFirst paragraph\nwith unwrapped\ntrailing\tlines\n\n(Yes, unwrapped!)");
110+
assert_commit_body("are preserved", "\tLeading\n\ttabs\n\nare preserved"); /* tabs around newlines are collapsed down to a single space */
111+
assert_commit_body("are preserved", " Leading\n Spaces\n\nare preserved"); /* spaces around newlines are collapsed down to a single space */
112+
assert_commit_body(NULL, "Trailing tabs\tare removed\t\t");
113+
assert_commit_body(NULL, "Trailing spaces are removed ");
114+
assert_commit_body("are removed", "Trailing tabs\t\n\nare removed");
115+
assert_commit_body("are removed", "Trailing spaces \n\nare removed");
116+
assert_commit_body(NULL,"Newlines\nare\nreplaced by spaces\n");
117+
assert_commit_body(NULL , "\n Spaces after newlines\n are\n collapsed\n "); /* newlines at the very beginning are ignored and not collapsed */
118+
assert_commit_body(NULL , " \nSpaces before newlines \nare \ncollapsed \n");
119+
assert_commit_body(NULL , " \n Spaces around newlines \n are \n collapsed \n ");
120+
assert_commit_body("collapsed" , " \n Trailing newlines \n are \n\n collapsed \n ");
121+
assert_commit_body(NULL, " \n Trailing spaces \n are stripped \n\n \n \t ");
122+
assert_commit_body(NULL , "");
123+
assert_commit_body(NULL , " ");
124+
assert_commit_body(NULL , "\n");
125+
assert_commit_body(NULL , "\n \n");
126+
}

0 commit comments

Comments
 (0)