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