diff options
| author | Victoria Dye <vdye@github.com> | 2023-11-14 19:53:52 +0000 |
|---|---|---|
| committer | Junio C Hamano <gitster@pobox.com> | 2023-11-16 14:02:59 +0900 |
| commit | e7574b0c6b1e0cf2de9e000766a13d23fd9516e6 (patch) | |
| tree | eea6d4b3510f6f7d3d60215d67ddcba89a29aa14 /builtin/branch.c | |
| parent | ref-filter.h: move contains caches into filter (diff) | |
| download | git-e7574b0c6b1e0cf2de9e000766a13d23fd9516e6.tar.gz git-e7574b0c6b1e0cf2de9e000766a13d23fd9516e6.zip | |
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 'builtin/branch.c')
| -rw-r--r-- | builtin/branch.c | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/builtin/branch.c b/builtin/branch.c index 49198b548e..40da31e640 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -437,8 +437,6 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin { int i; struct ref_array array; - struct strbuf out = STRBUF_INIT; - struct strbuf err = STRBUF_INIT; int maxwidth = 0; const char *remote_prefix = ""; char *to_free = NULL; @@ -468,24 +466,27 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin filter_ahead_behind(the_repository, format, &array); ref_array_sort(sorting, &array); - for (i = 0; i < array.nr; i++) { - strbuf_reset(&err); - strbuf_reset(&out); - if (format_ref_array_item(array.items[i], format, &out, &err)) - die("%s", err.buf); - if (column_active(colopts)) { - assert(!filter->verbose && "--column and --verbose are incompatible"); - /* format to a string_list to let print_columns() do its job */ + if (column_active(colopts)) { + struct strbuf out = STRBUF_INIT, err = STRBUF_INIT; + + assert(!filter->verbose && "--column and --verbose are incompatible"); + + for (i = 0; i < array.nr; i++) { + strbuf_reset(&err); + strbuf_reset(&out); + if (format_ref_array_item(array.items[i], format, &out, &err)) + die("%s", err.buf); + + /* format to a string_list to let print_columns() do its job */ string_list_append(output, out.buf); - } else { - fwrite(out.buf, 1, out.len, stdout); - if (out.len || !format->array_opts.omit_empty) - putchar('\n'); } + + strbuf_release(&err); + strbuf_release(&out); + } else { + print_formatted_ref_array(&array, format); } - strbuf_release(&err); - strbuf_release(&out); ref_array_clear(&array); free(to_free); } |
