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