The seventh batch
[alt-git.git] / builtin / fmt-merge-msg.c
blob189cd1096a0b92d0093c69ce019f48af1f281601
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "fmt-merge-msg.h"
5 #include "gettext.h"
6 #include "parse-options.h"
8 static const char * const fmt_merge_msg_usage[] = {
9 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"),
10 NULL
13 int cmd_fmt_merge_msg(int argc,
14 const char **argv,
15 const char *prefix,
16 struct repository *repo UNUSED)
18 char *inpath = NULL;
19 const char *message = NULL;
20 char *into_name = NULL;
21 int shortlog_len = -1;
22 struct option options[] = {
23 { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"),
24 N_("populate log with at most <n> entries from shortlog"),
25 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
26 { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"),
27 N_("alias for --log (deprecated)"),
28 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
29 DEFAULT_MERGE_LOG_LEN },
30 OPT_STRING('m', "message", &message, N_("text"),
31 N_("use <text> as start of message")),
32 OPT_STRING(0, "into-name", &into_name, N_("name"),
33 N_("use <name> instead of the real target branch")),
34 OPT_FILENAME('F', "file", &inpath, N_("file to read from")),
35 OPT_END()
38 FILE *in = stdin;
39 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
40 int ret;
41 struct fmt_merge_msg_opts opts;
43 git_config(fmt_merge_msg_config, NULL);
44 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
45 0);
46 if (argc > 0)
47 usage_with_options(fmt_merge_msg_usage, options);
48 if (shortlog_len < 0)
49 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
51 if (inpath && strcmp(inpath, "-")) {
52 in = fopen(inpath, "r");
53 if (!in)
54 die_errno("cannot open '%s'", inpath);
57 if (strbuf_read(&input, fileno(in), 0) < 0)
58 die_errno("could not read input file");
60 if (message)
61 strbuf_addstr(&output, message);
63 memset(&opts, 0, sizeof(opts));
64 opts.add_title = !message;
65 opts.credit_people = 1;
66 opts.shortlog_len = shortlog_len;
67 opts.into_name = into_name;
69 ret = fmt_merge_msg(&input, &output, &opts);
70 if (ret)
71 return ret;
72 write_in_full(STDOUT_FILENO, output.buf, output.len);
74 strbuf_release(&input);
75 strbuf_release(&output);
76 free(inpath);
77 return 0;