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