]>
Commit | Line | Data |
---|---|---|
25f38f06 | 1 | #include "builtin.h" |
00449f99 JS |
2 | #include "cache.h" |
3 | #include "commit.h" | |
4 | #include "diff.h" | |
5 | #include "revision.h" | |
6 | #include "tag.h" | |
7 | ||
c8ef0383 PH |
8 | static const char * const fmt_merge_msg_usage[] = { |
9 | "git fmt-merge-msg [--log|--no-log] [--file <file>]", | |
10 | NULL | |
11 | }; | |
00449f99 | 12 | |
96f1e58f | 13 | static int merge_summary; |
00449f99 | 14 | |
ef90d6d4 | 15 | static int fmt_merge_msg_config(const char *key, const char *value, void *cb) |
00449f99 | 16 | { |
6cd9cfef SG |
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)) | |
00449f99 JS |
23 | merge_summary = git_config_bool(key, value); |
24 | return 0; | |
25 | } | |
26 | ||
27 | struct list { | |
28 | char **list; | |
29 | void **payload; | |
30 | unsigned nr, alloc; | |
31 | }; | |
32 | ||
33 | static void append_to_list(struct list *list, char *value, void *payload) | |
34 | { | |
35 | if (list->nr == list->alloc) { | |
36 | list->alloc += 32; | |
83572c1a JF |
37 | list->list = xrealloc(list->list, sizeof(char *) * list->alloc); |
38 | list->payload = xrealloc(list->payload, | |
00449f99 JS |
39 | sizeof(char *) * list->alloc); |
40 | } | |
41 | list->payload[list->nr] = payload; | |
42 | list->list[list->nr++] = value; | |
43 | } | |
44 | ||
45 | static int find_in_list(struct list *list, char *value) | |
46 | { | |
47 | int i; | |
48 | ||
49 | for (i = 0; i < list->nr; i++) | |
50 | if (!strcmp(list->list[i], value)) | |
51 | return i; | |
52 | ||
53 | return -1; | |
54 | } | |
55 | ||
56 | static void free_list(struct list *list) | |
57 | { | |
58 | int i; | |
59 | ||
60 | if (list->alloc == 0) | |
61 | return; | |
62 | ||
63 | for (i = 0; i < list->nr; i++) { | |
64 | free(list->list[i]); | |
4cac42b1 | 65 | free(list->payload[i]); |
00449f99 JS |
66 | } |
67 | free(list->list); | |
68 | free(list->payload); | |
69 | list->nr = list->alloc = 0; | |
70 | } | |
71 | ||
72 | struct src_data { | |
73 | struct list branch, tag, r_branch, generic; | |
74 | int head_status; | |
75 | }; | |
76 | ||
77 | static struct list srcs = { NULL, NULL, 0, 0}; | |
78 | static struct list origins = { NULL, NULL, 0, 0}; | |
79 | ||
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; | |
e918c6ab | 86 | int pulling_head = 0; |
00449f99 JS |
87 | |
88 | if (len < 43 || line[40] != '\t') | |
89 | return 1; | |
90 | ||
cc44c765 | 91 | if (!prefixcmp(line + 41, "not-for-merge")) |
00449f99 JS |
92 | return 0; |
93 | ||
94 | if (line[41] != '\t') | |
95 | return 2; | |
96 | ||
97 | line[40] = 0; | |
98 | sha1 = xmalloc(20); | |
99 | i = get_sha1(line, sha1); | |
100 | line[40] = '\t'; | |
101 | if (i) | |
102 | return 3; | |
103 | ||
104 | if (line[len - 1] == '\n') | |
105 | line[len - 1] = 0; | |
106 | line += 42; | |
107 | ||
108 | src = strstr(line, " of "); | |
109 | if (src) { | |
110 | *src = 0; | |
111 | src += 4; | |
e918c6ab JH |
112 | pulling_head = 0; |
113 | } else { | |
114 | src = line; | |
115 | pulling_head = 1; | |
116 | } | |
00449f99 JS |
117 | |
118 | i = find_in_list(&srcs, src); | |
119 | if (i < 0) { | |
120 | i = srcs.nr; | |
9befac47 | 121 | append_to_list(&srcs, xstrdup(src), |
00449f99 JS |
122 | xcalloc(1, sizeof(struct src_data))); |
123 | } | |
124 | src_data = srcs.payload[i]; | |
125 | ||
e918c6ab | 126 | if (pulling_head) { |
9befac47 | 127 | origin = xstrdup(src); |
e918c6ab | 128 | src_data->head_status |= 1; |
cc44c765 | 129 | } else if (!prefixcmp(line, "branch ")) { |
9befac47 | 130 | origin = xstrdup(line + 7); |
00449f99 JS |
131 | append_to_list(&src_data->branch, origin, NULL); |
132 | src_data->head_status |= 2; | |
cc44c765 | 133 | } else if (!prefixcmp(line, "tag ")) { |
00449f99 | 134 | origin = line; |
9befac47 | 135 | append_to_list(&src_data->tag, xstrdup(origin + 4), NULL); |
00449f99 | 136 | src_data->head_status |= 2; |
cc44c765 | 137 | } else if (!prefixcmp(line, "remote branch ")) { |
9befac47 | 138 | origin = xstrdup(line + 14); |
00449f99 JS |
139 | append_to_list(&src_data->r_branch, origin, NULL); |
140 | src_data->head_status |= 2; | |
00449f99 | 141 | } else { |
9befac47 SP |
142 | origin = xstrdup(src); |
143 | append_to_list(&src_data->generic, xstrdup(line), NULL); | |
00449f99 JS |
144 | src_data->head_status |= 2; |
145 | } | |
146 | ||
147 | if (!strcmp(".", src) || !strcmp(src, origin)) { | |
148 | int len = strlen(origin); | |
149 | if (origin[0] == '\'' && origin[len - 1] == '\'') { | |
182af834 PH |
150 | origin = xmemdupz(origin + 1, len - 2); |
151 | } else { | |
9befac47 | 152 | origin = xstrdup(origin); |
182af834 | 153 | } |
00449f99 | 154 | } else { |
2d7320d0 | 155 | char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5); |
00449f99 JS |
156 | sprintf(new_origin, "%s of %s", origin, src); |
157 | origin = new_origin; | |
158 | } | |
159 | append_to_list(&origins, origin, sha1); | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static void print_joined(const char *singular, const char *plural, | |
0b9a969e | 164 | struct list *list, struct strbuf *out) |
00449f99 JS |
165 | { |
166 | if (list->nr == 0) | |
167 | return; | |
168 | if (list->nr == 1) { | |
0b9a969e | 169 | strbuf_addf(out, "%s%s", singular, list->list[0]); |
00449f99 JS |
170 | } else { |
171 | int i; | |
0b9a969e | 172 | strbuf_addstr(out, plural); |
00449f99 | 173 | for (i = 0; i < list->nr - 1; i++) |
0b9a969e MV |
174 | strbuf_addf(out, "%s%s", i > 0 ? ", " : "", list->list[i]); |
175 | strbuf_addf(out, " and %s", list->list[list->nr - 1]); | |
00449f99 JS |
176 | } |
177 | } | |
178 | ||
179 | static void shortlog(const char *name, unsigned char *sha1, | |
0b9a969e MV |
180 | struct commit *head, struct rev_info *rev, int limit, |
181 | struct strbuf *out) | |
00449f99 JS |
182 | { |
183 | int i, count = 0; | |
184 | struct commit *commit; | |
185 | struct object *branch; | |
186 | struct list subjects = { NULL, NULL, 0, 0 }; | |
7dc0fe3b | 187 | int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED; |
00449f99 JS |
188 | |
189 | branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40); | |
1974632c | 190 | if (!branch || branch->type != OBJ_COMMIT) |
00449f99 JS |
191 | return; |
192 | ||
193 | setup_revisions(0, NULL, rev, NULL); | |
194 | rev->ignore_merges = 1; | |
195 | add_pending_object(rev, branch, name); | |
196 | add_pending_object(rev, &head->object, "^HEAD"); | |
197 | head->object.flags |= UNINTERESTING; | |
3d51e1b5 MK |
198 | if (prepare_revision_walk(rev)) |
199 | die("revision walk setup failed"); | |
00449f99 JS |
200 | while ((commit = get_revision(rev)) != NULL) { |
201 | char *oneline, *bol, *eol; | |
202 | ||
203 | /* ignore merges */ | |
204 | if (commit->parents && commit->parents->next) | |
205 | continue; | |
206 | ||
207 | count++; | |
208 | if (subjects.nr > limit) | |
209 | continue; | |
210 | ||
211 | bol = strstr(commit->buffer, "\n\n"); | |
6a28518a LT |
212 | if (bol) { |
213 | unsigned char c; | |
214 | do { | |
215 | c = *++bol; | |
216 | } while (isspace(c)); | |
217 | if (!c) | |
218 | bol = NULL; | |
219 | } | |
220 | ||
00449f99 | 221 | if (!bol) { |
9befac47 | 222 | append_to_list(&subjects, xstrdup(sha1_to_hex( |
00449f99 JS |
223 | commit->object.sha1)), |
224 | NULL); | |
225 | continue; | |
226 | } | |
227 | ||
00449f99 | 228 | eol = strchr(bol, '\n'); |
00449f99 | 229 | if (eol) { |
182af834 PH |
230 | oneline = xmemdupz(bol, eol - bol); |
231 | } else { | |
9befac47 | 232 | oneline = xstrdup(bol); |
182af834 | 233 | } |
00449f99 JS |
234 | append_to_list(&subjects, oneline, NULL); |
235 | } | |
236 | ||
237 | if (count > limit) | |
0b9a969e | 238 | strbuf_addf(out, "\n* %s: (%d commits)\n", name, count); |
00449f99 | 239 | else |
0b9a969e | 240 | strbuf_addf(out, "\n* %s:\n", name); |
00449f99 JS |
241 | |
242 | for (i = 0; i < subjects.nr; i++) | |
243 | if (i >= limit) | |
0b9a969e | 244 | strbuf_addf(out, " ...\n"); |
00449f99 | 245 | else |
0b9a969e | 246 | strbuf_addf(out, " %s\n", subjects.list[i]); |
00449f99 JS |
247 | |
248 | clear_commit_marks((struct commit *)branch, flags); | |
249 | clear_commit_marks(head, flags); | |
250 | free_commit_list(rev->commits); | |
251 | rev->commits = NULL; | |
252 | rev->pending.nr = 0; | |
253 | ||
254 | free_list(&subjects); | |
255 | } | |
256 | ||
0b9a969e MV |
257 | int fmt_merge_msg(int merge_summary, struct strbuf *in, struct strbuf *out) { |
258 | int limit = 20, i = 0, pos = 0; | |
00449f99 | 259 | char line[1024]; |
0b9a969e | 260 | char *p = line, *sep = ""; |
00449f99 | 261 | unsigned char head_sha1[20]; |
ed378ec7 | 262 | const char *current_branch; |
00449f99 | 263 | |
00449f99 | 264 | /* get current branch */ |
8da19775 | 265 | current_branch = resolve_ref("HEAD", head_sha1, 1, NULL); |
6b1f8c32 LT |
266 | if (!current_branch) |
267 | die("No current branch"); | |
cc44c765 | 268 | if (!prefixcmp(current_branch, "refs/heads/")) |
00449f99 JS |
269 | current_branch += 11; |
270 | ||
0b9a969e MV |
271 | /* get a line */ |
272 | while (pos < in->len) { | |
273 | int len; | |
274 | char *newline; | |
275 | ||
276 | p = in->buf + pos; | |
277 | newline = strchr(p, '\n'); | |
278 | len = newline ? newline - p : strlen(p); | |
279 | pos += len + !!newline; | |
00449f99 | 280 | i++; |
0b9a969e MV |
281 | p[len] = 0; |
282 | if (handle_line(p)) | |
283 | die ("Error in line %d: %.*s", i, len, p); | |
00449f99 JS |
284 | } |
285 | ||
0b9a969e | 286 | strbuf_addstr(out, "Merge "); |
00449f99 JS |
287 | for (i = 0; i < srcs.nr; i++) { |
288 | struct src_data *src_data = srcs.payload[i]; | |
289 | const char *subsep = ""; | |
290 | ||
0b9a969e | 291 | strbuf_addstr(out, sep); |
00449f99 JS |
292 | sep = "; "; |
293 | ||
294 | if (src_data->head_status == 1) { | |
0b9a969e | 295 | strbuf_addstr(out, srcs.list[i]); |
00449f99 JS |
296 | continue; |
297 | } | |
298 | if (src_data->head_status == 3) { | |
299 | subsep = ", "; | |
0b9a969e | 300 | strbuf_addstr(out, "HEAD"); |
00449f99 JS |
301 | } |
302 | if (src_data->branch.nr) { | |
0b9a969e | 303 | strbuf_addstr(out, subsep); |
00449f99 | 304 | subsep = ", "; |
0b9a969e MV |
305 | print_joined("branch ", "branches ", &src_data->branch, |
306 | out); | |
00449f99 JS |
307 | } |
308 | if (src_data->r_branch.nr) { | |
0b9a969e | 309 | strbuf_addstr(out, subsep); |
00449f99 JS |
310 | subsep = ", "; |
311 | print_joined("remote branch ", "remote branches ", | |
0b9a969e | 312 | &src_data->r_branch, out); |
00449f99 JS |
313 | } |
314 | if (src_data->tag.nr) { | |
0b9a969e | 315 | strbuf_addstr(out, subsep); |
00449f99 | 316 | subsep = ", "; |
0b9a969e | 317 | print_joined("tag ", "tags ", &src_data->tag, out); |
00449f99 JS |
318 | } |
319 | if (src_data->generic.nr) { | |
0b9a969e MV |
320 | strbuf_addstr(out, subsep); |
321 | print_joined("commit ", "commits ", &src_data->generic, | |
322 | out); | |
00449f99 JS |
323 | } |
324 | if (strcmp(".", srcs.list[i])) | |
0b9a969e | 325 | strbuf_addf(out, " of %s", srcs.list[i]); |
00449f99 JS |
326 | } |
327 | ||
328 | if (!strcmp("master", current_branch)) | |
0b9a969e | 329 | strbuf_addch(out, '\n'); |
00449f99 | 330 | else |
0b9a969e | 331 | strbuf_addf(out, " into %s\n", current_branch); |
00449f99 JS |
332 | |
333 | if (merge_summary) { | |
334 | struct commit *head; | |
335 | struct rev_info rev; | |
336 | ||
337 | head = lookup_commit(head_sha1); | |
0b9a969e | 338 | init_revisions(&rev, NULL); |
00449f99 JS |
339 | rev.commit_format = CMIT_FMT_ONELINE; |
340 | rev.ignore_merges = 1; | |
341 | rev.limited = 1; | |
342 | ||
343 | for (i = 0; i < origins.nr; i++) | |
344 | shortlog(origins.list[i], origins.payload[i], | |
0b9a969e | 345 | head, &rev, limit, out); |
00449f99 | 346 | } |
0b9a969e MV |
347 | return 0; |
348 | } | |
349 | ||
350 | int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) | |
351 | { | |
c8ef0383 PH |
352 | const char *inpath = NULL; |
353 | struct option options[] = { | |
354 | OPT_BOOLEAN(0, "log", &merge_summary, "populate log with the shortlog"), | |
355 | OPT_BOOLEAN(0, "summary", &merge_summary, "alias for --log"), | |
356 | OPT_STRING('F', "file", &inpath, "file", "file to read from"), | |
357 | OPT_END() | |
358 | }; | |
359 | ||
0b9a969e | 360 | FILE *in = stdin; |
f285a2d7 | 361 | struct strbuf input = STRBUF_INIT, output = STRBUF_INIT; |
0b9a969e MV |
362 | int ret; |
363 | ||
364 | git_config(fmt_merge_msg_config, NULL); | |
c8ef0383 PH |
365 | argc = parse_options(argc, argv, options, fmt_merge_msg_usage, 0); |
366 | if (argc > 0) | |
367 | usage_with_options(fmt_merge_msg_usage, options); | |
368 | ||
369 | if (inpath && strcmp(inpath, "-")) { | |
370 | in = fopen(inpath, "r"); | |
371 | if (!in) | |
372 | die("cannot open %s", inpath); | |
0b9a969e MV |
373 | } |
374 | ||
0b9a969e MV |
375 | if (strbuf_read(&input, fileno(in), 0) < 0) |
376 | die("could not read input file %s", strerror(errno)); | |
00449f99 | 377 | |
0b9a969e MV |
378 | ret = fmt_merge_msg(merge_summary, &input, &output); |
379 | if (ret) | |
380 | return ret; | |
c8ef0383 | 381 | write_in_full(STDOUT_FILENO, output.buf, output.len); |
00449f99 JS |
382 | return 0; |
383 | } |