Git fork
at reftables-rust 91 lines 2.4 kB view raw
1#include "builtin.h" 2#include "config.h" 3#include "fmt-merge-msg.h" 4#include "gettext.h" 5#include "parse-options.h" 6 7static const char * const fmt_merge_msg_usage[] = { 8 N_("git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]"), 9 NULL 10}; 11 12int cmd_fmt_merge_msg(int argc, 13 const char **argv, 14 const char *prefix, 15 struct repository *repo) 16{ 17 char *inpath = NULL; 18 const char *message = NULL; 19 char *into_name = NULL; 20 int shortlog_len = -1; 21 int merge_log_config = -1; 22 struct option options[] = { 23 { 24 .type = OPTION_INTEGER, 25 .long_name = "log", 26 .value = &shortlog_len, 27 .precision = sizeof(shortlog_len), 28 .argh = N_("n"), 29 .help = N_("populate log with at most <n> entries from shortlog"), 30 .flags = PARSE_OPT_OPTARG, 31 .defval = DEFAULT_MERGE_LOG_LEN, 32 }, 33 { 34 .type = OPTION_INTEGER, 35 .long_name = "summary", 36 .value = &shortlog_len, 37 .precision = sizeof(shortlog_len), 38 .argh = N_("n"), 39 .help = N_("alias for --log (deprecated)"), 40 .flags = PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, 41 .defval = DEFAULT_MERGE_LOG_LEN, 42 }, 43 OPT_STRING('m', "message", &message, N_("text"), 44 N_("use <text> as start of message")), 45 OPT_STRING(0, "into-name", &into_name, N_("name"), 46 N_("use <name> instead of the real target branch")), 47 OPT_FILENAME('F', "file", &inpath, N_("file to read from")), 48 OPT_END() 49 }; 50 51 FILE *in = stdin; 52 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT; 53 int ret; 54 struct fmt_merge_msg_opts opts; 55 56 repo_config(repo, fmt_merge_msg_config, &merge_log_config); 57 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage, 58 0); 59 if (argc > 0) 60 usage_with_options(fmt_merge_msg_usage, options); 61 if (shortlog_len < 0) 62 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; 63 64 if (inpath && strcmp(inpath, "-")) { 65 in = fopen(inpath, "r"); 66 if (!in) 67 die_errno("cannot open '%s'", inpath); 68 } 69 70 if (strbuf_read(&input, fileno(in), 0) < 0) 71 die_errno("could not read input file"); 72 73 if (message) 74 strbuf_addstr(&output, message); 75 76 memset(&opts, 0, sizeof(opts)); 77 opts.add_title = !message; 78 opts.credit_people = 1; 79 opts.shortlog_len = shortlog_len; 80 opts.into_name = into_name; 81 82 ret = fmt_merge_msg(&input, &output, &opts); 83 if (ret) 84 return ret; 85 write_in_full(STDOUT_FILENO, output.buf, output.len); 86 87 strbuf_release(&input); 88 strbuf_release(&output); 89 free(inpath); 90 return 0; 91}