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