7b492f91771c62f5dce216a4a015e77eee375d4f
[git/git.git] / builtin / fmt-merge-msg.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "tag.h"
7 #include "string-list.h"
8
9 static const char * const fmt_merge_msg_usage[] = {
10 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
11 NULL
12 };
13
14 static int shortlog_len;
15
16 static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
17 {
18 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
19 int is_bool;
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;
25 }
26 return 0;
27 }
28
29 /* merge data per repository where the merged tips came from */
30 struct src_data {
31 struct string_list branch, tag, r_branch, generic;
32 int head_status;
33 };
34
35 static void init_src_data(struct src_data *data)
36 {
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;
41 }
42
43 static struct string_list srcs = STRING_LIST_INIT_DUP;
44 static struct string_list origins = STRING_LIST_INIT_DUP;
45
46 static int handle_line(char *line)
47 {
48 int i, len = strlen(line);
49 unsigned char *sha1;
50 char *src, *origin;
51 struct src_data *src_data;
52 struct string_list_item *item;
53 int pulling_head = 0;
54
55 if (len < 43 || line[40] != '\t')
56 return 1;
57
58 if (!prefixcmp(line + 41, "not-for-merge"))
59 return 0;
60
61 if (line[41] != '\t')
62 return 2;
63
64 line[40] = 0;
65 sha1 = xmalloc(20);
66 i = get_sha1(line, sha1);
67 line[40] = '\t';
68 if (i)
69 return 3;
70
71 if (line[len - 1] == '\n')
72 line[len - 1] = 0;
73 line += 42;
74
75 /*
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.
79 */
80 src = strstr(line, " of ");
81 if (src) {
82 *src = 0;
83 src += 4;
84 pulling_head = 0;
85 } else {
86 src = line;
87 pulling_head = 1;
88 }
89
90 item = unsorted_string_list_lookup(&srcs, src);
91 if (!item) {
92 item = string_list_append(&srcs, src);
93 item->util = xcalloc(1, sizeof(struct src_data));
94 init_src_data(item->util);
95 }
96 src_data = item->util;
97
98 if (pulling_head) {
99 origin = src;
100 src_data->head_status |= 1;
101 } else if (!prefixcmp(line, "branch ")) {
102 origin = line + 7;
103 string_list_append(&src_data->branch, origin);
104 src_data->head_status |= 2;
105 } else if (!prefixcmp(line, "tag ")) {
106 origin = line;
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;
113 } else {
114 origin = src;
115 string_list_append(&src_data->generic, line);
116 src_data->head_status |= 2;
117 }
118
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);
123 } else {
124 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
125 sprintf(new_origin, "%s of %s", origin, src);
126 origin = new_origin;
127 }
128 string_list_append(&origins, origin)->util = sha1;
129 return 0;
130 }
131
132 static void print_joined(const char *singular, const char *plural,
133 struct string_list *list, struct strbuf *out)
134 {
135 if (list->nr == 0)
136 return;
137 if (list->nr == 1) {
138 strbuf_addf(out, "%s%s", singular, list->items[0].string);
139 } else {
140 int i;
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);
146 }
147 }
148
149 static void shortlog(const char *name, unsigned char *sha1,
150 struct commit *head, struct rev_info *rev, int limit,
151 struct strbuf *out)
152 {
153 int i, count = 0;
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;
159
160 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
161 if (!branch || branch->type != OBJ_COMMIT)
162 return;
163
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};
173
174 /* ignore merges */
175 if (commit->parents && commit->parents->next)
176 continue;
177
178 count++;
179 if (subjects.nr > limit)
180 continue;
181
182 format_commit_message(commit, "%s", &sb, &ctx);
183 strbuf_ltrim(&sb);
184
185 if (!sb.len)
186 string_list_append(&subjects,
187 sha1_to_hex(commit->object.sha1));
188 else
189 string_list_append(&subjects, strbuf_detach(&sb, NULL));
190 }
191
192 if (count > limit)
193 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
194 else
195 strbuf_addf(out, "\n* %s:\n", name);
196
197 for (i = 0; i < subjects.nr; i++)
198 if (i >= limit)
199 strbuf_addf(out, " ...\n");
200 else
201 strbuf_addf(out, " %s\n", subjects.items[i].string);
202
203 clear_commit_marks((struct commit *)branch, flags);
204 clear_commit_marks(head, flags);
205 free_commit_list(rev->commits);
206 rev->commits = NULL;
207 rev->pending.nr = 0;
208
209 string_list_clear(&subjects, 0);
210 }
211
212 static void do_fmt_merge_msg_title(struct strbuf *out,
213 const char *current_branch) {
214 int i = 0;
215 char *sep = "";
216
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 = "";
221
222 strbuf_addstr(out, sep);
223 sep = "; ";
224
225 if (src_data->head_status == 1) {
226 strbuf_addstr(out, srcs.items[i].string);
227 continue;
228 }
229 if (src_data->head_status == 3) {
230 subsep = ", ";
231 strbuf_addstr(out, "HEAD");
232 }
233 if (src_data->branch.nr) {
234 strbuf_addstr(out, subsep);
235 subsep = ", ";
236 print_joined("branch ", "branches ", &src_data->branch,
237 out);
238 }
239 if (src_data->r_branch.nr) {
240 strbuf_addstr(out, subsep);
241 subsep = ", ";
242 print_joined("remote-tracking branch ", "remote-tracking branches ",
243 &src_data->r_branch, out);
244 }
245 if (src_data->tag.nr) {
246 strbuf_addstr(out, subsep);
247 subsep = ", ";
248 print_joined("tag ", "tags ", &src_data->tag, out);
249 }
250 if (src_data->generic.nr) {
251 strbuf_addstr(out, subsep);
252 print_joined("commit ", "commits ", &src_data->generic,
253 out);
254 }
255 if (strcmp(".", srcs.items[i].string))
256 strbuf_addf(out, " of %s", srcs.items[i].string);
257 }
258
259 if (!strcmp("master", current_branch))
260 strbuf_addch(out, '\n');
261 else
262 strbuf_addf(out, " into %s\n", current_branch);
263 }
264
265 static int do_fmt_merge_msg(int merge_title, struct strbuf *in,
266 struct strbuf *out, int shortlog_len) {
267 int i = 0, pos = 0;
268 unsigned char head_sha1[20];
269 const char *current_branch;
270
271 /* get current branch */
272 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
273 if (!current_branch)
274 die("No current branch");
275 if (!prefixcmp(current_branch, "refs/heads/"))
276 current_branch += 11;
277
278 /* get a line */
279 while (pos < in->len) {
280 int len;
281 char *newline, *p = in->buf + pos;
282
283 newline = strchr(p, '\n');
284 len = newline ? newline - p : strlen(p);
285 pos += len + !!newline;
286 i++;
287 p[len] = 0;
288 if (handle_line(p))
289 die ("Error in line %d: %.*s", i, len, p);
290 }
291
292 if (merge_title && srcs.nr)
293 do_fmt_merge_msg_title(out, current_branch);
294
295 if (shortlog_len) {
296 struct commit *head;
297 struct rev_info rev;
298
299 head = lookup_commit_or_die(head_sha1, "HEAD");
300 init_revisions(&rev, NULL);
301 rev.commit_format = CMIT_FMT_ONELINE;
302 rev.ignore_merges = 1;
303 rev.limited = 1;
304
305 if (suffixcmp(out->buf, "\n"))
306 strbuf_addch(out, '\n');
307
308 for (i = 0; i < origins.nr; i++)
309 shortlog(origins.items[i].string, origins.items[i].util,
310 head, &rev, shortlog_len, out);
311 }
312 if (out->len && out->buf[out->len-1] != '\n')
313 strbuf_addch(out, '\n');
314 return 0;
315 }
316
317 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
318 int merge_title, int shortlog_len) {
319 return do_fmt_merge_msg(merge_title, in, out, shortlog_len);
320 }
321
322 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
323 {
324 const char *inpath = NULL;
325 const char *message = NULL;
326 struct option options[] = {
327 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
328 "populate log with at most <n> entries from shortlog",
329 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
330 { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
331 "alias for --log (deprecated)",
332 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
333 DEFAULT_MERGE_LOG_LEN },
334 OPT_STRING('m', "message", &message, "text",
335 "use <text> as start of message"),
336 OPT_FILENAME('F', "file", &inpath, "file to read from"),
337 OPT_END()
338 };
339
340 FILE *in = stdin;
341 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
342 int ret;
343
344 git_config(fmt_merge_msg_config, NULL);
345 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
346 0);
347 if (argc > 0)
348 usage_with_options(fmt_merge_msg_usage, options);
349
350 if (shortlog_len < 0)
351 die("Negative --log=%d", shortlog_len);
352
353 if (inpath && strcmp(inpath, "-")) {
354 in = fopen(inpath, "r");
355 if (!in)
356 die_errno("cannot open '%s'", inpath);
357 }
358
359 if (strbuf_read(&input, fileno(in), 0) < 0)
360 die_errno("could not read input file");
361
362 if (message)
363 strbuf_addstr(&output, message);
364 ret = fmt_merge_msg(&input, &output,
365 message ? 0 : 1,
366 shortlog_len);
367
368 if (ret)
369 return ret;
370 write_in_full(STDOUT_FILENO, output.buf, output.len);
371 return 0;
372 }