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