ref-filter: add function to print single ref_array_item

ref-filter functions are useful for printing git object information using a format specifier. However, some other modules may not want to use this functionality on a ref-array but only print a single item. Expose a pretty_print_ref function to create, pretty print and free individual ref-items. Signed-off-by: Lukas Puehringer <luk.puehringer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lukas Puehringer committed Jan 17, 2017 at 18:37 UTC 2111aa794bb420f767895682232b494d3461ab31
2 files changed +28 -6
ref-filter.c
+21 -6
@@ -1329,7 +1329,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
1329 return ref;
1330 }
1331
1332 -static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1332 +static int ref_kind_from_refname(const char *refname)
1333 {
1334 unsigned int i;
1335
@@ -1342,11 +1342,7 @@ static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1342 { "refs/tags/", FILTER_REFS_TAGS}
1343 };
1344
1345 - if (filter->kind == FILTER_REFS_BRANCHES ||
1346 - filter->kind == FILTER_REFS_REMOTES ||
1347 - filter->kind == FILTER_REFS_TAGS)
1348 - return filter->kind;
1349 - else if (!strcmp(refname, "HEAD"))
1345 + if (!strcmp(refname, "HEAD"))
1346 return FILTER_REFS_DETACHED_HEAD;
1347
1348 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
@@ -1357,6 +1353,15 @@ static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1353 return FILTER_REFS_OTHERS;
1354 }
1355
1356 +static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1357 +{
1358 + if (filter->kind == FILTER_REFS_BRANCHES ||
1359 + filter->kind == FILTER_REFS_REMOTES ||
1360 + filter->kind == FILTER_REFS_TAGS)
1361 + return filter->kind;
1362 + return ref_kind_from_refname(refname);
1363 +}
1364 +
1365 /*
1366 * A call-back given to for_each_ref(). Filter refs and keep them for
1367 * later object processing.
@@ -1637,6 +1642,16 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
1642 putchar('\n');
1643 }
1644
1645 +void pretty_print_ref(const char *name, const unsigned char *sha1,
1646 + const char *format)
1647 +{
1648 + struct ref_array_item *ref_item;
1649 + ref_item = new_ref_array_item(name, sha1, 0);
1650 + ref_item->kind = ref_kind_from_refname(name);
1651 + show_ref_array_item(ref_item, format, 0);
1652 + free_array_item(ref_item);
1653 +}
1654 +
1655 /* If no sorting option is given, use refname to sort as default */
1656 struct ref_sorting *ref_default_sorting(void)
1657 {
ref-filter.h
+7
@@ -107,4 +107,11 @@ struct ref_sorting *ref_default_sorting(void);
107 /* Function to parse --merged and --no-merged options */
108 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
109
110 +/*
111 + * Print a single ref, outside of any ref-filter. Note that the
112 + * name must be a fully qualified refname.
113 + */
114 +void pretty_print_ref(const char *name, const unsigned char *sha1,
115 + const char *format);
116 +
117 #endif /* REF_FILTER_H */