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