Skip to content

Commit c69f239

Browse files
committed
mailinfo: introduce "struct mailinfo" to hold globals
In this first step, move only 'email' and 'name' fields in there and remove the corresponding globals. In subsequent patches, more globals will be moved to this and the structure will be passed around as a new parameter to more functions. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6e21b50 commit c69f239

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

builtin/mailinfo.c

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ static FILE *cmitmsg, *patchfile, *fin, *fout;
1212
static int keep_subject;
1313
static int keep_non_patch_brackets_in_subject;
1414
static const char *metainfo_charset;
15-
static struct strbuf name = STRBUF_INIT;
16-
static struct strbuf email = STRBUF_INIT;
15+
16+
struct mailinfo {
17+
struct strbuf name;
18+
struct strbuf email;
19+
};
1720
static char *message_id;
1821

1922
static enum {
@@ -53,15 +56,15 @@ static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf
5356
strbuf_addbuf(out, src);
5457
}
5558

56-
static void parse_bogus_from(const struct strbuf *line)
59+
static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
5760
{
5861
/* John Doe <johndoe> */
5962

6063
char *bra, *ket;
6164
/* This is fallback, so do not bother if we already have an
6265
* e-mail address.
6366
*/
64-
if (email.len)
67+
if (mi->email.len)
6568
return;
6669

6770
bra = strchr(line->buf, '<');
@@ -71,16 +74,16 @@ static void parse_bogus_from(const struct strbuf *line)
7174
if (!ket)
7275
return;
7376

74-
strbuf_reset(&email);
75-
strbuf_add(&email, bra + 1, ket - bra - 1);
77+
strbuf_reset(&mi->email);
78+
strbuf_add(&mi->email, bra + 1, ket - bra - 1);
7679

77-
strbuf_reset(&name);
78-
strbuf_add(&name, line->buf, bra - line->buf);
79-
strbuf_trim(&name);
80-
get_sane_name(&name, &name, &email);
80+
strbuf_reset(&mi->name);
81+
strbuf_add(&mi->name, line->buf, bra - line->buf);
82+
strbuf_trim(&mi->name);
83+
get_sane_name(&mi->name, &mi->name, &mi->email);
8184
}
8285

83-
static void handle_from(const struct strbuf *from)
86+
static void handle_from(struct mailinfo *mi, const struct strbuf *from)
8487
{
8588
char *at;
8689
size_t el;
@@ -91,14 +94,14 @@ static void handle_from(const struct strbuf *from)
9194

9295
at = strchr(f.buf, '@');
9396
if (!at) {
94-
parse_bogus_from(from);
97+
parse_bogus_from(mi, from);
9598
return;
9699
}
97100

98101
/*
99102
* If we already have one email, don't take any confusing lines
100103
*/
101-
if (email.len && strchr(at + 1, '@')) {
104+
if (mi->email.len && strchr(at + 1, '@')) {
102105
strbuf_release(&f);
103106
return;
104107
}
@@ -117,8 +120,8 @@ static void handle_from(const struct strbuf *from)
117120
at--;
118121
}
119122
el = strcspn(at, " \n\t\r\v\f>");
120-
strbuf_reset(&email);
121-
strbuf_add(&email, at, el);
123+
strbuf_reset(&mi->email);
124+
strbuf_add(&mi->email, at, el);
122125
strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
123126

124127
/* The remainder is name. It could be
@@ -140,7 +143,7 @@ static void handle_from(const struct strbuf *from)
140143
strbuf_setlen(&f, f.len - 1);
141144
}
142145

143-
get_sane_name(&name, &f, &email);
146+
get_sane_name(&mi->name, &f, &mi->email);
144147
strbuf_release(&f);
145148
}
146149

@@ -927,7 +930,7 @@ static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf
927930
}
928931
}
929932

930-
static void handle_info(void)
933+
static void handle_info(struct mailinfo *mi)
931934
{
932935
struct strbuf *hdr;
933936
int i;
@@ -949,9 +952,9 @@ static void handle_info(void)
949952
output_header_lines(fout, "Subject", hdr);
950953
} else if (!strcmp(header[i], "From")) {
951954
cleanup_space(hdr);
952-
handle_from(hdr);
953-
fprintf(fout, "Author: %s\n", name.buf);
954-
fprintf(fout, "Email: %s\n", email.buf);
955+
handle_from(mi, hdr);
956+
fprintf(fout, "Author: %s\n", mi->name.buf);
957+
fprintf(fout, "Email: %s\n", mi->email.buf);
955958
} else {
956959
cleanup_space(hdr);
957960
fprintf(fout, "%s: %s\n", header[i], hdr->buf);
@@ -960,7 +963,8 @@ static void handle_info(void)
960963
fprintf(fout, "\n");
961964
}
962965

963-
static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch)
966+
static int mailinfo(struct mailinfo *mi,
967+
FILE *in, FILE *out, const char *msg, const char *patch)
964968
{
965969
int peek;
966970
struct strbuf line = STRBUF_INIT;
@@ -995,7 +999,7 @@ static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch)
995999
handle_body(&line);
9961000
fclose(patchfile);
9971001

998-
handle_info();
1002+
handle_info(mi);
9991003
strbuf_release(&line);
10001004
return 0;
10011005
}
@@ -1012,17 +1016,33 @@ static int git_mailinfo_config(const char *var, const char *value, void *unused)
10121016
return 0;
10131017
}
10141018

1019+
static void setup_mailinfo(struct mailinfo *mi)
1020+
{
1021+
memset(mi, 0, sizeof(*mi));
1022+
strbuf_init(&mi->name, 0);
1023+
strbuf_init(&mi->email, 0);
1024+
git_config(git_mailinfo_config, &mi);
1025+
}
1026+
1027+
static void clear_mailinfo(struct mailinfo *mi)
1028+
{
1029+
strbuf_release(&mi->name);
1030+
strbuf_release(&mi->email);
1031+
}
1032+
10151033
static const char mailinfo_usage[] =
10161034
"git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
10171035

10181036
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
10191037
{
10201038
const char *def_charset;
1039+
struct mailinfo mi;
1040+
int status;
10211041

10221042
/* NEEDSWORK: might want to do the optional .git/ directory
10231043
* discovery
10241044
*/
1025-
git_config(git_mailinfo_config, NULL);
1045+
setup_mailinfo(&mi);
10261046

10271047
def_charset = get_commit_output_encoding();
10281048
metainfo_charset = def_charset;
@@ -1054,5 +1074,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
10541074
if (argc != 3)
10551075
usage(mailinfo_usage);
10561076

1057-
return !!mailinfo(stdin, stdout, argv[1], argv[2]);
1077+
status = !!mailinfo(&mi, stdin, stdout, argv[1], argv[2]);
1078+
clear_mailinfo(&mi);
1079+
1080+
return status;
10581081
}

0 commit comments

Comments
 (0)