Increase the pipe buffer size.
[git/mingw.git] / builtin-fmt-merge-msg.c
blobbcfbb1bc1ef1342bd1678dded10375ed838b6205
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 [--summary] [--no-summary] [--file <file>]";
11 static int merge_summary;
13 static int fmt_merge_msg_config(const char *key, const char *value)
15 if (!strcmp("merge.summary", key))
16 merge_summary = 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, --len;
99 if (line[len - 1] == '\r')
100 line[len - 1] = 0;
101 line += 42;
103 src = strstr(line, " of ");
104 if (src) {
105 *src = 0;
106 src += 4;
107 pulling_head = 0;
108 } else {
109 src = line;
110 pulling_head = 1;
113 i = find_in_list(&srcs, src);
114 if (i < 0) {
115 i = srcs.nr;
116 append_to_list(&srcs, xstrdup(src),
117 xcalloc(1, sizeof(struct src_data)));
119 src_data = srcs.payload[i];
121 if (pulling_head) {
122 origin = xstrdup(src);
123 src_data->head_status |= 1;
124 } else if (!prefixcmp(line, "branch ")) {
125 origin = xstrdup(line + 7);
126 append_to_list(&src_data->branch, origin, NULL);
127 src_data->head_status |= 2;
128 } else if (!prefixcmp(line, "tag ")) {
129 origin = line;
130 append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
131 src_data->head_status |= 2;
132 } else if (!prefixcmp(line, "remote branch ")) {
133 origin = xstrdup(line + 14);
134 append_to_list(&src_data->r_branch, origin, NULL);
135 src_data->head_status |= 2;
136 } else {
137 origin = xstrdup(src);
138 append_to_list(&src_data->generic, xstrdup(line), NULL);
139 src_data->head_status |= 2;
142 if (!strcmp(".", src) || !strcmp(src, origin)) {
143 int len = strlen(origin);
144 if (origin[0] == '\'' && origin[len - 1] == '\'') {
145 char *new_origin = xmalloc(len - 1);
146 memcpy(new_origin, origin + 1, len - 2);
147 new_origin[len - 2] = 0;
148 origin = new_origin;
149 } else
150 origin = xstrdup(origin);
151 } else {
152 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
153 sprintf(new_origin, "%s of %s", origin, src);
154 origin = new_origin;
156 append_to_list(&origins, origin, sha1);
157 return 0;
160 static void print_joined(const char *singular, const char *plural,
161 struct list *list)
163 if (list->nr == 0)
164 return;
165 if (list->nr == 1) {
166 printf("%s%s", singular, list->list[0]);
167 } else {
168 int i;
169 printf("%s", plural);
170 for (i = 0; i < list->nr - 1; i++)
171 printf("%s%s", i > 0 ? ", " : "", list->list[i]);
172 printf(" and %s", list->list[list->nr - 1]);
176 static void shortlog(const char *name, unsigned char *sha1,
177 struct commit *head, struct rev_info *rev, int limit)
179 int i, count = 0;
180 struct commit *commit;
181 struct object *branch;
182 struct list subjects = { NULL, NULL, 0, 0 };
183 int flags = UNINTERESTING | TREECHANGE | SEEN | SHOWN | ADDED;
185 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
186 if (!branch || branch->type != OBJ_COMMIT)
187 return;
189 setup_revisions(0, NULL, rev, NULL);
190 rev->ignore_merges = 1;
191 add_pending_object(rev, branch, name);
192 add_pending_object(rev, &head->object, "^HEAD");
193 head->object.flags |= UNINTERESTING;
194 prepare_revision_walk(rev);
195 while ((commit = get_revision(rev)) != NULL) {
196 char *oneline, *bol, *eol;
198 /* ignore merges */
199 if (commit->parents && commit->parents->next)
200 continue;
202 count++;
203 if (subjects.nr > limit)
204 continue;
206 bol = strstr(commit->buffer, "\n\n");
207 if (!bol) {
208 append_to_list(&subjects, xstrdup(sha1_to_hex(
209 commit->object.sha1)),
210 NULL);
211 continue;
214 bol += 2;
215 eol = strchr(bol, '\n');
217 if (eol) {
218 int len = eol - bol;
219 oneline = xmalloc(len + 1);
220 memcpy(oneline, bol, len);
221 oneline[len] = 0;
222 } else
223 oneline = xstrdup(bol);
224 append_to_list(&subjects, oneline, NULL);
227 if (count > limit)
228 printf("\n* %s: (%d commits)\n", name, count);
229 else
230 printf("\n* %s:\n", name);
232 for (i = 0; i < subjects.nr; i++)
233 if (i >= limit)
234 printf(" ...\n");
235 else
236 printf(" %s\n", subjects.list[i]);
238 clear_commit_marks((struct commit *)branch, flags);
239 clear_commit_marks(head, flags);
240 free_commit_list(rev->commits);
241 rev->commits = NULL;
242 rev->pending.nr = 0;
244 free_list(&subjects);
247 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
249 int limit = 20, i = 0;
250 char line[1024];
251 FILE *in = stdin;
252 const char *sep = "";
253 unsigned char head_sha1[20];
254 const char *current_branch;
256 git_config(fmt_merge_msg_config);
258 while (argc > 1) {
259 if (!strcmp(argv[1], "--summary"))
260 merge_summary = 1;
261 else if (!strcmp(argv[1], "--no-summary"))
262 merge_summary = 0;
263 else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
264 if (argc < 2)
265 die ("Which file?");
266 if (!strcmp(argv[2], "-"))
267 in = stdin;
268 else {
269 fclose(in);
270 in = fopen(argv[2], "r");
272 argc--; argv++;
273 } else
274 break;
275 argc--; argv++;
278 if (argc > 1)
279 usage(fmt_merge_msg_usage);
281 /* get current branch */
282 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
283 if (!current_branch)
284 die("No current branch");
285 if (!prefixcmp(current_branch, "refs/heads/"))
286 current_branch += 11;
288 while (fgets(line, sizeof(line), in)) {
289 i++;
290 if (line[0] == 0)
291 continue;
292 if (handle_line(line))
293 die ("Error in line %d: %s", i, line);
296 printf("Merge ");
297 for (i = 0; i < srcs.nr; i++) {
298 struct src_data *src_data = srcs.payload[i];
299 const char *subsep = "";
301 printf(sep);
302 sep = "; ";
304 if (src_data->head_status == 1) {
305 printf(srcs.list[i]);
306 continue;
308 if (src_data->head_status == 3) {
309 subsep = ", ";
310 printf("HEAD");
312 if (src_data->branch.nr) {
313 printf(subsep);
314 subsep = ", ";
315 print_joined("branch ", "branches ", &src_data->branch);
317 if (src_data->r_branch.nr) {
318 printf(subsep);
319 subsep = ", ";
320 print_joined("remote branch ", "remote branches ",
321 &src_data->r_branch);
323 if (src_data->tag.nr) {
324 printf(subsep);
325 subsep = ", ";
326 print_joined("tag ", "tags ", &src_data->tag);
328 if (src_data->generic.nr) {
329 printf(subsep);
330 print_joined("commit ", "commits ", &src_data->generic);
332 if (strcmp(".", srcs.list[i]))
333 printf(" of %s", srcs.list[i]);
336 if (!strcmp("master", current_branch))
337 putchar('\n');
338 else
339 printf(" into %s\n", current_branch);
341 if (merge_summary) {
342 struct commit *head;
343 struct rev_info rev;
345 head = lookup_commit(head_sha1);
346 init_revisions(&rev, prefix);
347 rev.commit_format = CMIT_FMT_ONELINE;
348 rev.ignore_merges = 1;
349 rev.limited = 1;
351 for (i = 0; i < origins.nr; i++)
352 shortlog(origins.list[i], origins.payload[i],
353 head, &rev, limit);
356 /* No cleanup yet; is standalone anyway */
358 return 0;