blob: 48a8699de728a950053b182fae23e73b14fba6a6 [file] [log] [blame]
Matthias Kestenholz25f38f02006-08-02 23:52:00 +02001#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Junio C Hamano898eacd2011-10-06 23:12:09 -07003#include "fmt-merge-msg.h"
Denton Liuce6521e2020-03-23 21:07:51 -04004#include "parse-options.h"
Johannes Schindelin00449f92006-07-03 17:18:43 +02005
Pierre Habouzitc8ef0382008-10-02 14:59:18 +02006static const char * const fmt_merge_msg_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -07007 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
Pierre Habouzitc8ef0382008-10-02 14:59:18 +02008 NULL
9};
Johannes Schindelin00449f92006-07-03 17:18:43 +020010
Miklos Vajna0b9a9692008-06-27 18:21:59 +020011int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
12{
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020013 const char *inpath = NULL;
Jonathan Nieder21024402010-08-17 18:00:34 -050014 const char *message = NULL;
Junio C Hamano898eacd2011-10-06 23:12:09 -070015 int shortlog_len = -1;
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020016 struct option options[] = {
Nguyễn Thái Ngọc Duy93eced62012-08-20 19:32:10 +070017 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
18 N_("populate log with at most <n> entries from shortlog"),
Ramkumar Ramachandra96e94202010-09-08 23:29:54 +053019 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
Nguyễn Thái Ngọc Duy93eced62012-08-20 19:32:10 +070020 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
21 N_("alias for --log (deprecated)"),
Ramkumar Ramachandra96e94202010-09-08 23:29:54 +053022 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
23 DEFAULT_MERGE_LOG_LEN },
Nguyễn Thái Ngọc Duy93eced62012-08-20 19:32:10 +070024 OPT_STRING('m', "message", &message, N_("text"),
25 N_("use <text> as start of message")),
26 OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020027 OPT_END()
28 };
29
Miklos Vajna0b9a9692008-06-27 18:21:59 +020030 FILE *in = stdin;
Brandon Caseyf285a2d2008-10-09 14:12:12 -050031 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
Miklos Vajna0b9a9692008-06-27 18:21:59 +020032 int ret;
Junio C Hamanocbda1212011-11-04 17:35:42 -070033 struct fmt_merge_msg_opts opts;
Miklos Vajna0b9a9692008-06-27 18:21:59 +020034
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010035 git_config(fmt_merge_msg_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -070036 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
37 0);
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020038 if (argc > 0)
39 usage_with_options(fmt_merge_msg_usage, options);
Junio C Hamano898eacd2011-10-06 23:12:09 -070040 if (shortlog_len < 0)
41 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
Johannes Schindelin00449f92006-07-03 17:18:43 +020042
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020043 if (inpath && strcmp(inpath, "-")) {
44 in = fopen(inpath, "r");
45 if (!in)
Thomas Rast0721c312009-06-27 17:58:47 +020046 die_errno("cannot open '%s'", inpath);
Johannes Schindelin00449f92006-07-03 17:18:43 +020047 }
48
Miklos Vajna0b9a9692008-06-27 18:21:59 +020049 if (strbuf_read(&input, fileno(in), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +020050 die_errno("could not read input file");
Ramkumar Ramachandra18761662010-09-08 23:29:53 +053051
52 if (message)
Jonathan Nieder21024402010-08-17 18:00:34 -050053 strbuf_addstr(&output, message);
Ramkumar Ramachandra18761662010-09-08 23:29:53 +053054
Junio C Hamanocbda1212011-11-04 17:35:42 -070055 memset(&opts, 0, sizeof(opts));
56 opts.add_title = !message;
Junio C Hamano9bcbb1c2012-12-28 15:29:31 -080057 opts.credit_people = 1;
Junio C Hamanocbda1212011-11-04 17:35:42 -070058 opts.shortlog_len = shortlog_len;
59
60 ret = fmt_merge_msg(&input, &output, &opts);
Miklos Vajna0b9a9692008-06-27 18:21:59 +020061 if (ret)
62 return ret;
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020063 write_in_full(STDOUT_FILENO, output.buf, output.len);
Johannes Schindelin00449f92006-07-03 17:18:43 +020064 return 0;
65}