Skip to content

Commit d582992

Browse files
sgngitster
authored andcommitted
mailinfo: load default metainfo_charset lazily
In a later change, we will use parse_option to parse mailinfo's options. In mailinfo, both "-u", "-n", and "--encoding" try to set the same field, with "-u" reset that field to some default value from configuration variable "i18n.commitEncoding". Let's delay the setting of that field until we finish processing all options. By doing that, "i18n.commitEncoding" can be parsed on demand. More importantly, it cleans the way for using parse_option. This change introduces some inconsistent brackets "{}" in "if/else if" construct, however, we will rewrite them in the next few changes. Signed-off-by: Đoàn Trần Công Danh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7e39198 commit d582992

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

builtin/mailinfo.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@
1111
static const char mailinfo_usage[] =
1212
"git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
1313

14+
struct metainfo_charset
15+
{
16+
enum {
17+
CHARSET_DEFAULT,
18+
CHARSET_NO_REENCODE,
19+
CHARSET_EXPLICIT,
20+
} policy;
21+
const char *charset;
22+
};
23+
1424
int cmd_mailinfo(int argc, const char **argv, const char *prefix)
1525
{
16-
const char *def_charset;
26+
struct metainfo_charset meta_charset;
1727
struct mailinfo mi;
1828
int status;
1929
char *msgfile, *patchfile;
2030

2131
setup_mailinfo(&mi);
22-
23-
def_charset = get_commit_output_encoding();
24-
mi.metainfo_charset = def_charset;
32+
meta_charset.policy = CHARSET_DEFAULT;
2533

2634
while (1 < argc && argv[1][0] == '-') {
2735
if (!strcmp(argv[1], "-k"))
@@ -31,12 +39,13 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
3139
else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
3240
mi.add_message_id = 1;
3341
else if (!strcmp(argv[1], "-u"))
34-
mi.metainfo_charset = def_charset;
42+
meta_charset.policy = CHARSET_DEFAULT;
3543
else if (!strcmp(argv[1], "-n"))
36-
mi.metainfo_charset = NULL;
37-
else if (starts_with(argv[1], "--encoding="))
38-
mi.metainfo_charset = argv[1] + 11;
39-
else if (!strcmp(argv[1], "--scissors"))
44+
meta_charset.policy = CHARSET_NO_REENCODE;
45+
else if (starts_with(argv[1], "--encoding=")) {
46+
meta_charset.policy = CHARSET_EXPLICIT;
47+
meta_charset.charset = argv[1] + 11;
48+
} else if (!strcmp(argv[1], "--scissors"))
4049
mi.use_scissors = 1;
4150
else if (!strcmp(argv[1], "--no-scissors"))
4251
mi.use_scissors = 0;
@@ -50,6 +59,19 @@ int cmd_mailinfo(int argc, const char **argv, const char *prefix)
5059
if (argc != 3)
5160
usage(mailinfo_usage);
5261

62+
switch (meta_charset.policy) {
63+
case CHARSET_DEFAULT:
64+
mi.metainfo_charset = get_commit_output_encoding();
65+
break;
66+
case CHARSET_NO_REENCODE:
67+
mi.metainfo_charset = NULL;
68+
break;
69+
case CHARSET_EXPLICIT:
70+
break;
71+
default:
72+
BUG("invalid meta_charset.policy");
73+
}
74+
5375
mi.input = stdin;
5476
mi.output = stdout;
5577

0 commit comments

Comments
 (0)