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