7 #include "string-list.h"
9 static const char * const fmt_merge_msg_usage[] = {
10 "git fmt-merge-msg [--log|--no-log] [--file <file>]",
14 static int merge_summary;
16 static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
18 static int found_merge_log = 0;
19 if (!strcmp("merge.log", key)) {
21 merge_summary = git_config_bool(key, value);
23 if (!found_merge_log && !strcmp("merge.summary", key))
24 merge_summary = git_config_bool(key, value);
29 struct string_list branch, tag, r_branch, generic;
33 void init_src_data(struct src_data *data)
35 data->branch.strdup_strings = 1;
36 data->tag.strdup_strings = 1;
37 data->r_branch.strdup_strings = 1;
38 data->generic.strdup_strings = 1;
41 static struct string_list srcs = { NULL, 0, 0, 1 };
42 static struct string_list origins = { NULL, 0, 0, 1 };
44 static int handle_line(char *line)
46 int i, len = strlen(line);
49 struct src_data *src_data;
50 struct string_list_item *item;
53 if (len < 43 || line[40] != '\t')
56 if (!prefixcmp(line + 41, "not-for-merge"))
64 i = get_sha1(line, sha1);
69 if (line[len - 1] == '\n')
73 src = strstr(line, " of ");
83 item = unsorted_string_list_lookup(&srcs, src);
85 item = string_list_append(src, &srcs);
86 item->util = xcalloc(1, sizeof(struct src_data));
87 init_src_data(item->util);
89 src_data = item->util;
93 src_data->head_status |= 1;
94 } else if (!prefixcmp(line, "branch ")) {
96 string_list_append(origin, &src_data->branch);
97 src_data->head_status |= 2;
98 } else if (!prefixcmp(line, "tag ")) {
100 string_list_append(origin + 4, &src_data->tag);
101 src_data->head_status |= 2;
102 } else if (!prefixcmp(line, "remote branch ")) {
104 string_list_append(origin, &src_data->r_branch);
105 src_data->head_status |= 2;
108 string_list_append(line, &src_data->generic);
109 src_data->head_status |= 2;
112 if (!strcmp(".", src) || !strcmp(src, origin)) {
113 int len = strlen(origin);
114 if (origin[0] == '\'' && origin[len - 1] == '\'')
115 origin = xmemdupz(origin + 1, len - 2);
117 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
118 sprintf(new_origin, "%s of %s", origin, src);
121 string_list_append(origin, &origins)->util = sha1;
125 static void print_joined(const char *singular, const char *plural,
126 struct string_list *list, struct strbuf *out)
131 strbuf_addf(out, "%s%s", singular, list->items[0].string);
134 strbuf_addstr(out, plural);
135 for (i = 0; i < list->nr - 1; i++)
136 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
137 list->items[i].string);
138 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
142 static void shortlog(const char *name, unsigned char *sha1,
143 struct commit *head, struct rev_info *rev, int limit,
147 struct commit *commit;
148 struct object *branch;
149 struct string_list subjects = { NULL, 0, 0, 1 };
150 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
151 struct strbuf sb = STRBUF_INIT;
153 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
154 if (!branch || branch->type != OBJ_COMMIT)
157 setup_revisions(0, NULL, rev, NULL);
158 rev->ignore_merges = 1;
159 add_pending_object(rev, branch, name);
160 add_pending_object(rev, &head->object, "^HEAD");
161 head->object.flags |= UNINTERESTING;
162 if (prepare_revision_walk(rev))
163 die("revision walk setup failed");
164 while ((commit = get_revision(rev)) != NULL) {
165 struct pretty_print_context ctx = {0};
168 if (commit->parents && commit->parents->next)
172 if (subjects.nr > limit)
175 format_commit_message(commit, "%s", &sb, &ctx);
179 string_list_append(sha1_to_hex(commit->object.sha1),
182 string_list_append(strbuf_detach(&sb, NULL), &subjects);
186 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
188 strbuf_addf(out, "\n* %s:\n", name);
190 for (i = 0; i < subjects.nr; i++)
192 strbuf_addf(out, " ...\n");
194 strbuf_addf(out, " %s\n", subjects.items[i].string);
196 clear_commit_marks((struct commit *)branch, flags);
197 clear_commit_marks(head, flags);
198 free_commit_list(rev->commits);
202 string_list_clear(&subjects, 0);
205 int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
206 int limit = 20, i = 0, pos = 0;
208 unsigned char head_sha1[20];
209 const char *current_branch;
211 /* get current branch */
212 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
214 die("No current branch");
215 if (!prefixcmp(current_branch, "refs/heads/"))
216 current_branch += 11;
219 while (pos < in->len) {
221 char *newline, *p = in->buf + pos;
223 newline = strchr(p, '\n');
224 len = newline ? newline - p : strlen(p);
225 pos += len + !!newline;
229 die ("Error in line %d: %.*s", i, len, p);
235 strbuf_addstr(out, "Merge ");
236 for (i = 0; i < srcs.nr; i++) {
237 struct src_data *src_data = srcs.items[i].util;
238 const char *subsep = "";
240 strbuf_addstr(out, sep);
243 if (src_data->head_status == 1) {
244 strbuf_addstr(out, srcs.items[i].string);
247 if (src_data->head_status == 3) {
249 strbuf_addstr(out, "HEAD");
251 if (src_data->branch.nr) {
252 strbuf_addstr(out, subsep);
254 print_joined("branch ", "branches ", &src_data->branch,
257 if (src_data->r_branch.nr) {
258 strbuf_addstr(out, subsep);
260 print_joined("remote branch ", "remote branches ",
261 &src_data->r_branch, out);
263 if (src_data->tag.nr) {
264 strbuf_addstr(out, subsep);
266 print_joined("tag ", "tags ", &src_data->tag, out);
268 if (src_data->generic.nr) {
269 strbuf_addstr(out, subsep);
270 print_joined("commit ", "commits ", &src_data->generic,
273 if (strcmp(".", srcs.items[i].string))
274 strbuf_addf(out, " of %s", srcs.items[i].string);
277 if (!strcmp("master", current_branch))
278 strbuf_addch(out, '\n');
280 strbuf_addf(out, " into %s\n", current_branch);
286 head = lookup_commit(head_sha1);
287 init_revisions(&rev, NULL);
288 rev.commit_format = CMIT_FMT_ONELINE;
289 rev.ignore_merges = 1;
292 for (i = 0; i < origins.nr; i++)
293 shortlog(origins.items[i].string, origins.items[i].util,
294 head, &rev, limit, out);
299 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
301 const char *inpath = NULL;
302 struct option options[] = {
303 OPT_BOOLEAN(0, "log", &merge_summary, "populate log with the shortlog"),
304 { OPTION_BOOLEAN, 0, "summary", &merge_summary, NULL,
305 "alias for --log (deprecated)",
306 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
307 OPT_FILENAME('F', "file", &inpath, "file to read from"),
312 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
315 git_config(fmt_merge_msg_config, NULL);
316 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
319 usage_with_options(fmt_merge_msg_usage, options);
321 if (inpath && strcmp(inpath, "-")) {
322 in = fopen(inpath, "r");
324 die_errno("cannot open '%s'", inpath);
327 if (strbuf_read(&input, fileno(in), 0) < 0)
328 die_errno("could not read input file");
329 ret = fmt_merge_msg(merge_summary, &input, &output);
332 write_in_full(STDOUT_FILENO, output.buf, output.len);