fmt-merge-msg: package options into a structure
[git/git.git] / builtin / fmt-merge-msg.c
CommitLineData
25f38f06 1#include "builtin.h"
00449f99
JS
2#include "cache.h"
3#include "commit.h"
4#include "diff.h"
5#include "revision.h"
6#include "tag.h"
fcb243f7 7#include "string-list.h"
00449f99 8
c8ef0383 9static const char * const fmt_merge_msg_usage[] = {
96e9420c 10 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
c8ef0383
PH
11 NULL
12};
00449f99 13
96e9420c 14static int shortlog_len;
00449f99 15
ef90d6d4 16static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
00449f99 17{
bda3b8ff
RR
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;
6cd9cfef 25 }
00449f99
JS
26 return 0;
27}
28
4c0ea82d 29/* merge data per repository where the merged tips came from */
fcb243f7
SB
30struct src_data {
31 struct string_list branch, tag, r_branch, generic;
32 int head_status;
00449f99
JS
33};
34
c2e86add 35static void init_src_data(struct src_data *data)
00449f99 36{
fcb243f7
SB
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;
00449f99
JS
41}
42
183113a5
TF
43static struct string_list srcs = STRING_LIST_INIT_DUP;
44static struct string_list origins = STRING_LIST_INIT_DUP;
00449f99
JS
45
46static 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;
fcb243f7 52 struct string_list_item *item;
e918c6ab 53 int pulling_head = 0;
00449f99
JS
54
55 if (len < 43 || line[40] != '\t')
56 return 1;
57
cc44c765 58 if (!prefixcmp(line + 41, "not-for-merge"))
00449f99
JS
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
4c0ea82d
JH
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 */
00449f99
JS
80 src = strstr(line, " of ");
81 if (src) {
82 *src = 0;
83 src += 4;
e918c6ab
JH
84 pulling_head = 0;
85 } else {
86 src = line;
87 pulling_head = 1;
88 }
00449f99 89
fcb243f7
SB
90 item = unsorted_string_list_lookup(&srcs, src);
91 if (!item) {
1d2f80fa 92 item = string_list_append(&srcs, src);
fcb243f7
SB
93 item->util = xcalloc(1, sizeof(struct src_data));
94 init_src_data(item->util);
00449f99 95 }
fcb243f7 96 src_data = item->util;
00449f99 97
e918c6ab 98 if (pulling_head) {
fcb243f7 99 origin = src;
e918c6ab 100 src_data->head_status |= 1;
cc44c765 101 } else if (!prefixcmp(line, "branch ")) {
fcb243f7 102 origin = line + 7;
1d2f80fa 103 string_list_append(&src_data->branch, origin);
00449f99 104 src_data->head_status |= 2;
cc44c765 105 } else if (!prefixcmp(line, "tag ")) {
00449f99 106 origin = line;
1d2f80fa 107 string_list_append(&src_data->tag, origin + 4);
00449f99 108 src_data->head_status |= 2;
13931236
MM
109 } else if (!prefixcmp(line, "remote-tracking branch ")) {
110 origin = line + strlen("remote-tracking branch ");
1d2f80fa 111 string_list_append(&src_data->r_branch, origin);
00449f99 112 src_data->head_status |= 2;
00449f99 113 } else {
fcb243f7 114 origin = src;
1d2f80fa 115 string_list_append(&src_data->generic, line);
00449f99
JS
116 src_data->head_status |= 2;
117 }
118
119 if (!strcmp(".", src) || !strcmp(src, origin)) {
120 int len = strlen(origin);
fcb243f7 121 if (origin[0] == '\'' && origin[len - 1] == '\'')
182af834 122 origin = xmemdupz(origin + 1, len - 2);
00449f99 123 } else {
2d7320d0 124 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
00449f99
JS
125 sprintf(new_origin, "%s of %s", origin, src);
126 origin = new_origin;
127 }
1d2f80fa 128 string_list_append(&origins, origin)->util = sha1;
00449f99
JS
129 return 0;
130}
131
132static void print_joined(const char *singular, const char *plural,
fcb243f7 133 struct string_list *list, struct strbuf *out)
00449f99
JS
134{
135 if (list->nr == 0)
136 return;
137 if (list->nr == 1) {
fcb243f7 138 strbuf_addf(out, "%s%s", singular, list->items[0].string);
00449f99
JS
139 } else {
140 int i;
0b9a969e 141 strbuf_addstr(out, plural);
00449f99 142 for (i = 0; i < list->nr - 1; i++)
fcb243f7
SB
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);
00449f99
JS
146 }
147}
148
149static void shortlog(const char *name, unsigned char *sha1,
0b9a969e
MV
150 struct commit *head, struct rev_info *rev, int limit,
151 struct strbuf *out)
00449f99
JS
152{
153 int i, count = 0;
154 struct commit *commit;
155 struct object *branch;
183113a5 156 struct string_list subjects = STRING_LIST_INIT_DUP;
7dc0fe3b 157 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
15cb5007 158 struct strbuf sb = STRBUF_INIT;
00449f99
JS
159
160 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
1974632c 161 if (!branch || branch->type != OBJ_COMMIT)
00449f99
JS
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;
3d51e1b5
MK
169 if (prepare_revision_walk(rev))
170 die("revision walk setup failed");
00449f99 171 while ((commit = get_revision(rev)) != NULL) {
15cb5007 172 struct pretty_print_context ctx = {0};
00449f99
JS
173
174 /* ignore merges */
175 if (commit->parents && commit->parents->next)
176 continue;
177
178 count++;
179 if (subjects.nr > limit)
180 continue;
181
15cb5007
SB
182 format_commit_message(commit, "%s", &sb, &ctx);
183 strbuf_ltrim(&sb);
6a28518a 184
15cb5007 185 if (!sb.len)
1d2f80fa
JP
186 string_list_append(&subjects,
187 sha1_to_hex(commit->object.sha1));
15cb5007 188 else
1d2f80fa 189 string_list_append(&subjects, strbuf_detach(&sb, NULL));
00449f99
JS
190 }
191
192 if (count > limit)
0b9a969e 193 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
00449f99 194 else
0b9a969e 195 strbuf_addf(out, "\n* %s:\n", name);
00449f99
JS
196
197 for (i = 0; i < subjects.nr; i++)
198 if (i >= limit)
0b9a969e 199 strbuf_addf(out, " ...\n");
00449f99 200 else
fcb243f7 201 strbuf_addf(out, " %s\n", subjects.items[i].string);
00449f99
JS
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
fcb243f7 209 string_list_clear(&subjects, 0);
00449f99
JS
210}
211
cbda121c 212static void fmt_merge_msg_title(struct strbuf *out,
403994e8
TRC
213 const char *current_branch) {
214 int i = 0;
fd13b21f 215 char *sep = "";
419fe5bc 216
0b9a969e 217 strbuf_addstr(out, "Merge ");
00449f99 218 for (i = 0; i < srcs.nr; i++) {
fcb243f7 219 struct src_data *src_data = srcs.items[i].util;
00449f99
JS
220 const char *subsep = "";
221
0b9a969e 222 strbuf_addstr(out, sep);
00449f99
JS
223 sep = "; ";
224
225 if (src_data->head_status == 1) {
fcb243f7 226 strbuf_addstr(out, srcs.items[i].string);
00449f99
JS
227 continue;
228 }
229 if (src_data->head_status == 3) {
230 subsep = ", ";
0b9a969e 231 strbuf_addstr(out, "HEAD");
00449f99
JS
232 }
233 if (src_data->branch.nr) {
0b9a969e 234 strbuf_addstr(out, subsep);
00449f99 235 subsep = ", ";
0b9a969e
MV
236 print_joined("branch ", "branches ", &src_data->branch,
237 out);
00449f99
JS
238 }
239 if (src_data->r_branch.nr) {
0b9a969e 240 strbuf_addstr(out, subsep);
00449f99 241 subsep = ", ";
13931236 242 print_joined("remote-tracking branch ", "remote-tracking branches ",
0b9a969e 243 &src_data->r_branch, out);
00449f99
JS
244 }
245 if (src_data->tag.nr) {
0b9a969e 246 strbuf_addstr(out, subsep);
00449f99 247 subsep = ", ";
0b9a969e 248 print_joined("tag ", "tags ", &src_data->tag, out);
00449f99
JS
249 }
250 if (src_data->generic.nr) {
0b9a969e
MV
251 strbuf_addstr(out, subsep);
252 print_joined("commit ", "commits ", &src_data->generic,
253 out);
00449f99 254 }
fcb243f7
SB
255 if (strcmp(".", srcs.items[i].string))
256 strbuf_addf(out, " of %s", srcs.items[i].string);
00449f99
JS
257 }
258
259 if (!strcmp("master", current_branch))
0b9a969e 260 strbuf_addch(out, '\n');
00449f99 261 else
0b9a969e 262 strbuf_addf(out, " into %s\n", current_branch);
403994e8
TRC
263}
264
cbda121c
JH
265int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
266 struct fmt_merge_msg_opts *opts)
267{
1876166a 268 int i = 0, pos = 0;
403994e8
TRC
269 unsigned char head_sha1[20];
270 const char *current_branch;
271
272 /* get current branch */
273 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
274 if (!current_branch)
275 die("No current branch");
276 if (!prefixcmp(current_branch, "refs/heads/"))
277 current_branch += 11;
278
279 /* get a line */
280 while (pos < in->len) {
281 int len;
282 char *newline, *p = in->buf + pos;
283
284 newline = strchr(p, '\n');
285 len = newline ? newline - p : strlen(p);
286 pos += len + !!newline;
287 i++;
288 p[len] = 0;
289 if (handle_line(p))
290 die ("Error in line %d: %.*s", i, len, p);
291 }
292
cbda121c
JH
293 if (opts->add_title && srcs.nr)
294 fmt_merge_msg_title(out, current_branch);
00449f99 295
cbda121c 296 if (opts->shortlog_len) {
00449f99
JS
297 struct commit *head;
298 struct rev_info rev;
299
baf18fc2 300 head = lookup_commit_or_die(head_sha1, "HEAD");
0b9a969e 301 init_revisions(&rev, NULL);
00449f99
JS
302 rev.commit_format = CMIT_FMT_ONELINE;
303 rev.ignore_merges = 1;
304 rev.limited = 1;
305
f0ecac2b
TRC
306 if (suffixcmp(out->buf, "\n"))
307 strbuf_addch(out, '\n');
308
00449f99 309 for (i = 0; i < origins.nr; i++)
fcb243f7 310 shortlog(origins.items[i].string, origins.items[i].util,
cbda121c 311 head, &rev, opts->shortlog_len, out);
00449f99 312 }
4c0ea82d
JH
313 if (out->len && out->buf[out->len-1] != '\n')
314 strbuf_addch(out, '\n');
0b9a969e
MV
315 return 0;
316}
317
318int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
319{
c8ef0383 320 const char *inpath = NULL;
2102440c 321 const char *message = NULL;
c8ef0383 322 struct option options[] = {
96e9420c
RR
323 { OPTION_INTEGER, 0, "log", &shortlog_len, "n",
324 "populate log with at most <n> entries from shortlog",
325 PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN },
326 { OPTION_INTEGER, 0, "summary", &shortlog_len, "n",
63e67150 327 "alias for --log (deprecated)",
96e9420c
RR
328 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
329 DEFAULT_MERGE_LOG_LEN },
2102440c
JN
330 OPT_STRING('m', "message", &message, "text",
331 "use <text> as start of message"),
df217ed6 332 OPT_FILENAME('F', "file", &inpath, "file to read from"),
c8ef0383
PH
333 OPT_END()
334 };
335
0b9a969e 336 FILE *in = stdin;
f285a2d7 337 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
0b9a969e 338 int ret;
cbda121c 339 struct fmt_merge_msg_opts opts;
0b9a969e
MV
340
341 git_config(fmt_merge_msg_config, NULL);
37782920
SB
342 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
343 0);
c8ef0383
PH
344 if (argc > 0)
345 usage_with_options(fmt_merge_msg_usage, options);
4c0ea82d 346
96e9420c
RR
347 if (shortlog_len < 0)
348 die("Negative --log=%d", shortlog_len);
c8ef0383
PH
349
350 if (inpath && strcmp(inpath, "-")) {
351 in = fopen(inpath, "r");
352 if (!in)
0721c314 353 die_errno("cannot open '%s'", inpath);
0b9a969e
MV
354 }
355
0b9a969e 356 if (strbuf_read(&input, fileno(in), 0) < 0)
d824cbba 357 die_errno("could not read input file");
1876166a
RR
358
359 if (message)
2102440c 360 strbuf_addstr(&output, message);
1876166a 361
cbda121c
JH
362 memset(&opts, 0, sizeof(opts));
363 opts.add_title = !message;
364 opts.shortlog_len = shortlog_len;
365
366 ret = fmt_merge_msg(&input, &output, &opts);
0b9a969e
MV
367 if (ret)
368 return ret;
c8ef0383 369 write_in_full(STDOUT_FILENO, output.buf, output.len);
00449f99
JS
370 return 0;
371}