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