1#include "builtin.h"
2#include "cache.h"
3#include "commit.h"
4#include "diff.h"
5#include "revision.h"
6#include "tag.h"
78
static const char * const fmt_merge_msg_usage[] = {
9"git fmt-merge-msg [--log|--no-log] [--file <file>]",
10NULL
11};
1213
static int merge_summary;
1415
static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
16{
17static int found_merge_log = 0;
18if (!strcmp("merge.log", key)) {
19found_merge_log = 1;
20merge_summary = git_config_bool(key, value);
21}
22if (!found_merge_log && !strcmp("merge.summary", key))
23merge_summary = git_config_bool(key, value);
24return 0;
25}
2627
struct list {
28char **list;
29void **payload;
30unsigned nr, alloc;
31};
3233
static void append_to_list(struct list *list, char *value, void *payload)
34{
35if (list->nr == list->alloc) {
36list->alloc += 32;
37list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
38list->payload = xrealloc(list->payload,
39sizeof(char *) * list->alloc);
40}
41list->payload[list->nr] = payload;
42list->list[list->nr++] = value;
43}
4445
static int find_in_list(struct list *list, char *value)
46{
47int i;
4849
for (i = 0; i < list->nr; i++)
50if (!strcmp(list->list[i], value))
51return i;
5253
return -1;
54}
5556
static void free_list(struct list *list)
57{
58int i;
5960
if (list->alloc == 0)
61return;
6263
for (i = 0; i < list->nr; i++) {
64free(list->list[i]);
65free(list->payload[i]);
66}
67free(list->list);
68free(list->payload);
69list->nr = list->alloc = 0;
70}
7172
struct src_data {
73struct list branch, tag, r_branch, generic;
74int head_status;
75};
7677
static struct list srcs = { NULL, NULL, 0, 0};
78static struct list origins = { NULL, NULL, 0, 0};
7980
static int handle_line(char *line)
81{
82int i, len = strlen(line);
83unsigned char *sha1;
84char *src, *origin;
85struct src_data *src_data;
86int pulling_head = 0;
8788
if (len < 43 || line[40] != '\t')
89return 1;
9091
if (!prefixcmp(line + 41, "not-for-merge"))
92return 0;
9394
if (line[41] != '\t')
95return 2;
9697
line[40] = 0;
98sha1 = xmalloc(20);
99i = get_sha1(line, sha1);
100line[40] = '\t';
101if (i)
102return 3;
103104
if (line[len - 1] == '\n')
105line[len - 1] = 0;
106line += 42;
107108
src = strstr(line, " of ");
109if (src) {
110*src = 0;
111src += 4;
112pulling_head = 0;
113} else {
114src = line;
115pulling_head = 1;
116}
117118
i = find_in_list(&srcs, src);
119if (i < 0) {
120i = srcs.nr;
121append_to_list(&srcs, xstrdup(src),
122xcalloc(1, sizeof(struct src_data)));
123}
124src_data = srcs.payload[i];
125126
if (pulling_head) {
127origin = xstrdup(src);
128src_data->head_status |= 1;
129} else if (!prefixcmp(line, "branch ")) {
130origin = xstrdup(line + 7);
131append_to_list(&src_data->branch, origin, NULL);
132src_data->head_status |= 2;
133} else if (!prefixcmp(line, "tag ")) {
134origin = line;
135append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
136src_data->head_status |= 2;
137} else if (!prefixcmp(line, "remote branch ")) {
138origin = xstrdup(line + 14);
139append_to_list(&src_data->r_branch, origin, NULL);
140src_data->head_status |= 2;
141} else {
142origin = xstrdup(src);
143append_to_list(&src_data->generic, xstrdup(line), NULL);
144src_data->head_status |= 2;
145}
146147
if (!strcmp(".", src) || !strcmp(src, origin)) {
148int len = strlen(origin);
149if (origin[0] == '\'' && origin[len - 1] == '\'') {
150origin = xmemdupz(origin + 1, len - 2);
151} else {
152origin = xstrdup(origin);
153}
154} else {
155char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
156sprintf(new_origin, "%s of %s", origin, src);
157origin = new_origin;
158}
159append_to_list(&origins, origin, sha1);
160return 0;
161}
162163
static void print_joined(const char *singular, const char *plural,
164struct list *list, struct strbuf *out)
165{
166if (list->nr == 0)
167return;
168if (list->nr == 1) {
169strbuf_addf(out, "%s%s", singular, list->list[0]);
170} else {
171int i;
172strbuf_addstr(out, plural);
173for (i = 0; i < list->nr - 1; i++)
174strbuf_addf(out, "%s%s", i > 0 ? ", " : "", list->list[i]);
175strbuf_addf(out, " and %s", list->list[list->nr - 1]);
176}
177}
178179
static void shortlog(const char *name, unsigned char *sha1,
180struct commit *head, struct rev_info *rev, int limit,
181struct strbuf *out)
182{
183int i, count = 0;
184struct commit *commit;
185struct object *branch;
186struct list subjects = { NULL, NULL, 0, 0 };
187int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
188struct strbuf sb = STRBUF_INIT;
189190
branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
191if (!branch || branch->type != OBJ_COMMIT)
192return;
193194
setup_revisions(0, NULL, rev, NULL);
195rev->ignore_merges = 1;
196add_pending_object(rev, branch, name);
197add_pending_object(rev, &head->object, "^HEAD");
198head->object.flags |= UNINTERESTING;
199if (prepare_revision_walk(rev))
200die("revision walk setup failed");
201while ((commit = get_revision(rev)) != NULL) {
202struct pretty_print_context ctx = {0};
203204
/* ignore merges */
205if (commit->parents && commit->parents->next)
206continue;
207208
count++;
209if (subjects.nr > limit)
210continue;
211212
format_commit_message(commit, "%s", &sb, &ctx);
213strbuf_ltrim(&sb);
214215
if (!sb.len)
216append_to_list(&subjects, xstrdup(sha1_to_hex(
217commit->object.sha1)),
218NULL);
219else
220append_to_list(&subjects, strbuf_detach(&sb, NULL),
221NULL);
222}
223224
if (count > limit)
225strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
226else
227strbuf_addf(out, "\n* %s:\n", name);
228229
for (i = 0; i < subjects.nr; i++)
230if (i >= limit)
231strbuf_addf(out, " ...\n");
232else
233strbuf_addf(out, " %s\n", subjects.list[i]);
234235
clear_commit_marks((struct commit *)branch, flags);
236clear_commit_marks(head, flags);
237free_commit_list(rev->commits);
238rev->commits = NULL;
239rev->pending.nr = 0;
240241
free_list(&subjects);
242}
243244
int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
245int limit = 20, i = 0, pos = 0;
246char *sep = "";
247unsigned char head_sha1[20];
248const char *current_branch;
249250
/* get current branch */
251current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
252if (!current_branch)
253die("No current branch");
254if (!prefixcmp(current_branch, "refs/heads/"))
255current_branch += 11;
256257
/* get a line */
258while (pos < in->len) {
259int len;
260char *newline, *p = in->buf + pos;
261262
newline = strchr(p, '\n');
263len = newline ? newline - p : strlen(p);
264pos += len + !!newline;
265i++;
266p[len] = 0;
267if (handle_line(p))
268die ("Error in line %d: %.*s", i, len, p);
269}
270271
if (!srcs.nr)
272return 0;
273274
strbuf_addstr(out, "Merge ");
275for (i = 0; i < srcs.nr; i++) {
276struct src_data *src_data = srcs.payload[i];
277const char *subsep = "";
278279
strbuf_addstr(out, sep);
280sep = "; ";
281282
if (src_data->head_status == 1) {
283strbuf_addstr(out, srcs.list[i]);
284continue;
285}
286if (src_data->head_status == 3) {
287subsep = ", ";
288strbuf_addstr(out, "HEAD");
289}
290if (src_data->branch.nr) {
291strbuf_addstr(out, subsep);
292subsep = ", ";
293print_joined("branch ", "branches ", &src_data->branch,
294out);
295}
296if (src_data->r_branch.nr) {
297strbuf_addstr(out, subsep);
298subsep = ", ";
299print_joined("remote branch ", "remote branches ",
300&src_data->r_branch, out);
301}
302if (src_data->tag.nr) {
303strbuf_addstr(out, subsep);
304subsep = ", ";
305print_joined("tag ", "tags ", &src_data->tag, out);
306}
307if (src_data->generic.nr) {
308strbuf_addstr(out, subsep);
309print_joined("commit ", "commits ", &src_data->generic,
310out);
311}
312if (strcmp(".", srcs.list[i]))
313strbuf_addf(out, " of %s", srcs.list[i]);
314}
315316
if (!strcmp("master", current_branch))
317strbuf_addch(out, '\n');
318else
319strbuf_addf(out, " into %s\n", current_branch);
320321
if (merge_summary) {
322struct commit *head;
323struct rev_info rev;
324325
head = lookup_commit(head_sha1);
326init_revisions(&rev, NULL);
327rev.commit_format = CMIT_FMT_ONELINE;
328rev.ignore_merges = 1;
329rev.limited = 1;
330331
for (i = 0; i < origins.nr; i++)
332shortlog(origins.list[i], origins.payload[i],
333head, &rev, limit, out);
334}
335return 0;
336}
337338
int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
339{
340const char *inpath = NULL;
341struct option options[] = {
342OPT_BOOLEAN(0, "log", &merge_summary, "populate log with the shortlog"),
343OPT_BOOLEAN(0, "summary", &merge_summary, "alias for --log"),
344OPT_FILENAME('F', "file", &inpath, "file to read from"),
345OPT_END()
346};
347348
FILE *in = stdin;
349struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
350int ret;
351352
git_config(fmt_merge_msg_config, NULL);
353argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
3540);
355if (argc > 0)
356usage_with_options(fmt_merge_msg_usage, options);
357358
if (inpath && strcmp(inpath, "-")) {
359in = fopen(inpath, "r");
360if (!in)
361die_errno("cannot open '%s'", inpath);
362}
363364
if (strbuf_read(&input, fileno(in), 0) < 0)
365die_errno("could not read input file");
366ret = fmt_merge_msg(merge_summary, &input, &output);
367if (ret)
368return ret;
369write_in_full(STDOUT_FILENO, output.buf, output.len);
370return 0;
371}