gitweb: use new Git::Repo API, and add optional caching
[git/gitweb-caching.git] / builtin-fmt-merge-msg.c
bloba1879f1cffc654c3e672783eb78c033111bfda10
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "revision.h"
6 #include "tag.h"
8 static const char *fmt_merge_msg_usage =
9 "git fmt-merge-msg [--log] [--no-log] [--file <file>]";
11 static int merge_log;
13 static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
15 if (!strcmp("merge.log", key))
16 merge_log = git_config_bool(key, value);
17 return 0;
20 struct list {
21 char **list;
22 void **payload;
23 unsigned nr, alloc;
26 static void append_to_list(struct list *list, char *value, void *payload)
28 if (list->nr == list->alloc) {
29 list->alloc += 32;
30 list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
31 list->payload = xrealloc(list->payload,
32 sizeof(char *) * list->alloc);
34 list->payload[list->nr] = payload;
35 list->list[list->nr++] = value;
38 static int find_in_list(struct list *list, char *value)
40 int i;
42 for (i = 0; i < list->nr; i++)
43 if (!strcmp(list->list[i], value))
44 return i;
46 return -1;
49 static void free_list(struct list *list)
51 int i;
53 if (list->alloc == 0)
54 return;
56 for (i = 0; i < list->nr; i++) {
57 free(list->list[i]);
58 free(list->payload[i]);
60 free(list->list);
61 free(list->payload);
62 list->nr = list->alloc = 0;
65 struct src_data {
66 struct list branch, tag, r_branch, generic;
67 int head_status;
70 static struct list srcs = { NULL, NULL, 0, 0};
71 static struct list origins = { NULL, NULL, 0, 0};
73 static int handle_line(char *line)
75 int i, len = strlen(line);
76 unsigned char *sha1;
77 char *src, *origin;
78 struct src_data *src_data;
79 int pulling_head = 0;
81 if (len < 43 || line[40] != '\t')
82 return 1;
84 if (!prefixcmp(line + 41, "not-for-merge"))
85 return 0;
87 if (line[41] != '\t')
88 return 2;
90 line[40] = 0;
91 sha1 = xmalloc(20);
92 i = get_sha1(line, sha1);
93 line[40] = '\t';
94 if (i)
95 return 3;
97 if (line[len - 1] == '\n')
98 line[len - 1] = 0;
99 line += 42;
101 src = strstr(line, " of ");
102 if (src) {
103 *src = 0;
104 src += 4;
105 pulling_head = 0;
106 } else {
107 src = line;
108 pulling_head = 1;
111 i = find_in_list(&srcs, src);
112 if (i < 0) {
113 i = srcs.nr;
114 append_to_list(&srcs, xstrdup(src),
115 xcalloc(1, sizeof(struct src_data)));
117 src_data = srcs.payload[i];
119 if (pulling_head) {
120 origin = xstrdup(src);
121 src_data->head_status |= 1;
122 } else if (!prefixcmp(line, "branch ")) {
123 origin = xstrdup(line + 7);
124 append_to_list(&src_data->branch, origin, NULL);
125 src_data->head_status |= 2;
126 } else if (!prefixcmp(line, "tag ")) {
127 origin = line;
128 append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
129 src_data->head_status |= 2;
130 } else if (!prefixcmp(line, "remote branch ")) {
131 origin = xstrdup(line + 14);
132 append_to_list(&src_data->r_branch, origin, NULL);
133 src_data->head_status |= 2;
134 } else {
135 origin = xstrdup(src);
136 append_to_list(&src_data->generic, xstrdup(line), NULL);
137 src_data->head_status |= 2;
140 if (!strcmp(".", src) || !strcmp(src, origin)) {
141 int len = strlen(origin);
142 if (origin[0] == '\'' && origin[len - 1] == '\'') {
143 origin = xmemdupz(origin + 1, len - 2);
144 } else {
145 origin = xstrdup(origin);
147 } else {
148 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
149 sprintf(new_origin, "%s of %s", origin, src);
150 origin = new_origin;
152 append_to_list(&origins, origin, sha1);
153 return 0;
156 static void print_joined(const char *singular, const char *plural,
157 struct list *list, struct strbuf *out)
159 if (list->nr == 0)
160 return;
161 if (list->nr == 1) {
162 strbuf_addf(out, "%s%s", singular, list->list[0]);
163 } else {
164 int i;
165 strbuf_addstr(out, plural);
166 for (i = 0; i < list->nr - 1; i++)
167 strbuf_addf(out, "%s%s", i > 0 ? ", " : "", list->list[i]);
168 strbuf_addf(out, " and %s", list->list[list->nr - 1]);
172 static void shortlog(const char *name, unsigned char *sha1,
173 struct commit *head, struct rev_info *rev, int limit,
174 struct strbuf *out)
176 int i, count = 0;
177 struct commit *commit;
178 struct object *branch;
179 struct list subjects = { NULL, NULL, 0, 0 };
180 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
182 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
183 if (!branch || branch->type != OBJ_COMMIT)
184 return;
186 setup_revisions(0, NULL, rev, NULL);
187 rev->ignore_merges = 1;
188 add_pending_object(rev, branch, name);
189 add_pending_object(rev, &head->object, "^HEAD");
190 head->object.flags |= UNINTERESTING;
191 if (prepare_revision_walk(rev))
192 die("revision walk setup failed");
193 while ((commit = get_revision(rev)) != NULL) {
194 char *oneline, *bol, *eol;
196 /* ignore merges */
197 if (commit->parents && commit->parents->next)
198 continue;
200 count++;
201 if (subjects.nr > limit)
202 continue;
204 bol = strstr(commit->buffer, "\n\n");
205 if (bol) {
206 unsigned char c;
207 do {
208 c = *++bol;
209 } while (isspace(c));
210 if (!c)
211 bol = NULL;
214 if (!bol) {
215 append_to_list(&subjects, xstrdup(sha1_to_hex(
216 commit->object.sha1)),
217 NULL);
218 continue;
221 eol = strchr(bol, '\n');
222 if (eol) {
223 oneline = xmemdupz(bol, eol - bol);
224 } else {
225 oneline = xstrdup(bol);
227 append_to_list(&subjects, oneline, NULL);
230 if (count > limit)
231 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
232 else
233 strbuf_addf(out, "\n* %s:\n", name);
235 for (i = 0; i < subjects.nr; i++)
236 if (i >= limit)
237 strbuf_addf(out, " ...\n");
238 else
239 strbuf_addf(out, " %s\n", subjects.list[i]);
241 clear_commit_marks((struct commit *)branch, flags);
242 clear_commit_marks(head, flags);
243 free_commit_list(rev->commits);
244 rev->commits = NULL;
245 rev->pending.nr = 0;
247 free_list(&subjects);
250 int fmt_merge_msg(int merge_log, struct strbuf *in, struct strbuf *out) {
251 int limit = 20, i = 0, pos = 0;
252 char line[1024];
253 char *p = line, *sep = "";
254 unsigned char head_sha1[20];
255 const char *current_branch;
257 /* get current branch */
258 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
259 if (!current_branch)
260 die("No current branch");
261 if (!prefixcmp(current_branch, "refs/heads/"))
262 current_branch += 11;
264 /* get a line */
265 while (pos < in->len) {
266 int len;
267 char *newline;
269 p = in->buf + pos;
270 newline = strchr(p, '\n');
271 len = newline ? newline - p : strlen(p);
272 pos += len + !!newline;
273 i++;
274 p[len] = 0;
275 if (handle_line(p))
276 die ("Error in line %d: %.*s", i, len, p);
279 strbuf_addstr(out, "Merge ");
280 for (i = 0; i < srcs.nr; i++) {
281 struct src_data *src_data = srcs.payload[i];
282 const char *subsep = "";
284 strbuf_addstr(out, sep);
285 sep = "; ";
287 if (src_data->head_status == 1) {
288 strbuf_addstr(out, srcs.list[i]);
289 continue;
291 if (src_data->head_status == 3) {
292 subsep = ", ";
293 strbuf_addstr(out, "HEAD");
295 if (src_data->branch.nr) {
296 strbuf_addstr(out, subsep);
297 subsep = ", ";
298 print_joined("branch ", "branches ", &src_data->branch,
299 out);
301 if (src_data->r_branch.nr) {
302 strbuf_addstr(out, subsep);
303 subsep = ", ";
304 print_joined("remote branch ", "remote branches ",
305 &src_data->r_branch, out);
307 if (src_data->tag.nr) {
308 strbuf_addstr(out, subsep);
309 subsep = ", ";
310 print_joined("tag ", "tags ", &src_data->tag, out);
312 if (src_data->generic.nr) {
313 strbuf_addstr(out, subsep);
314 print_joined("commit ", "commits ", &src_data->generic,
315 out);
317 if (strcmp(".", srcs.list[i]))
318 strbuf_addf(out, " of %s", srcs.list[i]);
321 if (!strcmp("master", current_branch))
322 strbuf_addch(out, '\n');
323 else
324 strbuf_addf(out, " into %s\n", current_branch);
326 if (merge_log) {
327 struct commit *head;
328 struct rev_info rev;
330 head = lookup_commit(head_sha1);
331 init_revisions(&rev, NULL);
332 rev.commit_format = CMIT_FMT_ONELINE;
333 rev.ignore_merges = 1;
334 rev.limited = 1;
336 for (i = 0; i < origins.nr; i++)
337 shortlog(origins.list[i], origins.payload[i],
338 head, &rev, limit, out);
340 return 0;
343 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
345 FILE *in = stdin;
346 struct strbuf input, output;
347 int ret;
349 git_config(fmt_merge_msg_config, NULL);
351 while (argc > 1) {
352 if (!strcmp(argv[1], "--log"))
353 merge_log = 1;
354 else if (!strcmp(argv[1], "--no-log"))
355 merge_log = 0;
356 else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
357 if (argc < 3)
358 die ("Which file?");
359 if (!strcmp(argv[2], "-"))
360 in = stdin;
361 else {
362 fclose(in);
363 in = fopen(argv[2], "r");
364 if (!in)
365 die("cannot open %s", argv[2]);
367 argc--; argv++;
368 } else
369 break;
370 argc--; argv++;
373 if (argc > 1)
374 usage(fmt_merge_msg_usage);
376 strbuf_init(&input, 0);
377 if (strbuf_read(&input, fileno(in), 0) < 0)
378 die("could not read input file %s", strerror(errno));
379 strbuf_init(&output, 0);
381 ret = fmt_merge_msg(merge_log, &input, &output);
382 if (ret)
383 return ret;
384 printf("%s", output.buf);
385 return 0;