Merge branch 'ds/more-index-cleanups'
[git] / builtin / fmt-merge-msg.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "fmt-merge-msg.h"
4 #include "parse-options.h"
5
6 static const char * const fmt_merge_msg_usage[] = {
7         N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
8         NULL
9 };
10
11 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
12 {
13         const char *inpath = NULL;
14         const char *message = NULL;
15         int shortlog_len = -1;
16         struct option options[] = {
17                 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
18                   N_("populate log with at most <n> entries from shortlog"),
19                   PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
20                 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
21                   N_("alias for --log (deprecated)"),
22                   PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
23                   DEFAULT_MERGE_LOG_LEN },
24                 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")),
27                 OPT_END()
28         };
29
30         FILE *in = stdin;
31         struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
32         int ret;
33         struct fmt_merge_msg_opts opts;
34
35         git_config(fmt_merge_msg_config, NULL);
36         argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
37                              0);
38         if (argc > 0)
39                 usage_with_options(fmt_merge_msg_usage, options);
40         if (shortlog_len < 0)
41                 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
42
43         if (inpath && strcmp(inpath, "-")) {
44                 in = fopen(inpath, "r");
45                 if (!in)
46                         die_errno("cannot open '%s'", inpath);
47         }
48
49         if (strbuf_read(&input, fileno(in), 0) < 0)
50                 die_errno("could not read input file");
51
52         if (message)
53                 strbuf_addstr(&output, message);
54
55         memset(&opts, 0, sizeof(opts));
56         opts.add_title = !message;
57         opts.credit_people = 1;
58         opts.shortlog_len = shortlog_len;
59
60         ret = fmt_merge_msg(&input, &output, &opts);
61         if (ret)
62                 return ret;
63         write_in_full(STDOUT_FILENO, output.buf, output.len);
64         return 0;
65 }