3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 #include "wt-status.h"
19 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
20 typedef enum { COMPARE_EQUAL
, COMPARE_UNEQUAL
, COMPARE_NONE
} cmp_status
;
28 cmp_status cmp_status
;
30 unsigned int then_atom_seen
: 1,
32 condition_satisfied
: 1;
36 enum { R_NORMAL
, R_SHORT
, R_STRIP
} option
;
41 * An atom is a valid field atom listed below, possibly prefixed with
42 * a "*" to denote deref_tag().
44 * We parse given format string and sort specifiers, and make a list
45 * of properties that we need to extract out of objects. ref_array_item
46 * structure will hold an array of values extracted that can be
47 * indexed with the "atom number", which is an index into this
50 static struct used_atom
{
54 char color
[COLOR_MAXLEN
];
57 enum { RR_REF
, RR_TRACK
, RR_TRACKSHORT
} option
;
58 struct refname_atom refname
;
59 unsigned int nobracket
: 1;
62 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
, C_TRAILERS
} option
;
66 cmp_status cmp_status
;
70 enum { O_FULL
, O_LENGTH
, O_SHORT
} option
;
73 struct refname_atom refname
;
76 static int used_atom_cnt
, need_tagged
, need_symref
;
77 static int need_color_reset_at_eol
;
79 static void color_atom_parser(struct used_atom
*atom
, const char *color_value
)
82 die(_("expected format: %%(color:<color>)"));
83 if (color_parse(color_value
, atom
->u
.color
) < 0)
84 die(_("unrecognized color: %%(color:%s)"), color_value
);
87 static void refname_atom_parser_internal(struct refname_atom
*atom
,
88 const char *arg
, const char *name
)
91 atom
->option
= R_NORMAL
;
92 else if (!strcmp(arg
, "short"))
93 atom
->option
= R_SHORT
;
94 else if (skip_prefix(arg
, "strip=", &arg
)) {
95 atom
->option
= R_STRIP
;
96 if (strtoul_ui(arg
, 10, &atom
->strip
) || atom
->strip
<= 0)
97 die(_("positive value expected refname:strip=%s"), arg
);
99 die(_("unrecognized %%(%s) argument: %s"), name
, arg
);
102 static void remote_ref_atom_parser(struct used_atom
*atom
, const char *arg
)
104 struct string_list params
= STRING_LIST_INIT_DUP
;
108 atom
->u
.remote_ref
.option
= RR_REF
;
109 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
114 atom
->u
.remote_ref
.nobracket
= 0;
115 string_list_split(¶ms
, arg
, ',', -1);
117 for (i
= 0; i
< params
.nr
; i
++) {
118 const char *s
= params
.items
[i
].string
;
120 if (!strcmp(s
, "track"))
121 atom
->u
.remote_ref
.option
= RR_TRACK
;
122 else if (!strcmp(s
, "trackshort"))
123 atom
->u
.remote_ref
.option
= RR_TRACKSHORT
;
124 else if (!strcmp(s
, "nobracket"))
125 atom
->u
.remote_ref
.nobracket
= 1;
127 atom
->u
.remote_ref
.option
= RR_REF
;
128 refname_atom_parser_internal(&atom
->u
.remote_ref
.refname
,
133 string_list_clear(¶ms
, 0);
136 static void body_atom_parser(struct used_atom
*atom
, const char *arg
)
139 die(_("%%(body) does not take arguments"));
140 atom
->u
.contents
.option
= C_BODY_DEP
;
143 static void subject_atom_parser(struct used_atom
*atom
, const char *arg
)
146 die(_("%%(subject) does not take arguments"));
147 atom
->u
.contents
.option
= C_SUB
;
150 static void trailers_atom_parser(struct used_atom
*atom
, const char *arg
)
153 die(_("%%(trailers) does not take arguments"));
154 atom
->u
.contents
.option
= C_TRAILERS
;
157 static void contents_atom_parser(struct used_atom
*atom
, const char *arg
)
160 atom
->u
.contents
.option
= C_BARE
;
161 else if (!strcmp(arg
, "body"))
162 atom
->u
.contents
.option
= C_BODY
;
163 else if (!strcmp(arg
, "signature"))
164 atom
->u
.contents
.option
= C_SIG
;
165 else if (!strcmp(arg
, "subject"))
166 atom
->u
.contents
.option
= C_SUB
;
167 else if (!strcmp(arg
, "trailers"))
168 atom
->u
.contents
.option
= C_TRAILERS
;
169 else if (skip_prefix(arg
, "lines=", &arg
)) {
170 atom
->u
.contents
.option
= C_LINES
;
171 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
172 die(_("positive value expected contents:lines=%s"), arg
);
174 die(_("unrecognized %%(contents) argument: %s"), arg
);
177 static void objectname_atom_parser(struct used_atom
*atom
, const char *arg
)
180 atom
->u
.objectname
.option
= O_FULL
;
181 else if (!strcmp(arg
, "short"))
182 atom
->u
.objectname
.option
= O_SHORT
;
183 else if (skip_prefix(arg
, "short=", &arg
)) {
184 atom
->u
.objectname
.option
= O_LENGTH
;
185 if (strtoul_ui(arg
, 10, &atom
->u
.objectname
.length
) ||
186 atom
->u
.objectname
.length
== 0)
187 die(_("positive value expected objectname:short=%s"), arg
);
188 if (atom
->u
.objectname
.length
< MINIMUM_ABBREV
)
189 atom
->u
.objectname
.length
= MINIMUM_ABBREV
;
191 die(_("unrecognized %%(objectname) argument: %s"), arg
);
194 static void refname_atom_parser(struct used_atom
*atom
, const char *arg
)
196 return refname_atom_parser_internal(&atom
->u
.refname
, arg
, atom
->name
);
199 static align_type
parse_align_position(const char *s
)
201 if (!strcmp(s
, "right"))
203 else if (!strcmp(s
, "middle"))
205 else if (!strcmp(s
, "left"))
210 static void align_atom_parser(struct used_atom
*atom
, const char *arg
)
212 struct align
*align
= &atom
->u
.align
;
213 struct string_list params
= STRING_LIST_INIT_DUP
;
215 unsigned int width
= ~0U;
218 die(_("expected format: %%(align:<width>,<position>)"));
220 align
->position
= ALIGN_LEFT
;
222 string_list_split(¶ms
, arg
, ',', -1);
223 for (i
= 0; i
< params
.nr
; i
++) {
224 const char *s
= params
.items
[i
].string
;
227 if (skip_prefix(s
, "position=", &s
)) {
228 position
= parse_align_position(s
);
230 die(_("unrecognized position:%s"), s
);
231 align
->position
= position
;
232 } else if (skip_prefix(s
, "width=", &s
)) {
233 if (strtoul_ui(s
, 10, &width
))
234 die(_("unrecognized width:%s"), s
);
235 } else if (!strtoul_ui(s
, 10, &width
))
237 else if ((position
= parse_align_position(s
)) >= 0)
238 align
->position
= position
;
240 die(_("unrecognized %%(align) argument: %s"), s
);
244 die(_("positive width expected with the %%(align) atom"));
245 align
->width
= width
;
246 string_list_clear(¶ms
, 0);
249 static void if_atom_parser(struct used_atom
*atom
, const char *arg
)
252 atom
->u
.if_then_else
.cmp_status
= COMPARE_NONE
;
254 } else if (skip_prefix(arg
, "equals=", &atom
->u
.if_then_else
.str
)) {
255 atom
->u
.if_then_else
.cmp_status
= COMPARE_EQUAL
;
256 } else if (skip_prefix(arg
, "notequals=", &atom
->u
.if_then_else
.str
)) {
257 atom
->u
.if_then_else
.cmp_status
= COMPARE_UNEQUAL
;
259 die(_("unrecognized %%(if) argument: %s"), arg
);
267 void (*parser
)(struct used_atom
*atom
, const char *arg
);
269 { "refname" , FIELD_STR
, refname_atom_parser
},
271 { "objectsize", FIELD_ULONG
},
272 { "objectname", FIELD_STR
, objectname_atom_parser
},
275 { "numparent", FIELD_ULONG
},
282 { "authordate", FIELD_TIME
},
285 { "committeremail" },
286 { "committerdate", FIELD_TIME
},
290 { "taggerdate", FIELD_TIME
},
292 { "creatordate", FIELD_TIME
},
293 { "subject", FIELD_STR
, subject_atom_parser
},
294 { "body", FIELD_STR
, body_atom_parser
},
295 { "trailers", FIELD_STR
, trailers_atom_parser
},
296 { "contents", FIELD_STR
, contents_atom_parser
},
297 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
298 { "push", FIELD_STR
, remote_ref_atom_parser
},
299 { "symref", FIELD_STR
, refname_atom_parser
},
302 { "color", FIELD_STR
, color_atom_parser
},
303 { "align", FIELD_STR
, align_atom_parser
},
305 { "if", FIELD_STR
, if_atom_parser
},
310 #define REF_FORMATTING_STATE_INIT { 0, NULL }
312 struct ref_formatting_stack
{
313 struct ref_formatting_stack
*prev
;
314 struct strbuf output
;
315 void (*at_end
)(struct ref_formatting_stack
**stack
);
319 struct ref_formatting_state
{
321 struct ref_formatting_stack
*stack
;
326 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
327 unsigned long ul
; /* used for sorting when not FIELD_STR */
328 struct used_atom
*atom
;
332 * Used to parse format string and sort specifiers
334 int parse_ref_filter_atom(const char *atom
, const char *ep
)
341 if (*sp
== '*' && sp
< ep
)
344 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
346 /* Do we have the atom already used elsewhere? */
347 for (i
= 0; i
< used_atom_cnt
; i
++) {
348 int len
= strlen(used_atom
[i
].name
);
349 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
354 * If the atom name has a colon, strip it and everything after
355 * it off - it specifies the format for this entry, and
356 * shouldn't be used for checking against the valid_atom
359 arg
= memchr(sp
, ':', ep
- sp
);
360 atom_len
= (arg
? arg
: ep
) - sp
;
362 /* Is the atom a valid one? */
363 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
364 int len
= strlen(valid_atom
[i
].name
);
365 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
369 if (ARRAY_SIZE(valid_atom
) <= i
)
370 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
372 /* Add it in, including the deref prefix */
375 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
376 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
377 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
379 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
380 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
381 if (valid_atom
[i
].parser
)
382 valid_atom
[i
].parser(&used_atom
[at
], arg
);
385 if (!strcmp(valid_atom
[i
].name
, "symref"))
390 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
392 switch (quote_style
) {
394 strbuf_addstr(s
, str
);
397 sq_quote_buf(s
, str
);
400 perl_quote_buf(s
, str
);
403 python_quote_buf(s
, str
);
406 tcl_quote_buf(s
, str
);
411 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
414 * Quote formatting is only done when the stack has a single
415 * element. Otherwise quote formatting is done on the
416 * element's entire output strbuf when the %(end) atom is
419 if (!state
->stack
->prev
)
420 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
422 strbuf_addstr(&state
->stack
->output
, v
->s
);
425 static void push_stack_element(struct ref_formatting_stack
**stack
)
427 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
429 strbuf_init(&s
->output
, 0);
434 static void pop_stack_element(struct ref_formatting_stack
**stack
)
436 struct ref_formatting_stack
*current
= *stack
;
437 struct ref_formatting_stack
*prev
= current
->prev
;
440 strbuf_addbuf(&prev
->output
, ¤t
->output
);
441 strbuf_release(¤t
->output
);
446 static void end_align_handler(struct ref_formatting_stack
**stack
)
448 struct ref_formatting_stack
*cur
= *stack
;
449 struct align
*align
= (struct align
*)cur
->at_end_data
;
450 struct strbuf s
= STRBUF_INIT
;
452 strbuf_utf8_align(&s
, align
->position
, align
->width
, cur
->output
.buf
);
453 strbuf_swap(&cur
->output
, &s
);
457 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
459 struct ref_formatting_stack
*new;
461 push_stack_element(&state
->stack
);
463 new->at_end
= end_align_handler
;
464 new->at_end_data
= &atomv
->atom
->u
.align
;
467 static void if_then_else_handler(struct ref_formatting_stack
**stack
)
469 struct ref_formatting_stack
*cur
= *stack
;
470 struct ref_formatting_stack
*prev
= cur
->prev
;
471 struct if_then_else
*if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
473 if (!if_then_else
->then_atom_seen
)
474 die(_("format: %%(if) atom used without a %%(then) atom"));
476 if (if_then_else
->else_atom_seen
) {
478 * There is an %(else) atom: we need to drop one state from the
479 * stack, either the %(else) branch if the condition is satisfied, or
480 * the %(then) branch if it isn't.
482 if (if_then_else
->condition_satisfied
) {
483 strbuf_reset(&cur
->output
);
484 pop_stack_element(&cur
);
486 strbuf_swap(&cur
->output
, &prev
->output
);
487 strbuf_reset(&cur
->output
);
488 pop_stack_element(&cur
);
490 } else if (!if_then_else
->condition_satisfied
) {
492 * No %(else) atom: just drop the %(then) branch if the
493 * condition is not satisfied.
495 strbuf_reset(&cur
->output
);
502 static void if_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
504 struct ref_formatting_stack
*new;
505 struct if_then_else
*if_then_else
= xcalloc(sizeof(struct if_then_else
), 1);
507 if_then_else
->str
= atomv
->atom
->u
.if_then_else
.str
;
508 if_then_else
->cmp_status
= atomv
->atom
->u
.if_then_else
.cmp_status
;
510 push_stack_element(&state
->stack
);
512 new->at_end
= if_then_else_handler
;
513 new->at_end_data
= if_then_else
;
516 static int is_empty(const char *s
)
526 static void then_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
528 struct ref_formatting_stack
*cur
= state
->stack
;
529 struct if_then_else
*if_then_else
= NULL
;
531 if (cur
->at_end
== if_then_else_handler
)
532 if_then_else
= (struct if_then_else
*)cur
->at_end_data
;
534 die(_("format: %%(then) atom used without an %%(if) atom"));
535 if (if_then_else
->then_atom_seen
)
536 die(_("format: %%(then) atom used more than once"));
537 if (if_then_else
->else_atom_seen
)
538 die(_("format: %%(then) atom used after %%(else)"));
539 if_then_else
->then_atom_seen
= 1;
541 * If the 'equals' or 'notequals' attribute is used then
542 * perform the required comparison. If not, only non-empty
543 * strings satisfy the 'if' condition.
545 if (if_then_else
->cmp_status
== COMPARE_EQUAL
) {
546 if (!strcmp(if_then_else
->str
, cur
->output
.buf
))
547 if_then_else
->condition_satisfied
= 1;
548 } else if (if_then_else
->cmp_status
== COMPARE_UNEQUAL
) {
549 if (strcmp(if_then_else
->str
, cur
->output
.buf
))
550 if_then_else
->condition_satisfied
= 1;
551 } else if (cur
->output
.len
&& !is_empty(cur
->output
.buf
))
552 if_then_else
->condition_satisfied
= 1;
553 strbuf_reset(&cur
->output
);
556 static void else_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
558 struct ref_formatting_stack
*prev
= state
->stack
;
559 struct if_then_else
*if_then_else
= NULL
;
561 if (prev
->at_end
== if_then_else_handler
)
562 if_then_else
= (struct if_then_else
*)prev
->at_end_data
;
564 die(_("format: %%(else) atom used without an %%(if) atom"));
565 if (!if_then_else
->then_atom_seen
)
566 die(_("format: %%(else) atom used without a %%(then) atom"));
567 if (if_then_else
->else_atom_seen
)
568 die(_("format: %%(else) atom used more than once"));
569 if_then_else
->else_atom_seen
= 1;
570 push_stack_element(&state
->stack
);
571 state
->stack
->at_end_data
= prev
->at_end_data
;
572 state
->stack
->at_end
= prev
->at_end
;
575 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
577 struct ref_formatting_stack
*current
= state
->stack
;
578 struct strbuf s
= STRBUF_INIT
;
580 if (!current
->at_end
)
581 die(_("format: %%(end) atom used without corresponding atom"));
582 current
->at_end(&state
->stack
);
584 /* Stack may have been popped within at_end(), hence reset the current pointer */
585 current
= state
->stack
;
588 * Perform quote formatting when the stack element is that of
589 * a supporting atom. If nested then perform quote formatting
590 * only on the topmost supporting atom.
592 if (!current
->prev
->prev
) {
593 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
594 strbuf_swap(¤t
->output
, &s
);
597 pop_stack_element(&state
->stack
);
601 * In a format string, find the next occurrence of %(atom).
603 static const char *find_next(const char *cp
)
608 * %( is the start of an atom;
609 * %% is a quoted per-cent.
613 else if (cp
[1] == '%')
614 cp
++; /* skip over two % */
615 /* otherwise this is a singleton, literal % */
623 * Make sure the format string is well formed, and parse out
626 int verify_ref_format(const char *format
)
630 need_color_reset_at_eol
= 0;
631 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
632 const char *color
, *ep
= strchr(sp
, ')');
636 return error(_("malformed format string %s"), sp
);
637 /* sp points at "%(" and ep points at the closing ")" */
638 at
= parse_ref_filter_atom(sp
+ 2, ep
);
641 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
642 need_color_reset_at_eol
= !!strcmp(color
, "reset");
648 * Given an object name, read the object data and size, and return a
649 * "struct object". If the object data we are returning is also borrowed
650 * by the "struct object" representation, set *eaten as well---it is a
651 * signal from parse_object_buffer to us not to free the buffer.
653 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
655 enum object_type type
;
656 void *buf
= read_sha1_file(sha1
, &type
, sz
);
659 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
665 static int grab_objectname(const char *name
, const unsigned char *sha1
,
666 struct atom_value
*v
, struct used_atom
*atom
)
668 if (starts_with(name
, "objectname")) {
669 if (atom
->u
.objectname
.option
== O_SHORT
) {
670 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
672 } else if (atom
->u
.objectname
.option
== O_FULL
) {
673 v
->s
= xstrdup(sha1_to_hex(sha1
));
675 } else if (atom
->u
.objectname
.option
== O_LENGTH
) {
676 v
->s
= xstrdup(find_unique_abbrev(sha1
, atom
->u
.objectname
.length
));
679 die("BUG: unknown %%(objectname) option");
684 /* See grab_values */
685 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
689 for (i
= 0; i
< used_atom_cnt
; i
++) {
690 const char *name
= used_atom
[i
].name
;
691 struct atom_value
*v
= &val
[i
];
692 if (!!deref
!= (*name
== '*'))
696 if (!strcmp(name
, "objecttype"))
697 v
->s
= typename(obj
->type
);
698 else if (!strcmp(name
, "objectsize")) {
700 v
->s
= xstrfmt("%lu", sz
);
703 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
707 /* See grab_values */
708 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
711 struct tag
*tag
= (struct tag
*) obj
;
713 for (i
= 0; i
< used_atom_cnt
; i
++) {
714 const char *name
= used_atom
[i
].name
;
715 struct atom_value
*v
= &val
[i
];
716 if (!!deref
!= (*name
== '*'))
720 if (!strcmp(name
, "tag"))
722 else if (!strcmp(name
, "type") && tag
->tagged
)
723 v
->s
= typename(tag
->tagged
->type
);
724 else if (!strcmp(name
, "object") && tag
->tagged
)
725 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
729 /* See grab_values */
730 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
733 struct commit
*commit
= (struct commit
*) obj
;
735 for (i
= 0; i
< used_atom_cnt
; i
++) {
736 const char *name
= used_atom
[i
].name
;
737 struct atom_value
*v
= &val
[i
];
738 if (!!deref
!= (*name
== '*'))
742 if (!strcmp(name
, "tree")) {
743 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
745 else if (!strcmp(name
, "numparent")) {
746 v
->ul
= commit_list_count(commit
->parents
);
747 v
->s
= xstrfmt("%lu", v
->ul
);
749 else if (!strcmp(name
, "parent")) {
750 struct commit_list
*parents
;
751 struct strbuf s
= STRBUF_INIT
;
752 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
753 struct commit
*parent
= parents
->item
;
754 if (parents
!= commit
->parents
)
755 strbuf_addch(&s
, ' ');
756 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
758 v
->s
= strbuf_detach(&s
, NULL
);
763 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
767 if (!strncmp(buf
, who
, wholen
) &&
769 return buf
+ wholen
+ 1;
770 eol
= strchr(buf
, '\n');
775 return ""; /* end of header */
781 static const char *copy_line(const char *buf
)
783 const char *eol
= strchrnul(buf
, '\n');
784 return xmemdupz(buf
, eol
- buf
);
787 static const char *copy_name(const char *buf
)
790 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
791 if (!strncmp(cp
, " <", 2))
792 return xmemdupz(buf
, cp
- buf
);
797 static const char *copy_email(const char *buf
)
799 const char *email
= strchr(buf
, '<');
803 eoemail
= strchr(email
, '>');
806 return xmemdupz(email
, eoemail
+ 1 - email
);
809 static char *copy_subject(const char *buf
, unsigned long len
)
811 char *r
= xmemdupz(buf
, len
);
814 for (i
= 0; i
< len
; i
++)
821 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
823 const char *eoemail
= strstr(buf
, "> ");
825 unsigned long timestamp
;
827 struct date_mode date_mode
= { DATE_NORMAL
};
831 * We got here because atomname ends in "date" or "date<something>";
832 * it's not possible that <something> is not ":<format>" because
833 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
834 * ":" means no format is specified, and use the default.
836 formatp
= strchr(atomname
, ':');
837 if (formatp
!= NULL
) {
839 parse_date_format(formatp
, &date_mode
);
844 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
845 if (timestamp
== ULONG_MAX
)
847 tz
= strtol(zone
, NULL
, 10);
848 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
850 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
858 /* See grab_values */
859 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
862 int wholen
= strlen(who
);
863 const char *wholine
= NULL
;
865 for (i
= 0; i
< used_atom_cnt
; i
++) {
866 const char *name
= used_atom
[i
].name
;
867 struct atom_value
*v
= &val
[i
];
868 if (!!deref
!= (*name
== '*'))
872 if (strncmp(who
, name
, wholen
))
874 if (name
[wholen
] != 0 &&
875 strcmp(name
+ wholen
, "name") &&
876 strcmp(name
+ wholen
, "email") &&
877 !starts_with(name
+ wholen
, "date"))
880 wholine
= find_wholine(who
, wholen
, buf
, sz
);
882 return; /* no point looking for it */
883 if (name
[wholen
] == 0)
884 v
->s
= copy_line(wholine
);
885 else if (!strcmp(name
+ wholen
, "name"))
886 v
->s
= copy_name(wholine
);
887 else if (!strcmp(name
+ wholen
, "email"))
888 v
->s
= copy_email(wholine
);
889 else if (starts_with(name
+ wholen
, "date"))
890 grab_date(wholine
, v
, name
);
894 * For a tag or a commit object, if "creator" or "creatordate" is
895 * requested, do something special.
897 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
898 return; /* "author" for commit object is not wanted */
900 wholine
= find_wholine(who
, wholen
, buf
, sz
);
903 for (i
= 0; i
< used_atom_cnt
; i
++) {
904 const char *name
= used_atom
[i
].name
;
905 struct atom_value
*v
= &val
[i
];
906 if (!!deref
!= (*name
== '*'))
911 if (starts_with(name
, "creatordate"))
912 grab_date(wholine
, v
, name
);
913 else if (!strcmp(name
, "creator"))
914 v
->s
= copy_line(wholine
);
918 static void find_subpos(const char *buf
, unsigned long sz
,
919 const char **sub
, unsigned long *sublen
,
920 const char **body
, unsigned long *bodylen
,
921 unsigned long *nonsiglen
,
922 const char **sig
, unsigned long *siglen
)
925 /* skip past header until we hit empty line */
926 while (*buf
&& *buf
!= '\n') {
927 eol
= strchrnul(buf
, '\n');
932 /* skip any empty lines */
936 /* parse signature first; we might not even have a subject line */
937 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
938 *siglen
= strlen(*sig
);
940 /* subject is first non-empty line */
942 /* subject goes to first empty line */
943 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
944 eol
= strchrnul(buf
, '\n');
949 *sublen
= buf
- *sub
;
950 /* drop trailing newline, if present */
951 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
954 /* skip any empty lines */
958 *bodylen
= strlen(buf
);
959 *nonsiglen
= *sig
- buf
;
963 * If 'lines' is greater than 0, append that many lines from the given
964 * 'buf' of length 'size' to the given strbuf.
966 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
969 const char *sp
, *eol
;
974 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
976 strbuf_addstr(out
, "\n ");
977 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
978 len
= eol
? eol
- sp
: size
- (sp
- buf
);
979 strbuf_add(out
, sp
, len
);
986 /* See grab_values */
987 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
990 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
991 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
993 for (i
= 0; i
< used_atom_cnt
; i
++) {
994 struct used_atom
*atom
= &used_atom
[i
];
995 const char *name
= atom
->name
;
996 struct atom_value
*v
= &val
[i
];
997 if (!!deref
!= (*name
== '*'))
1001 if (strcmp(name
, "subject") &&
1002 strcmp(name
, "body") &&
1003 strcmp(name
, "trailers") &&
1004 !starts_with(name
, "contents"))
1007 find_subpos(buf
, sz
,
1009 &bodypos
, &bodylen
, &nonsiglen
,
1012 if (atom
->u
.contents
.option
== C_SUB
)
1013 v
->s
= copy_subject(subpos
, sublen
);
1014 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
1015 v
->s
= xmemdupz(bodypos
, bodylen
);
1016 else if (atom
->u
.contents
.option
== C_BODY
)
1017 v
->s
= xmemdupz(bodypos
, nonsiglen
);
1018 else if (atom
->u
.contents
.option
== C_SIG
)
1019 v
->s
= xmemdupz(sigpos
, siglen
);
1020 else if (atom
->u
.contents
.option
== C_LINES
) {
1021 struct strbuf s
= STRBUF_INIT
;
1022 const char *contents_end
= bodylen
+ bodypos
- siglen
;
1024 /* Size is the length of the message after removing the signature */
1025 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
1026 v
->s
= strbuf_detach(&s
, NULL
);
1027 } else if (atom
->u
.contents
.option
== C_TRAILERS
) {
1028 struct trailer_info info
;
1030 /* Search for trailer info */
1031 trailer_info_get(&info
, subpos
);
1032 v
->s
= xmemdupz(info
.trailer_start
,
1033 info
.trailer_end
- info
.trailer_start
);
1034 trailer_info_release(&info
);
1035 } else if (atom
->u
.contents
.option
== C_BARE
)
1036 v
->s
= xstrdup(subpos
);
1041 * We want to have empty print-string for field requests
1042 * that do not apply (e.g. "authordate" for a tag object)
1044 static void fill_missing_values(struct atom_value
*val
)
1047 for (i
= 0; i
< used_atom_cnt
; i
++) {
1048 struct atom_value
*v
= &val
[i
];
1055 * val is a list of atom_value to hold returned values. Extract
1056 * the values for atoms in used_atom array out of (obj, buf, sz).
1057 * when deref is false, (obj, buf, sz) is the object that is
1058 * pointed at by the ref itself; otherwise it is the object the
1059 * ref (which is a tag) refers to.
1061 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
1063 grab_common_values(val
, deref
, obj
, buf
, sz
);
1064 switch (obj
->type
) {
1066 grab_tag_values(val
, deref
, obj
, buf
, sz
);
1067 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1068 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
1071 grab_commit_values(val
, deref
, obj
, buf
, sz
);
1072 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
1073 grab_person("author", val
, deref
, obj
, buf
, sz
);
1074 grab_person("committer", val
, deref
, obj
, buf
, sz
);
1077 /* grab_tree_values(val, deref, obj, buf, sz); */
1080 /* grab_blob_values(val, deref, obj, buf, sz); */
1083 die("Eh? Object of type %d?", obj
->type
);
1087 static inline char *copy_advance(char *dst
, const char *src
)
1094 static const char *strip_ref_components(const char *refname
, unsigned int len
)
1096 long remaining
= len
;
1097 const char *start
= refname
;
1102 die(_("ref '%s' does not have %ud components to :strip"),
1112 static const char *show_ref(struct refname_atom
*atom
, const char *refname
)
1114 if (atom
->option
== R_SHORT
)
1115 return shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
1116 else if (atom
->option
== R_STRIP
)
1117 return strip_ref_components(refname
, atom
->strip
);
1122 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
1123 struct branch
*branch
, const char **s
)
1125 int num_ours
, num_theirs
;
1126 if (atom
->u
.remote_ref
.option
== RR_REF
)
1127 *s
= show_ref(&atom
->u
.remote_ref
.refname
, refname
);
1128 else if (atom
->u
.remote_ref
.option
== RR_TRACK
) {
1129 if (stat_tracking_info(branch
, &num_ours
,
1130 &num_theirs
, NULL
)) {
1131 *s
= xstrdup("gone");
1132 } else if (!num_ours
&& !num_theirs
)
1135 *s
= xstrfmt("behind %d", num_theirs
);
1136 else if (!num_theirs
)
1137 *s
= xstrfmt("ahead %d", num_ours
);
1139 *s
= xstrfmt("ahead %d, behind %d",
1140 num_ours
, num_theirs
);
1141 if (!atom
->u
.remote_ref
.nobracket
&& *s
[0]) {
1142 const char *to_free
= *s
;
1143 *s
= xstrfmt("[%s]", *s
);
1144 free((void *)to_free
);
1146 } else if (atom
->u
.remote_ref
.option
== RR_TRACKSHORT
) {
1147 if (stat_tracking_info(branch
, &num_ours
,
1151 if (!num_ours
&& !num_theirs
)
1155 else if (!num_theirs
)
1160 die("BUG: unhandled RR_* enum");
1163 char *get_head_description(void)
1165 struct strbuf desc
= STRBUF_INIT
;
1166 struct wt_status_state state
;
1167 memset(&state
, 0, sizeof(state
));
1168 wt_status_get_state(&state
, 1);
1169 if (state
.rebase_in_progress
||
1170 state
.rebase_interactive_in_progress
)
1171 strbuf_addf(&desc
, _("(no branch, rebasing %s)"),
1173 else if (state
.bisect_in_progress
)
1174 strbuf_addf(&desc
, _("(no branch, bisect started on %s)"),
1176 else if (state
.detached_from
) {
1177 /* TRANSLATORS: make sure these match _("HEAD detached at ")
1178 and _("HEAD detached from ") in wt-status.c */
1179 if (state
.detached_at
)
1180 strbuf_addf(&desc
, _("(HEAD detached at %s)"),
1181 state
.detached_from
);
1183 strbuf_addf(&desc
, _("(HEAD detached from %s)"),
1184 state
.detached_from
);
1187 strbuf_addstr(&desc
, _("(no branch)"));
1190 free(state
.detached_from
);
1191 return strbuf_detach(&desc
, NULL
);
1194 static const char *get_symref(struct used_atom
*atom
, struct ref_array_item
*ref
)
1199 return show_ref(&atom
->u
.refname
, ref
->symref
);
1202 static const char *get_refname(struct used_atom
*atom
, struct ref_array_item
*ref
)
1204 if (ref
->kind
& FILTER_REFS_DETACHED_HEAD
)
1205 return get_head_description();
1206 return show_ref(&atom
->u
.refname
, ref
->refname
);
1210 * Parse the object referred by ref, and grab needed value.
1212 static void populate_value(struct ref_array_item
*ref
)
1218 const unsigned char *tagged
;
1220 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
1222 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
1223 unsigned char unused1
[20];
1224 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
1230 /* Fill in specials first */
1231 for (i
= 0; i
< used_atom_cnt
; i
++) {
1232 struct used_atom
*atom
= &used_atom
[i
];
1233 const char *name
= used_atom
[i
].name
;
1234 struct atom_value
*v
= &ref
->value
[i
];
1236 const char *refname
;
1237 struct branch
*branch
= NULL
;
1239 v
->handler
= append_atom
;
1247 if (starts_with(name
, "refname"))
1248 refname
= get_refname(atom
, ref
);
1249 else if (starts_with(name
, "symref"))
1250 refname
= get_symref(atom
, ref
);
1251 else if (starts_with(name
, "upstream")) {
1252 const char *branch_name
;
1253 /* only local branches may have an upstream */
1254 if (!skip_prefix(ref
->refname
, "refs/heads/",
1257 branch
= branch_get(branch_name
);
1259 refname
= branch_get_upstream(branch
, NULL
);
1261 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1263 } else if (starts_with(name
, "push")) {
1264 const char *branch_name
;
1265 if (!skip_prefix(ref
->refname
, "refs/heads/",
1268 branch
= branch_get(branch_name
);
1270 refname
= branch_get_push(branch
, NULL
);
1273 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
1275 } else if (starts_with(name
, "color:")) {
1276 v
->s
= atom
->u
.color
;
1278 } else if (!strcmp(name
, "flag")) {
1279 char buf
[256], *cp
= buf
;
1280 if (ref
->flag
& REF_ISSYMREF
)
1281 cp
= copy_advance(cp
, ",symref");
1282 if (ref
->flag
& REF_ISPACKED
)
1283 cp
= copy_advance(cp
, ",packed");
1288 v
->s
= xstrdup(buf
+ 1);
1291 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
, atom
)) {
1293 } else if (!strcmp(name
, "HEAD")) {
1295 unsigned char sha1
[20];
1297 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1299 if (head
&& !strcmp(ref
->refname
, head
))
1304 } else if (starts_with(name
, "align")) {
1305 v
->handler
= align_atom_handler
;
1307 } else if (!strcmp(name
, "end")) {
1308 v
->handler
= end_atom_handler
;
1310 } else if (starts_with(name
, "if")) {
1313 if (skip_prefix(name
, "if:", &s
))
1315 v
->handler
= if_atom_handler
;
1317 } else if (!strcmp(name
, "then")) {
1318 v
->handler
= then_atom_handler
;
1320 } else if (!strcmp(name
, "else")) {
1321 v
->handler
= else_atom_handler
;
1329 v
->s
= xstrfmt("%s^{}", refname
);
1332 for (i
= 0; i
< used_atom_cnt
; i
++) {
1333 struct atom_value
*v
= &ref
->value
[i
];
1340 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1342 die(_("missing object %s for %s"),
1343 sha1_to_hex(ref
->objectname
), ref
->refname
);
1345 die(_("parse_object_buffer failed on %s for %s"),
1346 sha1_to_hex(ref
->objectname
), ref
->refname
);
1348 grab_values(ref
->value
, 0, obj
, buf
, size
);
1353 * If there is no atom that wants to know about tagged
1354 * object, we are done.
1356 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1360 * If it is a tag object, see if we use a value that derefs
1361 * the object, and if we do grab the object it refers to.
1363 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1366 * NEEDSWORK: This derefs tag only once, which
1367 * is good to deal with chains of trust, but
1368 * is not consistent with what deref_tag() does
1369 * which peels the onion to the core.
1371 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1373 die(_("missing object %s for %s"),
1374 sha1_to_hex(tagged
), ref
->refname
);
1376 die(_("parse_object_buffer failed on %s for %s"),
1377 sha1_to_hex(tagged
), ref
->refname
);
1378 grab_values(ref
->value
, 1, obj
, buf
, size
);
1384 * Given a ref, return the value for the atom. This lazily gets value
1385 * out of the object by calling populate value.
1387 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1390 populate_value(ref
);
1391 fill_missing_values(ref
->value
);
1393 *v
= &ref
->value
[atom
];
1396 enum contains_result
{
1397 CONTAINS_UNKNOWN
= -1,
1403 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1406 * At each recursion step, the stack items points to the commits whose
1407 * ancestors are to be inspected.
1409 struct contains_stack
{
1411 struct contains_stack_entry
{
1412 struct commit
*commit
;
1413 struct commit_list
*parents
;
1417 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1419 for (; want
; want
= want
->next
)
1420 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1426 * Test whether the candidate or one of its parents is contained in the list.
1427 * Do not recurse to find out, though, but return -1 if inconclusive.
1429 static enum contains_result
contains_test(struct commit
*candidate
,
1430 const struct commit_list
*want
)
1432 /* was it previously marked as containing a want commit? */
1433 if (candidate
->object
.flags
& TMP_MARK
)
1435 /* or marked as not possibly containing a want commit? */
1436 if (candidate
->object
.flags
& UNINTERESTING
)
1439 if (in_commit_list(want
, candidate
)) {
1440 candidate
->object
.flags
|= TMP_MARK
;
1444 if (parse_commit(candidate
) < 0)
1450 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1452 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1453 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1454 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1457 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1458 const struct commit_list
*want
)
1460 struct contains_stack contains_stack
= { 0, 0, NULL
};
1461 int result
= contains_test(candidate
, want
);
1463 if (result
!= CONTAINS_UNKNOWN
)
1466 push_to_contains_stack(candidate
, &contains_stack
);
1467 while (contains_stack
.nr
) {
1468 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1469 struct commit
*commit
= entry
->commit
;
1470 struct commit_list
*parents
= entry
->parents
;
1473 commit
->object
.flags
|= UNINTERESTING
;
1474 contains_stack
.nr
--;
1477 * If we just popped the stack, parents->item has been marked,
1478 * therefore contains_test will return a meaningful 0 or 1.
1480 else switch (contains_test(parents
->item
, want
)) {
1482 commit
->object
.flags
|= TMP_MARK
;
1483 contains_stack
.nr
--;
1486 entry
->parents
= parents
->next
;
1488 case CONTAINS_UNKNOWN
:
1489 push_to_contains_stack(parents
->item
, &contains_stack
);
1493 free(contains_stack
.contains_stack
);
1494 return contains_test(candidate
, want
);
1497 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1499 if (filter
->with_commit_tag_algo
)
1500 return contains_tag_algo(commit
, filter
->with_commit
);
1501 return is_descendant_of(commit
, filter
->with_commit
);
1505 * Return 1 if the refname matches one of the patterns, otherwise 0.
1506 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1507 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1508 * matches "refs/heads/mas*", too).
1510 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1512 const char **patterns
= filter
->name_patterns
;
1515 if (filter
->ignore_case
)
1516 flags
|= WM_CASEFOLD
;
1519 * When no '--format' option is given we need to skip the prefix
1520 * for matching refs of tags and branches.
1522 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1523 skip_prefix(refname
, "refs/heads/", &refname
) ||
1524 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1525 skip_prefix(refname
, "refs/", &refname
));
1527 for (; *patterns
; patterns
++) {
1528 if (!wildmatch(*patterns
, refname
, flags
, NULL
))
1535 * Return 1 if the refname matches one of the patterns, otherwise 0.
1536 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1537 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1538 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1540 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1542 const char **pattern
= filter
->name_patterns
;
1543 int namelen
= strlen(refname
);
1544 unsigned flags
= WM_PATHNAME
;
1546 if (filter
->ignore_case
)
1547 flags
|= WM_CASEFOLD
;
1549 for (; *pattern
; pattern
++) {
1550 const char *p
= *pattern
;
1551 int plen
= strlen(p
);
1553 if ((plen
<= namelen
) &&
1554 !strncmp(refname
, p
, plen
) &&
1555 (refname
[plen
] == '\0' ||
1556 refname
[plen
] == '/' ||
1559 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1565 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1566 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1568 if (!*filter
->name_patterns
)
1569 return 1; /* No pattern always matches */
1570 if (filter
->match_as_path
)
1571 return match_name_as_path(filter
, refname
);
1572 return match_pattern(filter
, refname
);
1576 * Given a ref (sha1, refname), check if the ref belongs to the array
1577 * of sha1s. If the given ref is a tag, check if the given tag points
1578 * at one of the sha1s in the given sha1 array.
1579 * the given sha1_array.
1581 * 1. Only a single level of inderection is obtained, we might want to
1582 * change this to account for multiple levels (e.g. annotated tags
1583 * pointing to annotated tags pointing to a commit.)
1584 * 2. As the refs are cached we might know what refname peels to without
1585 * the need to parse the object via parse_object(). peel_ref() might be a
1586 * more efficient alternative to obtain the pointee.
1588 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1589 const unsigned char *sha1
,
1590 const char *refname
)
1592 const unsigned char *tagged_sha1
= NULL
;
1595 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1597 obj
= parse_object(sha1
);
1599 die(_("malformed object at '%s'"), refname
);
1600 if (obj
->type
== OBJ_TAG
)
1601 tagged_sha1
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1602 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1607 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1608 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1609 const unsigned char *objectname
,
1612 struct ref_array_item
*ref
;
1613 FLEX_ALLOC_STR(ref
, refname
, refname
);
1614 hashcpy(ref
->objectname
, objectname
);
1620 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1628 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1629 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1630 { "refs/tags/", FILTER_REFS_TAGS
}
1633 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1634 filter
->kind
== FILTER_REFS_REMOTES
||
1635 filter
->kind
== FILTER_REFS_TAGS
)
1636 return filter
->kind
;
1637 else if (!strcmp(refname
, "HEAD"))
1638 return FILTER_REFS_DETACHED_HEAD
;
1640 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1641 if (starts_with(refname
, ref_kind
[i
].prefix
))
1642 return ref_kind
[i
].kind
;
1645 return FILTER_REFS_OTHERS
;
1649 * A call-back given to for_each_ref(). Filter refs and keep them for
1650 * later object processing.
1652 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1654 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1655 struct ref_filter
*filter
= ref_cbdata
->filter
;
1656 struct ref_array_item
*ref
;
1657 struct commit
*commit
= NULL
;
1660 if (flag
& REF_BAD_NAME
) {
1661 warning(_("ignoring ref with broken name %s"), refname
);
1665 if (flag
& REF_ISBROKEN
) {
1666 warning(_("ignoring broken ref %s"), refname
);
1670 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1671 kind
= filter_ref_kind(filter
, refname
);
1672 if (!(kind
& filter
->kind
))
1675 if (!filter_pattern_match(filter
, refname
))
1678 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1682 * A merge filter is applied on refs pointing to commits. Hence
1683 * obtain the commit using the 'oid' available and discard all
1684 * non-commits early. The actual filtering is done later.
1686 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->verbose
) {
1687 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1690 /* We perform the filtering for the '--contains' option */
1691 if (filter
->with_commit
&&
1692 !commit_contains(filter
, commit
))
1697 * We do not open the object yet; sort may only need refname
1698 * to do its job and the resulting list may yet to be pruned
1699 * by maxcount logic.
1701 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1702 ref
->commit
= commit
;
1704 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1705 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1710 /* Free memory allocated for a ref_array_item */
1711 static void free_array_item(struct ref_array_item
*item
)
1713 free((char *)item
->symref
);
1717 /* Free all memory allocated for ref_array */
1718 void ref_array_clear(struct ref_array
*array
)
1722 for (i
= 0; i
< array
->nr
; i
++)
1723 free_array_item(array
->items
[i
]);
1725 array
->items
= NULL
;
1726 array
->nr
= array
->alloc
= 0;
1729 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1731 struct rev_info revs
;
1733 struct ref_filter
*filter
= ref_cbdata
->filter
;
1734 struct ref_array
*array
= ref_cbdata
->array
;
1735 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1737 init_revisions(&revs
, NULL
);
1739 for (i
= 0; i
< array
->nr
; i
++) {
1740 struct ref_array_item
*item
= array
->items
[i
];
1741 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1742 to_clear
[i
] = item
->commit
;
1745 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1746 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1749 if (prepare_revision_walk(&revs
))
1750 die(_("revision walk setup failed"));
1755 for (i
= 0; i
< old_nr
; i
++) {
1756 struct ref_array_item
*item
= array
->items
[i
];
1757 struct commit
*commit
= item
->commit
;
1759 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1761 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1762 array
->items
[array
->nr
++] = array
->items
[i
];
1764 free_array_item(item
);
1767 for (i
= 0; i
< old_nr
; i
++)
1768 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1769 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1774 * API for filtering a set of refs. Based on the type of refs the user
1775 * has requested, we iterate through those refs and apply filters
1776 * as per the given ref_filter structure and finally store the
1777 * filtered refs in the ref_array structure.
1779 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1781 struct ref_filter_cbdata ref_cbdata
;
1783 unsigned int broken
= 0;
1785 ref_cbdata
.array
= array
;
1786 ref_cbdata
.filter
= filter
;
1788 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1790 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1792 /* Simple per-ref filtering */
1794 die("filter_refs: invalid type");
1797 * For common cases where we need only branches or remotes or tags,
1798 * we only iterate through those refs. If a mix of refs is needed,
1799 * we iterate over all refs and filter out required refs with the help
1800 * of filter_ref_kind().
1802 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1803 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1804 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1805 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1806 else if (filter
->kind
== FILTER_REFS_TAGS
)
1807 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1808 else if (filter
->kind
& FILTER_REFS_ALL
)
1809 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1810 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1811 head_ref(ref_filter_handler
, &ref_cbdata
);
1815 /* Filters that need revision walking */
1816 if (filter
->merge_commit
)
1817 do_merge_filter(&ref_cbdata
);
1822 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1824 struct atom_value
*va
, *vb
;
1826 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1827 int (*cmp_fn
)(const char *, const char *);
1829 get_ref_atom_value(a
, s
->atom
, &va
);
1830 get_ref_atom_value(b
, s
->atom
, &vb
);
1831 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
1833 cmp
= versioncmp(va
->s
, vb
->s
);
1834 else if (cmp_type
== FIELD_STR
)
1835 cmp
= cmp_fn(va
->s
, vb
->s
);
1837 if (va
->ul
< vb
->ul
)
1839 else if (va
->ul
== vb
->ul
)
1840 cmp
= cmp_fn(a
->refname
, b
->refname
);
1845 return (s
->reverse
) ? -cmp
: cmp
;
1848 static struct ref_sorting
*ref_sorting
;
1849 static int compare_refs(const void *a_
, const void *b_
)
1851 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1852 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1853 struct ref_sorting
*s
;
1855 for (s
= ref_sorting
; s
; s
= s
->next
) {
1856 int cmp
= cmp_ref_sorting(s
, a
, b
);
1863 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1865 ref_sorting
= sorting
;
1866 QSORT(array
->items
, array
->nr
, compare_refs
);
1869 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1871 struct strbuf
*s
= &state
->stack
->output
;
1873 while (*cp
&& (!ep
|| cp
< ep
)) {
1878 int ch
= hex2chr(cp
+ 1);
1880 strbuf_addch(s
, ch
);
1886 strbuf_addch(s
, *cp
);
1891 void format_ref_array_item(struct ref_array_item
*info
, const char *format
,
1892 int quote_style
, struct strbuf
*final_buf
)
1894 const char *cp
, *sp
, *ep
;
1895 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1897 state
.quote_style
= quote_style
;
1898 push_stack_element(&state
.stack
);
1900 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1901 struct atom_value
*atomv
;
1903 ep
= strchr(sp
, ')');
1905 append_literal(cp
, sp
, &state
);
1906 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1907 atomv
->handler(atomv
, &state
);
1910 sp
= cp
+ strlen(cp
);
1911 append_literal(cp
, sp
, &state
);
1913 if (need_color_reset_at_eol
) {
1914 struct atom_value resetv
;
1915 char color
[COLOR_MAXLEN
] = "";
1917 if (color_parse("reset", color
) < 0)
1918 die("BUG: couldn't parse 'reset' as a color");
1920 append_atom(&resetv
, &state
);
1922 if (state
.stack
->prev
)
1923 die(_("format: %%(end) atom missing"));
1924 strbuf_addbuf(final_buf
, &state
.stack
->output
);
1925 pop_stack_element(&state
.stack
);
1928 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1930 struct strbuf final_buf
= STRBUF_INIT
;
1932 format_ref_array_item(info
, format
, quote_style
, &final_buf
);
1933 fwrite(final_buf
.buf
, 1, final_buf
.len
, stdout
);
1934 strbuf_release(&final_buf
);
1938 /* If no sorting option is given, use refname to sort as default */
1939 struct ref_sorting
*ref_default_sorting(void)
1941 static const char cstr_name
[] = "refname";
1943 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1945 sorting
->next
= NULL
;
1946 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1950 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1952 struct ref_sorting
**sorting_tail
= opt
->value
;
1953 struct ref_sorting
*s
;
1956 if (!arg
) /* should --no-sort void the list ? */
1959 s
= xcalloc(1, sizeof(*s
));
1960 s
->next
= *sorting_tail
;
1967 if (skip_prefix(arg
, "version:", &arg
) ||
1968 skip_prefix(arg
, "v:", &arg
))
1971 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1975 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1977 struct ref_filter
*rf
= opt
->value
;
1978 unsigned char sha1
[20];
1980 rf
->merge
= starts_with(opt
->long_name
, "no")
1981 ? REF_FILTER_MERGED_OMIT
1982 : REF_FILTER_MERGED_INCLUDE
;
1984 if (get_sha1(arg
, sha1
))
1985 die(_("malformed object name %s"), arg
);
1987 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1988 if (!rf
->merge_commit
)
1989 return opterror(opt
, "must point to a commit", 0);