]>
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" |
898eacd8 JH |
8 | #include "branch.h" |
9 | #include "fmt-merge-msg.h" | |
895680f0 | 10 | #include "gpg-interface.h" |
00449f99 | 11 | |
c8ef0383 | 12 | static const char * const fmt_merge_msg_usage[] = { |
93eced6c | 13 | N_("git fmt-merge-msg [-m <message>] [--log[=<n>]|--no-log] [--file <file>]"), |
c8ef0383 PH |
14 | NULL |
15 | }; | |
00449f99 | 16 | |
898eacd8 | 17 | static int use_branch_desc; |
00449f99 | 18 | |
898eacd8 | 19 | int fmt_merge_msg_config(const char *key, const char *value, void *cb) |
00449f99 | 20 | { |
bda3b8ff RR |
21 | if (!strcmp(key, "merge.log") || !strcmp(key, "merge.summary")) { |
22 | int is_bool; | |
898eacd8 JH |
23 | merge_log_config = git_config_bool_or_int(key, value, &is_bool); |
24 | if (!is_bool && merge_log_config < 0) | |
bda3b8ff | 25 | return error("%s: negative length %s", key, value); |
898eacd8 JH |
26 | if (is_bool && merge_log_config) |
27 | merge_log_config = DEFAULT_MERGE_LOG_LEN; | |
28 | } else if (!strcmp(key, "merge.branchdesc")) { | |
29 | use_branch_desc = git_config_bool(key, value); | |
418a1435 JH |
30 | } else { |
31 | return git_default_config(key, value, cb); | |
6cd9cfef | 32 | } |
00449f99 JS |
33 | return 0; |
34 | } | |
35 | ||
4c0ea82d | 36 | /* merge data per repository where the merged tips came from */ |
fcb243f7 SB |
37 | struct src_data { |
38 | struct string_list branch, tag, r_branch, generic; | |
39 | int head_status; | |
00449f99 JS |
40 | }; |
41 | ||
898eacd8 JH |
42 | struct origin_data { |
43 | unsigned char sha1[20]; | |
90a321c0 | 44 | unsigned is_local_branch:1; |
898eacd8 JH |
45 | }; |
46 | ||
c2e86add | 47 | static void init_src_data(struct src_data *data) |
00449f99 | 48 | { |
fcb243f7 SB |
49 | data->branch.strdup_strings = 1; |
50 | data->tag.strdup_strings = 1; | |
51 | data->r_branch.strdup_strings = 1; | |
52 | data->generic.strdup_strings = 1; | |
00449f99 JS |
53 | } |
54 | ||
183113a5 TF |
55 | static struct string_list srcs = STRING_LIST_INIT_DUP; |
56 | static struct string_list origins = STRING_LIST_INIT_DUP; | |
00449f99 | 57 | |
5802f81b JH |
58 | struct merge_parents { |
59 | int alloc, nr; | |
60 | struct merge_parent { | |
61 | unsigned char given[20]; | |
62 | unsigned char commit[20]; | |
63 | unsigned char used; | |
64 | } *item; | |
65 | }; | |
66 | ||
67 | /* | |
68 | * I know, I know, this is inefficient, but you won't be pulling and merging | |
69 | * hundreds of heads at a time anyway. | |
70 | */ | |
71 | static struct merge_parent *find_merge_parent(struct merge_parents *table, | |
72 | unsigned char *given, | |
73 | unsigned char *commit) | |
74 | { | |
75 | int i; | |
76 | for (i = 0; i < table->nr; i++) { | |
77 | if (given && hashcmp(table->item[i].given, given)) | |
78 | continue; | |
79 | if (commit && hashcmp(table->item[i].commit, commit)) | |
80 | continue; | |
81 | return &table->item[i]; | |
82 | } | |
83 | return NULL; | |
84 | } | |
85 | ||
86 | static void add_merge_parent(struct merge_parents *table, | |
87 | unsigned char *given, | |
88 | unsigned char *commit) | |
89 | { | |
90 | if (table->nr && find_merge_parent(table, given, commit)) | |
91 | return; | |
92 | ALLOC_GROW(table->item, table->nr + 1, table->alloc); | |
93 | hashcpy(table->item[table->nr].given, given); | |
94 | hashcpy(table->item[table->nr].commit, commit); | |
95 | table->item[table->nr].used = 0; | |
96 | table->nr++; | |
97 | } | |
98 | ||
99 | static int handle_line(char *line, struct merge_parents *merge_parents) | |
00449f99 JS |
100 | { |
101 | int i, len = strlen(line); | |
898eacd8 | 102 | struct origin_data *origin_data; |
95b567c7 JK |
103 | char *src; |
104 | const char *origin; | |
00449f99 | 105 | struct src_data *src_data; |
fcb243f7 | 106 | struct string_list_item *item; |
e918c6ab | 107 | int pulling_head = 0; |
5802f81b | 108 | unsigned char sha1[20]; |
00449f99 JS |
109 | |
110 | if (len < 43 || line[40] != '\t') | |
111 | return 1; | |
112 | ||
59556548 | 113 | if (starts_with(line + 41, "not-for-merge")) |
00449f99 JS |
114 | return 0; |
115 | ||
116 | if (line[41] != '\t') | |
117 | return 2; | |
118 | ||
5802f81b JH |
119 | i = get_sha1_hex(line, sha1); |
120 | if (i) | |
00449f99 | 121 | return 3; |
5802f81b JH |
122 | |
123 | if (!find_merge_parent(merge_parents, sha1, NULL)) | |
124 | return 0; /* subsumed by other parents */ | |
125 | ||
126 | origin_data = xcalloc(1, sizeof(struct origin_data)); | |
127 | hashcpy(origin_data->sha1, sha1); | |
00449f99 JS |
128 | |
129 | if (line[len - 1] == '\n') | |
130 | line[len - 1] = 0; | |
131 | line += 42; | |
132 | ||
4c0ea82d JH |
133 | /* |
134 | * At this point, line points at the beginning of comment e.g. | |
135 | * "branch 'frotz' of git://that/repository.git". | |
136 | * Find the repository name and point it with src. | |
137 | */ | |
00449f99 JS |
138 | src = strstr(line, " of "); |
139 | if (src) { | |
140 | *src = 0; | |
141 | src += 4; | |
e918c6ab JH |
142 | pulling_head = 0; |
143 | } else { | |
144 | src = line; | |
145 | pulling_head = 1; | |
146 | } | |
00449f99 | 147 | |
fcb243f7 SB |
148 | item = unsorted_string_list_lookup(&srcs, src); |
149 | if (!item) { | |
1d2f80fa | 150 | item = string_list_append(&srcs, src); |
fcb243f7 SB |
151 | item->util = xcalloc(1, sizeof(struct src_data)); |
152 | init_src_data(item->util); | |
00449f99 | 153 | } |
fcb243f7 | 154 | src_data = item->util; |
00449f99 | 155 | |
e918c6ab | 156 | if (pulling_head) { |
fcb243f7 | 157 | origin = src; |
e918c6ab | 158 | src_data->head_status |= 1; |
59556548 | 159 | } else if (starts_with(line, "branch ")) { |
898eacd8 | 160 | origin_data->is_local_branch = 1; |
fcb243f7 | 161 | origin = line + 7; |
1d2f80fa | 162 | string_list_append(&src_data->branch, origin); |
00449f99 | 163 | src_data->head_status |= 2; |
59556548 | 164 | } else if (starts_with(line, "tag ")) { |
00449f99 | 165 | origin = line; |
1d2f80fa | 166 | string_list_append(&src_data->tag, origin + 4); |
00449f99 | 167 | src_data->head_status |= 2; |
95b567c7 | 168 | } else if (skip_prefix(line, "remote-tracking branch ", &origin)) { |
1d2f80fa | 169 | string_list_append(&src_data->r_branch, origin); |
00449f99 | 170 | src_data->head_status |= 2; |
00449f99 | 171 | } else { |
fcb243f7 | 172 | origin = src; |
1d2f80fa | 173 | string_list_append(&src_data->generic, line); |
00449f99 JS |
174 | src_data->head_status |= 2; |
175 | } | |
176 | ||
177 | if (!strcmp(".", src) || !strcmp(src, origin)) { | |
178 | int len = strlen(origin); | |
fcb243f7 | 179 | if (origin[0] == '\'' && origin[len - 1] == '\'') |
182af834 | 180 | origin = xmemdupz(origin + 1, len - 2); |
28310186 JK |
181 | } else |
182 | origin = xstrfmt("%s of %s", origin, src); | |
898eacd8 JH |
183 | if (strcmp(".", src)) |
184 | origin_data->is_local_branch = 0; | |
185 | string_list_append(&origins, origin)->util = origin_data; | |
00449f99 JS |
186 | return 0; |
187 | } | |
188 | ||
189 | static void print_joined(const char *singular, const char *plural, | |
fcb243f7 | 190 | struct string_list *list, struct strbuf *out) |
00449f99 JS |
191 | { |
192 | if (list->nr == 0) | |
193 | return; | |
194 | if (list->nr == 1) { | |
fcb243f7 | 195 | strbuf_addf(out, "%s%s", singular, list->items[0].string); |
00449f99 JS |
196 | } else { |
197 | int i; | |
0b9a969e | 198 | strbuf_addstr(out, plural); |
00449f99 | 199 | for (i = 0; i < list->nr - 1; i++) |
fcb243f7 SB |
200 | strbuf_addf(out, "%s%s", i > 0 ? ", " : "", |
201 | list->items[i].string); | |
202 | strbuf_addf(out, " and %s", list->items[list->nr - 1].string); | |
00449f99 JS |
203 | } |
204 | } | |
205 | ||
898eacd8 JH |
206 | static void add_branch_desc(struct strbuf *out, const char *name) |
207 | { | |
208 | struct strbuf desc = STRBUF_INIT; | |
209 | ||
210 | if (!read_branch_desc(&desc, name)) { | |
211 | const char *bp = desc.buf; | |
212 | while (*bp) { | |
213 | const char *ep = strchrnul(bp, '\n'); | |
214 | if (*ep) | |
215 | ep++; | |
216 | strbuf_addf(out, " : %.*s", (int)(ep - bp), bp); | |
217 | bp = ep; | |
218 | } | |
a0d4923d | 219 | strbuf_complete_line(out); |
898eacd8 JH |
220 | } |
221 | strbuf_release(&desc); | |
222 | } | |
223 | ||
418a1435 JH |
224 | #define util_as_integral(elem) ((intptr_t)((elem)->util)) |
225 | ||
226 | static void record_person(int which, struct string_list *people, | |
227 | struct commit *commit) | |
228 | { | |
bc6b8fc1 | 229 | const char *buffer; |
e21ab134 | 230 | char *name_buf, *name, *name_end; |
418a1435 | 231 | struct string_list_item *elem; |
9bcbb1c2 | 232 | const char *field; |
418a1435 | 233 | |
9bcbb1c2 | 234 | field = (which == 'a') ? "\nauthor " : "\ncommitter "; |
8597ea3a | 235 | buffer = get_commit_buffer(commit, NULL); |
bc6b8fc1 | 236 | name = strstr(buffer, field); |
418a1435 JH |
237 | if (!name) |
238 | return; | |
239 | name += strlen(field); | |
240 | name_end = strchrnul(name, '<'); | |
241 | if (*name_end) | |
242 | name_end--; | |
243 | while (isspace(*name_end) && name <= name_end) | |
244 | name_end--; | |
e21ab134 | 245 | if (name_end < name) |
418a1435 | 246 | return; |
e21ab134 | 247 | name_buf = xmemdupz(name, name_end - name + 1); |
bc6b8fc1 | 248 | unuse_commit_buffer(commit, buffer); |
418a1435 JH |
249 | |
250 | elem = string_list_lookup(people, name_buf); | |
251 | if (!elem) { | |
252 | elem = string_list_insert(people, name_buf); | |
253 | elem->util = (void *)0; | |
254 | } | |
255 | elem->util = (void*)(util_as_integral(elem) + 1); | |
e21ab134 | 256 | free(name_buf); |
418a1435 JH |
257 | } |
258 | ||
259 | static int cmp_string_list_util_as_integral(const void *a_, const void *b_) | |
260 | { | |
261 | const struct string_list_item *a = a_, *b = b_; | |
262 | return util_as_integral(b) - util_as_integral(a); | |
263 | } | |
264 | ||
265 | static void add_people_count(struct strbuf *out, struct string_list *people) | |
266 | { | |
267 | if (people->nr == 1) | |
268 | strbuf_addf(out, "%s", people->items[0].string); | |
269 | else if (people->nr == 2) | |
270 | strbuf_addf(out, "%s (%d) and %s (%d)", | |
271 | people->items[0].string, | |
272 | (int)util_as_integral(&people->items[0]), | |
273 | people->items[1].string, | |
274 | (int)util_as_integral(&people->items[1])); | |
275 | else if (people->nr) | |
276 | strbuf_addf(out, "%s (%d) and others", | |
277 | people->items[0].string, | |
278 | (int)util_as_integral(&people->items[0])); | |
279 | } | |
280 | ||
281 | static void credit_people(struct strbuf *out, | |
282 | struct string_list *them, | |
283 | int kind) | |
284 | { | |
285 | const char *label; | |
286 | const char *me; | |
287 | ||
288 | if (kind == 'a') { | |
9927ebed | 289 | label = "By"; |
418a1435 JH |
290 | me = git_author_info(IDENT_NO_DATE); |
291 | } else { | |
9927ebed | 292 | label = "Via"; |
418a1435 JH |
293 | me = git_committer_info(IDENT_NO_DATE); |
294 | } | |
295 | ||
296 | if (!them->nr || | |
297 | (them->nr == 1 && | |
298 | me && | |
cf4fff57 | 299 | skip_prefix(me, them->items->string, &me) && |
c0264180 | 300 | starts_with(me, " <"))) |
418a1435 | 301 | return; |
9927ebed | 302 | strbuf_addf(out, "\n%c %s ", comment_line_char, label); |
418a1435 JH |
303 | add_people_count(out, them); |
304 | } | |
305 | ||
306 | static void add_people_info(struct strbuf *out, | |
307 | struct string_list *authors, | |
308 | struct string_list *committers) | |
309 | { | |
310 | if (authors->nr) | |
311 | qsort(authors->items, | |
312 | authors->nr, sizeof(authors->items[0]), | |
313 | cmp_string_list_util_as_integral); | |
314 | if (committers->nr) | |
315 | qsort(committers->items, | |
316 | committers->nr, sizeof(committers->items[0]), | |
317 | cmp_string_list_util_as_integral); | |
318 | ||
319 | credit_people(out, authors, 'a'); | |
320 | credit_people(out, committers, 'c'); | |
321 | } | |
322 | ||
898eacd8 JH |
323 | static void shortlog(const char *name, |
324 | struct origin_data *origin_data, | |
325 | struct commit *head, | |
9bcbb1c2 JH |
326 | struct rev_info *rev, |
327 | struct fmt_merge_msg_opts *opts, | |
898eacd8 | 328 | struct strbuf *out) |
00449f99 JS |
329 | { |
330 | int i, count = 0; | |
331 | struct commit *commit; | |
332 | struct object *branch; | |
183113a5 | 333 | struct string_list subjects = STRING_LIST_INIT_DUP; |
418a1435 JH |
334 | struct string_list authors = STRING_LIST_INIT_DUP; |
335 | struct string_list committers = STRING_LIST_INIT_DUP; | |
7dc0fe3b | 336 | int flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED; |
15cb5007 | 337 | struct strbuf sb = STRBUF_INIT; |
898eacd8 | 338 | const unsigned char *sha1 = origin_data->sha1; |
9bcbb1c2 | 339 | int limit = opts->shortlog_len; |
00449f99 JS |
340 | |
341 | branch = deref_tag(parse_object(sha1), sha1_to_hex(sha1), 40); | |
1974632c | 342 | if (!branch || branch->type != OBJ_COMMIT) |
00449f99 JS |
343 | return; |
344 | ||
345 | setup_revisions(0, NULL, rev, NULL); | |
00449f99 JS |
346 | add_pending_object(rev, branch, name); |
347 | add_pending_object(rev, &head->object, "^HEAD"); | |
348 | head->object.flags |= UNINTERESTING; | |
3d51e1b5 MK |
349 | if (prepare_revision_walk(rev)) |
350 | die("revision walk setup failed"); | |
00449f99 | 351 | while ((commit = get_revision(rev)) != NULL) { |
15cb5007 | 352 | struct pretty_print_context ctx = {0}; |
00449f99 | 353 | |
418a1435 JH |
354 | if (commit->parents && commit->parents->next) { |
355 | /* do not list a merge but count committer */ | |
9bcbb1c2 JH |
356 | if (opts->credit_people) |
357 | record_person('c', &committers, commit); | |
00449f99 | 358 | continue; |
418a1435 | 359 | } |
9bcbb1c2 | 360 | if (!count && opts->credit_people) |
418a1435 JH |
361 | /* the 'tip' committer */ |
362 | record_person('c', &committers, commit); | |
9bcbb1c2 JH |
363 | if (opts->credit_people) |
364 | record_person('a', &authors, commit); | |
00449f99 JS |
365 | count++; |
366 | if (subjects.nr > limit) | |
367 | continue; | |
368 | ||
15cb5007 SB |
369 | format_commit_message(commit, "%s", &sb, &ctx); |
370 | strbuf_ltrim(&sb); | |
6a28518a | 371 | |
15cb5007 | 372 | if (!sb.len) |
1d2f80fa JP |
373 | string_list_append(&subjects, |
374 | sha1_to_hex(commit->object.sha1)); | |
15cb5007 | 375 | else |
1d2f80fa | 376 | string_list_append(&subjects, strbuf_detach(&sb, NULL)); |
00449f99 JS |
377 | } |
378 | ||
9bcbb1c2 JH |
379 | if (opts->credit_people) |
380 | add_people_info(out, &authors, &committers); | |
00449f99 | 381 | if (count > limit) |
0b9a969e | 382 | strbuf_addf(out, "\n* %s: (%d commits)\n", name, count); |
00449f99 | 383 | else |
0b9a969e | 384 | strbuf_addf(out, "\n* %s:\n", name); |
00449f99 | 385 | |
898eacd8 JH |
386 | if (origin_data->is_local_branch && use_branch_desc) |
387 | add_branch_desc(out, name); | |
388 | ||
00449f99 JS |
389 | for (i = 0; i < subjects.nr; i++) |
390 | if (i >= limit) | |
0b9a969e | 391 | strbuf_addf(out, " ...\n"); |
00449f99 | 392 | else |
fcb243f7 | 393 | strbuf_addf(out, " %s\n", subjects.items[i].string); |
00449f99 JS |
394 | |
395 | clear_commit_marks((struct commit *)branch, flags); | |
396 | clear_commit_marks(head, flags); | |
397 | free_commit_list(rev->commits); | |
398 | rev->commits = NULL; | |
399 | rev->pending.nr = 0; | |
400 | ||
418a1435 JH |
401 | string_list_clear(&authors, 0); |
402 | string_list_clear(&committers, 0); | |
fcb243f7 | 403 | string_list_clear(&subjects, 0); |
00449f99 JS |
404 | } |
405 | ||
cbda121c | 406 | static void fmt_merge_msg_title(struct strbuf *out, |
403994e8 TRC |
407 | const char *current_branch) { |
408 | int i = 0; | |
fd13b21f | 409 | char *sep = ""; |
419fe5bc | 410 | |
0b9a969e | 411 | strbuf_addstr(out, "Merge "); |
00449f99 | 412 | for (i = 0; i < srcs.nr; i++) { |
fcb243f7 | 413 | struct src_data *src_data = srcs.items[i].util; |
00449f99 JS |
414 | const char *subsep = ""; |
415 | ||
0b9a969e | 416 | strbuf_addstr(out, sep); |
00449f99 JS |
417 | sep = "; "; |
418 | ||
419 | if (src_data->head_status == 1) { | |
fcb243f7 | 420 | strbuf_addstr(out, srcs.items[i].string); |
00449f99 JS |
421 | continue; |
422 | } | |
423 | if (src_data->head_status == 3) { | |
424 | subsep = ", "; | |
0b9a969e | 425 | strbuf_addstr(out, "HEAD"); |
00449f99 JS |
426 | } |
427 | if (src_data->branch.nr) { | |
0b9a969e | 428 | strbuf_addstr(out, subsep); |
00449f99 | 429 | subsep = ", "; |
0b9a969e MV |
430 | print_joined("branch ", "branches ", &src_data->branch, |
431 | out); | |
00449f99 JS |
432 | } |
433 | if (src_data->r_branch.nr) { | |
0b9a969e | 434 | strbuf_addstr(out, subsep); |
00449f99 | 435 | subsep = ", "; |
13931236 | 436 | print_joined("remote-tracking branch ", "remote-tracking branches ", |
0b9a969e | 437 | &src_data->r_branch, out); |
00449f99 JS |
438 | } |
439 | if (src_data->tag.nr) { | |
0b9a969e | 440 | strbuf_addstr(out, subsep); |
00449f99 | 441 | subsep = ", "; |
0b9a969e | 442 | print_joined("tag ", "tags ", &src_data->tag, out); |
00449f99 JS |
443 | } |
444 | if (src_data->generic.nr) { | |
0b9a969e MV |
445 | strbuf_addstr(out, subsep); |
446 | print_joined("commit ", "commits ", &src_data->generic, | |
447 | out); | |
00449f99 | 448 | } |
fcb243f7 SB |
449 | if (strcmp(".", srcs.items[i].string)) |
450 | strbuf_addf(out, " of %s", srcs.items[i].string); | |
00449f99 JS |
451 | } |
452 | ||
453 | if (!strcmp("master", current_branch)) | |
0b9a969e | 454 | strbuf_addch(out, '\n'); |
00449f99 | 455 | else |
0b9a969e | 456 | strbuf_addf(out, " into %s\n", current_branch); |
403994e8 TRC |
457 | } |
458 | ||
895680f0 JH |
459 | static void fmt_tag_signature(struct strbuf *tagbuf, |
460 | struct strbuf *sig, | |
461 | const char *buf, | |
462 | unsigned long len) | |
463 | { | |
464 | const char *tag_body = strstr(buf, "\n\n"); | |
465 | if (tag_body) { | |
466 | tag_body += 2; | |
467 | strbuf_add(tagbuf, tag_body, buf + len - tag_body); | |
468 | } | |
469 | strbuf_complete_line(tagbuf); | |
a3347b98 LT |
470 | if (sig->len) { |
471 | strbuf_addch(tagbuf, '\n'); | |
eff80a9f | 472 | strbuf_add_commented_lines(tagbuf, sig->buf, sig->len); |
a3347b98 | 473 | } |
895680f0 JH |
474 | } |
475 | ||
476 | static void fmt_merge_msg_sigs(struct strbuf *out) | |
477 | { | |
478 | int i, tag_number = 0, first_tag = 0; | |
479 | struct strbuf tagbuf = STRBUF_INIT; | |
480 | ||
481 | for (i = 0; i < origins.nr; i++) { | |
482 | unsigned char *sha1 = origins.items[i].util; | |
483 | enum object_type type; | |
484 | unsigned long size, len; | |
485 | char *buf = read_sha1_file(sha1, &type, &size); | |
486 | struct strbuf sig = STRBUF_INIT; | |
487 | ||
488 | if (!buf || type != OBJ_TAG) | |
489 | goto next; | |
490 | len = parse_signature(buf, size); | |
491 | ||
492 | if (size == len) | |
493 | ; /* merely annotated */ | |
9cc4ac8f | 494 | else if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) { |
895680f0 JH |
495 | if (!sig.len) |
496 | strbuf_addstr(&sig, "gpg verification failed.\n"); | |
497 | } | |
498 | ||
499 | if (!tag_number++) { | |
500 | fmt_tag_signature(&tagbuf, &sig, buf, len); | |
501 | first_tag = i; | |
502 | } else { | |
503 | if (tag_number == 2) { | |
504 | struct strbuf tagline = STRBUF_INIT; | |
89c3bbd8 RT |
505 | strbuf_addch(&tagline, '\n'); |
506 | strbuf_add_commented_lines(&tagline, | |
507 | origins.items[first_tag].string, | |
508 | strlen(origins.items[first_tag].string)); | |
895680f0 JH |
509 | strbuf_insert(&tagbuf, 0, tagline.buf, |
510 | tagline.len); | |
511 | strbuf_release(&tagline); | |
512 | } | |
89c3bbd8 RT |
513 | strbuf_addch(&tagbuf, '\n'); |
514 | strbuf_add_commented_lines(&tagbuf, | |
515 | origins.items[i].string, | |
516 | strlen(origins.items[i].string)); | |
895680f0 JH |
517 | fmt_tag_signature(&tagbuf, &sig, buf, len); |
518 | } | |
519 | strbuf_release(&sig); | |
520 | next: | |
521 | free(buf); | |
522 | } | |
523 | if (tagbuf.len) { | |
524 | strbuf_addch(out, '\n'); | |
525 | strbuf_addbuf(out, &tagbuf); | |
526 | } | |
527 | strbuf_release(&tagbuf); | |
528 | } | |
529 | ||
5802f81b JH |
530 | static void find_merge_parents(struct merge_parents *result, |
531 | struct strbuf *in, unsigned char *head) | |
532 | { | |
533 | struct commit_list *parents, *next; | |
534 | struct commit *head_commit; | |
535 | int pos = 0, i, j; | |
536 | ||
537 | parents = NULL; | |
538 | while (pos < in->len) { | |
539 | int len; | |
540 | char *p = in->buf + pos; | |
541 | char *newline = strchr(p, '\n'); | |
542 | unsigned char sha1[20]; | |
543 | struct commit *parent; | |
544 | struct object *obj; | |
545 | ||
546 | len = newline ? newline - p : strlen(p); | |
547 | pos += len + !!newline; | |
548 | ||
549 | if (len < 43 || | |
550 | get_sha1_hex(p, sha1) || | |
551 | p[40] != '\t' || | |
552 | p[41] != '\t') | |
553 | continue; /* skip not-for-merge */ | |
554 | /* | |
555 | * Do not use get_merge_parent() here; we do not have | |
556 | * "name" here and we do not want to contaminate its | |
557 | * util field yet. | |
558 | */ | |
559 | obj = parse_object(sha1); | |
560 | parent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT); | |
561 | if (!parent) | |
562 | continue; | |
563 | commit_list_insert(parent, &parents); | |
564 | add_merge_parent(result, obj->sha1, parent->object.sha1); | |
565 | } | |
566 | head_commit = lookup_commit(head); | |
567 | if (head_commit) | |
568 | commit_list_insert(head_commit, &parents); | |
569 | parents = reduce_heads(parents); | |
570 | ||
571 | while (parents) { | |
572 | for (i = 0; i < result->nr; i++) | |
573 | if (!hashcmp(result->item[i].commit, | |
574 | parents->item->object.sha1)) | |
575 | result->item[i].used = 1; | |
576 | next = parents->next; | |
577 | free(parents); | |
578 | parents = next; | |
579 | } | |
580 | ||
581 | for (i = j = 0; i < result->nr; i++) { | |
582 | if (result->item[i].used) { | |
583 | if (i != j) | |
584 | result->item[j] = result->item[i]; | |
585 | j++; | |
586 | } | |
587 | } | |
588 | result->nr = j; | |
589 | } | |
590 | ||
cbda121c JH |
591 | int fmt_merge_msg(struct strbuf *in, struct strbuf *out, |
592 | struct fmt_merge_msg_opts *opts) | |
593 | { | |
1876166a | 594 | int i = 0, pos = 0; |
403994e8 TRC |
595 | unsigned char head_sha1[20]; |
596 | const char *current_branch; | |
96ec7b1e | 597 | void *current_branch_to_free; |
5802f81b JH |
598 | struct merge_parents merge_parents; |
599 | ||
600 | memset(&merge_parents, 0, sizeof(merge_parents)); | |
403994e8 TRC |
601 | |
602 | /* get current branch */ | |
96ec7b1e | 603 | current_branch = current_branch_to_free = |
7695d118 | 604 | resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL); |
403994e8 TRC |
605 | if (!current_branch) |
606 | die("No current branch"); | |
59556548 | 607 | if (starts_with(current_branch, "refs/heads/")) |
403994e8 TRC |
608 | current_branch += 11; |
609 | ||
5802f81b JH |
610 | find_merge_parents(&merge_parents, in, head_sha1); |
611 | ||
403994e8 TRC |
612 | /* get a line */ |
613 | while (pos < in->len) { | |
614 | int len; | |
615 | char *newline, *p = in->buf + pos; | |
616 | ||
617 | newline = strchr(p, '\n'); | |
618 | len = newline ? newline - p : strlen(p); | |
619 | pos += len + !!newline; | |
620 | i++; | |
621 | p[len] = 0; | |
5802f81b | 622 | if (handle_line(p, &merge_parents)) |
403994e8 TRC |
623 | die ("Error in line %d: %.*s", i, len, p); |
624 | } | |
625 | ||
cbda121c JH |
626 | if (opts->add_title && srcs.nr) |
627 | fmt_merge_msg_title(out, current_branch); | |
403994e8 | 628 | |
895680f0 JH |
629 | if (origins.nr) |
630 | fmt_merge_msg_sigs(out); | |
00449f99 | 631 | |
cbda121c | 632 | if (opts->shortlog_len) { |
00449f99 JS |
633 | struct commit *head; |
634 | struct rev_info rev; | |
635 | ||
baf18fc2 | 636 | head = lookup_commit_or_die(head_sha1, "HEAD"); |
0b9a969e | 637 | init_revisions(&rev, NULL); |
00449f99 JS |
638 | rev.commit_format = CMIT_FMT_ONELINE; |
639 | rev.ignore_merges = 1; | |
640 | rev.limited = 1; | |
641 | ||
a3347b98 | 642 | strbuf_complete_line(out); |
f0ecac2b | 643 | |
00449f99 | 644 | for (i = 0; i < origins.nr; i++) |
898eacd8 JH |
645 | shortlog(origins.items[i].string, |
646 | origins.items[i].util, | |
9bcbb1c2 | 647 | head, &rev, opts, out); |
00449f99 | 648 | } |
0b9a969e | 649 | |
895680f0 | 650 | strbuf_complete_line(out); |
96ec7b1e | 651 | free(current_branch_to_free); |
5802f81b | 652 | free(merge_parents.item); |
0b9a969e | 653 | return 0; |
2234ec54 TRC |
654 | } |
655 | ||
0b9a969e MV |
656 | int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix) |
657 | { | |
c8ef0383 | 658 | const char *inpath = NULL; |
2102440c | 659 | const char *message = NULL; |
898eacd8 | 660 | int shortlog_len = -1; |
c8ef0383 | 661 | struct option options[] = { |
93eced6c NTND |
662 | { OPTION_INTEGER, 0, "log", &shortlog_len, N_("n"), |
663 | N_("populate log with at most <n> entries from shortlog"), | |
96e9420c | 664 | PARSE_OPT_OPTARG, NULL, DEFAULT_MERGE_LOG_LEN }, |
93eced6c NTND |
665 | { OPTION_INTEGER, 0, "summary", &shortlog_len, N_("n"), |
666 | N_("alias for --log (deprecated)"), | |
96e9420c RR |
667 | PARSE_OPT_OPTARG | PARSE_OPT_HIDDEN, NULL, |
668 | DEFAULT_MERGE_LOG_LEN }, | |
93eced6c NTND |
669 | OPT_STRING('m', "message", &message, N_("text"), |
670 | N_("use <text> as start of message")), | |
671 | OPT_FILENAME('F', "file", &inpath, N_("file to read from")), | |
c8ef0383 PH |
672 | OPT_END() |
673 | }; | |
674 | ||
0b9a969e | 675 | FILE *in = stdin; |
f285a2d7 | 676 | struct strbuf input = STRBUF_INIT, output = STRBUF_INIT; |
0b9a969e | 677 | int ret; |
cbda121c | 678 | struct fmt_merge_msg_opts opts; |
0b9a969e MV |
679 | |
680 | git_config(fmt_merge_msg_config, NULL); | |
37782920 SB |
681 | argc = parse_options(argc, argv, prefix, options, fmt_merge_msg_usage, |
682 | 0); | |
c8ef0383 PH |
683 | if (argc > 0) |
684 | usage_with_options(fmt_merge_msg_usage, options); | |
898eacd8 JH |
685 | if (shortlog_len < 0) |
686 | shortlog_len = (merge_log_config > 0) ? merge_log_config : 0; | |
c8ef0383 PH |
687 | |
688 | if (inpath && strcmp(inpath, "-")) { | |
689 | in = fopen(inpath, "r"); | |
690 | if (!in) | |
0721c314 | 691 | die_errno("cannot open '%s'", inpath); |
0b9a969e MV |
692 | } |
693 | ||
0b9a969e | 694 | if (strbuf_read(&input, fileno(in), 0) < 0) |
d824cbba | 695 | die_errno("could not read input file"); |
1876166a RR |
696 | |
697 | if (message) | |
2102440c | 698 | strbuf_addstr(&output, message); |
1876166a | 699 | |
cbda121c JH |
700 | memset(&opts, 0, sizeof(opts)); |
701 | opts.add_title = !message; | |
9bcbb1c2 | 702 | opts.credit_people = 1; |
cbda121c JH |
703 | opts.shortlog_len = shortlog_len; |
704 | ||
705 | ret = fmt_merge_msg(&input, &output, &opts); | |
0b9a969e MV |
706 | if (ret) |
707 | return ret; | |
c8ef0383 | 708 | write_in_full(STDOUT_FILENO, output.buf, output.len); |
00449f99 JS |
709 | return 0; |
710 | } |