From 254e0a33ee66c65948e15efbe93a810c4077e324 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 24 Nov 2015 13:43:43 +0100 Subject: [PATCH] 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. --- include/git2/diff.h | 5 ++- src/diff.c | 11 ++++- tests/diff/format_email.c | 41 ++++++++++++++++++ .../resources/diff_format_email/.gitted/index | Bin 256 -> 289 bytes .../62/7e7e12d87e07a83fad5b6bfa25e86ead4a5270 | Bin 0 -> 289 bytes .../73/09653445ecf038d3e3dd9ed55edb6cb541a4ba | Bin 0 -> 28 bytes .../d5/ff67764c82f729b13c26a09576570d884d9687 | Bin 0 -> 121 bytes .../.gitted/refs/heads/master | Bin 41 -> 41 bytes tests/resources/diff_format_email/file3.txt | Bin 31 -> 37 bytes 9 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/resources/diff_format_email/.gitted/objects/62/7e7e12d87e07a83fad5b6bfa25e86ead4a5270 create mode 100644 tests/resources/diff_format_email/.gitted/objects/73/09653445ecf038d3e3dd9ed55edb6cb541a4ba create mode 100644 tests/resources/diff_format_email/.gitted/objects/d5/ff67764c82f729b13c26a09576570d884d9687 diff --git a/include/git2/diff.h b/include/git2/diff.h index cbffdb49a..3eb265652 100644 --- a/include/git2/diff.h +++ b/include/git2/diff.h @@ -1286,12 +1286,15 @@ typedef struct { /** Summary of the change */ const char *summary; + /** Commit message's body */ + const char *body; + /** Author of the change */ const git_signature *author; } git_diff_format_email_options; #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. diff --git a/src/diff.c b/src/diff.c index 9402b6e61..67fab0763 100644 --- a/src/diff.c +++ b/src/diff.c @@ -1659,6 +1659,7 @@ int git_diff_format_email__append_header_tobuf( const git_oid *id, const git_signature *author, const char *summary, + const char *body, size_t patch_no, size_t total_patches, bool exclude_patchno_marker) @@ -1698,6 +1699,13 @@ int git_diff_format_email__append_header_tobuf( 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; } @@ -1775,7 +1783,7 @@ int git_diff_format_email( error = git_diff_format_email__append_header_tobuf(out, 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) goto on_error; @@ -1818,6 +1826,7 @@ int git_diff_commit_as_email( opts.total_patches = total_patches; opts.id = git_commit_id(commit); opts.summary = git_commit_summary(commit); + opts.body = git_commit_body(commit); opts.author = git_commit_author(commit); if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0) diff --git a/tests/diff/format_email.c b/tests/diff/format_email.c index 18ad99bd5..8a0128898 100644 --- a/tests/diff/format_email.c +++ b/tests/diff/format_email.c @@ -97,6 +97,47 @@ void test_diff_format_email__simple(void) 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 \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) { git_oid oid; diff --git a/tests/resources/diff_format_email/.gitted/index b/tests/resources/diff_format_email/.gitted/index index f73027e565387ebbe041eab863c6cce17c04085c..d94f87de830619993545c15c09363479043f1d9c 100644 GIT binary patch delta 222 zcmZo*TF7MJ;u+-3z`(!+#LS^82OT)v#q41;1H(T?h6eE`3=EBLfzmHPc%p%h3QQhG zL)AS6s#^kA*Pv1x7UC%*{ozgpnC@0)g{pgotPW_eYB6W3iR+sW7MCC2op&|vcFtDE zCA%0n(=u~XjrB?@N*F?dTwQ_cBpHkq47jfTPcQRn`mVXrMs2~=vT)uG-)ZfJIakiP VoZfL{_Pq~(g)TTLO|xf=002wANY?-W delta 189 zcmZ3;)WBrm;u+-3z`(!+#LU4SkugA;0Y)7k2CH-1tpHTF1g@?@B?gB&MPzkAbL3~~R&X79#r10M&PyxGp6&ksv0BH4fio>L hC)HT5q@sjjCbK1To7|RzlX5z99et-tKipgw3jp#`G1UM7 diff --git a/tests/resources/diff_format_email/.gitted/objects/62/7e7e12d87e07a83fad5b6bfa25e86ead4a5270 b/tests/resources/diff_format_email/.gitted/objects/62/7e7e12d87e07a83fad5b6bfa25e86ead4a5270 new file mode 100644 index 0000000000000000000000000000000000000000..269a5bcf4b8a7c651185dcb36180a472876b9f79 GIT binary patch literal 289 zcmV++0p9+20hN$VPQx$|g<1O)vqy@O*s)y+Avi!KBu?O;abh*GBYR5e?Qtt$!H)Il z``(-9^0q|+A*Kf^h~TFXq*78iJq9%%?7-b9O@Ek_oK)|%7XBc$V!J7r2(;q5mqCV6 zq2vSF-i{oFouoxG+F;xu3|E|)GcCEmo1qfj3El}~T1@c-PkVjYPqmBNGYmp#F11hx zc2_z{%UOY n*N(B*wSMz4+|%3EW-ex5Dzw!b<<|2JcexaFv^MAqRH2qTLja9U literal 0 HcmV?d00001 diff --git a/tests/resources/diff_format_email/.gitted/objects/73/09653445ecf038d3e3dd9ed55edb6cb541a4ba b/tests/resources/diff_format_email/.gitted/objects/73/09653445ecf038d3e3dd9ed55edb6cb541a4ba new file mode 100644 index 0000000000000000000000000000000000000000..ba9c5fa577c6b40c213f96f53b4f253d2d919394 GIT binary patch literal 28 jcmb7v}7mEv56!`27!$qQis literal 41 ucmV~$!4Uv31O&i;s=*1`aT)qAA-k0*iH)_KfDVO*ao*sxar>z^66XWv?F%9R diff --git a/tests/resources/diff_format_email/file3.txt b/tests/resources/diff_format_email/file3.txt index 9a2d780ac2ea0aeabdb9d2a876e6bbfff17b2c44..7309653445ecf038d3e3dd9ed55edb6cb541a4ba 100644 GIT binary patch delta 11 Scmb1FogmMamYI`k%mn}t!~)j< delta 4 LcmY$DpCAta0yF^h