fmt-merge-msg: package options into a structure
[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 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 int fmt_merge_msg(struct strbuf *in, struct strbuf *out,
266                   struct fmt_merge_msg_opts *opts)
267 {
268         int i = 0, pos = 0;
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
293         if (opts->add_title && srcs.nr)
294                 fmt_merge_msg_title(out, current_branch);
295
296         if (opts->shortlog_len) {
297                 struct commit *head;
298                 struct rev_info rev;
299
300                 head = lookup_commit_or_die(head_sha1, "HEAD");
301                 init_revisions(&rev, NULL);
302                 rev.commit_format = CMIT_FMT_ONELINE;
303                 rev.ignore_merges = 1;
304                 rev.limited = 1;
305
306                 if (suffixcmp(out->buf, "\n"))
307                         strbuf_addch(out, '\n');
308
309                 for (i = 0; i < origins.nr; i++)
310                         shortlog(origins.items[i].string, origins.items[i].util,
311                                  head, &rev, opts->shortlog_len, out);
312         }
313         if (out->len && out->buf[out->len-1] != '\n')
314                 strbuf_addch(out, '\n');
315         return 0;
316 }
317
318 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
319 {
320         const char *inpath = NULL;
321         const char *message = NULL;
322         struct option options[] = {
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",
327                   "alias for --log (deprecated)",
328                   PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL,
329                   DEFAULT_MERGE_LOG_LEN },
330                 OPT_STRING('m', "message", &message, "text",
331                         "use <text> as start of message"),
332                 OPT_FILENAME('F', "file", &inpath, "file to read from"),
333                 OPT_END()
334         };
335
336         FILE *in = stdin;
337         struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
338         int ret;
339         struct fmt_merge_msg_opts opts;
340
341         git_config(fmt_merge_msg_config, NULL);
342         argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
343                              0);
344         if (argc > 0)
345                 usage_with_options(fmt_merge_msg_usage, options);
346
347         if (shortlog_len < 0)
348                 die("Negative --log=%d", shortlog_len);
349
350         if (inpath && strcmp(inpath, "-")) {
351                 in = fopen(inpath, "r");
352                 if (!in)
353                         die_errno("cannot open '%s'", inpath);
354         }
355
356         if (strbuf_read(&input, fileno(in), 0) < 0)
357                 die_errno("could not read input file");
358
359         if (message)
360                 strbuf_addstr(&output, message);
361
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);
367         if (ret)
368                 return ret;
369         write_in_full(STDOUT_FILENO, output.buf, output.len);
370         return 0;
371 }