1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
11 static void cleanup_space(struct strbuf
*sb
)
14 for (pos
= 0; pos
< sb
->len
; pos
++) {
15 if (isspace(sb
->buf
[pos
])) {
17 for (cnt
= 0; isspace(sb
->buf
[pos
+ cnt
+ 1]); cnt
++);
18 strbuf_remove(sb
, pos
+ 1, cnt
);
23 static void get_sane_name(struct strbuf
*out
, struct strbuf
*name
, struct strbuf
*email
)
25 struct strbuf
*src
= name
;
26 if (!name
->len
|| 60 < name
->len
|| strpbrk(name
->buf
, "@<>"))
31 strbuf_addbuf(out
, src
);
34 static void parse_bogus_from(struct mailinfo
*mi
, const struct strbuf
*line
)
36 /* John Doe <johndoe> */
39 /* This is fallback, so do not bother if we already have an
45 bra
= strchr(line
->buf
, '<');
48 ket
= strchr(bra
, '>');
52 strbuf_reset(&mi
->email
);
53 strbuf_add(&mi
->email
, bra
+ 1, ket
- bra
- 1);
55 strbuf_reset(&mi
->name
);
56 strbuf_add(&mi
->name
, line
->buf
, bra
- line
->buf
);
57 strbuf_trim(&mi
->name
);
58 get_sane_name(&mi
->name
, &mi
->name
, &mi
->email
);
61 static const char *unquote_comment(struct strbuf
*outbuf
, const char *in
)
63 int take_next_literally
= 0;
66 strbuf_addch(outbuf
, '(');
70 if (take_next_literally
== 1) {
71 take_next_literally
= 0;
75 take_next_literally
= 1;
78 strbuf_addch(outbuf
, '(');
82 strbuf_addch(outbuf
, ')');
89 strbuf_addch(outbuf
, c
);
95 static const char *unquote_quoted_string(struct strbuf
*outbuf
, const char *in
)
97 int take_next_literally
= 0;
101 if (take_next_literally
== 1) {
102 take_next_literally
= 0;
106 take_next_literally
= 1;
113 strbuf_addch(outbuf
, c
);
119 static void unquote_quoted_pair(struct strbuf
*line
)
121 struct strbuf outbuf
;
122 const char *in
= line
->buf
;
125 strbuf_init(&outbuf
, line
->len
);
127 while ((c
= *in
++) != 0) {
130 in
= unquote_quoted_string(&outbuf
, in
);
133 in
= unquote_comment(&outbuf
, in
);
137 strbuf_addch(&outbuf
, c
);
140 strbuf_swap(&outbuf
, line
);
141 strbuf_release(&outbuf
);
145 static void handle_from(struct mailinfo
*mi
, const struct strbuf
*from
)
151 strbuf_init(&f
, from
->len
);
152 strbuf_addbuf(&f
, from
);
154 unquote_quoted_pair(&f
);
156 at
= strchr(f
.buf
, '@');
158 parse_bogus_from(mi
, from
);
163 * If we already have one email, don't take any confusing lines
165 if (mi
->email
.len
&& strchr(at
+ 1, '@'))
168 /* Pick up the string around '@', possibly delimited with <>
169 * pair; that is the email part.
181 el
= strcspn(at
, " \n\t\r\v\f>");
182 strbuf_reset(&mi
->email
);
183 strbuf_add(&mi
->email
, at
, el
);
184 strbuf_remove(&f
, at
- f
.buf
, el
+ (at
[el
] ? 1 : 0));
186 /* The remainder is name. It could be
188 * - "John Doe <john.doe@xz>" (a), or
189 * - "john.doe@xz (John Doe)" (b), or
190 * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
192 * but we have removed the email part, so
194 * - remove extra spaces which could stay after email (case 'c'), and
195 * - trim from both ends, possibly removing the () pair at the end
196 * (cases 'a' and 'b').
200 if (f
.buf
[0] == '(' && f
.len
&& f
.buf
[f
.len
- 1] == ')') {
201 strbuf_remove(&f
, 0, 1);
202 strbuf_setlen(&f
, f
.len
- 1);
205 get_sane_name(&mi
->name
, &f
, &mi
->email
);
210 static void handle_header(struct strbuf
**out
, const struct strbuf
*line
)
213 *out
= xmalloc(sizeof(struct strbuf
));
214 strbuf_init(*out
, line
->len
);
218 strbuf_addbuf(*out
, line
);
221 /* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
222 * to have enough heuristics to grok MIME encoded patches often found
223 * on our mailing lists. For example, we do not even treat header lines
224 * case insensitively.
227 static int slurp_attr(const char *line
, const char *name
, struct strbuf
*attr
)
229 const char *ends
, *ap
= strcasestr(line
, name
);
232 strbuf_setlen(attr
, 0);
242 sz
= strcspn(ap
, ends
);
243 strbuf_add(attr
, ap
, sz
);
247 static int has_attr_value(const char *line
, const char *name
, const char *value
)
249 struct strbuf sb
= STRBUF_INIT
;
250 int rc
= slurp_attr(line
, name
, &sb
) && !strcasecmp(sb
.buf
, value
);
255 static void handle_content_type(struct mailinfo
*mi
, struct strbuf
*line
)
257 struct strbuf
*boundary
= xmalloc(sizeof(struct strbuf
));
258 strbuf_init(boundary
, line
->len
);
260 mi
->format_flowed
= has_attr_value(line
->buf
, "format=", "flowed");
261 mi
->delsp
= has_attr_value(line
->buf
, "delsp=", "yes");
263 if (slurp_attr(line
->buf
, "boundary=", boundary
)) {
264 strbuf_insertstr(boundary
, 0, "--");
265 if (++mi
->content_top
>= &mi
->content
[MAX_BOUNDARIES
]) {
266 error("Too many boundaries to handle");
267 mi
->input_error
= -1;
268 mi
->content_top
= &mi
->content
[MAX_BOUNDARIES
] - 1;
271 *(mi
->content_top
) = boundary
;
274 slurp_attr(line
->buf
, "charset=", &mi
->charset
);
277 strbuf_release(boundary
);
282 static void handle_content_transfer_encoding(struct mailinfo
*mi
,
283 const struct strbuf
*line
)
285 if (strcasestr(line
->buf
, "base64"))
286 mi
->transfer_encoding
= TE_BASE64
;
287 else if (strcasestr(line
->buf
, "quoted-printable"))
288 mi
->transfer_encoding
= TE_QP
;
290 mi
->transfer_encoding
= TE_DONTCARE
;
293 static int is_multipart_boundary(struct mailinfo
*mi
, const struct strbuf
*line
)
295 struct strbuf
*content_top
= *(mi
->content_top
);
297 return ((content_top
->len
<= line
->len
) &&
298 !memcmp(line
->buf
, content_top
->buf
, content_top
->len
));
301 static void cleanup_subject(struct mailinfo
*mi
, struct strbuf
*subject
)
305 while (at
< subject
->len
) {
309 switch (subject
->buf
[at
]) {
311 if (subject
->len
<= at
+ 3)
313 if ((subject
->buf
[at
+ 1] == 'e' ||
314 subject
->buf
[at
+ 1] == 'E') &&
315 subject
->buf
[at
+ 2] == ':') {
316 strbuf_remove(subject
, at
, 3);
321 case ' ': case '\t': case ':':
322 strbuf_remove(subject
, at
, 1);
325 pos
= strchr(subject
->buf
+ at
, ']');
328 remove
= pos
- (subject
->buf
+ at
) + 1;
329 if (!mi
->keep_non_patch_brackets_in_subject
||
331 memmem(subject
->buf
+ at
, remove
, "PATCH", 5)))
332 strbuf_remove(subject
, at
, remove
);
336 * If the input had a space after the ], keep
337 * it. We don't bother with finding the end of
338 * the space, since we later normalize it
341 if (isspace(subject
->buf
[at
]))
348 strbuf_trim(subject
);
351 #define MAX_HDR_PARSED 10
352 static const char *header
[MAX_HDR_PARSED
] = {
353 "From","Subject","Date",
356 static inline int skip_header(const struct strbuf
*line
, const char *hdr
,
360 if (!skip_iprefix(line
->buf
, hdr
, &val
) ||
363 while (isspace(*val
))
369 static int is_format_patch_separator(const char *line
, int len
)
371 static const char SAMPLE
[] =
372 "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
375 if (len
!= strlen(SAMPLE
))
377 if (!skip_prefix(line
, "From ", &cp
))
379 if (strspn(cp
, "0123456789abcdef") != 40)
382 return !memcmp(SAMPLE
+ (cp
- line
), cp
, strlen(SAMPLE
) - (cp
- line
));
385 static struct strbuf
*decode_q_segment(const struct strbuf
*q_seg
, int rfc2047
)
387 const char *in
= q_seg
->buf
;
389 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
390 strbuf_init(out
, q_seg
->len
);
392 while ((c
= *in
++) != 0) {
396 break; /* drop trailing newline */
399 strbuf_addch(out
, ch
);
403 /* garbage -- fall through */
405 if (rfc2047
&& c
== '_') /* rfc2047 4.2 (2) */
407 strbuf_addch(out
, c
);
412 static struct strbuf
*decode_b_segment(const struct strbuf
*b_seg
)
414 /* Decode in..ep, possibly in-place to ot */
415 int c
, pos
= 0, acc
= 0;
416 const char *in
= b_seg
->buf
;
417 struct strbuf
*out
= xmalloc(sizeof(struct strbuf
));
418 strbuf_init(out
, b_seg
->len
);
420 while ((c
= *in
++) != 0) {
425 else if ('A' <= c
&& c
<= 'Z')
427 else if ('a' <= c
&& c
<= 'z')
429 else if ('0' <= c
&& c
<= '9')
432 continue; /* garbage */
438 strbuf_addch(out
, (acc
| (c
>> 4)));
442 strbuf_addch(out
, (acc
| (c
>> 2)));
446 strbuf_addch(out
, (acc
| c
));
454 static int convert_to_utf8(struct mailinfo
*mi
,
455 struct strbuf
*line
, const char *charset
)
460 if (!mi
->metainfo_charset
|| !charset
|| !*charset
)
463 if (same_encoding(mi
->metainfo_charset
, charset
))
465 out
= reencode_string_len(line
->buf
, line
->len
,
466 mi
->metainfo_charset
, charset
, &out_len
);
468 mi
->input_error
= -1;
469 return error("cannot convert from %s to %s",
470 charset
, mi
->metainfo_charset
);
472 strbuf_attach(line
, out
, out_len
, out_len
);
476 static void decode_header(struct mailinfo
*mi
, struct strbuf
*it
)
479 struct strbuf outbuf
= STRBUF_INIT
, *dec
;
480 struct strbuf charset_q
= STRBUF_INIT
, piecebuf
= STRBUF_INIT
;
481 int found_error
= 1; /* pessimism */
484 while (in
- it
->buf
<= it
->len
&& (ep
= strstr(in
, "=?")) != NULL
) {
486 strbuf_reset(&charset_q
);
487 strbuf_reset(&piecebuf
);
491 * We are about to process an encoded-word
492 * that begins at ep, but there is something
493 * before the encoded word.
496 for (scan
= in
; scan
< ep
; scan
++)
500 if (scan
!= ep
|| in
== it
->buf
) {
502 * We should not lose that "something",
503 * unless we have just processed an
504 * encoded-word, and there is only LWS
505 * before the one we are about to process.
507 strbuf_add(&outbuf
, in
, ep
- in
);
511 * ep : "=?iso-2022-jp?B?GyR...?= foo"
512 * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
516 if (ep
- it
->buf
>= it
->len
|| !(cp
= strchr(ep
, '?')))
519 if (cp
+ 3 - it
->buf
> it
->len
)
521 strbuf_add(&charset_q
, ep
, cp
- ep
);
524 if (!encoding
|| cp
[2] != '?')
526 ep
= strstr(cp
+ 3, "?=");
529 strbuf_add(&piecebuf
, cp
+ 3, ep
- cp
- 3);
530 switch (tolower(encoding
)) {
534 dec
= decode_b_segment(&piecebuf
);
537 dec
= decode_q_segment(&piecebuf
, 1);
540 if (convert_to_utf8(mi
, dec
, charset_q
.buf
))
543 strbuf_addbuf(&outbuf
, dec
);
548 strbuf_addstr(&outbuf
, in
);
550 strbuf_addbuf(it
, &outbuf
);
553 strbuf_release(&outbuf
);
554 strbuf_release(&charset_q
);
555 strbuf_release(&piecebuf
);
558 mi
->input_error
= -1;
562 * Returns true if "line" contains a header matching "hdr", in which case "val"
563 * will contain the value of the header with any RFC2047 B and Q encoding
564 * unwrapped, and optionally normalize the meta information to utf8.
566 static int parse_header(const struct strbuf
*line
,
573 if (!skip_header(line
, hdr
, &val_str
))
575 strbuf_addstr(val
, val_str
);
576 decode_header(mi
, val
);
580 static int check_header(struct mailinfo
*mi
,
581 const struct strbuf
*line
,
582 struct strbuf
*hdr_data
[], int overwrite
)
585 struct strbuf sb
= STRBUF_INIT
;
587 /* search for the interesting parts */
588 for (i
= 0; header
[i
]; i
++) {
589 if ((!hdr_data
[i
] || overwrite
) &&
590 parse_header(line
, header
[i
], mi
, &sb
)) {
591 handle_header(&hdr_data
[i
], &sb
);
593 goto check_header_out
;
598 if (parse_header(line
, "Content-Type", mi
, &sb
)) {
599 handle_content_type(mi
, &sb
);
601 goto check_header_out
;
603 if (parse_header(line
, "Content-Transfer-Encoding", mi
, &sb
)) {
604 handle_content_transfer_encoding(mi
, &sb
);
606 goto check_header_out
;
608 if (parse_header(line
, "Message-ID", mi
, &sb
)) {
609 if (mi
->add_message_id
)
610 mi
->message_id
= strbuf_detach(&sb
, NULL
);
612 goto check_header_out
;
621 * Returns 1 if the given line or any line beginning with the given line is an
622 * in-body header (that is, check_header will succeed when passed
625 static int is_inbody_header(const struct mailinfo
*mi
,
626 const struct strbuf
*line
)
630 for (i
= 0; header
[i
]; i
++)
631 if (!mi
->s_hdr_data
[i
] && skip_header(line
, header
[i
], &val
))
636 static void decode_transfer_encoding(struct mailinfo
*mi
, struct strbuf
*line
)
640 switch (mi
->transfer_encoding
) {
642 ret
= decode_q_segment(line
, 0);
645 ret
= decode_b_segment(line
);
652 strbuf_addbuf(line
, ret
);
657 static inline int patchbreak(const struct strbuf
*line
)
661 /* Beginning of a "diff -" header? */
662 if (starts_with(line
->buf
, "diff -"))
665 /* CVS "Index: " line? */
666 if (starts_with(line
->buf
, "Index: "))
670 * "--- <filename>" starts patches without headers
671 * "---<sp>*" is a manual separator
676 if (starts_with(line
->buf
, "---")) {
677 /* space followed by a filename? */
678 if (line
->buf
[3] == ' ' && !isspace(line
->buf
[4]))
680 /* Just whitespace? */
681 for (i
= 3; i
< line
->len
; i
++) {
682 unsigned char c
= line
->buf
[i
];
693 static int is_scissors_line(const char *line
)
696 int scissors
= 0, gap
= 0;
697 const char *first_nonblank
= NULL
, *last_nonblank
= NULL
;
698 int visible
, perforation
= 0, in_perforation
= 0;
700 for (c
= line
; *c
; c
++) {
702 if (in_perforation
) {
716 if (starts_with(c
, ">8") || starts_with(c
, "8<") ||
717 starts_with(c
, ">%") || starts_with(c
, "%<")) {
728 * The mark must be at least 8 bytes long (e.g. "-- >8 --").
729 * Even though there can be arbitrary cruft on the same line
730 * (e.g. "cut here"), in order to avoid misidentification, the
731 * perforation must occupy more than a third of the visible
732 * width of the line, and dashes and scissors must occupy more
733 * than half of the perforation.
736 if (first_nonblank
&& last_nonblank
)
737 visible
= last_nonblank
- first_nonblank
+ 1;
740 return (scissors
&& 8 <= visible
&&
741 visible
< perforation
* 3 &&
742 gap
* 2 < perforation
);
745 static void flush_inbody_header_accum(struct mailinfo
*mi
)
747 if (!mi
->inbody_header_accum
.len
)
749 if (!check_header(mi
, &mi
->inbody_header_accum
, mi
->s_hdr_data
, 0))
750 BUG("inbody_header_accum, if not empty, must always contain a valid in-body header");
751 strbuf_reset(&mi
->inbody_header_accum
);
754 static int check_inbody_header(struct mailinfo
*mi
, const struct strbuf
*line
)
756 if (mi
->inbody_header_accum
.len
&&
757 (line
->buf
[0] == ' ' || line
->buf
[0] == '\t')) {
758 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
760 * This is a scissors line; do not consider this line
761 * as a header continuation line.
763 flush_inbody_header_accum(mi
);
766 strbuf_strip_suffix(&mi
->inbody_header_accum
, "\n");
767 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
771 flush_inbody_header_accum(mi
);
773 if (starts_with(line
->buf
, ">From") && isspace(line
->buf
[5]))
774 return is_format_patch_separator(line
->buf
+ 1, line
->len
- 1);
775 if (starts_with(line
->buf
, "[PATCH]") && isspace(line
->buf
[7])) {
777 for (i
= 0; header
[i
]; i
++)
778 if (!strcmp("Subject", header
[i
])) {
779 handle_header(&mi
->s_hdr_data
[i
], line
);
784 if (is_inbody_header(mi
, line
)) {
785 strbuf_addbuf(&mi
->inbody_header_accum
, line
);
791 static int handle_commit_msg(struct mailinfo
*mi
, struct strbuf
*line
)
793 assert(!mi
->filter_stage
);
795 if (mi
->header_stage
) {
796 if (!line
->len
|| (line
->len
== 1 && line
->buf
[0] == '\n')) {
797 if (mi
->inbody_header_accum
.len
) {
798 flush_inbody_header_accum(mi
);
799 mi
->header_stage
= 0;
805 if (mi
->use_inbody_headers
&& mi
->header_stage
) {
806 mi
->header_stage
= check_inbody_header(mi
, line
);
807 if (mi
->header_stage
)
810 /* Only trim the first (blank) line of the commit message
811 * when ignoring in-body headers.
813 mi
->header_stage
= 0;
815 /* normalize the log message to UTF-8. */
816 if (convert_to_utf8(mi
, line
, mi
->charset
.buf
))
817 return 0; /* mi->input_error already set */
819 if (mi
->use_scissors
&& is_scissors_line(line
->buf
)) {
822 strbuf_setlen(&mi
->log_message
, 0);
823 mi
->header_stage
= 1;
826 * We may have already read "secondary headers"; purge
827 * them to give ourselves a clean restart.
829 for (i
= 0; header
[i
]; i
++) {
830 if (mi
->s_hdr_data
[i
])
831 strbuf_release(mi
->s_hdr_data
[i
]);
832 FREE_AND_NULL(mi
->s_hdr_data
[i
]);
837 if (patchbreak(line
)) {
839 strbuf_addf(&mi
->log_message
,
840 "Message-ID: %s\n", mi
->message_id
);
844 strbuf_addbuf(&mi
->log_message
, line
);
848 static void handle_patch(struct mailinfo
*mi
, const struct strbuf
*line
)
850 fwrite(line
->buf
, 1, line
->len
, mi
->patchfile
);
854 static void handle_filter(struct mailinfo
*mi
, struct strbuf
*line
)
856 switch (mi
->filter_stage
) {
858 if (!handle_commit_msg(mi
, line
))
863 handle_patch(mi
, line
);
868 static int is_rfc2822_header(const struct strbuf
*line
)
871 * The section that defines the loosest possible
872 * field name is "3.6.8 Optional fields".
874 * optional-field = field-name ":" unstructured CRLF
875 * field-name = 1*ftext
876 * ftext = %d33-57 / %59-126
879 char *cp
= line
->buf
;
881 /* Count mbox From headers as headers */
882 if (starts_with(cp
, "From ") || starts_with(cp
, ">From "))
885 while ((ch
= *cp
++)) {
888 if ((33 <= ch
&& ch
<= 57) ||
889 (59 <= ch
&& ch
<= 126))
896 static int read_one_header_line(struct strbuf
*line
, FILE *in
)
898 struct strbuf continuation
= STRBUF_INIT
;
900 /* Get the first part of the line. */
901 if (strbuf_getline_lf(line
, in
))
905 * Is it an empty line or not a valid rfc2822 header?
906 * If so, stop here, and return false ("not a header")
909 if (!line
->len
|| !is_rfc2822_header(line
)) {
910 /* Re-add the newline */
911 strbuf_addch(line
, '\n');
916 * Now we need to eat all the continuation lines..
917 * Yuck, 2822 header "folding"
926 if (peek
!= ' ' && peek
!= '\t')
928 if (strbuf_getline_lf(&continuation
, in
))
930 continuation
.buf
[0] = ' ';
931 strbuf_rtrim(&continuation
);
932 strbuf_addbuf(line
, &continuation
);
934 strbuf_release(&continuation
);
939 static int find_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
941 while (!strbuf_getline_lf(line
, mi
->input
)) {
942 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
))
948 static int handle_boundary(struct mailinfo
*mi
, struct strbuf
*line
)
950 struct strbuf newline
= STRBUF_INIT
;
952 strbuf_addch(&newline
, '\n');
954 if (line
->len
>= (*(mi
->content_top
))->len
+ 2 &&
955 !memcmp(line
->buf
+ (*(mi
->content_top
))->len
, "--", 2)) {
956 /* we hit an end boundary */
957 /* pop the current boundary off the stack */
958 strbuf_release(*(mi
->content_top
));
959 FREE_AND_NULL(*(mi
->content_top
));
961 /* technically won't happen as is_multipart_boundary()
962 will fail first. But just in case..
964 if (--mi
->content_top
< mi
->content
) {
965 error("Detected mismatched boundaries, can't recover");
966 mi
->input_error
= -1;
967 mi
->content_top
= mi
->content
;
968 strbuf_release(&newline
);
971 handle_filter(mi
, &newline
);
972 strbuf_release(&newline
);
976 /* skip to the next boundary */
977 if (!find_boundary(mi
, line
))
982 /* set some defaults */
983 mi
->transfer_encoding
= TE_DONTCARE
;
984 strbuf_reset(&mi
->charset
);
986 /* slurp in this section's info */
987 while (read_one_header_line(line
, mi
->input
))
988 check_header(mi
, line
, mi
->p_hdr_data
, 0);
990 strbuf_release(&newline
);
992 if (strbuf_getline_lf(line
, mi
->input
))
994 strbuf_addch(line
, '\n');
998 static void handle_filter_flowed(struct mailinfo
*mi
, struct strbuf
*line
,
1001 size_t len
= line
->len
;
1004 if (!mi
->format_flowed
) {
1006 line
->buf
[len
- 2] == '\r' &&
1007 line
->buf
[len
- 1] == '\n') {
1008 mi
->have_quoted_cr
= 1;
1009 if (mi
->quoted_cr
== quoted_cr_strip
) {
1010 strbuf_setlen(line
, len
- 2);
1011 strbuf_addch(line
, '\n');
1015 handle_filter(mi
, line
);
1019 if (line
->buf
[len
- 1] == '\n') {
1021 if (len
&& line
->buf
[len
- 1] == '\r')
1025 /* Keep signature separator as-is. */
1026 if (skip_prefix(line
->buf
, "-- ", &rest
) && rest
- line
->buf
== len
) {
1028 handle_filter(mi
, prev
);
1031 handle_filter(mi
, line
);
1035 /* Unstuff space-stuffed line. */
1036 if (len
&& line
->buf
[0] == ' ') {
1037 strbuf_remove(line
, 0, 1);
1041 /* Save flowed line for later, but without the soft line break. */
1042 if (len
&& line
->buf
[len
- 1] == ' ') {
1043 strbuf_add(prev
, line
->buf
, len
- !!mi
->delsp
);
1047 /* Prepend any previous partial lines */
1048 strbuf_insert(line
, 0, prev
->buf
, prev
->len
);
1051 handle_filter(mi
, line
);
1054 static void summarize_quoted_cr(struct mailinfo
*mi
)
1056 if (mi
->have_quoted_cr
&&
1057 mi
->quoted_cr
== quoted_cr_warn
)
1058 warning(_("quoted CRLF detected"));
1061 static void handle_body(struct mailinfo
*mi
, struct strbuf
*line
)
1063 struct strbuf prev
= STRBUF_INIT
;
1065 /* Skip up to the first boundary */
1066 if (*(mi
->content_top
)) {
1067 if (!find_boundary(mi
, line
))
1068 goto handle_body_out
;
1072 /* process any boundary lines */
1073 if (*(mi
->content_top
) && is_multipart_boundary(mi
, line
)) {
1074 /* flush any leftover */
1076 handle_filter(mi
, &prev
);
1077 strbuf_reset(&prev
);
1079 summarize_quoted_cr(mi
);
1080 mi
->have_quoted_cr
= 0;
1081 if (!handle_boundary(mi
, line
))
1082 goto handle_body_out
;
1085 /* Unwrap transfer encoding */
1086 decode_transfer_encoding(mi
, line
);
1088 switch (mi
->transfer_encoding
) {
1092 struct strbuf
**lines
, **it
, *sb
;
1094 /* Prepend any previous partial lines */
1095 strbuf_insert(line
, 0, prev
.buf
, prev
.len
);
1096 strbuf_reset(&prev
);
1099 * This is a decoded line that may contain
1100 * multiple new lines. Pass only one chunk
1101 * at a time to handle_filter()
1103 lines
= strbuf_split(line
, '\n');
1104 for (it
= lines
; (sb
= *it
); it
++) {
1105 if (!*(it
+ 1)) /* The last line */
1106 if (sb
->buf
[sb
->len
- 1] != '\n') {
1107 /* Partial line, save it for later. */
1108 strbuf_addbuf(&prev
, sb
);
1111 handle_filter_flowed(mi
, sb
, &prev
);
1114 * The partial chunk is saved in "prev" and will be
1115 * appended by the next iteration of read_line_with_nul().
1117 strbuf_list_free(lines
);
1121 handle_filter_flowed(mi
, line
, &prev
);
1124 if (mi
->input_error
)
1126 } while (!strbuf_getwholeline(line
, mi
->input
, '\n'));
1129 handle_filter(mi
, &prev
);
1130 summarize_quoted_cr(mi
);
1132 flush_inbody_header_accum(mi
);
1135 strbuf_release(&prev
);
1138 static void output_header_lines(FILE *fout
, const char *hdr
, const struct strbuf
*data
)
1140 const char *sp
= data
->buf
;
1142 char *ep
= strchr(sp
, '\n');
1148 fprintf(fout
, "%s: %.*s\n", hdr
, len
, sp
);
1155 static void handle_info(struct mailinfo
*mi
)
1160 for (i
= 0; header
[i
]; i
++) {
1161 /* only print inbody headers if we output a patch file */
1162 if (mi
->patch_lines
&& mi
->s_hdr_data
[i
])
1163 hdr
= mi
->s_hdr_data
[i
];
1164 else if (mi
->p_hdr_data
[i
])
1165 hdr
= mi
->p_hdr_data
[i
];
1169 if (memchr(hdr
->buf
, '\0', hdr
->len
)) {
1170 error("a NUL byte in '%s' is not allowed.", header
[i
]);
1171 mi
->input_error
= -1;
1174 if (!strcmp(header
[i
], "Subject")) {
1175 if (!mi
->keep_subject
) {
1176 cleanup_subject(mi
, hdr
);
1179 output_header_lines(mi
->output
, "Subject", hdr
);
1180 } else if (!strcmp(header
[i
], "From")) {
1182 handle_from(mi
, hdr
);
1183 fprintf(mi
->output
, "Author: %s\n", mi
->name
.buf
);
1184 fprintf(mi
->output
, "Email: %s\n", mi
->email
.buf
);
1187 fprintf(mi
->output
, "%s: %s\n", header
[i
], hdr
->buf
);
1190 fprintf(mi
->output
, "\n");
1193 int mailinfo(struct mailinfo
*mi
, const char *msg
, const char *patch
)
1197 struct strbuf line
= STRBUF_INIT
;
1199 cmitmsg
= fopen(msg
, "w");
1204 mi
->patchfile
= fopen(patch
, "w");
1205 if (!mi
->patchfile
) {
1211 mi
->p_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->p_hdr_data
)));
1212 mi
->s_hdr_data
= xcalloc(MAX_HDR_PARSED
, sizeof(*(mi
->s_hdr_data
)));
1215 peek
= fgetc(mi
->input
);
1218 return error("empty patch: '%s'", patch
);
1220 } while (isspace(peek
));
1221 ungetc(peek
, mi
->input
);
1223 /* process the email header */
1224 while (read_one_header_line(&line
, mi
->input
))
1225 check_header(mi
, &line
, mi
->p_hdr_data
, 1);
1227 handle_body(mi
, &line
);
1228 fwrite(mi
->log_message
.buf
, 1, mi
->log_message
.len
, cmitmsg
);
1230 fclose(mi
->patchfile
);
1233 strbuf_release(&line
);
1234 return mi
->input_error
;
1237 int mailinfo_parse_quoted_cr_action(const char *actionstr
, int *action
)
1239 if (!strcmp(actionstr
, "nowarn"))
1240 *action
= quoted_cr_nowarn
;
1241 else if (!strcmp(actionstr
, "warn"))
1242 *action
= quoted_cr_warn
;
1243 else if (!strcmp(actionstr
, "strip"))
1244 *action
= quoted_cr_strip
;
1250 static int git_mailinfo_config(const char *var
, const char *value
,
1251 const struct config_context
*ctx
, void *mi_
)
1253 struct mailinfo
*mi
= mi_
;
1255 if (!starts_with(var
, "mailinfo."))
1256 return git_default_config(var
, value
, ctx
, NULL
);
1257 if (!strcmp(var
, "mailinfo.scissors")) {
1258 mi
->use_scissors
= git_config_bool(var
, value
);
1261 if (!strcmp(var
, "mailinfo.quotedcr")) {
1263 return config_error_nonbool(var
);
1264 if (mailinfo_parse_quoted_cr_action(value
, &mi
->quoted_cr
) != 0)
1265 return error(_("bad action '%s' for '%s'"), value
, var
);
1268 /* perhaps others here */
1272 void setup_mailinfo(struct mailinfo
*mi
)
1274 memset(mi
, 0, sizeof(*mi
));
1275 strbuf_init(&mi
->name
, 0);
1276 strbuf_init(&mi
->email
, 0);
1277 strbuf_init(&mi
->charset
, 0);
1278 strbuf_init(&mi
->log_message
, 0);
1279 strbuf_init(&mi
->inbody_header_accum
, 0);
1280 mi
->quoted_cr
= quoted_cr_warn
;
1281 mi
->header_stage
= 1;
1282 mi
->use_inbody_headers
= 1;
1283 mi
->content_top
= mi
->content
;
1284 git_config(git_mailinfo_config
, mi
);
1287 void clear_mailinfo(struct mailinfo
*mi
)
1289 strbuf_release(&mi
->name
);
1290 strbuf_release(&mi
->email
);
1291 strbuf_release(&mi
->charset
);
1292 strbuf_release(&mi
->inbody_header_accum
);
1293 free(mi
->message_id
);
1295 strbuf_list_free(mi
->p_hdr_data
);
1296 strbuf_list_free(mi
->s_hdr_data
);
1298 while (mi
->content
< mi
->content_top
) {
1299 free(*(mi
->content_top
));
1303 strbuf_release(&mi
->log_message
);