]>
Commit | Line | Data |
---|---|---|
5f1c3f07 | 1 | #include "cache.h" |
db757e8b | 2 | #include "commit-reach.h" |
b2141fc1 | 3 | #include "config.h" |
5f1c3f07 | 4 | #include "diff.h" |
32a8f510 | 5 | #include "environment.h" |
41771fa4 | 6 | #include "hex.h" |
dabab1d6 | 7 | #include "object-name.h" |
cbd53a21 | 8 | #include "object-store.h" |
109cd76d | 9 | #include "repository.h" |
7b90ab46 | 10 | #include "tmp-objdir.h" |
5f1c3f07 | 11 | #include "commit.h" |
cab4feb6 | 12 | #include "tag.h" |
7fefda5c | 13 | #include "graph.h" |
5f1c3f07 | 14 | #include "log-tree.h" |
db757e8b | 15 | #include "merge-ort.h" |
8860fd42 | 16 | #include "reflog-walk.h" |
cab4feb6 | 17 | #include "refs.h" |
cbeab747 | 18 | #include "replace-object.h" |
b079c50e | 19 | #include "string-list.h" |
67a4b586 | 20 | #include "color.h" |
0c37f1fc | 21 | #include "gpg-interface.h" |
959a2623 | 22 | #include "sequencer.h" |
12da1d1f | 23 | #include "line-log.h" |
3ac68a93 | 24 | #include "help.h" |
40ce4160 | 25 | #include "range-diff.h" |
20323d10 | 26 | #include "strmap.h" |
d48be35c | 27 | #include "write-or-die.h" |
5f1c3f07 | 28 | |
2608c249 | 29 | static struct decoration name_decoration = { "object names" }; |
76c61fbd JH |
30 | static int decoration_loaded; |
31 | static int decoration_flags; | |
a7524128 | 32 | |
67a4b586 NR |
33 | static char decoration_colors[][COLOR_MAXLEN] = { |
34 | GIT_COLOR_RESET, | |
35 | GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */ | |
36 | GIT_COLOR_BOLD_RED, /* REF_REMOTE */ | |
37 | GIT_COLOR_BOLD_YELLOW, /* REF_TAG */ | |
38 | GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */ | |
39 | GIT_COLOR_BOLD_CYAN, /* REF_HEAD */ | |
76f5df30 | 40 | GIT_COLOR_BOLD_BLUE, /* GRAFTED */ |
67a4b586 NR |
41 | }; |
42 | ||
a73b3680 NTND |
43 | static const char *color_decorate_slots[] = { |
44 | [DECORATION_REF_LOCAL] = "branch", | |
45 | [DECORATION_REF_REMOTE] = "remoteBranch", | |
46 | [DECORATION_REF_TAG] = "tag", | |
47 | [DECORATION_REF_STASH] = "stash", | |
48 | [DECORATION_REF_HEAD] = "HEAD", | |
09c4ba41 | 49 | [DECORATION_GRAFTED] = "grafted", |
a73b3680 NTND |
50 | }; |
51 | ||
67a4b586 NR |
52 | static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix) |
53 | { | |
daa0c3d9 | 54 | if (want_color(decorate_use_color)) |
67a4b586 NR |
55 | return decoration_colors[ix]; |
56 | return ""; | |
57 | } | |
58 | ||
3ac68a93 | 59 | define_list_config_array(color_decorate_slots); |
5e11bee6 | 60 | |
8852117a | 61 | int parse_decorate_color_config(const char *var, const char *slot_name, const char *value) |
5e11bee6 | 62 | { |
a73b3680 | 63 | int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name); |
5e11bee6 NR |
64 | if (slot < 0) |
65 | return 0; | |
66 | if (!value) | |
67 | return config_error_nonbool(var); | |
f6c5a296 | 68 | return color_parse(value, decoration_colors[slot]); |
5e11bee6 NR |
69 | } |
70 | ||
67a4b586 NR |
71 | /* |
72 | * log-tree.c uses DIFF_OPT_TST for determining whether to use color | |
73 | * for showing the commit sha1, use the same check for --decorate | |
74 | */ | |
75 | #define decorate_get_color_opt(o, ix) \ | |
f1c96261 | 76 | decorate_get_color((o)->use_color, ix) |
67a4b586 | 77 | |
662174d2 | 78 | void add_name_decoration(enum decoration_type type, const char *name, struct object *obj) |
cab4feb6 | 79 | { |
96ffc06f JK |
80 | struct name_decoration *res; |
81 | FLEX_ALLOC_STR(res, name, name); | |
a7524128 | 82 | res->type = type; |
cab4feb6 RS |
83 | res->next = add_decoration(&name_decoration, obj, res); |
84 | } | |
85 | ||
2608c249 JK |
86 | const struct name_decoration *get_name_decoration(const struct object *obj) |
87 | { | |
0cc7380d | 88 | load_ref_decorations(NULL, DECORATE_SHORT_REFS); |
2608c249 JK |
89 | return lookup_decoration(&name_decoration, obj); |
90 | } | |
91 | ||
c9f7a793 DS |
92 | static int match_ref_pattern(const char *refname, |
93 | const struct string_list_item *item) | |
94 | { | |
95 | int matched = 0; | |
afe8a907 | 96 | if (!item->util) { |
c9f7a793 DS |
97 | if (!wildmatch(item->string, refname, 0)) |
98 | matched = 1; | |
99 | } else { | |
100 | const char *rest; | |
101 | if (skip_prefix(refname, item->string, &rest) && | |
102 | (!*rest || *rest == '/')) | |
103 | matched = 1; | |
104 | } | |
105 | return matched; | |
106 | } | |
107 | ||
108 | static int ref_filter_match(const char *refname, | |
109 | const struct decoration_filter *filter) | |
110 | { | |
111 | struct string_list_item *item; | |
112 | const struct string_list *exclude_patterns = filter->exclude_ref_pattern; | |
113 | const struct string_list *include_patterns = filter->include_ref_pattern; | |
a6be5e67 DS |
114 | const struct string_list *exclude_patterns_config = |
115 | filter->exclude_ref_config_pattern; | |
c9f7a793 DS |
116 | |
117 | if (exclude_patterns && exclude_patterns->nr) { | |
118 | for_each_string_list_item(item, exclude_patterns) { | |
119 | if (match_ref_pattern(refname, item)) | |
120 | return 0; | |
121 | } | |
122 | } | |
123 | ||
124 | if (include_patterns && include_patterns->nr) { | |
c9f7a793 | 125 | for_each_string_list_item(item, include_patterns) { |
a6be5e67 DS |
126 | if (match_ref_pattern(refname, item)) |
127 | return 1; | |
c9f7a793 | 128 | } |
a6be5e67 DS |
129 | return 0; |
130 | } | |
c9f7a793 | 131 | |
a6be5e67 DS |
132 | if (exclude_patterns_config && exclude_patterns_config->nr) { |
133 | for_each_string_list_item(item, exclude_patterns_config) { | |
134 | if (match_ref_pattern(refname, item)) | |
135 | return 0; | |
136 | } | |
c9f7a793 | 137 | } |
a6be5e67 | 138 | |
c9f7a793 DS |
139 | return 1; |
140 | } | |
141 | ||
f124b730 | 142 | static int add_ref_decoration(const char *refname, const struct object_id *oid, |
5cf88fd8 | 143 | int flags UNUSED, |
63e14ee2 | 144 | void *cb_data) |
cab4feb6 | 145 | { |
94d421b8 | 146 | int i; |
5267d292 | 147 | struct object *obj; |
88473c8b | 148 | enum object_type objtype; |
6afb265b | 149 | enum decoration_type deco_type = DECORATION_NONE; |
65516f58 | 150 | struct decoration_filter *filter = (struct decoration_filter *)cb_data; |
97e61e0f | 151 | const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; |
5267d292 | 152 | |
c9f7a793 | 153 | if (filter && !ref_filter_match(refname, filter)) |
65516f58 | 154 | return 0; |
429ad204 | 155 | |
58d121b2 | 156 | if (starts_with(refname, git_replace_ref_base)) { |
3d79f657 | 157 | struct object_id original_oid; |
6ebd1caf | 158 | if (!read_replace_refs) |
b9ad5002 | 159 | return 0; |
31a0ad54 JH |
160 | if (get_oid_hex(refname + strlen(git_replace_ref_base), |
161 | &original_oid)) { | |
5267d292 NTND |
162 | warning("invalid replace ref %s", refname); |
163 | return 0; | |
164 | } | |
109cd76d | 165 | obj = parse_object(the_repository, &original_oid); |
5267d292 NTND |
166 | if (obj) |
167 | add_name_decoration(DECORATION_GRAFTED, "replaced", obj); | |
168 | return 0; | |
169 | } | |
170 | ||
88473c8b JK |
171 | objtype = oid_object_info(the_repository, oid, NULL); |
172 | if (objtype < 0) | |
cab4feb6 | 173 | return 0; |
88473c8b | 174 | obj = lookup_object_by_type(the_repository, oid, objtype); |
a7524128 | 175 | |
94d421b8 DS |
176 | for (i = 0; i < ARRAY_SIZE(ref_namespace); i++) { |
177 | struct ref_namespace_info *info = &ref_namespace[i]; | |
178 | ||
179 | if (!info->decoration) | |
180 | continue; | |
181 | if (info->exact) { | |
182 | if (!strcmp(refname, info->ref)) { | |
183 | deco_type = info->decoration; | |
184 | break; | |
185 | } | |
186 | } else if (starts_with(refname, info->ref)) { | |
187 | deco_type = info->decoration; | |
188 | break; | |
189 | } | |
190 | } | |
a7524128 | 191 | |
6afb265b | 192 | add_name_decoration(deco_type, refname, obj); |
cab4feb6 | 193 | while (obj->type == OBJ_TAG) { |
d1ed8d6c JK |
194 | if (!obj->parsed) |
195 | parse_object(the_repository, &obj->oid); | |
cab4feb6 RS |
196 | obj = ((struct tag *)obj)->tagged; |
197 | if (!obj) | |
198 | break; | |
a7524128 | 199 | add_name_decoration(DECORATION_REF_TAG, refname, obj); |
cab4feb6 RS |
200 | } |
201 | return 0; | |
202 | } | |
203 | ||
43090008 JK |
204 | static int add_graft_decoration(const struct commit_graft *graft, |
205 | void *cb_data UNUSED) | |
76f5df30 | 206 | { |
c1f5eb49 | 207 | struct commit *commit = lookup_commit(the_repository, &graft->oid); |
76f5df30 NTND |
208 | if (!commit) |
209 | return 0; | |
210 | add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object); | |
211 | return 0; | |
212 | } | |
213 | ||
65516f58 | 214 | void load_ref_decorations(struct decoration_filter *filter, int flags) |
cab4feb6 | 215 | { |
76c61fbd | 216 | if (!decoration_loaded) { |
65516f58 RA |
217 | if (filter) { |
218 | struct string_list_item *item; | |
219 | for_each_string_list_item(item, filter->exclude_ref_pattern) { | |
220 | normalize_glob_ref(item, NULL, item->string); | |
221 | } | |
222 | for_each_string_list_item(item, filter->include_ref_pattern) { | |
223 | normalize_glob_ref(item, NULL, item->string); | |
224 | } | |
a6be5e67 DS |
225 | for_each_string_list_item(item, filter->exclude_ref_config_pattern) { |
226 | normalize_glob_ref(item, NULL, item->string); | |
227 | } | |
65516f58 | 228 | } |
76c61fbd JH |
229 | decoration_loaded = 1; |
230 | decoration_flags = flags; | |
65516f58 RA |
231 | for_each_ref(add_ref_decoration, filter); |
232 | head_ref(add_ref_decoration, filter); | |
233 | for_each_commit_graft(add_graft_decoration, filter); | |
cab4feb6 RS |
234 | } |
235 | } | |
236 | ||
4d7b0efc | 237 | static void show_parents(struct commit *commit, int abbrev, FILE *file) |
c8c893c6 LT |
238 | { |
239 | struct commit_list *p; | |
240 | for (p = commit->parents; p ; p = p->next) { | |
241 | struct commit *parent = p->item; | |
d850b7a5 ÆAB |
242 | fprintf(file, " %s", |
243 | repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev)); | |
c8c893c6 LT |
244 | } |
245 | } | |
246 | ||
91b849b2 JS |
247 | static void show_children(struct rev_info *opt, struct commit *commit, int abbrev) |
248 | { | |
249 | struct commit_list *p = lookup_decoration(&opt->children, &commit->object); | |
250 | for ( ; p; p = p->next) { | |
d850b7a5 ÆAB |
251 | fprintf(opt->diffopt.file, " %s", |
252 | repo_find_unique_abbrev(the_repository, &p->item->object.oid, abbrev)); | |
91b849b2 JS |
253 | } |
254 | } | |
255 | ||
51ff0f27 JH |
256 | /* |
257 | * Do we have HEAD in the output, and also the branch it points at? | |
258 | * If so, find that decoration entry for that current branch. | |
259 | */ | |
260 | static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration) | |
261 | { | |
262 | const struct name_decoration *list, *head = NULL; | |
263 | const char *branch_name = NULL; | |
51ff0f27 JH |
264 | int rru_flags; |
265 | ||
266 | /* First find HEAD */ | |
267 | for (list = decoration; list; list = list->next) | |
268 | if (list->type == DECORATION_REF_HEAD) { | |
269 | head = list; | |
270 | break; | |
271 | } | |
272 | if (!head) | |
273 | return NULL; | |
274 | ||
275 | /* Now resolve and find the matching current branch */ | |
744c040b | 276 | branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags); |
d79be498 | 277 | if (!branch_name || !(rru_flags & REF_ISSYMREF)) |
51ff0f27 | 278 | return NULL; |
76c61fbd | 279 | |
429ad204 | 280 | if (!starts_with(branch_name, "refs/")) |
51ff0f27 JH |
281 | return NULL; |
282 | ||
283 | /* OK, do we have that ref in the list? */ | |
284 | for (list = decoration; list; list = list->next) | |
285 | if ((list->type == DECORATION_REF_LOCAL) && | |
286 | !strcmp(branch_name, list->name)) { | |
287 | return list; | |
288 | } | |
289 | ||
290 | return NULL; | |
291 | } | |
292 | ||
429ad204 JH |
293 | static void show_name(struct strbuf *sb, const struct name_decoration *decoration) |
294 | { | |
295 | if (decoration_flags == DECORATE_SHORT_REFS) | |
296 | strbuf_addstr(sb, prettify_refname(decoration->name)); | |
297 | else | |
298 | strbuf_addstr(sb, decoration->name); | |
299 | } | |
300 | ||
9d3f002f | 301 | /* |
9271095c HJ |
302 | * The caller makes sure there is no funny color before calling. |
303 | * format_decorations_extended makes sure the same after return. | |
9d3f002f | 304 | */ |
9271095c | 305 | void format_decorations_extended(struct strbuf *sb, |
9d3f002f | 306 | const struct commit *commit, |
9271095c HJ |
307 | int use_color, |
308 | const char *prefix, | |
309 | const char *separator, | |
310 | const char *suffix) | |
ca135e7a | 311 | { |
2608c249 | 312 | const struct name_decoration *decoration; |
51ff0f27 | 313 | const struct name_decoration *current_and_HEAD; |
67a4b586 | 314 | const char *color_commit = |
9d3f002f | 315 | diff_get_color(use_color, DIFF_COMMIT); |
67a4b586 | 316 | const char *color_reset = |
9d3f002f | 317 | decorate_get_color(use_color, DECORATION_NONE); |
ca135e7a | 318 | |
2608c249 | 319 | decoration = get_name_decoration(&commit->object); |
ca135e7a LT |
320 | if (!decoration) |
321 | return; | |
51ff0f27 JH |
322 | |
323 | current_and_HEAD = current_pointed_by_HEAD(decoration); | |
ca135e7a | 324 | while (decoration) { |
51ff0f27 JH |
325 | /* |
326 | * When both current and HEAD are there, only | |
327 | * show HEAD->current where HEAD would have | |
328 | * appeared, skipping the entry for current. | |
329 | */ | |
330 | if (decoration != current_and_HEAD) { | |
331 | strbuf_addstr(sb, color_commit); | |
332 | strbuf_addstr(sb, prefix); | |
333 | strbuf_addstr(sb, color_reset); | |
334 | strbuf_addstr(sb, decorate_get_color(use_color, decoration->type)); | |
335 | if (decoration->type == DECORATION_REF_TAG) | |
336 | strbuf_addstr(sb, "tag: "); | |
337 | ||
429ad204 | 338 | show_name(sb, decoration); |
51ff0f27 JH |
339 | |
340 | if (current_and_HEAD && | |
341 | decoration->type == DECORATION_REF_HEAD) { | |
51ff0f27 JH |
342 | strbuf_addstr(sb, " -> "); |
343 | strbuf_addstr(sb, color_reset); | |
344 | strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type)); | |
429ad204 | 345 | show_name(sb, current_and_HEAD); |
51ff0f27 JH |
346 | } |
347 | strbuf_addstr(sb, color_reset); | |
348 | ||
349 | prefix = separator; | |
350 | } | |
ca135e7a LT |
351 | decoration = decoration->next; |
352 | } | |
9d3f002f | 353 | strbuf_addstr(sb, color_commit); |
9271095c | 354 | strbuf_addstr(sb, suffix); |
9d3f002f NTND |
355 | strbuf_addstr(sb, color_reset); |
356 | } | |
357 | ||
358 | void show_decorations(struct rev_info *opt, struct commit *commit) | |
359 | { | |
360 | struct strbuf sb = STRBUF_INIT; | |
361 | ||
87be2523 NTND |
362 | if (opt->sources) { |
363 | char **slot = revision_sources_peek(opt->sources, commit); | |
364 | ||
365 | if (slot && *slot) | |
366 | fprintf(opt->diffopt.file, "\t%s", *slot); | |
367 | } | |
9d3f002f NTND |
368 | if (!opt->show_decorations) |
369 | return; | |
370 | format_decorations(&sb, commit, opt->diffopt.use_color); | |
4d7b0efc | 371 | fputs(sb.buf, opt->diffopt.file); |
9d3f002f | 372 | strbuf_release(&sb); |
ca135e7a LT |
373 | } |
374 | ||
e00de24b JS |
375 | static unsigned int digits_in_number(unsigned int number) |
376 | { | |
377 | unsigned int i = 10, result = 1; | |
378 | while (i <= number) { | |
379 | i *= 10; | |
380 | result++; | |
381 | } | |
382 | return result; | |
383 | } | |
384 | ||
d28b5d47 JH |
385 | void fmt_output_subject(struct strbuf *filename, |
386 | const char *subject, | |
021f2f4c | 387 | struct rev_info *info) |
6fa8e627 | 388 | { |
021f2f4c JH |
389 | const char *suffix = info->patch_suffix; |
390 | int nr = info->nr; | |
d28b5d47 | 391 | int start_len = filename->len; |
3baf58bf | 392 | int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1); |
d28b5d47 | 393 | |
db91988a ZH |
394 | if (info->reroll_count) { |
395 | struct strbuf temp = STRBUF_INIT; | |
396 | ||
397 | strbuf_addf(&temp, "v%s", info->reroll_count); | |
398 | format_sanitized_subject(filename, temp.buf, temp.len); | |
399 | strbuf_addstr(filename, "-"); | |
400 | strbuf_release(&temp); | |
401 | } | |
d28b5d47 JH |
402 | strbuf_addf(filename, "%04d-%s", nr, subject); |
403 | ||
404 | if (max_len < filename->len) | |
405 | strbuf_setlen(filename, max_len); | |
406 | strbuf_addstr(filename, suffix); | |
407 | } | |
408 | ||
409 | void fmt_output_commit(struct strbuf *filename, | |
410 | struct commit *commit, | |
411 | struct rev_info *info) | |
412 | { | |
413 | struct pretty_print_context ctx = {0}; | |
414 | struct strbuf subject = STRBUF_INIT; | |
415 | ||
bab82164 ÆAB |
416 | repo_format_commit_message(the_repository, commit, "%f", &subject, |
417 | &ctx); | |
d28b5d47 JH |
418 | fmt_output_subject(filename, subject.buf, info); |
419 | strbuf_release(&subject); | |
6fa8e627 SB |
420 | } |
421 | ||
8ffc8dc6 RS |
422 | void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt) |
423 | { | |
424 | if (opt->total > 0) { | |
425 | strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ", | |
426 | opt->subject_prefix, | |
427 | *opt->subject_prefix ? " " : "", | |
428 | digits_in_number(opt->total), | |
429 | opt->nr, opt->total); | |
430 | } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) { | |
431 | strbuf_addf(sb, "Subject: [%s] ", | |
432 | opt->subject_prefix); | |
433 | } else { | |
434 | strbuf_addstr(sb, "Subject: "); | |
435 | } | |
436 | } | |
437 | ||
108dab28 | 438 | void log_write_email_headers(struct rev_info *opt, struct commit *commit, |
267123b4 | 439 | const char **extra_headers_p, |
50cd54ef | 440 | int *need_8bit_cte_p, |
441 | int maybe_multipart) | |
b02bd65f | 442 | { |
b02bd65f | 443 | const char *extra_headers = opt->extra_headers; |
3a30aa17 | 444 | const char *name = oid_to_hex(opt->zero_commit ? |
14228447 | 445 | null_oid() : &commit->object.oid); |
267123b4 JH |
446 | |
447 | *need_8bit_cte_p = 0; /* unknown */ | |
b02bd65f | 448 | |
4d7b0efc | 449 | fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name); |
7fefda5c AS |
450 | graph_show_oneline(opt->graph); |
451 | if (opt->message_id) { | |
4d7b0efc | 452 | fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id); |
7fefda5c AS |
453 | graph_show_oneline(opt->graph); |
454 | } | |
b079c50e TR |
455 | if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) { |
456 | int i, n; | |
457 | n = opt->ref_message_ids->nr; | |
4d7b0efc | 458 | fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string); |
b079c50e | 459 | for (i = 0; i < n; i++) |
4d7b0efc | 460 | fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "), |
b079c50e | 461 | opt->ref_message_ids->items[i].string); |
7fefda5c AS |
462 | graph_show_oneline(opt->graph); |
463 | } | |
50cd54ef | 464 | if (opt->mime_boundary && maybe_multipart) { |
d50b69b8 JK |
465 | static struct strbuf subject_buffer = STRBUF_INIT; |
466 | static struct strbuf buffer = STRBUF_INIT; | |
108dab28 | 467 | struct strbuf filename = STRBUF_INIT; |
267123b4 | 468 | *need_8bit_cte_p = -1; /* NEVER */ |
d50b69b8 JK |
469 | |
470 | strbuf_reset(&subject_buffer); | |
471 | strbuf_reset(&buffer); | |
472 | ||
473 | strbuf_addf(&subject_buffer, | |
b02bd65f DB |
474 | "%s" |
475 | "MIME-Version: 1.0\n" | |
476 | "Content-Type: multipart/mixed;" | |
477 | " boundary=\"%s%s\"\n" | |
478 | "\n" | |
479 | "This is a multi-part message in MIME " | |
480 | "format.\n" | |
481 | "--%s%s\n" | |
482 | "Content-Type: text/plain; " | |
483 | "charset=UTF-8; format=fixed\n" | |
484 | "Content-Transfer-Encoding: 8bit\n\n", | |
485 | extra_headers ? extra_headers : "", | |
486 | mime_boundary_leader, opt->mime_boundary, | |
487 | mime_boundary_leader, opt->mime_boundary); | |
d50b69b8 | 488 | extra_headers = subject_buffer.buf; |
b02bd65f | 489 | |
38ec23ac JH |
490 | if (opt->numbered_files) |
491 | strbuf_addf(&filename, "%d", opt->nr); | |
492 | else | |
d28b5d47 | 493 | fmt_output_commit(&filename, commit, opt); |
d50b69b8 | 494 | strbuf_addf(&buffer, |
6b2fbaaf | 495 | "\n--%s%s\n" |
b02bd65f | 496 | "Content-Type: text/x-patch;" |
108dab28 | 497 | " name=\"%s\"\n" |
b02bd65f DB |
498 | "Content-Transfer-Encoding: 8bit\n" |
499 | "Content-Disposition: %s;" | |
108dab28 | 500 | " filename=\"%s\"\n\n", |
b02bd65f | 501 | mime_boundary_leader, opt->mime_boundary, |
108dab28 | 502 | filename.buf, |
b02bd65f | 503 | opt->no_inline ? "attachment" : "inline", |
108dab28 | 504 | filename.buf); |
d50b69b8 | 505 | opt->diffopt.stat_sep = buffer.buf; |
108dab28 | 506 | strbuf_release(&filename); |
b02bd65f | 507 | } |
b02bd65f DB |
508 | *extra_headers_p = extra_headers; |
509 | } | |
510 | ||
c6b3ec41 JH |
511 | static void show_sig_lines(struct rev_info *opt, int status, const char *bol) |
512 | { | |
513 | const char *color, *reset, *eol; | |
514 | ||
515 | color = diff_get_color_opt(&opt->diffopt, | |
516 | status ? DIFF_WHITESPACE : DIFF_FRAGINFO); | |
517 | reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET); | |
518 | while (*bol) { | |
519 | eol = strchrnul(bol, '\n'); | |
4d7b0efc | 520 | fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset, |
c6b3ec41 | 521 | *eol ? "\n" : ""); |
cf3983d1 | 522 | graph_show_oneline(opt->graph); |
c6b3ec41 JH |
523 | bol = (*eol) ? (eol + 1) : eol; |
524 | } | |
525 | } | |
526 | ||
0c37f1fc JH |
527 | static void show_signature(struct rev_info *opt, struct commit *commit) |
528 | { | |
529 | struct strbuf payload = STRBUF_INIT; | |
530 | struct strbuf signature = STRBUF_INIT; | |
67948981 | 531 | struct signature_check sigc = { 0 }; |
0c37f1fc | 532 | int status; |
0c37f1fc | 533 | |
1fb5cf0d | 534 | if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0) |
0c37f1fc JH |
535 | goto out; |
536 | ||
4bbf3780 | 537 | sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT; |
02769437 FS |
538 | sigc.payload = strbuf_detach(&payload, &sigc.payload_len); |
539 | status = check_signature(&sigc, signature.buf, signature.len); | |
b5726a5d | 540 | if (status && !sigc.output) |
67948981 HJI |
541 | show_sig_lines(opt, status, "No signature\n"); |
542 | else | |
b5726a5d | 543 | show_sig_lines(opt, status, sigc.output); |
67948981 | 544 | signature_check_clear(&sigc); |
0c37f1fc JH |
545 | |
546 | out: | |
0c37f1fc JH |
547 | strbuf_release(&payload); |
548 | strbuf_release(&signature); | |
549 | } | |
550 | ||
a92ea68f | 551 | static int which_parent(const struct object_id *oid, const struct commit *commit) |
824958e5 JH |
552 | { |
553 | int nth; | |
554 | const struct commit_list *parent; | |
555 | ||
556 | for (nth = 0, parent = commit->parents; parent; parent = parent->next) { | |
4a7e27e9 | 557 | if (oideq(&parent->item->object.oid, oid)) |
824958e5 JH |
558 | return nth; |
559 | nth++; | |
560 | } | |
561 | return -1; | |
562 | } | |
563 | ||
d041ffa5 JH |
564 | static int is_common_merge(const struct commit *commit) |
565 | { | |
566 | return (commit->parents | |
567 | && commit->parents->next | |
568 | && !commit->parents->next->next); | |
569 | } | |
570 | ||
fef461ea JS |
571 | static int show_one_mergetag(struct commit *commit, |
572 | struct commit_extra_header *extra, | |
573 | void *data) | |
824958e5 | 574 | { |
063da62b | 575 | struct rev_info *opt = (struct rev_info *)data; |
a92ea68f | 576 | struct object_id oid; |
824958e5 JH |
577 | struct tag *tag; |
578 | struct strbuf verify_message; | |
67948981 | 579 | struct signature_check sigc = { 0 }; |
824958e5 | 580 | int status, nth; |
482c1191 | 581 | struct strbuf payload = STRBUF_INIT; |
582 | struct strbuf signature = STRBUF_INIT; | |
824958e5 | 583 | |
2dcde20e | 584 | hash_object_file(the_hash_algo, extra->value, extra->len, |
44439c1c | 585 | OBJ_TAG, &oid); |
ce71efb7 | 586 | tag = lookup_tag(the_repository, &oid); |
824958e5 | 587 | if (!tag) |
fef461ea | 588 | return -1; /* error message already given */ |
824958e5 JH |
589 | |
590 | strbuf_init(&verify_message, 256); | |
0e740fed | 591 | if (parse_tag_buffer(the_repository, tag, extra->value, extra->len)) |
824958e5 | 592 | strbuf_addstr(&verify_message, "malformed mergetag\n"); |
d041ffa5 | 593 | else if (is_common_merge(commit) && |
4a7e27e9 JK |
594 | oideq(&tag->tagged->oid, |
595 | &commit->parents->next->item->object.oid)) | |
d041ffa5 JH |
596 | strbuf_addf(&verify_message, |
597 | "merged tag '%s'\n", tag->tag); | |
a92ea68f | 598 | else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0) |
824958e5 | 599 | strbuf_addf(&verify_message, "tag %s names a non-parent %s\n", |
237a2817 | 600 | tag->tag, oid_to_hex(&tag->tagged->oid)); |
824958e5 JH |
601 | else |
602 | strbuf_addf(&verify_message, | |
603 | "parent #%d, tagged '%s'\n", nth + 1, tag->tag); | |
824958e5 | 604 | |
1315093f | 605 | status = -1; |
482c1191 | 606 | if (parse_signature(extra->value, extra->len, &payload, &signature)) { |
42c55ce4 | 607 | /* could have a good signature */ |
4bbf3780 | 608 | sigc.payload_type = SIGNATURE_PAYLOAD_TAG; |
02769437 FS |
609 | sigc.payload = strbuf_detach(&payload, &sigc.payload_len); |
610 | status = check_signature(&sigc, signature.buf, signature.len); | |
b5726a5d FS |
611 | if (sigc.output) |
612 | strbuf_addstr(&verify_message, sigc.output); | |
67948981 | 613 | else |
42c55ce4 | 614 | strbuf_addstr(&verify_message, "No signature\n"); |
67948981 | 615 | signature_check_clear(&sigc); |
42c55ce4 MG |
616 | /* otherwise we couldn't verify, which is shown as bad */ |
617 | } | |
824958e5 JH |
618 | |
619 | show_sig_lines(opt, status, verify_message.buf); | |
620 | strbuf_release(&verify_message); | |
482c1191 | 621 | strbuf_release(&payload); |
622 | strbuf_release(&signature); | |
fef461ea | 623 | return 0; |
824958e5 JH |
624 | } |
625 | ||
fef461ea | 626 | static int show_mergetag(struct rev_info *opt, struct commit *commit) |
824958e5 | 627 | { |
fef461ea | 628 | return for_each_mergetag(show_one_mergetag, commit, opt); |
824958e5 JH |
629 | } |
630 | ||
3fcc7a23 ES |
631 | static void next_commentary_block(struct rev_info *opt, struct strbuf *sb) |
632 | { | |
633 | const char *x = opt->shown_dashes ? "\n" : "---\n"; | |
634 | if (sb) | |
635 | strbuf_addstr(sb, x); | |
636 | else | |
637 | fputs(x, opt->diffopt.file); | |
638 | opt->shown_dashes = 1; | |
639 | } | |
640 | ||
02865655 | 641 | void show_log(struct rev_info *opt) |
91539833 | 642 | { |
f285a2d7 | 643 | struct strbuf msgbuf = STRBUF_INIT; |
39bc9a6c | 644 | struct log_info *log = opt->loginfo; |
91539833 | 645 | struct commit *commit = log->commit, *parent = log->parent; |
2ed1960a | 646 | int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz; |
dd2e794a TR |
647 | const char *extra_headers = opt->extra_headers; |
648 | struct pretty_print_context ctx = {0}; | |
91539833 LT |
649 | |
650 | opt->loginfo = NULL; | |
651 | if (!opt->verbose_header) { | |
7fefda5c AS |
652 | graph_show_commit(opt->graph); |
653 | ||
1df2d656 | 654 | if (!opt->graph) |
b1b47554 | 655 | put_revision_mark(opt, commit); |
d850b7a5 ÆAB |
656 | fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, abbrev_commit), |
657 | opt->diffopt.file); | |
885cf808 | 658 | if (opt->print_parents) |
4d7b0efc | 659 | show_parents(commit, abbrev_commit, opt->diffopt.file); |
91b849b2 JS |
660 | if (opt->children.name) |
661 | show_children(opt, commit, abbrev_commit); | |
0f3a290b | 662 | show_decorations(opt, commit); |
7fefda5c | 663 | if (opt->graph && !graph_is_commit_finished(opt->graph)) { |
4d7b0efc | 664 | putc('\n', opt->diffopt.file); |
7fefda5c AS |
665 | graph_show_remainder(opt->graph); |
666 | } | |
4d7b0efc | 667 | putc(opt->diffopt.line_termination, opt->diffopt.file); |
91539833 LT |
668 | return; |
669 | } | |
670 | ||
671 | /* | |
8715227d JK |
672 | * If use_terminator is set, we already handled any record termination |
673 | * at the end of the last record. | |
02865655 AS |
674 | * Otherwise, add a diffopt.line_termination character before all |
675 | * entries but the first. (IOW, as a separator between entries) | |
91539833 | 676 | */ |
7fefda5c AS |
677 | if (opt->shown_one && !opt->use_terminator) { |
678 | /* | |
679 | * If entries are separated by a newline, the output | |
680 | * should look human-readable. If the last entry ended | |
681 | * with a newline, print the graph output before this | |
682 | * newline. Otherwise it will end up as a completely blank | |
683 | * line and will look like a gap in the graph. | |
684 | * | |
685 | * If the entry separator is not a newline, the output is | |
686 | * primarily intended for programmatic consumption, and we | |
687 | * never want the extra graph output before the entry | |
688 | * separator. | |
689 | */ | |
690 | if (opt->diffopt.line_termination == '\n' && | |
691 | !opt->missing_newline) | |
692 | graph_show_padding(opt->graph); | |
4d7b0efc | 693 | putc(opt->diffopt.line_termination, opt->diffopt.file); |
7fefda5c | 694 | } |
91539833 LT |
695 | opt->shown_one = 1; |
696 | ||
7fefda5c AS |
697 | /* |
698 | * If the history graph was requested, | |
699 | * print the graph, up to this commit's line | |
700 | */ | |
701 | graph_show_commit(opt->graph); | |
702 | ||
91539833 LT |
703 | /* |
704 | * Print header line of header.. | |
705 | */ | |
3eefc189 | 706 | |
9f23e040 | 707 | if (cmit_fmt_is_mail(opt->commit_format)) { |
6d167fd7 | 708 | log_write_email_headers(opt, commit, &extra_headers, |
50cd54ef | 709 | &ctx.need_8bit_cte, 1); |
6d167fd7 RS |
710 | ctx.rev = opt; |
711 | ctx.print_email_subject = 1; | |
e52a5de4 | 712 | } else if (opt->commit_format != CMIT_FMT_USERFORMAT) { |
4d7b0efc | 713 | fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file); |
74bd9029 | 714 | if (opt->commit_format != CMIT_FMT_ONELINE) |
4d7b0efc | 715 | fputs("commit ", opt->diffopt.file); |
7528f27d | 716 | |
1df2d656 | 717 | if (!opt->graph) |
b1b47554 | 718 | put_revision_mark(opt, commit); |
d850b7a5 ÆAB |
719 | fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, |
720 | abbrev_commit), | |
4d7b0efc | 721 | opt->diffopt.file); |
885cf808 | 722 | if (opt->print_parents) |
4d7b0efc | 723 | show_parents(commit, abbrev_commit, opt->diffopt.file); |
91b849b2 JS |
724 | if (opt->children.name) |
725 | show_children(opt, commit, abbrev_commit); | |
73f0a157 | 726 | if (parent) |
4d7b0efc | 727 | fprintf(opt->diffopt.file, " (from %s)", |
d850b7a5 | 728 | repo_find_unique_abbrev(the_repository, &parent->object.oid, abbrev_commit)); |
4d7b0efc | 729 | fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file); |
0f3a290b | 730 | show_decorations(opt, commit); |
7fefda5c | 731 | if (opt->commit_format == CMIT_FMT_ONELINE) { |
4d7b0efc | 732 | putc(' ', opt->diffopt.file); |
7fefda5c | 733 | } else { |
4d7b0efc | 734 | putc('\n', opt->diffopt.file); |
7fefda5c AS |
735 | graph_show_oneline(opt->graph); |
736 | } | |
903b45fe | 737 | if (opt->reflog_info) { |
7fefda5c AS |
738 | /* |
739 | * setup_revisions() ensures that opt->reflog_info | |
740 | * and opt->graph cannot both be set, | |
741 | * so we don't need to worry about printing the | |
742 | * graph info here. | |
743 | */ | |
4d12a471 | 744 | show_reflog_message(opt->reflog_info, |
55ccf85a | 745 | opt->commit_format == CMIT_FMT_ONELINE, |
a5481a6c | 746 | &opt->date_mode, |
55ccf85a | 747 | opt->date_mode_explicit); |
02865655 | 748 | if (opt->commit_format == CMIT_FMT_ONELINE) |
903b45fe | 749 | return; |
903b45fe | 750 | } |
3eefc189 | 751 | } |
91539833 | 752 | |
824958e5 | 753 | if (opt->show_signature) { |
0c37f1fc | 754 | show_signature(opt, commit); |
824958e5 JH |
755 | show_mergetag(opt, commit); |
756 | } | |
0c37f1fc | 757 | |
ddf333f6 JH |
758 | if (opt->show_notes) { |
759 | int raw; | |
760 | struct strbuf notebuf = STRBUF_INIT; | |
761 | ||
762 | raw = (opt->commit_format == CMIT_FMT_USERFORMAT); | |
fb61e4d3 | 763 | format_display_notes(&commit->object.oid, ¬ebuf, |
ddf333f6 | 764 | get_log_output_encoding(), raw); |
82f51af3 | 765 | ctx.notes_message = strbuf_detach(¬ebuf, NULL); |
ddf333f6 JH |
766 | } |
767 | ||
91539833 LT |
768 | /* |
769 | * And then the pretty-printed message itself | |
770 | */ | |
5289c56a NTND |
771 | if (ctx.need_8bit_cte >= 0 && opt->add_signoff) |
772 | ctx.need_8bit_cte = | |
39ab4d09 | 773 | has_non_ascii(fmt_name(WANT_COMMITTER_IDENT)); |
dd2e794a | 774 | ctx.date_mode = opt->date_mode; |
f026c756 | 775 | ctx.date_mode_explicit = opt->date_mode_explicit; |
dd2e794a TR |
776 | ctx.abbrev = opt->diffopt.abbrev; |
777 | ctx.after_subject = extra_headers; | |
9553d2b2 | 778 | ctx.preserve_subject = opt->preserve_subject; |
19d097e3 | 779 | ctx.encode_email_headers = opt->encode_email_headers; |
8f8f5476 | 780 | ctx.reflog_info = opt->reflog_info; |
6bf13944 | 781 | ctx.fmt = opt->commit_format; |
0e2913b0 | 782 | ctx.mailmap = opt->mailmap; |
30825178 | 783 | ctx.color = opt->diffopt.use_color; |
7cc13c71 | 784 | ctx.expand_tabs_in_log = opt->expand_tabs_in_log; |
ecaee805 | 785 | ctx.output_encoding = get_log_output_encoding(); |
ad6f028f | 786 | ctx.rev = opt; |
a9080475 JK |
787 | if (opt->from_ident.mail_begin && opt->from_ident.name_begin) |
788 | ctx.from_ident = &opt->from_ident; | |
3ad87c80 JK |
789 | if (opt->graph) |
790 | ctx.graph_width = graph_width(opt->graph); | |
6bf13944 | 791 | pretty_print_commit(&ctx, commit, &msgbuf); |
212620fe JH |
792 | |
793 | if (opt->add_signoff) | |
5289c56a | 794 | append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP); |
212620fe | 795 | |
5a664cf2 | 796 | if ((ctx.fmt != CMIT_FMT_USERFORMAT) && |
bd1470b8 | 797 | ctx.notes_message && *ctx.notes_message) { |
3fcc7a23 ES |
798 | if (cmit_fmt_is_mail(ctx.fmt)) |
799 | next_commentary_block(opt, &msgbuf); | |
5a664cf2 | 800 | strbuf_addstr(&msgbuf, ctx.notes_message); |
bd1470b8 | 801 | } |
cf2251b6 | 802 | |
7fefda5c | 803 | if (opt->show_log_size) { |
4d7b0efc | 804 | fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len); |
7fefda5c AS |
805 | graph_show_oneline(opt->graph); |
806 | } | |
9fa3465d | 807 | |
7fefda5c AS |
808 | /* |
809 | * Set opt->missing_newline if msgbuf doesn't | |
810 | * end in a newline (including if it is empty) | |
811 | */ | |
812 | if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n') | |
813 | opt->missing_newline = 1; | |
814 | else | |
815 | opt->missing_newline = 0; | |
816 | ||
660e113c | 817 | graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf); |
b9c7d6e4 | 818 | if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) { |
7fefda5c AS |
819 | if (!opt->missing_newline) |
820 | graph_show_padding(opt->graph); | |
4d7b0efc | 821 | putc(opt->diffopt.line_termination, opt->diffopt.file); |
7fefda5c AS |
822 | } |
823 | ||
674d1727 | 824 | strbuf_release(&msgbuf); |
ddf333f6 | 825 | free(ctx.notes_message); |
ee6cbf71 ES |
826 | |
827 | if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) { | |
828 | struct diff_queue_struct dq; | |
829 | ||
830 | memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff)); | |
831 | DIFF_QUEUE_CLEAR(&diff_queued_diff); | |
832 | ||
833 | next_commentary_block(opt, NULL); | |
834 | fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title); | |
72a72390 ES |
835 | show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2, |
836 | &opt->diffopt); | |
ee6cbf71 ES |
837 | |
838 | memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff)); | |
839 | } | |
40ce4160 ES |
840 | |
841 | if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) { | |
842 | struct diff_queue_struct dq; | |
ac0edf1f | 843 | struct diff_options opts; |
f1ce6c19 JS |
844 | struct range_diff_options range_diff_opts = { |
845 | .creation_factor = opt->creation_factor, | |
846 | .dual_color = 1, | |
847 | .diffopt = &opts | |
848 | }; | |
40ce4160 ES |
849 | |
850 | memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff)); | |
851 | DIFF_QUEUE_CLEAR(&diff_queued_diff); | |
852 | ||
853 | next_commentary_block(opt, NULL); | |
854 | fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title); | |
ac0edf1f MÅ |
855 | /* |
856 | * Pass minimum required diff-options to range-diff; others | |
857 | * can be added later if deemed desirable. | |
858 | */ | |
08539032 | 859 | repo_diff_setup(the_repository, &opts); |
ac0edf1f MÅ |
860 | opts.file = opt->diffopt.file; |
861 | opts.use_color = opt->diffopt.use_color; | |
862 | diff_setup_done(&opts); | |
f1ce6c19 | 863 | show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts); |
40ce4160 ES |
864 | |
865 | memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff)); | |
866 | } | |
91539833 LT |
867 | } |
868 | ||
cd2bdc53 | 869 | int log_tree_diff_flush(struct rev_info *opt) |
5f1c3f07 | 870 | { |
bd1470b8 | 871 | opt->shown_dashes = 0; |
5f1c3f07 | 872 | diffcore_std(&opt->diffopt); |
91539833 | 873 | |
95433eee | 874 | if (diff_queue_is_empty(&opt->diffopt)) { |
5f1c3f07 JH |
875 | int saved_fmt = opt->diffopt.output_format; |
876 | opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT; | |
877 | diff_flush(&opt->diffopt); | |
878 | opt->diffopt.output_format = saved_fmt; | |
879 | return 0; | |
880 | } | |
91539833 | 881 | |
3969cf7d | 882 | if (opt->loginfo && !opt->no_commit_id) { |
02865655 | 883 | show_log(opt); |
304b5af6 LT |
884 | if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) && |
885 | opt->verbose_header && | |
b9c7d6e4 JK |
886 | opt->commit_format != CMIT_FMT_ONELINE && |
887 | !commit_format_is_empty(opt->commit_format)) { | |
1d34c50f JH |
888 | /* |
889 | * When showing a verbose header (i.e. log message), | |
890 | * and not in --pretty=oneline format, we would want | |
891 | * an extra newline between the end of log and the | |
892 | * diff/diffstat output for readability. | |
893 | */ | |
3969cf7d | 894 | int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH; |
81bd1b2a BY |
895 | if (opt->diffopt.output_prefix) { |
896 | struct strbuf *msg = NULL; | |
897 | msg = opt->diffopt.output_prefix(&opt->diffopt, | |
898 | opt->diffopt.output_prefix_data); | |
4d7b0efc | 899 | fwrite(msg->buf, msg->len, 1, opt->diffopt.file); |
81bd1b2a | 900 | } |
1d34c50f JH |
901 | |
902 | /* | |
903 | * We may have shown three-dashes line early | |
3fcc7a23 ES |
904 | * between generated commentary (notes, etc.) |
905 | * and the log message, in which case we only | |
906 | * want a blank line after the commentary | |
907 | * without (an extra) three-dashes line. | |
1d34c50f JH |
908 | * Otherwise, we show the three-dashes line if |
909 | * we are showing the patch with diffstat, but | |
910 | * in that case, there is no extra blank line | |
911 | * after the three-dashes line. | |
912 | */ | |
913 | if (!opt->shown_dashes && | |
914 | (pch & opt->diffopt.output_format) == pch) | |
4d7b0efc JS |
915 | fprintf(opt->diffopt.file, "---"); |
916 | putc('\n', opt->diffopt.file); | |
3969cf7d JH |
917 | } |
918 | } | |
5f1c3f07 JH |
919 | diff_flush(&opt->diffopt); |
920 | return 1; | |
921 | } | |
922 | ||
cd2bdc53 | 923 | static int do_diff_combined(struct rev_info *opt, struct commit *commit) |
5f1c3f07 | 924 | { |
d01141de | 925 | diff_tree_combined_merge(commit, opt); |
91539833 | 926 | return !opt->loginfo; |
5f1c3f07 JH |
927 | } |
928 | ||
20323d10 EN |
929 | static void setup_additional_headers(struct diff_options *o, |
930 | struct strmap *all_headers) | |
931 | { | |
932 | struct hashmap_iter iter; | |
933 | struct strmap_entry *entry; | |
934 | ||
935 | /* | |
936 | * Make o->additional_path_headers contain the subset of all_headers | |
937 | * that match o->pathspec. If there aren't any that match o->pathspec, | |
938 | * then make o->additional_path_headers be NULL. | |
939 | */ | |
940 | ||
941 | if (!o->pathspec.nr) { | |
942 | o->additional_path_headers = all_headers; | |
943 | return; | |
944 | } | |
945 | ||
946 | o->additional_path_headers = xmalloc(sizeof(struct strmap)); | |
947 | strmap_init_with_options(o->additional_path_headers, NULL, 0); | |
948 | strmap_for_each_entry(all_headers, &iter, entry) { | |
949 | if (match_pathspec(the_repository->index, &o->pathspec, | |
950 | entry->key, strlen(entry->key), | |
951 | 0 /* prefix */, NULL /* seen */, | |
952 | 0 /* is_dir */)) | |
953 | strmap_put(o->additional_path_headers, | |
954 | entry->key, entry->value); | |
955 | } | |
956 | if (!strmap_get_size(o->additional_path_headers)) { | |
957 | strmap_clear(o->additional_path_headers, 0); | |
958 | FREE_AND_NULL(o->additional_path_headers); | |
959 | } | |
960 | } | |
961 | ||
962 | static void cleanup_additional_headers(struct diff_options *o) | |
963 | { | |
964 | if (!o->pathspec.nr) { | |
965 | o->additional_path_headers = NULL; | |
966 | return; | |
967 | } | |
968 | if (!o->additional_path_headers) | |
969 | return; | |
970 | ||
971 | strmap_clear(o->additional_path_headers, 0); | |
972 | FREE_AND_NULL(o->additional_path_headers); | |
973 | } | |
974 | ||
db757e8b EN |
975 | static int do_remerge_diff(struct rev_info *opt, |
976 | struct commit_list *parents, | |
e2841f70 | 977 | struct object_id *oid) |
db757e8b EN |
978 | { |
979 | struct merge_options o; | |
980 | struct commit_list *bases; | |
981 | struct merge_result res = {0}; | |
982 | struct pretty_print_context ctx = {0}; | |
983 | struct commit *parent1 = parents->item; | |
984 | struct commit *parent2 = parents->next->item; | |
985 | struct strbuf parent1_desc = STRBUF_INIT; | |
986 | struct strbuf parent2_desc = STRBUF_INIT; | |
987 | ||
988 | /* Setup merge options */ | |
989 | init_merge_options(&o, the_repository); | |
990 | o.show_rename_progress = 0; | |
20323d10 EN |
991 | o.record_conflict_msgs_as_headers = 1; |
992 | o.msg_header_prefix = "remerge"; | |
db757e8b EN |
993 | |
994 | ctx.abbrev = DEFAULT_ABBREV; | |
bab82164 ÆAB |
995 | repo_format_commit_message(the_repository, parent1, "%h (%s)", |
996 | &parent1_desc, &ctx); | |
997 | repo_format_commit_message(the_repository, parent2, "%h (%s)", | |
998 | &parent2_desc, &ctx); | |
db757e8b EN |
999 | o.branch1 = parent1_desc.buf; |
1000 | o.branch2 = parent2_desc.buf; | |
1001 | ||
1002 | /* Parse the relevant commits and get the merge bases */ | |
1003 | parse_commit_or_die(parent1); | |
1004 | parse_commit_or_die(parent2); | |
cb338c23 | 1005 | bases = repo_get_merge_bases(the_repository, parent1, parent2); |
db757e8b EN |
1006 | |
1007 | /* Re-merge the parents */ | |
1008 | merge_incore_recursive(&o, bases, parent1, parent2, &res); | |
1009 | ||
1010 | /* Show the diff */ | |
20323d10 | 1011 | setup_additional_headers(&opt->diffopt, res.path_messages); |
db757e8b EN |
1012 | diff_tree_oid(&res.tree->object.oid, oid, "", &opt->diffopt); |
1013 | log_tree_diff_flush(opt); | |
1014 | ||
1015 | /* Cleanup */ | |
20323d10 | 1016 | cleanup_additional_headers(&opt->diffopt); |
db757e8b EN |
1017 | strbuf_release(&parent1_desc); |
1018 | strbuf_release(&parent2_desc); | |
1019 | merge_finalize(&o, &res); | |
7b90ab46 EN |
1020 | |
1021 | /* Clean up the contents of the temporary object directory */ | |
1022 | if (opt->remerge_objdir) | |
1023 | tmp_objdir_discard_objects(opt->remerge_objdir); | |
1024 | else | |
1025 | BUG("did a remerge diff without remerge_objdir?!?"); | |
db757e8b EN |
1026 | |
1027 | return !opt->loginfo; | |
1028 | } | |
1029 | ||
91539833 LT |
1030 | /* |
1031 | * Show the diff of a commit. | |
1032 | * | |
1033 | * Return true if we printed any log info messages | |
1034 | */ | |
1035 | static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log) | |
5f1c3f07 | 1036 | { |
91539833 | 1037 | int showed_log; |
5f1c3f07 | 1038 | struct commit_list *parents; |
f2fd0760 | 1039 | struct object_id *oid; |
a6d19ecc SO |
1040 | int is_merge; |
1041 | int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status; | |
5f1c3f07 | 1042 | |
a6d19ecc | 1043 | if (!all_need_diff && !opt->merges_need_diff) |
91539833 LT |
1044 | return 0; |
1045 | ||
7059dccc | 1046 | parse_commit_or_die(commit); |
2e27bd77 | 1047 | oid = get_commit_tree_oid(commit); |
d1b9b767 | 1048 | |
53d00b39 | 1049 | parents = get_saved_parents(opt, commit); |
a6d19ecc SO |
1050 | is_merge = parents && parents->next; |
1051 | if (!is_merge && !all_need_diff) | |
1052 | return 0; | |
1053 | ||
1054 | /* Root commit? */ | |
91539833 | 1055 | if (!parents) { |
2b60356d | 1056 | if (opt->show_root_diff) { |
7b8dea0c | 1057 | diff_root_tree_oid(oid, "", &opt->diffopt); |
2b60356d RS |
1058 | log_tree_diff_flush(opt); |
1059 | } | |
91539833 | 1060 | return !opt->loginfo; |
5f1c3f07 JH |
1061 | } |
1062 | ||
a6d19ecc | 1063 | if (is_merge) { |
db757e8b EN |
1064 | int octopus = (parents->next->next != NULL); |
1065 | ||
1066 | if (opt->remerge_diff) { | |
1067 | if (octopus) { | |
1068 | show_log(opt); | |
1069 | fprintf(opt->diffopt.file, | |
1070 | "diff: warning: Skipping remerge-diff " | |
1071 | "for octopus merges.\n"); | |
1072 | return 1; | |
1073 | } | |
e2841f70 | 1074 | return do_remerge_diff(opt, parents, oid); |
db757e8b | 1075 | } |
1a2c4d80 | 1076 | if (opt->combine_merges) |
5f1c3f07 | 1077 | return do_diff_combined(opt, commit); |
1a2c4d80 SO |
1078 | if (opt->separate_merges) { |
1079 | if (!opt->first_parent_merges) { | |
1080 | /* Show parent info for multiple diffs */ | |
1081 | log->parent = parents->item; | |
1082 | } | |
1083 | } else | |
1084 | return 0; | |
5f1c3f07 JH |
1085 | } |
1086 | ||
91539833 LT |
1087 | showed_log = 0; |
1088 | for (;;) { | |
5f1c3f07 | 1089 | struct commit *parent = parents->item; |
5f1c3f07 | 1090 | |
7059dccc | 1091 | parse_commit_or_die(parent); |
2e27bd77 | 1092 | diff_tree_oid(get_commit_tree_oid(parent), |
66f414f8 | 1093 | oid, "", &opt->diffopt); |
91539833 LT |
1094 | log_tree_diff_flush(opt); |
1095 | ||
1096 | showed_log |= !opt->loginfo; | |
1097 | ||
1098 | /* Set up the log info for the next parent, if any.. */ | |
1099 | parents = parents->next; | |
3291eea3 | 1100 | if (!parents || opt->first_parent_merges) |
91539833 LT |
1101 | break; |
1102 | log->parent = parents->item; | |
1103 | opt->loginfo = log; | |
1104 | } | |
1105 | return showed_log; | |
1106 | } | |
1107 | ||
1108 | int log_tree_commit(struct rev_info *opt, struct commit *commit) | |
1109 | { | |
1110 | struct log_info log; | |
e900d494 ÆAB |
1111 | int shown; |
1112 | /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */ | |
1113 | int no_free = opt->diffopt.no_free; | |
91539833 LT |
1114 | |
1115 | log.commit = commit; | |
1116 | log.parent = NULL; | |
1117 | opt->loginfo = &log; | |
e900d494 | 1118 | opt->diffopt.no_free = 1; |
91539833 | 1119 | |
f8781bfd | 1120 | /* NEEDSWORK: no restoring of no_free? Why? */ |
12da1d1f TR |
1121 | if (opt->line_level_traverse) |
1122 | return line_log_print(opt, commit); | |
1123 | ||
1b32dece | 1124 | if (opt->track_linear && !opt->linear && !opt->reverse_output_stage) |
4d7b0efc | 1125 | fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar); |
3eefc189 JH |
1126 | shown = log_tree_diff(opt, commit, &log); |
1127 | if (!shown && opt->loginfo && opt->always_show_header) { | |
91539833 | 1128 | log.parent = NULL; |
02865655 | 1129 | show_log(opt); |
3eefc189 | 1130 | shown = 1; |
5f1c3f07 | 1131 | } |
1b32dece | 1132 | if (opt->track_linear && !opt->linear && opt->reverse_output_stage) |
4d7b0efc | 1133 | fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar); |
91539833 | 1134 | opt->loginfo = NULL; |
4d7b0efc | 1135 | maybe_flush_or_die(opt->diffopt.file, "stdout"); |
e900d494 ÆAB |
1136 | opt->diffopt.no_free = no_free; |
1137 | diff_free(&opt->diffopt); | |
3eefc189 | 1138 | return shown; |
5f1c3f07 | 1139 | } |