]> Pileus Git - ~andy/git/blob - builtin-fmt-merge-msg.c
merge, pull: add '--(no-)log' command line option
[~andy/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"
7
8 static const char *fmt_merge_msg_usage =
9         "git-fmt-merge-msg [--log] [--no-log] [--file <file>]";
10
11 static int merge_summary;
12
13 static int fmt_merge_msg_config(const char *key, const char *value)
14 {
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))
21                 merge_summary = git_config_bool(key, value);
22         return 0;
23 }
24
25 struct list {
26         char **list;
27         void **payload;
28         unsigned nr, alloc;
29 };
30
31 static void append_to_list(struct list *list, char *value, void *payload)
32 {
33         if (list->nr == list->alloc) {
34                 list->alloc += 32;
35                 list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
36                 list->payload = xrealloc(list->payload,
37                                 sizeof(char *) * list->alloc);
38         }
39         list->payload[list->nr] = payload;
40         list->list[list->nr++] = value;
41 }
42
43 static 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
54 static 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]);
63                 free(list->payload[i]);
64         }
65         free(list->list);
66         free(list->payload);
67         list->nr = list->alloc = 0;
68 }
69
70 struct src_data {
71         struct list branch, tag, r_branch, generic;
72         int head_status;
73 };
74
75 static struct list srcs = { NULL, NULL, 0, 0};
76 static struct list origins = { NULL, NULL, 0, 0};
77
78 static 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;
84         int pulling_head = 0;
85
86         if (len < 43 || line[40] != '\t')
87                 return 1;
88
89         if (!prefixcmp(line + 41, "not-for-merge"))
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;
110                 pulling_head = 0;
111         } else {
112                 src = line;
113                 pulling_head = 1;
114         }
115
116         i = find_in_list(&srcs, src);
117         if (i < 0) {
118                 i = srcs.nr;
119                 append_to_list(&srcs, xstrdup(src),
120                                 xcalloc(1, sizeof(struct src_data)));
121         }
122         src_data = srcs.payload[i];
123
124         if (pulling_head) {
125                 origin = xstrdup(src);
126                 src_data->head_status |= 1;
127         } else if (!prefixcmp(line, "branch ")) {
128                 origin = xstrdup(line + 7);
129                 append_to_list(&src_data->branch, origin, NULL);
130                 src_data->head_status |= 2;
131         } else if (!prefixcmp(line, "tag ")) {
132                 origin = line;
133                 append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
134                 src_data->head_status |= 2;
135         } else if (!prefixcmp(line, "remote branch ")) {
136                 origin = xstrdup(line + 14);
137                 append_to_list(&src_data->r_branch, origin, NULL);
138                 src_data->head_status |= 2;
139         } else {
140                 origin = xstrdup(src);
141                 append_to_list(&src_data->generic, xstrdup(line), NULL);
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] == '\'') {
148                         origin = xmemdupz(origin + 1, len - 2);
149                 } else {
150                         origin = xstrdup(origin);
151                 }
152         } else {
153                 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
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
161 static 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
177 static 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 };
184         int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
185
186         branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
187         if (!branch || branch->type != OBJ_COMMIT)
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;
195         if (prepare_revision_walk(rev))
196                 die("revision walk setup failed");
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");
209                 if (!bol) {
210                         append_to_list(&subjects, xstrdup(sha1_to_hex(
211                                                         commit->object.sha1)),
212                                         NULL);
213                         continue;
214                 }
215
216                 bol += 2;
217                 eol = strchr(bol, '\n');
218                 if (eol) {
219                         oneline = xmemdupz(bol, eol - bol);
220                 } else {
221                         oneline = xstrdup(bol);
222                 }
223                 append_to_list(&subjects, oneline, NULL);
224         }
225
226         if (count > limit)
227                 printf("\n* %s: (%d commits)\n", name, count);
228         else
229                 printf("\n* %s:\n", name);
230
231         for (i = 0; i < subjects.nr; i++)
232                 if (i >= limit)
233                         printf("  ...\n");
234                 else
235                         printf("  %s\n", subjects.list[i]);
236
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;
242
243         free_list(&subjects);
244 }
245
246 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
247 {
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 *current_branch;
254
255         git_config(fmt_merge_msg_config);
256
257         while (argc > 1) {
258                 if (!strcmp(argv[1], "--log") || !strcmp(argv[1], "--summary"))
259                         merge_summary = 1;
260                 else if (!strcmp(argv[1], "--no-log")
261                                 || !strcmp(argv[1], "--no-summary"))
262                         merge_summary = 0;
263                 else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
264                         if (argc < 3)
265                                 die ("Which file?");
266                         if (!strcmp(argv[2], "-"))
267                                 in = stdin;
268                         else {
269                                 fclose(in);
270                                 in = fopen(argv[2], "r");
271                                 if (!in)
272                                         die("cannot open %s", argv[2]);
273                         }
274                         argc--; argv++;
275                 } else
276                         break;
277                 argc--; argv++;
278         }
279
280         if (argc > 1)
281                 usage(fmt_merge_msg_usage);
282
283         /* get current branch */
284         current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
285         if (!current_branch)
286                 die("No current branch");
287         if (!prefixcmp(current_branch, "refs/heads/"))
288                 current_branch += 11;
289
290         while (fgets(line, sizeof(line), in)) {
291                 i++;
292                 if (line[0] == 0)
293                         continue;
294                 if (handle_line(line))
295                         die ("Error in line %d: %s", i, line);
296         }
297
298         printf("Merge ");
299         for (i = 0; i < srcs.nr; i++) {
300                 struct src_data *src_data = srcs.payload[i];
301                 const char *subsep = "";
302
303                 printf(sep);
304                 sep = "; ";
305
306                 if (src_data->head_status == 1) {
307                         printf(srcs.list[i]);
308                         continue;
309                 }
310                 if (src_data->head_status == 3) {
311                         subsep = ", ";
312                         printf("HEAD");
313                 }
314                 if (src_data->branch.nr) {
315                         printf(subsep);
316                         subsep = ", ";
317                         print_joined("branch ", "branches ", &src_data->branch);
318                 }
319                 if (src_data->r_branch.nr) {
320                         printf(subsep);
321                         subsep = ", ";
322                         print_joined("remote branch ", "remote branches ",
323                                         &src_data->r_branch);
324                 }
325                 if (src_data->tag.nr) {
326                         printf(subsep);
327                         subsep = ", ";
328                         print_joined("tag ", "tags ", &src_data->tag);
329                 }
330                 if (src_data->generic.nr) {
331                         printf(subsep);
332                         print_joined("commit ", "commits ", &src_data->generic);
333                 }
334                 if (strcmp(".", srcs.list[i]))
335                         printf(" of %s", srcs.list[i]);
336         }
337
338         if (!strcmp("master", current_branch))
339                 putchar('\n');
340         else
341                 printf(" into %s\n", current_branch);
342
343         if (merge_summary) {
344                 struct commit *head;
345                 struct rev_info rev;
346
347                 head = lookup_commit(head_sha1);
348                 init_revisions(&rev, prefix);
349                 rev.commit_format = CMIT_FMT_ONELINE;
350                 rev.ignore_merges = 1;
351                 rev.limited = 1;
352
353                 for (i = 0; i < origins.nr; i++)
354                         shortlog(origins.list[i], origins.payload[i],
355                                         head, &rev, limit);
356         }
357
358         /* No cleanup yet; is standalone anyway */
359
360         return 0;
361 }