Skip to content

Commit 67b1d01

Browse files
committed
email: include renames by default
`git format-patch` includes diffs with rename detection enabled by default when creating emails. Match this behavior.
1 parent 323f222 commit 67b1d01

File tree

3 files changed

+132
-5
lines changed

3 files changed

+132
-5
lines changed

include/git2/email.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ typedef enum {
3232
* patch is for a single commit (1/1).
3333
*/
3434
GIT_EMAIL_CREATE_ALWAYS_NUMBER = (1u << 1),
35+
36+
/** Do not perform rename or similarity detection. */
37+
GIT_EMAIL_CREATE_NO_RENAMES = (1u << 2),
3538
} git_email_create_flags_t;
3639

3740
/**
@@ -46,6 +49,9 @@ typedef struct {
4649
/** Options to use when creating diffs */
4750
git_diff_options diff_opts;
4851

52+
/** Options for finding similarities within diffs */
53+
git_diff_find_options diff_find_opts;
54+
4955
/**
5056
* The subject prefix, by default "PATCH". If set to an empty
5157
* string ("") then only the patch numbers will be shown in the
@@ -65,14 +71,16 @@ typedef struct {
6571
} git_email_create_options;
6672

6773
/*
68-
* By default, our options include binary diffs to match `git format-patch`.
74+
* By default, our options include rename detection and binary
75+
* diffs to match `git format-patch`.
6976
*/
7077
#define GIT_EMAIL_CREATE_OPTIONS_VERSION 1
7178
#define GIT_EMAIL_CREATE_OPTIONS_INIT \
7279
{ \
7380
GIT_EMAIL_CREATE_OPTIONS_VERSION, \
7481
GIT_EMAIL_CREATE_DEFAULT, \
75-
{ GIT_DIFF_OPTIONS_VERSION, GIT_DIFF_SHOW_BINARY, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3 } \
82+
{ GIT_DIFF_OPTIONS_VERSION, GIT_DIFF_SHOW_BINARY, GIT_SUBMODULE_IGNORE_UNSPECIFIED, {NULL,0}, NULL, NULL, NULL, 3 }, \
83+
GIT_DIFF_FIND_OPTIONS_INIT \
7684
}
7785

