]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fmt-merge-msg.c
fmt-merge-msg: avoid early returns
[thirdparty/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
403994e8
TRC
212static void do_fmt_merge_msg_title(struct strbuf *out,
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
1876166a
RR
265static int do_fmt_merge_msg(int merge_title, struct strbuf *in,
266 struct strbuf *out, int shortlog_len) {
267 int i = 0, pos = 0;
403994e8
TRC
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
4c0ea82d 292 if (merge_title && srcs.nr)
8c6bdfdf 293 do_fmt_merge_msg_title(out, current_branch);
00449f99 294
1876166a 295 if (shortlog_len) {
00449f99
JS
296 struct commit *head;
297 struct rev_info rev;
298
baf18fc2 299 head = lookup_commit_or_die(head_sha1, "HEAD");
0b9a969e 300 init_revisions(&rev, NULL);
00449f99
JS
301 rev.commit_format = CMIT_FMT_ONELINE;
302 rev.ignore_merges = 1;
303 rev.limited = 1;
304
f0ecac2b
TRC
305 if (suffixcmp(out->buf, "\n"))
306 strbuf_addch(out, '\n');
307
00449f99 308 for (i = 0; i < origins.nr; i++)
fcb243f7 309 shortlog(origins.items[i].string, origins.items[i].util,
1876166a 310 head, &rev, shortlog_len, out);
00449f99 311 }
4c0ea82d
JH
312 if (out->len && out->buf[out->len-1] != '\n')
313 strbuf_addch(out, '\n');
0b9a969e
MV
314 return 0;
315}
316
1876166a
RR
317int 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);
2234ec54
TRC
320}
321
0b9a969e
MV
322int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
323{
c8ef0383 324 const char *inpath = NULL;
2102440c 325 const char *message = NULL;
c8ef0383 326 struct option options[] = {
96e9420c
RR
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",
63e67150 331 "alias for --log (deprecated)",
96e9420c
RR
332 PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
333 DEFAULT_MERGE_LOG_LEN },
2102440c
JN
334 OPT_STRING('m', "message", &message, "text",
335 "use <text> as start of message"),
df217ed6 336 OPT_FILENAME('F', "file", &inpath, "file to read from"),
c8ef0383
PH
337 OPT_END()
338 };
339
0b9a969e 340 FILE *in = stdin;
f285a2d7 341 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
0b9a969e
MV
342 int ret;
343
344 git_config(fmt_merge_msg_config, NULL);
37782920
SB
345 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
346 0);
c8ef0383
PH
347 if (argc > 0)
348 usage_with_options(fmt_merge_msg_usage, options);
4c0ea82d 349
96e9420c
RR
350 if (shortlog_len < 0)
351 die("Negative --log=%d", shortlog_len);
c8ef0383
PH
352
353 if (inpath && strcmp(inpath, "-")) {
354 in = fopen(inpath, "r");
355 if (!in)
0721c314 356 die_errno("cannot open '%s'", inpath);
0b9a969e
MV
357 }
358
0b9a969e 359 if (strbuf_read(&input, fileno(in), 0) < 0)
d824cbba 360 die_errno("could not read input file");
1876166a
RR
361
362 if (message)
2102440c 363 strbuf_addstr(&output, message);
1876166a
RR
364 ret = fmt_merge_msg(&input, &output,
365 message ? 0 : 1,
96e9420c 366 shortlog_len);
1876166a 367
0b9a969e
MV
368 if (ret)
369 return ret;
c8ef0383 370 write_in_full(STDOUT_FILENO, output.buf, output.len);
00449f99
JS
371 return 0;
372}