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