Skip to content

Commit 108dab2

Browse files
bebarinogitster
authored andcommitted
format-patch: --attach/inline uses filename instead of SHA1
Currently when format-patch is used with --attach or --inline the patch attachment has the SHA1 of the commit for its filename. This replaces the SHA1 with the filename used by format-patch when outputting to files. Fix tests relying on the SHA1 output and add a test showing how the --suffix option affects the attachment filename output. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6fa8e62 commit 108dab2

14 files changed

+112
-44
lines changed

builtin-log.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
607607
int nr, struct commit **list, struct commit *head)
608608
{
609609
const char *committer;
610-
char *head_sha1;
611610
const char *subject_start = NULL;
612611
const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
613612
const char *msg;
@@ -624,7 +623,6 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
624623
die("Cover letter needs email format");
625624

626625
committer = git_committer_info(0);
627-
head_sha1 = sha1_to_hex(head->object.sha1);
628626

629627
if (!numbered_files) {
630628
/*
@@ -639,7 +637,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
639637
"author %s\n"
640638
"committer %s\n\n"
641639
"cover letter\n",
642-
head_sha1, committer, committer);
640+
sha1_to_hex(head->object.sha1), committer, committer);
643641
}
644642

645643
if (!use_stdout && reopen_stdout(commit, rev))
@@ -651,7 +649,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
651649
free(commit);
652650
}
653651

654-
log_write_email_headers(rev, head_sha1, &subject_start, &extra_headers,
652+
log_write_email_headers(rev, head, &subject_start, &extra_headers,
655653
&need_8bit_cte);
656654

657655
msg = body;
@@ -1011,6 +1009,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10111009
const char *msgid = clean_message_id(in_reply_to);
10121010
string_list_append(msgid, rev.ref_message_ids);
10131011
}
1012+
rev.numbered_files = numbered_files;
1013+
rev.patch_suffix = fmt_patch_suffix;
10141014
if (cover_letter) {
10151015
if (thread)
10161016
gen_message_id(&rev, "cover");

log-tree.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,14 @@ void get_patch_filename(struct commit *commit, int nr, const char *suffix,
201201
}
202202
}
203203

204-
void log_write_email_headers(struct rev_info *opt, const char *name,
204+
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
205205
const char **subject_p,
206206
const char **extra_headers_p,
207207
int *need_8bit_cte_p)
208208
{
209209
const char *subject = NULL;
210210
const char *extra_headers = opt->extra_headers;
211+
const char *name = sha1_to_hex(commit->object.sha1);
211212

212213
*need_8bit_cte_p = 0; /* unknown */
213214
if (opt->total > 0) {
@@ -246,6 +247,7 @@ void log_write_email_headers(struct rev_info *opt, const char *name,
246247
if (opt->mime_boundary) {
247248
static char subject_buffer[1024];
248249
static char buffer[1024];
250+
struct strbuf filename = STRBUF_INIT;
249251
*need_8bit_cte_p = -1; /* NEVER */
250252
snprintf(subject_buffer, sizeof(subject_buffer) - 1,
251253
"%s"
@@ -264,18 +266,21 @@ void log_write_email_headers(struct rev_info *opt, const char *name,
264266
mime_boundary_leader, opt->mime_boundary);
265267
extra_headers = subject_buffer;
266268

269+
get_patch_filename(opt->numbered_files ? NULL : commit, opt->nr,
270+
opt->patch_suffix, &filename);
267271
snprintf(buffer, sizeof(buffer) - 1,
268272
"\n--%s%s\n"
269273
"Content-Type: text/x-patch;"
270-
" name=\"%s.diff\"\n"
274+
" name=\"%s\"\n"
271275
"Content-Transfer-Encoding: 8bit\n"
272276
"Content-Disposition: %s;"
273-
" filename=\"%s.diff\"\n\n",
277+
" filename=\"%s\"\n\n",
274278
mime_boundary_leader, opt->mime_boundary,
275-
name,
279+
filename.buf,
276280
opt->no_inline ? "attachment" : "inline",
277-
name);
281+
filename.buf);
278282
opt->diffopt.stat_sep = buffer;
283+
strbuf_release(&filename);
279284
}
280285
*subject_p = subject;
281286
*extra_headers_p = extra_headers;
@@ -355,8 +360,7 @@ void show_log(struct rev_info *opt)
355360
*/
356361

357362
if (opt->commit_format == CMIT_FMT_EMAIL) {
358-
log_write_email_headers(opt, sha1_to_hex(commit->object.sha1),
359-
&subject, &extra_headers,
363+
log_write_email_headers(opt, commit, &subject, &extra_headers,
360364
&need_8bit_cte);
361365
} else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
362366
fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), stdout);

log-tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ int log_tree_commit(struct rev_info *, struct commit *);
1313
int log_tree_opt_parse(struct rev_info *, const char **, int);
1414
void show_log(struct rev_info *opt);
1515
void show_decorations(struct rev_info *opt, struct commit *commit);
16-
void log_write_email_headers(struct rev_info *opt, const char *name,
16+
void log_write_email_headers(struct rev_info *opt, struct commit *commit,
1717
const char **subject_p,
1818
const char **extra_headers_p,
1919
int *need_8bit_cte_p);

revision.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ struct rev_info {
8686
struct log_info *loginfo;
8787
int nr, total;
8888
const char *mime_boundary;
89+
const char *patch_suffix;
90+
int numbered_files;
8991
char *message_id;
9092
struct string_list *ref_message_ids;
9193
const char *add_signoff;

t/t4013-diff-various.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ format-patch --stdout initial..master
246246
format-patch --stdout --no-numbered initial..master
247247
format-patch --stdout --numbered initial..master
248248
format-patch --attach --stdout initial..side
249+
format-patch --attach --stdout --suffix=.diff initial..side
249250
format-patch --attach --stdout initial..master^
250251
format-patch --attach --stdout initial..master
251252
format-patch --inline --stdout initial..side
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
$ git format-patch --attach --stdout --suffix=.diff initial..side
2+
From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
3+
From: A U Thor <[email protected]>
4+
Date: Mon, 26 Jun 2006 00:03:00 +0000
5+
Subject: [PATCH] Side
6+
MIME-Version: 1.0
7+
Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
8+
9+
This is a multi-part message in MIME format.
10+
--------------g-i-t--v-e-r-s-i-o-n
11+
Content-Type: text/plain; charset=UTF-8; format=fixed
12+
Content-Transfer-Encoding: 8bit
13+
14+
---
15+
dir/sub | 2 ++
16+
file0 | 3 +++
17+
file3 | 4 ++++
18+
3 files changed, 9 insertions(+), 0 deletions(-)
19+
create mode 100644 file3
20+
21+
22+
--------------g-i-t--v-e-r-s-i-o-n
23+
Content-Type: text/x-patch; name="0001-Side.diff"
24+
Content-Transfer-Encoding: 8bit
25+
Content-Disposition: attachment; filename="0001-Side.diff"
26+
27+
diff --git a/dir/sub b/dir/sub
28+
index 35d242b..7289e35 100644
29+
--- a/dir/sub
30+
+++ b/dir/sub
31+
@@ -1,2 +1,4 @@
32+
A
33+
B
34+
+1
35+
+2
36+
diff --git a/file0 b/file0
37+
index 01e79c3..f4615da 100644
38+
--- a/file0
39+
+++ b/file0
40+
@@ -1,3 +1,6 @@
41+
1
42+
2
43+
3
44+
+A
45+
+B
46+
+C
47+
diff --git a/file3 b/file3
48+
new file mode 100644
49+
index 0000000..7289e35
50+
--- /dev/null
51+
+++ b/file3
52+
@@ -0,0 +1,4 @@
53+
+A
54+
+B
55+
+1
56+
+2
57+
58+
--------------g-i-t--v-e-r-s-i-o-n--
59+
60+
61+
$

t/t4013/diff.format-patch_--attach_--stdout_initial..master

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ This is the second commit.
2222

2323

2424
--------------g-i-t--v-e-r-s-i-o-n
25-
Content-Type: text/x-patch; name="1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44.diff"
25+
Content-Type: text/x-patch; name="0001-Second.patch"
2626
Content-Transfer-Encoding: 8bit
27-
Content-Disposition: attachment; filename="1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44.diff"
27+
Content-Disposition: attachment; filename="0001-Second.patch"
2828

2929
diff --git a/dir/sub b/dir/sub
3030
index 35d242b..8422d40 100644
@@ -80,9 +80,9 @@ Content-Transfer-Encoding: 8bit
8080

8181

8282
--------------g-i-t--v-e-r-s-i-o-n
83-
Content-Type: text/x-patch; name="9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0.diff"
83+
Content-Type: text/x-patch; name="0002-Third.patch"
8484
Content-Transfer-Encoding: 8bit
85-
Content-Disposition: attachment; filename="9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0.diff"
85+
Content-Disposition: attachment; filename="0002-Third.patch"
8686

8787
diff --git a/dir/sub b/dir/sub
8888
index 8422d40..cead32e 100644
@@ -129,9 +129,9 @@ Content-Transfer-Encoding: 8bit
129129

130130

131131
--------------g-i-t--v-e-r-s-i-o-n
132-
Content-Type: text/x-patch; name="c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a.diff"
132+
Content-Type: text/x-patch; name="0003-Side.patch"
133133
Content-Transfer-Encoding: 8bit
134-
Content-Disposition: attachment; filename="c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a.diff"
134+
Content-Disposition: attachment; filename="0003-Side.patch"
135135

136136
diff --git a/dir/sub b/dir/sub
137137
index 35d242b..7289e35 100644

t/t4013/diff.format-patch_--attach_--stdout_initial..master^

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ This is the second commit.
2222

2323

2424
--------------g-i-t--v-e-r-s-i-o-n
25-
Content-Type: text/x-patch; name="1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44.diff"
25+
Content-Type: text/x-patch; name="0001-Second.patch"
2626
Content-Transfer-Encoding: 8bit
27-
Content-Disposition: attachment; filename="1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44.diff"
27+
Content-Disposition: attachment; filename="0001-Second.patch"
2828

2929
diff --git a/dir/sub b/dir/sub
3030
index 35d242b..8422d40 100644
@@ -80,9 +80,9 @@ Content-Transfer-Encoding: 8bit
8080

8181

8282
--------------g-i-t--v-e-r-s-i-o-n
83-
Content-Type: text/x-patch; name="9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0.diff"
83+
Content-Type: text/x-patch; name="0002-Third.patch"
8484
Content-Transfer-Encoding: 8bit
85-
Content-Disposition: attachment; filename="9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0.diff"
85+
Content-Disposition: attachment; filename="0002-Third.patch"
8686

8787
diff --git a/dir/sub b/dir/sub
8888
index 8422d40..cead32e 100644

t/t4013/diff.format-patch_--attach_--stdout_initial..side

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ Content-Transfer-Encoding: 8bit
2020

2121

2222
--------------g-i-t--v-e-r-s-i-o-n
23-
Content-Type: text/x-patch; name="c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a.diff"
23+
Content-Type: text/x-patch; name="0001-Side.patch"
2424
Content-Transfer-Encoding: 8bit
25-
Content-Disposition: attachment; filename="c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a.diff"
25+
Content-Disposition: attachment; filename="0001-Side.patch"
2626

2727
diff --git a/dir/sub b/dir/sub
2828
index 35d242b..7289e35 100644

t/t4013/diff.format-patch_--inline_--stdout_--subject-prefix=TESTCASE_initial..master

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ This is the second commit.
2222

2323

2424
--------------g-i-t--v-e-r-s-i-o-n
25-
Content-Type: text/x-patch; name="1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44.diff"
25+
Content-Type: text/x-patch; name="0001-Second.patch"
2626
Content-Transfer-Encoding: 8bit
27-
Content-Disposition: inline; filename="1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44.diff"
27+
Content-Disposition: inline; filename="0001-Second.patch"
2828

2929
diff --git a/dir/sub b/dir/sub
3030
index 35d242b..8422d40 100644
@@ -80,9 +80,9 @@ Content-Transfer-Encoding: 8bit
8080

8181

8282
--------------g-i-t--v-e-r-s-i-o-n
83-
Content-Type: text/x-patch; name="9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0.diff"
83+
Content-Type: text/x-patch; name="0002-Third.patch"
8484
Content-Transfer-Encoding: 8bit
85-
Content-Disposition: inline; filename="9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0.diff"
85+
Content-Disposition: inline; filename="0002-Third.patch"
8686

8787
diff --git a/dir/sub b/dir/sub
8888
index 8422d40..cead32e 100644
@@ -129,9 +129,9 @@ Content-Transfer-Encoding: 8bit
129129

130130

131131
--------------g-i-t--v-e-r-s-i-o-n
132-
Content-Type: text/x-patch; name="c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a.diff"
132+
Content-Type: text/x-patch; name="0003-Side.patch"
133133
Content-Transfer-Encoding: 8bit
134-
Content-Disposition: inline; filename="c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a.diff"
134+
Content-Disposition: inline; filename="0003-Side.patch"
135135

136136
diff --git a/dir/sub b/dir/sub
137137
index 35d242b..7289e35 100644

0 commit comments

Comments
 (0)