|
7 | 7 | #include "utf8.h"
|
8 | 8 | #include "strbuf.h"
|
9 | 9 | #include "mailinfo.h"
|
| 10 | +#include "parse-options.h" |
10 | 11 |
|
11 |
| -static const char mailinfo_usage[] = |
12 |
| - "git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info"; |
| 12 | +static const char * const mailinfo_usage[] = { |
| 13 | + /* TRANSLATORS: keep <> in "<" mail ">" info. */ |
| 14 | + N_("git mailinfo [<options>] <msg> <patch> < mail >info"), |
| 15 | + NULL, |
| 16 | +}; |
| 17 | + |
| 18 | +struct metainfo_charset |
| 19 | +{ |
| 20 | + enum { |
| 21 | + CHARSET_DEFAULT, |
| 22 | + CHARSET_NO_REENCODE, |
| 23 | + CHARSET_EXPLICIT, |
| 24 | + } policy; |
| 25 | + const char *charset; |
| 26 | +}; |
| 27 | + |
| 28 | +static int parse_opt_explicit_encoding(const struct option *opt, |
| 29 | + const char *arg, int unset) |
| 30 | +{ |
| 31 | + struct metainfo_charset *meta_charset = opt->value; |
| 32 | + |
| 33 | + BUG_ON_OPT_NEG(unset); |
| 34 | + |
| 35 | + meta_charset->policy = CHARSET_EXPLICIT; |
| 36 | + meta_charset->charset = arg; |
| 37 | + |
| 38 | + return 0; |
| 39 | +} |
| 40 | + |
| 41 | +static int parse_opt_quoted_cr(const struct option *opt, const char *arg, int unset) |
| 42 | +{ |
| 43 | + BUG_ON_OPT_NEG(unset); |
| 44 | + |
| 45 | + if (mailinfo_parse_quoted_cr_action(arg, opt->value) != 0) |
| 46 | + return error(_("bad action '%s' for '%s'"), arg, "--quoted-cr"); |
| 47 | + return 0; |
| 48 | +} |
13 | 49 |
|
14 | 50 | int cmd_mailinfo(int argc, const char **argv, const char *prefix)
|
15 | 51 | {
|
16 |
| - const char *def_charset; |
| 52 | + struct metainfo_charset meta_charset; |
17 | 53 | struct mailinfo mi;
|
18 | 54 | int status;
|
19 | 55 | char *msgfile, *patchfile;
|
20 | 56 |
|
| 57 | + struct option options[] = { |
| 58 | + OPT_BOOL('k', NULL, &mi.keep_subject, N_("keep subject")), |
| 59 | + OPT_BOOL('b', NULL, &mi.keep_non_patch_brackets_in_subject, |
| 60 | + N_("keep non patch brackets in subject")), |
| 61 | + OPT_BOOL('m', "message-id", &mi.add_message_id, |
| 62 | + N_("copy Message-ID to the end of commit message")), |
| 63 | + OPT_SET_INT_F('u', NULL, &meta_charset.policy, |
| 64 | + N_("re-code metadata to i18n.commitEncoding"), |
| 65 | + CHARSET_DEFAULT, PARSE_OPT_NONEG), |
| 66 | + OPT_SET_INT_F('n', NULL, &meta_charset.policy, |
| 67 | + N_("disable charset re-coding of metadata"), |
| 68 | + CHARSET_NO_REENCODE, PARSE_OPT_NONEG), |
| 69 | + OPT_CALLBACK_F(0, "encoding", &meta_charset, N_("encoding"), |
| 70 | + N_("re-code metadata to this encoding"), |
| 71 | + PARSE_OPT_NONEG, parse_opt_explicit_encoding), |
| 72 | + OPT_BOOL(0, "scissors", &mi.use_scissors, N_("use scissors")), |
| 73 | + OPT_CALLBACK_F(0, "quoted-cr", &mi.quoted_cr, N_("<action>"), |
| 74 | + N_("action when quoted CR is found"), |
| 75 | + PARSE_OPT_NONEG, parse_opt_quoted_cr), |
| 76 | + OPT_HIDDEN_BOOL(0, "inbody-headers", &mi.use_inbody_headers, |
| 77 | + N_("use headers in message's body")), |
| 78 | + OPT_END() |
| 79 | + }; |
| 80 | + |
21 | 81 | setup_mailinfo(&mi);
|
| 82 | + meta_charset.policy = CHARSET_DEFAULT; |
22 | 83 |
|
23 |
| - def_charset = get_commit_output_encoding(); |
24 |
| - mi.metainfo_charset = def_charset; |
25 |
| - |
26 |
| - while (1 < argc && argv[1][0] == '-') { |
27 |
| - if (!strcmp(argv[1], "-k")) |
28 |
| - mi.keep_subject = 1; |
29 |
| - else if (!strcmp(argv[1], "-b")) |
30 |
| - mi.keep_non_patch_brackets_in_subject = 1; |
31 |
| - else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id")) |
32 |
| - mi.add_message_id = 1; |
33 |
| - else if (!strcmp(argv[1], "-u")) |
34 |
| - mi.metainfo_charset = def_charset; |
35 |
| - 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")) |
40 |
| - mi.use_scissors = 1; |
41 |
| - else if (!strcmp(argv[1], "--no-scissors")) |
42 |
| - mi.use_scissors = 0; |
43 |
| - else if (!strcmp(argv[1], "--no-inbody-headers")) |
44 |
| - mi.use_inbody_headers = 0; |
45 |
| - else |
46 |
| - usage(mailinfo_usage); |
47 |
| - argc--; argv++; |
48 |
| - } |
| 84 | + argc = parse_options(argc, argv, prefix, options, mailinfo_usage, 0); |
49 | 85 |
|
50 |
| - if (argc != 3) |
51 |
| - usage(mailinfo_usage); |
| 86 | + if (argc != 2) |
| 87 | + usage_with_options(mailinfo_usage, options); |
| 88 | + |
| 89 | + switch (meta_charset.policy) { |
| 90 | + case CHARSET_DEFAULT: |
| 91 | + mi.metainfo_charset = get_commit_output_encoding(); |
| 92 | + break; |
| 93 | + case CHARSET_NO_REENCODE: |
| 94 | + mi.metainfo_charset = NULL; |
| 95 | + break; |
| 96 | + case CHARSET_EXPLICIT: |
| 97 | + break; |
| 98 | + default: |
| 99 | + BUG("invalid meta_charset.policy"); |
| 100 | + } |
52 | 101 |
|
53 | 102 | mi.input = stdin;
|
54 | 103 | mi.output = stdout;
|
55 | 104 |
|
56 |
| - msgfile = prefix_filename(prefix, argv[1]); |
57 |
| - patchfile = prefix_filename(prefix, argv[2]); |
| 105 | + msgfile = prefix_filename(prefix, argv[0]); |
| 106 | + patchfile = prefix_filename(prefix, argv[1]); |
58 | 107 |
|
59 | 108 | status = !!mailinfo(&mi, msgfile, patchfile);
|
60 | 109 | clear_mailinfo(&mi);
|
|
0 commit comments