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