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