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