7886
/**

src/email.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,10 @@ int git_email_create_from_commit(
258258
const git_email_create_options *given_opts)
259259
{
260260
git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
261-
const git_diff_options *diff_opts;
262261
git_diff *diff = NULL;
263262
git_repository *repo;
263+
git_diff_options *diff_opts;
264+
git_diff_find_options *find_opts;
264265
const git_signature *author;
265266
const char *summary, *body;
266267
const git_oid *commit_id;
@@ -282,10 +283,15 @@ int git_email_create_from_commit(
282283
body = git_commit_body(commit);
283284
commit_id = git_commit_id(commit);
284285
diff_opts = &opts.diff_opts;
286+
find_opts = &opts.diff_find_opts;
285287

286288
if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
287289
goto done;
288290

291+
if ((opts.flags & GIT_EMAIL_CREATE_NO_RENAMES) == 0 &&
292+
(error = git_diff_find_similar(diff, find_opts)) < 0)
293+
goto done;
294+
289295
error = git_email_create_from_diff(out, diff, 1, 1, commit_id, summary, body, author, &opts);
290296

291297
done:

tests/email/create.c

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static void assert_subject_match(
6969

7070
void test_email_create__commit(void)
7171
{
72-
const char *email =
72+
const char *expected =
7373
"From 9264b96c6d104d0e07ae33d3007b6a48246c6f92 Mon Sep 17 00:00:00 2001\n" \
7474
"From: Jacques Germishuys <[email protected]>\n" \
7575
"Date: Wed, 9 Apr 2014 20:57:01 +0200\n" \
@@ -109,7 +109,120 @@ void test_email_create__commit(void)
109109
"\n";
110110

111111
assert_email_match(
112-
email, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL);
112+
expected, "9264b96c6d104d0e07ae33d3007b6a48246c6f92", NULL);
113+
}
114+
115+
void test_email_create__rename(void)
116+
{
117+
const char *expected =
118+
"From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
119+
"From: Jacques Germishuys <[email protected]>\n" \
120+
"Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
121+
"Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
122+
"\n" \
123+
"---\n" \
124+
" file1.txt => file1.txt.renamed | 4 ++--\n" \
125+
" 1 file changed, 2 insertions(+), 2 deletions(-)\n" \
126+
"\n" \
127+
"diff --git a/file1.txt b/file1.txt.renamed\n" \
128+
"similarity index 86%\n" \
129+
"rename from file1.txt\n" \
130+
"rename to file1.txt.renamed\n" \
131+
"index af8f41d..a97157a 100644\n" \
132+
"--- a/file1.txt\n" \
133+
"+++ b/file1.txt.renamed\n" \
134+
"@@ -3,13 +3,13 @@ file1.txt\n" \
135+
" _file1.txt_\n" \
136+
" file1.txt\n" \
137+
" file1.txt\n" \
138+
"-file1.txt\n" \
139+
"+file1.txt_renamed\n" \
140+
" file1.txt\n" \
141+
" \n" \
142+
" \n" \
143+
" file1.txt\n" \
144+
" file1.txt\n" \
145+
"-file1.txt\n" \
146+
"+file1.txt_renamed\n" \
147+
" file1.txt\n" \
148+
" file1.txt\n" \
149+
" _file1.txt_\n" \
150+
"--\n" \
151+
"libgit2 " LIBGIT2_VERSION "\n" \
152+
"\n";
153+
154+
assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", NULL);
155+
}
156+
157+
void test_email_create__rename_as_add_delete(void)
158+
{
159+
const char *expected =
160+
"From 6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d Mon Sep 17 00:00:00 2001\n" \
161+
"From: Jacques Germishuys <[email protected]>\n" \
162+
"Date: Wed, 9 Apr 2014 21:15:56 +0200\n" \
163+
"Subject: [PATCH] Renamed file1.txt -> file1.txt.renamed\n" \
164+
"\n" \
165+
"---\n" \
166+
" file1.txt | 17 -----------------\n" \
167+
" file1.txt.renamed | 17 +++++++++++++++++\n" \
168+
" 2 files changed, 17 insertions(+), 17 deletions(-)\n" \
169+
" delete mode 100644 file1.txt\n" \
170+
" create mode 100644 file1.txt.renamed\n" \
171+
"\n" \
172+
"diff --git a/file1.txt b/file1.txt\n" \
173+
"deleted file mode 100644\n" \
174+
"index af8f41d..0000000\n" \
175+
"--- a/file1.txt\n" \
176+
"+++ /dev/null\n" \
177+
"@@ -1,17 +0,0 @@\n" \
178+
"-file1.txt\n" \
179+
"-file1.txt\n" \
180+
"-_file1.txt_\n" \
181+
"-file1.txt\n" \
182+
"-file1.txt\n" \
183+
"-file1.txt\n" \
184+
"-file1.txt\n" \
185+
"-\n" \
186+
"-\n" \
187+
"-file1.txt\n" \
188+
"-file1.txt\n" \
189+
"-file1.txt\n" \
190+
"-file1.txt\n" \
191+
"-file1.txt\n" \
192+
"-_file1.txt_\n" \
193+
"-_file1.txt_\n" \
194+
"-file1.txt\n" \
195+
"diff --git a/file1.txt.renamed b/file1.txt.renamed\n" \
196+
"new file mode 100644\n" \
197+
"index 0000000..a97157a\n" \
198+
"--- /dev/null\n" \
199+
"+++ b/file1.txt.renamed\n" \
200+
"@@ -0,0 +1,17 @@\n" \
201+
"+file1.txt\n" \
202+
"+file1.txt\n" \
203+
"+_file1.txt_\n" \
204+
"+file1.txt\n" \
205+
"+file1.txt\n" \
206+
"+file1.txt_renamed\n" \
207+
"+file1.txt\n" \
208+
"+\n" \
209+
"+\n" \
210+
"+file1.txt\n" \
211+
"+file1.txt\n" \
212+
"+file1.txt_renamed\n" \
213+
"+file1.txt\n" \
214+
"+file1.txt\n" \
215+
"+_file1.txt_\n" \
216+
"+_file1.txt_\n" \
217+
"+file1.txt\n" \
218+
"--\n" \
219+
"libgit2 " LIBGIT2_VERSION "\n" \
220+
"\n";
221+
222+
git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
223+
opts.flags |= GIT_EMAIL_CREATE_NO_RENAMES;
224+
225+
assert_email_match(expected, "6e05acc5a5dab507d91a0a0cc0fb05a3dd98892d", &opts);
113226
}
114227

115228
void test_email_create__binary(void)

0 commit comments

Comments
 (0)