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