format_trailers(): use strbuf instead of FILE

This is another preparatory refactor to unify the trailer formatters. Make format_trailers() also write to a strbuf, to align with format_trailers_from_commit() which also does the same. Doing this makes format_trailers() behave similar to format_trailer_info() (which will soon help us replace one with the other). Signed-off-by: Linus Arver <linusa@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Arver committed Mar 1, 2024 at 00:14 UTC bf35e0a018cf6d35834e762ac524754024800ad6
3 files changed +14 -8
builtin/interpret-trailers.c
+5 -1
@@ -140,6 +140,7 @@ static void interpret_trailers(const struct process_trailer_options *opts,
140 {
141 LIST_HEAD(head);
142 struct strbuf sb = STRBUF_INIT;
143 + struct strbuf trailer_block = STRBUF_INIT;
144 struct trailer_info info;
145 FILE *outfile = stdout;
146
@@ -169,8 +170,11 @@ static void interpret_trailers(const struct process_trailer_options *opts,
170 process_trailers_lists(&head, &arg_head);
171 }
172
172 - format_trailers(opts, &head, outfile);
173 + /* Print trailer block. */
174 + format_trailers(opts, &head, &trailer_block);
175 free_trailers(&head);
176 + fwrite(trailer_block.buf, 1, trailer_block.len, outfile);
177 + strbuf_release(&trailer_block);
178
179 /* Print the lines after the trailers as is */
180 if (!opts->only_trailers)
trailer.c
+7 -6
@@ -144,12 +144,12 @@ static char last_non_space_char(const char *s)
144 return '\0';
145 }
146
147 -static void print_tok_val(FILE *outfile, const char *tok, const char *val)
147 +static void print_tok_val(struct strbuf *out, const char *tok, const char *val)
148 {
149 char c;
150
151 if (!tok) {
152 - fprintf(outfile, "%s\n", val);
152 + strbuf_addf(out, "%s\n", val);
153 return;
154 }
155
@@ -157,13 +157,14 @@ static void print_tok_val(FILE *outfile, const char *tok, const char *val)
157 if (!c)
158 return;
159 if (strchr(separators, c))
160 - fprintf(outfile, "%s%s\n", tok, val);
160 + strbuf_addf(out, "%s%s\n", tok, val);
161 else
162 - fprintf(outfile, "%s%c %s\n", tok, separators[0], val);
162 + strbuf_addf(out, "%s%c %s\n", tok, separators[0], val);
163 }
164
165 void format_trailers(const struct process_trailer_options *opts,
166 - struct list_head *trailers, FILE *outfile)
166 + struct list_head *trailers,
167 + struct strbuf *out)
168 {
169 struct list_head *pos;
170 struct trailer_item *item;
@@ -171,7 +172,7 @@ void format_trailers(const struct process_trailer_options *opts,
172 item = list_entry(pos, struct trailer_item, list);
173 if ((!opts->trim_empty || strlen(item->value) > 0) &&
174 (!opts->only_trailers || item->token))
174 - print_tok_val(outfile, item->token, item->value);
175 + print_tok_val(out, item->token, item->value);
176 }
177 }
178
trailer.h
+2 -1
@@ -102,7 +102,8 @@ void trailer_info_release(struct trailer_info *info);
102
103 void trailer_config_init(void);
104 void format_trailers(const struct process_trailer_options *,
105 - struct list_head *trailers, FILE *outfile);
105 + struct list_head *trailers,
106 + struct strbuf *out);
107 void free_trailers(struct list_head *);
108
109 /*