]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/mailinfo.c
environment.h: move declarations for environment.c functions from cache.h
[thirdparty/git.git] / builtin / mailinfo.c
CommitLineData
2744b234
LT
1/*
2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
4 */
f1f909e3 5#include "cache.h"
0b027f6c 6#include "abspath.h"
34488e3c 7#include "builtin.h"
32a8f510 8#include "environment.h"
f394e093 9#include "gettext.h"
b45974a6 10#include "utf8.h"
3b6121f6 11#include "strbuf.h"
c6905e45 12#include "mailinfo.h"
dd9323b7 13#include "parse-options.h"
c69f2395 14
dd9323b7
ĐTCD
15static const char * const mailinfo_usage[] = {
16 /* TRANSLATORS: keep <> in "<" mail ">" info. */
17 N_("git mailinfo [<options>] <msg> <patch> < mail >info"),
18 NULL,
19};
d4a9ce78 20
d582992e
ĐTCD
21struct metainfo_charset
22{
23 enum {
24 CHARSET_DEFAULT,
25 CHARSET_NO_REENCODE,
26 CHARSET_EXPLICIT,
27 } policy;
28 const char *charset;
29};
30
dd9323b7
ĐTCD
31static 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
f1aa2994
ĐTCD
44static 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
a633fca0 53int cmd_mailinfo(int argc, const char **argv, const char *prefix)
2744b234 54{
d582992e 55 struct metainfo_charset meta_charset;
c69f2395
JH
56 struct mailinfo mi;
57 int status;
3f0ec068 58 char *msgfile, *patchfile;
bb1091a4 59
dd9323b7
ĐTCD
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")),
f1aa2994
ĐTCD
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),
dd9323b7
ĐTCD
79 OPT_HIDDEN_BOOL(0, "inbody-headers", &mi.use_inbody_headers,
80 N_("use headers in message's body")),
81 OPT_END()
82 };
83
c69f2395 84 setup_mailinfo(&mi);
d582992e 85 meta_charset.policy = CHARSET_DEFAULT;
bb1091a4 86
dd9323b7 87 argc = parse_options(argc, argv, prefix, options, mailinfo_usage, 0);
6bff6a60 88
dd9323b7
ĐTCD
89 if (argc != 2)
90 usage_with_options(mailinfo_usage, options);
34488e3c 91
d582992e
ĐTCD
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
173aef7c
JH
105 mi.input = stdin;
106 mi.output = stdout;
3f0ec068 107
dd9323b7
ĐTCD
108 msgfile = prefix_filename(prefix, argv[0]);
109 patchfile = prefix_filename(prefix, argv[1]);
3f0ec068
JH
110
111 status = !!mailinfo(&mi, msgfile, patchfile);
c69f2395
JH
112 clear_mailinfo(&mi);
113
3f0ec068
JH
114 free(msgfile);
115 free(patchfile);
c69f2395 116 return status;
2744b234 117}