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 { "objectsize", FIELD_ULONG
},
29 { "numparent", FIELD_ULONG
},
36 { "authordate", FIELD_TIME
},
40 { "committerdate", FIELD_TIME
},
44 { "taggerdate", FIELD_TIME
},
46 { "creatordate", FIELD_TIME
},
60 #define REF_FORMATTING_STATE_INIT { 0, NULL }
72 struct ref_formatting_stack
{
73 struct ref_formatting_stack
*prev
;
75 void (*at_end
)(struct ref_formatting_stack
*stack
);
79 struct ref_formatting_state
{
81 struct ref_formatting_stack
*stack
;
88 struct contents contents
;
90 void (*handler
)(struct atom_value
*atomv
, struct ref_formatting_state
*state
);
91 unsigned long ul
; /* used for sorting when not FIELD_STR */
95 * An atom is a valid field atom listed above, possibly prefixed with
96 * a "*" to denote deref_tag().
98 * We parse given format string and sort specifiers, and make a list
99 * of properties that we need to extract out of objects. ref_array_item
100 * structure will hold an array of values extracted that can be
101 * indexed with the "atom number", which is an index into this
104 static const char **used_atom
;
105 static cmp_type
*used_atom_type
;
106 static int used_atom_cnt
, need_tagged
, need_symref
;
107 static int need_color_reset_at_eol
;
110 * Used to parse format string and sort specifiers
112 int parse_ref_filter_atom(const char *atom
, const char *ep
)
118 if (*sp
== '*' && sp
< ep
)
121 die("malformed field name: %.*s", (int)(ep
-atom
), atom
);
123 /* Do we have the atom already used elsewhere? */
124 for (i
= 0; i
< used_atom_cnt
; i
++) {
125 int len
= strlen(used_atom
[i
]);
126 if (len
== ep
- atom
&& !memcmp(used_atom
[i
], atom
, len
))
130 /* Is the atom a valid one? */
131 for (i
= 0; i
< ARRAY_SIZE(valid_atom
); i
++) {
132 int len
= strlen(valid_atom
[i
].name
);
134 * If the atom name has a colon, strip it and everything after
135 * it off - it specifies the format for this entry, and
136 * shouldn't be used for checking against the valid_atom
139 const char *formatp
= strchr(sp
, ':');
140 if (!formatp
|| ep
< formatp
)
142 if (len
== formatp
- sp
&& !memcmp(valid_atom
[i
].name
, sp
, len
))
146 if (ARRAY_SIZE(valid_atom
) <= i
)
147 die("unknown field name: %.*s", (int)(ep
-atom
), atom
);
149 /* Add it in, including the deref prefix */
152 REALLOC_ARRAY(used_atom
, used_atom_cnt
);
153 REALLOC_ARRAY(used_atom_type
, used_atom_cnt
);
154 used_atom
[at
] = xmemdupz(atom
, ep
- atom
);
155 used_atom_type
[at
] = valid_atom
[i
].cmp_type
;
158 if (!strcmp(used_atom
[at
], "symref"))
163 static void quote_formatting(struct strbuf
*s
, const char *str
, int quote_style
)
165 switch (quote_style
) {
167 strbuf_addstr(s
, str
);
170 sq_quote_buf(s
, str
);
173 perl_quote_buf(s
, str
);
176 python_quote_buf(s
, str
);
179 tcl_quote_buf(s
, str
);
184 static void append_atom(struct atom_value
*v
, struct ref_formatting_state
*state
)
187 * Quote formatting is only done when the stack has a single
188 * element. Otherwise quote formatting is done on the
189 * element's entire output strbuf when the %(end) atom is
192 if (!state
->stack
->prev
)
193 quote_formatting(&state
->stack
->output
, v
->s
, state
->quote_style
);
195 strbuf_addstr(&state
->stack
->output
, v
->s
);
198 static void push_stack_element(struct ref_formatting_stack
**stack
)
200 struct ref_formatting_stack
*s
= xcalloc(1, sizeof(struct ref_formatting_stack
));
202 strbuf_init(&s
->output
, 0);
207 static void pop_stack_element(struct ref_formatting_stack
**stack
)
209 struct ref_formatting_stack
*current
= *stack
;
210 struct ref_formatting_stack
*prev
= current
->prev
;
213 strbuf_addbuf(&prev
->output
, ¤t
->output
);
214 strbuf_release(¤t
->output
);
219 static void end_align_handler(struct ref_formatting_stack
*stack
)
221 struct align
*align
= (struct align
*)stack
->at_end_data
;
222 struct strbuf s
= STRBUF_INIT
;
224 strbuf_utf8_align(&s
, align
->position
, align
->width
, stack
->output
.buf
);
225 strbuf_swap(&stack
->output
, &s
);
229 static void align_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
231 struct ref_formatting_stack
*new;
233 push_stack_element(&state
->stack
);
235 new->at_end
= end_align_handler
;
236 new->at_end_data
= &atomv
->u
.align
;
239 static void end_atom_handler(struct atom_value
*atomv
, struct ref_formatting_state
*state
)
241 struct ref_formatting_stack
*current
= state
->stack
;
242 struct strbuf s
= STRBUF_INIT
;
244 if (!current
->at_end
)
245 die(_("format: %%(end) atom used without corresponding atom"));
246 current
->at_end(current
);
249 * Perform quote formatting when the stack element is that of
250 * a supporting atom. If nested then perform quote formatting
251 * only on the topmost supporting atom.
253 if (!state
->stack
->prev
->prev
) {
254 quote_formatting(&s
, current
->output
.buf
, state
->quote_style
);
255 strbuf_swap(¤t
->output
, &s
);
258 pop_stack_element(&state
->stack
);
261 static int match_atom_name(const char *name
, const char *atom_name
, const char **val
)
265 if (!skip_prefix(name
, atom_name
, &body
))
266 return 0; /* doesn't even begin with "atom_name" */
268 *val
= NULL
; /* %(atom_name) and no customization */
272 return 0; /* "atom_namefoo" is not "atom_name" or "atom_name:..." */
273 *val
= body
+ 1; /* "atom_name:val" */
278 * In a format string, find the next occurrence of %(atom).
280 static const char *find_next(const char *cp
)
285 * %( is the start of an atom;
286 * %% is a quoted per-cent.
290 else if (cp
[1] == '%')
291 cp
++; /* skip over two % */
292 /* otherwise this is a singleton, literal % */
300 * Make sure the format string is well formed, and parse out
303 int verify_ref_format(const char *format
)
307 need_color_reset_at_eol
= 0;
308 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); ) {
309 const char *color
, *ep
= strchr(sp
, ')');
313 return error("malformed format string %s", sp
);
314 /* sp points at "%(" and ep points at the closing ")" */
315 at
= parse_ref_filter_atom(sp
+ 2, ep
);
318 if (skip_prefix(used_atom
[at
], "color:", &color
))
319 need_color_reset_at_eol
= !!strcmp(color
, "reset");
325 * Given an object name, read the object data and size, and return a
326 * "struct object". If the object data we are returning is also borrowed
327 * by the "struct object" representation, set *eaten as well---it is a
328 * signal from parse_object_buffer to us not to free the buffer.
330 static void *get_obj(const unsigned char *sha1
, struct object
**obj
, unsigned long *sz
, int *eaten
)
332 enum object_type type
;
333 void *buf
= read_sha1_file(sha1
, &type
, sz
);
336 *obj
= parse_object_buffer(sha1
, type
, *sz
, buf
, eaten
);
342 static int grab_objectname(const char *name
, const unsigned char *sha1
,
343 struct atom_value
*v
)
345 if (!strcmp(name
, "objectname")) {
346 char *s
= xmalloc(41);
347 strcpy(s
, sha1_to_hex(sha1
));
351 if (!strcmp(name
, "objectname:short")) {
352 v
->s
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
358 /* See grab_values */
359 static void grab_common_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
363 for (i
= 0; i
< used_atom_cnt
; i
++) {
364 const char *name
= used_atom
[i
];
365 struct atom_value
*v
= &val
[i
];
366 if (!!deref
!= (*name
== '*'))
370 if (!strcmp(name
, "objecttype"))
371 v
->s
= typename(obj
->type
);
372 else if (!strcmp(name
, "objectsize")) {
373 char *s
= xmalloc(40);
374 sprintf(s
, "%lu", sz
);
379 grab_objectname(name
, obj
->sha1
, v
);
383 /* See grab_values */
384 static void grab_tag_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
387 struct tag
*tag
= (struct tag
*) obj
;
389 for (i
= 0; i
< used_atom_cnt
; i
++) {
390 const char *name
= used_atom
[i
];
391 struct atom_value
*v
= &val
[i
];
392 if (!!deref
!= (*name
== '*'))
396 if (!strcmp(name
, "tag"))
398 else if (!strcmp(name
, "type") && tag
->tagged
)
399 v
->s
= typename(tag
->tagged
->type
);
400 else if (!strcmp(name
, "object") && tag
->tagged
) {
401 char *s
= xmalloc(41);
402 strcpy(s
, sha1_to_hex(tag
->tagged
->sha1
));
408 /* See grab_values */
409 static void grab_commit_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
412 struct commit
*commit
= (struct commit
*) obj
;
414 for (i
= 0; i
< used_atom_cnt
; i
++) {
415 const char *name
= used_atom
[i
];
416 struct atom_value
*v
= &val
[i
];
417 if (!!deref
!= (*name
== '*'))
421 if (!strcmp(name
, "tree")) {
422 char *s
= xmalloc(41);
423 strcpy(s
, sha1_to_hex(commit
->tree
->object
.sha1
));
426 if (!strcmp(name
, "numparent")) {
427 char *s
= xmalloc(40);
428 v
->ul
= commit_list_count(commit
->parents
);
429 sprintf(s
, "%lu", v
->ul
);
432 else if (!strcmp(name
, "parent")) {
433 int num
= commit_list_count(commit
->parents
);
435 struct commit_list
*parents
;
436 char *s
= xmalloc(41 * num
+ 1);
438 for (i
= 0, parents
= commit
->parents
;
440 parents
= parents
->next
, i
= i
+ 41) {
441 struct commit
*parent
= parents
->item
;
442 strcpy(s
+i
, sha1_to_hex(parent
->object
.sha1
));
452 static const char *find_wholine(const char *who
, int wholen
, const char *buf
, unsigned long sz
)
456 if (!strncmp(buf
, who
, wholen
) &&
458 return buf
+ wholen
+ 1;
459 eol
= strchr(buf
, '\n');
464 return ""; /* end of header */
470 static const char *copy_line(const char *buf
)
472 const char *eol
= strchrnul(buf
, '\n');
473 return xmemdupz(buf
, eol
- buf
);
476 static const char *copy_name(const char *buf
)
479 for (cp
= buf
; *cp
&& *cp
!= '\n'; cp
++) {
480 if (!strncmp(cp
, " <", 2))
481 return xmemdupz(buf
, cp
- buf
);
486 static const char *copy_email(const char *buf
)
488 const char *email
= strchr(buf
, '<');
492 eoemail
= strchr(email
, '>');
495 return xmemdupz(email
, eoemail
+ 1 - email
);
498 static char *copy_subject(const char *buf
, unsigned long len
)
500 char *r
= xmemdupz(buf
, len
);
503 for (i
= 0; i
< len
; i
++)
510 static void grab_date(const char *buf
, struct atom_value
*v
, const char *atomname
)
512 const char *eoemail
= strstr(buf
, "> ");
514 unsigned long timestamp
;
516 struct date_mode date_mode
= { DATE_NORMAL
};
520 * We got here because atomname ends in "date" or "date<something>";
521 * it's not possible that <something> is not ":<format>" because
522 * parse_ref_filter_atom() wouldn't have allowed it, so we can assume that no
523 * ":" means no format is specified, and use the default.
525 formatp
= strchr(atomname
, ':');
526 if (formatp
!= NULL
) {
528 parse_date_format(formatp
, &date_mode
);
533 timestamp
= strtoul(eoemail
+ 2, &zone
, 10);
534 if (timestamp
== ULONG_MAX
)
536 tz
= strtol(zone
, NULL
, 10);
537 if ((tz
== LONG_MIN
|| tz
== LONG_MAX
) && errno
== ERANGE
)
539 v
->s
= xstrdup(show_date(timestamp
, tz
, &date_mode
));
547 /* See grab_values */
548 static void grab_person(const char *who
, struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
551 int wholen
= strlen(who
);
552 const char *wholine
= NULL
;
554 for (i
= 0; i
< used_atom_cnt
; i
++) {
555 const char *name
= used_atom
[i
];
556 struct atom_value
*v
= &val
[i
];
557 if (!!deref
!= (*name
== '*'))
561 if (strncmp(who
, name
, wholen
))
563 if (name
[wholen
] != 0 &&
564 strcmp(name
+ wholen
, "name") &&
565 strcmp(name
+ wholen
, "email") &&
566 !starts_with(name
+ wholen
, "date"))
569 wholine
= find_wholine(who
, wholen
, buf
, sz
);
571 return; /* no point looking for it */
572 if (name
[wholen
] == 0)
573 v
->s
= copy_line(wholine
);
574 else if (!strcmp(name
+ wholen
, "name"))
575 v
->s
= copy_name(wholine
);
576 else if (!strcmp(name
+ wholen
, "email"))
577 v
->s
= copy_email(wholine
);
578 else if (starts_with(name
+ wholen
, "date"))
579 grab_date(wholine
, v
, name
);
583 * For a tag or a commit object, if "creator" or "creatordate" is
584 * requested, do something special.
586 if (strcmp(who
, "tagger") && strcmp(who
, "committer"))
587 return; /* "author" for commit object is not wanted */
589 wholine
= find_wholine(who
, wholen
, buf
, sz
);
592 for (i
= 0; i
< used_atom_cnt
; i
++) {
593 const char *name
= used_atom
[i
];
594 struct atom_value
*v
= &val
[i
];
595 if (!!deref
!= (*name
== '*'))
600 if (starts_with(name
, "creatordate"))
601 grab_date(wholine
, v
, name
);
602 else if (!strcmp(name
, "creator"))
603 v
->s
= copy_line(wholine
);
607 static void find_subpos(const char *buf
, unsigned long sz
,
608 const char **sub
, unsigned long *sublen
,
609 const char **body
, unsigned long *bodylen
,
610 unsigned long *nonsiglen
,
611 const char **sig
, unsigned long *siglen
)
614 /* skip past header until we hit empty line */
615 while (*buf
&& *buf
!= '\n') {
616 eol
= strchrnul(buf
, '\n');
621 /* skip any empty lines */
625 /* parse signature first; we might not even have a subject line */
626 *sig
= buf
+ parse_signature(buf
, strlen(buf
));
627 *siglen
= strlen(*sig
);
629 /* subject is first non-empty line */
631 /* subject goes to first empty line */
632 while (buf
< *sig
&& *buf
&& *buf
!= '\n') {
633 eol
= strchrnul(buf
, '\n');
638 *sublen
= buf
- *sub
;
639 /* drop trailing newline, if present */
640 if (*sublen
&& (*sub
)[*sublen
- 1] == '\n')
643 /* skip any empty lines */
647 *bodylen
= strlen(buf
);
648 *nonsiglen
= *sig
- buf
;
652 * If 'lines' is greater than 0, append that many lines from the given
653 * 'buf' of length 'size' to the given strbuf.
655 static void append_lines(struct strbuf
*out
, const char *buf
, unsigned long size
, int lines
)
658 const char *sp
, *eol
;
663 for (i
= 0; i
< lines
&& sp
< buf
+ size
; i
++) {
665 strbuf_addstr(out
, "\n ");
666 eol
= memchr(sp
, '\n', size
- (sp
- buf
));
667 len
= eol
? eol
- sp
: size
- (sp
- buf
);
668 strbuf_add(out
, sp
, len
);
675 /* See grab_values */
676 static void grab_sub_body_contents(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
679 const char *subpos
= NULL
, *bodypos
= NULL
, *sigpos
= NULL
;
680 unsigned long sublen
= 0, bodylen
= 0, nonsiglen
= 0, siglen
= 0;
682 for (i
= 0; i
< used_atom_cnt
; i
++) {
683 const char *name
= used_atom
[i
];
684 struct atom_value
*v
= &val
[i
];
685 const char *valp
= NULL
;
686 if (!!deref
!= (*name
== '*'))
690 if (strcmp(name
, "subject") &&
691 strcmp(name
, "body") &&
692 strcmp(name
, "contents") &&
693 strcmp(name
, "contents:subject") &&
694 strcmp(name
, "contents:body") &&
695 strcmp(name
, "contents:signature") &&
696 !starts_with(name
, "contents:lines="))
701 &bodypos
, &bodylen
, &nonsiglen
,
704 if (!strcmp(name
, "subject"))
705 v
->s
= copy_subject(subpos
, sublen
);
706 else if (!strcmp(name
, "contents:subject"))
707 v
->s
= copy_subject(subpos
, sublen
);
708 else if (!strcmp(name
, "body"))
709 v
->s
= xmemdupz(bodypos
, bodylen
);
710 else if (!strcmp(name
, "contents:body"))
711 v
->s
= xmemdupz(bodypos
, nonsiglen
);
712 else if (!strcmp(name
, "contents:signature"))
713 v
->s
= xmemdupz(sigpos
, siglen
);
714 else if (!strcmp(name
, "contents"))
715 v
->s
= xstrdup(subpos
);
716 else if (skip_prefix(name
, "contents:lines=", &valp
)) {
717 struct strbuf s
= STRBUF_INIT
;
718 const char *contents_end
= bodylen
+ bodypos
- siglen
;
720 if (strtoul_ui(valp
, 10, &v
->u
.contents
.lines
))
721 die(_("positive value expected contents:lines=%s"), valp
);
722 /* Size is the length of the message after removing the signature */
723 append_lines(&s
, subpos
, contents_end
- subpos
, v
->u
.contents
.lines
);
724 v
->s
= strbuf_detach(&s
, NULL
);
730 * We want to have empty print-string for field requests
731 * that do not apply (e.g. "authordate" for a tag object)
733 static void fill_missing_values(struct atom_value
*val
)
736 for (i
= 0; i
< used_atom_cnt
; i
++) {
737 struct atom_value
*v
= &val
[i
];
744 * val is a list of atom_value to hold returned values. Extract
745 * the values for atoms in used_atom array out of (obj, buf, sz).
746 * when deref is false, (obj, buf, sz) is the object that is
747 * pointed at by the ref itself; otherwise it is the object the
748 * ref (which is a tag) refers to.
750 static void grab_values(struct atom_value
*val
, int deref
, struct object
*obj
, void *buf
, unsigned long sz
)
752 grab_common_values(val
, deref
, obj
, buf
, sz
);
755 grab_tag_values(val
, deref
, obj
, buf
, sz
);
756 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
757 grab_person("tagger", val
, deref
, obj
, buf
, sz
);
760 grab_commit_values(val
, deref
, obj
, buf
, sz
);
761 grab_sub_body_contents(val
, deref
, obj
, buf
, sz
);
762 grab_person("author", val
, deref
, obj
, buf
, sz
);
763 grab_person("committer", val
, deref
, obj
, buf
, sz
);
766 /* grab_tree_values(val, deref, obj, buf, sz); */
769 /* grab_blob_values(val, deref, obj, buf, sz); */
772 die("Eh? Object of type %d?", obj
->type
);
776 static inline char *copy_advance(char *dst
, const char *src
)
784 * Parse the object referred by ref, and grab needed value.
786 static void populate_value(struct ref_array_item
*ref
)
792 const unsigned char *tagged
;
794 ref
->value
= xcalloc(used_atom_cnt
, sizeof(struct atom_value
));
796 if (need_symref
&& (ref
->flag
& REF_ISSYMREF
) && !ref
->symref
) {
797 unsigned char unused1
[20];
798 ref
->symref
= resolve_refdup(ref
->refname
, RESOLVE_REF_READING
,
804 /* Fill in specials first */
805 for (i
= 0; i
< used_atom_cnt
; i
++) {
806 const char *name
= used_atom
[i
];
807 struct atom_value
*v
= &ref
->value
[i
];
812 struct branch
*branch
= NULL
;
814 v
->handler
= append_atom
;
821 if (starts_with(name
, "refname"))
822 refname
= ref
->refname
;
823 else if (starts_with(name
, "symref"))
824 refname
= ref
->symref
? ref
->symref
: "";
825 else if (starts_with(name
, "upstream")) {
826 const char *branch_name
;
827 /* only local branches may have an upstream */
828 if (!skip_prefix(ref
->refname
, "refs/heads/",
831 branch
= branch_get(branch_name
);
833 refname
= branch_get_upstream(branch
, NULL
);
836 } else if (starts_with(name
, "push")) {
837 const char *branch_name
;
838 if (!skip_prefix(ref
->refname
, "refs/heads/",
841 branch
= branch_get(branch_name
);
843 refname
= branch_get_push(branch
, NULL
);
846 } else if (match_atom_name(name
, "color", &valp
)) {
847 char color
[COLOR_MAXLEN
] = "";
850 die(_("expected format: %%(color:<color>)"));
851 if (color_parse(valp
, color
) < 0)
852 die(_("unable to parse format"));
853 v
->s
= xstrdup(color
);
855 } else if (!strcmp(name
, "flag")) {
856 char buf
[256], *cp
= buf
;
857 if (ref
->flag
& REF_ISSYMREF
)
858 cp
= copy_advance(cp
, ",symref");
859 if (ref
->flag
& REF_ISPACKED
)
860 cp
= copy_advance(cp
, ",packed");
865 v
->s
= xstrdup(buf
+ 1);
868 } else if (!deref
&& grab_objectname(name
, ref
->objectname
, v
)) {
870 } else if (!strcmp(name
, "HEAD")) {
872 unsigned char sha1
[20];
874 head
= resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
,
876 if (!strcmp(ref
->refname
, head
))
881 } else if (match_atom_name(name
, "align", &valp
)) {
882 struct align
*align
= &v
->u
.align
;
883 struct strbuf
**s
, **to_free
;
887 die(_("expected format: %%(align:<width>,<position>)"));
890 * TODO: Implement a function similar to strbuf_split_str()
891 * which would omit the separator from the end of each value.
893 s
= to_free
= strbuf_split_str(valp
, ',', 0);
895 align
->position
= ALIGN_LEFT
;
898 /* Strip trailing comma */
900 strbuf_setlen(s
[0], s
[0]->len
- 1);
901 if (!strtoul_ui(s
[0]->buf
, 10, (unsigned int *)&width
))
903 else if (!strcmp(s
[0]->buf
, "left"))
904 align
->position
= ALIGN_LEFT
;
905 else if (!strcmp(s
[0]->buf
, "right"))
906 align
->position
= ALIGN_RIGHT
;
907 else if (!strcmp(s
[0]->buf
, "middle"))
908 align
->position
= ALIGN_MIDDLE
;
910 die(_("improper format entered align:%s"), s
[0]->buf
);
915 die(_("positive width expected with the %%(align) atom"));
916 align
->width
= width
;
917 strbuf_list_free(to_free
);
918 v
->handler
= align_atom_handler
;
920 } else if (!strcmp(name
, "end")) {
921 v
->handler
= end_atom_handler
;
926 formatp
= strchr(name
, ':');
928 int num_ours
, num_theirs
;
931 if (!strcmp(formatp
, "short"))
932 refname
= shorten_unambiguous_ref(refname
,
933 warn_ambiguous_refs
);
934 else if (!strcmp(formatp
, "track") &&
935 (starts_with(name
, "upstream") ||
936 starts_with(name
, "push"))) {
939 if (stat_tracking_info(branch
, &num_ours
,
943 if (!num_ours
&& !num_theirs
)
945 else if (!num_ours
) {
946 sprintf(buf
, "[behind %d]", num_theirs
);
948 } else if (!num_theirs
) {
949 sprintf(buf
, "[ahead %d]", num_ours
);
952 sprintf(buf
, "[ahead %d, behind %d]",
953 num_ours
, num_theirs
);
957 } else if (!strcmp(formatp
, "trackshort") &&
958 (starts_with(name
, "upstream") ||
959 starts_with(name
, "push"))) {
962 if (stat_tracking_info(branch
, &num_ours
,
966 if (!num_ours
&& !num_theirs
)
970 else if (!num_theirs
)
976 die("unknown %.*s format %s",
977 (int)(formatp
- name
), name
, formatp
);
983 int len
= strlen(refname
);
984 char *s
= xmalloc(len
+ 4);
985 sprintf(s
, "%s^{}", refname
);
990 for (i
= 0; i
< used_atom_cnt
; i
++) {
991 struct atom_value
*v
= &ref
->value
[i
];
998 buf
= get_obj(ref
->objectname
, &obj
, &size
, &eaten
);
1000 die("missing object %s for %s",
1001 sha1_to_hex(ref
->objectname
), ref
->refname
);
1003 die("parse_object_buffer failed on %s for %s",
1004 sha1_to_hex(ref
->objectname
), ref
->refname
);
1006 grab_values(ref
->value
, 0, obj
, buf
, size
);
1011 * If there is no atom that wants to know about tagged
1012 * object, we are done.
1014 if (!need_tagged
|| (obj
->type
!= OBJ_TAG
))
1018 * If it is a tag object, see if we use a value that derefs
1019 * the object, and if we do grab the object it refers to.
1021 tagged
= ((struct tag
*)obj
)->tagged
->sha1
;
1024 * NEEDSWORK: This derefs tag only once, which
1025 * is good to deal with chains of trust, but
1026 * is not consistent with what deref_tag() does
1027 * which peels the onion to the core.
1029 buf
= get_obj(tagged
, &obj
, &size
, &eaten
);
1031 die("missing object %s for %s",
1032 sha1_to_hex(tagged
), ref
->refname
);
1034 die("parse_object_buffer failed on %s for %s",
1035 sha1_to_hex(tagged
), ref
->refname
);
1036 grab_values(ref
->value
, 1, obj
, buf
, size
);
1042 * Given a ref, return the value for the atom. This lazily gets value
1043 * out of the object by calling populate value.
1045 static void get_ref_atom_value(struct ref_array_item
*ref
, int atom
, struct atom_value
**v
)
1048 populate_value(ref
);
1049 fill_missing_values(ref
->value
);
1051 *v
= &ref
->value
[atom
];
1054 enum contains_result
{
1055 CONTAINS_UNKNOWN
= -1,
1061 * Mimicking the real stack, this stack lives on the heap, avoiding stack
1064 * At each recursion step, the stack items points to the commits whose
1065 * ancestors are to be inspected.
1067 struct contains_stack
{
1069 struct contains_stack_entry
{
1070 struct commit
*commit
;
1071 struct commit_list
*parents
;
1075 static int in_commit_list(const struct commit_list
*want
, struct commit
*c
)
1077 for (; want
; want
= want
->next
)
1078 if (!hashcmp(want
->item
->object
.sha1
, c
->object
.sha1
))
1084 * Test whether the candidate or one of its parents is contained in the list.
1085 * Do not recurse to find out, though, but return -1 if inconclusive.
1087 static enum contains_result
contains_test(struct commit
*candidate
,
1088 const struct commit_list
*want
)
1090 /* was it previously marked as containing a want commit? */
1091 if (candidate
->object
.flags
& TMP_MARK
)
1093 /* or marked as not possibly containing a want commit? */
1094 if (candidate
->object
.flags
& UNINTERESTING
)
1097 if (in_commit_list(want
, candidate
)) {
1098 candidate
->object
.flags
|= TMP_MARK
;
1102 if (parse_commit(candidate
) < 0)
1108 static void push_to_contains_stack(struct commit
*candidate
, struct contains_stack
*contains_stack
)
1110 ALLOC_GROW(contains_stack
->contains_stack
, contains_stack
->nr
+ 1, contains_stack
->alloc
);
1111 contains_stack
->contains_stack
[contains_stack
->nr
].commit
= candidate
;
1112 contains_stack
->contains_stack
[contains_stack
->nr
++].parents
= candidate
->parents
;
1115 static enum contains_result
contains_tag_algo(struct commit
*candidate
,
1116 const struct commit_list
*want
)
1118 struct contains_stack contains_stack
= { 0, 0, NULL
};
1119 int result
= contains_test(candidate
, want
);
1121 if (result
!= CONTAINS_UNKNOWN
)
1124 push_to_contains_stack(candidate
, &contains_stack
);
1125 while (contains_stack
.nr
) {
1126 struct contains_stack_entry
*entry
= &contains_stack
.contains_stack
[contains_stack
.nr
- 1];
1127 struct commit
*commit
= entry
->commit
;
1128 struct commit_list
*parents
= entry
->parents
;
1131 commit
->object
.flags
|= UNINTERESTING
;
1132 contains_stack
.nr
--;
1135 * If we just popped the stack, parents->item has been marked,
1136 * therefore contains_test will return a meaningful 0 or 1.
1138 else switch (contains_test(parents
->item
, want
)) {
1140 commit
->object
.flags
|= TMP_MARK
;
1141 contains_stack
.nr
--;
1144 entry
->parents
= parents
->next
;
1146 case CONTAINS_UNKNOWN
:
1147 push_to_contains_stack(parents
->item
, &contains_stack
);
1151 free(contains_stack
.contains_stack
);
1152 return contains_test(candidate
, want
);
1155 static int commit_contains(struct ref_filter
*filter
, struct commit
*commit
)
1157 if (filter
->with_commit_tag_algo
)
1158 return contains_tag_algo(commit
, filter
->with_commit
);
1159 return is_descendant_of(commit
, filter
->with_commit
);
1163 * Return 1 if the refname matches one of the patterns, otherwise 0.
1164 * A pattern can be a literal prefix (e.g. a refname "refs/heads/master"
1165 * matches a pattern "refs/heads/mas") or a wildcard (e.g. the same ref
1166 * matches "refs/heads/mas*", too).
1168 static int match_pattern(const char **patterns
, const char *refname
)
1171 * When no '--format' option is given we need to skip the prefix
1172 * for matching refs of tags and branches.
1174 (void)(skip_prefix(refname
, "refs/tags/", &refname
) ||
1175 skip_prefix(refname
, "refs/heads/", &refname
) ||
1176 skip_prefix(refname
, "refs/remotes/", &refname
) ||
1177 skip_prefix(refname
, "refs/", &refname
));
1179 for (; *patterns
; patterns
++) {
1180 if (!wildmatch(*patterns
, refname
, 0, NULL
))
1187 * Return 1 if the refname matches one of the patterns, otherwise 0.
1188 * A pattern can be path prefix (e.g. a refname "refs/heads/master"
1189 * matches a pattern "refs/heads/" but not "refs/heads/m") or a
1190 * wildcard (e.g. the same ref matches "refs/heads/m*", too).
1192 static int match_name_as_path(const char **pattern
, const char *refname
)
1194 int namelen
= strlen(refname
);
1195 for (; *pattern
; pattern
++) {
1196 const char *p
= *pattern
;
1197 int plen
= strlen(p
);
1199 if ((plen
<= namelen
) &&
1200 !strncmp(refname
, p
, plen
) &&
1201 (refname
[plen
] == '\0' ||
1202 refname
[plen
] == '/' ||
1205 if (!wildmatch(p
, refname
, WM_PATHNAME
, NULL
))
1211 /* Return 1 if the refname matches one of the patterns, otherwise 0. */
1212 static int filter_pattern_match(struct ref_filter
*filter
, const char *refname
)
1214 if (!*filter
->name_patterns
)
1215 return 1; /* No pattern always matches */
1216 if (filter
->match_as_path
)
1217 return match_name_as_path(filter
->name_patterns
, refname
);
1218 return match_pattern(filter
->name_patterns
, refname
);
1222 * Given a ref (sha1, refname), check if the ref belongs to the array
1223 * of sha1s. If the given ref is a tag, check if the given tag points
1224 * at one of the sha1s in the given sha1 array.
1225 * the given sha1_array.
1227 * 1. Only a single level of inderection is obtained, we might want to
1228 * change this to account for multiple levels (e.g. annotated tags
1229 * pointing to annotated tags pointing to a commit.)
1230 * 2. As the refs are cached we might know what refname peels to without
1231 * the need to parse the object via parse_object(). peel_ref() might be a
1232 * more efficient alternative to obtain the pointee.
1234 static const unsigned char *match_points_at(struct sha1_array
*points_at
,
1235 const unsigned char *sha1
,
1236 const char *refname
)
1238 const unsigned char *tagged_sha1
= NULL
;
1241 if (sha1_array_lookup(points_at
, sha1
) >= 0)
1243 obj
= parse_object(sha1
);
1245 die(_("malformed object at '%s'"), refname
);
1246 if (obj
->type
== OBJ_TAG
)
1247 tagged_sha1
= ((struct tag
*)obj
)->tagged
->sha1
;
1248 if (tagged_sha1
&& sha1_array_lookup(points_at
, tagged_sha1
) >= 0)
1253 /* Allocate space for a new ref_array_item and copy the objectname and flag to it */
1254 static struct ref_array_item
*new_ref_array_item(const char *refname
,
1255 const unsigned char *objectname
,
1258 size_t len
= strlen(refname
);
1259 struct ref_array_item
*ref
= xcalloc(1, sizeof(struct ref_array_item
) + len
+ 1);
1260 memcpy(ref
->refname
, refname
, len
);
1261 ref
->refname
[len
] = '\0';
1262 hashcpy(ref
->objectname
, objectname
);
1268 static int filter_ref_kind(struct ref_filter
*filter
, const char *refname
)
1276 { "refs/heads/" , FILTER_REFS_BRANCHES
},
1277 { "refs/remotes/" , FILTER_REFS_REMOTES
},
1278 { "refs/tags/", FILTER_REFS_TAGS
}
1281 if (filter
->kind
== FILTER_REFS_BRANCHES
||
1282 filter
->kind
== FILTER_REFS_REMOTES
||
1283 filter
->kind
== FILTER_REFS_TAGS
)
1284 return filter
->kind
;
1285 else if (!strcmp(refname
, "HEAD"))
1286 return FILTER_REFS_DETACHED_HEAD
;
1288 for (i
= 0; i
< ARRAY_SIZE(ref_kind
); i
++) {
1289 if (starts_with(refname
, ref_kind
[i
].prefix
))
1290 return ref_kind
[i
].kind
;
1293 return FILTER_REFS_OTHERS
;
1297 * A call-back given to for_each_ref(). Filter refs and keep them for
1298 * later object processing.
1300 static int ref_filter_handler(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
1302 struct ref_filter_cbdata
*ref_cbdata
= cb_data
;
1303 struct ref_filter
*filter
= ref_cbdata
->filter
;
1304 struct ref_array_item
*ref
;
1305 struct commit
*commit
= NULL
;
1308 if (flag
& REF_BAD_NAME
) {
1309 warning("ignoring ref with broken name %s", refname
);
1313 if (flag
& REF_ISBROKEN
) {
1314 warning("ignoring broken ref %s", refname
);
1318 /* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
1319 kind
= filter_ref_kind(filter
, refname
);
1320 if (!(kind
& filter
->kind
))
1323 if (!filter_pattern_match(filter
, refname
))
1326 if (filter
->points_at
.nr
&& !match_points_at(&filter
->points_at
, oid
->hash
, refname
))
1330 * A merge filter is applied on refs pointing to commits. Hence
1331 * obtain the commit using the 'oid' available and discard all
1332 * non-commits early. The actual filtering is done later.
1334 if (filter
->merge_commit
|| filter
->with_commit
) {
1335 commit
= lookup_commit_reference_gently(oid
->hash
, 1);
1338 /* We perform the filtering for the '--contains' option */
1339 if (filter
->with_commit
&&
1340 !commit_contains(filter
, commit
))
1345 * We do not open the object yet; sort may only need refname
1346 * to do its job and the resulting list may yet to be pruned
1347 * by maxcount logic.
1349 ref
= new_ref_array_item(refname
, oid
->hash
, flag
);
1350 ref
->commit
= commit
;
1352 REALLOC_ARRAY(ref_cbdata
->array
->items
, ref_cbdata
->array
->nr
+ 1);
1353 ref_cbdata
->array
->items
[ref_cbdata
->array
->nr
++] = ref
;
1358 /* Free memory allocated for a ref_array_item */
1359 static void free_array_item(struct ref_array_item
*item
)
1361 free((char *)item
->symref
);
1365 /* Free all memory allocated for ref_array */
1366 void ref_array_clear(struct ref_array
*array
)
1370 for (i
= 0; i
< array
->nr
; i
++)
1371 free_array_item(array
->items
[i
]);
1373 array
->items
= NULL
;
1374 array
->nr
= array
->alloc
= 0;
1377 static void do_merge_filter(struct ref_filter_cbdata
*ref_cbdata
)
1379 struct rev_info revs
;
1381 struct ref_filter
*filter
= ref_cbdata
->filter
;
1382 struct ref_array
*array
= ref_cbdata
->array
;
1383 struct commit
**to_clear
= xcalloc(sizeof(struct commit
*), array
->nr
);
1385 init_revisions(&revs
, NULL
);
1387 for (i
= 0; i
< array
->nr
; i
++) {
1388 struct ref_array_item
*item
= array
->items
[i
];
1389 add_pending_object(&revs
, &item
->commit
->object
, item
->refname
);
1390 to_clear
[i
] = item
->commit
;
1393 filter
->merge_commit
->object
.flags
|= UNINTERESTING
;
1394 add_pending_object(&revs
, &filter
->merge_commit
->object
, "");
1397 if (prepare_revision_walk(&revs
))
1398 die(_("revision walk setup failed"));
1403 for (i
= 0; i
< old_nr
; i
++) {
1404 struct ref_array_item
*item
= array
->items
[i
];
1405 struct commit
*commit
= item
->commit
;
1407 int is_merged
= !!(commit
->object
.flags
& UNINTERESTING
);
1409 if (is_merged
== (filter
->merge
== REF_FILTER_MERGED_INCLUDE
))
1410 array
->items
[array
->nr
++] = array
->items
[i
];
1412 free_array_item(item
);
1415 for (i
= 0; i
< old_nr
; i
++)
1416 clear_commit_marks(to_clear
[i
], ALL_REV_FLAGS
);
1417 clear_commit_marks(filter
->merge_commit
, ALL_REV_FLAGS
);
1422 * API for filtering a set of refs. Based on the type of refs the user
1423 * has requested, we iterate through those refs and apply filters
1424 * as per the given ref_filter structure and finally store the
1425 * filtered refs in the ref_array structure.
1427 int filter_refs(struct ref_array
*array
, struct ref_filter
*filter
, unsigned int type
)
1429 struct ref_filter_cbdata ref_cbdata
;
1431 unsigned int broken
= 0;
1433 ref_cbdata
.array
= array
;
1434 ref_cbdata
.filter
= filter
;
1436 if (type
& FILTER_REFS_INCLUDE_BROKEN
)
1438 filter
->kind
= type
& FILTER_REFS_KIND_MASK
;
1440 /* Simple per-ref filtering */
1442 die("filter_refs: invalid type");
1445 * For common cases where we need only branches or remotes or tags,
1446 * we only iterate through those refs. If a mix of refs is needed,
1447 * we iterate over all refs and filter out required refs with the help
1448 * of filter_ref_kind().
1450 if (filter
->kind
== FILTER_REFS_BRANCHES
)
1451 ret
= for_each_fullref_in("refs/heads/", ref_filter_handler
, &ref_cbdata
, broken
);
1452 else if (filter
->kind
== FILTER_REFS_REMOTES
)
1453 ret
= for_each_fullref_in("refs/remotes/", ref_filter_handler
, &ref_cbdata
, broken
);
1454 else if (filter
->kind
== FILTER_REFS_TAGS
)
1455 ret
= for_each_fullref_in("refs/tags/", ref_filter_handler
, &ref_cbdata
, broken
);
1456 else if (filter
->kind
& FILTER_REFS_ALL
)
1457 ret
= for_each_fullref_in("", ref_filter_handler
, &ref_cbdata
, broken
);
1458 if (!ret
&& (filter
->kind
& FILTER_REFS_DETACHED_HEAD
))
1459 head_ref(ref_filter_handler
, &ref_cbdata
);
1463 /* Filters that need revision walking */
1464 if (filter
->merge_commit
)
1465 do_merge_filter(&ref_cbdata
);
1470 static int cmp_ref_sorting(struct ref_sorting
*s
, struct ref_array_item
*a
, struct ref_array_item
*b
)
1472 struct atom_value
*va
, *vb
;
1474 cmp_type cmp_type
= used_atom_type
[s
->atom
];
1476 get_ref_atom_value(a
, s
->atom
, &va
);
1477 get_ref_atom_value(b
, s
->atom
, &vb
);
1479 cmp
= versioncmp(va
->s
, vb
->s
);
1480 else if (cmp_type
== FIELD_STR
)
1481 cmp
= strcmp(va
->s
, vb
->s
);
1483 if (va
->ul
< vb
->ul
)
1485 else if (va
->ul
== vb
->ul
)
1491 return (s
->reverse
) ? -cmp
: cmp
;
1494 static struct ref_sorting
*ref_sorting
;
1495 static int compare_refs(const void *a_
, const void *b_
)
1497 struct ref_array_item
*a
= *((struct ref_array_item
**)a_
);
1498 struct ref_array_item
*b
= *((struct ref_array_item
**)b_
);
1499 struct ref_sorting
*s
;
1501 for (s
= ref_sorting
; s
; s
= s
->next
) {
1502 int cmp
= cmp_ref_sorting(s
, a
, b
);
1509 void ref_array_sort(struct ref_sorting
*sorting
, struct ref_array
*array
)
1511 ref_sorting
= sorting
;
1512 qsort(array
->items
, array
->nr
, sizeof(struct ref_array_item
*), compare_refs
);
1515 static int hex1(char ch
)
1517 if ('0' <= ch
&& ch
<= '9')
1519 else if ('a' <= ch
&& ch
<= 'f')
1520 return ch
- 'a' + 10;
1521 else if ('A' <= ch
&& ch
<= 'F')
1522 return ch
- 'A' + 10;
1525 static int hex2(const char *cp
)
1528 return (hex1(cp
[0]) << 4) | hex1(cp
[1]);
1533 static void append_literal(const char *cp
, const char *ep
, struct ref_formatting_state
*state
)
1535 struct strbuf
*s
= &state
->stack
->output
;
1537 while (*cp
&& (!ep
|| cp
< ep
)) {
1542 int ch
= hex2(cp
+ 1);
1544 strbuf_addch(s
, ch
);
1550 strbuf_addch(s
, *cp
);
1555 void show_ref_array_item(struct ref_array_item
*info
, const char *format
, int quote_style
)
1557 const char *cp
, *sp
, *ep
;
1558 struct strbuf
*final_buf
;
1559 struct ref_formatting_state state
= REF_FORMATTING_STATE_INIT
;
1561 state
.quote_style
= quote_style
;
1562 push_stack_element(&state
.stack
);
1564 for (cp
= format
; *cp
&& (sp
= find_next(cp
)); cp
= ep
+ 1) {
1565 struct atom_value
*atomv
;
1567 ep
= strchr(sp
, ')');
1569 append_literal(cp
, sp
, &state
);
1570 get_ref_atom_value(info
, parse_ref_filter_atom(sp
+ 2, ep
), &atomv
);
1571 atomv
->handler(atomv
, &state
);
1574 sp
= cp
+ strlen(cp
);
1575 append_literal(cp
, sp
, &state
);
1577 if (need_color_reset_at_eol
) {
1578 struct atom_value resetv
;
1579 char color
[COLOR_MAXLEN
] = "";
1581 if (color_parse("reset", color
) < 0)
1582 die("BUG: couldn't parse 'reset' as a color");
1584 append_atom(&resetv
, &state
);
1586 if (state
.stack
->prev
)
1587 die(_("format: %%(end) atom missing"));
1588 final_buf
= &state
.stack
->output
;
1589 fwrite(final_buf
->buf
, 1, final_buf
->len
, stdout
);
1590 pop_stack_element(&state
.stack
);
1594 /* If no sorting option is given, use refname to sort as default */
1595 struct ref_sorting
*ref_default_sorting(void)
1597 static const char cstr_name
[] = "refname";
1599 struct ref_sorting
*sorting
= xcalloc(1, sizeof(*sorting
));
1601 sorting
->next
= NULL
;
1602 sorting
->atom
= parse_ref_filter_atom(cstr_name
, cstr_name
+ strlen(cstr_name
));
1606 int parse_opt_ref_sorting(const struct option
*opt
, const char *arg
, int unset
)
1608 struct ref_sorting
**sorting_tail
= opt
->value
;
1609 struct ref_sorting
*s
;
1612 if (!arg
) /* should --no-sort void the list ? */
1615 s
= xcalloc(1, sizeof(*s
));
1616 s
->next
= *sorting_tail
;
1623 if (skip_prefix(arg
, "version:", &arg
) ||
1624 skip_prefix(arg
, "v:", &arg
))
1627 s
->atom
= parse_ref_filter_atom(arg
, arg
+len
);
1631 int parse_opt_merge_filter(const struct option
*opt
, const char *arg
, int unset
)
1633 struct ref_filter
*rf
= opt
->value
;
1634 unsigned char sha1
[20];
1636 rf
->merge
= starts_with(opt
->long_name
, "no")
1637 ? REF_FILTER_MERGED_OMIT
1638 : REF_FILTER_MERGED_INCLUDE
;
1640 if (get_sha1(arg
, sha1
))
1641 die(_("malformed object name %s"), arg
);
1643 rf
->merge_commit
= lookup_commit_reference_gently(sha1
, 0);
1644 if (!rf
->merge_commit
)
1645 return opterror(opt
, "must point to a commit", 0);