3 #include "parse-options.h"
6 #include "object-store.h"
12 #include "ref-filter.h"
15 #include "git-compat-util.h"
18 #include "wt-status.h"
19 #include "commit-slab.h"
20 #include "commit-graph.h"
22 static struct ref_msg
{
26 const char *ahead_behind
;
28 /* Untranslated plumbing messages: */
35 void setup_ref_filter_porcelain_msg(void)
37 msgs
.gone
= _("gone");
38 msgs
.ahead
= _("ahead %d");
39 msgs
.behind
= _("behind %d");
40 msgs
.ahead_behind
= _("ahead %d, behind %d");
43 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
44 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
52 cmp_status cmp_status
;
54 unsigned int then_atom_seen
: 1,
56 condition_satisfied
: 1;
60 enum { R_NORMAL
, R_SHORT
, R_LSTRIP
, R_RSTRIP
} option
;
65 * An atom is a valid field atom listed below, possibly prefixed with
66 * a "*" to denote deref_tag().
68 * We parse given format string and sort specifiers, and make a list
69 * of properties that we need to extract out of objects. ref_array_item
70 * structure will hold an array of values extracted that can be
71 * indexed with the "atom number", which is an index into this
74 static struct used_atom
{
78 char color
[COLOR_MAXLEN
];
82 RR_REF
, RR_TRACK
, RR_TRACKSHORT
, RR_REMOTE_NAME
, RR_REMOTE_REF
84 struct refname_atom refname
;
85 unsigned int nobracket
: 1, push
: 1, push_remote
: 1;
88 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
89 struct process_trailer_options trailer_opts
;
93 cmp_status cmp_status
;
97 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
100 struct refname_atom refname
;
104 static int used_atom_cnt
, need_tagged
, need_symref
;
107 * Expand string, append it to strbuf *sb, then return error code ret.
108 * Allow to save few lines of code.
110 static int strbuf_addf_ret(struct strbuf
*sb
, int ret
, const char *fmt
, ...)
114 strbuf_vaddf(sb
, fmt
, ap
);
119 static int color_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
120 const char *color_value
, struct strbuf
*err
)
123 return strbuf_addf_ret(err
, -1, _("expected format: %%(color:<color>)"));
124 if (color_parse(color_value
, atom
->u
.color
) < 0)
125 return strbuf_addf_ret(err
, -1, _("unrecognized color: %%(color:%s)"),
128 * We check this after we've parsed the color, which lets us complain
129 * about syntactically bogus color names even if they won't be used.
131 if (!want_color(format
->use_color
))
132 color_parse("", atom
->u
.color
);
136 static int refname_atom_parser_internal(struct refname_atom
*atom
, const char *arg
,
137 const char *name
, struct strbuf
*err
)
140 atom
->option
= R_NORMAL
;
141 else if (!strcmp(arg
, "short"))
142 atom
->option
= R_SHORT
;
143 else if (skip_prefix(arg
, "lstrip=", &arg
) ||
144 skip_prefix(arg
, "strip=", &arg
)) {
145 atom
->option
= R_LSTRIP
;
146 if (strtol_i(arg
, 10, &atom
->lstrip
))
147 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:lstrip=%s"), arg
);
148 } else if (skip_prefix(arg
, "rstrip=", &arg
)) {
149 atom
->option
= R_RSTRIP
;
150 if (strtol_i(arg
, 10, &atom
->rstrip
))
151 return strbuf_addf_ret(err
, -1, _("Integer value expected refname:rstrip=%s"), arg
);
153 return strbuf_addf_ret(err
, -1, _("unrecognized %%(%s) argument: %s"), name
, arg
);
157 static int remote_ref_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
158 const char *arg
, struct strbuf
*err
)
160 struct string_list params
= STRING_LIST_INIT_DUP
;
163 if (!strcmp(atom
->name
, "push") || starts_with(atom
->name
, "push:"))
164 atom
->u
.remote_ref
.push
= 1;
167 atom
->u
.remote_ref
.option
= RR_REF
;
168 return refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
169 arg
, atom
->name
, err
);
172 atom
->u
.remote_ref
.nobracket
= 0;
173 string_list_split(¶ms
, arg
, ',', -1);
175 for (i
= 0; i
< params
.nr
; i
++) {
176 const char *s
= params
.items
[i
].string
;
178 if (!strcmp(s
, "track"))
179 atom
->u
.remote_ref
.option
= RR_TRACK
;
180 else if (!strcmp(s
, "trackshort"))
181 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
182 else if (!strcmp(s
, "nobracket"))
183 atom
->u
.remote_ref
.nobracket
= 1;
184 else if (!strcmp(s
, "remotename")) {
185 atom
->u
.remote_ref
.option
= RR_REMOTE_NAME
;
186 atom
->u
.remote_ref
.push_remote
= 1;
187 } else if (!strcmp(s
, "remoteref")) {
188 atom
->u
.remote_ref
.option
= RR_REMOTE_REF
;
189 atom
->u
.remote_ref
.push_remote
= 1;
191 atom
->u
.remote_ref
.option
= RR_REF
;
192 if (refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
193 arg
, atom
->name
, err
)) {
194 string_list_clear(¶ms
, 0);
200 string_list_clear(¶ms
, 0);
204 static int body_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
205 const char *arg
, struct strbuf
*err
)
208 return strbuf_addf_ret(err
, -1, _("%%(body) does not take arguments"));
209 atom
->u
.contents
.option
= C_BODY_DEP
;
213 static int subject_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
214 const char *arg
, struct strbuf
*err
)
217 return strbuf_addf_ret(err
, -1, _("%%(subject) does not take arguments"));
218 atom
->u
.contents
.option
= C_SUB
;
222 static int trailers_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
223 const char *arg
, struct strbuf
*err
)
225 struct string_list params
= STRING_LIST_INIT_DUP
;
229 string_list_split(¶ms
, arg
, ',', -1);
230 for (i
= 0; i
< params
.nr
; i
++) {
231 const char *s
= params
.items
[i
].string
;
232 if (!strcmp(s
, "unfold"))
233 atom
->u
.contents
.trailer_opts
.unfold
= 1;
234 else if (!strcmp(s
, "only"))
235 atom
->u
.contents
.trailer_opts
.only_trailers
= 1;
237 strbuf_addf(err
, _("unknown %%(trailers) argument: %s"), s
);
238 string_list_clear(¶ms
, 0);
243 atom
->u
.contents
.option
= C_TRAILERS
;
244 string_list_clear(¶ms
, 0);
248 static int contents_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
249 const char *arg
, struct strbuf
*err
)
252 atom
->u
.contents
.option
= C_BARE
;
253 else if (!strcmp(arg
, "body"))
254 atom
->u
.contents
.option
= C_BODY
;
255 else if (!strcmp(arg
, "signature"))
256 atom
->u
.contents
.option
= C_SIG
;
257 else if (!strcmp(arg
, "subject"))
258 atom
->u
.contents
.option
= C_SUB
;
259 else if (skip_prefix(arg
, "trailers", &arg
)) {
260 skip_prefix(arg
, ":", &arg
);
261 if (trailers_atom_parser(format
, atom
, *arg
? arg
: NULL
, err
))
263 } else if (skip_prefix(arg
, "lines=", &arg
)) {
264 atom
->u
.contents
.option
= C_LINES
;
265 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
266 return strbuf_addf_ret(err
, -1, _("positive value expected contents:lines=%s"), arg
);
268 return strbuf_addf_ret(err
, -1, _("unrecognized %%(contents) argument: %s"), arg
);
272 static int objectname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
273 const char *arg
, struct strbuf
*err
)
276 atom
->u
.objectname
.option
= O_FULL
;
277 else if (!strcmp(arg
, "short"))
278 atom
->u
.objectname
.option
= O_SHORT
;
279 else if (skip_prefix(arg
, "short=", &arg
)) {
280 atom
->u
.objectname
.option
= O_LENGTH
;
281 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
282 atom
->u
.objectname
.length
== 0)
283 return strbuf_addf_ret(err
, -1, _("positive value expected objectname:short=%s"), arg
);
284 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
285 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
287 return strbuf_addf_ret(err
, -1, _("unrecognized %%(objectname) argument: %s"), arg
);
291 static int refname_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
292 const char *arg
, struct strbuf
*err
)
294 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
, err
);
297 static align_type
parse_align_position(const char *s
)
299 if (!strcmp(s
, "right"))
301 else if (!strcmp(s
, "middle"))
303 else if (!strcmp(s
, "left"))
308 static int align_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
309 const char *arg
, struct strbuf
*err
)
311 struct align
*align
= &atom
->u
.align
;
312 struct string_list params
= STRING_LIST_INIT_DUP
;
314 unsigned int width
= ~0U;
317 return strbuf_addf_ret(err
, -1, _("expected format: %%(align:<width>,<position>)"));
319 align
->position
= ALIGN_LEFT
;
321 string_list_split(¶ms
, arg
, ',', -1);
322 for (i
= 0; i
< params
.nr
; i
++) {
323 const char *s
= params
.items
[i
].string
;
326 if (skip_prefix(s
, "position=", &s
)) {
327 position
= parse_align_position(s
);
329 strbuf_addf(err
, _("unrecognized position:%s"), s
);
330 string_list_clear(¶ms
, 0);
333 align
->position
= position
;
334 } else if (skip_prefix(s
, "width=", &s
)) {
335 if (strtoul_ui(s
, 10, &width
)) {
336 strbuf_addf(err
, _("unrecognized width:%s"), s
);
337 string_list_clear(¶ms
, 0);
340 } else if (!strtoul_ui(s
, 10, &width
))
342 else if ((position
= parse_align_position(s
)) >= 0)
343 align
->position
= position
;
345 strbuf_addf(err
, _("unrecognized %%(align) argument: %s"), s
);
346 string_list_clear(¶ms
, 0);
352 string_list_clear(¶ms
, 0);
353 return strbuf_addf_ret(err
, -1, _("positive width expected with the %%(align) atom"));
355 align
->width
= width
;
356 string_list_clear(¶ms
, 0);
360 static int if_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
361 const char *arg
, struct strbuf
*err
)
364 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
366 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
367 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
368 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
369 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
371 return strbuf_addf_ret(err
, -1, _("unrecognized %%(if) argument: %s"), arg
);
375 static int head_atom_parser(const struct ref_format
*format
, struct used_atom
*atom
,
376 const char *arg
, struct strbuf
*unused_err
)
378 atom
->u
.head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
385 int (*parser
)(const struct ref_format
*format
, struct used_atom
*atom
,
386 const char *arg
, struct strbuf
*err
);
388 { "refname" , FIELD_STR
, refname_atom_parser
},
390 { "objectsize", FIELD_ULONG
},
391 { "objectname", FIELD_STR
, objectname_atom_parser
},
394 { "numparent", FIELD_ULONG
},
401 { "authordate", FIELD_TIME
},
404 { "committeremail" },
405 { "committerdate", FIELD_TIME
},
409 { "taggerdate", FIELD_TIME
},
411 { "creatordate", FIELD_TIME
},
412 { "subject", FIELD_STR
, subject_atom_parser
},
413 { "body", FIELD_STR
, body_atom_parser
},
414 { "trailers", FIELD_STR
, trailers_atom_parser
},
415 { "contents", FIELD_STR
, contents_atom_parser
},
416 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
417 { "push", FIELD_STR
, remote_ref_atom_parser
},
418 { "symref", FIELD_STR
, refname_atom_parser
},
420 { "HEAD", FIELD_STR
, head_atom_parser
},
421 { "color", FIELD_STR
, color_atom_parser
},
422 { "align", FIELD_STR
, align_atom_parser
},
424 { "if", FIELD_STR
, if_atom_parser
},
429 #define REF_FORMATTING_STATE_INIT { 0, NULL }
431 struct ref_formatting_stack
{
432 struct ref_formatting_stack
*prev
;
433 struct strbuf output
;
434 void (*at_end
)(struct ref_formatting_stack
**stack
);
438 struct ref_formatting_state
{
440 struct ref_formatting_stack
*stack
;
445 int (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
447 uintmax_t value
; /* used for sorting when not FIELD_STR */
448 struct used_atom
*atom
;
452 * Used to parse format string and sort specifiers
454 static int parse_ref_filter_atom(const struct ref_format
*format
,
455 const char *atom
, const char *ep
,
463 if (*sp
== '*' && sp
< ep
)
466 return strbuf_addf_ret(err
, -1, _("malformed field name: %.*s"),
467 (int)(ep
-atom
), atom
);
469 /* Do we have the atom already used elsewhere? */
470 for (i
= 0; i
< used_atom_cnt
; i
++) {
471 int len
= strlen(used_atom
[i
].name
);
472 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
477 * If the atom name has a colon, strip it and everything after
478 * it off - it specifies the format for this entry, and
479 * shouldn't be used for checking against the valid_atom
482 arg
= memchr(sp
, ':', ep
- sp
);
483 atom_len
= (arg
? arg
: ep
) - sp
;
485 /* Is the atom a valid one? */
486 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
487 int len
= strlen(valid_atom
[i
].name
);
488 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
492 if (ARRAY_SIZE(valid_atom
) <= i
)
493 return strbuf_addf_ret(err
, -1, _("unknown field name: %.*s"),
494 (int)(ep
-atom
), atom
);
496 /* Add it in, including the deref prefix */
499 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
500 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
501 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
503 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
506 * Treat empty sub-arguments list as NULL (i.e.,
507 * "%(atom:)" is equivalent to "%(atom)").
512 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
513 if (valid_atom
[i
].parser
&& valid_atom
[i
].parser(format
, &used_atom
[at
], arg
, err
))
517 if (!strcmp(valid_atom
[i
].name
, "symref"))
522 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
524 switch (quote_style
) {
526 strbuf_addstr(s
, str
);
529 sq_quote_buf(s
, str
);
532 perl_quote_buf(s
, str
);
535 python_quote_buf(s
, str
);
538 tcl_quote_buf(s
, str
);
543 static int append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
,
544 struct strbuf
*unused_err
)
547 * Quote formatting is only done when the stack has a single
548 * element. Otherwise quote formatting is done on the
549 * element's entire output strbuf when the %(end) atom is
552 if (!state
->stack
->prev
)
553 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
555 strbuf_addstr(&state
->stack
->output
, v
->s
);
559 static void push_stack_element(struct ref_formatting_stack
**stack
)
561 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
563 strbuf_init(&s
->output
, 0);
568 static void pop_stack_element(struct ref_formatting_stack
**stack
)
570 struct ref_formatting_stack
*current
= *stack
;
571 struct ref_formatting_stack
*prev
= current
->prev
;
574 strbuf_addbuf(&prev
->output
, ¤t
->output
);
575 strbuf_release(¤t
->output
);
580 static void end_align_handler(struct ref_formatting_stack
**stack
)
582 struct ref_formatting_stack
*cur
= *stack
;
583 struct align
*align
= (struct align
*)cur
->at_end_data
;
584 struct strbuf s
= STRBUF_INIT
;
586 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
587 strbuf_swap(&cur
->output
, &s
);
591 static int align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
592 struct strbuf
*unused_err
)
594 struct ref_formatting_stack
*new_stack
;
596 push_stack_element(&state
->stack
);
597 new_stack
= state
->stack
;
598 new_stack
->at_end
= end_align_handler
;
599 new_stack
->at_end_data
= &atomv
->atom
->u
.align
;
603 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
605 struct ref_formatting_stack
*cur
= *stack
;
606 struct ref_formatting_stack
*prev
= cur
->prev
;
607 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
609 if (!if_then_else
->then_atom_seen
)
610 die(_("format: %%(if) atom used without a %%(then) atom"));
612 if (if_then_else
->else_atom_seen
) {
614 * There is an %(else) atom: we need to drop one state from the
615 * stack, either the %(else) branch if the condition is satisfied, or
616 * the %(then) branch if it isn't.
618 if (if_then_else
->condition_satisfied
) {
619 strbuf_reset(&cur
->output
);
620 pop_stack_element(&cur
);
622 strbuf_swap(&cur
->output
, &prev
->output
);
623 strbuf_reset(&cur
->output
);
624 pop_stack_element(&cur
);
626 } else if (!if_then_else
->condition_satisfied
) {
628 * No %(else) atom: just drop the %(then) branch if the
629 * condition is not satisfied.
631 strbuf_reset(&cur
->output
);
638 static int if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
639 struct strbuf
*unused_err
)
641 struct ref_formatting_stack
*new_stack
;
642 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
644 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
645 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
647 push_stack_element(&state
->stack
);
648 new_stack
= state
->stack
;
649 new_stack
->at_end
= if_then_else_handler
;
650 new_stack
->at_end_data
= if_then_else
;
654 static int is_empty(const char *s
)
664 static int then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
667 struct ref_formatting_stack
*cur
= state
->stack
;
668 struct if_then_else
*if_then_else
= NULL
;
670 if (cur
->at_end
== if_then_else_handler
)
671 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
673 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used without an %%(if) atom"));
674 if (if_then_else
->then_atom_seen
)
675 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used more than once"));
676 if (if_then_else
->else_atom_seen
)
677 return strbuf_addf_ret(err
, -1, _("format: %%(then) atom used after %%(else)"));
678 if_then_else
->then_atom_seen
= 1;
680 * If the 'equals' or 'notequals' attribute is used then
681 * perform the required comparison. If not, only non-empty
682 * strings satisfy the 'if' condition.
684 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
685 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
686 if_then_else
->condition_satisfied
= 1;
687 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
688 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
689 if_then_else
->condition_satisfied
= 1;
690 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
691 if_then_else
->condition_satisfied
= 1;
692 strbuf_reset(&cur
->output
);
696 static int else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
699 struct ref_formatting_stack
*prev
= state
->stack
;
700 struct if_then_else
*if_then_else
= NULL
;
702 if (prev
->at_end
== if_then_else_handler
)
703 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
705 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without an %%(if) atom"));
706 if (!if_then_else
->then_atom_seen
)
707 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used without a %%(then) atom"));
708 if (if_then_else
->else_atom_seen
)
709 return strbuf_addf_ret(err
, -1, _("format: %%(else) atom used more than once"));
710 if_then_else
->else_atom_seen
= 1;
711 push_stack_element(&state
->stack
);
712 state
->stack
->at_end_data
= prev
->at_end_data
;
713 state
->stack
->at_end
= prev
->at_end
;
717 static int end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
,
720 struct ref_formatting_stack
*current
= state
->stack
;
721 struct strbuf s
= STRBUF_INIT
;
723 if (!current
->at_end
)
724 return strbuf_addf_ret(err
, -1, _("format: %%(end) atom used without corresponding atom"));
725 current
->at_end(&state
->stack
);
727 /* Stack may have been popped within at_end(), hence reset the current pointer */
728 current
= state
->stack
;
731 * Perform quote formatting when the stack element is that of
732 * a supporting atom. If nested then perform quote formatting
733 * only on the topmost supporting atom.
735 if (!current
->prev
->prev
) {
736 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
737 strbuf_swap(¤t
->output
, &s
);
740 pop_stack_element(&state
->stack
);
745 * In a format string, find the next occurrence of %(atom).
747 static const char *find_next(const char *cp
)
752 * %( is the start of an atom;
753 * %% is a quoted per-cent.
757 else if (cp
[1] == '%')
758 cp
++; /* skip over two % */
759 /* otherwise this is a singleton, literal % */
767 * Make sure the format string is well formed, and parse out
770 int verify_ref_format(struct ref_format
*format
)
774 format
->need_color_reset_at_eol
= 0;
775 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); ) {
776 struct strbuf err
= STRBUF_INIT
;
777 const char *color
, *ep
= strchr(sp
, ')');
781 return error(_("malformed format string %s"), sp
);
782 /* sp points at "%(" and ep points at the closing ")" */
783 at
= parse_ref_filter_atom(format
, sp
+ 2, ep
, &err
);
788 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
789 format
->need_color_reset_at_eol
= !!strcmp(color
, "reset");
790 strbuf_release(&err
);
792 if (format
->need_color_reset_at_eol
&& !want_color(format
->use_color
))
793 format
->need_color_reset_at_eol
= 0;
798 * Given an object name, read the object data and size, and return a
799 * "struct object". If the object data we are returning is also borrowed
800 * by the "struct object" representation, set *eaten as well---it is a
801 * signal from parse_object_buffer to us not to free the buffer.
803 static void *get_obj(const struct object_id
*oid
, struct object
**obj
, unsigned long *sz
, int *eaten
)
805 enum object_type type
;
806 void *buf
= read_object_file(oid
, &type
, sz
);
809 *obj
= parse_object_buffer(oid
, type
, *sz
, buf
, eaten
);
815 static int grab_objectname(const char *name
, const struct object_id
*oid
,
816 struct atom_value
*v
, struct used_atom
*atom
)
818 if (starts_with(name
, "objectname")) {
819 if (atom
->u
.objectname
.option
== O_SHORT
) {
820 v
->s
= xstrdup(find_unique_abbrev(oid
, DEFAULT_ABBREV
));
822 } else if (atom
->u
.objectname
.option
== O_FULL
) {
823 v
->s
= xstrdup(oid_to_hex(oid
));
825 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
826 v
->s
= xstrdup(find_unique_abbrev(oid
, atom
->u
.objectname
.length
));
829 BUG("unknown %%(objectname) option");
834 /* See grab_values */
835 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
839 for (i
= 0; i
< used_atom_cnt
; i
++) {
840 const char *name
= used_atom
[i
].name
;
841 struct atom_value
*v
= &val
[i
];
842 if (!!deref
!= (*name
== '*'))
846 if (!strcmp(name
, "objecttype"))
847 v
->s
= type_name(obj
->type
);
848 else if (!strcmp(name
, "objectsize")) {
850 v
->s
= xstrfmt("%lu", sz
);
853 grab_objectname(name
, &obj
->oid
, v
, &used_atom
[i
]);
857 /* See grab_values */
858 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
861 struct tag
*tag
= (struct tag
*) obj
;
863 for (i
= 0; i
< used_atom_cnt
; i
++) {
864 const char *name
= used_atom
[i
].name
;
865 struct atom_value
*v
= &val
[i
];
866 if (!!deref
!= (*name
== '*'))
870 if (!strcmp(name
, "tag"))
872 else if (!strcmp(name
, "type") && tag
->tagged
)
873 v
->s
= type_name(tag
->tagged
->type
);
874 else if (!strcmp(name
, "object") && tag
->tagged
)
875 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
879 /* See grab_values */
880 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
883 struct commit
*commit
= (struct commit
*) obj
;
885 for (i
= 0; i
< used_atom_cnt
; i
++) {
886 const char *name
= used_atom
[i
].name
;
887 struct atom_value
*v
= &val
[i
];
888 if (!!deref
!= (*name
== '*'))
892 if (!strcmp(name
, "tree")) {
893 v
->s
= xstrdup(oid_to_hex(get_commit_tree_oid(commit
)));
895 else if (!strcmp(name
, "numparent")) {
896 v
->value
= commit_list_count(commit
->parents
);
897 v
->s
= xstrfmt("%lu", (unsigned long)v
->value
);
899 else if (!strcmp(name
, "parent")) {
900 struct commit_list
*parents
;
901 struct strbuf s
= STRBUF_INIT
;
902 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
903 struct commit
*parent
= parents
->item
;
904 if (parents
!= commit
->parents
)
905 strbuf_addch(&s
, ' ');
906 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
908 v
->s
= strbuf_detach(&s
, NULL
);
913 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
917 if (!strncmp(buf
, who
, wholen
) &&
919 return buf
+ wholen
+ 1;
920 eol
= strchr(buf
, '\n');
925 return ""; /* end of header */
931 static const char *copy_line(const char *buf
)
933 const char *eol
= strchrnul(buf
, '\n');
934 return xmemdupz(buf
, eol
- buf
);
937 static const char *copy_name(const char *buf
)
940 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
941 if (!strncmp(cp
, " <", 2))
942 return xmemdupz(buf
, cp
- buf
);
947 static const char *copy_email(const char *buf
)
949 const char *email
= strchr(buf
, '<');
953 eoemail
= strchr(email
, '>');
956 return xmemdupz(email
, eoemail
+ 1 - email
);
959 static char *copy_subject(const char *buf
, unsigned long len
)
961 char *r
= xmemdupz(buf
, len
);
964 for (i
= 0; i
< len
; i
++)
971 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
973 const char *eoemail
= strstr(buf
, "> ");
975 timestamp_t timestamp
;
977 struct date_mode date_mode
= { DATE_NORMAL
};
981 * We got here because atomname ends in "date" or "date<something>";
982 * it's not possible that <something> is not ":<format>" because
983 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
984 * ":" means no format is specified, and use the default.
986 formatp
= strchr(atomname
, ':');
987 if (formatp
!= NULL
) {
989 parse_date_format(formatp
, &date_mode
);
994 timestamp
= parse_timestamp(eoemail
+ 2, &zone
, 10);
995 if (timestamp
== TIME_MAX
)
997 tz
= strtol(zone
, NULL
, 10);
998 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
1000 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
1001 v
->value
= timestamp
;
1008 /* See grab_values */
1009 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1012 int wholen
= strlen(who
);
1013 const char *wholine
= NULL
;
1015 for (i
= 0; i
< used_atom_cnt
; i
++) {
1016 const char *name
= used_atom
[i
].name
;
1017 struct atom_value
*v
= &val
[i
];
1018 if (!!deref
!= (*name
== '*'))
1022 if (strncmp(who
, name
, wholen
))
1024 if (name
[wholen
] != 0 &&
1025 strcmp(name
+ wholen
, "name") &&
1026 strcmp(name
+ wholen
, "email") &&
1027 !starts_with(name
+ wholen
, "date"))
1030 wholine
= find_wholine(who
, wholen
, buf
, sz
);
1032 return; /* no point looking for it */
1033 if (name
[wholen
] == 0)
1034 v
->s
= copy_line(wholine
);
1035 else if (!strcmp(name
+ wholen
, "name"))
1036 v
->s
= copy_name(wholine
);
1037 else if (!strcmp(name
+ wholen
, "email"))
1038 v
->s
= copy_email(wholine
);
1039 else if (starts_with(name
+ wholen
, "date"))
1040 grab_date(wholine
, v
, name
);
1044 * For a tag or a commit object, if "creator" or "creatordate" is
1045 * requested, do something special.
1047 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
1048 return; /* "author" for commit object is not wanted */
1050 wholine
= find_wholine(who
, wholen
, buf
, sz
);
1053 for (i
= 0; i
< used_atom_cnt
; i
++) {
1054 const char *name
= used_atom
[i
].name
;
1055 struct atom_value
*v
= &val
[i
];
1056 if (!!deref
!= (*name
== '*'))
1061 if (starts_with(name
, "creatordate"))
1062 grab_date(wholine
, v
, name
);
1063 else if (!strcmp(name
, "creator"))
1064 v
->s
= copy_line(wholine
);
1068 static void find_subpos(const char *buf
, unsigned long sz
,
1069 const char **sub
, unsigned long *sublen
,
1070 const char **body
, unsigned long *bodylen
,
1071 unsigned long *nonsiglen
,
1072 const char **sig
, unsigned long *siglen
)
1075 /* skip past header until we hit empty line */
1076 while (*buf
&& *buf
!= '\n') {
1077 eol
= strchrnul(buf
, '\n');
1082 /* skip any empty lines */
1083 while (*buf
== '\n')
1086 /* parse signature first; we might not even have a subject line */
1087 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
1088 *siglen
= strlen(*sig
);
1090 /* subject is first non-empty line */
1092 /* subject goes to first empty line */
1093 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
1094 eol
= strchrnul(buf
, '\n');
1099 *sublen
= buf
- *sub
;
1100 /* drop trailing newline, if present */
1101 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
1104 /* skip any empty lines */
1105 while (*buf
== '\n')
1108 *bodylen
= strlen(buf
);
1109 *nonsiglen
= *sig
- buf
;
1113 * If 'lines' is greater than 0, append that many lines from the given
1114 * 'buf' of length 'size' to the given strbuf.
1116 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
1119 const char *sp
, *eol
;
1124 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
1126 strbuf_addstr(out
, "\n ");
1127 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
1128 len
= eol
? eol
- sp
: size
- (sp
- buf
);
1129 strbuf_add(out
, sp
, len
);
1136 /* See grab_values */
1137 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1140 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
1141 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
1143 for (i
= 0; i
< used_atom_cnt
; i
++) {
1144 struct used_atom
*atom
= &used_atom
[i
];
1145 const char *name
= atom
->name
;
1146 struct atom_value
*v
= &val
[i
];
1147 if (!!deref
!= (*name
== '*'))
1151 if (strcmp(name
, "subject") &&
1152 strcmp(name
, "body") &&
1153 !starts_with(name
, "trailers") &&
1154 !starts_with(name
, "contents"))
1157 find_subpos(buf
, sz
,
1159 &bodypos
, &bodylen
, &nonsiglen
,
1162 if (atom
->u
.contents
.option
== C_SUB
)
1163 v
->s
= copy_subject(subpos
, sublen
);
1164 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1165 v
->s
= xmemdupz(bodypos
, bodylen
);
1166 else if (atom
->u
.contents
.option
== C_BODY
)
1167 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1168 else if (atom
->u
.contents
.option
== C_SIG
)
1169 v
->s
= xmemdupz(sigpos
, siglen
);
1170 else if (atom
->u
.contents
.option
== C_LINES
) {
1171 struct strbuf s
= STRBUF_INIT
;
1172 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1174 /* Size is the length of the message after removing the signature */
1175 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1176 v
->s
= strbuf_detach(&s
, NULL
);
1177 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1178 struct strbuf s
= STRBUF_INIT
;
1180 /* Format the trailer info according to the trailer_opts given */
1181 format_trailers_from_commit(&s
, subpos
, &atom
->u
.contents
.trailer_opts
);
1183 v
->s
= strbuf_detach(&s
, NULL
);
1184 } else if (atom
->u
.contents
.option
== C_BARE
)
1185 v
->s
= xstrdup(subpos
);
1190 * We want to have empty print-string for field requests
1191 * that do not apply (e.g. "authordate" for a tag object)
1193 static void fill_missing_values(struct atom_value
*val
)
1196 for (i
= 0; i
< used_atom_cnt
; i
++) {
1197 struct atom_value
*v
= &val
[i
];
1204 * val is a list of atom_value to hold returned values. Extract
1205 * the values for atoms in used_atom array out of (obj, buf, sz).
1206 * when deref is false, (obj, buf, sz) is the object that is
1207 * pointed at by the ref itself; otherwise it is the object the
1208 * ref (which is a tag) refers to.
1210 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1212 grab_common_values(val
, deref
, obj
, buf
, sz
);
1213 switch (obj
->type
) {
1215 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1216 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1217 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1220 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1221 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1222 grab_person("author", val
, deref
, obj
, buf
, sz
);
1223 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1226 /* grab_tree_values(val, deref, obj, buf, sz); */
1229 /* grab_blob_values(val, deref, obj, buf, sz); */
1232 die("Eh? Object of type %d?", obj
->type
);
1236 static inline char *copy_advance(char *dst
, const char *src
)
1243 static const char *lstrip_ref_components(const char *refname
, int len
)
1245 long remaining
= len
;
1246 const char *start
= refname
;
1250 const char *p
= refname
;
1252 /* Find total no of '/' separated path-components */
1253 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1256 * The number of components we need to strip is now
1257 * the total minus the components to be left (Plus one
1258 * because we count the number of '/', but the number
1259 * of components is one more than the no of '/').
1261 remaining
= i
+ len
+ 1;
1264 while (remaining
> 0) {
1277 static const char *rstrip_ref_components(const char *refname
, int len
)
1279 long remaining
= len
;
1280 char *start
= xstrdup(refname
);
1284 const char *p
= refname
;
1286 /* Find total no of '/' separated path-components */
1287 for (i
= 0; p
[i
]; p
[i
] == '/' ? i
++ : *p
++)
1290 * The number of components we need to strip is now
1291 * the total minus the components to be left (Plus one
1292 * because we count the number of '/', but the number
1293 * of components is one more than the no of '/').
1295 remaining
= i
+ len
+ 1;
1298 while (remaining
-- > 0) {
1299 char *p
= strrchr(start
, '/');
1308 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1310 if (atom
->option
== R_SHORT
)
1311 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1312 else if (atom
->option
== R_LSTRIP
)
1313 return lstrip_ref_components(refname
, atom
->lstrip
);
1314 else if (atom
->option
== R_RSTRIP
)
1315 return rstrip_ref_components(refname
, atom
->rstrip
);
1320 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1321 struct branch
*branch
, const char **s
)
1323 int num_ours
, num_theirs
;
1324 if (atom
->u
.remote_ref
.option
== RR_REF
)
1325 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1326 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1327 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1328 NULL
, AHEAD_BEHIND_FULL
) < 0) {
1329 *s
= xstrdup(msgs
.gone
);
1330 } else if (!num_ours
&& !num_theirs
)
1333 *s
= xstrfmt(msgs
.behind
, num_theirs
);
1334 else if (!num_theirs
)
1335 *s
= xstrfmt(msgs
.ahead
, num_ours
);
1337 *s
= xstrfmt(msgs
.ahead_behind
,
1338 num_ours
, num_theirs
);
1339 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1340 const char *to_free
= *s
;
1341 *s
= xstrfmt("[%s]", *s
);
1342 free((void *)to_free
);
1344 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1345 if (stat_tracking_info(branch
, &num_ours
, &num_theirs
,
1346 NULL
, AHEAD_BEHIND_FULL
) < 0)
1349 if (!num_ours
&& !num_theirs
)
1353 else if (!num_theirs
)
1357 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_NAME
) {
1359 const char *remote
= atom
->u
.remote_ref
.push
?
1360 pushremote_for_branch(branch
, &explicit) :
1361 remote_for_branch(branch
, &explicit);
1363 *s
= xstrdup(remote
);
1366 } else if (atom
->u
.remote_ref
.option
== RR_REMOTE_REF
) {
1370 merge
= remote_ref_for_branch(branch
, atom
->u
.remote_ref
.push
,
1373 *s
= xstrdup(merge
);
1377 BUG("unhandled RR_* enum");
1380 char *get_head_description(void)
1382 struct strbuf desc
= STRBUF_INIT
;
1383 struct wt_status_state state
;
1384 memset(&state
, 0, sizeof(state
));
1385 wt_status_get_state(&state
, 1);
1386 if (state
.rebase_in_progress
||
1387 state
.rebase_interactive_in_progress
) {
1389 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1392 strbuf_addf(&desc
, _("(no branch, rebasing detached HEAD %s)"),
1393 state
.detached_from
);
1394 } else if (state
.bisect_in_progress
)
1395 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1397 else if (state
.detached_from
) {
1398 if (state
.detached_at
)
1400 * TRANSLATORS: make sure this matches "HEAD
1401 * detached at " in wt-status.c
1403 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1404 state
.detached_from
);
1407 * TRANSLATORS: make sure this matches "HEAD
1408 * detached from " in wt-status.c
1410 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1411 state
.detached_from
);
1414 strbuf_addstr(&desc
, _("(no branch)"));
1417 free(state
.detached_from
);
1418 return strbuf_detach(&desc
, NULL
);
1421 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1426 return show_ref(&atom
->u
.refname
, ref
->symref
);
1429 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1431 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1432 return get_head_description();
1433 return show_ref(&atom
->u
.refname
, ref
->refname
);
1436 static int get_object(struct ref_array_item
*ref
, const struct object_id
*oid
,
1437 int deref
, struct object
**obj
, struct strbuf
*err
)
1442 void *buf
= get_obj(oid
, obj
, &size
, &eaten
);
1444 ret
= strbuf_addf_ret(err
, -1, _("missing object %s for %s"),
1445 oid_to_hex(oid
), ref
->refname
);
1447 ret
= strbuf_addf_ret(err
, -1, _("parse_object_buffer failed on %s for %s"),
1448 oid_to_hex(oid
), ref
->refname
);
1450 grab_values(ref
->value
, deref
, *obj
, buf
, size
);
1457 * Parse the object referred by ref, and grab needed value.
1459 static int populate_value(struct ref_array_item
*ref
, struct strbuf
*err
)
1463 const struct object_id
*tagged
;
1465 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1467 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1468 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1474 /* Fill in specials first */
1475 for (i
= 0; i
< used_atom_cnt
; i
++) {
1476 struct used_atom
*atom
= &used_atom
[i
];
1477 const char *name
= used_atom
[i
].name
;
1478 struct atom_value
*v
= &ref
->value
[i
];
1480 const char *refname
;
1481 struct branch
*branch
= NULL
;
1483 v
->handler
= append_atom
;
1491 if (starts_with(name
, "refname"))
1492 refname
= get_refname(atom
, ref
);
1493 else if (starts_with(name
, "symref"))
1494 refname
= get_symref(atom
, ref
);
1495 else if (starts_with(name
, "upstream")) {
1496 const char *branch_name
;
1497 /* only local branches may have an upstream */
1498 if (!skip_prefix(ref
->refname
, "refs/heads/",
1501 branch
= branch_get(branch_name
);
1503 refname
= branch_get_upstream(branch
, NULL
);
1505 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1507 } else if (atom
->u
.remote_ref
.push
) {
1508 const char *branch_name
;
1509 if (!skip_prefix(ref
->refname
, "refs/heads/",
1512 branch
= branch_get(branch_name
);
1514 if (atom
->u
.remote_ref
.push_remote
)
1517 refname
= branch_get_push(branch
, NULL
);
1521 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1523 } else if (starts_with(name
, "color:")) {
1524 v
->s
= atom
->u
.color
;
1526 } else if (!strcmp(name
, "flag")) {
1527 char buf
[256], *cp
= buf
;
1528 if (ref
->flag
& REF_ISSYMREF
)
1529 cp
= copy_advance(cp
, ",symref");
1530 if (ref
->flag
& REF_ISPACKED
)
1531 cp
= copy_advance(cp
, ",packed");
1536 v
->s
= xstrdup(buf
+ 1);
1539 } else if (!deref
&& grab_objectname(name
, &ref
->objectname
, v
, atom
)) {
1541 } else if (!strcmp(name
, "HEAD")) {
1542 if (atom
->u
.head
&& !strcmp(ref
->refname
, atom
->u
.head
))
1547 } else if (starts_with(name
, "align")) {
1548 v
->handler
= align_atom_handler
;
1550 } else if (!strcmp(name
, "end")) {
1551 v
->handler
= end_atom_handler
;
1553 } else if (starts_with(name
, "if")) {
1556 if (skip_prefix(name
, "if:", &s
))
1558 v
->handler
= if_atom_handler
;
1560 } else if (!strcmp(name
, "then")) {
1561 v
->handler
= then_atom_handler
;
1563 } else if (!strcmp(name
, "else")) {
1564 v
->handler
= else_atom_handler
;
1572 v
->s
= xstrfmt("%s^{}", refname
);
1575 for (i
= 0; i
< used_atom_cnt
; i
++) {
1576 struct atom_value
*v
= &ref
->value
[i
];
1580 if (used_atom_cnt
<= i
)
1583 if (get_object(ref
, &ref
->objectname
, 0, &obj
, err
))
1587 * If there is no atom that wants to know about tagged
1588 * object, we are done.
1590 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1594 * If it is a tag object, see if we use a value that derefs
1595 * the object, and if we do grab the object it refers to.
1597 tagged
= &((struct tag
*)obj
)->tagged
->oid
;
1600 * NEEDSWORK: This derefs tag only once, which
1601 * is good to deal with chains of trust, but
1602 * is not consistent with what deref_tag() does
1603 * which peels the onion to the core.
1605 return get_object(ref
, tagged
, 1, &obj
, err
);
1609 * Given a ref, return the value for the atom. This lazily gets value
1610 * out of the object by calling populate value.
1612 static int get_ref_atom_value(struct ref_array_item
*ref
, int atom
,
1613 struct atom_value
**v
, struct strbuf
*err
)
1616 if (populate_value(ref
, err
))
1618 fill_missing_values(ref
->value
);
1620 *v
= &ref
->value
[atom
];
1625 * Unknown has to be "0" here, because that's the default value for
1626 * contains_cache slab entries that have not yet been assigned.
1628 enum contains_result
{
1629 CONTAINS_UNKNOWN
= 0,
1634 define_commit_slab(contains_cache
, enum contains_result
);
1636 struct ref_filter_cbdata
{
1637 struct ref_array
*array
;
1638 struct ref_filter
*filter
;
1639 struct contains_cache contains_cache
;
1640 struct contains_cache no_contains_cache
;
1644 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1647 * At each recursion step, the stack items points to the commits whose
1648 * ancestors are to be inspected.
1650 struct contains_stack
{
1652 struct contains_stack_entry
{
1653 struct commit
*commit
;
1654 struct commit_list
*parents
;
1658 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1660 for (; want
; want
= want
->next
)
1661 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1667 * Test whether the candidate is contained in the list.
1668 * Do not recurse to find out, though, but return -1 if inconclusive.
1670 static enum contains_result
contains_test(struct commit
*candidate
,
1671 const struct commit_list
*want
,
1672 struct contains_cache
*cache
,
1675 enum contains_result
*cached
= contains_cache_at(cache
, candidate
);
1677 /* If we already have the answer cached, return that. */
1682 if (in_commit_list(want
, candidate
)) {
1683 *cached
= CONTAINS_YES
;
1684 return CONTAINS_YES
;
1687 /* Otherwise, we don't know; prepare to recurse */
1688 parse_commit_or_die(candidate
);
1690 if (candidate
->generation
< cutoff
)
1693 return CONTAINS_UNKNOWN
;
1696 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1698 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1699 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1700 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1703 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1704 const struct commit_list
*want
,
1705 struct contains_cache
*cache
)
1707 struct contains_stack contains_stack
= { 0, 0, NULL
};
1708 enum contains_result result
;
1709 uint32_t cutoff
= GENERATION_NUMBER_INFINITY
;
1710 const struct commit_list
*p
;
1712 for (p
= want
; p
; p
= p
->next
) {
1713 struct commit
*c
= p
->item
;
1714 load_commit_graph_info(c
);
1715 if (c
->generation
< cutoff
)
1716 cutoff
= c
->generation
;
1719 result
= contains_test(candidate
, want
, cache
, cutoff
);
1720 if (result
!= CONTAINS_UNKNOWN
)
1723 push_to_contains_stack(candidate
, &contains_stack
);
1724 while (contains_stack
.nr
) {
1725 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1726 struct commit
*commit
= entry
->commit
;
1727 struct commit_list
*parents
= entry
->parents
;
1730 *contains_cache_at(cache
, commit
) = CONTAINS_NO
;
1731 contains_stack
.nr
--;
1734 * If we just popped the stack, parents->item has been marked,
1735 * therefore contains_test will return a meaningful yes/no.
1737 else switch (contains_test(parents
->item
, want
, cache
, cutoff
)) {
1739 *contains_cache_at(cache
, commit
) = CONTAINS_YES
;
1740 contains_stack
.nr
--;
1743 entry
->parents
= parents
->next
;
1745 case CONTAINS_UNKNOWN
:
1746 push_to_contains_stack(parents
->item
, &contains_stack
);
1750 free(contains_stack
.contains_stack
);
1751 return contains_test(candidate
, want
, cache
, cutoff
);
1754 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
,
1755 struct commit_list
*list
, struct contains_cache
*cache
)
1757 if (filter
->with_commit_tag_algo
)
1758 return contains_tag_algo(commit
, list
, cache
) == CONTAINS_YES
;
1759 return is_descendant_of(commit
, list
);
1763 * Return 1 if the refname matches one of the patterns, otherwise 0.
1764 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1765 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1766 * matches "refs/heads/mas*", too).
1768 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1770 const char **patterns
= filter
->name_patterns
;
1773 if (filter
->ignore_case
)
1774 flags
|= WM_CASEFOLD
;
1777 * When no '--format' option is given we need to skip the prefix
1778 * for matching refs of tags and branches.
1780 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1781 skip_prefix(refname
, "refs/heads/", &refname
) ||
1782 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1783 skip_prefix(refname
, "refs/", &refname
));
1785 for (; *patterns
; patterns
++) {
1786 if (!wildmatch(*patterns
, refname
, flags
))
1793 * Return 1 if the refname matches one of the patterns, otherwise 0.
1794 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1795 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1796 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1798 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1800 const char **pattern
= filter
->name_patterns
;
1801 int namelen
= strlen(refname
);
1802 unsigned flags
= WM_PATHNAME
;
1804 if (filter
->ignore_case
)
1805 flags
|= WM_CASEFOLD
;
1807 for (; *pattern
; pattern
++) {
1808 const char *p
= *pattern
;
1809 int plen
= strlen(p
);
1811 if ((plen
<= namelen
) &&
1812 !strncmp(refname
, p
, plen
) &&
1813 (refname
[plen
] == '\0' ||
1814 refname
[plen
] == '/' ||
1817 if (!wildmatch(p
, refname
, WM_PATHNAME
))
1823 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1824 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1826 if (!*filter
->name_patterns
)
1827 return 1; /* No pattern always matches */
1828 if (filter
->match_as_path
)
1829 return match_name_as_path(filter
, refname
);
1830 return match_pattern(filter
, refname
);
1834 * Find the longest prefix of pattern we can pass to
1835 * `for_each_fullref_in()`, namely the part of pattern preceding the
1836 * first glob character. (Note that `for_each_fullref_in()` is
1837 * perfectly happy working with a prefix that doesn't end at a
1838 * pathname component boundary.)
1840 static void find_longest_prefix(struct strbuf
*out
, const char *pattern
)
1844 for (p
= pattern
; *p
&& !is_glob_special(*p
); p
++)
1847 strbuf_add(out
, pattern
, p
- pattern
);
1851 * This is the same as for_each_fullref_in(), but it tries to iterate
1852 * only over the patterns we'll care about. Note that it _doesn't_ do a full
1853 * pattern match, so the callback still has to match each ref individually.
1855 static int for_each_fullref_in_pattern(struct ref_filter
*filter
,
1860 struct strbuf prefix
= STRBUF_INIT
;
1863 if (!filter
->match_as_path
) {
1865 * in this case, the patterns are applied after
1866 * prefixes like "refs/heads/" etc. are stripped off,
1867 * so we have to look at everything:
1869 return for_each_fullref_in("", cb
, cb_data
, broken
);
1872 if (!filter
->name_patterns
[0]) {
1873 /* no patterns; we have to look at everything */
1874 return for_each_fullref_in("", cb
, cb_data
, broken
);
1877 if (filter
->name_patterns
[1]) {
1879 * multiple patterns; in theory this could still work as long
1880 * as the patterns are disjoint. We'd just make multiple calls
1881 * to for_each_ref(). But if they're not disjoint, we'd end up
1882 * reporting the same ref multiple times. So let's punt on that
1885 return for_each_fullref_in("", cb
, cb_data
, broken
);
1888 find_longest_prefix(&prefix
, filter
->name_patterns
[0]);
1890 ret
= for_each_fullref_in(prefix
.buf
, cb
, cb_data
, broken
);
1891 strbuf_release(&prefix
);
1896 * Given a ref (sha1, refname), check if the ref belongs to the array
1897 * of sha1s. If the given ref is a tag, check if the given tag points
1898 * at one of the sha1s in the given sha1 array.
1899 * the given sha1_array.
1901 * 1. Only a single level of inderection is obtained, we might want to
1902 * change this to account for multiple levels (e.g. annotated tags
1903 * pointing to annotated tags pointing to a commit.)
1904 * 2. As the refs are cached we might know what refname peels to without
1905 * the need to parse the object via parse_object(). peel_ref() might be a
1906 * more efficient alternative to obtain the pointee.
1908 static const struct object_id
*match_points_at(struct oid_array
*points_at
,
1909 const struct object_id
*oid
,
1910 const char *refname
)
1912 const struct object_id
*tagged_oid
= NULL
;
1915 if (oid_array_lookup(points_at
, oid
) >= 0)
1917 obj
= parse_object(oid
);
1919 die(_("malformed object at '%s'"), refname
);
1920 if (obj
->type
== OBJ_TAG
)
1921 tagged_oid
= &((struct tag
*)obj
)->tagged
->oid
;
1922 if (tagged_oid
&& oid_array_lookup(points_at
, tagged_oid
) >= 0)
1928 * Allocate space for a new ref_array_item and copy the name and oid to it.
1930 * Callers can then fill in other struct members at their leisure.
1932 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1933 const struct object_id
*oid
)
1935 struct ref_array_item
*ref
;
1937 FLEX_ALLOC_STR(ref
, refname
, refname
);
1938 oidcpy(&ref
->objectname
, oid
);
1943 struct ref_array_item
*ref_array_push(struct ref_array
*array
,
1944 const char *refname
,
1945 const struct object_id
*oid
)
1947 struct ref_array_item
*ref
= new_ref_array_item(refname
, oid
);
1949 ALLOC_GROW(array
->items
, array
->nr
+ 1, array
->alloc
);
1950 array
->items
[array
->nr
++] = ref
;
1955 static int ref_kind_from_refname(const char *refname
)
1963 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1964 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1965 { "refs/tags/", FILTER_REFS_TAGS
}
1968 if (!strcmp(refname
, "HEAD"))
1969 return FILTER_REFS_DETACHED_HEAD
;
1971 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1972 if (starts_with(refname
, ref_kind
[i
].prefix
))
1973 return ref_kind
[i
].kind
;
1976 return FILTER_REFS_OTHERS
;
1979 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1981 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1982 filter
->kind
== FILTER_REFS_REMOTES
||
1983 filter
->kind
== FILTER_REFS_TAGS
)
1984 return filter
->kind
;
1985 return ref_kind_from_refname(refname
);
1989 * A call-back given to for_each_ref(). Filter refs and keep them for
1990 * later object processing.
1992 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1994 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1995 struct ref_filter
*filter
= ref_cbdata
->filter
;
1996 struct ref_array_item
*ref
;
1997 struct commit
*commit
= NULL
;
2000 if (flag
& REF_BAD_NAME
) {
2001 warning(_("ignoring ref with broken name %s"), refname
);
2005 if (flag
& REF_ISBROKEN
) {
2006 warning(_("ignoring broken ref %s"), refname
);
2010 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
2011 kind
= filter_ref_kind(filter
, refname
);
2012 if (!(kind
& filter
->kind
))
2015 if (!filter_pattern_match(filter
, refname
))
2018 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
, refname
))
2022 * A merge filter is applied on refs pointing to commits. Hence
2023 * obtain the commit using the 'oid' available and discard all
2024 * non-commits early. The actual filtering is done later.
2026 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->no_commit
|| filter
->verbose
) {
2027 commit
= lookup_commit_reference_gently(oid
, 1);
2030 /* We perform the filtering for the '--contains' option... */
2031 if (filter
->with_commit
&&
2032 !commit_contains(filter
, commit
, filter
->with_commit
, &ref_cbdata
->contains_cache
))
2034 /* ...or for the `--no-contains' option */
2035 if (filter
->no_commit
&&
2036 commit_contains(filter
, commit
, filter
->no_commit
, &ref_cbdata
->no_contains_cache
))
2041 * We do not open the object yet; sort may only need refname
2042 * to do its job and the resulting list may yet to be pruned
2043 * by maxcount logic.
2045 ref
= ref_array_push(ref_cbdata
->array
, refname
, oid
);
2046 ref
->commit
= commit
;
2053 /* Free memory allocated for a ref_array_item */
2054 static void free_array_item(struct ref_array_item
*item
)
2056 free((char *)item
->symref
);
2060 /* Free all memory allocated for ref_array */
2061 void ref_array_clear(struct ref_array
*array
)
2065 for (i
= 0; i
< array
->nr
; i
++)
2066 free_array_item(array
->items
[i
]);
2067 FREE_AND_NULL(array
->items
);
2068 array
->nr
= array
->alloc
= 0;
2071 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
2073 struct rev_info revs
;
2075 struct ref_filter
*filter
= ref_cbdata
->filter
;
2076 struct ref_array
*array
= ref_cbdata
->array
;
2077 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
2079 init_revisions(&revs
, NULL
);
2081 for (i
= 0; i
< array
->nr
; i
++) {
2082 struct ref_array_item
*item
= array
->items
[i
];
2083 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
2084 to_clear
[i
] = item
->commit
;
2087 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
2088 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
2091 if (prepare_revision_walk(&revs
))
2092 die(_("revision walk setup failed"));
2097 for (i
= 0; i
< old_nr
; i
++) {
2098 struct ref_array_item
*item
= array
->items
[i
];
2099 struct commit
*commit
= item
->commit
;
2101 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
2103 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
2104 array
->items
[array
->nr
++] = array
->items
[i
];
2106 free_array_item(item
);
2109 clear_commit_marks_many(old_nr
, to_clear
, ALL_REV_FLAGS
);
2110 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
2115 * API for filtering a set of refs. Based on the type of refs the user
2116 * has requested, we iterate through those refs and apply filters
2117 * as per the given ref_filter structure and finally store the
2118 * filtered refs in the ref_array structure.
2120 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
2122 struct ref_filter_cbdata ref_cbdata
;
2124 unsigned int broken
= 0;
2126 ref_cbdata
.array
= array
;
2127 ref_cbdata
.filter
= filter
;
2129 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
2131 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
2133 init_contains_cache(&ref_cbdata
.contains_cache
);
2134 init_contains_cache(&ref_cbdata
.no_contains_cache
);
2136 /* Simple per-ref filtering */
2138 die("filter_refs: invalid type");
2141 * For common cases where we need only branches or remotes or tags,
2142 * we only iterate through those refs. If a mix of refs is needed,
2143 * we iterate over all refs and filter out required refs with the help
2144 * of filter_ref_kind().
2146 if (filter
->kind
== FILTER_REFS_BRANCHES
)
2147 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
2148 else if (filter
->kind
== FILTER_REFS_REMOTES
)
2149 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
2150 else if (filter
->kind
== FILTER_REFS_TAGS
)
2151 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
2152 else if (filter
->kind
& FILTER_REFS_ALL
)
2153 ret
= for_each_fullref_in_pattern(filter
, ref_filter_handler
, &ref_cbdata
, broken
);
2154 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
2155 head_ref(ref_filter_handler
, &ref_cbdata
);
2158 clear_contains_cache(&ref_cbdata
.contains_cache
);
2159 clear_contains_cache(&ref_cbdata
.no_contains_cache
);
2161 /* Filters that need revision walking */
2162 if (filter
->merge_commit
)
2163 do_merge_filter(&ref_cbdata
);
2168 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
2170 struct atom_value
*va
, *vb
;
2172 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
2173 int (*cmp_fn
)(const char *, const char *);
2174 struct strbuf err
= STRBUF_INIT
;
2176 if (get_ref_atom_value(a
, s
->atom
, &va
, &err
))
2178 if (get_ref_atom_value(b
, s
->atom
, &vb
, &err
))
2180 strbuf_release(&err
);
2181 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
2183 cmp
= versioncmp(va
->s
, vb
->s
);
2184 else if (cmp_type
== FIELD_STR
)
2185 cmp
= cmp_fn(va
->s
, vb
->s
);
2187 if (va
->value
< vb
->value
)
2189 else if (va
->value
== vb
->value
)
2190 cmp
= cmp_fn(a
->refname
, b
->refname
);
2195 return (s
->reverse
) ? -cmp
: cmp
;
2198 static int compare_refs(const void *a_
, const void *b_
, void *ref_sorting
)
2200 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
2201 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
2202 struct ref_sorting
*s
;
2204 for (s
= ref_sorting
; s
; s
= s
->next
) {
2205 int cmp
= cmp_ref_sorting(s
, a
, b
);
2212 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
2214 QSORT_S(array
->items
, array
->nr
, compare_refs
, sorting
);
2217 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
2219 struct strbuf
*s
= &state
->stack
->output
;
2221 while (*cp
&& (!ep
|| cp
< ep
)) {
2226 int ch
= hex2chr(cp
+ 1);
2228 strbuf_addch(s
, ch
);
2234 strbuf_addch(s
, *cp
);
2239 int format_ref_array_item(struct ref_array_item
*info
,
2240 const struct ref_format
*format
,
2241 struct strbuf
*final_buf
,
2242 struct strbuf
*error_buf
)
2244 const char *cp
, *sp
, *ep
;
2245 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
2247 state
.quote_style
= format
->quote_style
;
2248 push_stack_element(&state
.stack
);
2250 for (cp
= format
->format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
2251 struct atom_value
*atomv
;
2254 ep
= strchr(sp
, ')');
2256 append_literal(cp
, sp
, &state
);
2257 pos
= parse_ref_filter_atom(format
, sp
+ 2, ep
, error_buf
);
2258 if (pos
< 0 || get_ref_atom_value(info
, pos
, &atomv
, error_buf
) ||
2259 atomv
->handler(atomv
, &state
, error_buf
)) {
2260 pop_stack_element(&state
.stack
);
2265 sp
= cp
+ strlen(cp
);
2266 append_literal(cp
, sp
, &state
);
2268 if (format
->need_color_reset_at_eol
) {
2269 struct atom_value resetv
;
2270 resetv
.s
= GIT_COLOR_RESET
;
2271 if (append_atom(&resetv
, &state
, error_buf
)) {
2272 pop_stack_element(&state
.stack
);
2276 if (state
.stack
->prev
) {
2277 pop_stack_element(&state
.stack
);
2278 return strbuf_addf_ret(error_buf
, -1, _("format: %%(end) atom missing"));
2280 strbuf_addbuf(final_buf
, &state
.stack
->output
);
2281 pop_stack_element(&state
.stack
);
2285 void show_ref_array_item(struct ref_array_item
*info
,
2286 const struct ref_format
*format
)
2288 struct strbuf final_buf
= STRBUF_INIT
;
2289 struct strbuf error_buf
= STRBUF_INIT
;
2291 if (format_ref_array_item(info
, format
, &final_buf
, &error_buf
))
2292 die("%s", error_buf
.buf
);
2293 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
2294 strbuf_release(&error_buf
);
2295 strbuf_release(&final_buf
);
2299 void pretty_print_ref(const char *name
, const struct object_id
*oid
,
2300 const struct ref_format
*format
)
2302 struct ref_array_item
*ref_item
;
2303 ref_item
= new_ref_array_item(name
, oid
);
2304 ref_item
->kind
= ref_kind_from_refname(name
);
2305 show_ref_array_item(ref_item
, format
);
2306 free_array_item(ref_item
);
2309 static int parse_sorting_atom(const char *atom
)
2312 * This parses an atom using a dummy ref_format, since we don't
2313 * actually care about the formatting details.
2315 struct ref_format dummy
= REF_FORMAT_INIT
;
2316 const char *end
= atom
+ strlen(atom
);
2317 struct strbuf err
= STRBUF_INIT
;
2318 int res
= parse_ref_filter_atom(&dummy
, atom
, end
, &err
);
2321 strbuf_release(&err
);
2325 /* If no sorting option is given, use refname to sort as default */
2326 struct ref_sorting
*ref_default_sorting(void)
2328 static const char cstr_name
[] = "refname";
2330 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
2332 sorting
->next
= NULL
;
2333 sorting
->atom
= parse_sorting_atom(cstr_name
);
2337 void parse_ref_sorting(struct ref_sorting
**sorting_tail
, const char *arg
)
2339 struct ref_sorting
*s
;
2341 s
= xcalloc(1, sizeof(*s
));
2342 s
->next
= *sorting_tail
;
2349 if (skip_prefix(arg
, "version:", &arg
) ||
2350 skip_prefix(arg
, "v:", &arg
))
2352 s
->atom
= parse_sorting_atom(arg
);
2355 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
2357 if (!arg
) /* should --no-sort void the list ? */
2359 parse_ref_sorting(opt
->value
, arg
);
2363 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
2365 struct ref_filter
*rf
= opt
->value
;
2366 struct object_id oid
;
2367 int no_merged
= starts_with(opt
->long_name
, "no");
2371 return opterror(opt
, "is incompatible with --merged", 0);
2373 return opterror(opt
, "is incompatible with --no-merged", 0);
2377 rf
->merge
= no_merged
2378 ? REF_FILTER_MERGED_OMIT
2379 : REF_FILTER_MERGED_INCLUDE
;
2381 if (get_oid(arg
, &oid
))
2382 die(_("malformed object name %s"), arg
);
2384 rf
->merge_commit
= lookup_commit_reference_gently(&oid
, 0);
2385 if (!rf
->merge_commit
)
2386 return opterror(opt
, "must point to a commit", 0);