ref-filter: start adding strbufs with errors

This is a first step in removing die() calls from ref-filter formatting logic, so that it could be used by other commands that do not want to die during formatting process. die() calls related to bugs in code will not be touched in this patch. Everything would be the same for show_ref_array_item() users. But, if you want to deal with errors by your own, you could invoke format_ref_array_item(). It means that you need to print everything (the result and errors) on your side. This commit changes signature of format_ref_array_item() by adding return value and strbuf parameter for errors, and adjusts its callers. While at it, reduce the scope of the out-variable. Signed-off-by: Olga Telezhnaia <olyatelezhnaya@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Olga Telezhnaya committed Mar 29, 2018 at 12:49 UTC 3019eca918d168d5f0cb773c8853222745e1b37a
3 files changed +21 -10
builtin/branch.c
+5 -2
@@ -391,7 +391,6 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
391 struct ref_array array;
392 int maxwidth = 0;
393 const char *remote_prefix = "";
394 - struct strbuf out = STRBUF_INIT;
394 char *to_free = NULL;
395
396 /*
@@ -419,7 +418,10 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
418 ref_array_sort(sorting, &array);
419
420 for (i = 0; i < array.nr; i++) {
422 - format_ref_array_item(array.items[i], format, &out);
421 + struct strbuf out = STRBUF_INIT;
422 + struct strbuf err = STRBUF_INIT;
423 + if (format_ref_array_item(array.items[i], format, &out, &err))
424 + die("%s", err.buf);
425 if (column_active(colopts)) {
426 assert(!filter->verbose && "--column and --verbose are incompatible");
427 /* format to a string_list to let print_columns() do its job */
@@ -428,6 +430,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
430 fwrite(out.buf, 1, out.len, stdout);
431 putchar('\n');
432 }
433 + strbuf_release(&err);
434 strbuf_release(&out);
435 }
436
ref-filter.c
+12 -5
@@ -2131,9 +2131,10 @@ static void append_literal(const char *cp, const char *ep, struct ref_formatting
2131 }
2132 }
2133
2134 -void format_ref_array_item(struct ref_array_item *info,
2134 +int format_ref_array_item(struct ref_array_item *info,
2135 const struct ref_format *format,
2136 - struct strbuf *final_buf)
2136 + struct strbuf *final_buf,
2137 + struct strbuf *error_buf)
2138 {
2139 const char *cp, *sp, *ep;
2140 struct ref_formatting_state state = REF_FORMATTING_STATE_INIT;
@@ -2161,19 +2162,25 @@ void format_ref_array_item(struct ref_array_item *info,
2162 resetv.s = GIT_COLOR_RESET;
2163 append_atom(&resetv, &state);
2164 }
2164 - if (state.stack->prev)
2165 - die(_("format: %%(end) atom missing"));
2165 + if (state.stack->prev) {
2166 + pop_stack_element(&state.stack);
2167 + return strbuf_addf_ret(error_buf, -1, _("format: %%(end) atom missing"));
2168 + }
2169 strbuf_addbuf(final_buf, &state.stack->output);
2170 pop_stack_element(&state.stack);
2171 + return 0;
2172 }
2173
2174 void show_ref_array_item(struct ref_array_item *info,
2175 const struct ref_format *format)
2176 {
2177 struct strbuf final_buf = STRBUF_INIT;
2178 + struct strbuf error_buf = STRBUF_INIT;
2179
2175 - format_ref_array_item(info, format, &final_buf);
2180 + if (format_ref_array_item(info, format, &final_buf, &error_buf))
2181 + die("%s", error_buf.buf);
2182 fwrite(final_buf.buf, 1, final_buf.len, stdout);
2183 + strbuf_release(&error_buf);
2184 strbuf_release(&final_buf);
2185 putchar('\n');
2186 }
ref-filter.h
+4 -3
@@ -110,9 +110,10 @@ int verify_ref_format(struct ref_format *format);
110 /* Sort the given ref_array as per the ref_sorting provided */
111 void ref_array_sort(struct ref_sorting *sort, struct ref_array *array);
112 /* Based on the given format and quote_style, fill the strbuf */
113 -void format_ref_array_item(struct ref_array_item *info,
114 - const struct ref_format *format,
115 - struct strbuf *final_buf);
113 +int format_ref_array_item(struct ref_array_item *info,
114 + const struct ref_format *format,
115 + struct strbuf *final_buf,
116 + struct strbuf *error_buf);
117 /* Print the ref using the given format and quote_style */
118 void show_ref_array_item(struct ref_array_item *info, const struct ref_format *format);
119 /* Parse a single sort specifier and add it to the list */