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 <gitster@pobox.com>

Junio C Hamano committed Oct 18, 2015 at 22:22 UTC c69f2395ba6f4bc96542d6d52987989992331226
1 file changed +47 -24
builtin/mailinfo.c
+47 -24
@@ -12,8 +12,11 @@ static FILE *cmitmsg, *patchfile, *fin, *fout;
12 static int keep_subject;
13 static int keep_non_patch_brackets_in_subject;
14 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 +};
20 static char *message_id;
21
22 static enum {
@@ -53,7 +56,7 @@ static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf
56 strbuf_addbuf(out, src);
57 }
58
56 -static void parse_bogus_from(const struct strbuf *line)
59 +static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
60 {
61 /* John Doe <johndoe> */
62
@@ -61,7 +64,7 @@ static void parse_bogus_from(const struct strbuf *line)
64 /* This is fallback, so do not bother if we already have an
65 * e-mail address.
66 */
64 - if (email.len)
67 + if (mi->email.len)
68 return;
69
70 bra = strchr(line->buf, '<');
@@ -71,16 +74,16 @@ static void parse_bogus_from(const struct strbuf *line)
74 if (!ket)
75 return;
76
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);
79
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);
84 }
85
83 -static void handle_from(const struct strbuf *from)
86 +static void handle_from(struct mailinfo *mi, const struct strbuf *from)
87 {
88 char *at;
89 size_t el;
@@ -91,14 +94,14 @@ static void handle_from(const struct strbuf *from)
94
95 at = strchr(f.buf, '@');
96 if (!at) {
94 - parse_bogus_from(from);
97 + parse_bogus_from(mi, from);
98 return;
99 }
100
101 /*
102 * If we already have one email, don't take any confusing lines
103 */
101 - if (email.len && strchr(at + 1, '@')) {
104 + if (mi->email.len && strchr(at + 1, '@')) {
105 strbuf_release(&f);
106 return;
107 }
@@ -117,8 +120,8 @@ static void handle_from(const struct strbuf *from)
120 at--;
121 }
122 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);
125 strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
126
127 /* The remainder is name. It could be
@@ -140,7 +143,7 @@ static void handle_from(const struct strbuf *from)
143 strbuf_setlen(&f, f.len - 1);
144 }
145
143 - get_sane_name(&name, &f, &email);
146 + get_sane_name(&mi->name, &f, &mi->email);
147 strbuf_release(&f);
148 }
149
@@ -927,7 +930,7 @@ static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf
930 }
931 }
932
930 -static void handle_info(void)
933 +static void handle_info(struct mailinfo *mi)
934 {
935 struct strbuf *hdr;
936 int i;
@@ -949,9 +952,9 @@ static void handle_info(void)
952 output_header_lines(fout, "Subject", hdr);
953 } else if (!strcmp(header[i], "From")) {
954 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);
958 } else {
959 cleanup_space(hdr);
960 fprintf(fout, "%s: %s\n", header[i], hdr->buf);
@@ -960,7 +963,8 @@ static void handle_info(void)
963 fprintf(fout, "\n");
964 }
965
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)
968 {
969 int peek;
970 struct strbuf line = STRBUF_INIT;
@@ -995,7 +999,7 @@ static int mailinfo(FILE *in, FILE *out, const char *msg, const char *patch)
999 handle_body(&line);
1000 fclose(patchfile);
1001
998 - handle_info();
1002 + handle_info(mi);
1003 strbuf_release(&line);
1004 return 0;
1005 }
@@ -1012,17 +1016,33 @@ static int git_mailinfo_config(const char *var, const char *value, void *unused)
1016 return 0;
1017 }
1018
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 +
1033 static const char mailinfo_usage[] =
1034 "git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
1035
1036 int cmd_mailinfo(int argc, const char **argv, const char *prefix)
1037 {
1038 const char *def_charset;
1039 + struct mailinfo mi;
1040 + int status;
1041
1042 /* NEEDSWORK: might want to do the optional .git/ directory
1043 * discovery
1044 */
1025 - git_config(git_mailinfo_config, NULL);
1045 + setup_mailinfo(&mi);
1046
1047 def_charset = get_commit_output_encoding();
1048 metainfo_charset = def_charset;
@@ -1054,5 +1074,8 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
1074 if (argc != 3)
1075 usage(mailinfo_usage);
1076
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;
1081 }