| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 1 | #include "cache.h" |
| Brandon Williams | b2141fc | 2017-06-14 18:07:36 | [diff] [blame] | 2 | #include "config.h" |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 3 | #include "diff.h" |
| Stefan Beller | cbd53a2 | 2018-05-15 23:42:15 | [diff] [blame] | 4 | #include "object-store.h" |
| Stefan Beller | 109cd76 | 2018-06-29 01:21:51 | [diff] [blame] | 5 | #include "repository.h" |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 6 | #include "commit.h" |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 7 | #include "tag.h" |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 8 | #include "graph.h" |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 9 | #include "log-tree.h" |
| Johannes Schindelin | 8860fd4 | 2007-01-11 10:47:48 | [diff] [blame] | 10 | #include "reflog-walk.h" |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 11 | #include "refs.h" |
| Thomas Rast | b079c50 | 2009-02-19 21:26:31 | [diff] [blame] | 12 | #include "string-list.h" |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 13 | #include "color.h" |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 14 | #include "gpg-interface.h" |
| Brandon Casey | 959a262 | 2013-02-12 10:17:39 | [diff] [blame] | 15 | #include "sequencer.h" |
| Thomas Rast | 12da1d1 | 2013-03-28 16:47:32 | [diff] [blame] | 16 | #include "line-log.h" |
| Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 13:55:24 | [diff] [blame] | 17 | #include "help.h" |
| Eric Sunshine | 40ce416 | 2018-07-22 09:57:17 | [diff] [blame] | 18 | #include "range-diff.h" |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 19 | |
| Jeff King | 2608c24 | 2014-08-26 10:23:54 | [diff] [blame] | 20 | static struct decoration name_decoration = { "object names" }; |
| Junio C Hamano | 76c61fb | 2015-05-13 17:25:18 | [diff] [blame] | 21 | static int decoration_loaded; |
| 22 | static int decoration_flags; |
| Nazri Ramliy | a752412 | 2010-06-19 01:37:34 | [diff] [blame] | 23 | |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 24 | static char decoration_colors[][COLOR_MAXLEN] = { |
| 25 | GIT_COLOR_RESET, |
| 26 | GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */ |
| 27 | GIT_COLOR_BOLD_RED, /* REF_REMOTE */ |
| 28 | GIT_COLOR_BOLD_YELLOW, /* REF_TAG */ |
| 29 | GIT_COLOR_BOLD_MAGENTA, /* REF_STASH */ |
| 30 | GIT_COLOR_BOLD_CYAN, /* REF_HEAD */ |
| Nguyễn Thái Ngọc Duy | 76f5df3 | 2011-08-18 12:29:37 | [diff] [blame] | 31 | GIT_COLOR_BOLD_BLUE, /* GRAFTED */ |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 32 | }; |
| 33 | |
| Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 13:55:21 | [diff] [blame] | 34 | static const char *color_decorate_slots[] = { |
| 35 | [DECORATION_REF_LOCAL] = "branch", |
| 36 | [DECORATION_REF_REMOTE] = "remoteBranch", |
| 37 | [DECORATION_REF_TAG] = "tag", |
| 38 | [DECORATION_REF_STASH] = "stash", |
| 39 | [DECORATION_REF_HEAD] = "HEAD", |
| Nguyễn Thái Ngọc Duy | 09c4ba4 | 2018-05-26 13:55:31 | [diff] [blame] | 40 | [DECORATION_GRAFTED] = "grafted", |
| Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 13:55:21 | [diff] [blame] | 41 | }; |
| 42 | |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 43 | static const char *decorate_get_color(int decorate_use_color, enum decoration_type ix) |
| 44 | { |
| Jeff King | daa0c3d | 2011-08-18 05:04:23 | [diff] [blame] | 45 | if (want_color(decorate_use_color)) |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 46 | return decoration_colors[ix]; |
| 47 | return ""; |
| 48 | } |
| 49 | |
| Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 13:55:24 | [diff] [blame] | 50 | define_list_config_array(color_decorate_slots); |
| Nazri Ramliy | 5e11bee | 2010-06-24 00:21:16 | [diff] [blame] | 51 | |
| Jonathan Nieder | 8852117 | 2014-10-07 19:16:57 | [diff] [blame] | 52 | int parse_decorate_color_config(const char *var, const char *slot_name, const char *value) |
| Nazri Ramliy | 5e11bee | 2010-06-24 00:21:16 | [diff] [blame] | 53 | { |
| Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 13:55:21 | [diff] [blame] | 54 | int slot = LOOKUP_CONFIG(color_decorate_slots, slot_name); |
| Nazri Ramliy | 5e11bee | 2010-06-24 00:21:16 | [diff] [blame] | 55 | if (slot < 0) |
| 56 | return 0; |
| 57 | if (!value) |
| 58 | return config_error_nonbool(var); |
| Jeff King | f6c5a29 | 2014-10-07 19:33:09 | [diff] [blame] | 59 | return color_parse(value, decoration_colors[slot]); |
| Nazri Ramliy | 5e11bee | 2010-06-24 00:21:16 | [diff] [blame] | 60 | } |
| 61 | |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 62 | /* |
| 63 | * log-tree.c uses DIFF_OPT_TST for determining whether to use color |
| 64 | * for showing the commit sha1, use the same check for --decorate |
| 65 | */ |
| 66 | #define decorate_get_color_opt(o, ix) \ |
| Jeff King | f1c9626 | 2011-08-18 05:03:12 | [diff] [blame] | 67 | decorate_get_color((o)->use_color, ix) |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 68 | |
| Jeff King | 662174d | 2014-08-26 10:23:36 | [diff] [blame] | 69 | void add_name_decoration(enum decoration_type type, const char *name, struct object *obj) |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 70 | { |
| Jeff King | 96ffc06 | 2016-02-22 22:44:32 | [diff] [blame] | 71 | struct name_decoration *res; |
| 72 | FLEX_ALLOC_STR(res, name, name); |
| Nazri Ramliy | a752412 | 2010-06-19 01:37:34 | [diff] [blame] | 73 | res->type = type; |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 74 | res->next = add_decoration(&name_decoration, obj, res); |
| 75 | } |
| 76 | |
| Jeff King | 2608c24 | 2014-08-26 10:23:54 | [diff] [blame] | 77 | const struct name_decoration *get_name_decoration(const struct object *obj) |
| 78 | { |
| René Scharfe | 0cc7380 | 2019-09-08 17:58:51 | [diff] [blame] | 79 | load_ref_decorations(NULL, DECORATE_SHORT_REFS); |
| Jeff King | 2608c24 | 2014-08-26 10:23:54 | [diff] [blame] | 80 | return lookup_decoration(&name_decoration, obj); |
| 81 | } |
| 82 | |
| Derrick Stolee | c9f7a79 | 2020-04-16 14:15:48 | [diff] [blame] | 83 | static int match_ref_pattern(const char *refname, |
| 84 | const struct string_list_item *item) |
| 85 | { |
| 86 | int matched = 0; |
| 87 | if (item->util == NULL) { |
| 88 | if (!wildmatch(item->string, refname, 0)) |
| 89 | matched = 1; |
| 90 | } else { |
| 91 | const char *rest; |
| 92 | if (skip_prefix(refname, item->string, &rest) && |
| 93 | (!*rest || *rest == '/')) |
| 94 | matched = 1; |
| 95 | } |
| 96 | return matched; |
| 97 | } |
| 98 | |
| 99 | static int ref_filter_match(const char *refname, |
| 100 | const struct decoration_filter *filter) |
| 101 | { |
| 102 | struct string_list_item *item; |
| 103 | const struct string_list *exclude_patterns = filter->exclude_ref_pattern; |
| 104 | const struct string_list *include_patterns = filter->include_ref_pattern; |
| Derrick Stolee | a6be5e6 | 2020-04-16 14:15:49 | [diff] [blame] | 105 | const struct string_list *exclude_patterns_config = |
| 106 | filter->exclude_ref_config_pattern; |
| Derrick Stolee | c9f7a79 | 2020-04-16 14:15:48 | [diff] [blame] | 107 | |
| 108 | if (exclude_patterns && exclude_patterns->nr) { |
| 109 | for_each_string_list_item(item, exclude_patterns) { |
| 110 | if (match_ref_pattern(refname, item)) |
| 111 | return 0; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (include_patterns && include_patterns->nr) { |
| Derrick Stolee | c9f7a79 | 2020-04-16 14:15:48 | [diff] [blame] | 116 | for_each_string_list_item(item, include_patterns) { |
| Derrick Stolee | a6be5e6 | 2020-04-16 14:15:49 | [diff] [blame] | 117 | if (match_ref_pattern(refname, item)) |
| 118 | return 1; |
| Derrick Stolee | c9f7a79 | 2020-04-16 14:15:48 | [diff] [blame] | 119 | } |
| Derrick Stolee | a6be5e6 | 2020-04-16 14:15:49 | [diff] [blame] | 120 | return 0; |
| Derrick Stolee | c9f7a79 | 2020-04-16 14:15:48 | [diff] [blame] | 121 | } |
| Derrick Stolee | a6be5e6 | 2020-04-16 14:15:49 | [diff] [blame] | 122 | |
| 123 | if (exclude_patterns_config && exclude_patterns_config->nr) { |
| 124 | for_each_string_list_item(item, exclude_patterns_config) { |
| 125 | if (match_ref_pattern(refname, item)) |
| 126 | return 0; |
| 127 | } |
| 128 | } |
| 129 | |
| Derrick Stolee | c9f7a79 | 2020-04-16 14:15:48 | [diff] [blame] | 130 | return 1; |
| 131 | } |
| 132 | |
| Michael Haggerty | f124b73 | 2015-05-25 18:38:57 | [diff] [blame] | 133 | static int add_ref_decoration(const char *refname, const struct object_id *oid, |
| 134 | int flags, void *cb_data) |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 135 | { |
| Nguyễn Thái Ngọc Duy | 5267d29 | 2011-08-19 12:43:50 | [diff] [blame] | 136 | struct object *obj; |
| Jeff King | 88473c8 | 2021-06-22 17:06:48 | [diff] [blame] | 137 | enum object_type objtype; |
| Jeff King | 6afb265 | 2021-06-22 17:09:26 | [diff] [blame] | 138 | enum decoration_type deco_type = DECORATION_NONE; |
| Rafael Ascensão | 65516f5 | 2017-11-21 21:33:41 | [diff] [blame] | 139 | struct decoration_filter *filter = (struct decoration_filter *)cb_data; |
| Nguyễn Thái Ngọc Duy | 5267d29 | 2011-08-19 12:43:50 | [diff] [blame] | 140 | |
| Derrick Stolee | c9f7a79 | 2020-04-16 14:15:48 | [diff] [blame] | 141 | if (filter && !ref_filter_match(refname, filter)) |
| Rafael Ascensão | 65516f5 | 2017-11-21 21:33:41 | [diff] [blame] | 142 | return 0; |
| Junio C Hamano | 429ad20 | 2015-05-13 19:40:35 | [diff] [blame] | 143 | |
| Mike Hommey | 58d121b | 2015-06-11 21:34:59 | [diff] [blame] | 144 | if (starts_with(refname, git_replace_ref_base)) { |
| Michael Haggerty | 3d79f65 | 2015-05-25 18:38:58 | [diff] [blame] | 145 | struct object_id original_oid; |
| Jeff King | 6ebd1ca | 2018-07-18 20:45:20 | [diff] [blame] | 146 | if (!read_replace_refs) |
| Michael J Gruber | b9ad500 | 2011-08-25 15:09:30 | [diff] [blame] | 147 | return 0; |
| Junio C Hamano | 31a0ad5 | 2015-08-03 18:01:10 | [diff] [blame] | 148 | if (get_oid_hex(refname + strlen(git_replace_ref_base), |
| 149 | &original_oid)) { |
| Nguyễn Thái Ngọc Duy | 5267d29 | 2011-08-19 12:43:50 | [diff] [blame] | 150 | warning("invalid replace ref %s", refname); |
| 151 | return 0; |
| 152 | } |
| Stefan Beller | 109cd76 | 2018-06-29 01:21:51 | [diff] [blame] | 153 | obj = parse_object(the_repository, &original_oid); |
| Nguyễn Thái Ngọc Duy | 5267d29 | 2011-08-19 12:43:50 | [diff] [blame] | 154 | if (obj) |
| 155 | add_name_decoration(DECORATION_GRAFTED, "replaced", obj); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| Jeff King | 88473c8 | 2021-06-22 17:06:48 | [diff] [blame] | 159 | objtype = oid_object_info(the_repository, oid, NULL); |
| 160 | if (objtype < 0) |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 161 | return 0; |
| Jeff King | 88473c8 | 2021-06-22 17:06:48 | [diff] [blame] | 162 | obj = lookup_object_by_type(the_repository, oid, objtype); |
| Nazri Ramliy | a752412 | 2010-06-19 01:37:34 | [diff] [blame] | 163 | |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 164 | if (starts_with(refname, "refs/heads/")) |
| Jeff King | 6afb265 | 2021-06-22 17:09:26 | [diff] [blame] | 165 | deco_type = DECORATION_REF_LOCAL; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 166 | else if (starts_with(refname, "refs/remotes/")) |
| Jeff King | 6afb265 | 2021-06-22 17:09:26 | [diff] [blame] | 167 | deco_type = DECORATION_REF_REMOTE; |
| Christian Couder | 5955654 | 2013-11-30 20:55:40 | [diff] [blame] | 168 | else if (starts_with(refname, "refs/tags/")) |
| Jeff King | 6afb265 | 2021-06-22 17:09:26 | [diff] [blame] | 169 | deco_type = DECORATION_REF_TAG; |
| Nguyễn Thái Ngọc Duy | 97ba642 | 2012-01-05 12:39:40 | [diff] [blame] | 170 | else if (!strcmp(refname, "refs/stash")) |
| Jeff King | 6afb265 | 2021-06-22 17:09:26 | [diff] [blame] | 171 | deco_type = DECORATION_REF_STASH; |
| Nguyễn Thái Ngọc Duy | 97ba642 | 2012-01-05 12:39:40 | [diff] [blame] | 172 | else if (!strcmp(refname, "HEAD")) |
| Jeff King | 6afb265 | 2021-06-22 17:09:26 | [diff] [blame] | 173 | deco_type = DECORATION_REF_HEAD; |
| Nazri Ramliy | a752412 | 2010-06-19 01:37:34 | [diff] [blame] | 174 | |
| Jeff King | 6afb265 | 2021-06-22 17:09:26 | [diff] [blame] | 175 | add_name_decoration(deco_type, refname, obj); |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 176 | while (obj->type == OBJ_TAG) { |
| Jeff King | d1ed8d6 | 2021-07-14 16:31:36 | [diff] [blame] | 177 | if (!obj->parsed) |
| 178 | parse_object(the_repository, &obj->oid); |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 179 | obj = ((struct tag *)obj)->tagged; |
| 180 | if (!obj) |
| 181 | break; |
| Nazri Ramliy | a752412 | 2010-06-19 01:37:34 | [diff] [blame] | 182 | add_name_decoration(DECORATION_REF_TAG, refname, obj); |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 183 | } |
| 184 | return 0; |
| 185 | } |
| 186 | |
| Nguyễn Thái Ngọc Duy | 76f5df3 | 2011-08-18 12:29:37 | [diff] [blame] | 187 | static int add_graft_decoration(const struct commit_graft *graft, void *cb_data) |
| 188 | { |
| Stefan Beller | c1f5eb4 | 2018-06-29 01:21:59 | [diff] [blame] | 189 | struct commit *commit = lookup_commit(the_repository, &graft->oid); |
| Nguyễn Thái Ngọc Duy | 76f5df3 | 2011-08-18 12:29:37 | [diff] [blame] | 190 | if (!commit) |
| 191 | return 0; |
| 192 | add_name_decoration(DECORATION_GRAFTED, "grafted", &commit->object); |
| 193 | return 0; |
| 194 | } |
| 195 | |
| Rafael Ascensão | 65516f5 | 2017-11-21 21:33:41 | [diff] [blame] | 196 | void load_ref_decorations(struct decoration_filter *filter, int flags) |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 197 | { |
| Junio C Hamano | 76c61fb | 2015-05-13 17:25:18 | [diff] [blame] | 198 | if (!decoration_loaded) { |
| Rafael Ascensão | 65516f5 | 2017-11-21 21:33:41 | [diff] [blame] | 199 | if (filter) { |
| 200 | struct string_list_item *item; |
| 201 | for_each_string_list_item(item, filter->exclude_ref_pattern) { |
| 202 | normalize_glob_ref(item, NULL, item->string); |
| 203 | } |
| 204 | for_each_string_list_item(item, filter->include_ref_pattern) { |
| 205 | normalize_glob_ref(item, NULL, item->string); |
| 206 | } |
| Derrick Stolee | a6be5e6 | 2020-04-16 14:15:49 | [diff] [blame] | 207 | for_each_string_list_item(item, filter->exclude_ref_config_pattern) { |
| 208 | normalize_glob_ref(item, NULL, item->string); |
| 209 | } |
| Rafael Ascensão | 65516f5 | 2017-11-21 21:33:41 | [diff] [blame] | 210 | } |
| Junio C Hamano | 76c61fb | 2015-05-13 17:25:18 | [diff] [blame] | 211 | decoration_loaded = 1; |
| 212 | decoration_flags = flags; |
| Rafael Ascensão | 65516f5 | 2017-11-21 21:33:41 | [diff] [blame] | 213 | for_each_ref(add_ref_decoration, filter); |
| 214 | head_ref(add_ref_decoration, filter); |
| 215 | for_each_commit_graft(add_graft_decoration, filter); |
| René Scharfe | cab4feb | 2008-09-04 21:39:21 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 219 | static void show_parents(struct commit *commit, int abbrev, FILE *file) |
| Linus Torvalds | c8c893c | 2006-05-03 14:59:00 | [diff] [blame] | 220 | { |
| 221 | struct commit_list *p; |
| 222 | for (p = commit->parents; p ; p = p->next) { |
| 223 | struct commit *parent = p->item; |
| brian m. carlson | aab9583 | 2018-03-12 02:27:30 | [diff] [blame] | 224 | fprintf(file, " %s", find_unique_abbrev(&parent->object.oid, abbrev)); |
| Linus Torvalds | c8c893c | 2006-05-03 14:59:00 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| Jay Soffian | 91b849b | 2011-10-04 14:02:03 | [diff] [blame] | 228 | static void show_children(struct rev_info *opt, struct commit *commit, int abbrev) |
| 229 | { |
| 230 | struct commit_list *p = lookup_decoration(&opt->children, &commit->object); |
| 231 | for ( ; p; p = p->next) { |
| brian m. carlson | aab9583 | 2018-03-12 02:27:30 | [diff] [blame] | 232 | fprintf(opt->diffopt.file, " %s", find_unique_abbrev(&p->item->object.oid, abbrev)); |
| Jay Soffian | 91b849b | 2011-10-04 14:02:03 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 236 | /* |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 237 | * Do we have HEAD in the output, and also the branch it points at? |
| 238 | * If so, find that decoration entry for that current branch. |
| 239 | */ |
| 240 | static const struct name_decoration *current_pointed_by_HEAD(const struct name_decoration *decoration) |
| 241 | { |
| 242 | const struct name_decoration *list, *head = NULL; |
| 243 | const char *branch_name = NULL; |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 244 | int rru_flags; |
| 245 | |
| 246 | /* First find HEAD */ |
| 247 | for (list = decoration; list; list = list->next) |
| 248 | if (list->type == DECORATION_REF_HEAD) { |
| 249 | head = list; |
| 250 | break; |
| 251 | } |
| 252 | if (!head) |
| 253 | return NULL; |
| 254 | |
| 255 | /* Now resolve and find the matching current branch */ |
| René Scharfe | 744c040 | 2017-09-23 09:45:04 | [diff] [blame] | 256 | branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags); |
| Jeff King | d79be49 | 2017-10-19 17:49:01 | [diff] [blame] | 257 | if (!branch_name || !(rru_flags & REF_ISSYMREF)) |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 258 | return NULL; |
| Junio C Hamano | 76c61fb | 2015-05-13 17:25:18 | [diff] [blame] | 259 | |
| Junio C Hamano | 429ad20 | 2015-05-13 19:40:35 | [diff] [blame] | 260 | if (!starts_with(branch_name, "refs/")) |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 261 | return NULL; |
| 262 | |
| 263 | /* OK, do we have that ref in the list? */ |
| 264 | for (list = decoration; list; list = list->next) |
| 265 | if ((list->type == DECORATION_REF_LOCAL) && |
| 266 | !strcmp(branch_name, list->name)) { |
| 267 | return list; |
| 268 | } |
| 269 | |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| Junio C Hamano | 429ad20 | 2015-05-13 19:40:35 | [diff] [blame] | 273 | static void show_name(struct strbuf *sb, const struct name_decoration *decoration) |
| 274 | { |
| 275 | if (decoration_flags == DECORATE_SHORT_REFS) |
| 276 | strbuf_addstr(sb, prettify_refname(decoration->name)); |
| 277 | else |
| 278 | strbuf_addstr(sb, decoration->name); |
| 279 | } |
| 280 | |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 281 | /* |
| Harry Jeffery | 9271095 | 2014-09-18 20:53:53 | [diff] [blame] | 282 | * The caller makes sure there is no funny color before calling. |
| 283 | * format_decorations_extended makes sure the same after return. |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 284 | */ |
| Harry Jeffery | 9271095 | 2014-09-18 20:53:53 | [diff] [blame] | 285 | void format_decorations_extended(struct strbuf *sb, |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 286 | const struct commit *commit, |
| Harry Jeffery | 9271095 | 2014-09-18 20:53:53 | [diff] [blame] | 287 | int use_color, |
| 288 | const char *prefix, |
| 289 | const char *separator, |
| 290 | const char *suffix) |
| Linus Torvalds | ca135e7 | 2007-04-16 23:05:10 | [diff] [blame] | 291 | { |
| Jeff King | 2608c24 | 2014-08-26 10:23:54 | [diff] [blame] | 292 | const struct name_decoration *decoration; |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 293 | const struct name_decoration *current_and_HEAD; |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 294 | const char *color_commit = |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 295 | diff_get_color(use_color, DIFF_COMMIT); |
| Nazri Ramliy | 67a4b58 | 2010-06-19 01:37:35 | [diff] [blame] | 296 | const char *color_reset = |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 297 | decorate_get_color(use_color, DECORATION_NONE); |
| Linus Torvalds | ca135e7 | 2007-04-16 23:05:10 | [diff] [blame] | 298 | |
| Jeff King | 2608c24 | 2014-08-26 10:23:54 | [diff] [blame] | 299 | decoration = get_name_decoration(&commit->object); |
| Linus Torvalds | ca135e7 | 2007-04-16 23:05:10 | [diff] [blame] | 300 | if (!decoration) |
| 301 | return; |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 302 | |
| 303 | current_and_HEAD = current_pointed_by_HEAD(decoration); |
| Linus Torvalds | ca135e7 | 2007-04-16 23:05:10 | [diff] [blame] | 304 | while (decoration) { |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 305 | /* |
| 306 | * When both current and HEAD are there, only |
| 307 | * show HEAD->current where HEAD would have |
| 308 | * appeared, skipping the entry for current. |
| 309 | */ |
| 310 | if (decoration != current_and_HEAD) { |
| 311 | strbuf_addstr(sb, color_commit); |
| 312 | strbuf_addstr(sb, prefix); |
| 313 | strbuf_addstr(sb, color_reset); |
| 314 | strbuf_addstr(sb, decorate_get_color(use_color, decoration->type)); |
| 315 | if (decoration->type == DECORATION_REF_TAG) |
| 316 | strbuf_addstr(sb, "tag: "); |
| 317 | |
| Junio C Hamano | 429ad20 | 2015-05-13 19:40:35 | [diff] [blame] | 318 | show_name(sb, decoration); |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 319 | |
| 320 | if (current_and_HEAD && |
| 321 | decoration->type == DECORATION_REF_HEAD) { |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 322 | strbuf_addstr(sb, " -> "); |
| 323 | strbuf_addstr(sb, color_reset); |
| 324 | strbuf_addstr(sb, decorate_get_color(use_color, current_and_HEAD->type)); |
| Junio C Hamano | 429ad20 | 2015-05-13 19:40:35 | [diff] [blame] | 325 | show_name(sb, current_and_HEAD); |
| Junio C Hamano | 51ff0f2 | 2015-03-10 13:53:21 | [diff] [blame] | 326 | } |
| 327 | strbuf_addstr(sb, color_reset); |
| 328 | |
| 329 | prefix = separator; |
| 330 | } |
| Linus Torvalds | ca135e7 | 2007-04-16 23:05:10 | [diff] [blame] | 331 | decoration = decoration->next; |
| 332 | } |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 333 | strbuf_addstr(sb, color_commit); |
| Harry Jeffery | 9271095 | 2014-09-18 20:53:53 | [diff] [blame] | 334 | strbuf_addstr(sb, suffix); |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 335 | strbuf_addstr(sb, color_reset); |
| 336 | } |
| 337 | |
| 338 | void show_decorations(struct rev_info *opt, struct commit *commit) |
| 339 | { |
| 340 | struct strbuf sb = STRBUF_INIT; |
| 341 | |
| Nguyễn Thái Ngọc Duy | 87be252 | 2018-05-19 05:28:24 | [diff] [blame] | 342 | if (opt->sources) { |
| 343 | char **slot = revision_sources_peek(opt->sources, commit); |
| 344 | |
| 345 | if (slot && *slot) |
| 346 | fprintf(opt->diffopt.file, "\t%s", *slot); |
| 347 | } |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 348 | if (!opt->show_decorations) |
| 349 | return; |
| 350 | format_decorations(&sb, commit, opt->diffopt.use_color); |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 351 | fputs(sb.buf, opt->diffopt.file); |
| Nguyễn Thái Ngọc Duy | 9d3f002 | 2013-04-18 23:08:43 | [diff] [blame] | 352 | strbuf_release(&sb); |
| Linus Torvalds | ca135e7 | 2007-04-16 23:05:10 | [diff] [blame] | 353 | } |
| 354 | |
| Johannes Schindelin | e00de24 | 2007-02-09 00:43:54 | [diff] [blame] | 355 | static unsigned int digits_in_number(unsigned int number) |
| 356 | { |
| 357 | unsigned int i = 10, result = 1; |
| 358 | while (i <= number) { |
| 359 | i *= 10; |
| 360 | result++; |
| 361 | } |
| 362 | return result; |
| 363 | } |
| 364 | |
| Junio C Hamano | d28b5d4 | 2012-12-22 06:06:01 | [diff] [blame] | 365 | void fmt_output_subject(struct strbuf *filename, |
| 366 | const char *subject, |
| Junio C Hamano | 021f2f4 | 2012-12-22 07:26:16 | [diff] [blame] | 367 | struct rev_info *info) |
| Stephen Boyd | 6fa8e62 | 2009-03-23 02:14:04 | [diff] [blame] | 368 | { |
| Junio C Hamano | 021f2f4 | 2012-12-22 07:26:16 | [diff] [blame] | 369 | const char *suffix = info->patch_suffix; |
| 370 | int nr = info->nr; |
| Junio C Hamano | d28b5d4 | 2012-12-22 06:06:01 | [diff] [blame] | 371 | int start_len = filename->len; |
| Junio C Hamano | 3baf58b | 2020-11-06 21:56:24 | [diff] [blame] | 372 | int max_len = start_len + info->patch_name_max - (strlen(suffix) + 1); |
| Stephen Boyd | 6fa8e62 | 2009-03-23 02:14:04 | [diff] [blame] | 373 | |
| ZheNing Hu | db91988 | 2021-03-23 11:12:25 | [diff] [blame] | 374 | if (info->reroll_count) { |
| 375 | struct strbuf temp = STRBUF_INIT; |
| 376 | |
| 377 | strbuf_addf(&temp, "v%s", info->reroll_count); |
| 378 | format_sanitized_subject(filename, temp.buf, temp.len); |
| 379 | strbuf_addstr(filename, "-"); |
| 380 | strbuf_release(&temp); |
| 381 | } |
| Junio C Hamano | d28b5d4 | 2012-12-22 06:06:01 | [diff] [blame] | 382 | strbuf_addf(filename, "%04d-%s", nr, subject); |
| Christian Couder | b09b868 | 2009-03-27 00:13:01 | [diff] [blame] | 383 | |
| Junio C Hamano | d28b5d4 | 2012-12-22 06:06:01 | [diff] [blame] | 384 | if (max_len < filename->len) |
| 385 | strbuf_setlen(filename, max_len); |
| 386 | strbuf_addstr(filename, suffix); |
| 387 | } |
| Jeff King | a21c2f9 | 2012-05-21 23:10:32 | [diff] [blame] | 388 | |
| Junio C Hamano | d28b5d4 | 2012-12-22 06:06:01 | [diff] [blame] | 389 | void fmt_output_commit(struct strbuf *filename, |
| 390 | struct commit *commit, |
| 391 | struct rev_info *info) |
| 392 | { |
| 393 | struct pretty_print_context ctx = {0}; |
| 394 | struct strbuf subject = STRBUF_INIT; |
| 395 | |
| 396 | format_commit_message(commit, "%f", &subject, &ctx); |
| 397 | fmt_output_subject(filename, subject.buf, info); |
| 398 | strbuf_release(&subject); |
| Stephen Boyd | 6fa8e62 | 2009-03-23 02:14:04 | [diff] [blame] | 399 | } |
| 400 | |
| René Scharfe | 8ffc8dc | 2017-03-01 11:36:38 | [diff] [blame] | 401 | void fmt_output_email_subject(struct strbuf *sb, struct rev_info *opt) |
| 402 | { |
| 403 | if (opt->total > 0) { |
| 404 | strbuf_addf(sb, "Subject: [%s%s%0*d/%d] ", |
| 405 | opt->subject_prefix, |
| 406 | *opt->subject_prefix ? " " : "", |
| 407 | digits_in_number(opt->total), |
| 408 | opt->nr, opt->total); |
| 409 | } else if (opt->total == 0 && opt->subject_prefix && *opt->subject_prefix) { |
| 410 | strbuf_addf(sb, "Subject: [%s] ", |
| 411 | opt->subject_prefix); |
| 412 | } else { |
| 413 | strbuf_addstr(sb, "Subject: "); |
| 414 | } |
| 415 | } |
| 416 | |
| Stephen Boyd | 108dab2 | 2009-03-23 02:14:05 | [diff] [blame] | 417 | void log_write_email_headers(struct rev_info *opt, struct commit *commit, |
| Junio C Hamano | 267123b | 2008-03-15 07:09:20 | [diff] [blame] | 418 | const char **extra_headers_p, |
| brian m. carlson | 50cd54e | 2018-05-02 02:20:52 | [diff] [blame] | 419 | int *need_8bit_cte_p, |
| 420 | int maybe_multipart) |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 421 | { |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 422 | const char *extra_headers = opt->extra_headers; |
| brian m. carlson | 3a30aa1 | 2015-12-15 01:52:04 | [diff] [blame] | 423 | const char *name = oid_to_hex(opt->zero_commit ? |
| brian m. carlson | 1422844 | 2021-04-26 01:02:56 | [diff] [blame] | 424 | null_oid() : &commit->object.oid); |
| Junio C Hamano | 267123b | 2008-03-15 07:09:20 | [diff] [blame] | 425 | |
| 426 | *need_8bit_cte_p = 0; /* unknown */ |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 427 | |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 428 | fprintf(opt->diffopt.file, "From %s Mon Sep 17 00:00:00 2001\n", name); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 429 | graph_show_oneline(opt->graph); |
| 430 | if (opt->message_id) { |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 431 | fprintf(opt->diffopt.file, "Message-Id: <%s>\n", opt->message_id); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 432 | graph_show_oneline(opt->graph); |
| 433 | } |
| Thomas Rast | b079c50 | 2009-02-19 21:26:31 | [diff] [blame] | 434 | if (opt->ref_message_ids && opt->ref_message_ids->nr > 0) { |
| 435 | int i, n; |
| 436 | n = opt->ref_message_ids->nr; |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 437 | fprintf(opt->diffopt.file, "In-Reply-To: <%s>\n", opt->ref_message_ids->items[n-1].string); |
| Thomas Rast | b079c50 | 2009-02-19 21:26:31 | [diff] [blame] | 438 | for (i = 0; i < n; i++) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 439 | fprintf(opt->diffopt.file, "%s<%s>\n", (i > 0 ? "\t" : "References: "), |
| Thomas Rast | b079c50 | 2009-02-19 21:26:31 | [diff] [blame] | 440 | opt->ref_message_ids->items[i].string); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 441 | graph_show_oneline(opt->graph); |
| 442 | } |
| brian m. carlson | 50cd54e | 2018-05-02 02:20:52 | [diff] [blame] | 443 | if (opt->mime_boundary && maybe_multipart) { |
| Jeff King | d50b69b | 2018-05-19 01:57:44 | [diff] [blame] | 444 | static struct strbuf subject_buffer = STRBUF_INIT; |
| 445 | static struct strbuf buffer = STRBUF_INIT; |
| Stephen Boyd | 108dab2 | 2009-03-23 02:14:05 | [diff] [blame] | 446 | struct strbuf filename = STRBUF_INIT; |
| Junio C Hamano | 267123b | 2008-03-15 07:09:20 | [diff] [blame] | 447 | *need_8bit_cte_p = -1; /* NEVER */ |
| Jeff King | d50b69b | 2018-05-19 01:57:44 | [diff] [blame] | 448 | |
| 449 | strbuf_reset(&subject_buffer); |
| 450 | strbuf_reset(&buffer); |
| 451 | |
| 452 | strbuf_addf(&subject_buffer, |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 453 | "%s" |
| 454 | "MIME-Version: 1.0\n" |
| 455 | "Content-Type: multipart/mixed;" |
| 456 | " boundary=\"%s%s\"\n" |
| 457 | "\n" |
| 458 | "This is a multi-part message in MIME " |
| 459 | "format.\n" |
| 460 | "--%s%s\n" |
| 461 | "Content-Type: text/plain; " |
| 462 | "charset=UTF-8; format=fixed\n" |
| 463 | "Content-Transfer-Encoding: 8bit\n\n", |
| 464 | extra_headers ? extra_headers : "", |
| 465 | mime_boundary_leader, opt->mime_boundary, |
| 466 | mime_boundary_leader, opt->mime_boundary); |
| Jeff King | d50b69b | 2018-05-19 01:57:44 | [diff] [blame] | 467 | extra_headers = subject_buffer.buf; |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 468 | |
| Junio C Hamano | 38ec23a | 2012-12-22 05:39:37 | [diff] [blame] | 469 | if (opt->numbered_files) |
| 470 | strbuf_addf(&filename, "%d", opt->nr); |
| 471 | else |
| Junio C Hamano | d28b5d4 | 2012-12-22 06:06:01 | [diff] [blame] | 472 | fmt_output_commit(&filename, commit, opt); |
| Jeff King | d50b69b | 2018-05-19 01:57:44 | [diff] [blame] | 473 | strbuf_addf(&buffer, |
| Kevin Ballard | 6b2fbaa | 2008-07-30 05:49:33 | [diff] [blame] | 474 | "\n--%s%s\n" |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 475 | "Content-Type: text/x-patch;" |
| Stephen Boyd | 108dab2 | 2009-03-23 02:14:05 | [diff] [blame] | 476 | " name=\"%s\"\n" |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 477 | "Content-Transfer-Encoding: 8bit\n" |
| 478 | "Content-Disposition: %s;" |
| Stephen Boyd | 108dab2 | 2009-03-23 02:14:05 | [diff] [blame] | 479 | " filename=\"%s\"\n\n", |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 480 | mime_boundary_leader, opt->mime_boundary, |
| Stephen Boyd | 108dab2 | 2009-03-23 02:14:05 | [diff] [blame] | 481 | filename.buf, |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 482 | opt->no_inline ? "attachment" : "inline", |
| Stephen Boyd | 108dab2 | 2009-03-23 02:14:05 | [diff] [blame] | 483 | filename.buf); |
| Jeff King | d50b69b | 2018-05-19 01:57:44 | [diff] [blame] | 484 | opt->diffopt.stat_sep = buffer.buf; |
| Stephen Boyd | 108dab2 | 2009-03-23 02:14:05 | [diff] [blame] | 485 | strbuf_release(&filename); |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 486 | } |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 487 | *extra_headers_p = extra_headers; |
| 488 | } |
| 489 | |
| Junio C Hamano | c6b3ec4 | 2012-01-04 21:48:45 | [diff] [blame] | 490 | static void show_sig_lines(struct rev_info *opt, int status, const char *bol) |
| 491 | { |
| 492 | const char *color, *reset, *eol; |
| 493 | |
| 494 | color = diff_get_color_opt(&opt->diffopt, |
| 495 | status ? DIFF_WHITESPACE : DIFF_FRAGINFO); |
| 496 | reset = diff_get_color_opt(&opt->diffopt, DIFF_RESET); |
| 497 | while (*bol) { |
| 498 | eol = strchrnul(bol, '\n'); |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 499 | fprintf(opt->diffopt.file, "%s%.*s%s%s", color, (int)(eol - bol), bol, reset, |
| Junio C Hamano | c6b3ec4 | 2012-01-04 21:48:45 | [diff] [blame] | 500 | *eol ? "\n" : ""); |
| Zoltan Klinger | cf3983d | 2014-07-09 02:10:21 | [diff] [blame] | 501 | graph_show_oneline(opt->graph); |
| Junio C Hamano | c6b3ec4 | 2012-01-04 21:48:45 | [diff] [blame] | 502 | bol = (*eol) ? (eol + 1) : eol; |
| 503 | } |
| 504 | } |
| 505 | |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 506 | static void show_signature(struct rev_info *opt, struct commit *commit) |
| 507 | { |
| 508 | struct strbuf payload = STRBUF_INIT; |
| 509 | struct strbuf signature = STRBUF_INIT; |
| Hans Jerry Illikainen | 6794898 | 2020-03-04 11:48:04 | [diff] [blame] | 510 | struct signature_check sigc = { 0 }; |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 511 | int status; |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 512 | |
| brian m. carlson | 1fb5cf0 | 2021-01-18 23:49:11 | [diff] [blame] | 513 | if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0) |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 514 | goto out; |
| 515 | |
| Fabian Stelzer | 4bbf378 | 2021-12-09 08:52:46 | [diff] [blame] | 516 | sigc.payload_type = SIGNATURE_PAYLOAD_COMMIT; |
| Fabian Stelzer | 0276943 | 2021-12-09 08:52:43 | [diff] [blame] | 517 | sigc.payload = strbuf_detach(&payload, &sigc.payload_len); |
| 518 | status = check_signature(&sigc, signature.buf, signature.len); |
| Fabian Stelzer | b5726a5 | 2021-09-10 20:07:34 | [diff] [blame] | 519 | if (status && !sigc.output) |
| Hans Jerry Illikainen | 6794898 | 2020-03-04 11:48:04 | [diff] [blame] | 520 | show_sig_lines(opt, status, "No signature\n"); |
| 521 | else |
| Fabian Stelzer | b5726a5 | 2021-09-10 20:07:34 | [diff] [blame] | 522 | show_sig_lines(opt, status, sigc.output); |
| Hans Jerry Illikainen | 6794898 | 2020-03-04 11:48:04 | [diff] [blame] | 523 | signature_check_clear(&sigc); |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 524 | |
| 525 | out: |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 526 | strbuf_release(&payload); |
| 527 | strbuf_release(&signature); |
| 528 | } |
| 529 | |
| brian m. carlson | a92ea68 | 2017-05-06 22:10:18 | [diff] [blame] | 530 | static int which_parent(const struct object_id *oid, const struct commit *commit) |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 531 | { |
| 532 | int nth; |
| 533 | const struct commit_list *parent; |
| 534 | |
| 535 | for (nth = 0, parent = commit->parents; parent; parent = parent->next) { |
| Jeff King | 4a7e27e | 2018-08-28 21:22:40 | [diff] [blame] | 536 | if (oideq(&parent->item->object.oid, oid)) |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 537 | return nth; |
| 538 | nth++; |
| 539 | } |
| 540 | return -1; |
| 541 | } |
| 542 | |
| Junio C Hamano | d041ffa | 2012-01-05 00:23:12 | [diff] [blame] | 543 | static int is_common_merge(const struct commit *commit) |
| 544 | { |
| 545 | return (commit->parents |
| 546 | && commit->parents->next |
| 547 | && !commit->parents->next->next); |
| 548 | } |
| 549 | |
| Johannes Schindelin | fef461e | 2018-04-25 09:54:04 | [diff] [blame] | 550 | static int show_one_mergetag(struct commit *commit, |
| 551 | struct commit_extra_header *extra, |
| 552 | void *data) |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 553 | { |
| Christian Couder | 063da62 | 2014-07-07 06:35:37 | [diff] [blame] | 554 | struct rev_info *opt = (struct rev_info *)data; |
| brian m. carlson | a92ea68 | 2017-05-06 22:10:18 | [diff] [blame] | 555 | struct object_id oid; |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 556 | struct tag *tag; |
| 557 | struct strbuf verify_message; |
| Hans Jerry Illikainen | 6794898 | 2020-03-04 11:48:04 | [diff] [blame] | 558 | struct signature_check sigc = { 0 }; |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 559 | int status, nth; |
| brian m. carlson | 482c119 | 2021-02-11 02:08:03 | [diff] [blame] | 560 | struct strbuf payload = STRBUF_INIT; |
| 561 | struct strbuf signature = STRBUF_INIT; |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 562 | |
| Matheus Tavares | 2dcde20 | 2020-01-30 20:32:22 | [diff] [blame] | 563 | hash_object_file(the_hash_algo, extra->value, extra->len, |
| 564 | type_name(OBJ_TAG), &oid); |
| Stefan Beller | ce71efb | 2018-06-29 01:22:03 | [diff] [blame] | 565 | tag = lookup_tag(the_repository, &oid); |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 566 | if (!tag) |
| Johannes Schindelin | fef461e | 2018-04-25 09:54:04 | [diff] [blame] | 567 | return -1; /* error message already given */ |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 568 | |
| 569 | strbuf_init(&verify_message, 256); |
| Stefan Beller | 0e740fe | 2018-06-29 01:22:04 | [diff] [blame] | 570 | if (parse_tag_buffer(the_repository, tag, extra->value, extra->len)) |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 571 | strbuf_addstr(&verify_message, "malformed mergetag\n"); |
| Junio C Hamano | d041ffa | 2012-01-05 00:23:12 | [diff] [blame] | 572 | else if (is_common_merge(commit) && |
| Jeff King | 4a7e27e | 2018-08-28 21:22:40 | [diff] [blame] | 573 | oideq(&tag->tagged->oid, |
| 574 | &commit->parents->next->item->object.oid)) |
| Junio C Hamano | d041ffa | 2012-01-05 00:23:12 | [diff] [blame] | 575 | strbuf_addf(&verify_message, |
| 576 | "merged tag '%s'\n", tag->tag); |
| brian m. carlson | a92ea68 | 2017-05-06 22:10:18 | [diff] [blame] | 577 | else if ((nth = which_parent(&tag->tagged->oid, commit)) < 0) |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 578 | strbuf_addf(&verify_message, "tag %s names a non-parent %s\n", |
| Harald van Dijk | 237a281 | 2020-02-29 13:07:57 | [diff] [blame] | 579 | tag->tag, oid_to_hex(&tag->tagged->oid)); |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 580 | else |
| 581 | strbuf_addf(&verify_message, |
| 582 | "parent #%d, tagged '%s'\n", nth + 1, tag->tag); |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 583 | |
| Michael J Gruber | 1315093 | 2013-02-14 16:04:43 | [diff] [blame] | 584 | status = -1; |
| brian m. carlson | 482c119 | 2021-02-11 02:08:03 | [diff] [blame] | 585 | if (parse_signature(extra->value, extra->len, &payload, &signature)) { |
| Michael J Gruber | 42c55ce | 2014-06-27 13:18:36 | [diff] [blame] | 586 | /* could have a good signature */ |
| Fabian Stelzer | 4bbf378 | 2021-12-09 08:52:46 | [diff] [blame] | 587 | sigc.payload_type = SIGNATURE_PAYLOAD_TAG; |
| Fabian Stelzer | 0276943 | 2021-12-09 08:52:43 | [diff] [blame] | 588 | sigc.payload = strbuf_detach(&payload, &sigc.payload_len); |
| 589 | status = check_signature(&sigc, signature.buf, signature.len); |
| Fabian Stelzer | b5726a5 | 2021-09-10 20:07:34 | [diff] [blame] | 590 | if (sigc.output) |
| 591 | strbuf_addstr(&verify_message, sigc.output); |
| Hans Jerry Illikainen | 6794898 | 2020-03-04 11:48:04 | [diff] [blame] | 592 | else |
| Michael J Gruber | 42c55ce | 2014-06-27 13:18:36 | [diff] [blame] | 593 | strbuf_addstr(&verify_message, "No signature\n"); |
| Hans Jerry Illikainen | 6794898 | 2020-03-04 11:48:04 | [diff] [blame] | 594 | signature_check_clear(&sigc); |
| Michael J Gruber | 42c55ce | 2014-06-27 13:18:36 | [diff] [blame] | 595 | /* otherwise we couldn't verify, which is shown as bad */ |
| 596 | } |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 597 | |
| 598 | show_sig_lines(opt, status, verify_message.buf); |
| 599 | strbuf_release(&verify_message); |
| brian m. carlson | 482c119 | 2021-02-11 02:08:03 | [diff] [blame] | 600 | strbuf_release(&payload); |
| 601 | strbuf_release(&signature); |
| Johannes Schindelin | fef461e | 2018-04-25 09:54:04 | [diff] [blame] | 602 | return 0; |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 603 | } |
| 604 | |
| Johannes Schindelin | fef461e | 2018-04-25 09:54:04 | [diff] [blame] | 605 | static int show_mergetag(struct rev_info *opt, struct commit *commit) |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 606 | { |
| Johannes Schindelin | fef461e | 2018-04-25 09:54:04 | [diff] [blame] | 607 | return for_each_mergetag(show_one_mergetag, commit, opt); |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 608 | } |
| 609 | |
| Eric Sunshine | 3fcc7a2 | 2018-07-22 09:57:08 | [diff] [blame] | 610 | static void next_commentary_block(struct rev_info *opt, struct strbuf *sb) |
| 611 | { |
| 612 | const char *x = opt->shown_dashes ? "\n" : "---\n"; |
| 613 | if (sb) |
| 614 | strbuf_addstr(sb, x); |
| 615 | else |
| 616 | fputs(x, opt->diffopt.file); |
| 617 | opt->shown_dashes = 1; |
| 618 | } |
| 619 | |
| Adam Simpkins | 0286565 | 2008-04-29 08:32:59 | [diff] [blame] | 620 | void show_log(struct rev_info *opt) |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 621 | { |
| Brandon Casey | f285a2d | 2008-10-09 19:12:12 | [diff] [blame] | 622 | struct strbuf msgbuf = STRBUF_INIT; |
| Timo Hirvonen | 39bc9a6 | 2006-06-25 10:54:14 | [diff] [blame] | 623 | struct log_info *log = opt->loginfo; |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 624 | struct commit *commit = log->commit, *parent = log->parent; |
| brian m. carlson | 2ed1960 | 2018-07-16 01:28:06 | [diff] [blame] | 625 | int abbrev_commit = opt->abbrev_commit ? opt->abbrev : the_hash_algo->hexsz; |
| Thomas Rast | dd2e794 | 2009-10-19 15:48:08 | [diff] [blame] | 626 | const char *extra_headers = opt->extra_headers; |
| 627 | struct pretty_print_context ctx = {0}; |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 628 | |
| 629 | opt->loginfo = NULL; |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 630 | if (!opt->verbose_header) { |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 631 | graph_show_commit(opt->graph); |
| 632 | |
| Michael J Gruber | 1df2d65 | 2011-03-07 12:31:39 | [diff] [blame] | 633 | if (!opt->graph) |
| Michael J Gruber | b1b4755 | 2011-03-10 14:45:03 | [diff] [blame] | 634 | put_revision_mark(opt, commit); |
| brian m. carlson | aab9583 | 2018-03-12 02:27:30 | [diff] [blame] | 635 | fputs(find_unique_abbrev(&commit->object.oid, abbrev_commit), opt->diffopt.file); |
| Adam Simpkins | 885cf80 | 2008-05-04 10:36:52 | [diff] [blame] | 636 | if (opt->print_parents) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 637 | show_parents(commit, abbrev_commit, opt->diffopt.file); |
| Jay Soffian | 91b849b | 2011-10-04 14:02:03 | [diff] [blame] | 638 | if (opt->children.name) |
| 639 | show_children(opt, commit, abbrev_commit); |
| Linus Torvalds | 0f3a290 | 2008-10-27 19:51:59 | [diff] [blame] | 640 | show_decorations(opt, commit); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 641 | if (opt->graph && !graph_is_commit_finished(opt->graph)) { |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 642 | putc('\n', opt->diffopt.file); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 643 | graph_show_remainder(opt->graph); |
| 644 | } |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 645 | putc(opt->diffopt.line_termination, opt->diffopt.file); |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
| 649 | /* |
| Jeff King | 8715227 | 2009-07-01 07:26:28 | [diff] [blame] | 650 | * If use_terminator is set, we already handled any record termination |
| 651 | * at the end of the last record. |
| Adam Simpkins | 0286565 | 2008-04-29 08:32:59 | [diff] [blame] | 652 | * Otherwise, add a diffopt.line_termination character before all |
| 653 | * entries but the first. (IOW, as a separator between entries) |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 654 | */ |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 655 | if (opt->shown_one && !opt->use_terminator) { |
| 656 | /* |
| 657 | * If entries are separated by a newline, the output |
| 658 | * should look human-readable. If the last entry ended |
| 659 | * with a newline, print the graph output before this |
| 660 | * newline. Otherwise it will end up as a completely blank |
| 661 | * line and will look like a gap in the graph. |
| 662 | * |
| 663 | * If the entry separator is not a newline, the output is |
| 664 | * primarily intended for programmatic consumption, and we |
| 665 | * never want the extra graph output before the entry |
| 666 | * separator. |
| 667 | */ |
| 668 | if (opt->diffopt.line_termination == '\n' && |
| 669 | !opt->missing_newline) |
| 670 | graph_show_padding(opt->graph); |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 671 | putc(opt->diffopt.line_termination, opt->diffopt.file); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 672 | } |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 673 | opt->shown_one = 1; |
| 674 | |
| 675 | /* |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 676 | * If the history graph was requested, |
| 677 | * print the graph, up to this commit's line |
| 678 | */ |
| 679 | graph_show_commit(opt->graph); |
| 680 | |
| 681 | /* |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 682 | * Print header line of header.. |
| 683 | */ |
| Junio C Hamano | 3eefc18 | 2006-04-18 23:45:27 | [diff] [blame] | 684 | |
| Eric Wong | 9f23e04 | 2016-06-05 04:46:39 | [diff] [blame] | 685 | if (cmit_fmt_is_mail(opt->commit_format)) { |
| René Scharfe | 6d167fd | 2017-03-01 11:37:07 | [diff] [blame] | 686 | log_write_email_headers(opt, commit, &extra_headers, |
| brian m. carlson | 50cd54e | 2018-05-02 02:20:52 | [diff] [blame] | 687 | &ctx.need_8bit_cte, 1); |
| René Scharfe | 6d167fd | 2017-03-01 11:37:07 | [diff] [blame] | 688 | ctx.rev = opt; |
| 689 | ctx.print_email_subject = 1; |
| Johannes Schindelin | e52a5de | 2007-02-23 00:35:03 | [diff] [blame] | 690 | } else if (opt->commit_format != CMIT_FMT_USERFORMAT) { |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 691 | fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file); |
| Junio C Hamano | 74bd902 | 2006-12-16 23:31:25 | [diff] [blame] | 692 | if (opt->commit_format != CMIT_FMT_ONELINE) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 693 | fputs("commit ", opt->diffopt.file); |
| Adam Simpkins | 7528f27 | 2008-05-25 07:07:21 | [diff] [blame] | 694 | |
| Michael J Gruber | 1df2d65 | 2011-03-07 12:31:39 | [diff] [blame] | 695 | if (!opt->graph) |
| Michael J Gruber | b1b4755 | 2011-03-10 14:45:03 | [diff] [blame] | 696 | put_revision_mark(opt, commit); |
| brian m. carlson | aab9583 | 2018-03-12 02:27:30 | [diff] [blame] | 697 | fputs(find_unique_abbrev(&commit->object.oid, |
| 698 | abbrev_commit), |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 699 | opt->diffopt.file); |
| Adam Simpkins | 885cf80 | 2008-05-04 10:36:52 | [diff] [blame] | 700 | if (opt->print_parents) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 701 | show_parents(commit, abbrev_commit, opt->diffopt.file); |
| Jay Soffian | 91b849b | 2011-10-04 14:02:03 | [diff] [blame] | 702 | if (opt->children.name) |
| 703 | show_children(opt, commit, abbrev_commit); |
| Junio C Hamano | 73f0a15 | 2006-05-24 19:19:47 | [diff] [blame] | 704 | if (parent) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 705 | fprintf(opt->diffopt.file, " (from %s)", |
| brian m. carlson | aab9583 | 2018-03-12 02:27:30 | [diff] [blame] | 706 | find_unique_abbrev(&parent->object.oid, abbrev_commit)); |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 707 | fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file); |
| Linus Torvalds | 0f3a290 | 2008-10-27 19:51:59 | [diff] [blame] | 708 | show_decorations(opt, commit); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 709 | if (opt->commit_format == CMIT_FMT_ONELINE) { |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 710 | putc(' ', opt->diffopt.file); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 711 | } else { |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 712 | putc('\n', opt->diffopt.file); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 713 | graph_show_oneline(opt->graph); |
| 714 | } |
| Nicolas Pitre | 903b45f | 2007-01-28 03:40:36 | [diff] [blame] | 715 | if (opt->reflog_info) { |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 716 | /* |
| 717 | * setup_revisions() ensures that opt->reflog_info |
| 718 | * and opt->graph cannot both be set, |
| 719 | * so we don't need to worry about printing the |
| 720 | * graph info here. |
| 721 | */ |
| Junio C Hamano | 4d12a47 | 2007-01-20 08:51:41 | [diff] [blame] | 722 | show_reflog_message(opt->reflog_info, |
| Junio C Hamano | 55ccf85 | 2012-05-07 21:11:32 | [diff] [blame] | 723 | opt->commit_format == CMIT_FMT_ONELINE, |
| Jeff King | a5481a6 | 2015-06-25 16:55:02 | [diff] [blame] | 724 | &opt->date_mode, |
| Junio C Hamano | 55ccf85 | 2012-05-07 21:11:32 | [diff] [blame] | 725 | opt->date_mode_explicit); |
| Adam Simpkins | 0286565 | 2008-04-29 08:32:59 | [diff] [blame] | 726 | if (opt->commit_format == CMIT_FMT_ONELINE) |
| Nicolas Pitre | 903b45f | 2007-01-28 03:40:36 | [diff] [blame] | 727 | return; |
| Nicolas Pitre | 903b45f | 2007-01-28 03:40:36 | [diff] [blame] | 728 | } |
| Junio C Hamano | 3eefc18 | 2006-04-18 23:45:27 | [diff] [blame] | 729 | } |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 730 | |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 731 | if (opt->show_signature) { |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 732 | show_signature(opt, commit); |
| Junio C Hamano | 824958e | 2012-01-04 21:51:28 | [diff] [blame] | 733 | show_mergetag(opt, commit); |
| 734 | } |
| Junio C Hamano | 0c37f1f | 2011-10-18 22:53:23 | [diff] [blame] | 735 | |
| Junio C Hamano | ddf333f | 2012-10-18 01:51:47 | [diff] [blame] | 736 | if (opt->show_notes) { |
| 737 | int raw; |
| 738 | struct strbuf notebuf = STRBUF_INIT; |
| 739 | |
| 740 | raw = (opt->commit_format == CMIT_FMT_USERFORMAT); |
| brian m. carlson | fb61e4d | 2017-05-30 17:30:41 | [diff] [blame] | 741 | format_display_notes(&commit->object.oid, ¬ebuf, |
| Junio C Hamano | ddf333f | 2012-10-18 01:51:47 | [diff] [blame] | 742 | get_log_output_encoding(), raw); |
| René Scharfe | 82f51af | 2019-08-25 12:53:26 | [diff] [blame] | 743 | ctx.notes_message = strbuf_detach(¬ebuf, NULL); |
| Junio C Hamano | ddf333f | 2012-10-18 01:51:47 | [diff] [blame] | 744 | } |
| 745 | |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 746 | /* |
| 747 | * And then the pretty-printed message itself |
| 748 | */ |
| Nguyễn Thái Ngọc Duy | 5289c56 | 2013-02-12 10:17:38 | [diff] [blame] | 749 | if (ctx.need_8bit_cte >= 0 && opt->add_signoff) |
| 750 | ctx.need_8bit_cte = |
| William Hubbs | 39ab4d0 | 2019-02-04 18:48:50 | [diff] [blame] | 751 | has_non_ascii(fmt_name(WANT_COMMITTER_IDENT)); |
| Thomas Rast | dd2e794 | 2009-10-19 15:48:08 | [diff] [blame] | 752 | ctx.date_mode = opt->date_mode; |
| Jeff King | f026c75 | 2012-05-04 05:25:18 | [diff] [blame] | 753 | ctx.date_mode_explicit = opt->date_mode_explicit; |
| Thomas Rast | dd2e794 | 2009-10-19 15:48:08 | [diff] [blame] | 754 | ctx.abbrev = opt->diffopt.abbrev; |
| 755 | ctx.after_subject = extra_headers; |
| Jeff King | 9553d2b | 2011-05-26 22:28:17 | [diff] [blame] | 756 | ctx.preserve_subject = opt->preserve_subject; |
| Emma Brooks | 19d097e | 2020-04-08 04:31:38 | [diff] [blame] | 757 | ctx.encode_email_headers = opt->encode_email_headers; |
| Thomas Rast | 8f8f547 | 2009-10-19 15:48:10 | [diff] [blame] | 758 | ctx.reflog_info = opt->reflog_info; |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 759 | ctx.fmt = opt->commit_format; |
| Antoine Pelisse | 0e2913b | 2013-01-05 21:26:41 | [diff] [blame] | 760 | ctx.mailmap = opt->mailmap; |
| Junio C Hamano | 3082517 | 2012-12-17 22:56:49 | [diff] [blame] | 761 | ctx.color = opt->diffopt.use_color; |
| Linus Torvalds | 7cc13c7 | 2016-03-16 16:15:53 | [diff] [blame] | 762 | ctx.expand_tabs_in_log = opt->expand_tabs_in_log; |
| Alexey Shumkin | ecaee80 | 2013-06-26 10:19:50 | [diff] [blame] | 763 | ctx.output_encoding = get_log_output_encoding(); |
| Issac Trotts | ad6f028 | 2019-01-11 06:30:46 | [diff] [blame] | 764 | ctx.rev = opt; |
| Jeff King | a908047 | 2013-07-03 07:08:22 | [diff] [blame] | 765 | if (opt->from_ident.mail_begin && opt->from_ident.name_begin) |
| 766 | ctx.from_ident = &opt->from_ident; |
| Josef Kufner | 3ad87c8 | 2016-06-16 13:18:37 | [diff] [blame] | 767 | if (opt->graph) |
| 768 | ctx.graph_width = graph_width(opt->graph); |
| Jeff King | 6bf1394 | 2011-05-26 22:27:49 | [diff] [blame] | 769 | pretty_print_commit(&ctx, commit, &msgbuf); |
| Junio C Hamano | 212620f | 2012-10-18 03:48:25 | [diff] [blame] | 770 | |
| 771 | if (opt->add_signoff) |
| Nguyễn Thái Ngọc Duy | 5289c56 | 2013-02-12 10:17:38 | [diff] [blame] | 772 | append_signoff(&msgbuf, 0, APPEND_SIGNOFF_DEDUP); |
| Junio C Hamano | 212620f | 2012-10-18 03:48:25 | [diff] [blame] | 773 | |
| Junio C Hamano | 5a664cf | 2012-10-18 02:02:46 | [diff] [blame] | 774 | if ((ctx.fmt != CMIT_FMT_USERFORMAT) && |
| Junio C Hamano | bd1470b | 2012-10-18 04:27:22 | [diff] [blame] | 775 | ctx.notes_message && *ctx.notes_message) { |
| Eric Sunshine | 3fcc7a2 | 2018-07-22 09:57:08 | [diff] [blame] | 776 | if (cmit_fmt_is_mail(ctx.fmt)) |
| 777 | next_commentary_block(opt, &msgbuf); |
| Junio C Hamano | 5a664cf | 2012-10-18 02:02:46 | [diff] [blame] | 778 | strbuf_addstr(&msgbuf, ctx.notes_message); |
| Junio C Hamano | bd1470b | 2012-10-18 04:27:22 | [diff] [blame] | 779 | } |
| Junio C Hamano | cf2251b | 2006-05-31 22:11:49 | [diff] [blame] | 780 | |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 781 | if (opt->show_log_size) { |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 782 | fprintf(opt->diffopt.file, "log size %i\n", (int)msgbuf.len); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 783 | graph_show_oneline(opt->graph); |
| 784 | } |
| Marco Costalba | 9fa3465 | 2007-07-20 18:15:13 | [diff] [blame] | 785 | |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 786 | /* |
| 787 | * Set opt->missing_newline if msgbuf doesn't |
| 788 | * end in a newline (including if it is empty) |
| 789 | */ |
| 790 | if (!msgbuf.len || msgbuf.buf[msgbuf.len - 1] != '\n') |
| 791 | opt->missing_newline = 1; |
| 792 | else |
| 793 | opt->missing_newline = 0; |
| 794 | |
| Jacob Keller | 660e113 | 2016-08-31 23:27:20 | [diff] [blame] | 795 | graph_show_commit_msg(opt->graph, opt->diffopt.file, &msgbuf); |
| Jeff King | b9c7d6e | 2014-07-29 17:56:48 | [diff] [blame] | 796 | if (opt->use_terminator && !commit_format_is_empty(opt->commit_format)) { |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 797 | if (!opt->missing_newline) |
| 798 | graph_show_padding(opt->graph); |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 799 | putc(opt->diffopt.line_termination, opt->diffopt.file); |
| Adam Simpkins | 7fefda5 | 2008-05-04 10:36:54 | [diff] [blame] | 800 | } |
| 801 | |
| Pierre Habouzit | 674d172 | 2007-09-10 10:35:06 | [diff] [blame] | 802 | strbuf_release(&msgbuf); |
| Junio C Hamano | ddf333f | 2012-10-18 01:51:47 | [diff] [blame] | 803 | free(ctx.notes_message); |
| Eric Sunshine | ee6cbf7 | 2018-07-22 09:57:09 | [diff] [blame] | 804 | |
| 805 | if (cmit_fmt_is_mail(ctx.fmt) && opt->idiff_oid1) { |
| 806 | struct diff_queue_struct dq; |
| 807 | |
| 808 | memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff)); |
| 809 | DIFF_QUEUE_CLEAR(&diff_queued_diff); |
| 810 | |
| 811 | next_commentary_block(opt, NULL); |
| 812 | fprintf_ln(opt->diffopt.file, "%s", opt->idiff_title); |
| Eric Sunshine | 72a7239 | 2020-09-08 07:16:09 | [diff] [blame] | 813 | show_interdiff(opt->idiff_oid1, opt->idiff_oid2, 2, |
| 814 | &opt->diffopt); |
| Eric Sunshine | ee6cbf7 | 2018-07-22 09:57:09 | [diff] [blame] | 815 | |
| 816 | memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff)); |
| 817 | } |
| Eric Sunshine | 40ce416 | 2018-07-22 09:57:17 | [diff] [blame] | 818 | |
| 819 | if (cmit_fmt_is_mail(ctx.fmt) && opt->rdiff1) { |
| 820 | struct diff_queue_struct dq; |
| Martin Ågren | ac0edf1 | 2018-12-03 21:21:31 | [diff] [blame] | 821 | struct diff_options opts; |
| Johannes Schindelin | f1ce6c1 | 2021-02-05 14:46:11 | [diff] [blame] | 822 | struct range_diff_options range_diff_opts = { |
| 823 | .creation_factor = opt->creation_factor, |
| 824 | .dual_color = 1, |
| 825 | .diffopt = &opts |
| 826 | }; |
| Eric Sunshine | 40ce416 | 2018-07-22 09:57:17 | [diff] [blame] | 827 | |
| 828 | memcpy(&dq, &diff_queued_diff, sizeof(diff_queued_diff)); |
| 829 | DIFF_QUEUE_CLEAR(&diff_queued_diff); |
| 830 | |
| 831 | next_commentary_block(opt, NULL); |
| 832 | fprintf_ln(opt->diffopt.file, "%s", opt->rdiff_title); |
| Martin Ågren | ac0edf1 | 2018-12-03 21:21:31 | [diff] [blame] | 833 | /* |
| 834 | * Pass minimum required diff-options to range-diff; others |
| 835 | * can be added later if deemed desirable. |
| 836 | */ |
| 837 | diff_setup(&opts); |
| 838 | opts.file = opt->diffopt.file; |
| 839 | opts.use_color = opt->diffopt.use_color; |
| 840 | diff_setup_done(&opts); |
| Johannes Schindelin | f1ce6c1 | 2021-02-05 14:46:11 | [diff] [blame] | 841 | show_range_diff(opt->rdiff1, opt->rdiff2, &range_diff_opts); |
| Eric Sunshine | 40ce416 | 2018-07-22 09:57:17 | [diff] [blame] | 842 | |
| 843 | memcpy(&diff_queued_diff, &dq, sizeof(diff_queued_diff)); |
| 844 | } |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 845 | } |
| 846 | |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 847 | int log_tree_diff_flush(struct rev_info *opt) |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 848 | { |
| Junio C Hamano | bd1470b | 2012-10-18 04:27:22 | [diff] [blame] | 849 | opt->shown_dashes = 0; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 850 | diffcore_std(&opt->diffopt); |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 851 | |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 852 | if (diff_queue_is_empty()) { |
| 853 | int saved_fmt = opt->diffopt.output_format; |
| 854 | opt->diffopt.output_format = DIFF_FORMAT_NO_OUTPUT; |
| 855 | diff_flush(&opt->diffopt); |
| 856 | opt->diffopt.output_format = saved_fmt; |
| 857 | return 0; |
| 858 | } |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 859 | |
| Junio C Hamano | 3969cf7 | 2006-06-27 22:08:19 | [diff] [blame] | 860 | if (opt->loginfo && !opt->no_commit_id) { |
| Adam Simpkins | 0286565 | 2008-04-29 08:32:59 | [diff] [blame] | 861 | show_log(opt); |
| Linus Torvalds | 304b5af | 2007-10-09 16:35:22 | [diff] [blame] | 862 | if ((opt->diffopt.output_format & ~DIFF_FORMAT_NO_OUTPUT) && |
| 863 | opt->verbose_header && |
| Jeff King | b9c7d6e | 2014-07-29 17:56:48 | [diff] [blame] | 864 | opt->commit_format != CMIT_FMT_ONELINE && |
| 865 | !commit_format_is_empty(opt->commit_format)) { |
| Junio C Hamano | 1d34c50 | 2012-11-13 18:09:07 | [diff] [blame] | 866 | /* |
| 867 | * When showing a verbose header (i.e. log message), |
| 868 | * and not in --pretty=oneline format, we would want |
| 869 | * an extra newline between the end of log and the |
| 870 | * diff/diffstat output for readability. |
| 871 | */ |
| Junio C Hamano | 3969cf7 | 2006-06-27 22:08:19 | [diff] [blame] | 872 | int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH; |
| Bo Yang | 81bd1b2 | 2010-05-26 07:08:03 | [diff] [blame] | 873 | if (opt->diffopt.output_prefix) { |
| 874 | struct strbuf *msg = NULL; |
| 875 | msg = opt->diffopt.output_prefix(&opt->diffopt, |
| 876 | opt->diffopt.output_prefix_data); |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 877 | fwrite(msg->buf, msg->len, 1, opt->diffopt.file); |
| Bo Yang | 81bd1b2 | 2010-05-26 07:08:03 | [diff] [blame] | 878 | } |
| Junio C Hamano | 1d34c50 | 2012-11-13 18:09:07 | [diff] [blame] | 879 | |
| 880 | /* |
| 881 | * We may have shown three-dashes line early |
| Eric Sunshine | 3fcc7a2 | 2018-07-22 09:57:08 | [diff] [blame] | 882 | * between generated commentary (notes, etc.) |
| 883 | * and the log message, in which case we only |
| 884 | * want a blank line after the commentary |
| 885 | * without (an extra) three-dashes line. |
| Junio C Hamano | 1d34c50 | 2012-11-13 18:09:07 | [diff] [blame] | 886 | * Otherwise, we show the three-dashes line if |
| 887 | * we are showing the patch with diffstat, but |
| 888 | * in that case, there is no extra blank line |
| 889 | * after the three-dashes line. |
| 890 | */ |
| 891 | if (!opt->shown_dashes && |
| 892 | (pch & opt->diffopt.output_format) == pch) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 893 | fprintf(opt->diffopt.file, "---"); |
| 894 | putc('\n', opt->diffopt.file); |
| Junio C Hamano | 3969cf7 | 2006-06-27 22:08:19 | [diff] [blame] | 895 | } |
| 896 | } |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 897 | diff_flush(&opt->diffopt); |
| 898 | return 1; |
| 899 | } |
| 900 | |
| Linus Torvalds | cd2bdc5 | 2006-04-14 23:52:13 | [diff] [blame] | 901 | static int do_diff_combined(struct rev_info *opt, struct commit *commit) |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 902 | { |
| Sergey Organov | d01141d | 2020-09-29 11:31:22 | [diff] [blame] | 903 | diff_tree_combined_merge(commit, opt); |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 904 | return !opt->loginfo; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 905 | } |
| 906 | |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 907 | /* |
| 908 | * Show the diff of a commit. |
| 909 | * |
| 910 | * Return true if we printed any log info messages |
| 911 | */ |
| 912 | static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log) |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 913 | { |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 914 | int showed_log; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 915 | struct commit_list *parents; |
| brian m. carlson | f2fd076 | 2015-11-10 02:22:28 | [diff] [blame] | 916 | struct object_id *oid; |
| Sergey Organov | a6d19ec | 2020-12-21 15:19:53 | [diff] [blame] | 917 | int is_merge; |
| 918 | int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 919 | |
| Sergey Organov | a6d19ec | 2020-12-21 15:19:53 | [diff] [blame] | 920 | if (!all_need_diff && !opt->merges_need_diff) |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 921 | return 0; |
| 922 | |
| Jeff King | 7059dcc | 2013-10-24 08:52:36 | [diff] [blame] | 923 | parse_commit_or_die(commit); |
| Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 | [diff] [blame] | 924 | oid = get_commit_tree_oid(commit); |
| Thomas Rast | d1b9b76 | 2013-03-28 08:19:34 | [diff] [blame] | 925 | |
| Thomas Rast | 53d00b3 | 2013-07-31 20:13:20 | [diff] [blame] | 926 | parents = get_saved_parents(opt, commit); |
| Sergey Organov | a6d19ec | 2020-12-21 15:19:53 | [diff] [blame] | 927 | is_merge = parents && parents->next; |
| 928 | if (!is_merge && !all_need_diff) |
| 929 | return 0; |
| 930 | |
| 931 | /* Root commit? */ |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 932 | if (!parents) { |
| Rene Scharfe | 2b60356 | 2006-10-26 16:52:39 | [diff] [blame] | 933 | if (opt->show_root_diff) { |
| Brandon Williams | 7b8dea0 | 2017-05-30 17:30:57 | [diff] [blame] | 934 | diff_root_tree_oid(oid, "", &opt->diffopt); |
| Rene Scharfe | 2b60356 | 2006-10-26 16:52:39 | [diff] [blame] | 935 | log_tree_diff_flush(opt); |
| 936 | } |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 937 | return !opt->loginfo; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 938 | } |
| 939 | |
| Sergey Organov | a6d19ec | 2020-12-21 15:19:53 | [diff] [blame] | 940 | if (is_merge) { |
| Sergey Organov | 1a2c4d8 | 2020-12-21 15:19:46 | [diff] [blame] | 941 | if (opt->combine_merges) |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 942 | return do_diff_combined(opt, commit); |
| Sergey Organov | 1a2c4d8 | 2020-12-21 15:19:46 | [diff] [blame] | 943 | if (opt->separate_merges) { |
| 944 | if (!opt->first_parent_merges) { |
| 945 | /* Show parent info for multiple diffs */ |
| 946 | log->parent = parents->item; |
| 947 | } |
| 948 | } else |
| 949 | return 0; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 950 | } |
| 951 | |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 952 | showed_log = 0; |
| 953 | for (;;) { |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 954 | struct commit *parent = parents->item; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 955 | |
| Jeff King | 7059dcc | 2013-10-24 08:52:36 | [diff] [blame] | 956 | parse_commit_or_die(parent); |
| Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 | [diff] [blame] | 957 | diff_tree_oid(get_commit_tree_oid(parent), |
| Brandon Williams | 66f414f | 2017-05-30 17:31:03 | [diff] [blame] | 958 | oid, "", &opt->diffopt); |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 959 | log_tree_diff_flush(opt); |
| 960 | |
| 961 | showed_log |= !opt->loginfo; |
| 962 | |
| 963 | /* Set up the log info for the next parent, if any.. */ |
| 964 | parents = parents->next; |
| Sergey Organov | 3291eea | 2020-12-21 15:19:40 | [diff] [blame] | 965 | if (!parents || opt->first_parent_merges) |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 966 | break; |
| 967 | log->parent = parents->item; |
| 968 | opt->loginfo = log; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 969 | } |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 970 | return showed_log; |
| 971 | } |
| 972 | |
| 973 | int log_tree_commit(struct rev_info *opt, struct commit *commit) |
| 974 | { |
| 975 | struct log_info log; |
| Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 10:45:34 | [diff] [blame] | 976 | int shown; |
| 977 | /* maybe called by e.g. cmd_log_walk(), maybe stand-alone */ |
| 978 | int no_free = opt->diffopt.no_free; |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 979 | |
| 980 | log.commit = commit; |
| 981 | log.parent = NULL; |
| 982 | opt->loginfo = &log; |
| Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 10:45:34 | [diff] [blame] | 983 | opt->diffopt.no_free = 1; |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 984 | |
| Thomas Rast | 12da1d1 | 2013-03-28 16:47:32 | [diff] [blame] | 985 | if (opt->line_level_traverse) |
| 986 | return line_log_print(opt, commit); |
| 987 | |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 988 | if (opt->track_linear && !opt->linear && !opt->reverse_output_stage) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 989 | fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar); |
| Junio C Hamano | 3eefc18 | 2006-04-18 23:45:27 | [diff] [blame] | 990 | shown = log_tree_diff(opt, commit, &log); |
| 991 | if (!shown && opt->loginfo && opt->always_show_header) { |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 992 | log.parent = NULL; |
| Adam Simpkins | 0286565 | 2008-04-29 08:32:59 | [diff] [blame] | 993 | show_log(opt); |
| Junio C Hamano | 3eefc18 | 2006-04-18 23:45:27 | [diff] [blame] | 994 | shown = 1; |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 995 | } |
| Nguyễn Thái Ngọc Duy | 1b32dec | 2014-03-25 13:23:27 | [diff] [blame] | 996 | if (opt->track_linear && !opt->linear && opt->reverse_output_stage) |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 997 | fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar); |
| Linus Torvalds | 9153983 | 2006-04-17 18:59:32 | [diff] [blame] | 998 | opt->loginfo = NULL; |
| Johannes Schindelin | 4d7b0ef | 2016-06-22 15:01:32 | [diff] [blame] | 999 | maybe_flush_or_die(opt->diffopt.file, "stdout"); |
| Ævar Arnfjörð Bjarmason | e900d49 | 2021-02-11 10:45:34 | [diff] [blame] | 1000 | opt->diffopt.no_free = no_free; |
| 1001 | diff_free(&opt->diffopt); |
| Junio C Hamano | 3eefc18 | 2006-04-18 23:45:27 | [diff] [blame] | 1002 | return shown; |
| Junio C Hamano | 5f1c3f0 | 2006-04-09 08:11:11 | [diff] [blame] | 1003 | } |