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