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