mailinfo: get rid of function-local static states

Two helper functions use "static int" in their scope to keep track of the state while repeatedly getting called once for each input line. Move these state variables to their ultimate caller and pass down pointers to them along the callchain, as a small step in preparation for making this entire callchain more reentrant. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 13, 2015 at 11:13 UTC 269e239c48ac8f70248beb4539535af0ed930682
1 file changed +19 -22
builtin/mailinfo.c
+19 -22
@@ -643,27 +643,25 @@ static int is_scissors_line(const struct strbuf *line)
643 gap * 2 < perforation);
644 }
645
646 -static int handle_commit_msg(struct strbuf *line)
646 +static int handle_commit_msg(struct strbuf *line, int *still_looking)
647 {
648 - static int still_looking = 1;
649 -
648 if (!cmitmsg)
649 return 0;
650
653 - if (still_looking) {
651 + if (*still_looking) {
652 if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
653 return 0;
654 }
655
658 - if (use_inbody_headers && still_looking) {
659 - still_looking = check_header(line, s_hdr_data, 0);
660 - if (still_looking)
656 + if (use_inbody_headers && *still_looking) {
657 + *still_looking = check_header(line, s_hdr_data, 0);
658 + if (*still_looking)
659 return 0;
660 } else
661 /* Only trim the first (blank) line of the commit message
662 * when ignoring in-body headers.
663 */
666 - still_looking = 0;
664 + *still_looking = 0;
665
666 /* normalize the log message to UTF-8. */
667 if (metainfo_charset)
@@ -675,7 +673,7 @@ static int handle_commit_msg(struct strbuf *line)
673 die_errno("Could not rewind output message file");
674 if (ftruncate(fileno(cmitmsg), 0))
675 die_errno("Could not truncate output message file at scissors");
678 - still_looking = 1;
676 + *still_looking = 1;
677
678 /*
679 * We may have already read "secondary headers"; purge
@@ -707,16 +705,13 @@ static void handle_patch(const struct strbuf *line)
705 patch_lines++;
706 }
707
710 -static void handle_filter(struct strbuf *line)
708 +static void handle_filter(struct strbuf *line, int *filter_stage, int *header_stage)
709 {
712 - static int filter = 0;
713 -
714 - /* filter tells us which part we left off on */
715 - switch (filter) {
710 + switch (*filter_stage) {
711 case 0:
717 - if (!handle_commit_msg(line))
712 + if (!handle_commit_msg(line, header_stage))
713 break;
719 - filter++;
714 + (*filter_stage)++;
715 case 1:
716 handle_patch(line);
717 break;
@@ -800,7 +795,7 @@ static int find_boundary(void)
795 return 0;
796 }
797
803 -static int handle_boundary(void)
798 +static int handle_boundary(int *filter_stage, int *header_stage)
799 {
800 struct strbuf newline = STRBUF_INIT;
801
@@ -822,7 +817,7 @@ again:
817 "can't recover\n");
818 exit(1);
819 }
825 - handle_filter(&newline);
820 + handle_filter(&newline, filter_stage, header_stage);
821 strbuf_release(&newline);
822
823 /* skip to the next boundary */
@@ -850,6 +845,8 @@ again:
845 static void handle_body(void)
846 {
847 struct strbuf prev = STRBUF_INIT;
848 + int filter_stage = 0;
849 + int header_stage = 1;
850
851 /* Skip up to the first boundary */
852 if (*content_top) {
@@ -862,10 +859,10 @@ static void handle_body(void)
859 if (*content_top && is_multipart_boundary(&line)) {
860 /* flush any leftover */
861 if (prev.len) {
865 - handle_filter(&prev);
862 + handle_filter(&prev, &filter_stage, &header_stage);
863 strbuf_reset(&prev);
864 }
868 - if (!handle_boundary())
865 + if (!handle_boundary(&filter_stage, &header_stage))
866 goto handle_body_out;
867 }
868
@@ -895,7 +892,7 @@ static void handle_body(void)
892 strbuf_addbuf(&prev, sb);
893 break;
894 }
898 - handle_filter(sb);
895 + handle_filter(sb, &filter_stage, &header_stage);
896 }
897 /*
898 * The partial chunk is saved in "prev" and will be
@@ -905,7 +902,7 @@ static void handle_body(void)
902 break;
903 }
904 default:
908 - handle_filter(&line);
905 + handle_filter(&line, &filter_stage, &header_stage);
906 }
907
908 } while (!strbuf_getwholeline(&line, fin, '\n'));