summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-09-14 09:44:52 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-09-18 08:32:41 -0400
commit323f222f7ec8967f8610b603a67959215d3a5b1f (patch)
tree8cd84d3edb4e92272f29315b990b53c476112dfa
parentc443495bd36ef2a22d3828d449d4f03c85464cf6 (diff)
downloadlibgit2-323f222f7ec8967f8610b603a67959215d3a5b1f.tar.gz
email: include binary diffs by default
`git format-patch` includes binary diffs by default when creating emails. Match this behavior.
-rw-r--r--include/git2/email.h14
-rw-r--r--src/email.c12
-rw-r--r--tests/email/create.c53
3 files changed, 70 insertions, 9 deletions
diff --git a/include/git2/email.h b/include/git2/email.h
index 48715fe76..4b2d70ead 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 0bec5155c..8b6e13347 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 2b16d1da1..8bbde20d7 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" \