]>
Commit | Line | Data |
---|---|---|
2744b234 LT |
1 | /* |
2 | * Another stupid program, this one parsing the headers of an | |
3 | * email to figure out authorship and subject | |
4 | */ | |
34488e3c | 5 | #include "builtin.h" |
bc5c5ec0 | 6 | #include "abspath.h" |
32a8f510 | 7 | #include "environment.h" |
f394e093 | 8 | #include "gettext.h" |
3b6121f6 | 9 | #include "strbuf.h" |
c6905e45 | 10 | #include "mailinfo.h" |
dd9323b7 | 11 | #include "parse-options.h" |
c69f2395 | 12 | |
dd9323b7 ĐTCD |
13 | static const char * const mailinfo_usage[] = { |
14 | /* TRANSLATORS: keep <> in "<" mail ">" info. */ | |
15 | N_("git mailinfo [<options>] <msg> <patch> < mail >info"), | |
16 | NULL, | |
17 | }; | |
d4a9ce78 | 18 | |
d582992e ĐTCD |
19 | struct metainfo_charset |
20 | { | |
21 | enum { | |
22 | CHARSET_DEFAULT, | |
23 | CHARSET_NO_REENCODE, | |
24 | CHARSET_EXPLICIT, | |
25 | } policy; | |
26 | const char *charset; | |
27 | }; | |
28 | ||
dd9323b7 ĐTCD |
29 | static int parse_opt_explicit_encoding(const struct option *opt, |
30 | const char *arg, int unset) | |
31 | { | |
32 | struct metainfo_charset *meta_charset = opt->value; | |
33 | ||
34 | BUG_ON_OPT_NEG(unset); | |
35 | ||
36 | meta_charset->policy = CHARSET_EXPLICIT; | |
37 | meta_charset->charset = arg; | |
38 | ||
39 | return 0; | |
40 | } | |
41 | ||
f1aa2994 ĐTCD |
42 | static int parse_opt_quoted_cr(const struct option *opt, const char *arg, int unset) |
43 | { | |
44 | BUG_ON_OPT_NEG(unset); | |
45 | ||
46 | if (mailinfo_parse_quoted_cr_action(arg, opt->value) != 0) | |
47 | return error(_("bad action '%s' for '%s'"), arg, "--quoted-cr"); | |
48 | return 0; | |
49 | } | |
50 | ||
a633fca0 | 51 | int cmd_mailinfo(int argc, const char **argv, const char *prefix) |
2744b234 | 52 | { |
d582992e | 53 | struct metainfo_charset meta_charset; |
c69f2395 JH |
54 | struct mailinfo mi; |
55 | int status; | |
3f0ec068 | 56 | char *msgfile, *patchfile; |
bb1091a4 | 57 | |
dd9323b7 ĐTCD |
58 | struct option options[] = { |
59 | OPT_BOOL('k', NULL, &mi.keep_subject, N_("keep subject")), | |
60 | OPT_BOOL('b', NULL, &mi.keep_non_patch_brackets_in_subject, | |
61 | N_("keep non patch brackets in subject")), | |
62 | OPT_BOOL('m', "message-id", &mi.add_message_id, | |
63 | N_("copy Message-ID to the end of commit message")), | |
64 | OPT_SET_INT_F('u', NULL, &meta_charset.policy, | |
65 | N_("re-code metadata to i18n.commitEncoding"), | |
66 | CHARSET_DEFAULT, PARSE_OPT_NONEG), | |
67 | OPT_SET_INT_F('n', NULL, &meta_charset.policy, | |
68 | N_("disable charset re-coding of metadata"), | |
69 | CHARSET_NO_REENCODE, PARSE_OPT_NONEG), | |
70 | OPT_CALLBACK_F(0, "encoding", &meta_charset, N_("encoding"), | |
71 | N_("re-code metadata to this encoding"), | |
72 | PARSE_OPT_NONEG, parse_opt_explicit_encoding), | |
73 | OPT_BOOL(0, "scissors", &mi.use_scissors, N_("use scissors")), | |
f1aa2994 ĐTCD |
74 | OPT_CALLBACK_F(0, "quoted-cr", &mi.quoted_cr, N_("<action>"), |
75 | N_("action when quoted CR is found"), | |
76 | PARSE_OPT_NONEG, parse_opt_quoted_cr), | |
dd9323b7 ĐTCD |
77 | OPT_HIDDEN_BOOL(0, "inbody-headers", &mi.use_inbody_headers, |
78 | N_("use headers in message's body")), | |
79 | OPT_END() | |
80 | }; | |
81 | ||
c69f2395 | 82 | setup_mailinfo(&mi); |
d582992e | 83 | meta_charset.policy = CHARSET_DEFAULT; |
bb1091a4 | 84 | |
dd9323b7 | 85 | argc = parse_options(argc, argv, prefix, options, mailinfo_usage, 0); |
6bff6a60 | 86 | |
dd9323b7 ĐTCD |
87 | if (argc != 2) |
88 | usage_with_options(mailinfo_usage, options); | |
34488e3c | 89 | |
d582992e ĐTCD |
90 | switch (meta_charset.policy) { |
91 | case CHARSET_DEFAULT: | |
92 | mi.metainfo_charset = get_commit_output_encoding(); | |
93 | break; | |
94 | case CHARSET_NO_REENCODE: | |
95 | mi.metainfo_charset = NULL; | |
96 | break; | |
97 | case CHARSET_EXPLICIT: | |
98 | break; | |
99 | default: | |
100 | BUG("invalid meta_charset.policy"); | |
101 | } | |
102 | ||
173aef7c JH |
103 | mi.input = stdin; |
104 | mi.output = stdout; | |
3f0ec068 | 105 | |
dd9323b7 ĐTCD |
106 | msgfile = prefix_filename(prefix, argv[0]); |
107 | patchfile = prefix_filename(prefix, argv[1]); | |
3f0ec068 JH |
108 | |
109 | status = !!mailinfo(&mi, msgfile, patchfile); | |
c69f2395 JH |
110 | clear_mailinfo(&mi); |
111 | ||
3f0ec068 JH |
112 | free(msgfile); |
113 | free(patchfile); | |
c69f2395 | 114 | return status; |
2744b234 | 115 | } |