Provide git_config with a callback-data parameter
[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"
7
8static const char *fmt_merge_msg_usage =
6cd9cfef 9 "git-fmt-merge-msg [--log] [--no-log] [--file <file>]";
00449f99 10
96f1e58f 11static int merge_summary;
00449f99 12
ef90d6d4 13static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
00449f99 14{
6cd9cfef
SG
15 static int found_merge_log = 0;
16 if (!strcmp("merge.log", key)) {
17 found_merge_log = 1;
18 merge_summary = git_config_bool(key, value);
19 }
20 if (!found_merge_log && !strcmp("merge.summary", key))
00449f99
JS
21 merge_summary = git_config_bool(key, value);
22 return 0;
23}
24
25struct list {
26 char **list;
27 void **payload;
28 unsigned nr, alloc;
29};
30
31static void append_to_list(struct list *list, char *value, void *payload)
32{
33 if (list->nr == list->alloc) {
34 list->alloc += 32;
83572c1a
JF
35 list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
36 list->payload = xrealloc(list->payload,
00449f99
JS
37 sizeof(char *) * list->alloc);
38 }
39 list->payload[list->nr] = payload;
40 list->list[list->nr++] = value;
41}
42
43static int find_in_list(struct list *list, char *value)
44{
45 int i;
46
47 for (i = 0; i < list->nr; i++)
48 if (!strcmp(list->list[i], value))
49 return i;
50
51 return -1;
52}
53
54static void free_list(struct list *list)
55{
56 int i;
57
58 if (list->alloc == 0)
59 return;
60
61 for (i = 0; i < list->nr; i++) {
62 free(list->list[i]);
4cac42b1 63 free(list->payload[i]);
00449f99
JS
64 }
65 free(list->list);
66 free(list->payload);
67 list->nr = list->alloc = 0;
68}
69
70struct src_data {
71 struct list branch, tag, r_branch, generic;
72 int head_status;
73};
74
75static struct list srcs = { NULL, NULL, 0, 0};
76static struct list origins = { NULL, NULL, 0, 0};
77
78static int handle_line(char *line)
79{
80 int i, len = strlen(line);
81 unsigned char *sha1;
82 char *src, *origin;
83 struct src_data *src_data;
e918c6ab 84 int pulling_head = 0;
00449f99
JS
85
86 if (len < 43 || line[40] != '\t')
87 return 1;
88
cc44c765 89 if (!prefixcmp(line + 41, "not-for-merge"))
00449f99
JS
90 return 0;
91
92 if (line[41] != '\t')
93 return 2;
94
95 line[40] = 0;
96 sha1 = xmalloc(20);
97 i = get_sha1(line, sha1);
98 line[40] = '\t';
99 if (i)
100 return 3;
101
102 if (line[len - 1] == '\n')
103 line[len - 1] = 0;
104 line += 42;
105
106 src = strstr(line, " of ");
107 if (src) {
108 *src = 0;
109 src += 4;
e918c6ab
JH
110 pulling_head = 0;
111 } else {
112 src = line;
113 pulling_head = 1;
114 }
00449f99
JS
115
116 i = find_in_list(&srcs, src);
117 if (i < 0) {
118 i = srcs.nr;
9befac47 119 append_to_list(&srcs, xstrdup(src),
00449f99
JS
120 xcalloc(1, sizeof(struct src_data)));
121 }
122 src_data = srcs.payload[i];
123
e918c6ab 124 if (pulling_head) {
9befac47 125 origin = xstrdup(src);
e918c6ab 126 src_data->head_status |= 1;
cc44c765 127 } else if (!prefixcmp(line, "branch ")) {
9befac47 128 origin = xstrdup(line + 7);
00449f99
JS
129 append_to_list(&src_data->branch, origin, NULL);
130 src_data->head_status |= 2;
cc44c765 131 } else if (!prefixcmp(line, "tag ")) {
00449f99 132 origin = line;
9befac47 133 append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
00449f99 134 src_data->head_status |= 2;
cc44c765 135 } else if (!prefixcmp(line, "remote branch ")) {
9befac47 136 origin = xstrdup(line + 14);
00449f99
JS
137 append_to_list(&src_data->r_branch, origin, NULL);
138 src_data->head_status |= 2;
00449f99 139 } else {
9befac47
SP
140 origin = xstrdup(src);
141 append_to_list(&src_data->generic, xstrdup(line), NULL);
00449f99
JS
142 src_data->head_status |= 2;
143 }
144
145 if (!strcmp(".", src) || !strcmp(src, origin)) {
146 int len = strlen(origin);
147 if (origin[0] == '\'' && origin[len - 1] == '\'') {
182af834
PH
148 origin = xmemdupz(origin + 1, len - 2);
149 } else {
9befac47 150 origin = xstrdup(origin);
182af834 151 }
00449f99 152 } else {
2d7320d0 153 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
00449f99
JS
154 sprintf(new_origin, "%s of %s", origin, src);
155 origin = new_origin;
156 }
157 append_to_list(&origins, origin, sha1);
158 return 0;
159}
160
161static void print_joined(const char *singular, const char *plural,
162 struct list *list)
163{
164 if (list->nr == 0)
165 return;
166 if (list->nr == 1) {
167 printf("%s%s", singular, list->list[0]);
168 } else {
169 int i;
170 printf("%s", plural);
171 for (i = 0; i < list->nr - 1; i++)
172 printf("%s%s", i > 0 ? ", " : "", list->list[i]);
173 printf(" and %s", list->list[list->nr - 1]);
174 }
175}
176
177static void shortlog(const char *name, unsigned char *sha1,
178 struct commit *head, struct rev_info *rev, int limit)
179{
180 int i, count = 0;
181 struct commit *commit;
182 struct object *branch;
183 struct list subjects = { NULL, NULL, 0, 0 };
7dc0fe3b 184 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
00449f99
JS
185
186 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
1974632c 187 if (!branch || branch->type != OBJ_COMMIT)
00449f99
JS
188 return;
189
190 setup_revisions(0, NULL, rev, NULL);
191 rev->ignore_merges = 1;
192 add_pending_object(rev, branch, name);
193 add_pending_object(rev, &head->object, "^HEAD");
194 head->object.flags |= UNINTERESTING;
3d51e1b5
MK
195 if (prepare_revision_walk(rev))
196 die("revision walk setup failed");
00449f99
JS
197 while ((commit = get_revision(rev)) != NULL) {
198 char *oneline, *bol, *eol;
199
200 /* ignore merges */
201 if (commit->parents && commit->parents->next)
202 continue;
203
204 count++;
205 if (subjects.nr > limit)
206 continue;
207
208 bol = strstr(commit->buffer, "\n\n");
6a28518a
LT
209 if (bol) {
210 unsigned char c;
211 do {
212 c = *++bol;
213 } while (isspace(c));
214 if (!c)
215 bol = NULL;
216 }
217
00449f99 218 if (!bol) {
9befac47 219 append_to_list(&subjects, xstrdup(sha1_to_hex(
00449f99
JS
220 commit->object.sha1)),
221 NULL);
222 continue;
223 }
224
00449f99 225 eol = strchr(bol, '\n');
00449f99 226 if (eol) {
182af834
PH
227 oneline = xmemdupz(bol, eol - bol);
228 } else {
9befac47 229 oneline = xstrdup(bol);
182af834 230 }
00449f99
JS
231 append_to_list(&subjects, oneline, NULL);
232 }
233
234 if (count > limit)
235 printf("\n* %s: (%d commits)\n", name, count);
236 else
237 printf("\n* %s:\n", name);
238
239 for (i = 0; i < subjects.nr; i++)
240 if (i >= limit)
241 printf(" ...\n");
242 else
243 printf(" %s\n", subjects.list[i]);
244
245 clear_commit_marks((struct commit *)branch, flags);
246 clear_commit_marks(head, flags);
247 free_commit_list(rev->commits);
248 rev->commits = NULL;
249 rev->pending.nr = 0;
250
251 free_list(&subjects);
252}
253
25f38f06 254int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
00449f99
JS
255{
256 int limit = 20, i = 0;
257 char line[1024];
258 FILE *in = stdin;
259 const char *sep = "";
260 unsigned char head_sha1[20];
ed378ec7 261 const char *current_branch;
00449f99 262
ef90d6d4 263 git_config(fmt_merge_msg_config, NULL);
00449f99
JS
264
265 while (argc > 1) {
6cd9cfef 266 if (!strcmp(argv[1], "--log") || !strcmp(argv[1], "--summary"))
00449f99 267 merge_summary = 1;
6cd9cfef
SG
268 else if (!strcmp(argv[1], "--no-log")
269 || !strcmp(argv[1], "--no-summary"))
00449f99
JS
270 merge_summary = 0;
271 else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
163d7b9b 272 if (argc < 3)
00449f99
JS
273 die ("Which file?");
274 if (!strcmp(argv[2], "-"))
275 in = stdin;
276 else {
277 fclose(in);
278 in = fopen(argv[2], "r");
163d7b9b
MC
279 if (!in)
280 die("cannot open %s", argv[2]);
00449f99
JS
281 }
282 argc--; argv++;
283 } else
284 break;
285 argc--; argv++;
286 }
287
288 if (argc > 1)
289 usage(fmt_merge_msg_usage);
290
291 /* get current branch */
8da19775 292 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
6b1f8c32
LT
293 if (!current_branch)
294 die("No current branch");
cc44c765 295 if (!prefixcmp(current_branch, "refs/heads/"))
00449f99
JS
296 current_branch += 11;
297
298 while (fgets(line, sizeof(line), in)) {
299 i++;
300 if (line[0] == 0)
301 continue;
302 if (handle_line(line))
303 die ("Error in line %d: %s", i, line);
304 }
305
306 printf("Merge ");
307 for (i = 0; i < srcs.nr; i++) {
308 struct src_data *src_data = srcs.payload[i];
309 const char *subsep = "";
310
311 printf(sep);
312 sep = "; ";
313
314 if (src_data->head_status == 1) {
315 printf(srcs.list[i]);
316 continue;
317 }
318 if (src_data->head_status == 3) {
319 subsep = ", ";
320 printf("HEAD");
321 }
322 if (src_data->branch.nr) {
323 printf(subsep);
324 subsep = ", ";
325 print_joined("branch ", "branches ", &src_data->branch);
326 }
327 if (src_data->r_branch.nr) {
328 printf(subsep);
329 subsep = ", ";
330 print_joined("remote branch ", "remote branches ",
331 &src_data->r_branch);
332 }
333 if (src_data->tag.nr) {
334 printf(subsep);
335 subsep = ", ";
336 print_joined("tag ", "tags ", &src_data->tag);
337 }
338 if (src_data->generic.nr) {
339 printf(subsep);
340 print_joined("commit ", "commits ", &src_data->generic);
341 }
342 if (strcmp(".", srcs.list[i]))
343 printf(" of %s", srcs.list[i]);
344 }
345
346 if (!strcmp("master", current_branch))
347 putchar('\n');
348 else
349 printf(" into %s\n", current_branch);
350
351 if (merge_summary) {
352 struct commit *head;
353 struct rev_info rev;
354
355 head = lookup_commit(head_sha1);
db6296a5 356 init_revisions(&rev, prefix);
00449f99
JS
357 rev.commit_format = CMIT_FMT_ONELINE;
358 rev.ignore_merges = 1;
359 rev.limited = 1;
360
361 for (i = 0; i < origins.nr; i++)
362 shortlog(origins.list[i], origins.payload[i],
363 head, &rev, limit);
364 }
365
366 /* No cleanup yet; is standalone anyway */
367
368 return 0;
369}