Commit 254e0a33 by Patrick Steinhardt

diff: include commit message when formatting patch

When formatting a patch as email we do not include the commit's
message in the formatted patch output. Implement this and add a
test that verifies behavior.
parent 7f8fe1d4
...@@ -1286,12 +1286,15 @@ typedef struct { ...@@ -1286,12 +1286,15 @@ typedef struct {
/** Summary of the change */ /** Summary of the change */
const char *summary; const char *summary;
/** Commit message's body */
const char *body;
/** Author of the change */ /** Author of the change */
const git_signature *author; const git_signature *author;
} git_diff_format_email_options; } git_diff_format_email_options;
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1 #define GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION 1
#define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL} #define GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT {GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION, 0, 1, 1, NULL, NULL, NULL, NULL}
/** /**
* Create an e-mail ready patch from a diff. * Create an e-mail ready patch from a diff.
......
...@@ -1659,6 +1659,7 @@ int git_diff_format_email__append_header_tobuf( ...@@ -1659,6 +1659,7 @@ int git_diff_format_email__append_header_tobuf(
const git_oid *id, const git_oid *id,
const git_signature *author, const git_signature *author,
const char *summary, const char *summary,
const char *body,
size_t patch_no, size_t patch_no,
size_t total_patches, size_t total_patches,
bool exclude_patchno_marker) bool exclude_patchno_marker)
...@@ -1698,6 +1699,13 @@ int git_diff_format_email__append_header_tobuf( ...@@ -1698,6 +1699,13 @@ int git_diff_format_email__append_header_tobuf(
error = git_buf_printf(out, "%s\n\n", summary); error = git_buf_printf(out, "%s\n\n", summary);
if (body) {
git_buf_puts(out, body);
if (out->ptr[out->size - 1] != '\n')
git_buf_putc(out, '\n');
}
return error; return error;
} }
...@@ -1775,7 +1783,7 @@ int git_diff_format_email( ...@@ -1775,7 +1783,7 @@ int git_diff_format_email(
error = git_diff_format_email__append_header_tobuf(out, error = git_diff_format_email__append_header_tobuf(out,
opts->id, opts->author, summary == NULL ? opts->summary : summary, opts->id, opts->author, summary == NULL ? opts->summary : summary,
opts->patch_no, opts->total_patches, ignore_marker); opts->body, opts->patch_no, opts->total_patches, ignore_marker);
if (error < 0) if (error < 0)
goto on_error; goto on_error;
...@@ -1818,6 +1826,7 @@ int git_diff_commit_as_email( ...@@ -1818,6 +1826,7 @@ int git_diff_commit_as_email(
opts.total_patches = total_patches; opts.total_patches = total_patches;
opts.id = git_commit_id(commit); opts.id = git_commit_id(commit);
opts.summary = git_commit_summary(commit); opts.summary = git_commit_summary(commit);
opts.body = git_commit_body(commit);
opts.author = git_commit_author(commit); opts.author = git_commit_author(commit);
if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0) if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
......
...@@ -97,6 +97,47 @@ void test_diff_format_email__simple(void) ...@@ -97,6 +97,47 @@ void test_diff_format_email__simple(void)
email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts); email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", &opts);
} }
void test_diff_format_email__with_message(void)
{
git_diff_format_email_options opts = GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
const char *email = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \
"From: Patrick Steinhardt <ps@pks.im>\n" \
"Date: Tue, 24 Nov 2015 13:34:39 +0100\n" \
"Subject: [PATCH] Modify content with message\n" \
"\n" \
"Modify content of file3.txt by appending a new line. Make this\n" \
"commit message somewhat longer to test behavior with newlines\n" \
"embedded in the message body.\n" \
"\n" \
"Also test if new paragraphs are included correctly.\n" \
"---\n" \
" file3.txt | 1 +\n" \
" 1 file changed, 1 insertion(+), 0 deletions(-)\n" \
"\n" \
"diff --git a/file3.txt b/file3.txt\n" \
"index 9a2d780..7309653 100644\n" \
"--- a/file3.txt\n" \
"+++ b/file3.txt\n" \
"@@ -3,3 +3,4 @@ file3!\n" \
" file3\n" \
" file3\n" \
" file3\n" \
"+file3\n" \
"--\n" \
"libgit2 0.23.0\n" \
"\n";
opts.body = "Modify content of file3.txt by appending a new line. Make this\n" \
"commit message somewhat longer to test behavior with newlines\n" \
"embedded in the message body.\n" \
"\n" \
"Also test if new paragraphs are included correctly.";
assert_email_match(
email, "627e7e12d87e07a83fad5b6bfa25e86ead4a5270", &opts);
}
void test_diff_format_email__multiple(void) void test_diff_format_email__multiple(void)
{ {
git_oid oid; git_oid oid;
......
873806f6f27e631eb0b23e4b56bea2bfac14a373 627e7e12d87e07a83fad5b6bfa25e86ead4a5270
...@@ -3,3 +3,4 @@ file3! ...@@ -3,3 +3,4 @@ file3!
file3 file3
file3 file3
file3 file3
file3
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment