Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "cache.h" |
| 3 | #include "parse-options.h" |
| 4 | #include "refs.h" |
| 5 | #include "wildmatch.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 6 | #include "object-store.h" |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 7 | #include "repository.h" |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 8 | #include "commit.h" |
| 9 | #include "remote.h" |
| 10 | #include "color.h" |
| 11 | #include "tag.h" |
| 12 | #include "quote.h" |
| 13 | #include "ref-filter.h" |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 14 | #include "revision.h" |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 15 | #include "utf8.h" |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 16 | #include "git-compat-util.h" |
| 17 | #include "version.h" |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 18 | #include "trailer.h" |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 19 | #include "wt-status.h" |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 20 | #include "commit-slab.h" |
Derrick Stolee | 819807b | 2018-05-01 12:47:15 +0000 | [diff] [blame] | 21 | #include "commit-graph.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 22 | #include "commit-reach.h" |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 23 | |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 24 | static struct ref_msg { |
| 25 | const char *gone; |
| 26 | const char *ahead; |
| 27 | const char *behind; |
| 28 | const char *ahead_behind; |
| 29 | } msgs = { |
| 30 | /* Untranslated plumbing messages: */ |
| 31 | "gone", |
| 32 | "ahead %d", |
| 33 | "behind %d", |
| 34 | "ahead %d, behind %d" |
| 35 | }; |
| 36 | |
| 37 | void setup_ref_filter_porcelain_msg(void) |
| 38 | { |
| 39 | msgs.gone = _("gone"); |
| 40 | msgs.ahead = _("ahead %d"); |
| 41 | msgs.behind = _("behind %d"); |
| 42 | msgs.ahead_behind = _("ahead %d, behind %d"); |
| 43 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 44 | |
| 45 | typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 46 | typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 47 | typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 48 | |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 49 | struct align { |
| 50 | align_type position; |
| 51 | unsigned int width; |
| 52 | }; |
| 53 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 54 | struct if_then_else { |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 55 | cmp_status cmp_status; |
| 56 | const char *str; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 57 | unsigned int then_atom_seen : 1, |
| 58 | else_atom_seen : 1, |
| 59 | condition_satisfied : 1; |
| 60 | }; |
| 61 | |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 62 | struct refname_atom { |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 63 | enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option; |
| 64 | int lstrip, rstrip; |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 65 | }; |
| 66 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 67 | static struct expand_data { |
| 68 | struct object_id oid; |
| 69 | enum object_type type; |
| 70 | unsigned long size; |
| 71 | off_t disk_size; |
| 72 | struct object_id delta_base_oid; |
| 73 | void *content; |
| 74 | |
| 75 | struct object_info info; |
| 76 | } oi, oi_deref; |
| 77 | |
Karthik Nayak | 50cd83d | 2016-02-17 23:36:10 +0530 | [diff] [blame] | 78 | /* |
| 79 | * An atom is a valid field atom listed below, possibly prefixed with |
| 80 | * a "*" to denote deref_tag(). |
| 81 | * |
| 82 | * We parse given format string and sort specifiers, and make a list |
| 83 | * of properties that we need to extract out of objects. ref_array_item |
| 84 | * structure will hold an array of values extracted that can be |
| 85 | * indexed with the "atom number", which is an index into this |
| 86 | * array. |
| 87 | */ |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 88 | static struct used_atom { |
| 89 | const char *name; |
| 90 | cmp_type type; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 91 | info_source source; |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 92 | union { |
| 93 | char color[COLOR_MAXLEN]; |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 94 | struct align align; |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 95 | struct { |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 96 | enum { |
J Wyman | 9700fae | 2017-11-07 17:31:08 +0100 | [diff] [blame] | 97 | RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 98 | } option; |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 99 | struct refname_atom refname; |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 100 | unsigned int nobracket : 1, push : 1, push_remote : 1; |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 101 | } remote_ref; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 102 | struct { |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 103 | enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option; |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 104 | struct process_trailer_options trailer_opts; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 105 | unsigned int nlines; |
| 106 | } contents; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 107 | struct { |
| 108 | cmp_status cmp_status; |
| 109 | const char *str; |
| 110 | } if_then_else; |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 111 | struct { |
| 112 | enum { O_FULL, O_LENGTH, O_SHORT } option; |
| 113 | unsigned int length; |
| 114 | } objectname; |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 115 | struct refname_atom refname; |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 116 | char *head; |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 117 | } u; |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 118 | } *used_atom; |
Karthik Nayak | 50cd83d | 2016-02-17 23:36:10 +0530 | [diff] [blame] | 119 | static int used_atom_cnt, need_tagged, need_symref; |
Karthik Nayak | 50cd83d | 2016-02-17 23:36:10 +0530 | [diff] [blame] | 120 | |
Olga Telezhnaya | e2e7a24 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 121 | /* |
| 122 | * Expand string, append it to strbuf *sb, then return error code ret. |
| 123 | * Allow to save few lines of code. |
| 124 | */ |
| 125 | static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...) |
| 126 | { |
| 127 | va_list ap; |
| 128 | va_start(ap, fmt); |
| 129 | strbuf_vaddf(sb, fmt, ap); |
| 130 | va_end(ap); |
| 131 | return ret; |
| 132 | } |
| 133 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 134 | static int color_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 135 | const char *color_value, struct strbuf *err) |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 136 | { |
| 137 | if (!color_value) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 138 | return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)")); |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 139 | if (color_parse(color_value, atom->u.color) < 0) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 140 | return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"), |
| 141 | color_value); |
Jeff King | 11b087a | 2017-07-13 11:09:32 -0400 | [diff] [blame] | 142 | /* |
| 143 | * We check this after we've parsed the color, which lets us complain |
| 144 | * about syntactically bogus color names even if they won't be used. |
| 145 | */ |
| 146 | if (!want_color(format->use_color)) |
| 147 | color_parse("", atom->u.color); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 148 | return 0; |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 149 | } |
| 150 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 151 | static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg, |
| 152 | const char *name, struct strbuf *err) |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 153 | { |
| 154 | if (!arg) |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 155 | atom->option = R_NORMAL; |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 156 | else if (!strcmp(arg, "short")) |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 157 | atom->option = R_SHORT; |
Junio C Hamano | 44a6b6c | 2017-02-07 11:50:34 -0800 | [diff] [blame] | 158 | else if (skip_prefix(arg, "lstrip=", &arg) || |
| 159 | skip_prefix(arg, "strip=", &arg)) { |
Karthik Nayak | 17938f1 | 2017-01-10 14:19:46 +0530 | [diff] [blame] | 160 | atom->option = R_LSTRIP; |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 161 | if (strtol_i(arg, 10, &atom->lstrip)) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 162 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg); |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 163 | } else if (skip_prefix(arg, "rstrip=", &arg)) { |
| 164 | atom->option = R_RSTRIP; |
| 165 | if (strtol_i(arg, 10, &atom->rstrip)) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 166 | return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg); |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 167 | } else |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 168 | return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg); |
| 169 | return 0; |
Karthik Nayak | b180e6f | 2017-01-10 14:19:43 +0530 | [diff] [blame] | 170 | } |
| 171 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 172 | static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 173 | const char *arg, struct strbuf *err) |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 174 | { |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 175 | struct string_list params = STRING_LIST_INIT_DUP; |
| 176 | int i; |
| 177 | |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 178 | if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:")) |
| 179 | atom->u.remote_ref.push = 1; |
| 180 | |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 181 | if (!arg) { |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 182 | atom->u.remote_ref.option = RR_REF; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 183 | return refname_atom_parser_internal(&atom->u.remote_ref.refname, |
| 184 | arg, atom->name, err); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | atom->u.remote_ref.nobracket = 0; |
| 188 | string_list_split(¶ms, arg, ',', -1); |
| 189 | |
| 190 | for (i = 0; i < params.nr; i++) { |
| 191 | const char *s = params.items[i].string; |
| 192 | |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 193 | if (!strcmp(s, "track")) |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 194 | atom->u.remote_ref.option = RR_TRACK; |
| 195 | else if (!strcmp(s, "trackshort")) |
| 196 | atom->u.remote_ref.option = RR_TRACKSHORT; |
| 197 | else if (!strcmp(s, "nobracket")) |
| 198 | atom->u.remote_ref.nobracket = 1; |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 199 | else if (!strcmp(s, "remotename")) { |
| 200 | atom->u.remote_ref.option = RR_REMOTE_NAME; |
| 201 | atom->u.remote_ref.push_remote = 1; |
J Wyman | 9700fae | 2017-11-07 17:31:08 +0100 | [diff] [blame] | 202 | } else if (!strcmp(s, "remoteref")) { |
| 203 | atom->u.remote_ref.option = RR_REMOTE_REF; |
| 204 | atom->u.remote_ref.push_remote = 1; |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 205 | } else { |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 206 | atom->u.remote_ref.option = RR_REF; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 207 | if (refname_atom_parser_internal(&atom->u.remote_ref.refname, |
| 208 | arg, atom->name, err)) { |
| 209 | string_list_clear(¶ms, 0); |
| 210 | return -1; |
| 211 | } |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 212 | } |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | string_list_clear(¶ms, 0); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 216 | return 0; |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 217 | } |
| 218 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 219 | static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 220 | const char *arg, struct strbuf *err) |
| 221 | { |
| 222 | if (arg) |
| 223 | return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments")); |
| 224 | if (*atom->name == '*') |
| 225 | oi_deref.info.typep = &oi_deref.type; |
| 226 | else |
| 227 | oi.info.typep = &oi.type; |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 232 | const char *arg, struct strbuf *err) |
| 233 | { |
| 234 | if (arg) |
| 235 | return strbuf_addf_ret(err, -1, _("%%(objectsize) does not take arguments")); |
| 236 | if (*atom->name == '*') |
| 237 | oi_deref.info.sizep = &oi_deref.size; |
| 238 | else |
| 239 | oi.info.sizep = &oi.size; |
| 240 | return 0; |
| 241 | } |
| 242 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 243 | static int body_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 244 | const char *arg, struct strbuf *err) |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 245 | { |
| 246 | if (arg) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 247 | return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments")); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 248 | atom->u.contents.option = C_BODY_DEP; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 249 | return 0; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 250 | } |
| 251 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 252 | static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 253 | const char *arg, struct strbuf *err) |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 254 | { |
| 255 | if (arg) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 256 | return strbuf_addf_ret(err, -1, _("%%(subject) does not take arguments")); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 257 | atom->u.contents.option = C_SUB; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 258 | return 0; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 259 | } |
| 260 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 261 | static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 262 | const char *arg, struct strbuf *err) |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 263 | { |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 264 | struct string_list params = STRING_LIST_INIT_DUP; |
| 265 | int i; |
| 266 | |
Jeff King | e5fba5d | 2018-08-22 20:50:17 -0400 | [diff] [blame] | 267 | atom->u.contents.trailer_opts.no_divider = 1; |
| 268 | |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 269 | if (arg) { |
| 270 | string_list_split(¶ms, arg, ',', -1); |
| 271 | for (i = 0; i < params.nr; i++) { |
| 272 | const char *s = params.items[i].string; |
| 273 | if (!strcmp(s, "unfold")) |
| 274 | atom->u.contents.trailer_opts.unfold = 1; |
| 275 | else if (!strcmp(s, "only")) |
| 276 | atom->u.contents.trailer_opts.only_trailers = 1; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 277 | else { |
| 278 | strbuf_addf(err, _("unknown %%(trailers) argument: %s"), s); |
| 279 | string_list_clear(¶ms, 0); |
| 280 | return -1; |
| 281 | } |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 284 | atom->u.contents.option = C_TRAILERS; |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 285 | string_list_clear(¶ms, 0); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 286 | return 0; |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 289 | static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 290 | const char *arg, struct strbuf *err) |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 291 | { |
| 292 | if (!arg) |
| 293 | atom->u.contents.option = C_BARE; |
| 294 | else if (!strcmp(arg, "body")) |
| 295 | atom->u.contents.option = C_BODY; |
| 296 | else if (!strcmp(arg, "signature")) |
| 297 | atom->u.contents.option = C_SIG; |
| 298 | else if (!strcmp(arg, "subject")) |
| 299 | atom->u.contents.option = C_SUB; |
Taylor Blau | 7a5edbd | 2017-10-01 22:25:24 -0700 | [diff] [blame] | 300 | else if (skip_prefix(arg, "trailers", &arg)) { |
| 301 | skip_prefix(arg, ":", &arg); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 302 | if (trailers_atom_parser(format, atom, *arg ? arg : NULL, err)) |
| 303 | return -1; |
Taylor Blau | 7a5edbd | 2017-10-01 22:25:24 -0700 | [diff] [blame] | 304 | } else if (skip_prefix(arg, "lines=", &arg)) { |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 305 | atom->u.contents.option = C_LINES; |
| 306 | if (strtoul_ui(arg, 10, &atom->u.contents.nlines)) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 307 | return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 308 | } else |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 309 | return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg); |
| 310 | return 0; |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 311 | } |
| 312 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 313 | static int objectname_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 314 | const char *arg, struct strbuf *err) |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 315 | { |
| 316 | if (!arg) |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 317 | atom->u.objectname.option = O_FULL; |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 318 | else if (!strcmp(arg, "short")) |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 319 | atom->u.objectname.option = O_SHORT; |
| 320 | else if (skip_prefix(arg, "short=", &arg)) { |
| 321 | atom->u.objectname.option = O_LENGTH; |
| 322 | if (strtoul_ui(arg, 10, &atom->u.objectname.length) || |
| 323 | atom->u.objectname.length == 0) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 324 | return strbuf_addf_ret(err, -1, _("positive value expected objectname:short=%s"), arg); |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 325 | if (atom->u.objectname.length < MINIMUM_ABBREV) |
| 326 | atom->u.objectname.length = MINIMUM_ABBREV; |
| 327 | } else |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 328 | return strbuf_addf_ret(err, -1, _("unrecognized %%(objectname) argument: %s"), arg); |
| 329 | return 0; |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 330 | } |
| 331 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 332 | static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 333 | const char *arg, struct strbuf *err) |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 334 | { |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 335 | return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 336 | } |
| 337 | |
Karthik Nayak | 25a8d79 | 2016-02-17 23:36:14 +0530 | [diff] [blame] | 338 | static align_type parse_align_position(const char *s) |
| 339 | { |
| 340 | if (!strcmp(s, "right")) |
| 341 | return ALIGN_RIGHT; |
| 342 | else if (!strcmp(s, "middle")) |
| 343 | return ALIGN_MIDDLE; |
| 344 | else if (!strcmp(s, "left")) |
| 345 | return ALIGN_LEFT; |
| 346 | return -1; |
| 347 | } |
| 348 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 349 | static int align_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 350 | const char *arg, struct strbuf *err) |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 351 | { |
| 352 | struct align *align = &atom->u.align; |
| 353 | struct string_list params = STRING_LIST_INIT_DUP; |
| 354 | int i; |
| 355 | unsigned int width = ~0U; |
| 356 | |
| 357 | if (!arg) |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 358 | return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)")); |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 359 | |
| 360 | align->position = ALIGN_LEFT; |
| 361 | |
| 362 | string_list_split(¶ms, arg, ',', -1); |
| 363 | for (i = 0; i < params.nr; i++) { |
| 364 | const char *s = params.items[i].string; |
| 365 | int position; |
| 366 | |
Karthik Nayak | 395fb8f | 2016-02-17 23:36:16 +0530 | [diff] [blame] | 367 | if (skip_prefix(s, "position=", &s)) { |
| 368 | position = parse_align_position(s); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 369 | if (position < 0) { |
| 370 | strbuf_addf(err, _("unrecognized position:%s"), s); |
| 371 | string_list_clear(¶ms, 0); |
| 372 | return -1; |
| 373 | } |
Karthik Nayak | 395fb8f | 2016-02-17 23:36:16 +0530 | [diff] [blame] | 374 | align->position = position; |
| 375 | } else if (skip_prefix(s, "width=", &s)) { |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 376 | if (strtoul_ui(s, 10, &width)) { |
| 377 | strbuf_addf(err, _("unrecognized width:%s"), s); |
| 378 | string_list_clear(¶ms, 0); |
| 379 | return -1; |
| 380 | } |
Karthik Nayak | 395fb8f | 2016-02-17 23:36:16 +0530 | [diff] [blame] | 381 | } else if (!strtoul_ui(s, 10, &width)) |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 382 | ; |
| 383 | else if ((position = parse_align_position(s)) >= 0) |
| 384 | align->position = position; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 385 | else { |
| 386 | strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s); |
| 387 | string_list_clear(¶ms, 0); |
| 388 | return -1; |
| 389 | } |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 390 | } |
| 391 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 392 | if (width == ~0U) { |
| 393 | string_list_clear(¶ms, 0); |
| 394 | return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom")); |
| 395 | } |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 396 | align->width = width; |
| 397 | string_list_clear(¶ms, 0); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 398 | return 0; |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 399 | } |
| 400 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 401 | static int if_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 402 | const char *arg, struct strbuf *err) |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 403 | { |
| 404 | if (!arg) { |
| 405 | atom->u.if_then_else.cmp_status = COMPARE_NONE; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 406 | return 0; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 407 | } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) { |
| 408 | atom->u.if_then_else.cmp_status = COMPARE_EQUAL; |
| 409 | } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) { |
| 410 | atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 411 | } else |
| 412 | return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg); |
| 413 | return 0; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 414 | } |
| 415 | |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 416 | static int head_atom_parser(const struct ref_format *format, struct used_atom *atom, |
| 417 | const char *arg, struct strbuf *unused_err) |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 418 | { |
René Scharfe | efbd4fd | 2017-10-01 09:29:03 +0200 | [diff] [blame] | 419 | atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 420 | return 0; |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 421 | } |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 422 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 423 | static struct { |
| 424 | const char *name; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 425 | info_source source; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 426 | cmp_type cmp_type; |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 427 | int (*parser)(const struct ref_format *format, struct used_atom *atom, |
| 428 | const char *arg, struct strbuf *err); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 429 | } valid_atom[] = { |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 430 | { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser }, |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 431 | { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser }, |
| 432 | { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser }, |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 433 | { "objectname", SOURCE_OTHER, FIELD_STR, objectname_atom_parser }, |
| 434 | { "tree", SOURCE_OBJ }, |
| 435 | { "parent", SOURCE_OBJ }, |
| 436 | { "numparent", SOURCE_OBJ, FIELD_ULONG }, |
| 437 | { "object", SOURCE_OBJ }, |
| 438 | { "type", SOURCE_OBJ }, |
| 439 | { "tag", SOURCE_OBJ }, |
| 440 | { "author", SOURCE_OBJ }, |
| 441 | { "authorname", SOURCE_OBJ }, |
| 442 | { "authoremail", SOURCE_OBJ }, |
| 443 | { "authordate", SOURCE_OBJ, FIELD_TIME }, |
| 444 | { "committer", SOURCE_OBJ }, |
| 445 | { "committername", SOURCE_OBJ }, |
| 446 | { "committeremail", SOURCE_OBJ }, |
| 447 | { "committerdate", SOURCE_OBJ, FIELD_TIME }, |
| 448 | { "tagger", SOURCE_OBJ }, |
| 449 | { "taggername", SOURCE_OBJ }, |
| 450 | { "taggeremail", SOURCE_OBJ }, |
| 451 | { "taggerdate", SOURCE_OBJ, FIELD_TIME }, |
| 452 | { "creator", SOURCE_OBJ }, |
| 453 | { "creatordate", SOURCE_OBJ, FIELD_TIME }, |
| 454 | { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser }, |
| 455 | { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser }, |
| 456 | { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser }, |
| 457 | { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser }, |
| 458 | { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, |
| 459 | { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, |
| 460 | { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser }, |
| 461 | { "flag", SOURCE_NONE }, |
| 462 | { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser }, |
| 463 | { "color", SOURCE_NONE, FIELD_STR, color_atom_parser }, |
| 464 | { "align", SOURCE_NONE, FIELD_STR, align_atom_parser }, |
| 465 | { "end", SOURCE_NONE }, |
| 466 | { "if", SOURCE_NONE, FIELD_STR, if_atom_parser }, |
| 467 | { "then", SOURCE_NONE }, |
| 468 | { "else", SOURCE_NONE }, |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 469 | }; |
| 470 | |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 471 | #define REF_FORMATTING_STATE_INIT { 0, NULL } |
| 472 | |
| 473 | struct ref_formatting_stack { |
| 474 | struct ref_formatting_stack *prev; |
| 475 | struct strbuf output; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 476 | void (*at_end)(struct ref_formatting_stack **stack); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 477 | void *at_end_data; |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | struct ref_formatting_state { |
| 481 | int quote_style; |
| 482 | struct ref_formatting_stack *stack; |
| 483 | }; |
| 484 | |
Karthik Nayak | 3a25761 | 2015-08-22 09:09:37 +0530 | [diff] [blame] | 485 | struct atom_value { |
| 486 | const char *s; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 487 | int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state, |
| 488 | struct strbuf *err); |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 489 | uintmax_t value; /* used for sorting when not FIELD_STR */ |
Karthik Nayak | c58fc85 | 2017-01-10 14:19:35 +0530 | [diff] [blame] | 490 | struct used_atom *atom; |
Karthik Nayak | 3a25761 | 2015-08-22 09:09:37 +0530 | [diff] [blame] | 491 | }; |
| 492 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 493 | /* |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 494 | * Used to parse format string and sort specifiers |
| 495 | */ |
Jeff King | ab7ded3 | 2017-07-13 11:06:40 -0400 | [diff] [blame] | 496 | static int parse_ref_filter_atom(const struct ref_format *format, |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 497 | const char *atom, const char *ep, |
| 498 | struct strbuf *err) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 499 | { |
| 500 | const char *sp; |
Karthik Nayak | 4de707e | 2016-02-17 23:36:12 +0530 | [diff] [blame] | 501 | const char *arg; |
SZEDER Gábor | e94ce13 | 2016-10-02 18:35:11 +0200 | [diff] [blame] | 502 | int i, at, atom_len; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 503 | |
| 504 | sp = atom; |
| 505 | if (*sp == '*' && sp < ep) |
| 506 | sp++; /* deref */ |
| 507 | if (ep <= sp) |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 508 | return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"), |
| 509 | (int)(ep-atom), atom); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 510 | |
| 511 | /* Do we have the atom already used elsewhere? */ |
| 512 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 513 | int len = strlen(used_atom[i].name); |
| 514 | if (len == ep - atom && !memcmp(used_atom[i].name, atom, len)) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 515 | return i; |
| 516 | } |
| 517 | |
SZEDER Gábor | e94ce13 | 2016-10-02 18:35:11 +0200 | [diff] [blame] | 518 | /* |
| 519 | * If the atom name has a colon, strip it and everything after |
| 520 | * it off - it specifies the format for this entry, and |
| 521 | * shouldn't be used for checking against the valid_atom |
| 522 | * table. |
| 523 | */ |
| 524 | arg = memchr(sp, ':', ep - sp); |
| 525 | atom_len = (arg ? arg : ep) - sp; |
| 526 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 527 | /* Is the atom a valid one? */ |
| 528 | for (i = 0; i < ARRAY_SIZE(valid_atom); i++) { |
| 529 | int len = strlen(valid_atom[i].name); |
SZEDER Gábor | e94ce13 | 2016-10-02 18:35:11 +0200 | [diff] [blame] | 530 | if (len == atom_len && !memcmp(valid_atom[i].name, sp, len)) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 531 | break; |
| 532 | } |
| 533 | |
| 534 | if (ARRAY_SIZE(valid_atom) <= i) |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 535 | return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"), |
| 536 | (int)(ep-atom), atom); |
SZEDER Gábor | 47bd3d0 | 2018-11-14 13:27:25 +0100 | [diff] [blame] | 537 | if (valid_atom[i].source != SOURCE_NONE && !have_git_dir()) |
| 538 | return strbuf_addf_ret(err, -1, |
| 539 | _("not a git repository, but the field '%.*s' requires access to object data"), |
| 540 | (int)(ep-atom), atom); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 541 | |
| 542 | /* Add it in, including the deref prefix */ |
| 543 | at = used_atom_cnt; |
| 544 | used_atom_cnt++; |
| 545 | REALLOC_ARRAY(used_atom, used_atom_cnt); |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 546 | used_atom[at].name = xmemdupz(atom, ep - atom); |
| 547 | used_atom[at].type = valid_atom[i].cmp_type; |
Olga Telezhnaya | a8e7e38 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 548 | used_atom[at].source = valid_atom[i].source; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 549 | if (used_atom[at].source == SOURCE_OBJ) { |
| 550 | if (*atom == '*') |
| 551 | oi_deref.info.contentp = &oi_deref.content; |
| 552 | else |
| 553 | oi.info.contentp = &oi.content; |
| 554 | } |
Taylor Blau | bea4dbe | 2017-10-02 09:10:34 -0700 | [diff] [blame] | 555 | if (arg) { |
Karthik Nayak | 4de707e | 2016-02-17 23:36:12 +0530 | [diff] [blame] | 556 | arg = used_atom[at].name + (arg - atom) + 1; |
Taylor Blau | bea4dbe | 2017-10-02 09:10:34 -0700 | [diff] [blame] | 557 | if (!*arg) { |
| 558 | /* |
| 559 | * Treat empty sub-arguments list as NULL (i.e., |
| 560 | * "%(atom:)" is equivalent to "%(atom)"). |
| 561 | */ |
| 562 | arg = NULL; |
| 563 | } |
| 564 | } |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 565 | memset(&used_atom[at].u, 0, sizeof(used_atom[at].u)); |
Olga Telezhnaya | 74efea9 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 566 | if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err)) |
| 567 | return -1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 568 | if (*atom == '*') |
| 569 | need_tagged = 1; |
Karthik Nayak | 01f9582 | 2017-01-10 14:19:42 +0530 | [diff] [blame] | 570 | if (!strcmp(valid_atom[i].name, "symref")) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 571 | need_symref = 1; |
| 572 | return at; |
| 573 | } |
| 574 | |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 575 | static void quote_formatting(struct strbuf *s, const char *str, int quote_style) |
| 576 | { |
| 577 | switch (quote_style) { |
| 578 | case QUOTE_NONE: |
| 579 | strbuf_addstr(s, str); |
| 580 | break; |
| 581 | case QUOTE_SHELL: |
| 582 | sq_quote_buf(s, str); |
| 583 | break; |
| 584 | case QUOTE_PERL: |
| 585 | perl_quote_buf(s, str); |
| 586 | break; |
| 587 | case QUOTE_PYTHON: |
| 588 | python_quote_buf(s, str); |
| 589 | break; |
| 590 | case QUOTE_TCL: |
| 591 | tcl_quote_buf(s, str); |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 596 | static int append_atom(struct atom_value *v, struct ref_formatting_state *state, |
| 597 | struct strbuf *unused_err) |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 598 | { |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 599 | /* |
| 600 | * Quote formatting is only done when the stack has a single |
| 601 | * element. Otherwise quote formatting is done on the |
| 602 | * element's entire output strbuf when the %(end) atom is |
| 603 | * encountered. |
| 604 | */ |
| 605 | if (!state->stack->prev) |
| 606 | quote_formatting(&state->stack->output, v->s, state->quote_style); |
| 607 | else |
| 608 | strbuf_addstr(&state->stack->output, v->s); |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 609 | return 0; |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 610 | } |
| 611 | |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 612 | static void push_stack_element(struct ref_formatting_stack **stack) |
| 613 | { |
| 614 | struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack)); |
| 615 | |
| 616 | strbuf_init(&s->output, 0); |
| 617 | s->prev = *stack; |
| 618 | *stack = s; |
| 619 | } |
| 620 | |
| 621 | static void pop_stack_element(struct ref_formatting_stack **stack) |
| 622 | { |
| 623 | struct ref_formatting_stack *current = *stack; |
| 624 | struct ref_formatting_stack *prev = current->prev; |
| 625 | |
| 626 | if (prev) |
| 627 | strbuf_addbuf(&prev->output, ¤t->output); |
| 628 | strbuf_release(¤t->output); |
| 629 | free(current); |
| 630 | *stack = prev; |
| 631 | } |
| 632 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 633 | static void end_align_handler(struct ref_formatting_stack **stack) |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 634 | { |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 635 | struct ref_formatting_stack *cur = *stack; |
| 636 | struct align *align = (struct align *)cur->at_end_data; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 637 | struct strbuf s = STRBUF_INIT; |
| 638 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 639 | strbuf_utf8_align(&s, align->position, align->width, cur->output.buf); |
| 640 | strbuf_swap(&cur->output, &s); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 641 | strbuf_release(&s); |
| 642 | } |
| 643 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 644 | static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
| 645 | struct strbuf *unused_err) |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 646 | { |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 647 | struct ref_formatting_stack *new_stack; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 648 | |
| 649 | push_stack_element(&state->stack); |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 650 | new_stack = state->stack; |
| 651 | new_stack->at_end = end_align_handler; |
| 652 | new_stack->at_end_data = &atomv->atom->u.align; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 653 | return 0; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 654 | } |
| 655 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 656 | static void if_then_else_handler(struct ref_formatting_stack **stack) |
| 657 | { |
| 658 | struct ref_formatting_stack *cur = *stack; |
| 659 | struct ref_formatting_stack *prev = cur->prev; |
| 660 | struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data; |
| 661 | |
| 662 | if (!if_then_else->then_atom_seen) |
| 663 | die(_("format: %%(if) atom used without a %%(then) atom")); |
| 664 | |
| 665 | if (if_then_else->else_atom_seen) { |
| 666 | /* |
| 667 | * There is an %(else) atom: we need to drop one state from the |
| 668 | * stack, either the %(else) branch if the condition is satisfied, or |
| 669 | * the %(then) branch if it isn't. |
| 670 | */ |
| 671 | if (if_then_else->condition_satisfied) { |
| 672 | strbuf_reset(&cur->output); |
| 673 | pop_stack_element(&cur); |
| 674 | } else { |
| 675 | strbuf_swap(&cur->output, &prev->output); |
| 676 | strbuf_reset(&cur->output); |
| 677 | pop_stack_element(&cur); |
| 678 | } |
| 679 | } else if (!if_then_else->condition_satisfied) { |
| 680 | /* |
| 681 | * No %(else) atom: just drop the %(then) branch if the |
| 682 | * condition is not satisfied. |
| 683 | */ |
| 684 | strbuf_reset(&cur->output); |
| 685 | } |
| 686 | |
| 687 | *stack = cur; |
| 688 | free(if_then_else); |
| 689 | } |
| 690 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 691 | static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
| 692 | struct strbuf *unused_err) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 693 | { |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 694 | struct ref_formatting_stack *new_stack; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 695 | struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1); |
| 696 | |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 697 | if_then_else->str = atomv->atom->u.if_then_else.str; |
| 698 | if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status; |
| 699 | |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 700 | push_stack_element(&state->stack); |
Brandon Williams | 1472b5b | 2018-02-14 10:59:46 -0800 | [diff] [blame] | 701 | new_stack = state->stack; |
| 702 | new_stack->at_end = if_then_else_handler; |
| 703 | new_stack->at_end_data = if_then_else; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 704 | return 0; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | static int is_empty(const char *s) |
| 708 | { |
| 709 | while (*s != '\0') { |
| 710 | if (!isspace(*s)) |
| 711 | return 0; |
| 712 | s++; |
| 713 | } |
| 714 | return 1; |
| 715 | } |
| 716 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 717 | static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
| 718 | struct strbuf *err) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 719 | { |
| 720 | struct ref_formatting_stack *cur = state->stack; |
| 721 | struct if_then_else *if_then_else = NULL; |
| 722 | |
| 723 | if (cur->at_end == if_then_else_handler) |
| 724 | if_then_else = (struct if_then_else *)cur->at_end_data; |
| 725 | if (!if_then_else) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 726 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 727 | if (if_then_else->then_atom_seen) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 728 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 729 | if (if_then_else->else_atom_seen) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 730 | return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 731 | if_then_else->then_atom_seen = 1; |
| 732 | /* |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 733 | * If the 'equals' or 'notequals' attribute is used then |
| 734 | * perform the required comparison. If not, only non-empty |
| 735 | * strings satisfy the 'if' condition. |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 736 | */ |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 737 | if (if_then_else->cmp_status == COMPARE_EQUAL) { |
| 738 | if (!strcmp(if_then_else->str, cur->output.buf)) |
| 739 | if_then_else->condition_satisfied = 1; |
| 740 | } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) { |
| 741 | if (strcmp(if_then_else->str, cur->output.buf)) |
| 742 | if_then_else->condition_satisfied = 1; |
| 743 | } else if (cur->output.len && !is_empty(cur->output.buf)) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 744 | if_then_else->condition_satisfied = 1; |
| 745 | strbuf_reset(&cur->output); |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 746 | return 0; |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 747 | } |
| 748 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 749 | static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
| 750 | struct strbuf *err) |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 751 | { |
| 752 | struct ref_formatting_stack *prev = state->stack; |
| 753 | struct if_then_else *if_then_else = NULL; |
| 754 | |
| 755 | if (prev->at_end == if_then_else_handler) |
| 756 | if_then_else = (struct if_then_else *)prev->at_end_data; |
| 757 | if (!if_then_else) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 758 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 759 | if (!if_then_else->then_atom_seen) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 760 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 761 | if (if_then_else->else_atom_seen) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 762 | return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 763 | if_then_else->else_atom_seen = 1; |
| 764 | push_stack_element(&state->stack); |
| 765 | state->stack->at_end_data = prev->at_end_data; |
| 766 | state->stack->at_end = prev->at_end; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 767 | return 0; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 768 | } |
| 769 | |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 770 | static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state, |
| 771 | struct strbuf *err) |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 772 | { |
| 773 | struct ref_formatting_stack *current = state->stack; |
| 774 | struct strbuf s = STRBUF_INIT; |
| 775 | |
| 776 | if (!current->at_end) |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 777 | return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom")); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 778 | current->at_end(&state->stack); |
| 779 | |
| 780 | /* Stack may have been popped within at_end(), hence reset the current pointer */ |
| 781 | current = state->stack; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 782 | |
| 783 | /* |
| 784 | * Perform quote formatting when the stack element is that of |
| 785 | * a supporting atom. If nested then perform quote formatting |
| 786 | * only on the topmost supporting atom. |
| 787 | */ |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 788 | if (!current->prev->prev) { |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 789 | quote_formatting(&s, current->output.buf, state->quote_style); |
| 790 | strbuf_swap(¤t->output, &s); |
| 791 | } |
| 792 | strbuf_release(&s); |
| 793 | pop_stack_element(&state->stack); |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 794 | return 0; |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 795 | } |
| 796 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 797 | /* |
| 798 | * In a format string, find the next occurrence of %(atom). |
| 799 | */ |
| 800 | static const char *find_next(const char *cp) |
| 801 | { |
| 802 | while (*cp) { |
| 803 | if (*cp == '%') { |
| 804 | /* |
| 805 | * %( is the start of an atom; |
| 806 | * %% is a quoted per-cent. |
| 807 | */ |
| 808 | if (cp[1] == '(') |
| 809 | return cp; |
| 810 | else if (cp[1] == '%') |
| 811 | cp++; /* skip over two % */ |
| 812 | /* otherwise this is a singleton, literal % */ |
| 813 | } |
| 814 | cp++; |
| 815 | } |
| 816 | return NULL; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * Make sure the format string is well formed, and parse out |
| 821 | * the used atoms. |
| 822 | */ |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 823 | int verify_ref_format(struct ref_format *format) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 824 | { |
| 825 | const char *cp, *sp; |
| 826 | |
Jeff King | bf285ae | 2017-07-13 11:02:30 -0400 | [diff] [blame] | 827 | format->need_color_reset_at_eol = 0; |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 828 | for (cp = format->format; *cp && (sp = find_next(cp)); ) { |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 829 | struct strbuf err = STRBUF_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 830 | const char *color, *ep = strchr(sp, ')'); |
| 831 | int at; |
| 832 | |
| 833 | if (!ep) |
Nguyễn Thái Ngọc Duy | 1823c61 | 2016-02-27 13:42:04 +0700 | [diff] [blame] | 834 | return error(_("malformed format string %s"), sp); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 835 | /* sp points at "%(" and ep points at the closing ")" */ |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 836 | at = parse_ref_filter_atom(format, sp + 2, ep, &err); |
| 837 | if (at < 0) |
| 838 | die("%s", err.buf); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 839 | cp = ep + 1; |
| 840 | |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 841 | if (skip_prefix(used_atom[at].name, "color:", &color)) |
Jeff King | bf285ae | 2017-07-13 11:02:30 -0400 | [diff] [blame] | 842 | format->need_color_reset_at_eol = !!strcmp(color, "reset"); |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 843 | strbuf_release(&err); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 844 | } |
Jeff King | 11b087a | 2017-07-13 11:09:32 -0400 | [diff] [blame] | 845 | if (format->need_color_reset_at_eol && !want_color(format->use_color)) |
| 846 | format->need_color_reset_at_eol = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
brian m. carlson | 1776979 | 2018-03-12 02:27:27 +0000 | [diff] [blame] | 850 | static int grab_objectname(const char *name, const struct object_id *oid, |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 851 | struct atom_value *v, struct used_atom *atom) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 852 | { |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 853 | if (starts_with(name, "objectname")) { |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 854 | if (atom->u.objectname.option == O_SHORT) { |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 855 | v->s = xstrdup(find_unique_abbrev(oid, DEFAULT_ABBREV)); |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 856 | return 1; |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 857 | } else if (atom->u.objectname.option == O_FULL) { |
brian m. carlson | 1776979 | 2018-03-12 02:27:27 +0000 | [diff] [blame] | 858 | v->s = xstrdup(oid_to_hex(oid)); |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 859 | return 1; |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 860 | } else if (atom->u.objectname.option == O_LENGTH) { |
brian m. carlson | aab9583 | 2018-03-12 02:27:30 +0000 | [diff] [blame] | 861 | v->s = xstrdup(find_unique_abbrev(oid, atom->u.objectname.length)); |
Karthik Nayak | 42d0eb0 | 2017-01-10 14:19:37 +0530 | [diff] [blame] | 862 | return 1; |
Karthik Nayak | fe63c4d | 2016-02-17 23:36:19 +0530 | [diff] [blame] | 863 | } else |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 864 | BUG("unknown %%(objectname) option"); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 865 | } |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | /* See grab_values */ |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 870 | static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 871 | { |
| 872 | int i; |
| 873 | |
| 874 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 875 | const char *name = used_atom[i].name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 876 | struct atom_value *v = &val[i]; |
| 877 | if (!!deref != (*name == '*')) |
| 878 | continue; |
| 879 | if (deref) |
| 880 | name++; |
| 881 | if (!strcmp(name, "objecttype")) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 882 | v->s = xstrdup(type_name(oi->type)); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 883 | else if (!strcmp(name, "objectsize")) { |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 884 | v->value = oi->size; |
Torsten Bögershausen | ca473ce | 2018-11-11 08:05:04 +0100 | [diff] [blame] | 885 | v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 886 | } |
| 887 | else if (deref) |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 888 | grab_objectname(name, &oi->oid, v, &used_atom[i]); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
| 892 | /* See grab_values */ |
| 893 | static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) |
| 894 | { |
| 895 | int i; |
| 896 | struct tag *tag = (struct tag *) obj; |
| 897 | |
| 898 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 899 | const char *name = used_atom[i].name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 900 | struct atom_value *v = &val[i]; |
| 901 | if (!!deref != (*name == '*')) |
| 902 | continue; |
| 903 | if (deref) |
| 904 | name++; |
| 905 | if (!strcmp(name, "tag")) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 906 | v->s = xstrdup(tag->tag); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 907 | else if (!strcmp(name, "type") && tag->tagged) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 908 | v->s = xstrdup(type_name(tag->tagged->type)); |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 909 | else if (!strcmp(name, "object") && tag->tagged) |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 910 | v->s = xstrdup(oid_to_hex(&tag->tagged->oid)); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | /* See grab_values */ |
| 915 | static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) |
| 916 | { |
| 917 | int i; |
| 918 | struct commit *commit = (struct commit *) obj; |
| 919 | |
| 920 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 921 | const char *name = used_atom[i].name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 922 | struct atom_value *v = &val[i]; |
| 923 | if (!!deref != (*name == '*')) |
| 924 | continue; |
| 925 | if (deref) |
| 926 | name++; |
| 927 | if (!strcmp(name, "tree")) { |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 928 | v->s = xstrdup(oid_to_hex(get_commit_tree_oid(commit))); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 929 | } |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 930 | else if (!strcmp(name, "numparent")) { |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 931 | v->value = commit_list_count(commit->parents); |
| 932 | v->s = xstrfmt("%lu", (unsigned long)v->value); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 933 | } |
| 934 | else if (!strcmp(name, "parent")) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 935 | struct commit_list *parents; |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 936 | struct strbuf s = STRBUF_INIT; |
| 937 | for (parents = commit->parents; parents; parents = parents->next) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 938 | struct commit *parent = parents->item; |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 939 | if (parents != commit->parents) |
| 940 | strbuf_addch(&s, ' '); |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 941 | strbuf_addstr(&s, oid_to_hex(&parent->object.oid)); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 942 | } |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 943 | v->s = strbuf_detach(&s, NULL); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz) |
| 949 | { |
| 950 | const char *eol; |
| 951 | while (*buf) { |
| 952 | if (!strncmp(buf, who, wholen) && |
| 953 | buf[wholen] == ' ') |
| 954 | return buf + wholen + 1; |
| 955 | eol = strchr(buf, '\n'); |
| 956 | if (!eol) |
| 957 | return ""; |
| 958 | eol++; |
| 959 | if (*eol == '\n') |
| 960 | return ""; /* end of header */ |
| 961 | buf = eol; |
| 962 | } |
| 963 | return ""; |
| 964 | } |
| 965 | |
| 966 | static const char *copy_line(const char *buf) |
| 967 | { |
| 968 | const char *eol = strchrnul(buf, '\n'); |
| 969 | return xmemdupz(buf, eol - buf); |
| 970 | } |
| 971 | |
| 972 | static const char *copy_name(const char *buf) |
| 973 | { |
| 974 | const char *cp; |
| 975 | for (cp = buf; *cp && *cp != '\n'; cp++) { |
| 976 | if (!strncmp(cp, " <", 2)) |
| 977 | return xmemdupz(buf, cp - buf); |
| 978 | } |
| 979 | return ""; |
| 980 | } |
| 981 | |
| 982 | static const char *copy_email(const char *buf) |
| 983 | { |
| 984 | const char *email = strchr(buf, '<'); |
| 985 | const char *eoemail; |
| 986 | if (!email) |
| 987 | return ""; |
| 988 | eoemail = strchr(email, '>'); |
| 989 | if (!eoemail) |
| 990 | return ""; |
| 991 | return xmemdupz(email, eoemail + 1 - email); |
| 992 | } |
| 993 | |
| 994 | static char *copy_subject(const char *buf, unsigned long len) |
| 995 | { |
| 996 | char *r = xmemdupz(buf, len); |
| 997 | int i; |
| 998 | |
| 999 | for (i = 0; i < len; i++) |
| 1000 | if (r[i] == '\n') |
| 1001 | r[i] = ' '; |
| 1002 | |
| 1003 | return r; |
| 1004 | } |
| 1005 | |
| 1006 | static void grab_date(const char *buf, struct atom_value *v, const char *atomname) |
| 1007 | { |
| 1008 | const char *eoemail = strstr(buf, "> "); |
| 1009 | char *zone; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 1010 | timestamp_t timestamp; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1011 | long tz; |
Junio C Hamano | d939af1 | 2015-08-03 11:01:27 -0700 | [diff] [blame] | 1012 | struct date_mode date_mode = { DATE_NORMAL }; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1013 | const char *formatp; |
| 1014 | |
| 1015 | /* |
| 1016 | * We got here because atomname ends in "date" or "date<something>"; |
| 1017 | * it's not possible that <something> is not ":<format>" because |
| 1018 | * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no |
| 1019 | * ":" means no format is specified, and use the default. |
| 1020 | */ |
| 1021 | formatp = strchr(atomname, ':'); |
| 1022 | if (formatp != NULL) { |
| 1023 | formatp++; |
Junio C Hamano | d939af1 | 2015-08-03 11:01:27 -0700 | [diff] [blame] | 1024 | parse_date_format(formatp, &date_mode); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | if (!eoemail) |
| 1028 | goto bad; |
Johannes Schindelin | 1aeb7e7 | 2017-04-21 12:45:44 +0200 | [diff] [blame] | 1029 | timestamp = parse_timestamp(eoemail + 2, &zone, 10); |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 1030 | if (timestamp == TIME_MAX) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1031 | goto bad; |
| 1032 | tz = strtol(zone, NULL, 10); |
| 1033 | if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE) |
| 1034 | goto bad; |
Junio C Hamano | d939af1 | 2015-08-03 11:01:27 -0700 | [diff] [blame] | 1035 | v->s = xstrdup(show_date(timestamp, tz, &date_mode)); |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 1036 | v->value = timestamp; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1037 | return; |
| 1038 | bad: |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1039 | v->s = xstrdup(""); |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 1040 | v->value = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* See grab_values */ |
| 1044 | static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) |
| 1045 | { |
| 1046 | int i; |
| 1047 | int wholen = strlen(who); |
| 1048 | const char *wholine = NULL; |
| 1049 | |
| 1050 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1051 | const char *name = used_atom[i].name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1052 | struct atom_value *v = &val[i]; |
| 1053 | if (!!deref != (*name == '*')) |
| 1054 | continue; |
| 1055 | if (deref) |
| 1056 | name++; |
| 1057 | if (strncmp(who, name, wholen)) |
| 1058 | continue; |
| 1059 | if (name[wholen] != 0 && |
| 1060 | strcmp(name + wholen, "name") && |
| 1061 | strcmp(name + wholen, "email") && |
| 1062 | !starts_with(name + wholen, "date")) |
| 1063 | continue; |
| 1064 | if (!wholine) |
| 1065 | wholine = find_wholine(who, wholen, buf, sz); |
| 1066 | if (!wholine) |
| 1067 | return; /* no point looking for it */ |
| 1068 | if (name[wholen] == 0) |
| 1069 | v->s = copy_line(wholine); |
| 1070 | else if (!strcmp(name + wholen, "name")) |
| 1071 | v->s = copy_name(wholine); |
| 1072 | else if (!strcmp(name + wholen, "email")) |
| 1073 | v->s = copy_email(wholine); |
| 1074 | else if (starts_with(name + wholen, "date")) |
| 1075 | grab_date(wholine, v, name); |
| 1076 | } |
| 1077 | |
| 1078 | /* |
| 1079 | * For a tag or a commit object, if "creator" or "creatordate" is |
| 1080 | * requested, do something special. |
| 1081 | */ |
| 1082 | if (strcmp(who, "tagger") && strcmp(who, "committer")) |
| 1083 | return; /* "author" for commit object is not wanted */ |
| 1084 | if (!wholine) |
| 1085 | wholine = find_wholine(who, wholen, buf, sz); |
| 1086 | if (!wholine) |
| 1087 | return; |
| 1088 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1089 | const char *name = used_atom[i].name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1090 | struct atom_value *v = &val[i]; |
| 1091 | if (!!deref != (*name == '*')) |
| 1092 | continue; |
| 1093 | if (deref) |
| 1094 | name++; |
| 1095 | |
| 1096 | if (starts_with(name, "creatordate")) |
| 1097 | grab_date(wholine, v, name); |
| 1098 | else if (!strcmp(name, "creator")) |
| 1099 | v->s = copy_line(wholine); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | static void find_subpos(const char *buf, unsigned long sz, |
| 1104 | const char **sub, unsigned long *sublen, |
| 1105 | const char **body, unsigned long *bodylen, |
| 1106 | unsigned long *nonsiglen, |
| 1107 | const char **sig, unsigned long *siglen) |
| 1108 | { |
| 1109 | const char *eol; |
| 1110 | /* skip past header until we hit empty line */ |
| 1111 | while (*buf && *buf != '\n') { |
| 1112 | eol = strchrnul(buf, '\n'); |
| 1113 | if (*eol) |
| 1114 | eol++; |
| 1115 | buf = eol; |
| 1116 | } |
| 1117 | /* skip any empty lines */ |
| 1118 | while (*buf == '\n') |
| 1119 | buf++; |
| 1120 | |
| 1121 | /* parse signature first; we might not even have a subject line */ |
| 1122 | *sig = buf + parse_signature(buf, strlen(buf)); |
| 1123 | *siglen = strlen(*sig); |
| 1124 | |
| 1125 | /* subject is first non-empty line */ |
| 1126 | *sub = buf; |
| 1127 | /* subject goes to first empty line */ |
| 1128 | while (buf < *sig && *buf && *buf != '\n') { |
| 1129 | eol = strchrnul(buf, '\n'); |
| 1130 | if (*eol) |
| 1131 | eol++; |
| 1132 | buf = eol; |
| 1133 | } |
| 1134 | *sublen = buf - *sub; |
| 1135 | /* drop trailing newline, if present */ |
| 1136 | if (*sublen && (*sub)[*sublen - 1] == '\n') |
| 1137 | *sublen -= 1; |
| 1138 | |
| 1139 | /* skip any empty lines */ |
| 1140 | while (*buf == '\n') |
| 1141 | buf++; |
| 1142 | *body = buf; |
| 1143 | *bodylen = strlen(buf); |
| 1144 | *nonsiglen = *sig - buf; |
| 1145 | } |
| 1146 | |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1147 | /* |
| 1148 | * If 'lines' is greater than 0, append that many lines from the given |
| 1149 | * 'buf' of length 'size' to the given strbuf. |
| 1150 | */ |
| 1151 | static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines) |
| 1152 | { |
| 1153 | int i; |
| 1154 | const char *sp, *eol; |
| 1155 | size_t len; |
| 1156 | |
| 1157 | sp = buf; |
| 1158 | |
| 1159 | for (i = 0; i < lines && sp < buf + size; i++) { |
| 1160 | if (i) |
| 1161 | strbuf_addstr(out, "\n "); |
| 1162 | eol = memchr(sp, '\n', size - (sp - buf)); |
| 1163 | len = eol ? eol - sp : size - (sp - buf); |
| 1164 | strbuf_add(out, sp, len); |
| 1165 | if (!eol) |
| 1166 | break; |
| 1167 | sp = eol + 1; |
| 1168 | } |
| 1169 | } |
| 1170 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1171 | /* See grab_values */ |
| 1172 | static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) |
| 1173 | { |
| 1174 | int i; |
| 1175 | const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL; |
| 1176 | unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0; |
| 1177 | |
| 1178 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1179 | struct used_atom *atom = &used_atom[i]; |
| 1180 | const char *name = atom->name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1181 | struct atom_value *v = &val[i]; |
| 1182 | if (!!deref != (*name == '*')) |
| 1183 | continue; |
| 1184 | if (deref) |
| 1185 | name++; |
| 1186 | if (strcmp(name, "subject") && |
| 1187 | strcmp(name, "body") && |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 1188 | !starts_with(name, "trailers") && |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1189 | !starts_with(name, "contents")) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1190 | continue; |
| 1191 | if (!subpos) |
| 1192 | find_subpos(buf, sz, |
| 1193 | &subpos, &sublen, |
| 1194 | &bodypos, &bodylen, &nonsiglen, |
| 1195 | &sigpos, &siglen); |
| 1196 | |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1197 | if (atom->u.contents.option == C_SUB) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1198 | v->s = copy_subject(subpos, sublen); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1199 | else if (atom->u.contents.option == C_BODY_DEP) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1200 | v->s = xmemdupz(bodypos, bodylen); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1201 | else if (atom->u.contents.option == C_BODY) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1202 | v->s = xmemdupz(bodypos, nonsiglen); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1203 | else if (atom->u.contents.option == C_SIG) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1204 | v->s = xmemdupz(sigpos, siglen); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1205 | else if (atom->u.contents.option == C_LINES) { |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1206 | struct strbuf s = STRBUF_INIT; |
| 1207 | const char *contents_end = bodylen + bodypos - siglen; |
| 1208 | |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1209 | /* Size is the length of the message after removing the signature */ |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1210 | append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines); |
Karthik Nayak | 1bb38e5 | 2015-09-11 20:34:16 +0530 | [diff] [blame] | 1211 | v->s = strbuf_detach(&s, NULL); |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 1212 | } else if (atom->u.contents.option == C_TRAILERS) { |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 1213 | struct strbuf s = STRBUF_INIT; |
Jacob Keller | b1d31c8 | 2016-11-18 16:58:15 -0800 | [diff] [blame] | 1214 | |
Taylor Blau | 67a20a0 | 2017-10-01 22:25:23 -0700 | [diff] [blame] | 1215 | /* Format the trailer info according to the trailer_opts given */ |
| 1216 | format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts); |
| 1217 | |
| 1218 | v->s = strbuf_detach(&s, NULL); |
Karthik Nayak | 452db39 | 2016-02-17 23:36:18 +0530 | [diff] [blame] | 1219 | } else if (atom->u.contents.option == C_BARE) |
| 1220 | v->s = xstrdup(subpos); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * We want to have empty print-string for field requests |
| 1226 | * that do not apply (e.g. "authordate" for a tag object) |
| 1227 | */ |
| 1228 | static void fill_missing_values(struct atom_value *val) |
| 1229 | { |
| 1230 | int i; |
| 1231 | for (i = 0; i < used_atom_cnt; i++) { |
| 1232 | struct atom_value *v = &val[i]; |
| 1233 | if (v->s == NULL) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1234 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * val is a list of atom_value to hold returned values. Extract |
| 1240 | * the values for atoms in used_atom array out of (obj, buf, sz). |
| 1241 | * when deref is false, (obj, buf, sz) is the object that is |
| 1242 | * pointed at by the ref itself; otherwise it is the object the |
| 1243 | * ref (which is a tag) refers to. |
| 1244 | */ |
| 1245 | static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz) |
| 1246 | { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1247 | switch (obj->type) { |
| 1248 | case OBJ_TAG: |
| 1249 | grab_tag_values(val, deref, obj, buf, sz); |
| 1250 | grab_sub_body_contents(val, deref, obj, buf, sz); |
| 1251 | grab_person("tagger", val, deref, obj, buf, sz); |
| 1252 | break; |
| 1253 | case OBJ_COMMIT: |
| 1254 | grab_commit_values(val, deref, obj, buf, sz); |
| 1255 | grab_sub_body_contents(val, deref, obj, buf, sz); |
| 1256 | grab_person("author", val, deref, obj, buf, sz); |
| 1257 | grab_person("committer", val, deref, obj, buf, sz); |
| 1258 | break; |
| 1259 | case OBJ_TREE: |
| 1260 | /* grab_tree_values(val, deref, obj, buf, sz); */ |
| 1261 | break; |
| 1262 | case OBJ_BLOB: |
| 1263 | /* grab_blob_values(val, deref, obj, buf, sz); */ |
| 1264 | break; |
| 1265 | default: |
| 1266 | die("Eh? Object of type %d?", obj->type); |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | static inline char *copy_advance(char *dst, const char *src) |
| 1271 | { |
| 1272 | while (*src) |
| 1273 | *dst++ = *src++; |
| 1274 | return dst; |
| 1275 | } |
| 1276 | |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 1277 | static const char *lstrip_ref_components(const char *refname, int len) |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 1278 | { |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1279 | long remaining = len; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1280 | const char *start = xstrdup(refname); |
| 1281 | const char *to_free = start; |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 1282 | |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 1283 | if (len < 0) { |
| 1284 | int i; |
| 1285 | const char *p = refname; |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 1286 | |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 1287 | /* Find total no of '/' separated path-components */ |
| 1288 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) |
| 1289 | ; |
| 1290 | /* |
| 1291 | * The number of components we need to strip is now |
| 1292 | * the total minus the components to be left (Plus one |
| 1293 | * because we count the number of '/', but the number |
| 1294 | * of components is one more than the no of '/'). |
| 1295 | */ |
| 1296 | remaining = i + len + 1; |
| 1297 | } |
| 1298 | |
| 1299 | while (remaining > 0) { |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 1300 | switch (*start++) { |
| 1301 | case '\0': |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1302 | free((char *)to_free); |
| 1303 | return xstrdup(""); |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 1304 | case '/': |
| 1305 | remaining--; |
| 1306 | break; |
| 1307 | } |
| 1308 | } |
Karthik Nayak | 1a0ca5e | 2017-01-10 14:19:48 +0530 | [diff] [blame] | 1309 | |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1310 | start = xstrdup(start); |
| 1311 | free((char *)to_free); |
Jeff King | 0571979 | 2016-01-25 22:00:05 -0500 | [diff] [blame] | 1312 | return start; |
| 1313 | } |
| 1314 | |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 1315 | static const char *rstrip_ref_components(const char *refname, int len) |
| 1316 | { |
| 1317 | long remaining = len; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1318 | const char *start = xstrdup(refname); |
| 1319 | const char *to_free = start; |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 1320 | |
| 1321 | if (len < 0) { |
| 1322 | int i; |
| 1323 | const char *p = refname; |
| 1324 | |
| 1325 | /* Find total no of '/' separated path-components */ |
| 1326 | for (i = 0; p[i]; p[i] == '/' ? i++ : *p++) |
| 1327 | ; |
| 1328 | /* |
| 1329 | * The number of components we need to strip is now |
| 1330 | * the total minus the components to be left (Plus one |
| 1331 | * because we count the number of '/', but the number |
| 1332 | * of components is one more than the no of '/'). |
| 1333 | */ |
| 1334 | remaining = i + len + 1; |
| 1335 | } |
| 1336 | |
| 1337 | while (remaining-- > 0) { |
| 1338 | char *p = strrchr(start, '/'); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1339 | if (p == NULL) { |
| 1340 | free((char *)to_free); |
| 1341 | return xstrdup(""); |
| 1342 | } else |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 1343 | p[0] = '\0'; |
| 1344 | } |
| 1345 | return start; |
| 1346 | } |
| 1347 | |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1348 | static const char *show_ref(struct refname_atom *atom, const char *refname) |
| 1349 | { |
| 1350 | if (atom->option == R_SHORT) |
| 1351 | return shorten_unambiguous_ref(refname, warn_ambiguous_refs); |
Karthik Nayak | 17938f1 | 2017-01-10 14:19:46 +0530 | [diff] [blame] | 1352 | else if (atom->option == R_LSTRIP) |
| 1353 | return lstrip_ref_components(refname, atom->lstrip); |
Karthik Nayak | 1a34728 | 2017-01-10 14:19:49 +0530 | [diff] [blame] | 1354 | else if (atom->option == R_RSTRIP) |
| 1355 | return rstrip_ref_components(refname, atom->rstrip); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1356 | else |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1357 | return xstrdup(refname); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1358 | } |
| 1359 | |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1360 | static void fill_remote_ref_details(struct used_atom *atom, const char *refname, |
| 1361 | struct branch *branch, const char **s) |
| 1362 | { |
| 1363 | int num_ours, num_theirs; |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 1364 | if (atom->u.remote_ref.option == RR_REF) |
| 1365 | *s = show_ref(&atom->u.remote_ref.refname, refname); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 1366 | else if (atom->u.remote_ref.option == RR_TRACK) { |
Jeff Hostetler | d7d1b49 | 2018-01-09 18:50:15 +0000 | [diff] [blame] | 1367 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
| 1368 | NULL, AHEAD_BEHIND_FULL) < 0) { |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 1369 | *s = xstrdup(msgs.gone); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 1370 | } else if (!num_ours && !num_theirs) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1371 | *s = xstrdup(""); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1372 | else if (!num_ours) |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 1373 | *s = xstrfmt(msgs.behind, num_theirs); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1374 | else if (!num_theirs) |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 1375 | *s = xstrfmt(msgs.ahead, num_ours); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1376 | else |
Karthik Nayak | 6eac70f | 2017-01-10 14:19:50 +0530 | [diff] [blame] | 1377 | *s = xstrfmt(msgs.ahead_behind, |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1378 | num_ours, num_theirs); |
Karthik Nayak | 7743fcc | 2017-01-10 14:19:41 +0530 | [diff] [blame] | 1379 | if (!atom->u.remote_ref.nobracket && *s[0]) { |
| 1380 | const char *to_free = *s; |
| 1381 | *s = xstrfmt("[%s]", *s); |
| 1382 | free((void *)to_free); |
| 1383 | } |
| 1384 | } else if (atom->u.remote_ref.option == RR_TRACKSHORT) { |
Jeff Hostetler | d7d1b49 | 2018-01-09 18:50:15 +0000 | [diff] [blame] | 1385 | if (stat_tracking_info(branch, &num_ours, &num_theirs, |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1386 | NULL, AHEAD_BEHIND_FULL) < 0) { |
| 1387 | *s = xstrdup(""); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1388 | return; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1389 | } |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1390 | if (!num_ours && !num_theirs) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1391 | *s = xstrdup("="); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1392 | else if (!num_ours) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1393 | *s = xstrdup("<"); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1394 | else if (!num_theirs) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1395 | *s = xstrdup(">"); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1396 | else |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1397 | *s = xstrdup("<>"); |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 1398 | } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) { |
| 1399 | int explicit; |
| 1400 | const char *remote = atom->u.remote_ref.push ? |
| 1401 | pushremote_for_branch(branch, &explicit) : |
| 1402 | remote_for_branch(branch, &explicit); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1403 | *s = xstrdup(explicit ? remote : ""); |
J Wyman | 9700fae | 2017-11-07 17:31:08 +0100 | [diff] [blame] | 1404 | } else if (atom->u.remote_ref.option == RR_REMOTE_REF) { |
| 1405 | int explicit; |
| 1406 | const char *merge; |
| 1407 | |
| 1408 | merge = remote_ref_for_branch(branch, atom->u.remote_ref.push, |
| 1409 | &explicit); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1410 | *s = xstrdup(explicit ? merge : ""); |
Karthik Nayak | 3ba308c | 2017-01-10 14:19:45 +0530 | [diff] [blame] | 1411 | } else |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 1412 | BUG("unhandled RR_* enum"); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1413 | } |
| 1414 | |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 1415 | char *get_head_description(void) |
| 1416 | { |
| 1417 | struct strbuf desc = STRBUF_INIT; |
| 1418 | struct wt_status_state state; |
| 1419 | memset(&state, 0, sizeof(state)); |
| 1420 | wt_status_get_state(&state, 1); |
| 1421 | if (state.rebase_in_progress || |
Kaartic Sivaraam | a236f90 | 2018-04-03 10:01:00 +0530 | [diff] [blame] | 1422 | state.rebase_interactive_in_progress) { |
| 1423 | if (state.branch) |
| 1424 | strbuf_addf(&desc, _("(no branch, rebasing %s)"), |
| 1425 | state.branch); |
| 1426 | else |
| 1427 | strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"), |
| 1428 | state.detached_from); |
| 1429 | } else if (state.bisect_in_progress) |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 1430 | strbuf_addf(&desc, _("(no branch, bisect started on %s)"), |
| 1431 | state.branch); |
| 1432 | else if (state.detached_from) { |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 1433 | if (state.detached_at) |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 1434 | /* |
| 1435 | * TRANSLATORS: make sure this matches "HEAD |
| 1436 | * detached at " in wt-status.c |
| 1437 | */ |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 1438 | strbuf_addf(&desc, _("(HEAD detached at %s)"), |
| 1439 | state.detached_from); |
| 1440 | else |
Ævar Arnfjörð Bjarmason | 66f5f6d | 2017-05-11 21:20:12 +0000 | [diff] [blame] | 1441 | /* |
| 1442 | * TRANSLATORS: make sure this matches "HEAD |
| 1443 | * detached from " in wt-status.c |
| 1444 | */ |
Karthik Nayak | d4919bb | 2017-01-10 14:19:38 +0530 | [diff] [blame] | 1445 | strbuf_addf(&desc, _("(HEAD detached from %s)"), |
| 1446 | state.detached_from); |
| 1447 | } |
| 1448 | else |
| 1449 | strbuf_addstr(&desc, _("(no branch)")); |
| 1450 | free(state.branch); |
| 1451 | free(state.onto); |
| 1452 | free(state.detached_from); |
| 1453 | return strbuf_detach(&desc, NULL); |
| 1454 | } |
| 1455 | |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1456 | static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref) |
| 1457 | { |
| 1458 | if (!ref->symref) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1459 | return xstrdup(""); |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1460 | else |
| 1461 | return show_ref(&atom->u.refname, ref->symref); |
| 1462 | } |
| 1463 | |
| 1464 | static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref) |
| 1465 | { |
| 1466 | if (ref->kind & FILTER_REFS_DETACHED_HEAD) |
| 1467 | return get_head_description(); |
| 1468 | return show_ref(&atom->u.refname, ref->refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1469 | } |
| 1470 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1471 | static int get_object(struct ref_array_item *ref, int deref, struct object **obj, |
| 1472 | struct expand_data *oi, struct strbuf *err) |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 1473 | { |
Olga Telezhnaya | 04f6ee1 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1474 | /* parse_object_buffer() will set eaten to 0 if free() will be needed */ |
| 1475 | int eaten = 1; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1476 | if (oi->info.contentp) { |
| 1477 | /* We need to know that to use parse_object_buffer properly */ |
| 1478 | oi->info.sizep = &oi->size; |
| 1479 | oi->info.typep = &oi->type; |
Olga Telezhnaya | e225517 | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1480 | } |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1481 | if (oid_object_info_extended(the_repository, &oi->oid, &oi->info, |
| 1482 | OBJECT_INFO_LOOKUP_REPLACE)) |
| 1483 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), |
| 1484 | oid_to_hex(&oi->oid), ref->refname); |
| 1485 | |
| 1486 | if (oi->info.contentp) { |
Junio C Hamano | c83149a | 2018-08-17 13:09:57 -0700 | [diff] [blame] | 1487 | *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten); |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1488 | if (!obj) { |
| 1489 | if (!eaten) |
| 1490 | free(oi->content); |
| 1491 | return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"), |
| 1492 | oid_to_hex(&oi->oid), ref->refname); |
| 1493 | } |
| 1494 | grab_values(ref->value, deref, *obj, oi->content, oi->size); |
| 1495 | } |
| 1496 | |
| 1497 | grab_common_values(ref->value, deref, oi); |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 1498 | if (!eaten) |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1499 | free(oi->content); |
| 1500 | return 0; |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1503 | /* |
| 1504 | * Parse the object referred by ref, and grab needed value. |
| 1505 | */ |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1506 | static int populate_value(struct ref_array_item *ref, struct strbuf *err) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1507 | { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1508 | struct object *obj; |
Olga Telezhnaya | 2bbc6e8 | 2018-02-21 06:59:00 +0000 | [diff] [blame] | 1509 | int i; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1510 | struct object_info empty = OBJECT_INFO_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1511 | |
| 1512 | ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value)); |
| 1513 | |
| 1514 | if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1515 | ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, |
René Scharfe | efbd4fd | 2017-10-01 09:29:03 +0200 | [diff] [blame] | 1516 | NULL, NULL); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1517 | if (!ref->symref) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1518 | ref->symref = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | /* Fill in specials first */ |
| 1522 | for (i = 0; i < used_atom_cnt; i++) { |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 1523 | struct used_atom *atom = &used_atom[i]; |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 1524 | const char *name = used_atom[i].name; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1525 | struct atom_value *v = &ref->value[i]; |
| 1526 | int deref = 0; |
| 1527 | const char *refname; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1528 | struct branch *branch = NULL; |
| 1529 | |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 1530 | v->handler = append_atom; |
Karthik Nayak | c58fc85 | 2017-01-10 14:19:35 +0530 | [diff] [blame] | 1531 | v->atom = atom; |
Karthik Nayak | 63d89fb | 2015-09-10 21:18:20 +0530 | [diff] [blame] | 1532 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1533 | if (*name == '*') { |
| 1534 | deref = 1; |
| 1535 | name++; |
| 1536 | } |
| 1537 | |
| 1538 | if (starts_with(name, "refname")) |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1539 | refname = get_refname(atom, ref); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1540 | else if (starts_with(name, "symref")) |
Karthik Nayak | a798410 | 2017-01-10 14:19:44 +0530 | [diff] [blame] | 1541 | refname = get_symref(atom, ref); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1542 | else if (starts_with(name, "upstream")) { |
| 1543 | const char *branch_name; |
| 1544 | /* only local branches may have an upstream */ |
| 1545 | if (!skip_prefix(ref->refname, "refs/heads/", |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1546 | &branch_name)) { |
| 1547 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1548 | continue; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1549 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1550 | branch = branch_get(branch_name); |
| 1551 | |
| 1552 | refname = branch_get_upstream(branch, NULL); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1553 | if (refname) |
| 1554 | fill_remote_ref_details(atom, refname, branch, &v->s); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1555 | else |
| 1556 | v->s = xstrdup(""); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1557 | continue; |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 1558 | } else if (atom->u.remote_ref.push) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1559 | const char *branch_name; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1560 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1561 | if (!skip_prefix(ref->refname, "refs/heads/", |
| 1562 | &branch_name)) |
| 1563 | continue; |
| 1564 | branch = branch_get(branch_name); |
| 1565 | |
Johannes Schindelin | cc72385 | 2017-10-05 14:19:09 +0200 | [diff] [blame] | 1566 | if (atom->u.remote_ref.push_remote) |
| 1567 | refname = NULL; |
| 1568 | else { |
| 1569 | refname = branch_get_push(branch, NULL); |
| 1570 | if (!refname) |
| 1571 | continue; |
| 1572 | } |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1573 | /* We will definitely re-init v->s on the next line. */ |
| 1574 | free((char *)v->s); |
Karthik Nayak | 5339bda | 2016-02-17 23:36:17 +0530 | [diff] [blame] | 1575 | fill_remote_ref_details(atom, refname, branch, &v->s); |
| 1576 | continue; |
Karthik Nayak | fd935cc | 2016-02-17 23:36:13 +0530 | [diff] [blame] | 1577 | } else if (starts_with(name, "color:")) { |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1578 | v->s = xstrdup(atom->u.color); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1579 | continue; |
| 1580 | } else if (!strcmp(name, "flag")) { |
| 1581 | char buf[256], *cp = buf; |
| 1582 | if (ref->flag & REF_ISSYMREF) |
| 1583 | cp = copy_advance(cp, ",symref"); |
| 1584 | if (ref->flag & REF_ISPACKED) |
| 1585 | cp = copy_advance(cp, ",packed"); |
| 1586 | if (cp == buf) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1587 | v->s = xstrdup(""); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1588 | else { |
| 1589 | *cp = '\0'; |
| 1590 | v->s = xstrdup(buf + 1); |
| 1591 | } |
| 1592 | continue; |
brian m. carlson | 1776979 | 2018-03-12 02:27:27 +0000 | [diff] [blame] | 1593 | } else if (!deref && grab_objectname(name, &ref->objectname, v, atom)) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1594 | continue; |
| 1595 | } else if (!strcmp(name, "HEAD")) { |
Jeff King | 613a0e5 | 2017-05-19 02:12:12 -0400 | [diff] [blame] | 1596 | if (atom->u.head && !strcmp(ref->refname, atom->u.head)) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1597 | v->s = xstrdup("*"); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1598 | else |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1599 | v->s = xstrdup(" "); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1600 | continue; |
Karthik Nayak | 5bd881d | 2016-02-17 23:36:15 +0530 | [diff] [blame] | 1601 | } else if (starts_with(name, "align")) { |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1602 | v->handler = align_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1603 | v->s = xstrdup(""); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1604 | continue; |
| 1605 | } else if (!strcmp(name, "end")) { |
| 1606 | v->handler = end_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1607 | v->s = xstrdup(""); |
Karthik Nayak | ce59208 | 2015-09-11 20:33:07 +0530 | [diff] [blame] | 1608 | continue; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 1609 | } else if (starts_with(name, "if")) { |
| 1610 | const char *s; |
Karthik Nayak | 4f3e3b3 | 2017-01-10 14:19:36 +0530 | [diff] [blame] | 1611 | if (skip_prefix(name, "if:", &s)) |
| 1612 | v->s = xstrdup(s); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1613 | else |
| 1614 | v->s = xstrdup(""); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1615 | v->handler = if_atom_handler; |
| 1616 | continue; |
| 1617 | } else if (!strcmp(name, "then")) { |
| 1618 | v->handler = then_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1619 | v->s = xstrdup(""); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1620 | continue; |
| 1621 | } else if (!strcmp(name, "else")) { |
| 1622 | v->handler = else_atom_handler; |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1623 | v->s = xstrdup(""); |
Karthik Nayak | c58492d | 2017-01-10 14:19:34 +0530 | [diff] [blame] | 1624 | continue; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1625 | } else |
| 1626 | continue; |
| 1627 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1628 | if (!deref) |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1629 | v->s = xstrdup(refname); |
Jeff King | a5e03bf | 2015-09-24 17:07:12 -0400 | [diff] [blame] | 1630 | else |
| 1631 | v->s = xstrfmt("%s^{}", refname); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 1632 | free((char *)refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | for (i = 0; i < used_atom_cnt; i++) { |
| 1636 | struct atom_value *v = &ref->value[i]; |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1637 | if (v->s == NULL && used_atom[i].source == SOURCE_NONE) |
| 1638 | return strbuf_addf_ret(err, -1, _("missing object %s for %s"), |
| 1639 | oid_to_hex(&ref->objectname), ref->refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1640 | } |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1641 | |
| 1642 | if (need_tagged) |
| 1643 | oi.info.contentp = &oi.content; |
| 1644 | if (!memcmp(&oi.info, &empty, sizeof(empty)) && |
| 1645 | !memcmp(&oi_deref.info, &empty, sizeof(empty))) |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1646 | return 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1647 | |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1648 | |
| 1649 | oi.oid = ref->objectname; |
| 1650 | if (get_object(ref, 0, &obj, &oi, err)) |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1651 | return -1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1652 | |
| 1653 | /* |
| 1654 | * If there is no atom that wants to know about tagged |
| 1655 | * object, we are done. |
| 1656 | */ |
| 1657 | if (!need_tagged || (obj->type != OBJ_TAG)) |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1658 | return 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1659 | |
| 1660 | /* |
| 1661 | * If it is a tag object, see if we use a value that derefs |
| 1662 | * the object, and if we do grab the object it refers to. |
| 1663 | */ |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1664 | oi_deref.oid = ((struct tag *)obj)->tagged->oid; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1665 | |
| 1666 | /* |
| 1667 | * NEEDSWORK: This derefs tag only once, which |
| 1668 | * is good to deal with chains of trust, but |
| 1669 | * is not consistent with what deref_tag() does |
| 1670 | * which peels the onion to the core. |
| 1671 | */ |
Olga Telezhnaya | aa46a0d | 2018-07-17 08:22:57 +0000 | [diff] [blame] | 1672 | return get_object(ref, 1, &obj, &oi_deref, err); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * Given a ref, return the value for the atom. This lazily gets value |
| 1677 | * out of the object by calling populate value. |
| 1678 | */ |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1679 | static int get_ref_atom_value(struct ref_array_item *ref, int atom, |
| 1680 | struct atom_value **v, struct strbuf *err) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1681 | { |
| 1682 | if (!ref->value) { |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1683 | if (populate_value(ref, err)) |
| 1684 | return -1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1685 | fill_missing_values(ref->value); |
| 1686 | } |
| 1687 | *v = &ref->value[atom]; |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 1688 | return 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1689 | } |
| 1690 | |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 1691 | /* |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1692 | * Return 1 if the refname matches one of the patterns, otherwise 0. |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1693 | * A pattern can be a literal prefix (e.g. a refname "refs/heads/master" |
| 1694 | * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref |
| 1695 | * matches "refs/heads/mas*", too). |
| 1696 | */ |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 1697 | static int match_pattern(const struct ref_filter *filter, const char *refname) |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1698 | { |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 1699 | const char **patterns = filter->name_patterns; |
| 1700 | unsigned flags = 0; |
| 1701 | |
| 1702 | if (filter->ignore_case) |
| 1703 | flags |= WM_CASEFOLD; |
| 1704 | |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1705 | /* |
| 1706 | * When no '--format' option is given we need to skip the prefix |
| 1707 | * for matching refs of tags and branches. |
| 1708 | */ |
| 1709 | (void)(skip_prefix(refname, "refs/tags/", &refname) || |
| 1710 | skip_prefix(refname, "refs/heads/", &refname) || |
| 1711 | skip_prefix(refname, "refs/remotes/", &refname) || |
| 1712 | skip_prefix(refname, "refs/", &refname)); |
| 1713 | |
| 1714 | for (; *patterns; patterns++) { |
Ævar Arnfjörð Bjarmason | 55d3426 | 2017-06-22 21:38:08 +0000 | [diff] [blame] | 1715 | if (!wildmatch(*patterns, refname, flags)) |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1716 | return 1; |
| 1717 | } |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Return 1 if the refname matches one of the patterns, otherwise 0. |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1723 | * A pattern can be path prefix (e.g. a refname "refs/heads/master" |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1724 | * matches a pattern "refs/heads/" but not "refs/heads/m") or a |
| 1725 | * wildcard (e.g. the same ref matches "refs/heads/m*", too). |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1726 | */ |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 1727 | static int match_name_as_path(const struct ref_filter *filter, const char *refname) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1728 | { |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 1729 | const char **pattern = filter->name_patterns; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1730 | int namelen = strlen(refname); |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 1731 | unsigned flags = WM_PATHNAME; |
| 1732 | |
| 1733 | if (filter->ignore_case) |
| 1734 | flags |= WM_CASEFOLD; |
| 1735 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1736 | for (; *pattern; pattern++) { |
| 1737 | const char *p = *pattern; |
| 1738 | int plen = strlen(p); |
| 1739 | |
| 1740 | if ((plen <= namelen) && |
| 1741 | !strncmp(refname, p, plen) && |
| 1742 | (refname[plen] == '\0' || |
| 1743 | refname[plen] == '/' || |
| 1744 | p[plen-1] == '/')) |
| 1745 | return 1; |
Aleksandr Makarov | 639ab5e | 2018-07-02 17:11:59 -0400 | [diff] [blame] | 1746 | if (!wildmatch(p, refname, flags)) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1747 | return 1; |
| 1748 | } |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1752 | /* Return 1 if the refname matches one of the patterns, otherwise 0. */ |
| 1753 | static int filter_pattern_match(struct ref_filter *filter, const char *refname) |
| 1754 | { |
| 1755 | if (!*filter->name_patterns) |
| 1756 | return 1; /* No pattern always matches */ |
| 1757 | if (filter->match_as_path) |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 1758 | return match_name_as_path(filter, refname); |
| 1759 | return match_pattern(filter, refname); |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1760 | } |
| 1761 | |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 1762 | /* |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 1763 | * Find the longest prefix of pattern we can pass to |
| 1764 | * `for_each_fullref_in()`, namely the part of pattern preceding the |
| 1765 | * first glob character. (Note that `for_each_fullref_in()` is |
| 1766 | * perfectly happy working with a prefix that doesn't end at a |
| 1767 | * pathname component boundary.) |
| 1768 | */ |
| 1769 | static void find_longest_prefix(struct strbuf *out, const char *pattern) |
| 1770 | { |
| 1771 | const char *p; |
| 1772 | |
| 1773 | for (p = pattern; *p && !is_glob_special(*p); p++) |
| 1774 | ; |
| 1775 | |
| 1776 | strbuf_add(out, pattern, p - pattern); |
| 1777 | } |
| 1778 | |
| 1779 | /* |
| 1780 | * This is the same as for_each_fullref_in(), but it tries to iterate |
| 1781 | * only over the patterns we'll care about. Note that it _doesn't_ do a full |
| 1782 | * pattern match, so the callback still has to match each ref individually. |
| 1783 | */ |
| 1784 | static int for_each_fullref_in_pattern(struct ref_filter *filter, |
| 1785 | each_ref_fn cb, |
| 1786 | void *cb_data, |
| 1787 | int broken) |
| 1788 | { |
| 1789 | struct strbuf prefix = STRBUF_INIT; |
| 1790 | int ret; |
| 1791 | |
| 1792 | if (!filter->match_as_path) { |
| 1793 | /* |
| 1794 | * in this case, the patterns are applied after |
| 1795 | * prefixes like "refs/heads/" etc. are stripped off, |
| 1796 | * so we have to look at everything: |
| 1797 | */ |
| 1798 | return for_each_fullref_in("", cb, cb_data, broken); |
| 1799 | } |
| 1800 | |
Jeff King | e674eb2 | 2018-07-02 17:12:42 -0400 | [diff] [blame] | 1801 | if (filter->ignore_case) { |
| 1802 | /* |
| 1803 | * we can't handle case-insensitive comparisons, |
| 1804 | * so just return everything and let the caller |
| 1805 | * sort it out. |
| 1806 | */ |
| 1807 | return for_each_fullref_in("", cb, cb_data, broken); |
| 1808 | } |
| 1809 | |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 1810 | if (!filter->name_patterns[0]) { |
| 1811 | /* no patterns; we have to look at everything */ |
| 1812 | return for_each_fullref_in("", cb, cb_data, broken); |
| 1813 | } |
| 1814 | |
| 1815 | if (filter->name_patterns[1]) { |
| 1816 | /* |
| 1817 | * multiple patterns; in theory this could still work as long |
| 1818 | * as the patterns are disjoint. We'd just make multiple calls |
| 1819 | * to for_each_ref(). But if they're not disjoint, we'd end up |
| 1820 | * reporting the same ref multiple times. So let's punt on that |
| 1821 | * for now. |
| 1822 | */ |
| 1823 | return for_each_fullref_in("", cb, cb_data, broken); |
| 1824 | } |
| 1825 | |
| 1826 | find_longest_prefix(&prefix, filter->name_patterns[0]); |
| 1827 | |
| 1828 | ret = for_each_fullref_in(prefix.buf, cb, cb_data, broken); |
| 1829 | strbuf_release(&prefix); |
| 1830 | return ret; |
| 1831 | } |
| 1832 | |
| 1833 | /* |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 1834 | * Given a ref (sha1, refname), check if the ref belongs to the array |
| 1835 | * of sha1s. If the given ref is a tag, check if the given tag points |
| 1836 | * at one of the sha1s in the given sha1 array. |
| 1837 | * the given sha1_array. |
| 1838 | * NEEDSWORK: |
| 1839 | * 1. Only a single level of inderection is obtained, we might want to |
| 1840 | * change this to account for multiple levels (e.g. annotated tags |
| 1841 | * pointing to annotated tags pointing to a commit.) |
| 1842 | * 2. As the refs are cached we might know what refname peels to without |
| 1843 | * the need to parse the object via parse_object(). peel_ref() might be a |
| 1844 | * more efficient alternative to obtain the pointee. |
| 1845 | */ |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1846 | static const struct object_id *match_points_at(struct oid_array *points_at, |
brian m. carlson | 4ce3621 | 2017-03-31 01:39:57 +0000 | [diff] [blame] | 1847 | const struct object_id *oid, |
| 1848 | const char *refname) |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 1849 | { |
brian m. carlson | 4ce3621 | 2017-03-31 01:39:57 +0000 | [diff] [blame] | 1850 | const struct object_id *tagged_oid = NULL; |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 1851 | struct object *obj; |
| 1852 | |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1853 | if (oid_array_lookup(points_at, oid) >= 0) |
brian m. carlson | 4ce3621 | 2017-03-31 01:39:57 +0000 | [diff] [blame] | 1854 | return oid; |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 1855 | obj = parse_object(the_repository, oid); |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 1856 | if (!obj) |
| 1857 | die(_("malformed object at '%s'"), refname); |
| 1858 | if (obj->type == OBJ_TAG) |
brian m. carlson | 4ce3621 | 2017-03-31 01:39:57 +0000 | [diff] [blame] | 1859 | tagged_oid = &((struct tag *)obj)->tagged->oid; |
brian m. carlson | 910650d | 2017-03-31 01:40:00 +0000 | [diff] [blame] | 1860 | if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0) |
brian m. carlson | 4ce3621 | 2017-03-31 01:39:57 +0000 | [diff] [blame] | 1861 | return tagged_oid; |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 1862 | return NULL; |
| 1863 | } |
| 1864 | |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 1865 | /* |
| 1866 | * Allocate space for a new ref_array_item and copy the name and oid to it. |
| 1867 | * |
| 1868 | * Callers can then fill in other struct members at their leisure. |
| 1869 | */ |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1870 | static struct ref_array_item *new_ref_array_item(const char *refname, |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 1871 | const struct object_id *oid) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1872 | { |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 1873 | struct ref_array_item *ref; |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 1874 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 1875 | FLEX_ALLOC_STR(ref, refname, refname); |
Jeff King | 53df97a | 2018-04-06 14:58:32 -0400 | [diff] [blame] | 1876 | oidcpy(&ref->objectname, oid); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1877 | |
| 1878 | return ref; |
| 1879 | } |
| 1880 | |
Jeff King | 427cbc9 | 2018-04-06 14:59:45 -0400 | [diff] [blame] | 1881 | struct ref_array_item *ref_array_push(struct ref_array *array, |
| 1882 | const char *refname, |
| 1883 | const struct object_id *oid) |
| 1884 | { |
| 1885 | struct ref_array_item *ref = new_ref_array_item(refname, oid); |
| 1886 | |
| 1887 | ALLOC_GROW(array->items, array->nr + 1, array->alloc); |
| 1888 | array->items[array->nr++] = ref; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1889 | |
| 1890 | return ref; |
| 1891 | } |
| 1892 | |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 1893 | static int ref_kind_from_refname(const char *refname) |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 1894 | { |
| 1895 | unsigned int i; |
| 1896 | |
| 1897 | static struct { |
| 1898 | const char *prefix; |
| 1899 | unsigned int kind; |
| 1900 | } ref_kind[] = { |
| 1901 | { "refs/heads/" , FILTER_REFS_BRANCHES }, |
| 1902 | { "refs/remotes/" , FILTER_REFS_REMOTES }, |
| 1903 | { "refs/tags/", FILTER_REFS_TAGS} |
| 1904 | }; |
| 1905 | |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 1906 | if (!strcmp(refname, "HEAD")) |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 1907 | return FILTER_REFS_DETACHED_HEAD; |
| 1908 | |
| 1909 | for (i = 0; i < ARRAY_SIZE(ref_kind); i++) { |
| 1910 | if (starts_with(refname, ref_kind[i].prefix)) |
| 1911 | return ref_kind[i].kind; |
| 1912 | } |
| 1913 | |
| 1914 | return FILTER_REFS_OTHERS; |
| 1915 | } |
| 1916 | |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 1917 | static int filter_ref_kind(struct ref_filter *filter, const char *refname) |
| 1918 | { |
| 1919 | if (filter->kind == FILTER_REFS_BRANCHES || |
| 1920 | filter->kind == FILTER_REFS_REMOTES || |
| 1921 | filter->kind == FILTER_REFS_TAGS) |
| 1922 | return filter->kind; |
| 1923 | return ref_kind_from_refname(refname); |
| 1924 | } |
| 1925 | |
Derrick Stolee | 920f93c | 2018-07-20 16:33:08 +0000 | [diff] [blame] | 1926 | struct ref_filter_cbdata { |
| 1927 | struct ref_array *array; |
| 1928 | struct ref_filter *filter; |
| 1929 | struct contains_cache contains_cache; |
| 1930 | struct contains_cache no_contains_cache; |
| 1931 | }; |
| 1932 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1933 | /* |
| 1934 | * A call-back given to for_each_ref(). Filter refs and keep them for |
| 1935 | * later object processing. |
| 1936 | */ |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 1937 | static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1938 | { |
| 1939 | struct ref_filter_cbdata *ref_cbdata = cb_data; |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 1940 | struct ref_filter *filter = ref_cbdata->filter; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1941 | struct ref_array_item *ref; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 1942 | struct commit *commit = NULL; |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 1943 | unsigned int kind; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1944 | |
| 1945 | if (flag & REF_BAD_NAME) { |
Nguyễn Thái Ngọc Duy | 1823c61 | 2016-02-27 13:42:04 +0700 | [diff] [blame] | 1946 | warning(_("ignoring ref with broken name %s"), refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1947 | return 0; |
| 1948 | } |
| 1949 | |
Junio C Hamano | 7ebc8cb | 2015-08-03 11:01:10 -0700 | [diff] [blame] | 1950 | if (flag & REF_ISBROKEN) { |
Nguyễn Thái Ngọc Duy | 1823c61 | 2016-02-27 13:42:04 +0700 | [diff] [blame] | 1951 | warning(_("ignoring broken ref %s"), refname); |
Junio C Hamano | 7ebc8cb | 2015-08-03 11:01:10 -0700 | [diff] [blame] | 1952 | return 0; |
| 1953 | } |
| 1954 | |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 1955 | /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */ |
| 1956 | kind = filter_ref_kind(filter, refname); |
| 1957 | if (!(kind & filter->kind)) |
| 1958 | return 0; |
| 1959 | |
Karthik Nayak | bef0e12 | 2015-09-10 21:18:26 +0530 | [diff] [blame] | 1960 | if (!filter_pattern_match(filter, refname)) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1961 | return 0; |
| 1962 | |
brian m. carlson | 4ce3621 | 2017-03-31 01:39:57 +0000 | [diff] [blame] | 1963 | if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname)) |
Karthik Nayak | 6841104 | 2015-07-07 21:36:09 +0530 | [diff] [blame] | 1964 | return 0; |
| 1965 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1966 | /* |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 1967 | * A merge filter is applied on refs pointing to commits. Hence |
| 1968 | * obtain the commit using the 'oid' available and discard all |
| 1969 | * non-commits early. The actual filtering is done later. |
| 1970 | */ |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 1971 | if (filter->merge_commit || filter->with_commit || filter->no_commit || filter->verbose) { |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 1972 | commit = lookup_commit_reference_gently(the_repository, oid, |
| 1973 | 1); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 1974 | if (!commit) |
| 1975 | return 0; |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 1976 | /* We perform the filtering for the '--contains' option... */ |
Karthik Nayak | ee2bd06 | 2015-07-07 21:36:16 +0530 | [diff] [blame] | 1977 | if (filter->with_commit && |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 1978 | !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache)) |
| 1979 | return 0; |
| 1980 | /* ...or for the `--no-contains' option */ |
| 1981 | if (filter->no_commit && |
| 1982 | commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache)) |
Karthik Nayak | ee2bd06 | 2015-07-07 21:36:16 +0530 | [diff] [blame] | 1983 | return 0; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 1984 | } |
| 1985 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1986 | /* |
| 1987 | * We do not open the object yet; sort may only need refname |
| 1988 | * to do its job and the resulting list may yet to be pruned |
| 1989 | * by maxcount logic. |
| 1990 | */ |
Jeff King | 427cbc9 | 2018-04-06 14:59:45 -0400 | [diff] [blame] | 1991 | ref = ref_array_push(ref_cbdata->array, refname, oid); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 1992 | ref->commit = commit; |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 1993 | ref->flag = flag; |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 1994 | ref->kind = kind; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1995 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | /* Free memory allocated for a ref_array_item */ |
| 2000 | static void free_array_item(struct ref_array_item *item) |
| 2001 | { |
| 2002 | free((char *)item->symref); |
Olga Telezhnaya | f0062d3 | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2003 | if (item->value) { |
| 2004 | free((char *)item->value->s); |
| 2005 | free(item->value); |
| 2006 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2007 | free(item); |
| 2008 | } |
| 2009 | |
| 2010 | /* Free all memory allocated for ref_array */ |
| 2011 | void ref_array_clear(struct ref_array *array) |
| 2012 | { |
| 2013 | int i; |
| 2014 | |
Olga Telezhnaya | 23941dd | 2018-10-18 07:28:54 +0000 | [diff] [blame] | 2015 | for (i = 0; i < used_atom_cnt; i++) |
| 2016 | free((char *)used_atom[i].name); |
| 2017 | FREE_AND_NULL(used_atom); |
| 2018 | used_atom_cnt = 0; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2019 | for (i = 0; i < array->nr; i++) |
| 2020 | free_array_item(array->items[i]); |
Ævar Arnfjörð Bjarmason | 6a83d90 | 2017-06-15 23:15:46 +0000 | [diff] [blame] | 2021 | FREE_AND_NULL(array->items); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2022 | array->nr = array->alloc = 0; |
| 2023 | } |
| 2024 | |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2025 | static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata) |
| 2026 | { |
| 2027 | struct rev_info revs; |
| 2028 | int i, old_nr; |
| 2029 | struct ref_filter *filter = ref_cbdata->filter; |
| 2030 | struct ref_array *array = ref_cbdata->array; |
| 2031 | struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr); |
| 2032 | |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 2033 | repo_init_revisions(the_repository, &revs, NULL); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2034 | |
| 2035 | for (i = 0; i < array->nr; i++) { |
| 2036 | struct ref_array_item *item = array->items[i]; |
| 2037 | add_pending_object(&revs, &item->commit->object, item->refname); |
| 2038 | to_clear[i] = item->commit; |
| 2039 | } |
| 2040 | |
| 2041 | filter->merge_commit->object.flags |= UNINTERESTING; |
| 2042 | add_pending_object(&revs, &filter->merge_commit->object, ""); |
| 2043 | |
| 2044 | revs.limited = 1; |
| 2045 | if (prepare_revision_walk(&revs)) |
| 2046 | die(_("revision walk setup failed")); |
| 2047 | |
| 2048 | old_nr = array->nr; |
| 2049 | array->nr = 0; |
| 2050 | |
| 2051 | for (i = 0; i < old_nr; i++) { |
| 2052 | struct ref_array_item *item = array->items[i]; |
| 2053 | struct commit *commit = item->commit; |
| 2054 | |
| 2055 | int is_merged = !!(commit->object.flags & UNINTERESTING); |
| 2056 | |
| 2057 | if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE)) |
| 2058 | array->items[array->nr++] = array->items[i]; |
| 2059 | else |
| 2060 | free_array_item(item); |
| 2061 | } |
| 2062 | |
René Scharfe | 5dee6d6 | 2017-12-25 18:44:12 +0100 | [diff] [blame] | 2063 | clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2064 | clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS); |
| 2065 | free(to_clear); |
| 2066 | } |
| 2067 | |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 2068 | /* |
| 2069 | * API for filtering a set of refs. Based on the type of refs the user |
| 2070 | * has requested, we iterate through those refs and apply filters |
| 2071 | * as per the given ref_filter structure and finally store the |
| 2072 | * filtered refs in the ref_array structure. |
| 2073 | */ |
| 2074 | int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type) |
| 2075 | { |
| 2076 | struct ref_filter_cbdata ref_cbdata; |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2077 | int ret = 0; |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2078 | unsigned int broken = 0; |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 2079 | |
| 2080 | ref_cbdata.array = array; |
| 2081 | ref_cbdata.filter = filter; |
| 2082 | |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2083 | if (type & FILTER_REFS_INCLUDE_BROKEN) |
| 2084 | broken = 1; |
| 2085 | filter->kind = type & FILTER_REFS_KIND_MASK; |
| 2086 | |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 2087 | init_contains_cache(&ref_cbdata.contains_cache); |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 2088 | init_contains_cache(&ref_cbdata.no_contains_cache); |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 2089 | |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2090 | /* Simple per-ref filtering */ |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2091 | if (!filter->kind) |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 2092 | die("filter_refs: invalid type"); |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2093 | else { |
| 2094 | /* |
| 2095 | * For common cases where we need only branches or remotes or tags, |
| 2096 | * we only iterate through those refs. If a mix of refs is needed, |
| 2097 | * we iterate over all refs and filter out required refs with the help |
| 2098 | * of filter_ref_kind(). |
| 2099 | */ |
| 2100 | if (filter->kind == FILTER_REFS_BRANCHES) |
| 2101 | ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken); |
| 2102 | else if (filter->kind == FILTER_REFS_REMOTES) |
| 2103 | ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken); |
| 2104 | else if (filter->kind == FILTER_REFS_TAGS) |
| 2105 | ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken); |
| 2106 | else if (filter->kind & FILTER_REFS_ALL) |
Jeff King | cfe004a | 2017-05-22 16:17:54 +0200 | [diff] [blame] | 2107 | ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken); |
Karthik Nayak | 5b4f285 | 2015-09-10 21:18:23 +0530 | [diff] [blame] | 2108 | if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD)) |
| 2109 | head_ref(ref_filter_handler, &ref_cbdata); |
| 2110 | } |
| 2111 | |
Jeff King | a91aca4 | 2017-03-09 08:29:49 -0500 | [diff] [blame] | 2112 | clear_contains_cache(&ref_cbdata.contains_cache); |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 2113 | clear_contains_cache(&ref_cbdata.no_contains_cache); |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 2114 | |
| 2115 | /* Filters that need revision walking */ |
| 2116 | if (filter->merge_commit) |
| 2117 | do_merge_filter(&ref_cbdata); |
| 2118 | |
| 2119 | return ret; |
Karthik Nayak | 14de7fb | 2015-06-14 01:07:28 +0530 | [diff] [blame] | 2120 | } |
| 2121 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2122 | static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b) |
| 2123 | { |
| 2124 | struct atom_value *va, *vb; |
| 2125 | int cmp; |
Karthik Nayak | b072add | 2016-02-17 23:36:11 +0530 | [diff] [blame] | 2126 | cmp_type cmp_type = used_atom[s->atom].type; |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2127 | int (*cmp_fn)(const char *, const char *); |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2128 | struct strbuf err = STRBUF_INIT; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2129 | |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2130 | if (get_ref_atom_value(a, s->atom, &va, &err)) |
| 2131 | die("%s", err.buf); |
| 2132 | if (get_ref_atom_value(b, s->atom, &vb, &err)) |
| 2133 | die("%s", err.buf); |
| 2134 | strbuf_release(&err); |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2135 | cmp_fn = s->ignore_case ? strcasecmp : strcmp; |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 2136 | if (s->version) |
| 2137 | cmp = versioncmp(va->s, vb->s); |
| 2138 | else if (cmp_type == FIELD_STR) |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2139 | cmp = cmp_fn(va->s, vb->s); |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 2140 | else { |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 2141 | if (va->value < vb->value) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2142 | cmp = -1; |
Johannes Schindelin | e467dc1 | 2017-04-20 22:52:09 +0200 | [diff] [blame] | 2143 | else if (va->value == vb->value) |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 2144 | cmp = cmp_fn(a->refname, b->refname); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2145 | else |
| 2146 | cmp = 1; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2147 | } |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 2148 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2149 | return (s->reverse) ? -cmp : cmp; |
| 2150 | } |
| 2151 | |
René Scharfe | 83fc4d6 | 2017-01-22 18:58:07 +0100 | [diff] [blame] | 2152 | static int compare_refs(const void *a_, const void *b_, void *ref_sorting) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2153 | { |
| 2154 | struct ref_array_item *a = *((struct ref_array_item **)a_); |
| 2155 | struct ref_array_item *b = *((struct ref_array_item **)b_); |
| 2156 | struct ref_sorting *s; |
| 2157 | |
| 2158 | for (s = ref_sorting; s; s = s->next) { |
| 2159 | int cmp = cmp_ref_sorting(s, a, b); |
| 2160 | if (cmp) |
| 2161 | return cmp; |
| 2162 | } |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
Jeff King | 76f9e56 | 2020-05-03 05:11:57 -0400 | [diff] [blame^] | 2166 | void ref_sorting_icase_all(struct ref_sorting *sorting, int flag) |
| 2167 | { |
| 2168 | for (; sorting; sorting = sorting->next) |
| 2169 | sorting->ignore_case = !!flag; |
| 2170 | } |
| 2171 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2172 | void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array) |
| 2173 | { |
René Scharfe | 83fc4d6 | 2017-01-22 18:58:07 +0100 | [diff] [blame] | 2174 | QSORT_S(array->items, array->nr, compare_refs, sorting); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2175 | } |
| 2176 | |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2177 | static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2178 | { |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2179 | struct strbuf *s = &state->stack->output; |
| 2180 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2181 | while (*cp && (!ep || cp < ep)) { |
| 2182 | if (*cp == '%') { |
| 2183 | if (cp[1] == '%') |
| 2184 | cp++; |
| 2185 | else { |
René Scharfe | d233097 | 2016-09-03 17:59:20 +0200 | [diff] [blame] | 2186 | int ch = hex2chr(cp + 1); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2187 | if (0 <= ch) { |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2188 | strbuf_addch(s, ch); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2189 | cp += 3; |
| 2190 | continue; |
| 2191 | } |
| 2192 | } |
| 2193 | } |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2194 | strbuf_addch(s, *cp); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2195 | cp++; |
| 2196 | } |
| 2197 | } |
| 2198 | |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2199 | int format_ref_array_item(struct ref_array_item *info, |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 2200 | const struct ref_format *format, |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2201 | struct strbuf *final_buf, |
| 2202 | struct strbuf *error_buf) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2203 | { |
| 2204 | const char *cp, *sp, *ep; |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2205 | struct ref_formatting_state state = REF_FORMATTING_STATE_INIT; |
| 2206 | |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 2207 | state.quote_style = format->quote_style; |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2208 | push_stack_element(&state.stack); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2209 | |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 2210 | for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2211 | struct atom_value *atomv; |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2212 | int pos; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2213 | |
| 2214 | ep = strchr(sp, ')'); |
| 2215 | if (cp < sp) |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2216 | append_literal(cp, sp, &state); |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2217 | pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf); |
Olga Telezhnaya | e339611 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2218 | if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) || |
| 2219 | atomv->handler(atomv, &state, error_buf)) { |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2220 | pop_stack_element(&state.stack); |
| 2221 | return -1; |
| 2222 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2223 | } |
| 2224 | if (*cp) { |
| 2225 | sp = cp + strlen(cp); |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2226 | append_literal(cp, sp, &state); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2227 | } |
Jeff King | bf285ae | 2017-07-13 11:02:30 -0400 | [diff] [blame] | 2228 | if (format->need_color_reset_at_eol) { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2229 | struct atom_value resetv; |
Jeff King | 51331aa | 2017-07-13 10:58:56 -0400 | [diff] [blame] | 2230 | resetv.s = GIT_COLOR_RESET; |
Olga Telezhnaya | 3fc8439 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2231 | if (append_atom(&resetv, &state, error_buf)) { |
| 2232 | pop_stack_element(&state.stack); |
| 2233 | return -1; |
| 2234 | } |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2235 | } |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2236 | if (state.stack->prev) { |
| 2237 | pop_stack_element(&state.stack); |
| 2238 | return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing")); |
| 2239 | } |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 2240 | strbuf_addbuf(final_buf, &state.stack->output); |
Karthik Nayak | 574e96a | 2015-09-10 21:18:18 +0530 | [diff] [blame] | 2241 | pop_stack_element(&state.stack); |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2242 | return 0; |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 2243 | } |
| 2244 | |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 2245 | void show_ref_array_item(struct ref_array_item *info, |
| 2246 | const struct ref_format *format) |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 2247 | { |
| 2248 | struct strbuf final_buf = STRBUF_INIT; |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2249 | struct strbuf error_buf = STRBUF_INIT; |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 2250 | |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2251 | if (format_ref_array_item(info, format, &final_buf, &error_buf)) |
| 2252 | die("%s", error_buf.buf); |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 2253 | fwrite(final_buf.buf, 1, final_buf.len, stdout); |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2254 | strbuf_release(&error_buf); |
Karthik Nayak | 99c6a71 | 2017-01-10 14:19:39 +0530 | [diff] [blame] | 2255 | strbuf_release(&final_buf); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2256 | putchar('\n'); |
| 2257 | } |
| 2258 | |
Jeff King | 53df97a | 2018-04-06 14:58:32 -0400 | [diff] [blame] | 2259 | void pretty_print_ref(const char *name, const struct object_id *oid, |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 2260 | const struct ref_format *format) |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 2261 | { |
| 2262 | struct ref_array_item *ref_item; |
Jeff King | 0ffaa00 | 2018-04-06 14:59:26 -0400 | [diff] [blame] | 2263 | ref_item = new_ref_array_item(name, oid); |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 2264 | ref_item->kind = ref_kind_from_refname(name); |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 2265 | show_ref_array_item(ref_item, format); |
Lukas Puehringer | 2111aa7 | 2017-01-17 18:37:19 -0500 | [diff] [blame] | 2266 | free_array_item(ref_item); |
| 2267 | } |
| 2268 | |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 2269 | static int parse_sorting_atom(const char *atom) |
| 2270 | { |
Jeff King | ab7ded3 | 2017-07-13 11:06:40 -0400 | [diff] [blame] | 2271 | /* |
| 2272 | * This parses an atom using a dummy ref_format, since we don't |
| 2273 | * actually care about the formatting details. |
| 2274 | */ |
| 2275 | struct ref_format dummy = REF_FORMAT_INIT; |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 2276 | const char *end = atom + strlen(atom); |
Olga Telezhnaya | e6ff7b3 | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 2277 | struct strbuf err = STRBUF_INIT; |
| 2278 | int res = parse_ref_filter_atom(&dummy, atom, end, &err); |
| 2279 | if (res < 0) |
| 2280 | die("%s", err.buf); |
| 2281 | strbuf_release(&err); |
| 2282 | return res; |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 2283 | } |
| 2284 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2285 | /* If no sorting option is given, use refname to sort as default */ |
| 2286 | struct ref_sorting *ref_default_sorting(void) |
| 2287 | { |
| 2288 | static const char cstr_name[] = "refname"; |
| 2289 | |
| 2290 | struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting)); |
| 2291 | |
| 2292 | sorting->next = NULL; |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 2293 | sorting->atom = parse_sorting_atom(cstr_name); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2294 | return sorting; |
| 2295 | } |
| 2296 | |
Jeff King | 18a2565 | 2017-07-13 11:02:44 -0400 | [diff] [blame] | 2297 | void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg) |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2298 | { |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2299 | struct ref_sorting *s; |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2300 | |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2301 | s = xcalloc(1, sizeof(*s)); |
| 2302 | s->next = *sorting_tail; |
| 2303 | *sorting_tail = s; |
| 2304 | |
| 2305 | if (*arg == '-') { |
| 2306 | s->reverse = 1; |
| 2307 | arg++; |
| 2308 | } |
Karthik Nayak | 90c0040 | 2015-09-10 21:18:25 +0530 | [diff] [blame] | 2309 | if (skip_prefix(arg, "version:", &arg) || |
| 2310 | skip_prefix(arg, "v:", &arg)) |
| 2311 | s->version = 1; |
Jeff King | 29ef53c | 2017-07-13 11:02:58 -0400 | [diff] [blame] | 2312 | s->atom = parse_sorting_atom(arg); |
Jeff King | 18a2565 | 2017-07-13 11:02:44 -0400 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset) |
| 2316 | { |
| 2317 | if (!arg) /* should --no-sort void the list ? */ |
| 2318 | return -1; |
| 2319 | parse_ref_sorting(opt->value, arg); |
Karthik Nayak | c95b758 | 2015-06-14 01:07:27 +0530 | [diff] [blame] | 2320 | return 0; |
| 2321 | } |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 2322 | |
| 2323 | int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset) |
| 2324 | { |
| 2325 | struct ref_filter *rf = opt->value; |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 2326 | struct object_id oid; |
Ævar Arnfjörð Bjarmason | 17d6c74 | 2017-03-21 12:58:49 +0000 | [diff] [blame] | 2327 | int no_merged = starts_with(opt->long_name, "no"); |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 2328 | |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 2329 | BUG_ON_OPT_NEG(unset); |
| 2330 | |
Ævar Arnfjörð Bjarmason | 17d6c74 | 2017-03-21 12:58:49 +0000 | [diff] [blame] | 2331 | if (rf->merge) { |
| 2332 | if (no_merged) { |
| 2333 | return opterror(opt, "is incompatible with --merged", 0); |
| 2334 | } else { |
| 2335 | return opterror(opt, "is incompatible with --no-merged", 0); |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | rf->merge = no_merged |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 2340 | ? REF_FILTER_MERGED_OMIT |
| 2341 | : REF_FILTER_MERGED_INCLUDE; |
| 2342 | |
brian m. carlson | 1e43ed9 | 2017-05-06 22:10:09 +0000 | [diff] [blame] | 2343 | if (get_oid(arg, &oid)) |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 2344 | die(_("malformed object name %s"), arg); |
| 2345 | |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 2346 | rf->merge_commit = lookup_commit_reference_gently(the_repository, |
| 2347 | &oid, 0); |
Karthik Nayak | 5afcb90 | 2015-07-07 21:36:11 +0530 | [diff] [blame] | 2348 | if (!rf->merge_commit) |
| 2349 | return opterror(opt, "must point to a commit", 0); |
| 2350 | |
| 2351 | return 0; |
| 2352 | } |