blob: 0c45ed9d94a4bd4bfab6c9d441ca9bc7de7ebf31 [file] [log] [blame]
Karthik Nayakc95b7582015-06-14 01:07:27 +05301#include "builtin.h"
2#include "cache.h"
3#include "parse-options.h"
4#include "refs.h"
5#include "wildmatch.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07006#include "object-store.h"
Stefan Beller109cd762018-06-28 18:21:51 -07007#include "repository.h"
Karthik Nayakc95b7582015-06-14 01:07:27 +05308#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 Nayak35257aa2015-07-07 21:36:12 +053014#include "revision.h"
Karthik Nayakce592082015-09-11 20:33:07 +053015#include "utf8.h"
Karthik Nayak90c00402015-09-10 21:18:25 +053016#include "git-compat-util.h"
17#include "version.h"
Jacob Kellerb1d31c82016-11-18 16:58:15 -080018#include "trailer.h"
Karthik Nayakd4919bb2017-01-10 14:19:38 +053019#include "wt-status.h"
Jeff Kinga91aca42017-03-09 08:29:49 -050020#include "commit-slab.h"
Derrick Stolee819807b2018-05-01 12:47:15 +000021#include "commit-graph.h"
Derrick Stolee64043552018-07-20 16:33:04 +000022#include "commit-reach.h"
Karthik Nayakc95b7582015-06-14 01:07:27 +053023
Karthik Nayak6eac70f2017-01-10 14:19:50 +053024static struct ref_msg {
25 const char *gone;
26 const char *ahead;
27 const char *behind;
28 const char *ahead_behind;
29} msgs = {
30 /* Untranslated plumbing messages: */
31 "gone",
32 "ahead %d",
33 "behind %d",
34 "ahead %d, behind %d"
35};
36
37void setup_ref_filter_porcelain_msg(void)
38{
39 msgs.gone = _("gone");
40 msgs.ahead = _("ahead %d");
41 msgs.behind = _("behind %d");
42 msgs.ahead_behind = _("ahead %d, behind %d");
43}
Karthik Nayakc95b7582015-06-14 01:07:27 +053044
45typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +053046typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +000047typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source;
Karthik Nayakc95b7582015-06-14 01:07:27 +053048
Karthik Nayak5bd881d2016-02-17 23:36:15 +053049struct align {
50 align_type position;
51 unsigned int width;
52};
53
Karthik Nayakc58492d2017-01-10 14:19:34 +053054struct if_then_else {
Karthik Nayak4f3e3b32017-01-10 14:19:36 +053055 cmp_status cmp_status;
56 const char *str;
Karthik Nayakc58492d2017-01-10 14:19:34 +053057 unsigned int then_atom_seen : 1,
58 else_atom_seen : 1,
59 condition_satisfied : 1;
60};
61
Karthik Nayakb180e6f2017-01-10 14:19:43 +053062struct refname_atom {
Karthik Nayak1a347282017-01-10 14:19:49 +053063 enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option;
64 int lstrip, rstrip;
Karthik Nayakb180e6f2017-01-10 14:19:43 +053065};
66
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +000067static struct expand_data {
68 struct object_id oid;
69 enum object_type type;
70 unsigned long size;
71 off_t disk_size;
72 struct object_id delta_base_oid;
73 void *content;
74
75 struct object_info info;
76} oi, oi_deref;
77
Karthik Nayak50cd83d2016-02-17 23:36:10 +053078/*
79 * An atom is a valid field atom listed below, possibly prefixed with
80 * a "*" to denote deref_tag().
81 *
82 * We parse given format string and sort specifiers, and make a list
83 * of properties that we need to extract out of objects. ref_array_item
84 * structure will hold an array of values extracted that can be
85 * indexed with the "atom number", which is an index into this
86 * array.
87 */
Karthik Nayakb072add2016-02-17 23:36:11 +053088static struct used_atom {
89 const char *name;
90 cmp_type type;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +000091 info_source source;
Karthik Nayakfd935cc2016-02-17 23:36:13 +053092 union {
93 char color[COLOR_MAXLEN];
Karthik Nayak5bd881d2016-02-17 23:36:15 +053094 struct align align;
Karthik Nayak7743fcc2017-01-10 14:19:41 +053095 struct {
Johannes Schindelincc723852017-10-05 14:19:09 +020096 enum {
J Wyman9700fae2017-11-07 17:31:08 +010097 RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
Johannes Schindelincc723852017-10-05 14:19:09 +020098 } option;
Karthik Nayak3ba308c2017-01-10 14:19:45 +053099 struct refname_atom refname;
Johannes Schindelincc723852017-10-05 14:19:09 +0200100 unsigned int nobracket : 1, push : 1, push_remote : 1;
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530101 } remote_ref;
Karthik Nayak452db392016-02-17 23:36:18 +0530102 struct {
Jacob Kellerb1d31c82016-11-18 16:58:15 -0800103 enum { C_BARE, C_BODY, C_BODY_DEP, C_LINES, C_SIG, C_SUB, C_TRAILERS } option;
Taylor Blau67a20a02017-10-01 22:25:23 -0700104 struct process_trailer_options trailer_opts;
Karthik Nayak452db392016-02-17 23:36:18 +0530105 unsigned int nlines;
106 } contents;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530107 struct {
108 cmp_status cmp_status;
109 const char *str;
110 } if_then_else;
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530111 struct {
112 enum { O_FULL, O_LENGTH, O_SHORT } option;
113 unsigned int length;
114 } objectname;
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530115 struct refname_atom refname;
Jeff King613a0e52017-05-19 02:12:12 -0400116 char *head;
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530117 } u;
Karthik Nayakb072add2016-02-17 23:36:11 +0530118} *used_atom;
Karthik Nayak50cd83d2016-02-17 23:36:10 +0530119static int used_atom_cnt, need_tagged, need_symref;
Karthik Nayak50cd83d2016-02-17 23:36:10 +0530120
Olga Telezhnayae2e7a242018-03-29 12:49:45 +0000121/*
122 * Expand string, append it to strbuf *sb, then return error code ret.
123 * Allow to save few lines of code.
124 */
125static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
126{
127 va_list ap;
128 va_start(ap, fmt);
129 strbuf_vaddf(sb, fmt, ap);
130 va_end(ap);
131 return ret;
132}
133
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000134static int color_atom_parser(const struct ref_format *format, struct used_atom *atom,
135 const char *color_value, struct strbuf *err)
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530136{
137 if (!color_value)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000138 return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530139 if (color_parse(color_value, atom->u.color) < 0)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000140 return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
141 color_value);
Jeff King11b087a2017-07-13 11:09:32 -0400142 /*
143 * We check this after we've parsed the color, which lets us complain
144 * about syntactically bogus color names even if they won't be used.
145 */
146 if (!want_color(format->use_color))
147 color_parse("", atom->u.color);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000148 return 0;
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530149}
150
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000151static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
152 const char *name, struct strbuf *err)
Karthik Nayak5339bda2016-02-17 23:36:17 +0530153{
154 if (!arg)
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530155 atom->option = R_NORMAL;
Karthik Nayak5339bda2016-02-17 23:36:17 +0530156 else if (!strcmp(arg, "short"))
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530157 atom->option = R_SHORT;
Junio C Hamano44a6b6c2017-02-07 11:50:34 -0800158 else if (skip_prefix(arg, "lstrip=", &arg) ||
159 skip_prefix(arg, "strip=", &arg)) {
Karthik Nayak17938f12017-01-10 14:19:46 +0530160 atom->option = R_LSTRIP;
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +0530161 if (strtol_i(arg, 10, &atom->lstrip))
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000162 return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
Karthik Nayak1a347282017-01-10 14:19:49 +0530163 } else if (skip_prefix(arg, "rstrip=", &arg)) {
164 atom->option = R_RSTRIP;
165 if (strtol_i(arg, 10, &atom->rstrip))
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000166 return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530167 } else
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000168 return strbuf_addf_ret(err, -1, _("unrecognized %%(%s) argument: %s"), name, arg);
169 return 0;
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530170}
171
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000172static int remote_ref_atom_parser(const struct ref_format *format, struct used_atom *atom,
173 const char *arg, struct strbuf *err)
Karthik Nayak5339bda2016-02-17 23:36:17 +0530174{
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530175 struct string_list params = STRING_LIST_INIT_DUP;
176 int i;
177
Johannes Schindelincc723852017-10-05 14:19:09 +0200178 if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
179 atom->u.remote_ref.push = 1;
180
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530181 if (!arg) {
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530182 atom->u.remote_ref.option = RR_REF;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000183 return refname_atom_parser_internal(&atom->u.remote_ref.refname,
184 arg, atom->name, err);
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530185 }
186
187 atom->u.remote_ref.nobracket = 0;
188 string_list_split(&params, arg, ',', -1);
189
190 for (i = 0; i < params.nr; i++) {
191 const char *s = params.items[i].string;
192
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530193 if (!strcmp(s, "track"))
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530194 atom->u.remote_ref.option = RR_TRACK;
195 else if (!strcmp(s, "trackshort"))
196 atom->u.remote_ref.option = RR_TRACKSHORT;
197 else if (!strcmp(s, "nobracket"))
198 atom->u.remote_ref.nobracket = 1;
Johannes Schindelincc723852017-10-05 14:19:09 +0200199 else if (!strcmp(s, "remotename")) {
200 atom->u.remote_ref.option = RR_REMOTE_NAME;
201 atom->u.remote_ref.push_remote = 1;
J Wyman9700fae2017-11-07 17:31:08 +0100202 } else if (!strcmp(s, "remoteref")) {
203 atom->u.remote_ref.option = RR_REMOTE_REF;
204 atom->u.remote_ref.push_remote = 1;
Johannes Schindelincc723852017-10-05 14:19:09 +0200205 } else {
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530206 atom->u.remote_ref.option = RR_REF;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000207 if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
208 arg, atom->name, err)) {
209 string_list_clear(&params, 0);
210 return -1;
211 }
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530212 }
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530213 }
214
215 string_list_clear(&params, 0);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000216 return 0;
Karthik Nayak5339bda2016-02-17 23:36:17 +0530217}
218
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000219static int objecttype_atom_parser(const struct ref_format *format, struct used_atom *atom,
220 const char *arg, struct strbuf *err)
221{
222 if (arg)
223 return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments"));
224 if (*atom->name == '*')
225 oi_deref.info.typep = &oi_deref.type;
226 else
227 oi.info.typep = &oi.type;
228 return 0;
229}
230
231static int objectsize_atom_parser(const struct ref_format *format, struct used_atom *atom,
232 const char *arg, struct strbuf *err)
233{
234 if (arg)
235 return strbuf_addf_ret(err, -1, _("%%(objectsize) does not take arguments"));
236 if (*atom->name == '*')
237 oi_deref.info.sizep = &oi_deref.size;
238 else
239 oi.info.sizep = &oi.size;
240 return 0;
241}
242
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000243static int body_atom_parser(const struct ref_format *format, struct used_atom *atom,
244 const char *arg, struct strbuf *err)
Karthik Nayak452db392016-02-17 23:36:18 +0530245{
246 if (arg)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000247 return strbuf_addf_ret(err, -1, _("%%(body) does not take arguments"));
Karthik Nayak452db392016-02-17 23:36:18 +0530248 atom->u.contents.option = C_BODY_DEP;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000249 return 0;
Karthik Nayak452db392016-02-17 23:36:18 +0530250}
251
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000252static int subject_atom_parser(const struct ref_format *format, struct used_atom *atom,
253 const char *arg, struct strbuf *err)
Karthik Nayak452db392016-02-17 23:36:18 +0530254{
255 if (arg)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000256 return strbuf_addf_ret(err, -1, _("%%(subject) does not take arguments"));
Karthik Nayak452db392016-02-17 23:36:18 +0530257 atom->u.contents.option = C_SUB;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000258 return 0;
Karthik Nayak452db392016-02-17 23:36:18 +0530259}
260
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000261static int trailers_atom_parser(const struct ref_format *format, struct used_atom *atom,
262 const char *arg, struct strbuf *err)
Jacob Kellerb1d31c82016-11-18 16:58:15 -0800263{
Taylor Blau67a20a02017-10-01 22:25:23 -0700264 struct string_list params = STRING_LIST_INIT_DUP;
265 int i;
266
Jeff Kinge5fba5d2018-08-22 20:50:17 -0400267 atom->u.contents.trailer_opts.no_divider = 1;
268
Taylor Blau67a20a02017-10-01 22:25:23 -0700269 if (arg) {
270 string_list_split(&params, arg, ',', -1);
271 for (i = 0; i < params.nr; i++) {
272 const char *s = params.items[i].string;
273 if (!strcmp(s, "unfold"))
274 atom->u.contents.trailer_opts.unfold = 1;
275 else if (!strcmp(s, "only"))
276 atom->u.contents.trailer_opts.only_trailers = 1;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000277 else {
278 strbuf_addf(err, _("unknown %%(trailers) argument: %s"), s);
279 string_list_clear(&params, 0);
280 return -1;
281 }
Taylor Blau67a20a02017-10-01 22:25:23 -0700282 }
283 }
Jacob Kellerb1d31c82016-11-18 16:58:15 -0800284 atom->u.contents.option = C_TRAILERS;
Taylor Blau67a20a02017-10-01 22:25:23 -0700285 string_list_clear(&params, 0);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000286 return 0;
Jacob Kellerb1d31c82016-11-18 16:58:15 -0800287}
288
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000289static int contents_atom_parser(const struct ref_format *format, struct used_atom *atom,
290 const char *arg, struct strbuf *err)
Karthik Nayak452db392016-02-17 23:36:18 +0530291{
292 if (!arg)
293 atom->u.contents.option = C_BARE;
294 else if (!strcmp(arg, "body"))
295 atom->u.contents.option = C_BODY;
296 else if (!strcmp(arg, "signature"))
297 atom->u.contents.option = C_SIG;
298 else if (!strcmp(arg, "subject"))
299 atom->u.contents.option = C_SUB;
Taylor Blau7a5edbd2017-10-01 22:25:24 -0700300 else if (skip_prefix(arg, "trailers", &arg)) {
301 skip_prefix(arg, ":", &arg);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000302 if (trailers_atom_parser(format, atom, *arg ? arg : NULL, err))
303 return -1;
Taylor Blau7a5edbd2017-10-01 22:25:24 -0700304 } else if (skip_prefix(arg, "lines=", &arg)) {
Karthik Nayak452db392016-02-17 23:36:18 +0530305 atom->u.contents.option = C_LINES;
306 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000307 return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
Karthik Nayak452db392016-02-17 23:36:18 +0530308 } else
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000309 return strbuf_addf_ret(err, -1, _("unrecognized %%(contents) argument: %s"), arg);
310 return 0;
Karthik Nayak452db392016-02-17 23:36:18 +0530311}
312
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000313static int objectname_atom_parser(const struct ref_format *format, struct used_atom *atom,
314 const char *arg, struct strbuf *err)
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530315{
316 if (!arg)
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530317 atom->u.objectname.option = O_FULL;
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530318 else if (!strcmp(arg, "short"))
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530319 atom->u.objectname.option = O_SHORT;
320 else if (skip_prefix(arg, "short=", &arg)) {
321 atom->u.objectname.option = O_LENGTH;
322 if (strtoul_ui(arg, 10, &atom->u.objectname.length) ||
323 atom->u.objectname.length == 0)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000324 return strbuf_addf_ret(err, -1, _("positive value expected objectname:short=%s"), arg);
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530325 if (atom->u.objectname.length < MINIMUM_ABBREV)
326 atom->u.objectname.length = MINIMUM_ABBREV;
327 } else
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000328 return strbuf_addf_ret(err, -1, _("unrecognized %%(objectname) argument: %s"), arg);
329 return 0;
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530330}
331
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000332static int refname_atom_parser(const struct ref_format *format, struct used_atom *atom,
333 const char *arg, struct strbuf *err)
Karthik Nayaka7984102017-01-10 14:19:44 +0530334{
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000335 return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
Karthik Nayaka7984102017-01-10 14:19:44 +0530336}
337
Karthik Nayak25a8d792016-02-17 23:36:14 +0530338static align_type parse_align_position(const char *s)
339{
340 if (!strcmp(s, "right"))
341 return ALIGN_RIGHT;
342 else if (!strcmp(s, "middle"))
343 return ALIGN_MIDDLE;
344 else if (!strcmp(s, "left"))
345 return ALIGN_LEFT;
346 return -1;
347}
348
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000349static int align_atom_parser(const struct ref_format *format, struct used_atom *atom,
350 const char *arg, struct strbuf *err)
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530351{
352 struct align *align = &atom->u.align;
353 struct string_list params = STRING_LIST_INIT_DUP;
354 int i;
355 unsigned int width = ~0U;
356
357 if (!arg)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000358 return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530359
360 align->position = ALIGN_LEFT;
361
362 string_list_split(&params, arg, ',', -1);
363 for (i = 0; i < params.nr; i++) {
364 const char *s = params.items[i].string;
365 int position;
366
Karthik Nayak395fb8f2016-02-17 23:36:16 +0530367 if (skip_prefix(s, "position=", &s)) {
368 position = parse_align_position(s);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000369 if (position < 0) {
370 strbuf_addf(err, _("unrecognized position:%s"), s);
371 string_list_clear(&params, 0);
372 return -1;
373 }
Karthik Nayak395fb8f2016-02-17 23:36:16 +0530374 align->position = position;
375 } else if (skip_prefix(s, "width=", &s)) {
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000376 if (strtoul_ui(s, 10, &width)) {
377 strbuf_addf(err, _("unrecognized width:%s"), s);
378 string_list_clear(&params, 0);
379 return -1;
380 }
Karthik Nayak395fb8f2016-02-17 23:36:16 +0530381 } else if (!strtoul_ui(s, 10, &width))
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530382 ;
383 else if ((position = parse_align_position(s)) >= 0)
384 align->position = position;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000385 else {
386 strbuf_addf(err, _("unrecognized %%(align) argument: %s"), s);
387 string_list_clear(&params, 0);
388 return -1;
389 }
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530390 }
391
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000392 if (width == ~0U) {
393 string_list_clear(&params, 0);
394 return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
395 }
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530396 align->width = width;
397 string_list_clear(&params, 0);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000398 return 0;
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530399}
400
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000401static int if_atom_parser(const struct ref_format *format, struct used_atom *atom,
402 const char *arg, struct strbuf *err)
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530403{
404 if (!arg) {
405 atom->u.if_then_else.cmp_status = COMPARE_NONE;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000406 return 0;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530407 } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
408 atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
409 } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
410 atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000411 } else
412 return strbuf_addf_ret(err, -1, _("unrecognized %%(if) argument: %s"), arg);
413 return 0;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530414}
415
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000416static int head_atom_parser(const struct ref_format *format, struct used_atom *atom,
417 const char *arg, struct strbuf *unused_err)
Jeff King613a0e52017-05-19 02:12:12 -0400418{
René Scharfeefbd4fd2017-10-01 09:29:03 +0200419 atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000420 return 0;
Jeff King613a0e52017-05-19 02:12:12 -0400421}
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530422
Karthik Nayakc95b7582015-06-14 01:07:27 +0530423static struct {
424 const char *name;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +0000425 info_source source;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530426 cmp_type cmp_type;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000427 int (*parser)(const struct ref_format *format, struct used_atom *atom,
428 const char *arg, struct strbuf *err);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530429} valid_atom[] = {
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +0000430 { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser },
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000431 { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
432 { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +0000433 { "objectname", SOURCE_OTHER, FIELD_STR, objectname_atom_parser },
434 { "tree", SOURCE_OBJ },
435 { "parent", SOURCE_OBJ },
436 { "numparent", SOURCE_OBJ, FIELD_ULONG },
437 { "object", SOURCE_OBJ },
438 { "type", SOURCE_OBJ },
439 { "tag", SOURCE_OBJ },
440 { "author", SOURCE_OBJ },
441 { "authorname", SOURCE_OBJ },
442 { "authoremail", SOURCE_OBJ },
443 { "authordate", SOURCE_OBJ, FIELD_TIME },
444 { "committer", SOURCE_OBJ },
445 { "committername", SOURCE_OBJ },
446 { "committeremail", SOURCE_OBJ },
447 { "committerdate", SOURCE_OBJ, FIELD_TIME },
448 { "tagger", SOURCE_OBJ },
449 { "taggername", SOURCE_OBJ },
450 { "taggeremail", SOURCE_OBJ },
451 { "taggerdate", SOURCE_OBJ, FIELD_TIME },
452 { "creator", SOURCE_OBJ },
453 { "creatordate", SOURCE_OBJ, FIELD_TIME },
454 { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser },
455 { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser },
456 { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },
457 { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser },
458 { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
459 { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
460 { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser },
461 { "flag", SOURCE_NONE },
462 { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
463 { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
464 { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
465 { "end", SOURCE_NONE },
466 { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
467 { "then", SOURCE_NONE },
468 { "else", SOURCE_NONE },
Karthik Nayakc95b7582015-06-14 01:07:27 +0530469};
470
Karthik Nayak574e96a2015-09-10 21:18:18 +0530471#define REF_FORMATTING_STATE_INIT { 0, NULL }
472
473struct ref_formatting_stack {
474 struct ref_formatting_stack *prev;
475 struct strbuf output;
Karthik Nayakc58492d2017-01-10 14:19:34 +0530476 void (*at_end)(struct ref_formatting_stack **stack);
Karthik Nayakce592082015-09-11 20:33:07 +0530477 void *at_end_data;
Karthik Nayak574e96a2015-09-10 21:18:18 +0530478};
479
480struct ref_formatting_state {
481 int quote_style;
482 struct ref_formatting_stack *stack;
483};
484
Karthik Nayak3a257612015-08-22 09:09:37 +0530485struct atom_value {
486 const char *s;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000487 int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
488 struct strbuf *err);
Johannes Schindeline467dc12017-04-20 22:52:09 +0200489 uintmax_t value; /* used for sorting when not FIELD_STR */
Karthik Nayakc58fc852017-01-10 14:19:35 +0530490 struct used_atom *atom;
Karthik Nayak3a257612015-08-22 09:09:37 +0530491};
492
Karthik Nayakc95b7582015-06-14 01:07:27 +0530493/*
Karthik Nayakc95b7582015-06-14 01:07:27 +0530494 * Used to parse format string and sort specifiers
495 */
Jeff Kingab7ded32017-07-13 11:06:40 -0400496static int parse_ref_filter_atom(const struct ref_format *format,
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +0000497 const char *atom, const char *ep,
498 struct strbuf *err)
Karthik Nayakc95b7582015-06-14 01:07:27 +0530499{
500 const char *sp;
Karthik Nayak4de707e2016-02-17 23:36:12 +0530501 const char *arg;
SZEDER Gábore94ce132016-10-02 18:35:11 +0200502 int i, at, atom_len;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530503
504 sp = atom;
505 if (*sp == '*' && sp < ep)
506 sp++; /* deref */
507 if (ep <= sp)
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +0000508 return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
509 (int)(ep-atom), atom);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530510
511 /* Do we have the atom already used elsewhere? */
512 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +0530513 int len = strlen(used_atom[i].name);
514 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
Karthik Nayakc95b7582015-06-14 01:07:27 +0530515 return i;
516 }
517
SZEDER Gábore94ce132016-10-02 18:35:11 +0200518 /*
519 * If the atom name has a colon, strip it and everything after
520 * it off - it specifies the format for this entry, and
521 * shouldn't be used for checking against the valid_atom
522 * table.
523 */
524 arg = memchr(sp, ':', ep - sp);
525 atom_len = (arg ? arg : ep) - sp;
526
Karthik Nayakc95b7582015-06-14 01:07:27 +0530527 /* Is the atom a valid one? */
528 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
529 int len = strlen(valid_atom[i].name);
SZEDER Gábore94ce132016-10-02 18:35:11 +0200530 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
Karthik Nayakc95b7582015-06-14 01:07:27 +0530531 break;
532 }
533
534 if (ARRAY_SIZE(valid_atom) <= i)
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +0000535 return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
536 (int)(ep-atom), atom);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530537
538 /* Add it in, including the deref prefix */
539 at = used_atom_cnt;
540 used_atom_cnt++;
541 REALLOC_ARRAY(used_atom, used_atom_cnt);
Karthik Nayakb072add2016-02-17 23:36:11 +0530542 used_atom[at].name = xmemdupz(atom, ep - atom);
543 used_atom[at].type = valid_atom[i].cmp_type;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +0000544 used_atom[at].source = valid_atom[i].source;
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000545 if (used_atom[at].source == SOURCE_OBJ) {
546 if (*atom == '*')
547 oi_deref.info.contentp = &oi_deref.content;
548 else
549 oi.info.contentp = &oi.content;
550 }
Taylor Blaubea4dbe2017-10-02 09:10:34 -0700551 if (arg) {
Karthik Nayak4de707e2016-02-17 23:36:12 +0530552 arg = used_atom[at].name + (arg - atom) + 1;
Taylor Blaubea4dbe2017-10-02 09:10:34 -0700553 if (!*arg) {
554 /*
555 * Treat empty sub-arguments list as NULL (i.e.,
556 * "%(atom:)" is equivalent to "%(atom)").
557 */
558 arg = NULL;
559 }
560 }
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530561 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000562 if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
563 return -1;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530564 if (*atom == '*')
565 need_tagged = 1;
Karthik Nayak01f95822017-01-10 14:19:42 +0530566 if (!strcmp(valid_atom[i].name, "symref"))
Karthik Nayakc95b7582015-06-14 01:07:27 +0530567 need_symref = 1;
568 return at;
569}
570
Karthik Nayak63d89fb2015-09-10 21:18:20 +0530571static void quote_formatting(struct strbuf *s, const char *str, int quote_style)
572{
573 switch (quote_style) {
574 case QUOTE_NONE:
575 strbuf_addstr(s, str);
576 break;
577 case QUOTE_SHELL:
578 sq_quote_buf(s, str);
579 break;
580 case QUOTE_PERL:
581 perl_quote_buf(s, str);
582 break;
583 case QUOTE_PYTHON:
584 python_quote_buf(s, str);
585 break;
586 case QUOTE_TCL:
587 tcl_quote_buf(s, str);
588 break;
589 }
590}
591
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000592static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
593 struct strbuf *unused_err)
Karthik Nayak63d89fb2015-09-10 21:18:20 +0530594{
Karthik Nayakce592082015-09-11 20:33:07 +0530595 /*
596 * Quote formatting is only done when the stack has a single
597 * element. Otherwise quote formatting is done on the
598 * element's entire output strbuf when the %(end) atom is
599 * encountered.
600 */
601 if (!state->stack->prev)
602 quote_formatting(&state->stack->output, v->s, state->quote_style);
603 else
604 strbuf_addstr(&state->stack->output, v->s);
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000605 return 0;
Karthik Nayak63d89fb2015-09-10 21:18:20 +0530606}
607
Karthik Nayak574e96a2015-09-10 21:18:18 +0530608static void push_stack_element(struct ref_formatting_stack **stack)
609{
610 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
611
612 strbuf_init(&s->output, 0);
613 s->prev = *stack;
614 *stack = s;
615}
616
617static void pop_stack_element(struct ref_formatting_stack **stack)
618{
619 struct ref_formatting_stack *current = *stack;
620 struct ref_formatting_stack *prev = current->prev;
621
622 if (prev)
623 strbuf_addbuf(&prev->output, &current->output);
624 strbuf_release(&current->output);
625 free(current);
626 *stack = prev;
627}
628
Karthik Nayakc58492d2017-01-10 14:19:34 +0530629static void end_align_handler(struct ref_formatting_stack **stack)
Karthik Nayakce592082015-09-11 20:33:07 +0530630{
Karthik Nayakc58492d2017-01-10 14:19:34 +0530631 struct ref_formatting_stack *cur = *stack;
632 struct align *align = (struct align *)cur->at_end_data;
Karthik Nayakce592082015-09-11 20:33:07 +0530633 struct strbuf s = STRBUF_INIT;
634
Karthik Nayakc58492d2017-01-10 14:19:34 +0530635 strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
636 strbuf_swap(&cur->output, &s);
Karthik Nayakce592082015-09-11 20:33:07 +0530637 strbuf_release(&s);
638}
639
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000640static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
641 struct strbuf *unused_err)
Karthik Nayakce592082015-09-11 20:33:07 +0530642{
Brandon Williams1472b5b2018-02-14 10:59:46 -0800643 struct ref_formatting_stack *new_stack;
Karthik Nayakce592082015-09-11 20:33:07 +0530644
645 push_stack_element(&state->stack);
Brandon Williams1472b5b2018-02-14 10:59:46 -0800646 new_stack = state->stack;
647 new_stack->at_end = end_align_handler;
648 new_stack->at_end_data = &atomv->atom->u.align;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000649 return 0;
Karthik Nayakce592082015-09-11 20:33:07 +0530650}
651
Karthik Nayakc58492d2017-01-10 14:19:34 +0530652static void if_then_else_handler(struct ref_formatting_stack **stack)
653{
654 struct ref_formatting_stack *cur = *stack;
655 struct ref_formatting_stack *prev = cur->prev;
656 struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
657
658 if (!if_then_else->then_atom_seen)
659 die(_("format: %%(if) atom used without a %%(then) atom"));
660
661 if (if_then_else->else_atom_seen) {
662 /*
663 * There is an %(else) atom: we need to drop one state from the
664 * stack, either the %(else) branch if the condition is satisfied, or
665 * the %(then) branch if it isn't.
666 */
667 if (if_then_else->condition_satisfied) {
668 strbuf_reset(&cur->output);
669 pop_stack_element(&cur);
670 } else {
671 strbuf_swap(&cur->output, &prev->output);
672 strbuf_reset(&cur->output);
673 pop_stack_element(&cur);
674 }
675 } else if (!if_then_else->condition_satisfied) {
676 /*
677 * No %(else) atom: just drop the %(then) branch if the
678 * condition is not satisfied.
679 */
680 strbuf_reset(&cur->output);
681 }
682
683 *stack = cur;
684 free(if_then_else);
685}
686
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000687static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
688 struct strbuf *unused_err)
Karthik Nayakc58492d2017-01-10 14:19:34 +0530689{
Brandon Williams1472b5b2018-02-14 10:59:46 -0800690 struct ref_formatting_stack *new_stack;
Karthik Nayakc58492d2017-01-10 14:19:34 +0530691 struct if_then_else *if_then_else = xcalloc(sizeof(struct if_then_else), 1);
692
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530693 if_then_else->str = atomv->atom->u.if_then_else.str;
694 if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
695
Karthik Nayakc58492d2017-01-10 14:19:34 +0530696 push_stack_element(&state->stack);
Brandon Williams1472b5b2018-02-14 10:59:46 -0800697 new_stack = state->stack;
698 new_stack->at_end = if_then_else_handler;
699 new_stack->at_end_data = if_then_else;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000700 return 0;
Karthik Nayakc58492d2017-01-10 14:19:34 +0530701}
702
703static int is_empty(const char *s)
704{
705 while (*s != '\0') {
706 if (!isspace(*s))
707 return 0;
708 s++;
709 }
710 return 1;
711}
712
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000713static int then_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
714 struct strbuf *err)
Karthik Nayakc58492d2017-01-10 14:19:34 +0530715{
716 struct ref_formatting_stack *cur = state->stack;
717 struct if_then_else *if_then_else = NULL;
718
719 if (cur->at_end == if_then_else_handler)
720 if_then_else = (struct if_then_else *)cur->at_end_data;
721 if (!if_then_else)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000722 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used without an %%(if) atom"));
Karthik Nayakc58492d2017-01-10 14:19:34 +0530723 if (if_then_else->then_atom_seen)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000724 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
Karthik Nayakc58492d2017-01-10 14:19:34 +0530725 if (if_then_else->else_atom_seen)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000726 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
Karthik Nayakc58492d2017-01-10 14:19:34 +0530727 if_then_else->then_atom_seen = 1;
728 /*
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530729 * If the 'equals' or 'notequals' attribute is used then
730 * perform the required comparison. If not, only non-empty
731 * strings satisfy the 'if' condition.
Karthik Nayakc58492d2017-01-10 14:19:34 +0530732 */
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530733 if (if_then_else->cmp_status == COMPARE_EQUAL) {
734 if (!strcmp(if_then_else->str, cur->output.buf))
735 if_then_else->condition_satisfied = 1;
736 } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
737 if (strcmp(if_then_else->str, cur->output.buf))
738 if_then_else->condition_satisfied = 1;
739 } else if (cur->output.len && !is_empty(cur->output.buf))
Karthik Nayakc58492d2017-01-10 14:19:34 +0530740 if_then_else->condition_satisfied = 1;
741 strbuf_reset(&cur->output);
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000742 return 0;
Karthik Nayakc58492d2017-01-10 14:19:34 +0530743}
744
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000745static int else_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
746 struct strbuf *err)
Karthik Nayakc58492d2017-01-10 14:19:34 +0530747{
748 struct ref_formatting_stack *prev = state->stack;
749 struct if_then_else *if_then_else = NULL;
750
751 if (prev->at_end == if_then_else_handler)
752 if_then_else = (struct if_then_else *)prev->at_end_data;
753 if (!if_then_else)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000754 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without an %%(if) atom"));
Karthik Nayakc58492d2017-01-10 14:19:34 +0530755 if (!if_then_else->then_atom_seen)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000756 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used without a %%(then) atom"));
Karthik Nayakc58492d2017-01-10 14:19:34 +0530757 if (if_then_else->else_atom_seen)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000758 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
Karthik Nayakc58492d2017-01-10 14:19:34 +0530759 if_then_else->else_atom_seen = 1;
760 push_stack_element(&state->stack);
761 state->stack->at_end_data = prev->at_end_data;
762 state->stack->at_end = prev->at_end;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000763 return 0;
Karthik Nayakce592082015-09-11 20:33:07 +0530764}
765
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000766static int end_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
767 struct strbuf *err)
Karthik Nayakce592082015-09-11 20:33:07 +0530768{
769 struct ref_formatting_stack *current = state->stack;
770 struct strbuf s = STRBUF_INIT;
771
772 if (!current->at_end)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000773 return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
Karthik Nayakc58492d2017-01-10 14:19:34 +0530774 current->at_end(&state->stack);
775
776 /* Stack may have been popped within at_end(), hence reset the current pointer */
777 current = state->stack;
Karthik Nayakce592082015-09-11 20:33:07 +0530778
779 /*
780 * Perform quote formatting when the stack element is that of
781 * a supporting atom. If nested then perform quote formatting
782 * only on the topmost supporting atom.
783 */
Karthik Nayakc58492d2017-01-10 14:19:34 +0530784 if (!current->prev->prev) {
Karthik Nayakce592082015-09-11 20:33:07 +0530785 quote_formatting(&s, current->output.buf, state->quote_style);
786 strbuf_swap(&current->output, &s);
787 }
788 strbuf_release(&s);
789 pop_stack_element(&state->stack);
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000790 return 0;
Karthik Nayakce592082015-09-11 20:33:07 +0530791}
792
Karthik Nayakc95b7582015-06-14 01:07:27 +0530793/*
794 * In a format string, find the next occurrence of %(atom).
795 */
796static const char *find_next(const char *cp)
797{
798 while (*cp) {
799 if (*cp == '%') {
800 /*
801 * %( is the start of an atom;
802 * %% is a quoted per-cent.
803 */
804 if (cp[1] == '(')
805 return cp;
806 else if (cp[1] == '%')
807 cp++; /* skip over two % */
808 /* otherwise this is a singleton, literal % */
809 }
810 cp++;
811 }
812 return NULL;
813}
814
815/*
816 * Make sure the format string is well formed, and parse out
817 * the used atoms.
818 */
Jeff King4a68e362017-07-13 11:01:18 -0400819int verify_ref_format(struct ref_format *format)
Karthik Nayakc95b7582015-06-14 01:07:27 +0530820{
821 const char *cp, *sp;
822
Jeff Kingbf285ae2017-07-13 11:02:30 -0400823 format->need_color_reset_at_eol = 0;
Jeff King4a68e362017-07-13 11:01:18 -0400824 for (cp = format->format; *cp && (sp = find_next(cp)); ) {
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +0000825 struct strbuf err = STRBUF_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530826 const char *color, *ep = strchr(sp, ')');
827 int at;
828
829 if (!ep)
Nguyễn Thái Ngọc Duy1823c612016-02-27 13:42:04 +0700830 return error(_("malformed format string %s"), sp);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530831 /* sp points at "%(" and ep points at the closing ")" */
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +0000832 at = parse_ref_filter_atom(format, sp + 2, ep, &err);
833 if (at < 0)
834 die("%s", err.buf);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530835 cp = ep + 1;
836
Karthik Nayakb072add2016-02-17 23:36:11 +0530837 if (skip_prefix(used_atom[at].name, "color:", &color))
Jeff Kingbf285ae2017-07-13 11:02:30 -0400838 format->need_color_reset_at_eol = !!strcmp(color, "reset");
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +0000839 strbuf_release(&err);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530840 }
Jeff King11b087a2017-07-13 11:09:32 -0400841 if (format->need_color_reset_at_eol && !want_color(format->use_color))
842 format->need_color_reset_at_eol = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530843 return 0;
844}
845
brian m. carlson17769792018-03-12 02:27:27 +0000846static int grab_objectname(const char *name, const struct object_id *oid,
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530847 struct atom_value *v, struct used_atom *atom)
Karthik Nayakc95b7582015-06-14 01:07:27 +0530848{
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530849 if (starts_with(name, "objectname")) {
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530850 if (atom->u.objectname.option == O_SHORT) {
brian m. carlsonaab95832018-03-12 02:27:30 +0000851 v->s = xstrdup(find_unique_abbrev(oid, DEFAULT_ABBREV));
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530852 return 1;
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530853 } else if (atom->u.objectname.option == O_FULL) {
brian m. carlson17769792018-03-12 02:27:27 +0000854 v->s = xstrdup(oid_to_hex(oid));
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530855 return 1;
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530856 } else if (atom->u.objectname.option == O_LENGTH) {
brian m. carlsonaab95832018-03-12 02:27:30 +0000857 v->s = xstrdup(find_unique_abbrev(oid, atom->u.objectname.length));
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530858 return 1;
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530859 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +0200860 BUG("unknown %%(objectname) option");
Karthik Nayakc95b7582015-06-14 01:07:27 +0530861 }
862 return 0;
863}
864
865/* See grab_values */
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000866static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)
Karthik Nayakc95b7582015-06-14 01:07:27 +0530867{
868 int i;
869
870 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +0530871 const char *name = used_atom[i].name;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530872 struct atom_value *v = &val[i];
873 if (!!deref != (*name == '*'))
874 continue;
875 if (deref)
876 name++;
877 if (!strcmp(name, "objecttype"))
Olga Telezhnayaf0062d32018-10-18 07:28:54 +0000878 v->s = xstrdup(type_name(oi->type));
Karthik Nayakc95b7582015-06-14 01:07:27 +0530879 else if (!strcmp(name, "objectsize")) {
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000880 v->value = oi->size;
881 v->s = xstrfmt("%lu", oi->size);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530882 }
883 else if (deref)
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000884 grab_objectname(name, &oi->oid, v, &used_atom[i]);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530885 }
886}
887
888/* See grab_values */
889static void grab_tag_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
890{
891 int i;
892 struct tag *tag = (struct tag *) obj;
893
894 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +0530895 const char *name = used_atom[i].name;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530896 struct atom_value *v = &val[i];
897 if (!!deref != (*name == '*'))
898 continue;
899 if (deref)
900 name++;
901 if (!strcmp(name, "tag"))
Olga Telezhnayaf0062d32018-10-18 07:28:54 +0000902 v->s = xstrdup(tag->tag);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530903 else if (!strcmp(name, "type") && tag->tagged)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +0000904 v->s = xstrdup(type_name(tag->tagged->type));
Jeff Kinga5e03bf2015-09-24 17:07:12 -0400905 else if (!strcmp(name, "object") && tag->tagged)
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000906 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
Karthik Nayakc95b7582015-06-14 01:07:27 +0530907 }
908}
909
910/* See grab_values */
911static void grab_commit_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
912{
913 int i;
914 struct commit *commit = (struct commit *) obj;
915
916 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +0530917 const char *name = used_atom[i].name;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530918 struct atom_value *v = &val[i];
919 if (!!deref != (*name == '*'))
920 continue;
921 if (deref)
922 name++;
923 if (!strcmp(name, "tree")) {
Derrick Stolee2e27bd72018-04-06 19:09:38 +0000924 v->s = xstrdup(oid_to_hex(get_commit_tree_oid(commit)));
Karthik Nayakc95b7582015-06-14 01:07:27 +0530925 }
Jeff Kinga5e03bf2015-09-24 17:07:12 -0400926 else if (!strcmp(name, "numparent")) {
Johannes Schindeline467dc12017-04-20 22:52:09 +0200927 v->value = commit_list_count(commit->parents);
928 v->s = xstrfmt("%lu", (unsigned long)v->value);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530929 }
930 else if (!strcmp(name, "parent")) {
Karthik Nayakc95b7582015-06-14 01:07:27 +0530931 struct commit_list *parents;
Jeff Kinga5e03bf2015-09-24 17:07:12 -0400932 struct strbuf s = STRBUF_INIT;
933 for (parents = commit->parents; parents; parents = parents->next) {
Karthik Nayakc95b7582015-06-14 01:07:27 +0530934 struct commit *parent = parents->item;
Jeff Kinga5e03bf2015-09-24 17:07:12 -0400935 if (parents != commit->parents)
936 strbuf_addch(&s, ' ');
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000937 strbuf_addstr(&s, oid_to_hex(&parent->object.oid));
Karthik Nayakc95b7582015-06-14 01:07:27 +0530938 }
Jeff Kinga5e03bf2015-09-24 17:07:12 -0400939 v->s = strbuf_detach(&s, NULL);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530940 }
941 }
942}
943
944static const char *find_wholine(const char *who, int wholen, const char *buf, unsigned long sz)
945{
946 const char *eol;
947 while (*buf) {
948 if (!strncmp(buf, who, wholen) &&
949 buf[wholen] == ' ')
950 return buf + wholen + 1;
951 eol = strchr(buf, '\n');
952 if (!eol)
953 return "";
954 eol++;
955 if (*eol == '\n')
956 return ""; /* end of header */
957 buf = eol;
958 }
959 return "";
960}
961
962static const char *copy_line(const char *buf)
963{
964 const char *eol = strchrnul(buf, '\n');
965 return xmemdupz(buf, eol - buf);
966}
967
968static const char *copy_name(const char *buf)
969{
970 const char *cp;
971 for (cp = buf; *cp && *cp != '\n'; cp++) {
972 if (!strncmp(cp, " <", 2))
973 return xmemdupz(buf, cp - buf);
974 }
975 return "";
976}
977
978static const char *copy_email(const char *buf)
979{
980 const char *email = strchr(buf, '<');
981 const char *eoemail;
982 if (!email)
983 return "";
984 eoemail = strchr(email, '>');
985 if (!eoemail)
986 return "";
987 return xmemdupz(email, eoemail + 1 - email);
988}
989
990static char *copy_subject(const char *buf, unsigned long len)
991{
992 char *r = xmemdupz(buf, len);
993 int i;
994
995 for (i = 0; i < len; i++)
996 if (r[i] == '\n')
997 r[i] = ' ';
998
999 return r;
1000}
1001
1002static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
1003{
1004 const char *eoemail = strstr(buf, "> ");
1005 char *zone;
Johannes Schindelindddbad72017-04-26 21:29:31 +02001006 timestamp_t timestamp;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301007 long tz;
Junio C Hamanod939af12015-08-03 11:01:27 -07001008 struct date_mode date_mode = { DATE_NORMAL };
Karthik Nayakc95b7582015-06-14 01:07:27 +05301009 const char *formatp;
1010
1011 /*
1012 * We got here because atomname ends in "date" or "date<something>";
1013 * it's not possible that <something> is not ":<format>" because
1014 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1015 * ":" means no format is specified, and use the default.
1016 */
1017 formatp = strchr(atomname, ':');
1018 if (formatp != NULL) {
1019 formatp++;
Junio C Hamanod939af12015-08-03 11:01:27 -07001020 parse_date_format(formatp, &date_mode);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301021 }
1022
1023 if (!eoemail)
1024 goto bad;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +02001025 timestamp = parse_timestamp(eoemail + 2, &zone, 10);
Johannes Schindelindddbad72017-04-26 21:29:31 +02001026 if (timestamp == TIME_MAX)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301027 goto bad;
1028 tz = strtol(zone, NULL, 10);
1029 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
1030 goto bad;
Junio C Hamanod939af12015-08-03 11:01:27 -07001031 v->s = xstrdup(show_date(timestamp, tz, &date_mode));
Johannes Schindeline467dc12017-04-20 22:52:09 +02001032 v->value = timestamp;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301033 return;
1034 bad:
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001035 v->s = xstrdup("");
Johannes Schindeline467dc12017-04-20 22:52:09 +02001036 v->value = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301037}
1038
1039/* See grab_values */
1040static void grab_person(const char *who, struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
1041{
1042 int i;
1043 int wholen = strlen(who);
1044 const char *wholine = NULL;
1045
1046 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +05301047 const char *name = used_atom[i].name;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301048 struct atom_value *v = &val[i];
1049 if (!!deref != (*name == '*'))
1050 continue;
1051 if (deref)
1052 name++;
1053 if (strncmp(who, name, wholen))
1054 continue;
1055 if (name[wholen] != 0 &&
1056 strcmp(name + wholen, "name") &&
1057 strcmp(name + wholen, "email") &&
1058 !starts_with(name + wholen, "date"))
1059 continue;
1060 if (!wholine)
1061 wholine = find_wholine(who, wholen, buf, sz);
1062 if (!wholine)
1063 return; /* no point looking for it */
1064 if (name[wholen] == 0)
1065 v->s = copy_line(wholine);
1066 else if (!strcmp(name + wholen, "name"))
1067 v->s = copy_name(wholine);
1068 else if (!strcmp(name + wholen, "email"))
1069 v->s = copy_email(wholine);
1070 else if (starts_with(name + wholen, "date"))
1071 grab_date(wholine, v, name);
1072 }
1073
1074 /*
1075 * For a tag or a commit object, if "creator" or "creatordate" is
1076 * requested, do something special.
1077 */
1078 if (strcmp(who, "tagger") && strcmp(who, "committer"))
1079 return; /* "author" for commit object is not wanted */
1080 if (!wholine)
1081 wholine = find_wholine(who, wholen, buf, sz);
1082 if (!wholine)
1083 return;
1084 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +05301085 const char *name = used_atom[i].name;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301086 struct atom_value *v = &val[i];
1087 if (!!deref != (*name == '*'))
1088 continue;
1089 if (deref)
1090 name++;
1091
1092 if (starts_with(name, "creatordate"))
1093 grab_date(wholine, v, name);
1094 else if (!strcmp(name, "creator"))
1095 v->s = copy_line(wholine);
1096 }
1097}
1098
1099static void find_subpos(const char *buf, unsigned long sz,
1100 const char **sub, unsigned long *sublen,
1101 const char **body, unsigned long *bodylen,
1102 unsigned long *nonsiglen,
1103 const char **sig, unsigned long *siglen)
1104{
1105 const char *eol;
1106 /* skip past header until we hit empty line */
1107 while (*buf && *buf != '\n') {
1108 eol = strchrnul(buf, '\n');
1109 if (*eol)
1110 eol++;
1111 buf = eol;
1112 }
1113 /* skip any empty lines */
1114 while (*buf == '\n')
1115 buf++;
1116
1117 /* parse signature first; we might not even have a subject line */
1118 *sig = buf + parse_signature(buf, strlen(buf));
1119 *siglen = strlen(*sig);
1120
1121 /* subject is first non-empty line */
1122 *sub = buf;
1123 /* subject goes to first empty line */
1124 while (buf < *sig && *buf && *buf != '\n') {
1125 eol = strchrnul(buf, '\n');
1126 if (*eol)
1127 eol++;
1128 buf = eol;
1129 }
1130 *sublen = buf - *sub;
1131 /* drop trailing newline, if present */
1132 if (*sublen && (*sub)[*sublen - 1] == '\n')
1133 *sublen -= 1;
1134
1135 /* skip any empty lines */
1136 while (*buf == '\n')
1137 buf++;
1138 *body = buf;
1139 *bodylen = strlen(buf);
1140 *nonsiglen = *sig - buf;
1141}
1142
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301143/*
1144 * If 'lines' is greater than 0, append that many lines from the given
1145 * 'buf' of length 'size' to the given strbuf.
1146 */
1147static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
1148{
1149 int i;
1150 const char *sp, *eol;
1151 size_t len;
1152
1153 sp = buf;
1154
1155 for (i = 0; i < lines && sp < buf + size; i++) {
1156 if (i)
1157 strbuf_addstr(out, "\n ");
1158 eol = memchr(sp, '\n', size - (sp - buf));
1159 len = eol ? eol - sp : size - (sp - buf);
1160 strbuf_add(out, sp, len);
1161 if (!eol)
1162 break;
1163 sp = eol + 1;
1164 }
1165}
1166
Karthik Nayakc95b7582015-06-14 01:07:27 +05301167/* See grab_values */
1168static void grab_sub_body_contents(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
1169{
1170 int i;
1171 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
1172 unsigned long sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
1173
1174 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayak452db392016-02-17 23:36:18 +05301175 struct used_atom *atom = &used_atom[i];
1176 const char *name = atom->name;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301177 struct atom_value *v = &val[i];
1178 if (!!deref != (*name == '*'))
1179 continue;
1180 if (deref)
1181 name++;
1182 if (strcmp(name, "subject") &&
1183 strcmp(name, "body") &&
Taylor Blau67a20a02017-10-01 22:25:23 -07001184 !starts_with(name, "trailers") &&
Karthik Nayak452db392016-02-17 23:36:18 +05301185 !starts_with(name, "contents"))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301186 continue;
1187 if (!subpos)
1188 find_subpos(buf, sz,
1189 &subpos, &sublen,
1190 &bodypos, &bodylen, &nonsiglen,
1191 &sigpos, &siglen);
1192
Karthik Nayak452db392016-02-17 23:36:18 +05301193 if (atom->u.contents.option == C_SUB)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301194 v->s = copy_subject(subpos, sublen);
Karthik Nayak452db392016-02-17 23:36:18 +05301195 else if (atom->u.contents.option == C_BODY_DEP)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301196 v->s = xmemdupz(bodypos, bodylen);
Karthik Nayak452db392016-02-17 23:36:18 +05301197 else if (atom->u.contents.option == C_BODY)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301198 v->s = xmemdupz(bodypos, nonsiglen);
Karthik Nayak452db392016-02-17 23:36:18 +05301199 else if (atom->u.contents.option == C_SIG)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301200 v->s = xmemdupz(sigpos, siglen);
Karthik Nayak452db392016-02-17 23:36:18 +05301201 else if (atom->u.contents.option == C_LINES) {
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301202 struct strbuf s = STRBUF_INIT;
1203 const char *contents_end = bodylen + bodypos - siglen;
1204
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301205 /* Size is the length of the message after removing the signature */
Karthik Nayak452db392016-02-17 23:36:18 +05301206 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301207 v->s = strbuf_detach(&s, NULL);
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001208 } else if (atom->u.contents.option == C_TRAILERS) {
Taylor Blau67a20a02017-10-01 22:25:23 -07001209 struct strbuf s = STRBUF_INIT;
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001210
Taylor Blau67a20a02017-10-01 22:25:23 -07001211 /* Format the trailer info according to the trailer_opts given */
1212 format_trailers_from_commit(&s, subpos, &atom->u.contents.trailer_opts);
1213
1214 v->s = strbuf_detach(&s, NULL);
Karthik Nayak452db392016-02-17 23:36:18 +05301215 } else if (atom->u.contents.option == C_BARE)
1216 v->s = xstrdup(subpos);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301217 }
1218}
1219
1220/*
1221 * We want to have empty print-string for field requests
1222 * that do not apply (e.g. "authordate" for a tag object)
1223 */
1224static void fill_missing_values(struct atom_value *val)
1225{
1226 int i;
1227 for (i = 0; i < used_atom_cnt; i++) {
1228 struct atom_value *v = &val[i];
1229 if (v->s == NULL)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001230 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301231 }
1232}
1233
1234/*
1235 * val is a list of atom_value to hold returned values. Extract
1236 * the values for atoms in used_atom array out of (obj, buf, sz).
1237 * when deref is false, (obj, buf, sz) is the object that is
1238 * pointed at by the ref itself; otherwise it is the object the
1239 * ref (which is a tag) refers to.
1240 */
1241static void grab_values(struct atom_value *val, int deref, struct object *obj, void *buf, unsigned long sz)
1242{
Karthik Nayakc95b7582015-06-14 01:07:27 +05301243 switch (obj->type) {
1244 case OBJ_TAG:
1245 grab_tag_values(val, deref, obj, buf, sz);
1246 grab_sub_body_contents(val, deref, obj, buf, sz);
1247 grab_person("tagger", val, deref, obj, buf, sz);
1248 break;
1249 case OBJ_COMMIT:
1250 grab_commit_values(val, deref, obj, buf, sz);
1251 grab_sub_body_contents(val, deref, obj, buf, sz);
1252 grab_person("author", val, deref, obj, buf, sz);
1253 grab_person("committer", val, deref, obj, buf, sz);
1254 break;
1255 case OBJ_TREE:
1256 /* grab_tree_values(val, deref, obj, buf, sz); */
1257 break;
1258 case OBJ_BLOB:
1259 /* grab_blob_values(val, deref, obj, buf, sz); */
1260 break;
1261 default:
1262 die("Eh? Object of type %d?", obj->type);
1263 }
1264}
1265
1266static inline char *copy_advance(char *dst, const char *src)
1267{
1268 while (*src)
1269 *dst++ = *src++;
1270 return dst;
1271}
1272
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05301273static const char *lstrip_ref_components(const char *refname, int len)
Jeff King05719792016-01-25 22:00:05 -05001274{
Karthik Nayaka7984102017-01-10 14:19:44 +05301275 long remaining = len;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001276 const char *start = xstrdup(refname);
1277 const char *to_free = start;
Jeff King05719792016-01-25 22:00:05 -05001278
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05301279 if (len < 0) {
1280 int i;
1281 const char *p = refname;
Jeff King05719792016-01-25 22:00:05 -05001282
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05301283 /* Find total no of '/' separated path-components */
1284 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1285 ;
1286 /*
1287 * The number of components we need to strip is now
1288 * the total minus the components to be left (Plus one
1289 * because we count the number of '/', but the number
1290 * of components is one more than the no of '/').
1291 */
1292 remaining = i + len + 1;
1293 }
1294
1295 while (remaining > 0) {
Jeff King05719792016-01-25 22:00:05 -05001296 switch (*start++) {
1297 case '\0':
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001298 free((char *)to_free);
1299 return xstrdup("");
Jeff King05719792016-01-25 22:00:05 -05001300 case '/':
1301 remaining--;
1302 break;
1303 }
1304 }
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05301305
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001306 start = xstrdup(start);
1307 free((char *)to_free);
Jeff King05719792016-01-25 22:00:05 -05001308 return start;
1309}
1310
Karthik Nayak1a347282017-01-10 14:19:49 +05301311static const char *rstrip_ref_components(const char *refname, int len)
1312{
1313 long remaining = len;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001314 const char *start = xstrdup(refname);
1315 const char *to_free = start;
Karthik Nayak1a347282017-01-10 14:19:49 +05301316
1317 if (len < 0) {
1318 int i;
1319 const char *p = refname;
1320
1321 /* Find total no of '/' separated path-components */
1322 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
1323 ;
1324 /*
1325 * The number of components we need to strip is now
1326 * the total minus the components to be left (Plus one
1327 * because we count the number of '/', but the number
1328 * of components is one more than the no of '/').
1329 */
1330 remaining = i + len + 1;
1331 }
1332
1333 while (remaining-- > 0) {
1334 char *p = strrchr(start, '/');
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001335 if (p == NULL) {
1336 free((char *)to_free);
1337 return xstrdup("");
1338 } else
Karthik Nayak1a347282017-01-10 14:19:49 +05301339 p[0] = '\0';
1340 }
1341 return start;
1342}
1343
Karthik Nayaka7984102017-01-10 14:19:44 +05301344static const char *show_ref(struct refname_atom *atom, const char *refname)
1345{
1346 if (atom->option == R_SHORT)
1347 return shorten_unambiguous_ref(refname, warn_ambiguous_refs);
Karthik Nayak17938f12017-01-10 14:19:46 +05301348 else if (atom->option == R_LSTRIP)
1349 return lstrip_ref_components(refname, atom->lstrip);
Karthik Nayak1a347282017-01-10 14:19:49 +05301350 else if (atom->option == R_RSTRIP)
1351 return rstrip_ref_components(refname, atom->rstrip);
Karthik Nayaka7984102017-01-10 14:19:44 +05301352 else
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001353 return xstrdup(refname);
Karthik Nayaka7984102017-01-10 14:19:44 +05301354}
1355
Karthik Nayak5339bda2016-02-17 23:36:17 +05301356static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
1357 struct branch *branch, const char **s)
1358{
1359 int num_ours, num_theirs;
Karthik Nayak3ba308c2017-01-10 14:19:45 +05301360 if (atom->u.remote_ref.option == RR_REF)
1361 *s = show_ref(&atom->u.remote_ref.refname, refname);
Karthik Nayak7743fcc2017-01-10 14:19:41 +05301362 else if (atom->u.remote_ref.option == RR_TRACK) {
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001363 if (stat_tracking_info(branch, &num_ours, &num_theirs,
1364 NULL, AHEAD_BEHIND_FULL) < 0) {
Karthik Nayak6eac70f2017-01-10 14:19:50 +05301365 *s = xstrdup(msgs.gone);
Karthik Nayak7743fcc2017-01-10 14:19:41 +05301366 } else if (!num_ours && !num_theirs)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001367 *s = xstrdup("");
Karthik Nayak5339bda2016-02-17 23:36:17 +05301368 else if (!num_ours)
Karthik Nayak6eac70f2017-01-10 14:19:50 +05301369 *s = xstrfmt(msgs.behind, num_theirs);
Karthik Nayak5339bda2016-02-17 23:36:17 +05301370 else if (!num_theirs)
Karthik Nayak6eac70f2017-01-10 14:19:50 +05301371 *s = xstrfmt(msgs.ahead, num_ours);
Karthik Nayak5339bda2016-02-17 23:36:17 +05301372 else
Karthik Nayak6eac70f2017-01-10 14:19:50 +05301373 *s = xstrfmt(msgs.ahead_behind,
Karthik Nayak5339bda2016-02-17 23:36:17 +05301374 num_ours, num_theirs);
Karthik Nayak7743fcc2017-01-10 14:19:41 +05301375 if (!atom->u.remote_ref.nobracket && *s[0]) {
1376 const char *to_free = *s;
1377 *s = xstrfmt("[%s]", *s);
1378 free((void *)to_free);
1379 }
1380 } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00001381 if (stat_tracking_info(branch, &num_ours, &num_theirs,
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001382 NULL, AHEAD_BEHIND_FULL) < 0) {
1383 *s = xstrdup("");
Karthik Nayak5339bda2016-02-17 23:36:17 +05301384 return;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001385 }
Karthik Nayak5339bda2016-02-17 23:36:17 +05301386 if (!num_ours && !num_theirs)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001387 *s = xstrdup("=");
Karthik Nayak5339bda2016-02-17 23:36:17 +05301388 else if (!num_ours)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001389 *s = xstrdup("<");
Karthik Nayak5339bda2016-02-17 23:36:17 +05301390 else if (!num_theirs)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001391 *s = xstrdup(">");
Karthik Nayak5339bda2016-02-17 23:36:17 +05301392 else
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001393 *s = xstrdup("<>");
Johannes Schindelincc723852017-10-05 14:19:09 +02001394 } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
1395 int explicit;
1396 const char *remote = atom->u.remote_ref.push ?
1397 pushremote_for_branch(branch, &explicit) :
1398 remote_for_branch(branch, &explicit);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001399 *s = xstrdup(explicit ? remote : "");
J Wyman9700fae2017-11-07 17:31:08 +01001400 } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
1401 int explicit;
1402 const char *merge;
1403
1404 merge = remote_ref_for_branch(branch, atom->u.remote_ref.push,
1405 &explicit);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001406 *s = xstrdup(explicit ? merge : "");
Karthik Nayak3ba308c2017-01-10 14:19:45 +05301407 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02001408 BUG("unhandled RR_* enum");
Karthik Nayak5339bda2016-02-17 23:36:17 +05301409}
1410
Karthik Nayakd4919bb2017-01-10 14:19:38 +05301411char *get_head_description(void)
1412{
1413 struct strbuf desc = STRBUF_INIT;
1414 struct wt_status_state state;
1415 memset(&state, 0, sizeof(state));
1416 wt_status_get_state(&state, 1);
1417 if (state.rebase_in_progress ||
Kaartic Sivaraama236f902018-04-03 10:01:00 +05301418 state.rebase_interactive_in_progress) {
1419 if (state.branch)
1420 strbuf_addf(&desc, _("(no branch, rebasing %s)"),
1421 state.branch);
1422 else
1423 strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"),
1424 state.detached_from);
1425 } else if (state.bisect_in_progress)
Karthik Nayakd4919bb2017-01-10 14:19:38 +05301426 strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
1427 state.branch);
1428 else if (state.detached_from) {
Karthik Nayakd4919bb2017-01-10 14:19:38 +05301429 if (state.detached_at)
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +00001430 /*
1431 * TRANSLATORS: make sure this matches "HEAD
1432 * detached at " in wt-status.c
1433 */
Karthik Nayakd4919bb2017-01-10 14:19:38 +05301434 strbuf_addf(&desc, _("(HEAD detached at %s)"),
1435 state.detached_from);
1436 else
Ævar Arnfjörð Bjarmason66f5f6d2017-05-11 21:20:12 +00001437 /*
1438 * TRANSLATORS: make sure this matches "HEAD
1439 * detached from " in wt-status.c
1440 */
Karthik Nayakd4919bb2017-01-10 14:19:38 +05301441 strbuf_addf(&desc, _("(HEAD detached from %s)"),
1442 state.detached_from);
1443 }
1444 else
1445 strbuf_addstr(&desc, _("(no branch)"));
1446 free(state.branch);
1447 free(state.onto);
1448 free(state.detached_from);
1449 return strbuf_detach(&desc, NULL);
1450}
1451
Karthik Nayaka7984102017-01-10 14:19:44 +05301452static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
1453{
1454 if (!ref->symref)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001455 return xstrdup("");
Karthik Nayaka7984102017-01-10 14:19:44 +05301456 else
1457 return show_ref(&atom->u.refname, ref->symref);
1458}
1459
1460static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
1461{
1462 if (ref->kind & FILTER_REFS_DETACHED_HEAD)
1463 return get_head_description();
1464 return show_ref(&atom->u.refname, ref->refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301465}
1466
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001467static int get_object(struct ref_array_item *ref, int deref, struct object **obj,
1468 struct expand_data *oi, struct strbuf *err)
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00001469{
Olga Telezhnaya04f6ee12018-07-17 08:22:57 +00001470 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
1471 int eaten = 1;
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001472 if (oi->info.contentp) {
1473 /* We need to know that to use parse_object_buffer properly */
1474 oi->info.sizep = &oi->size;
1475 oi->info.typep = &oi->type;
Olga Telezhnayae2255172018-07-17 08:22:57 +00001476 }
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001477 if (oid_object_info_extended(the_repository, &oi->oid, &oi->info,
1478 OBJECT_INFO_LOOKUP_REPLACE))
1479 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1480 oid_to_hex(&oi->oid), ref->refname);
1481
1482 if (oi->info.contentp) {
Junio C Hamanoc83149a2018-08-17 13:09:57 -07001483 *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001484 if (!obj) {
1485 if (!eaten)
1486 free(oi->content);
1487 return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
1488 oid_to_hex(&oi->oid), ref->refname);
1489 }
1490 grab_values(ref->value, deref, *obj, oi->content, oi->size);
1491 }
1492
1493 grab_common_values(ref->value, deref, oi);
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00001494 if (!eaten)
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001495 free(oi->content);
1496 return 0;
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00001497}
1498
Karthik Nayakc95b7582015-06-14 01:07:27 +05301499/*
1500 * Parse the object referred by ref, and grab needed value.
1501 */
Olga Telezhnayae3396112018-03-29 12:49:45 +00001502static int populate_value(struct ref_array_item *ref, struct strbuf *err)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301503{
Karthik Nayakc95b7582015-06-14 01:07:27 +05301504 struct object *obj;
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00001505 int i;
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001506 struct object_info empty = OBJECT_INFO_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301507
1508 ref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));
1509
1510 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05301511 ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING,
René Scharfeefbd4fd2017-10-01 09:29:03 +02001512 NULL, NULL);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301513 if (!ref->symref)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001514 ref->symref = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301515 }
1516
1517 /* Fill in specials first */
1518 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakfd935cc2016-02-17 23:36:13 +05301519 struct used_atom *atom = &used_atom[i];
Karthik Nayakb072add2016-02-17 23:36:11 +05301520 const char *name = used_atom[i].name;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301521 struct atom_value *v = &ref->value[i];
1522 int deref = 0;
1523 const char *refname;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301524 struct branch *branch = NULL;
1525
Karthik Nayak63d89fb2015-09-10 21:18:20 +05301526 v->handler = append_atom;
Karthik Nayakc58fc852017-01-10 14:19:35 +05301527 v->atom = atom;
Karthik Nayak63d89fb2015-09-10 21:18:20 +05301528
Karthik Nayakc95b7582015-06-14 01:07:27 +05301529 if (*name == '*') {
1530 deref = 1;
1531 name++;
1532 }
1533
1534 if (starts_with(name, "refname"))
Karthik Nayaka7984102017-01-10 14:19:44 +05301535 refname = get_refname(atom, ref);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301536 else if (starts_with(name, "symref"))
Karthik Nayaka7984102017-01-10 14:19:44 +05301537 refname = get_symref(atom, ref);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301538 else if (starts_with(name, "upstream")) {
1539 const char *branch_name;
1540 /* only local branches may have an upstream */
1541 if (!skip_prefix(ref->refname, "refs/heads/",
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001542 &branch_name)) {
1543 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301544 continue;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001545 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05301546 branch = branch_get(branch_name);
1547
1548 refname = branch_get_upstream(branch, NULL);
Karthik Nayak5339bda2016-02-17 23:36:17 +05301549 if (refname)
1550 fill_remote_ref_details(atom, refname, branch, &v->s);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001551 else
1552 v->s = xstrdup("");
Karthik Nayak5339bda2016-02-17 23:36:17 +05301553 continue;
Johannes Schindelincc723852017-10-05 14:19:09 +02001554 } else if (atom->u.remote_ref.push) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05301555 const char *branch_name;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001556 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301557 if (!skip_prefix(ref->refname, "refs/heads/",
1558 &branch_name))
1559 continue;
1560 branch = branch_get(branch_name);
1561
Johannes Schindelincc723852017-10-05 14:19:09 +02001562 if (atom->u.remote_ref.push_remote)
1563 refname = NULL;
1564 else {
1565 refname = branch_get_push(branch, NULL);
1566 if (!refname)
1567 continue;
1568 }
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001569 /* We will definitely re-init v->s on the next line. */
1570 free((char *)v->s);
Karthik Nayak5339bda2016-02-17 23:36:17 +05301571 fill_remote_ref_details(atom, refname, branch, &v->s);
1572 continue;
Karthik Nayakfd935cc2016-02-17 23:36:13 +05301573 } else if (starts_with(name, "color:")) {
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001574 v->s = xstrdup(atom->u.color);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301575 continue;
1576 } else if (!strcmp(name, "flag")) {
1577 char buf[256], *cp = buf;
1578 if (ref->flag & REF_ISSYMREF)
1579 cp = copy_advance(cp, ",symref");
1580 if (ref->flag & REF_ISPACKED)
1581 cp = copy_advance(cp, ",packed");
1582 if (cp == buf)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001583 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301584 else {
1585 *cp = '\0';
1586 v->s = xstrdup(buf + 1);
1587 }
1588 continue;
brian m. carlson17769792018-03-12 02:27:27 +00001589 } else if (!deref && grab_objectname(name, &ref->objectname, v, atom)) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05301590 continue;
1591 } else if (!strcmp(name, "HEAD")) {
Jeff King613a0e52017-05-19 02:12:12 -04001592 if (atom->u.head && !strcmp(ref->refname, atom->u.head))
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001593 v->s = xstrdup("*");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301594 else
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001595 v->s = xstrdup(" ");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301596 continue;
Karthik Nayak5bd881d2016-02-17 23:36:15 +05301597 } else if (starts_with(name, "align")) {
Karthik Nayakce592082015-09-11 20:33:07 +05301598 v->handler = align_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001599 v->s = xstrdup("");
Karthik Nayakce592082015-09-11 20:33:07 +05301600 continue;
1601 } else if (!strcmp(name, "end")) {
1602 v->handler = end_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001603 v->s = xstrdup("");
Karthik Nayakce592082015-09-11 20:33:07 +05301604 continue;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05301605 } else if (starts_with(name, "if")) {
1606 const char *s;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05301607 if (skip_prefix(name, "if:", &s))
1608 v->s = xstrdup(s);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001609 else
1610 v->s = xstrdup("");
Karthik Nayakc58492d2017-01-10 14:19:34 +05301611 v->handler = if_atom_handler;
1612 continue;
1613 } else if (!strcmp(name, "then")) {
1614 v->handler = then_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001615 v->s = xstrdup("");
Karthik Nayakc58492d2017-01-10 14:19:34 +05301616 continue;
1617 } else if (!strcmp(name, "else")) {
1618 v->handler = else_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001619 v->s = xstrdup("");
Karthik Nayakc58492d2017-01-10 14:19:34 +05301620 continue;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301621 } else
1622 continue;
1623
Karthik Nayakc95b7582015-06-14 01:07:27 +05301624 if (!deref)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001625 v->s = xstrdup(refname);
Jeff Kinga5e03bf2015-09-24 17:07:12 -04001626 else
1627 v->s = xstrfmt("%s^{}", refname);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001628 free((char *)refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301629 }
1630
1631 for (i = 0; i < used_atom_cnt; i++) {
1632 struct atom_value *v = &ref->value[i];
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001633 if (v->s == NULL && used_atom[i].source == SOURCE_NONE)
1634 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
1635 oid_to_hex(&ref->objectname), ref->refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301636 }
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001637
1638 if (need_tagged)
1639 oi.info.contentp = &oi.content;
1640 if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
1641 !memcmp(&oi_deref.info, &empty, sizeof(empty)))
Olga Telezhnayae3396112018-03-29 12:49:45 +00001642 return 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301643
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001644
1645 oi.oid = ref->objectname;
1646 if (get_object(ref, 0, &obj, &oi, err))
Olga Telezhnayae3396112018-03-29 12:49:45 +00001647 return -1;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301648
1649 /*
1650 * If there is no atom that wants to know about tagged
1651 * object, we are done.
1652 */
1653 if (!need_tagged || (obj->type != OBJ_TAG))
Olga Telezhnayae3396112018-03-29 12:49:45 +00001654 return 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301655
1656 /*
1657 * If it is a tag object, see if we use a value that derefs
1658 * the object, and if we do grab the object it refers to.
1659 */
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001660 oi_deref.oid = ((struct tag *)obj)->tagged->oid;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301661
1662 /*
1663 * NEEDSWORK: This derefs tag only once, which
1664 * is good to deal with chains of trust, but
1665 * is not consistent with what deref_tag() does
1666 * which peels the onion to the core.
1667 */
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001668 return get_object(ref, 1, &obj, &oi_deref, err);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301669}
1670
1671/*
1672 * Given a ref, return the value for the atom. This lazily gets value
1673 * out of the object by calling populate value.
1674 */
Olga Telezhnayae3396112018-03-29 12:49:45 +00001675static int get_ref_atom_value(struct ref_array_item *ref, int atom,
1676 struct atom_value **v, struct strbuf *err)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301677{
1678 if (!ref->value) {
Olga Telezhnayae3396112018-03-29 12:49:45 +00001679 if (populate_value(ref, err))
1680 return -1;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301681 fill_missing_values(ref->value);
1682 }
1683 *v = &ref->value[atom];
Olga Telezhnayae3396112018-03-29 12:49:45 +00001684 return 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301685}
1686
Jeff Kinga91aca42017-03-09 08:29:49 -05001687/*
Karthik Nayakc95b7582015-06-14 01:07:27 +05301688 * Return 1 if the refname matches one of the patterns, otherwise 0.
Karthik Nayakbef0e122015-09-10 21:18:26 +05301689 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1690 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1691 * matches "refs/heads/mas*", too).
1692 */
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07001693static int match_pattern(const struct ref_filter *filter, const char *refname)
Karthik Nayakbef0e122015-09-10 21:18:26 +05301694{
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07001695 const char **patterns = filter->name_patterns;
1696 unsigned flags = 0;
1697
1698 if (filter->ignore_case)
1699 flags |= WM_CASEFOLD;
1700
Karthik Nayakbef0e122015-09-10 21:18:26 +05301701 /*
1702 * When no '--format' option is given we need to skip the prefix
1703 * for matching refs of tags and branches.
1704 */
1705 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
1706 skip_prefix(refname, "refs/heads/", &refname) ||
1707 skip_prefix(refname, "refs/remotes/", &refname) ||
1708 skip_prefix(refname, "refs/", &refname));
1709
1710 for (; *patterns; patterns++) {
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +00001711 if (!wildmatch(*patterns, refname, flags))
Karthik Nayakbef0e122015-09-10 21:18:26 +05301712 return 1;
1713 }
1714 return 0;
1715}
1716
1717/*
1718 * Return 1 if the refname matches one of the patterns, otherwise 0.
Karthik Nayakc95b7582015-06-14 01:07:27 +05301719 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
Karthik Nayakbef0e122015-09-10 21:18:26 +05301720 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1721 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
Karthik Nayakc95b7582015-06-14 01:07:27 +05301722 */
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07001723static int match_name_as_path(const struct ref_filter *filter, const char *refname)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301724{
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07001725 const char **pattern = filter->name_patterns;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301726 int namelen = strlen(refname);
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07001727 unsigned flags = WM_PATHNAME;
1728
1729 if (filter->ignore_case)
1730 flags |= WM_CASEFOLD;
1731
Karthik Nayakc95b7582015-06-14 01:07:27 +05301732 for (; *pattern; pattern++) {
1733 const char *p = *pattern;
1734 int plen = strlen(p);
1735
1736 if ((plen <= namelen) &&
1737 !strncmp(refname, p, plen) &&
1738 (refname[plen] == '\0' ||
1739 refname[plen] == '/' ||
1740 p[plen-1] == '/'))
1741 return 1;
Aleksandr Makarov639ab5e2018-07-02 17:11:59 -04001742 if (!wildmatch(p, refname, flags))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301743 return 1;
1744 }
1745 return 0;
1746}
1747
Karthik Nayakbef0e122015-09-10 21:18:26 +05301748/* Return 1 if the refname matches one of the patterns, otherwise 0. */
1749static int filter_pattern_match(struct ref_filter *filter, const char *refname)
1750{
1751 if (!*filter->name_patterns)
1752 return 1; /* No pattern always matches */
1753 if (filter->match_as_path)
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07001754 return match_name_as_path(filter, refname);
1755 return match_pattern(filter, refname);
Karthik Nayakbef0e122015-09-10 21:18:26 +05301756}
1757
Karthik Nayak68411042015-07-07 21:36:09 +05301758/*
Jeff Kingcfe004a2017-05-22 16:17:54 +02001759 * Find the longest prefix of pattern we can pass to
1760 * `for_each_fullref_in()`, namely the part of pattern preceding the
1761 * first glob character. (Note that `for_each_fullref_in()` is
1762 * perfectly happy working with a prefix that doesn't end at a
1763 * pathname component boundary.)
1764 */
1765static void find_longest_prefix(struct strbuf *out, const char *pattern)
1766{
1767 const char *p;
1768
1769 for (p = pattern; *p && !is_glob_special(*p); p++)
1770 ;
1771
1772 strbuf_add(out, pattern, p - pattern);
1773}
1774
1775/*
1776 * This is the same as for_each_fullref_in(), but it tries to iterate
1777 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1778 * pattern match, so the callback still has to match each ref individually.
1779 */
1780static int for_each_fullref_in_pattern(struct ref_filter *filter,
1781 each_ref_fn cb,
1782 void *cb_data,
1783 int broken)
1784{
1785 struct strbuf prefix = STRBUF_INIT;
1786 int ret;
1787
1788 if (!filter->match_as_path) {
1789 /*
1790 * in this case, the patterns are applied after
1791 * prefixes like "refs/heads/" etc. are stripped off,
1792 * so we have to look at everything:
1793 */
1794 return for_each_fullref_in("", cb, cb_data, broken);
1795 }
1796
Jeff Kinge674eb22018-07-02 17:12:42 -04001797 if (filter->ignore_case) {
1798 /*
1799 * we can't handle case-insensitive comparisons,
1800 * so just return everything and let the caller
1801 * sort it out.
1802 */
1803 return for_each_fullref_in("", cb, cb_data, broken);
1804 }
1805
Jeff Kingcfe004a2017-05-22 16:17:54 +02001806 if (!filter->name_patterns[0]) {
1807 /* no patterns; we have to look at everything */
1808 return for_each_fullref_in("", cb, cb_data, broken);
1809 }
1810
1811 if (filter->name_patterns[1]) {
1812 /*
1813 * multiple patterns; in theory this could still work as long
1814 * as the patterns are disjoint. We'd just make multiple calls
1815 * to for_each_ref(). But if they're not disjoint, we'd end up
1816 * reporting the same ref multiple times. So let's punt on that
1817 * for now.
1818 */
1819 return for_each_fullref_in("", cb, cb_data, broken);
1820 }
1821
1822 find_longest_prefix(&prefix, filter->name_patterns[0]);
1823
1824 ret = for_each_fullref_in(prefix.buf, cb, cb_data, broken);
1825 strbuf_release(&prefix);
1826 return ret;
1827}
1828
1829/*
Karthik Nayak68411042015-07-07 21:36:09 +05301830 * Given a ref (sha1, refname), check if the ref belongs to the array
1831 * of sha1s. If the given ref is a tag, check if the given tag points
1832 * at one of the sha1s in the given sha1 array.
1833 * the given sha1_array.
1834 * NEEDSWORK:
1835 * 1. Only a single level of inderection is obtained, we might want to
1836 * change this to account for multiple levels (e.g. annotated tags
1837 * pointing to annotated tags pointing to a commit.)
1838 * 2. As the refs are cached we might know what refname peels to without
1839 * the need to parse the object via parse_object(). peel_ref() might be a
1840 * more efficient alternative to obtain the pointee.
1841 */
brian m. carlson910650d2017-03-31 01:40:00 +00001842static const struct object_id *match_points_at(struct oid_array *points_at,
brian m. carlson4ce36212017-03-31 01:39:57 +00001843 const struct object_id *oid,
1844 const char *refname)
Karthik Nayak68411042015-07-07 21:36:09 +05301845{
brian m. carlson4ce36212017-03-31 01:39:57 +00001846 const struct object_id *tagged_oid = NULL;
Karthik Nayak68411042015-07-07 21:36:09 +05301847 struct object *obj;
1848
brian m. carlson910650d2017-03-31 01:40:00 +00001849 if (oid_array_lookup(points_at, oid) >= 0)
brian m. carlson4ce36212017-03-31 01:39:57 +00001850 return oid;
Stefan Beller109cd762018-06-28 18:21:51 -07001851 obj = parse_object(the_repository, oid);
Karthik Nayak68411042015-07-07 21:36:09 +05301852 if (!obj)
1853 die(_("malformed object at '%s'"), refname);
1854 if (obj->type == OBJ_TAG)
brian m. carlson4ce36212017-03-31 01:39:57 +00001855 tagged_oid = &((struct tag *)obj)->tagged->oid;
brian m. carlson910650d2017-03-31 01:40:00 +00001856 if (tagged_oid && oid_array_lookup(points_at, tagged_oid) >= 0)
brian m. carlson4ce36212017-03-31 01:39:57 +00001857 return tagged_oid;
Karthik Nayak68411042015-07-07 21:36:09 +05301858 return NULL;
1859}
1860
Jeff King0ffaa002018-04-06 14:59:26 -04001861/*
1862 * Allocate space for a new ref_array_item and copy the name and oid to it.
1863 *
1864 * Callers can then fill in other struct members at their leisure.
1865 */
Karthik Nayakc95b7582015-06-14 01:07:27 +05301866static struct ref_array_item *new_ref_array_item(const char *refname,
Jeff King0ffaa002018-04-06 14:59:26 -04001867 const struct object_id *oid)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301868{
Jeff King96ffc062016-02-22 17:44:32 -05001869 struct ref_array_item *ref;
Jeff King0ffaa002018-04-06 14:59:26 -04001870
Jeff King96ffc062016-02-22 17:44:32 -05001871 FLEX_ALLOC_STR(ref, refname, refname);
Jeff King53df97a2018-04-06 14:58:32 -04001872 oidcpy(&ref->objectname, oid);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301873
1874 return ref;
1875}
1876
Jeff King427cbc92018-04-06 14:59:45 -04001877struct ref_array_item *ref_array_push(struct ref_array *array,
1878 const char *refname,
1879 const struct object_id *oid)
1880{
1881 struct ref_array_item *ref = new_ref_array_item(refname, oid);
1882
1883 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
1884 array->items[array->nr++] = ref;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301885
1886 return ref;
1887}
1888
Lukas Puehringer2111aa72017-01-17 18:37:19 -05001889static int ref_kind_from_refname(const char *refname)
Karthik Nayak5b4f2852015-09-10 21:18:23 +05301890{
1891 unsigned int i;
1892
1893 static struct {
1894 const char *prefix;
1895 unsigned int kind;
1896 } ref_kind[] = {
1897 { "refs/heads/" , FILTER_REFS_BRANCHES },
1898 { "refs/remotes/" , FILTER_REFS_REMOTES },
1899 { "refs/tags/", FILTER_REFS_TAGS}
1900 };
1901
Lukas Puehringer2111aa72017-01-17 18:37:19 -05001902 if (!strcmp(refname, "HEAD"))
Karthik Nayak5b4f2852015-09-10 21:18:23 +05301903 return FILTER_REFS_DETACHED_HEAD;
1904
1905 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
1906 if (starts_with(refname, ref_kind[i].prefix))
1907 return ref_kind[i].kind;
1908 }
1909
1910 return FILTER_REFS_OTHERS;
1911}
1912
Lukas Puehringer2111aa72017-01-17 18:37:19 -05001913static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1914{
1915 if (filter->kind == FILTER_REFS_BRANCHES ||
1916 filter->kind == FILTER_REFS_REMOTES ||
1917 filter->kind == FILTER_REFS_TAGS)
1918 return filter->kind;
1919 return ref_kind_from_refname(refname);
1920}
1921
Derrick Stolee920f93c2018-07-20 16:33:08 +00001922struct ref_filter_cbdata {
1923 struct ref_array *array;
1924 struct ref_filter *filter;
1925 struct contains_cache contains_cache;
1926 struct contains_cache no_contains_cache;
1927};
1928
Karthik Nayakc95b7582015-06-14 01:07:27 +05301929/*
1930 * A call-back given to for_each_ref(). Filter refs and keep them for
1931 * later object processing.
1932 */
Karthik Nayak14de7fb2015-06-14 01:07:28 +05301933static int ref_filter_handler(const char *refname, const struct object_id *oid, int flag, void *cb_data)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301934{
1935 struct ref_filter_cbdata *ref_cbdata = cb_data;
Karthik Nayak14de7fb2015-06-14 01:07:28 +05301936 struct ref_filter *filter = ref_cbdata->filter;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301937 struct ref_array_item *ref;
Karthik Nayak35257aa2015-07-07 21:36:12 +05301938 struct commit *commit = NULL;
Karthik Nayak5b4f2852015-09-10 21:18:23 +05301939 unsigned int kind;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301940
1941 if (flag & REF_BAD_NAME) {
Nguyễn Thái Ngọc Duy1823c612016-02-27 13:42:04 +07001942 warning(_("ignoring ref with broken name %s"), refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301943 return 0;
1944 }
1945
Junio C Hamano7ebc8cb2015-08-03 11:01:10 -07001946 if (flag & REF_ISBROKEN) {
Nguyễn Thái Ngọc Duy1823c612016-02-27 13:42:04 +07001947 warning(_("ignoring broken ref %s"), refname);
Junio C Hamano7ebc8cb2015-08-03 11:01:10 -07001948 return 0;
1949 }
1950
Karthik Nayak5b4f2852015-09-10 21:18:23 +05301951 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1952 kind = filter_ref_kind(filter, refname);
1953 if (!(kind & filter->kind))
1954 return 0;
1955
Karthik Nayakbef0e122015-09-10 21:18:26 +05301956 if (!filter_pattern_match(filter, refname))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301957 return 0;
1958
brian m. carlson4ce36212017-03-31 01:39:57 +00001959 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
Karthik Nayak68411042015-07-07 21:36:09 +05301960 return 0;
1961
Karthik Nayakc95b7582015-06-14 01:07:27 +05301962 /*
Karthik Nayak35257aa2015-07-07 21:36:12 +05301963 * A merge filter is applied on refs pointing to commits. Hence
1964 * obtain the commit using the 'oid' available and discard all
1965 * non-commits early. The actual filtering is done later.
1966 */
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +00001967 if (filter->merge_commit || filter->with_commit || filter->no_commit || filter->verbose) {
Stefan Beller21e1ee82018-06-28 18:21:57 -07001968 commit = lookup_commit_reference_gently(the_repository, oid,
1969 1);
Karthik Nayak35257aa2015-07-07 21:36:12 +05301970 if (!commit)
1971 return 0;
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +00001972 /* We perform the filtering for the '--contains' option... */
Karthik Nayakee2bd062015-07-07 21:36:16 +05301973 if (filter->with_commit &&
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +00001974 !commit_contains(filter, commit, filter->with_commit, &ref_cbdata->contains_cache))
1975 return 0;
1976 /* ...or for the `--no-contains' option */
1977 if (filter->no_commit &&
1978 commit_contains(filter, commit, filter->no_commit, &ref_cbdata->no_contains_cache))
Karthik Nayakee2bd062015-07-07 21:36:16 +05301979 return 0;
Karthik Nayak35257aa2015-07-07 21:36:12 +05301980 }
1981
Karthik Nayakc95b7582015-06-14 01:07:27 +05301982 /*
1983 * We do not open the object yet; sort may only need refname
1984 * to do its job and the resulting list may yet to be pruned
1985 * by maxcount logic.
1986 */
Jeff King427cbc92018-04-06 14:59:45 -04001987 ref = ref_array_push(ref_cbdata->array, refname, oid);
Karthik Nayak35257aa2015-07-07 21:36:12 +05301988 ref->commit = commit;
Jeff King0ffaa002018-04-06 14:59:26 -04001989 ref->flag = flag;
Karthik Nayak5b4f2852015-09-10 21:18:23 +05301990 ref->kind = kind;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301991
Karthik Nayakc95b7582015-06-14 01:07:27 +05301992 return 0;
1993}
1994
1995/* Free memory allocated for a ref_array_item */
1996static void free_array_item(struct ref_array_item *item)
1997{
1998 free((char *)item->symref);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001999 if (item->value) {
2000 free((char *)item->value->s);
2001 free(item->value);
2002 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05302003 free(item);
2004}
2005
2006/* Free all memory allocated for ref_array */
2007void ref_array_clear(struct ref_array *array)
2008{
2009 int i;
2010
Olga Telezhnaya23941dd2018-10-18 07:28:54 +00002011 for (i = 0; i < used_atom_cnt; i++)
2012 free((char *)used_atom[i].name);
2013 FREE_AND_NULL(used_atom);
2014 used_atom_cnt = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302015 for (i = 0; i < array->nr; i++)
2016 free_array_item(array->items[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002017 FREE_AND_NULL(array->items);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302018 array->nr = array->alloc = 0;
2019}
2020
Karthik Nayak35257aa2015-07-07 21:36:12 +05302021static void do_merge_filter(struct ref_filter_cbdata *ref_cbdata)
2022{
2023 struct rev_info revs;
2024 int i, old_nr;
2025 struct ref_filter *filter = ref_cbdata->filter;
2026 struct ref_array *array = ref_cbdata->array;
2027 struct commit **to_clear = xcalloc(sizeof(struct commit *), array->nr);
2028
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +02002029 repo_init_revisions(the_repository, &revs, NULL);
Karthik Nayak35257aa2015-07-07 21:36:12 +05302030
2031 for (i = 0; i < array->nr; i++) {
2032 struct ref_array_item *item = array->items[i];
2033 add_pending_object(&revs, &item->commit->object, item->refname);
2034 to_clear[i] = item->commit;
2035 }
2036
2037 filter->merge_commit->object.flags |= UNINTERESTING;
2038 add_pending_object(&revs, &filter->merge_commit->object, "");
2039
2040 revs.limited = 1;
2041 if (prepare_revision_walk(&revs))
2042 die(_("revision walk setup failed"));
2043
2044 old_nr = array->nr;
2045 array->nr = 0;
2046
2047 for (i = 0; i < old_nr; i++) {
2048 struct ref_array_item *item = array->items[i];
2049 struct commit *commit = item->commit;
2050
2051 int is_merged = !!(commit->object.flags & UNINTERESTING);
2052
2053 if (is_merged == (filter->merge == REF_FILTER_MERGED_INCLUDE))
2054 array->items[array->nr++] = array->items[i];
2055 else
2056 free_array_item(item);
2057 }
2058
René Scharfe5dee6d62017-12-25 18:44:12 +01002059 clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
Karthik Nayak35257aa2015-07-07 21:36:12 +05302060 clear_commit_marks(filter->merge_commit, ALL_REV_FLAGS);
2061 free(to_clear);
2062}
2063
Karthik Nayak14de7fb2015-06-14 01:07:28 +05302064/*
2065 * API for filtering a set of refs. Based on the type of refs the user
2066 * has requested, we iterate through those refs and apply filters
2067 * as per the given ref_filter structure and finally store the
2068 * filtered refs in the ref_array structure.
2069 */
2070int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
2071{
2072 struct ref_filter_cbdata ref_cbdata;
Karthik Nayak35257aa2015-07-07 21:36:12 +05302073 int ret = 0;
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302074 unsigned int broken = 0;
Karthik Nayak14de7fb2015-06-14 01:07:28 +05302075
2076 ref_cbdata.array = array;
2077 ref_cbdata.filter = filter;
2078
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302079 if (type & FILTER_REFS_INCLUDE_BROKEN)
2080 broken = 1;
2081 filter->kind = type & FILTER_REFS_KIND_MASK;
2082
Jeff Kinga91aca42017-03-09 08:29:49 -05002083 init_contains_cache(&ref_cbdata.contains_cache);
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +00002084 init_contains_cache(&ref_cbdata.no_contains_cache);
Jeff Kinga91aca42017-03-09 08:29:49 -05002085
Karthik Nayak35257aa2015-07-07 21:36:12 +05302086 /* Simple per-ref filtering */
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302087 if (!filter->kind)
Karthik Nayak14de7fb2015-06-14 01:07:28 +05302088 die("filter_refs: invalid type");
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302089 else {
2090 /*
2091 * For common cases where we need only branches or remotes or tags,
2092 * we only iterate through those refs. If a mix of refs is needed,
2093 * we iterate over all refs and filter out required refs with the help
2094 * of filter_ref_kind().
2095 */
2096 if (filter->kind == FILTER_REFS_BRANCHES)
2097 ret = for_each_fullref_in("refs/heads/", ref_filter_handler, &ref_cbdata, broken);
2098 else if (filter->kind == FILTER_REFS_REMOTES)
2099 ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata, broken);
2100 else if (filter->kind == FILTER_REFS_TAGS)
2101 ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken);
2102 else if (filter->kind & FILTER_REFS_ALL)
Jeff Kingcfe004a2017-05-22 16:17:54 +02002103 ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken);
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302104 if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
2105 head_ref(ref_filter_handler, &ref_cbdata);
2106 }
2107
Jeff Kinga91aca42017-03-09 08:29:49 -05002108 clear_contains_cache(&ref_cbdata.contains_cache);
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +00002109 clear_contains_cache(&ref_cbdata.no_contains_cache);
Karthik Nayak35257aa2015-07-07 21:36:12 +05302110
2111 /* Filters that need revision walking */
2112 if (filter->merge_commit)
2113 do_merge_filter(&ref_cbdata);
2114
2115 return ret;
Karthik Nayak14de7fb2015-06-14 01:07:28 +05302116}
2117
Karthik Nayakc95b7582015-06-14 01:07:27 +05302118static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
2119{
2120 struct atom_value *va, *vb;
2121 int cmp;
Karthik Nayakb072add2016-02-17 23:36:11 +05302122 cmp_type cmp_type = used_atom[s->atom].type;
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002123 int (*cmp_fn)(const char *, const char *);
Olga Telezhnayae3396112018-03-29 12:49:45 +00002124 struct strbuf err = STRBUF_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302125
Olga Telezhnayae3396112018-03-29 12:49:45 +00002126 if (get_ref_atom_value(a, s->atom, &va, &err))
2127 die("%s", err.buf);
2128 if (get_ref_atom_value(b, s->atom, &vb, &err))
2129 die("%s", err.buf);
2130 strbuf_release(&err);
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002131 cmp_fn = s->ignore_case ? strcasecmp : strcmp;
Karthik Nayak90c00402015-09-10 21:18:25 +05302132 if (s->version)
2133 cmp = versioncmp(va->s, vb->s);
2134 else if (cmp_type == FIELD_STR)
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002135 cmp = cmp_fn(va->s, vb->s);
Karthik Nayak90c00402015-09-10 21:18:25 +05302136 else {
Johannes Schindeline467dc12017-04-20 22:52:09 +02002137 if (va->value < vb->value)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302138 cmp = -1;
Johannes Schindeline467dc12017-04-20 22:52:09 +02002139 else if (va->value == vb->value)
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002140 cmp = cmp_fn(a->refname, b->refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302141 else
2142 cmp = 1;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302143 }
Karthik Nayak90c00402015-09-10 21:18:25 +05302144
Karthik Nayakc95b7582015-06-14 01:07:27 +05302145 return (s->reverse) ? -cmp : cmp;
2146}
2147
René Scharfe83fc4d62017-01-22 18:58:07 +01002148static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302149{
2150 struct ref_array_item *a = *((struct ref_array_item **)a_);
2151 struct ref_array_item *b = *((struct ref_array_item **)b_);
2152 struct ref_sorting *s;
2153
2154 for (s = ref_sorting; s; s = s->next) {
2155 int cmp = cmp_ref_sorting(s, a, b);
2156 if (cmp)
2157 return cmp;
2158 }
2159 return 0;
2160}
2161
2162void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
2163{
René Scharfe83fc4d62017-01-22 18:58:07 +01002164 QSORT_S(array->items, array->nr, compare_refs, sorting);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302165}
2166
Karthik Nayak574e96a2015-09-10 21:18:18 +05302167static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302168{
Karthik Nayak574e96a2015-09-10 21:18:18 +05302169 struct strbuf *s = &state->stack->output;
2170
Karthik Nayakc95b7582015-06-14 01:07:27 +05302171 while (*cp && (!ep || cp < ep)) {
2172 if (*cp == '%') {
2173 if (cp[1] == '%')
2174 cp++;
2175 else {
René Scharfed2330972016-09-03 17:59:20 +02002176 int ch = hex2chr(cp + 1);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302177 if (0 <= ch) {
Karthik Nayak574e96a2015-09-10 21:18:18 +05302178 strbuf_addch(s, ch);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302179 cp += 3;
2180 continue;
2181 }
2182 }
2183 }
Karthik Nayak574e96a2015-09-10 21:18:18 +05302184 strbuf_addch(s, *cp);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302185 cp++;
2186 }
2187}
2188
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00002189int format_ref_array_item(struct ref_array_item *info,
Jeff King4a68e362017-07-13 11:01:18 -04002190 const struct ref_format *format,
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00002191 struct strbuf *final_buf,
2192 struct strbuf *error_buf)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302193{
2194 const char *cp, *sp, *ep;
Karthik Nayak574e96a2015-09-10 21:18:18 +05302195 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
2196
Jeff King4a68e362017-07-13 11:01:18 -04002197 state.quote_style = format->quote_style;
Karthik Nayak574e96a2015-09-10 21:18:18 +05302198 push_stack_element(&state.stack);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302199
Jeff King4a68e362017-07-13 11:01:18 -04002200 for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05302201 struct atom_value *atomv;
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00002202 int pos;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302203
2204 ep = strchr(sp, ')');
2205 if (cp < sp)
Karthik Nayak574e96a2015-09-10 21:18:18 +05302206 append_literal(cp, sp, &state);
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00002207 pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
Olga Telezhnayae3396112018-03-29 12:49:45 +00002208 if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
2209 atomv->handler(atomv, &state, error_buf)) {
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00002210 pop_stack_element(&state.stack);
2211 return -1;
2212 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05302213 }
2214 if (*cp) {
2215 sp = cp + strlen(cp);
Karthik Nayak574e96a2015-09-10 21:18:18 +05302216 append_literal(cp, sp, &state);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302217 }
Jeff Kingbf285ae2017-07-13 11:02:30 -04002218 if (format->need_color_reset_at_eol) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05302219 struct atom_value resetv;
Jeff King51331aa2017-07-13 10:58:56 -04002220 resetv.s = GIT_COLOR_RESET;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00002221 if (append_atom(&resetv, &state, error_buf)) {
2222 pop_stack_element(&state.stack);
2223 return -1;
2224 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05302225 }
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00002226 if (state.stack->prev) {
2227 pop_stack_element(&state.stack);
2228 return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
2229 }
Karthik Nayak99c6a712017-01-10 14:19:39 +05302230 strbuf_addbuf(final_buf, &state.stack->output);
Karthik Nayak574e96a2015-09-10 21:18:18 +05302231 pop_stack_element(&state.stack);
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00002232 return 0;
Karthik Nayak99c6a712017-01-10 14:19:39 +05302233}
2234
Jeff King4a68e362017-07-13 11:01:18 -04002235void show_ref_array_item(struct ref_array_item *info,
2236 const struct ref_format *format)
Karthik Nayak99c6a712017-01-10 14:19:39 +05302237{
2238 struct strbuf final_buf = STRBUF_INIT;
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00002239 struct strbuf error_buf = STRBUF_INIT;
Karthik Nayak99c6a712017-01-10 14:19:39 +05302240
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00002241 if (format_ref_array_item(info, format, &final_buf, &error_buf))
2242 die("%s", error_buf.buf);
Karthik Nayak99c6a712017-01-10 14:19:39 +05302243 fwrite(final_buf.buf, 1, final_buf.len, stdout);
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00002244 strbuf_release(&error_buf);
Karthik Nayak99c6a712017-01-10 14:19:39 +05302245 strbuf_release(&final_buf);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302246 putchar('\n');
2247}
2248
Jeff King53df97a2018-04-06 14:58:32 -04002249void pretty_print_ref(const char *name, const struct object_id *oid,
Jeff King4a68e362017-07-13 11:01:18 -04002250 const struct ref_format *format)
Lukas Puehringer2111aa72017-01-17 18:37:19 -05002251{
2252 struct ref_array_item *ref_item;
Jeff King0ffaa002018-04-06 14:59:26 -04002253 ref_item = new_ref_array_item(name, oid);
Lukas Puehringer2111aa72017-01-17 18:37:19 -05002254 ref_item->kind = ref_kind_from_refname(name);
Jeff King4a68e362017-07-13 11:01:18 -04002255 show_ref_array_item(ref_item, format);
Lukas Puehringer2111aa72017-01-17 18:37:19 -05002256 free_array_item(ref_item);
2257}
2258
Jeff King29ef53c2017-07-13 11:02:58 -04002259static int parse_sorting_atom(const char *atom)
2260{
Jeff Kingab7ded32017-07-13 11:06:40 -04002261 /*
2262 * This parses an atom using a dummy ref_format, since we don't
2263 * actually care about the formatting details.
2264 */
2265 struct ref_format dummy = REF_FORMAT_INIT;
Jeff King29ef53c2017-07-13 11:02:58 -04002266 const char *end = atom + strlen(atom);
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00002267 struct strbuf err = STRBUF_INIT;
2268 int res = parse_ref_filter_atom(&dummy, atom, end, &err);
2269 if (res < 0)
2270 die("%s", err.buf);
2271 strbuf_release(&err);
2272 return res;
Jeff King29ef53c2017-07-13 11:02:58 -04002273}
2274
Karthik Nayakc95b7582015-06-14 01:07:27 +05302275/* If no sorting option is given, use refname to sort as default */
2276struct ref_sorting *ref_default_sorting(void)
2277{
2278 static const char cstr_name[] = "refname";
2279
2280 struct ref_sorting *sorting = xcalloc(1, sizeof(*sorting));
2281
2282 sorting->next = NULL;
Jeff King29ef53c2017-07-13 11:02:58 -04002283 sorting->atom = parse_sorting_atom(cstr_name);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302284 return sorting;
2285}
2286
Jeff King18a25652017-07-13 11:02:44 -04002287void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302288{
Karthik Nayakc95b7582015-06-14 01:07:27 +05302289 struct ref_sorting *s;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302290
Karthik Nayakc95b7582015-06-14 01:07:27 +05302291 s = xcalloc(1, sizeof(*s));
2292 s->next = *sorting_tail;
2293 *sorting_tail = s;
2294
2295 if (*arg == '-') {
2296 s->reverse = 1;
2297 arg++;
2298 }
Karthik Nayak90c00402015-09-10 21:18:25 +05302299 if (skip_prefix(arg, "version:", &arg) ||
2300 skip_prefix(arg, "v:", &arg))
2301 s->version = 1;
Jeff King29ef53c2017-07-13 11:02:58 -04002302 s->atom = parse_sorting_atom(arg);
Jeff King18a25652017-07-13 11:02:44 -04002303}
2304
2305int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
2306{
2307 if (!arg) /* should --no-sort void the list ? */
2308 return -1;
2309 parse_ref_sorting(opt->value, arg);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302310 return 0;
2311}
Karthik Nayak5afcb902015-07-07 21:36:11 +05302312
2313int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
2314{
2315 struct ref_filter *rf = opt->value;
brian m. carlson1e43ed92017-05-06 22:10:09 +00002316 struct object_id oid;
Ævar Arnfjörð Bjarmason17d6c742017-03-21 12:58:49 +00002317 int no_merged = starts_with(opt->long_name, "no");
Karthik Nayak5afcb902015-07-07 21:36:11 +05302318
Ævar Arnfjörð Bjarmason17d6c742017-03-21 12:58:49 +00002319 if (rf->merge) {
2320 if (no_merged) {
2321 return opterror(opt, "is incompatible with --merged", 0);
2322 } else {
2323 return opterror(opt, "is incompatible with --no-merged", 0);
2324 }
2325 }
2326
2327 rf->merge = no_merged
Karthik Nayak5afcb902015-07-07 21:36:11 +05302328 ? REF_FILTER_MERGED_OMIT
2329 : REF_FILTER_MERGED_INCLUDE;
2330
brian m. carlson1e43ed92017-05-06 22:10:09 +00002331 if (get_oid(arg, &oid))
Karthik Nayak5afcb902015-07-07 21:36:11 +05302332 die(_("malformed object name %s"), arg);
2333
Stefan Beller21e1ee82018-06-28 18:21:57 -07002334 rf->merge_commit = lookup_commit_reference_gently(the_repository,
2335 &oid, 0);
Karthik Nayak5afcb902015-07-07 21:36:11 +05302336 if (!rf->merge_commit)
2337 return opterror(opt, "must point to a commit", 0);
2338
2339 return 0;
2340}