]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-fmt-merge-msg.c
Merge branch 'jc/fmt-merge-msg-test'
[thirdparty/git.git] / builtin-fmt-merge-msg.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "diff.h"
4 #include "revision.h"
5 #include "tag.h"
6
7 static const char *fmt_merge_msg_usage =
8 "git-fmt-merge-msg [--summary] [--no-summary] [--file <file>]";
9
10 static int merge_summary = 0;
11
12 static int fmt_merge_msg_config(const char *key, const char *value)
13 {
14 if (!strcmp("merge.summary", key))
15 merge_summary = git_config_bool(key, value);
16 return 0;
17 }
18
19 struct list {
20 char **list;
21 void **payload;
22 unsigned nr, alloc;
23 };
24
25 static void append_to_list(struct list *list, char *value, void *payload)
26 {
27 if (list->nr == list->alloc) {
28 list->alloc += 32;
29 list->list = realloc(list->list, sizeof(char *) * list->alloc);
30 list->payload = realloc(list->payload,
31 sizeof(char *) * list->alloc);
32 }
33 list->payload[list->nr] = payload;
34 list->list[list->nr++] = value;
35 }
36
37 static int find_in_list(struct list *list, char *value)
38 {
39 int i;
40
41 for (i = 0; i < list->nr; i++)
42 if (!strcmp(list->list[i], value))
43 return i;
44
45 return -1;
46 }
47
48 static void free_list(struct list *list)
49 {
50 int i;
51
52 if (list->alloc == 0)
53 return;
54
55 for (i = 0; i < list->nr; i++) {
56 free(list->list[i]);
57 if (list->payload[i])
58 free(list->payload[i]);
59 }
60 free(list->list);
61 free(list->payload);
62 list->nr = list->alloc = 0;
63 }
64
65 struct src_data {
66 struct list branch, tag, r_branch, generic;
67 int head_status;
68 };
69
70 static struct list srcs = { NULL, NULL, 0, 0};
71 static struct list origins = { NULL, NULL, 0, 0};
72
73 static int handle_line(char *line)
74 {
75 int i, len = strlen(line);
76 unsigned char *sha1;
77 char *src, *origin;
78 struct src_data *src_data;
79
80 if (len < 43 || line[40] != '\t')
81 return 1;
82
83 if (!strncmp(line + 41, "not-for-merge", 13))
84 return 0;
85
86 if (line[41] != '\t')
87 return 2;
88
89 line[40] = 0;
90 sha1 = xmalloc(20);
91 i = get_sha1(line, sha1);
92 line[40] = '\t';
93 if (i)
94 return 3;
95
96 if (line[len - 1] == '\n')
97 line[len - 1] = 0;
98 line += 42;
99
100 src = strstr(line, " of ");
101 if (src) {
102 *src = 0;
103 src += 4;
104 } else
105 src = "HEAD";
106
107 i = find_in_list(&srcs, src);
108 if (i < 0) {
109 i = srcs.nr;
110 append_to_list(&srcs, strdup(src),
111 xcalloc(1, sizeof(struct src_data)));
112 }
113 src_data = srcs.payload[i];
114
115 if (!strncmp(line, "branch ", 7)) {
116 origin = strdup(line + 7);
117 append_to_list(&src_data->branch, origin, NULL);
118 src_data->head_status |= 2;
119 } else if (!strncmp(line, "tag ", 4)) {
120 origin = line;
121 append_to_list(&src_data->tag, strdup(origin + 4), NULL);
122 src_data->head_status |= 2;
123 } else if (!strncmp(line, "remote branch ", 14)) {
124 origin = strdup(line + 14);
125 append_to_list(&src_data->r_branch, origin, NULL);
126 src_data->head_status |= 2;
127 } else if (!strcmp(line, "HEAD")) {
128 origin = strdup(src);
129 src_data->head_status |= 1;
130 } else {
131 origin = strdup(src);
132 append_to_list(&src_data->generic, strdup(line), NULL);
133 src_data->head_status |= 2;
134 }
135
136 if (!strcmp(".", src) || !strcmp(src, origin)) {
137 int len = strlen(origin);
138 if (origin[0] == '\'' && origin[len - 1] == '\'') {
139 char *new_origin = malloc(len - 1);
140 memcpy(new_origin, origin + 1, len - 2);
141 new_origin[len - 1] = 0;
142 origin = new_origin;
143 } else
144 origin = strdup(origin);
145 } else {
146 char *new_origin = malloc(strlen(origin) + strlen(src) + 5);
147 sprintf(new_origin, "%s of %s", origin, src);
148 origin = new_origin;
149 }
150 append_to_list(&origins, origin, sha1);
151 return 0;
152 }
153
154 static void print_joined(const char *singular, const char *plural,
155 struct list *list)
156 {
157 if (list->nr == 0)
158 return;
159 if (list->nr == 1) {
160 printf("%s%s", singular, list->list[0]);
161 } else {
162 int i;
163 printf("%s", plural);
164 for (i = 0; i < list->nr - 1; i++)
165 printf("%s%s", i > 0 ? ", " : "", list->list[i]);
166 printf(" and %s", list->list[list->nr - 1]);
167 }
168 }
169
170 static void shortlog(const char *name, unsigned char *sha1,
171 struct commit *head, struct rev_info *rev, int limit)
172 {
173 int i, count = 0;
174 struct commit *commit;
175 struct object *branch;
176 struct list subjects = { NULL, NULL, 0, 0 };
177 int flags = UNINTERESTING | TREECHANGE | SEEN | SHOWN | ADDED;
178
179 branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40);
180 if (!branch || branch->type != TYPE_COMMIT)
181 return;
182
183 setup_revisions(0, NULL, rev, NULL);
184 rev->ignore_merges = 1;
185 add_pending_object(rev, branch, name);
186 add_pending_object(rev, &head->object, "^HEAD");
187 head->object.flags |= UNINTERESTING;
188 prepare_revision_walk(rev);
189 while ((commit = get_revision(rev)) != NULL) {
190 char *oneline, *bol, *eol;
191
192 /* ignore merges */
193 if (commit->parents && commit->parents->next)
194 continue;
195
196 count++;
197 if (subjects.nr > limit)
198 continue;
199
200 bol = strstr(commit->buffer, "\n\n");
201 if (!bol) {
202 append_to_list(&subjects, strdup(sha1_to_hex(
203 commit->object.sha1)),
204 NULL);
205 continue;
206 }
207
208 bol += 2;
209 eol = strchr(bol, '\n');
210
211 if (eol) {
212 int len = eol - bol;
213 oneline = malloc(len + 1);
214 memcpy(oneline, bol, len);
215 oneline[len] = 0;
216 } else
217 oneline = strdup(bol);
218 append_to_list(&subjects, oneline, NULL);
219 }
220
221 if (count > limit)
222 printf("\n* %s: (%d commits)\n", name, count);
223 else
224 printf("\n* %s:\n", name);
225
226 for (i = 0; i < subjects.nr; i++)
227 if (i >= limit)
228 printf(" ...\n");
229 else
230 printf(" %s\n", subjects.list[i]);
231
232 clear_commit_marks((struct commit *)branch, flags);
233 clear_commit_marks(head, flags);
234 free_commit_list(rev->commits);
235 rev->commits = NULL;
236 rev->pending.nr = 0;
237
238 free_list(&subjects);
239 }
240
241 int cmd_fmt_merge_msg(int argc, char **argv, char **envp)
242 {
243 int limit = 20, i = 0;
244 char line[1024];
245 FILE *in = stdin;
246 const char *sep = "";
247 unsigned char head_sha1[20];
248 const char *head, *current_branch;
249
250 git_config(fmt_merge_msg_config);
251
252 while (argc > 1) {
253 if (!strcmp(argv[1], "--summary"))
254 merge_summary = 1;
255 else if (!strcmp(argv[1], "--no-summary"))
256 merge_summary = 0;
257 else if (!strcmp(argv[1], "-F") || !strcmp(argv[1], "--file")) {
258 if (argc < 2)
259 die ("Which file?");
260 if (!strcmp(argv[2], "-"))
261 in = stdin;
262 else {
263 fclose(in);
264 in = fopen(argv[2], "r");
265 }
266 argc--; argv++;
267 } else
268 break;
269 argc--; argv++;
270 }
271
272 if (argc > 1)
273 usage(fmt_merge_msg_usage);
274
275 /* get current branch */
276 head = strdup(git_path("HEAD"));
277 current_branch = resolve_ref(head, head_sha1, 1);
278 current_branch += strlen(head) - 4;
279 free((char *)head);
280 if (!strncmp(current_branch, "refs/heads/", 11))
281 current_branch += 11;
282
283 while (fgets(line, sizeof(line), in)) {
284 i++;
285 if (line[0] == 0)
286 continue;
287 if (handle_line(line))
288 die ("Error in line %d: %s", i, line);
289 }
290
291 printf("Merge ");
292 for (i = 0; i < srcs.nr; i++) {
293 struct src_data *src_data = srcs.payload[i];
294 const char *subsep = "";
295
296 printf(sep);
297 sep = "; ";
298
299 if (src_data->head_status == 1) {
300 printf(srcs.list[i]);
301 continue;
302 }
303 if (src_data->head_status == 3) {
304 subsep = ", ";
305 printf("HEAD");
306 }
307 if (src_data->branch.nr) {
308 printf(subsep);
309 subsep = ", ";
310 print_joined("branch ", "branches ", &src_data->branch);
311 }
312 if (src_data->r_branch.nr) {
313 printf(subsep);
314 subsep = ", ";
315 print_joined("remote branch ", "remote branches ",
316 &src_data->r_branch);
317 }
318 if (src_data->tag.nr) {
319 printf(subsep);
320 subsep = ", ";
321 print_joined("tag ", "tags ", &src_data->tag);
322 }
323 if (src_data->generic.nr) {
324 printf(subsep);
325 print_joined("commit ", "commits ", &src_data->generic);
326 }
327 if (strcmp(".", srcs.list[i]))
328 printf(" of %s", srcs.list[i]);
329 }
330
331 if (!strcmp("master", current_branch))
332 putchar('\n');
333 else
334 printf(" into %s\n", current_branch);
335
336 if (merge_summary) {
337 struct commit *head;
338 struct rev_info rev;
339
340 head = lookup_commit(head_sha1);
341 init_revisions(&rev);
342 rev.commit_format = CMIT_FMT_ONELINE;
343 rev.ignore_merges = 1;
344 rev.limited = 1;
345
346 for (i = 0; i < origins.nr; i++)
347 shortlog(origins.list[i], origins.payload[i],
348 head, &rev, limit);
349 }
350
351 /* No cleanup yet; is standalone anyway */
352
353 return 0;
354 }
355