Skip to content

Commit 13c6df2

Browse files
committed
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 <[email protected]>
1 parent 173aef7 commit 13c6df2

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

builtin/mailinfo.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ struct mailinfo {
1919
struct strbuf email;
2020
int keep_subject;
2121
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? */
2225
};
2326
static char *message_id;
2427

@@ -648,25 +651,25 @@ static int is_scissors_line(const struct strbuf *line)
648651
gap * 2 < perforation);
649652
}
650653

651-
static int handle_commit_msg(struct strbuf *line, int *still_looking)
654+
static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
652655
{
653656
if (!cmitmsg)
654657
return 0;
655658

656-
if (*still_looking) {
659+
if (mi->header_stage) {
657660
if (!line->len || (line->len == 1 && line->buf[0] == '\n'))
658661
return 0;
659662
}
660663

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)
664667
return 0;
665668
} else
666669
/* Only trim the first (blank) line of the commit message
667670
* when ignoring in-body headers.
668671
*/
669-
*still_looking = 0;
672+
mi->header_stage = 0;
670673

671674
/* normalize the log message to UTF-8. */
672675
if (metainfo_charset)
@@ -678,7 +681,7 @@ static int handle_commit_msg(struct strbuf *line, int *still_looking)
678681
die_errno("Could not rewind output message file");
679682
if (ftruncate(fileno(cmitmsg), 0))
680683
die_errno("Could not truncate output message file at scissors");
681-
*still_looking = 1;
684+
mi->header_stage = 1;
682685

683686
/*
684687
* We may have already read "secondary headers"; purge
@@ -710,13 +713,13 @@ static void handle_patch(const struct strbuf *line)
710713
patch_lines++;
711714
}
712715

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)
714717
{
715-
switch (*filter_stage) {
718+
switch (mi->filter_stage) {
716719
case 0:
717-
if (!handle_commit_msg(line, header_stage))
720+
if (!handle_commit_msg(mi, line))
718721
break;
719-
(*filter_stage)++;
722+
mi->filter_stage++;
720723
case 1:
721724
handle_patch(line);
722725
break;
@@ -800,8 +803,7 @@ static int find_boundary(struct mailinfo *mi, struct strbuf *line)
800803
return 0;
801804
}
802805

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)
805807
{
806808
struct strbuf newline = STRBUF_INIT;
807809

@@ -823,7 +825,7 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line,
823825
"can't recover\n");
824826
exit(1);
825827
}
826-
handle_filter(&newline, filter_stage, header_stage);
828+
handle_filter(mi, &newline);
827829
strbuf_release(&newline);
828830

829831
/* skip to the next boundary */
@@ -851,8 +853,6 @@ static int handle_boundary(struct mailinfo *mi, struct strbuf *line,
851853
static void handle_body(struct mailinfo *mi, struct strbuf *line)
852854
{
853855
struct strbuf prev = STRBUF_INIT;
854-
int filter_stage = 0;
855-
int header_stage = 1;
856856

857857
/* Skip up to the first boundary */
858858
if (*content_top) {
@@ -865,10 +865,10 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
865865
if (*content_top && is_multipart_boundary(line)) {
866866
/* flush any leftover */
867867
if (prev.len) {
868-
handle_filter(&prev, &filter_stage, &header_stage);
868+
handle_filter(mi, &prev);
869869
strbuf_reset(&prev);
870870
}
871-
if (!handle_boundary(mi, line, &filter_stage, &header_stage))
871+
if (!handle_boundary(mi, line))
872872
goto handle_body_out;
873873
}
874874

@@ -898,7 +898,7 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
898898
strbuf_addbuf(&prev, sb);
899899
break;
900900
}
901-
handle_filter(sb, &filter_stage, &header_stage);
901+
handle_filter(mi, sb);
902902
}
903903
/*
904904
* The partial chunk is saved in "prev" and will be
@@ -908,7 +908,7 @@ static void handle_body(struct mailinfo *mi, struct strbuf *line)
908908
break;
909909
}
910910
default:
911-
handle_filter(line, &filter_stage, &header_stage);
911+
handle_filter(mi, line);
912912
}
913913

914914
} while (!strbuf_getwholeline(line, mi->input, '\n'));
@@ -1021,6 +1021,7 @@ static void setup_mailinfo(struct mailinfo *mi)
10211021
memset(mi, 0, sizeof(*mi));
10221022
strbuf_init(&mi->name, 0);
10231023
strbuf_init(&mi->email, 0);
1024+
mi->header_stage = 1;
10241025
git_config(git_mailinfo_config, &mi);
10251026
}
10261027

0 commit comments

Comments
 (0)