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