Merge branch 'jc/request-pull-show-head-4'
[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 #include "branch.h"
9 #include "fmt-merge-msg.h"
10
11 static const char * const fmt_merge_msg_usage[] = {
12 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
13 NULL
14 };
15
16 static int use_branch_desc;
17
18 int fmt_merge_msg_config(const char *key, const char *value, void *cb)
19 {
20 if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) {
21 int is_bool;
22 merge_log_config = git_config_bool_or_int(key, value, &is_bool);
23 if (!is_bool && merge_log_config < 0)
24 return error("%s: negative length %s", key, value);
25 if (is_bool && merge_log_config)
26 merge_log_config = DEFAULT_MERGE_LOG_LEN;
27 } else if (!strcmp(key, "merge.branchdesc")) {
28 use_branch_desc = git_config_bool(key, value);
29 }
30 return 0;
31 }
32
33 struct src_data {
34 struct string_list branch, tag, r_branch, generic;
35 int head_status;
36 };
37
38 struct origin_data {
39 unsigned char sha1[20];
40 unsigned is_local_branch:1;
41 };
42
43 static void init_src_data(struct src_data *data)
44 {
45 data->branch.strdup_strings = 1;
46 data->tag.strdup_strings = 1;
47 data->r_branch.strdup_strings = 1;
48 data->generic.strdup_strings = 1;
49 }
50
51 static struct string_list srcs = STRING_LIST_INIT_DUP;
52 static struct string_list origins = STRING_LIST_INIT_DUP;
53
54 static int handle_line(char *line)
55 {
56 int i, len = strlen(line);
57 struct origin_data *origin_data;
58 char *src, *origin;
59 struct src_data *src_data;
60 struct string_list_item *item;
61 int pulling_head = 0;
62
63 if (len < 43 || line[40] != '\t')
64 return 1;
65
66 if (!prefixcmp(line + 41, "not-for-merge"))
67 return 0;
68
69 if (line[41] != '\t')
70 return 2;
71
72 line[40] = 0;
73 origin_data = xcalloc(1, sizeof(struct origin_data));
74 i = get_sha1(line, origin_data->sha1);
75 line[40] = '\t';
76 if (i) {
77 free(origin_data);
78 return 3;
79 }
80
81 if (line[len - 1] == '\n')
82 line[len - 1] = 0;
83 line += 42;
84
85 src = strstr(line, " of ");
86 if (src) {
87 *src = 0;
88 src += 4;
89 pulling_head = 0;
90 } else {
91 src = line;
92 pulling_head = 1;
93 }
94
95 item = unsorted_string_list_lookup(&srcs, src);
96 if (!item) {
97 item = string_list_append(&srcs, src);
98 item->util = xcalloc(1, sizeof(struct src_data));
99 init_src_data(item->util);
100 }
101 src_data = item->util;
102
103 if (pulling_head) {
104 origin = src;
105 src_data->head_status |= 1;
106 } else if (!prefixcmp(line, "branch ")) {
107 origin_data->is_local_branch = 1;
108 origin = line + 7;
109 string_list_append(&src_data->branch, origin);
110 src_data->head_status |= 2;
111 } else if (!prefixcmp(line, "tag ")) {
112 origin = line;
113 string_list_append(&src_data->tag, origin + 4);
114 src_data->head_status |= 2;
115 } else if (!prefixcmp(line, "remote-tracking branch ")) {
116 origin = line + strlen("remote-tracking branch ");
117 string_list_append(&src_data->r_branch, origin);
118 src_data->head_status |= 2;
119 } else {
120 origin = src;
121 string_list_append(&src_data->generic, line);
122 src_data->head_status |= 2;
123 }
124
125 if (!strcmp(".", src) || !strcmp(src, origin)) {
126 int len = strlen(origin);
127 if (origin[0] == '\'' && origin[len - 1] == '\'')
128 origin = xmemdupz(origin + 1, len - 2);
129 } else {
130 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
131 sprintf(new_origin, "%s of %s", origin, src);
132 origin = new_origin;
133 }
134 if (strcmp(".", src))
135 origin_data->is_local_branch = 0;
136 string_list_append(&origins, origin)->util = origin_data;
137 return 0;
138 }
139
140 static void print_joined(const char *singular, const char *plural,
141 struct string_list *list, struct strbuf *out)
142 {
143 if (list->nr == 0)
144 return;
145 if (list->nr == 1) {
146 strbuf_addf(out, "%s%s", singular, list->items[0].string);
147 } else {
148 int i;
149 strbuf_addstr(out, plural);
150 for (i = 0; i < list->nr - 1; i++)
151 strbuf_addf(out, "%s%s", i > 0 ? ", " : "",
152 list->items[i].string);
153 strbuf_addf(out, " and %s", list->items[list->nr - 1].string);
154 }
155 }
156
157 static void add_branch_desc(struct strbuf *out, const char *name)
158 {
159 struct strbuf desc = STRBUF_INIT;
160
161 if (!read_branch_desc(&desc, name)) {
162 const char *bp = desc.buf;
163 while (*bp) {
164 const char *ep = strchrnul(bp, '\n');
165 if (*ep)
166 ep++;
167 strbuf_addf(out, " : %.*s", (int)(ep - bp), bp);
168 bp = ep;
169 }
170 if (out->buf[out->len - 1] != '\n')
171 strbuf_addch(out, '\n');
172 }
173 strbuf_release(&desc);
174 }
175
176 static void shortlog(const char *name,
177 struct origin_data *origin_data,
178 struct commit *head,
179 struct rev_info *rev, int limit,
180 struct strbuf *out)
181 {
182 int i, count = 0;
183 struct commit *commit;
184 struct object *branch;
185 struct string_list subjects = STRING_LIST_INIT_DUP;
186 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
187 struct strbuf sb = STRBUF_INIT;
188 const unsigned char *sha1 = origin_data->sha1;
189
190 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
191 if (!branch || branch->type != OBJ_COMMIT)
192 return;
193
194 setup_revisions(0, NULL, rev, NULL);
195 rev->ignore_merges = 1;
196 add_pending_object(rev, branch, name);
197 add_pending_object(rev, &head->object, "^HEAD");
198 head->object.flags |= UNINTERESTING;
199 if (prepare_revision_walk(rev))
200 die("revision walk setup failed");
201 while ((commit = get_revision(rev)) != NULL) {
202 struct pretty_print_context ctx = {0};
203
204 /* ignore merges */
205 if (commit->parents && commit->parents->next)
206 continue;
207
208 count++;
209 if (subjects.nr > limit)
210 continue;
211
212 format_commit_message(commit, "%s", &sb, &ctx);
213 strbuf_ltrim(&sb);
214
215 if (!sb.len)
216 string_list_append(&subjects,
217 sha1_to_hex(commit->object.sha1));
218 else
219 string_list_append(&subjects, strbuf_detach(&sb, NULL));
220 }
221
222 if (count > limit)
223 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
224 else
225 strbuf_addf(out, "\n* %s:\n", name);
226
227 if (origin_data->is_local_branch && use_branch_desc)
228 add_branch_desc(out, name);
229
230 for (i = 0; i < subjects.nr; i++)
231 if (i >= limit)
232 strbuf_addf(out, " ...\n");
233 else
234 strbuf_addf(out, " %s\n", subjects.items[i].string);
235
236 clear_commit_marks((struct commit *)branch, flags);
237 clear_commit_marks(head, flags);
238 free_commit_list(rev->commits);
239 rev->commits = NULL;
240 rev->pending.nr = 0;
241
242 string_list_clear(&subjects, 0);
243 }
244
245 static void do_fmt_merge_msg_title(struct strbuf *out,
246 const char *current_branch) {
247 int i = 0;
248 char *sep = "";
249
250 strbuf_addstr(out, "Merge ");
251 for (i = 0; i < srcs.nr; i++) {
252 struct src_data *src_data = srcs.items[i].util;
253 const char *subsep = "";
254
255 strbuf_addstr(out, sep);
256 sep = "; ";
257
258 if (src_data->head_status == 1) {
259 strbuf_addstr(out, srcs.items[i].string);
260 continue;
261 }
262 if (src_data->head_status == 3) {
263 subsep = ", ";
264 strbuf_addstr(out, "HEAD");
265 }
266 if (src_data->branch.nr) {
267 strbuf_addstr(out, subsep);
268 subsep = ", ";
269 print_joined("branch ", "branches ", &src_data->branch,
270 out);
271 }
272 if (src_data->r_branch.nr) {
273 strbuf_addstr(out, subsep);
274 subsep = ", ";
275 print_joined("remote-tracking branch ", "remote-tracking branches ",
276 &src_data->r_branch, out);
277 }
278 if (src_data->tag.nr) {
279 strbuf_addstr(out, subsep);
280 subsep = ", ";
281 print_joined("tag ", "tags ", &src_data->tag, out);
282 }
283 if (src_data->generic.nr) {
284 strbuf_addstr(out, subsep);
285 print_joined("commit ", "commits ", &src_data->generic,
286 out);
287 }
288 if (strcmp(".", srcs.items[i].string))
289 strbuf_addf(out, " of %s", srcs.items[i].string);
290 }
291
292 if (!strcmp("master", current_branch))
293 strbuf_addch(out, '\n');
294 else
295 strbuf_addf(out, " into %s\n", current_branch);
296 }
297
298 static int do_fmt_merge_msg(int merge_title, struct strbuf *in,
299 struct strbuf *out, int shortlog_len) {
300 int i = 0, pos = 0;
301 unsigned char head_sha1[20];
302 const char *current_branch;
303
304 /* get current branch */
305 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
306 if (!current_branch)
307 die("No current branch");
308 if (!prefixcmp(current_branch, "refs/heads/"))
309 current_branch += 11;
310
311 /* get a line */
312 while (pos < in->len) {
313 int len;
314 char *newline, *p = in->buf + pos;
315
316 newline = strchr(p, '\n');
317 len = newline ? newline - p : strlen(p);
318 pos += len + !!newline;
319 i++;
320 p[len] = 0;
321 if (handle_line(p))
322 die ("Error in line %d: %.*s", i, len, p);
323 }
324
325 if (!srcs.nr)
326 return 0;
327
328 if (merge_title)
329 do_fmt_merge_msg_title(out, current_branch);
330
331 if (shortlog_len) {
332 struct commit *head;
333 struct rev_info rev;
334
335 head = lookup_commit_or_die(head_sha1, "HEAD");
336 init_revisions(&rev, NULL);
337 rev.commit_format = CMIT_FMT_ONELINE;
338 rev.ignore_merges = 1;
339 rev.limited = 1;
340
341 if (suffixcmp(out->buf, "\n"))
342 strbuf_addch(out, '\n');
343
344 for (i = 0; i < origins.nr; i++)
345 shortlog(origins.items[i].string,
346 origins.items[i].util,
347 head, &rev, shortlog_len, out);
348 }
349 return 0;
350 }
351
352 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
353 int merge_title, int shortlog_len) {
354 return do_fmt_merge_msg(merge_title, in, out, shortlog_len);
355 }
356
357 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
358 {
359 const char *inpath = NULL;
360 const char *message = NULL;
361 int shortlog_len = -1;
362 struct option options[] = {
363 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
364 "populate log with at most <n> entries from shortlog",
365 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
366 { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
367 "alias for --log (deprecated)",
368 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
369 DEFAULT_MERGE_LOG_LEN },
370 OPT_STRING('m', "message", &message, "text",
371 "use <text> as start of message"),
372 OPT_FILENAME('F', "file", &inpath, "file to read from"),
373 OPT_END()
374 };
375
376 FILE *in = stdin;
377 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
378 int ret;
379
380 git_config(fmt_merge_msg_config, NULL);
381 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
382 0);
383 if (argc > 0)
384 usage_with_options(fmt_merge_msg_usage, options);
385 if (shortlog_len < 0)
386 shortlog_len = (merge_log_config > 0) ? merge_log_config : 0;
387 if (message && !shortlog_len) {
388 char nl = '\n';
389 write_in_full(STDOUT_FILENO, message, strlen(message));
390 write_in_full(STDOUT_FILENO, &nl, 1);
391 return 0;
392 }
393 if (shortlog_len < 0)
394 die("Negative --log=%d", shortlog_len);
395
396 if (inpath && strcmp(inpath, "-")) {
397 in = fopen(inpath, "r");
398 if (!in)
399 die_errno("cannot open '%s'", inpath);
400 }
401
402 if (strbuf_read(&input, fileno(in), 0) < 0)
403 die_errno("could not read input file");
404
405 if (message)
406 strbuf_addstr(&output, message);
407 ret = fmt_merge_msg(&input, &output,
408 message ? 0 : 1,
409 shortlog_len);
410
411 if (ret)
412 return ret;
413 write_in_full(STDOUT_FILENO, output.buf, output.len);
414 return 0;
415 }