fmt-merge-msg.c: Fix an "dubious one-bit signed bitfield" sparse error
[git/gitweb.git] / builtin / fmt-merge-msg.c
blobf6d92e263ce6c8b37c1d554e15d6693a049be071
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"
11 static const char * const fmt_merge_msg_usage[] = {
12 "git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]",
13 NULL
16 static int use_branch_desc;
18 int fmt_merge_msg_config(const char *key, const char *value, void *cb)
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);
30 return 0;
33 struct src_data {
34 struct string_list branch, tag, r_branch, generic;
35 int head_status;
38 struct origin_data {
39 unsigned char sha1[20];
40 unsigned is_local_branch:1;
43 static void init_src_data(struct src_data *data)
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;
51 static struct string_list srcs = STRING_LIST_INIT_DUP;
52 static struct string_list origins = STRING_LIST_INIT_DUP;
54 static int handle_line(char *line)
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;
63 if (len < 43 || line[40] != '\t')
64 return 1;
66 if (!prefixcmp(line + 41, "not-for-merge"))
67 return 0;
69 if (line[41] != '\t')
70 return 2;
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;
81 if (line[len - 1] == '\n')
82 line[len - 1] = 0;
83 line += 42;
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;
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);
101 src_data = item->util;
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;
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;
134 if (strcmp(".", src))
135 origin_data->is_local_branch = 0;
136 string_list_append(&origins, origin)->util = origin_data;
137 return 0;
140 static void print_joined(const char *singular, const char *plural,
141 struct string_list *list, struct strbuf *out)
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);
157 static void add_branch_desc(struct strbuf *out, const char *name)
159 struct strbuf desc = STRBUF_INIT;
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;
170 if (out->buf[out->len - 1] != '\n')
171 strbuf_addch(out, '\n');
173 strbuf_release(&desc);
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)
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;
190 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
191 if (!branch || branch->type != OBJ_COMMIT)
192 return;
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};
204 /* ignore merges */
205 if (commit->parents && commit->parents->next)
206 continue;
208 count++;
209 if (subjects.nr > limit)
210 continue;
212 format_commit_message(commit, "%s", &sb, &ctx);
213 strbuf_ltrim(&sb);
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));
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);
227 if (origin_data->is_local_branch && use_branch_desc)
228 add_branch_desc(out, name);
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);
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;
242 string_list_clear(&subjects, 0);
245 static void do_fmt_merge_msg_title(struct strbuf *out,
246 const char *current_branch) {
247 int i = 0;
248 char *sep = "";
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 = "";
255 strbuf_addstr(out, sep);
256 sep = "; ";
258 if (src_data->head_status == 1) {
259 strbuf_addstr(out, srcs.items[i].string);
260 continue;
262 if (src_data->head_status == 3) {
263 subsep = ", ";
264 strbuf_addstr(out, "HEAD");
266 if (src_data->branch.nr) {
267 strbuf_addstr(out, subsep);
268 subsep = ", ";
269 print_joined("branch ", "branches ", &src_data->branch,
270 out);
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);
278 if (src_data->tag.nr) {
279 strbuf_addstr(out, subsep);
280 subsep = ", ";
281 print_joined("tag ", "tags ", &src_data->tag, out);
283 if (src_data->generic.nr) {
284 strbuf_addstr(out, subsep);
285 print_joined("commit ", "commits ", &src_data->generic,
286 out);
288 if (strcmp(".", srcs.items[i].string))
289 strbuf_addf(out, " of %s", srcs.items[i].string);
292 if (!strcmp("master", current_branch))
293 strbuf_addch(out, '\n');
294 else
295 strbuf_addf(out, " into %s\n", current_branch);
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;
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;
311 /* get a line */
312 while (pos < in->len) {
313 int len;
314 char *newline, *p = in->buf + pos;
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);
325 if (!srcs.nr)
326 return 0;
328 if (merge_title)
329 do_fmt_merge_msg_title(out, current_branch);
331 if (shortlog_len) {
332 struct commit *head;
333 struct rev_info rev;
335 head = lookup_commit(head_sha1);
336 init_revisions(&rev, NULL);
337 rev.commit_format = CMIT_FMT_ONELINE;
338 rev.ignore_merges = 1;
339 rev.limited = 1;
341 if (suffixcmp(out->buf, "\n"))
342 strbuf_addch(out, '\n');
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);
349 return 0;
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);
357 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
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()
376 FILE *in = stdin;
377 struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
378 int ret;
380 git_config(fmt_merge_msg_config, NULL);
381 argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
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;
393 if (shortlog_len < 0)
394 die("Negative --log=%d", shortlog_len);
396 if (inpath && strcmp(inpath, "-")) {
397 in = fopen(inpath, "r");
398 if (!in)
399 die_errno("cannot open '%s'", inpath);
402 if (strbuf_read(&input, fileno(in), 0) < 0)
403 die_errno("could not read input file");
405 if (message)
406 strbuf_addstr(&output, message);
407 ret = fmt_merge_msg(&input, &output,
408 message ? 0 : 1,
409 shortlog_len);
411 if (ret)
412 return ret;
413 write_in_full(STDOUT_FILENO, output.buf, output.len);
414 return 0;