diff options
author | Victoria Dye <vdye@github.com> | 2023-11-14 19:53:52 (GMT) |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-11-16 05:02:59 (GMT) |
commit | e7574b0c6b1e0cf2de9e000766a13d23fd9516e6 (patch) | |
tree | eea6d4b3510f6f7d3d60215d67ddcba89a29aa14 /ref-filter.c | |
parent | 6d6e5c53b0c3d5566680ee8786d40405db917917 (diff) | |
download | git-e7574b0c6b1e0cf2de9e000766a13d23fd9516e6.zip git-e7574b0c6b1e0cf2de9e000766a13d23fd9516e6.tar.gz git-e7574b0c6b1e0cf2de9e000766a13d23fd9516e6.tar.bz2 |
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 <vdye@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'ref-filter.c')
-rw-r--r-- | ref-filter.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/ref-filter.c b/ref-filter.c index 3de4cef..f062bb4 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -2939,6 +2939,18 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int return ret; } +void filter_and_format_refs(struct ref_filter *filter, unsigned int type, + struct ref_sorting *sorting, + struct ref_format *format) +{ + struct ref_array array = { 0 }; + filter_refs(&array, filter, type); + filter_ahead_behind(the_repository, format, &array); + ref_array_sort(sorting, &array); + print_formatted_ref_array(&array, format); + ref_array_clear(&array); +} + static int compare_detached_head(struct ref_array_item *a, struct ref_array_item *b) { if (!(a->kind ^ b->kind)) @@ -3128,6 +3140,29 @@ int format_ref_array_item(struct ref_array_item *info, return 0; } +void print_formatted_ref_array(struct ref_array *array, struct ref_format *format) +{ + int total; + struct strbuf output = STRBUF_INIT, err = STRBUF_INIT; + + total = format->array_opts.max_count; + if (!total || array->nr < total) + total = array->nr; + for (int i = 0; i < total; i++) { + strbuf_reset(&err); + strbuf_reset(&output); + if (format_ref_array_item(array->items[i], format, &output, &err)) + die("%s", err.buf); + if (output.len || !format->array_opts.omit_empty) { + fwrite(output.buf, 1, output.len, stdout); + putchar('\n'); + } + } + + strbuf_release(&err); + strbuf_release(&output); +} + void pretty_print_ref(const char *name, const struct object_id *oid, struct ref_format *format) { |