Code

fmt-merge-msg: use pretty.c routines
[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 * const fmt_merge_msg_usage[] = {
9         "git fmt-merge-msg [--log|--no-log] [--file <file>]",
10         NULL
11 };
13 static int merge_summary;
15 static int fmt_merge_msg_config(const char *key, const char *value, void *cb)
16 {
17         static int found_merge_log = 0;
18         if (!strcmp("merge.log", key)) {
19                 found_merge_log = 1;
20                 merge_summary = git_config_bool(key, value);
21         }
22         if (!found_merge_log && !strcmp("merge.summary", key))
23                 merge_summary = git_config_bool(key, value);
24         return 0;
25 }
27 struct list {
28         char **list;
29         void **payload;
30         unsigned nr, alloc;
31 };
33 static void append_to_list(struct list *list, char *value, void *payload)
34 {
35         if (list->nr == list->alloc) {
36                 list->alloc += 32;
37                 list->list = xrealloc(list->list, sizeof(char *) * list->alloc);
38                 list->payload = xrealloc(list->payload,
39                                 sizeof(char *) * list->alloc);
40         }
41         list->payload[list->nr] = payload;
42         list->list[list->nr++] = value;
43 }
45 static int find_in_list(struct list *list, char *value)
46 {
47         int i;
49         for (i = 0; i < list->nr; i++)
50                 if (!strcmp(list->list[i], value))
51                         return i;
53         return -1;
54 }
56 static void free_list(struct list *list)
57 {
58         int i;
60         if (list->alloc == 0)
61                 return;
63         for (i = 0; i < list->nr; i++) {
64                 free(list->list[i]);
65                 free(list->payload[i]);
66         }
67         free(list->list);
68         free(list->payload);
69         list->nr = list->alloc = 0;
70 }
72 struct src_data {
73         struct list branch, tag, r_branch, generic;
74         int head_status;
75 };
77 static struct list srcs = { NULL, NULL, 0, 0};
78 static struct list origins = { NULL, NULL, 0, 0};
80 static int handle_line(char *line)
81 {
82         int i, len = strlen(line);
83         unsigned char *sha1;
84         char *src, *origin;
85         struct src_data *src_data;
86         int pulling_head = 0;
88         if (len < 43 || line[40] != '\t')
89                 return 1;
91         if (!prefixcmp(line + 41, "not-for-merge"))
92                 return 0;
94         if (line[41] != '\t')
95                 return 2;
97         line[40] = 0;
98         sha1 = xmalloc(20);
99         i = get_sha1(line, sha1);
100         line[40] = '\t';
101         if (i)
102                 return 3;
104         if (line[len - 1] == '\n')
105                 line[len - 1] = 0;
106         line += 42;
108         src = strstr(line, " of ");
109         if (src) {
110                 *src = 0;
111                 src += 4;
112                 pulling_head = 0;
113         } else {
114                 src = line;
115                 pulling_head = 1;
116         }
118         i = find_in_list(&srcs, src);
119         if (i < 0) {
120                 i = srcs.nr;
121                 append_to_list(&srcs, xstrdup(src),
122                                 xcalloc(1, sizeof(struct src_data)));
123         }
124         src_data = srcs.payload[i];
126         if (pulling_head) {
127                 origin = xstrdup(src);
128                 src_data->head_status |= 1;
129         } else if (!prefixcmp(line, "branch ")) {
130                 origin = xstrdup(line + 7);
131                 append_to_list(&src_data->branch, origin, NULL);
132                 src_data->head_status |= 2;
133         } else if (!prefixcmp(line, "tag ")) {
134                 origin = line;
135                 append_to_list(&src_data->tag, xstrdup(origin + 4), NULL);
136                 src_data->head_status |= 2;
137         } else if (!prefixcmp(line, "remote branch ")) {
138                 origin = xstrdup(line + 14);
139                 append_to_list(&src_data->r_branch, origin, NULL);
140                 src_data->head_status |= 2;
141         } else {
142                 origin = xstrdup(src);
143                 append_to_list(&src_data->generic, xstrdup(line), NULL);
144                 src_data->head_status |= 2;
145         }
147         if (!strcmp(".", src) || !strcmp(src, origin)) {
148                 int len = strlen(origin);
149                 if (origin[0] == '\'' && origin[len - 1] == '\'') {
150                         origin = xmemdupz(origin + 1, len - 2);
151                 } else {
152                         origin = xstrdup(origin);
153                 }
154         } else {
155                 char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
156                 sprintf(new_origin, "%s of %s", origin, src);
157                 origin = new_origin;
158         }
159         append_to_list(&origins, origin, sha1);
160         return 0;
163 static void print_joined(const char *singular, const char *plural,
164                 struct list *list, struct strbuf *out)
166         if (list->nr == 0)
167                 return;
168         if (list->nr == 1) {
169                 strbuf_addf(out, "%s%s", singular, list->list[0]);
170         } else {
171                 int i;
172                 strbuf_addstr(out, plural);
173                 for (i = 0; i < list->nr - 1; i++)
174                         strbuf_addf(out, "%s%s", i > 0 ? ", " : "", list->list[i]);
175                 strbuf_addf(out, " and %s", list->list[list->nr - 1]);
176         }
179 static void shortlog(const char *name, unsigned char *sha1,
180                 struct commit *head, struct rev_info *rev, int limit,
181                 struct strbuf *out)
183         int i, count = 0;
184         struct commit *commit;
185         struct object *branch;
186         struct list subjects = { NULL, NULL, 0, 0 };
187         int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;
188         struct strbuf sb = STRBUF_INIT;
190         branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
191         if (!branch || branch->type != OBJ_COMMIT)
192                 return;
194         setup_revisions(0, NULL, rev, NULL);
195         rev->ignore_merges = 1;
196         add_pending_object(rev, branch, name);
197         add_pending_object(rev, &head->object, "^HEAD");
198         head->object.flags |= UNINTERESTING;
199         if (prepare_revision_walk(rev))
200                 die("revision walk setup failed");
201         while ((commit = get_revision(rev)) != NULL) {
202                 struct pretty_print_context ctx = {0};
204                 /* ignore merges */
205                 if (commit->parents && commit->parents->next)
206                         continue;
208                 count++;
209                 if (subjects.nr > limit)
210                         continue;
212                 format_commit_message(commit, "%s", &sb, &ctx);
213                 strbuf_ltrim(&sb);
215                 if (!sb.len)
216                         append_to_list(&subjects, xstrdup(sha1_to_hex(
217                                                         commit->object.sha1)),
218                                         NULL);
219                 else
220                         append_to_list(&subjects, strbuf_detach(&sb, NULL),
221                                         NULL);
222         }
224         if (count > limit)
225                 strbuf_addf(out, "\n* %s: (%d commits)\n", name, count);
226         else
227                 strbuf_addf(out, "\n* %s:\n", name);
229         for (i = 0; i < subjects.nr; i++)
230                 if (i >= limit)
231                         strbuf_addf(out, "  ...\n");
232                 else
233                         strbuf_addf(out, "  %s\n", subjects.list[i]);
235         clear_commit_marks((struct commit *)branch, flags);
236         clear_commit_marks(head, flags);
237         free_commit_list(rev->commits);
238         rev->commits = NULL;
239         rev->pending.nr = 0;
241         free_list(&subjects);
244 int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) {
245         int limit = 20, i = 0, pos = 0;
246         char *sep = "";
247         unsigned char head_sha1[20];
248         const char *current_branch;
250         /* get current branch */
251         current_branch = resolve_ref("HEAD", head_sha1, 1, NULL);
252         if (!current_branch)
253                 die("No current branch");
254         if (!prefixcmp(current_branch, "refs/heads/"))
255                 current_branch += 11;
257         /* get a line */
258         while (pos < in->len) {
259                 int len;
260                 char *newline, *p = in->buf + pos;
262                 newline = strchr(p, '\n');
263                 len = newline ? newline - p : strlen(p);
264                 pos += len + !!newline;
265                 i++;
266                 p[len] = 0;
267                 if (handle_line(p))
268                         die ("Error in line %d: %.*s", i, len, p);
269         }
271         if (!srcs.nr)
272                 return 0;
274         strbuf_addstr(out, "Merge ");
275         for (i = 0; i < srcs.nr; i++) {
276                 struct src_data *src_data = srcs.payload[i];
277                 const char *subsep = "";
279                 strbuf_addstr(out, sep);
280                 sep = "; ";
282                 if (src_data->head_status == 1) {
283                         strbuf_addstr(out, srcs.list[i]);
284                         continue;
285                 }
286                 if (src_data->head_status == 3) {
287                         subsep = ", ";
288                         strbuf_addstr(out, "HEAD");
289                 }
290                 if (src_data->branch.nr) {
291                         strbuf_addstr(out, subsep);
292                         subsep = ", ";
293                         print_joined("branch ", "branches ", &src_data->branch,
294                                         out);
295                 }
296                 if (src_data->r_branch.nr) {
297                         strbuf_addstr(out, subsep);
298                         subsep = ", ";
299                         print_joined("remote branch ", "remote branches ",
300                                         &src_data->r_branch, out);
301                 }
302                 if (src_data->tag.nr) {
303                         strbuf_addstr(out, subsep);
304                         subsep = ", ";
305                         print_joined("tag ", "tags ", &src_data->tag, out);
306                 }
307                 if (src_data->generic.nr) {
308                         strbuf_addstr(out, subsep);
309                         print_joined("commit ", "commits ", &src_data->generic,
310                                         out);
311                 }
312                 if (strcmp(".", srcs.list[i]))
313                         strbuf_addf(out, " of %s", srcs.list[i]);
314         }
316         if (!strcmp("master", current_branch))
317                 strbuf_addch(out, '\n');
318         else
319                 strbuf_addf(out, " into %s\n", current_branch);
321         if (merge_summary) {
322                 struct commit *head;
323                 struct rev_info rev;
325                 head = lookup_commit(head_sha1);
326                 init_revisions(&rev, NULL);
327                 rev.commit_format = CMIT_FMT_ONELINE;
328                 rev.ignore_merges = 1;
329                 rev.limited = 1;
331                 for (i = 0; i < origins.nr; i++)
332                         shortlog(origins.list[i], origins.payload[i],
333                                         head, &rev, limit, out);
334         }
335         return 0;
338 int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)
340         const char *inpath = NULL;
341         struct option options[] = {
342                 OPT_BOOLEAN(0, "log",     &merge_summary, "populate log with the shortlog"),
343                 OPT_BOOLEAN(0, "summary", &merge_summary, "alias for --log"),
344                 OPT_FILENAME('F', "file", &inpath, "file to read from"),
345                 OPT_END()
346         };
348         FILE *in = stdin;
349         struct strbuf input = STRBUF_INIT, output = STRBUF_INIT;
350         int ret;
352         git_config(fmt_merge_msg_config, NULL);
353         argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage,
354                              0);
355         if (argc > 0)
356                 usage_with_options(fmt_merge_msg_usage, options);
358         if (inpath && strcmp(inpath, "-")) {
359                 in = fopen(inpath, "r");
360                 if (!in)
361                         die_errno("cannot open '%s'", inpath);
362         }
364         if (strbuf_read(&input, fileno(in), 0) < 0)
365                 die_errno("could not read input file");
366         ret = fmt_merge_msg(merge_summary, &input, &output);
367         if (ret)
368                 return ret;
369         write_in_full(STDOUT_FILENO, output.buf, output.len);
370         return 0;