skip t5512 because remote does not yet work
[git/platforms/storm.git] / builtin-fmt-merge-msg.c
blobc7de27c849d4105cb8c4cd486c012739244abf07
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 origin = xmemdupz(origin + 1, len - 2);
146 } else {
147 origin = xstrdup(origin);
149 } else {
150 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
151 sprintf(new_origin, "%s of %s", origin, src);
152 origin = new_origin;
154 append_to_list(&origins, origin, sha1);
155 return 0;
158 static void print_joined(const char *singular, const char *plural,
159 struct list *list)
161 if (list->nr == 0)
162 return;
163 if (list->nr == 1) {
164 printf("%s%s", singular, list->list[0]);
165 } else {
166 int i;
167 printf("%s", plural);
168 for (i = 0; i < list->nr - 1; i++)
169 printf("%s%s", i > 0 ? ", " : "", list->list[i]);
170 printf(" and %s", list->list[list->nr - 1]);
174 static void shortlog(const char *name, unsigned char *sha1,
175 struct commit *head, struct rev_info *rev, int limit)
177 int i, count = 0;
178 struct commit *commit;
179 struct object *branch;
180 struct list subjects = { NULL, NULL, 0, 0 };
181 int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
183 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
184 if (!branch || branch->type != OBJ_COMMIT)
185 return;
187 setup_revisions(0, NULL, rev, NULL);
188 rev->ignore_merges = 1;
189 add_pending_object(rev, branch, name);
190 add_pending_object(rev, &head->object, "^HEAD");
191 head->object.flags |= UNINTERESTING;
192 prepare_revision_walk(rev);
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 append_to_list(&subjects, xstrdup(sha1_to_hex(
207 commit->object.sha1)),
208 NULL);
209 continue;
212 bol += 2;
213 eol = strchr(bol, '\n');
214 if (eol) {
215 oneline = xmemdupz(bol, eol - bol);
216 } else {
217 oneline = xstrdup(bol);
219 append_to_list(&subjects, oneline, NULL);
222 if (count > limit)
223 printf("\n* %s: (%d commits)\n", name, count);
224 else
225 printf("\n* %s:\n", name);
227 for (i = 0; i < subjects.nr; i++)
228 if (i >= limit)
229 printf(" ...\n");
230 else
231 printf(" %s\n", subjects.list[i]);
233 clear_commit_marks((struct commit *)branch, flags);
234 clear_commit_marks(head, flags);
235 free_commit_list(rev->commits);
236 rev->commits = NULL;
237 rev->pending.nr = 0;
239 free_list(&subjects);
242 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
244 int limit = 20, i = 0;
245 char line[1024];
246 FILE *in = stdin;
247 const char *sep = "";
248 unsigned char head_sha1[20];
249 const char *current_branch;
251 git_config(fmt_merge_msg_config);
253 while (argc > 1) {
254 if (!strcmp(argv[1], "--summary"))
255 merge_summary = 1;
256 else if (!strcmp(argv[1], "--no-summary"))
257 merge_summary = 0;
258 else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
259 if (argc < 3)
260 die ("Which file?");
261 if (!strcmp(argv[2], "-"))
262 in = stdin;
263 else {
264 fclose(in);
265 in = fopen(argv[2], "r");
266 if (!in)
267 die("cannot open %s", argv[2]);
269 argc--; argv++;
270 } else
271 break;
272 argc--; argv++;
275 if (argc > 1)
276 usage(fmt_merge_msg_usage);
278 /* get current branch */
279 current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
280 if (!current_branch)
281 die("No current branch");
282 if (!prefixcmp(current_branch, "refs/heads/"))
283 current_branch += 11;
285 while (fgets(line, sizeof(line), in)) {
286 i++;
287 if (line[0] == 0)
288 continue;
289 if (handle_line(line))
290 die ("Error in line %d: %s", i, line);
293 printf("Merge ");
294 for (i = 0; i < srcs.nr; i++) {
295 struct src_data *src_data = srcs.payload[i];
296 const char *subsep = "";
298 printf(sep);
299 sep = "; ";
301 if (src_data->head_status == 1) {
302 printf(srcs.list[i]);
303 continue;
305 if (src_data->head_status == 3) {
306 subsep = ", ";
307 printf("HEAD");
309 if (src_data->branch.nr) {
310 printf(subsep);
311 subsep = ", ";
312 print_joined("branch ", "branches ", &src_data->branch);
314 if (src_data->r_branch.nr) {
315 printf(subsep);
316 subsep = ", ";
317 print_joined("remote branch ", "remote branches ",
318 &src_data->r_branch);
320 if (src_data->tag.nr) {
321 printf(subsep);
322 subsep = ", ";
323 print_joined("tag ", "tags ", &src_data->tag);
325 if (src_data->generic.nr) {
326 printf(subsep);
327 print_joined("commit ", "commits ", &src_data->generic);
329 if (strcmp(".", srcs.list[i]))
330 printf(" of %s", srcs.list[i]);
333 if (!strcmp("master", current_branch))
334 putchar('\n');
335 else
336 printf(" into %s\n", current_branch);
338 if (merge_summary) {
339 struct commit *head;
340 struct rev_info rev;
342 head = lookup_commit(head_sha1);
343 init_revisions(&rev, prefix);
344 rev.commit_format = CMIT_FMT_ONELINE;
345 rev.ignore_merges = 1;
346 rev.limited = 1;
348 for (i = 0; i < origins.nr; i++)
349 shortlog(origins.list[i], origins.payload[i],
350 head, &rev, limit);
353 /* No cleanup yet; is standalone anyway */
355 return 0;