Commit 323f222f7ec8967f8610b603a67959215d3a5b1f

Edward Thomson 2021-09-14T09:44:52

email: include binary diffs by default `git format-patch` includes binary diffs by default when creating emails. Match this behavior.

diff --git a/include/git2/email.h b/include/git2/email.h
index 48715fe..4b2d70e 100644
--- a/include/git2/email.h
+++ b/include/git2/email.h
@@ -64,12 +64,16 @@ typedef struct {
 	size_t reroll_number;
 } git_email_create_options;
 
+/*
+ * By default, our options include binary diffs to match `git format-patch`.
+ */
 #define GIT_EMAIL_CREATE_OPTIONS_VERSION 1
-#define GIT_EMAIL_CREATE_OPTIONS_INIT { \
-		GIT_EMAIL_CREATE_OPTIONS_VERSION, \
-		GIT_EMAIL_CREATE_DEFAULT, \
-		GIT_DIFF_OPTIONS_INIT \
-	}
+#define GIT_EMAIL_CREATE_OPTIONS_INIT \
+{ \
+	GIT_EMAIL_CREATE_OPTIONS_VERSION, \
+	GIT_EMAIL_CREATE_DEFAULT, \
+	{ GIT_DIFF_OPTIONS_VERSION, GIT_DIFF_SHOW_BINARY, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3 } \
+}
 
 /**
  * Create a diff for a commit in mbox format for sending via email.
diff --git a/src/email.c b/src/email.c
index 0bec515..8b6e133 100644
--- a/src/email.c
+++ b/src/email.c
@@ -255,8 +255,9 @@ int git_email_create_from_diff(
 int git_email_create_from_commit(
 	git_buf *out,
 	git_commit *commit,
-	const git_email_create_options *opts)
+	const git_email_create_options *given_opts)
 {
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
 	const git_diff_options *diff_opts;
 	git_diff *diff = NULL;
 	git_repository *repo;
@@ -268,21 +269,24 @@ int git_email_create_from_commit(
 	GIT_ASSERT_ARG(out);
 	GIT_ASSERT_ARG(commit);
 
-	GIT_ERROR_CHECK_VERSION(opts,
+	GIT_ERROR_CHECK_VERSION(given_opts,
 		GIT_EMAIL_CREATE_OPTIONS_VERSION,
 		"git_email_create_options");
 
+	if (given_opts)
+		memcpy(&opts, given_opts, sizeof(git_email_create_options));
+
 	repo = git_commit_owner(commit);
 	author = git_commit_author(commit);
 	summary = git_commit_summary(commit);
 	body = git_commit_body(commit);
 	commit_id = git_commit_id(commit);
-	diff_opts = opts ? &opts->diff_opts : NULL;
+	diff_opts = &opts.diff_opts;
 
 	if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
 		goto done;
 
-	error = git_email_create_from_diff(out, diff, 1, 1, commit_id, summary, body, author, opts);
+	error = git_email_create_from_diff(out, diff, 1, 1, commit_id, summary, body, author, &opts);
 
 done:
 	git_diff_free(diff);
diff --git a/tests/email/create.c b/tests/email/create.c
index 2b16d1d..8bbde20 100644
--- a/tests/email/create.c
+++ b/tests/email/create.c
@@ -112,6 +112,59 @@ void test_email_create__commit(void)
 		email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL);
 }
 
+void test_email_create__binary(void)
+{
+	const char *expected =
+	"From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
+	"From: Jacques Germishuys <jacquesg@striata.com>\n" \
+	"Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
+	"Subject: [PATCH] Modified binary file\n" \
+	"\n" \
+	"---\n" \
+	" binary.bin | Bin 3 -> 5 bytes\n" \
+	" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
+	"\n" \
+	"diff --git a/binary.bin b/binary.bin\n" \
+	"index bd474b2519cc15eab801ff851cc7d50f0dee49a1..9ac35ff15cd8864aeafd889e4826a3150f0b06c4 100644\n" \
+	"GIT binary patch\n" \
+	"literal 5\n" \
+	"Mc${NkU}WL~000&M4gdfE\n" \
+	"\n" \
+	"literal 3\n" \
+	"Kc${Nk-~s>u4FC%O\n" \
+	"\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", NULL);
+}
+
+void test_email_create__binary_not_included(void)
+{
+	const char *expected =
+	"From 8d7523f6fcb2404257889abe0d96f093d9f524f9 Mon Sep 17 00:00:00 2001\n" \
+	"From: Jacques Germishuys <jacquesg@striata.com>\n" \
+	"Date: Sun, 13 Apr 2014 18:10:18 +0200\n" \
+	"Subject: [PATCH] Modified binary file\n" \
+	"\n" \
+	"---\n" \
+	" binary.bin | Bin 3 -> 5 bytes\n" \
+	" 1 file changed, 0 insertions(+), 0 deletions(-)\n" \
+	"\n" \
+	"diff --git a/binary.bin b/binary.bin\n" \
+	"index bd474b2..9ac35ff 100644\n" \
+	"Binary files a/binary.bin and b/binary.bin differ\n" \
+	"--\n" \
+	"libgit2 " LIBGIT2_VERSION "\n" \
+	"\n";
+
+	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
+	opts.diff_opts.flags &= ~GIT_DIFF_SHOW_BINARY;
+
+	assert_email_match(expected, "8d7523f6fcb2404257889abe0d96f093d9f524f9", &opts);
+}
+
 void test_email_create__custom_summary_and_body(void)
 {
 	const char *expected = "From 627e7e12d87e07a83fad5b6bfa25e86ead4a5270 Mon Sep 17 00:00:00 2001\n" \