blob: 0f9855b680eb7bf4e534af40847e32af835cfa13 [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"
Elijah Newrenf394e092023-03-21 06:25:54 +00004#include "gettext.h"
Denton Liuce6521e2020-03-23 21:07:51 -04005#include "parse-options.h"
Johannes Schindelin00449f92006-07-03 17:18:43 +02006
Pierre Habouzitc8ef0382008-10-02 14:59:18 +02007static const char * const fmt_merge_msg_usage[] = {
Alex Henrie9c9b4f22015-01-13 00:44:47 -07008 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
Pierre Habouzitc8ef0382008-10-02 14:59:18 +02009 NULL
10};
Johannes Schindelin00449f92006-07-03 17:18:43 +020011
Miklos Vajna0b9a9692008-06-27 18:21:59 +020012int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
13{
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020014 const char *inpath = NULL;
Jonathan Nieder21024402010-08-17 18:00:34 -050015 const char *message = NULL;
Junio C Hamanobd2bc942021-12-20 14:53:43 -080016 char *into_name = NULL;
Junio C Hamano898eacd2011-10-06 23:12:09 -070017 int shortlog_len = -1;
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020018 struct option options[] = {
Nguyễn Thái Ngọc Duy93eced62012-08-20 19:32:10 +070019 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
20 N_("populate log with at most <n> entries from shortlog"),
Ramkumar Ramachandra96e94202010-09-08 23:29:54 +053021 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
Nguyễn Thái Ngọc Duy93eced62012-08-20 19:32:10 +070022 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
23 N_("alias for --log (deprecated)"),
Ramkumar Ramachandra96e94202010-09-08 23:29:54 +053024 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
25 DEFAULT_MERGE_LOG_LEN },
Nguyễn Thái Ngọc Duy93eced62012-08-20 19:32:10 +070026 OPT_STRING('m', "message", &message, N_("text"),
27 N_("use <text> as start of message")),
Junio C Hamanobd2bc942021-12-20 14:53:43 -080028 OPT_STRING(0, "into-name", &into_name, N_("name"),
29 N_("use <name> instead of the real target branch")),
Nguyễn Thái Ngọc Duy93eced62012-08-20 19:32:10 +070030 OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020031 OPT_END()
32 };
33
Miklos Vajna0b9a9692008-06-27 18:21:59 +020034 FILE *in = stdin;
Brandon Caseyf285a2d2008-10-09 14:12:12 -050035 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
Miklos Vajna0b9a9692008-06-27 18:21:59 +020036 int ret;
Junio C Hamanocbda1212011-11-04 17:35:42 -070037 struct fmt_merge_msg_opts opts;
Miklos Vajna0b9a9692008-06-27 18:21:59 +020038
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010039 git_config(fmt_merge_msg_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -070040 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
41 0);
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020042 if (argc > 0)
43 usage_with_options(fmt_merge_msg_usage, options);
Junio C Hamano898eacd2011-10-06 23:12:09 -070044 if (shortlog_len < 0)
45 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
Johannes Schindelin00449f92006-07-03 17:18:43 +020046
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020047 if (inpath && strcmp(inpath, "-")) {
48 in = fopen(inpath, "r");
49 if (!in)
Thomas Rast0721c312009-06-27 17:58:47 +020050 die_errno("cannot open '%s'", inpath);
Johannes Schindelin00449f92006-07-03 17:18:43 +020051 }
52
Miklos Vajna0b9a9692008-06-27 18:21:59 +020053 if (strbuf_read(&input, fileno(in), 0) < 0)
Thomas Rastd824cbb2009-06-27 17:58:46 +020054 die_errno("could not read input file");
Ramkumar Ramachandra18761662010-09-08 23:29:53 +053055
56 if (message)
Jonathan Nieder21024402010-08-17 18:00:34 -050057 strbuf_addstr(&output, message);
Ramkumar Ramachandra18761662010-09-08 23:29:53 +053058
Junio C Hamanocbda1212011-11-04 17:35:42 -070059 memset(&opts, 0, sizeof(opts));
60 opts.add_title = !message;
Junio C Hamano9bcbb1c2012-12-28 15:29:31 -080061 opts.credit_people = 1;
Junio C Hamanocbda1212011-11-04 17:35:42 -070062 opts.shortlog_len = shortlog_len;
Junio C Hamanobd2bc942021-12-20 14:53:43 -080063 opts.into_name = into_name;
Junio C Hamanocbda1212011-11-04 17:35:42 -070064
65 ret = fmt_merge_msg(&input, &output, &opts);
Miklos Vajna0b9a9692008-06-27 18:21:59 +020066 if (ret)
67 return ret;
Pierre Habouzitc8ef0382008-10-02 14:59:18 +020068 write_in_full(STDOUT_FILENO, output.buf, output.len);
Johannes Schindelin00449f92006-07-03 17:18:43 +020069 return 0;
70}