3 #include "parse-options.h"
11 #include "ref-filter.h"
14 #include "git-compat-util.h"
17 typedef enum { FIELD_STR
, FIELD_ULONG
, FIELD_TIME
} cmp_type
;
25 * An atom is a valid field atom listed below, possibly prefixed with
26 * a "*" to denote deref_tag().
28 * We parse given format string and sort specifiers, and make a list
29 * of properties that we need to extract out of objects. ref_array_item
30 * structure will hold an array of values extracted that can be
31 * indexed with the "atom number", which is an index into this
34 static struct used_atom
{
38 char color
[COLOR_MAXLEN
];
40 enum { RR_NORMAL
, RR_SHORTEN
, RR_TRACK
, RR_TRACKSHORT
}
43 enum { C_BARE
, C_BODY
, C_BODY_DEP
, C_LINES
, C_SIG
, C_SUB
} option
;
46 enum { O_FULL
, O_SHORT
} objectname
;
49 static int used_atom_cnt
, need_tagged
, need_symref
;
50 static int need_color_reset_at_eol
;
52 static void color_atom_parser(struct used_atom
*atom
, const char *color_value
)
55 die(_("expected format: %%(color:<color>)"));
56 if (color_parse(color_value
, atom
->u
.color
) < 0)
57 die(_("unrecognized color: %%(color:%s)"), color_value
);
60 static void remote_ref_atom_parser(struct used_atom
*atom
, const char *arg
)
63 atom
->u
.remote_ref
= RR_NORMAL
;
64 else if (!strcmp(arg
, "short"))
65 atom
->u
.remote_ref
= RR_SHORTEN
;
66 else if (!strcmp(arg
, "track"))
67 atom
->u
.remote_ref
= RR_TRACK
;
68 else if (!strcmp(arg
, "trackshort"))
69 atom
->u
.remote_ref
= RR_TRACKSHORT
;
71 die(_("unrecognized format: %%(%s)"), atom
->name
);
74 static void body_atom_parser(struct used_atom
*atom
, const char *arg
)
77 die(_("%%(body) does not take arguments"));
78 atom
->u
.contents
.option
= C_BODY_DEP
;
81 static void subject_atom_parser(struct used_atom
*atom
, const char *arg
)
84 die(_("%%(subject) does not take arguments"));
85 atom
->u
.contents
.option
= C_SUB
;
88 static void contents_atom_parser(struct used_atom
*atom
, const char *arg
)
91 atom
->u
.contents
.option
= C_BARE
;
92 else if (!strcmp(arg
, "body"))
93 atom
->u
.contents
.option
= C_BODY
;
94 else if (!strcmp(arg
, "signature"))
95 atom
->u
.contents
.option
= C_SIG
;
96 else if (!strcmp(arg
, "subject"))
97 atom
->u
.contents
.option
= C_SUB
;
98 else if (skip_prefix(arg
, "lines=", &arg
)) {
99 atom
->u
.contents
.option
= C_LINES
;
100 if (strtoul_ui(arg
, 10, &atom
->u
.contents
.nlines
))
101 die(_("positive value expected contents:lines=%s"), arg
);
103 die(_("unrecognized %%(contents) argument: %s"), arg
);
106 static void objectname_atom_parser(struct used_atom
*atom
, const char *arg
)
109 atom
->u
.objectname
= O_FULL
;
110 else if (!strcmp(arg
, "short"))
111 atom
->u
.objectname
= O_SHORT
;
113 die(_("unrecognized %%(objectname) argument: %s"), arg
);
116 static align_type
parse_align_position(const char *s
)
118 if (!strcmp(s
, "right"))
120 else if (!strcmp(s
, "middle"))
122 else if (!strcmp(s
, "left"))
127 static void align_atom_parser(struct used_atom
*atom
, const char *arg
)
129 struct align
*align
= &atom
->u
.align
;
130 struct string_list params
= STRING_LIST_INIT_DUP
;
132 unsigned int width
= ~0U;
135 die(_("expected format: %%(align:<width>,<position>)"));
137 align
->position
= ALIGN_LEFT
;
139 string_list_split(¶ms
, arg
, ',', -1);
140 for (i
= 0; i
< params
.nr
; i
++) {
141 const char *s
= params
.items
[i
].string
;
144 if (skip_prefix(s
, "position=", &s
)) {
145 position
= parse_align_position(s
);
147 die(_("unrecognized position:%s"), s
);
148 align
->position
= position
;
149 } else if (skip_prefix(s
, "width=", &s
)) {
150 if (strtoul_ui(s
, 10, &width
))
151 die(_("unrecognized width:%s"), s
);
152 } else if (!strtoul_ui(s
, 10, &width
))
154 else if ((position
= parse_align_position(s
)) >= 0)
155 align
->position
= position
;
157 die(_("unrecognized %%(align) argument: %s"), s
);
161 die(_("positive width expected with the %%(align) atom"));
162 align
->width
= width
;
163 string_list_clear(¶ms
, 0);
169 void (*parser
)(struct used_atom
*atom
, const char *arg
);
173 { "objectsize", FIELD_ULONG
},
174 { "objectname", FIELD_STR
, objectname_atom_parser
},
177 { "numparent", FIELD_ULONG
},
184 { "authordate", FIELD_TIME
},
187 { "committeremail" },
188 { "committerdate", FIELD_TIME
},
192 { "taggerdate", FIELD_TIME
},
194 { "creatordate", FIELD_TIME
},
195 { "subject", FIELD_STR
, subject_atom_parser
},
196 { "body", FIELD_STR
, body_atom_parser
},
197 { "contents", FIELD_STR
, contents_atom_parser
},
198 { "upstream", FIELD_STR
, remote_ref_atom_parser
},
199 { "push", FIELD_STR
, remote_ref_atom_parser
},
203 { "color", FIELD_STR
, color_atom_parser
},
204 { "align", FIELD_STR
, align_atom_parser
},
208 #define REF_FORMATTING_STATE_INIT { 0, NULL }
210 struct ref_formatting_stack
{
211 struct ref_formatting_stack
*prev
;
212 struct strbuf output
;
213 void (*at_end
)(struct ref_formatting_stack
*stack
);
217 struct ref_formatting_state
{
219 struct ref_formatting_stack
*stack
;
227 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
228 unsigned long ul
; /* used for sorting when not FIELD_STR */
232 * Used to parse format string and sort specifiers
234 int parse_ref_filter_atom(const char *atom
, const char *ep
)
241 if (*sp
== '*' && sp
< ep
)
244 die(_("malformed field name: %.*s"), (int)(ep
-atom
), atom
);
246 /* Do we have the atom already used elsewhere? */
247 for (i
= 0; i
< used_atom_cnt
; i
++) {
248 int len
= strlen(used_atom
[i
].name
);
249 if (len
== ep
- atom
&& !memcmp(used_atom
[i
].name
, atom
, len
))
254 * If the atom name has a colon, strip it and everything after
255 * it off - it specifies the format for this entry, and
256 * shouldn't be used for checking against the valid_atom
259 arg
= memchr(sp
, ':', ep
- sp
);
260 atom_len
= (arg
? arg
: ep
) - sp
;
262 /* Is the atom a valid one? */
263 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
264 int len
= strlen(valid_atom
[i
].name
);
265 if (len
== atom_len
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
269 if (ARRAY_SIZE(valid_atom
) <= i
)
270 die(_("unknown field name: %.*s"), (int)(ep
-atom
), atom
);
272 /* Add it in, including the deref prefix */
275 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
276 used_atom
[at
].name
= xmemdupz(atom
, ep
- atom
);
277 used_atom
[at
].type
= valid_atom
[i
].cmp_type
;
279 arg
= used_atom
[at
].name
+ (arg
- atom
) + 1;
280 memset(&used_atom
[at
].u
, 0, sizeof(used_atom
[at
].u
));
281 if (valid_atom
[i
].parser
)
282 valid_atom
[i
].parser(&used_atom
[at
], arg
);
285 if (!strcmp(used_atom
[at
].name
, "symref"))
290 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
292 switch (quote_style
) {
294 strbuf_addstr(s
, str
);
297 sq_quote_buf(s
, str
);
300 perl_quote_buf(s
, str
);
303 python_quote_buf(s
, str
);
306 tcl_quote_buf(s
, str
);
311 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
314 * Quote formatting is only done when the stack has a single
315 * element. Otherwise quote formatting is done on the
316 * element's entire output strbuf when the %(end) atom is
319 if (!state
->stack
->prev
)
320 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
322 strbuf_addstr(&state
->stack
->output
, v
->s
);
325 static void push_stack_element(struct ref_formatting_stack
**stack
)
327 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
329 strbuf_init(&s
->output
, 0);
334 static void pop_stack_element(struct ref_formatting_stack
**stack
)
336 struct ref_formatting_stack
*current
= *stack
;
337 struct ref_formatting_stack
*prev
= current
->prev
;
340 strbuf_addbuf(&prev
->output
, ¤t
->output
);
341 strbuf_release(¤t
->output
);
346 static void end_align_handler(struct ref_formatting_stack
*stack
)
348 struct align
*align
= (struct align
*)stack
->at_end_data
;
349 struct strbuf s
= STRBUF_INIT
;
351 strbuf_utf8_align(&s
, align
->position
, align
->width
, stack
->output
.buf
);
352 strbuf_swap(&stack
->output
, &s
);
356 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
358 struct ref_formatting_stack
*new;
360 push_stack_element(&state
->stack
);
362 new->at_end
= end_align_handler
;
363 new->at_end_data
= &atomv
->u
.align
;
366 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
368 struct ref_formatting_stack
*current
= state
->stack
;
369 struct strbuf s
= STRBUF_INIT
;
371 if (!current
->at_end
)
372 die(_("format: %%(end) atom used without corresponding atom"));
373 current
->at_end(current
);
376 * Perform quote formatting when the stack element is that of
377 * a supporting atom. If nested then perform quote formatting
378 * only on the topmost supporting atom.
380 if (!state
->stack
->prev
->prev
) {
381 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
382 strbuf_swap(¤t
->output
, &s
);
385 pop_stack_element(&state
->stack
);
389 * In a format string, find the next occurrence of %(atom).
391 static const char *find_next(const char *cp
)
396 * %( is the start of an atom;
397 * %% is a quoted per-cent.
401 else if (cp
[1] == '%')
402 cp
++; /* skip over two % */
403 /* otherwise this is a singleton, literal % */
411 * Make sure the format string is well formed, and parse out
414 int verify_ref_format(const char *format
)
418 need_color_reset_at_eol
= 0;
419 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
420 const char *color
, *ep
= strchr(sp
, ')');
424 return error(_("malformed format string %s"), sp
);
425 /* sp points at "%(" and ep points at the closing ")" */
426 at
= parse_ref_filter_atom(sp
+ 2, ep
);
429 if (skip_prefix(used_atom
[at
].name
, "color:", &color
))
430 need_color_reset_at_eol
= !!strcmp(color
, "reset");
436 * Given an object name, read the object data and size, and return a
437 * "struct object". If the object data we are returning is also borrowed
438 * by the "struct object" representation, set *eaten as well---it is a
439 * signal from parse_object_buffer to us not to free the buffer.
441 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
443 enum object_type type
;
444 void *buf
= read_sha1_file(sha1
, &type
, sz
);
447 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
453 static int grab_objectname(const char *name
, const unsigned char *sha1
,
454 struct atom_value
*v
, struct used_atom
*atom
)
456 if (starts_with(name
, "objectname")) {
457 if (atom
->u
.objectname
== O_SHORT
) {
458 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
460 } else if (atom
->u
.objectname
== O_FULL
) {
461 v
->s
= xstrdup(sha1_to_hex(sha1
));
464 die("BUG: unknown %%(objectname) option");
469 /* See grab_values */
470 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
474 for (i
= 0; i
< used_atom_cnt
; i
++) {
475 const char *name
= used_atom
[i
].name
;
476 struct atom_value
*v
= &val
[i
];
477 if (!!deref
!= (*name
== '*'))
481 if (!strcmp(name
, "objecttype"))
482 v
->s
= typename(obj
->type
);
483 else if (!strcmp(name
, "objectsize")) {
485 v
->s
= xstrfmt("%lu", sz
);
488 grab_objectname(name
, obj
->oid
.hash
, v
, &used_atom
[i
]);
492 /* See grab_values */
493 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
496 struct tag
*tag
= (struct tag
*) obj
;
498 for (i
= 0; i
< used_atom_cnt
; i
++) {
499 const char *name
= used_atom
[i
].name
;
500 struct atom_value
*v
= &val
[i
];
501 if (!!deref
!= (*name
== '*'))
505 if (!strcmp(name
, "tag"))
507 else if (!strcmp(name
, "type") && tag
->tagged
)
508 v
->s
= typename(tag
->tagged
->type
);
509 else if (!strcmp(name
, "object") && tag
->tagged
)
510 v
->s
= xstrdup(oid_to_hex(&tag
->tagged
->oid
));
514 /* See grab_values */
515 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
518 struct commit
*commit
= (struct commit
*) obj
;
520 for (i
= 0; i
< used_atom_cnt
; i
++) {
521 const char *name
= used_atom
[i
].name
;
522 struct atom_value
*v
= &val
[i
];
523 if (!!deref
!= (*name
== '*'))
527 if (!strcmp(name
, "tree")) {
528 v
->s
= xstrdup(oid_to_hex(&commit
->tree
->object
.oid
));
530 else if (!strcmp(name
, "numparent")) {
531 v
->ul
= commit_list_count(commit
->parents
);
532 v
->s
= xstrfmt("%lu", v
->ul
);
534 else if (!strcmp(name
, "parent")) {
535 struct commit_list
*parents
;
536 struct strbuf s
= STRBUF_INIT
;
537 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
538 struct commit
*parent
= parents
->item
;
539 if (parents
!= commit
->parents
)
540 strbuf_addch(&s
, ' ');
541 strbuf_addstr(&s
, oid_to_hex(&parent
->object
.oid
));
543 v
->s
= strbuf_detach(&s
, NULL
);
548 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
552 if (!strncmp(buf
, who
, wholen
) &&
554 return buf
+ wholen
+ 1;
555 eol
= strchr(buf
, '\n');
560 return ""; /* end of header */
566 static const char *copy_line(const char *buf
)
568 const char *eol
= strchrnul(buf
, '\n');
569 return xmemdupz(buf
, eol
- buf
);
572 static const char *copy_name(const char *buf
)
575 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
576 if (!strncmp(cp
, " <", 2))
577 return xmemdupz(buf
, cp
- buf
);
582 static const char *copy_email(const char *buf
)
584 const char *email
= strchr(buf
, '<');
588 eoemail
= strchr(email
, '>');
591 return xmemdupz(email
, eoemail
+ 1 - email
);
594 static char *copy_subject(const char *buf
, unsigned long len
)
596 char *r
= xmemdupz(buf
, len
);
599 for (i
= 0; i
< len
; i
++)
606 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
608 const char *eoemail
= strstr(buf
, "> ");
610 unsigned long timestamp
;
612 struct date_mode date_mode
= { DATE_NORMAL
};
616 * We got here because atomname ends in "date" or "date<something>";
617 * it's not possible that <something> is not ":<format>" because
618 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
619 * ":" means no format is specified, and use the default.
621 formatp
= strchr(atomname
, ':');
622 if (formatp
!= NULL
) {
624 parse_date_format(formatp
, &date_mode
);
629 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
630 if (timestamp
== ULONG_MAX
)
632 tz
= strtol(zone
, NULL
, 10);
633 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
635 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
643 /* See grab_values */
644 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
647 int wholen
= strlen(who
);
648 const char *wholine
= NULL
;
650 for (i
= 0; i
< used_atom_cnt
; i
++) {
651 const char *name
= used_atom
[i
].name
;
652 struct atom_value
*v
= &val
[i
];
653 if (!!deref
!= (*name
== '*'))
657 if (strncmp(who
, name
, wholen
))
659 if (name
[wholen
] != 0 &&
660 strcmp(name
+ wholen
, "name") &&
661 strcmp(name
+ wholen
, "email") &&
662 !starts_with(name
+ wholen
, "date"))
665 wholine
= find_wholine(who
, wholen
, buf
, sz
);
667 return; /* no point looking for it */
668 if (name
[wholen
] == 0)
669 v
->s
= copy_line(wholine
);
670 else if (!strcmp(name
+ wholen
, "name"))
671 v
->s
= copy_name(wholine
);
672 else if (!strcmp(name
+ wholen
, "email"))
673 v
->s
= copy_email(wholine
);
674 else if (starts_with(name
+ wholen
, "date"))
675 grab_date(wholine
, v
, name
);
679 * For a tag or a commit object, if "creator" or "creatordate" is
680 * requested, do something special.
682 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
683 return; /* "author" for commit object is not wanted */
685 wholine
= find_wholine(who
, wholen
, buf
, sz
);
688 for (i
= 0; i
< used_atom_cnt
; i
++) {
689 const char *name
= used_atom
[i
].name
;
690 struct atom_value
*v
= &val
[i
];
691 if (!!deref
!= (*name
== '*'))
696 if (starts_with(name
, "creatordate"))
697 grab_date(wholine
, v
, name
);
698 else if (!strcmp(name
, "creator"))
699 v
->s
= copy_line(wholine
);
703 static void find_subpos(const char *buf
, unsigned long sz
,
704 const char **sub
, unsigned long *sublen
,
705 const char **body
, unsigned long *bodylen
,
706 unsigned long *nonsiglen
,
707 const char **sig
, unsigned long *siglen
)
710 /* skip past header until we hit empty line */
711 while (*buf
&& *buf
!= '\n') {
712 eol
= strchrnul(buf
, '\n');
717 /* skip any empty lines */
721 /* parse signature first; we might not even have a subject line */
722 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
723 *siglen
= strlen(*sig
);
725 /* subject is first non-empty line */
727 /* subject goes to first empty line */
728 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
729 eol
= strchrnul(buf
, '\n');
734 *sublen
= buf
- *sub
;
735 /* drop trailing newline, if present */
736 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
739 /* skip any empty lines */
743 *bodylen
= strlen(buf
);
744 *nonsiglen
= *sig
- buf
;
748 * If 'lines' is greater than 0, append that many lines from the given
749 * 'buf' of length 'size' to the given strbuf.
751 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
754 const char *sp
, *eol
;
759 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
761 strbuf_addstr(out
, "\n ");
762 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
763 len
= eol
? eol
- sp
: size
- (sp
- buf
);
764 strbuf_add(out
, sp
, len
);
771 /* See grab_values */
772 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
775 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
776 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
778 for (i
= 0; i
< used_atom_cnt
; i
++) {
779 struct used_atom
*atom
= &used_atom
[i
];
780 const char *name
= atom
->name
;
781 struct atom_value
*v
= &val
[i
];
782 if (!!deref
!= (*name
== '*'))
786 if (strcmp(name
, "subject") &&
787 strcmp(name
, "body") &&
788 !starts_with(name
, "contents"))
793 &bodypos
, &bodylen
, &nonsiglen
,
796 if (atom
->u
.contents
.option
== C_SUB
)
797 v
->s
= copy_subject(subpos
, sublen
);
798 else if (atom
->u
.contents
.option
== C_BODY_DEP
)
799 v
->s
= xmemdupz(bodypos
, bodylen
);
800 else if (atom
->u
.contents
.option
== C_BODY
)
801 v
->s
= xmemdupz(bodypos
, nonsiglen
);
802 else if (atom
->u
.contents
.option
== C_SIG
)
803 v
->s
= xmemdupz(sigpos
, siglen
);
804 else if (atom
->u
.contents
.option
== C_LINES
) {
805 struct strbuf s
= STRBUF_INIT
;
806 const char *contents_end
= bodylen
+ bodypos
- siglen
;
808 /* Size is the length of the message after removing the signature */
809 append_lines(&s
, subpos
, contents_end
- subpos
, atom
->u
.contents
.nlines
);
810 v
->s
= strbuf_detach(&s
, NULL
);
811 } else if (atom
->u
.contents
.option
== C_BARE
)
812 v
->s
= xstrdup(subpos
);
817 * We want to have empty print-string for field requests
818 * that do not apply (e.g. "authordate" for a tag object)
820 static void fill_missing_values(struct atom_value
*val
)
823 for (i
= 0; i
< used_atom_cnt
; i
++) {
824 struct atom_value
*v
= &val
[i
];
831 * val is a list of atom_value to hold returned values. Extract
832 * the values for atoms in used_atom array out of (obj, buf, sz).
833 * when deref is false, (obj, buf, sz) is the object that is
834 * pointed at by the ref itself; otherwise it is the object the
835 * ref (which is a tag) refers to.
837 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
839 grab_common_values(val
, deref
, obj
, buf
, sz
);
842 grab_tag_values(val
, deref
, obj
, buf
, sz
);
843 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
844 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
847 grab_commit_values(val
, deref
, obj
, buf
, sz
);
848 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
849 grab_person("author", val
, deref
, obj
, buf
, sz
);
850 grab_person("committer", val
, deref
, obj
, buf
, sz
);
853 /* grab_tree_values(val, deref, obj, buf, sz); */
856 /* grab_blob_values(val, deref, obj, buf, sz); */
859 die("Eh? Object of type %d?", obj
->type
);
863 static inline char *copy_advance(char *dst
, const char *src
)
870 static const char *strip_ref_components(const char *refname
, const char *nr_arg
)
873 long nr
= strtol(nr_arg
, &end
, 10);
875 const char *start
= refname
;
877 if (nr
< 1 || *end
!= '\0')
878 die(_(":strip= requires a positive integer argument"));
883 die(_("ref '%s' does not have %ld components to :strip"),
893 static void fill_remote_ref_details(struct used_atom
*atom
, const char *refname
,
894 struct branch
*branch
, const char **s
)
896 int num_ours
, num_theirs
;
897 if (atom
->u
.remote_ref
== RR_SHORTEN
)
898 *s
= shorten_unambiguous_ref(refname
, warn_ambiguous_refs
);
899 else if (atom
->u
.remote_ref
== RR_TRACK
) {
900 if (stat_tracking_info(branch
, &num_ours
,
904 if (!num_ours
&& !num_theirs
)
907 *s
= xstrfmt("[behind %d]", num_theirs
);
908 else if (!num_theirs
)
909 *s
= xstrfmt("[ahead %d]", num_ours
);
911 *s
= xstrfmt("[ahead %d, behind %d]",
912 num_ours
, num_theirs
);
913 } else if (atom
->u
.remote_ref
== RR_TRACKSHORT
) {
914 if (stat_tracking_info(branch
, &num_ours
,
918 if (!num_ours
&& !num_theirs
)
922 else if (!num_theirs
)
926 } else /* RR_NORMAL */
931 * Parse the object referred by ref, and grab needed value.
933 static void populate_value(struct ref_array_item
*ref
)
939 const unsigned char *tagged
;
941 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
943 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
944 unsigned char unused1
[20];
945 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
951 /* Fill in specials first */
952 for (i
= 0; i
< used_atom_cnt
; i
++) {
953 struct used_atom
*atom
= &used_atom
[i
];
954 const char *name
= used_atom
[i
].name
;
955 struct atom_value
*v
= &ref
->value
[i
];
959 struct branch
*branch
= NULL
;
961 v
->handler
= append_atom
;
968 if (starts_with(name
, "refname"))
969 refname
= ref
->refname
;
970 else if (starts_with(name
, "symref"))
971 refname
= ref
->symref
? ref
->symref
: "";
972 else if (starts_with(name
, "upstream")) {
973 const char *branch_name
;
974 /* only local branches may have an upstream */
975 if (!skip_prefix(ref
->refname
, "refs/heads/",
978 branch
= branch_get(branch_name
);
980 refname
= branch_get_upstream(branch
, NULL
);
982 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
984 } else if (starts_with(name
, "push")) {
985 const char *branch_name
;
986 if (!skip_prefix(ref
->refname
, "refs/heads/",
989 branch
= branch_get(branch_name
);
991 refname
= branch_get_push(branch
, NULL
);
994 fill_remote_ref_details(atom
, refname
, branch
, &v
->s
);
996 } else if (starts_with(name
, "color:")) {
997 v
->s
= atom
->u
.color
;
999 } else if (!strcmp(name
, "flag")) {
1000 char buf
[256], *cp
= buf
;
1001 if (ref
->flag
& REF_ISSYMREF
)
1002 cp
= copy_advance(cp
, ",symref");
1003 if (ref
->flag
& REF_ISPACKED
)
1004 cp
= copy_advance(cp
, ",packed");
1009 v
->s
= xstrdup(buf
+ 1);
1012 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
, atom
)) {
1014 } else if (!strcmp(name
, "HEAD")) {
1016 unsigned char sha1
[20];
1018 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
1020 if (head
&& !strcmp(ref
->refname
, head
))
1025 } else if (starts_with(name
, "align")) {
1026 v
->u
.align
= atom
->u
.align
;
1027 v
->handler
= align_atom_handler
;
1029 } else if (!strcmp(name
, "end")) {
1030 v
->handler
= end_atom_handler
;
1035 formatp
= strchr(name
, ':');
1040 if (!strcmp(formatp
, "short"))
1041 refname
= shorten_unambiguous_ref(refname
,
1042 warn_ambiguous_refs
);
1043 else if (skip_prefix(formatp
, "strip=", &arg
))
1044 refname
= strip_ref_components(refname
, arg
);
1046 die(_("unknown %.*s format %s"),
1047 (int)(formatp
- name
), name
, formatp
);
1053 v
->s
= xstrfmt("%s^{}", refname
);
1056 for (i
= 0; i
< used_atom_cnt
; i
++) {
1057 struct atom_value
*v
= &ref
->value
[i
];
1064 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1066 die(_("missing object %s for %s"),
1067 sha1_to_hex(ref
->objectname
), ref
->refname
);
1069 die(_("parse_object_buffer failed on %s for %s"),
1070 sha1_to_hex(ref
->objectname
), ref
->refname
);
1072 grab_values(ref
->value
, 0, obj
, buf
, size
);
1077 * If there is no atom that wants to know about tagged
1078 * object, we are done.
1080 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1084 * If it is a tag object, see if we use a value that derefs
1085 * the object, and if we do grab the object it refers to.
1087 tagged
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1090 * NEEDSWORK: This derefs tag only once, which
1091 * is good to deal with chains of trust, but
1092 * is not consistent with what deref_tag() does
1093 * which peels the onion to the core.
1095 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1097 die(_("missing object %s for %s"),
1098 sha1_to_hex(tagged
), ref
->refname
);
1100 die(_("parse_object_buffer failed on %s for %s"),
1101 sha1_to_hex(tagged
), ref
->refname
);
1102 grab_values(ref
->value
, 1, obj
, buf
, size
);
1108 * Given a ref, return the value for the atom. This lazily gets value
1109 * out of the object by calling populate value.
1111 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1114 populate_value(ref
);
1115 fill_missing_values(ref
->value
);
1117 *v
= &ref
->value
[atom
];
1120 enum contains_result
{
1121 CONTAINS_UNKNOWN
= -1,
1127 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1130 * At each recursion step, the stack items points to the commits whose
1131 * ancestors are to be inspected.
1133 struct contains_stack
{
1135 struct contains_stack_entry
{
1136 struct commit
*commit
;
1137 struct commit_list
*parents
;
1141 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1143 for (; want
; want
= want
->next
)
1144 if (!oidcmp(&want
->item
->object
.oid
, &c
->object
.oid
))
1150 * Test whether the candidate or one of its parents is contained in the list.
1151 * Do not recurse to find out, though, but return -1 if inconclusive.
1153 static enum contains_result
contains_test(struct commit
*candidate
,
1154 const struct commit_list
*want
)
1156 /* was it previously marked as containing a want commit? */
1157 if (candidate
->object
.flags
& TMP_MARK
)
1159 /* or marked as not possibly containing a want commit? */
1160 if (candidate
->object
.flags
& UNINTERESTING
)
1163 if (in_commit_list(want
, candidate
)) {
1164 candidate
->object
.flags
|= TMP_MARK
;
1168 if (parse_commit(candidate
) < 0)
1174 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1176 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1177 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1178 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1181 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1182 const struct commit_list
*want
)
1184 struct contains_stack contains_stack
= { 0, 0, NULL
};
1185 int result
= contains_test(candidate
, want
);
1187 if (result
!= CONTAINS_UNKNOWN
)
1190 push_to_contains_stack(candidate
, &contains_stack
);
1191 while (contains_stack
.nr
) {
1192 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1193 struct commit
*commit
= entry
->commit
;
1194 struct commit_list
*parents
= entry
->parents
;
1197 commit
->object
.flags
|= UNINTERESTING
;
1198 contains_stack
.nr
--;
1201 * If we just popped the stack, parents->item has been marked,
1202 * therefore contains_test will return a meaningful 0 or 1.
1204 else switch (contains_test(parents
->item
, want
)) {
1206 commit
->object
.flags
|= TMP_MARK
;
1207 contains_stack
.nr
--;
1210 entry
->parents
= parents
->next
;
1212 case CONTAINS_UNKNOWN
:
1213 push_to_contains_stack(parents
->item
, &contains_stack
);
1217 free(contains_stack
.contains_stack
);
1218 return contains_test(candidate
, want
);
1221 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1223 if (filter
->with_commit_tag_algo
)
1224 return contains_tag_algo(commit
, filter
->with_commit
);
1225 return is_descendant_of(commit
, filter
->with_commit
);
1229 * Return 1 if the refname matches one of the patterns, otherwise 0.
1230 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1231 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1232 * matches "refs/heads/mas*", too).
1234 static int match_pattern(const struct ref_filter
*filter
, const char *refname
)
1236 const char **patterns
= filter
->name_patterns
;
1239 if (filter
->ignore_case
)
1240 flags
|= WM_CASEFOLD
;
1243 * When no '--format' option is given we need to skip the prefix
1244 * for matching refs of tags and branches.
1246 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1247 skip_prefix(refname
, "refs/heads/", &refname
) ||
1248 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1249 skip_prefix(refname
, "refs/", &refname
));
1251 for (; *patterns
; patterns
++) {
1252 if (!wildmatch(*patterns
, refname
, flags
, NULL
))
1259 * Return 1 if the refname matches one of the patterns, otherwise 0.
1260 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1261 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1262 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1264 static int match_name_as_path(const struct ref_filter
*filter
, const char *refname
)
1266 const char **pattern
= filter
->name_patterns
;
1267 int namelen
= strlen(refname
);
1268 unsigned flags
= WM_PATHNAME
;
1270 if (filter
->ignore_case
)
1271 flags
|= WM_CASEFOLD
;
1273 for (; *pattern
; pattern
++) {
1274 const char *p
= *pattern
;
1275 int plen
= strlen(p
);
1277 if ((plen
<= namelen
) &&
1278 !strncmp(refname
, p
, plen
) &&
1279 (refname
[plen
] == '\0' ||
1280 refname
[plen
] == '/' ||
1283 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1289 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1290 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1292 if (!*filter
->name_patterns
)
1293 return 1; /* No pattern always matches */
1294 if (filter
->match_as_path
)
1295 return match_name_as_path(filter
, refname
);
1296 return match_pattern(filter
, refname
);
1300 * Given a ref (sha1, refname), check if the ref belongs to the array
1301 * of sha1s. If the given ref is a tag, check if the given tag points
1302 * at one of the sha1s in the given sha1 array.
1303 * the given sha1_array.
1305 * 1. Only a single level of inderection is obtained, we might want to
1306 * change this to account for multiple levels (e.g. annotated tags
1307 * pointing to annotated tags pointing to a commit.)
1308 * 2. As the refs are cached we might know what refname peels to without
1309 * the need to parse the object via parse_object(). peel_ref() might be a
1310 * more efficient alternative to obtain the pointee.
1312 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1313 const unsigned char *sha1
,
1314 const char *refname
)
1316 const unsigned char *tagged_sha1
= NULL
;
1319 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1321 obj
= parse_object(sha1
);
1323 die(_("malformed object at '%s'"), refname
);
1324 if (obj
->type
== OBJ_TAG
)
1325 tagged_sha1
= ((struct tag
*)obj
)->tagged
->oid
.hash
;
1326 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1331 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1332 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1333 const unsigned char *objectname
,
1336 struct ref_array_item
*ref
;
1337 FLEX_ALLOC_STR(ref
, refname
, refname
);
1338 hashcpy(ref
->objectname
, objectname
);
1344 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1352 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1353 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1354 { "refs/tags/", FILTER_REFS_TAGS
}
1357 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1358 filter
->kind
== FILTER_REFS_REMOTES
||
1359 filter
->kind
== FILTER_REFS_TAGS
)
1360 return filter
->kind
;
1361 else if (!strcmp(refname
, "HEAD"))
1362 return FILTER_REFS_DETACHED_HEAD
;
1364 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1365 if (starts_with(refname
, ref_kind
[i
].prefix
))
1366 return ref_kind
[i
].kind
;
1369 return FILTER_REFS_OTHERS
;
1373 * A call-back given to for_each_ref(). Filter refs and keep them for
1374 * later object processing.
1376 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1378 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1379 struct ref_filter
*filter
= ref_cbdata
->filter
;
1380 struct ref_array_item
*ref
;
1381 struct commit
*commit
= NULL
;
1384 if (flag
& REF_BAD_NAME
) {
1385 warning(_("ignoring ref with broken name %s"), refname
);
1389 if (flag
& REF_ISBROKEN
) {
1390 warning(_("ignoring broken ref %s"), refname
);
1394 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1395 kind
= filter_ref_kind(filter
, refname
);
1396 if (!(kind
& filter
->kind
))
1399 if (!filter_pattern_match(filter
, refname
))
1402 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1406 * A merge filter is applied on refs pointing to commits. Hence
1407 * obtain the commit using the 'oid' available and discard all
1408 * non-commits early. The actual filtering is done later.
1410 if (filter
->merge_commit
|| filter
->with_commit
|| filter
->verbose
) {
1411 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1414 /* We perform the filtering for the '--contains' option */
1415 if (filter
->with_commit
&&
1416 !commit_contains(filter
, commit
))
1421 * We do not open the object yet; sort may only need refname
1422 * to do its job and the resulting list may yet to be pruned
1423 * by maxcount logic.
1425 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1426 ref
->commit
= commit
;
1428 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1429 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1434 /* Free memory allocated for a ref_array_item */
1435 static void free_array_item(struct ref_array_item
*item
)
1437 free((char *)item
->symref
);
1441 /* Free all memory allocated for ref_array */
1442 void ref_array_clear(struct ref_array
*array
)
1446 for (i
= 0; i
< array
->nr
; i
++)
1447 free_array_item(array
->items
[i
]);
1449 array
->items
= NULL
;
1450 array
->nr
= array
->alloc
= 0;
1453 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1455 struct rev_info revs
;
1457 struct ref_filter
*filter
= ref_cbdata
->filter
;
1458 struct ref_array
*array
= ref_cbdata
->array
;
1459 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1461 init_revisions(&revs
, NULL
);
1463 for (i
= 0; i
< array
->nr
; i
++) {
1464 struct ref_array_item
*item
= array
->items
[i
];
1465 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1466 to_clear
[i
] = item
->commit
;
1469 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1470 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1473 if (prepare_revision_walk(&revs
))
1474 die(_("revision walk setup failed"));
1479 for (i
= 0; i
< old_nr
; i
++) {
1480 struct ref_array_item
*item
= array
->items
[i
];
1481 struct commit
*commit
= item
->commit
;
1483 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1485 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1486 array
->items
[array
->nr
++] = array
->items
[i
];
1488 free_array_item(item
);
1491 for (i
= 0; i
< old_nr
; i
++)
1492 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1493 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1498 * API for filtering a set of refs. Based on the type of refs the user
1499 * has requested, we iterate through those refs and apply filters
1500 * as per the given ref_filter structure and finally store the
1501 * filtered refs in the ref_array structure.
1503 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1505 struct ref_filter_cbdata ref_cbdata
;
1507 unsigned int broken
= 0;
1509 ref_cbdata
.array
= array
;
1510 ref_cbdata
.filter
= filter
;
1512 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1514 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1516 /* Simple per-ref filtering */
1518 die("filter_refs: invalid type");
1521 * For common cases where we need only branches or remotes or tags,
1522 * we only iterate through those refs. If a mix of refs is needed,
1523 * we iterate over all refs and filter out required refs with the help
1524 * of filter_ref_kind().
1526 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1527 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1528 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1529 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1530 else if (filter
->kind
== FILTER_REFS_TAGS
)
1531 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1532 else if (filter
->kind
& FILTER_REFS_ALL
)
1533 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1534 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1535 head_ref(ref_filter_handler
, &ref_cbdata
);
1539 /* Filters that need revision walking */
1540 if (filter
->merge_commit
)
1541 do_merge_filter(&ref_cbdata
);
1546 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1548 struct atom_value
*va
, *vb
;
1550 cmp_type cmp_type
= used_atom
[s
->atom
].type
;
1551 int (*cmp_fn
)(const char *, const char *);
1553 get_ref_atom_value(a
, s
->atom
, &va
);
1554 get_ref_atom_value(b
, s
->atom
, &vb
);
1555 cmp_fn
= s
->ignore_case
? strcasecmp
: strcmp
;
1557 cmp
= versioncmp(va
->s
, vb
->s
);
1558 else if (cmp_type
== FIELD_STR
)
1559 cmp
= cmp_fn(va
->s
, vb
->s
);
1561 if (va
->ul
< vb
->ul
)
1563 else if (va
->ul
== vb
->ul
)
1564 cmp
= cmp_fn(a
->refname
, b
->refname
);
1569 return (s
->reverse
) ? -cmp
: cmp
;
1572 static struct ref_sorting
*ref_sorting
;
1573 static int compare_refs(const void *a_
, const void *b_
)
1575 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1576 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1577 struct ref_sorting
*s
;
1579 for (s
= ref_sorting
; s
; s
= s
->next
) {
1580 int cmp
= cmp_ref_sorting(s
, a
, b
);
1587 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1589 ref_sorting
= sorting
;
1590 QSORT(array
->items
, array
->nr
, compare_refs
);
1593 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1595 struct strbuf
*s
= &state
->stack
->output
;
1597 while (*cp
&& (!ep
|| cp
< ep
)) {
1602 int ch
= hex2chr(cp
+ 1);
1604 strbuf_addch(s
, ch
);
1610 strbuf_addch(s
, *cp
);
1615 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1617 const char *cp
, *sp
, *ep
;
1618 struct strbuf
*final_buf
;
1619 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1621 state
.quote_style
= quote_style
;
1622 push_stack_element(&state
.stack
);
1624 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1625 struct atom_value
*atomv
;
1627 ep
= strchr(sp
, ')');
1629 append_literal(cp
, sp
, &state
);
1630 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1631 atomv
->handler(atomv
, &state
);
1634 sp
= cp
+ strlen(cp
);
1635 append_literal(cp
, sp
, &state
);
1637 if (need_color_reset_at_eol
) {
1638 struct atom_value resetv
;
1639 char color
[COLOR_MAXLEN
] = "";
1641 if (color_parse("reset", color
) < 0)
1642 die("BUG: couldn't parse 'reset' as a color");
1644 append_atom(&resetv
, &state
);
1646 if (state
.stack
->prev
)
1647 die(_("format: %%(end) atom missing"));
1648 final_buf
= &state
.stack
->output
;
1649 fwrite(final_buf
->buf
, 1, final_buf
->len
, stdout
);
1650 pop_stack_element(&state
.stack
);
1654 /* If no sorting option is given, use refname to sort as default */
1655 struct ref_sorting
*ref_default_sorting(void)
1657 static const char cstr_name
[] = "refname";
1659 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1661 sorting
->next
= NULL
;
1662 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1666 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1668 struct ref_sorting
**sorting_tail
= opt
->value
;
1669 struct ref_sorting
*s
;
1672 if (!arg
) /* should --no-sort void the list ? */
1675 s
= xcalloc(1, sizeof(*s
));
1676 s
->next
= *sorting_tail
;
1683 if (skip_prefix(arg
, "version:", &arg
) ||
1684 skip_prefix(arg
, "v:", &arg
))
1687 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1691 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1693 struct ref_filter
*rf
= opt
->value
;
1694 unsigned char sha1
[20];
1696 rf
->merge
= starts_with(opt
->long_name
, "no")
1697 ? REF_FILTER_MERGED_OMIT
1698 : REF_FILTER_MERGED_INCLUDE
;
1700 if (get_sha1(arg
, sha1
))
1701 die(_("malformed object name %s"), arg
);
1703 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1704 if (!rf
->merge_commit
)
1705 return opterror(opt
, "must point to a commit", 0);