Merge branch 'jk/interpret-branch-name'
[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 static char *prefix_copy(const char *prefix, const char *filename)
15 {
16         if (!prefix || is_absolute_path(filename))
17                 return xstrdup(filename);
18         return xstrdup(prefix_filename(prefix, strlen(prefix), filename));
19 }
20
21 int cmd_mailinfo(int argc, const char **argv, const char *prefix)
22 {
23         const char *def_charset;
24         struct mailinfo mi;
25         int status;
26         char *msgfile, *patchfile;
27
28         setup_mailinfo(&mi);
29
30         def_charset = get_commit_output_encoding();
31         mi.metainfo_charset = def_charset;
32
33         while (1 < argc && argv[1][0] == '-') {
34                 if (!strcmp(argv[1], "-k"))
35                         mi.keep_subject = 1;
36                 else if (!strcmp(argv[1], "-b"))
37                         mi.keep_non_patch_brackets_in_subject = 1;
38                 else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
39                         mi.add_message_id = 1;
40                 else if (!strcmp(argv[1], "-u"))
41                         mi.metainfo_charset = def_charset;
42                 else if (!strcmp(argv[1], "-n"))
43                         mi.metainfo_charset = NULL;
44                 else if (starts_with(argv[1], "--encoding="))
45                         mi.metainfo_charset = argv[1] + 11;
46                 else if (!strcmp(argv[1], "--scissors"))
47                         mi.use_scissors = 1;
48                 else if (!strcmp(argv[1], "--no-scissors"))
49                         mi.use_scissors = 0;
50                 else if (!strcmp(argv[1], "--no-inbody-headers"))
51                         mi.use_inbody_headers = 0;
52                 else
53                         usage(mailinfo_usage);
54                 argc--; argv++;
55         }
56
57         if (argc != 3)
58                 usage(mailinfo_usage);
59
60         mi.input = stdin;
61         mi.output = stdout;
62
63         msgfile = prefix_copy(prefix, argv[1]);
64         patchfile = prefix_copy(prefix, argv[2]);
65
66         status = !!mailinfo(&mi, msgfile, patchfile);
67         clear_mailinfo(&mi);
68
69         free(msgfile);
70         free(patchfile);
71         return status;
72 }