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