Merge branch 'ja/do-not-ask-needless-questions'
[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 "builtin.h"
7 #include "utf8.h"
8 #include "strbuf.h"
9 #include "mailinfo.h"
10
11 static const char mailinfo_usage[] =
12         "git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
13
14 int cmd_mailinfo(int argc, const char **argv, const char *prefix)
15 {
16         const char *def_charset;
17         struct mailinfo mi;
18         int status;
19         char *msgfile, *patchfile;
20
21         setup_mailinfo(&mi);
22
23         def_charset = get_commit_output_encoding();
24         mi.metainfo_charset = def_charset;
25
26         while (1 < argc && argv[1][0] == '-') {
27                 if (!strcmp(argv[1], "-k"))
28                         mi.keep_subject = 1;
29                 else if (!strcmp(argv[1], "-b"))
30                         mi.keep_non_patch_brackets_in_subject = 1;
31                 else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
32                         mi.add_message_id = 1;
33                 else if (!strcmp(argv[1], "-u"))
34                         mi.metainfo_charset = def_charset;
35                 else if (!strcmp(argv[1], "-n"))
36                         mi.metainfo_charset = NULL;
37                 else if (starts_with(argv[1], "--encoding="))
38                         mi.metainfo_charset = argv[1] + 11;
39                 else if (!strcmp(argv[1], "--scissors"))
40                         mi.use_scissors = 1;
41                 else if (!strcmp(argv[1], "--no-scissors"))
42                         mi.use_scissors = 0;
43                 else if (!strcmp(argv[1], "--no-inbody-headers"))
44                         mi.use_inbody_headers = 0;
45                 else
46                         usage(mailinfo_usage);
47                 argc--; argv++;
48         }
49
50         if (argc != 3)
51                 usage(mailinfo_usage);
52
53         mi.input = stdin;
54         mi.output = stdout;
55
56         msgfile = prefix_filename(prefix, argv[1]);
57         patchfile = prefix_filename(prefix, argv[2]);
58
59         status = !!mailinfo(&mi, msgfile, patchfile);
60         clear_mailinfo(&mi);
61
62         free(msgfile);
63         free(patchfile);
64         return status;
65 }