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