Skip to content

Commit 2111aa7

Browse files
lukpuehgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94240b9 commit 2111aa7

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

ref-filter.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,7 +1329,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
13291329
return ref;
13301330
}
13311331

1332-
static int filter_ref_kind(struct ref_filter *filter, const char *refname)
1332+
static int ref_kind_from_refname(const char *refname)
13331333
{
13341334
unsigned int i;
13351335

@@ -1342,11 +1342,7 @@ static int filter_ref_kind(struct ref_filter *filter, const char *refname)
13421342
{ "refs/tags/", FILTER_REFS_TAGS}
13431343
};
13441344

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"))
13501346
return FILTER_REFS_DETACHED_HEAD;
13511347

13521348
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)
13571353
return FILTER_REFS_OTHERS;
13581354
}
13591355

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+
13601365
/*
13611366
* A call-back given to for_each_ref(). Filter refs and keep them for
13621367
* later object processing.
@@ -1637,6 +1642,16 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
16371642
putchar('\n');
16381643
}
16391644

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+
16401655
/* If no sorting option is given, use refname to sort as default */
16411656
struct ref_sorting *ref_default_sorting(void)
16421657
{

ref-filter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,11 @@ struct ref_sorting *ref_default_sorting(void);
107107
/* Function to parse --merged and --no-merged options */
108108
int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
109109

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+
110117
#endif /* REF_FILTER_H */

0 commit comments

Comments
 (0)