blob: cfb667a594c8452b8f4259bb3da1976965906f95 [file] [log] [blame]
Linus Torvalds2744b232005-04-11 23:46:50 -07001/*
2 * Another stupid program, this one parsing the headers of an
3 * email to figure out authorship and subject
4 */
Junio C Hamanof1f909e2005-11-27 16:29:38 -08005#include "cache.h"
Lukas Sandström34488e32006-06-13 22:21:50 +02006#include "builtin.h"
Junio C Hamanob45974a2006-12-23 23:36:55 -08007#include "utf8.h"
Lukas Sandström3b6121f2008-07-13 20:30:12 +02008#include "strbuf.h"
Junio C Hamanoc6905e42015-10-14 17:44:55 -07009#include "mailinfo.h"
Junio C Hamanoc69f2392015-10-18 22:22:10 -070010
Junio C Hamano6bff6a62005-08-16 22:18:27 -070011static const char mailinfo_usage[] =
Alex Henrie9c9b4f22015-01-13 00:44:47 -070012 "git mailinfo [-k | -b] [-m | --message-id] [-u | --encoding=<encoding> | -n] [--scissors | --no-scissors] <msg> <patch> < mail >info";
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070013
Linus Torvaldsa633fca2006-07-28 22:44:25 -070014int cmd_mailinfo(int argc, const char **argv, const char *prefix)
Linus Torvalds2744b232005-04-11 23:46:50 -070015{
Junio C Hamanobb1091a2007-01-09 21:31:36 -080016 const char *def_charset;
Junio C Hamanoc69f2392015-10-18 22:22:10 -070017 struct mailinfo mi;
18 int status;
Junio C Hamano3f0ec062016-11-22 13:13:16 -080019 char *msgfile, *patchfile;
Junio C Hamanobb1091a2007-01-09 21:31:36 -080020
Junio C Hamanoc69f2392015-10-18 22:22:10 -070021 setup_mailinfo(&mi);
Junio C Hamanof1f909e2005-11-27 16:29:38 -080022
Pat Notza6fa5992010-11-02 13:59:07 -060023 def_charset = get_commit_output_encoding();
Junio C Hamano28be2d02015-10-14 16:15:40 -070024 mi.metainfo_charset = def_charset;
Junio C Hamanobb1091a2007-01-09 21:31:36 -080025
Junio C Hamano6bff6a62005-08-16 22:18:27 -070026 while (1 < argc && argv[1][0] == '-') {
27 if (!strcmp(argv[1], "-k"))
Junio C Hamano849106d2015-10-14 15:39:37 -070028 mi.keep_subject = 1;
Junio C Hamano17635fc2009-07-15 15:31:12 -070029 else if (!strcmp(argv[1], "-b"))
Junio C Hamano849106d2015-10-14 15:39:37 -070030 mi.keep_non_patch_brackets_in_subject = 1;
Paolo Bonzini452dfbe2014-11-25 15:00:55 +010031 else if (!strcmp(argv[1], "-m") || !strcmp(argv[1], "--message-id"))
Junio C Hamano6200b752015-10-18 22:27:56 -070032 mi.add_message_id = 1;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070033 else if (!strcmp(argv[1], "-u"))
Junio C Hamano28be2d02015-10-14 16:15:40 -070034 mi.metainfo_charset = def_charset;
Junio C Hamanobb1091a2007-01-09 21:31:36 -080035 else if (!strcmp(argv[1], "-n"))
Junio C Hamano28be2d02015-10-14 16:15:40 -070036 mi.metainfo_charset = NULL;
Christian Couder59556542013-11-30 21:55:40 +010037 else if (starts_with(argv[1], "--encoding="))
Junio C Hamano28be2d02015-10-14 16:15:40 -070038 mi.metainfo_charset = argv[1] + 11;
Junio C Hamano017678b2009-08-26 21:36:05 -070039 else if (!strcmp(argv[1], "--scissors"))
Junio C Hamanoad57ef92015-10-14 16:14:57 -070040 mi.use_scissors = 1;
Junio C Hamano017678b2009-08-26 21:36:05 -070041 else if (!strcmp(argv[1], "--no-scissors"))
Junio C Hamanoad57ef92015-10-14 16:14:57 -070042 mi.use_scissors = 0;
Lukas Sandströmd25e5152009-11-20 17:12:47 +010043 else if (!strcmp(argv[1], "--no-inbody-headers"))
Junio C Hamanoad57ef92015-10-14 16:14:57 -070044 mi.use_inbody_headers = 0;
Junio C Hamanod4a9ce72005-08-28 12:33:16 -070045 else
Junio C Hamanof1f909e2005-11-27 16:29:38 -080046 usage(mailinfo_usage);
Junio C Hamano6bff6a62005-08-16 22:18:27 -070047 argc--; argv++;
48 }
49
Linus Torvaldsa196d8d2005-06-23 09:40:23 -070050 if (argc != 3)
Junio C Hamanof1f909e2005-11-27 16:29:38 -080051 usage(mailinfo_usage);
Lukas Sandström34488e32006-06-13 22:21:50 +020052
Junio C Hamano173aef72015-10-14 15:40:04 -070053 mi.input = stdin;
54 mi.output = stdout;
Junio C Hamano3f0ec062016-11-22 13:13:16 -080055
Jeff Kinge4da43b2017-03-20 21:28:49 -040056 msgfile = prefix_filename(prefix, argv[1]);
57 patchfile = prefix_filename(prefix, argv[2]);
Junio C Hamano3f0ec062016-11-22 13:13:16 -080058
59 status = !!mailinfo(&mi, msgfile, patchfile);
Junio C Hamanoc69f2392015-10-18 22:22:10 -070060 clear_mailinfo(&mi);
61
Junio C Hamano3f0ec062016-11-22 13:13:16 -080062 free(msgfile);
63 free(patchfile);
Junio C Hamanoc69f2392015-10-18 22:22:10 -070064 return status;
Linus Torvalds2744b232005-04-11 23:46:50 -070065}