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

Skip to content

Commit 6aa06b6

Browse files
committed
Merge pull request libgit2#3522 from pks-t/email-format-commit-message
diff: include commit message when formatting patch
2 parents dc49eb5 + 254e0a3 commit 6aa06b6

File tree

13 files changed

+139
-3
lines changed

13 files changed

+139
-3
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
*

include/git2/diff.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,12 +1286,15 @@ typedef struct {
12861286
/** Summary of the change */
12871287
const char *summary;
12881288

1289+
/** Commit message's body */
1290+
const char *body;
1291+
12891292
/** Author of the change */
12901293
const git_signature *author;
12911294
} git_diff_format_email_options;
12921295

12931296
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1
1294-
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL}
1297+
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL}
12951298

12961299
/**
12971300
* Create an e-mail ready patch from a diff.

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);

src/diff.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,7 @@ int git_diff_format_email__append_header_tobuf(
16591659
const git_oid *id,
16601660
const git_signature *author,
16611661
const char *summary,
1662+
const char *body,
16621663
size_t patch_no,
16631664
size_t total_patches,
16641665
bool exclude_patchno_marker)
@@ -1698,6 +1699,13 @@ int git_diff_format_email__append_header_tobuf(
16981699

16991700
error = git_buf_printf(out, "%s\n\n", summary);
17001701

1702+
if (body) {
1703+
git_buf_puts(out, body);
1704+
1705+
if (out->ptr[out->size - 1] != '\n')
1706+
git_buf_putc(out, '\n');
1707+
}
1708+
17011709
return error;
17021710
}
17031711

@@ -1775,7 +1783,7 @@ int git_diff_format_email(
17751783

17761784
error = git_diff_format_email__append_header_tobuf(out,
17771785
opts->id, opts->author, summary == NULL ? opts->summary : summary,
1778-
opts->patch_no, opts->total_patches, ignore_marker);
1786+
opts->body, opts->patch_no, opts->total_patches, ignore_marker);
17791787

17801788
if (error < 0)
17811789
goto on_error;
@@ -1818,6 +1826,7 @@ int git_diff_commit_as_email(
18181826
opts.total_patches = total_patches;
18191827
opts.id = git_commit_id(commit);
18201828
opts.summary = git_commit_summary(commit);
1829+
opts.body = git_commit_body(commit);
18211830
opts.author = git_commit_author(commit);
18221831

18231832
if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)

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+
}

tests/diff/format_email.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,47 @@ void test_diff_format_email__simple(void)
9797
email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
9898
}
9999

100+
void test_diff_format_email__with_message(void)
101+
{
102+
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
103+
const char *email = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
104+
"From: Patrick Steinhardt <[email protected]>\n" \
105+
"Date: Tue, 24 Nov 2015 13:34:39 +0100\n" \
106+
"Subject: [PATCH] Modify content with message\n" \
107+
"\n" \
108+
"Modify content of file3.txt by appending a new line. Make this\n" \
109+
"commit message somewhat longer to test behavior with newlines\n" \
110+
"embedded in the message body.\n" \
111+
"\n" \
112+
"Also test if new paragraphs are included correctly.\n" \
113+
"---\n" \
114+
" file3.txt | 1 +\n" \
115+
" 1 file changed, 1 insertion(+), 0 deletions(-)\n" \
116+
"\n" \
117+
"diff --git a/file3.txt b/file3.txt\n" \
118+
"index 9a2d780..7309653 100644\n" \
119+
"--- a/file3.txt\n" \
120+
"+++ b/file3.txt\n" \
121+
"@@ -3,3 +3,4 @@ file3!\n" \
122+
" file3\n" \
123+
" file3\n" \
124+
" file3\n" \
125+
"+file3\n" \
126+
"--\n" \
127+
"libgit2 0.23.0\n" \
128+
"\n";
129+
130+
opts.body = "Modify content of file3.txt by appending a new line. Make this\n" \
131+
"commit message somewhat longer to test behavior with newlines\n" \
132+
"embedded in the message body.\n" \
133+
"\n" \
134+
"Also test if new paragraphs are included correctly.";
135+
136+
assert_email_match(
137+
email, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270", &opts);
138+
}
139+
140+
100141
void test_diff_format_email__multiple(void)
101142
{
102143
git_oid oid;
33 Bytes
Binary file not shown.

tests/resources/diff_format_email/.gitted/objects/62/7e7e12d87e07a83fad5b6bfa25e86ead4a5270

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x��MN�0�Y��G��ر] !8@%$N��qb5�#{J��q+`�����ޛ��E!�*���RR8=5���(M���T�� �b�)��⺗A�Q���ގ���E3�`� W��3-����� � cZL�O{}�O���3 Bh.�Px쇾g�ޏ��$;f�\Ntkz��������c⊼�O{��|L3Hx�5&��hN���]�G5oxY ���ܺR��L����޾�7��Sn�G15�jl�׎�׵~�1����f.f_*�����w�t�6� T��;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
873806f6f27e631eb0b23e4b56bea2bfac14a373
1+
627e7e12d87e07a83fad5b6bfa25e86ead4a5270

tests/resources/diff_format_email/file3.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ file3!
33
file3
44
file3
55
file3
6+
file3

0 commit comments

Comments
 (0)