Skip to content

Commit e7574b0

Browse files
vdyegitster
authored andcommitted
ref-filter.h: add functions for filter/format & format-only
Add two new public methods to 'ref-filter.h': * 'print_formatted_ref_array()' which, given a format specification & array of ref items, formats and prints the items to stdout. * 'filter_and_format_refs()' which combines 'filter_refs()', 'ref_array_sort()', and 'print_formatted_ref_array()' into a single function. This consolidates much of the code used to filter and format refs in 'builtin/for-each-ref.c', 'builtin/tag.c', and 'builtin/branch.c', reducing duplication and simplifying the future changes needed to optimize the filter & format process. Signed-off-by: Victoria Dye <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d6e5c5 commit e7574b0

File tree

5 files changed

+68
-64
lines changed

5 files changed

+68
-64
lines changed

builtin/branch.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,6 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
437437
{
438438
int i;
439439
struct ref_array array;
440-
struct strbuf out = STRBUF_INIT;
441-
struct strbuf err = STRBUF_INIT;
442440
int maxwidth = 0;
443441
const char *remote_prefix = "";
444442
char *to_free = NULL;
@@ -468,24 +466,27 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
468466
filter_ahead_behind(the_repository, format, &array);
469467
ref_array_sort(sorting, &array);
470468

471-
for (i = 0; i < array.nr; i++) {
472-
strbuf_reset(&err);
473-
strbuf_reset(&out);
474-
if (format_ref_array_item(array.items[i], format, &out, &err))
475-
die("%s", err.buf);
476-
if (column_active(colopts)) {
477-
assert(!filter->verbose && "--column and --verbose are incompatible");
478-
/* format to a string_list to let print_columns() do its job */
469+
if (column_active(colopts)) {
470+
struct strbuf out = STRBUF_INIT, err = STRBUF_INIT;
471+
472+
assert(!filter->verbose && "--column and --verbose are incompatible");
473+
474+
for (i = 0; i < array.nr; i++) {
475+
strbuf_reset(&err);
476+
strbuf_reset(&out);
477+
if (format_ref_array_item(array.items[i], format, &out, &err))
478+
die("%s", err.buf);
479+
480+
/* format to a string_list to let print_columns() do its job */
479481
string_list_append(output, out.buf);
480-
} else {
481-
fwrite(out.buf, 1, out.len, stdout);
482-
if (out.len || !format->array_opts.omit_empty)
483-
putchar('\n');
484482
}
483+
484+
strbuf_release(&err);
485+
strbuf_release(&out);
486+
} else {
487+
print_formatted_ref_array(&array, format);
485488
}
486489

487-
strbuf_release(&err);
488-
strbuf_release(&out);
489490
ref_array_clear(&array);
490491
free(to_free);
491492
}

builtin/for-each-ref.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@ static char const * const for_each_ref_usage[] = {
1919

2020
int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
2121
{
22-
int i, total;
2322
struct ref_sorting *sorting;
2423
struct string_list sorting_options = STRING_LIST_INIT_DUP;
2524
int icase = 0;
26-
struct ref_array array;
2725
struct ref_filter filter = REF_FILTER_INIT;
2826
struct ref_format format = REF_FORMAT_INIT;
29-
struct strbuf output = STRBUF_INIT;
30-
struct strbuf err = STRBUF_INIT;
3127
int from_stdin = 0;
3228
struct strvec vec = STRVEC_INIT;
3329

@@ -61,8 +57,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
6157
OPT_END(),
6258
};
6359

64-
memset(&array, 0, sizeof(array));
65-
6660
format.format = "%(objectname) %(objecttype)\t%(refname)";
6761

6862
git_config(git_default_config, NULL);
@@ -104,27 +98,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
10498
}
10599

106100
filter.match_as_path = 1;
107-
filter_refs(&array, &filter, FILTER_REFS_ALL);
108-
filter_ahead_behind(the_repository, &format, &array);
109-
110-
ref_array_sort(sorting, &array);
111-
112-
total = format.array_opts.max_count;
113-
if (!total || array.nr < total)
114-
total = array.nr;
115-
for (i = 0; i < total; i++) {
116-
strbuf_reset(&err);
117-
strbuf_reset(&output);
118-
if (format_ref_array_item(array.items[i], &format, &output, &err))
119-
die("%s", err.buf);
120-
fwrite(output.buf, 1, output.len, stdout);
121-
if (output.len || !format.array_opts.omit_empty)
122-
putchar('\n');
123-
}
101+
filter_and_format_refs(&filter, FILTER_REFS_ALL, sorting, &format);
124102

125-
strbuf_release(&err);
126-
strbuf_release(&output);
127-
ref_array_clear(&array);
128103
ref_filter_clear(&filter);
129104
ref_sorting_release(sorting);
130105
strvec_clear(&vec);

builtin/tag.c

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,7 @@ static int config_sign_tag = -1; /* unspecified */
4848
static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
4949
struct ref_format *format)
5050
{
51-
struct ref_array array;
52-
struct strbuf output = STRBUF_INIT;
53-
struct strbuf err = STRBUF_INIT;
5451
char *to_free = NULL;
55-
int i;
56-
57-
memset(&array, 0, sizeof(array));
5852

5953
if (filter->lines == -1)
6054
filter->lines = 0;
@@ -72,23 +66,8 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting,
7266
if (verify_ref_format(format))
7367
die(_("unable to parse format string"));
7468
filter->with_commit_tag_algo = 1;
75-
filter_refs(&array, filter, FILTER_REFS_TAGS);
76-
filter_ahead_behind(the_repository, format, &array);
77-
ref_array_sort(sorting, &array);
78-
79-
for (i = 0; i < array.nr; i++) {
80-
strbuf_reset(&output);
81-
strbuf_reset(&err);
82-
if (format_ref_array_item(array.items[i], format, &output, &err))
83-
die("%s", err.buf);
84-
fwrite(output.buf, 1, output.len, stdout);
85-
if (output.len || !format->array_opts.omit_empty)
86-
putchar('\n');
87-
}
69+
filter_and_format_refs(filter, FILTER_REFS_TAGS, sorting, format);
8870

89-
strbuf_release(&err);
90-
strbuf_release(&output);
91-
ref_array_clear(&array);
9271
free(to_free);
9372

9473
return 0;

ref-filter.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,6 +2939,18 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
29392939
return ret;
29402940
}
29412941

2942+
void filter_and_format_refs(struct ref_filter *filter, unsigned int type,
2943+
struct ref_sorting *sorting,
2944+
struct ref_format *format)
2945+
{
2946+
struct ref_array array = { 0 };
2947+
filter_refs(&array, filter, type);
2948+
filter_ahead_behind(the_repository, format, &array);
2949+
ref_array_sort(sorting, &array);
2950+
print_formatted_ref_array(&array, format);
2951+
ref_array_clear(&array);
2952+
}
2953+
29422954
static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b)
29432955
{
29442956
if (!(a->kind ^ b->kind))
@@ -3128,6 +3140,29 @@ int format_ref_array_item(struct ref_array_item *info,
31283140
return 0;
31293141
}
31303142

3143+
void print_formatted_ref_array(struct ref_array *array, struct ref_format *format)
3144+
{
3145+
int total;
3146+
struct strbuf output = STRBUF_INIT, err = STRBUF_INIT;
3147+
3148+
total = format->array_opts.max_count;
3149+
if (!total || array->nr < total)
3150+
total = array->nr;
3151+
for (int i = 0; i < total; i++) {
3152+
strbuf_reset(&err);
3153+
strbuf_reset(&output);
3154+
if (format_ref_array_item(array->items[i], format, &output, &err))
3155+
die("%s", err.buf);
3156+
if (output.len || !format->array_opts.omit_empty) {
3157+
fwrite(output.buf, 1, output.len, stdout);
3158+
putchar('\n');
3159+
}
3160+
}
3161+
3162+
strbuf_release(&err);
3163+
strbuf_release(&output);
3164+
}
3165+
31313166
void pretty_print_ref(const char *name, const struct object_id *oid,
31323167
struct ref_format *format)
31333168
{

ref-filter.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ struct ref_format {
137137
* filtered refs in the ref_array structure.
138138
*/
139139
int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int type);
140+
/*
141+
* Filter refs using the given ref_filter and type, sort the contents
142+
* according to the given ref_sorting, format the filtered refs with the
143+
* given ref_format, and print them to stdout.
144+
*/
145+
void filter_and_format_refs(struct ref_filter *filter, unsigned int type,
146+
struct ref_sorting *sorting,
147+
struct ref_format *format);
140148
/* Clear all memory allocated to ref_array */
141149
void ref_array_clear(struct ref_array *array);
142150
/* Used to verify if the given format is correct and to parse out the used atoms */
@@ -161,6 +169,12 @@ char *get_head_description(void);
161169
/* Set up translated strings in the output. */
162170
void setup_ref_filter_porcelain_msg(void);
163171

172+
/*
173+
* Print up to maxcount ref_array elements to stdout using the given
174+
* ref_format.
175+
*/
176+
void print_formatted_ref_array(struct ref_array *array, struct ref_format *format);
177+
164178
/*
165179
* Print a single ref, outside of any ref-filter. Note that the
166180
* name must be a fully qualified refname.

0 commit comments

Comments
 (0)