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