Code

Merge branch 'jc/sideband'
[git.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"
8 static const char *fmt_merge_msg_usage =
9         "git-fmt-merge-msg [--summary] [--no-summary] [--file <file>]";
11 static int merge_summary = 0;
13 static int fmt_merge_msg_config(const char *key, const char *value)
14 {
15         if (!strcmp("merge.summary", key))
16                 merge_summary = git_config_bool(key, value);
17         return 0;
18 }
20 struct list {
21         char **list;
22         void **payload;
23         unsigned nr, alloc;
24 };
26 static void append_to_list(struct list *list, char *value, void *payload)
27 {
28         if (list->nr == list->alloc) {
29                 list->alloc += 32;
30                 list->list = realloc(list->list, sizeof(char *) * list->alloc);
31                 list->payload = realloc(list->payload,
32                                 sizeof(char *) * list->alloc);
33         }
34         list->payload[list->nr] = payload;
35         list->list[list->nr++] = value;
36 }
38 static int find_in_list(struct list *list, char *value)
39 {
40         int i;
42         for (i = 0; i < list->nr; i++)
43                 if (!strcmp(list->list[i], value))
44                         return i;
46         return -1;
47 }
49 static void free_list(struct list *list)
50 {
51         int i;
53         if (list->alloc == 0)
54                 return;
56         for (i = 0; i < list->nr; i++) {
57                 free(list->list[i]);
58                 if (list->payload[i])
59                         free(list->payload[i]);
60         }
61         free(list->list);
62         free(list->payload);
63         list->nr = list->alloc = 0;
64 }
66 struct src_data {
67         struct list branch, tag, r_branch, generic;
68         int head_status;
69 };
71 static struct list srcs = { NULL, NULL, 0, 0};
72 static struct list origins = { NULL, NULL, 0, 0};
74 static int handle_line(char *line)
75 {
76         int i, len = strlen(line);
77         unsigned char *sha1;
78         char *src, *origin;
79         struct src_data *src_data;
80         int pulling_head = 0;
82         if (len < 43 || line[40] != '\t')
83                 return 1;
85         if (!strncmp(line + 41, "not-for-merge", 13))
86                 return 0;
88         if (line[41] != '\t')
89                 return 2;
91         line[40] = 0;
92         sha1 = xmalloc(20);
93         i = get_sha1(line, sha1);
94         line[40] = '\t';
95         if (i)
96                 return 3;
98         if (line[len - 1] == '\n')
99                 line[len - 1] = 0;
100         line += 42;
102         src = strstr(line, " of ");
103         if (src) {
104                 *src = 0;
105                 src += 4;
106                 pulling_head = 0;
107         } else {
108                 src = line;
109                 pulling_head = 1;
110         }
112         i = find_in_list(&srcs, src);
113         if (i < 0) {
114                 i = srcs.nr;
115                 append_to_list(&srcs, strdup(src),
116                                 xcalloc(1, sizeof(struct src_data)));
117         }
118         src_data = srcs.payload[i];
120         if (pulling_head) {
121                 origin = strdup(src);
122                 src_data->head_status |= 1;
123         } else if (!strncmp(line, "branch ", 7)) {
124                 origin = strdup(line + 7);
125                 append_to_list(&src_data->branch, origin, NULL);
126                 src_data->head_status |= 2;
127         } else if (!strncmp(line, "tag ", 4)) {
128                 origin = line;
129                 append_to_list(&src_data->tag, strdup(origin + 4), NULL);
130                 src_data->head_status |= 2;
131         } else if (!strncmp(line, "remote branch ", 14)) {
132                 origin = strdup(line + 14);
133                 append_to_list(&src_data->r_branch, origin, NULL);
134                 src_data->head_status |= 2;
135         } else {
136                 origin = strdup(src);
137                 append_to_list(&src_data->generic, strdup(line), NULL);
138                 src_data->head_status |= 2;
139         }
141         if (!strcmp(".", src) || !strcmp(src, origin)) {
142                 int len = strlen(origin);
143                 if (origin[0] == '\'' && origin[len - 1] == '\'') {
144                         char *new_origin = malloc(len - 1);
145                         memcpy(new_origin, origin + 1, len - 2);
146                         new_origin[len - 1] = 0;
147                         origin = new_origin;
148                 } else
149                         origin = strdup(origin);
150         } else {
151                 char *new_origin = malloc(strlen(origin) + strlen(src) + 5);
152                 sprintf(new_origin, "%s of %s", origin, src);
153                 origin = new_origin;
154         }
155         append_to_list(&origins, origin, sha1);
156         return 0;
159 static void print_joined(const char *singular, const char *plural,
160                 struct list *list)
162         if (list->nr == 0)
163                 return;
164         if (list->nr == 1) {
165                 printf("%s%s", singular, list->list[0]);
166         } else {
167                 int i;
168                 printf("%s", plural);
169                 for (i = 0; i < list->nr - 1; i++)
170                         printf("%s%s", i > 0 ? ", " : "", list->list[i]);
171                 printf(" and %s", list->list[list->nr - 1]);
172         }
175 static void shortlog(const char *name, unsigned char *sha1,
176                 struct commit *head, struct rev_info *rev, int limit)
178         int i, count = 0;
179         struct commit *commit;
180         struct object *branch;
181         struct list subjects = { NULL, NULL, 0, 0 };
182         int flags = UNINTERESTING | TREECHANGE | SEEN | SHOWN | ADDED;
184         branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
185         if (!branch || branch->type != OBJ_COMMIT)
186                 return;
188         setup_revisions(0, NULL, rev, NULL);
189         rev->ignore_merges = 1;
190         add_pending_object(rev, branch, name);
191         add_pending_object(rev, &head->object, "^HEAD");
192         head->object.flags |= UNINTERESTING;
193         prepare_revision_walk(rev);
194         while ((commit = get_revision(rev)) != NULL) {
195                 char *oneline, *bol, *eol;
197                 /* ignore merges */
198                 if (commit->parents && commit->parents->next)
199                         continue;
201                 count++;
202                 if (subjects.nr > limit)
203                         continue;
205                 bol = strstr(commit->buffer, "\n\n");
206                 if (!bol) {
207                         append_to_list(&subjects, strdup(sha1_to_hex(
208                                                         commit->object.sha1)),
209                                         NULL);
210                         continue;
211                 }
213                 bol += 2;
214                 eol = strchr(bol, '\n');
216                 if (eol) {
217                         int len = eol - bol;
218                         oneline = malloc(len + 1);
219                         memcpy(oneline, bol, len);
220                         oneline[len] = 0;
221                 } else
222                         oneline = strdup(bol);
223                 append_to_list(&subjects, oneline, NULL);
224         }
226         if (count > limit)
227                 printf("\n* %s: (%d commits)\n", name, count);
228         else
229                 printf("\n* %s:\n", name);
231         for (i = 0; i < subjects.nr; i++)
232                 if (i >= limit)
233                         printf("  ...\n");
234                 else
235                         printf("  %s\n", subjects.list[i]);
237         clear_commit_marks((struct commit *)branch, flags);
238         clear_commit_marks(head, flags);
239         free_commit_list(rev->commits);
240         rev->commits = NULL;
241         rev->pending.nr = 0;
243         free_list(&subjects);
246 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
248         int limit = 20, i = 0;
249         char line[1024];
250         FILE *in = stdin;
251         const char *sep = "";
252         unsigned char head_sha1[20];
253         const char *head, *current_branch;
255         git_config(fmt_merge_msg_config);
257         while (argc > 1) {
258                 if (!strcmp(argv[1], "--summary"))
259                         merge_summary = 1;
260                 else if (!strcmp(argv[1], "--no-summary"))
261                         merge_summary = 0;
262                 else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
263                         if (argc < 2)
264                                 die ("Which file?");
265                         if (!strcmp(argv[2], "-"))
266                                 in = stdin;
267                         else {
268                                 fclose(in);
269                                 in = fopen(argv[2], "r");
270                         }
271                         argc--; argv++;
272                 } else
273                         break;
274                 argc--; argv++;
275         }
277         if (argc > 1)
278                 usage(fmt_merge_msg_usage);
280         /* get current branch */
281         head = strdup(git_path("HEAD"));
282         current_branch = resolve_ref(head, head_sha1, 1);
283         current_branch += strlen(head) - 4;
284         free((char *)head);
285         if (!strncmp(current_branch, "refs/heads/", 11))
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);
294         }
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;
307                 }
308                 if (src_data->head_status == 3) {
309                         subsep = ", ";
310                         printf("HEAD");
311                 }
312                 if (src_data->branch.nr) {
313                         printf(subsep);
314                         subsep = ", ";
315                         print_joined("branch ", "branches ", &src_data->branch);
316                 }
317                 if (src_data->r_branch.nr) {
318                         printf(subsep);
319                         subsep = ", ";
320                         print_joined("remote branch ", "remote branches ",
321                                         &src_data->r_branch);
322                 }
323                 if (src_data->tag.nr) {
324                         printf(subsep);
325                         subsep = ", ";
326                         print_joined("tag ", "tags ", &src_data->tag);
327                 }
328                 if (src_data->generic.nr) {
329                         printf(subsep);
330                         print_joined("commit ", "commits ", &src_data->generic);
331                 }
332                 if (strcmp(".", srcs.list[i]))
333                         printf(" of %s", srcs.list[i]);
334         }
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);
354         }
356         /* No cleanup yet; is standalone anyway */
358         return 0;