blob: f7fb0c7e0ec1b8ab3af987245dcb048a87ca1f3a [file] [log] [blame]
Elijah Newren5e3f94d2023-04-22 20:17:23 +00001#include "git-compat-util.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00002#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00003#include "gettext.h"
Kousik Sanagavarapuf46094a2023-07-23 21:49:58 +05304#include "config.h"
Elijah Newrend4a4f922023-04-22 20:17:26 +00005#include "gpg-interface.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00006#include "hex.h"
Karthik Nayakc95b7582015-06-14 01:07:27 +05307#include "parse-options.h"
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +05308#include "run-command.h"
Karthik Nayakc95b7582015-06-14 01:07:27 +05309#include "refs.h"
10#include "wildmatch.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070011#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +000012#include "object-store-ll.h"
Elijah Newren6f2d7432023-04-11 03:00:42 +000013#include "oid-array.h"
Stefan Beller109cd762018-06-28 18:21:51 -070014#include "repository.h"
Karthik Nayakc95b7582015-06-14 01:07:27 +053015#include "commit.h"
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +053016#include "mailmap.h"
17#include "ident.h"
Karthik Nayakc95b7582015-06-14 01:07:27 +053018#include "remote.h"
19#include "color.h"
20#include "tag.h"
21#include "quote.h"
22#include "ref-filter.h"
Karthik Nayak35257aa2015-07-07 21:36:12 +053023#include "revision.h"
Karthik Nayakce592082015-09-11 20:33:07 +053024#include "utf8.h"
Elijah Newren34676632023-04-22 20:17:17 +000025#include "versioncmp.h"
Jacob Kellerb1d31c82016-11-18 16:58:15 -080026#include "trailer.h"
Karthik Nayakd4919bb2017-01-10 14:19:38 +053027#include "wt-status.h"
Jeff Kinga91aca42017-03-09 08:29:49 -050028#include "commit-slab.h"
Derrick Stolee64043552018-07-20 16:33:04 +000029#include "commit-reach.h"
Nickolai Belakovski25820832019-04-28 22:19:42 -070030#include "worktree.h"
31#include "hashmap.h"
Karthik Nayakc95b7582015-06-14 01:07:27 +053032
Karthik Nayak6eac70f2017-01-10 14:19:50 +053033static struct ref_msg {
34 const char *gone;
35 const char *ahead;
36 const char *behind;
37 const char *ahead_behind;
38} msgs = {
39 /* Untranslated plumbing messages: */
40 "gone",
41 "ahead %d",
42 "behind %d",
43 "ahead %d, behind %d"
44};
45
46void setup_ref_filter_porcelain_msg(void)
47{
48 msgs.gone = _("gone");
49 msgs.ahead = _("ahead %d");
50 msgs.behind = _("behind %d");
51 msgs.ahead_behind = _("ahead %d, behind %d");
52}
Karthik Nayakc95b7582015-06-14 01:07:27 +053053
54typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +053055typedef enum { COMPARE_EQUAL, COMPARE_UNEQUAL, COMPARE_NONE } cmp_status;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +000056typedef enum { SOURCE_NONE = 0, SOURCE_OBJ, SOURCE_OTHER } info_source;
Karthik Nayakc95b7582015-06-14 01:07:27 +053057
Karthik Nayak5bd881d2016-02-17 23:36:15 +053058struct align {
59 align_type position;
60 unsigned int width;
61};
62
Karthik Nayakc58492d2017-01-10 14:19:34 +053063struct if_then_else {
Karthik Nayak4f3e3b32017-01-10 14:19:36 +053064 cmp_status cmp_status;
65 const char *str;
Karthik Nayakc58492d2017-01-10 14:19:34 +053066 unsigned int then_atom_seen : 1,
67 else_atom_seen : 1,
68 condition_satisfied : 1;
69};
70
Karthik Nayakb180e6f2017-01-10 14:19:43 +053071struct refname_atom {
Karthik Nayak1a347282017-01-10 14:19:49 +053072 enum { R_NORMAL, R_SHORT, R_LSTRIP, R_RSTRIP } option;
73 int lstrip, rstrip;
Karthik Nayakb180e6f2017-01-10 14:19:43 +053074};
75
Hariom Vermaee82a482021-02-13 01:52:43 +000076static struct ref_trailer_buf {
77 struct string_list filter_list;
78 struct strbuf sepbuf;
79 struct strbuf kvsepbuf;
80} ref_trailer_buf = {STRING_LIST_INIT_NODUP, STRBUF_INIT, STRBUF_INIT};
81
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +000082static struct expand_data {
83 struct object_id oid;
84 enum object_type type;
85 unsigned long size;
86 off_t disk_size;
87 struct object_id delta_base_oid;
88 void *content;
89
90 struct object_info info;
91} oi, oi_deref;
92
Nickolai Belakovski25820832019-04-28 22:19:42 -070093struct ref_to_worktree_entry {
Eric Wonge2b50382019-10-06 23:30:43 +000094 struct hashmap_entry ent;
Nickolai Belakovski25820832019-04-28 22:19:42 -070095 struct worktree *wt; /* key is wt->head_ref */
96};
97
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020098static int ref_to_worktree_map_cmpfnc(const void *lookupdata UNUSED,
Eric Wong939af162019-10-06 23:30:37 +000099 const struct hashmap_entry *eptr,
100 const struct hashmap_entry *kptr,
Nickolai Belakovski25820832019-04-28 22:19:42 -0700101 const void *keydata_aka_refname)
102{
Eric Wong939af162019-10-06 23:30:37 +0000103 const struct ref_to_worktree_entry *e, *k;
104
105 e = container_of(eptr, const struct ref_to_worktree_entry, ent);
106 k = container_of(kptr, const struct ref_to_worktree_entry, ent);
107
Nickolai Belakovski25820832019-04-28 22:19:42 -0700108 return strcmp(e->wt->head_ref,
109 keydata_aka_refname ? keydata_aka_refname : k->wt->head_ref);
110}
111
112static struct ref_to_worktree_map {
113 struct hashmap map;
114 struct worktree **worktrees;
115} ref_to_worktree_map;
116
Karthik Nayak50cd83d2016-02-17 23:36:10 +0530117/*
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000118 * The enum atom_type is used as the index of valid_atom array.
119 * In the atom parsing stage, it will be passed to used_atom.atom_type
120 * as the identifier of the atom type. We can check the type of used_atom
121 * entry by `if (used_atom[i].atom_type == ATOM_*)`.
122 */
123enum atom_type {
124 ATOM_REFNAME,
125 ATOM_OBJECTTYPE,
126 ATOM_OBJECTSIZE,
127 ATOM_OBJECTNAME,
128 ATOM_DELTABASE,
129 ATOM_TREE,
130 ATOM_PARENT,
131 ATOM_NUMPARENT,
132 ATOM_OBJECT,
133 ATOM_TYPE,
134 ATOM_TAG,
135 ATOM_AUTHOR,
136 ATOM_AUTHORNAME,
137 ATOM_AUTHOREMAIL,
138 ATOM_AUTHORDATE,
139 ATOM_COMMITTER,
140 ATOM_COMMITTERNAME,
141 ATOM_COMMITTEREMAIL,
142 ATOM_COMMITTERDATE,
143 ATOM_TAGGER,
144 ATOM_TAGGERNAME,
145 ATOM_TAGGEREMAIL,
146 ATOM_TAGGERDATE,
147 ATOM_CREATOR,
148 ATOM_CREATORDATE,
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +0530149 ATOM_DESCRIBE,
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000150 ATOM_SUBJECT,
151 ATOM_BODY,
152 ATOM_TRAILERS,
153 ATOM_CONTENTS,
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +0530154 ATOM_SIGNATURE,
ZheNing Hubd0708c2021-07-26 03:26:47 +0000155 ATOM_RAW,
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000156 ATOM_UPSTREAM,
157 ATOM_PUSH,
158 ATOM_SYMREF,
159 ATOM_FLAG,
160 ATOM_HEAD,
161 ATOM_COLOR,
162 ATOM_WORKTREEPATH,
163 ATOM_ALIGN,
164 ATOM_END,
165 ATOM_IF,
166 ATOM_THEN,
167 ATOM_ELSE,
ZheNing Hub9dee072021-07-26 03:26:50 +0000168 ATOM_REST,
Derrick Stolee49abcd22023-03-20 11:26:54 +0000169 ATOM_AHEADBEHIND,
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000170};
171
172/*
Karthik Nayak50cd83d2016-02-17 23:36:10 +0530173 * An atom is a valid field atom listed below, possibly prefixed with
174 * a "*" to denote deref_tag().
175 *
176 * We parse given format string and sort specifiers, and make a list
177 * of properties that we need to extract out of objects. ref_array_item
178 * structure will hold an array of values extracted that can be
179 * indexed with the "atom number", which is an index into this
180 * array.
181 */
Karthik Nayakb072add2016-02-17 23:36:11 +0530182static struct used_atom {
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000183 enum atom_type atom_type;
Karthik Nayakb072add2016-02-17 23:36:11 +0530184 const char *name;
185 cmp_type type;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +0000186 info_source source;
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530187 union {
188 char color[COLOR_MAXLEN];
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530189 struct align align;
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530190 struct {
Johannes Schindelincc723852017-10-05 14:19:09 +0200191 enum {
J Wyman9700fae2017-11-07 17:31:08 +0100192 RR_REF, RR_TRACK, RR_TRACKSHORT, RR_REMOTE_NAME, RR_REMOTE_REF
Johannes Schindelincc723852017-10-05 14:19:09 +0200193 } option;
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530194 struct refname_atom refname;
Johannes Schindelincc723852017-10-05 14:19:09 +0200195 unsigned int nobracket : 1, push : 1, push_remote : 1;
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530196 } remote_ref;
Karthik Nayak452db392016-02-17 23:36:18 +0530197 struct {
Hariom Verma905f0a42020-08-21 21:41:50 +0000198 enum { C_BARE, C_BODY, C_BODY_DEP, C_LENGTH, C_LINES,
199 C_SIG, C_SUB, C_SUB_SANITIZE, C_TRAILERS } option;
Taylor Blau67a20a02017-10-01 22:25:23 -0700200 struct process_trailer_options trailer_opts;
Karthik Nayak452db392016-02-17 23:36:18 +0530201 unsigned int nlines;
202 } contents;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530203 struct {
ZheNing Hubd0708c2021-07-26 03:26:47 +0000204 enum { RAW_BARE, RAW_LENGTH } option;
205 } raw_data;
206 struct {
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530207 cmp_status cmp_status;
208 const char *str;
209 } if_then_else;
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530210 struct {
211 enum { O_FULL, O_LENGTH, O_SHORT } option;
212 unsigned int length;
Hariom Verma87d3beb2020-08-21 21:41:46 +0000213 } oid;
ZheNing Hu0caf20f2021-05-13 15:15:37 +0000214 struct {
215 enum { O_SIZE, O_SIZE_DISK } option;
216 } objectsize;
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530217 struct {
218 enum { N_RAW, N_MAILMAP } option;
219 } name_option;
220 struct {
221 enum {
222 EO_RAW = 0,
223 EO_TRIM = 1<<0,
224 EO_LOCALPART = 1<<1,
225 EO_MAILMAP = 1<<2,
226 } option;
Hariom Vermab82445d2020-08-21 21:41:43 +0000227 } email_option;
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +0530228 struct {
229 enum { S_BARE, S_GRADE, S_SIGNER, S_KEY,
230 S_FINGERPRINT, S_PRI_KEY_FP, S_TRUST_LEVEL } option;
231 } signature;
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +0530232 const char **describe_args;
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530233 struct refname_atom refname;
Jeff King613a0e52017-05-19 02:12:12 -0400234 char *head;
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530235 } u;
Karthik Nayakb072add2016-02-17 23:36:11 +0530236} *used_atom;
Karthik Nayak50cd83d2016-02-17 23:36:10 +0530237static int used_atom_cnt, need_tagged, need_symref;
Karthik Nayak50cd83d2016-02-17 23:36:10 +0530238
Olga Telezhnayae2e7a242018-03-29 12:49:45 +0000239/*
240 * Expand string, append it to strbuf *sb, then return error code ret.
241 * Allow to save few lines of code.
242 */
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +0200243__attribute__((format (printf, 3, 4)))
Olga Telezhnayae2e7a242018-03-29 12:49:45 +0000244static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...)
245{
246 va_list ap;
247 va_start(ap, fmt);
248 strbuf_vaddf(sb, fmt, ap);
249 va_end(ap);
250 return ret;
251}
252
Jeff Kinga33d0fa2022-12-14 11:19:43 -0500253static int err_no_arg(struct strbuf *sb, const char *name)
254{
Jeff King1955ef12022-12-14 11:23:53 -0500255 size_t namelen = strchrnul(name, ':') - name;
256 strbuf_addf(sb, _("%%(%.*s) does not take arguments"),
257 (int)namelen, name);
Jeff Kinga33d0fa2022-12-14 11:19:43 -0500258 return -1;
259}
260
Jeff Kingdda4fc12022-12-14 11:20:19 -0500261static int err_bad_arg(struct strbuf *sb, const char *name, const char *arg)
262{
Jeff King1955ef12022-12-14 11:23:53 -0500263 size_t namelen = strchrnul(name, ':') - name;
264 strbuf_addf(sb, _("unrecognized %%(%.*s) argument: %s"),
265 (int)namelen, name, arg);
Jeff Kingdda4fc12022-12-14 11:20:19 -0500266 return -1;
267}
268
Kousik Sanagavarapuf46094a2023-07-23 21:49:58 +0530269/*
270 * Parse option of name "candidate" in the option string "to_parse" of
271 * the form
272 *
273 * "candidate1[=val1],candidate2[=val2],candidate3[=val3],..."
274 *
275 * The remaining part of "to_parse" is stored in "end" (if we are
276 * parsing the last candidate, then this is NULL) and the value of
277 * the candidate is stored in "valuestart" and its length in "valuelen",
278 * that is the portion after "=". Since it is possible for a "candidate"
279 * to not have a value, in such cases, "valuestart" is set to point to
280 * NULL and "valuelen" to 0.
281 *
282 * The function returns 1 on success. It returns 0 if we don't find
283 * "candidate" in "to_parse" or we find "candidate" but it is followed
284 * by more chars (for example, "candidatefoo"), that is, we don't find
285 * an exact match.
286 *
287 * This function only does the above for one "candidate" at a time. So
288 * it has to be called each time trying to parse a "candidate" in the
289 * option string "to_parse".
290 */
291static int match_atom_arg_value(const char *to_parse, const char *candidate,
292 const char **end, const char **valuestart,
293 size_t *valuelen)
294{
295 const char *atom;
296
297 if (!skip_prefix(to_parse, candidate, &atom))
298 return 0; /* definitely not "candidate" */
299
300 if (*atom == '=') {
301 /* we just saw "candidate=" */
302 *valuestart = atom + 1;
303 atom = strchrnul(*valuestart, ',');
304 *valuelen = atom - *valuestart;
305 } else if (*atom != ',' && *atom != '\0') {
306 /* key begins with "candidate" but has more chars */
307 return 0;
308 } else {
309 /* just "candidate" without "=val" */
310 *valuestart = NULL;
311 *valuelen = 0;
312 }
313
314 /* atom points at either the ',' or NUL after this key[=val] */
315 if (*atom == ',')
316 atom++;
317 else if (*atom)
318 BUG("Why is *atom not NULL yet?");
319
320 *end = atom;
321 return 1;
322}
323
324/*
325 * Parse boolean option of name "candidate" in the option list "to_parse"
326 * of the form
327 *
328 * "candidate1[=bool1],candidate2[=bool2],candidate3[=bool3],..."
329 *
330 * The remaining part of "to_parse" is stored in "end" (if we are parsing
331 * the last candidate, then this is NULL) and the value (if given) is
332 * parsed and stored in "val", so "val" always points to either 0 or 1.
333 * If the value is not given, then "val" is set to point to 1.
334 *
335 * The boolean value is parsed using "git_parse_maybe_bool()", so the
336 * accepted values are
337 *
338 * to set true - "1", "yes", "true"
339 * to set false - "0", "no", "false"
340 *
341 * This function returns 1 on success. It returns 0 when we don't find
342 * an exact match for "candidate" or when the boolean value given is
343 * not valid.
344 */
345static int match_atom_bool_arg(const char *to_parse, const char *candidate,
346 const char **end, int *val)
347{
348 const char *argval;
349 char *strval;
350 size_t arglen;
351 int v;
352
353 if (!match_atom_arg_value(to_parse, candidate, end, &argval, &arglen))
354 return 0;
355
356 if (!argval) {
357 *val = 1;
358 return 1;
359 }
360
361 strval = xstrndup(argval, arglen);
362 v = git_parse_maybe_bool(strval);
363 free(strval);
364
365 if (v == -1)
366 return 0;
367
368 *val = v;
369
370 return 1;
371}
372
ZheNing Hue85fcb32021-07-26 03:26:49 +0000373static int color_atom_parser(struct ref_format *format, struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000374 const char *color_value, struct strbuf *err)
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530375{
376 if (!color_value)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000377 return strbuf_addf_ret(err, -1, _("expected format: %%(color:<color>)"));
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530378 if (color_parse(color_value, atom->u.color) < 0)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000379 return strbuf_addf_ret(err, -1, _("unrecognized color: %%(color:%s)"),
380 color_value);
Jeff King11b087a2017-07-13 11:09:32 -0400381 /*
382 * We check this after we've parsed the color, which lets us complain
383 * about syntactically bogus color names even if they won't be used.
384 */
385 if (!want_color(format->use_color))
386 color_parse("", atom->u.color);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000387 return 0;
Karthik Nayakfd935cc2016-02-17 23:36:13 +0530388}
389
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000390static int refname_atom_parser_internal(struct refname_atom *atom, const char *arg,
391 const char *name, struct strbuf *err)
Karthik Nayak5339bda2016-02-17 23:36:17 +0530392{
393 if (!arg)
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530394 atom->option = R_NORMAL;
Karthik Nayak5339bda2016-02-17 23:36:17 +0530395 else if (!strcmp(arg, "short"))
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530396 atom->option = R_SHORT;
Junio C Hamano44a6b6c2017-02-07 11:50:34 -0800397 else if (skip_prefix(arg, "lstrip=", &arg) ||
398 skip_prefix(arg, "strip=", &arg)) {
Karthik Nayak17938f12017-01-10 14:19:46 +0530399 atom->option = R_LSTRIP;
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +0530400 if (strtol_i(arg, 10, &atom->lstrip))
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000401 return strbuf_addf_ret(err, -1, _("Integer value expected refname:lstrip=%s"), arg);
Karthik Nayak1a347282017-01-10 14:19:49 +0530402 } else if (skip_prefix(arg, "rstrip=", &arg)) {
403 atom->option = R_RSTRIP;
404 if (strtol_i(arg, 10, &atom->rstrip))
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000405 return strbuf_addf_ret(err, -1, _("Integer value expected refname:rstrip=%s"), arg);
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530406 } else
Jeff King1955ef12022-12-14 11:23:53 -0500407 return err_bad_arg(err, name, arg);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000408 return 0;
Karthik Nayakb180e6f2017-01-10 14:19:43 +0530409}
410
Jeff King5fe9e1c2023-02-24 01:39:06 -0500411static int remote_ref_atom_parser(struct ref_format *format UNUSED,
412 struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000413 const char *arg, struct strbuf *err)
Karthik Nayak5339bda2016-02-17 23:36:17 +0530414{
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530415 struct string_list params = STRING_LIST_INIT_DUP;
416 int i;
417
Johannes Schindelincc723852017-10-05 14:19:09 +0200418 if (!strcmp(atom->name, "push") || starts_with(atom->name, "push:"))
419 atom->u.remote_ref.push = 1;
420
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530421 if (!arg) {
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530422 atom->u.remote_ref.option = RR_REF;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000423 return refname_atom_parser_internal(&atom->u.remote_ref.refname,
424 arg, atom->name, err);
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530425 }
426
427 atom->u.remote_ref.nobracket = 0;
428 string_list_split(&params, arg, ',', -1);
429
430 for (i = 0; i < params.nr; i++) {
431 const char *s = params.items[i].string;
432
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530433 if (!strcmp(s, "track"))
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530434 atom->u.remote_ref.option = RR_TRACK;
435 else if (!strcmp(s, "trackshort"))
436 atom->u.remote_ref.option = RR_TRACKSHORT;
437 else if (!strcmp(s, "nobracket"))
438 atom->u.remote_ref.nobracket = 1;
Johannes Schindelincc723852017-10-05 14:19:09 +0200439 else if (!strcmp(s, "remotename")) {
440 atom->u.remote_ref.option = RR_REMOTE_NAME;
441 atom->u.remote_ref.push_remote = 1;
J Wyman9700fae2017-11-07 17:31:08 +0100442 } else if (!strcmp(s, "remoteref")) {
443 atom->u.remote_ref.option = RR_REMOTE_REF;
444 atom->u.remote_ref.push_remote = 1;
Johannes Schindelincc723852017-10-05 14:19:09 +0200445 } else {
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530446 atom->u.remote_ref.option = RR_REF;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000447 if (refname_atom_parser_internal(&atom->u.remote_ref.refname,
448 arg, atom->name, err)) {
449 string_list_clear(&params, 0);
450 return -1;
451 }
Karthik Nayak3ba308c2017-01-10 14:19:45 +0530452 }
Karthik Nayak7743fcc2017-01-10 14:19:41 +0530453 }
454
455 string_list_clear(&params, 0);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000456 return 0;
Karthik Nayak5339bda2016-02-17 23:36:17 +0530457}
458
Jeff King5fe9e1c2023-02-24 01:39:06 -0500459static int objecttype_atom_parser(struct ref_format *format UNUSED,
460 struct used_atom *atom,
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000461 const char *arg, struct strbuf *err)
462{
463 if (arg)
Jeff Kinga33d0fa2022-12-14 11:19:43 -0500464 return err_no_arg(err, "objecttype");
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000465 if (*atom->name == '*')
466 oi_deref.info.typep = &oi_deref.type;
467 else
468 oi.info.typep = &oi.type;
469 return 0;
470}
471
Jeff King5fe9e1c2023-02-24 01:39:06 -0500472static int objectsize_atom_parser(struct ref_format *format UNUSED,
473 struct used_atom *atom,
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000474 const char *arg, struct strbuf *err)
475{
Olga Telezhnaya1867ce62018-12-24 13:24:30 +0000476 if (!arg) {
ZheNing Hu0caf20f2021-05-13 15:15:37 +0000477 atom->u.objectsize.option = O_SIZE;
Olga Telezhnaya1867ce62018-12-24 13:24:30 +0000478 if (*atom->name == '*')
479 oi_deref.info.sizep = &oi_deref.size;
480 else
481 oi.info.sizep = &oi.size;
482 } else if (!strcmp(arg, "disk")) {
ZheNing Hu0caf20f2021-05-13 15:15:37 +0000483 atom->u.objectsize.option = O_SIZE_DISK;
Olga Telezhnaya1867ce62018-12-24 13:24:30 +0000484 if (*atom->name == '*')
485 oi_deref.info.disk_sizep = &oi_deref.disk_size;
486 else
487 oi.info.disk_sizep = &oi.disk_size;
488 } else
Jeff Kingdda4fc12022-12-14 11:20:19 -0500489 return err_bad_arg(err, "objectsize", arg);
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +0000490 return 0;
491}
492
Jeff King5fe9e1c2023-02-24 01:39:06 -0500493static int deltabase_atom_parser(struct ref_format *format UNUSED,
494 struct used_atom *atom,
Olga Telezhnaya33311fa2018-12-24 13:24:30 +0000495 const char *arg, struct strbuf *err)
496{
Karthik Nayakc95b7582015-06-14 01:07:27 +0530497 if (arg)
Jeff Kinga33d0fa2022-12-14 11:19:43 -0500498 return err_no_arg(err, "deltabase");
Karthik Nayakc95b7582015-06-14 01:07:27 +0530499 if (*atom->name == '*')
Jeff Kingb99b6bc2020-02-23 23:36:56 -0500500 oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530501 else
Jeff Kingb99b6bc2020-02-23 23:36:56 -0500502 oi.info.delta_base_oid = &oi.delta_base_oid;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530503 return 0;
504}
505
Jeff King5fe9e1c2023-02-24 01:39:06 -0500506static int body_atom_parser(struct ref_format *format UNUSED,
507 struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000508 const char *arg, struct strbuf *err)
Karthik Nayak452db392016-02-17 23:36:18 +0530509{
510 if (arg)
Jeff Kinga33d0fa2022-12-14 11:19:43 -0500511 return err_no_arg(err, "body");
Karthik Nayak452db392016-02-17 23:36:18 +0530512 atom->u.contents.option = C_BODY_DEP;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000513 return 0;
Karthik Nayak452db392016-02-17 23:36:18 +0530514}
515
Jeff King5fe9e1c2023-02-24 01:39:06 -0500516static int subject_atom_parser(struct ref_format *format UNUSED,
517 struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000518 const char *arg, struct strbuf *err)
Karthik Nayak452db392016-02-17 23:36:18 +0530519{
Hariom Verma905f0a42020-08-21 21:41:50 +0000520 if (!arg)
521 atom->u.contents.option = C_SUB;
522 else if (!strcmp(arg, "sanitize"))
523 atom->u.contents.option = C_SUB_SANITIZE;
524 else
Jeff Kingdda4fc12022-12-14 11:20:19 -0500525 return err_bad_arg(err, "subject", arg);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000526 return 0;
Karthik Nayak452db392016-02-17 23:36:18 +0530527}
528
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +0530529static int parse_signature_option(const char *arg)
530{
531 if (!arg)
532 return S_BARE;
533 else if (!strcmp(arg, "signer"))
534 return S_SIGNER;
535 else if (!strcmp(arg, "grade"))
536 return S_GRADE;
537 else if (!strcmp(arg, "key"))
538 return S_KEY;
539 else if (!strcmp(arg, "fingerprint"))
540 return S_FINGERPRINT;
541 else if (!strcmp(arg, "primarykeyfingerprint"))
542 return S_PRI_KEY_FP;
543 else if (!strcmp(arg, "trustlevel"))
544 return S_TRUST_LEVEL;
545 return -1;
546}
547
548static int signature_atom_parser(struct ref_format *format UNUSED,
549 struct used_atom *atom,
550 const char *arg, struct strbuf *err)
551{
552 int opt = parse_signature_option(arg);
553 if (opt < 0)
554 return err_bad_arg(err, "signature", arg);
555 atom->u.signature.option = opt;
556 return 0;
557}
558
Jeff King29c9f2c2023-08-29 19:45:06 -0400559static int trailers_atom_parser(struct ref_format *format UNUSED,
560 struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000561 const char *arg, struct strbuf *err)
Jacob Kellerb1d31c82016-11-18 16:58:15 -0800562{
Jeff Kinge5fba5d2018-08-22 20:50:17 -0400563 atom->u.contents.trailer_opts.no_divider = 1;
564
Taylor Blau67a20a02017-10-01 22:25:23 -0700565 if (arg) {
Hariom Vermaee82a482021-02-13 01:52:43 +0000566 const char *argbuf = xstrfmt("%s)", arg);
567 char *invalid_arg = NULL;
568
569 if (format_set_trailers_options(&atom->u.contents.trailer_opts,
570 &ref_trailer_buf.filter_list,
571 &ref_trailer_buf.sepbuf,
572 &ref_trailer_buf.kvsepbuf,
573 &argbuf, &invalid_arg)) {
574 if (!invalid_arg)
575 strbuf_addf(err, _("expected %%(trailers:key=<value>)"));
576 else
577 strbuf_addf(err, _("unknown %%(trailers) argument: %s"), invalid_arg);
578 free((char *)invalid_arg);
579 return -1;
Taylor Blau67a20a02017-10-01 22:25:23 -0700580 }
581 }
Jacob Kellerb1d31c82016-11-18 16:58:15 -0800582 atom->u.contents.option = C_TRAILERS;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000583 return 0;
Jacob Kellerb1d31c82016-11-18 16:58:15 -0800584}
585
ZheNing Hue85fcb32021-07-26 03:26:49 +0000586static int contents_atom_parser(struct ref_format *format, struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000587 const char *arg, struct strbuf *err)
Karthik Nayak452db392016-02-17 23:36:18 +0530588{
589 if (!arg)
590 atom->u.contents.option = C_BARE;
591 else if (!strcmp(arg, "body"))
592 atom->u.contents.option = C_BODY;
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +0530593 else if (!strcmp(arg, "size")) {
594 atom->type = FIELD_ULONG;
Christian Couderb6839fd2020-07-16 14:19:40 +0200595 atom->u.contents.option = C_LENGTH;
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +0530596 } else if (!strcmp(arg, "signature"))
Karthik Nayak452db392016-02-17 23:36:18 +0530597 atom->u.contents.option = C_SIG;
598 else if (!strcmp(arg, "subject"))
599 atom->u.contents.option = C_SUB;
Hariom Verma2c22e102020-08-21 21:06:14 +0000600 else if (!strcmp(arg, "trailers")) {
601 if (trailers_atom_parser(format, atom, NULL, err))
602 return -1;
603 } else if (skip_prefix(arg, "trailers:", &arg)) {
604 if (trailers_atom_parser(format, atom, arg, err))
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000605 return -1;
Taylor Blau7a5edbd2017-10-01 22:25:24 -0700606 } else if (skip_prefix(arg, "lines=", &arg)) {
Karthik Nayak452db392016-02-17 23:36:18 +0530607 atom->u.contents.option = C_LINES;
608 if (strtoul_ui(arg, 10, &atom->u.contents.nlines))
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000609 return strbuf_addf_ret(err, -1, _("positive value expected contents:lines=%s"), arg);
Karthik Nayak452db392016-02-17 23:36:18 +0530610 } else
Jeff Kingdda4fc12022-12-14 11:20:19 -0500611 return err_bad_arg(err, "contents", arg);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000612 return 0;
Karthik Nayak452db392016-02-17 23:36:18 +0530613}
614
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +0530615static int describe_atom_option_parser(struct strvec *args, const char **arg,
616 struct strbuf *err)
617{
618 const char *argval;
619 size_t arglen = 0;
620 int optval = 0;
621
622 if (match_atom_bool_arg(*arg, "tags", arg, &optval)) {
623 if (!optval)
624 strvec_push(args, "--no-tags");
625 else
626 strvec_push(args, "--tags");
627 return 1;
628 }
629
630 if (match_atom_arg_value(*arg, "abbrev", arg, &argval, &arglen)) {
631 char *endptr;
632
633 if (!arglen)
634 return strbuf_addf_ret(err, -1,
635 _("argument expected for %s"),
636 "describe:abbrev");
637 if (strtol(argval, &endptr, 10) < 0)
638 return strbuf_addf_ret(err, -1,
639 _("positive value expected %s=%s"),
640 "describe:abbrev", argval);
641 if (endptr - argval != arglen)
642 return strbuf_addf_ret(err, -1,
643 _("cannot fully parse %s=%s"),
644 "describe:abbrev", argval);
645
646 strvec_pushf(args, "--abbrev=%.*s", (int)arglen, argval);
647 return 1;
648 }
649
650 if (match_atom_arg_value(*arg, "match", arg, &argval, &arglen)) {
651 if (!arglen)
652 return strbuf_addf_ret(err, -1,
653 _("value expected %s="),
654 "describe:match");
655
656 strvec_pushf(args, "--match=%.*s", (int)arglen, argval);
657 return 1;
658 }
659
660 if (match_atom_arg_value(*arg, "exclude", arg, &argval, &arglen)) {
661 if (!arglen)
662 return strbuf_addf_ret(err, -1,
663 _("value expected %s="),
664 "describe:exclude");
665
666 strvec_pushf(args, "--exclude=%.*s", (int)arglen, argval);
667 return 1;
668 }
669
670 return 0;
671}
672
673static int describe_atom_parser(struct ref_format *format UNUSED,
674 struct used_atom *atom,
675 const char *arg, struct strbuf *err)
676{
677 struct strvec args = STRVEC_INIT;
678
679 for (;;) {
680 int found = 0;
681 const char *bad_arg = arg;
682
683 if (!arg || !*arg)
684 break;
685
686 found = describe_atom_option_parser(&args, &arg, err);
687 if (found < 0)
688 return found;
689 if (!found)
690 return err_bad_arg(err, "describe", bad_arg);
691 }
692 atom->u.describe_args = strvec_detach(&args);
693 return 0;
694}
695
Jeff King5fe9e1c2023-02-24 01:39:06 -0500696static int raw_atom_parser(struct ref_format *format UNUSED,
697 struct used_atom *atom,
698 const char *arg, struct strbuf *err)
ZheNing Hubd0708c2021-07-26 03:26:47 +0000699{
700 if (!arg)
701 atom->u.raw_data.option = RAW_BARE;
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +0530702 else if (!strcmp(arg, "size")) {
703 atom->type = FIELD_ULONG;
ZheNing Hubd0708c2021-07-26 03:26:47 +0000704 atom->u.raw_data.option = RAW_LENGTH;
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +0530705 } else
Jeff Kingdda4fc12022-12-14 11:20:19 -0500706 return err_bad_arg(err, "raw", arg);
ZheNing Hubd0708c2021-07-26 03:26:47 +0000707 return 0;
708}
709
Jeff King5fe9e1c2023-02-24 01:39:06 -0500710static int oid_atom_parser(struct ref_format *format UNUSED,
711 struct used_atom *atom,
Hariom Verma87d3beb2020-08-21 21:41:46 +0000712 const char *arg, struct strbuf *err)
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530713{
714 if (!arg)
Hariom Verma87d3beb2020-08-21 21:41:46 +0000715 atom->u.oid.option = O_FULL;
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530716 else if (!strcmp(arg, "short"))
Hariom Verma87d3beb2020-08-21 21:41:46 +0000717 atom->u.oid.option = O_SHORT;
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530718 else if (skip_prefix(arg, "short=", &arg)) {
Hariom Verma87d3beb2020-08-21 21:41:46 +0000719 atom->u.oid.option = O_LENGTH;
720 if (strtoul_ui(arg, 10, &atom->u.oid.length) ||
721 atom->u.oid.length == 0)
Hariom Vermae7601eb2020-08-21 21:41:45 +0000722 return strbuf_addf_ret(err, -1, _("positive value expected '%s' in %%(%s)"), arg, atom->name);
Hariom Verma87d3beb2020-08-21 21:41:46 +0000723 if (atom->u.oid.length < MINIMUM_ABBREV)
724 atom->u.oid.length = MINIMUM_ABBREV;
Karthik Nayak42d0eb02017-01-10 14:19:37 +0530725 } else
Jeff King1955ef12022-12-14 11:23:53 -0500726 return err_bad_arg(err, atom->name, arg);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000727 return 0;
Karthik Nayakfe63c4d2016-02-17 23:36:19 +0530728}
729
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530730static int person_name_atom_parser(struct ref_format *format UNUSED,
731 struct used_atom *atom,
732 const char *arg, struct strbuf *err)
733{
734 if (!arg)
735 atom->u.name_option.option = N_RAW;
736 else if (!strcmp(arg, "mailmap"))
737 atom->u.name_option.option = N_MAILMAP;
738 else
739 return err_bad_arg(err, atom->name, arg);
740 return 0;
741}
742
743static int email_atom_option_parser(struct used_atom *atom,
744 const char **arg, struct strbuf *err)
745{
746 if (!*arg)
747 return EO_RAW;
748 if (skip_prefix(*arg, "trim", arg))
749 return EO_TRIM;
750 if (skip_prefix(*arg, "localpart", arg))
751 return EO_LOCALPART;
752 if (skip_prefix(*arg, "mailmap", arg))
753 return EO_MAILMAP;
754 return -1;
755}
756
Jeff King5fe9e1c2023-02-24 01:39:06 -0500757static int person_email_atom_parser(struct ref_format *format UNUSED,
758 struct used_atom *atom,
Hariom Vermab82445d2020-08-21 21:41:43 +0000759 const char *arg, struct strbuf *err)
760{
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530761 for (;;) {
762 int opt = email_atom_option_parser(atom, &arg, err);
763 const char *bad_arg = arg;
764
765 if (opt < 0)
766 return err_bad_arg(err, atom->name, bad_arg);
767 atom->u.email_option.option |= opt;
768
769 if (!arg || !*arg)
770 break;
771 if (*arg == ',')
772 arg++;
773 else
774 return err_bad_arg(err, atom->name, bad_arg);
775 }
Karthik Nayakc95b7582015-06-14 01:07:27 +0530776 return 0;
777}
778
Jeff King5fe9e1c2023-02-24 01:39:06 -0500779static int refname_atom_parser(struct ref_format *format UNUSED,
780 struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000781 const char *arg, struct strbuf *err)
Karthik Nayaka7984102017-01-10 14:19:44 +0530782{
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000783 return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err);
Karthik Nayaka7984102017-01-10 14:19:44 +0530784}
785
Karthik Nayak25a8d792016-02-17 23:36:14 +0530786static align_type parse_align_position(const char *s)
787{
788 if (!strcmp(s, "right"))
789 return ALIGN_RIGHT;
790 else if (!strcmp(s, "middle"))
791 return ALIGN_MIDDLE;
792 else if (!strcmp(s, "left"))
793 return ALIGN_LEFT;
794 return -1;
795}
796
Jeff King5fe9e1c2023-02-24 01:39:06 -0500797static int align_atom_parser(struct ref_format *format UNUSED,
798 struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000799 const char *arg, struct strbuf *err)
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530800{
801 struct align *align = &atom->u.align;
802 struct string_list params = STRING_LIST_INIT_DUP;
803 int i;
804 unsigned int width = ~0U;
805
806 if (!arg)
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000807 return strbuf_addf_ret(err, -1, _("expected format: %%(align:<width>,<position>)"));
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530808
809 align->position = ALIGN_LEFT;
810
811 string_list_split(&params, arg, ',', -1);
812 for (i = 0; i < params.nr; i++) {
813 const char *s = params.items[i].string;
814 int position;
815
Karthik Nayak395fb8f2016-02-17 23:36:16 +0530816 if (skip_prefix(s, "position=", &s)) {
817 position = parse_align_position(s);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000818 if (position < 0) {
819 strbuf_addf(err, _("unrecognized position:%s"), s);
820 string_list_clear(&params, 0);
821 return -1;
822 }
Karthik Nayak395fb8f2016-02-17 23:36:16 +0530823 align->position = position;
824 } else if (skip_prefix(s, "width=", &s)) {
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000825 if (strtoul_ui(s, 10, &width)) {
826 strbuf_addf(err, _("unrecognized width:%s"), s);
827 string_list_clear(&params, 0);
828 return -1;
829 }
Karthik Nayak395fb8f2016-02-17 23:36:16 +0530830 } else if (!strtoul_ui(s, 10, &width))
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530831 ;
832 else if ((position = parse_align_position(s)) >= 0)
833 align->position = position;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000834 else {
Jean-Noël Avila68e2ea02022-01-05 20:02:21 +0000835 strbuf_addf(err, _("unrecognized %%(%s) argument: %s"), "align", s);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000836 string_list_clear(&params, 0);
837 return -1;
838 }
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530839 }
840
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000841 if (width == ~0U) {
842 string_list_clear(&params, 0);
843 return strbuf_addf_ret(err, -1, _("positive width expected with the %%(align) atom"));
844 }
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530845 align->width = width;
846 string_list_clear(&params, 0);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000847 return 0;
Karthik Nayak5bd881d2016-02-17 23:36:15 +0530848}
849
Jeff King5fe9e1c2023-02-24 01:39:06 -0500850static int if_atom_parser(struct ref_format *format UNUSED,
851 struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000852 const char *arg, struct strbuf *err)
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530853{
854 if (!arg) {
855 atom->u.if_then_else.cmp_status = COMPARE_NONE;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000856 return 0;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530857 } else if (skip_prefix(arg, "equals=", &atom->u.if_then_else.str)) {
858 atom->u.if_then_else.cmp_status = COMPARE_EQUAL;
859 } else if (skip_prefix(arg, "notequals=", &atom->u.if_then_else.str)) {
860 atom->u.if_then_else.cmp_status = COMPARE_UNEQUAL;
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000861 } else
Jeff Kingdda4fc12022-12-14 11:20:19 -0500862 return err_bad_arg(err, "if", arg);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000863 return 0;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530864}
865
Jeff King29c9f2c2023-08-29 19:45:06 -0400866static int rest_atom_parser(struct ref_format *format UNUSED,
Jeff King5fe9e1c2023-02-24 01:39:06 -0500867 struct used_atom *atom UNUSED,
ZheNing Hub9dee072021-07-26 03:26:50 +0000868 const char *arg, struct strbuf *err)
869{
870 if (arg)
Jeff Kinga33d0fa2022-12-14 11:19:43 -0500871 return err_no_arg(err, "rest");
ZheNing Hub9dee072021-07-26 03:26:50 +0000872 return 0;
873}
874
Jeff King29c9f2c2023-08-29 19:45:06 -0400875static int ahead_behind_atom_parser(struct ref_format *format,
876 struct used_atom *atom UNUSED,
Derrick Stolee49abcd22023-03-20 11:26:54 +0000877 const char *arg, struct strbuf *err)
878{
879 struct string_list_item *item;
880
881 if (!arg)
882 return strbuf_addf_ret(err, -1, _("expected format: %%(ahead-behind:<committish>)"));
883
884 item = string_list_append(&format->bases, arg);
885 item->util = lookup_commit_reference_by_name(arg);
886 if (!item->util)
887 die("failed to find '%s'", arg);
888
889 return 0;
890}
891
Jeff King5fe9e1c2023-02-24 01:39:06 -0500892static int head_atom_parser(struct ref_format *format UNUSED,
893 struct used_atom *atom,
Jeff Kingafc1a942022-12-14 11:18:49 -0500894 const char *arg, struct strbuf *err)
Jeff King613a0e52017-05-19 02:12:12 -0400895{
Jeff Kingafc1a942022-12-14 11:18:49 -0500896 if (arg)
Jeff Kinga33d0fa2022-12-14 11:19:43 -0500897 return err_no_arg(err, "HEAD");
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +0200898 atom->u.head = refs_resolve_refdup(get_main_ref_store(the_repository),
899 "HEAD", RESOLVE_REF_READING, NULL,
900 NULL);
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000901 return 0;
Jeff King613a0e52017-05-19 02:12:12 -0400902}
Karthik Nayak4f3e3b32017-01-10 14:19:36 +0530903
Karthik Nayakc95b7582015-06-14 01:07:27 +0530904static struct {
905 const char *name;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +0000906 info_source source;
Karthik Nayakc95b7582015-06-14 01:07:27 +0530907 cmp_type cmp_type;
ZheNing Hue85fcb32021-07-26 03:26:49 +0000908 int (*parser)(struct ref_format *format, struct used_atom *atom,
Olga Telezhnaya74efea92018-03-29 12:49:45 +0000909 const char *arg, struct strbuf *err);
Karthik Nayakc95b7582015-06-14 01:07:27 +0530910} valid_atom[] = {
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000911 [ATOM_REFNAME] = { "refname", SOURCE_NONE, FIELD_STR, refname_atom_parser },
912 [ATOM_OBJECTTYPE] = { "objecttype", SOURCE_OTHER, FIELD_STR, objecttype_atom_parser },
913 [ATOM_OBJECTSIZE] = { "objectsize", SOURCE_OTHER, FIELD_ULONG, objectsize_atom_parser },
914 [ATOM_OBJECTNAME] = { "objectname", SOURCE_OTHER, FIELD_STR, oid_atom_parser },
915 [ATOM_DELTABASE] = { "deltabase", SOURCE_OTHER, FIELD_STR, deltabase_atom_parser },
916 [ATOM_TREE] = { "tree", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
917 [ATOM_PARENT] = { "parent", SOURCE_OBJ, FIELD_STR, oid_atom_parser },
918 [ATOM_NUMPARENT] = { "numparent", SOURCE_OBJ, FIELD_ULONG },
919 [ATOM_OBJECT] = { "object", SOURCE_OBJ },
920 [ATOM_TYPE] = { "type", SOURCE_OBJ },
921 [ATOM_TAG] = { "tag", SOURCE_OBJ },
922 [ATOM_AUTHOR] = { "author", SOURCE_OBJ },
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530923 [ATOM_AUTHORNAME] = { "authorname", SOURCE_OBJ, FIELD_STR, person_name_atom_parser },
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000924 [ATOM_AUTHOREMAIL] = { "authoremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
925 [ATOM_AUTHORDATE] = { "authordate", SOURCE_OBJ, FIELD_TIME },
926 [ATOM_COMMITTER] = { "committer", SOURCE_OBJ },
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530927 [ATOM_COMMITTERNAME] = { "committername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser },
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000928 [ATOM_COMMITTEREMAIL] = { "committeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
929 [ATOM_COMMITTERDATE] = { "committerdate", SOURCE_OBJ, FIELD_TIME },
930 [ATOM_TAGGER] = { "tagger", SOURCE_OBJ },
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +0530931 [ATOM_TAGGERNAME] = { "taggername", SOURCE_OBJ, FIELD_STR, person_name_atom_parser },
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000932 [ATOM_TAGGEREMAIL] = { "taggeremail", SOURCE_OBJ, FIELD_STR, person_email_atom_parser },
933 [ATOM_TAGGERDATE] = { "taggerdate", SOURCE_OBJ, FIELD_TIME },
934 [ATOM_CREATOR] = { "creator", SOURCE_OBJ },
935 [ATOM_CREATORDATE] = { "creatordate", SOURCE_OBJ, FIELD_TIME },
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +0530936 [ATOM_DESCRIBE] = { "describe", SOURCE_OBJ, FIELD_STR, describe_atom_parser },
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000937 [ATOM_SUBJECT] = { "subject", SOURCE_OBJ, FIELD_STR, subject_atom_parser },
938 [ATOM_BODY] = { "body", SOURCE_OBJ, FIELD_STR, body_atom_parser },
939 [ATOM_TRAILERS] = { "trailers", SOURCE_OBJ, FIELD_STR, trailers_atom_parser },
940 [ATOM_CONTENTS] = { "contents", SOURCE_OBJ, FIELD_STR, contents_atom_parser },
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +0530941 [ATOM_SIGNATURE] = { "signature", SOURCE_OBJ, FIELD_STR, signature_atom_parser },
ZheNing Hubd0708c2021-07-26 03:26:47 +0000942 [ATOM_RAW] = { "raw", SOURCE_OBJ, FIELD_STR, raw_atom_parser },
ZheNing Hu1197f1a2021-05-13 15:15:38 +0000943 [ATOM_UPSTREAM] = { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
944 [ATOM_PUSH] = { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser },
945 [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser },
946 [ATOM_FLAG] = { "flag", SOURCE_NONE },
947 [ATOM_HEAD] = { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser },
948 [ATOM_COLOR] = { "color", SOURCE_NONE, FIELD_STR, color_atom_parser },
949 [ATOM_WORKTREEPATH] = { "worktreepath", SOURCE_NONE },
950 [ATOM_ALIGN] = { "align", SOURCE_NONE, FIELD_STR, align_atom_parser },
951 [ATOM_END] = { "end", SOURCE_NONE },
952 [ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
953 [ATOM_THEN] = { "then", SOURCE_NONE },
954 [ATOM_ELSE] = { "else", SOURCE_NONE },
ZheNing Hub9dee072021-07-26 03:26:50 +0000955 [ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser },
Derrick Stolee49abcd22023-03-20 11:26:54 +0000956 [ATOM_AHEADBEHIND] = { "ahead-behind", SOURCE_OTHER, FIELD_STR, ahead_behind_atom_parser },
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +0700957 /*
958 * Please update $__git_ref_fieldlist in git-completion.bash
959 * when you add new atoms
960 */
Karthik Nayakc95b7582015-06-14 01:07:27 +0530961};
962
Ævar Arnfjörð Bjarmason9865b6e2021-09-27 14:54:25 +0200963#define REF_FORMATTING_STATE_INIT { 0 }
Karthik Nayak574e96a2015-09-10 21:18:18 +0530964
965struct ref_formatting_stack {
966 struct ref_formatting_stack *prev;
967 struct strbuf output;
Karthik Nayakc58492d2017-01-10 14:19:34 +0530968 void (*at_end)(struct ref_formatting_stack **stack);
Karthik Nayakce592082015-09-11 20:33:07 +0530969 void *at_end_data;
Karthik Nayak574e96a2015-09-10 21:18:18 +0530970};
971
972struct ref_formatting_state {
973 int quote_style;
974 struct ref_formatting_stack *stack;
975};
976
Karthik Nayak3a257612015-08-22 09:09:37 +0530977struct atom_value {
978 const char *s;
ZheNing Hubd0708c2021-07-26 03:26:47 +0000979 ssize_t s_size;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +0000980 int (*handler)(struct atom_value *atomv, struct ref_formatting_state *state,
981 struct strbuf *err);
Johannes Schindeline467dc12017-04-20 22:52:09 +0200982 uintmax_t value; /* used for sorting when not FIELD_STR */
Karthik Nayakc58fc852017-01-10 14:19:35 +0530983 struct used_atom *atom;
Karthik Nayak3a257612015-08-22 09:09:37 +0530984};
985
ZheNing Hubd0708c2021-07-26 03:26:47 +0000986#define ATOM_SIZE_UNSPECIFIED (-1)
987
988#define ATOM_VALUE_INIT { \
989 .s_size = ATOM_SIZE_UNSPECIFIED \
990}
991
Karthik Nayakc95b7582015-06-14 01:07:27 +0530992/*
Karthik Nayakc95b7582015-06-14 01:07:27 +0530993 * Used to parse format string and sort specifiers
994 */
ZheNing Hue85fcb32021-07-26 03:26:49 +0000995static int parse_ref_filter_atom(struct ref_format *format,
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +0000996 const char *atom, const char *ep,
997 struct strbuf *err)
Karthik Nayakc95b7582015-06-14 01:07:27 +0530998{
999 const char *sp;
Karthik Nayak4de707e2016-02-17 23:36:12 +05301000 const char *arg;
SZEDER Gábore94ce132016-10-02 18:35:11 +02001001 int i, at, atom_len;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301002
1003 sp = atom;
1004 if (*sp == '*' && sp < ep)
1005 sp++; /* deref */
1006 if (ep <= sp)
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00001007 return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"),
1008 (int)(ep-atom), atom);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301009
SZEDER Gábore94ce132016-10-02 18:35:11 +02001010 /*
1011 * If the atom name has a colon, strip it and everything after
1012 * it off - it specifies the format for this entry, and
1013 * shouldn't be used for checking against the valid_atom
1014 * table.
1015 */
1016 arg = memchr(sp, ':', ep - sp);
1017 atom_len = (arg ? arg : ep) - sp;
1018
ZheNing Hubd0708c2021-07-26 03:26:47 +00001019 /* Do we have the atom already used elsewhere? */
1020 for (i = 0; i < used_atom_cnt; i++) {
1021 int len = strlen(used_atom[i].name);
1022 if (len == ep - atom && !memcmp(used_atom[i].name, atom, len))
1023 return i;
1024 }
1025
Karthik Nayakc95b7582015-06-14 01:07:27 +05301026 /* Is the atom a valid one? */
1027 for (i = 0; i < ARRAY_SIZE(valid_atom); i++) {
1028 int len = strlen(valid_atom[i].name);
SZEDER Gábore94ce132016-10-02 18:35:11 +02001029 if (len == atom_len && !memcmp(valid_atom[i].name, sp, len))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301030 break;
1031 }
1032
1033 if (ARRAY_SIZE(valid_atom) <= i)
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00001034 return strbuf_addf_ret(err, -1, _("unknown field name: %.*s"),
1035 (int)(ep-atom), atom);
SZEDER Gábor47bd3d02018-11-14 13:27:25 +01001036 if (valid_atom[i].source != SOURCE_NONE && !have_git_dir())
1037 return strbuf_addf_ret(err, -1,
1038 _("not a git repository, but the field '%.*s' requires access to object data"),
1039 (int)(ep-atom), atom);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301040
1041 /* Add it in, including the deref prefix */
1042 at = used_atom_cnt;
1043 used_atom_cnt++;
1044 REALLOC_ARRAY(used_atom, used_atom_cnt);
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001045 used_atom[at].atom_type = i;
Karthik Nayakb072add2016-02-17 23:36:11 +05301046 used_atom[at].name = xmemdupz(atom, ep - atom);
1047 used_atom[at].type = valid_atom[i].cmp_type;
Olga Telezhnayaa8e7e382018-07-17 08:22:57 +00001048 used_atom[at].source = valid_atom[i].source;
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001049 if (used_atom[at].source == SOURCE_OBJ) {
1050 if (*atom == '*')
1051 oi_deref.info.contentp = &oi_deref.content;
1052 else
1053 oi.info.contentp = &oi.content;
1054 }
Taylor Blaubea4dbe2017-10-02 09:10:34 -07001055 if (arg) {
Karthik Nayak4de707e2016-02-17 23:36:12 +05301056 arg = used_atom[at].name + (arg - atom) + 1;
Taylor Blaubea4dbe2017-10-02 09:10:34 -07001057 if (!*arg) {
1058 /*
1059 * Treat empty sub-arguments list as NULL (i.e.,
1060 * "%(atom:)" is equivalent to "%(atom)").
1061 */
1062 arg = NULL;
1063 }
1064 }
Karthik Nayakfd935cc2016-02-17 23:36:13 +05301065 memset(&used_atom[at].u, 0, sizeof(used_atom[at].u));
Olga Telezhnaya74efea92018-03-29 12:49:45 +00001066 if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err))
1067 return -1;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301068 if (*atom == '*')
1069 need_tagged = 1;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001070 if (i == ATOM_SYMREF)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301071 need_symref = 1;
1072 return at;
1073}
1074
ZheNing Hubd0708c2021-07-26 03:26:47 +00001075static void quote_formatting(struct strbuf *s, const char *str, ssize_t len, int quote_style)
Karthik Nayak63d89fb2015-09-10 21:18:20 +05301076{
1077 switch (quote_style) {
1078 case QUOTE_NONE:
ZheNing Hubd0708c2021-07-26 03:26:47 +00001079 if (len < 0)
1080 strbuf_addstr(s, str);
1081 else
1082 strbuf_add(s, str, len);
Karthik Nayak63d89fb2015-09-10 21:18:20 +05301083 break;
1084 case QUOTE_SHELL:
1085 sq_quote_buf(s, str);
1086 break;
1087 case QUOTE_PERL:
ZheNing Hu7121c4d2021-07-26 03:26:48 +00001088 if (len < 0)
1089 perl_quote_buf(s, str);
1090 else
1091 perl_quote_buf_with_len(s, str, len);
Karthik Nayak63d89fb2015-09-10 21:18:20 +05301092 break;
1093 case QUOTE_PYTHON:
1094 python_quote_buf(s, str);
1095 break;
1096 case QUOTE_TCL:
1097 tcl_quote_buf(s, str);
1098 break;
1099 }
1100}
1101
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001102static int append_atom(struct atom_value *v, struct ref_formatting_state *state,
Jeff King5fe9e1c2023-02-24 01:39:06 -05001103 struct strbuf *err UNUSED)
Karthik Nayak63d89fb2015-09-10 21:18:20 +05301104{
Karthik Nayakce592082015-09-11 20:33:07 +05301105 /*
1106 * Quote formatting is only done when the stack has a single
1107 * element. Otherwise quote formatting is done on the
1108 * element's entire output strbuf when the %(end) atom is
1109 * encountered.
1110 */
1111 if (!state->stack->prev)
ZheNing Hubd0708c2021-07-26 03:26:47 +00001112 quote_formatting(&state->stack->output, v->s, v->s_size, state->quote_style);
1113 else if (v->s_size < 0)
Karthik Nayakce592082015-09-11 20:33:07 +05301114 strbuf_addstr(&state->stack->output, v->s);
ZheNing Hubd0708c2021-07-26 03:26:47 +00001115 else
1116 strbuf_add(&state->stack->output, v->s, v->s_size);
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001117 return 0;
Karthik Nayak63d89fb2015-09-10 21:18:20 +05301118}
1119
Karthik Nayak574e96a2015-09-10 21:18:18 +05301120static void push_stack_element(struct ref_formatting_stack **stack)
1121{
1122 struct ref_formatting_stack *s = xcalloc(1, sizeof(struct ref_formatting_stack));
1123
1124 strbuf_init(&s->output, 0);
1125 s->prev = *stack;
1126 *stack = s;
1127}
1128
1129static void pop_stack_element(struct ref_formatting_stack **stack)
1130{
1131 struct ref_formatting_stack *current = *stack;
1132 struct ref_formatting_stack *prev = current->prev;
1133
1134 if (prev)
1135 strbuf_addbuf(&prev->output, &current->output);
1136 strbuf_release(&current->output);
1137 free(current);
1138 *stack = prev;
1139}
1140
Karthik Nayakc58492d2017-01-10 14:19:34 +05301141static void end_align_handler(struct ref_formatting_stack **stack)
Karthik Nayakce592082015-09-11 20:33:07 +05301142{
Karthik Nayakc58492d2017-01-10 14:19:34 +05301143 struct ref_formatting_stack *cur = *stack;
1144 struct align *align = (struct align *)cur->at_end_data;
Karthik Nayakce592082015-09-11 20:33:07 +05301145 struct strbuf s = STRBUF_INIT;
1146
Karthik Nayakc58492d2017-01-10 14:19:34 +05301147 strbuf_utf8_align(&s, align->position, align->width, cur->output.buf);
1148 strbuf_swap(&cur->output, &s);
Karthik Nayakce592082015-09-11 20:33:07 +05301149 strbuf_release(&s);
1150}
1151
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001152static int align_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
Jeff King5fe9e1c2023-02-24 01:39:06 -05001153 struct strbuf *err UNUSED)
Karthik Nayakce592082015-09-11 20:33:07 +05301154{
Brandon Williams1472b5b2018-02-14 10:59:46 -08001155 struct ref_formatting_stack *new_stack;
Karthik Nayakce592082015-09-11 20:33:07 +05301156
1157 push_stack_element(&state->stack);
Brandon Williams1472b5b2018-02-14 10:59:46 -08001158 new_stack = state->stack;
1159 new_stack->at_end = end_align_handler;
1160 new_stack->at_end_data = &atomv->atom->u.align;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001161 return 0;
Karthik Nayakce592082015-09-11 20:33:07 +05301162}
1163
Karthik Nayakc58492d2017-01-10 14:19:34 +05301164static void if_then_else_handler(struct ref_formatting_stack **stack)
1165{
1166 struct ref_formatting_stack *cur = *stack;
1167 struct ref_formatting_stack *prev = cur->prev;
1168 struct if_then_else *if_then_else = (struct if_then_else *)cur->at_end_data;
1169
1170 if (!if_then_else->then_atom_seen)
Jean-Noël Avilad7d30ba2022-01-05 20:02:23 +00001171 die(_("format: %%(%s) atom used without a %%(%s) atom"), "if", "then");
Karthik Nayakc58492d2017-01-10 14:19:34 +05301172
1173 if (if_then_else->else_atom_seen) {
1174 /*
1175 * There is an %(else) atom: we need to drop one state from the
1176 * stack, either the %(else) branch if the condition is satisfied, or
1177 * the %(then) branch if it isn't.
1178 */
1179 if (if_then_else->condition_satisfied) {
1180 strbuf_reset(&cur->output);
1181 pop_stack_element(&cur);
1182 } else {
1183 strbuf_swap(&cur->output, &prev->output);
1184 strbuf_reset(&cur->output);
1185 pop_stack_element(&cur);
1186 }
1187 } else if (!if_then_else->condition_satisfied) {
1188 /*
1189 * No %(else) atom: just drop the %(then) branch if the
1190 * condition is not satisfied.
1191 */
1192 strbuf_reset(&cur->output);
1193 }
1194
1195 *stack = cur;
1196 free(if_then_else);
1197}
1198
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001199static int if_atom_handler(struct atom_value *atomv, struct ref_formatting_state *state,
Jeff King5fe9e1c2023-02-24 01:39:06 -05001200 struct strbuf *err UNUSED)
Karthik Nayakc58492d2017-01-10 14:19:34 +05301201{
Brandon Williams1472b5b2018-02-14 10:59:46 -08001202 struct ref_formatting_stack *new_stack;
René Scharfe241b5d32021-03-06 12:26:19 +01001203 struct if_then_else *if_then_else = xcalloc(1,
1204 sizeof(struct if_then_else));
Karthik Nayakc58492d2017-01-10 14:19:34 +05301205
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05301206 if_then_else->str = atomv->atom->u.if_then_else.str;
1207 if_then_else->cmp_status = atomv->atom->u.if_then_else.cmp_status;
1208
Karthik Nayakc58492d2017-01-10 14:19:34 +05301209 push_stack_element(&state->stack);
Brandon Williams1472b5b2018-02-14 10:59:46 -08001210 new_stack = state->stack;
1211 new_stack->at_end = if_then_else_handler;
1212 new_stack->at_end_data = if_then_else;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001213 return 0;
Karthik Nayakc58492d2017-01-10 14:19:34 +05301214}
1215
ZheNing Hubd0708c2021-07-26 03:26:47 +00001216static int is_empty(struct strbuf *buf)
Karthik Nayakc58492d2017-01-10 14:19:34 +05301217{
ZheNing Hubd0708c2021-07-26 03:26:47 +00001218 const char *cur = buf->buf;
1219 const char *end = buf->buf + buf->len;
1220
1221 while (cur != end && (isspace(*cur)))
1222 cur++;
1223
1224 return cur == end;
1225 }
Karthik Nayakc58492d2017-01-10 14:19:34 +05301226
Jeff King5fe9e1c2023-02-24 01:39:06 -05001227static int then_atom_handler(struct atom_value *atomv UNUSED,
1228 struct ref_formatting_state *state,
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001229 struct strbuf *err)
Karthik Nayakc58492d2017-01-10 14:19:34 +05301230{
1231 struct ref_formatting_stack *cur = state->stack;
1232 struct if_then_else *if_then_else = NULL;
ZheNing Hubd0708c2021-07-26 03:26:47 +00001233 size_t str_len = 0;
Karthik Nayakc58492d2017-01-10 14:19:34 +05301234
1235 if (cur->at_end == if_then_else_handler)
1236 if_then_else = (struct if_then_else *)cur->at_end_data;
1237 if (!if_then_else)
Jean-Noël Avilad7d30ba2022-01-05 20:02:23 +00001238 return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "then", "if");
Karthik Nayakc58492d2017-01-10 14:19:34 +05301239 if (if_then_else->then_atom_seen)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001240 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used more than once"));
Karthik Nayakc58492d2017-01-10 14:19:34 +05301241 if (if_then_else->else_atom_seen)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001242 return strbuf_addf_ret(err, -1, _("format: %%(then) atom used after %%(else)"));
Karthik Nayakc58492d2017-01-10 14:19:34 +05301243 if_then_else->then_atom_seen = 1;
ZheNing Hubd0708c2021-07-26 03:26:47 +00001244 if (if_then_else->str)
1245 str_len = strlen(if_then_else->str);
Karthik Nayakc58492d2017-01-10 14:19:34 +05301246 /*
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05301247 * If the 'equals' or 'notequals' attribute is used then
1248 * perform the required comparison. If not, only non-empty
1249 * strings satisfy the 'if' condition.
Karthik Nayakc58492d2017-01-10 14:19:34 +05301250 */
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05301251 if (if_then_else->cmp_status == COMPARE_EQUAL) {
ZheNing Hubd0708c2021-07-26 03:26:47 +00001252 if (str_len == cur->output.len &&
1253 !memcmp(if_then_else->str, cur->output.buf, cur->output.len))
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05301254 if_then_else->condition_satisfied = 1;
1255 } else if (if_then_else->cmp_status == COMPARE_UNEQUAL) {
ZheNing Hubd0708c2021-07-26 03:26:47 +00001256 if (str_len != cur->output.len ||
1257 memcmp(if_then_else->str, cur->output.buf, cur->output.len))
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05301258 if_then_else->condition_satisfied = 1;
ZheNing Hubd0708c2021-07-26 03:26:47 +00001259 } else if (cur->output.len && !is_empty(&cur->output))
Karthik Nayakc58492d2017-01-10 14:19:34 +05301260 if_then_else->condition_satisfied = 1;
1261 strbuf_reset(&cur->output);
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001262 return 0;
Karthik Nayakc58492d2017-01-10 14:19:34 +05301263}
1264
Jeff King5fe9e1c2023-02-24 01:39:06 -05001265static int else_atom_handler(struct atom_value *atomv UNUSED,
1266 struct ref_formatting_state *state,
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001267 struct strbuf *err)
Karthik Nayakc58492d2017-01-10 14:19:34 +05301268{
1269 struct ref_formatting_stack *prev = state->stack;
1270 struct if_then_else *if_then_else = NULL;
1271
1272 if (prev->at_end == if_then_else_handler)
1273 if_then_else = (struct if_then_else *)prev->at_end_data;
1274 if (!if_then_else)
Jean-Noël Avilad7d30ba2022-01-05 20:02:23 +00001275 return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "if");
Karthik Nayakc58492d2017-01-10 14:19:34 +05301276 if (!if_then_else->then_atom_seen)
Jean-Noël Avilad7d30ba2022-01-05 20:02:23 +00001277 return strbuf_addf_ret(err, -1, _("format: %%(%s) atom used without a %%(%s) atom"), "else", "then");
Karthik Nayakc58492d2017-01-10 14:19:34 +05301278 if (if_then_else->else_atom_seen)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001279 return strbuf_addf_ret(err, -1, _("format: %%(else) atom used more than once"));
Karthik Nayakc58492d2017-01-10 14:19:34 +05301280 if_then_else->else_atom_seen = 1;
1281 push_stack_element(&state->stack);
1282 state->stack->at_end_data = prev->at_end_data;
1283 state->stack->at_end = prev->at_end;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001284 return 0;
Karthik Nayakce592082015-09-11 20:33:07 +05301285}
1286
Jeff King5fe9e1c2023-02-24 01:39:06 -05001287static int end_atom_handler(struct atom_value *atomv UNUSED,
1288 struct ref_formatting_state *state,
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001289 struct strbuf *err)
Karthik Nayakce592082015-09-11 20:33:07 +05301290{
1291 struct ref_formatting_stack *current = state->stack;
1292 struct strbuf s = STRBUF_INIT;
1293
1294 if (!current->at_end)
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001295 return strbuf_addf_ret(err, -1, _("format: %%(end) atom used without corresponding atom"));
Karthik Nayakc58492d2017-01-10 14:19:34 +05301296 current->at_end(&state->stack);
1297
1298 /* Stack may have been popped within at_end(), hence reset the current pointer */
1299 current = state->stack;
Karthik Nayakce592082015-09-11 20:33:07 +05301300
1301 /*
1302 * Perform quote formatting when the stack element is that of
1303 * a supporting atom. If nested then perform quote formatting
1304 * only on the topmost supporting atom.
1305 */
Karthik Nayakc58492d2017-01-10 14:19:34 +05301306 if (!current->prev->prev) {
ZheNing Hubd0708c2021-07-26 03:26:47 +00001307 quote_formatting(&s, current->output.buf, current->output.len, state->quote_style);
Karthik Nayakce592082015-09-11 20:33:07 +05301308 strbuf_swap(&current->output, &s);
1309 }
1310 strbuf_release(&s);
1311 pop_stack_element(&state->stack);
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00001312 return 0;
Karthik Nayakce592082015-09-11 20:33:07 +05301313}
1314
Karthik Nayakc95b7582015-06-14 01:07:27 +05301315/*
1316 * In a format string, find the next occurrence of %(atom).
1317 */
1318static const char *find_next(const char *cp)
1319{
1320 while (*cp) {
1321 if (*cp == '%') {
1322 /*
1323 * %( is the start of an atom;
1324 * %% is a quoted per-cent.
1325 */
1326 if (cp[1] == '(')
1327 return cp;
1328 else if (cp[1] == '%')
1329 cp++; /* skip over two % */
1330 /* otherwise this is a singleton, literal % */
1331 }
1332 cp++;
1333 }
1334 return NULL;
1335}
1336
ZheNing Hub9dee072021-07-26 03:26:50 +00001337static int reject_atom(enum atom_type atom_type)
1338{
1339 return atom_type == ATOM_REST;
1340}
1341
Karthik Nayakc95b7582015-06-14 01:07:27 +05301342/*
1343 * Make sure the format string is well formed, and parse out
1344 * the used atoms.
1345 */
Jeff King4a68e362017-07-13 11:01:18 -04001346int verify_ref_format(struct ref_format *format)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301347{
1348 const char *cp, *sp;
1349
Jeff Kingbf285ae2017-07-13 11:02:30 -04001350 format->need_color_reset_at_eol = 0;
Jeff King4a68e362017-07-13 11:01:18 -04001351 for (cp = format->format; *cp && (sp = find_next(cp)); ) {
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00001352 struct strbuf err = STRBUF_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301353 const char *color, *ep = strchr(sp, ')');
1354 int at;
1355
1356 if (!ep)
Nguyễn Thái Ngọc Duy1823c612016-02-27 13:42:04 +07001357 return error(_("malformed format string %s"), sp);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301358 /* sp points at "%(" and ep points at the closing ")" */
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00001359 at = parse_ref_filter_atom(format, sp + 2, ep, &err);
1360 if (at < 0)
1361 die("%s", err.buf);
ZheNing Hub9dee072021-07-26 03:26:50 +00001362 if (reject_atom(used_atom[at].atom_type))
1363 die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2);
ZheNing Hu7121c4d2021-07-26 03:26:48 +00001364
1365 if ((format->quote_style == QUOTE_PYTHON ||
1366 format->quote_style == QUOTE_SHELL ||
1367 format->quote_style == QUOTE_TCL) &&
1368 used_atom[at].atom_type == ATOM_RAW &&
1369 used_atom[at].u.raw_data.option == RAW_BARE)
Jiang Xinf7337192021-11-01 10:14:17 +08001370 die(_("--format=%.*s cannot be used with "
ZheNing Hu7121c4d2021-07-26 03:26:48 +00001371 "--python, --shell, --tcl"), (int)(ep - sp - 2), sp + 2);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301372 cp = ep + 1;
1373
Karthik Nayakb072add2016-02-17 23:36:11 +05301374 if (skip_prefix(used_atom[at].name, "color:", &color))
Jeff Kingbf285ae2017-07-13 11:02:30 -04001375 format->need_color_reset_at_eol = !!strcmp(color, "reset");
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00001376 strbuf_release(&err);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301377 }
Jeff King11b087a2017-07-13 11:09:32 -04001378 if (format->need_color_reset_at_eol && !want_color(format->use_color))
1379 format->need_color_reset_at_eol = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301380 return 0;
1381}
1382
Hariom Verma87d3beb2020-08-21 21:41:46 +00001383static const char *do_grab_oid(const char *field, const struct object_id *oid,
1384 struct used_atom *atom)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301385{
Hariom Verma87d3beb2020-08-21 21:41:46 +00001386 switch (atom->u.oid.option) {
Hariom Verma51011002020-08-21 21:41:44 +00001387 case O_FULL:
1388 return oid_to_hex(oid);
1389 case O_LENGTH:
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001390 return repo_find_unique_abbrev(the_repository, oid,
1391 atom->u.oid.length);
Hariom Verma51011002020-08-21 21:41:44 +00001392 case O_SHORT:
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02001393 return repo_find_unique_abbrev(the_repository, oid,
1394 DEFAULT_ABBREV);
Hariom Verma51011002020-08-21 21:41:44 +00001395 default:
1396 BUG("unknown %%(%s) option", field);
1397 }
1398}
1399
Hariom Verma87d3beb2020-08-21 21:41:46 +00001400static int grab_oid(const char *name, const char *field, const struct object_id *oid,
1401 struct atom_value *v, struct used_atom *atom)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301402{
Hariom Verma51011002020-08-21 21:41:44 +00001403 if (starts_with(name, field)) {
Hariom Verma87d3beb2020-08-21 21:41:46 +00001404 v->s = xstrdup(do_grab_oid(field, oid, atom));
Hariom Verma51011002020-08-21 21:41:44 +00001405 return 1;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301406 }
1407 return 0;
1408}
1409
1410/* See grab_values */
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00001411static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301412{
1413 int i;
1414
1415 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +05301416 const char *name = used_atom[i].name;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001417 enum atom_type atom_type = used_atom[i].atom_type;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301418 struct atom_value *v = &val[i];
1419 if (!!deref != (*name == '*'))
1420 continue;
1421 if (deref)
1422 name++;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001423 if (atom_type == ATOM_OBJECTTYPE)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001424 v->s = xstrdup(type_name(oi->type));
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001425 else if (atom_type == ATOM_OBJECTSIZE) {
ZheNing Hu0caf20f2021-05-13 15:15:37 +00001426 if (used_atom[i].u.objectsize.option == O_SIZE_DISK) {
1427 v->value = oi->disk_size;
1428 v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size);
1429 } else if (used_atom[i].u.objectsize.option == O_SIZE) {
1430 v->value = oi->size;
1431 v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
1432 }
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001433 } else if (atom_type == ATOM_DELTABASE)
Olga Telezhnaya33311fa2018-12-24 13:24:30 +00001434 v->s = xstrdup(oid_to_hex(&oi->delta_base_oid));
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001435 else if (atom_type == ATOM_OBJECTNAME && deref)
Hariom Verma87d3beb2020-08-21 21:41:46 +00001436 grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301437 }
1438}
1439
1440/* See grab_values */
Jeff Kinge0329192019-02-14 00:50:54 -05001441static void grab_tag_values(struct atom_value *val, int deref, struct object *obj)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301442{
1443 int i;
1444 struct tag *tag = (struct tag *) obj;
1445
1446 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +05301447 const char *name = used_atom[i].name;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001448 enum atom_type atom_type = used_atom[i].atom_type;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301449 struct atom_value *v = &val[i];
1450 if (!!deref != (*name == '*'))
1451 continue;
1452 if (deref)
1453 name++;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001454 if (atom_type == ATOM_TAG)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001455 v->s = xstrdup(tag->tag);
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001456 else if (atom_type == ATOM_TYPE && tag->tagged)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001457 v->s = xstrdup(type_name(tag->tagged->type));
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001458 else if (atom_type == ATOM_OBJECT && tag->tagged)
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001459 v->s = xstrdup(oid_to_hex(&tag->tagged->oid));
Karthik Nayakc95b7582015-06-14 01:07:27 +05301460 }
1461}
1462
1463/* See grab_values */
Jeff Kinge0329192019-02-14 00:50:54 -05001464static void grab_commit_values(struct atom_value *val, int deref, struct object *obj)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301465{
1466 int i;
1467 struct commit *commit = (struct commit *) obj;
1468
1469 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +05301470 const char *name = used_atom[i].name;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001471 enum atom_type atom_type = used_atom[i].atom_type;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301472 struct atom_value *v = &val[i];
1473 if (!!deref != (*name == '*'))
1474 continue;
1475 if (deref)
1476 name++;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001477 if (atom_type == ATOM_TREE &&
1478 grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i]))
Hariom Verma837adb12020-08-21 21:41:47 +00001479 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001480 if (atom_type == ATOM_NUMPARENT) {
Johannes Schindeline467dc12017-04-20 22:52:09 +02001481 v->value = commit_list_count(commit->parents);
1482 v->s = xstrfmt("%lu", (unsigned long)v->value);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301483 }
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001484 else if (atom_type == ATOM_PARENT) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05301485 struct commit_list *parents;
Jeff Kinga5e03bf2015-09-24 17:07:12 -04001486 struct strbuf s = STRBUF_INIT;
1487 for (parents = commit->parents; parents; parents = parents->next) {
Hariom Verma26bc0aa2020-08-21 21:41:48 +00001488 struct object_id *oid = &parents->item->object.oid;
Jeff Kinga5e03bf2015-09-24 17:07:12 -04001489 if (parents != commit->parents)
1490 strbuf_addch(&s, ' ');
Hariom Verma26bc0aa2020-08-21 21:41:48 +00001491 strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i]));
Karthik Nayakc95b7582015-06-14 01:07:27 +05301492 }
Jeff Kinga5e03bf2015-09-24 17:07:12 -04001493 v->s = strbuf_detach(&s, NULL);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301494 }
1495 }
1496}
1497
Jeff King5c326d12019-02-14 00:51:03 -05001498static const char *find_wholine(const char *who, int wholen, const char *buf)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301499{
1500 const char *eol;
1501 while (*buf) {
1502 if (!strncmp(buf, who, wholen) &&
1503 buf[wholen] == ' ')
1504 return buf + wholen + 1;
1505 eol = strchr(buf, '\n');
1506 if (!eol)
1507 return "";
1508 eol++;
1509 if (*eol == '\n')
1510 return ""; /* end of header */
1511 buf = eol;
1512 }
1513 return "";
1514}
1515
1516static const char *copy_line(const char *buf)
1517{
1518 const char *eol = strchrnul(buf, '\n');
1519 return xmemdupz(buf, eol - buf);
1520}
1521
1522static const char *copy_name(const char *buf)
1523{
1524 const char *cp;
1525 for (cp = buf; *cp && *cp != '\n'; cp++) {
Jeff King20869d12023-01-07 08:26:18 -05001526 if (starts_with(cp, " <"))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301527 return xmemdupz(buf, cp - buf);
1528 }
Mischa POSLAWSKY8b3f33e2019-08-17 23:51:07 +02001529 return xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05301530}
1531
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301532static const char *find_end_of_email(const char *email, int opt)
1533{
1534 const char *eoemail;
1535
1536 if (opt & EO_LOCALPART) {
1537 eoemail = strchr(email, '@');
1538 if (eoemail)
1539 return eoemail;
1540 return strchr(email, '>');
1541 }
1542
1543 if (opt & EO_TRIM)
1544 return strchr(email, '>');
1545
1546 /*
1547 * The option here is either the raw email option or the raw
1548 * mailmap option (that is EO_RAW or EO_MAILMAP). In such cases,
1549 * we directly grab the whole email including the closing
1550 * angle brackets.
1551 *
1552 * If EO_MAILMAP was set with any other option (that is either
1553 * EO_TRIM or EO_LOCALPART), we already grab the end of email
1554 * above.
1555 */
1556 eoemail = strchr(email, '>');
1557 if (eoemail)
1558 eoemail++;
1559 return eoemail;
1560}
1561
Hariom Vermab82445d2020-08-21 21:41:43 +00001562static const char *copy_email(const char *buf, struct used_atom *atom)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301563{
1564 const char *email = strchr(buf, '<');
1565 const char *eoemail;
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301566 int opt = atom->u.email_option.option;
1567
Karthik Nayakc95b7582015-06-14 01:07:27 +05301568 if (!email)
Mischa POSLAWSKY8b3f33e2019-08-17 23:51:07 +02001569 return xstrdup("");
Hariom Vermab82445d2020-08-21 21:41:43 +00001570
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301571 if (opt & (EO_LOCALPART | EO_TRIM))
1572 email++;
1573
1574 eoemail = find_end_of_email(email, opt);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301575 if (!eoemail)
Mischa POSLAWSKY8b3f33e2019-08-17 23:51:07 +02001576 return xstrdup("");
Hariom Vermab82445d2020-08-21 21:41:43 +00001577 return xmemdupz(email, eoemail - email);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301578}
1579
1580static char *copy_subject(const char *buf, unsigned long len)
1581{
Philippe Blain9f75ce32020-10-29 12:48:28 +00001582 struct strbuf sb = STRBUF_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301583 int i;
1584
Philippe Blain9f75ce32020-10-29 12:48:28 +00001585 for (i = 0; i < len; i++) {
1586 if (buf[i] == '\r' && i + 1 < len && buf[i + 1] == '\n')
1587 continue; /* ignore CR in CRLF */
Karthik Nayakc95b7582015-06-14 01:07:27 +05301588
Philippe Blain9f75ce32020-10-29 12:48:28 +00001589 if (buf[i] == '\n')
1590 strbuf_addch(&sb, ' ');
1591 else
1592 strbuf_addch(&sb, buf[i]);
1593 }
1594 return strbuf_detach(&sb, NULL);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301595}
1596
1597static void grab_date(const char *buf, struct atom_value *v, const char *atomname)
1598{
1599 const char *eoemail = strstr(buf, "> ");
1600 char *zone;
Johannes Schindelindddbad72017-04-26 21:29:31 +02001601 timestamp_t timestamp;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301602 long tz;
Ævar Arnfjörð Bjarmasonf1842892022-02-16 09:14:03 +01001603 struct date_mode date_mode = DATE_MODE_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301604 const char *formatp;
1605
1606 /*
1607 * We got here because atomname ends in "date" or "date<something>";
1608 * it's not possible that <something> is not ":<format>" because
1609 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
1610 * ":" means no format is specified, and use the default.
1611 */
1612 formatp = strchr(atomname, ':');
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001613 if (formatp) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05301614 formatp++;
Junio C Hamanod939af12015-08-03 11:01:27 -07001615 parse_date_format(formatp, &date_mode);
Victoria Dye46176d72024-02-08 01:57:19 +00001616
1617 /*
1618 * If this is a sort field and a format was specified, we'll
1619 * want to compare formatted date by string value.
1620 */
1621 v->atom->type = FIELD_STR;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301622 }
1623
1624 if (!eoemail)
1625 goto bad;
Johannes Schindelin1aeb7e72017-04-21 12:45:44 +02001626 timestamp = parse_timestamp(eoemail + 2, &zone, 10);
Johannes Schindelindddbad72017-04-26 21:29:31 +02001627 if (timestamp == TIME_MAX)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301628 goto bad;
1629 tz = strtol(zone, NULL, 10);
1630 if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
1631 goto bad;
René Scharfe9720d232024-04-05 19:44:59 +02001632 v->s = xstrdup(show_date(timestamp, tz, date_mode));
Johannes Schindeline467dc12017-04-20 22:52:09 +02001633 v->value = timestamp;
Ævar Arnfjörð Bjarmason974c9192022-02-16 09:14:05 +01001634 date_mode_release(&date_mode);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301635 return;
1636 bad:
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00001637 v->s = xstrdup("");
Johannes Schindeline467dc12017-04-20 22:52:09 +02001638 v->value = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301639}
1640
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301641static struct string_list mailmap = STRING_LIST_INIT_NODUP;
1642
Karthik Nayakc95b7582015-06-14 01:07:27 +05301643/* See grab_values */
Jeff King5c326d12019-02-14 00:51:03 -05001644static void grab_person(const char *who, struct atom_value *val, int deref, void *buf)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301645{
1646 int i;
1647 int wholen = strlen(who);
1648 const char *wholine = NULL;
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301649 const char *headers[] = { "author ", "committer ",
1650 "tagger ", NULL };
Karthik Nayakc95b7582015-06-14 01:07:27 +05301651
1652 for (i = 0; i < used_atom_cnt; i++) {
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301653 struct used_atom *atom = &used_atom[i];
1654 const char *name = atom->name;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301655 struct atom_value *v = &val[i];
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301656 struct strbuf mailmap_buf = STRBUF_INIT;
1657
Karthik Nayakc95b7582015-06-14 01:07:27 +05301658 if (!!deref != (*name == '*'))
1659 continue;
1660 if (deref)
1661 name++;
1662 if (strncmp(who, name, wholen))
1663 continue;
1664 if (name[wholen] != 0 &&
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301665 !starts_with(name + wholen, "name") &&
Hariom Vermab82445d2020-08-21 21:41:43 +00001666 !starts_with(name + wholen, "email") &&
Karthik Nayakc95b7582015-06-14 01:07:27 +05301667 !starts_with(name + wholen, "date"))
1668 continue;
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301669
1670 if ((starts_with(name + wholen, "name") &&
1671 (atom->u.name_option.option == N_MAILMAP)) ||
1672 (starts_with(name + wholen, "email") &&
1673 (atom->u.email_option.option & EO_MAILMAP))) {
1674 if (!mailmap.items)
1675 read_mailmap(&mailmap);
1676 strbuf_addstr(&mailmap_buf, buf);
1677 apply_mailmap_to_header(&mailmap_buf, headers, &mailmap);
1678 wholine = find_wholine(who, wholen, mailmap_buf.buf);
1679 } else {
Jeff King5c326d12019-02-14 00:51:03 -05001680 wholine = find_wholine(who, wholen, buf);
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301681 }
1682
Karthik Nayakc95b7582015-06-14 01:07:27 +05301683 if (!wholine)
1684 return; /* no point looking for it */
1685 if (name[wholen] == 0)
1686 v->s = copy_line(wholine);
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301687 else if (starts_with(name + wholen, "name"))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301688 v->s = copy_name(wholine);
Hariom Vermab82445d2020-08-21 21:41:43 +00001689 else if (starts_with(name + wholen, "email"))
1690 v->s = copy_email(wholine, &used_atom[i]);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301691 else if (starts_with(name + wholen, "date"))
1692 grab_date(wholine, v, name);
Kousik Sanagavarapua3d2e832023-09-25 23:13:10 +05301693
1694 strbuf_release(&mailmap_buf);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301695 }
1696
1697 /*
1698 * For a tag or a commit object, if "creator" or "creatordate" is
1699 * requested, do something special.
1700 */
1701 if (strcmp(who, "tagger") && strcmp(who, "committer"))
1702 return; /* "author" for commit object is not wanted */
1703 if (!wholine)
Jeff King5c326d12019-02-14 00:51:03 -05001704 wholine = find_wholine(who, wholen, buf);
Karthik Nayakc95b7582015-06-14 01:07:27 +05301705 if (!wholine)
1706 return;
1707 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakb072add2016-02-17 23:36:11 +05301708 const char *name = used_atom[i].name;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001709 enum atom_type atom_type = used_atom[i].atom_type;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301710 struct atom_value *v = &val[i];
1711 if (!!deref != (*name == '*'))
1712 continue;
1713 if (deref)
1714 name++;
1715
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001716 if (atom_type == ATOM_CREATORDATE)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301717 grab_date(wholine, v, name);
ZheNing Hu1197f1a2021-05-13 15:15:38 +00001718 else if (atom_type == ATOM_CREATOR)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301719 v->s = copy_line(wholine);
1720 }
1721}
1722
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +05301723static void grab_signature(struct atom_value *val, int deref, struct object *obj)
1724{
1725 int i;
1726 struct commit *commit = (struct commit *) obj;
1727 struct signature_check sigc = { 0 };
1728 int signature_checked = 0;
1729
1730 for (i = 0; i < used_atom_cnt; i++) {
1731 struct used_atom *atom = &used_atom[i];
1732 const char *name = atom->name;
1733 struct atom_value *v = &val[i];
1734 int opt;
1735
1736 if (!!deref != (*name == '*'))
1737 continue;
1738 if (deref)
1739 name++;
1740
1741 if (!skip_prefix(name, "signature", &name) ||
1742 (*name && *name != ':'))
1743 continue;
1744 if (!*name)
1745 name = NULL;
1746 else
1747 name++;
1748
1749 opt = parse_signature_option(name);
1750 if (opt < 0)
1751 continue;
1752
1753 if (!signature_checked) {
1754 check_commit_signature(commit, &sigc);
1755 signature_checked = 1;
1756 }
1757
1758 switch (opt) {
1759 case S_BARE:
1760 v->s = xstrdup(sigc.output ? sigc.output: "");
1761 break;
1762 case S_SIGNER:
1763 v->s = xstrdup(sigc.signer ? sigc.signer : "");
1764 break;
1765 case S_GRADE:
1766 switch (sigc.result) {
1767 case 'G':
1768 switch (sigc.trust_level) {
1769 case TRUST_UNDEFINED:
1770 case TRUST_NEVER:
1771 v->s = xstrfmt("%c", (char)'U');
1772 break;
1773 default:
1774 v->s = xstrfmt("%c", (char)'G');
1775 break;
1776 }
1777 break;
1778 case 'B':
1779 case 'E':
1780 case 'N':
1781 case 'X':
1782 case 'Y':
1783 case 'R':
1784 v->s = xstrfmt("%c", (char)sigc.result);
1785 break;
1786 }
1787 break;
1788 case S_KEY:
1789 v->s = xstrdup(sigc.key ? sigc.key : "");
1790 break;
1791 case S_FINGERPRINT:
1792 v->s = xstrdup(sigc.fingerprint ?
1793 sigc.fingerprint : "");
1794 break;
1795 case S_PRI_KEY_FP:
1796 v->s = xstrdup(sigc.primary_key_fingerprint ?
1797 sigc.primary_key_fingerprint : "");
1798 break;
1799 case S_TRUST_LEVEL:
1800 v->s = xstrdup(gpg_trust_level_to_str(sigc.trust_level));
1801 break;
1802 }
1803 }
1804
1805 if (signature_checked)
1806 signature_check_clear(&sigc);
1807}
1808
Jeff King5c326d12019-02-14 00:51:03 -05001809static void find_subpos(const char *buf,
brian m. carlson83dff3e2021-01-18 23:49:10 +00001810 const char **sub, size_t *sublen,
1811 const char **body, size_t *bodylen,
1812 size_t *nonsiglen,
1813 const char **sig, size_t *siglen)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301814{
brian m. carlson482c1192021-02-11 02:08:03 +00001815 struct strbuf payload = STRBUF_INIT;
1816 struct strbuf signature = STRBUF_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301817 const char *eol;
brian m. carlson482c1192021-02-11 02:08:03 +00001818 const char *end = buf + strlen(buf);
1819 const char *sigstart;
1820
brian m. carlson88bce0e2021-02-11 02:08:05 +00001821 /* parse signature first; we might not even have a subject line */
1822 parse_signature(buf, end - buf, &payload, &signature);
Ævar Arnfjörð Bjarmasonb6046ab2022-11-08 19:17:42 +01001823 strbuf_release(&payload);
brian m. carlson482c1192021-02-11 02:08:03 +00001824
Karthik Nayakc95b7582015-06-14 01:07:27 +05301825 /* skip past header until we hit empty line */
1826 while (*buf && *buf != '\n') {
1827 eol = strchrnul(buf, '\n');
1828 if (*eol)
1829 eol++;
1830 buf = eol;
1831 }
1832 /* skip any empty lines */
1833 while (*buf == '\n')
1834 buf++;
brian m. carlson482c1192021-02-11 02:08:03 +00001835 *sig = strbuf_detach(&signature, siglen);
1836 sigstart = buf + parse_signed_buffer(buf, strlen(buf));
Karthik Nayakc95b7582015-06-14 01:07:27 +05301837
1838 /* subject is first non-empty line */
1839 *sub = buf;
Philippe Blain9f75ce32020-10-29 12:48:28 +00001840 /* subject goes to first empty line before signature begins */
Jeff King8e1c5fc2022-11-02 03:44:00 -04001841 if ((eol = strstr(*sub, "\n\n")) ||
1842 (eol = strstr(*sub, "\r\n\r\n"))) {
brian m. carlson482c1192021-02-11 02:08:03 +00001843 eol = eol < sigstart ? eol : sigstart;
Jeff King8e1c5fc2022-11-02 03:44:00 -04001844 } else {
Philippe Blain9f75ce32020-10-29 12:48:28 +00001845 /* treat whole message as subject */
Jeff Kingb01e1c72022-11-02 03:42:07 -04001846 eol = sigstart;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301847 }
Philippe Blain9f75ce32020-10-29 12:48:28 +00001848 buf = eol;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301849 *sublen = buf - *sub;
1850 /* drop trailing newline, if present */
Philippe Blain9f75ce32020-10-29 12:48:28 +00001851 while (*sublen && ((*sub)[*sublen - 1] == '\n' ||
1852 (*sub)[*sublen - 1] == '\r'))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301853 *sublen -= 1;
1854
1855 /* skip any empty lines */
Philippe Blain9f75ce32020-10-29 12:48:28 +00001856 while (*buf == '\n' || *buf == '\r')
Karthik Nayakc95b7582015-06-14 01:07:27 +05301857 buf++;
1858 *body = buf;
1859 *bodylen = strlen(buf);
brian m. carlson482c1192021-02-11 02:08:03 +00001860 *nonsiglen = sigstart - buf;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301861}
1862
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301863/*
1864 * If 'lines' is greater than 0, append that many lines from the given
1865 * 'buf' of length 'size' to the given strbuf.
1866 */
1867static void append_lines(struct strbuf *out, const char *buf, unsigned long size, int lines)
1868{
1869 int i;
1870 const char *sp, *eol;
1871 size_t len;
1872
1873 sp = buf;
1874
1875 for (i = 0; i < lines && sp < buf + size; i++) {
1876 if (i)
1877 strbuf_addstr(out, "\n ");
1878 eol = memchr(sp, '\n', size - (sp - buf));
1879 len = eol ? eol - sp : size - (sp - buf);
1880 strbuf_add(out, sp, len);
1881 if (!eol)
1882 break;
1883 sp = eol + 1;
1884 }
1885}
1886
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +05301887static void grab_describe_values(struct atom_value *val, int deref,
1888 struct object *obj)
1889{
1890 struct commit *commit = (struct commit *)obj;
1891 int i;
1892
1893 for (i = 0; i < used_atom_cnt; i++) {
1894 struct used_atom *atom = &used_atom[i];
1895 enum atom_type type = atom->atom_type;
1896 const char *name = atom->name;
1897 struct atom_value *v = &val[i];
1898
1899 struct child_process cmd = CHILD_PROCESS_INIT;
1900 struct strbuf out = STRBUF_INIT;
1901 struct strbuf err = STRBUF_INIT;
1902
1903 if (type != ATOM_DESCRIBE)
1904 continue;
1905
1906 if (!!deref != (*name == '*'))
1907 continue;
1908
1909 cmd.git_cmd = 1;
1910 strvec_push(&cmd.args, "describe");
1911 strvec_pushv(&cmd.args, atom->u.describe_args);
1912 strvec_push(&cmd.args, oid_to_hex(&commit->object.oid));
1913 if (pipe_command(&cmd, NULL, 0, &out, 0, &err, 0) < 0) {
1914 error(_("failed to run 'describe'"));
1915 v->s = xstrdup("");
1916 continue;
1917 }
1918 strbuf_rtrim(&out);
1919 v->s = strbuf_detach(&out, NULL);
1920
1921 strbuf_release(&err);
1922 }
1923}
1924
Karthik Nayakc95b7582015-06-14 01:07:27 +05301925/* See grab_values */
ZheNing Hu311d0b82021-07-26 03:26:46 +00001926static void grab_sub_body_contents(struct atom_value *val, int deref, struct expand_data *data)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301927{
1928 int i;
1929 const char *subpos = NULL, *bodypos = NULL, *sigpos = NULL;
brian m. carlson83dff3e2021-01-18 23:49:10 +00001930 size_t sublen = 0, bodylen = 0, nonsiglen = 0, siglen = 0;
ZheNing Hu311d0b82021-07-26 03:26:46 +00001931 void *buf = data->content;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301932
1933 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayak452db392016-02-17 23:36:18 +05301934 struct used_atom *atom = &used_atom[i];
1935 const char *name = atom->name;
Karthik Nayakc95b7582015-06-14 01:07:27 +05301936 struct atom_value *v = &val[i];
ZheNing Hubd0708c2021-07-26 03:26:47 +00001937 enum atom_type atom_type = atom->atom_type;
brian m. carlson482c1192021-02-11 02:08:03 +00001938
Karthik Nayakc95b7582015-06-14 01:07:27 +05301939 if (!!deref != (*name == '*'))
1940 continue;
1941 if (deref)
1942 name++;
ZheNing Hu311d0b82021-07-26 03:26:46 +00001943
ZheNing Hubd0708c2021-07-26 03:26:47 +00001944 if (atom_type == ATOM_RAW) {
1945 unsigned long buf_size = data->size;
1946
1947 if (atom->u.raw_data.option == RAW_BARE) {
1948 v->s = xmemdupz(buf, buf_size);
1949 v->s_size = buf_size;
1950 } else if (atom->u.raw_data.option == RAW_LENGTH) {
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +05301951 v->value = buf_size;
1952 v->s = xstrfmt("%"PRIuMAX, v->value);
ZheNing Hubd0708c2021-07-26 03:26:47 +00001953 }
1954 continue;
1955 }
1956
ZheNing Hu311d0b82021-07-26 03:26:46 +00001957 if ((data->type != OBJ_TAG &&
1958 data->type != OBJ_COMMIT) ||
1959 (strcmp(name, "body") &&
1960 !starts_with(name, "subject") &&
1961 !starts_with(name, "trailers") &&
1962 !starts_with(name, "contents")))
Karthik Nayakc95b7582015-06-14 01:07:27 +05301963 continue;
1964 if (!subpos)
Jeff King5c326d12019-02-14 00:51:03 -05001965 find_subpos(buf,
Karthik Nayakc95b7582015-06-14 01:07:27 +05301966 &subpos, &sublen,
1967 &bodypos, &bodylen, &nonsiglen,
1968 &sigpos, &siglen);
1969
Karthik Nayak452db392016-02-17 23:36:18 +05301970 if (atom->u.contents.option == C_SUB)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301971 v->s = copy_subject(subpos, sublen);
Hariom Verma905f0a42020-08-21 21:41:50 +00001972 else if (atom->u.contents.option == C_SUB_SANITIZE) {
1973 struct strbuf sb = STRBUF_INIT;
1974 format_sanitized_subject(&sb, subpos, sublen);
1975 v->s = strbuf_detach(&sb, NULL);
1976 } else if (atom->u.contents.option == C_BODY_DEP)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301977 v->s = xmemdupz(bodypos, bodylen);
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +05301978 else if (atom->u.contents.option == C_LENGTH) {
1979 v->value = strlen(subpos);
1980 v->s = xstrfmt("%"PRIuMAX, v->value);
1981 } else if (atom->u.contents.option == C_BODY)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301982 v->s = xmemdupz(bodypos, nonsiglen);
Karthik Nayak452db392016-02-17 23:36:18 +05301983 else if (atom->u.contents.option == C_SIG)
Karthik Nayakc95b7582015-06-14 01:07:27 +05301984 v->s = xmemdupz(sigpos, siglen);
Karthik Nayak452db392016-02-17 23:36:18 +05301985 else if (atom->u.contents.option == C_LINES) {
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301986 struct strbuf s = STRBUF_INIT;
brian m. carlson88bce0e2021-02-11 02:08:05 +00001987 const char *contents_end = bodypos + nonsiglen;
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301988
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301989 /* Size is the length of the message after removing the signature */
Karthik Nayak452db392016-02-17 23:36:18 +05301990 append_lines(&s, subpos, contents_end - subpos, atom->u.contents.nlines);
Karthik Nayak1bb38e52015-09-11 20:34:16 +05301991 v->s = strbuf_detach(&s, NULL);
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001992 } else if (atom->u.contents.option == C_TRAILERS) {
Taylor Blau67a20a02017-10-01 22:25:23 -07001993 struct strbuf s = STRBUF_INIT;
Jacob Kellerb1d31c82016-11-18 16:58:15 -08001994
Taylor Blau67a20a02017-10-01 22:25:23 -07001995 /* Format the trailer info according to the trailer_opts given */
Linus Arver0383dc52024-03-01 00:14:42 +00001996 format_trailers_from_commit(&atom->u.contents.trailer_opts, subpos, &s);
Taylor Blau67a20a02017-10-01 22:25:23 -07001997
1998 v->s = strbuf_detach(&s, NULL);
Karthik Nayak452db392016-02-17 23:36:18 +05301999 } else if (atom->u.contents.option == C_BARE)
2000 v->s = xstrdup(subpos);
brian m. carlson482c1192021-02-11 02:08:03 +00002001
Karthik Nayakc95b7582015-06-14 01:07:27 +05302002 }
brian m. carlson482c1192021-02-11 02:08:03 +00002003 free((void *)sigpos);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302004}
2005
2006/*
2007 * We want to have empty print-string for field requests
2008 * that do not apply (e.g. "authordate" for a tag object)
2009 */
2010static void fill_missing_values(struct atom_value *val)
2011{
2012 int i;
2013 for (i = 0; i < used_atom_cnt; i++) {
2014 struct atom_value *v = &val[i];
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002015 if (!v->s)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002016 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302017 }
2018}
2019
2020/*
2021 * val is a list of atom_value to hold returned values. Extract
2022 * the values for atoms in used_atom array out of (obj, buf, sz).
2023 * when deref is false, (obj, buf, sz) is the object that is
2024 * pointed at by the ref itself; otherwise it is the object the
2025 * ref (which is a tag) refers to.
2026 */
ZheNing Hu311d0b82021-07-26 03:26:46 +00002027static void grab_values(struct atom_value *val, int deref, struct object *obj, struct expand_data *data)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302028{
ZheNing Hu311d0b82021-07-26 03:26:46 +00002029 void *buf = data->content;
2030
Karthik Nayakc95b7582015-06-14 01:07:27 +05302031 switch (obj->type) {
2032 case OBJ_TAG:
Jeff Kinge0329192019-02-14 00:50:54 -05002033 grab_tag_values(val, deref, obj);
ZheNing Hu311d0b82021-07-26 03:26:46 +00002034 grab_sub_body_contents(val, deref, data);
Jeff King5c326d12019-02-14 00:51:03 -05002035 grab_person("tagger", val, deref, buf);
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +05302036 grab_describe_values(val, deref, obj);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302037 break;
2038 case OBJ_COMMIT:
Jeff Kinge0329192019-02-14 00:50:54 -05002039 grab_commit_values(val, deref, obj);
ZheNing Hu311d0b82021-07-26 03:26:46 +00002040 grab_sub_body_contents(val, deref, data);
Jeff King5c326d12019-02-14 00:51:03 -05002041 grab_person("author", val, deref, buf);
2042 grab_person("committer", val, deref, buf);
Kousik Sanagavarapu26c9c032023-06-04 23:52:47 +05302043 grab_signature(val, deref, obj);
Kousik Sanagavarapuf5d18f82023-07-23 21:49:59 +05302044 grab_describe_values(val, deref, obj);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302045 break;
2046 case OBJ_TREE:
2047 /* grab_tree_values(val, deref, obj, buf, sz); */
ZheNing Hubd0708c2021-07-26 03:26:47 +00002048 grab_sub_body_contents(val, deref, data);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302049 break;
2050 case OBJ_BLOB:
2051 /* grab_blob_values(val, deref, obj, buf, sz); */
ZheNing Hubd0708c2021-07-26 03:26:47 +00002052 grab_sub_body_contents(val, deref, data);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302053 break;
2054 default:
2055 die("Eh? Object of type %d?", obj->type);
2056 }
2057}
2058
2059static inline char *copy_advance(char *dst, const char *src)
2060{
2061 while (*src)
2062 *dst++ = *src++;
2063 return dst;
2064}
2065
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05302066static const char *lstrip_ref_components(const char *refname, int len)
Jeff King05719792016-01-25 22:00:05 -05002067{
Karthik Nayaka7984102017-01-10 14:19:44 +05302068 long remaining = len;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002069 const char *start = xstrdup(refname);
2070 const char *to_free = start;
Jeff King05719792016-01-25 22:00:05 -05002071
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05302072 if (len < 0) {
2073 int i;
2074 const char *p = refname;
Jeff King05719792016-01-25 22:00:05 -05002075
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05302076 /* Find total no of '/' separated path-components */
2077 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
2078 ;
2079 /*
2080 * The number of components we need to strip is now
2081 * the total minus the components to be left (Plus one
2082 * because we count the number of '/', but the number
2083 * of components is one more than the no of '/').
2084 */
2085 remaining = i + len + 1;
2086 }
2087
2088 while (remaining > 0) {
Jeff King05719792016-01-25 22:00:05 -05002089 switch (*start++) {
2090 case '\0':
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002091 free((char *)to_free);
2092 return xstrdup("");
Jeff King05719792016-01-25 22:00:05 -05002093 case '/':
2094 remaining--;
2095 break;
2096 }
2097 }
Karthik Nayak1a0ca5e2017-01-10 14:19:48 +05302098
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002099 start = xstrdup(start);
2100 free((char *)to_free);
Jeff King05719792016-01-25 22:00:05 -05002101 return start;
2102}
2103
Karthik Nayak1a347282017-01-10 14:19:49 +05302104static const char *rstrip_ref_components(const char *refname, int len)
2105{
2106 long remaining = len;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002107 const char *start = xstrdup(refname);
2108 const char *to_free = start;
Karthik Nayak1a347282017-01-10 14:19:49 +05302109
2110 if (len < 0) {
2111 int i;
2112 const char *p = refname;
2113
2114 /* Find total no of '/' separated path-components */
2115 for (i = 0; p[i]; p[i] == '/' ? i++ : *p++)
2116 ;
2117 /*
2118 * The number of components we need to strip is now
2119 * the total minus the components to be left (Plus one
2120 * because we count the number of '/', but the number
2121 * of components is one more than the no of '/').
2122 */
2123 remaining = i + len + 1;
2124 }
2125
2126 while (remaining-- > 0) {
2127 char *p = strrchr(start, '/');
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002128 if (!p) {
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002129 free((char *)to_free);
2130 return xstrdup("");
2131 } else
Karthik Nayak1a347282017-01-10 14:19:49 +05302132 p[0] = '\0';
2133 }
2134 return start;
2135}
2136
Karthik Nayaka7984102017-01-10 14:19:44 +05302137static const char *show_ref(struct refname_atom *atom, const char *refname)
2138{
2139 if (atom->option == R_SHORT)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02002140 return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
2141 refname,
2142 warn_ambiguous_refs);
Karthik Nayak17938f12017-01-10 14:19:46 +05302143 else if (atom->option == R_LSTRIP)
2144 return lstrip_ref_components(refname, atom->lstrip);
Karthik Nayak1a347282017-01-10 14:19:49 +05302145 else if (atom->option == R_RSTRIP)
2146 return rstrip_ref_components(refname, atom->rstrip);
Karthik Nayaka7984102017-01-10 14:19:44 +05302147 else
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002148 return xstrdup(refname);
Karthik Nayaka7984102017-01-10 14:19:44 +05302149}
2150
Karthik Nayak5339bda2016-02-17 23:36:17 +05302151static void fill_remote_ref_details(struct used_atom *atom, const char *refname,
2152 struct branch *branch, const char **s)
2153{
2154 int num_ours, num_theirs;
Karthik Nayak3ba308c2017-01-10 14:19:45 +05302155 if (atom->u.remote_ref.option == RR_REF)
2156 *s = show_ref(&atom->u.remote_ref.refname, refname);
Karthik Nayak7743fcc2017-01-10 14:19:41 +05302157 else if (atom->u.remote_ref.option == RR_TRACK) {
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00002158 if (stat_tracking_info(branch, &num_ours, &num_theirs,
Damien Robertc646d092019-04-16 14:16:46 +02002159 NULL, atom->u.remote_ref.push,
2160 AHEAD_BEHIND_FULL) < 0) {
Karthik Nayak6eac70f2017-01-10 14:19:50 +05302161 *s = xstrdup(msgs.gone);
Karthik Nayak7743fcc2017-01-10 14:19:41 +05302162 } else if (!num_ours && !num_theirs)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002163 *s = xstrdup("");
Karthik Nayak5339bda2016-02-17 23:36:17 +05302164 else if (!num_ours)
Karthik Nayak6eac70f2017-01-10 14:19:50 +05302165 *s = xstrfmt(msgs.behind, num_theirs);
Karthik Nayak5339bda2016-02-17 23:36:17 +05302166 else if (!num_theirs)
Karthik Nayak6eac70f2017-01-10 14:19:50 +05302167 *s = xstrfmt(msgs.ahead, num_ours);
Karthik Nayak5339bda2016-02-17 23:36:17 +05302168 else
Karthik Nayak6eac70f2017-01-10 14:19:50 +05302169 *s = xstrfmt(msgs.ahead_behind,
Karthik Nayak5339bda2016-02-17 23:36:17 +05302170 num_ours, num_theirs);
Karthik Nayak7743fcc2017-01-10 14:19:41 +05302171 if (!atom->u.remote_ref.nobracket && *s[0]) {
2172 const char *to_free = *s;
2173 *s = xstrfmt("[%s]", *s);
2174 free((void *)to_free);
2175 }
2176 } else if (atom->u.remote_ref.option == RR_TRACKSHORT) {
Jeff Hostetlerd7d1b492018-01-09 18:50:15 +00002177 if (stat_tracking_info(branch, &num_ours, &num_theirs,
Damien Robertc646d092019-04-16 14:16:46 +02002178 NULL, atom->u.remote_ref.push,
2179 AHEAD_BEHIND_FULL) < 0) {
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002180 *s = xstrdup("");
Karthik Nayak5339bda2016-02-17 23:36:17 +05302181 return;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002182 }
Karthik Nayak5339bda2016-02-17 23:36:17 +05302183 if (!num_ours && !num_theirs)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002184 *s = xstrdup("=");
Karthik Nayak5339bda2016-02-17 23:36:17 +05302185 else if (!num_ours)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002186 *s = xstrdup("<");
Karthik Nayak5339bda2016-02-17 23:36:17 +05302187 else if (!num_theirs)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002188 *s = xstrdup(">");
Karthik Nayak5339bda2016-02-17 23:36:17 +05302189 else
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002190 *s = xstrdup("<>");
Johannes Schindelincc723852017-10-05 14:19:09 +02002191 } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) {
2192 int explicit;
2193 const char *remote = atom->u.remote_ref.push ?
2194 pushremote_for_branch(branch, &explicit) :
2195 remote_for_branch(branch, &explicit);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002196 *s = xstrdup(explicit ? remote : "");
J Wyman9700fae2017-11-07 17:31:08 +01002197 } else if (atom->u.remote_ref.option == RR_REMOTE_REF) {
J Wyman9700fae2017-11-07 17:31:08 +01002198 const char *merge;
2199
Jeff Kingaf8ccd82020-03-03 17:12:22 +01002200 merge = remote_ref_for_branch(branch, atom->u.remote_ref.push);
2201 *s = xstrdup(merge ? merge : "");
Karthik Nayak3ba308c2017-01-10 14:19:45 +05302202 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02002203 BUG("unhandled RR_* enum");
Karthik Nayak5339bda2016-02-17 23:36:17 +05302204}
2205
Karthik Nayakd4919bb2017-01-10 14:19:38 +05302206char *get_head_description(void)
2207{
2208 struct strbuf desc = STRBUF_INIT;
2209 struct wt_status_state state;
2210 memset(&state, 0, sizeof(state));
Nguyễn Thái Ngọc Duy78845452018-11-10 06:48:50 +01002211 wt_status_get_state(the_repository, &state, 1);
Karthik Nayakd4919bb2017-01-10 14:19:38 +05302212 if (state.rebase_in_progress ||
Kaartic Sivaraama236f902018-04-03 10:01:00 +05302213 state.rebase_interactive_in_progress) {
2214 if (state.branch)
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01002215 strbuf_addf(&desc, _("(no branch, rebasing %s)"),
Kaartic Sivaraama236f902018-04-03 10:01:00 +05302216 state.branch);
2217 else
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01002218 strbuf_addf(&desc, _("(no branch, rebasing detached HEAD %s)"),
Kaartic Sivaraama236f902018-04-03 10:01:00 +05302219 state.detached_from);
2220 } else if (state.bisect_in_progress)
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01002221 strbuf_addf(&desc, _("(no branch, bisect started on %s)"),
Rubén Justo990adcc2023-09-09 22:12:47 +02002222 state.bisecting_from);
Karthik Nayakd4919bb2017-01-10 14:19:38 +05302223 else if (state.detached_from) {
Karthik Nayakd4919bb2017-01-10 14:19:38 +05302224 if (state.detached_at)
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01002225 strbuf_addf(&desc, _("(HEAD detached at %s)"),
2226 state.detached_from);
Karthik Nayakd4919bb2017-01-10 14:19:38 +05302227 else
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01002228 strbuf_addf(&desc, _("(HEAD detached from %s)"),
2229 state.detached_from);
2230 } else
2231 strbuf_addstr(&desc, _("(no branch)"));
Matthew DeVore28438e82019-06-18 15:29:15 -07002232
Rubén Justoabcac2e2022-09-25 00:53:18 +02002233 wt_status_state_free_buffers(&state);
2234
Karthik Nayakd4919bb2017-01-10 14:19:38 +05302235 return strbuf_detach(&desc, NULL);
2236}
2237
Karthik Nayaka7984102017-01-10 14:19:44 +05302238static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref)
2239{
2240 if (!ref->symref)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002241 return xstrdup("");
Karthik Nayaka7984102017-01-10 14:19:44 +05302242 else
2243 return show_ref(&atom->u.refname, ref->symref);
2244}
2245
2246static const char *get_refname(struct used_atom *atom, struct ref_array_item *ref)
2247{
2248 if (ref->kind & FILTER_REFS_DETACHED_HEAD)
2249 return get_head_description();
2250 return show_ref(&atom->u.refname, ref->refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302251}
2252
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002253static int get_object(struct ref_array_item *ref, int deref, struct object **obj,
2254 struct expand_data *oi, struct strbuf *err)
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00002255{
Olga Telezhnaya04f6ee12018-07-17 08:22:57 +00002256 /* parse_object_buffer() will set eaten to 0 if free() will be needed */
2257 int eaten = 1;
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002258 if (oi->info.contentp) {
2259 /* We need to know that to use parse_object_buffer properly */
2260 oi->info.sizep = &oi->size;
2261 oi->info.typep = &oi->type;
Olga Telezhnayae2255172018-07-17 08:22:57 +00002262 }
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002263 if (oid_object_info_extended(the_repository, &oi->oid, &oi->info,
2264 OBJECT_INFO_LOOKUP_REPLACE))
2265 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
2266 oid_to_hex(&oi->oid), ref->refname);
Olga Telezhnaya5305a552018-12-24 13:24:30 +00002267 if (oi->info.disk_sizep && oi->disk_size < 0)
2268 BUG("Object size is less than zero.");
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002269
2270 if (oi->info.contentp) {
Junio C Hamanoc83149a2018-08-17 13:09:57 -07002271 *obj = parse_object_buffer(the_repository, &oi->oid, oi->type, oi->size, oi->content, &eaten);
Jeff Kingc6854502021-04-01 04:32:24 -04002272 if (!*obj) {
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002273 if (!eaten)
2274 free(oi->content);
2275 return strbuf_addf_ret(err, -1, _("parse_object_buffer failed on %s for %s"),
2276 oid_to_hex(&oi->oid), ref->refname);
2277 }
ZheNing Hu311d0b82021-07-26 03:26:46 +00002278 grab_values(ref->value, deref, *obj, oi);
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002279 }
2280
2281 grab_common_values(ref->value, deref, oi);
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00002282 if (!eaten)
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002283 free(oi->content);
2284 return 0;
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00002285}
2286
Nickolai Belakovski25820832019-04-28 22:19:42 -07002287static void populate_worktree_map(struct hashmap *map, struct worktree **worktrees)
2288{
2289 int i;
2290
2291 for (i = 0; worktrees[i]; i++) {
2292 if (worktrees[i]->head_ref) {
2293 struct ref_to_worktree_entry *entry;
2294 entry = xmalloc(sizeof(*entry));
2295 entry->wt = worktrees[i];
Eric Wongd22245a2019-10-06 23:30:27 +00002296 hashmap_entry_init(&entry->ent,
2297 strhash(worktrees[i]->head_ref));
Nickolai Belakovski25820832019-04-28 22:19:42 -07002298
Eric Wongb94e5c12019-10-06 23:30:29 +00002299 hashmap_add(map, &entry->ent);
Nickolai Belakovski25820832019-04-28 22:19:42 -07002300 }
2301 }
2302}
2303
2304static void lazy_init_worktree_map(void)
2305{
2306 if (ref_to_worktree_map.worktrees)
2307 return;
2308
Eric Sunshine03f24652020-06-19 19:35:44 -04002309 ref_to_worktree_map.worktrees = get_worktrees();
Nickolai Belakovski25820832019-04-28 22:19:42 -07002310 hashmap_init(&(ref_to_worktree_map.map), ref_to_worktree_map_cmpfnc, NULL, 0);
2311 populate_worktree_map(&(ref_to_worktree_map.map), ref_to_worktree_map.worktrees);
2312}
2313
Jeff Kingfe6258c2023-02-24 01:34:44 -05002314static char *get_worktree_path(const struct ref_array_item *ref)
Nickolai Belakovski25820832019-04-28 22:19:42 -07002315{
Eric Wongf23a4652019-10-06 23:30:36 +00002316 struct hashmap_entry entry, *e;
Nickolai Belakovski25820832019-04-28 22:19:42 -07002317 struct ref_to_worktree_entry *lookup_result;
2318
2319 lazy_init_worktree_map();
2320
2321 hashmap_entry_init(&entry, strhash(ref->refname));
Eric Wongf23a4652019-10-06 23:30:36 +00002322 e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname);
Nickolai Belakovski25820832019-04-28 22:19:42 -07002323
Eric Wongf23a4652019-10-06 23:30:36 +00002324 if (!e)
Nickolai Belakovski25820832019-04-28 22:19:42 -07002325 return xstrdup("");
Eric Wongf23a4652019-10-06 23:30:36 +00002326
2327 lookup_result = container_of(e, struct ref_to_worktree_entry, ent);
2328
2329 return xstrdup(lookup_result->wt->path);
Nickolai Belakovski25820832019-04-28 22:19:42 -07002330}
2331
Karthik Nayakc95b7582015-06-14 01:07:27 +05302332/*
2333 * Parse the object referred by ref, and grab needed value.
2334 */
Olga Telezhnayae3396112018-03-29 12:49:45 +00002335static int populate_value(struct ref_array_item *ref, struct strbuf *err)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302336{
Karthik Nayakc95b7582015-06-14 01:07:27 +05302337 struct object *obj;
Olga Telezhnaya2bbc6e82018-02-21 06:59:00 +00002338 int i;
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002339 struct object_info empty = OBJECT_INFO_INIT;
Derrick Stolee49abcd22023-03-20 11:26:54 +00002340 int ahead_behind_atoms = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302341
René Scharfeca56dad2021-03-13 17:17:22 +01002342 CALLOC_ARRAY(ref->value, used_atom_cnt);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302343
2344 if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02002345 ref->symref = refs_resolve_refdup(get_main_ref_store(the_repository),
2346 ref->refname,
2347 RESOLVE_REF_READING,
2348 NULL, NULL);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302349 if (!ref->symref)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002350 ref->symref = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302351 }
2352
2353 /* Fill in specials first */
2354 for (i = 0; i < used_atom_cnt; i++) {
Karthik Nayakfd935cc2016-02-17 23:36:13 +05302355 struct used_atom *atom = &used_atom[i];
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002356 enum atom_type atom_type = atom->atom_type;
Karthik Nayakb072add2016-02-17 23:36:11 +05302357 const char *name = used_atom[i].name;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302358 struct atom_value *v = &ref->value[i];
2359 int deref = 0;
2360 const char *refname;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302361 struct branch *branch = NULL;
2362
ZheNing Hubd0708c2021-07-26 03:26:47 +00002363 v->s_size = ATOM_SIZE_UNSPECIFIED;
Karthik Nayak63d89fb2015-09-10 21:18:20 +05302364 v->handler = append_atom;
Kousik Sanagavarapu6d79cd82023-09-02 14:30:39 +05302365 v->value = 0;
Karthik Nayakc58fc852017-01-10 14:19:35 +05302366 v->atom = atom;
Karthik Nayak63d89fb2015-09-10 21:18:20 +05302367
Karthik Nayakc95b7582015-06-14 01:07:27 +05302368 if (*name == '*') {
2369 deref = 1;
2370 name++;
2371 }
2372
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002373 if (atom_type == ATOM_REFNAME)
Karthik Nayaka7984102017-01-10 14:19:44 +05302374 refname = get_refname(atom, ref);
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002375 else if (atom_type == ATOM_WORKTREEPATH) {
Nickolai Belakovski25820832019-04-28 22:19:42 -07002376 if (ref->kind == FILTER_REFS_BRANCHES)
Jeff Kingfe6258c2023-02-24 01:34:44 -05002377 v->s = get_worktree_path(ref);
Nickolai Belakovski25820832019-04-28 22:19:42 -07002378 else
2379 v->s = xstrdup("");
2380 continue;
2381 }
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002382 else if (atom_type == ATOM_SYMREF)
Karthik Nayaka7984102017-01-10 14:19:44 +05302383 refname = get_symref(atom, ref);
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002384 else if (atom_type == ATOM_UPSTREAM) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05302385 const char *branch_name;
2386 /* only local branches may have an upstream */
2387 if (!skip_prefix(ref->refname, "refs/heads/",
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002388 &branch_name)) {
2389 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302390 continue;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002391 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05302392 branch = branch_get(branch_name);
2393
2394 refname = branch_get_upstream(branch, NULL);
Karthik Nayak5339bda2016-02-17 23:36:17 +05302395 if (refname)
2396 fill_remote_ref_details(atom, refname, branch, &v->s);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002397 else
2398 v->s = xstrdup("");
Karthik Nayak5339bda2016-02-17 23:36:17 +05302399 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002400 } else if (atom_type == ATOM_PUSH && atom->u.remote_ref.push) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05302401 const char *branch_name;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002402 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302403 if (!skip_prefix(ref->refname, "refs/heads/",
2404 &branch_name))
2405 continue;
2406 branch = branch_get(branch_name);
2407
Johannes Schindelincc723852017-10-05 14:19:09 +02002408 if (atom->u.remote_ref.push_remote)
2409 refname = NULL;
2410 else {
2411 refname = branch_get_push(branch, NULL);
2412 if (!refname)
2413 continue;
2414 }
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002415 /* We will definitely re-init v->s on the next line. */
2416 free((char *)v->s);
Karthik Nayak5339bda2016-02-17 23:36:17 +05302417 fill_remote_ref_details(atom, refname, branch, &v->s);
2418 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002419 } else if (atom_type == ATOM_COLOR) {
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002420 v->s = xstrdup(atom->u.color);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302421 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002422 } else if (atom_type == ATOM_FLAG) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05302423 char buf[256], *cp = buf;
2424 if (ref->flag & REF_ISSYMREF)
2425 cp = copy_advance(cp, ",symref");
2426 if (ref->flag & REF_ISPACKED)
2427 cp = copy_advance(cp, ",packed");
2428 if (cp == buf)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002429 v->s = xstrdup("");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302430 else {
2431 *cp = '\0';
2432 v->s = xstrdup(buf + 1);
2433 }
2434 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002435 } else if (!deref && atom_type == ATOM_OBJECTNAME &&
2436 grab_oid(name, "objectname", &ref->objectname, v, atom)) {
2437 continue;
2438 } else if (atom_type == ATOM_HEAD) {
Jeff King613a0e52017-05-19 02:12:12 -04002439 if (atom->u.head && !strcmp(ref->refname, atom->u.head))
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002440 v->s = xstrdup("*");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302441 else
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002442 v->s = xstrdup(" ");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302443 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002444 } else if (atom_type == ATOM_ALIGN) {
Karthik Nayakce592082015-09-11 20:33:07 +05302445 v->handler = align_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002446 v->s = xstrdup("");
Karthik Nayakce592082015-09-11 20:33:07 +05302447 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002448 } else if (atom_type == ATOM_END) {
Karthik Nayakce592082015-09-11 20:33:07 +05302449 v->handler = end_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002450 v->s = xstrdup("");
Karthik Nayakce592082015-09-11 20:33:07 +05302451 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002452 } else if (atom_type == ATOM_IF) {
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05302453 const char *s;
Karthik Nayak4f3e3b32017-01-10 14:19:36 +05302454 if (skip_prefix(name, "if:", &s))
2455 v->s = xstrdup(s);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002456 else
2457 v->s = xstrdup("");
Karthik Nayakc58492d2017-01-10 14:19:34 +05302458 v->handler = if_atom_handler;
2459 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002460 } else if (atom_type == ATOM_THEN) {
Karthik Nayakc58492d2017-01-10 14:19:34 +05302461 v->handler = then_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002462 v->s = xstrdup("");
Karthik Nayakc58492d2017-01-10 14:19:34 +05302463 continue;
ZheNing Hu1197f1a2021-05-13 15:15:38 +00002464 } else if (atom_type == ATOM_ELSE) {
Karthik Nayakc58492d2017-01-10 14:19:34 +05302465 v->handler = else_atom_handler;
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002466 v->s = xstrdup("");
Karthik Nayakc58492d2017-01-10 14:19:34 +05302467 continue;
ZheNing Hub9dee072021-07-26 03:26:50 +00002468 } else if (atom_type == ATOM_REST) {
2469 if (ref->rest)
2470 v->s = xstrdup(ref->rest);
2471 else
2472 v->s = xstrdup("");
2473 continue;
Derrick Stolee49abcd22023-03-20 11:26:54 +00002474 } else if (atom_type == ATOM_AHEADBEHIND) {
2475 if (ref->counts) {
2476 const struct ahead_behind_count *count;
2477 count = ref->counts[ahead_behind_atoms++];
2478 v->s = xstrfmt("%d %d", count->ahead, count->behind);
2479 } else {
2480 /* Not a commit. */
2481 v->s = xstrdup("");
2482 }
2483 continue;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302484 } else
2485 continue;
2486
Karthik Nayakc95b7582015-06-14 01:07:27 +05302487 if (!deref)
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002488 v->s = xstrdup(refname);
Jeff Kinga5e03bf2015-09-24 17:07:12 -04002489 else
2490 v->s = xstrfmt("%s^{}", refname);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002491 free((char *)refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302492 }
2493
2494 for (i = 0; i < used_atom_cnt; i++) {
2495 struct atom_value *v = &ref->value[i];
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002496 if (v->s == NULL && used_atom[i].source == SOURCE_NONE)
2497 return strbuf_addf_ret(err, -1, _("missing object %s for %s"),
2498 oid_to_hex(&ref->objectname), ref->refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302499 }
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002500
2501 if (need_tagged)
2502 oi.info.contentp = &oi.content;
2503 if (!memcmp(&oi.info, &empty, sizeof(empty)) &&
2504 !memcmp(&oi_deref.info, &empty, sizeof(empty)))
Olga Telezhnayae3396112018-03-29 12:49:45 +00002505 return 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302506
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002507
2508 oi.oid = ref->objectname;
2509 if (get_object(ref, 0, &obj, &oi, err))
Olga Telezhnayae3396112018-03-29 12:49:45 +00002510 return -1;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302511
2512 /*
2513 * If there is no atom that wants to know about tagged
2514 * object, we are done.
2515 */
2516 if (!need_tagged || (obj->type != OBJ_TAG))
Olga Telezhnayae3396112018-03-29 12:49:45 +00002517 return 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302518
2519 /*
Victoria Dye188782e2023-11-14 19:53:57 +00002520 * If it is a tag object, see if we use the peeled value. If we do,
2521 * grab the peeled OID.
Karthik Nayakc95b7582015-06-14 01:07:27 +05302522 */
Patrick Steinhardt30aaff42024-05-17 10:19:04 +02002523 if (need_tagged && peel_iterated_oid(the_repository, &obj->oid, &oi_deref.oid))
Victoria Dye188782e2023-11-14 19:53:57 +00002524 die("bad tag");
Karthik Nayakc95b7582015-06-14 01:07:27 +05302525
Olga Telezhnayaaa46a0d2018-07-17 08:22:57 +00002526 return get_object(ref, 1, &obj, &oi_deref, err);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302527}
2528
2529/*
2530 * Given a ref, return the value for the atom. This lazily gets value
2531 * out of the object by calling populate value.
2532 */
Olga Telezhnayae3396112018-03-29 12:49:45 +00002533static int get_ref_atom_value(struct ref_array_item *ref, int atom,
2534 struct atom_value **v, struct strbuf *err)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302535{
2536 if (!ref->value) {
Olga Telezhnayae3396112018-03-29 12:49:45 +00002537 if (populate_value(ref, err))
2538 return -1;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302539 fill_missing_values(ref->value);
2540 }
2541 *v = &ref->value[atom];
Olga Telezhnayae3396112018-03-29 12:49:45 +00002542 return 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302543}
2544
Jeff Kinga91aca42017-03-09 08:29:49 -05002545/*
Karthik Nayakc95b7582015-06-14 01:07:27 +05302546 * Return 1 if the refname matches one of the patterns, otherwise 0.
Karthik Nayakbef0e122015-09-10 21:18:26 +05302547 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
2548 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
2549 * matches "refs/heads/mas*", too).
2550 */
Jeff King284c55d2023-07-10 17:12:16 -04002551static int match_pattern(const char **patterns, const char *refname,
2552 int ignore_case)
Karthik Nayakbef0e122015-09-10 21:18:26 +05302553{
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002554 unsigned flags = 0;
2555
Jeff King284c55d2023-07-10 17:12:16 -04002556 if (ignore_case)
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002557 flags |= WM_CASEFOLD;
2558
Karthik Nayakbef0e122015-09-10 21:18:26 +05302559 /*
2560 * When no '--format' option is given we need to skip the prefix
2561 * for matching refs of tags and branches.
2562 */
2563 (void)(skip_prefix(refname, "refs/tags/", &refname) ||
2564 skip_prefix(refname, "refs/heads/", &refname) ||
2565 skip_prefix(refname, "refs/remotes/", &refname) ||
2566 skip_prefix(refname, "refs/", &refname));
2567
2568 for (; *patterns; patterns++) {
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +00002569 if (!wildmatch(*patterns, refname, flags))
Karthik Nayakbef0e122015-09-10 21:18:26 +05302570 return 1;
2571 }
2572 return 0;
2573}
2574
2575/*
2576 * Return 1 if the refname matches one of the patterns, otherwise 0.
Karthik Nayakc95b7582015-06-14 01:07:27 +05302577 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
Karthik Nayakbef0e122015-09-10 21:18:26 +05302578 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
2579 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
Karthik Nayakc95b7582015-06-14 01:07:27 +05302580 */
Jeff King284c55d2023-07-10 17:12:16 -04002581static int match_name_as_path(const char **pattern, const char *refname,
2582 int ignore_case)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302583{
2584 int namelen = strlen(refname);
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002585 unsigned flags = WM_PATHNAME;
2586
Jeff King284c55d2023-07-10 17:12:16 -04002587 if (ignore_case)
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07002588 flags |= WM_CASEFOLD;
2589
Karthik Nayakc95b7582015-06-14 01:07:27 +05302590 for (; *pattern; pattern++) {
2591 const char *p = *pattern;
2592 int plen = strlen(p);
2593
2594 if ((plen <= namelen) &&
2595 !strncmp(refname, p, plen) &&
2596 (refname[plen] == '\0' ||
2597 refname[plen] == '/' ||
2598 p[plen-1] == '/'))
2599 return 1;
Aleksandr Makarov639ab5e2018-07-02 17:11:59 -04002600 if (!wildmatch(p, refname, flags))
Karthik Nayakc95b7582015-06-14 01:07:27 +05302601 return 1;
2602 }
2603 return 0;
2604}
2605
Karthik Nayakbef0e122015-09-10 21:18:26 +05302606/* Return 1 if the refname matches one of the patterns, otherwise 0. */
2607static int filter_pattern_match(struct ref_filter *filter, const char *refname)
2608{
2609 if (!*filter->name_patterns)
2610 return 1; /* No pattern always matches */
2611 if (filter->match_as_path)
Jeff King284c55d2023-07-10 17:12:16 -04002612 return match_name_as_path(filter->name_patterns, refname,
2613 filter->ignore_case);
2614 return match_pattern(filter->name_patterns, refname,
2615 filter->ignore_case);
Karthik Nayakbef0e122015-09-10 21:18:26 +05302616}
2617
Taylor Blau8255dd82023-07-10 17:12:19 -04002618static int filter_exclude_match(struct ref_filter *filter, const char *refname)
2619{
2620 if (!filter->exclude.nr)
2621 return 0;
2622 if (filter->match_as_path)
2623 return match_name_as_path(filter->exclude.v, refname,
2624 filter->ignore_case);
2625 return match_pattern(filter->exclude.v, refname, filter->ignore_case);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302626}
2627
Jeff Kingcfe004a2017-05-22 16:17:54 +02002628/*
2629 * This is the same as for_each_fullref_in(), but it tries to iterate
2630 * only over the patterns we'll care about. Note that it _doesn't_ do a full
2631 * pattern match, so the callback still has to match each ref individually.
2632 */
2633static int for_each_fullref_in_pattern(struct ref_filter *filter,
2634 each_ref_fn cb,
Jeff King67985e42021-09-24 14:48:48 -04002635 void *cb_data)
Jeff Kingcfe004a2017-05-22 16:17:54 +02002636{
Patrick Steinhardtf1701f22024-05-15 08:51:05 +02002637 if (filter->kind & FILTER_REFS_ROOT_REFS) {
Karthik Nayak33d15b52024-02-23 11:01:12 +01002638 /* In this case, we want to print all refs including root refs. */
2639 return refs_for_each_include_root_refs(get_main_ref_store(the_repository),
2640 cb, cb_data);
2641 }
2642
Jeff Kingcfe004a2017-05-22 16:17:54 +02002643 if (!filter->match_as_path) {
2644 /*
2645 * in this case, the patterns are applied after
2646 * prefixes like "refs/heads/" etc. are stripped off,
2647 * so we have to look at everything:
2648 */
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02002649 return refs_for_each_fullref_in(get_main_ref_store(the_repository),
2650 "", NULL, cb, cb_data);
Jeff Kingcfe004a2017-05-22 16:17:54 +02002651 }
2652
Jeff Kinge674eb22018-07-02 17:12:42 -04002653 if (filter->ignore_case) {
2654 /*
2655 * we can't handle case-insensitive comparisons,
2656 * so just return everything and let the caller
2657 * sort it out.
2658 */
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02002659 return refs_for_each_fullref_in(get_main_ref_store(the_repository),
2660 "", NULL, cb, cb_data);
Jeff Kinge674eb22018-07-02 17:12:42 -04002661 }
2662
Jeff Kingcfe004a2017-05-22 16:17:54 +02002663 if (!filter->name_patterns[0]) {
2664 /* no patterns; we have to look at everything */
Taylor Blau59c35fa2023-07-10 17:12:28 -04002665 return refs_for_each_fullref_in(get_main_ref_store(the_repository),
2666 "", filter->exclude.v, cb, cb_data);
Jeff Kingcfe004a2017-05-22 16:17:54 +02002667 }
2668
Jeff King91e2ab12022-12-13 06:11:10 -05002669 return refs_for_each_fullref_in_prefixes(get_main_ref_store(the_repository),
2670 NULL, filter->name_patterns,
Taylor Blau59c35fa2023-07-10 17:12:28 -04002671 filter->exclude.v,
Jeff King91e2ab12022-12-13 06:11:10 -05002672 cb, cb_data);
Jeff Kingcfe004a2017-05-22 16:17:54 +02002673}
2674
2675/*
Jeff Kingc79eddf2020-03-30 10:04:11 -04002676 * Given a ref (oid, refname), check if the ref belongs to the array
2677 * of oids. If the given ref is a tag, check if the given tag points
Jeff Kingd9e00622023-07-02 18:38:29 -04002678 * at one of the oids in the given oid array. Returns non-zero if a
2679 * match is found.
2680 *
Karthik Nayak68411042015-07-07 21:36:09 +05302681 * NEEDSWORK:
Jan Klötzke468887f2023-07-01 22:57:02 +02002682 * As the refs are cached we might know what refname peels to without
Karthik Nayak68411042015-07-07 21:36:09 +05302683 * the need to parse the object via parse_object(). peel_ref() might be a
2684 * more efficient alternative to obtain the pointee.
2685 */
Jeff Kingd9e00622023-07-02 18:38:29 -04002686static int match_points_at(struct oid_array *points_at,
2687 const struct object_id *oid,
2688 const char *refname)
Karthik Nayak68411042015-07-07 21:36:09 +05302689{
Karthik Nayak68411042015-07-07 21:36:09 +05302690 struct object *obj;
2691
brian m. carlson910650d2017-03-31 01:40:00 +00002692 if (oid_array_lookup(points_at, oid) >= 0)
Jeff Kingd9e00622023-07-02 18:38:29 -04002693 return 1;
Jeff King870eb532023-07-02 18:37:47 -04002694 obj = parse_object_with_flags(the_repository, oid,
2695 PARSE_OBJECT_SKIP_HASH_CHECK);
Jan Klötzke468887f2023-07-01 22:57:02 +02002696 while (obj && obj->type == OBJ_TAG) {
Jeff Kingb9584c52023-07-02 18:35:40 -04002697 struct tag *tag = (struct tag *)obj;
2698
2699 if (parse_tag(tag) < 0) {
2700 obj = NULL;
2701 break;
2702 }
2703
Jeff Kingd9e00622023-07-02 18:38:29 -04002704 if (oid_array_lookup(points_at, get_tagged_oid(tag)) >= 0)
2705 return 1;
Jeff Kingb9584c52023-07-02 18:35:40 -04002706
2707 obj = tag->tagged;
Jan Klötzke468887f2023-07-01 22:57:02 +02002708 }
Karthik Nayak68411042015-07-07 21:36:09 +05302709 if (!obj)
2710 die(_("malformed object at '%s'"), refname);
Jeff Kingd9e00622023-07-02 18:38:29 -04002711 return 0;
Karthik Nayak68411042015-07-07 21:36:09 +05302712}
2713
Jeff King0ffaa002018-04-06 14:59:26 -04002714/*
2715 * Allocate space for a new ref_array_item and copy the name and oid to it.
2716 *
2717 * Callers can then fill in other struct members at their leisure.
2718 */
Karthik Nayakc95b7582015-06-14 01:07:27 +05302719static struct ref_array_item *new_ref_array_item(const char *refname,
Jeff King0ffaa002018-04-06 14:59:26 -04002720 const struct object_id *oid)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302721{
Jeff King96ffc062016-02-22 17:44:32 -05002722 struct ref_array_item *ref;
Jeff King0ffaa002018-04-06 14:59:26 -04002723
Jeff King96ffc062016-02-22 17:44:32 -05002724 FLEX_ALLOC_STR(ref, refname, refname);
Jeff King53df97a2018-04-06 14:58:32 -04002725 oidcpy(&ref->objectname, oid);
ZheNing Hub9dee072021-07-26 03:26:50 +00002726 ref->rest = NULL;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302727
2728 return ref;
2729}
2730
Victoria Dye613d9912023-11-14 19:53:54 +00002731static void ref_array_append(struct ref_array *array, struct ref_array_item *ref)
2732{
2733 ALLOC_GROW(array->items, array->nr + 1, array->alloc);
2734 array->items[array->nr++] = ref;
2735}
2736
Jeff King427cbc92018-04-06 14:59:45 -04002737struct ref_array_item *ref_array_push(struct ref_array *array,
2738 const char *refname,
2739 const struct object_id *oid)
2740{
2741 struct ref_array_item *ref = new_ref_array_item(refname, oid);
Victoria Dye613d9912023-11-14 19:53:54 +00002742 ref_array_append(array, ref);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302743 return ref;
2744}
2745
Lukas Puehringer2111aa72017-01-17 18:37:19 -05002746static int ref_kind_from_refname(const char *refname)
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302747{
2748 unsigned int i;
2749
2750 static struct {
2751 const char *prefix;
2752 unsigned int kind;
2753 } ref_kind[] = {
2754 { "refs/heads/" , FILTER_REFS_BRANCHES },
2755 { "refs/remotes/" , FILTER_REFS_REMOTES },
2756 { "refs/tags/", FILTER_REFS_TAGS}
2757 };
2758
Lukas Puehringer2111aa72017-01-17 18:37:19 -05002759 if (!strcmp(refname, "HEAD"))
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302760 return FILTER_REFS_DETACHED_HEAD;
2761
2762 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
2763 if (starts_with(refname, ref_kind[i].prefix))
2764 return ref_kind[i].kind;
2765 }
2766
Patrick Steinhardtf1701f22024-05-15 08:51:05 +02002767 if (is_pseudo_ref(refname))
Karthik Nayak33d15b52024-02-23 11:01:12 +01002768 return FILTER_REFS_PSEUDOREFS;
Patrick Steinhardtf1701f22024-05-15 08:51:05 +02002769 if (is_root_ref(refname))
2770 return FILTER_REFS_ROOT_REFS;
Karthik Nayak33d15b52024-02-23 11:01:12 +01002771
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302772 return FILTER_REFS_OTHERS;
2773}
2774
Lukas Puehringer2111aa72017-01-17 18:37:19 -05002775static int filter_ref_kind(struct ref_filter *filter, const char *refname)
2776{
2777 if (filter->kind == FILTER_REFS_BRANCHES ||
2778 filter->kind == FILTER_REFS_REMOTES ||
2779 filter->kind == FILTER_REFS_TAGS)
2780 return filter->kind;
2781 return ref_kind_from_refname(refname);
2782}
2783
Victoria Dye613d9912023-11-14 19:53:54 +00002784static struct ref_array_item *apply_ref_filter(const char *refname, const struct object_id *oid,
2785 int flag, struct ref_filter *filter)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302786{
Karthik Nayakc95b7582015-06-14 01:07:27 +05302787 struct ref_array_item *ref;
Karthik Nayak35257aa2015-07-07 21:36:12 +05302788 struct commit *commit = NULL;
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302789 unsigned int kind;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302790
2791 if (flag & REF_BAD_NAME) {
Nguyễn Thái Ngọc Duy1823c612016-02-27 13:42:04 +07002792 warning(_("ignoring ref with broken name %s"), refname);
Victoria Dye613d9912023-11-14 19:53:54 +00002793 return NULL;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302794 }
2795
Junio C Hamano7ebc8cb2015-08-03 11:01:10 -07002796 if (flag & REF_ISBROKEN) {
Nguyễn Thái Ngọc Duy1823c612016-02-27 13:42:04 +07002797 warning(_("ignoring broken ref %s"), refname);
Victoria Dye613d9912023-11-14 19:53:54 +00002798 return NULL;
Junio C Hamano7ebc8cb2015-08-03 11:01:10 -07002799 }
2800
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302801 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2802 kind = filter_ref_kind(filter, refname);
Karthik Nayak33d15b52024-02-23 11:01:12 +01002803
2804 /*
2805 * Generally HEAD refs are printed with special description denoting a rebase,
2806 * detached state and so forth. This is useful when only printing the HEAD ref
Patrick Steinhardtf1701f22024-05-15 08:51:05 +02002807 * But when it is being printed along with other root refs, it makes sense to
2808 * keep the formatting consistent. So we mask the type to act like a root ref.
Karthik Nayak33d15b52024-02-23 11:01:12 +01002809 */
Patrick Steinhardtf1701f22024-05-15 08:51:05 +02002810 if (filter->kind & FILTER_REFS_ROOT_REFS && kind == FILTER_REFS_DETACHED_HEAD)
2811 kind = FILTER_REFS_ROOT_REFS;
Karthik Nayak33d15b52024-02-23 11:01:12 +01002812 else if (!(kind & filter->kind))
Victoria Dye613d9912023-11-14 19:53:54 +00002813 return NULL;
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302814
Karthik Nayakbef0e122015-09-10 21:18:26 +05302815 if (!filter_pattern_match(filter, refname))
Victoria Dye613d9912023-11-14 19:53:54 +00002816 return NULL;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302817
Taylor Blau8255dd82023-07-10 17:12:19 -04002818 if (filter_exclude_match(filter, refname))
Victoria Dye613d9912023-11-14 19:53:54 +00002819 return NULL;
Taylor Blau8255dd82023-07-10 17:12:19 -04002820
brian m. carlson4ce36212017-03-31 01:39:57 +00002821 if (filter->points_at.nr && !match_points_at(&filter->points_at, oid, refname))
Victoria Dye613d9912023-11-14 19:53:54 +00002822 return NULL;
Karthik Nayak68411042015-07-07 21:36:09 +05302823
Karthik Nayakc95b7582015-06-14 01:07:27 +05302824 /*
Karthik Nayak35257aa2015-07-07 21:36:12 +05302825 * A merge filter is applied on refs pointing to commits. Hence
2826 * obtain the commit using the 'oid' available and discard all
2827 * non-commits early. The actual filtering is done later.
2828 */
Aaron Lipman21bf9332020-09-15 22:08:40 -04002829 if (filter->reachable_from || filter->unreachable_from ||
2830 filter->with_commit || filter->no_commit || filter->verbose) {
2831 commit = lookup_commit_reference_gently(the_repository, oid, 1);
Karthik Nayak35257aa2015-07-07 21:36:12 +05302832 if (!commit)
Victoria Dye613d9912023-11-14 19:53:54 +00002833 return NULL;
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +00002834 /* We perform the filtering for the '--contains' option... */
Karthik Nayakee2bd062015-07-07 21:36:16 +05302835 if (filter->with_commit &&
Victoria Dye613d9912023-11-14 19:53:54 +00002836 !commit_contains(filter, commit, filter->with_commit, &filter->internal.contains_cache))
2837 return NULL;
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +00002838 /* ...or for the `--no-contains' option */
2839 if (filter->no_commit &&
Victoria Dye613d9912023-11-14 19:53:54 +00002840 commit_contains(filter, commit, filter->no_commit, &filter->internal.no_contains_cache))
2841 return NULL;
Karthik Nayak35257aa2015-07-07 21:36:12 +05302842 }
2843
Karthik Nayakc95b7582015-06-14 01:07:27 +05302844 /*
2845 * We do not open the object yet; sort may only need refname
2846 * to do its job and the resulting list may yet to be pruned
2847 * by maxcount logic.
2848 */
Victoria Dye613d9912023-11-14 19:53:54 +00002849 ref = new_ref_array_item(refname, oid);
Karthik Nayak35257aa2015-07-07 21:36:12 +05302850 ref->commit = commit;
Jeff King0ffaa002018-04-06 14:59:26 -04002851 ref->flag = flag;
Karthik Nayak5b4f2852015-09-10 21:18:23 +05302852 ref->kind = kind;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302853
Victoria Dye613d9912023-11-14 19:53:54 +00002854 return ref;
2855}
2856
Karthik Nayakc95b7582015-06-14 01:07:27 +05302857struct ref_filter_cbdata {
2858 struct ref_array *array;
2859 struct ref_filter *filter;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302860};
2861
2862/*
2863 * A call-back given to for_each_ref(). Filter refs and keep them for
2864 * later object processing.
2865 */
Victoria Dye9c575b02023-11-14 19:53:53 +00002866static int filter_one(const char *refname, const struct object_id *oid, int flag, void *cb_data)
Karthik Nayakc95b7582015-06-14 01:07:27 +05302867{
2868 struct ref_filter_cbdata *ref_cbdata = cb_data;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302869 struct ref_array_item *ref;
Karthik Nayakc95b7582015-06-14 01:07:27 +05302870
Victoria Dye613d9912023-11-14 19:53:54 +00002871 ref = apply_ref_filter(refname, oid, flag, ref_cbdata->filter);
2872 if (ref)
2873 ref_array_append(ref_cbdata->array, ref);
Karthik Nayak14de7fb2015-06-14 01:07:28 +05302874
Karthik Nayakc95b7582015-06-14 01:07:27 +05302875 return 0;
2876}
2877
2878/* Free memory allocated for a ref_array_item */
2879static void free_array_item(struct ref_array_item *item)
2880{
2881 free((char *)item->symref);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002882 if (item->value) {
Martin Ågren14d30cd2019-07-10 20:36:39 +02002883 int i;
2884 for (i = 0; i < used_atom_cnt; i++)
2885 free((char *)item->value[i].s);
Olga Telezhnayaf0062d32018-10-18 07:28:54 +00002886 free(item->value);
2887 }
Derrick Stolee49abcd22023-03-20 11:26:54 +00002888 free(item->counts);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302889 free(item);
2890}
2891
Victoria Dyebd98f972023-11-14 19:53:55 +00002892struct ref_filter_and_format_cbdata {
2893 struct ref_filter *filter;
2894 struct ref_format *format;
2895
2896 struct ref_filter_and_format_internal {
2897 int count;
2898 } internal;
2899};
2900
2901static int filter_and_format_one(const char *refname, const struct object_id *oid, int flag, void *cb_data)
2902{
2903 struct ref_filter_and_format_cbdata *ref_cbdata = cb_data;
2904 struct ref_array_item *ref;
2905 struct strbuf output = STRBUF_INIT, err = STRBUF_INIT;
2906
2907 ref = apply_ref_filter(refname, oid, flag, ref_cbdata->filter);
2908 if (!ref)
2909 return 0;
2910
2911 if (format_ref_array_item(ref, ref_cbdata->format, &output, &err))
2912 die("%s", err.buf);
2913
2914 if (output.len || !ref_cbdata->format->array_opts.omit_empty) {
2915 fwrite(output.buf, 1, output.len, stdout);
2916 putchar('\n');
2917 }
2918
2919 strbuf_release(&output);
2920 strbuf_release(&err);
2921 free_array_item(ref);
2922
2923 /*
2924 * Increment the running count of refs that match the filter. If
2925 * max_count is set and we've reached the max, stop the ref
2926 * iteration by returning a nonzero value.
2927 */
2928 if (ref_cbdata->format->array_opts.max_count &&
2929 ++ref_cbdata->internal.count >= ref_cbdata->format->array_opts.max_count)
2930 return 1;
2931
2932 return 0;
2933}
2934
Karthik Nayakc95b7582015-06-14 01:07:27 +05302935/* Free all memory allocated for ref_array */
2936void ref_array_clear(struct ref_array *array)
2937{
2938 int i;
2939
2940 for (i = 0; i < array->nr; i++)
2941 free_array_item(array->items[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +00002942 FREE_AND_NULL(array->items);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302943 array->nr = array->alloc = 0;
Martin Ågren14d30cd2019-07-10 20:36:39 +02002944
Andrzej Huntd7cf4182021-07-25 15:08:24 +02002945 for (i = 0; i < used_atom_cnt; i++) {
2946 struct used_atom *atom = &used_atom[i];
2947 if (atom->atom_type == ATOM_HEAD)
2948 free(atom->u.head);
2949 free((char *)atom->name);
2950 }
Martin Ågren14d30cd2019-07-10 20:36:39 +02002951 FREE_AND_NULL(used_atom);
2952 used_atom_cnt = 0;
2953
Nickolai Belakovski25820832019-04-28 22:19:42 -07002954 if (ref_to_worktree_map.worktrees) {
Elijah Newren6da1a252020-11-02 18:55:05 +00002955 hashmap_clear_and_free(&(ref_to_worktree_map.map),
Eric Wongc8e424c2019-10-06 23:30:40 +00002956 struct ref_to_worktree_entry, ent);
Nickolai Belakovski25820832019-04-28 22:19:42 -07002957 free_worktrees(ref_to_worktree_map.worktrees);
2958 ref_to_worktree_map.worktrees = NULL;
2959 }
Derrick Stolee49abcd22023-03-20 11:26:54 +00002960
2961 FREE_AND_NULL(array->counts);
Karthik Nayakc95b7582015-06-14 01:07:27 +05302962}
2963
Aaron Lipmana1b19aa2020-09-18 17:58:41 -04002964#define EXCLUDE_REACHED 0
2965#define INCLUDE_REACHED 1
2966static void reach_filter(struct ref_array *array,
Jeff King311bfe12023-07-10 17:12:10 -04002967 struct commit_list **check_reachable,
Aaron Lipmana1b19aa2020-09-18 17:58:41 -04002968 int include_reached)
Karthik Nayak35257aa2015-07-07 21:36:12 +05302969{
Karthik Nayak35257aa2015-07-07 21:36:12 +05302970 int i, old_nr;
René Scharfe5336d502020-09-26 10:37:29 +02002971 struct commit **to_clear;
Aaron Lipman21bf9332020-09-15 22:08:40 -04002972
Jeff King311bfe12023-07-10 17:12:10 -04002973 if (!*check_reachable)
Aaron Lipman21bf9332020-09-15 22:08:40 -04002974 return;
Karthik Nayak35257aa2015-07-07 21:36:12 +05302975
René Scharfeca56dad2021-03-13 17:17:22 +01002976 CALLOC_ARRAY(to_clear, array->nr);
Karthik Nayak35257aa2015-07-07 21:36:12 +05302977 for (i = 0; i < array->nr; i++) {
2978 struct ref_array_item *item = array->items[i];
Karthik Nayak35257aa2015-07-07 21:36:12 +05302979 to_clear[i] = item->commit;
2980 }
2981
Derrick Stoleecbfe3602023-03-20 11:26:55 +00002982 tips_reachable_from_bases(the_repository,
Jeff King311bfe12023-07-10 17:12:10 -04002983 *check_reachable,
Derrick Stoleecbfe3602023-03-20 11:26:55 +00002984 to_clear, array->nr,
2985 UNINTERESTING);
Karthik Nayak35257aa2015-07-07 21:36:12 +05302986
2987 old_nr = array->nr;
2988 array->nr = 0;
2989
2990 for (i = 0; i < old_nr; i++) {
2991 struct ref_array_item *item = array->items[i];
2992 struct commit *commit = item->commit;
2993
2994 int is_merged = !!(commit->object.flags & UNINTERESTING);
2995
Aaron Lipmana1b19aa2020-09-18 17:58:41 -04002996 if (is_merged == include_reached)
Karthik Nayak35257aa2015-07-07 21:36:12 +05302997 array->items[array->nr++] = array->items[i];
2998 else
2999 free_array_item(item);
3000 }
3001
René Scharfe5dee6d62017-12-25 18:44:12 +01003002 clear_commit_marks_many(old_nr, to_clear, ALL_REV_FLAGS);
Aaron Lipman21bf9332020-09-15 22:08:40 -04003003
Jeff King311bfe12023-07-10 17:12:10 -04003004 while (*check_reachable) {
3005 struct commit *merge_commit = pop_commit(check_reachable);
Aaron Lipman21bf9332020-09-15 22:08:40 -04003006 clear_commit_marks(merge_commit, ALL_REV_FLAGS);
3007 }
3008
Karthik Nayak35257aa2015-07-07 21:36:12 +05303009 free(to_clear);
3010}
3011
Derrick Stolee49abcd22023-03-20 11:26:54 +00003012void filter_ahead_behind(struct repository *r,
3013 struct ref_format *format,
3014 struct ref_array *array)
3015{
3016 struct commit **commits;
3017 size_t commits_nr = format->bases.nr + array->nr;
3018
3019 if (!format->bases.nr || !array->nr)
3020 return;
3021
3022 ALLOC_ARRAY(commits, commits_nr);
3023 for (size_t i = 0; i < format->bases.nr; i++)
3024 commits[i] = format->bases.items[i].util;
3025
3026 ALLOC_ARRAY(array->counts, st_mult(format->bases.nr, array->nr));
3027
3028 commits_nr = format->bases.nr;
3029 array->counts_nr = 0;
3030 for (size_t i = 0; i < array->nr; i++) {
3031 const char *name = array->items[i]->refname;
3032 commits[commits_nr] = lookup_commit_reference_by_name(name);
3033
3034 if (!commits[commits_nr])
3035 continue;
3036
3037 CALLOC_ARRAY(array->items[i]->counts, format->bases.nr);
3038 for (size_t j = 0; j < format->bases.nr; j++) {
3039 struct ahead_behind_count *count;
3040 count = &array->counts[array->counts_nr++];
3041 count->tip_index = commits_nr;
3042 count->base_index = j;
3043
3044 array->items[i]->counts[j] = count;
3045 }
3046 commits_nr++;
3047 }
3048
3049 ahead_behind(r, commits, commits_nr, array->counts, array->counts_nr);
3050 free(commits);
3051}
3052
Victoria Dye613d9912023-11-14 19:53:54 +00003053static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref_fn fn, void *cb_data)
Karthik Nayak14de7fb2015-06-14 01:07:28 +05303054{
Karthik Nayak35257aa2015-07-07 21:36:12 +05303055 int ret = 0;
Karthik Nayak14de7fb2015-06-14 01:07:28 +05303056
Karthik Nayak5b4f2852015-09-10 21:18:23 +05303057 filter->kind = type & FILTER_REFS_KIND_MASK;
3058
Victoria Dye6d6e5c52023-11-14 19:53:51 +00003059 init_contains_cache(&filter->internal.contains_cache);
3060 init_contains_cache(&filter->internal.no_contains_cache);
Jeff Kinga91aca42017-03-09 08:29:49 -05003061
Karthik Nayak35257aa2015-07-07 21:36:12 +05303062 /* Simple per-ref filtering */
Karthik Nayak5b4f2852015-09-10 21:18:23 +05303063 if (!filter->kind)
Karthik Nayak14de7fb2015-06-14 01:07:28 +05303064 die("filter_refs: invalid type");
Karthik Nayak5b4f2852015-09-10 21:18:23 +05303065 else {
3066 /*
3067 * For common cases where we need only branches or remotes or tags,
3068 * we only iterate through those refs. If a mix of refs is needed,
3069 * we iterate over all refs and filter out required refs with the help
3070 * of filter_ref_kind().
3071 */
3072 if (filter->kind == FILTER_REFS_BRANCHES)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02003073 ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
3074 "refs/heads/", NULL,
3075 fn, cb_data);
Karthik Nayak5b4f2852015-09-10 21:18:23 +05303076 else if (filter->kind == FILTER_REFS_REMOTES)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02003077 ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
3078 "refs/remotes/", NULL,
3079 fn, cb_data);
Karthik Nayak5b4f2852015-09-10 21:18:23 +05303080 else if (filter->kind == FILTER_REFS_TAGS)
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02003081 ret = refs_for_each_fullref_in(get_main_ref_store(the_repository),
3082 "refs/tags/", NULL, fn,
3083 cb_data);
Karthik Nayak810f7a12024-02-23 11:01:11 +01003084 else if (filter->kind & FILTER_REFS_REGULAR)
Victoria Dye613d9912023-11-14 19:53:54 +00003085 ret = for_each_fullref_in_pattern(filter, fn, cb_data);
Karthik Nayak33d15b52024-02-23 11:01:12 +01003086
3087 /*
3088 * When printing all ref types, HEAD is already included,
3089 * so we don't want to print HEAD again.
3090 */
Patrick Steinhardtf1701f22024-05-15 08:51:05 +02003091 if (!ret && !(filter->kind & FILTER_REFS_ROOT_REFS) &&
Karthik Nayak33d15b52024-02-23 11:01:12 +01003092 (filter->kind & FILTER_REFS_DETACHED_HEAD))
Patrick Steinhardt2e5c4752024-05-07 09:11:53 +02003093 refs_head_ref(get_main_ref_store(the_repository), fn,
3094 cb_data);
Karthik Nayak5b4f2852015-09-10 21:18:23 +05303095 }
3096
Victoria Dye6d6e5c52023-11-14 19:53:51 +00003097 clear_contains_cache(&filter->internal.contains_cache);
3098 clear_contains_cache(&filter->internal.no_contains_cache);
Karthik Nayak35257aa2015-07-07 21:36:12 +05303099
Victoria Dye613d9912023-11-14 19:53:54 +00003100 return ret;
3101}
3102
Karthik Nayakb072add2016-02-17 23:36:11 +05303103/*
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07003104 * API for filtering a set of refs. Based on the type of refs the user
Karthik Nayakc95b7582015-06-14 01:07:27 +05303105 * has requested, we iterate through those refs and apply filters
3106 * as per the given ref_filter structure and finally store the
3107 * filtered refs in the ref_array structure.
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07003108 */
Karthik Nayak90c00402015-09-10 21:18:25 +05303109int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type)
3110{
3111 struct ref_filter_cbdata ref_cbdata;
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07003112 int save_commit_buffer_orig;
Karthik Nayak90c00402015-09-10 21:18:25 +05303113 int ret = 0;
Johannes Schindeline467dc12017-04-20 22:52:09 +02003114
Karthik Nayakc95b7582015-06-14 01:07:27 +05303115 ref_cbdata.array = array;
Johannes Schindeline467dc12017-04-20 22:52:09 +02003116 ref_cbdata.filter = filter;
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +07003117
Karthik Nayakc95b7582015-06-14 01:07:27 +05303118 save_commit_buffer_orig = save_commit_buffer;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303119 save_commit_buffer = 0;
Karthik Nayak90c00402015-09-10 21:18:25 +05303120
Victoria Dye613d9912023-11-14 19:53:54 +00003121 ret = do_filter_refs(filter, type, filter_one, &ref_cbdata);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303122
3123 /* Filters that need revision walking */
Jeff King311bfe12023-07-10 17:12:10 -04003124 reach_filter(array, &filter->reachable_from, INCLUDE_REACHED);
3125 reach_filter(array, &filter->unreachable_from, EXCLUDE_REACHED);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303126
Jeff King359b01c2022-07-11 10:48:06 -04003127 save_commit_buffer = save_commit_buffer_orig;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303128 return ret;
3129}
3130
Victoria Dyebd98f972023-11-14 19:53:55 +00003131static inline int can_do_iterative_format(struct ref_filter *filter,
3132 struct ref_sorting *sorting,
3133 struct ref_format *format)
3134{
3135 /*
3136 * Filtering & formatting results within a single ref iteration
3137 * callback is not compatible with options that require
3138 * post-processing a filtered ref_array. These include:
3139 * - filtering on reachability
3140 * - sorting the filtered results
3141 * - including ahead-behind information in the formatted output
3142 */
3143 return !(filter->reachable_from ||
3144 filter->unreachable_from ||
3145 sorting ||
3146 format->bases.nr);
3147}
3148
Victoria Dyee7574b02023-11-14 19:53:52 +00003149void filter_and_format_refs(struct ref_filter *filter, unsigned int type,
3150 struct ref_sorting *sorting,
3151 struct ref_format *format)
3152{
Victoria Dyebd98f972023-11-14 19:53:55 +00003153 if (can_do_iterative_format(filter, sorting, format)) {
3154 int save_commit_buffer_orig;
3155 struct ref_filter_and_format_cbdata ref_cbdata = {
3156 .filter = filter,
3157 .format = format,
3158 };
3159
3160 save_commit_buffer_orig = save_commit_buffer;
3161 save_commit_buffer = 0;
3162
3163 do_filter_refs(filter, type, filter_and_format_one, &ref_cbdata);
3164
3165 save_commit_buffer = save_commit_buffer_orig;
3166 } else {
3167 struct ref_array array = { 0 };
3168 filter_refs(&array, filter, type);
3169 filter_ahead_behind(the_repository, format, &array);
3170 ref_array_sort(sorting, &array);
3171 print_formatted_ref_array(&array, format);
3172 ref_array_clear(&array);
3173 }
Victoria Dyee7574b02023-11-14 19:53:52 +00003174}
3175
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01003176static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b)
3177{
3178 if (!(a->kind ^ b->kind))
3179 BUG("ref_kind_from_refname() should only mark one ref as HEAD");
3180 if (a->kind & FILTER_REFS_DETACHED_HEAD)
3181 return -1;
3182 else if (b->kind & FILTER_REFS_DETACHED_HEAD)
3183 return 1;
3184 BUG("should have died in the xor check above");
3185 return 0;
3186}
3187
ZheNing Hubd0708c2021-07-26 03:26:47 +00003188static int memcasecmp(const void *vs1, const void *vs2, size_t n)
3189{
3190 const char *s1 = vs1, *s2 = vs2;
3191 const char *end = s1 + n;
3192
3193 for (; s1 < end; s1++, s2++) {
3194 int diff = tolower(*s1) - tolower(*s2);
3195 if (diff)
3196 return diff;
3197 }
3198 return 0;
3199}
3200
Junio C Hamano98e7ab62021-10-20 12:23:53 -07003201struct ref_sorting {
3202 struct ref_sorting *next;
3203 int atom; /* index into used_atom array (internal) */
3204 enum ref_sorting_order sort_flags;
3205};
3206
Karthik Nayakc95b7582015-06-14 01:07:27 +05303207static int cmp_ref_sorting(struct ref_sorting *s, struct ref_array_item *a, struct ref_array_item *b)
3208{
3209 struct atom_value *va, *vb;
3210 int cmp;
Ævar Arnfjörð Bjarmason4045f652021-01-07 10:51:53 +01003211 int cmp_detached_head = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303212 cmp_type cmp_type = used_atom[s->atom].type;
Olga Telezhnayae3396112018-03-29 12:49:45 +00003213 struct strbuf err = STRBUF_INIT;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303214
Olga Telezhnayae3396112018-03-29 12:49:45 +00003215 if (get_ref_atom_value(a, s->atom, &va, &err))
3216 die("%s", err.buf);
3217 if (get_ref_atom_value(b, s->atom, &vb, &err))
3218 die("%s", err.buf);
3219 strbuf_release(&err);
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01003220 if (s->sort_flags & REF_SORTING_DETACHED_HEAD_FIRST &&
3221 ((a->kind | b->kind) & FILTER_REFS_DETACHED_HEAD)) {
3222 cmp = compare_detached_head(a, b);
Ævar Arnfjörð Bjarmason4045f652021-01-07 10:51:53 +01003223 cmp_detached_head = 1;
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +01003224 } else if (s->sort_flags & REF_SORTING_VERSION) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05303225 cmp = versioncmp(va->s, vb->s);
Ævar Arnfjörð Bjarmason75c50e52021-01-07 10:51:49 +01003226 } else if (cmp_type == FIELD_STR) {
ZheNing Hubd0708c2021-07-26 03:26:47 +00003227 if (va->s_size < 0 && vb->s_size < 0) {
3228 int (*cmp_fn)(const char *, const char *);
3229 cmp_fn = s->sort_flags & REF_SORTING_ICASE
3230 ? strcasecmp : strcmp;
3231 cmp = cmp_fn(va->s, vb->s);
3232 } else {
3233 size_t a_size = va->s_size < 0 ?
3234 strlen(va->s) : va->s_size;
3235 size_t b_size = vb->s_size < 0 ?
3236 strlen(vb->s) : vb->s_size;
3237 int (*cmp_fn)(const void *, const void *, size_t);
3238 cmp_fn = s->sort_flags & REF_SORTING_ICASE
3239 ? memcasecmp : memcmp;
3240
3241 cmp = cmp_fn(va->s, vb->s, b_size > a_size ?
3242 a_size : b_size);
3243 if (!cmp) {
3244 if (a_size > b_size)
3245 cmp = 1;
3246 else if (a_size < b_size)
3247 cmp = -1;
3248 }
3249 }
Ævar Arnfjörð Bjarmason75c50e52021-01-07 10:51:49 +01003250 } else {
Karthik Nayakc95b7582015-06-14 01:07:27 +05303251 if (va->value < vb->value)
3252 cmp = -1;
3253 else if (va->value == vb->value)
Jeff King7c5045f2020-05-03 05:13:09 -04003254 cmp = 0;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303255 else
3256 cmp = 1;
3257 }
3258
Ævar Arnfjörð Bjarmason4045f652021-01-07 10:51:53 +01003259 return (s->sort_flags & REF_SORTING_REVERSE && !cmp_detached_head)
3260 ? -cmp : cmp;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303261}
3262
René Scharfe83fc4d62017-01-22 18:58:07 +01003263static int compare_refs(const void *a_, const void *b_, void *ref_sorting)
Karthik Nayakc95b7582015-06-14 01:07:27 +05303264{
3265 struct ref_array_item *a = *((struct ref_array_item **)a_);
3266 struct ref_array_item *b = *((struct ref_array_item **)b_);
3267 struct ref_sorting *s;
3268
3269 for (s = ref_sorting; s; s = s->next) {
3270 int cmp = cmp_ref_sorting(s, a, b);
3271 if (cmp)
3272 return cmp;
3273 }
Jeff King7c5045f2020-05-03 05:13:09 -04003274 s = ref_sorting;
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +01003275 return s && s->sort_flags & REF_SORTING_ICASE ?
Jeff King7c5045f2020-05-03 05:13:09 -04003276 strcasecmp(a->refname, b->refname) :
3277 strcmp(a->refname, b->refname);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303278}
3279
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +01003280void ref_sorting_set_sort_flags_all(struct ref_sorting *sorting,
3281 unsigned int mask, int on)
Jeff King76f9e562020-05-03 05:11:57 -04003282{
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +01003283 for (; sorting; sorting = sorting->next) {
3284 if (on)
3285 sorting->sort_flags |= mask;
3286 else
3287 sorting->sort_flags &= ~mask;
3288 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05303289}
3290
3291void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
3292{
Victoria Dye56d26ad2023-11-14 19:53:49 +00003293 if (sorting)
3294 QSORT_S(array->items, array->nr, compare_refs, sorting);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303295}
3296
Karthik Nayak574e96a2015-09-10 21:18:18 +05303297static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
Karthik Nayakc95b7582015-06-14 01:07:27 +05303298{
Karthik Nayak574e96a2015-09-10 21:18:18 +05303299 struct strbuf *s = &state->stack->output;
3300
Karthik Nayakc95b7582015-06-14 01:07:27 +05303301 while (*cp && (!ep || cp < ep)) {
3302 if (*cp == '%') {
3303 if (cp[1] == '%')
3304 cp++;
3305 else {
René Scharfed2330972016-09-03 17:59:20 +02003306 int ch = hex2chr(cp + 1);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303307 if (0 <= ch) {
Karthik Nayak574e96a2015-09-10 21:18:18 +05303308 strbuf_addch(s, ch);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303309 cp += 3;
3310 continue;
3311 }
3312 }
3313 }
Karthik Nayak574e96a2015-09-10 21:18:18 +05303314 strbuf_addch(s, *cp);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303315 cp++;
3316 }
3317}
3318
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00003319int format_ref_array_item(struct ref_array_item *info,
ZheNing Hue85fcb32021-07-26 03:26:49 +00003320 struct ref_format *format,
3321 struct strbuf *final_buf,
3322 struct strbuf *error_buf)
Karthik Nayakc95b7582015-06-14 01:07:27 +05303323{
3324 const char *cp, *sp, *ep;
Karthik Nayak574e96a2015-09-10 21:18:18 +05303325 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
3326
Jeff King4a68e362017-07-13 11:01:18 -04003327 state.quote_style = format->quote_style;
Karthik Nayak574e96a2015-09-10 21:18:18 +05303328 push_stack_element(&state.stack);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303329
Jeff King4a68e362017-07-13 11:01:18 -04003330 for (cp = format->format; *cp && (sp = find_next(cp)); cp = ep + 1) {
Karthik Nayakc95b7582015-06-14 01:07:27 +05303331 struct atom_value *atomv;
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00003332 int pos;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303333
3334 ep = strchr(sp, ')');
3335 if (cp < sp)
Karthik Nayak574e96a2015-09-10 21:18:18 +05303336 append_literal(cp, sp, &state);
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00003337 pos = parse_ref_filter_atom(format, sp + 2, ep, error_buf);
Olga Telezhnayae3396112018-03-29 12:49:45 +00003338 if (pos < 0 || get_ref_atom_value(info, pos, &atomv, error_buf) ||
3339 atomv->handler(atomv, &state, error_buf)) {
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00003340 pop_stack_element(&state.stack);
3341 return -1;
3342 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05303343 }
3344 if (*cp) {
3345 sp = cp + strlen(cp);
Karthik Nayak574e96a2015-09-10 21:18:18 +05303346 append_literal(cp, sp, &state);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303347 }
Jeff Kingbf285ae2017-07-13 11:02:30 -04003348 if (format->need_color_reset_at_eol) {
ZheNing Hubd0708c2021-07-26 03:26:47 +00003349 struct atom_value resetv = ATOM_VALUE_INIT;
Jeff King51331aa2017-07-13 10:58:56 -04003350 resetv.s = GIT_COLOR_RESET;
Olga Telezhnaya3fc84392018-03-29 12:49:45 +00003351 if (append_atom(&resetv, &state, error_buf)) {
3352 pop_stack_element(&state.stack);
3353 return -1;
3354 }
Karthik Nayakc95b7582015-06-14 01:07:27 +05303355 }
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00003356 if (state.stack->prev) {
3357 pop_stack_element(&state.stack);
3358 return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
3359 }
Karthik Nayak99c6a712017-01-10 14:19:39 +05303360 strbuf_addbuf(final_buf, &state.stack->output);
Karthik Nayak574e96a2015-09-10 21:18:18 +05303361 pop_stack_element(&state.stack);
Olga Telezhnaya3019eca2018-03-29 12:49:45 +00003362 return 0;
Karthik Nayak99c6a712017-01-10 14:19:39 +05303363}
3364
Victoria Dyee7574b02023-11-14 19:53:52 +00003365void print_formatted_ref_array(struct ref_array *array, struct ref_format *format)
3366{
3367 int total;
3368 struct strbuf output = STRBUF_INIT, err = STRBUF_INIT;
3369
3370 total = format->array_opts.max_count;
3371 if (!total || array->nr < total)
3372 total = array->nr;
3373 for (int i = 0; i < total; i++) {
3374 strbuf_reset(&err);
3375 strbuf_reset(&output);
3376 if (format_ref_array_item(array->items[i], format, &output, &err))
3377 die("%s", err.buf);
3378 if (output.len || !format->array_opts.omit_empty) {
3379 fwrite(output.buf, 1, output.len, stdout);
3380 putchar('\n');
3381 }
3382 }
3383
3384 strbuf_release(&err);
3385 strbuf_release(&output);
3386}
3387
Jeff King53df97a2018-04-06 14:58:32 -04003388void pretty_print_ref(const char *name, const struct object_id *oid,
ZheNing Hue85fcb32021-07-26 03:26:49 +00003389 struct ref_format *format)
Lukas Puehringer2111aa72017-01-17 18:37:19 -05003390{
3391 struct ref_array_item *ref_item;
ZheNing Hu22f69a82021-04-19 11:28:44 +00003392 struct strbuf output = STRBUF_INIT;
3393 struct strbuf err = STRBUF_INIT;
3394
Jeff King0ffaa002018-04-06 14:59:26 -04003395 ref_item = new_ref_array_item(name, oid);
Lukas Puehringer2111aa72017-01-17 18:37:19 -05003396 ref_item->kind = ref_kind_from_refname(name);
ZheNing Hu22f69a82021-04-19 11:28:44 +00003397 if (format_ref_array_item(ref_item, format, &output, &err))
3398 die("%s", err.buf);
3399 fwrite(output.buf, 1, output.len, stdout);
3400 putchar('\n');
3401
3402 strbuf_release(&err);
3403 strbuf_release(&output);
Lukas Puehringer2111aa72017-01-17 18:37:19 -05003404 free_array_item(ref_item);
3405}
3406
Jeff King29ef53c2017-07-13 11:02:58 -04003407static int parse_sorting_atom(const char *atom)
3408{
Jeff Kingab7ded32017-07-13 11:06:40 -04003409 /*
3410 * This parses an atom using a dummy ref_format, since we don't
3411 * actually care about the formatting details.
3412 */
3413 struct ref_format dummy = REF_FORMAT_INIT;
Jeff King29ef53c2017-07-13 11:02:58 -04003414 const char *end = atom + strlen(atom);
Olga Telezhnayae6ff7b32018-03-29 12:49:45 +00003415 struct strbuf err = STRBUF_INIT;
3416 int res = parse_ref_filter_atom(&dummy, atom, end, &err);
3417 if (res < 0)
3418 die("%s", err.buf);
3419 strbuf_release(&err);
3420 return res;
Jeff King29ef53c2017-07-13 11:02:58 -04003421}
3422
Junio C Hamano98e7ab62021-10-20 12:23:53 -07003423static void parse_ref_sorting(struct ref_sorting **sorting_tail, const char *arg)
Karthik Nayakc95b7582015-06-14 01:07:27 +05303424{
Karthik Nayakc95b7582015-06-14 01:07:27 +05303425 struct ref_sorting *s;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303426
René Scharfeca56dad2021-03-13 17:17:22 +01003427 CALLOC_ARRAY(s, 1);
Karthik Nayakc95b7582015-06-14 01:07:27 +05303428 s->next = *sorting_tail;
3429 *sorting_tail = s;
3430
3431 if (*arg == '-') {
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +01003432 s->sort_flags |= REF_SORTING_REVERSE;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303433 arg++;
3434 }
Karthik Nayak90c00402015-09-10 21:18:25 +05303435 if (skip_prefix(arg, "version:", &arg) ||
3436 skip_prefix(arg, "v:", &arg))
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +01003437 s->sort_flags |= REF_SORTING_VERSION;
Jeff King29ef53c2017-07-13 11:02:58 -04003438 s->atom = parse_sorting_atom(arg);
Jeff King18a25652017-07-13 11:02:44 -04003439}
3440
Junio C Hamano98e7ab62021-10-20 12:23:53 -07003441struct ref_sorting *ref_sorting_options(struct string_list *options)
Jeff King18a25652017-07-13 11:02:44 -04003442{
Junio C Hamano98e7ab62021-10-20 12:23:53 -07003443 struct string_list_item *item;
3444 struct ref_sorting *sorting = NULL, **tail = &sorting;
Jeff King95be7172019-03-20 16:22:15 -04003445
Victoria Dye56d26ad2023-11-14 19:53:49 +00003446 if (options->nr) {
Junio C Hamano98e7ab62021-10-20 12:23:53 -07003447 for_each_string_list_item(item, options)
3448 parse_ref_sorting(tail, item->string);
3449 }
3450
3451 /*
3452 * From here on, the ref_sorting list should be used to talk
3453 * about the sort order used for the output. The caller
3454 * should not touch the string form anymore.
3455 */
3456 string_list_clear(options, 0);
3457 return sorting;
Karthik Nayakc95b7582015-06-14 01:07:27 +05303458}
Karthik Nayak5afcb902015-07-07 21:36:11 +05303459
Ævar Arnfjörð Bjarmasone5fb0282021-10-20 20:27:20 +02003460void ref_sorting_release(struct ref_sorting *sorting)
3461{
3462 while (sorting) {
3463 struct ref_sorting *next = sorting->next;
3464 free(sorting);
3465 sorting = next;
3466 }
3467}
3468
Karthik Nayak5afcb902015-07-07 21:36:11 +05303469int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
3470{
3471 struct ref_filter *rf = opt->value;
brian m. carlson1e43ed92017-05-06 22:10:09 +00003472 struct object_id oid;
Aaron Lipman21bf9332020-09-15 22:08:40 -04003473 struct commit *merge_commit;
Karthik Nayak5afcb902015-07-07 21:36:11 +05303474
Jeff King517fe802018-11-05 01:45:42 -05003475 BUG_ON_OPT_NEG(unset);
3476
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +02003477 if (repo_get_oid(the_repository, arg, &oid))
Karthik Nayak5afcb902015-07-07 21:36:11 +05303478 die(_("malformed object name %s"), arg);
3479
Aaron Lipman21bf9332020-09-15 22:08:40 -04003480 merge_commit = lookup_commit_reference_gently(the_repository, &oid, 0);
3481
3482 if (!merge_commit)
Nguyễn Thái Ngọc Duy9440b832018-11-10 06:16:11 +01003483 return error(_("option `%s' must point to a commit"), opt->long_name);
Karthik Nayak5afcb902015-07-07 21:36:11 +05303484
Aaron Lipman21bf9332020-09-15 22:08:40 -04003485 if (starts_with(opt->long_name, "no"))
3486 commit_list_insert(merge_commit, &rf->unreachable_from);
3487 else
3488 commit_list_insert(merge_commit, &rf->reachable_from);
3489
Karthik Nayak5afcb902015-07-07 21:36:11 +05303490 return 0;
3491}
Jeff Kingb571fb92023-07-10 17:12:13 -04003492
3493void ref_filter_init(struct ref_filter *filter)
3494{
3495 struct ref_filter blank = REF_FILTER_INIT;
3496 memcpy(filter, &blank, sizeof(blank));
3497}
3498
3499void ref_filter_clear(struct ref_filter *filter)
3500{
Taylor Blau8255dd82023-07-10 17:12:19 -04003501 strvec_clear(&filter->exclude);
Jeff Kingb571fb92023-07-10 17:12:13 -04003502 oid_array_clear(&filter->points_at);
3503 free_commit_list(filter->with_commit);
3504 free_commit_list(filter->no_commit);
3505 free_commit_list(filter->reachable_from);
3506 free_commit_list(filter->unreachable_from);
3507 ref_filter_init(filter);
3508}