interpret-trailers: factor trailer rewriting

Extract the trailer rewriting logic into a helper that appends to an output strbuf. Update interpret_trailers() to handle file I/O only: read input once, call the helper, and write the buffered result. This separation makes it easier to move the helper into trailer.c in the next commit. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Li Chen <me@linux.beauty> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Li Chen committed Mar 6, 2026 at 14:53 UTC 8efabc9e64f9c93a764b0d38981bf99d75f502bd
1 file changed +30 -23
builtin/interpret-trailers.c
+30 -23
@@ -136,32 +136,21 @@ static void read_input_file(struct strbuf *sb, const char *file)
136 strbuf_complete_line(sb);
137 }
138
139 -static void interpret_trailers(const struct process_trailer_options *opts,
140 - struct list_head *new_trailer_head,
141 - const char *file)
139 +static void process_trailers(const struct process_trailer_options *opts,
140 + struct list_head *new_trailer_head,
141 + struct strbuf *input, struct strbuf *out)
142 {
143 LIST_HEAD(head);
144 - struct strbuf sb = STRBUF_INIT;
145 - struct strbuf trailer_block_sb = STRBUF_INIT;
144 struct trailer_block *trailer_block;
147 - FILE *outfile = stdout;
145
149 - trailer_config_init();
150 -
151 - read_input_file(&sb, file);
152 -
153 - if (opts->in_place)
154 - outfile = create_in_place_tempfile(file);
155 -
156 - trailer_block = parse_trailers(opts, sb.buf, &head);
146 + trailer_block = parse_trailers(opts, input->buf, &head);
147
148 /* Print the lines before the trailer block */
149 if (!opts->only_trailers)
160 - fwrite(sb.buf, 1, trailer_block_start(trailer_block), outfile);
150 + strbuf_add(out, input->buf, trailer_block_start(trailer_block));
151
152 if (!opts->only_trailers && !blank_line_before_trailer_block(trailer_block))
163 - fprintf(outfile, "\n");
164 -
153 + strbuf_addch(out, '\n');
154
155 if (!opts->only_input) {
156 LIST_HEAD(config_head);
@@ -173,22 +162,40 @@ static void interpret_trailers(const struct process_trailer_options *opts,
162 }
163
164 /* Print trailer block. */
176 - format_trailers(opts, &head, &trailer_block_sb);
165 + format_trailers(opts, &head, out);
166 free_trailers(&head);
178 - fwrite(trailer_block_sb.buf, 1, trailer_block_sb.len, outfile);
179 - strbuf_release(&trailer_block_sb);
167
168 /* Print the lines after the trailer block as is. */
169 if (!opts->only_trailers)
183 - fwrite(sb.buf + trailer_block_end(trailer_block), 1,
184 - sb.len - trailer_block_end(trailer_block), outfile);
170 + strbuf_add(out, input->buf + trailer_block_end(trailer_block),
171 + input->len - trailer_block_end(trailer_block));
172 trailer_block_release(trailer_block);
173 +}
174 +
175 +static void interpret_trailers(const struct process_trailer_options *opts,
176 + struct list_head *new_trailer_head,
177 + const char *file)
178 +{
179 + struct strbuf input = STRBUF_INIT;
180 + struct strbuf out = STRBUF_INIT;
181 + FILE *outfile = stdout;
182 +
183 + trailer_config_init();
184 +
185 + read_input_file(&input, file);
186 +
187 + if (opts->in_place)
188 + outfile = create_in_place_tempfile(file);
189 +
190 + process_trailers(opts, new_trailer_head, &input, &out);
191
192 + strbuf_write(&out, outfile);
193 if (opts->in_place)
194 if (rename_tempfile(&trailers_tempfile, file))
195 die_errno(_("could not rename temporary file to %s"), file);
196
191 - strbuf_release(&sb);
197 + strbuf_release(&input);
198 + strbuf_release(&out);
199 }
200
201 int cmd_interpret_trailers(int argc,