mailinfo: move filter/header stage to struct mailinfo

Earlier we got rid of two function-scope static variables that kept track of the states of helper functions by making them extra arguments that are passed throughout the callchain. Now we have a convenient place to store and pass them around in the form of "struct mailinfo", change them into two fields in the struct. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 14, 2015 at 16:13 UTC 13c6df2642343ec3d49e17082f38f191f175c742
1 file changed +21 -20
builtin/mailinfo.c
+21 -20
@@ -19,6 +19,9 @@ struct mailinfo {
19 struct strbuf email;
20 int keep_subject;
21 int keep_non_patch_brackets_in_subject;
22 +
23 + int filter_stage; /* still reading log or are we copying patch? */
24 + int header_stage; /* still checking in-body headers? */
25 };
26 static char *message_id;
27
@@ -648,25 +651,25 @@ static int is_scissors_line(const struct strbuf *line)
651 gap * 2 < perforation);
652 }
653
651 -static int handle_commit_msg(struct strbuf *line, int *still_looking)
654 +static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
655 {
656 if (!cmitmsg)
657 return 0;
658
656 - if (*still_looking) {
659 + if (mi->header_stage) {
660 if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
661 return 0;
662 }
663
661 - if (use_inbody_headers && *still_looking) {
662 - *still_looking = check_header(line, s_hdr_data, 0);
663 - if (*still_looking)
664 + if (use_inbody_headers && mi->header_stage) {
665 + mi->header_stage = check_header(line, s_hdr_data, 0);
666 + if (mi->header_stage)
667 return 0;
668 } else
669 /* Only trim the first (blank) line of the commit message
670 * when ignoring in-body headers.
671 */
669 - *still_looking = 0;
672 + mi->header_stage = 0;
673
674 /* normalize the log message to UTF-8. */
675 if (metainfo_charset)
@@ -678,7 +681,7 @@ static int handle_commit_msg(struct strbuf *line, int *still_looking)
681 die_errno("Could not rewind output message file");
682 if (ftruncate(fileno(cmitmsg), 0))
683 die_errno("Could not truncate output message file at scissors");
681 - *still_looking = 1;
684 + mi->header_stage = 1;
685
686 /*
687 * We may have already read "secondary headers"; purge
@@ -710,13 +713,13 @@ static void handle_patch(const struct strbuf *line)
713 patch_lines++;
714 }
715
713 -static void handle_filter(struct strbuf *line, int *filter_stage, int *header_stage)
716 +static void handle_filter(struct mailinfo *mi, struct strbuf *line)
717 {
715 - switch (*filter_stage) {
718 + switch (mi->filter_stage) {
719 case 0:
717 - if (!handle_commit_msg(line, header_stage))
720 + if (!handle_commit_msg(mi, line))
721 break;
719 - (*filter_stage)++;
722 + mi->filter_stage++;
723 case 1:
724 handle_patch(line);
725 break;
@@ -800,8 +803,7 @@ static int find_boundary(struct mailinfo *mi, struct strbuf *line)
803 return 0;
804 }
805
803 -static int handle_boundary(struct mailinfo *mi, struct strbuf *line,
804 - int *filter_stage, int *header_stage)
806 +static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
807 {
808 struct strbuf newline = STRBUF_INIT;
809
@@ -823,7 +825,7 @@ again:
825 "can't recover\n");
826 exit(1);
827 }
826 - handle_filter(&newline, filter_stage, header_stage);
828 + handle_filter(mi, &newline);
829 strbuf_release(&newline);
830
831 /* skip to the next boundary */
@@ -851,8 +853,6 @@ again:
853 static void handle_body(struct mailinfo *mi, struct strbuf *line)
854 {
855 struct strbuf prev = STRBUF_INIT;
854 - int filter_stage = 0;
855 - int header_stage = 1;
856
857 /* Skip up to the first boundary */
858 if (*content_top) {
@@ -865,10 +865,10 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
865 if (*content_top && is_multipart_boundary(line)) {
866 /* flush any leftover */
867 if (prev.len) {
868 - handle_filter(&prev, &filter_stage, &header_stage);
868 + handle_filter(mi, &prev);
869 strbuf_reset(&prev);
870 }
871 - if (!handle_boundary(mi, line, &filter_stage, &header_stage))
871 + if (!handle_boundary(mi, line))
872 goto handle_body_out;
873 }
874
@@ -898,7 +898,7 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
898 strbuf_addbuf(&prev, sb);
899 break;
900 }
901 - handle_filter(sb, &filter_stage, &header_stage);
901 + handle_filter(mi, sb);
902 }
903 /*
904 * The partial chunk is saved in "prev" and will be
@@ -908,7 +908,7 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
908 break;
909 }
910 default:
911 - handle_filter(line, &filter_stage, &header_stage);
911 + handle_filter(mi, line);
912 }
913
914 } while (!strbuf_getwholeline(line, mi->input, '\n'));
@@ -1021,6 +1021,7 @@ static void setup_mailinfo(struct mailinfo *mi)
1021 memset(mi, 0, sizeof(*mi));
1022 strbuf_init(&mi->name, 0);
1023 strbuf_init(&mi->email, 0);
1024 + mi->header_stage = 1;
1025 git_config(git_mailinfo_config, &mi);
1026 }
1027