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;
188189
branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
190if (!branch || branch->type != OBJ_COMMIT)
191return;
192193
setup_revisions(0, NULL, rev, NULL);
194rev->ignore_merges = 1;
195add_pending_object(rev, branch, name);
196add_pending_object(rev, &head->object, "^HEAD");
197head->object.flags |= UNINTERESTING;
198if (prepare_revision_walk(rev))
199die("revision walk setup failed");
200while ((commit = get_revision(rev)) != NULL) {
201char *oneline, *bol, *eol;
202203
/* ignore merges */
204if (commit->parents && commit->parents->next)
205continue;
206207
count++;
208if (subjects.nr > limit)
209continue;
210211
bol = strstr(commit->buffer, "\n\n");
212if (bol) {
213unsigned char c;
214do {
215c = *++bol;
216} while (isspace(c));
217if (!c)
218bol = NULL;
219}
220221
if (!bol) {
222append_to_list(&subjects, xstrdup(sha1_to_hex(
223commit->object.sha1)),
224NULL);
225continue;
226}
227228
eol = strchr(bol, '\n');
229if (eol) {
230oneline = xmemdupz(bol, eol - bol);
231} else {
232oneline = xstrdup(bol);
233}
234append_to_list(&subjects, oneline, NULL);
235}
236237
if (count > limit)
238strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
239else
240strbuf_addf(out, "\n* %s:\n", name);
241242
for (i = 0; i < subjects.nr; i++)
243if (i >= limit)
244strbuf_addf(out, " ...\n");
245else
246strbuf_addf(out, " %s\n", subjects.list[i]);
247248
clear_commit_marks((struct commit *)branch, flags);
249clear_commit_marks(head, flags);
250free_commit_list(rev->commits);
251rev->commits = NULL;
252rev->pending.nr = 0;
253254
free_list(&subjects);
255}
256257
int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
258int limit = 20, i = 0, pos = 0;
259char line[1024];
260char *p = line, *sep = "";
261unsigned char head_sha1[20];
262const char *current_branch;
263264
/* get current branch */
265current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
266if (!current_branch)
267die("No current branch");
268if (!prefixcmp(current_branch, "refs/heads/"))
269current_branch += 11;
270271
/* get a line */
272while (pos < in->len) {
273int len;
274char *newline;
275276
p = in->buf + pos;
277newline = strchr(p, '\n');
278len = newline ? newline - p : strlen(p);
279pos += len + !!newline;
280i++;
281p[len] = 0;
282if (handle_line(p))
283die ("Error in line %d: %.*s", i, len, p);
284}
285286
strbuf_addstr(out, "Merge ");
287for (i = 0; i < srcs.nr; i++) {
288struct src_data *src_data = srcs.payload[i];
289const char *subsep = "";
290291
strbuf_addstr(out, sep);
292sep = "; ";
293294
if (src_data->head_status == 1) {
295strbuf_addstr(out, srcs.list[i]);
296continue;
297}
298if (src_data->head_status == 3) {
299subsep = ", ";
300strbuf_addstr(out, "HEAD");
301}
302if (src_data->branch.nr) {
303strbuf_addstr(out, subsep);
304subsep = ", ";
305print_joined("branch ", "branches ", &src_data->branch,
306out);
307}
308if (src_data->r_branch.nr) {
309strbuf_addstr(out, subsep);
310subsep = ", ";
311print_joined("remote branch ", "remote branches ",
312&src_data->r_branch, out);
313}
314if (src_data->tag.nr) {
315strbuf_addstr(out, subsep);
316subsep = ", ";
317print_joined("tag ", "tags ", &src_data->tag, out);
318}
319if (src_data->generic.nr) {
320strbuf_addstr(out, subsep);
321print_joined("commit ", "commits ", &src_data->generic,
322out);
323}
324if (strcmp(".", srcs.list[i]))
325strbuf_addf(out, " of %s", srcs.list[i]);
326}
327328
if (!strcmp("master", current_branch))
329strbuf_addch(out, '\n');
330else
331strbuf_addf(out, " into %s\n", current_branch);
332333
if (merge_summary) {
334struct commit *head;
335struct rev_info rev;
336337
head = lookup_commit(head_sha1);
338init_revisions(&rev, NULL);
339rev.commit_format = CMIT_FMT_ONELINE;
340rev.ignore_merges = 1;
341rev.limited = 1;
342343
for (i = 0; i < origins.nr; i++)
344shortlog(origins.list[i], origins.payload[i],
345head, &rev, limit, out);
346}
347return 0;
348}
349350
int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
351{
352const char *inpath = NULL;
353struct option options[] = {
354OPT_BOOLEAN(0, "log", &merge_summary, "populate log with the shortlog"),
355OPT_BOOLEAN(0, "summary", &merge_summary, "alias for --log"),
356OPT_STRING('F', "file", &inpath, "file", "file to read from"),
357OPT_END()
358};
359360
FILE *in = stdin;
361struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
362int ret;
363364
git_config(fmt_merge_msg_config, NULL);
365argc = parse_options(argc, argv, options, fmt_merge_msg_usage, 0);
366if (argc > 0)
367usage_with_options(fmt_merge_msg_usage, options);
368369
if (inpath && strcmp(inpath, "-")) {
370in = fopen(inpath, "r");
371if (!in)
372die("cannot open %s", inpath);
373}
374375
if (strbuf_read(&input, fileno(in), 0) < 0)
376die("could not read input file %s", strerror(errno));
377378
ret = fmt_merge_msg(merge_summary, &input, &output);
379if (ret)
380return ret;
381write_in_full(STDOUT_FILENO, output.buf, output.len);
382return 0;
383}