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