Skip to content

Commit 269e239

Browse files
committed
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 <[email protected]>
1 parent c1b40bd commit 269e239

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

builtin/mailinfo.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -643,27 +643,25 @@ static int is_scissors_line(const struct strbuf *line)
643643
gap * 2 < perforation);
644644
}
645645

646-
static int handle_commit_msg(struct strbuf *line)
646+
static int handle_commit_msg(struct strbuf *line, int *still_looking)
647647
{
648-
static int still_looking = 1;
649-
650648
if (!cmitmsg)
651649
return 0;
652650

653-
if (still_looking) {
651+
if (*still_looking) {
654652
if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
655653
return 0;
656654
}
657655

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)
661659
return 0;
662660
} else
663661
/* Only trim the first (blank) line of the commit message
664662
* when ignoring in-body headers.
665663
*/
666-
still_looking = 0;
664+
*still_looking = 0;
667665

668666
/* normalize the log message to UTF-8. */
669667
if (metainfo_charset)
@@ -675,7 +673,7 @@ static int handle_commit_msg(struct strbuf *line)
675673
die_errno("Could not rewind output message file");
676674
if (ftruncate(fileno(cmitmsg), 0))
677675
die_errno("Could not truncate output message file at scissors");
678-
still_looking = 1;
676+
*still_looking = 1;
679677

680678
/*
681679
* We may have already read "secondary headers"; purge
@@ -707,16 +705,13 @@ static void handle_patch(const struct strbuf *line)
707705
patch_lines++;
708706
}
709707

710-
static void handle_filter(struct strbuf *line)
708+
static void handle_filter(struct strbuf *line, int *filter_stage, int *header_stage)
711709
{
712-
static int filter = 0;
713-
714-
/* filter tells us which part we left off on */
715-
switch (filter) {
710+
switch (*filter_stage) {
716711
case 0:
717-
if (!handle_commit_msg(line))
712+
if (!handle_commit_msg(line, header_stage))
718713
break;
719-
filter++;
714+
(*filter_stage)++;
720715
case 1:
721716
handle_patch(line);
722717
break;
@@ -800,7 +795,7 @@ static int find_boundary(void)
800795
return 0;
801796
}
802797

803-
static int handle_boundary(void)
798+
static int handle_boundary(int *filter_stage, int *header_stage)
804799
{
805800
struct strbuf newline = STRBUF_INIT;
806801

@@ -822,7 +817,7 @@ static int handle_boundary(void)
822817
"can't recover\n");
823818
exit(1);
824819
}
825-
handle_filter(&newline);
820+
handle_filter(&newline, filter_stage, header_stage);
826821
strbuf_release(&newline);
827822

828823
/* skip to the next boundary */
@@ -850,6 +845,8 @@ static int handle_boundary(void)
850845
static void handle_body(void)
851846
{
852847
struct strbuf prev = STRBUF_INIT;
848+
int filter_stage = 0;
849+
int header_stage = 1;
853850

854851
/* Skip up to the first boundary */
855852
if (*content_top) {
@@ -862,10 +859,10 @@ static void handle_body(void)
862859
if (*content_top && is_multipart_boundary(&line)) {
863860
/* flush any leftover */
864861
if (prev.len) {
865-
handle_filter(&prev);
862+
handle_filter(&prev, &filter_stage, &header_stage);
866863
strbuf_reset(&prev);
867864
}
868-
if (!handle_boundary())
865+
if (!handle_boundary(&filter_stage, &header_stage))
869866
goto handle_body_out;
870867
}
871868

@@ -895,7 +892,7 @@ static void handle_body(void)
895892
strbuf_addbuf(&prev, sb);
896893
break;
897894
}
898-
handle_filter(sb);
895+
handle_filter(sb, &filter_stage, &header_stage);
899896
}
900897
/*
901898
* The partial chunk is saved in "prev" and will be
@@ -905,7 +902,7 @@ static void handle_body(void)
905902
break;
906903
}
907904
default:
908-
handle_filter(&line);
905+
handle_filter(&line, &filter_stage, &header_stage);
909906
}
910907

911908
} while (!strbuf_getwholeline(&line, fin, '\n'));

0 commit comments

Comments
 (0)