7 #include "string-list.h"
9 static const char * const fmt_merge_msg_usage[] = {
10 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
14 static int shortlog_len;
16 static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
18 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
20 shortlog_len = git_config_bool_or_int(key, value, &is_bool);
21 if (!is_bool && shortlog_len < 0)
22 return error("%s: negative length %s", key, value);
23 if (is_bool && shortlog_len)
24 shortlog_len = DEFAULT_MERGE_LOG_LEN;
29 /* merge data per repository where the merged tips came from */
31 struct string_list branch, tag, r_branch, generic;
35 static void init_src_data(struct src_data *data)
37 data->branch.strdup_strings = 1;
38 data->tag.strdup_strings = 1;
39 data->r_branch.strdup_strings = 1;
40 data->generic.strdup_strings = 1;
43 static struct string_list srcs = STRING_LIST_INIT_DUP;
44 static struct string_list origins = STRING_LIST_INIT_DUP;
46 static int handle_line(char *line)
48 int i, len = strlen(line);
51 struct src_data *src_data;
52 struct string_list_item *item;
55 if (len < 43 || line[40] != '\t')
58 if (!prefixcmp(line + 41, "not-for-merge"))
66 i = get_sha1(line, sha1);
71 if (line[len - 1] == '\n')
76 * At this point, line points at the beginning of comment e.g.
77 * "branch 'frotz' of git://that/repository.git".
78 * Find the repository name and point it with src.
80 src = strstr(line, " of ");
90 item = unsorted_string_list_lookup(&srcs, src);
92 item = string_list_append(&srcs, src);
93 item->util = xcalloc(1, sizeof(struct src_data));
94 init_src_data(item->util);
96 src_data = item->util;
100 src_data->head_status |= 1;
101 } else if (!prefixcmp(line, "branch ")) {
103 string_list_append(&src_data->branch, origin);
104 src_data->head_status |= 2;
105 } else if (!prefixcmp(line, "tag ")) {
107 string_list_append(&src_data->tag, origin + 4);
108 src_data->head_status |= 2;
109 } else if (!prefixcmp(line, "remote-tracking branch ")) {
110 origin = line + strlen("remote-tracking branch ");
111 string_list_append(&src_data->r_branch, origin);
112 src_data->head_status |= 2;
115 string_list_append(&src_data->generic, line);
116 src_data->head_status |= 2;
119 if (!strcmp(".", src) || !strcmp(src, origin)) {
120 int len = strlen(origin);
121 if (origin[0] == '\'' && origin[len - 1] == '\'')
122 origin = xmemdupz(origin + 1, len - 2);
124 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
125 sprintf(new_origin, "%s of %s", origin, src);
128 string_list_append(&origins, origin)->util = sha1;
132 static void print_joined(const char *singular, const char *plural,
133 struct string_list *list, struct strbuf *out)
138 strbuf_addf(out, "%s%s", singular, list->items[0].string);
141 strbuf_addstr(out, plural);
142 for (i = 0; i < list->nr - 1; i++)
143 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
144 list->items[i].string);
145 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
149 static void shortlog(const char *name, unsigned char *sha1,
150 struct commit *head, struct rev_info *rev, int limit,
154 struct commit *commit;
155 struct object *branch;
156 struct string_list subjects = STRING_LIST_INIT_DUP;
157 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
158 struct strbuf sb = STRBUF_INIT;
160 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
161 if (!branch || branch->type != OBJ_COMMIT)
164 setup_revisions(0, NULL, rev, NULL);
165 rev->ignore_merges = 1;
166 add_pending_object(rev, branch, name);
167 add_pending_object(rev, &head->object, "^HEAD");
168 head->object.flags |= UNINTERESTING;
169 if (prepare_revision_walk(rev))
170 die("revision walk setup failed");
171 while ((commit = get_revision(rev)) != NULL) {
172 struct pretty_print_context ctx = {0};
175 if (commit->parents && commit->parents->next)
179 if (subjects.nr > limit)
182 format_commit_message(commit, "%s", &sb, &ctx);
186 string_list_append(&subjects,
187 sha1_to_hex(commit->object.sha1));
189 string_list_append(&subjects, strbuf_detach(&sb, NULL));
193 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
195 strbuf_addf(out, "\n* %s:\n", name);
197 for (i = 0; i < subjects.nr; i++)
199 strbuf_addf(out, " ...\n");
201 strbuf_addf(out, " %s\n", subjects.items[i].string);
203 clear_commit_marks((struct commit *)branch, flags);
204 clear_commit_marks(head, flags);
205 free_commit_list(rev->commits);
209 string_list_clear(&subjects, 0);
212 static void fmt_merge_msg_title(struct strbuf *out,
213 const char *current_branch) {
217 strbuf_addstr(out, "Merge ");
218 for (i = 0; i < srcs.nr; i++) {
219 struct src_data *src_data = srcs.items[i].util;
220 const char *subsep = "";
222 strbuf_addstr(out, sep);
225 if (src_data->head_status == 1) {
226 strbuf_addstr(out, srcs.items[i].string);
229 if (src_data->head_status == 3) {
231 strbuf_addstr(out, "HEAD");
233 if (src_data->branch.nr) {
234 strbuf_addstr(out, subsep);
236 print_joined("branch ", "branches ", &src_data->branch,
239 if (src_data->r_branch.nr) {
240 strbuf_addstr(out, subsep);
242 print_joined("remote-tracking branch ", "remote-tracking branches ",
243 &src_data->r_branch, out);
245 if (src_data->tag.nr) {
246 strbuf_addstr(out, subsep);
248 print_joined("tag ", "tags ", &src_data->tag, out);
250 if (src_data->generic.nr) {
251 strbuf_addstr(out, subsep);
252 print_joined("commit ", "commits ", &src_data->generic,
255 if (strcmp(".", srcs.items[i].string))
256 strbuf_addf(out, " of %s", srcs.items[i].string);
259 if (!strcmp("master", current_branch))
260 strbuf_addch(out, '\n');
262 strbuf_addf(out, " into %s\n", current_branch);
265 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
266 struct fmt_merge_msg_opts *opts)
269 unsigned char head_sha1[20];
270 const char *current_branch;
272 /* get current branch */
273 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
275 die("No current branch");
276 if (!prefixcmp(current_branch, "refs/heads/"))
277 current_branch += 11;
280 while (pos < in->len) {
282 char *newline, *p = in->buf + pos;
284 newline = strchr(p, '\n');
285 len = newline ? newline - p : strlen(p);
286 pos += len + !!newline;
290 die ("Error in line %d: %.*s", i, len, p);
293 if (opts->add_title && srcs.nr)
294 fmt_merge_msg_title(out, current_branch);
296 if (opts->shortlog_len) {
300 head = lookup_commit_or_die(head_sha1, "HEAD");
301 init_revisions(&rev, NULL);
302 rev.commit_format = CMIT_FMT_ONELINE;
303 rev.ignore_merges = 1;
306 if (suffixcmp(out->buf, "\n"))
307 strbuf_addch(out, '\n');
309 for (i = 0; i < origins.nr; i++)
310 shortlog(origins.items[i].string, origins.items[i].util,
311 head, &rev, opts->shortlog_len, out);
313 if (out->len && out->buf[out->len-1] != '\n')
314 strbuf_addch(out, '\n');
318 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
320 const char *inpath = NULL;
321 const char *message = NULL;
322 struct option options[] = {
323 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
324 "populate log with at most <n> entries from shortlog",
325 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
326 { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
327 "alias for --log (deprecated)",
328 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
329 DEFAULT_MERGE_LOG_LEN },
330 OPT_STRING('m', "message", &message, "text",
331 "use <text> as start of message"),
332 OPT_FILENAME('F', "file", &inpath, "file to read from"),
337 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
339 struct fmt_merge_msg_opts opts;
341 git_config(fmt_merge_msg_config, NULL);
342 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
345 usage_with_options(fmt_merge_msg_usage, options);
347 if (shortlog_len < 0)
348 die("Negative --log=%d", shortlog_len);
350 if (inpath && strcmp(inpath, "-")) {
351 in = fopen(inpath, "r");
353 die_errno("cannot open '%s'", inpath);
356 if (strbuf_read(&input, fileno(in), 0) < 0)
357 die_errno("could not read input file");
360 strbuf_addstr(&output, message);
362 memset(&opts, 0, sizeof(opts));
363 opts.add_title = !message;
364 opts.shortlog_len = shortlog_len;
366 ret = fmt_merge_msg(&input, &output, &opts);
369 write_in_full(STDOUT_FILENO, output.buf, output.len);